patch_realtek.c 200 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
/*
 * Universal Interface for Intel High Definition Audio Codec
 *
4
 * HD audio interface patch for Realtek ALC codecs
Linus Torvalds's avatar
Linus Torvalds committed
5
 *
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>
Jonathan Woithe's avatar
Jonathan Woithe committed
9
 *                    Jonathan Woithe <jwoithe@just42.net>
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
 *
 *  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>
30
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
31
#include <sound/core.h>
32
#include <sound/jack.h>
Linus Torvalds's avatar
Linus Torvalds committed
33
34
#include "hda_codec.h"
#include "hda_local.h"
35
#include "hda_auto_parser.h"
36
#include "hda_beep.h"
37
#include "hda_jack.h"
Linus Torvalds's avatar
Linus Torvalds committed
38

39
40
41
42
43
/* unsol event tags */
#define ALC_FRONT_EVENT		0x01
#define ALC_DCVOL_EVENT		0x02
#define ALC_HP_EVENT		0x04
#define ALC_MIC_EVENT		0x08
44

45
46
47
/* for GPIO Poll */
#define GPIO_MASK	0x03

48
49
50
51
52
53
54
55
56
/* extra amp-initialization sequence types */
enum {
	ALC_INIT_NONE,
	ALC_INIT_DEFAULT,
	ALC_INIT_GPIO1,
	ALC_INIT_GPIO2,
	ALC_INIT_GPIO3,
};

57
58
59
60
61
62
63
64
65
66
struct alc_customize_define {
	unsigned int  sku_cfg;
	unsigned char port_connectivity;
	unsigned char check_sum;
	unsigned char customization;
	unsigned char external_amp;
	unsigned int  enable_pcbeep:1;
	unsigned int  platform_type:1;
	unsigned int  swap:1;
	unsigned int  override:1;
67
	unsigned int  fixup:1; /* Means that this sku is set by driver, not read from hw */
68
69
};

70
71
72
73
74
75
struct alc_multi_io {
	hda_nid_t pin;		/* multi-io widget pin NID */
	hda_nid_t dac;		/* DAC to be connected */
	unsigned int ctl_in;	/* cached input-pin control value */
};

76
enum {
77
78
79
	ALC_AUTOMUTE_PIN,	/* change the pin control */
	ALC_AUTOMUTE_AMP,	/* mute/unmute the pin AMP */
	ALC_AUTOMUTE_MIXER,	/* mute/unmute mixer widget AMP */
80
81
};

82
83
#define MAX_VOL_NIDS	0x40

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* make compatible with old code */
#define alc_apply_pincfgs	snd_hda_apply_pincfgs
#define alc_apply_fixup		snd_hda_apply_fixup
#define alc_pick_fixup		snd_hda_pick_fixup
#define alc_fixup		hda_fixup
#define alc_pincfg		hda_pintbl
#define alc_model_fixup		hda_model_fixup

#define ALC_FIXUP_PINS	HDA_FIXUP_PINS
#define ALC_FIXUP_VERBS	HDA_FIXUP_VERBS
#define ALC_FIXUP_FUNC	HDA_FIXUP_FUNC

#define ALC_FIXUP_ACT_PRE_PROBE	HDA_FIXUP_ACT_PRE_PROBE
#define ALC_FIXUP_ACT_PROBE	HDA_FIXUP_ACT_PROBE
#define ALC_FIXUP_ACT_INIT	HDA_FIXUP_ACT_INIT
#define ALC_FIXUP_ACT_BUILD	HDA_FIXUP_ACT_BUILD


Linus Torvalds's avatar
Linus Torvalds committed
102
struct alc_spec {
103
104
	struct hda_gen_spec gen;

Linus Torvalds's avatar
Linus Torvalds committed
105
	/* codec parameterization */
106
	const struct snd_kcontrol_new *mixers[5];	/* mixer arrays */
Linus Torvalds's avatar
Linus Torvalds committed
107
	unsigned int num_mixers;
108
	const struct snd_kcontrol_new *cap_mixer;	/* capture mixer */
109
	unsigned int beep_amp;	/* beep amp value, set via set_beep_amp() */
Linus Torvalds's avatar
Linus Torvalds committed
110

111
	char stream_name_analog[32];	/* analog PCM stream */
112
113
114
115
	const struct hda_pcm_stream *stream_analog_playback;
	const struct hda_pcm_stream *stream_analog_capture;
	const struct hda_pcm_stream *stream_analog_alt_playback;
	const struct hda_pcm_stream *stream_analog_alt_capture;
Linus Torvalds's avatar
Linus Torvalds committed
116

117
	char stream_name_digital[32];	/* digital PCM stream */
118
119
	const struct hda_pcm_stream *stream_digital_playback;
	const struct hda_pcm_stream *stream_digital_capture;
Linus Torvalds's avatar
Linus Torvalds committed
120
121

