patch_realtek.c 576 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5
/*
 * Universal Interface for Intel High Definition Audio Codec
 *
 * HD audio interface patch for ALC 260/880/882 codecs
 *
6 7
 * Copyright (c) 2004 Kailang Yang <kailang@realtek.com.tw>
 *                    PeiSen Hou <pshou@realtek.com.tw>
Linus Torvalds's avatar
Linus Torvalds committed
8
 *                    Takashi Iwai <tiwai@suse.de>
9
 *                    Jonathan Woithe <jwoithe@physics.adelaide.edu.au>
Linus Torvalds's avatar
Linus Torvalds committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 *  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/delay.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include <sound/core.h>
#include "hda_codec.h"
#include "hda_local.h"
33
#include "hda_beep.h"
Linus Torvalds's avatar
Linus Torvalds committed
34

35 36 37 38
#define ALC880_FRONT_EVENT		0x01
#define ALC880_DCVOL_EVENT		0x02
#define ALC880_HP_EVENT			0x04
#define ALC880_MIC_EVENT		0x08
Linus Torvalds's avatar
Linus Torvalds committed
39 40 41 42 43 44 45 46

/* ALC880 board config type */
enum {
	ALC880_3ST,
	ALC880_3ST_DIG,
	ALC880_5ST,
	ALC880_5ST_DIG,
	ALC880_W810,
Takashi Iwai's avatar
Takashi Iwai committed
47
	ALC880_Z71V,
48
	ALC880_6ST,
49 50 51 52 53
	ALC880_6ST_DIG,
	ALC880_F1734,
	ALC880_ASUS,
	ALC880_ASUS_DIG,
	ALC880_ASUS_W1V,
54
	ALC880_ASUS_DIG2,
55
	ALC880_FUJITSU,
56
	ALC880_UNIWILL_DIG,
57 58
	ALC880_UNIWILL,
	ALC880_UNIWILL_P53,
59 60
	ALC880_CLEVO,
	ALC880_TCL_S700,
61
	ALC880_LG,
62
	ALC880_LG_LW,
63
	ALC880_MEDION_RIM,
64 65 66
#ifdef CONFIG_SND_DEBUG
	ALC880_TEST,
#endif
67
	ALC880_AUTO,
68 69 70 71 72 73 74
	ALC880_MODEL_LAST /* last tag */
};

/* ALC260 models */
enum {
	ALC260_BASIC,
	ALC260_HP,
75
	ALC260_HP_DC7600,
76 77
	ALC260_HP_3013,
	ALC260_FUJITSU_S702X,
78
	ALC260_ACER,
79 80
	ALC260_WILL,
	ALC260_REPLACER_672V,
81
	ALC260_FAVORIT100,
82 83 84
#ifdef CONFIG_SND_DEBUG
	ALC260_TEST,
#endif
85
	ALC260_AUTO,
86
	ALC260_MODEL_LAST /* last tag */
Linus Torvalds's avatar
Linus Torvalds committed
87 88
};

89 90 91
/* ALC262 models */
enum {
	ALC262_BASIC,
92 93
	ALC262_HIPPO,
	ALC262_HIPPO_1,
94
	ALC262_FUJITSU,
95
	ALC262_HP_BPC,
96 97
	ALC262_HP_BPC_D7000_WL,
	ALC262_HP_BPC_D7000_WF,
98
	ALC262_HP_TC_T5735,
99
	ALC262_HP_RP5700,
100
	ALC262_BENQ_ED8,
101
	ALC262_SONY_ASSAMD,
102
	ALC262_BENQ_T31,
103
	ALC262_ULTRA,
104
	ALC262_LENOVO_3000,
105
	ALC262_NEC,
106
	ALC262_TOSHIBA_S06,
107
	ALC262_TOSHIBA_RX1,
108
	ALC262_TYAN,
109 110 111 112
	ALC262_AUTO,
	ALC262_MODEL_LAST /* last tag */
};

113 114
/* ALC268 models */
enum {
115
	ALC267_QUANTA_IL1,
116
	ALC268_3ST,
117
	ALC268_TOSHIBA,
118
	ALC268_ACER,
119
	ALC268_ACER_DMIC,
120
	ALC268_ACER_ASPIRE_ONE,
121
	ALC268_DELL,
122
	ALC268_ZEPTO,
123 124 125
#ifdef CONFIG_SND_DEBUG
	ALC268_TEST,
#endif
126 127 128 129
	ALC268_AUTO,
	ALC268_MODEL_LAST /* last tag */
};

130 131 132
/* ALC269 models */
enum {
	ALC269_BASIC,
133
	ALC269_QUANTA_FL1,
134 135
	ALC269_ASUS_EEEPC_P703,
	ALC269_ASUS_EEEPC_P901,
136
	ALC269_FUJITSU,
137
	ALC269_LIFEBOOK,
138 139 140 141
	ALC269_AUTO,
	ALC269_MODEL_LAST /* last tag */
};

142 143 144
/* ALC861 models */
enum {
	ALC861_3ST,
145
	ALC660_3ST,
146 147
	ALC861_3ST_DIG,
	ALC861_6ST_DIG,
148
	ALC861_UNIWILL_M31,
149
	ALC861_TOSHIBA,
150
	ALC861_ASUS,
151
	ALC861_ASUS_LAPTOP,
152 153 154 155
	ALC861_AUTO,
	ALC861_MODEL_LAST,
};

