patch_analog.c 161.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2 3
 * HD audio interface patch for AD1882, AD1884, AD1981HD, AD1983, AD1984,
 *   AD1986A, AD1988
L
Linus Torvalds 已提交
4
 *
5
 * Copyright (c) 2005-2007 Takashi Iwai <tiwai@suse.de>
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *  This driver is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This driver is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <linux/init.h>
#include <linux/slab.h>
#include <linux/pci.h>
25
#include <linux/module.h>
26

L
Linus Torvalds 已提交
27 28 29
#include <sound/core.h>
#include "hda_codec.h"
#include "hda_local.h"
30
#include "hda_auto_parser.h"
31
#include "hda_beep.h"
32
#include "hda_jack.h"
33
#include "hda_generic.h"
L
Linus Torvalds 已提交
34

35 36
#define ENABLE_AD_STATIC_QUIRKS

37
struct ad198x_spec {
38 39
	struct hda_gen_spec gen;

40 41 42
	/* for auto parser */
	int smux_paths[4];
	unsigned int cur_smux;
43
	hda_nid_t eapd_nid;
44

45
	unsigned int beep_amp;	/* beep amp value, set via set_beep_amp() */
46 47 48 49

#ifdef ENABLE_AD_STATIC_QUIRKS
	const struct snd_kcontrol_new *mixers[6];
	int num_mixers;
50
	const struct hda_verb *init_verbs[6];	/* initialization verbs
T
Takashi Iwai 已提交
51 52 53 54 55 56 57 58 59
						 * don't forget NULL termination!
						 */
	unsigned int num_init_verbs;

	/* playback */
	struct hda_multi_out multiout;	/* playback set-up
					 * max_channels, dacs must be set
					 * dig_out_nid and hp_nid are optional
					 */
60
	unsigned int cur_eapd;
61
	unsigned int need_dac_fix;
T
Takashi Iwai 已提交
62 63 64

	/* capture */
	unsigned int num_adc_nids;
65
	const hda_nid_t *adc_nids;
T
Takashi Iwai 已提交
66 67 68
	hda_nid_t dig_in_nid;		/* digital-in NID; optional */

	/* capture source */
69
	const struct hda_input_mux *input_mux;
70
	const hda_nid_t *capsrc_nids;
T
Takashi Iwai 已提交
71 72 73
	unsigned int cur_mux[3];

	/* channel model */
74
	const struct hda_channel_mode *channel_mode;
T
Takashi Iwai 已提交
75 76 77
	int num_channel_mode;

	/* PCM information */
78
	struct hda_pcm pcm_rec[3];	/* used in alc_build_pcms() */
T
Takashi Iwai 已提交
79

80
	unsigned int spdif_route;
81

82 83 84
	unsigned int jack_present: 1;
	unsigned int inv_jack_detect: 1;/* inverted jack-detection */
	unsigned int analog_beep: 1;	/* analog beep input present */
85
	unsigned int avoid_init_slave_vol:1;
86

87
#ifdef CONFIG_PM
88 89
	struct hda_loopback_check loopback;
#endif
90 91
	/* for virtual master */
	hda_nid_t vmaster_nid;
92 93
	const char * const *slave_vols;
	const char * const *slave_sws;
94
#endif /* ENABLE_AD_STATIC_QUIRKS */
L
Linus Torvalds 已提交
95 96
};

97
#ifdef ENABLE_AD_STATIC_QUIRKS
98 99 100
/*
 * input MUX handling (common part)
 */
101
static int ad198x_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
102 103 104 105 106 107 108
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;

	return snd_hda_input_mux_info(spec->input_mux, uinfo);
}

109
static int ad198x_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
110 111 112
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;
T
Takashi Iwai 已提交
113
	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
114

T
Takashi Iwai 已提交
115
	ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
116 117 118
	return 0;
}

119
static int ad198x_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
120 121 122
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;
T
Takashi Iwai 已提交
123
	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
124 125

	return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
126 127
				     spec->capsrc_nids[adc_idx],
				     &spec->cur_mux[adc_idx]);
128 129 130 131 132 133 134 135
}

/*
 * initialization (common callbacks)
 */
static int ad198x_init(struct hda_codec *codec)
{
	struct ad198x_spec *spec = codec->spec;
T
Takashi Iwai 已提交
136 137 138 139
	int i;

	for (i = 0; i < spec->num_init_verbs; i++)
		snd_hda_sequence_write(codec, spec->init_verbs[i]);
140 141 142
	return 0;
}

143 144 145
static const char * const ad_slave_pfxs[] = {
	"Front", "Surround", "Center", "LFE", "Side",
	"Headphone", "Mono", "Speaker", "IEC958",
146 147 148
	NULL
};

149 150
static const char * const ad1988_6stack_fp_slave_pfxs[] = {
	"Front", "Surround", "Center", "LFE", "Side", "IEC958",
151 152
	NULL
};
153
#endif /* ENABLE_AD_STATIC_QUIRKS */
154

155
#ifdef CONFIG_SND_HDA_INPUT_BEEP
156
/* additional beep mixers; the actual parameters are overwritten at build */
157
static const struct snd_kcontrol_new ad_beep_mixer[] = {
158
	HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_OUTPUT),
159
	HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_OUTPUT),
160 161 162
	{ } /* end */
};

163
static const struct snd_kcontrol_new ad_beep2_mixer[] = {
164 165 166 167 168
	HDA_CODEC_VOLUME("Digital Beep Playback Volume", 0, 0, HDA_OUTPUT),
	HDA_CODEC_MUTE_BEEP("Digital Beep Playback Switch", 0, 0, HDA_OUTPUT),
	{ } /* end */
};

169 170
#define set_beep_amp(spec, nid, idx, dir) \
	((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir)) /* mono */
171 172 173
#else
#define set_beep_amp(spec, nid, idx, dir) /* NOP */
#endif
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
#ifdef CONFIG_SND_HDA_INPUT_BEEP
static int create_beep_ctls(struct hda_codec *codec)
{
	struct ad198x_spec *spec = codec->spec;
	const struct snd_kcontrol_new *knew;

	if (!spec->beep_amp)
		return 0;

	knew = spec->analog_beep ? ad_beep2_mixer : ad_beep_mixer;
	for ( ; knew->name; knew++) {
		int err;
		struct snd_kcontrol *kctl;
		kctl = snd_ctl_new1(knew, codec);
		if (!kctl)
			return -ENOMEM;
		kctl->private_value = spec->beep_amp;
		err = snd_hda_ctl_add(codec, 0, kctl);
		if (err < 0)
			return err;
	}
	return 0;
}
#else
#define create_beep_ctls(codec)		0
#endif

202
#ifdef ENABLE_AD_STATIC_QUIRKS
203 204 205
static int ad198x_build_controls(struct hda_codec *codec)
{
	struct ad198x_spec *spec = codec->spec;
206
	struct snd_kcontrol *kctl;
T
Takashi Iwai 已提交
207
	unsigned int i;
208 209
	int err;

T
Takashi Iwai 已提交
210 211 212 213 214 215
	for (i = 0; i < spec->num_mixers; i++) {
		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
		if (err < 0)
			return err;
	}
	if (spec->multiout.dig_out_nid) {
216 217 218
		err = snd_hda_create_spdif_out_ctls(codec,
						    spec->multiout.dig_out_nid,
						    spec->multiout.dig_out_nid);
T
Takashi Iwai 已提交
219 220
		if (err < 0)
			return err;
221 222 223 224 225
		err = snd_hda_create_spdif_share_sw(codec,
						    &spec->multiout);
		if (err < 0)
			return err;
		spec->multiout.share_spdif = 1;
T
Takashi Iwai 已提交
226 227 228 229 230 231
	} 
	if (spec->dig_in_nid) {
		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
		if (err < 0)
			return err;
	}
232

233
	/* create beep controls if needed */
234 235 236
	err = create_beep_ctls(codec);
	if (err < 0)
		return err;
237

238 239
	/* if we have no master control, let's create it */
	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
240
		unsigned int vmaster_tlv[4];
241
		snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
242
					HDA_OUTPUT, vmaster_tlv);
243
		err = __snd_hda_add_vmaster(codec, "Master Playback Volume",
244
					  vmaster_tlv,
245
					  (spec->slave_vols ?
246
					   spec->slave_vols : ad_slave_pfxs),
247
					  "Playback Volume",
248
					  !spec->avoid_init_slave_vol, NULL);
249 250 251 252 253 254 255
		if (err < 0)
			return err;
	}
	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
		err = snd_hda_add_vmaster(codec, "Master Playback Switch",
					  NULL,
					  (spec->slave_sws ?
256 257
					   spec->slave_sws : ad_slave_pfxs),
					  "Playback Switch");
258 259 260 261
		if (err < 0)
			return err;
	}

262 263 264 265 266
	/* assign Capture Source enums to NID */
	kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
	if (!kctl)
		kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
	for (i = 0; kctl && i < kctl->count; i++) {
267
		err = snd_hda_add_nid(codec, kctl, i, spec->capsrc_nids[i]);
268 269 270 271 272 273 274 275 276 277 278 279 280 281
		if (err < 0)
			return err;
	}

	/* assign IEC958 enums to NID */
	kctl = snd_hda_find_mixer_ctl(codec,
			SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source");
	if (kctl) {
		err = snd_hda_add_nid(codec, kctl, 0,
				      spec->multiout.dig_out_nid);
		if (err < 0)
			return err;
	}

282 283 284
	return 0;
}

285
#ifdef CONFIG_PM
286 287 288 289 290 291 292
static int ad198x_check_power_status(struct hda_codec *codec, hda_nid_t nid)
{
	struct ad198x_spec *spec = codec->spec;
	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
}
#endif

293 294 295 296 297
/*
 * Analog playback callbacks
 */
static int ad198x_playback_pcm_open(struct hda_pcm_stream *hinfo,
				    struct hda_codec *codec,
298
				    struct snd_pcm_substream *substream)
299 300
{
	struct ad198x_spec *spec = codec->spec;
301
	return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
302
					     hinfo);
303 304 305 306 307 308
}

static int ad198x_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
				       struct hda_codec *codec,
				       unsigned int stream_tag,
				       unsigned int format,
309
				       struct snd_pcm_substream *substream)
310 311 312 313 314 315 316 317
{
	struct ad198x_spec *spec = codec->spec;
	return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
						format, substream);
}

static int ad198x_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
				       struct hda_codec *codec,
318
				       struct snd_pcm_substream *substream)
319 320 321 322 323 324 325 326 327 328
{
	struct ad198x_spec *spec = codec->spec;
	return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
}

/*
 * Digital out
 */
static int ad198x_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
					struct hda_codec *codec,
329
					struct snd_pcm_substream *substream)
330 331 332 333 334 335 336
{
	struct ad198x_spec *spec = codec->spec;
	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
}

static int ad198x_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
					 struct hda_codec *codec,
337
					 struct snd_pcm_substream *substream)
338 339 340 341 342
{
	struct ad198x_spec *spec = codec->spec;
	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
}

343 344 345 346 347 348 349 350 351 352 353
static int ad198x_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
					   struct hda_codec *codec,
					   unsigned int stream_tag,
					   unsigned int format,
					   struct snd_pcm_substream *substream)
{
	struct ad198x_spec *spec = codec->spec;
	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
					     format, substream);
}

354 355 356 357 358 359 360 361
static int ad198x_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
					   struct hda_codec *codec,
					   struct snd_pcm_substream *substream)
{
	struct ad198x_spec *spec = codec->spec;
	return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
}

362 363 364 365 366 367 368
/*
 * Analog capture
 */
static int ad198x_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
				      struct hda_codec *codec,
				      unsigned int stream_tag,
				      unsigned int format,
369
				      struct snd_pcm_substream *substream)
370 371
{
	struct ad198x_spec *spec = codec->spec;
T
Takashi Iwai 已提交
372 373
	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
				   stream_tag, 0, format);
374 375 376 377 378
	return 0;
}

static int ad198x_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
				      struct hda_codec *codec,
379
				      struct snd_pcm_substream *substream)
380 381
{
	struct ad198x_spec *spec = codec->spec;
382
	snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
383 384 385 386 387
	return 0;
}

/*
 */
388
static const struct hda_pcm_stream ad198x_pcm_analog_playback = {
389 390
	.substreams = 1,
	.channels_min = 2,
T
Takashi Iwai 已提交
391
	.channels_max = 6, /* changed later */
392 393 394 395
	.nid = 0, /* fill later */
	.ops = {
		.open = ad198x_playback_pcm_open,
		.prepare = ad198x_playback_pcm_prepare,
396
		.cleanup = ad198x_playback_pcm_cleanup,
397 398 399
	},
};

400
static const struct hda_pcm_stream ad198x_pcm_analog_capture = {
T
Takashi Iwai 已提交
401
	.substreams = 1,
402 403 404 405 406 407 408 409 410
	.channels_min = 2,
	.channels_max = 2,
	.nid = 0, /* fill later */
	.ops = {
		.prepare = ad198x_capture_pcm_prepare,
		.cleanup = ad198x_capture_pcm_cleanup
	},
};

411
static const struct hda_pcm_stream ad198x_pcm_digital_playback = {
412 413 414 415 416 417
	.substreams = 1,
	.channels_min = 2,
	.channels_max = 2,
	.nid = 0, /* fill later */
	.ops = {
		.open = ad198x_dig_playback_pcm_open,
418
		.close = ad198x_dig_playback_pcm_close,
419 420
		.prepare = ad198x_dig_playback_pcm_prepare,
		.cleanup = ad198x_dig_playback_pcm_cleanup
421 422 423
	},
};

424
static const struct hda_pcm_stream ad198x_pcm_digital_capture = {
T
Takashi Iwai 已提交
425 426 427 428 429 430
	.substreams = 1,
	.channels_min = 2,
	.channels_max = 2,
	/* NID is set in alc_build_pcms */
};

431 432 433 434 435 436 437 438 439 440 441 442 443
static int ad198x_build_pcms(struct hda_codec *codec)
{
	struct ad198x_spec *spec = codec->spec;
	struct hda_pcm *info = spec->pcm_rec;

	codec->num_pcms = 1;
	codec->pcm_info = info;

	info->name = "AD198x Analog";
	info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ad198x_pcm_analog_playback;
	info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->multiout.max_channels;
	info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
	info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad198x_pcm_analog_capture;
T
Takashi Iwai 已提交
444 445
	info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adc_nids;
	info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
446 447 448 449

	if (spec->multiout.dig_out_nid) {
		info++;
		codec->num_pcms++;
450
		codec->spdif_status_reset = 1;
451
		info->name = "AD198x Digital";
452
		info->pcm_type = HDA_PCM_TYPE_SPDIF;
453 454
		info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ad198x_pcm_digital_playback;
		info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
T
Takashi Iwai 已提交
455 456 457 458
		if (spec->dig_in_nid) {
			info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad198x_pcm_digital_capture;
			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
		}
459 460 461 462
	}

	return 0;
}
463
#endif /* ENABLE_AD_STATIC_QUIRKS */
464

465 466 467
static void ad198x_power_eapd_write(struct hda_codec *codec, hda_nid_t front,
				hda_nid_t hp)
{
468 469
	if (snd_hda_query_pin_caps(codec, front) & AC_PINCAP_EAPD)
		snd_hda_codec_write(codec, front, 0, AC_VERB_SET_EAPD_BTLENABLE,
470
			    !codec->inv_eapd ? 0x00 : 0x02);
471 472
	if (snd_hda_query_pin_caps(codec, hp) & AC_PINCAP_EAPD)
		snd_hda_codec_write(codec, hp, 0, AC_VERB_SET_EAPD_BTLENABLE,
473
			    !codec->inv_eapd ? 0x00 : 0x02);
474 475 476 477 478 479 480 481 482 483 484 485 486 487
}

static void ad198x_power_eapd(struct hda_codec *codec)
{
	/* We currently only handle front, HP */
	switch (codec->vendor_id) {
	case 0x11d41882:
	case 0x11d4882a:
	case 0x11d41884:
	case 0x11d41984:
	case 0x11d41883:
	case 0x11d4184a:
	case 0x11d4194a:
	case 0x11d4194b:
488 489 490 491
	case 0x11d41988:
	case 0x11d4198b:
	case 0x11d4989a:
	case 0x11d4989b:
492 493 494 495 496 497 498 499 500 501 502 503
		ad198x_power_eapd_write(codec, 0x12, 0x11);
		break;
	case 0x11d41981:
	case 0x11d41983:
		ad198x_power_eapd_write(codec, 0x05, 0x06);
		break;
	case 0x11d41986:
		ad198x_power_eapd_write(codec, 0x1b, 0x1a);
		break;
	}
}

504 505 506 507 508 509
static void ad198x_shutup(struct hda_codec *codec)
{
	snd_hda_shutup_pins(codec);
	ad198x_power_eapd(codec);
}

510 511 512 513 514 515 516
static void ad198x_free(struct hda_codec *codec)
{
	struct ad198x_spec *spec = codec->spec;

	if (!spec)
		return;

517
	snd_hda_gen_spec_free(&spec->gen);
518 519
	kfree(spec);
	snd_hda_detach_beep_device(codec);
520 521
}

522
#ifdef CONFIG_PM
523
static int ad198x_suspend(struct hda_codec *codec)
524 525 526 527 528 529
{
	ad198x_shutup(codec);
	return 0;
}
#endif

530
#ifdef ENABLE_AD_STATIC_QUIRKS
531
static const struct hda_codec_ops ad198x_patch_ops = {
532 533 534 535
	.build_controls = ad198x_build_controls,
	.build_pcms = ad198x_build_pcms,
	.init = ad198x_init,
	.free = ad198x_free,
536
#ifdef CONFIG_PM
537
	.check_power_status = ad198x_check_power_status,
538 539 540
	.suspend = ad198x_suspend,
#endif
	.reboot_notify = ad198x_shutup,
541 542 543
};


544 545
/*
 * EAPD control
546
 * the private value = nid
547
 */
548
#define ad198x_eapd_info	snd_ctl_boolean_mono_info
549 550 551 552 553 554

static int ad198x_eapd_get(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;
555
	if (codec->inv_eapd)
556 557 558 559 560 561 562 563 564 565 566 567 568
		ucontrol->value.integer.value[0] = ! spec->cur_eapd;
	else
		ucontrol->value.integer.value[0] = spec->cur_eapd;
	return 0;
}

static int ad198x_eapd_put(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;
	hda_nid_t nid = kcontrol->private_value & 0xff;
	unsigned int eapd;
569
	eapd = !!ucontrol->value.integer.value[0];
570
	if (codec->inv_eapd)
571
		eapd = !eapd;
572
	if (eapd == spec->cur_eapd)
573 574
		return 0;
	spec->cur_eapd = eapd;
575 576 577
	snd_hda_codec_write_cache(codec, nid,
				  0, AC_VERB_SET_EAPD_BTLENABLE,
				  eapd ? 0x02 : 0x00);
578 579 580
	return 1;
}

581 582 583 584 585 586
static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_info *uinfo);
static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol);
static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol);
587
#endif /* ENABLE_AD_STATIC_QUIRKS */
588 589


590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
/*
 * Automatic parse of I/O pins from the BIOS configuration
 */

static int ad198x_auto_build_controls(struct hda_codec *codec)
{
	int err;

	err = snd_hda_gen_build_controls(codec);
	if (err < 0)
		return err;
	err = create_beep_ctls(codec);
	if (err < 0)
		return err;
	return 0;
}

static const struct hda_codec_ops ad198x_auto_patch_ops = {
	.build_controls = ad198x_auto_build_controls,
	.build_pcms = snd_hda_gen_build_pcms,
	.init = snd_hda_gen_init,
611
	.free = snd_hda_gen_free,
612
	.unsol_event = snd_hda_jack_unsol_event,
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
#ifdef CONFIG_PM
	.check_power_status = snd_hda_gen_check_power_status,
	.suspend = ad198x_suspend,
#endif
	.reboot_notify = ad198x_shutup,
};


static int ad198x_parse_auto_config(struct hda_codec *codec)
{
	struct ad198x_spec *spec = codec->spec;
	struct auto_pin_cfg *cfg = &spec->gen.autocfg;
	int err;

	codec->spdif_status_reset = 1;
	codec->no_trigger_sense = 1;
	codec->no_sticky_stream = 1;

	spec->gen.indep_hp = 1;

	err = snd_hda_parse_pin_defcfg(codec, cfg, NULL, 0);
	if (err < 0)
		return err;
	err = snd_hda_gen_parse_auto_config(codec, cfg);
	if (err < 0)
		return err;

	codec->patch_ops = ad198x_auto_patch_ops;

	return 0;
}

645 646 647 648
/*
 * AD1986A specific
 */

649
#ifdef ENABLE_AD_STATIC_QUIRKS
L
Linus Torvalds 已提交
650 651 652 653 654 655
#define AD1986A_SPDIF_OUT	0x02
#define AD1986A_FRONT_DAC	0x03
#define AD1986A_SURR_DAC	0x04
#define AD1986A_CLFE_DAC	0x05
#define AD1986A_ADC		0x06

656
static const hda_nid_t ad1986a_dac_nids[3] = {
L
Linus Torvalds 已提交
657 658
	AD1986A_FRONT_DAC, AD1986A_SURR_DAC, AD1986A_CLFE_DAC
};
659 660
static const hda_nid_t ad1986a_adc_nids[1] = { AD1986A_ADC };
static const hda_nid_t ad1986a_capsrc_nids[1] = { 0x12 };
L
Linus Torvalds 已提交
661

662
static const struct hda_input_mux ad1986a_capture_source = {
L
Linus Torvalds 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675
	.num_items = 7,
	.items = {
		{ "Mic", 0x0 },
		{ "CD", 0x1 },
		{ "Aux", 0x3 },
		{ "Line", 0x4 },
		{ "Mix", 0x5 },
		{ "Mono", 0x6 },
		{ "Phone", 0x7 },
	},
};


676
static const struct hda_bind_ctls ad1986a_bind_pcm_vol = {
677 678 679 680 681 682 683 684
	.ops = &snd_hda_bind_vol,
	.values = {
		HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT),
		HDA_COMPOSE_AMP_VAL(AD1986A_SURR_DAC, 3, 0, HDA_OUTPUT),
		HDA_COMPOSE_AMP_VAL(AD1986A_CLFE_DAC, 3, 0, HDA_OUTPUT),
		0
	},
};
L
Linus Torvalds 已提交
685