	/* playback */
122
123
124
125
	struct hda_multi_out multiout;	/* playback set-up
					 * max_channels, dacs must be set
					 * dig_out_nid and hp_nid are optional
					 */
126
	hda_nid_t alt_dac_nid;
127
	hda_nid_t slave_dig_outs[3];	/* optional - for auto-parsing */
128
	int dig_out_type;
Linus Torvalds's avatar
Linus Torvalds committed
129
130
131

	/* capture */
	unsigned int num_adc_nids;
132
133
	const hda_nid_t *adc_nids;
	const hda_nid_t *capsrc_nids;
134
	hda_nid_t dig_in_nid;		/* digital-in NID; optional */
135
	hda_nid_t mixer_nid;		/* analog-mixer NID */
136
137
	DECLARE_BITMAP(vol_ctls, MAX_VOL_NIDS << 1);
	DECLARE_BITMAP(sw_ctls, MAX_VOL_NIDS << 1);
Linus Torvalds's avatar
Linus Torvalds committed
138

139
140
141
142
143
	/* capture setup for dynamic dual-adc switch */
	hda_nid_t cur_adc;
	unsigned int cur_adc_stream_tag;
	unsigned int cur_adc_format;

Linus Torvalds's avatar
Linus Torvalds committed
144
	/* capture source */
145
	unsigned int num_mux_defs;
Linus Torvalds's avatar
Linus Torvalds committed
146
147
	const struct hda_input_mux *input_mux;
	unsigned int cur_mux[3];
148
149
150
	hda_nid_t ext_mic_pin;
	hda_nid_t dock_mic_pin;
	hda_nid_t int_mic_pin;
Linus Torvalds's avatar
Linus Torvalds committed
151
152

	/* channel model */
153
	const struct hda_channel_mode *channel_mode;
Linus Torvalds's avatar
Linus Torvalds committed
154
	int num_channel_mode;
155
	int need_dac_fix;
156
157
	int const_channel_count;	/* min. channel count (for speakers) */
	int ext_channel_count;		/* current channel count for multi-io */
Linus Torvalds's avatar
Linus Torvalds committed
158
159

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

162
163
	/* dynamic controls, init_verbs and input_mux */
	struct auto_pin_cfg autocfg;
164
	struct alc_customize_define cdefine;
165
	struct snd_array kctls;
166
	struct hda_input_mux private_imux[3];
167
	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
168
169
	hda_nid_t private_adc_nids[AUTO_CFG_MAX_OUTS];
	hda_nid_t private_capsrc_nids[AUTO_CFG_MAX_OUTS];
170
171
172
	hda_nid_t imux_pins[HDA_MAX_NUM_INPUTS];
	unsigned int dyn_adc_idx[HDA_MAX_NUM_INPUTS];
	int int_mic_idx, ext_mic_idx, dock_mic_idx; /* for auto-mic */
173
	hda_nid_t inv_dmic_pin;
174

175
176
	/* hooks */
	void (*init_hook)(struct hda_codec *codec);
177
#ifdef CONFIG_PM
178
	void (*power_hook)(struct hda_codec *codec);
179
#endif
180
	void (*shutup)(struct hda_codec *codec);
181
	void (*automute_hook)(struct hda_codec *codec);
182

183
	/* for pin sensing */
184
	unsigned int hp_jack_present:1;
185
	unsigned int line_jack_present:1;
186
	unsigned int master_mute:1;
187
	unsigned int auto_mic:1;
188
	unsigned int auto_mic_valid_imux:1;	/* valid imux for auto-mic */
189
190
191
192
193
194
	unsigned int automute_speaker:1; /* automute speaker outputs */
	unsigned int automute_lo:1; /* automute LO outputs */
	unsigned int detect_hp:1;	/* Headphone detection enabled */
	unsigned int detect_lo:1;	/* Line-out detection enabled */
	unsigned int automute_speaker_possible:1; /* there are speakers and either LO or HP */
	unsigned int automute_lo_possible:1;	  /* there are line outs and HP */
195
	unsigned int keep_vref_in_automute:1; /* Don't clear VREF in automute */
196

197
198
	/* other flags */
	unsigned int no_analog :1; /* digital I/O only */
199
	unsigned int dyn_adc_switch:1; /* switch ADCs (for ALC275) */
200
	unsigned int single_input_src:1;
201
	unsigned int vol_in_capsrc:1; /* use capsrc volume (ADC has no vol) */
202
	unsigned int parse_flags; /* passed to snd_hda_parse_pin_defcfg() */
203
	unsigned int shared_mic_hp:1; /* HP/Mic-in sharing */
204
205
	unsigned int inv_dmic_fixup:1; /* has inverted digital-mic workaround */
	unsigned int inv_dmic_muted:1; /* R-ch of inv d-mic is muted? */
206
	unsigned int no_primary_hp:1; /* Don't prefer HP pins to speaker pins */
207
208
209