156 157 158
/* ALC861-VD models */
enum {
	ALC660VD_3ST,
159
	ALC660VD_3ST_DIG,
160
	ALC660VD_ASUS_V1S,
161 162 163
	ALC861VD_3ST,
	ALC861VD_3ST_DIG,
	ALC861VD_6ST_DIG,
164
	ALC861VD_LENOVO,
165
	ALC861VD_DALLAS,
166
	ALC861VD_HP,
167 168 169 170
	ALC861VD_AUTO,
	ALC861VD_MODEL_LAST,
};

171 172 173 174 175 176 177
/* ALC662 models */
enum {
	ALC662_3ST_2ch_DIG,
	ALC662_3ST_6ch_DIG,
	ALC662_3ST_6ch,
	ALC662_5ST_DIG,
	ALC662_LENOVO_101E,
178
	ALC662_ASUS_EEEPC_P701,
179
	ALC662_ASUS_EEEPC_EP20,
Kailang Yang's avatar
Kailang Yang committed
180 181 182 183
	ALC663_ASUS_M51VA,
	ALC663_ASUS_G71V,
	ALC663_ASUS_H13,
	ALC663_ASUS_G50V,
184 185 186 187 188 189 190
	ALC662_ECS,
	ALC663_ASUS_MODE1,
	ALC662_ASUS_MODE2,
	ALC663_ASUS_MODE3,
	ALC663_ASUS_MODE4,
	ALC663_ASUS_MODE5,
	ALC663_ASUS_MODE6,
191 192
	ALC272_DELL,
	ALC272_DELL_ZM1,
193
	ALC272_SAMSUNG_NC10,
194 195 196 197
	ALC662_AUTO,
	ALC662_MODEL_LAST,
};

198 199 200 201
/* ALC882 models */
enum {
	ALC882_3ST_DIG,
	ALC882_6ST_DIG,
202
	ALC882_ARIMA,
203
	ALC882_W2JC,
204 205
	ALC882_TARGA,
	ALC882_ASUS_A7J,
206
	ALC882_ASUS_A7M,
207
	ALC885_MACPRO,
208
	ALC885_MBP3,
209
	ALC885_MB5,
210
	ALC885_IMAC24,
211
	ALC885_IMAC91,
212 213 214 215
	ALC883_3ST_2ch_DIG,
	ALC883_3ST_6ch_DIG,
	ALC883_3ST_6ch,
	ALC883_6ST_DIG,
216 217
	ALC883_TARGA_DIG,
	ALC883_TARGA_2ch_DIG,
218
	ALC883_TARGA_8ch_DIG,
219
	ALC883_ACER,
220
	ALC883_ACER_ASPIRE,
221
	ALC888_ACER_ASPIRE_4930G,
222
	ALC888_ACER_ASPIRE_6530G,
223
	ALC888_ACER_ASPIRE_8930G,
224
	ALC888_ACER_ASPIRE_7730G,
225
	ALC883_MEDION,
226
	ALC883_MEDION_MD2,
227
	ALC883_LAPTOP_EAPD,
228
	ALC883_LENOVO_101E_2ch,
229
	ALC883_LENOVO_NB0763,
230
	ALC888_LENOVO_MS7195_DIG,
231
	ALC888_LENOVO_SKY,
232
	ALC883_HAIER_W66,
233
	ALC888_3ST_HP,
234
	ALC888_6ST_DELL,
235
	ALC883_MITAC,
236
	ALC883_CLEVO_M540R,
237
	ALC883_CLEVO_M720,
238
	ALC883_FUJITSU_PI2515,
239
	ALC888_FUJITSU_XA3530,
240
	ALC883_3ST_6ch_INTEL,
241 242
	ALC889A_INTEL,
	ALC889_INTEL,
243 244
	ALC888_ASUS_M90V,
	ALC888_ASUS_EEE1601,
245
	ALC889A_MB31,
246
	ALC1200_ASUS_P5Q,
Guido Günther's avatar
Guido Günther committed
247
	ALC883_SONY_VAIO_TT,
248 249
	ALC882_AUTO,
	ALC882_MODEL_LAST,
250 251
};

252 253 254
/* for GPIO Poll */
#define GPIO_MASK	0x03

255 256 257 258 259 260 261 262 263
/* extra amp-initialization sequence types */
enum {
	ALC_INIT_NONE,
	ALC_INIT_DEFAULT,
	ALC_INIT_GPIO1,
	ALC_INIT_GPIO2,
	ALC_INIT_GPIO3,
};

264 265 266 267 268 269 270 271
struct alc_mic_route {
	hda_nid_t pin;
	unsigned char mux_idx;
	unsigned char amix_idx;
};

#define MUX_IDX_UNDEF	((unsigned char)-1)