686
static const struct hda_bind_ctls ad1986a_bind_pcm_sw = {
687 688 689 690 691 692 693 694
	.ops = &snd_hda_bind_sw,
	.values = {
		HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT),
		HDA_COMPOSE_AMP_VAL(AD1986A_SURR_DAC, 3, 0, HDA_OUTPUT),
		HDA_COMPOSE_AMP_VAL(AD1986A_CLFE_DAC, 3, 0, HDA_OUTPUT),
		0
	},
};
L
Linus Torvalds 已提交
695 696 697 698

/*
 * mixers
 */
699
static const struct snd_kcontrol_new ad1986a_mixers[] = {
700 701 702 703 704
	/*
	 * bind volumes/mutes of 3 DACs as a single PCM control for simplicity
	 */
	HDA_BIND_VOL("PCM Playback Volume", &ad1986a_bind_pcm_vol),
	HDA_BIND_SW("PCM Playback Switch", &ad1986a_bind_pcm_sw),
L
Linus Torvalds 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	HDA_CODEC_VOLUME("Front Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Front Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Surround Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Surround Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x1d, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x1d, 2, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x1d, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x1d, 2, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Headphone Playback Volume", 0x1a, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Headphone Playback Switch", 0x1a, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
723
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x0f, 0x0, HDA_OUTPUT),
L
Linus Torvalds 已提交
724 725 726 727 728 729 730
	HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Capture Source",
731 732 733
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
L
Linus Torvalds 已提交
734 735 736 737 738
	},
	HDA_CODEC_MUTE("Stereo Downmix Switch", 0x09, 0x0, HDA_OUTPUT),
	{ } /* end */
};

739
/* additional mixers for 3stack mode */
740
static const struct snd_kcontrol_new ad1986a_3st_mixers[] = {
741 742 743 744 745 746 747 748 749 750 751
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Channel Mode",
		.info = ad198x_ch_mode_info,
		.get = ad198x_ch_mode_get,
		.put = ad198x_ch_mode_put,
	},
	{ } /* end */
};

/* laptop model - 2ch only */
752
static const hda_nid_t ad1986a_laptop_dac_nids[1] = { AD1986A_FRONT_DAC };
753

754
/* master controls both pins 0x1a and 0x1b */
755
static const struct hda_bind_ctls ad1986a_laptop_master_vol = {
756 757 758 759 760 761 762 763
	.ops = &snd_hda_bind_vol,
	.values = {
		HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
		HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
		0,
	},
};

764
static const struct hda_bind_ctls ad1986a_laptop_master_sw = {
765 766 767 768 769 770 771 772
	.ops = &snd_hda_bind_sw,
	.values = {
		HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
		HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
		0,
	},
};

773
static const struct snd_kcontrol_new ad1986a_laptop_mixers[] = {
774 775
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
776 777
	HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
	HDA_BIND_SW("Master Playback Switch", &ad1986a_laptop_master_sw),
778 779 780 781 782 783 784 785
	HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
786
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x0f, 0x0, HDA_OUTPUT),
787
	/* 
788 789 790 791 792 793 794 795 796 797 798 799 800 801
	   HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
	   HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT), */
	HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Capture Source",
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
	},
	{ } /* end */
};

802 803
/* laptop-eapd model - 2ch only */

804
static const struct hda_input_mux ad1986a_laptop_eapd_capture_source = {
805 806 807 808 809 810 811 812
	.num_items = 3,
	.items = {
		{ "Mic", 0x0 },
		{ "Internal Mic", 0x4 },
		{ "Mix", 0x5 },
	},
};

813
static const struct hda_input_mux ad1986a_automic_capture_source = {
814 815 816 817 818 819 820
	.num_items = 2,
	.items = {
		{ "Mic", 0x0 },
		{ "Mix", 0x5 },
	},
};

821
static const struct snd_kcontrol_new ad1986a_laptop_master_mixers[] = {
822 823 824 825 826
	HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
	HDA_BIND_SW("Master Playback Switch", &ad1986a_laptop_master_sw),
	{ } /* end */
};

827
static const struct snd_kcontrol_new ad1986a_laptop_eapd_mixers[] = {
828 829 830 831
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
832
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x0f, 0x0, HDA_OUTPUT),
833 834 835 836 837 838 839 840 841 842 843 844
	HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Capture Source",
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "External Amplifier",
845
		.subdevice = HDA_SUBDEV_NID_FLAG | 0x1b,
846 847 848
		.info = ad198x_eapd_info,
		.get = ad198x_eapd_get,
		.put = ad198x_eapd_put,
849
		.private_value = 0x1b, /* port-D */
850 851 852 853
	},
	{ } /* end */
};

854
static const struct snd_kcontrol_new ad1986a_laptop_intmic_mixers[] = {
855 856 857 858 859
	HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0, HDA_OUTPUT),
	{ } /* end */
};

860 861 862 863
/* re-connect the mic boost input according to the jack sensing */
static void ad1986a_automic(struct hda_codec *codec)
{
	unsigned int present;
864
	present = snd_hda_jack_detect(codec, 0x1f);
865 866
	/* 0 = 0x1f, 2 = 0x1d, 4 = mixed */
	snd_hda_codec_write(codec, 0x0f, 0, AC_VERB_SET_CONNECT_SEL,
867
			    present ? 0 : 2);
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
}

#define AD1986A_MIC_EVENT		0x36

static void ad1986a_automic_unsol_event(struct hda_codec *codec,
					    unsigned int res)
{
	if ((res >> 26) != AD1986A_MIC_EVENT)
		return;
	ad1986a_automic(codec);
}

static int ad1986a_automic_init(struct hda_codec *codec)
{
	ad198x_init(codec);
	ad1986a_automic(codec);
	return 0;
}

887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
/* laptop-automute - 2ch only */

static void ad1986a_update_hp(struct hda_codec *codec)
{
	struct ad198x_spec *spec = codec->spec;
	unsigned int mute;

	if (spec->jack_present)
		mute = HDA_AMP_MUTE; /* mute internal speaker */
	else
		/* unmute internal speaker if necessary */
		mute = snd_hda_codec_amp_read(codec, 0x1a, 0, HDA_OUTPUT, 0);
	snd_hda_codec_amp_stereo(codec, 0x1b, HDA_OUTPUT, 0,
				 HDA_AMP_MUTE, mute);
}

static void ad1986a_hp_automute(struct hda_codec *codec)
{
	struct ad198x_spec *spec = codec->spec;

907
	spec->jack_present = snd_hda_jack_detect(codec, 0x1a);
908 909
	if (spec->inv_jack_detect)
		spec->jack_present = !spec->jack_present;
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
	ad1986a_update_hp(codec);
}

#define AD1986A_HP_EVENT		0x37

static void ad1986a_hp_unsol_event(struct hda_codec *codec, unsigned int res)
{
	if ((res >> 26) != AD1986A_HP_EVENT)
		return;
	ad1986a_hp_automute(codec);
}

static int ad1986a_hp_init(struct hda_codec *codec)
{
	ad198x_init(codec);
	ad1986a_hp_automute(codec);
	return 0;
}

/* bind hp and internal speaker mute (with plug check) */
static int ad1986a_hp_master_sw_put(struct snd_kcontrol *kcontrol,
				    struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
934
	int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
935 936 937 938 939
	if (change)
		ad1986a_update_hp(codec);
	return change;
}

940
static const struct snd_kcontrol_new ad1986a_automute_master_mixers[] = {
941 942 943 944
	HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Master Playback Switch",
945
		.subdevice = HDA_SUBDEV_AMP_FLAG,
946 947 948 949 950 951 952 953
		.info = snd_hda_mixer_amp_switch_info,
		.get = snd_hda_mixer_amp_switch_get,
		.put = ad1986a_hp_master_sw_put,
		.private_value = HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
	},
	{ } /* end */
};

954

L
Linus Torvalds 已提交
955 956 957
/*
 * initialization verbs
 */