	/* auto-mute control */
	int automute_mode;
210
	hda_nid_t automute_mixer_nid[AUTO_CFG_MAX_OUTS];
211

212
	int init_amp;
213
	int codec_variant;	/* flag for other variants */
214

215
216
	/* for virtual master */
	hda_nid_t vmaster_nid;
217
	struct hda_vmaster_mute_hook vmaster_mute;
218
#ifdef CONFIG_PM
219
	struct hda_loopback_check loopback;
220
221
	int num_loopbacks;
	struct hda_amp_list loopback_list[8];
222
#endif
223
224
225
226

	/* for PLL fix */
	hda_nid_t pll_nid;
	unsigned int pll_coef_idx, pll_coef_bit;
227
	unsigned int coef0;
228

229
230
231
	/* multi-io */
	int multi_ios;
	struct alc_multi_io multi_io[4];
232
233
234

	/* bind volumes */
	struct snd_array bind_ctls;
235
236
};

237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
			   int dir, unsigned int bits)
{
	if (!nid)
		return false;
	if (get_wcaps(codec, nid) & (1 << (dir + 1)))
		if (query_amp_caps(codec, nid, dir) & bits)
			return true;
	return false;
}

#define nid_has_mute(codec, nid, dir) \
	check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
#define nid_has_volume(codec, nid, dir) \
	check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)

Linus Torvalds's avatar
Linus Torvalds committed
253
254
255
/*
 * input MUX handling
 */
256
257
static int alc_mux_enum_info(struct snd_kcontrol *kcontrol,
			     struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
258
259
260
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct alc_spec *spec = codec->spec;
261
262
263
	unsigned int mux_idx = snd_ctl_get_ioffidx(kcontrol, &uinfo->id);
	if (mux_idx >= spec->num_mux_defs)
		mux_idx = 0;
264
265
	if (!spec->input_mux[mux_idx].num_items && mux_idx > 0)
		mux_idx = 0;
266
	return snd_hda_input_mux_info(&spec->input_mux[mux_idx], uinfo);
Linus Torvalds's avatar
Linus Torvalds committed
267
268
}

269
270
static int alc_mux_enum_get(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
271
272
273
274
275
276
277
278
279
{
	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;
}

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
static bool alc_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
{
	struct alc_spec *spec = codec->spec;
	hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];

	if (spec->cur_adc && spec->cur_adc != new_adc) {
		/* stream is running, let's swap the current ADC */
		__snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
		spec->cur_adc = new_adc;
		snd_hda_codec_setup_stream(codec, new_adc,
					   spec->cur_adc_stream_tag, 0,
					   spec->cur_adc_format);
		return true;
	}
	return false;
}

297
298
299
300
301
302
static inline hda_nid_t get_capsrc(struct alc_spec *spec, int idx)
{
	return spec->capsrc_nids ?
		spec->capsrc_nids[idx] : spec->adc_nids[idx];
}

303
static void call_update_outputs(struct hda_codec *codec);
304
static void alc_inv_dmic_sync(struct hda_codec *codec, bool force);
305

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* for shared I/O, change the pin-control accordingly */
static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
{
	struct alc_spec *spec = codec->spec;
	unsigned int val;
	hda_nid_t pin = spec->autocfg.inputs[1].pin;
	/* NOTE: this assumes that there are only two inputs, the
	 * first is the real internal mic and the second is HP/mic jack.
	 */

	val = snd_hda_get_default_vref(codec, pin);

	/* This pin does not have vref caps - let's enable vref on pin 0x18
	   instead, as suggested by Realtek */
	if (val == AC_PINCTL_VREF_HIZ) {
		const hda_nid_t vref_pin = 0x18;
		/* Sanity check pin 0x18 */
		if (get_wcaps_type(get_wcaps(codec, vref_pin)) == AC_WID_PIN &&
		    get_defcfg_connect(snd_hda_codec_get_pincfg(codec, vref_pin)) == AC_JACK_PORT_NONE) {
			unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
			if (vref_val != AC_PINCTL_VREF_HIZ)
				snd_hda_set_pin_ctl(codec, vref_pin, PIN_IN | (set_as_mic ? vref_val : 0));
		}
	}

	val = set_as_mic ? val | PIN_IN : PIN_HP;
	snd_hda_set_pin_ctl(codec, pin, val);

	spec->automute_speaker = !set_as_mic;
	call_update_outputs(codec);
}
337