Linus Torvalds's avatar
Linus Torvalds committed
272 273
struct alc_spec {
	/* codec parameterization */
274
	struct snd_kcontrol_new *mixers[5];	/* mixer arrays */
Linus Torvalds's avatar
Linus Torvalds committed
275
	unsigned int num_mixers;
276
	struct snd_kcontrol_new *cap_mixer;	/* capture mixer */
277
	unsigned int beep_amp;	/* beep amp value, set via set_beep_amp() */
Linus Torvalds's avatar
Linus Torvalds committed
278

279
	const struct hda_verb *init_verbs[10];	/* initialization verbs
280 281
						 * don't forget NULL
						 * termination!
282 283
						 */
	unsigned int num_init_verbs;
Linus Torvalds's avatar
Linus Torvalds committed
284

285
	char stream_name_analog[32];	/* analog PCM stream */
Linus Torvalds's avatar
Linus Torvalds committed
286 287
	struct hda_pcm_stream *stream_analog_playback;
	struct hda_pcm_stream *stream_analog_capture;
288 289
	struct hda_pcm_stream *stream_analog_alt_playback;
	struct hda_pcm_stream *stream_analog_alt_capture;
Linus Torvalds's avatar
Linus Torvalds committed
290

291
	char stream_name_digital[32];	/* digital PCM stream */
Linus Torvalds's avatar
Linus Torvalds committed
292 293 294 295
	struct hda_pcm_stream *stream_digital_playback;
	struct hda_pcm_stream *stream_digital_capture;

	/* playback */
296 297 298 299
	struct hda_multi_out multiout;	/* playback set-up
					 * max_channels, dacs must be set
					 * dig_out_nid and hp_nid are optional
					 */
300
	hda_nid_t alt_dac_nid;
301
	hda_nid_t slave_dig_outs[3];	/* optional - for auto-parsing */
302
	int dig_out_type;
Linus Torvalds's avatar
Linus Torvalds committed
303 304 305 306

	/* capture */
	unsigned int num_adc_nids;
	hda_nid_t *adc_nids;
307
	hda_nid_t *capsrc_nids;
308
	hda_nid_t dig_in_nid;		/* digital-in NID; optional */
Linus Torvalds's avatar
Linus Torvalds committed
309 310

	/* capture source */
311
	unsigned int num_mux_defs;
Linus Torvalds's avatar
Linus Torvalds committed
312 313
	const struct hda_input_mux *input_mux;
	unsigned int cur_mux[3];
314 315
	struct alc_mic_route ext_mic;
	struct alc_mic_route int_mic;
Linus Torvalds's avatar
Linus Torvalds committed
316 317

	/* channel model */
318
	const struct hda_channel_mode *channel_mode;
Linus Torvalds's avatar
Linus Torvalds committed
319
	int num_channel_mode;
320
	int need_dac_fix;
321 322
	int const_channel_count;
	int ext_channel_count;
Linus Torvalds's avatar
Linus Torvalds committed
323 324

	/* PCM information */
325
	struct hda_pcm pcm_rec[3];	/* used in alc_build_pcms() */
326

327 328
	/* dynamic controls, init_verbs and input_mux */
	struct auto_pin_cfg autocfg;
329
	struct snd_array kctls;
330
	struct hda_input_mux private_imux[3];
331
	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
332 333
	hda_nid_t private_adc_nids[AUTO_CFG_MAX_OUTS];
	hda_nid_t private_capsrc_nids[AUTO_CFG_MAX_OUTS];
334

335 336 337 338
	/* hooks */
	void (*init_hook)(struct hda_codec *codec);
	void (*unsol_event)(struct hda_codec *codec, unsigned int res);

339 340 341
	/* for pin sensing */
	unsigned int sense_updated: 1;
	unsigned int jack_present: 1;
342
	unsigned int master_sw: 1;
343
	unsigned int auto_mic:1;
344

345 346
	/* other flags */
	unsigned int no_analog :1; /* digital I/O only */
347
	int init_amp;
348

349 350
	/* for virtual master */
	hda_nid_t vmaster_nid;
351 352 353
#ifdef CONFIG_SND_HDA_POWER_SAVE
	struct hda_loopback_check loopback;
#endif
354 355 356 357

	/* for PLL fix */
	hda_nid_t pll_nid;
	unsigned int pll_coef_idx, pll_coef_bit;
358 359 360 361 362 363
};

/*
 * configuration template - to be copied to the spec instance
 */
struct alc_config_preset {
364 365 366
	struct snd_kcontrol_new *mixers[5]; /* should be identical size
					     * with spec
					     */
367
	struct snd_kcontrol_new *cap_mixer; /* capture mixer */
368 369 370 371 372
	const struct hda_verb *init_verbs[5];
	unsigned int num_dacs;
	hda_nid_t *dac_nids;
	hda_nid_t dig_out_nid;		/* optional */
	hda_nid_t hp_nid;		/* optional */
373
	hda_nid_t *slave_dig_outs;
374 375
	unsigned int num_adc_nids;
	hda_nid_t *adc_nids;
376
	hda_nid_t *capsrc_nids;
377 378 379
	hda_nid_t dig_in_nid;
	unsigned int num_channel_mode;
	const struct hda_channel_mode *channel_mode;
380
	int need_dac_fix;
381
	int const_channel_count;
382
	unsigned int num_mux_defs;
383
	const struct hda_input_mux *input_mux;
384
	void (*unsol_event)(struct hda_codec *, unsigned int);
385
	void (*setup)(struct hda_codec *);
386
	void (*init_hook)(struct hda_codec *);
387 388 389
#ifdef CONFIG_SND_HDA_POWER_SAVE
	struct hda_amp_list *loopbacks;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
390 391 392 393 394 395
};