958
static const struct hda_verb ad1986a_init_verbs[] = {
L
Linus Torvalds 已提交
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
	/* Front, Surround, CLFE DAC; mute as default */
	{0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	/* Downmix - off */
	{0x09, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	/* HP, Line-Out, Surround, CLFE selectors */
	{0x0a, AC_VERB_SET_CONNECT_SEL, 0x0},
	{0x0b, AC_VERB_SET_CONNECT_SEL, 0x0},
	{0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
	{0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* Mono selector */
	{0x0e, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* Mic selector: Mic 1/2 pin */
	{0x0f, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* Line-in selector: Line-in */
	{0x10, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* Mic 1/2 swap */
	{0x11, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* Record selector: mic */
	{0x12, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* Mic, Phone, CD, Aux, Line-In amp; mute as default */
	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	/* PC beep */
	{0x18, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* HP, Line-Out, Surround, CLFE, Mono pins; mute as default */
	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
	/* HP Pin */
	{0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
	/* Front, Surround, CLFE Pins */
	{0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
	{0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
	{0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
	/* Mono Pin */
	{0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
	/* Mic Pin */
	{0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
	/* Line, Aux, CD, Beep-In Pin */
	{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
	{0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
	{0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
	{0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
	{0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
L
Linus Torvalds 已提交
1010 1011 1012
	{ } /* end */
};

1013
static const struct hda_verb ad1986a_ch2_init[] = {
1014
	/* Surround out -> Line In */
1015 1016 1017
	{ 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
 	/* Line-in selectors */
	{ 0x10, AC_VERB_SET_CONNECT_SEL, 0x1 },
1018
	/* CLFE -> Mic in */
1019 1020 1021
	{ 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
	/* Mic selector, mix C/LFE (backmic) and Mic (frontmic) */
	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x4 },
1022 1023 1024
	{ } /* end */
};

1025
static const struct hda_verb ad1986a_ch4_init[] = {
1026
	/* Surround out -> Surround */
1027 1028
	{ 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
	{ 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
1029
	/* CLFE -> Mic in */
1030 1031
	{ 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x4 },
1032 1033 1034
	{ } /* end */
};

1035
static const struct hda_verb ad1986a_ch6_init[] = {
1036
	/* Surround out -> Surround out */
1037 1038
	{ 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
	{ 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
1039
	/* CLFE -> CLFE */
1040 1041
	{ 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x0 },
1042 1043 1044
	{ } /* end */
};

1045
static const struct hda_channel_mode ad1986a_modes[3] = {
1046 1047 1048 1049 1050
	{ 2, ad1986a_ch2_init },
	{ 4, ad1986a_ch4_init },
	{ 6, ad1986a_ch6_init },
};

1051
/* eapd initialization */
1052
static const struct hda_verb ad1986a_eapd_init_verbs[] = {
1053
	{0x1b, AC_VERB_SET_EAPD_BTLENABLE, 0x00 },
1054 1055 1056
	{}
};

1057
static const struct hda_verb ad1986a_automic_verbs[] = {
1058 1059 1060 1061 1062 1063 1064 1065
	{0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	/*{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},*/
	{0x0f, AC_VERB_SET_CONNECT_SEL, 0x0},
	{0x1f, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1986A_MIC_EVENT},
	{}
};

1066
/* Ultra initialization */
1067
static const struct hda_verb ad1986a_ultra_init[] = {
1068 1069 1070 1071 1072 1073 1074 1075 1076
	/* eapd initialization */
	{ 0x1b, AC_VERB_SET_EAPD_BTLENABLE, 0x00 },
	/* CLFE -> Mic in */
	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x2 },
	{ 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
	{ 0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080 },
	{ } /* end */
};

1077
/* pin sensing on HP jack */
1078
static const struct hda_verb ad1986a_hp_init_verbs[] = {
1079 1080 1081 1082
	{0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1986A_HP_EVENT},
	{}
};

1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
static void ad1986a_samsung_p50_unsol_event(struct hda_codec *codec,
					    unsigned int res)
{
	switch (res >> 26) {
	case AD1986A_HP_EVENT:
		ad1986a_hp_automute(codec);
		break;
	case AD1986A_MIC_EVENT:
		ad1986a_automic(codec);
		break;
	}
}

static int ad1986a_samsung_p50_init(struct hda_codec *codec)
{
	ad198x_init(codec);
	ad1986a_hp_automute(codec);
	ad1986a_automic(codec);
	return 0;
}

1104

1105
/* models */
1106
enum {
1107
	AD1986A_AUTO,
1108 1109 1110 1111
	AD1986A_6STACK,
	AD1986A_3STACK,
	AD1986A_LAPTOP,
	AD1986A_LAPTOP_EAPD,
1112
	AD1986A_LAPTOP_AUTOMUTE,
1113
	AD1986A_ULTRA,
1114
	AD1986A_SAMSUNG,
1115
	AD1986A_SAMSUNG_P50,
1116 1117 1118
	AD1986A_MODELS
};

1119
static const char * const ad1986a_models[AD1986A_MODELS] = {
1120
	[AD1986A_AUTO]		= "auto",
1121 1122 1123 1124
	[AD1986A_6STACK]	= "6stack",
	[AD1986A_3STACK]	= "3stack",
	[AD1986A_LAPTOP]	= "laptop",
	[AD1986A_LAPTOP_EAPD]	= "laptop-eapd",
1125
	[AD1986A_LAPTOP_AUTOMUTE] = "laptop-automute",
1126
	[AD1986A_ULTRA]		= "ultra",
1127
	[AD1986A_SAMSUNG]	= "samsung",
1128
	[AD1986A_SAMSUNG_P50]	= "samsung-p50",
1129 1130
};

1131
static const struct snd_pci_quirk ad1986a_cfg_tbl[] = {
1132 1133 1134
	SND_PCI_QUIRK(0x103c, 0x30af, "HP B2800", AD1986A_LAPTOP_EAPD),
	SND_PCI_QUIRK(0x1043, 0x1153, "ASUS M9", AD1986A_LAPTOP_EAPD),
	SND_PCI_QUIRK(0x1043, 0x11f7, "ASUS U5A", AD1986A_LAPTOP_EAPD),
1135
	SND_PCI_QUIRK(0x1043, 0x1213, "ASUS A6J", AD1986A_LAPTOP_EAPD),
1136 1137 1138 1139
	SND_PCI_QUIRK(0x1043, 0x1263, "ASUS U5F", AD1986A_LAPTOP_EAPD),
	SND_PCI_QUIRK(0x1043, 0x1297, "ASUS Z62F", AD1986A_LAPTOP_EAPD),
	SND_PCI_QUIRK(0x1043, 0x12b3, "ASUS V1j", AD1986A_LAPTOP_EAPD),
	SND_PCI_QUIRK(0x1043, 0x1302, "ASUS W3j", AD1986A_LAPTOP_EAPD),
T
Tobin Davis 已提交
1140
	SND_PCI_QUIRK(0x1043, 0x1443, "ASUS VX1", AD1986A_LAPTOP),
1141
	SND_PCI_QUIRK(0x1043, 0x1447, "ASUS A8J", AD1986A_3STACK),
1142 1143 1144 1145 1146
	SND_PCI_QUIRK(0x1043, 0x817f, "ASUS P5", AD1986A_3STACK),
	SND_PCI_QUIRK(0x1043, 0x818f, "ASUS P5", AD1986A_LAPTOP),
	SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS P5", AD1986A_3STACK),
	SND_PCI_QUIRK(0x1043, 0x81cb, "ASUS M2N", AD1986A_3STACK),
	SND_PCI_QUIRK(0x1043, 0x8234, "ASUS M2N", AD1986A_3STACK),
1147
	SND_PCI_QUIRK(0x10de, 0xcb84, "ASUS A8N-VM", AD1986A_3STACK),
1148
	SND_PCI_QUIRK(0x1179, 0xff40, "Toshiba Satellite L40-10Q", AD1986A_3STACK),
1149
	SND_PCI_QUIRK(0x144d, 0xb03c, "Samsung R55", AD1986A_3STACK),
1150
	SND_PCI_QUIRK(0x144d, 0xc01e, "FSC V2060", AD1986A_LAPTOP),
1151
	SND_PCI_QUIRK(0x144d, 0xc024, "Samsung P50", AD1986A_SAMSUNG_P50),
1152
	SND_PCI_QUIRK(0x144d, 0xc027, "Samsung Q1", AD1986A_ULTRA),
T
Takashi Iwai 已提交
1153
	SND_PCI_QUIRK_MASK(0x144d, 0xff00, 0xc000, "Samsung", AD1986A_SAMSUNG),
1154
	SND_PCI_QUIRK(0x144d, 0xc504, "Samsung Q35", AD1986A_3STACK),
1155
	SND_PCI_QUIRK(0x17aa, 0x1011, "Lenovo M55", AD1986A_LAPTOP),
1156
	SND_PCI_QUIRK(0x17aa, 0x1017, "Lenovo A60", AD1986A_3STACK),
1157
	SND_PCI_QUIRK(0x17aa, 0x2066, "Lenovo N100", AD1986A_LAPTOP_AUTOMUTE),
1158
	SND_PCI_QUIRK(0x17c0, 0x2017, "Samsung M50", AD1986A_LAPTOP),
1159 1160
	{}
};
L
Linus Torvalds 已提交
1161

1162
#ifdef CONFIG_PM
1163
static const struct hda_amp_list ad1986a_loopbacks[] = {
1164 1165 1166 1167 1168 1169 1170 1171 1172
	{ 0x13, HDA_OUTPUT, 0 }, /* Mic */
	{ 0x14, HDA_OUTPUT, 0 }, /* Phone */
	{ 0x15, HDA_OUTPUT, 0 }, /* CD */
	{ 0x16, HDA_OUTPUT, 0 }, /* Aux */
	{ 0x17, HDA_OUTPUT, 0 }, /* Line */
	{ } /* end */
};
#endif

1173 1174
static int is_jack_available(struct hda_codec *codec, hda_nid_t nid)
{
1175
	unsigned int conf = snd_hda_codec_get_pincfg(codec, nid);
1176 1177
	return get_defcfg_connect(conf) != AC_JACK_PORT_NONE;
}
1178
#endif /* ENABLE_AD_STATIC_QUIRKS */
1179

1180
static int alloc_ad_spec(struct hda_codec *codec)
L
Linus Torvalds 已提交
1181
{
1182
	struct ad198x_spec *spec;
L
Linus Torvalds 已提交
1183

1184
	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1185
	if (!spec)
1186 1187
		return -ENOMEM;
	codec->spec = spec;
1188
	snd_hda_gen_spec_init(&spec->gen);
1189 1190 1191
	return 0;
}

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
/*
 * AD1986A fixup codes
 */

/* Lenovo N100 seems to report the reversed bit for HP jack-sensing */
static void ad_fixup_inv_jack_detect(struct hda_codec *codec,
				     const struct hda_fixup *fix, int action)
{
	if (action == HDA_FIXUP_ACT_PRE_PROBE)
		codec->inv_jack_detect = 1;
}

enum {
	AD1986A_FIXUP_INV_JACK_DETECT,
};

static const struct hda_fixup ad1986a_fixups[] = {
	[AD1986A_FIXUP_INV_JACK_DETECT] = {
		.type = HDA_FIXUP_FUNC,
		.v.func = ad_fixup_inv_jack_detect,
	},
};

static const struct snd_pci_quirk ad1986a_fixup_tbl[] = {
	SND_PCI_QUIRK(0x17aa, 0x2066, "Lenovo N100", AD1986A_FIXUP_INV_JACK_DETECT),
	{}
};

1220 1221 1222 1223
/*
 */
static int ad1986a_parse_auto_config(struct hda_codec *codec)
{
1224 1225 1226 1227 1228 1229 1230
	int err;
	struct ad198x_spec *spec;

	err = alloc_ad_spec(codec);
	if (err < 0)
		return err;
	spec = codec->spec;
1231 1232 1233 1234

	/* AD1986A has the inverted EAPD implementation */
	codec->inv_eapd = 1;

1235
	spec->gen.mixer_nid = 0x07;
1236
	spec->gen.beep_nid = 0x19;
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
	set_beep_amp(spec, 0x18, 0, HDA_OUTPUT);

	/* AD1986A has a hardware problem that it can't share a stream
	 * with multiple output pins.  The copy of front to surrounds
	 * causes noisy or silent outputs at a certain timing, e.g.
	 * changing the volume.
	 * So, let's disable the shared stream.
	 */
	spec->gen.multiout.no_share_stream = 1;

1247 1248 1249
	snd_hda_pick_fixup(codec, NULL, ad1986a_fixup_tbl, ad1986a_fixups);
	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);

1250 1251
	err = ad198x_parse_auto_config(codec);
	if (err < 0) {
1252
		snd_hda_gen_free(codec);
1253 1254 1255
		return err;
	}

1256 1257
	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);

1258
	return 0;
1259 1260
}

1261
#ifdef ENABLE_AD_STATIC_QUIRKS
1262 1263 1264 1265 1266
static int patch_ad1986a(struct hda_codec *codec)
{
	struct ad198x_spec *spec;
	int err, board_config;

1267 1268 1269
	board_config = snd_hda_check_board_config(codec, AD1986A_MODELS,
						  ad1986a_models,
						  ad1986a_cfg_tbl);
1270 1271 1272 1273 1274 1275
	if (board_config < 0) {
		printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
		       codec->chip_name);
		board_config = AD1986A_AUTO;
	}

1276 1277 1278
	if (board_config == AD1986A_AUTO)
		return ad1986a_parse_auto_config(codec);

1279 1280 1281 1282
	err = alloc_ad_spec(codec);
	if (err < 0)
		return err;
	spec = codec->spec;
1283

1284 1285 1286 1287 1288 1289 1290
	err = snd_hda_attach_beep_device(codec, 0x19);
	if (err < 0) {
		ad198x_free(codec);
		return err;
	}
	set_beep_amp(spec, 0x18, 0, HDA_OUTPUT);

1291 1292 1293 1294
	spec->multiout.max_channels = 6;
	spec->multiout.num_dacs = ARRAY_SIZE(ad1986a_dac_nids);
	spec->multiout.dac_nids = ad1986a_dac_nids;
	spec->multiout.dig_out_nid = AD1986A_SPDIF_OUT;
T
Takashi Iwai 已提交
1295 1296
	spec->num_adc_nids = 1;
	spec->adc_nids = ad1986a_adc_nids;
T
Takashi Iwai 已提交
1297
	spec->capsrc_nids = ad1986a_capsrc_nids;
1298
	spec->input_mux = &ad1986a_capture_source;
T
Takashi Iwai 已提交
1299 1300 1301 1302
	spec->num_mixers = 1;
	spec->mixers[0] = ad1986a_mixers;
	spec->num_init_verbs = 1;
	spec->init_verbs[0] = ad1986a_init_verbs;
1303
#ifdef CONFIG_PM
1304 1305
	spec->loopback.amplist = ad1986a_loopbacks;
#endif
1306
	spec->vmaster_nid = 0x1b;
1307
	codec->inv_eapd = 1; /* AD1986A has the inverted EAPD implementation */
1308 1309

	codec->patch_ops = ad198x_patch_ops;
L
Linus Torvalds 已提交
1310

1311 1312 1313 1314 1315
	/* override some parameters */
	switch (board_config) {
	case AD1986A_3STACK:
		spec->num_mixers = 2;
		spec->mixers[1] = ad1986a_3st_mixers;
1316 1317
		spec->num_init_verbs = 2;
		spec->init_verbs[1] = ad1986a_ch2_init;
1318 1319
		spec->channel_mode = ad1986a_modes;
		spec->num_channel_mode = ARRAY_SIZE(ad1986a_modes);
1320 1321 1322
		spec->need_dac_fix = 1;
		spec->multiout.max_channels = 2;
		spec->multiout.num_dacs = 1;
1323 1324 1325 1326 1327 1328 1329
		break;
	case AD1986A_LAPTOP:
		spec->mixers[0] = ad1986a_laptop_mixers;
		spec->multiout.max_channels = 2;
		spec->multiout.num_dacs = 1;
		spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
		break;
1330
	case AD1986A_LAPTOP_EAPD:
1331 1332 1333 1334
		spec->num_mixers = 3;
		spec->mixers[0] = ad1986a_laptop_master_mixers;
		spec->mixers[1] = ad1986a_laptop_eapd_mixers;
		spec->mixers[2] = ad1986a_laptop_intmic_mixers;
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
		spec->num_init_verbs = 2;
		spec->init_verbs[1] = ad1986a_eapd_init_verbs;
		spec->multiout.max_channels = 2;
		spec->multiout.num_dacs = 1;
		spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
		if (!is_jack_available(codec, 0x25))
			spec->multiout.dig_out_nid = 0;
		spec->input_mux = &ad1986a_laptop_eapd_capture_source;
		break;
	case AD1986A_SAMSUNG:
1345 1346 1347
		spec->num_mixers = 2;
		spec->mixers[0] = ad1986a_laptop_master_mixers;
		spec->mixers[1] = ad1986a_laptop_eapd_mixers;
1348
		spec->num_init_verbs = 3;
1349
		spec->init_verbs[1] = ad1986a_eapd_init_verbs;
1350
		spec->init_verbs[2] = ad1986a_automic_verbs;
1351 1352 1353
		spec->multiout.max_channels = 2;
		spec->multiout.num_dacs = 1;
		spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1354 1355
		if (!is_jack_available(codec, 0x25))
			spec->multiout.dig_out_nid = 0;
1356 1357 1358
		spec->input_mux = &ad1986a_automic_capture_source;
		codec->patch_ops.unsol_event = ad1986a_automic_unsol_event;
		codec->patch_ops.init = ad1986a_automic_init;
1359
		break;
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
	case AD1986A_SAMSUNG_P50:
		spec->num_mixers = 2;
		spec->mixers[0] = ad1986a_automute_master_mixers;
		spec->mixers[1] = ad1986a_laptop_eapd_mixers;
		spec->num_init_verbs = 4;
		spec->init_verbs[1] = ad1986a_eapd_init_verbs;
		spec->init_verbs[2] = ad1986a_automic_verbs;
		spec->init_verbs[3] = ad1986a_hp_init_verbs;
		spec->multiout.max_channels = 2;
		spec->multiout.num_dacs = 1;
		spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
		if (!is_jack_available(codec, 0x25))
			spec->multiout.dig_out_nid = 0;
		spec->input_mux = &ad1986a_automic_capture_source;
		codec->patch_ops.unsol_event = ad1986a_samsung_p50_unsol_event;
		codec->patch_ops.init = ad1986a_samsung_p50_init;
		break;
1377
	case AD1986A_LAPTOP_AUTOMUTE:
1378 1379 1380 1381
		spec->num_mixers = 3;
		spec->mixers[0] = ad1986a_automute_master_mixers;
		spec->mixers[1] = ad1986a_laptop_eapd_mixers;
		spec->mixers[2] = ad1986a_laptop_intmic_mixers;
1382 1383 1384 1385 1386 1387
		spec->num_init_verbs = 3;
		spec->init_verbs[1] = ad1986a_eapd_init_verbs;
		spec->init_verbs[2] = ad1986a_hp_init_verbs;
		spec->multiout.max_channels = 2;
		spec->multiout.num_dacs = 1;
		spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1388 1389
		if (!is_jack_available(codec, 0x25))
			spec->multiout.dig_out_nid = 0;
1390 1391 1392
		spec->input_mux = &ad1986a_laptop_eapd_capture_source;
		codec->patch_ops.unsol_event = ad1986a_hp_unsol_event;
		codec->patch_ops.init = ad1986a_hp_init;
1393 1394 1395 1396
		/* Lenovo N100 seems to report the reversed bit
		 * for HP jack-sensing
		 */
		spec->inv_jack_detect = 1;
1397
		break;
1398 1399 1400 1401 1402 1403 1404 1405 1406
	case AD1986A_ULTRA:
		spec->mixers[0] = ad1986a_laptop_eapd_mixers;
		spec->num_init_verbs = 2;
		spec->init_verbs[1] = ad1986a_ultra_init;
		spec->multiout.max_channels = 2;
		spec->multiout.num_dacs = 1;
		spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
		spec->multiout.dig_out_nid = 0;
		break;
1407 1408
	}

1409 1410 1411 1412 1413 1414 1415 1416
	/* AD1986A has a hardware problem that it can't share a stream
	 * with multiple output pins.  The copy of front to surrounds
	 * causes noisy or silent outputs at a certain timing, e.g.
	 * changing the volume.
	 * So, let's disable the shared stream.
	 */
	spec->multiout.no_share_stream = 1;

1417
	codec->no_trigger_sense = 1;
1418
	codec->no_sticky_stream = 1;
1419

L
Linus Torvalds 已提交
1420 1421
	return 0;
}
1422 1423 1424
#else /* ENABLE_AD_STATIC_QUIRKS */
#define patch_ad1986a	ad1986a_parse_auto_config
#endif /* ENABLE_AD_STATIC_QUIRKS */
L
Linus Torvalds 已提交
1425 1426

/*
1427
 * AD1983 specific
L
Linus Torvalds 已提交
1428 1429
 */

1430
#ifdef ENABLE_AD_STATIC_QUIRKS
1431 1432 1433
#define AD1983_SPDIF_OUT	0x02
#define AD1983_DAC		0x03
#define AD1983_ADC		0x04
L
Linus Torvalds 已提交
1434

1435 1436 1437
static const hda_nid_t ad1983_dac_nids[1] = { AD1983_DAC };
static const hda_nid_t ad1983_adc_nids[1] = { AD1983_ADC };
static const hda_nid_t ad1983_capsrc_nids[1] = { 0x15 };
1438

1439
static const struct hda_input_mux ad1983_capture_source = {
1440 1441 1442 1443 1444 1445 1446 1447
	.num_items = 4,
	.items = {
		{ "Mic", 0x0 },
		{ "Line", 0x1 },
		{ "Mix", 0x2 },
		{ "Mix Mono", 0x3 },
	},
};
L
Linus Torvalds 已提交
1448 1449

/*
1450
 * SPDIF playback route
L
Linus Torvalds 已提交
1451
 */
1452
static int ad1983_spdif_route_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1453
{
1454
	static const char * const texts[] = { "PCM", "ADC" };
1455 1456 1457 1458 1459 1460 1461 1462

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 2;
	if (uinfo->value.enumerated.item > 1)
		uinfo->value.enumerated.item = 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
L
Linus Torvalds 已提交
1463 1464
}

1465
static int ad1983_spdif_route_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1466
{
1467 1468
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;
L
Linus Torvalds 已提交
1469

1470
	ucontrol->value.enumerated.item[0] = spec->spdif_route;
L
Linus Torvalds 已提交
1471 1472 1473
	return 0;
}

1474
static int ad1983_spdif_route_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1475
{
1476 1477 1478
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;

1479 1480
	if (ucontrol->value.enumerated.item[0] > 1)
		return -EINVAL;
1481 1482
	if (spec->spdif_route != ucontrol->value.enumerated.item[0]) {
		spec->spdif_route = ucontrol->value.enumerated.item[0];
1483 1484 1485
		snd_hda_codec_write_cache(codec, spec->multiout.dig_out_nid, 0,
					  AC_VERB_SET_CONNECT_SEL,
					  spec->spdif_route);
1486 1487
		return 1;
	}
L
Linus Torvalds 已提交
1488 1489 1490
	return 0;
}

1491
static const struct snd_kcontrol_new ad1983_mixers[] = {
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
	HDA_CODEC_VOLUME("Front Playback Volume", 0x05, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Front Playback Switch", 0x05, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Headphone Playback Volume", 0x06, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Headphone Playback Switch", 0x06, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x07, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x07, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Line Playback Volume", 0x13, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Line Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1504
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x0c, 0x0, HDA_OUTPUT),
1505 1506 1507 1508 1509 1510 1511 1512
	HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Capture Source",
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
L
Linus Torvalds 已提交
1513
	},
1514 1515
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1516
		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1517 1518 1519
		.info = ad1983_spdif_route_info,
		.get = ad1983_spdif_route_get,
		.put = ad1983_spdif_route_put,
L
Linus Torvalds 已提交
1520
	},
1521
	{ } /* end */
L
Linus Torvalds 已提交
1522 1523
};

1524
static const struct hda_verb ad1983_init_verbs[] = {
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
	/* Front, HP, Mono; mute as default */
	{0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x06, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x07, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	/* Beep, PCM, Mic, Line-In: mute */
	{0x10, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	/* Front, HP selectors; from Mix */
	{0x05, AC_VERB_SET_CONNECT_SEL, 0x01},
	{0x06, AC_VERB_SET_CONNECT_SEL, 0x01},
	/* Mono selector; from Mix */
	{0x0b, AC_VERB_SET_CONNECT_SEL, 0x03},
	/* Mic selector; Mic */
	{0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* Line-in selector: Line-in */
	{0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* Mic boost: 0dB */
	{0x0c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
	/* Record selector: mic */
	{0x15, AC_VERB_SET_CONNECT_SEL, 0x0},
	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	/* SPDIF route: PCM */
	{0x02, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* Front Pin */
	{0x05, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
	/* HP Pin */
	{0x06, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
	/* Mono Pin */
	{0x07, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
	/* Mic Pin */
	{0x08, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
	/* Line Pin */
	{0x09, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
	{ } /* end */
L
Linus Torvalds 已提交
1561 1562
};

1563
#ifdef CONFIG_PM
1564
static const struct hda_amp_list ad1983_loopbacks[] = {
1565 1566 1567 1568 1569
	{ 0x12, HDA_OUTPUT, 0 }, /* Mic */
	{ 0x13, HDA_OUTPUT, 0 }, /* Line */
	{ } /* end */
};
#endif
T
Takashi Iwai 已提交
1570

1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
/* models */
enum {
	AD1983_AUTO,
	AD1983_BASIC,
	AD1983_MODELS
};

static const char * const ad1983_models[AD1983_MODELS] = {
	[AD1983_AUTO]		= "auto",
	[AD1983_BASIC]		= "basic",
};
1582 1583
#endif /* ENABLE_AD_STATIC_QUIRKS */

1584

1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
/*
 * SPDIF mux control for AD1983 auto-parser
 */
static int ad1983_auto_smux_enum_info(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_info *uinfo)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;
	static const char * const texts2[] = { "PCM", "ADC" };
	static const char * const texts3[] = { "PCM", "ADC1", "ADC2" };
	hda_nid_t dig_out = spec->gen.multiout.dig_out_nid;
	int num_conns = snd_hda_get_num_conns(codec, dig_out);

	if (num_conns == 2)
		return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts2);
	else if (num_conns == 3)
		return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
	else
		return -EINVAL;
}

static int ad1983_auto_smux_enum_get(struct snd_kcontrol *kcontrol,
				     struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;

	ucontrol->value.enumerated.item[0] = spec->cur_smux;
	return 0;
}

static int ad1983_auto_smux_enum_put(struct snd_kcontrol *kcontrol,
				     struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;
	unsigned int val = ucontrol->value.enumerated.item[0];
	hda_nid_t dig_out = spec->gen.multiout.dig_out_nid;
	int num_conns = snd_hda_get_num_conns(codec, dig_out);

	if (val >= num_conns)
		return -EINVAL;
	if (spec->cur_smux == val)
		return 0;
	spec->cur_smux = val;
	snd_hda_codec_write_cache(codec, dig_out, 0,
				  AC_VERB_SET_CONNECT_SEL, val);
	return 1;
}

static struct snd_kcontrol_new ad1983_auto_smux_mixer = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "IEC958 Playback Source",
	.info = ad1983_auto_smux_enum_info,
	.get = ad1983_auto_smux_enum_get,
	.put = ad1983_auto_smux_enum_put,
};

static int ad1983_add_spdif_mux_ctl(struct hda_codec *codec)
{
	struct ad198x_spec *spec = codec->spec;
	hda_nid_t dig_out = spec->gen.multiout.dig_out_nid;
	int num_conns;

	if (!dig_out)
		return 0;
	num_conns = snd_hda_get_num_conns(codec, dig_out);
	if (num_conns != 2 && num_conns != 3)
		return 0;
	if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &ad1983_auto_smux_mixer))
		return -ENOMEM;
	return 0;
}

1659 1660
static int ad1983_parse_auto_config(struct hda_codec *codec)
{
1661
	struct ad198x_spec *spec;
1662
	int err;
1663

1664 1665 1666 1667 1668
	err = alloc_ad_spec(codec);
	if (err < 0)
		return err;
	spec = codec->spec;

1669
	spec->gen.beep_nid = 0x10;
1670
	set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
1671 1672
	err = ad198x_parse_auto_config(codec);
	if (err < 0)
1673
		goto error;
1674 1675
	err = ad1983_add_spdif_mux_ctl(codec);
	if (err < 0)
1676
		goto error;
1677
	return 0;
1678 1679

 error:
1680
	snd_hda_gen_free(codec);
1681
	return err;
1682 1683
}

1684
#ifdef ENABLE_AD_STATIC_QUIRKS
1685
static int patch_ad1983(struct hda_codec *codec)
L
Linus Torvalds 已提交
1686
{
1687
	struct ad198x_spec *spec;
1688
	int board_config;
1689
	int err;
L
Linus Torvalds 已提交
1690

1691 1692
	board_config = snd_hda_check_board_config(codec, AD1983_MODELS,
						  ad1983_models, NULL);
1693 1694 1695 1696 1697 1698
	if (board_config < 0) {
		printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
		       codec->chip_name);
		board_config = AD1983_AUTO;
	}

1699 1700 1701
	if (board_config == AD1983_AUTO)
		return ad1983_parse_auto_config(codec);

1702 1703 1704 1705
	err = alloc_ad_spec(codec);
	if (err < 0)
		return err;
	spec = codec->spec;
1706

1707 1708 1709 1710 1711 1712 1713
	err = snd_hda_attach_beep_device(codec, 0x10);
	if (err < 0) {
		ad198x_free(codec);
		return err;
	}
	set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);

1714 1715 1716 1717
	spec->multiout.max_channels = 2;
	spec->multiout.num_dacs = ARRAY_SIZE(ad1983_dac_nids);
	spec->multiout.dac_nids = ad1983_dac_nids;
	spec->multiout.dig_out_nid = AD1983_SPDIF_OUT;
T
Takashi Iwai 已提交
1718 1719
	spec->num_adc_nids = 1;
	spec->adc_nids = ad1983_adc_nids;
1720
	spec->capsrc_nids = ad1983_capsrc_nids;
1721
	spec->input_mux = &ad1983_capture_source;
T
Takashi Iwai 已提交
1722 1723 1724 1725
	spec->num_mixers = 1;
	spec->mixers[0] = ad1983_mixers;
	spec->num_init_verbs = 1;
	spec->init_verbs[0] = ad1983_init_verbs;
1726
	spec->spdif_route = 0;
1727
#ifdef CONFIG_PM
1728 1729
	spec->loopback.amplist = ad1983_loopbacks;
#endif
1730
	spec->vmaster_nid = 0x05;
L
Linus Torvalds 已提交
1731

1732
	codec->patch_ops = ad198x_patch_ops;
L
Linus Torvalds 已提交
1733

1734
	codec->no_trigger_sense = 1;
1735
	codec->no_sticky_stream = 1;
1736

L
Linus Torvalds 已提交
1737 1738
	return 0;
}
1739 1740 1741
#else /* ENABLE_AD_STATIC_QUIRKS */
#define patch_ad1983	ad1983_parse_auto_config
#endif /* ENABLE_AD_STATIC_QUIRKS */
L
Linus Torvalds 已提交
1742 1743


1744 1745 1746
/*
 * AD1981 HD specific
 */
L
Linus Torvalds 已提交
1747

1748
#ifdef ENABLE_AD_STATIC_QUIRKS
1749 1750 1751 1752
#define AD1981_SPDIF_OUT	0x02
#define AD1981_DAC		0x03
#define AD1981_ADC		0x04

1753 1754 1755
static const hda_nid_t ad1981_dac_nids[1] = { AD1981_DAC };
static const hda_nid_t ad1981_adc_nids[1] = { AD1981_ADC };
static const hda_nid_t ad1981_capsrc_nids[1] = { 0x15 };
1756 1757

/* 0x0c, 0x09, 0x0e, 0x0f, 0x19, 0x05, 0x18, 0x17 */
1758
static const struct hda_input_mux ad1981_capture_source = {
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
	.num_items = 7,
	.items = {
		{ "Front Mic", 0x0 },
		{ "Line", 0x1 },
		{ "Mix", 0x2 },
		{ "Mix Mono", 0x3 },
		{ "CD", 0x4 },
		{ "Mic", 0x6 },
		{ "Aux", 0x7 },
	},
L
Linus Torvalds 已提交
1769 1770
};

1771
static const struct snd_kcontrol_new ad1981_mixers[] = {
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
	HDA_CODEC_VOLUME("Front Playback Volume", 0x05, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Front Playback Switch", 0x05, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Headphone Playback Volume", 0x06, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Headphone Playback Switch", 0x06, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x07, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x07, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Front Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Line Playback Volume", 0x13, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Line Playback Switch", 0x13, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Aux Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Aux Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1790 1791
	HDA_CODEC_VOLUME("Front Mic Boost Volume", 0x08, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x18, 0x0, HDA_INPUT),
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
	HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Capture Source",
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
	},
	/* identical with AD1983 */
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1804
		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1805 1806 1807 1808 1809 1810 1811
		.info = ad1983_spdif_route_info,
		.get = ad1983_spdif_route_get,
		.put = ad1983_spdif_route_put,
	},
	{ } /* end */
};

1812
static const struct hda_verb ad1981_init_verbs[] = {
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
	/* Front, HP, Mono; mute as default */
	{0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x06, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x07, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	/* Beep, PCM, Front Mic, Line, Rear Mic, Aux, CD-In: mute */
	{0x0d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	/* Front, HP selectors; from Mix */
	{0x05, AC_VERB_SET_CONNECT_SEL, 0x01},
	{0x06, AC_VERB_SET_CONNECT_SEL, 0x01},
	/* Mono selector; from Mix */
	{0x0b, AC_VERB_SET_CONNECT_SEL, 0x03},
	/* Mic Mixer; select Front Mic */
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
	{0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	/* Mic boost: 0dB */
1834 1835
	{0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
	/* Record selector: Front mic */
	{0x15, AC_VERB_SET_CONNECT_SEL, 0x0},
	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	/* SPDIF route: PCM */
	{0x02, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* Front Pin */
	{0x05, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
	/* HP Pin */
	{0x06, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
	/* Mono Pin */
	{0x07, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
	/* Front & Rear Mic Pins */
	{0x08, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
	{0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
	/* Line Pin */
	{0x09, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
	/* Digital Beep */
	{0x0d, AC_VERB_SET_CONNECT_SEL, 0x00},
	/* Line-Out as Input: disabled */
	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
	{ } /* end */
};

1859
#ifdef CONFIG_PM
1860
static const struct hda_amp_list ad1981_loopbacks[] = {
1861 1862 1863 1864 1865 1866 1867 1868 1869
	{ 0x12, HDA_OUTPUT, 0 }, /* Front Mic */
	{ 0x13, HDA_OUTPUT, 0 }, /* Line */
	{ 0x1b, HDA_OUTPUT, 0 }, /* Aux */
	{ 0x1c, HDA_OUTPUT, 0 }, /* Mic */
	{ 0x1d, HDA_OUTPUT, 0 }, /* CD */
	{ } /* end */
};
#endif

1870 1871 1872
/*
 * Patch for HP nx6320
 *
1873
 * nx6320 uses EAPD in the reverse way - EAPD-on means the internal
1874 1875 1876 1877 1878 1879
 * speaker output enabled _and_ mute-LED off.
 */

#define AD1981_HP_EVENT		0x37
#define AD1981_MIC_EVENT	0x38

1880
static const struct hda_verb ad1981_hp_init_verbs[] = {
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
	{0x05, AC_VERB_SET_EAPD_BTLENABLE, 0x00 }, /* default off */
	/* pin sensing on HP and Mic jacks */
	{0x06, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_HP_EVENT},
	{0x08, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_MIC_EVENT},
	{}
};

/* turn on/off EAPD (+ mute HP) as a master switch */
static int ad1981_hp_master_sw_put(struct snd_kcontrol *kcontrol,
				   struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;

	if (! ad198x_eapd_put(kcontrol, ucontrol))
		return 0;
1897
	/* change speaker pin appropriately */
1898
	snd_hda_set_pin_ctl(codec, 0x05, spec->cur_eapd ? PIN_OUT : 0);
1899
	/* toggle HP mute appropriately */
1900 1901 1902
	snd_hda_codec_amp_stereo(codec, 0x06, HDA_OUTPUT, 0,
				 HDA_AMP_MUTE,
				 spec->cur_eapd ? 0 : HDA_AMP_MUTE);
1903 1904 1905 1906
	return 1;
}

/* bind volumes of both NID 0x05 and 0x06 */
1907
static const struct hda_bind_ctls ad1981_hp_bind_master_vol = {
1908 1909 1910 1911 1912 1913 1914
	.ops = &snd_hda_bind_vol,
	.values = {
		HDA_COMPOSE_AMP_VAL(0x05, 3, 0, HDA_OUTPUT),
		HDA_COMPOSE_AMP_VAL(0x06, 3, 0, HDA_OUTPUT),
		0
	},
};
1915 1916 1917 1918 1919 1920

/* mute internal speaker if HP is plugged */
static void ad1981_hp_automute(struct hda_codec *codec)
{
	unsigned int present;

1921
	present = snd_hda_jack_detect(codec, 0x06);
1922 1923
	snd_hda_codec_amp_stereo(codec, 0x05, HDA_OUTPUT, 0,
				 HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
1924 1925 1926 1927 1928
}

/* toggle input of built-in and mic jack appropriately */
static void ad1981_hp_automic(struct hda_codec *codec)
{
1929
	static const struct hda_verb mic_jack_on[] = {
1930 1931 1932 1933
		{0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
		{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
		{}
	};
1934
	static const struct hda_verb mic_jack_off[] = {
1935 1936 1937 1938 1939 1940
		{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
		{0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
		{}
	};
	unsigned int present;

1941
	present = snd_hda_jack_detect(codec, 0x08);
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
	if (present)
		snd_hda_sequence_write(codec, mic_jack_on);
	else
		snd_hda_sequence_write(codec, mic_jack_off);
}

/* unsolicited event for HP jack sensing */
static void ad1981_hp_unsol_event(struct hda_codec *codec,
				  unsigned int res)
{
	res >>= 26;
	switch (res) {
	case AD1981_HP_EVENT:
		ad1981_hp_automute(codec);
		break;
	case AD1981_MIC_EVENT:
		ad1981_hp_automic(codec);
		break;
	}
}

1963
static const struct hda_input_mux ad1981_hp_capture_source = {
1964 1965 1966
	.num_items = 3,
	.items = {
		{ "Mic", 0x0 },
1967
		{ "Dock Mic", 0x1 },
1968 1969 1970 1971
		{ "Mix", 0x2 },
	},
};

1972
static const struct snd_kcontrol_new ad1981_hp_mixers[] = {
1973
	HDA_BIND_VOL("Master Playback Volume", &ad1981_hp_bind_master_vol),
1974 1975
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1976
		.subdevice = HDA_SUBDEV_NID_FLAG | 0x05,
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
		.name = "Master Playback Switch",
		.info = ad198x_eapd_info,
		.get = ad198x_eapd_get,
		.put = ad1981_hp_master_sw_put,
		.private_value = 0x05,
	},
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
#if 0
	/* FIXME: analog mic/line loopback doesn't work with my tests...
	 *        (although recording is OK)
	 */
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1991 1992
	HDA_CODEC_VOLUME("Dock Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Dock Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1993 1994 1995 1996 1997 1998
	HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
	/* FIXME: does this laptop have analog CD connection? */
	HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
#endif
1999 2000
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x08, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Internal Mic Boost Volume", 0x18, 0x0, HDA_INPUT),
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
	HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Capture Source",
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
	},
	{ } /* end */
};

/* initialize jack-sensing, too */
static int ad1981_hp_init(struct hda_codec *codec)
{
	ad198x_init(codec);
	ad1981_hp_automute(codec);
	ad1981_hp_automic(codec);
	return 0;
}

2022
/* configuration for Toshiba Laptops */
2023
static const struct hda_verb ad1981_toshiba_init_verbs[] = {
2024 2025 2026 2027 2028 2029 2030
	{0x05, AC_VERB_SET_EAPD_BTLENABLE, 0x01 }, /* default on */
	/* pin sensing on HP and Mic jacks */
	{0x06, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_HP_EVENT},
	{0x08, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_MIC_EVENT},
	{}
};

2031
static const struct snd_kcontrol_new ad1981_toshiba_mixers[] = {
2032 2033 2034 2035 2036
	HDA_CODEC_VOLUME("Amp Volume", 0x1a, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Amp Switch", 0x1a, 0x0, HDA_OUTPUT),
	{ }
};

2037
/* configuration for Lenovo Thinkpad T60 */
2038
static const struct snd_kcontrol_new ad1981_thinkpad_mixers[] = {
2039 2040 2041 2042 2043 2044 2045 2046
	HDA_CODEC_VOLUME("Master Playback Volume", 0x05, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Master Playback Switch", 0x05, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
2047
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x08, 0x0, HDA_INPUT),
2048 2049 2050 2051 2052 2053 2054 2055 2056
	HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Capture Source",
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
	},
2057 2058 2059 2060 2061 2062 2063 2064
	/* identical with AD1983 */
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
		.info = ad1983_spdif_route_info,
		.get = ad1983_spdif_route_get,
		.put = ad1983_spdif_route_put,
	},
2065 2066 2067
	{ } /* end */
};

2068
static const struct hda_input_mux ad1981_thinkpad_capture_source = {
2069 2070 2071 2072 2073 2074 2075 2076
	.num_items = 3,
	.items = {
		{ "Mic", 0x0 },
		{ "Mix", 0x2 },
		{ "CD", 0x4 },
	},
};

2077
/* models */
2078
enum {
2079
	AD1981_AUTO,
2080 2081 2082
	AD1981_BASIC,
	AD1981_HP,
	AD1981_THINKPAD,
2083
	AD1981_TOSHIBA,
2084 2085
	AD1981_MODELS
};
2086

2087
static const char * const ad1981_models[AD1981_MODELS] = {
2088
	[AD1981_AUTO]		= "auto",
2089 2090 2091
	[AD1981_HP]		= "hp",
	[AD1981_THINKPAD]	= "thinkpad",
	[AD1981_BASIC]		= "basic",
2092
	[AD1981_TOSHIBA]	= "toshiba"
2093 2094
};

2095
static const struct snd_pci_quirk ad1981_cfg_tbl[] = {
2096
	SND_PCI_QUIRK(0x1014, 0x0597, "Lenovo Z60", AD1981_THINKPAD),
2097
	SND_PCI_QUIRK(0x1014, 0x05b7, "Lenovo Z60m", AD1981_THINKPAD),
2098
	/* All HP models */
T
Takashi Iwai 已提交
2099
	SND_PCI_QUIRK_VENDOR(0x103c, "HP nx", AD1981_HP),
2100
	SND_PCI_QUIRK(0x1179, 0x0001, "Toshiba U205", AD1981_TOSHIBA),
2101
	/* Lenovo Thinkpad T60/X60/Z6xx */
T
Takashi Iwai 已提交
2102
	SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo Thinkpad", AD1981_THINKPAD),
2103 2104
	/* HP nx6320 (reversed SSID, H/W bug) */
	SND_PCI_QUIRK(0x30b0, 0x103c, "HP nx6320", AD1981_HP),
2105 2106
	{}
};
2107 2108
#endif /* ENABLE_AD_STATIC_QUIRKS */

2109

2110 2111 2112 2113 2114
/* follow EAPD via vmaster hook */
static void ad_vmaster_eapd_hook(void *private_data, int enabled)
{
	struct hda_codec *codec = private_data;
	struct ad198x_spec *spec = codec->spec;
2115 2116 2117

	if (!spec->eapd_nid)
		return;
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
	snd_hda_codec_update_cache(codec, spec->eapd_nid, 0,
				   AC_VERB_SET_EAPD_BTLENABLE,
				   enabled ? 0x02 : 0x00);
}

static void ad1981_fixup_hp_eapd(struct hda_codec *codec,
				 const struct hda_fixup *fix, int action)
{
	struct ad198x_spec *spec = codec->spec;

	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
		spec->gen.vmaster_mute.hook = ad_vmaster_eapd_hook;
		spec->eapd_nid = 0x05;
	}
}

/* set the upper-limit for mixer amp to 0dB for avoiding the possible
 * damage by overloading
 */
static void ad1981_fixup_amp_override(struct hda_codec *codec,
				      const struct hda_fixup *fix, int action)
{
	if (action == HDA_FIXUP_ACT_PRE_PROBE)
		snd_hda_override_amp_caps(codec, 0x11, HDA_INPUT,
					  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
					  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
					  (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
					  (1 << AC_AMPCAP_MUTE_SHIFT));
}

enum {
	AD1981_FIXUP_AMP_OVERRIDE,
	AD1981_FIXUP_HP_EAPD,
};

static const struct hda_fixup ad1981_fixups[] = {
	[AD1981_FIXUP_AMP_OVERRIDE] = {
		.type = HDA_FIXUP_FUNC,
		.v.func = ad1981_fixup_amp_override,
	},
	[AD1981_FIXUP_HP_EAPD] = {
		.type = HDA_FIXUP_FUNC,
		.v.func = ad1981_fixup_hp_eapd,
		.chained = true,
		.chain_id = AD1981_FIXUP_AMP_OVERRIDE,
	},
};

static const struct snd_pci_quirk ad1981_fixup_tbl[] = {
	SND_PCI_QUIRK_VENDOR(0x1014, "Lenovo", AD1981_FIXUP_AMP_OVERRIDE),
	SND_PCI_QUIRK_VENDOR(0x103c, "HP", AD1981_FIXUP_HP_EAPD),
	SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo", AD1981_FIXUP_AMP_OVERRIDE),
	/* HP nx6320 (reversed SSID, H/W bug) */
	SND_PCI_QUIRK(0x30b0, 0x103c, "HP nx6320", AD1981_FIXUP_HP_EAPD),
	{}
};

2175 2176
static int ad1981_parse_auto_config(struct hda_codec *codec)
{
2177
	struct ad198x_spec *spec;
2178
	int err;
2179

2180 2181 2182 2183 2184
	err = alloc_ad_spec(codec);
	if (err < 0)
		return -ENOMEM;
	spec = codec->spec;

2185
	spec->gen.mixer_nid = 0x0e;
2186
	spec->gen.beep_nid = 0x10;
2187
	set_beep_amp(spec, 0x0d, 0, HDA_OUTPUT);
2188 2189 2190 2191

	snd_hda_pick_fixup(codec, NULL, ad1981_fixup_tbl, ad1981_fixups);
	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);

2192 2193
	err = ad198x_parse_auto_config(codec);
	if (err < 0)
2194
		goto error;
2195 2196
	err = ad1983_add_spdif_mux_ctl(codec);
	if (err < 0)
2197
		goto error;
2198 2199 2200

	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);

2201
	return 0;
2202 2203

 error:
2204
	snd_hda_gen_free(codec);
2205
	return err;
2206 2207
}

2208
#ifdef ENABLE_AD_STATIC_QUIRKS
2209
static int patch_ad1981(struct hda_codec *codec)
L
Linus Torvalds 已提交
2210
{
2211
	struct ad198x_spec *spec;
2212
	int err, board_config;
L
Linus Torvalds 已提交
2213

2214 2215 2216
	board_config = snd_hda_check_board_config(codec, AD1981_MODELS,
						  ad1981_models,
						  ad1981_cfg_tbl);
2217 2218 2219 2220 2221 2222
	if (board_config < 0) {
		printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
		       codec->chip_name);
		board_config = AD1981_AUTO;
	}

2223 2224 2225
	if (board_config == AD1981_AUTO)
		return ad1981_parse_auto_config(codec);

2226 2227
	err = alloc_ad_spec(codec);
	if (err < 0)
L
Linus Torvalds 已提交
2228
		return -ENOMEM;
2229
	spec = codec->spec;
L
Linus Torvalds 已提交
2230

2231 2232 2233 2234 2235 2236 2237
	err = snd_hda_attach_beep_device(codec, 0x10);
	if (err < 0) {
		ad198x_free(codec);
		return err;
	}
	set_beep_amp(spec, 0x0d, 0, HDA_OUTPUT);

2238 2239 2240 2241
	spec->multiout.max_channels = 2;
	spec->multiout.num_dacs = ARRAY_SIZE(ad1981_dac_nids);
	spec->multiout.dac_nids = ad1981_dac_nids;
	spec->multiout.dig_out_nid = AD1981_SPDIF_OUT;
T
Takashi Iwai 已提交
2242 2243
	spec->num_adc_nids = 1;
	spec->adc_nids = ad1981_adc_nids;
2244
	spec->capsrc_nids = ad1981_capsrc_nids;
2245
	spec->input_mux = &ad1981_capture_source;
T
Takashi Iwai 已提交
2246 2247 2248 2249
	spec->num_mixers = 1;
	spec->mixers[0] = ad1981_mixers;
	spec->num_init_verbs = 1;
	spec->init_verbs[0] = ad1981_init_verbs;
2250
	spec->spdif_route = 0;
2251
#ifdef CONFIG_PM
2252 2253
	spec->loopback.amplist = ad1981_loopbacks;
#endif
2254
	spec->vmaster_nid = 0x05;
L
Linus Torvalds 已提交
2255

2256
	codec->patch_ops = ad198x_patch_ops;
L
Linus Torvalds 已提交
2257

2258 2259 2260 2261 2262 2263
	/* override some parameters */
	switch (board_config) {
	case AD1981_HP:
		spec->mixers[0] = ad1981_hp_mixers;
		spec->num_init_verbs = 2;
		spec->init_verbs[1] = ad1981_hp_init_verbs;
2264 2265
		if (!is_jack_available(codec, 0x0a))
			spec->multiout.dig_out_nid = 0;
2266 2267 2268 2269
		spec->input_mux = &ad1981_hp_capture_source;

		codec->patch_ops.init = ad1981_hp_init;
		codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
2270 2271 2272 2273 2274 2275 2276 2277
		/* set the upper-limit for mixer amp to 0dB for avoiding the
		 * possible damage by overloading
		 */
		snd_hda_override_amp_caps(codec, 0x11, HDA_INPUT,
					  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
					  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
					  (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
					  (1 << AC_AMPCAP_MUTE_SHIFT));
2278
		break;
2279 2280 2281
	case AD1981_THINKPAD:
		spec->mixers[0] = ad1981_thinkpad_mixers;
		spec->input_mux = &ad1981_thinkpad_capture_source;
2282 2283 2284 2285 2286 2287 2288 2289
		/* set the upper-limit for mixer amp to 0dB for avoiding the
		 * possible damage by overloading
		 */
		snd_hda_override_amp_caps(codec, 0x11, HDA_INPUT,
					  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
					  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
					  (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
					  (1 << AC_AMPCAP_MUTE_SHIFT));
2290
		break;
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
	case AD1981_TOSHIBA:
		spec->mixers[0] = ad1981_hp_mixers;
		spec->mixers[1] = ad1981_toshiba_mixers;
		spec->num_init_verbs = 2;
		spec->init_verbs[1] = ad1981_toshiba_init_verbs;
		spec->multiout.dig_out_nid = 0;
		spec->input_mux = &ad1981_hp_capture_source;
		codec->patch_ops.init = ad1981_hp_init;
		codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
		break;
2301
	}
2302 2303

	codec->no_trigger_sense = 1;
2304
	codec->no_sticky_stream = 1;
2305

L
Linus Torvalds 已提交
2306 2307
	return 0;
}
2308 2309 2310
#else /* ENABLE_AD_STATIC_QUIRKS */
#define patch_ad1981	ad1981_parse_auto_config
#endif /* ENABLE_AD_STATIC_QUIRKS */
L
Linus Torvalds 已提交
2311

2312

2313 2314 2315 2316 2317
/*
 * AD1988
 *
 * Output pins and routes
 *
2318
 *        Pin               Mix     Sel     DAC (*)
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
 * port-A 0x11 (mute/hp) <- 0x22 <- 0x37 <- 03/04/06
 * port-B 0x14 (mute/hp) <- 0x2b <- 0x30 <- 03/04/06
 * port-C 0x15 (mute)    <- 0x2c <- 0x31 <- 05/0a
 * port-D 0x12 (mute/hp) <- 0x29         <- 04
 * port-E 0x17 (mute/hp) <- 0x26 <- 0x32 <- 05/0a
 * port-F 0x16 (mute)    <- 0x2a         <- 06
 * port-G 0x24 (mute)    <- 0x27         <- 05
 * port-H 0x25 (mute)    <- 0x28         <- 0a
 * mono   0x13 (mute/amp)<- 0x1e <- 0x36 <- 03/04/06
 *
2329 2330
 * DAC0 = 03h, DAC1 = 04h, DAC2 = 05h, DAC3 = 06h, DAC4 = 0ah
 * (*) DAC2/3/4 are swapped to DAC3/4/2 on AD198A rev.2 due to a h/w bug.
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
 *
 * Input pins and routes
 *
 *        pin     boost   mix input # / adc input #
 * port-A 0x11 -> 0x38 -> mix 2, ADC 0
 * port-B 0x14 -> 0x39 -> mix 0, ADC 1
 * port-C 0x15 -> 0x3a -> 33:0 - mix 1, ADC 2
 * port-D 0x12 -> 0x3d -> mix 3, ADC 8
 * port-E 0x17 -> 0x3c -> 34:0 - mix 4, ADC 4
 * port-F 0x16 -> 0x3b -> mix 5, ADC 3
 * port-G 0x24 -> N/A  -> 33:1 - mix 1, 34:1 - mix 4, ADC 6
 * port-H 0x25 -> N/A  -> 33:2 - mix 1, 34:2 - mix 4, ADC 7
 *
 *
 * DAC assignment
2346
 *   6stack - front/surr/CLFE/side/opt DACs - 04/06/05/0a/03
2347
 *   3stack - front/surr/CLFE/opt DACs - 04/05/0a/03
2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
 *
 * Inputs of Analog Mix (0x20)
 *   0:Port-B (front mic)
 *   1:Port-C/G/H (line-in)
 *   2:Port-A
 *   3:Port-D (line-in/2)
 *   4:Port-E/G/H (mic-in)
 *   5:Port-F (mic2-in)
 *   6:CD
 *   7:Beep
 *
 * ADC selection
 *   0:Port-A
 *   1:Port-B (front mic-in)
 *   2:Port-C (line-in)
 *   3:Port-F (mic2-in)
 *   4:Port-E (mic-in)
 *   5:CD
 *   6:Port-G
 *   7:Port-H
 *   8:Port-D (line-in/2)
 *   9:Mix
 *
 * Proposed pin assignments by the datasheet
 *
 * 6-stack
 * Port-A front headphone
 *      B front mic-in
 *      C rear line-in
 *      D rear front-out
 *      E rear mic-in
 *      F rear surround
 *      G rear CLFE
 *      H rear side
 *
 * 3-stack
 * Port-A front headphone
 *      B front mic
 *      C rear line-in/surround
 *      D rear front-out
 *      E rear mic-in/CLFE
 *
 * laptop
 * Port-A headphone
 *      B mic-in
 *      C docking station
 *      D internal speaker (with EAPD)
 *      E/F quad mic array
 */


2399
#ifdef ENABLE_AD_STATIC_QUIRKS
2400 2401
/* models */
enum {
2402
	AD1988_AUTO,
2403 2404 2405 2406 2407 2408 2409 2410 2411
	AD1988_6STACK,
	AD1988_6STACK_DIG,
	AD1988_3STACK,
	AD1988_3STACK_DIG,
	AD1988_LAPTOP,
	AD1988_LAPTOP_DIG,
	AD1988_MODEL_LAST,
};

2412 2413 2414
/* reivision id to check workarounds */
#define AD1988A_REV2		0x100200

2415 2416 2417
#define is_rev2(codec) \
	((codec)->vendor_id == 0x11d41988 && \
	 (codec)->revision_id == AD1988A_REV2)
2418 2419 2420 2421 2422

/*
 * mixers
 */

2423
static const hda_nid_t ad1988_6stack_dac_nids[4] = {
2424 2425 2426
	0x04, 0x06, 0x05, 0x0a
};

2427
static const hda_nid_t ad1988_3stack_dac_nids[3] = {
2428
	0x04, 0x05, 0x0a
2429 2430 2431
};

/* for AD1988A revision-2, DAC2-4 are swapped */
2432
static const hda_nid_t ad1988_6stack_dac_nids_rev2[4] = {
2433 2434 2435
	0x04, 0x05, 0x0a, 0x06
};

2436
static const hda_nid_t ad1988_alt_dac_nid[1] = {
2437 2438 2439
	0x03
};

2440
static const hda_nid_t ad1988_3stack_dac_nids_rev2[3] = {
2441
	0x04, 0x0a, 0x06
2442 2443
};

2444
static const hda_nid_t ad1988_adc_nids[3] = {
2445 2446 2447
	0x08, 0x09, 0x0f
};

2448
static const hda_nid_t ad1988_capsrc_nids[3] = {
2449 2450 2451
	0x0c, 0x0d, 0x0e
};

2452 2453
#define AD1988_SPDIF_OUT		0x02
#define AD1988_SPDIF_OUT_HDMI	0x0b
2454 2455
#define AD1988_SPDIF_IN		0x07

2456
static const hda_nid_t ad1989b_slave_dig_outs[] = {
2457
	AD1988_SPDIF_OUT, AD1988_SPDIF_OUT_HDMI, 0
2458 2459
};

2460
static const struct hda_input_mux ad1988_6stack_capture_source = {
2461 2462
	.num_items = 5,
	.items = {
2463 2464 2465
		{ "Front Mic", 0x1 },	/* port-B */
		{ "Line", 0x2 },	/* port-C */
		{ "Mic", 0x4 },		/* port-E */
2466 2467 2468 2469 2470
		{ "CD", 0x5 },
		{ "Mix", 0x9 },
	},
};

2471
static const struct hda_input_mux ad1988_laptop_capture_source = {
2472 2473
	.num_items = 3,
	.items = {
2474
		{ "Mic/Line", 0x1 },	/* port-B */
2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504
		{ "CD", 0x5 },
		{ "Mix", 0x9 },
	},
};

/*
 */
static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_info *uinfo)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;
	return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
				    spec->num_channel_mode);
}

static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;
	return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
				   spec->num_channel_mode, spec->multiout.max_channels);
}

static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;
2505 2506 2507
	int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
				      spec->num_channel_mode,
				      &spec->multiout.max_channels);
2508
	if (err >= 0 && spec->need_dac_fix)
2509
		spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2510
	return err;
2511 2512 2513
}

/* 6-stack mode */
2514
static const struct snd_kcontrol_new ad1988_6stack_mixers1[] = {
2515 2516 2517 2518 2519
	HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Surround Playback Volume", 0x06, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Side Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
2520
	{ } /* end */
2521 2522
};

2523
static const struct snd_kcontrol_new ad1988_6stack_mixers1_rev2[] = {
2524 2525 2526 2527 2528
	HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Surround Playback Volume", 0x05, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0a, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0a, 2, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Side Playback Volume", 0x06, 0x0, HDA_OUTPUT),
2529
	{ } /* end */
2530
};
2531

2532
static const struct snd_kcontrol_new ad1988_6stack_mixers2[] = {
2533
	HDA_CODEC_VOLUME("Headphone Playback Volume", 0x03, 0x0, HDA_OUTPUT),
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550
	HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
	HDA_BIND_MUTE("Surround Playback Switch", 0x2a, 2, HDA_INPUT),
	HDA_BIND_MUTE_MONO("Center Playback Switch", 0x27, 1, 2, HDA_INPUT),
	HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x27, 2, 2, HDA_INPUT),
	HDA_BIND_MUTE("Side Playback Switch", 0x28, 2, HDA_INPUT),
	HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
	HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),

	HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
	HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
	HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
	HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
	HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),

2551
	HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
2552 2553
	HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),

2554 2555
	HDA_CODEC_VOLUME("Front Mic Boost Volume", 0x39, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x3c, 0x0, HDA_OUTPUT),
2556 2557 2558 2559
	{ } /* end */
};

/* 3-stack mode */
2560
static const struct snd_kcontrol_new ad1988_3stack_mixers1[] = {
2561
	HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2562
	HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
2563 2564
	HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
2565
	{ } /* end */
2566 2567
};

2568
static const struct snd_kcontrol_new ad1988_3stack_mixers1_rev2[] = {
2569
	HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2570 2571 2572
	HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x06, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x06, 2, 0x0, HDA_OUTPUT),
2573
	{ } /* end */
2574
};
2575

2576
static const struct snd_kcontrol_new ad1988_3stack_mixers2[] = {
2577
	HDA_CODEC_VOLUME("Headphone Playback Volume", 0x03, 0x0, HDA_OUTPUT),
2578
	HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
2579 2580 2581
	HDA_BIND_MUTE("Surround Playback Switch", 0x2c, 2, HDA_INPUT),
	HDA_BIND_MUTE_MONO("Center Playback Switch", 0x26, 1, 2, HDA_INPUT),
	HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x26, 2, 2, HDA_INPUT),
2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
	HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
	HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),

	HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
	HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
	HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
	HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
	HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),

2594
	HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
2595 2596
	HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),

2597 2598
	HDA_CODEC_VOLUME("Front Mic Boost Volume", 0x39, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x3c, 0x0, HDA_OUTPUT),
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Channel Mode",
		.info = ad198x_ch_mode_info,
		.get = ad198x_ch_mode_get,
		.put = ad198x_ch_mode_put,
	},

	{ } /* end */
};

/* laptop mode */
2611
static const struct snd_kcontrol_new ad1988_laptop_mixers[] = {
2612
	HDA_CODEC_VOLUME("Headphone Playback Volume", 0x03, 0x0, HDA_OUTPUT),
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x29, 0x0, HDA_INPUT),
	HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),

	HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
	HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
	HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),

2624
	HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
2625 2626
	HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),

2627
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x39, 0x0, HDA_OUTPUT),
2628 2629 2630 2631

	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "External Amplifier",
2632
		.subdevice = HDA_SUBDEV_NID_FLAG | 0x12,
2633 2634 2635
		.info = ad198x_eapd_info,
		.get = ad198x_eapd_get,
		.put = ad198x_eapd_put,
2636
		.private_value = 0x12, /* port-D */
2637 2638 2639 2640 2641 2642
	},

	{ } /* end */
};

/* capture */
2643
static const struct snd_kcontrol_new ad1988_capture_mixers[] = {
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667
	HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x0e, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x0e, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		/* The multiple "Capture Source" controls confuse alsamixer
		 * So call somewhat different..
		 */
		/* .name = "Capture Source", */
		.name = "Input Source",
		.count = 3,
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
	},
	{ } /* end */
};

static int ad1988_spdif_playback_source_info(struct snd_kcontrol *kcontrol,
					     struct snd_ctl_elem_info *uinfo)
{
2668
	static const char * const texts[] = {
2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685
		"PCM", "ADC1", "ADC2", "ADC3"
	};
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 4;
	if (uinfo->value.enumerated.item >= 4)
		uinfo->value.enumerated.item = 3;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

static int ad1988_spdif_playback_source_get(struct snd_kcontrol *kcontrol,
					    struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	unsigned int sel;

2686 2687 2688 2689 2690
	sel = snd_hda_codec_read(codec, 0x1d, 0, AC_VERB_GET_AMP_GAIN_MUTE,
				 AC_AMP_GET_INPUT);
	if (!(sel & 0x80))
		ucontrol->value.enumerated.item[0] = 0;
	else {
2691 2692 2693
		sel = snd_hda_codec_read(codec, 0x0b, 0,
					 AC_VERB_GET_CONNECT_SEL, 0);
		if (sel < 3)
2694 2695 2696
			sel++;
		else
			sel = 0;
2697
		ucontrol->value.enumerated.item[0] = sel;
2698 2699 2700 2701 2702 2703 2704 2705
	}
	return 0;
}

static int ad1988_spdif_playback_source_put(struct snd_kcontrol *kcontrol,
					    struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2706
	unsigned int val, sel;
2707 2708
	int change;

2709
	val = ucontrol->value.enumerated.item[0];
2710 2711
	if (val > 3)
		return -EINVAL;
2712
	if (!val) {
2713 2714 2715 2716
		sel = snd_hda_codec_read(codec, 0x1d, 0,
					 AC_VERB_GET_AMP_GAIN_MUTE,
					 AC_AMP_GET_INPUT);
		change = sel & 0x80;
2717 2718 2719 2720 2721 2722 2723
		if (change) {
			snd_hda_codec_write_cache(codec, 0x1d, 0,
						  AC_VERB_SET_AMP_GAIN_MUTE,
						  AMP_IN_UNMUTE(0));
			snd_hda_codec_write_cache(codec, 0x1d, 0,
						  AC_VERB_SET_AMP_GAIN_MUTE,
						  AMP_IN_MUTE(1));
2724
		}
2725
	} else {
2726 2727 2728 2729
		sel = snd_hda_codec_read(codec, 0x1d, 0,
					 AC_VERB_GET_AMP_GAIN_MUTE,
					 AC_AMP_GET_INPUT | 0x01);
		change = sel & 0x80;
2730 2731 2732 2733 2734 2735 2736
		if (change) {
			snd_hda_codec_write_cache(codec, 0x1d, 0,
						  AC_VERB_SET_AMP_GAIN_MUTE,
						  AMP_IN_MUTE(0));
			snd_hda_codec_write_cache(codec, 0x1d, 0,
						  AC_VERB_SET_AMP_GAIN_MUTE,
						  AMP_IN_UNMUTE(1));
2737
		}
2738 2739 2740
		sel = snd_hda_codec_read(codec, 0x0b, 0,
					 AC_VERB_GET_CONNECT_SEL, 0) + 1;
		change |= sel != val;
2741 2742 2743 2744
		if (change)
			snd_hda_codec_write_cache(codec, 0x0b, 0,
						  AC_VERB_SET_CONNECT_SEL,
						  val - 1);
2745 2746 2747 2748
	}
	return change;
}

2749
static const struct snd_kcontrol_new ad1988_spdif_out_mixers[] = {
2750 2751 2752 2753
	HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "IEC958 Playback Source",
2754
		.subdevice = HDA_SUBDEV_NID_FLAG | 0x1b,
2755 2756 2757 2758 2759 2760 2761
		.info = ad1988_spdif_playback_source_info,
		.get = ad1988_spdif_playback_source_get,
		.put = ad1988_spdif_playback_source_put,
	},
	{ } /* end */
};

2762
static const struct snd_kcontrol_new ad1988_spdif_in_mixers[] = {
2763 2764 2765 2766
	HDA_CODEC_VOLUME("IEC958 Capture Volume", 0x1c, 0x0, HDA_INPUT),
	{ } /* end */
};

2767
static const struct snd_kcontrol_new ad1989_spdif_out_mixers[] = {
2768
	HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
2769
	HDA_CODEC_VOLUME("HDMI Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
2770 2771
	{ } /* end */
};
2772 2773 2774 2775 2776 2777 2778 2779

/*
 * initialization verbs
 */

/*
 * for 6-stack (+dig)
 */
2780
static const struct hda_verb ad1988_6stack_init_verbs[] = {
2781 2782 2783 2784 2785
	/* Front, Surround, CLFE, side DAC; unmute as default */
	{0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2786
	/* Port-A front headphon path */
2787
	{0x37, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC0:03h */
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
	{0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	/* Port-D line-out path */
	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	/* Port-F surround path */
	{0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	/* Port-G CLFE path */
	{0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	/* Port-H side path */
	{0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	/* Mono out path */
	{0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
	/* Port-B front mic-in path */
	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	{0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
	/* Port-C line-in path */
	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
	{0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
	{0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* Port-E mic-in path */
	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	{0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
	{0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
2832 2833
	/* Analog CD Input */
	{0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2834 2835
	/* Analog Mix output amp */
	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
2836 2837 2838 2839

	{ }
};

2840
static const struct hda_verb ad1988_6stack_fp_init_verbs[] = {
2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852
	/* Headphone; unmute as default */
	{0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	/* Port-A front headphon path */
	{0x37, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC0:03h */
	{0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},

	{ }
};

2853
static const struct hda_verb ad1988_capture_init_verbs[] = {
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870
	/* mute analog mix */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
	/* select ADCs - front-mic */
	{0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
	{0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
	{0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},

	{ }
};

2871
static const struct hda_verb ad1988_spdif_init_verbs[] = {
2872 2873 2874 2875
	/* SPDIF out sel */
	{0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
	{0x0b, AC_VERB_SET_CONNECT_SEL, 0x0}, /* ADC1 */
	{0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2876
	{0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2877 2878 2879 2880 2881 2882
	/* SPDIF out pin */
	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */

	{ }
};

2883
static const struct hda_verb ad1988_spdif_in_init_verbs[] = {
2884 2885 2886 2887 2888
	/* unmute SPDIF input pin */
	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
	{ }
};

2889
/* AD1989 has no ADC -> SPDIF route */
2890
static const struct hda_verb ad1989_spdif_init_verbs[] = {
2891 2892
	/* SPDIF-1 out pin */
	{0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2893
	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
2894 2895 2896
	/* SPDIF-2/HDMI out pin */
	{0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
	{0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
2897 2898 2899
	{ }
};

2900 2901 2902
/*
 * verbs for 3stack (+dig)
 */
2903
static const struct hda_verb ad1988_3stack_ch2_init[] = {
2904 2905 2906 2907 2908 2909 2910 2911 2912
	/* set port-C to line-in */
	{ 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
	{ 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
	/* set port-E to mic-in */
	{ 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
	{ 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
	{ } /* end */
};

2913
static const struct hda_verb ad1988_3stack_ch6_init[] = {
2914 2915
	/* set port-C to surround out */
	{ 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2916
	{ 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2917 2918
	/* set port-E to CLFE out */
	{ 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2919
	{ 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2920 2921 2922
	{ } /* end */
};

2923
static const struct hda_channel_mode ad1988_3stack_modes[2] = {
2924 2925 2926 2927
	{ 2, ad1988_3stack_ch2_init },
	{ 6, ad1988_3stack_ch6_init },
};

2928
static const struct hda_verb ad1988_3stack_init_verbs[] = {
2929 2930 2931 2932 2933
	/* Front, Surround, CLFE, side DAC; unmute as default */
	{0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2934
	/* Port-A front headphon path */
2935
	{0x37, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC0:03h */
2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954
	{0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	/* Port-D line-out path */
	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	/* Mono out path */
	{0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
	/* Port-B front mic-in path */
	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	{0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2955 2956 2957
	/* Port-C line-in/surround path - 6ch mode as default */
	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2958
	{0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2959
	{0x31, AC_VERB_SET_CONNECT_SEL, 0x0}, /* output sel: DAC 0x05 */
2960
	{0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2961 2962 2963
	/* Port-E mic-in/CLFE path - 6ch mode as default */
	{0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2964
	{0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2965
	{0x32, AC_VERB_SET_CONNECT_SEL, 0x1}, /* output sel: DAC 0x0a */
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979
	{0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* mute analog mix */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
	/* select ADCs - front-mic */
	{0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
	{0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
	{0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2980 2981
	/* Analog Mix output amp */
	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
2982 2983 2984 2985 2986 2987
	{ }
};

/*
 * verbs for laptop mode (+dig)
 */
2988
static const struct hda_verb ad1988_laptop_hp_on[] = {
2989 2990 2991 2992 2993
	/* unmute port-A and mute port-D */
	{ 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
	{ 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
	{ } /* end */
};
2994
static const struct hda_verb ad1988_laptop_hp_off[] = {
2995 2996 2997 2998 2999 3000 3001 3002
	/* mute port-A and unmute port-D */
	{ 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
	{ 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
	{ } /* end */
};

#define AD1988_HP_EVENT	0x01

3003
static const struct hda_verb ad1988_laptop_init_verbs[] = {
3004 3005 3006 3007 3008
	/* Front, Surround, CLFE, side DAC; unmute as default */
	{0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3009
	/* Port-A front headphon path */
3010
	{0x37, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC0:03h */
3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050
	{0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	/* unsolicited event for pin-sense */
	{0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1988_HP_EVENT },
	/* Port-D line-out path + EAPD */
	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	{0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x00}, /* EAPD-off */
	/* Mono out path */
	{0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
	/* Port-B mic-in path */
	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	{0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
	/* Port-C docking station - try to output */
	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	{0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
	{0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
	/* mute analog mix */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
	/* select ADCs - mic */
	{0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
	{0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
	{0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
3051 3052
	/* Analog Mix output amp */
	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
3053 3054 3055 3056 3057 3058 3059
	{ }
};

static void ad1988_laptop_unsol_event(struct hda_codec *codec, unsigned int res)
{
	if ((res >> 26) != AD1988_HP_EVENT)
		return;
3060
	if (snd_hda_jack_detect(codec, 0x11))
3061 3062 3063 3064 3065
		snd_hda_sequence_write(codec, ad1988_laptop_hp_on);
	else
		snd_hda_sequence_write(codec, ad1988_laptop_hp_off);
} 

3066
#ifdef CONFIG_PM
3067
static const struct hda_amp_list ad1988_loopbacks[] = {
3068 3069 3070 3071 3072 3073 3074
	{ 0x20, HDA_INPUT, 0 }, /* Front Mic */
	{ 0x20, HDA_INPUT, 1 }, /* Line */
	{ 0x20, HDA_INPUT, 4 }, /* Mic */
	{ 0x20, HDA_INPUT, 6 }, /* CD */
	{ } /* end */
};
#endif
3075
#endif /* ENABLE_AD_STATIC_QUIRKS */
3076

3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219
static int ad1988_auto_smux_enum_info(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_info *uinfo)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	static const char * const texts[] = {
		"PCM", "ADC1", "ADC2", "ADC3",
	};
	int num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1;
	if (num_conns > 4)
		num_conns = 4;
	return snd_hda_enum_helper_info(kcontrol, uinfo, num_conns, texts);
}

static int ad1988_auto_smux_enum_get(struct snd_kcontrol *kcontrol,
				     struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;

	ucontrol->value.enumerated.item[0] = spec->cur_smux;
	return 0;
}

static int ad1988_auto_smux_enum_put(struct snd_kcontrol *kcontrol,
				     struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct ad198x_spec *spec = codec->spec;
	unsigned int val = ucontrol->value.enumerated.item[0];
	struct nid_path *path;
	int num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1;

	if (val >= num_conns)
		return -EINVAL;
	if (spec->cur_smux == val)
		return 0;

	mutex_lock(&codec->control_mutex);
	codec->cached_write = 1;
	path = snd_hda_get_path_from_idx(codec,
					 spec->smux_paths[spec->cur_smux]);
	if (path)
		snd_hda_activate_path(codec, path, false, true);
	path = snd_hda_get_path_from_idx(codec, spec->smux_paths[val]);
	if (path)
		snd_hda_activate_path(codec, path, true, true);
	spec->cur_smux = val;
	codec->cached_write = 0;
	mutex_unlock(&codec->control_mutex);
	snd_hda_codec_flush_cache(codec); /* flush the updates */
	return 1;
}

static struct snd_kcontrol_new ad1988_auto_smux_mixer = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "IEC958 Playback Source",
	.info = ad1988_auto_smux_enum_info,
	.get = ad1988_auto_smux_enum_get,
	.put = ad1988_auto_smux_enum_put,
};

static int ad1988_auto_init(struct hda_codec *codec)
{
	struct ad198x_spec *spec = codec->spec;
	int i, err;

	err = snd_hda_gen_init(codec);
	if (err < 0)
		return err;
	if (!spec->gen.autocfg.dig_outs)
		return 0;

	for (i = 0; i < 4; i++) {
		struct nid_path *path;
		path = snd_hda_get_path_from_idx(codec, spec->smux_paths[i]);
		if (path)
			snd_hda_activate_path(codec, path, path->active, false);
	}

	return 0;
}

static int ad1988_add_spdif_mux_ctl(struct hda_codec *codec)
{
	struct ad198x_spec *spec = codec->spec;
	int i, num_conns;
	/* we create four static faked paths, since AD codecs have odd
	 * widget connections regarding the SPDIF out source
	 */
	static struct nid_path fake_paths[4] = {
		{
			.depth = 3,
			.path = { 0x02, 0x1d, 0x1b },
			.idx = { 0, 0, 0 },
			.multi = { 0, 0, 0 },
		},
		{
			.depth = 4,
			.path = { 0x08, 0x0b, 0x1d, 0x1b },
			.idx = { 0, 0, 1, 0 },
			.multi = { 0, 1, 0, 0 },
		},
		{
			.depth = 4,
			.path = { 0x09, 0x0b, 0x1d, 0x1b },
			.idx = { 0, 1, 1, 0 },
			.multi = { 0, 1, 0, 0 },
		},
		{
			.depth = 4,
			.path = { 0x0f, 0x0b, 0x1d, 0x1b },
			.idx = { 0, 2, 1, 0 },
			.multi = { 0, 1, 0, 0 },
		},
	};

	/* SPDIF source mux appears to be present only on AD1988A */
	if (!spec->gen.autocfg.dig_outs ||
	    get_wcaps_type(get_wcaps(codec, 0x1d)) != AC_WID_AUD_MIX)
		return 0;

	num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1;
	if (num_conns != 3 && num_conns != 4)
		return 0;

	for (i = 0; i < num_conns; i++) {
		struct nid_path *path = snd_array_new(&spec->gen.paths);
		if (!path)
			return -ENOMEM;
		*path = fake_paths[i];
		if (!i)
			path->active = 1;
		spec->smux_paths[i] = snd_hda_get_path_idx(codec, path);
	}

	if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &ad1988_auto_smux_mixer))
		return -ENOMEM;

	codec->patch_ops.init = ad1988_auto_init;

	return 0;
}

3220 3221 3222 3223 3224
/*
 */

static int ad1988_parse_auto_config(struct hda_codec *codec)
{
3225
	struct ad198x_spec *spec;
3226
	int err;
3227

3228 3229 3230 3231 3232
	err = alloc_ad_spec(codec);
	if (err < 0)
		return err;
	spec = codec->spec;

3233
	spec->gen.mixer_nid = 0x20;
3234
	spec->gen.mixer_merge_nid = 0x21;
3235
	spec->gen.beep_nid = 0x10;
3236
	set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
3237 3238
	err = ad198x_parse_auto_config(codec);
	if (err < 0)
3239
		goto error;
3240 3241
	err = ad1988_add_spdif_mux_ctl(codec);
	if (err < 0)
3242
		goto error;
3243
	return 0;
3244 3245

 error:
3246
	snd_hda_gen_free(codec);
3247
	return err;
3248 3249
}

3250 3251 3252
/*
 */

3253
#ifdef ENABLE_AD_STATIC_QUIRKS
3254
static const char * const ad1988_models[AD1988_MODEL_LAST] = {
3255 3256 3257 3258 3259 3260 3261
	[AD1988_6STACK]		= "6stack",
	[AD1988_6STACK_DIG]	= "6stack-dig",
	[AD1988_3STACK]		= "3stack",
	[AD1988_3STACK_DIG]	= "3stack-dig",
	[AD1988_LAPTOP]		= "laptop",
	[AD1988_LAPTOP_DIG]	= "laptop-dig",
	[AD1988_AUTO]		= "auto",
3262 3263
};

3264
static const struct snd_pci_quirk ad1988_cfg_tbl[] = {
3265
	SND_PCI_QUIRK(0x1043, 0x81ec, "Asus P5B-DLX", AD1988_6STACK_DIG),
3266
	SND_PCI_QUIRK(0x1043, 0x81f6, "Asus M2N-SLI", AD1988_6STACK_DIG),
3267
	SND_PCI_QUIRK(0x1043, 0x8277, "Asus P5K-E/WIFI-AP", AD1988_6STACK_DIG),
3268
	SND_PCI_QUIRK(0x1043, 0x82c0, "Asus M3N-HT Deluxe", AD1988_6STACK_DIG),
3269
	SND_PCI_QUIRK(0x1043, 0x8311, "Asus P5Q-Premium/Pro", AD1988_6STACK_DIG),
3270 3271 3272
	{}
};

3273 3274 3275
static int patch_ad1988(struct hda_codec *codec)
{
	struct ad198x_spec *spec;
3276
	int err, board_config;
3277

3278
	board_config = snd_hda_check_board_config(codec, AD1988_MODEL_LAST,
3279
						  ad1988_models, ad1988_cfg_tbl);
3280
	if (board_config < 0) {
3281 3282
		printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
		       codec->chip_name);
3283 3284 3285
		board_config = AD1988_AUTO;
	}

3286 3287 3288 3289 3290 3291 3292
	if (board_config == AD1988_AUTO)
		return ad1988_parse_auto_config(codec);

	err = alloc_ad_spec(codec);
	if (err < 0)
		return err;
	spec = codec->spec;
3293

3294 3295 3296
	if (is_rev2(codec))
		snd_printk(KERN_INFO "patch_analog: AD1988A rev.2 is detected, enable workarounds\n");

3297 3298 3299 3300 3301 3302 3303
	err = snd_hda_attach_beep_device(codec, 0x10);
	if (err < 0) {
		ad198x_free(codec);
		return err;
	}
	set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);

3304
	if (!spec->multiout.hp_nid)
3305
		spec->multiout.hp_nid = ad1988_alt_dac_nid[0];
3306 3307 3308 3309 3310
	switch (board_config) {
	case AD1988_6STACK:
	case AD1988_6STACK_DIG:
		spec->multiout.max_channels = 8;
		spec->multiout.num_dacs = 4;
3311
		if (is_rev2(codec))
3312 3313 3314
			spec->multiout.dac_nids = ad1988_6stack_dac_nids_rev2;
		else
			spec->multiout.dac_nids = ad1988_6stack_dac_nids;
3315
		spec->input_mux = &ad1988_6stack_capture_source;
3316
		spec->num_mixers = 2;
3317
		if (is_rev2(codec))
3318 3319 3320
			spec->mixers[0] = ad1988_6stack_mixers1_rev2;
		else
			spec->mixers[0] = ad1988_6stack_mixers1;
3321 3322 3323
		spec->mixers[1] = ad1988_6stack_mixers2;
		spec->num_init_verbs = 1;
		spec->init_verbs[0] = ad1988_6stack_init_verbs;
3324
		if (board_config == AD1988_6STACK_DIG) {
3325 3326 3327 3328 3329 3330 3331 3332
			spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
			spec->dig_in_nid = AD1988_SPDIF_IN;
		}
		break;
	case AD1988_3STACK:
	case AD1988_3STACK_DIG:
		spec->multiout.max_channels = 6;
		spec->multiout.num_dacs = 3;
3333
		if (is_rev2(codec))
3334 3335 3336
			spec->multiout.dac_nids = ad1988_3stack_dac_nids_rev2;
		else
			spec->multiout.dac_nids = ad1988_3stack_dac_nids;
3337 3338 3339
		spec->input_mux = &ad1988_6stack_capture_source;
		spec->channel_mode = ad1988_3stack_modes;
		spec->num_channel_mode = ARRAY_SIZE(ad1988_3stack_modes);
3340
		spec->num_mixers = 2;
3341
		if (is_rev2(codec))
3342 3343 3344 3345
			spec->mixers[0] = ad1988_3stack_mixers1_rev2;
		else
			spec->mixers[0] = ad1988_3stack_mixers1;
		spec->mixers[1] = ad1988_3stack_mixers2;
3346 3347 3348 3349 3350 3351 3352 3353 3354
		spec->num_init_verbs = 1;
		spec->init_verbs[0] = ad1988_3stack_init_verbs;
		if (board_config == AD1988_3STACK_DIG)
			spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
		break;
	case AD1988_LAPTOP:
	case AD1988_LAPTOP_DIG:
		spec->multiout.max_channels = 2;
		spec->multiout.num_dacs = 1;
3355
		spec->multiout.dac_nids = ad1988_3stack_dac_nids;
3356 3357 3358
		spec->input_mux = &ad1988_laptop_capture_source;
		spec->num_mixers = 1;
		spec->mixers[0] = ad1988_laptop_mixers;
3359
		codec->inv_eapd = 1; /* inverted EAPD */
3360 3361 3362 3363 3364 3365 3366
		spec->num_init_verbs = 1;
		spec->init_verbs[0] = ad1988_laptop_init_verbs;
		if (board_config == AD1988_LAPTOP_DIG)
			spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
		break;
	}

3367 3368 3369
	spec->num_adc_nids = ARRAY_SIZE(ad1988_adc_nids);
	spec->adc_nids = ad1988_adc_nids;
	spec->capsrc_nids = ad1988_capsrc_nids;
3370 3371 3372
	spec->mixers[spec->num_mixers++] = ad1988_capture_mixers;
	spec->init_verbs[spec->num_init_verbs++] = ad1988_capture_init_verbs;
	if (spec->multiout.dig_out_nid) {
3373 3374 3375 3376 3377
		if (codec->vendor_id >= 0x11d4989a) {
			spec->mixers[spec->num_mixers++] =
				ad1989_spdif_out_mixers;
			spec->init_verbs[spec->num_init_verbs++] =
				ad1989_spdif_init_verbs;
3378
			codec->slave_dig_outs = ad1989b_slave_dig_outs;
3379 3380 3381 3382 3383 3384
		} else {
			spec->mixers[spec->num_mixers++] =
				ad1988_spdif_out_mixers;
			spec->init_verbs[spec->num_init_verbs++] =
				ad1988_spdif_init_verbs;
		}
3385
	}
3386
	if (spec->dig_in_nid && codec->vendor_id < 0x11d4989a) {
3387
		spec->mixers[spec->num_mixers++] = ad1988_spdif_in_mixers;
3388 3389 3390
		spec->init_verbs[spec->num_init_verbs++] =
			ad1988_spdif_in_init_verbs;
	}
3391 3392 3393 3394 3395 3396 3397 3398

	codec->patch_ops = ad198x_patch_ops;
	switch (board_config) {
	case AD1988_LAPTOP:
	case AD1988_LAPTOP_DIG:
		codec->patch_ops.unsol_event = ad1988_laptop_unsol_event;
		break;
	}
3399
#ifdef CONFIG_PM
3400 3401
	spec->loopback.amplist = ad1988_loopbacks;
#endif
3402
	spec->vmaster_nid = 0x04;
3403

3404
	codec->no_trigger_sense = 1;
3405
	codec->no_sticky_stream = 1;
3406

3407 3408
	return 0;
}
3409 3410 3411
#else /* ENABLE_AD_STATIC_QUIRKS */
#define patch_ad1988	ad1988_parse_auto_config
#endif /* ENABLE_AD_STATIC_QUIRKS */
3412 3413


3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431
/*
 * AD1884 / AD1984
 *
 * port-B - front line/mic-in
 * port-E - aux in/out
 * port-F - aux in/out
 * port-C - rear line/mic-in
 * port-D - rear line/hp-out
 * port-A - front line/hp-out
 *
 * AD1984 = AD1884 + two digital mic-ins
 *
 * FIXME:
 * For simplicity, we share the single DAC for both HP and line-outs
 * right now.  The inidividual playbacks could be easily implemented,
 * but no build-up framework is given, so far.
 */

3432
#ifdef ENABLE_AD_STATIC_QUIRKS
3433
static const hda_nid_t ad1884_dac_nids[1] = {
3434 3435 3436
	0x04,
};

3437
static const hda_nid_t ad1884_adc_nids[2] = {
3438 3439 3440
	0x08, 0x09,
};

3441
static const hda_nid_t ad1884_capsrc_nids[2] = {
3442 3443 3444 3445 3446
	0x0c, 0x0d,
};

#define AD1884_SPDIF_OUT	0x02

3447
static const struct hda_input_mux ad1884_capture_source = {
3448 3449 3450 3451 3452 3453 3454 3455 3456
	.num_items = 4,
	.items = {
		{ "Front Mic", 0x0 },
		{ "Mic", 0x1 },
		{ "CD", 0x2 },
		{ "Mix", 0x3 },
	},
};

3457
static const struct snd_kcontrol_new ad1884_base_mixers[] = {
3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
	/* HDA_CODEC_VOLUME_IDX("PCM Playback Volume", 1, 0x03, 0x0, HDA_OUTPUT), */
	HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x02, HDA_INPUT),
	HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x02, HDA_INPUT),
3470 3471
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x15, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Front Mic Boost Volume", 0x14, 0x0, HDA_INPUT),
3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500
	HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		/* The multiple "Capture Source" controls confuse alsamixer
		 * So call somewhat different..
		 */
		/* .name = "Capture Source", */
		.name = "Input Source",
		.count = 2,
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
	},
	/* SPDIF controls */
	HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
		/* identical with ad1983 */
		.info = ad1983_spdif_route_info,
		.get = ad1983_spdif_route_get,
		.put = ad1983_spdif_route_put,
	},
	{ } /* end */
};

3501
static const struct snd_kcontrol_new ad1984_dmic_mixers[] = {
3502 3503 3504
	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x05, 0x0, HDA_INPUT),
	HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x05, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME_IDX("Digital Mic Capture Volume", 1, 0x06, 0x0,
3505
			     HDA_INPUT),
3506
	HDA_CODEC_MUTE_IDX("Digital Mic Capture Switch", 1, 0x06, 0x0,
3507
			   HDA_INPUT),
3508 3509 3510 3511 3512 3513
	{ } /* end */
};

/*
 * initialization verbs
 */
3514
static const struct hda_verb ad1884_init_verbs[] = {
3515
	/* DACs; mute as default */
3516 3517
	{0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
	{0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541
	/* Port-A (HP) mixer */
	{0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
	{0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Port-A pin */
	{0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* HP selector - select DAC2 */
	{0x22, AC_VERB_SET_CONNECT_SEL, 0x1},
	/* Port-D (Line-out) mixer */
	{0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
	{0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Port-D pin */
	{0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* Mono-out mixer */
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Mono-out pin */
	{0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* Mono selector */
	{0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
	/* Port-B (front mic) pin */
	{0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3542
	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3543 3544
	/* Port-C (rear mic) pin */
	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3545
	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558
	/* Analog mixer; mute as default */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
	/* Analog Mix output amp */
	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
	/* SPDIF output selector */
	{0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
	{ } /* end */
};

3559
#ifdef CONFIG_PM
3560
static const struct hda_amp_list ad1884_loopbacks[] = {
3561 3562 3563 3564 3565 3566 3567 3568
	{ 0x20, HDA_INPUT, 0 }, /* Front Mic */
	{ 0x20, HDA_INPUT, 1 }, /* Mic */
	{ 0x20, HDA_INPUT, 2 }, /* CD */
	{ 0x20, HDA_INPUT, 4 }, /* Docking */
	{ } /* end */
};
#endif

3569
static const char * const ad1884_slave_vols[] = {
3570
	"PCM", "Mic", "Mono", "Front Mic", "Mic", "CD",
3571
	"Internal Mic", "Dock Mic", /* "Beep", */ "IEC958",
3572 3573 3574
	NULL
};

3575 3576 3577 3578 3579 3580 3581 3582 3583 3584
enum {
	AD1884_AUTO,
	AD1884_BASIC,
	AD1884_MODELS
};

static const char * const ad1884_models[AD1884_MODELS] = {
	[AD1884_AUTO]		= "auto",
	[AD1884_BASIC]		= "basic",
};
3585
#endif /* ENABLE_AD_STATIC_QUIRKS */
3586

3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606

/* set the upper-limit for mixer amp to 0dB for avoiding the possible
 * damage by overloading
 */
static void ad1884_fixup_amp_override(struct hda_codec *codec,
				      const struct hda_fixup *fix, int action)
{
	if (action == HDA_FIXUP_ACT_PRE_PROBE)
		snd_hda_override_amp_caps(codec, 0x20, HDA_INPUT,
					  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
					  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
					  (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
					  (1 << AC_AMPCAP_MUTE_SHIFT));
}

static void ad1884_fixup_hp_eapd(struct hda_codec *codec,
				 const struct hda_fixup *fix, int action)
{
	struct ad198x_spec *spec = codec->spec;

3607 3608 3609 3610 3611
	switch (action) {
	case HDA_FIXUP_ACT_PRE_PROBE:
		spec->gen.vmaster_mute.hook = ad_vmaster_eapd_hook;
		break;
	case HDA_FIXUP_ACT_PROBE:
3612 3613 3614 3615
		if (spec->gen.autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
			spec->eapd_nid = spec->gen.autocfg.line_out_pins[0];
		else
			spec->eapd_nid = spec->gen.autocfg.speaker_pins[0];
3616
		break;
3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643
	}
}

enum {
	AD1884_FIXUP_AMP_OVERRIDE,
	AD1884_FIXUP_HP_EAPD,
};

static const struct hda_fixup ad1884_fixups[] = {
	[AD1884_FIXUP_AMP_OVERRIDE] = {
		.type = HDA_FIXUP_FUNC,
		.v.func = ad1884_fixup_amp_override,
	},
	[AD1884_FIXUP_HP_EAPD] = {
		.type = HDA_FIXUP_FUNC,
		.v.func = ad1884_fixup_hp_eapd,
		.chained = true,
		.chain_id = AD1884_FIXUP_AMP_OVERRIDE,
	},
};

static const struct snd_pci_quirk ad1884_fixup_tbl[] = {
	SND_PCI_QUIRK_VENDOR(0x103c, "HP", AD1884_FIXUP_HP_EAPD),
	{}
};


3644 3645
static int ad1884_parse_auto_config(struct hda_codec *codec)
{
3646
	struct ad198x_spec *spec;
3647
	int err;
3648

3649 3650 3651 3652 3653
	err = alloc_ad_spec(codec);
	if (err < 0)
		return err;
	spec = codec->spec;

3654
	spec->gen.mixer_nid = 0x20;
3655
	spec->gen.beep_nid = 0x10;
3656
	set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
3657 3658 3659 3660

	snd_hda_pick_fixup(codec, NULL, ad1884_fixup_tbl, ad1884_fixups);
	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);

3661 3662
	err = ad198x_parse_auto_config(codec);
	if (err < 0)
3663
		goto error;
3664 3665
	err = ad1983_add_spdif_mux_ctl(codec);
	if (err < 0)
3666
		goto error;
3667 3668 3669

	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);

3670
	return 0;
3671

3672
 error:
3673
	snd_hda_gen_free(codec);
3674
	return err;
3675 3676
}

3677
#ifdef ENABLE_AD_STATIC_QUIRKS
3678
static int patch_ad1884_basic(struct hda_codec *codec)
3679 3680
{
	struct ad198x_spec *spec;
3681
	int err;
3682

3683 3684 3685 3686
	err = alloc_ad_spec(codec);
	if (err < 0)
		return err;
	spec = codec->spec;
3687

3688 3689 3690 3691 3692 3693 3694
	err = snd_hda_attach_beep_device(codec, 0x10);
	if (err < 0) {
		ad198x_free(codec);
		return err;
	}
	set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);

3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707
	spec->multiout.max_channels = 2;
	spec->multiout.num_dacs = ARRAY_SIZE(ad1884_dac_nids);
	spec->multiout.dac_nids = ad1884_dac_nids;
	spec->multiout.dig_out_nid = AD1884_SPDIF_OUT;
	spec->num_adc_nids = ARRAY_SIZE(ad1884_adc_nids);
	spec->adc_nids = ad1884_adc_nids;
	spec->capsrc_nids = ad1884_capsrc_nids;
	spec->input_mux = &ad1884_capture_source;
	spec->num_mixers = 1;
	spec->mixers[0] = ad1884_base_mixers;
	spec->num_init_verbs = 1;
	spec->init_verbs[0] = ad1884_init_verbs;
	spec->spdif_route = 0;
3708
#ifdef CONFIG_PM
3709 3710
	spec->loopback.amplist = ad1884_loopbacks;
#endif
3711 3712 3713
	spec->vmaster_nid = 0x04;
	/* we need to cover all playback volumes */
	spec->slave_vols = ad1884_slave_vols;
3714 3715
	/* slaves may contain input volumes, so we can't raise to 0dB blindly */
	spec->avoid_init_slave_vol = 1;
3716 3717 3718

	codec->patch_ops = ad198x_patch_ops;

3719
	codec->no_trigger_sense = 1;
3720
	codec->no_sticky_stream = 1;
3721

3722 3723 3724
	return 0;
}

3725 3726 3727 3728 3729 3730
static int patch_ad1884(struct hda_codec *codec)
{
	int board_config;

	board_config = snd_hda_check_board_config(codec, AD1884_MODELS,
						  ad1884_models, NULL);
3731 3732 3733 3734 3735 3736
	if (board_config < 0) {
		printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
		       codec->chip_name);
		board_config = AD1884_AUTO;
	}

3737
	if (board_config == AD1884_AUTO)
3738
		return ad1884_parse_auto_config(codec);
3739 3740 3741
	else
		return patch_ad1884_basic(codec);
}
3742 3743 3744
#else /* ENABLE_AD_STATIC_QUIRKS */
#define patch_ad1884	ad1884_parse_auto_config
#endif /* ENABLE_AD_STATIC_QUIRKS */
3745

3746 3747

#ifdef ENABLE_AD_STATIC_QUIRKS
3748 3749 3750
/*
 * Lenovo Thinkpad T61/X61
 */
3751
static const struct hda_input_mux ad1984_thinkpad_capture_source = {
3752
	.num_items = 4,
3753 3754 3755 3756
	.items = {
		{ "Mic", 0x0 },
		{ "Internal Mic", 0x1 },
		{ "Mix", 0x3 },
3757
		{ "Dock Mic", 0x4 },
3758 3759 3760
	},
};

3761 3762 3763 3764

/*
 * Dell Precision T3400
 */
3765
static const struct hda_input_mux ad1984_dell_desktop_capture_source = {
3766 3767 3768 3769 3770 3771 3772 3773 3774
	.num_items = 3,
	.items = {
		{ "Front Mic", 0x0 },
		{ "Line-In", 0x1 },
		{ "Mix", 0x3 },
	},
};


3775
static const struct snd_kcontrol_new ad1984_thinkpad_mixers[] = {
3776 3777 3778 3779 3780 3781 3782 3783
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
	/* HDA_CODEC_VOLUME_IDX("PCM Playback Volume", 1, 0x03, 0x0, HDA_OUTPUT), */
	HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Speaker Playback Switch", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
3784 3785
	HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x03, HDA_INPUT),
	HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x03, HDA_INPUT),
3786 3787
	HDA_CODEC_VOLUME("Dock Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
	HDA_CODEC_MUTE("Dock Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
3788 3789 3790
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x14, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Internal Mic Boost Volume", 0x15, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Dock Mic Boost Volume", 0x25, 0x0, HDA_OUTPUT),
3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806
	HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		/* The multiple "Capture Source" controls confuse alsamixer
		 * So call somewhat different..
		 */
		/* .name = "Capture Source", */
		.name = "Input Source",
		.count = 2,
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
	},
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816
	/* SPDIF controls */
	HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
		/* identical with ad1983 */
		.info = ad1983_spdif_route_info,
		.get = ad1983_spdif_route_get,
		.put = ad1983_spdif_route_put,
	},
3817 3818 3819 3820
	{ } /* end */
};

/* additional verbs */
3821
static const struct hda_verb ad1984_thinkpad_init_verbs[] = {
3822 3823 3824 3825
	/* Port-E (docking station mic) pin */
	{0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* docking mic boost */
3826
	{0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3827 3828
	/* Analog PC Beeper - allow firmware/ACPI beeps */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3) | 0x1a},
3829 3830
	/* Analog mixer - docking mic; mute as default */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3831 3832
	/* enable EAPD bit */
	{0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x02},
3833 3834 3835
	{ } /* end */
};

3836 3837 3838
/*
 * Dell Precision T3400
 */
3839
static const struct snd_kcontrol_new ad1984_dell_desktop_mixers[] = {
3840 3841 3842 3843 3844 3845 3846 3847 3848
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Speaker Playback Switch", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_VOLUME("Line-In Playback Volume", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_MUTE("Line-In Playback Switch", 0x20, 0x01, HDA_INPUT),
3849 3850
	HDA_CODEC_VOLUME("Line-In Boost Volume", 0x15, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Front Mic Boost Volume", 0x14, 0x0, HDA_INPUT),
3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869
	HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		/* The multiple "Capture Source" controls confuse alsamixer
		 * So call somewhat different..
		 */
		/* .name = "Capture Source", */
		.name = "Input Source",
		.count = 2,
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
	},
	{ } /* end */
};

3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885
/* Digial MIC ADC NID 0x05 + 0x06 */
static int ad1984_pcm_dmic_prepare(struct hda_pcm_stream *hinfo,
				   struct hda_codec *codec,
				   unsigned int stream_tag,
				   unsigned int format,
				   struct snd_pcm_substream *substream)
{
	snd_hda_codec_setup_stream(codec, 0x05 + substream->number,
				   stream_tag, 0, format);
	return 0;
}

static int ad1984_pcm_dmic_cleanup(struct hda_pcm_stream *hinfo,
				   struct hda_codec *codec,
				   struct snd_pcm_substream *substream)
{
3886
	snd_hda_codec_cleanup_stream(codec, 0x05 + substream->number);
3887 3888 3889
	return 0;
}

3890
static const struct hda_pcm_stream ad1984_pcm_dmic_capture = {
3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919
	.substreams = 2,
	.channels_min = 2,
	.channels_max = 2,
	.nid = 0x05,
	.ops = {
		.prepare = ad1984_pcm_dmic_prepare,
		.cleanup = ad1984_pcm_dmic_cleanup
	},
};

static int ad1984_build_pcms(struct hda_codec *codec)
{
	struct ad198x_spec *spec = codec->spec;
	struct hda_pcm *info;
	int err;

	err = ad198x_build_pcms(codec);
	if (err < 0)
		return err;

	info = spec->pcm_rec + codec->num_pcms;
	codec->num_pcms++;
	info->name = "AD1984 Digital Mic";
	info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad1984_pcm_dmic_capture;
	return 0;
}

/* models */
enum {
3920
	AD1984_AUTO,
3921 3922
	AD1984_BASIC,
	AD1984_THINKPAD,
3923
	AD1984_DELL_DESKTOP,
3924 3925 3926
	AD1984_MODELS
};

3927
static const char * const ad1984_models[AD1984_MODELS] = {
3928
	[AD1984_AUTO]		= "auto",
3929 3930
	[AD1984_BASIC]		= "basic",
	[AD1984_THINKPAD]	= "thinkpad",
3931
	[AD1984_DELL_DESKTOP]	= "dell_desktop",
3932 3933
};

3934
static const struct snd_pci_quirk ad1984_cfg_tbl[] = {
3935
	/* Lenovo Thinkpad T61/X61 */
T
Takashi Iwai 已提交
3936
	SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo Thinkpad", AD1984_THINKPAD),
3937
	SND_PCI_QUIRK(0x1028, 0x0214, "Dell T3400", AD1984_DELL_DESKTOP),
3938
	SND_PCI_QUIRK(0x1028, 0x0233, "Dell Latitude E6400", AD1984_DELL_DESKTOP),
3939 3940 3941 3942 3943 3944 3945 3946
	{}
};

static int patch_ad1984(struct hda_codec *codec)
{
	struct ad198x_spec *spec;
	int board_config, err;

3947 3948
	board_config = snd_hda_check_board_config(codec, AD1984_MODELS,
						  ad1984_models, ad1984_cfg_tbl);
3949 3950 3951 3952 3953 3954
	if (board_config < 0) {
		printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
		       codec->chip_name);
		board_config = AD1984_AUTO;
	}

3955
	if (board_config == AD1984_AUTO)
3956
		return ad1884_parse_auto_config(codec);
3957 3958

	err = patch_ad1884_basic(codec);
3959 3960 3961
	if (err < 0)
		return err;
	spec = codec->spec;
3962

3963 3964 3965 3966 3967 3968 3969
	switch (board_config) {
	case AD1984_BASIC:
		/* additional digital mics */
		spec->mixers[spec->num_mixers++] = ad1984_dmic_mixers;
		codec->patch_ops.build_pcms = ad1984_build_pcms;
		break;
	case AD1984_THINKPAD:
3970 3971 3972 3973 3974 3975
		if (codec->subsystem_id == 0x17aa20fb) {
			/* Thinpad X300 does not have the ability to do SPDIF,
			   or attach to docking station to use SPDIF */
			spec->multiout.dig_out_nid = 0;
		} else
			spec->multiout.dig_out_nid = AD1884_SPDIF_OUT;
3976 3977 3978
		spec->input_mux = &ad1984_thinkpad_capture_source;
		spec->mixers[0] = ad1984_thinkpad_mixers;
		spec->init_verbs[spec->num_init_verbs++] = ad1984_thinkpad_init_verbs;
3979
		spec->analog_beep = 1;
3980
		break;
3981 3982 3983 3984 3985
	case AD1984_DELL_DESKTOP:
		spec->multiout.dig_out_nid = 0;
		spec->input_mux = &ad1984_dell_desktop_capture_source;
		spec->mixers[0] = ad1984_dell_desktop_mixers;
		break;
3986 3987 3988
	}
	return 0;
}
3989 3990 3991
#else /* ENABLE_AD_STATIC_QUIRKS */
#define patch_ad1984	ad1884_parse_auto_config
#endif /* ENABLE_AD_STATIC_QUIRKS */
3992 3993


3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011
/*
 * AD1883 / AD1884A / AD1984A / AD1984B
 *
 * port-B (0x14) - front mic-in
 * port-E (0x1c) - rear mic-in
 * port-F (0x16) - CD / ext out
 * port-C (0x15) - rear line-in
 * port-D (0x12) - rear line-out
 * port-A (0x11) - front hp-out
 *
 * AD1984A = AD1884A + digital-mic
 * AD1883 = equivalent with AD1984A
 * AD1984B = AD1984A + extra SPDIF-out
 *
 * FIXME:
 * We share the single DAC for both HP and line-outs (see AD1884/1984).
 */

4012
#ifdef ENABLE_AD_STATIC_QUIRKS
4013
static const hda_nid_t ad1884a_dac_nids[1] = {
4014 4015 4016 4017 4018 4019 4020 4021
	0x03,
};

#define ad1884a_adc_nids	ad1884_adc_nids
#define ad1884a_capsrc_nids	ad1884_capsrc_nids

#define AD1884A_SPDIF_OUT	0x02

4022
static const struct hda_input_mux ad1884a_capture_source = {
4023 4024 4025 4026 4027 4028 4029 4030 4031 4032
	.num_items = 5,
	.items = {
		{ "Front Mic", 0x0 },
		{ "Mic", 0x4 },
		{ "Line", 0x1 },
		{ "CD", 0x2 },
		{ "Mix", 0x3 },
	},
};

4033
static const struct snd_kcontrol_new ad1884a_base_mixers[] = {
4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049
	HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
	HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
	HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x02, HDA_INPUT),
	HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x02, HDA_INPUT),
4050 4051 4052
	HDA_CODEC_VOLUME("Front Mic Boost Volume", 0x14, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Line Boost Volume", 0x15, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x25, 0x0, HDA_OUTPUT),
4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084
	HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		/* The multiple "Capture Source" controls confuse alsamixer
		 * So call somewhat different..
		 */
		/* .name = "Capture Source", */
		.name = "Input Source",
		.count = 2,
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
	},
	/* SPDIF controls */
	HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
		/* identical with ad1983 */
		.info = ad1983_spdif_route_info,
		.get = ad1983_spdif_route_get,
		.put = ad1983_spdif_route_put,
	},
	{ } /* end */
};

/*
 * initialization verbs
 */
4085
static const struct hda_verb ad1884a_init_verbs[] = {
4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108
	/* DACs; unmute as default */
	{0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
	{0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
	/* Port-A (HP) mixer - route only from analog mixer */
	{0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Port-A pin */
	{0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* Port-D (Line-out) mixer - route only from analog mixer */
	{0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Port-D pin */
	{0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* Mono-out mixer - route only from analog mixer */
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Mono-out pin */
	{0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* Port-B (front mic) pin */
	{0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4109
	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4110 4111
	/* Port-C (rear line-in) pin */
	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
4112
	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138
	/* Port-E (rear mic) pin */
	{0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	{0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* no boost */
	/* Port-F (CD) pin */
	{0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* Analog mixer; mute as default */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)}, /* aux */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
	/* Analog Mix output amp */
	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* capture sources */
	{0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
	{0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	{0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
	{0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* SPDIF output amp */
	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
	{ } /* end */
};

4139
#ifdef CONFIG_PM
4140
static const struct hda_amp_list ad1884a_loopbacks[] = {
4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159
	{ 0x20, HDA_INPUT, 0 }, /* Front Mic */
	{ 0x20, HDA_INPUT, 1 }, /* Mic */
	{ 0x20, HDA_INPUT, 2 }, /* CD */
	{ 0x20, HDA_INPUT, 4 }, /* Docking */
	{ } /* end */
};
#endif

/*
 * Laptop model
 *
 * Port A: Headphone jack
 * Port B: MIC jack
 * Port C: Internal MIC
 * Port D: Dock Line Out (if enabled)
 * Port E: Dock Line In (if enabled)
 * Port F: Internal speakers
 */

4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171
static int ad1884a_mobile_master_sw_put(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	int ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
	int mute = (!ucontrol->value.integer.value[0] &&
		    !ucontrol->value.integer.value[1]);
	/* toggle GPIO1 according to the mute state */
	snd_hda_codec_write_cache(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
			    mute ? 0x02 : 0x0);
	return ret;
}
4172

4173
static const struct snd_kcontrol_new ad1884a_laptop_mixers[] = {
4174
	HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
4175 4176 4177
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Master Playback Switch",
4178
		.subdevice = HDA_SUBDEV_AMP_FLAG,
4179 4180 4181 4182 4183
		.info = snd_hda_mixer_amp_switch_info,
		.get = snd_hda_mixer_amp_switch_get,
		.put = ad1884a_mobile_master_sw_put,
		.private_value = HDA_COMPOSE_AMP_VAL(0x21, 3, 0, HDA_OUTPUT),
	},
4184 4185 4186 4187 4188 4189 4190 4191 4192
	HDA_CODEC_MUTE("Dock Playback Switch", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_VOLUME("Dock Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
	HDA_CODEC_MUTE("Dock Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
4193 4194 4195
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x14, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Internal Mic Boost Volume", 0x15, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Dock Mic Boost Volume", 0x25, 0x0, HDA_OUTPUT),
4196 4197 4198 4199 4200
	HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
	{ } /* end */
};

4201
static const struct snd_kcontrol_new ad1884a_mobile_mixers[] = {
4202
	HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
4203 4204 4205 4206
	/*HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),*/
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Master Playback Switch",
4207
		.subdevice = HDA_SUBDEV_AMP_FLAG,
4208 4209 4210 4211 4212
		.info = snd_hda_mixer_amp_switch_info,
		.get = snd_hda_mixer_amp_switch_get,
		.put = ad1884a_mobile_master_sw_put,
		.private_value = HDA_COMPOSE_AMP_VAL(0x21, 3, 0, HDA_OUTPUT),
	},
4213 4214
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
4215 4216
	HDA_CODEC_VOLUME("Mic Capture Volume", 0x14, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Internal Mic Capture Volume", 0x15, 0x0, HDA_INPUT),
4217 4218 4219 4220 4221
	HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
	{ } /* end */
};

4222 4223 4224 4225 4226
/* mute internal speaker if HP is plugged */
static void ad1884a_hp_automute(struct hda_codec *codec)
{
	unsigned int present;

4227
	present = snd_hda_jack_detect(codec, 0x11);
4228 4229 4230 4231 4232 4233
	snd_hda_codec_amp_stereo(codec, 0x16, HDA_OUTPUT, 0,
				 HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
	snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_EAPD_BTLENABLE,
			    present ? 0x00 : 0x02);
}

4234 4235 4236 4237 4238
/* switch to external mic if plugged */
static void ad1884a_hp_automic(struct hda_codec *codec)
{
	unsigned int present;

4239
	present = snd_hda_jack_detect(codec, 0x14);
4240 4241 4242 4243
	snd_hda_codec_write(codec, 0x0c, 0, AC_VERB_SET_CONNECT_SEL,
			    present ? 0 : 1);
}

4244
#define AD1884A_HP_EVENT		0x37
4245
#define AD1884A_MIC_EVENT		0x36
4246 4247 4248 4249

/* unsolicited event for HP jack sensing */
static void ad1884a_hp_unsol_event(struct hda_codec *codec, unsigned int res)
{
4250 4251 4252 4253 4254 4255 4256 4257
	switch (res >> 26) {
	case AD1884A_HP_EVENT:
		ad1884a_hp_automute(codec);
		break;
	case AD1884A_MIC_EVENT:
		ad1884a_hp_automic(codec);
		break;
	}
4258 4259 4260 4261 4262 4263 4264
}

/* initialize jack-sensing, too */
static int ad1884a_hp_init(struct hda_codec *codec)
{
	ad198x_init(codec);
	ad1884a_hp_automute(codec);
4265
	ad1884a_hp_automic(codec);
4266 4267 4268
	return 0;
}

4269 4270 4271 4272 4273
/* mute internal speaker if HP or docking HP is plugged */
static void ad1884a_laptop_automute(struct hda_codec *codec)
{
	unsigned int present;

4274 4275 4276
	present = snd_hda_jack_detect(codec, 0x11);
	if (!present)
		present = snd_hda_jack_detect(codec, 0x12);
4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287
	snd_hda_codec_amp_stereo(codec, 0x16, HDA_OUTPUT, 0,
				 HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
	snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_EAPD_BTLENABLE,
			    present ? 0x00 : 0x02);
}

/* switch to external mic if plugged */
static void ad1884a_laptop_automic(struct hda_codec *codec)
{
	unsigned int idx;

4288
	if (snd_hda_jack_detect(codec, 0x14))
4289
		idx = 0;
4290
	else if (snd_hda_jack_detect(codec, 0x1c))
4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319
		idx = 4;
	else
		idx = 1;
	snd_hda_codec_write(codec, 0x0c, 0, AC_VERB_SET_CONNECT_SEL, idx);
}

/* unsolicited event for HP jack sensing */
static void ad1884a_laptop_unsol_event(struct hda_codec *codec,
				       unsigned int res)
{
	switch (res >> 26) {
	case AD1884A_HP_EVENT:
		ad1884a_laptop_automute(codec);
		break;
	case AD1884A_MIC_EVENT:
		ad1884a_laptop_automic(codec);
		break;
	}
}

/* initialize jack-sensing, too */
static int ad1884a_laptop_init(struct hda_codec *codec)
{
	ad198x_init(codec);
	ad1884a_laptop_automute(codec);
	ad1884a_laptop_automic(codec);
	return 0;
}

4320
/* additional verbs for laptop model */
4321
static const struct hda_verb ad1884a_laptop_verbs[] = {
4322 4323 4324 4325 4326
	/* Port-A (HP) pin - always unmuted */
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	/* Port-F (int speaker) mixer - route only from analog mixer */
	{0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4327 4328
	/* Port-F (int speaker) pin */
	{0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4329
	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4330 4331
	/* required for compaq 6530s/6531s speaker output */
	{0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4332 4333 4334 4335
	/* Port-C pin - internal mic-in */
	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0x7002}, /* raise mic as default */
	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0x7002}, /* raise mic as default */
4336 4337
	/* Port-D (docking line-out) pin - default unmuted */
	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4338 4339 4340 4341
	/* analog mix */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
	/* unsolicited event for pin-sense */
	{0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
4342
	{0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
4343
	{0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_MIC_EVENT},
4344
	{0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_MIC_EVENT},
4345 4346 4347 4348
	/* allow to touch GPIO1 (for mute control) */
	{0x01, AC_VERB_SET_GPIO_MASK, 0x02},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x02}, /* first muted */
4349 4350 4351
	{ } /* end */
};

4352
static const struct hda_verb ad1884a_mobile_verbs[] = {
4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391
	/* DACs; unmute as default */
	{0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
	{0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
	/* Port-A (HP) mixer - route only from analog mixer */
	{0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Port-A pin */
	{0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	/* Port-A (HP) pin - always unmuted */
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	/* Port-B (mic jack) pin */
	{0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0x7002}, /* raise mic as default */
	/* Port-C (int mic) pin */
	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0x7002}, /* raise mic as default */
	/* Port-F (int speaker) mixer - route only from analog mixer */
	{0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Port-F pin */
	{0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* Analog mixer; mute as default */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
	/* Analog Mix output amp */
	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* capture sources */
	/* {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0}, */ /* set via unsol */
	{0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	{0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
	{0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* unsolicited event for pin-sense */
	{0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
	{0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_MIC_EVENT},
4392 4393 4394 4395
	/* allow to touch GPIO1 (for mute control) */
	{0x01, AC_VERB_SET_GPIO_MASK, 0x02},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x02}, /* first muted */
4396 4397 4398
	{ } /* end */
};

4399 4400 4401 4402 4403 4404 4405 4406
/*
 * Thinkpad X300
 * 0x11 - HP
 * 0x12 - speaker
 * 0x14 - mic-in
 * 0x17 - built-in mic
 */

4407
static const struct hda_verb ad1984a_thinkpad_verbs[] = {
4408 4409 4410 4411 4412 4413 4414 4415 4416 4417
	/* HP unmute */
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	/* analog mix */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
	/* turn on EAPD */
	{0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x02},
	/* unsolicited event for pin-sense */
	{0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
	/* internal mic - dmic */
	{0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
4418 4419 4420
	/* set magic COEFs for dmic */
	{0x01, AC_VERB_SET_COEF_INDEX, 0x13f7},
	{0x01, AC_VERB_SET_PROC_COEF, 0x08},
4421 4422 4423
	{ } /* end */
};

4424
static const struct snd_kcontrol_new ad1984a_thinkpad_mixers[] = {
4425 4426 4427 4428 4429 4430
	HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
4431 4432
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x14, 0x0, HDA_INPUT),
	HDA_CODEC_VOLUME("Internal Mic Boost Volume", 0x17, 0x0, HDA_INPUT),
4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444
	HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Capture Source",
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
	},
	{ } /* end */
};

4445
static const struct hda_input_mux ad1984a_thinkpad_capture_source = {
4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458
	.num_items = 3,
	.items = {
		{ "Mic", 0x0 },
		{ "Internal Mic", 0x5 },
		{ "Mix", 0x3 },
	},
};

/* mute internal speaker if HP is plugged */
static void ad1984a_thinkpad_automute(struct hda_codec *codec)
{
	unsigned int present;

4459
	present = snd_hda_jack_detect(codec, 0x11);
4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480
	snd_hda_codec_amp_stereo(codec, 0x12, HDA_OUTPUT, 0,
				 HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
}

/* unsolicited event for HP jack sensing */
static void ad1984a_thinkpad_unsol_event(struct hda_codec *codec,
					 unsigned int res)
{
	if ((res >> 26) != AD1884A_HP_EVENT)
		return;
	ad1984a_thinkpad_automute(codec);
}

/* initialize jack-sensing, too */
static int ad1984a_thinkpad_init(struct hda_codec *codec)
{
	ad198x_init(codec);
	ad1984a_thinkpad_automute(codec);
	return 0;
}

4481 4482 4483 4484 4485 4486 4487
/*
 * Precision R5500
 * 0x12 - HP/line-out
 * 0x13 - speaker (mono)
 * 0x15 - mic-in
 */

4488
static const struct hda_verb ad1984a_precision_verbs[] = {
4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513
	/* Unmute main output path */
	{0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE + 0x1f}, /* 0dB */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(5) + 0x17}, /* 0dB */
	/* Analog mixer; mute as default */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
	/* Select mic as input */
	{0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
	{0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE + 0x27}, /* 0dB */
	/* Configure as mic */
	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0x7002}, /* raise mic as default */
	/* HP unmute */
	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	/* turn on EAPD */
	{0x13, AC_VERB_SET_EAPD_BTLENABLE, 0x02},
	/* unsolicited event for pin-sense */
	{0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
	{ } /* end */
};

4514
static const struct snd_kcontrol_new ad1984a_precision_mixers[] = {
4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558
	HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x15, 0x0, HDA_INPUT),
	HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Speaker Playback Volume", 0x13, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
	{ } /* end */
};


/* mute internal speaker if HP is plugged */
static void ad1984a_precision_automute(struct hda_codec *codec)
{
	unsigned int present;

	present = snd_hda_jack_detect(codec, 0x12);
	snd_hda_codec_amp_stereo(codec, 0x13, HDA_OUTPUT, 0,
				 HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
}


/* unsolicited event for HP jack sensing */
static void ad1984a_precision_unsol_event(struct hda_codec *codec,
					 unsigned int res)
{
	if ((res >> 26) != AD1884A_HP_EVENT)
		return;
	ad1984a_precision_automute(codec);
}

/* initialize jack-sensing, too */
static int ad1984a_precision_init(struct hda_codec *codec)
{
	ad198x_init(codec);
	ad1984a_precision_automute(codec);
	return 0;
}


4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569
/*
 * HP Touchsmart
 * port-A (0x11)      - front hp-out
 * port-B (0x14)      - unused
 * port-C (0x15)      - unused
 * port-D (0x12)      - rear line out
 * port-E (0x1c)      - front mic-in
 * port-F (0x16)      - Internal speakers
 * digital-mic (0x17) - Internal mic
 */

4570
static const struct hda_verb ad1984a_touchsmart_verbs[] = {
4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621
	/* DACs; unmute as default */
	{0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
	{0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
	/* Port-A (HP) mixer - route only from analog mixer */
	{0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Port-A pin */
	{0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	/* Port-A (HP) pin - always unmuted */
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	/* Port-E (int speaker) mixer - route only from analog mixer */
	{0x25, AC_VERB_SET_AMP_GAIN_MUTE, 0x03},
	/* Port-E pin */
	{0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
	{0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	/* Port-F (int speaker) mixer - route only from analog mixer */
	{0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Port-F pin */
	{0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* Analog mixer; mute as default */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
	/* Analog Mix output amp */
	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* capture sources */
	/* {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0}, */ /* set via unsol */
	{0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	{0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
	{0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* unsolicited event for pin-sense */
	{0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
	{0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_MIC_EVENT},
	/* allow to touch GPIO1 (for mute control) */
	{0x01, AC_VERB_SET_GPIO_MASK, 0x02},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x02}, /* first muted */
	/* internal mic - dmic */
	{0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
	/* set magic COEFs for dmic */
	{0x01, AC_VERB_SET_COEF_INDEX, 0x13f7},
	{0x01, AC_VERB_SET_PROC_COEF, 0x08},
	{ } /* end */
};

4622
static const struct snd_kcontrol_new ad1984a_touchsmart_mixers[] = {
4623 4624 4625 4626
	HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
/*	HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),*/
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4627
		.subdevice = HDA_SUBDEV_AMP_FLAG,
4628 4629 4630 4631 4632 4633 4634 4635 4636 4637
		.name = "Master Playback Switch",
		.info = snd_hda_mixer_amp_switch_info,
		.get = snd_hda_mixer_amp_switch_get,
		.put = ad1884a_mobile_master_sw_put,
		.private_value = HDA_COMPOSE_AMP_VAL(0x21, 3, 0, HDA_OUTPUT),
	},
	HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
	HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
	HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
4638 4639
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x25, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Internal Mic Boost Volume", 0x17, 0x0, HDA_INPUT),
4640 4641 4642 4643 4644 4645
	{ } /* end */
};

/* switch to external mic if plugged */
static void ad1984a_touchsmart_automic(struct hda_codec *codec)
{
4646
	if (snd_hda_jack_detect(codec, 0x1c))
4647 4648
		snd_hda_codec_write(codec, 0x0c, 0,
				     AC_VERB_SET_CONNECT_SEL, 0x4);
4649
	else
4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678
		snd_hda_codec_write(codec, 0x0c, 0,
				     AC_VERB_SET_CONNECT_SEL, 0x5);
}


/* unsolicited event for HP jack sensing */
static void ad1984a_touchsmart_unsol_event(struct hda_codec *codec,
	unsigned int res)
{
	switch (res >> 26) {
	case AD1884A_HP_EVENT:
		ad1884a_hp_automute(codec);
		break;
	case AD1884A_MIC_EVENT:
		ad1984a_touchsmart_automic(codec);
		break;
	}
}

/* initialize jack-sensing, too */
static int ad1984a_touchsmart_init(struct hda_codec *codec)
{
	ad198x_init(codec);
	ad1884a_hp_automute(codec);
	ad1984a_touchsmart_automic(codec);
	return 0;
}


4679 4680 4681 4682
/*
 */

enum {
4683
	AD1884A_AUTO,
4684 4685
	AD1884A_DESKTOP,
	AD1884A_LAPTOP,
4686
	AD1884A_MOBILE,
4687
	AD1884A_THINKPAD,
4688
	AD1984A_TOUCHSMART,
4689
	AD1984A_PRECISION,
4690 4691 4692
	AD1884A_MODELS
};

4693
static const char * const ad1884a_models[AD1884A_MODELS] = {
4694
	[AD1884A_AUTO]		= "auto",
4695 4696
	[AD1884A_DESKTOP]	= "desktop",
	[AD1884A_LAPTOP]	= "laptop",
4697
	[AD1884A_MOBILE]	= "mobile",
4698
	[AD1884A_THINKPAD]	= "thinkpad",
4699
	[AD1984A_TOUCHSMART]	= "touchsmart",
4700
	[AD1984A_PRECISION]	= "precision",
4701 4702
};

4703
static const struct snd_pci_quirk ad1884a_cfg_tbl[] = {
4704
	SND_PCI_QUIRK(0x1028, 0x04ac, "Precision R5500", AD1984A_PRECISION),
4705
	SND_PCI_QUIRK(0x103c, 0x3030, "HP", AD1884A_MOBILE),
4706
	SND_PCI_QUIRK(0x103c, 0x3037, "HP 2230s", AD1884A_LAPTOP),
4707
	SND_PCI_QUIRK(0x103c, 0x3056, "HP", AD1884A_MOBILE),
4708
	SND_PCI_QUIRK_MASK(0x103c, 0xfff0, 0x3070, "HP", AD1884A_MOBILE),
4709
	SND_PCI_QUIRK_MASK(0x103c, 0xfff0, 0x30d0, "HP laptop", AD1884A_LAPTOP),
4710 4711
	SND_PCI_QUIRK_MASK(0x103c, 0xfff0, 0x30e0, "HP laptop", AD1884A_LAPTOP),
	SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3600, "HP laptop", AD1884A_LAPTOP),
4712
	SND_PCI_QUIRK_MASK(0x103c, 0xfff0, 0x7010, "HP laptop", AD1884A_MOBILE),
4713
	SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X300", AD1884A_THINKPAD),
4714
	SND_PCI_QUIRK(0x103c, 0x2a82, "Touchsmart", AD1984A_TOUCHSMART),
4715
	{}
4716 4717 4718 4719 4720
};

static int patch_ad1884a(struct hda_codec *codec)
{
	struct ad198x_spec *spec;
4721
	int err, board_config;
4722

4723 4724 4725
	board_config = snd_hda_check_board_config(codec, AD1884A_MODELS,
						  ad1884a_models,
						  ad1884a_cfg_tbl);
4726 4727 4728 4729 4730 4731 4732
	if (board_config < 0) {
		printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
		       codec->chip_name);
		board_config = AD1884A_AUTO;
	}

	if (board_config == AD1884A_AUTO)
4733
		return ad1884_parse_auto_config(codec);
4734

4735 4736 4737 4738
	err = alloc_ad_spec(codec);
	if (err < 0)
		return err;
	spec = codec->spec;
4739

4740 4741 4742 4743 4744 4745 4746
	err = snd_hda_attach_beep_device(codec, 0x10);
	if (err < 0) {
		ad198x_free(codec);
		return err;
	}
	set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);

4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759
	spec->multiout.max_channels = 2;
	spec->multiout.num_dacs = ARRAY_SIZE(ad1884a_dac_nids);
	spec->multiout.dac_nids = ad1884a_dac_nids;
	spec->multiout.dig_out_nid = AD1884A_SPDIF_OUT;
	spec->num_adc_nids = ARRAY_SIZE(ad1884a_adc_nids);
	spec->adc_nids = ad1884a_adc_nids;
	spec->capsrc_nids = ad1884a_capsrc_nids;
	spec->input_mux = &ad1884a_capture_source;
	spec->num_mixers = 1;
	spec->mixers[0] = ad1884a_base_mixers;
	spec->num_init_verbs = 1;
	spec->init_verbs[0] = ad1884a_init_verbs;
	spec->spdif_route = 0;
4760
#ifdef CONFIG_PM
4761 4762 4763 4764 4765 4766 4767 4768 4769 4770
	spec->loopback.amplist = ad1884a_loopbacks;
#endif
	codec->patch_ops = ad198x_patch_ops;

	/* override some parameters */
	switch (board_config) {
	case AD1884A_LAPTOP:
		spec->mixers[0] = ad1884a_laptop_mixers;
		spec->init_verbs[spec->num_init_verbs++] = ad1884a_laptop_verbs;
		spec->multiout.dig_out_nid = 0;
4771 4772
		codec->patch_ops.unsol_event = ad1884a_laptop_unsol_event;
		codec->patch_ops.init = ad1884a_laptop_init;
4773 4774 4775 4776 4777 4778 4779 4780
		/* set the upper-limit for mixer amp to 0dB for avoiding the
		 * possible damage by overloading
		 */
		snd_hda_override_amp_caps(codec, 0x20, HDA_INPUT,
					  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
					  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
					  (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
					  (1 << AC_AMPCAP_MUTE_SHIFT));
4781
		break;
4782 4783
	case AD1884A_MOBILE:
		spec->mixers[0] = ad1884a_mobile_mixers;
4784
		spec->init_verbs[0] = ad1884a_mobile_verbs;
4785 4786 4787
		spec->multiout.dig_out_nid = 0;
		codec->patch_ops.unsol_event = ad1884a_hp_unsol_event;
		codec->patch_ops.init = ad1884a_hp_init;
4788 4789 4790 4791 4792 4793 4794 4795
		/* set the upper-limit for mixer amp to 0dB for avoiding the
		 * possible damage by overloading
		 */
		snd_hda_override_amp_caps(codec, 0x20, HDA_INPUT,
					  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
					  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
					  (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
					  (1 << AC_AMPCAP_MUTE_SHIFT));
4796
		break;
4797 4798 4799 4800 4801 4802 4803 4804 4805
	case AD1884A_THINKPAD:
		spec->mixers[0] = ad1984a_thinkpad_mixers;
		spec->init_verbs[spec->num_init_verbs++] =
			ad1984a_thinkpad_verbs;
		spec->multiout.dig_out_nid = 0;
		spec->input_mux = &ad1984a_thinkpad_capture_source;
		codec->patch_ops.unsol_event = ad1984a_thinkpad_unsol_event;
		codec->patch_ops.init = ad1984a_thinkpad_init;
		break;
4806 4807 4808 4809 4810 4811 4812 4813
	case AD1984A_PRECISION:
		spec->mixers[0] = ad1984a_precision_mixers;
		spec->init_verbs[spec->num_init_verbs++] =
			ad1984a_precision_verbs;
		spec->multiout.dig_out_nid = 0;
		codec->patch_ops.unsol_event = ad1984a_precision_unsol_event;
		codec->patch_ops.init = ad1984a_precision_init;
		break;
4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828
	case AD1984A_TOUCHSMART:
		spec->mixers[0] = ad1984a_touchsmart_mixers;
		spec->init_verbs[0] = ad1984a_touchsmart_verbs;
		spec->multiout.dig_out_nid = 0;
		codec->patch_ops.unsol_event = ad1984a_touchsmart_unsol_event;
		codec->patch_ops.init = ad1984a_touchsmart_init;
		/* set the upper-limit for mixer amp to 0dB for avoiding the
		 * possible damage by overloading
		 */
		snd_hda_override_amp_caps(codec, 0x20, HDA_INPUT,
					  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
					  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
					  (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
					  (1 << AC_AMPCAP_MUTE_SHIFT));
		break;
4829 4830
	}

4831
	codec->no_trigger_sense = 1;
4832
	codec->no_sticky_stream = 1;
4833

4834 4835
	return 0;
}
4836 4837 4838
#else /* ENABLE_AD_STATIC_QUIRKS */
#define patch_ad1884a	ad1884_parse_auto_config
#endif /* ENABLE_AD_STATIC_QUIRKS */
4839 4840


4841
/*
4842
 * AD1882 / AD1882A
4843 4844 4845 4846 4847 4848 4849 4850 4851 4852
 *
 * port-A - front hp-out
 * port-B - front mic-in
 * port-C - rear line-in, shared surr-out (3stack)
 * port-D - rear line-out
 * port-E - rear mic-in, shared clfe-out (3stack)
 * port-F - rear surr-out (6stack)
 * port-G - rear clfe-out (6stack)
 */

4853
#ifdef ENABLE_AD_STATIC_QUIRKS
4854
static const hda_nid_t ad1882_dac_nids[3] = {
4855 4856 4857
	0x04, 0x03, 0x05
};

4858
static const hda_nid_t ad1882_adc_nids[2] = {
4859 4860 4861
	0x08, 0x09,
};

4862
static const hda_nid_t ad1882_capsrc_nids[2] = {
4863 4864 4865 4866 4867 4868
	0x0c, 0x0d,
};

#define AD1882_SPDIF_OUT	0x02

/* list: 0x11, 0x39, 0x3a, 0x18, 0x3c, 0x3b, 0x12, 0x20 */
4869
static const struct hda_input_mux ad1882_capture_source = {
4870 4871 4872 4873 4874 4875 4876 4877 4878 4879
	.num_items = 5,
	.items = {
		{ "Front Mic", 0x1 },
		{ "Mic", 0x4 },
		{ "Line", 0x2 },
		{ "CD", 0x3 },
		{ "Mix", 0x7 },
	},
};

4880
/* list: 0x11, 0x39, 0x3a, 0x3c, 0x18, 0x1f, 0x12, 0x20 */
4881
static const struct hda_input_mux ad1882a_capture_source = {
4882 4883 4884 4885 4886 4887 4888 4889 4890 4891
	.num_items = 5,
	.items = {
		{ "Front Mic", 0x1 },
		{ "Mic", 0x4},
		{ "Line", 0x2 },
		{ "Digital Mic", 0x06 },
		{ "Mix", 0x7 },
	},
};

4892
static const struct snd_kcontrol_new ad1882_base_mixers[] = {
4893 4894 4895 4896 4897 4898 4899 4900
	HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Surround Playback Volume", 0x03, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
4901

4902 4903 4904
	HDA_CODEC_VOLUME("Mic Boost Volume", 0x3c, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Front Mic Boost Volume", 0x39, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME("Line-In Boost Volume", 0x3a, 0x0, HDA_OUTPUT),
4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933
	HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		/* The multiple "Capture Source" controls confuse alsamixer
		 * So call somewhat different..
		 */
		/* .name = "Capture Source", */
		.name = "Input Source",
		.count = 2,
		.info = ad198x_mux_enum_info,
		.get = ad198x_mux_enum_get,
		.put = ad198x_mux_enum_put,
	},
	/* SPDIF controls */
	HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
		/* identical with ad1983 */
		.info = ad1983_spdif_route_info,
		.get = ad1983_spdif_route_get,
		.put = ad1983_spdif_route_put,
	},
	{ } /* end */
};

4934
static const struct snd_kcontrol_new ad1882_loopback_mixers[] = {
4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945
	HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x04, HDA_INPUT),
	HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x04, HDA_INPUT),
	HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x06, HDA_INPUT),
	HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x06, HDA_INPUT),
	{ } /* end */
};

4946
static const struct snd_kcontrol_new ad1882a_loopback_mixers[] = {
4947 4948 4949 4950 4951 4952 4953 4954
	HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
	HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
	HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
	HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x01, HDA_INPUT),
	HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x06, HDA_INPUT),
	HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x06, HDA_INPUT),
4955
	HDA_CODEC_VOLUME("Digital Mic Boost Volume", 0x1f, 0x0, HDA_INPUT),
4956 4957 4958
	{ } /* end */
};

4959
static const struct snd_kcontrol_new ad1882_3stack_mixers[] = {
4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972
	HDA_CODEC_MUTE("Surround Playback Switch", 0x15, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x17, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x17, 2, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Channel Mode",
		.info = ad198x_ch_mode_info,
		.get = ad198x_ch_mode_get,
		.put = ad198x_ch_mode_put,
	},
	{ } /* end */
};

4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998
/* simple auto-mute control for AD1882 3-stack board */
#define AD1882_HP_EVENT	0x01

static void ad1882_3stack_automute(struct hda_codec *codec)
{
	bool mute = snd_hda_jack_detect(codec, 0x11);
	snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
			    mute ? 0 : PIN_OUT);
}

static int ad1882_3stack_automute_init(struct hda_codec *codec)
{
	ad198x_init(codec);
	ad1882_3stack_automute(codec);
	return 0;
}

static void ad1882_3stack_unsol_event(struct hda_codec *codec, unsigned int res)
{
	switch (res >> 26) {
	case AD1882_HP_EVENT:
		ad1882_3stack_automute(codec);
		break;
	}
}

4999
static const struct snd_kcontrol_new ad1882_6stack_mixers[] = {
5000 5001 5002 5003 5004 5005
	HDA_CODEC_MUTE("Surround Playback Switch", 0x16, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x24, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x24, 2, 0x0, HDA_OUTPUT),
	{ } /* end */
};

5006
static const struct hda_verb ad1882_ch2_init[] = {
5007 5008 5009 5010 5011 5012 5013 5014 5015
	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
	{0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{ } /* end */
};

5016
static const struct hda_verb ad1882_ch4_init[] = {
5017 5018 5019 5020 5021 5022 5023 5024 5025
	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	{0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
	{0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	{0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{ } /* end */
};

5026
static const struct hda_verb ad1882_ch6_init[] = {
5027 5028 5029 5030 5031 5032 5033 5034 5035
	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	{0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
	{0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	{0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	{ } /* end */
};

5036
static const struct hda_channel_mode ad1882_modes[3] = {
5037 5038 5039 5040 5041 5042 5043 5044
	{ 2, ad1882_ch2_init },
	{ 4, ad1882_ch4_init },
	{ 6, ad1882_ch6_init },
};

/*
 * initialization verbs
 */
5045
static const struct hda_verb ad1882_init_verbs[] = {
5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112
	/* DACs; mute as default */
	{0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
	{0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
	{0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
	/* Port-A (HP) mixer */
	{0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
	{0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Port-A pin */
	{0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* HP selector - select DAC2 */
	{0x37, AC_VERB_SET_CONNECT_SEL, 0x1},
	/* Port-D (Line-out) mixer */
	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Port-D pin */
	{0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* Mono-out mixer */
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
	{0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
	/* Mono-out pin */
	{0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* Port-B (front mic) pin */
	{0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	{0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
	/* Port-C (line-in) pin */
	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	{0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
	/* Port-C mixer - mute as input */
	{0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	/* Port-E (mic-in) pin */
	{0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	{0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
	/* Port-E mixer - mute as input */
	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	/* Port-F (surround) */
	{0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* Port-G (CLFE) */
	{0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
	{0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
	/* Analog mixer; mute as default */
	/* list: 0x39, 0x3a, 0x11, 0x12, 0x3c, 0x3b, 0x18, 0x1a */
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
	/* Analog Mix output amp */
	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
	/* SPDIF output selector */
	{0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
	{0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
	{ } /* end */
};

5113 5114 5115 5116 5117
static const struct hda_verb ad1882_3stack_automute_verbs[] = {
	{0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1882_HP_EVENT},
	{ } /* end */
};

5118
#ifdef CONFIG_PM
5119
static const struct hda_amp_list ad1882_loopbacks[] = {
5120 5121 5122 5123 5124 5125 5126 5127
	{ 0x20, HDA_INPUT, 0 }, /* Front Mic */
	{ 0x20, HDA_INPUT, 1 }, /* Mic */
	{ 0x20, HDA_INPUT, 4 }, /* Line */
	{ 0x20, HDA_INPUT, 6 }, /* CD */
	{ } /* end */
};
#endif

5128 5129
/* models */
enum {
5130
	AD1882_AUTO,
5131 5132
	AD1882_3STACK,
	AD1882_6STACK,
5133
	AD1882_3STACK_AUTOMUTE,
5134 5135 5136
	AD1882_MODELS
};

5137
static const char * const ad1882_models[AD1986A_MODELS] = {
5138
	[AD1882_AUTO]		= "auto",
5139 5140
	[AD1882_3STACK]		= "3stack",
	[AD1882_6STACK]		= "6stack",
5141
	[AD1882_3STACK_AUTOMUTE] = "3stack-automute",
5142
};
5143
#endif /* ENABLE_AD_STATIC_QUIRKS */
5144

5145 5146
static int ad1882_parse_auto_config(struct hda_codec *codec)
{
5147
	struct ad198x_spec *spec;
5148
	int err;
5149

5150 5151 5152 5153 5154
	err = alloc_ad_spec(codec);
	if (err < 0)
		return err;
	spec = codec->spec;

5155
	spec->gen.mixer_nid = 0x20;
5156
	spec->gen.mixer_merge_nid = 0x21;
5157
	spec->gen.beep_nid = 0x10;
5158
	set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
5159 5160
	err = ad198x_parse_auto_config(codec);
	if (err < 0)
5161
		goto error;
5162 5163
	err = ad1988_add_spdif_mux_ctl(codec);
	if (err < 0)
5164
		goto error;
5165
	return 0;
5166 5167

 error:
5168
	snd_hda_gen_free(codec);
5169
	return err;
5170
}
5171

5172
#ifdef ENABLE_AD_STATIC_QUIRKS
5173 5174 5175
static int patch_ad1882(struct hda_codec *codec)
{
	struct ad198x_spec *spec;
5176
	int err, board_config;
5177

5178 5179
	board_config = snd_hda_check_board_config(codec, AD1882_MODELS,
						  ad1882_models, NULL);
5180 5181 5182 5183 5184 5185
	if (board_config < 0) {
		printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
		       codec->chip_name);
		board_config = AD1882_AUTO;
	}

5186 5187 5188
	if (board_config == AD1882_AUTO)
		return ad1882_parse_auto_config(codec);

5189 5190 5191 5192
	err = alloc_ad_spec(codec);
	if (err < 0)
		return err;
	spec = codec->spec;
5193

5194 5195 5196 5197 5198 5199 5200
	err = snd_hda_attach_beep_device(codec, 0x10);
	if (err < 0) {
		ad198x_free(codec);
		return err;
	}
	set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);

5201 5202 5203 5204 5205 5206 5207
	spec->multiout.max_channels = 6;
	spec->multiout.num_dacs = 3;
	spec->multiout.dac_nids = ad1882_dac_nids;
	spec->multiout.dig_out_nid = AD1882_SPDIF_OUT;
	spec->num_adc_nids = ARRAY_SIZE(ad1882_adc_nids);
	spec->adc_nids = ad1882_adc_nids;
	spec->capsrc_nids = ad1882_capsrc_nids;
5208
	if (codec->vendor_id == 0x11d41882)
5209 5210 5211 5212
		spec->input_mux = &ad1882_capture_source;
	else
		spec->input_mux = &ad1882a_capture_source;
	spec->num_mixers = 2;
5213
	spec->mixers[0] = ad1882_base_mixers;
5214
	if (codec->vendor_id == 0x11d41882)
5215 5216 5217
		spec->mixers[1] = ad1882_loopback_mixers;
	else
		spec->mixers[1] = ad1882a_loopback_mixers;
5218 5219 5220
	spec->num_init_verbs = 1;
	spec->init_verbs[0] = ad1882_init_verbs;
	spec->spdif_route = 0;
5221
#ifdef CONFIG_PM
5222 5223
	spec->loopback.amplist = ad1882_loopbacks;
#endif
5224
	spec->vmaster_nid = 0x04;
5225 5226 5227 5228 5229 5230 5231

	codec->patch_ops = ad198x_patch_ops;

	/* override some parameters */
	switch (board_config) {
	default:
	case AD1882_3STACK:
5232
	case AD1882_3STACK_AUTOMUTE:
5233 5234
		spec->num_mixers = 3;
		spec->mixers[2] = ad1882_3stack_mixers;
5235 5236 5237 5238 5239
		spec->channel_mode = ad1882_modes;
		spec->num_channel_mode = ARRAY_SIZE(ad1882_modes);
		spec->need_dac_fix = 1;
		spec->multiout.max_channels = 2;
		spec->multiout.num_dacs = 1;
5240 5241 5242 5243 5244 5245
		if (board_config != AD1882_3STACK) {
			spec->init_verbs[spec->num_init_verbs++] =
				ad1882_3stack_automute_verbs;
			codec->patch_ops.unsol_event = ad1882_3stack_unsol_event;
			codec->patch_ops.init = ad1882_3stack_automute_init;
		}
5246 5247
		break;
	case AD1882_6STACK:
5248 5249
		spec->num_mixers = 3;
		spec->mixers[2] = ad1882_6stack_mixers;
5250 5251
		break;
	}
5252 5253

	codec->no_trigger_sense = 1;
5254
	codec->no_sticky_stream = 1;
5255

5256 5257
	return 0;
}
5258 5259 5260
#else /* ENABLE_AD_STATIC_QUIRKS */
#define patch_ad1882	ad1882_parse_auto_config
#endif /* ENABLE_AD_STATIC_QUIRKS */
5261 5262


L
Linus Torvalds 已提交
5263 5264 5265
/*
 * patch entries
 */
5266
static const struct hda_codec_preset snd_hda_preset_analog[] = {
5267
	{ .id = 0x11d4184a, .name = "AD1884A", .patch = patch_ad1884a },
5268
	{ .id = 0x11d41882, .name = "AD1882", .patch = patch_ad1882 },
5269
	{ .id = 0x11d41883, .name = "AD1883", .patch = patch_ad1884a },
5270
	{ .id = 0x11d41884, .name = "AD1884", .patch = patch_ad1884 },
5271 5272
	{ .id = 0x11d4194a, .name = "AD1984A", .patch = patch_ad1884a },
	{ .id = 0x11d4194b, .name = "AD1984B", .patch = patch_ad1884a },
5273 5274
	{ .id = 0x11d41981, .name = "AD1981", .patch = patch_ad1981 },
	{ .id = 0x11d41983, .name = "AD1983", .patch = patch_ad1983 },
5275
	{ .id = 0x11d41984, .name = "AD1984", .patch = patch_ad1984 },
L
Linus Torvalds 已提交
5276
	{ .id = 0x11d41986, .name = "AD1986A", .patch = patch_ad1986a },
5277
	{ .id = 0x11d41988, .name = "AD1988", .patch = patch_ad1988 },
5278
	{ .id = 0x11d4198b, .name = "AD1988B", .patch = patch_ad1988 },
5279
	{ .id = 0x11d4882a, .name = "AD1882A", .patch = patch_ad1882 },
5280 5281
	{ .id = 0x11d4989a, .name = "AD1989A", .patch = patch_ad1988 },
	{ .id = 0x11d4989b, .name = "AD1989B", .patch = patch_ad1988 },
L
Linus Torvalds 已提交
5282 5283
	{} /* terminator */
};
5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306

MODULE_ALIAS("snd-hda-codec-id:11d4*");

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Analog Devices HD-audio codec");

static struct hda_codec_preset_list analog_list = {
	.preset = snd_hda_preset_analog,
	.owner = THIS_MODULE,
};

static int __init patch_analog_init(void)
{
	return snd_hda_add_codec_preset(&analog_list);
}

static void __exit patch_analog_exit(void)
{
	snd_hda_delete_codec_preset(&analog_list);
}

module_init(patch_analog_init)
module_exit(patch_analog_exit)