338
339
340
/* select the given imux item; either unmute exclusively or select the route */
static int alc_mux_select(struct hda_codec *codec, unsigned int adc_idx,
			  unsigned int idx, bool force)
Linus Torvalds's avatar
Linus Torvalds committed
341
342
{
	struct alc_spec *spec = codec->spec;
343
344
	const struct hda_input_mux *imux;
	unsigned int mux_idx;
345
	int i, type, num_conns;
346
	hda_nid_t nid;
Linus Torvalds's avatar
Linus Torvalds committed
347

348
349
350
	if (!spec->input_mux)
		return 0;

351
352
	mux_idx = adc_idx >= spec->num_mux_defs ? 0 : adc_idx;
	imux = &spec->input_mux[mux_idx];
353
354
	if (!imux->num_items && mux_idx > 0)
		imux = &spec->input_mux[0];
355
356
	if (!imux->num_items)
		return 0;
357

358
359
360
361
362
363
	if (idx >= imux->num_items)
		idx = imux->num_items - 1;
	if (spec->cur_mux[adc_idx] == idx && !force)
		return 0;
	spec->cur_mux[adc_idx] = idx;

364
365
	if (spec->shared_mic_hp)
		update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
366

367
368
369
370
371
	if (spec->dyn_adc_switch) {
		alc_dyn_adc_pcm_resetup(codec, idx);
		adc_idx = spec->dyn_adc_idx[idx];
	}

372
	nid = get_capsrc(spec, adc_idx);
373
374

	/* no selection? */
375
	num_conns = snd_hda_get_num_conns(codec, nid);
376
	if (num_conns <= 1)
377
378
		return 1;

379
	type = get_wcaps_type(get_wcaps(codec, nid));
380
	if (type == AC_WID_AUD_MIX) {
381
		/* Matrix-mixer style (e.g. ALC882) */
382
383
384
385
		int active = imux->items[idx].index;
		for (i = 0; i < num_conns; i++) {
			unsigned int v = (i == active) ? 0 : HDA_AMP_MUTE;
			snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, i,
386
387
388
389
						 HDA_AMP_MUTE, v);
		}
	} else {
		/* MUX style (e.g. ALC880) */
390
391
392
		snd_hda_codec_write_cache(codec, nid, 0,
					  AC_VERB_SET_CONNECT_SEL,
					  imux->items[idx].index);
393
	}
394
	alc_inv_dmic_sync(codec, true);
395
396
397
398
399
400
401
402
403
404
	return 1;
}

static int alc_mux_enum_put(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
	return alc_mux_select(codec, adc_idx,
			      ucontrol->value.enumerated.item[0], false);
405
}
406

407
408
409
410
411
412
413
/*
 * 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;
414
415
	if (auto_pin_type == AUTO_PIN_MIC)
		val |= snd_hda_get_default_vref(codec, nid);
416
	snd_hda_set_pin_ctl(codec, nid, val);
417
418
}

419
/*
420
421
422
 * Append the given mixer and verb elements for the later use
 * The mixer array is referred in build_controls(), and init_verbs are
 * called in init().
423
 */
424
static void add_mixer(struct alc_spec *spec, const struct snd_kcontrol_new *mix)
425
426
427
428
429
430
{
	if (snd_BUG_ON(spec->num_mixers >= ARRAY_SIZE(spec->mixers)))
		return;
	spec->mixers[spec->num_mixers++] = mix;
}

431
/*
432
 * GPIO setup tables, used in initialization
433
 */
434
/* Enable GPIO mask and set output */
435
static const struct hda_verb alc_gpio1_init_verbs[] = {
436
437
438
439
440
441
	{0x01, AC_VERB_SET_GPIO_MASK, 0x01},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x01},
	{ }
};