/*
 * input MUX handling
 */
396 397
static int alc_mux_enum_info(struct snd_kcontrol *kcontrol,
			     struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
398 399 400
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct alc_spec *spec = codec->spec;
401 402 403 404
	unsigned int mux_idx = snd_ctl_get_ioffidx(kcontrol, &uinfo->id);
	if (mux_idx >= spec->num_mux_defs)
		mux_idx = 0;
	return snd_hda_input_mux_info(&spec->input_mux[mux_idx], uinfo);
Linus Torvalds's avatar
Linus Torvalds committed
405 406
}

407 408
static int alc_mux_enum_get(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
409 410 411 412 413 414 415 416 417
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct alc_spec *spec = codec->spec;
	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);

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

418 419
static int alc_mux_enum_put(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
420 421 422
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct alc_spec *spec = codec->spec;
423
	const struct hda_input_mux *imux;
Linus Torvalds's avatar
Linus Torvalds committed
424
	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
425
	unsigned int mux_idx;
426 427
	hda_nid_t nid = spec->capsrc_nids ?
		spec->capsrc_nids[adc_idx] : spec->adc_nids[adc_idx];
428
	unsigned int type;
Linus Torvalds's avatar
Linus Torvalds committed
429

430 431 432
	mux_idx = adc_idx >= spec->num_mux_defs ? 0 : adc_idx;
	imux = &spec->input_mux[mux_idx];

433
	type = get_wcaps_type(get_wcaps(codec, nid));
434
	if (type == AC_WID_AUD_MIX) {
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
		/* Matrix-mixer style (e.g. ALC882) */
		unsigned int *cur_val = &spec->cur_mux[adc_idx];
		unsigned int i, idx;

		idx = ucontrol->value.enumerated.item[0];
		if (idx >= imux->num_items)
			idx = imux->num_items - 1;
		if (*cur_val == idx)
			return 0;
		for (i = 0; i < imux->num_items; i++) {
			unsigned int v = (i == idx) ? 0 : HDA_AMP_MUTE;
			snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT,
						 imux->items[i].index,
						 HDA_AMP_MUTE, v);
		}
		*cur_val = idx;
		return 1;
	} else {
		/* MUX style (e.g. ALC880) */
454
		return snd_hda_input_mux_put(codec, imux, ucontrol, nid,
455 456 457
					     &spec->cur_mux[adc_idx]);
	}
}
458

Linus Torvalds's avatar
Linus Torvalds committed
459 460 461
/*
 * channel mode setting
 */
462 463
static int alc_ch_mode_info(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
464 465 466
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct alc_spec *spec = codec->spec;
467 468
	return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
				    spec->num_channel_mode);
Linus Torvalds's avatar
Linus Torvalds committed
469 470
}

471 472
static int alc_ch_mode_get(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
473 474 475
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct alc_spec *spec = codec->spec;
476
	return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
477
				   spec->num_channel_mode,
478
				   spec->ext_channel_count);
Linus Torvalds's avatar
Linus Torvalds committed
479 480
}

481 482
static int alc_ch_mode_put(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
483 484 485
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct alc_spec *spec = codec->spec;
486 487
	int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
				      spec->num_channel_mode,
488 489 490 491 492 493
				      &spec->ext_channel_count);
	if (err >= 0 && !spec->const_channel_count) {
		spec->multiout.max_channels = spec->ext_channel_count;
		if (spec->need_dac_fix)
			spec->multiout.num_dacs = spec->multiout.max_channels / 2;
	}
494
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
495 496
}

497
/*
498
 * Control the mode of pin widget settings via the mixer.  "pc" is used
499
 * instead of "%" to avoid consequences of accidently treating the % as
500 501
 * being part of a format specifier.  Maximum allowed length of a value is
 * 63 characters plus NULL terminator.
502 503 504 505 506
 *
 * Note: some retasking pin complexes seem to ignore requests for input
 * states other than HiZ (eg: PIN_VREFxx) and revert to HiZ if any of these
 * are requested.  Therefore order this list so that this behaviour will not
 * cause problems when mixer clients move through the enum sequentially.
507 508
 * NIDs 0x0f and 0x10 have been observed to have this behaviour as of
 * March 2006.
509 510
 */
static char *alc_pin_mode_names[] = {
511 512
	"Mic 50pc bias", "Mic 80pc bias",
	"Line in", "Line out", "Headphone out",
513 514
};
static unsigned char alc_pin_mode_values[] = {
515
	PIN_VREF50, PIN_VREF80, PIN_IN, PIN_OUT, PIN_HP,
516 517
};
/* The control can present all 5 options, or it can limit the options based
518 519 520 521 522
 * in the pin being assumed to be exclusively an input or an output pin.  In
 * addition, "input" pins may or may not process the mic bias option
 * depending on actual widget capability (NIDs 0x0f and 0x10 don't seem to
 * accept requests for bias as of chip versions up to March 2006) and/or
 * wiring in the computer.
523
 */
