ALSA: HDA VIA: Modify vt1708_set_pinconfig_connect function.
[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 int via_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1026 struct hda_codec *codec,
1027 unsigned int stream_tag,
1028 unsigned int format,
1029 struct snd_pcm_substream *substream)
1030 {
1031 struct via_spec *spec = codec->spec;
1032 vt1708_start_hp_work(spec);
1033 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
1034 stream_tag, format, substream);
1035 }
1036
1037 static int via_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1038 struct hda_codec *codec,
1039 struct snd_pcm_substream *substream)
1040 {
1041 struct via_spec *spec = codec->spec;
1042 vt1708_stop_hp_work(spec);
1043 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
1044 }
1045
1046
1047 static void playback_multi_pcm_prep_0(struct hda_codec *codec,
1048 unsigned int stream_tag,
1049 unsigned int format,
1050 struct snd_pcm_substream *substream)
1051 {
1052 struct via_spec *spec = codec->spec;
1053 struct hda_multi_out *mout = &spec->multiout;
1054 hda_nid_t *nids = mout->dac_nids;
1055 int chs = substream->runtime->channels;
1056 int i;
1057
1058 mutex_lock(&codec->spdif_mutex);
1059 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1060 if (chs == 2 &&
1061 snd_hda_is_supported_format(codec, mout->dig_out_nid,
1062 format) &&
1063 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1064 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1065 /* turn off SPDIF once; otherwise the IEC958 bits won't
1066 * be updated */
1067 if (codec->spdif_ctls & AC_DIG1_ENABLE)
1068 snd_hda_codec_write(codec, mout->dig_out_nid, 0,
1069 AC_VERB_SET_DIGI_CONVERT_1,
1070 codec->spdif_ctls &
1071 ~AC_DIG1_ENABLE & 0xff);
1072 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1073 stream_tag, 0, format);
1074 /* turn on again (if needed) */
1075 if (codec->spdif_ctls & AC_DIG1_ENABLE)
1076 snd_hda_codec_write(codec, mout->dig_out_nid, 0,
1077 AC_VERB_SET_DIGI_CONVERT_1,
1078 codec->spdif_ctls & 0xff);
1079 } else {
1080 mout->dig_out_used = 0;
1081 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1082 0, 0, 0);
1083 }
1084 }
1085 mutex_unlock(&codec->spdif_mutex);
1086
1087 /* front */
1088 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
1089 0, format);
1090
1091 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
1092 !spec->hp_independent_mode)
1093 /* headphone out will just decode front left/right (stereo) */
1094 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
1095 0, format);
1096
1097 /* extra outputs copied from front */
1098 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1099 if (mout->extra_out_nid[i])
1100 snd_hda_codec_setup_stream(codec,
1101 mout->extra_out_nid[i],
1102 stream_tag, 0, format);
1103
1104 /* surrounds */
1105 for (i = 1; i < mout->num_dacs; i++) {
1106 if (chs >= (i + 1) * 2) /* independent out */
1107 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
1108 i * 2, format);
1109 else /* copy front */
1110 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
1111 0, format);
1112 }
1113 }
1114
1115 static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1116 struct hda_codec *codec,
1117 unsigned int stream_tag,
1118 unsigned int format,
1119 struct snd_pcm_substream *substream)
1120 {
1121 struct via_spec *spec = codec->spec;
1122 struct hda_multi_out *mout = &spec->multiout;
1123 hda_nid_t *nids = mout->dac_nids;
1124
1125 if (substream->number == 0)
1126 playback_multi_pcm_prep_0(codec, stream_tag, format,
1127 substream);
1128 else {
1129 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
1130 spec->hp_independent_mode)
1131 snd_hda_codec_setup_stream(codec, mout->hp_nid,
1132 stream_tag, 0, format);
1133 }
1134 vt1708_start_hp_work(spec);
1135 return 0;
1136 }
1137
1138 static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1139 struct hda_codec *codec,
1140 struct snd_pcm_substream *substream)
1141 {
1142 struct via_spec *spec = codec->spec;
1143 struct hda_multi_out *mout = &spec->multiout;
1144 hda_nid_t *nids = mout->dac_nids;
1145 int i;
1146
1147 if (substream->number == 0) {
1148 for (i = 0; i < mout->num_dacs; i++)
1149 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1150
1151 if (mout->hp_nid && !spec->hp_independent_mode)
1152 snd_hda_codec_setup_stream(codec, mout->hp_nid,
1153 0, 0, 0);
1154
1155 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1156 if (mout->extra_out_nid[i])
1157 snd_hda_codec_setup_stream(codec,
1158 mout->extra_out_nid[i],
1159 0, 0, 0);
1160 mutex_lock(&codec->spdif_mutex);
1161 if (mout->dig_out_nid &&
1162 mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1163 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1164 0, 0, 0);
1165 mout->dig_out_used = 0;
1166 }
1167 mutex_unlock(&codec->spdif_mutex);
1168 } else {
1169 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
1170 spec->hp_independent_mode)
1171 snd_hda_codec_setup_stream(codec, mout->hp_nid,
1172 0, 0, 0);
1173 }
1174 vt1708_stop_hp_work(spec);
1175 return 0;
1176 }
1177
1178 /*
1179 * Digital out
1180 */
1181 static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1182 struct hda_codec *codec,
1183 struct snd_pcm_substream *substream)
1184 {
1185 struct via_spec *spec = codec->spec;
1186 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1187 }
1188
1189 static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1190 struct hda_codec *codec,
1191 struct snd_pcm_substream *substream)
1192 {
1193 struct via_spec *spec = codec->spec;
1194 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1195 }
1196
1197 static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1198 struct hda_codec *codec,
1199 unsigned int stream_tag,
1200 unsigned int format,
1201 struct snd_pcm_substream *substream)
1202 {
1203 struct via_spec *spec = codec->spec;
1204 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1205 stream_tag, format, substream);
1206 }
1207
1208 static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1209 struct hda_codec *codec,
1210 struct snd_pcm_substream *substream)
1211 {
1212 struct via_spec *spec = codec->spec;
1213 snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
1214 return 0;
1215 }
1216
1217 /*
1218 * Analog capture
1219 */
1220 static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1221 struct hda_codec *codec,
1222 unsigned int stream_tag,
1223 unsigned int format,
1224 struct snd_pcm_substream *substream)
1225 {
1226 struct via_spec *spec = codec->spec;
1227
1228 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1229 stream_tag, 0, format);
1230 return 0;
1231 }
1232
1233 static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1234 struct hda_codec *codec,
1235 struct snd_pcm_substream *substream)
1236 {
1237 struct via_spec *spec = codec->spec;
1238 snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1239 return 0;
1240 }
1241
1242 static struct hda_pcm_stream vt1708_pcm_analog_playback = {
1243 .substreams = 2,
1244 .channels_min = 2,
1245 .channels_max = 8,
1246 .nid = 0x10, /* NID to query formats and rates */
1247 .ops = {
1248 .open = via_playback_pcm_open,
1249 .prepare = via_playback_multi_pcm_prepare,
1250 .cleanup = via_playback_multi_pcm_cleanup
1251 },
1252 };
1253
1254 static struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
1255 .substreams = 1,
1256 .channels_min = 2,
1257 .channels_max = 8,
1258 .nid = 0x10, /* NID to query formats and rates */
1259 /* We got noisy outputs on the right channel on VT1708 when
1260 * 24bit samples are used. Until any workaround is found,
1261 * disable the 24bit format, so far.
1262 */
1263 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1264 .ops = {
1265 .open = via_playback_pcm_open,
1266 .prepare = via_playback_pcm_prepare,
1267 .cleanup = via_playback_pcm_cleanup
1268 },
1269 };
1270
1271 static struct hda_pcm_stream vt1708_pcm_analog_capture = {
1272 .substreams = 2,
1273 .channels_min = 2,
1274 .channels_max = 2,
1275 .nid = 0x15, /* NID to query formats and rates */
1276 .ops = {
1277 .prepare = via_capture_pcm_prepare,
1278 .cleanup = via_capture_pcm_cleanup
1279 },
1280 };
1281
1282 static struct hda_pcm_stream vt1708_pcm_digital_playback = {
1283 .substreams = 1,
1284 .channels_min = 2,
1285 .channels_max = 2,
1286 /* NID is set in via_build_pcms */
1287 .ops = {
1288 .open = via_dig_playback_pcm_open,
1289 .close = via_dig_playback_pcm_close,
1290 .prepare = via_dig_playback_pcm_prepare,
1291 .cleanup = via_dig_playback_pcm_cleanup
1292 },
1293 };
1294
1295 static struct hda_pcm_stream vt1708_pcm_digital_capture = {
1296 .substreams = 1,
1297 .channels_min = 2,
1298 .channels_max = 2,
1299 };
1300
1301 static int via_build_controls(struct hda_codec *codec)
1302 {
1303 struct via_spec *spec = codec->spec;
1304 int err;
1305 int i;
1306
1307 for (i = 0; i < spec->num_mixers; i++) {
1308 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1309 if (err < 0)
1310 return err;
1311 }
1312
1313 if (spec->multiout.dig_out_nid) {
1314 err = snd_hda_create_spdif_out_ctls(codec,
1315 spec->multiout.dig_out_nid);
1316 if (err < 0)
1317 return err;
1318 err = snd_hda_create_spdif_share_sw(codec,
1319 &spec->multiout);
1320 if (err < 0)
1321 return err;
1322 spec->multiout.share_spdif = 1;
1323 }
1324 if (spec->dig_in_nid) {
1325 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1326 if (err < 0)
1327 return err;
1328 }
1329
1330 /* init power states */
1331 set_jack_power_state(codec);
1332 analog_low_current_mode(codec, 1);
1333
1334 via_free_kctls(codec); /* no longer needed */
1335 return 0;
1336 }
1337
1338 static int via_build_pcms(struct hda_codec *codec)
1339 {
1340 struct via_spec *spec = codec->spec;
1341 struct hda_pcm *info = spec->pcm_rec;
1342
1343 codec->num_pcms = 1;
1344 codec->pcm_info = info;
1345
1346 info->name = spec->stream_name_analog;
1347 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_analog_playback);
1348 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
1349 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
1350 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1351
1352 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1353 spec->multiout.max_channels;
1354
1355 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1356 codec->num_pcms++;
1357 info++;
1358 info->name = spec->stream_name_digital;
1359 info->pcm_type = HDA_PCM_TYPE_SPDIF;
1360 if (spec->multiout.dig_out_nid) {
1361 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1362 *(spec->stream_digital_playback);
1363 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1364 spec->multiout.dig_out_nid;
1365 }
1366 if (spec->dig_in_nid) {
1367 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1368 *(spec->stream_digital_capture);
1369 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1370 spec->dig_in_nid;
1371 }
1372 }
1373
1374 return 0;
1375 }
1376
1377 static void via_free(struct hda_codec *codec)
1378 {
1379 struct via_spec *spec = codec->spec;
1380
1381 if (!spec)
1382 return;
1383
1384 via_free_kctls(codec);
1385 vt1708_stop_hp_work(spec);
1386 kfree(codec->spec);
1387 }
1388
1389 /* mute internal speaker if HP is plugged */
1390 static void via_hp_automute(struct hda_codec *codec)
1391 {
1392 unsigned int present = 0;
1393 struct via_spec *spec = codec->spec;
1394
1395 present = snd_hda_codec_read(codec, spec->autocfg.hp_pins[0], 0,
1396 AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1397
1398 if (!spec->hp_independent_mode) {
1399 struct snd_ctl_elem_id id;
1400 /* auto mute */
1401 snd_hda_codec_amp_stereo(
1402 codec, spec->autocfg.line_out_pins[0], HDA_OUTPUT, 0,
1403 HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
1404 /* notify change */
1405 memset(&id, 0, sizeof(id));
1406 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1407 strcpy(id.name, "Front Playback Switch");
1408 snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
1409 &id);
1410 }
1411 }
1412
1413 static void via_gpio_control(struct hda_codec *codec)
1414 {
1415 unsigned int gpio_data;
1416 unsigned int vol_counter;
1417 unsigned int vol;
1418 unsigned int master_vol;
1419
1420 struct via_spec *spec = codec->spec;
1421
1422 gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
1423 AC_VERB_GET_GPIO_DATA, 0) & 0x03;
1424
1425 vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
1426 0xF84, 0) & 0x3F0000) >> 16;
1427
1428 vol = vol_counter & 0x1F;
1429 master_vol = snd_hda_codec_read(codec, 0x1A, 0,
1430 AC_VERB_GET_AMP_GAIN_MUTE,
1431 AC_AMP_GET_INPUT);
1432
1433 if (gpio_data == 0x02) {
1434 /* unmute line out */
1435 snd_hda_codec_amp_stereo(codec, spec->autocfg.line_out_pins[0],
1436 HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
1437
1438 if (vol_counter & 0x20) {
1439 /* decrease volume */
1440 if (vol > master_vol)
1441 vol = master_vol;
1442 snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
1443 0, HDA_AMP_VOLMASK,
1444 master_vol-vol);
1445 } else {
1446 /* increase volume */
1447 snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
1448 HDA_AMP_VOLMASK,
1449 ((master_vol+vol) > 0x2A) ? 0x2A :
1450 (master_vol+vol));
1451 }
1452 } else if (!(gpio_data & 0x02)) {
1453 /* mute line out */
1454 snd_hda_codec_amp_stereo(codec,
1455 spec->autocfg.line_out_pins[0],
1456 HDA_OUTPUT, 0, HDA_AMP_MUTE,
1457 HDA_AMP_MUTE);
1458 }
1459 }
1460
1461 /* unsolicited event for jack sensing */
1462 static void via_unsol_event(struct hda_codec *codec,
1463 unsigned int res)
1464 {
1465 res >>= 26;
1466 if (res & VIA_HP_EVENT)
1467 via_hp_automute(codec);
1468 if (res & VIA_GPIO_EVENT)
1469 via_gpio_control(codec);
1470 if (res & VIA_JACK_EVENT)
1471 set_jack_power_state(codec);
1472 }
1473
1474 static int via_init(struct hda_codec *codec)
1475 {
1476 struct via_spec *spec = codec->spec;
1477 int i;
1478 for (i = 0; i < spec->num_iverbs; i++)
1479 snd_hda_sequence_write(codec, spec->init_verbs[i]);
1480
1481 spec->codec_type = get_codec_type(codec);
1482 if (spec->codec_type == VT1708BCE)
1483 spec->codec_type = VT1708S; /* VT1708BCE & VT1708S are almost
1484 same */
1485 /* Lydia Add for EAPD enable */
1486 if (!spec->dig_in_nid) { /* No Digital In connection */
1487 if (spec->dig_in_pin) {
1488 snd_hda_codec_write(codec, spec->dig_in_pin, 0,
1489 AC_VERB_SET_PIN_WIDGET_CONTROL,
1490 PIN_OUT);
1491 snd_hda_codec_write(codec, spec->dig_in_pin, 0,
1492 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
1493 }
1494 } else /* enable SPDIF-input pin */
1495 snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
1496 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
1497
1498 /* assign slave outs */
1499 if (spec->slave_dig_outs[0])
1500 codec->slave_dig_outs = spec->slave_dig_outs;
1501
1502 return 0;
1503 }
1504
1505 #ifdef SND_HDA_NEEDS_RESUME
1506 static int via_suspend(struct hda_codec *codec, pm_message_t state)
1507 {
1508 struct via_spec *spec = codec->spec;
1509 vt1708_stop_hp_work(spec);
1510 return 0;
1511 }
1512 #endif
1513
1514 #ifdef CONFIG_SND_HDA_POWER_SAVE
1515 static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1516 {
1517 struct via_spec *spec = codec->spec;
1518 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1519 }
1520 #endif
1521
1522 /*
1523 */
1524 static struct hda_codec_ops via_patch_ops = {
1525 .build_controls = via_build_controls,
1526 .build_pcms = via_build_pcms,
1527 .init = via_init,
1528 .free = via_free,
1529 #ifdef SND_HDA_NEEDS_RESUME
1530 .suspend = via_suspend,
1531 #endif
1532 #ifdef CONFIG_SND_HDA_POWER_SAVE
1533 .check_power_status = via_check_power_status,
1534 #endif
1535 };
1536
1537 /* fill in the dac_nids table from the parsed pin configuration */
1538 static int vt1708_auto_fill_dac_nids(struct via_spec *spec,
1539 const struct auto_pin_cfg *cfg)
1540 {
1541 int i;
1542 hda_nid_t nid;
1543
1544 spec->multiout.num_dacs = cfg->line_outs;
1545
1546 spec->multiout.dac_nids = spec->private_dac_nids;
1547
1548 for(i = 0; i < 4; i++) {
1549 nid = cfg->line_out_pins[i];
1550 if (nid) {
1551 /* config dac list */
1552 switch (i) {
1553 case AUTO_SEQ_FRONT:
1554 spec->multiout.dac_nids[i] = 0x10;
1555 break;
1556 case AUTO_SEQ_CENLFE:
1557 spec->multiout.dac_nids[i] = 0x12;
1558 break;
1559 case AUTO_SEQ_SURROUND:
1560 spec->multiout.dac_nids[i] = 0x11;
1561 break;
1562 case AUTO_SEQ_SIDE:
1563 spec->multiout.dac_nids[i] = 0x13;
1564 break;
1565 }
1566 }
1567 }
1568
1569 return 0;
1570 }
1571
1572 /* add playback controls from the parsed DAC table */
1573 static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
1574 const struct auto_pin_cfg *cfg)
1575 {
1576 char name[32];
1577 static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
1578 hda_nid_t nid, nid_vol = 0;
1579 int i, err;
1580
1581 for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
1582 nid = cfg->line_out_pins[i];
1583
1584 if (!nid)
1585 continue;
1586
1587 if (i != AUTO_SEQ_FRONT)
1588 nid_vol = 0x18 + i;
1589
1590 if (i == AUTO_SEQ_CENLFE) {
1591 /* Center/LFE */
1592 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1593 "Center Playback Volume",
1594 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1595 HDA_OUTPUT));
1596 if (err < 0)
1597 return err;
1598 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1599 "LFE Playback Volume",
1600 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1601 HDA_OUTPUT));
1602 if (err < 0)
1603 return err;
1604 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1605 "Center Playback Switch",
1606 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1607 HDA_OUTPUT));
1608 if (err < 0)
1609 return err;
1610 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1611 "LFE Playback Switch",
1612 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1613 HDA_OUTPUT));
1614 if (err < 0)
1615 return err;
1616 } else if (i == AUTO_SEQ_FRONT){
1617 /* add control to mixer index 0 */
1618 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1619 "Master Front Playback Volume",
1620 HDA_COMPOSE_AMP_VAL(0x17, 3, 0,
1621 HDA_INPUT));
1622 if (err < 0)
1623 return err;
1624 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1625 "Master Front Playback Switch",
1626 HDA_COMPOSE_AMP_VAL(0x17, 3, 0,
1627 HDA_INPUT));
1628 if (err < 0)
1629 return err;
1630
1631 /* add control to PW3 */
1632 sprintf(name, "%s Playback Volume", chname[i]);
1633 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1634 HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1635 HDA_OUTPUT));
1636 if (err < 0)
1637 return err;
1638 sprintf(name, "%s Playback Switch", chname[i]);
1639 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1640 HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1641 HDA_OUTPUT));
1642 if (err < 0)
1643 return err;
1644 } else {
1645 sprintf(name, "%s Playback Volume", chname[i]);
1646 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1647 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1648 HDA_OUTPUT));
1649 if (err < 0)
1650 return err;
1651 sprintf(name, "%s Playback Switch", chname[i]);
1652 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1653 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1654 HDA_OUTPUT));
1655 if (err < 0)
1656 return err;
1657 }
1658 }
1659
1660 return 0;
1661 }
1662
1663 static void create_hp_imux(struct via_spec *spec)
1664 {
1665 int i;
1666 struct hda_input_mux *imux = &spec->private_imux[1];
1667 static const char *texts[] = { "OFF", "ON", NULL};
1668
1669 /* for hp mode select */
1670 i = 0;
1671 while (texts[i] != NULL) {
1672 imux->items[imux->num_items].label = texts[i];
1673 imux->items[imux->num_items].index = i;
1674 imux->num_items++;
1675 i++;
1676 }
1677
1678 spec->hp_mux = &spec->private_imux[1];
1679 }
1680
1681 static int vt1708_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
1682 {
1683 int err;
1684
1685 if (!pin)
1686 return 0;
1687
1688 spec->multiout.hp_nid = VT1708_HP_NID; /* AOW3 */
1689 spec->hp_independent_mode_index = 1;
1690
1691 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1692 "Headphone Playback Volume",
1693 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1694 if (err < 0)
1695 return err;
1696 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1697 "Headphone Playback Switch",
1698 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1699 if (err < 0)
1700 return err;
1701
1702 create_hp_imux(spec);
1703
1704 return 0;
1705 }
1706
1707 /* create playback/capture controls for input pins */
1708 static int vt1708_auto_create_analog_input_ctls(struct via_spec *spec,
1709 const struct auto_pin_cfg *cfg)
1710 {
1711 static char *labels[] = {
1712 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
1713 };
1714 struct hda_input_mux *imux = &spec->private_imux[0];
1715 int i, err, idx = 0;
1716
1717 /* for internal loopback recording select */
1718 imux->items[imux->num_items].label = "Stereo Mixer";
1719 imux->items[imux->num_items].index = idx;
1720 imux->num_items++;
1721
1722 for (i = 0; i < AUTO_PIN_LAST; i++) {
1723 if (!cfg->input_pins[i])
1724 continue;
1725
1726 switch (cfg->input_pins[i]) {
1727 case 0x1d: /* Mic */
1728 idx = 2;
1729 break;
1730
1731 case 0x1e: /* Line In */
1732 idx = 3;
1733 break;
1734
1735 case 0x21: /* Front Mic */
1736 idx = 4;
1737 break;
1738
1739 case 0x24: /* CD */
1740 idx = 1;
1741 break;
1742 }
1743 err = via_new_analog_input(spec, labels[i], idx, 0x17);
1744 if (err < 0)
1745 return err;
1746 imux->items[imux->num_items].label = labels[i];
1747 imux->items[imux->num_items].index = idx;
1748 imux->num_items++;
1749 }
1750 return 0;
1751 }
1752
1753 #ifdef CONFIG_SND_HDA_POWER_SAVE
1754 static struct hda_amp_list vt1708_loopbacks[] = {
1755 { 0x17, HDA_INPUT, 1 },
1756 { 0x17, HDA_INPUT, 2 },
1757 { 0x17, HDA_INPUT, 3 },
1758 { 0x17, HDA_INPUT, 4 },
1759 { } /* end */
1760 };
1761 #endif
1762
1763 static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
1764 {
1765 unsigned int def_conf;
1766 unsigned char seqassoc;
1767
1768 def_conf = snd_hda_codec_get_pincfg(codec, nid);
1769 seqassoc = (unsigned char) get_defcfg_association(def_conf);
1770 seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
1771 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
1772 && (seqassoc == 0xf0 || seqassoc == 0xff)) {
1773 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
1774 snd_hda_codec_set_pincfg(codec, nid, def_conf);
1775 }
1776
1777 return;
1778 }
1779
1780 static int vt1708_jack_detectect_get(struct snd_kcontrol *kcontrol,
1781 struct snd_ctl_elem_value *ucontrol)
1782 {
1783 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1784 struct via_spec *spec = codec->spec;
1785
1786 if (spec->codec_type != VT1708)
1787 return 0;
1788 spec->vt1708_jack_detectect =
1789 !((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
1790 ucontrol->value.integer.value[0] = spec->vt1708_jack_detectect;
1791 return 0;
1792 }
1793
1794 static int vt1708_jack_detectect_put(struct snd_kcontrol *kcontrol,
1795 struct snd_ctl_elem_value *ucontrol)
1796 {
1797 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1798 struct via_spec *spec = codec->spec;
1799 int change;
1800
1801 if (spec->codec_type != VT1708)
1802 return 0;
1803 spec->vt1708_jack_detectect = ucontrol->value.integer.value[0];
1804 change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
1805 == !spec->vt1708_jack_detectect;
1806 if (spec->vt1708_jack_detectect) {
1807 mute_aa_path(codec, 1);
1808 notify_aa_path_ctls(codec);
1809 }
1810 return change;
1811 }
1812
1813 static struct snd_kcontrol_new vt1708_jack_detectect[] = {
1814 {
1815 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1816 .name = "Jack Detect",
1817 .count = 1,
1818 .info = snd_ctl_boolean_mono_info,
1819 .get = vt1708_jack_detectect_get,
1820 .put = vt1708_jack_detectect_put,
1821 },
1822 {} /* end */
1823 };
1824
1825 static int vt1708_parse_auto_config(struct hda_codec *codec)
1826 {
1827 struct via_spec *spec = codec->spec;
1828 int err;
1829
1830 /* Add HP and CD pin config connect bit re-config action */
1831 vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
1832 vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
1833
1834 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1835 if (err < 0)
1836 return err;
1837 err = vt1708_auto_fill_dac_nids(spec, &spec->autocfg);
1838 if (err < 0)
1839 return err;
1840 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
1841 return 0; /* can't find valid BIOS pin config */
1842
1843 err = vt1708_auto_create_multi_out_ctls(spec, &spec->autocfg);
1844 if (err < 0)
1845 return err;
1846 err = vt1708_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
1847 if (err < 0)
1848 return err;
1849 err = vt1708_auto_create_analog_input_ctls(spec, &spec->autocfg);
1850 if (err < 0)
1851 return err;
1852 /* add jack detect on/off control */
1853 err = snd_hda_add_new_ctls(codec, vt1708_jack_detectect);
1854 if (err < 0)
1855 return err;
1856
1857 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1858
1859 if (spec->autocfg.dig_outs)
1860 spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
1861 spec->dig_in_pin = VT1708_DIGIN_PIN;
1862 if (spec->autocfg.dig_in_pin)
1863 spec->dig_in_nid = VT1708_DIGIN_NID;
1864
1865 if (spec->kctls.list)
1866 spec->mixers[spec->num_mixers++] = spec->kctls.list;
1867
1868 spec->init_verbs[spec->num_iverbs++] = vt1708_volume_init_verbs;
1869
1870 spec->input_mux = &spec->private_imux[0];
1871
1872 if (spec->hp_mux)
1873 spec->mixers[spec->num_mixers++] = via_hp_mixer;
1874
1875 spec->mixers[spec->num_mixers++] = via_smart51_mixer;
1876 return 1;
1877 }
1878
1879 /* init callback for auto-configuration model -- overriding the default init */
1880 static int via_auto_init(struct hda_codec *codec)
1881 {
1882 via_init(codec);
1883 via_auto_init_multi_out(codec);
1884 via_auto_init_hp_out(codec);
1885 via_auto_init_analog_input(codec);
1886 return 0;
1887 }
1888
1889 static void vt1708_update_hp_jack_state(struct work_struct *work)
1890 {
1891 struct via_spec *spec = container_of(work, struct via_spec,
1892 vt1708_hp_work.work);
1893 if (spec->codec_type != VT1708)
1894 return;
1895 /* if jack state toggled */
1896 if (spec->vt1708_hp_present
1897 != (snd_hda_codec_read(spec->codec, spec->autocfg.hp_pins[0], 0,
1898 AC_VERB_GET_PIN_SENSE, 0) >> 31)) {
1899 spec->vt1708_hp_present ^= 1;
1900 via_hp_automute(spec->codec);
1901 }
1902 vt1708_start_hp_work(spec);
1903 }
1904
1905 static int get_mux_nids(struct hda_codec *codec)
1906 {
1907 struct via_spec *spec = codec->spec;
1908 hda_nid_t nid, conn[8];
1909 unsigned int type;
1910 int i, n;
1911
1912 for (i = 0; i < spec->num_adc_nids; i++) {
1913 nid = spec->adc_nids[i];
1914 while (nid) {
1915 type = get_wcaps_type(get_wcaps(codec, nid));
1916 if (type == AC_WID_PIN)
1917 break;
1918 n = snd_hda_get_connections(codec, nid, conn,
1919 ARRAY_SIZE(conn));
1920 if (n <= 0)
1921 break;
1922 if (n > 1) {
1923 spec->mux_nids[i] = nid;
1924 break;
1925 }
1926 nid = conn[0];
1927 }
1928 }
1929 return 0;
1930 }
1931
1932 static int patch_vt1708(struct hda_codec *codec)
1933 {
1934 struct via_spec *spec;
1935 int err;
1936
1937 /* create a codec specific record */
1938 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1939 if (spec == NULL)
1940 return -ENOMEM;
1941
1942 codec->spec = spec;
1943
1944 /* automatic parse from the BIOS config */
1945 err = vt1708_parse_auto_config(codec);
1946 if (err < 0) {
1947 via_free(codec);
1948 return err;
1949 } else if (!err) {
1950 printk(KERN_INFO "hda_codec: Cannot set up configuration "
1951 "from BIOS. Using genenic mode...\n");
1952 }
1953
1954
1955 spec->stream_name_analog = "VT1708 Analog";
1956 spec->stream_analog_playback = &vt1708_pcm_analog_playback;
1957 /* disable 32bit format on VT1708 */
1958 if (codec->vendor_id == 0x11061708)
1959 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
1960 spec->stream_analog_capture = &vt1708_pcm_analog_capture;
1961
1962 spec->stream_name_digital = "VT1708 Digital";
1963 spec->stream_digital_playback = &vt1708_pcm_digital_playback;
1964 spec->stream_digital_capture = &vt1708_pcm_digital_capture;
1965
1966
1967 if (!spec->adc_nids && spec->input_mux) {
1968 spec->adc_nids = vt1708_adc_nids;
1969 spec->num_adc_nids = ARRAY_SIZE(vt1708_adc_nids);
1970 get_mux_nids(codec);
1971 spec->mixers[spec->num_mixers] = vt1708_capture_mixer;
1972 spec->num_mixers++;
1973 }
1974
1975 codec->patch_ops = via_patch_ops;
1976
1977 codec->patch_ops.init = via_auto_init;
1978 #ifdef CONFIG_SND_HDA_POWER_SAVE
1979 spec->loopback.amplist = vt1708_loopbacks;
1980 #endif
1981 spec->codec = codec;
1982 INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
1983 return 0;
1984 }
1985
1986 /* capture mixer elements */
1987 static struct snd_kcontrol_new vt1709_capture_mixer[] = {
1988 HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x0, HDA_INPUT),
1989 HDA_CODEC_MUTE("Capture Switch", 0x14, 0x0, HDA_INPUT),
1990 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x15, 0x0, HDA_INPUT),
1991 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x15, 0x0, HDA_INPUT),
1992 HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x16, 0x0, HDA_INPUT),
1993 HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x16, 0x0, HDA_INPUT),
1994 {
1995 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1996 /* The multiple "Capture Source" controls confuse alsamixer
1997 * So call somewhat different..
1998 */
1999 /* .name = "Capture Source", */
2000 .name = "Input Source",
2001 .count = 1,
2002 .info = via_mux_enum_info,
2003 .get = via_mux_enum_get,
2004 .put = via_mux_enum_put,
2005 },
2006 { } /* end */
2007 };
2008
2009 static struct hda_verb vt1709_uniwill_init_verbs[] = {
2010 {0x20, AC_VERB_SET_UNSOLICITED_ENABLE,
2011 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
2012 { }
2013 };
2014
2015 /*
2016 * generic initialization of ADC, input mixers and output mixers
2017 */
2018 static struct hda_verb vt1709_10ch_volume_init_verbs[] = {
2019 /*
2020 * Unmute ADC0-2 and set the default input to mic-in
2021 */
2022 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2023 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2024 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2025
2026
2027 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2028 * mixer widget
2029 */
2030 /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2031 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2032 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2033 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2034 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2035 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2036
2037 /*
2038 * Set up output selector (0x1a, 0x1b, 0x29)
2039 */
2040 /* set vol=0 to output mixers */
2041 {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2042 {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2043 {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2044
2045 /*
2046 * Unmute PW3 and PW4
2047 */
2048 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2049 {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2050
2051 /* Set input of PW4 as AOW4 */
2052 {0x20, AC_VERB_SET_CONNECT_SEL, 0x1},
2053 /* PW9 Output enable */
2054 {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2055 { }
2056 };
2057
2058 static struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
2059 .substreams = 1,
2060 .channels_min = 2,
2061 .channels_max = 10,
2062 .nid = 0x10, /* NID to query formats and rates */
2063 .ops = {
2064 .open = via_playback_pcm_open,
2065 .prepare = via_playback_pcm_prepare,
2066 .cleanup = via_playback_pcm_cleanup
2067 },
2068 };
2069
2070 static struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
2071 .substreams = 1,
2072 .channels_min = 2,
2073 .channels_max = 6,
2074 .nid = 0x10, /* NID to query formats and rates */
2075 .ops = {
2076 .open = via_playback_pcm_open,
2077 .prepare = via_playback_pcm_prepare,
2078 .cleanup = via_playback_pcm_cleanup
2079 },
2080 };
2081
2082 static struct hda_pcm_stream vt1709_pcm_analog_capture = {
2083 .substreams = 2,
2084 .channels_min = 2,
2085 .channels_max = 2,
2086 .nid = 0x14, /* NID to query formats and rates */
2087 .ops = {
2088 .prepare = via_capture_pcm_prepare,
2089 .cleanup = via_capture_pcm_cleanup
2090 },
2091 };
2092
2093 static struct hda_pcm_stream vt1709_pcm_digital_playback = {
2094 .substreams = 1,
2095 .channels_min = 2,
2096 .channels_max = 2,
2097 /* NID is set in via_build_pcms */
2098 .ops = {
2099 .open = via_dig_playback_pcm_open,
2100 .close = via_dig_playback_pcm_close
2101 },
2102 };
2103
2104 static struct hda_pcm_stream vt1709_pcm_digital_capture = {
2105 .substreams = 1,
2106 .channels_min = 2,
2107 .channels_max = 2,
2108 };
2109
2110 static int vt1709_auto_fill_dac_nids(struct via_spec *spec,
2111 const struct auto_pin_cfg *cfg)
2112 {
2113 int i;
2114 hda_nid_t nid;
2115
2116 if (cfg->line_outs == 4) /* 10 channels */
2117 spec->multiout.num_dacs = cfg->line_outs+1; /* AOW0~AOW4 */
2118 else if (cfg->line_outs == 3) /* 6 channels */
2119 spec->multiout.num_dacs = cfg->line_outs; /* AOW0~AOW2 */
2120
2121 spec->multiout.dac_nids = spec->private_dac_nids;
2122
2123 if (cfg->line_outs == 4) { /* 10 channels */
2124 for (i = 0; i < cfg->line_outs; i++) {
2125 nid = cfg->line_out_pins[i];
2126 if (nid) {
2127 /* config dac list */
2128 switch (i) {
2129 case AUTO_SEQ_FRONT:
2130 /* AOW0 */
2131 spec->multiout.dac_nids[i] = 0x10;
2132 break;
2133 case AUTO_SEQ_CENLFE:
2134 /* AOW2 */
2135 spec->multiout.dac_nids[i] = 0x12;
2136 break;
2137 case AUTO_SEQ_SURROUND:
2138 /* AOW3 */
2139 spec->multiout.dac_nids[i] = 0x11;
2140 break;
2141 case AUTO_SEQ_SIDE:
2142 /* AOW1 */
2143 spec->multiout.dac_nids[i] = 0x27;
2144 break;
2145 default:
2146 break;
2147 }
2148 }
2149 }
2150 spec->multiout.dac_nids[cfg->line_outs] = 0x28; /* AOW4 */
2151
2152 } else if (cfg->line_outs == 3) { /* 6 channels */
2153 for(i = 0; i < cfg->line_outs; i++) {
2154 nid = cfg->line_out_pins[i];
2155 if (nid) {
2156 /* config dac list */
2157 switch(i) {
2158 case AUTO_SEQ_FRONT:
2159 /* AOW0 */
2160 spec->multiout.dac_nids[i] = 0x10;
2161 break;
2162 case AUTO_SEQ_CENLFE:
2163 /* AOW2 */
2164 spec->multiout.dac_nids[i] = 0x12;
2165 break;
2166 case AUTO_SEQ_SURROUND:
2167 /* AOW1 */
2168 spec->multiout.dac_nids[i] = 0x11;
2169 break;
2170 default:
2171 break;
2172 }
2173 }
2174 }
2175 }
2176
2177 return 0;
2178 }
2179
2180 /* add playback controls from the parsed DAC table */
2181 static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
2182 const struct auto_pin_cfg *cfg)
2183 {
2184 char name[32];
2185 static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
2186 hda_nid_t nid = 0;
2187 int i, err;
2188
2189 for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2190 nid = cfg->line_out_pins[i];
2191
2192 if (!nid)
2193 continue;
2194
2195 if (i == AUTO_SEQ_CENLFE) {
2196 /* Center/LFE */
2197 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2198 "Center Playback Volume",
2199 HDA_COMPOSE_AMP_VAL(0x1b, 1, 0,
2200 HDA_OUTPUT));
2201 if (err < 0)
2202 return err;
2203 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2204 "LFE Playback Volume",
2205 HDA_COMPOSE_AMP_VAL(0x1b, 2, 0,
2206 HDA_OUTPUT));
2207 if (err < 0)
2208 return err;
2209 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2210 "Center Playback Switch",
2211 HDA_COMPOSE_AMP_VAL(0x1b, 1, 0,
2212 HDA_OUTPUT));
2213 if (err < 0)
2214 return err;
2215 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2216 "LFE Playback Switch",
2217 HDA_COMPOSE_AMP_VAL(0x1b, 2, 0,
2218 HDA_OUTPUT));
2219 if (err < 0)
2220 return err;
2221 } else if (i == AUTO_SEQ_FRONT){
2222 /* add control to mixer index 0 */
2223 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2224 "Master Front Playback Volume",
2225 HDA_COMPOSE_AMP_VAL(0x18, 3, 0,
2226 HDA_INPUT));
2227 if (err < 0)
2228 return err;
2229 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2230 "Master Front Playback Switch",
2231 HDA_COMPOSE_AMP_VAL(0x18, 3, 0,
2232 HDA_INPUT));
2233 if (err < 0)
2234 return err;
2235
2236 /* add control to PW3 */
2237 sprintf(name, "%s Playback Volume", chname[i]);
2238 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2239 HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2240 HDA_OUTPUT));
2241 if (err < 0)
2242 return err;
2243 sprintf(name, "%s Playback Switch", chname[i]);
2244 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2245 HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2246 HDA_OUTPUT));
2247 if (err < 0)
2248 return err;
2249 } else if (i == AUTO_SEQ_SURROUND) {
2250 sprintf(name, "%s Playback Volume", chname[i]);
2251 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2252 HDA_COMPOSE_AMP_VAL(0x1a, 3, 0,
2253 HDA_OUTPUT));
2254 if (err < 0)
2255 return err;
2256 sprintf(name, "%s Playback Switch", chname[i]);
2257 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2258 HDA_COMPOSE_AMP_VAL(0x1a, 3, 0,
2259 HDA_OUTPUT));
2260 if (err < 0)
2261 return err;
2262 } else if (i == AUTO_SEQ_SIDE) {
2263 sprintf(name, "%s Playback Volume", chname[i]);
2264 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2265 HDA_COMPOSE_AMP_VAL(0x29, 3, 0,
2266 HDA_OUTPUT));
2267 if (err < 0)
2268 return err;
2269 sprintf(name, "%s Playback Switch", chname[i]);
2270 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2271 HDA_COMPOSE_AMP_VAL(0x29, 3, 0,
2272 HDA_OUTPUT));
2273 if (err < 0)
2274 return err;
2275 }
2276 }
2277
2278 return 0;
2279 }
2280
2281 static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2282 {
2283 int err;
2284
2285 if (!pin)
2286 return 0;
2287
2288 if (spec->multiout.num_dacs == 5) /* 10 channels */
2289 spec->multiout.hp_nid = VT1709_HP_DAC_NID;
2290 else if (spec->multiout.num_dacs == 3) /* 6 channels */
2291 spec->multiout.hp_nid = 0;
2292 spec->hp_independent_mode_index = 1;
2293
2294 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2295 "Headphone Playback Volume",
2296 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2297 if (err < 0)
2298 return err;
2299 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2300 "Headphone Playback Switch",
2301 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2302 if (err < 0)
2303 return err;
2304
2305 return 0;
2306 }
2307
2308 /* create playback/capture controls for input pins */
2309 static int vt1709_auto_create_analog_input_ctls(struct via_spec *spec,
2310 const struct auto_pin_cfg *cfg)
2311 {
2312 static char *labels[] = {
2313 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
2314 };
2315 struct hda_input_mux *imux = &spec->private_imux[0];
2316 int i, err, idx = 0;
2317
2318 /* for internal loopback recording select */
2319 imux->items[imux->num_items].label = "Stereo Mixer";
2320 imux->items[imux->num_items].index = idx;
2321 imux->num_items++;
2322
2323 for (i = 0; i < AUTO_PIN_LAST; i++) {
2324 if (!cfg->input_pins[i])
2325 continue;
2326
2327 switch (cfg->input_pins[i]) {
2328 case 0x1d: /* Mic */
2329 idx = 2;
2330 break;
2331
2332 case 0x1e: /* Line In */
2333 idx = 3;
2334 break;
2335
2336 case 0x21: /* Front Mic */
2337 idx = 4;
2338 break;
2339
2340 case 0x23: /* CD */
2341 idx = 1;
2342 break;
2343 }
2344 err = via_new_analog_input(spec, labels[i], idx, 0x18);
2345 if (err < 0)
2346 return err;
2347 imux->items[imux->num_items].label = labels[i];
2348 imux->items[imux->num_items].index = idx;
2349 imux->num_items++;
2350 }
2351 return 0;
2352 }
2353
2354 static int vt1709_parse_auto_config(struct hda_codec *codec)
2355 {
2356 struct via_spec *spec = codec->spec;
2357 int err;
2358
2359 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2360 if (err < 0)
2361 return err;
2362 err = vt1709_auto_fill_dac_nids(spec, &spec->autocfg);
2363 if (err < 0)
2364 return err;
2365 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2366 return 0; /* can't find valid BIOS pin config */
2367
2368 err = vt1709_auto_create_multi_out_ctls(spec, &spec->autocfg);
2369 if (err < 0)
2370 return err;
2371 err = vt1709_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2372 if (err < 0)
2373 return err;
2374 err = vt1709_auto_create_analog_input_ctls(spec, &spec->autocfg);
2375 if (err < 0)
2376 return err;
2377
2378 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2379
2380 if (spec->autocfg.dig_outs)
2381 spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
2382 spec->dig_in_pin = VT1709_DIGIN_PIN;
2383 if (spec->autocfg.dig_in_pin)
2384 spec->dig_in_nid = VT1709_DIGIN_NID;
2385
2386 if (spec->kctls.list)
2387 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2388
2389 spec->input_mux = &spec->private_imux[0];
2390
2391 if (spec->hp_mux)
2392 spec->mixers[spec->num_mixers++] = via_hp_mixer;
2393
2394 spec->mixers[spec->num_mixers++] = via_smart51_mixer;
2395 return 1;
2396 }
2397
2398 #ifdef CONFIG_SND_HDA_POWER_SAVE
2399 static struct hda_amp_list vt1709_loopbacks[] = {
2400 { 0x18, HDA_INPUT, 1 },
2401 { 0x18, HDA_INPUT, 2 },
2402 { 0x18, HDA_INPUT, 3 },
2403 { 0x18, HDA_INPUT, 4 },
2404 { } /* end */
2405 };
2406 #endif
2407
2408 static int patch_vt1709_10ch(struct hda_codec *codec)
2409 {
2410 struct via_spec *spec;
2411 int err;
2412
2413 /* create a codec specific record */
2414 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2415 if (spec == NULL)
2416 return -ENOMEM;
2417
2418 codec->spec = spec;
2419
2420 err = vt1709_parse_auto_config(codec);
2421 if (err < 0) {
2422 via_free(codec);
2423 return err;
2424 } else if (!err) {
2425 printk(KERN_INFO "hda_codec: Cannot set up configuration. "
2426 "Using genenic mode...\n");
2427 }
2428
2429 spec->init_verbs[spec->num_iverbs++] = vt1709_10ch_volume_init_verbs;
2430 spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
2431
2432 spec->stream_name_analog = "VT1709 Analog";
2433 spec->stream_analog_playback = &vt1709_10ch_pcm_analog_playback;
2434 spec->stream_analog_capture = &vt1709_pcm_analog_capture;
2435
2436 spec->stream_name_digital = "VT1709 Digital";
2437 spec->stream_digital_playback = &vt1709_pcm_digital_playback;
2438 spec->stream_digital_capture = &vt1709_pcm_digital_capture;
2439
2440
2441 if (!spec->adc_nids && spec->input_mux) {
2442 spec->adc_nids = vt1709_adc_nids;
2443 spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
2444 get_mux_nids(codec);
2445 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
2446 spec->num_mixers++;
2447 }
2448
2449 codec->patch_ops = via_patch_ops;
2450
2451 codec->patch_ops.init = via_auto_init;
2452 codec->patch_ops.unsol_event = via_unsol_event;
2453 #ifdef CONFIG_SND_HDA_POWER_SAVE
2454 spec->loopback.amplist = vt1709_loopbacks;
2455 #endif
2456
2457 return 0;
2458 }
2459 /*
2460 * generic initialization of ADC, input mixers and output mixers
2461 */
2462 static struct hda_verb vt1709_6ch_volume_init_verbs[] = {
2463 /*
2464 * Unmute ADC0-2 and set the default input to mic-in
2465 */
2466 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2467 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2468 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2469
2470
2471 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2472 * mixer widget
2473 */
2474 /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2475 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2476 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2477 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2478 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2479 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2480
2481 /*
2482 * Set up output selector (0x1a, 0x1b, 0x29)
2483 */
2484 /* set vol=0 to output mixers */
2485 {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2486 {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2487 {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2488
2489 /*
2490 * Unmute PW3 and PW4
2491 */
2492 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2493 {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2494
2495 /* Set input of PW4 as MW0 */
2496 {0x20, AC_VERB_SET_CONNECT_SEL, 0},
2497 /* PW9 Output enable */
2498 {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2499 { }
2500 };
2501
2502 static int patch_vt1709_6ch(struct hda_codec *codec)
2503 {
2504 struct via_spec *spec;
2505 int err;
2506
2507 /* create a codec specific record */
2508 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2509 if (spec == NULL)
2510 return -ENOMEM;
2511
2512 codec->spec = spec;
2513
2514 err = vt1709_parse_auto_config(codec);
2515 if (err < 0) {
2516 via_free(codec);
2517 return err;
2518 } else if (!err) {
2519 printk(KERN_INFO "hda_codec: Cannot set up configuration. "
2520 "Using genenic mode...\n");
2521 }
2522
2523 spec->init_verbs[spec->num_iverbs++] = vt1709_6ch_volume_init_verbs;
2524 spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
2525
2526 spec->stream_name_analog = "VT1709 Analog";
2527 spec->stream_analog_playback = &vt1709_6ch_pcm_analog_playback;
2528 spec->stream_analog_capture = &vt1709_pcm_analog_capture;
2529
2530 spec->stream_name_digital = "VT1709 Digital";
2531 spec->stream_digital_playback = &vt1709_pcm_digital_playback;
2532 spec->stream_digital_capture = &vt1709_pcm_digital_capture;
2533
2534
2535 if (!spec->adc_nids && spec->input_mux) {
2536 spec->adc_nids = vt1709_adc_nids;
2537 spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
2538 get_mux_nids(codec);
2539 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
2540 spec->num_mixers++;
2541 }
2542
2543 codec->patch_ops = via_patch_ops;
2544
2545 codec->patch_ops.init = via_auto_init;
2546 codec->patch_ops.unsol_event = via_unsol_event;
2547 #ifdef CONFIG_SND_HDA_POWER_SAVE
2548 spec->loopback.amplist = vt1709_loopbacks;
2549 #endif
2550 return 0;
2551 }
2552
2553 /* capture mixer elements */
2554 static struct snd_kcontrol_new vt1708B_capture_mixer[] = {
2555 HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
2556 HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
2557 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
2558 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
2559 {
2560 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2561 /* The multiple "Capture Source" controls confuse alsamixer
2562 * So call somewhat different..
2563 */
2564 /* .name = "Capture Source", */
2565 .name = "Input Source",
2566 .count = 1,
2567 .info = via_mux_enum_info,
2568 .get = via_mux_enum_get,
2569 .put = via_mux_enum_put,
2570 },
2571 { } /* end */
2572 };
2573 /*
2574 * generic initialization of ADC, input mixers and output mixers
2575 */
2576 static struct hda_verb vt1708B_8ch_volume_init_verbs[] = {
2577 /*
2578 * Unmute ADC0-1 and set the default input to mic-in
2579 */
2580 {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2581 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2582
2583
2584 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2585 * mixer widget
2586 */
2587 /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2588 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2589 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2590 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2591 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2592 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2593
2594 /*
2595 * Set up output mixers
2596 */
2597 /* set vol=0 to output mixers */
2598 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2599 {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2600 {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2601
2602 /* Setup default input to PW4 */
2603 {0x1d, AC_VERB_SET_CONNECT_SEL, 0x1},
2604 /* PW9 Output enable */
2605 {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2606 /* PW10 Input enable */
2607 {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2608 { }
2609 };
2610
2611 static struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
2612 /*
2613 * Unmute ADC0-1 and set the default input to mic-in
2614 */
2615 {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2616 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2617
2618
2619 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2620 * mixer widget
2621 */
2622 /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2623 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2624 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2625 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2626 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2627 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2628
2629 /*
2630 * Set up output mixers
2631 */
2632 /* set vol=0 to output mixers */
2633 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2634 {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2635 {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2636
2637 /* Setup default input of PW4 to MW0 */
2638 {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
2639 /* PW9 Output enable */
2640 {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2641 /* PW10 Input enable */
2642 {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2643 { }
2644 };
2645
2646 static struct hda_verb vt1708B_uniwill_init_verbs[] = {
2647 {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
2648 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
2649 {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2650 {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2651 {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2652 {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2653 {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2654 {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2655 {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2656 { }
2657 };
2658
2659 static int via_pcm_open_close(struct hda_pcm_stream *hinfo,
2660 struct hda_codec *codec,
2661 struct snd_pcm_substream *substream)
2662 {
2663 int idle = substream->pstr->substream_opened == 1
2664 && substream->ref_count == 0;
2665
2666 analog_low_current_mode(codec, idle);
2667 return 0;
2668 }
2669
2670 static struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
2671 .substreams = 2,
2672 .channels_min = 2,
2673 .channels_max = 8,
2674 .nid = 0x10, /* NID to query formats and rates */
2675 .ops = {
2676 .open = via_playback_pcm_open,
2677 .prepare = via_playback_multi_pcm_prepare,
2678 .cleanup = via_playback_multi_pcm_cleanup,
2679 .close = via_pcm_open_close
2680 },
2681 };
2682
2683 static struct hda_pcm_stream vt1708B_4ch_pcm_analog_playback = {
2684 .substreams = 2,
2685 .channels_min = 2,
2686 .channels_max = 4,
2687 .nid = 0x10, /* NID to query formats and rates */
2688 .ops = {
2689 .open = via_playback_pcm_open,
2690 .prepare = via_playback_multi_pcm_prepare,
2691 .cleanup = via_playback_multi_pcm_cleanup
2692 },
2693 };
2694
2695 static struct hda_pcm_stream vt1708B_pcm_analog_capture = {
2696 .substreams = 2,
2697 .channels_min = 2,
2698 .channels_max = 2,
2699 .nid = 0x13, /* NID to query formats and rates */
2700 .ops = {
2701 .open = via_pcm_open_close,
2702 .prepare = via_capture_pcm_prepare,
2703 .cleanup = via_capture_pcm_cleanup,
2704 .close = via_pcm_open_close
2705 },
2706 };
2707
2708 static struct hda_pcm_stream vt1708B_pcm_digital_playback = {
2709 .substreams = 1,
2710 .channels_min = 2,
2711 .channels_max = 2,
2712 /* NID is set in via_build_pcms */
2713 .ops = {
2714 .open = via_dig_playback_pcm_open,
2715 .close = via_dig_playback_pcm_close,
2716 .prepare = via_dig_playback_pcm_prepare,
2717 .cleanup = via_dig_playback_pcm_cleanup
2718 },
2719 };
2720
2721 static struct hda_pcm_stream vt1708B_pcm_digital_capture = {
2722 .substreams = 1,
2723 .channels_min = 2,
2724 .channels_max = 2,
2725 };
2726
2727 /* fill in the dac_nids table from the parsed pin configuration */
2728 static int vt1708B_auto_fill_dac_nids(struct via_spec *spec,
2729 const struct auto_pin_cfg *cfg)
2730 {
2731 int i;
2732 hda_nid_t nid;
2733
2734 spec->multiout.num_dacs = cfg->line_outs;
2735
2736 spec->multiout.dac_nids = spec->private_dac_nids;
2737
2738 for (i = 0; i < 4; i++) {
2739 nid = cfg->line_out_pins[i];
2740 if (nid) {
2741 /* config dac list */
2742 switch (i) {
2743 case AUTO_SEQ_FRONT:
2744 spec->multiout.dac_nids[i] = 0x10;
2745 break;
2746 case AUTO_SEQ_CENLFE:
2747 spec->multiout.dac_nids[i] = 0x24;
2748 break;
2749 case AUTO_SEQ_SURROUND:
2750 spec->multiout.dac_nids[i] = 0x11;
2751 break;
2752 case AUTO_SEQ_SIDE:
2753 spec->multiout.dac_nids[i] = 0x25;
2754 break;
2755 }
2756 }
2757 }
2758
2759 return 0;
2760 }
2761
2762 /* add playback controls from the parsed DAC table */
2763 static int vt1708B_auto_create_multi_out_ctls(struct via_spec *spec,
2764 const struct auto_pin_cfg *cfg)
2765 {
2766 char name[32];
2767 static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
2768 hda_nid_t nid_vols[] = {0x16, 0x18, 0x26, 0x27};
2769 hda_nid_t nid, nid_vol = 0;
2770 int i, err;
2771
2772 for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2773 nid = cfg->line_out_pins[i];
2774
2775 if (!nid)
2776 continue;
2777
2778 nid_vol = nid_vols[i];
2779
2780 if (i == AUTO_SEQ_CENLFE) {
2781 /* Center/LFE */
2782 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2783 "Center Playback Volume",
2784 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2785 HDA_OUTPUT));
2786 if (err < 0)
2787 return err;
2788 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2789 "LFE Playback Volume",
2790 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2791 HDA_OUTPUT));
2792 if (err < 0)
2793 return err;
2794 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2795 "Center Playback Switch",
2796 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2797 HDA_OUTPUT));
2798 if (err < 0)
2799 return err;
2800 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2801 "LFE Playback Switch",
2802 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2803 HDA_OUTPUT));
2804 if (err < 0)
2805 return err;
2806 } else if (i == AUTO_SEQ_FRONT) {
2807 /* add control to mixer index 0 */
2808 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2809 "Master Front Playback Volume",
2810 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2811 HDA_INPUT));
2812 if (err < 0)
2813 return err;
2814 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2815 "Master Front Playback Switch",
2816 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2817 HDA_INPUT));
2818 if (err < 0)
2819 return err;
2820
2821 /* add control to PW3 */
2822 sprintf(name, "%s Playback Volume", chname[i]);
2823 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2824 HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2825 HDA_OUTPUT));
2826 if (err < 0)
2827 return err;
2828 sprintf(name, "%s Playback Switch", chname[i]);
2829 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2830 HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2831 HDA_OUTPUT));
2832 if (err < 0)
2833 return err;
2834 } else {
2835 sprintf(name, "%s Playback Volume", chname[i]);
2836 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2837 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2838 HDA_OUTPUT));
2839 if (err < 0)
2840 return err;
2841 sprintf(name, "%s Playback Switch", chname[i]);
2842 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2843 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2844 HDA_OUTPUT));
2845 if (err < 0)
2846 return err;
2847 }
2848 }
2849
2850 return 0;
2851 }
2852
2853 static int vt1708B_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2854 {
2855 int err;
2856
2857 if (!pin)
2858 return 0;
2859
2860 spec->multiout.hp_nid = VT1708B_HP_NID; /* AOW3 */
2861 spec->hp_independent_mode_index = 1;
2862
2863 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2864 "Headphone Playback Volume",
2865 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2866 if (err < 0)
2867 return err;
2868 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2869 "Headphone Playback Switch",
2870 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2871 if (err < 0)
2872 return err;
2873
2874 create_hp_imux(spec);
2875
2876 return 0;
2877 }
2878
2879 /* create playback/capture controls for input pins */
2880 static int vt1708B_auto_create_analog_input_ctls(struct via_spec *spec,
2881 const struct auto_pin_cfg *cfg)
2882 {
2883 static char *labels[] = {
2884 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
2885 };
2886 struct hda_input_mux *imux = &spec->private_imux[0];
2887 int i, err, idx = 0;
2888
2889 /* for internal loopback recording select */
2890 imux->items[imux->num_items].label = "Stereo Mixer";
2891 imux->items[imux->num_items].index = idx;
2892 imux->num_items++;
2893
2894 for (i = 0; i < AUTO_PIN_LAST; i++) {
2895 if (!cfg->input_pins[i])
2896 continue;
2897
2898 switch (cfg->input_pins[i]) {
2899 case 0x1a: /* Mic */
2900 idx = 2;
2901 break;
2902
2903 case 0x1b: /* Line In */
2904 idx = 3;
2905 break;
2906
2907 case 0x1e: /* Front Mic */
2908 idx = 4;
2909 break;
2910
2911 case 0x1f: /* CD */
2912 idx = 1;
2913 break;
2914 }
2915 err = via_new_analog_input(spec, labels[i], idx, 0x16);
2916 if (err < 0)
2917 return err;
2918 imux->items[imux->num_items].label = labels[i];
2919 imux->items[imux->num_items].index = idx;
2920 imux->num_items++;
2921 }
2922 return 0;
2923 }
2924
2925 static int vt1708B_parse_auto_config(struct hda_codec *codec)
2926 {
2927 struct via_spec *spec = codec->spec;
2928 int err;
2929
2930 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2931 if (err < 0)
2932 return err;
2933 err = vt1708B_auto_fill_dac_nids(spec, &spec->autocfg);
2934 if (err < 0)
2935 return err;
2936 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2937 return 0; /* can't find valid BIOS pin config */
2938
2939 err = vt1708B_auto_create_multi_out_ctls(spec, &spec->autocfg);
2940 if (err < 0)
2941 return err;
2942 err = vt1708B_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2943 if (err < 0)
2944 return err;
2945 err = vt1708B_auto_create_analog_input_ctls(spec, &spec->autocfg);
2946 if (err < 0)
2947 return err;
2948
2949 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2950
2951 if (spec->autocfg.dig_outs)
2952 spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
2953 spec->dig_in_pin = VT1708B_DIGIN_PIN;
2954 if (spec->autocfg.dig_in_pin)
2955 spec->dig_in_nid = VT1708B_DIGIN_NID;
2956
2957 if (spec->kctls.list)
2958 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2959
2960 spec->input_mux = &spec->private_imux[0];
2961
2962 if (spec->hp_mux)
2963 spec->mixers[spec->num_mixers++] = via_hp_mixer;
2964
2965 spec->mixers[spec->num_mixers++] = via_smart51_mixer;
2966 return 1;
2967 }
2968
2969 #ifdef CONFIG_SND_HDA_POWER_SAVE
2970 static struct hda_amp_list vt1708B_loopbacks[] = {
2971 { 0x16, HDA_INPUT, 1 },
2972 { 0x16, HDA_INPUT, 2 },
2973 { 0x16, HDA_INPUT, 3 },
2974 { 0x16, HDA_INPUT, 4 },
2975 { } /* end */
2976 };
2977 #endif
2978 static int patch_vt1708S(struct hda_codec *codec);
2979 static int patch_vt1708B_8ch(struct hda_codec *codec)
2980 {
2981 struct via_spec *spec;
2982 int err;
2983
2984 if (get_codec_type(codec) == VT1708BCE)
2985 return patch_vt1708S(codec);
2986 /* create a codec specific record */
2987 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2988 if (spec == NULL)
2989 return -ENOMEM;
2990
2991 codec->spec = spec;
2992
2993 /* automatic parse from the BIOS config */
2994 err = vt1708B_parse_auto_config(codec);
2995 if (err < 0) {
2996 via_free(codec);
2997 return err;
2998 } else if (!err) {
2999 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3000 "from BIOS. Using genenic mode...\n");
3001 }
3002
3003 spec->init_verbs[spec->num_iverbs++] = vt1708B_8ch_volume_init_verbs;
3004 spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
3005
3006 spec->stream_name_analog = "VT1708B Analog";
3007 spec->stream_analog_playback = &vt1708B_8ch_pcm_analog_playback;
3008 spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3009
3010 spec->stream_name_digital = "VT1708B Digital";
3011 spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3012 spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3013
3014 if (!spec->adc_nids && spec->input_mux) {
3015 spec->adc_nids = vt1708B_adc_nids;
3016 spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
3017 get_mux_nids(codec);
3018 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3019 spec->num_mixers++;
3020 }
3021
3022 codec->patch_ops = via_patch_ops;
3023
3024 codec->patch_ops.init = via_auto_init;
3025 codec->patch_ops.unsol_event = via_unsol_event;
3026 #ifdef CONFIG_SND_HDA_POWER_SAVE
3027 spec->loopback.amplist = vt1708B_loopbacks;
3028 #endif
3029
3030 return 0;
3031 }
3032
3033 static int patch_vt1708B_4ch(struct hda_codec *codec)
3034 {
3035 struct via_spec *spec;
3036 int err;
3037
3038 /* create a codec specific record */
3039 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3040 if (spec == NULL)
3041 return -ENOMEM;
3042
3043 codec->spec = spec;
3044
3045 /* automatic parse from the BIOS config */
3046 err = vt1708B_parse_auto_config(codec);
3047 if (err < 0) {
3048 via_free(codec);
3049 return err;
3050 } else if (!err) {
3051 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3052 "from BIOS. Using genenic mode...\n");
3053 }
3054
3055 spec->init_verbs[spec->num_iverbs++] = vt1708B_4ch_volume_init_verbs;
3056 spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
3057
3058 spec->stream_name_analog = "VT1708B Analog";
3059 spec->stream_analog_playback = &vt1708B_4ch_pcm_analog_playback;
3060 spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3061
3062 spec->stream_name_digital = "VT1708B Digital";
3063 spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3064 spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3065
3066 if (!spec->adc_nids && spec->input_mux) {
3067 spec->adc_nids = vt1708B_adc_nids;
3068 spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
3069 get_mux_nids(codec);
3070 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3071 spec->num_mixers++;
3072 }
3073
3074 codec->patch_ops = via_patch_ops;
3075
3076 codec->patch_ops.init = via_auto_init;
3077 codec->patch_ops.unsol_event = via_unsol_event;
3078 #ifdef CONFIG_SND_HDA_POWER_SAVE
3079 spec->loopback.amplist = vt1708B_loopbacks;
3080 #endif
3081
3082 return 0;
3083 }
3084
3085 /* Patch for VT1708S */
3086
3087 /* VT1708S software backdoor based override for buggy hardware micboost
3088 * setting */
3089 #define MIC_BOOST_VOLUME(xname, nid) { \
3090 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3091 .name = xname, \
3092 .index = 0, \
3093 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3094 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
3095 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
3096 .info = mic_boost_volume_info, \
3097 .get = snd_hda_mixer_amp_volume_get, \
3098 .put = snd_hda_mixer_amp_volume_put, \
3099 .tlv = { .c = mic_boost_tlv }, \
3100 .private_value = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT) }
3101
3102 /* capture mixer elements */
3103 static struct snd_kcontrol_new vt1708S_capture_mixer[] = {
3104 HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
3105 HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
3106 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
3107 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
3108 MIC_BOOST_VOLUME("Mic Boost Capture Volume", 0x1A),
3109 MIC_BOOST_VOLUME("Front Mic Boost Capture Volume", 0x1E),
3110 {
3111 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3112 /* The multiple "Capture Source" controls confuse alsamixer
3113 * So call somewhat different..
3114 */
3115 /* .name = "Capture Source", */
3116 .name = "Input Source",
3117 .count = 1,
3118 .info = via_mux_enum_info,
3119 .get = via_mux_enum_get,
3120 .put = via_mux_enum_put,
3121 },
3122 { } /* end */
3123 };
3124
3125 static struct hda_verb vt1708S_volume_init_verbs[] = {
3126 /* Unmute ADC0-1 and set the default input to mic-in */
3127 {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3128 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3129
3130 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the
3131 * analog-loopback mixer widget */
3132 /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3133 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3134 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3135 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3136 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3137 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3138
3139 /* Setup default input of PW4 to MW0 */
3140 {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
3141 /* PW9, PW10 Output enable */
3142 {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3143 {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3144 /* Enable Mic Boost Volume backdoor */
3145 {0x1, 0xf98, 0x1},
3146 { }
3147 };
3148
3149 static struct hda_verb vt1708S_uniwill_init_verbs[] = {
3150 {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3151 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3152 {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3153 {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3154 {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3155 {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3156 {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3157 {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3158 {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3159 { }
3160 };
3161
3162 static struct hda_pcm_stream vt1708S_pcm_analog_playback = {
3163 .substreams = 2,
3164 .channels_min = 2,
3165 .channels_max = 8,
3166 .nid = 0x10, /* NID to query formats and rates */
3167 .ops = {
3168 .open = via_playback_pcm_open,
3169 .prepare = via_playback_pcm_prepare,
3170 .cleanup = via_playback_pcm_cleanup,
3171 .close = via_pcm_open_close
3172 },
3173 };
3174
3175 static struct hda_pcm_stream vt1708S_pcm_analog_capture = {
3176 .substreams = 2,
3177 .channels_min = 2,
3178 .channels_max = 2,
3179 .nid = 0x13, /* NID to query formats and rates */
3180 .ops = {
3181 .open = via_pcm_open_close,
3182 .prepare = via_capture_pcm_prepare,
3183 .cleanup = via_capture_pcm_cleanup,
3184 .close = via_pcm_open_close
3185 },
3186 };
3187
3188 static struct hda_pcm_stream vt1708S_pcm_digital_playback = {
3189 .substreams = 1,
3190 .channels_min = 2,
3191 .channels_max = 2,
3192 /* NID is set in via_build_pcms */
3193 .ops = {
3194 .open = via_dig_playback_pcm_open,
3195 .close = via_dig_playback_pcm_close,
3196 .prepare = via_dig_playback_pcm_prepare,
3197 .cleanup = via_dig_playback_pcm_cleanup
3198 },
3199 };
3200
3201 /* fill in the dac_nids table from the parsed pin configuration */
3202 static int vt1708S_auto_fill_dac_nids(struct via_spec *spec,
3203 const struct auto_pin_cfg *cfg)
3204 {
3205 int i;
3206 hda_nid_t nid;
3207
3208 spec->multiout.num_dacs = cfg->line_outs;
3209
3210 spec->multiout.dac_nids = spec->private_dac_nids;
3211
3212 for (i = 0; i < 4; i++) {
3213 nid = cfg->line_out_pins[i];
3214 if (nid) {
3215 /* config dac list */
3216 switch (i) {
3217 case AUTO_SEQ_FRONT:
3218 spec->multiout.dac_nids[i] = 0x10;
3219 break;
3220 case AUTO_SEQ_CENLFE:
3221 spec->multiout.dac_nids[i] = 0x24;
3222 break;
3223 case AUTO_SEQ_SURROUND:
3224 spec->multiout.dac_nids[i] = 0x11;
3225 break;
3226 case AUTO_SEQ_SIDE:
3227 spec->multiout.dac_nids[i] = 0x25;
3228 break;
3229 }
3230 }
3231 }
3232
3233 return 0;
3234 }
3235
3236 /* add playback controls from the parsed DAC table */
3237 static int vt1708S_auto_create_multi_out_ctls(struct via_spec *spec,
3238 const struct auto_pin_cfg *cfg)
3239 {
3240 char name[32];
3241 static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
3242 hda_nid_t nid_vols[] = {0x10, 0x11, 0x24, 0x25};
3243 hda_nid_t nid_mutes[] = {0x1C, 0x18, 0x26, 0x27};
3244 hda_nid_t nid, nid_vol, nid_mute;
3245 int i, err;
3246
3247 for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
3248 nid = cfg->line_out_pins[i];
3249
3250 if (!nid)
3251 continue;
3252
3253 nid_vol = nid_vols[i];
3254 nid_mute = nid_mutes[i];
3255
3256 if (i == AUTO_SEQ_CENLFE) {
3257 /* Center/LFE */
3258 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3259 "Center Playback Volume",
3260 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3261 HDA_OUTPUT));
3262 if (err < 0)
3263 return err;
3264 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3265 "LFE Playback Volume",
3266 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3267 HDA_OUTPUT));
3268 if (err < 0)
3269 return err;
3270 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3271 "Center Playback Switch",
3272 HDA_COMPOSE_AMP_VAL(nid_mute,
3273 1, 0,
3274 HDA_OUTPUT));
3275 if (err < 0)
3276 return err;
3277 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3278 "LFE Playback Switch",
3279 HDA_COMPOSE_AMP_VAL(nid_mute,
3280 2, 0,
3281 HDA_OUTPUT));
3282 if (err < 0)
3283 return err;
3284 } else if (i == AUTO_SEQ_FRONT) {
3285 /* add control to mixer index 0 */
3286 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3287 "Master Front Playback Volume",
3288 HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3289 HDA_INPUT));
3290 if (err < 0)
3291 return err;
3292 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3293 "Master Front Playback Switch",
3294 HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3295 HDA_INPUT));
3296 if (err < 0)
3297 return err;
3298
3299 /* Front */
3300 sprintf(name, "%s Playback Volume", chname[i]);
3301 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3302 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3303 HDA_OUTPUT));
3304 if (err < 0)
3305 return err;
3306 sprintf(name, "%s Playback Switch", chname[i]);
3307 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3308 HDA_COMPOSE_AMP_VAL(nid_mute,
3309 3, 0,
3310 HDA_OUTPUT));
3311 if (err < 0)
3312 return err;
3313 } else {
3314 sprintf(name, "%s Playback Volume", chname[i]);
3315 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3316 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3317 HDA_OUTPUT));
3318 if (err < 0)
3319 return err;
3320 sprintf(name, "%s Playback Switch", chname[i]);
3321 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3322 HDA_COMPOSE_AMP_VAL(nid_mute,
3323 3, 0,
3324 HDA_OUTPUT));
3325 if (err < 0)
3326 return err;
3327 }
3328 }
3329
3330 return 0;
3331 }
3332
3333 static int vt1708S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3334 {
3335 int err;
3336
3337 if (!pin)
3338 return 0;
3339
3340 spec->multiout.hp_nid = VT1708S_HP_NID; /* AOW3 */
3341 spec->hp_independent_mode_index = 1;
3342
3343 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3344 "Headphone Playback Volume",
3345 HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
3346 if (err < 0)
3347 return err;
3348
3349 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3350 "Headphone Playback Switch",
3351 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3352 if (err < 0)
3353 return err;
3354
3355 create_hp_imux(spec);
3356
3357 return 0;
3358 }
3359
3360 /* create playback/capture controls for input pins */
3361 static int vt1708S_auto_create_analog_input_ctls(struct via_spec *spec,
3362 const struct auto_pin_cfg *cfg)
3363 {
3364 static char *labels[] = {
3365 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
3366 };
3367 struct hda_input_mux *imux = &spec->private_imux[0];
3368 int i, err, idx = 0;
3369
3370 /* for internal loopback recording select */
3371 imux->items[imux->num_items].label = "Stereo Mixer";
3372 imux->items[imux->num_items].index = 5;
3373 imux->num_items++;
3374
3375 for (i = 0; i < AUTO_PIN_LAST; i++) {
3376 if (!cfg->input_pins[i])
3377 continue;
3378
3379 switch (cfg->input_pins[i]) {
3380 case 0x1a: /* Mic */
3381 idx = 2;
3382 break;
3383
3384 case 0x1b: /* Line In */
3385 idx = 3;
3386 break;
3387
3388 case 0x1e: /* Front Mic */
3389 idx = 4;
3390 break;
3391
3392 case 0x1f: /* CD */
3393 idx = 1;
3394 break;
3395 }
3396 err = via_new_analog_input(spec, labels[i], idx, 0x16);
3397 if (err < 0)
3398 return err;
3399 imux->items[imux->num_items].label = labels[i];
3400 imux->items[imux->num_items].index = idx-1;
3401 imux->num_items++;
3402 }
3403 return 0;
3404 }
3405
3406 /* fill out digital output widgets; one for master and one for slave outputs */
3407 static void fill_dig_outs(struct hda_codec *codec)
3408 {
3409 struct via_spec *spec = codec->spec;
3410 int i;
3411
3412 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3413 hda_nid_t nid;
3414 int conn;
3415
3416 nid = spec->autocfg.dig_out_pins[i];
3417 if (!nid)
3418 continue;
3419 conn = snd_hda_get_connections(codec, nid, &nid, 1);
3420 if (conn < 1)
3421 continue;
3422 if (!spec->multiout.dig_out_nid)
3423 spec->multiout.dig_out_nid = nid;
3424 else {
3425 spec->slave_dig_outs[0] = nid;
3426 break; /* at most two dig outs */
3427 }
3428 }
3429 }
3430
3431 static int vt1708S_parse_auto_config(struct hda_codec *codec)
3432 {
3433 struct via_spec *spec = codec->spec;
3434 int err;
3435
3436 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3437 if (err < 0)
3438 return err;
3439 err = vt1708S_auto_fill_dac_nids(spec, &spec->autocfg);
3440 if (err < 0)
3441 return err;
3442 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3443 return 0; /* can't find valid BIOS pin config */
3444
3445 err = vt1708S_auto_create_multi_out_ctls(spec, &spec->autocfg);
3446 if (err < 0)
3447 return err;
3448 err = vt1708S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3449 if (err < 0)
3450 return err;
3451 err = vt1708S_auto_create_analog_input_ctls(spec, &spec->autocfg);
3452 if (err < 0)
3453 return err;
3454
3455 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3456
3457 fill_dig_outs(codec);
3458
3459 if (spec->kctls.list)
3460 spec->mixers[spec->num_mixers++] = spec->kctls.list;
3461
3462 spec->input_mux = &spec->private_imux[0];
3463
3464 if (spec->hp_mux)
3465 spec->mixers[spec->num_mixers++] = via_hp_mixer;
3466
3467 spec->mixers[spec->num_mixers++] = via_smart51_mixer;
3468 return 1;
3469 }
3470
3471 #ifdef CONFIG_SND_HDA_POWER_SAVE
3472 static struct hda_amp_list vt1708S_loopbacks[] = {
3473 { 0x16, HDA_INPUT, 1 },
3474 { 0x16, HDA_INPUT, 2 },
3475 { 0x16, HDA_INPUT, 3 },
3476 { 0x16, HDA_INPUT, 4 },
3477 { } /* end */
3478 };
3479 #endif
3480
3481 static int patch_vt1708S(struct hda_codec *codec)
3482 {
3483 struct via_spec *spec;
3484 int err;
3485
3486 /* create a codec specific record */
3487 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3488 if (spec == NULL)
3489 return -ENOMEM;
3490
3491 codec->spec = spec;
3492
3493 /* automatic parse from the BIOS config */
3494 err = vt1708S_parse_auto_config(codec);
3495 if (err < 0) {
3496 via_free(codec);
3497 return err;
3498 } else if (!err) {
3499 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3500 "from BIOS. Using genenic mode...\n");
3501 }
3502
3503 spec->init_verbs[spec->num_iverbs++] = vt1708S_volume_init_verbs;
3504 spec->init_verbs[spec->num_iverbs++] = vt1708S_uniwill_init_verbs;
3505
3506 spec->stream_name_analog = "VT1708S Analog";
3507 spec->stream_analog_playback = &vt1708S_pcm_analog_playback;
3508 spec->stream_analog_capture = &vt1708S_pcm_analog_capture;
3509
3510 spec->stream_name_digital = "VT1708S Digital";
3511 spec->stream_digital_playback = &vt1708S_pcm_digital_playback;
3512
3513 if (!spec->adc_nids && spec->input_mux) {
3514 spec->adc_nids = vt1708S_adc_nids;
3515 spec->num_adc_nids = ARRAY_SIZE(vt1708S_adc_nids);
3516 get_mux_nids(codec);
3517 spec->mixers[spec->num_mixers] = vt1708S_capture_mixer;
3518 spec->num_mixers++;
3519 }
3520
3521 codec->patch_ops = via_patch_ops;
3522
3523 codec->patch_ops.init = via_auto_init;
3524 codec->patch_ops.unsol_event = via_unsol_event;
3525 #ifdef CONFIG_SND_HDA_POWER_SAVE
3526 spec->loopback.amplist = vt1708S_loopbacks;
3527 #endif
3528
3529 /* correct names for VT1708BCE */
3530 if (get_codec_type(codec) == VT1708BCE) {
3531 kfree(codec->chip_name);
3532 codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3533 snprintf(codec->bus->card->mixername,
3534 sizeof(codec->bus->card->mixername),
3535 "%s %s", codec->vendor_name, codec->chip_name);
3536 spec->stream_name_analog = "VT1708BCE Analog";
3537 spec->stream_name_digital = "VT1708BCE Digital";
3538 }
3539 return 0;
3540 }
3541
3542 /* Patch for VT1702 */
3543
3544 /* capture mixer elements */
3545 static struct snd_kcontrol_new vt1702_capture_mixer[] = {
3546 HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_INPUT),
3547 HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_INPUT),
3548 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x20, 0x0, HDA_INPUT),
3549 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x20, 0x0, HDA_INPUT),
3550 HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x1F, 0x0, HDA_INPUT),
3551 HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x1F, 0x0, HDA_INPUT),
3552 HDA_CODEC_VOLUME("Digital Mic Boost Capture Volume", 0x1E, 0x0,
3553 HDA_INPUT),
3554 {
3555 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3556 /* The multiple "Capture Source" controls confuse alsamixer
3557 * So call somewhat different..
3558 */
3559 /* .name = "Capture Source", */
3560 .name = "Input Source",
3561 .count = 1,
3562 .info = via_mux_enum_info,
3563 .get = via_mux_enum_get,
3564 .put = via_mux_enum_put,
3565 },
3566 { } /* end */
3567 };
3568
3569 static struct hda_verb vt1702_volume_init_verbs[] = {
3570 /*
3571 * Unmute ADC0-1 and set the default input to mic-in
3572 */
3573 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3574 {0x1F, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3575 {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3576
3577
3578 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3579 * mixer widget
3580 */
3581 /* Amp Indices: Mic1 = 1, Line = 1, Mic2 = 3 */
3582 {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3583 {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3584 {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3585 {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3586 {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3587
3588 /* Setup default input of PW4 to MW0 */
3589 {0x17, AC_VERB_SET_CONNECT_SEL, 0x1},
3590 /* PW6 PW7 Output enable */
3591 {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3592 {0x1C, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3593 { }
3594 };
3595
3596 static struct hda_verb vt1702_uniwill_init_verbs[] = {
3597 {0x17, AC_VERB_SET_UNSOLICITED_ENABLE,
3598 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3599 {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3600 {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3601 {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3602 {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3603 { }
3604 };
3605
3606 static struct hda_pcm_stream vt1702_pcm_analog_playback = {
3607 .substreams = 2,
3608 .channels_min = 2,
3609 .channels_max = 2,
3610 .nid = 0x10, /* NID to query formats and rates */
3611 .ops = {
3612 .open = via_playback_pcm_open,
3613 .prepare = via_playback_multi_pcm_prepare,
3614 .cleanup = via_playback_multi_pcm_cleanup,
3615 .close = via_pcm_open_close
3616 },
3617 };
3618
3619 static struct hda_pcm_stream vt1702_pcm_analog_capture = {
3620 .substreams = 3,
3621 .channels_min = 2,
3622 .channels_max = 2,
3623 .nid = 0x12, /* NID to query formats and rates */
3624 .ops = {
3625 .open = via_pcm_open_close,
3626 .prepare = via_capture_pcm_prepare,
3627 .cleanup = via_capture_pcm_cleanup,
3628 .close = via_pcm_open_close
3629 },
3630 };
3631
3632 static struct hda_pcm_stream vt1702_pcm_digital_playback = {
3633 .substreams = 2,
3634 .channels_min = 2,
3635 .channels_max = 2,
3636 /* NID is set in via_build_pcms */
3637 .ops = {
3638 .open = via_dig_playback_pcm_open,
3639 .close = via_dig_playback_pcm_close,
3640 .prepare = via_dig_playback_pcm_prepare,
3641 .cleanup = via_dig_playback_pcm_cleanup
3642 },
3643 };
3644
3645 /* fill in the dac_nids table from the parsed pin configuration */
3646 static int vt1702_auto_fill_dac_nids(struct via_spec *spec,
3647 const struct auto_pin_cfg *cfg)
3648 {
3649 spec->multiout.num_dacs = 1;
3650 spec->multiout.dac_nids = spec->private_dac_nids;
3651
3652 if (cfg->line_out_pins[0]) {
3653 /* config dac list */
3654 spec->multiout.dac_nids[0] = 0x10;
3655 }
3656
3657 return 0;
3658 }
3659
3660 /* add playback controls from the parsed DAC table */
3661 static int vt1702_auto_create_line_out_ctls(struct via_spec *spec,
3662 const struct auto_pin_cfg *cfg)
3663 {
3664 int err;
3665
3666 if (!cfg->line_out_pins[0])
3667 return -1;
3668
3669 /* add control to mixer index 0 */
3670 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3671 "Master Front Playback Volume",
3672 HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
3673 if (err < 0)
3674 return err;
3675 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3676 "Master Front Playback Switch",
3677 HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
3678 if (err < 0)
3679 return err;
3680
3681 /* Front */
3682 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3683 "Front Playback Volume",
3684 HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT));
3685 if (err < 0)
3686 return err;
3687 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3688 "Front Playback Switch",
3689 HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_OUTPUT));
3690 if (err < 0)
3691 return err;
3692
3693 return 0;
3694 }
3695
3696 static int vt1702_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3697 {
3698 int err, i;
3699 struct hda_input_mux *imux;
3700 static const char *texts[] = { "ON", "OFF", NULL};
3701 if (!pin)
3702 return 0;
3703 spec->multiout.hp_nid = 0x1D;
3704 spec->hp_independent_mode_index = 0;
3705
3706 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3707 "Headphone Playback Volume",
3708 HDA_COMPOSE_AMP_VAL(0x1D, 3, 0, HDA_OUTPUT));
3709 if (err < 0)
3710 return err;
3711
3712 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3713 "Headphone Playback Switch",
3714 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3715 if (err < 0)
3716 return err;
3717
3718 imux = &spec->private_imux[1];
3719
3720 /* for hp mode select */
3721 i = 0;
3722 while (texts[i] != NULL) {
3723 imux->items[imux->num_items].label = texts[i];
3724 imux->items[imux->num_items].index = i;
3725 imux->num_items++;
3726 i++;
3727 }
3728
3729 spec->hp_mux = &spec->private_imux[1];
3730 return 0;
3731 }
3732
3733 /* create playback/capture controls for input pins */
3734 static int vt1702_auto_create_analog_input_ctls(struct via_spec *spec,
3735 const struct auto_pin_cfg *cfg)
3736 {
3737 static char *labels[] = {
3738 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
3739 };
3740 struct hda_input_mux *imux = &spec->private_imux[0];
3741 int i, err, idx = 0;
3742
3743 /* for internal loopback recording select */
3744 imux->items[imux->num_items].label = "Stereo Mixer";
3745 imux->items[imux->num_items].index = 3;
3746 imux->num_items++;
3747
3748 for (i = 0; i < AUTO_PIN_LAST; i++) {
3749 if (!cfg->input_pins[i])
3750 continue;
3751
3752 switch (cfg->input_pins[i]) {
3753 case 0x14: /* Mic */
3754 idx = 1;
3755 break;
3756
3757 case 0x15: /* Line In */
3758 idx = 2;
3759 break;
3760
3761 case 0x18: /* Front Mic */
3762 idx = 3;
3763 break;
3764 }
3765 err = via_new_analog_input(spec, labels[i], idx, 0x1A);
3766 if (err < 0)
3767 return err;
3768 imux->items[imux->num_items].label = labels[i];
3769 imux->items[imux->num_items].index = idx-1;
3770 imux->num_items++;
3771 }
3772 return 0;
3773 }
3774
3775 static int vt1702_parse_auto_config(struct hda_codec *codec)
3776 {
3777 struct via_spec *spec = codec->spec;
3778 int err;
3779
3780 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3781 if (err < 0)
3782 return err;
3783 err = vt1702_auto_fill_dac_nids(spec, &spec->autocfg);
3784 if (err < 0)
3785 return err;
3786 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3787 return 0; /* can't find valid BIOS pin config */
3788
3789 err = vt1702_auto_create_line_out_ctls(spec, &spec->autocfg);
3790 if (err < 0)
3791 return err;
3792 err = vt1702_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3793 if (err < 0)
3794 return err;
3795 /* limit AA path volume to 0 dB */
3796 snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
3797 (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
3798 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
3799 (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
3800 (1 << AC_AMPCAP_MUTE_SHIFT));
3801 err = vt1702_auto_create_analog_input_ctls(spec, &spec->autocfg);
3802 if (err < 0)
3803 return err;
3804
3805 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3806
3807 fill_dig_outs(codec);
3808
3809 if (spec->kctls.list)
3810 spec->mixers[spec->num_mixers++] = spec->kctls.list;
3811
3812 spec->input_mux = &spec->private_imux[0];
3813
3814 if (spec->hp_mux)
3815 spec->mixers[spec->num_mixers++] = via_hp_mixer;
3816
3817 return 1;
3818 }
3819
3820 #ifdef CONFIG_SND_HDA_POWER_SAVE
3821 static struct hda_amp_list vt1702_loopbacks[] = {
3822 { 0x1A, HDA_INPUT, 1 },
3823 { 0x1A, HDA_INPUT, 2 },
3824 { 0x1A, HDA_INPUT, 3 },
3825 { 0x1A, HDA_INPUT, 4 },
3826 { } /* end */
3827 };
3828 #endif
3829
3830 static int patch_vt1702(struct hda_codec *codec)
3831 {
3832 struct via_spec *spec;
3833 int err;
3834 unsigned int response;
3835 unsigned char control;
3836
3837 /* create a codec specific record */
3838 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3839 if (spec == NULL)
3840 return -ENOMEM;
3841
3842 codec->spec = spec;
3843
3844 /* automatic parse from the BIOS config */
3845 err = vt1702_parse_auto_config(codec);
3846 if (err < 0) {
3847 via_free(codec);
3848 return err;
3849 } else if (!err) {
3850 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3851 "from BIOS. Using genenic mode...\n");
3852 }
3853
3854 spec->init_verbs[spec->num_iverbs++] = vt1702_volume_init_verbs;
3855 spec->init_verbs[spec->num_iverbs++] = vt1702_uniwill_init_verbs;
3856
3857 spec->stream_name_analog = "VT1702 Analog";
3858 spec->stream_analog_playback = &vt1702_pcm_analog_playback;
3859 spec->stream_analog_capture = &vt1702_pcm_analog_capture;
3860
3861 spec->stream_name_digital = "VT1702 Digital";
3862 spec->stream_digital_playback = &vt1702_pcm_digital_playback;
3863
3864 if (!spec->adc_nids && spec->input_mux) {
3865 spec->adc_nids = vt1702_adc_nids;
3866 spec->num_adc_nids = ARRAY_SIZE(vt1702_adc_nids);
3867 get_mux_nids(codec);
3868 spec->mixers[spec->num_mixers] = vt1702_capture_mixer;
3869 spec->num_mixers++;
3870 }
3871
3872 codec->patch_ops = via_patch_ops;
3873
3874 codec->patch_ops.init = via_auto_init;
3875 codec->patch_ops.unsol_event = via_unsol_event;
3876 #ifdef CONFIG_SND_HDA_POWER_SAVE
3877 spec->loopback.amplist = vt1702_loopbacks;
3878 #endif
3879
3880 /* Open backdoor */
3881 response = snd_hda_codec_read(codec, codec->afg, 0, 0xF8C, 0);
3882 control = (unsigned char)(response & 0xff);
3883 control |= 0x3;
3884 snd_hda_codec_write(codec, codec->afg, 0, 0xF88, control);
3885
3886 /* Enable GPIO 0&1 for volume&mute control */
3887 /* Enable GPIO 2 for DMIC-DATA */
3888 response = snd_hda_codec_read(codec, codec->afg, 0, 0xF84, 0);
3889 control = (unsigned char)((response >> 16) & 0x3f);
3890 snd_hda_codec_write(codec, codec->afg, 0, 0xF82, control);
3891
3892 return 0;
3893 }
3894
3895 /*
3896 * patch entries
3897 */
3898 static struct hda_codec_preset snd_hda_preset_via[] = {
3899 { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
3900 { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
3901 { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
3902 { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
3903 { .id = 0x1106e710, .name = "VT1709 10-Ch",
3904 .patch = patch_vt1709_10ch},
3905 { .id = 0x1106e711, .name = "VT1709 10-Ch",
3906 .patch = patch_vt1709_10ch},
3907 { .id = 0x1106e712, .name = "VT1709 10-Ch",
3908 .patch = patch_vt1709_10ch},
3909 { .id = 0x1106e713, .name = "VT1709 10-Ch",
3910 .patch = patch_vt1709_10ch},
3911 { .id = 0x1106e714, .name = "VT1709 6-Ch",
3912 .patch = patch_vt1709_6ch},
3913 { .id = 0x1106e715, .name = "VT1709 6-Ch",
3914 .patch = patch_vt1709_6ch},
3915 { .id = 0x1106e716, .name = "VT1709 6-Ch",
3916 .patch = patch_vt1709_6ch},
3917 { .id = 0x1106e717, .name = "VT1709 6-Ch",
3918 .patch = patch_vt1709_6ch},
3919 { .id = 0x1106e720, .name = "VT1708B 8-Ch",
3920 .patch = patch_vt1708B_8ch},
3921 { .id = 0x1106e721, .name = "VT1708B 8-Ch",
3922 .patch = patch_vt1708B_8ch},
3923 { .id = 0x1106e722, .name = "VT1708B 8-Ch",
3924 .patch = patch_vt1708B_8ch},
3925 { .id = 0x1106e723, .name = "VT1708B 8-Ch",
3926 .patch = patch_vt1708B_8ch},
3927 { .id = 0x1106e724, .name = "VT1708B 4-Ch",
3928 .patch = patch_vt1708B_4ch},
3929 { .id = 0x1106e725, .name = "VT1708B 4-Ch",
3930 .patch = patch_vt1708B_4ch},
3931 { .id = 0x1106e726, .name = "VT1708B 4-Ch",
3932 .patch = patch_vt1708B_4ch},
3933 { .id = 0x1106e727, .name = "VT1708B 4-Ch",
3934 .patch = patch_vt1708B_4ch},
3935 { .id = 0x11060397, .name = "VT1708S",
3936 .patch = patch_vt1708S},
3937 { .id = 0x11061397, .name = "VT1708S",
3938 .patch = patch_vt1708S},
3939 { .id = 0x11062397, .name = "VT1708S",
3940 .patch = patch_vt1708S},
3941 { .id = 0x11063397, .name = "VT1708S",
3942 .patch = patch_vt1708S},
3943 { .id = 0x11064397, .name = "VT1708S",
3944 .patch = patch_vt1708S},
3945 { .id = 0x11065397, .name = "VT1708S",
3946 .patch = patch_vt1708S},
3947 { .id = 0x11066397, .name = "VT1708S",
3948 .patch = patch_vt1708S},
3949 { .id = 0x11067397, .name = "VT1708S",
3950 .patch = patch_vt1708S},
3951 { .id = 0x11060398, .name = "VT1702",
3952 .patch = patch_vt1702},
3953 { .id = 0x11061398, .name = "VT1702",
3954 .patch = patch_vt1702},
3955 { .id = 0x11062398, .name = "VT1702",
3956 .patch = patch_vt1702},
3957 { .id = 0x11063398, .name = "VT1702",
3958 .patch = patch_vt1702},
3959 { .id = 0x11064398, .name = "VT1702",
3960 .patch = patch_vt1702},
3961 { .id = 0x11065398, .name = "VT1702",
3962 .patch = patch_vt1702},
3963 { .id = 0x11066398, .name = "VT1702",
3964 .patch = patch_vt1702},
3965 { .id = 0x11067398, .name = "VT1702",
3966 .patch = patch_vt1702},
3967 {} /* terminator */
3968 };
3969
3970 MODULE_ALIAS("snd-hda-codec-id:1106*");
3971
3972 static struct hda_codec_preset_list via_list = {
3973 .preset = snd_hda_preset_via,
3974 .owner = THIS_MODULE,
3975 };
3976
3977 MODULE_LICENSE("GPL");
3978 MODULE_DESCRIPTION("VIA HD-audio codec");
3979
3980 static int __init patch_via_init(void)
3981 {
3982 return snd_hda_add_codec_preset(&via_list);
3983 }
3984
3985 static void __exit patch_via_exit(void)
3986 {
3987 snd_hda_delete_codec_preset(&via_list);
3988 }
3989
3990 module_init(patch_via_init)
3991 module_exit(patch_via_exit)
This page took 0.170773 seconds and 6 git commands to generate.