442
static const struct hda_verb alc_gpio2_init_verbs[] = {
443
444
445
446
447
448
	{0x01, AC_VERB_SET_GPIO_MASK, 0x02},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x02},
	{ }
};

449
static const struct hda_verb alc_gpio3_init_verbs[] = {
450
451
452
453
454
455
	{0x01, AC_VERB_SET_GPIO_MASK, 0x03},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x03},
	{ }
};

456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
 * 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);
}

488
489
490
491
492
493
/*
 * Jack detections for HP auto-mute and mic-switch
 */

/* check each pin in the given array; returns true if any of them is plugged */
static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
494
{
495
	int i, present = 0;
496

497
498
	for (i = 0; i < num_pins; i++) {
		hda_nid_t nid = pins[i];
499
500
		if (!nid)
			break;
501
		present |= snd_hda_jack_detect(codec, nid);
502
	}
503
504
	return present;
}
505

506
/* standard HP/line-out auto-mute helper */
507
static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
508
			bool mute, bool hp_out)
509
510
511
{
	struct alc_spec *spec = codec->spec;
	unsigned int mute_bits = mute ? HDA_AMP_MUTE : 0;
512
	unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
513
514
515
516
	int i;

	for (i = 0; i < num_pins; i++) {
		hda_nid_t nid = pins[i];
517
		unsigned int val;
518
519
		if (!nid)
			break;
520
521
		switch (spec->automute_mode) {
		case ALC_AUTOMUTE_PIN:
522
523
524
525
526
527
528
529
530
531
			/* don't reset VREF value in case it's controlling
			 * the amp (see alc861_fixup_asus_amp_vref_0f())
			 */
			if (spec->keep_vref_in_automute) {
				val = snd_hda_codec_read(codec, nid, 0,
					AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
				val &= ~PIN_HP;
			} else
				val = 0;
			val |= pin_bits;
532
			snd_hda_set_pin_ctl(codec, nid, val);
533
534
			break;
		case ALC_AUTOMUTE_AMP:
535
			snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
536
						 HDA_AMP_MUTE, mute_bits);
537
538
539
540
541
542
			break;
		case ALC_AUTOMUTE_MIXER:
			nid = spec->automute_mixer_nid[i];
			if (!nid)
				break;
			snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
543
						 HDA_AMP_MUTE, mute_bits);
544
			snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 1,
545
						 HDA_AMP_MUTE, mute_bits);
546
			break;
547
		}
548
	}
549
550
}

551
552
/* Toggle outputs muting */
static void update_outputs(struct hda_codec *codec)
553
554
{
	struct alc_spec *spec = codec->spec;
555
	int on;
556

557
558
559
560
	/* Control HP pins/amps depending on master_mute state;
	 * in general, HP pins/amps control should be enabled in all cases,
	 * but currently set only for master_mute, just to be safe
	 */
561
562
	if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
		do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
563
564
		    spec->autocfg.hp_pins, spec->master_mute, true);

565
	if (!spec->automute_speaker)
566
567
		on = 0;
	else
568
		on = spec->hp_jack_present | spec->line_jack_present;
569
	on |= spec->master_mute;
570
	do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
571
		    spec->autocfg.speaker_pins, on, false);
572
573

	/* toggle line-out mutes if needed, too */
574
575
576
	/* if LO is a copy of either HP or Speaker, don't need to handle it */
	if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
	    spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
577
		return;
578
	if (!spec->automute_lo)
579
580
		on = 0;
	else
581
		on = spec->hp_jack_present;
582
	on |= spec->master_mute;
583
	do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
584
		    spec->autocfg.line_out_pins, on, false);
585
586
}

587
static void call_update_outputs(struct hda_codec *codec)
588
589
590
591
592
{
	struct alc_spec *spec = codec->spec;
	if (spec->automute_hook)
		spec->automute_hook(codec);
	else
593
		update_outputs(codec);
594
595
}

596
/* standard HP-automute helper */
597
static void alc_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
598
599
600
{
	struct alc_spec *spec = codec->spec;

601
	spec->hp_jack_present =
602
603
		detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
			     spec->autocfg.hp_pins);
604
	if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
605
		return;
606
	call_update_outputs(codec);
607
608
}

609
/* standard line-out-automute helper */
610
static void alc_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
611
612
613
{
	struct alc_spec *spec = codec->spec;

614
615
	if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
		return;
616
617
618
619
	/* check LO jack only when it's different from HP */
	if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
		return;

620
621
622
	spec->line_jack_present =
		detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
			     spec->autocfg.line_out_pins);