524 525 526 527 528
#define ALC_PIN_DIR_IN              0x00
#define ALC_PIN_DIR_OUT             0x01
#define ALC_PIN_DIR_INOUT           0x02
#define ALC_PIN_DIR_IN_NOMICBIAS    0x03
#define ALC_PIN_DIR_INOUT_NOMICBIAS 0x04
529

530
/* Info about the pin modes supported by the different pin direction modes.
531 532
 * For each direction the minimum and maximum values are given.
 */
533
static signed char alc_pin_mode_dir_info[5][2] = {
534 535 536
	{ 0, 2 },    /* ALC_PIN_DIR_IN */
	{ 3, 4 },    /* ALC_PIN_DIR_OUT */
	{ 0, 4 },    /* ALC_PIN_DIR_INOUT */
537 538
	{ 2, 2 },    /* ALC_PIN_DIR_IN_NOMICBIAS */
	{ 2, 4 },    /* ALC_PIN_DIR_INOUT_NOMICBIAS */
539 540 541 542 543 544
};
#define alc_pin_mode_min(_dir) (alc_pin_mode_dir_info[_dir][0])
#define alc_pin_mode_max(_dir) (alc_pin_mode_dir_info[_dir][1])
#define alc_pin_mode_n_items(_dir) \
	(alc_pin_mode_max(_dir)-alc_pin_mode_min(_dir)+1)

545 546
static int alc_pin_mode_info(struct snd_kcontrol *kcontrol,
			     struct snd_ctl_elem_info *uinfo)
547
{
548 549 550 551
	unsigned int item_num = uinfo->value.enumerated.item;
	unsigned char dir = (kcontrol->private_value >> 16) & 0xff;

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
552
	uinfo->count = 1;
553 554 555 556 557
	uinfo->value.enumerated.items = alc_pin_mode_n_items(dir);

	if (item_num<alc_pin_mode_min(dir) || item_num>alc_pin_mode_max(dir))
		item_num = alc_pin_mode_min(dir);
	strcpy(uinfo->value.enumerated.name, alc_pin_mode_names[item_num]);
558 559 560
	return 0;
}

561 562
static int alc_pin_mode_get(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_value *ucontrol)
563
{
564
	unsigned int i;
565 566
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	hda_nid_t nid = kcontrol->private_value & 0xffff;
567
	unsigned char dir = (kcontrol->private_value >> 16) & 0xff;
568
	long *valp = ucontrol->value.integer.value;
569 570 571
	unsigned int pinctl = snd_hda_codec_read(codec, nid, 0,
						 AC_VERB_GET_PIN_WIDGET_CONTROL,
						 0x00);
572

573 574
	/* Find enumerated value for current pinctl setting */
	i = alc_pin_mode_min(dir);
Roel Kluin's avatar
Roel Kluin committed
575
	while (i <= alc_pin_mode_max(dir) && alc_pin_mode_values[i] != pinctl)
576
		i++;
577
	*valp = i <= alc_pin_mode_max(dir) ? i: alc_pin_mode_min(dir);
578 579 580
	return 0;
}

581 582
static int alc_pin_mode_put(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_value *ucontrol)
583
{
584
	signed int change;
585 586
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	hda_nid_t nid = kcontrol->private_value & 0xffff;
587 588
	unsigned char dir = (kcontrol->private_value >> 16) & 0xff;
	long val = *ucontrol->value.integer.value;
589 590 591
	unsigned int pinctl = snd_hda_codec_read(codec, nid, 0,
						 AC_VERB_GET_PIN_WIDGET_CONTROL,
						 0x00);
592

593
	if (val < alc_pin_mode_min(dir) || val > alc_pin_mode_max(dir))
594 595 596
		val = alc_pin_mode_min(dir);

	change = pinctl != alc_pin_mode_values[val];
597 598
	if (change) {
		/* Set pin mode to that requested */
599 600 601
		snd_hda_codec_write_cache(codec, nid, 0,
					  AC_VERB_SET_PIN_WIDGET_CONTROL,
					  alc_pin_mode_values[val]);
602

603
		/* Also enable the retasking pin's input/output as required
604 605 606 607
		 * for the requested pin mode.  Enum values of 2 or less are
		 * input modes.
		 *
		 * Dynamically switching the input/output buffers probably
608 609 610 611
		 * reduces noise slightly (particularly on input) so we'll
		 * do it.  However, having both input and output buffers
		 * enabled simultaneously doesn't seem to be problematic if
		 * this turns out to be necessary in the future.
612 613
		 */
		if (val <= 2) {
614 615 616 617
			snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
						 HDA_AMP_MUTE, HDA_AMP_MUTE);
			snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
						 HDA_AMP_MUTE, 0);
618
		} else {
619 620 621 622
			snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
						 HDA_AMP_MUTE, HDA_AMP_MUTE);
			snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
						 HDA_AMP_MUTE, 0);
623 624
		}
	}
625 626 627
	return change;
}

628
#define ALC_PIN_MODE(xname, nid, dir) \
629
	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
630 631 632 633
	  .info = alc_pin_mode_info, \
	  .get = alc_pin_mode_get, \
	  .put = alc_pin_mode_put, \
	  .private_value = nid | (dir<<16) }
634

635 636 637 638 639 640
/* A switch control for ALC260 GPIO pins.  Multiple GPIOs can be ganged
 * together using a mask with more than one bit set.  This control is
 * currently used only by the ALC260 test model.  At this stage they are not
 * needed for any "production" models.
 */
#ifdef CONFIG_SND_DEBUG
641
#define alc_gpio_data_info	snd_ctl_boolean_mono_info
642

643 644
static int alc_gpio_data_get(struct snd_kcontrol *kcontrol,
			     struct snd_ctl_elem_value *ucontrol)
645 646 647 648 649
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	hda_nid_t nid = kcontrol->private_value & 0xffff;
	unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
	long *valp = ucontrol->value.integer.value;
650 651
	unsigned int val = snd_hda_codec_read(codec, nid, 0,
					      AC_VERB_GET_GPIO_DATA, 0x00);
652 653 654 655

	*valp = (val & mask) != 0;
	return 0;
}
656 657
static int alc_gpio_data_put(struct snd_kcontrol *kcontrol,
			     struct snd_ctl_elem_value *ucontrol)
658 659 660 661 662 663
{
	signed int change;
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	hda_nid_t nid = kcontrol->private_value & 0xffff;
	unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
	long val = *ucontrol->value.integer.value;
664 665 666
	unsigned int gpio_data = snd_hda_codec_read(codec, nid, 0,
						    AC_VERB_GET_GPIO_DATA,
						    0x00);
667 668

	/* Set/unset the masked GPIO bit(s) as needed */
669 670
	change = (val == 0 ? 0 : mask) != (gpio_data & mask);
	if (val == 0)
671 672 673
		gpio_data &= ~mask;
	else
		gpio_data |= mask;
674 675
	snd_hda_codec_write_cache(codec, nid, 0,
				  AC_VERB_SET_GPIO_DATA, gpio_data);
676 677 678 679 680 681 682 683 684 685 686

	return change;
}
#define ALC_GPIO_DATA_SWITCH(xname, nid, mask) \
	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
	  .info = alc_gpio_data_info, \
	  .get = alc_gpio_data_get, \
	  .put = alc_gpio_data_put, \
	  .private_value = nid | (mask<<16) }
#endif   /* CONFIG_SND_DEBUG */

687 688 689 690 691 692 693 694
/* A switch control to allow the enabling of the digital IO pins on the
 * ALC260.  This is incredibly simplistic; the intention of this control is
 * to provide something in the test model allowing digital outputs to be
 * identified if present.  If models are found which can utilise these
 * outputs a more complete mixer control can be devised for those models if
 * necessary.
 */
#ifdef CONFIG_SND_DEBUG
695
#define alc_spdif_ctrl_info	snd_ctl_boolean_mono_info
696

697 698
static int alc_spdif_ctrl_get(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
699 700 701 702 703
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	hda_nid_t nid = kcontrol->private_value & 0xffff;
	unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
	long *valp = ucontrol->value.integer.value;
704
	unsigned int val = snd_hda_codec_read(codec, nid, 0,
705
					      AC_VERB_GET_DIGI_CONVERT_1, 0x00);
706 707 708 709

	*valp = (val & mask) != 0;
	return 0;
}
710 711
static int alc_spdif_ctrl_put(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
712 713 714 715 716 717
{
	signed int change;
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	hda_nid_t nid = kcontrol->private_value & 0xffff;
	unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
	long val = *ucontrol->value.integer.value;
718
	unsigned int ctrl_data = snd_hda_codec_read(codec, nid, 0,
719
						    AC_VERB_GET_DIGI_CONVERT_1,
720
						    0x00);
721 722

	/* Set/unset the masked control bit(s) as needed */
723
	change = (val == 0 ? 0 : mask) != (ctrl_data & mask);
724 725 726 727
	if (val==0)
		ctrl_data &= ~mask;
	else
		ctrl_data |= mask;
728 729
	snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
				  ctrl_data);
730 731 732 733 734 735 736 737 738 739 740

	return change;
}
#define ALC_SPDIF_CTRL_SWITCH(xname, nid, mask) \
	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
	  .info = alc_spdif_ctrl_info, \
	  .get = alc_spdif_ctrl_get, \
	  .put = alc_spdif_ctrl_put, \
	  .private_value = nid | (mask<<16) }
#endif   /* CONFIG_SND_DEBUG */

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
/* A switch control to allow the enabling EAPD digital outputs on the ALC26x.
 * Again, this is only used in the ALC26x test models to help identify when
 * the EAPD line must be asserted for features to work.
 */
#ifdef CONFIG_SND_DEBUG
#define alc_eapd_ctrl_info	snd_ctl_boolean_mono_info

static int alc_eapd_ctrl_get(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	hda_nid_t nid = kcontrol->private_value & 0xffff;
	unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
	long *valp = ucontrol->value.integer.value;
	unsigned int val = snd_hda_codec_read(codec, nid, 0,
					      AC_VERB_GET_EAPD_BTLENABLE, 0x00);

	*valp = (val & mask) != 0;
	return 0;
}