623
	if (!spec->automute_speaker || !spec->detect_lo)
624
		return;
625
	call_update_outputs(codec);
626
627
}

628
629
#define get_connection_index(codec, mux, nid) \
	snd_hda_get_conn_index(codec, mux, nid, 0)
630

631
/* standard mic auto-switch helper */
632
static void alc_mic_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
633
634
{
	struct alc_spec *spec = codec->spec;
635
	hda_nid_t *pins = spec->imux_pins;
636

637
	if (!spec->auto_mic || !spec->auto_mic_valid_imux)
638
639
640
		return;
	if (snd_BUG_ON(!spec->adc_nids))
		return;
641
	if (snd_BUG_ON(spec->int_mic_idx < 0 || spec->ext_mic_idx < 0))
642
		return;
643

644
645
646
647
648
649
650
	if (snd_hda_jack_detect(codec, pins[spec->ext_mic_idx]))
		alc_mux_select(codec, 0, spec->ext_mic_idx, false);
	else if (spec->dock_mic_idx >= 0 &&
		   snd_hda_jack_detect(codec, pins[spec->dock_mic_idx]))
		alc_mux_select(codec, 0, spec->dock_mic_idx, false);
	else
		alc_mux_select(codec, 0, spec->int_mic_idx, false);
651
652
}

653
/* update the master volume per volume-knob's unsol event */
654
static void alc_update_knob_master(struct hda_codec *codec, struct hda_jack_tbl *jack)
655
656
657
658
659
660
661
662
663
664
665
{
	unsigned int val;
	struct snd_kcontrol *kctl;
	struct snd_ctl_elem_value *uctl;

	kctl = snd_hda_find_mixer_ctl(codec, "Master Playback Volume");
	if (!kctl)
		return;
	uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
	if (!uctl)
		return;
666
	val = snd_hda_codec_read(codec, jack->nid, 0,
667
668
669
670
671
672
673
674
				 AC_VERB_GET_VOLUME_KNOB_CONTROL, 0);
	val &= HDA_AMP_VOLMASK;
	uctl->value.integer.value[0] = val;
	uctl->value.integer.value[1] = val;
	kctl->put(kctl, uctl);
	kfree(uctl);
}

675
static void alc880_unsol_event(struct hda_codec *codec, unsigned int res)
676
{
677
678
679
	/* For some reason, the res given from ALC880 is broken.
	   Here we adjust it properly. */
	snd_hda_jack_unsol_event(codec, res >> 2);
680
681
}

682
/* call init functions of standard auto-mute helpers */
683
684
static void alc_inithook(struct hda_codec *codec)
{
685
686
687
	alc_hp_automute(codec, NULL);
	alc_line_automute(codec, NULL);
	alc_mic_automute(codec, NULL);
688
689
}

690
691
692
693
694
695
696
697
/* additional initialization for ALC888 variants */
static void alc888_coef_init(struct hda_codec *codec)
{
	unsigned int tmp;

	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 0);
	tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
698
	if ((tmp & 0xf0) == 0x20)
699
700
701
702
703
704
705
706
707
		/* alc888S-VC */
		snd_hda_codec_read(codec, 0x20, 0,
				   AC_VERB_SET_PROC_COEF, 0x830);
	 else
		 /* alc888-VB */
		 snd_hda_codec_read(codec, 0x20, 0,
				    AC_VERB_SET_PROC_COEF, 0x3030);
}

708
/* additional initialization for ALC889 variants */
709
710
711
712
713
714
715
716
717
718
static void alc889_coef_init(struct hda_codec *codec)
{
	unsigned int tmp;

	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
	tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF, tmp|0x2010);
}

719
720
721
722
723
724
725
726
727
728
/* turn on/off EAPD control (only if available) */
static void set_eapd(struct hda_codec *codec, hda_nid_t nid, int on)
{
	if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
		return;
	if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
				    on ? 2 : 0);
}

729
730
731
732
/* turn on/off EAPD controls of the codec */
static void alc_auto_setup_eapd(struct hda_codec *codec, bool on)
{
	/* We currently only handle front, HP */
733
734
735
736
737
738
	static hda_nid_t pins[] = {
		0x0f, 0x10, 0x14, 0x15, 0
	};
	hda_nid_t *p;
	for (p = pins; *p; p++)
		set_eapd(codec, *p, on);
739
740
}

741
742
743
744
745
746
747
748
749
/* generic shutup callback;
 * just turning off EPAD and a little pause for avoiding pop-noise
 */
static void alc_eapd_shutup(struct hda_codec *codec)
{
	alc_auto_setup_eapd(codec, false);
	msleep(200);
}

750
/* generic EAPD initialization */
751
static void alc_auto_init_amp(struct hda_codec *codec, int type)
752
{
753
	unsigned int tmp;
754

755
	alc_auto_setup_eapd(codec, true);
756
757
	switch (type) {
	case ALC_INIT_GPIO1:
758
759
		snd_hda_sequence_write(codec, alc_gpio1_init_verbs);
		break;
760
	case ALC_INIT_GPIO2:
761
762
		snd_hda_sequence_write(codec, alc_gpio2_init_verbs);
		break;
763
	case ALC_INIT_GPIO3:
764
765
		snd_hda_sequence_write(codec, alc_gpio3_init_verbs);
		break;
766
	case ALC_INIT_DEFAULT:
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
		switch (codec->vendor_id) {
		case 0x10ec0260:
			snd_hda_codec_write(codec, 0x1a, 0,
					    AC_VERB_SET_COEF_INDEX, 7);
			tmp = snd_hda_codec_read(codec, 0x1a, 0,
						 AC_VERB_GET_PROC_COEF, 0);
			snd_hda_codec_write(codec, 0x1a, 0,
					    AC_VERB_SET_COEF_INDEX, 7);
			snd_hda_codec_write(codec, 0x1a, 0,
					    AC_VERB_SET_PROC_COEF,
					    tmp | 0x2010);
			break;
		case 0x10ec0262:
		case 0x10ec0880:
		case 0x10ec0882:
		case 0x10ec0883:
		case 0x10ec0885:
784
		case 0x10ec0887:
785
		/*case 0x10ec0889:*/ /* this causes an SPDIF problem */
786
			alc889_coef_init(codec);
787
			break;
788
		case 0x10ec0888:
789
			alc888_coef_init(codec);
790
			break;
791
#if 0 /* XXX: This may cause the silent output on speaker on some machines */
792
793
794
795
796
797
798
		case 0x10ec0267:
		case 0x10ec0268:
			snd_hda_codec_write(codec, 0x20, 0,
					    AC_VERB_SET_COEF_INDEX, 7);
			tmp = snd_hda_codec_read(codec, 0x20, 0,
						 AC_VERB_GET_PROC_COEF, 0);
			snd_hda_codec_write(codec, 0x20, 0,
799
					    AC_VERB_SET_COEF_INDEX, 7);
800
801
802
803
			snd_hda_codec_write(codec, 0x20, 0,
					    AC_VERB_SET_PROC_COEF,
					    tmp | 0x3000);
			break;
804
#endif /* XXX */
805
		}
806
807
808
809
		break;
	}
}

810
811
812
/*
 * Auto-Mute mode mixer enum support
 */
813
814
815
static int alc_automute_mode_info(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_info *uinfo)
{
816
817
818
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct alc_spec *spec = codec->spec;
	static const char * const texts3[] = {
819
		"Disabled", "Speaker Only", "Line Out+Speaker"
820
821
	};

822
823
824
	if (spec->automute_speaker_possible && spec->automute_lo_possible)
		return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
	return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
825
826
827
828
829
830
831
}

static int alc_automute_mode_get(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct alc_spec *spec = codec->spec;
832
833
834
835
836
837
	unsigned int val = 0;
	if (spec->automute_speaker)
		val++;
	if (spec->automute_lo)
		val++;

838
839
840
841
842
843
844
845
846
847
848
849
	ucontrol->value.enumerated.item[0] = val;
	return 0;
}

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

	switch (ucontrol->value.enumerated.item[0]) {
	case 0:
850
		if (!spec->automute_speaker && !spec->automute_lo)
851
			return 0;
852
853
		spec->automute_speaker = 0;
		spec->automute_lo = 0;
854
855
		break;
	case 1:
856
857
858
859
860
861
862
863
864
865
866
		if (spec->automute_speaker_possible) {
			if (!spec->automute_lo && spec->automute_speaker)
				return 0;
			spec->automute_speaker = 1;
			spec->automute_lo = 0;
		} else if (spec->automute_lo_possible) {
			if (spec->automute_lo)
				return 0;
			spec->automute_lo = 1;
		} else
			return -EINVAL;
867
868
		break;
	case 2:
869
		if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
870
			return -EINVAL;
871
		if (spec->automute_speaker && spec->automute_lo)
872
			return 0;
873
874
		spec->automute_speaker = 1;
		spec->automute_lo = 1;
875
876
877
878
		break;
	default:
		return -EINVAL;
	}