static int alc_eapd_ctrl_put(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
{
	int change;
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	hda_nid_t nid = kcontrol->private_value & 0xffff;
	unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
	long val = *ucontrol->value.integer.value;
	unsigned int ctrl_data = snd_hda_codec_read(codec, nid, 0,
						    AC_VERB_GET_EAPD_BTLENABLE,
						    0x00);

	/* Set/unset the masked control bit(s) as needed */
	change = (!val ? 0 : mask) != (ctrl_data & mask);
	if (!val)
		ctrl_data &= ~mask;
	else
		ctrl_data |= mask;
	snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
				  ctrl_data);

	return change;
}

#define ALC_EAPD_CTRL_SWITCH(xname, nid, mask) \
	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
	  .info = alc_eapd_ctrl_info, \
	  .get = alc_eapd_ctrl_get, \
	  .put = alc_eapd_ctrl_put, \
	  .private_value = nid | (mask<<16) }
#endif   /* CONFIG_SND_DEBUG */

794 795 796 797 798 799 800 801 802 803
/*
 * set up the input pin config (depending on the given auto-pin type)
 */
static void alc_set_input_pin(struct hda_codec *codec, hda_nid_t nid,
			      int auto_pin_type)
{
	unsigned int val = PIN_IN;

	if (auto_pin_type <= AUTO_PIN_FRONT_MIC) {
		unsigned int pincap;
804
		pincap = snd_hda_query_pin_caps(codec, nid);
805 806 807
		pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
		if (pincap & AC_PINCAP_VREF_80)
			val = PIN_VREF80;
808 809 810 811 812 813
		else if (pincap & AC_PINCAP_VREF_50)
			val = PIN_VREF50;
		else if (pincap & AC_PINCAP_VREF_100)
			val = PIN_VREF100;
		else if (pincap & AC_PINCAP_VREF_GRD)
			val = PIN_VREFGRD;
814 815 816 817
	}
	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, val);
}

818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
/*
 */
static void add_mixer(struct alc_spec *spec, struct snd_kcontrol_new *mix)
{
	if (snd_BUG_ON(spec->num_mixers >= ARRAY_SIZE(spec->mixers)))
		return;
	spec->mixers[spec->num_mixers++] = mix;
}

static void add_verb(struct alc_spec *spec, const struct hda_verb *verb)
{
	if (snd_BUG_ON(spec->num_init_verbs >= ARRAY_SIZE(spec->init_verbs)))
		return;
	spec->init_verbs[spec->num_init_verbs++] = verb;
}

834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
#ifdef CONFIG_PROC_FS
/*
 * hook for proc
 */
static void print_realtek_coef(struct snd_info_buffer *buffer,
			       struct hda_codec *codec, hda_nid_t nid)
{
	int coeff;

	if (nid != 0x20)
		return;
	coeff = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_PROC_COEF, 0);
	snd_iprintf(buffer, "  Processing Coefficient: 0x%02x\n", coeff);
	coeff = snd_hda_codec_read(codec, nid, 0,
				   AC_VERB_GET_COEF_INDEX, 0);
	snd_iprintf(buffer, "  Coefficient Index: 0x%02x\n", coeff);
}
#else
#define print_realtek_coef	NULL
#endif

855 856 857
/*
 * set up from the preset table
 */
858
static void setup_preset(struct hda_codec *codec,
859
			 const struct alc_config_preset *preset)
860
{
861
	struct alc_spec *spec = codec->spec;
862 863 864
	int i;

	for (i = 0; i < ARRAY_SIZE(preset->mixers) && preset->mixers[i]; i++)
865
		add_mixer(spec, preset->mixers[i]);
866
	spec->cap_mixer = preset->cap_mixer;
867 868
	for (i = 0; i < ARRAY_SIZE(preset->init_verbs) && preset->init_verbs[i];
	     i++)
869
		add_verb(spec, preset->init_verbs[i]);
870

871 872
	spec->channel_mode = preset->channel_mode;
	spec->num_channel_mode = preset->num_channel_mode;
873
	spec->need_dac_fix = preset->need_dac_fix;
874
	spec->const_channel_count = preset->const_channel_count;
875

876 877 878 879 880
	if (preset->const_channel_count)
		spec->multiout.max_channels = preset->const_channel_count;
	else
		spec->multiout.max_channels = spec->channel_mode[0].channels;
	spec->ext_channel_count = spec->channel_mode[0].channels;
881 882 883 884

	spec->multiout.num_dacs = preset->num_dacs;
	spec->multiout.dac_nids = preset->dac_nids;
	spec->multiout.dig_out_nid = preset->dig_out_nid;
885
	spec->multiout.slave_dig_outs = preset->slave_dig_outs;
886
	spec->multiout.hp_nid = preset->hp_nid;
887

888
	spec->num_mux_defs = preset->num_mux_defs;
889
	if (!spec->num_mux_defs)
890
		spec->num_mux_defs = 1;
891 892 893 894
	spec->input_mux = preset->input_mux;

	spec->num_adc_nids = preset->num_adc_nids;
	spec->adc_nids = preset->adc_nids;
895
	spec->capsrc_nids = preset->capsrc_nids;
896
	spec->dig_in_nid = preset->dig_in_nid;
897 898 899

	spec->unsol_event = preset->unsol_event;
	spec->init_hook = preset->init_hook;
900 901 902
#ifdef CONFIG_SND_HDA_POWER_SAVE
	spec->loopback.amplist = preset->loopbacks;
#endif
903 904 905

	if (preset->setup)
		preset->setup(codec);
906 907
}