879
	call_update_outputs(codec);
880
881
882
	return 1;
}

883
static const struct snd_kcontrol_new alc_automute_mode_enum = {
884
885
886
887
888
889
890
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Auto-Mute Mode",
	.info = alc_automute_mode_info,
	.get = alc_automute_mode_get,
	.put = alc_automute_mode_put,
};

891
892
893
static struct snd_kcontrol_new *
alc_kcontrol_new(struct alc_spec *spec, const char *name,
		 const struct snd_kcontrol_new *temp)
894
{
895
896
897
898
899
900
901
902
	struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
	if (!knew)
		return NULL;
	*knew = *temp;
	knew->name = kstrdup(name, GFP_KERNEL);
	if (!knew->name)
		return NULL;
	return knew;
903
}
904
905
906
907
908

static int alc_add_automute_mode_enum(struct hda_codec *codec)
{
	struct alc_spec *spec = codec->spec;

909
	if (!alc_kcontrol_new(spec, "Auto-Mute Mode", &alc_automute_mode_enum))
910
911
912
913
		return -ENOMEM;
	return 0;
}

914
915
916
917
/*
 * Check the availability of HP/line-out auto-mute;
 * Set up appropriately if really supported
 */
918
static int alc_init_automute(struct hda_codec *codec)
919
920
{
	struct alc_spec *spec = codec->spec;
921
	struct auto_pin_cfg *cfg = &spec->autocfg;
922
	int present = 0;
923
	int i, err;
924

925
926
927
928
929
930
931
	if (cfg->hp_pins[0])
		present++;
	if (cfg->line_out_pins[0])
		present++;
	if (cfg->speaker_pins[0])
		present++;
	if (present < 2) /* need two different output types */
932
		return 0;
933

934
935
	if (!cfg->speaker_pins[0] &&
	    cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
936
937
938
939
940
		memcpy(cfg->speaker_pins, cfg->line_out_pins,
		       sizeof(cfg->speaker_pins));
		cfg->speaker_outs = cfg->line_outs;
	}

941
942
	if (!cfg->hp_pins[0] &&
	    cfg->line_out_type == AUTO_PIN_HP_OUT) {
943
944
945
		memcpy(cfg->hp_pins, cfg->line_out_pins,
		       sizeof(cfg->hp_pins));
		cfg->hp_outs = cfg->line_outs;
946
947
	}

948
949
	spec->automute_mode = ALC_AUTOMUTE_PIN;

950
	for (i = 0; i < cfg->hp_outs; i++) {
951
		hda_nid_t nid = cfg->hp_pins[i];
952
		if (!is_jack_detectable(codec, nid))
953
			continue;
954
		snd_printdd("realtek: Enable HP auto-muting on NID 0x%x\n",
955
			    nid);
956
957
		snd_hda_jack_detect_enable_callback(codec, nid, ALC_HP_EVENT,
						    alc_hp_automute);
958
959
960
961
962
963
964
965
966
967
968
		spec->detect_hp = 1;
	}

	if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
		if (cfg->speaker_outs)
			for (i = 0; i < cfg->line_outs; i++) {
				hda_nid_t nid = cfg->line_out_pins[i];
				if (!is_jack_detectable(codec, nid))
					continue;
				snd_printdd("realtek: Enable Line-Out "
					    "auto-muting on NID 0x%x\n", nid);
969
970
				snd_hda_jack_detect_enable_callback(codec, nid, ALC_FRONT_EVENT,
								    alc_line_automute);
971
				spec->detect_lo = 1;
972
			}
973
		spec->automute_lo_possible = spec->detect_hp;
974
975
	}

976
977
978
979
980
981
	spec->automute_speaker_possible = cfg->speaker_outs &&
		(spec->detect_hp || spec->detect_lo);

	spec->automute_lo = spec->automute_lo_possible;
	spec->automute_speaker = spec->automute_speaker_possible;

982
	if (spec->automute_speaker_possible || spec->automute_lo_possible) {
983
		/* create a control for automute mode */
984
985
986
987
988
		err = alc_add_automute_mode_enum(codec);
		if (err < 0)
			return err;
	}
	return 0;
989
990
}

991
/* return the position of NID in the list, or -1 if not found */
992
993
994
995
996
997
998
999
1000
static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
{
	int i;
	for (i = 0; i < nums; i++)
		if (list[i] == nid)
			return i;
	return -1;
}