908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
/* Enable GPIO mask and set output */
static struct hda_verb alc_gpio1_init_verbs[] = {
	{0x01, AC_VERB_SET_GPIO_MASK, 0x01},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x01},
	{ }
};

static struct hda_verb alc_gpio2_init_verbs[] = {
	{0x01, AC_VERB_SET_GPIO_MASK, 0x02},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x02},
	{ }
};

923 924 925 926 927 928 929
static struct hda_verb alc_gpio3_init_verbs[] = {
	{0x01, AC_VERB_SET_GPIO_MASK, 0x03},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x03},
	{ }
};

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
/*
 * Fix hardware PLL issue
 * On some codecs, the analog PLL gating control must be off while
 * the default value is 1.
 */
static void alc_fix_pll(struct hda_codec *codec)
{
	struct alc_spec *spec = codec->spec;
	unsigned int val;

	if (!spec->pll_nid)
		return;
	snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
			    spec->pll_coef_idx);
	val = snd_hda_codec_read(codec, spec->pll_nid, 0,
				 AC_VERB_GET_PROC_COEF, 0);
	snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
			    spec->pll_coef_idx);
	snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_PROC_COEF,
			    val & ~(1 << spec->pll_coef_bit));
}

static void alc_fix_pll_init(struct hda_codec *codec, hda_nid_t nid,
			     unsigned int coef_idx, unsigned int coef_bit)
{
	struct alc_spec *spec = codec->spec;
	spec->pll_nid = nid;
	spec->pll_coef_idx = coef_idx;
	spec->pll_coef_bit = coef_bit;
	alc_fix_pll(codec);
}

962
static void alc_automute_pin(struct hda_codec *codec)
963 964
{
	struct alc_spec *spec = codec->spec;
965 966
	unsigned int nid = spec->autocfg.hp_pins[0];
	int i;
967

968 969
	if (!nid)
		return;
970
	spec->jack_present = snd_hda_jack_detect(codec, nid);
971 972 973 974 975 976 977 978
	for (i = 0; i < ARRAY_SIZE(spec->autocfg.speaker_pins); i++) {
		nid = spec->autocfg.speaker_pins[i];
		if (!nid)
			break;
		snd_hda_codec_write(codec, nid, 0,
				    AC_VERB_SET_PIN_WIDGET_CONTROL,
				    spec->jack_present ? 0 : PIN_OUT);
	}
979 980
}

981 982 983 984 985 986 987 988 989 990 991 992 993
static int get_connection_index(struct hda_codec *codec, hda_nid_t mux,
				hda_nid_t nid)
{
	hda_nid_t conn[HDA_MAX_NUM_INPUTS];
	int i, nums;

	nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
	for (i = 0; i < nums; i++)
		if (conn[i] == nid)
			return i;
	return -1;
}

994 995 996
static void alc_mic_automute(struct hda_codec *codec)
{
	struct alc_spec *spec = codec->spec;
997 998 999 1000
	struct alc_mic_route *dead, *alive;
	unsigned int present, type;
	hda_nid_t cap_nid;

1001 1002
	if (!spec->auto_mic)
		return;
1003 1004 1005 1006 1007 1008 1009
	if (!spec->int_mic.pin || !spec->ext_mic.pin)
		return;
	if (snd_BUG_ON(!spec->adc_nids))
		return;

	cap_nid = spec->capsrc_nids ? spec->capsrc_nids[0] : spec->adc_nids[0];

1010
	present = snd_hda_jack_detect(codec, spec->ext_mic.pin);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
	if (present) {
		alive = &spec->ext_mic;
		dead = &spec->int_mic;
	} else {
		alive = &spec->int_mic;
		dead = &spec->ext_mic;
	}

	type = get_wcaps_type(get_wcaps(codec, cap_nid));
	if (type == AC_WID_AUD_MIX) {
		/* Matrix-mixer style (e.g. ALC882) */
		snd_hda_codec_amp_stereo(codec, cap_nid, HDA_INPUT,
					 alive->mux_idx,
					 HDA_AMP_MUTE, 0);
		snd_hda_codec_amp_stereo(codec, cap_nid, HDA_INPUT,
					 dead->mux_idx,
					 HDA_AMP_MUTE, HDA_AMP_MUTE);
	} else {
		/* MUX style (e.g. ALC880) */
		snd_hda_codec_write_cache(codec, cap_nid, 0,
					  AC_VERB_SET_CONNECT_SEL,
					  alive->mux_idx);
	}

	/* FIXME: analog mixer */
1036 1037
}

1038 1039 1040 1041 1042 1043 1044
/* unsolicited event for HP jack sensing */
static void alc_sku_unsol_event(struct hda_codec *codec, unsigned int res)
{
	if (codec->vendor_id == 0x10ec0880)
		res >>= 28;
	else
		res >>= 26;
1045 1046 1047 1048 1049
	switch (res) {
	case ALC880_HP_EVENT:
		alc_automute_pin(codec);
		break;
	case ALC880_MIC_EVENT:
1050
		alc_mic_automute(codec);
1051 1052
		break;
	}
1053 1054 1055 1056
}