patch_realtek.c 248 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/dmi.h>
31
#include <linux/module.h>
32
#include <linux/input.h>
Linus Torvalds's avatar
Linus Torvalds committed
33
#include <sound/core.h>
34
#include <sound/jack.h>
Linus Torvalds's avatar
Linus Torvalds committed
35
36
#include "hda_codec.h"
#include "hda_local.h"
37
#include "hda_auto_parser.h"
38
#include "hda_jack.h"
39
#include "hda_generic.h"
Linus Torvalds's avatar
Linus Torvalds committed
40

41
42
43
/* keep halting ALC5505 DSP, for power saving */
#define HALT_REALTEK_ALC5505

44
45
46
47
48
49
50
51
52
/* extra amp-initialization sequence types */
enum {
	ALC_INIT_NONE,
	ALC_INIT_DEFAULT,
	ALC_INIT_GPIO1,
	ALC_INIT_GPIO2,
	ALC_INIT_GPIO3,
};

53
54
55
56
57
58
59
60
61
62
63
64
65
66
enum {
	ALC_HEADSET_MODE_UNKNOWN,
	ALC_HEADSET_MODE_UNPLUGGED,
	ALC_HEADSET_MODE_HEADSET,
	ALC_HEADSET_MODE_MIC,
	ALC_HEADSET_MODE_HEADPHONE,
};

enum {
	ALC_HEADSET_TYPE_UNKNOWN,
	ALC_HEADSET_TYPE_CTIA,
	ALC_HEADSET_TYPE_OMTP,
};

67
68
69
70
enum {
	ALC_KEY_MICMUTE_INDEX,
};

71
72
73
74
75
76
77
78
79
80
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;
81
	unsigned int  fixup:1; /* Means that this sku is set by driver, not read from hw */
82
83
};

Linus Torvalds's avatar
Linus Torvalds committed
84
struct alc_spec {
85
	struct hda_gen_spec gen; /* must be at head */
86

Linus Torvalds's avatar
Linus Torvalds committed
87
	/* codec parameterization */
88
	const struct snd_kcontrol_new *mixers[5];	/* mixer arrays */
Linus Torvalds's avatar
Linus Torvalds committed
89
	unsigned int num_mixers;
90
	unsigned int beep_amp;	/* beep amp value, set via set_beep_amp() */
Linus Torvalds's avatar
Linus Torvalds committed
91

92
	struct alc_customize_define cdefine;
93
	unsigned int parse_flags; /* flag for snd_hda_parse_pin_defcfg() */
94

95
96
97
	/* mute LED for HP laptops, see alc269_fixup_mic_mute_hook() */
	int mute_led_polarity;
	hda_nid_t mute_led_nid;
98
	hda_nid_t cap_mute_led_nid;
99

100
	unsigned int gpio_led; /* used for alc269_fixup_hp_gpio_led() */
101
102
	unsigned int gpio_mute_led_mask;
	unsigned int gpio_mic_led_mask;
103

104
105
106
107
108
	hda_nid_t headset_mic_pin;
	hda_nid_t headphone_mic_pin;
	int current_headset_mode;
	int current_headset_type;

109
110
	/* hooks */
	void (*init_hook)(struct hda_codec *codec);
111
#ifdef CONFIG_PM
112
	void (*power_hook)(struct hda_codec *codec);
113
#endif
114
	void (*shutup)(struct hda_codec *codec);
115
	void (*reboot_notify)(struct hda_codec *codec);
116

117
	int init_amp;
118
	int codec_variant;	/* flag for other variants */
119
120
	unsigned int has_alc5505_dsp:1;
	unsigned int no_depop_delay:1;
121

122
123
124
	/* for PLL fix */
	hda_nid_t pll_nid;
	unsigned int pll_coef_idx, pll_coef_bit;
125
	unsigned int coef0;
126
	struct input_dev *kb_dev;
127
	u8 alc_mute_keycode_map[1];
128
129
};

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * COEF access helper functions
 */

static int alc_read_coefex_idx(struct hda_codec *codec, hda_nid_t nid,
			       unsigned int coef_idx)
{
	unsigned int val;

	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_COEF_INDEX, coef_idx);
	val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_PROC_COEF, 0);
	return val;
}

#define alc_read_coef_idx(codec, coef_idx) \
	alc_read_coefex_idx(codec, 0x20, coef_idx)

static void alc_write_coefex_idx(struct hda_codec *codec, hda_nid_t nid,
				 unsigned int coef_idx, unsigned int coef_val)
{
	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_COEF_INDEX, coef_idx);
	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PROC_COEF, coef_val);
}

#define alc_write_coef_idx(codec, coef_idx, coef_val) \
	alc_write_coefex_idx(codec, 0x20, coef_idx, coef_val)

157
158
159
160
161
162
163
164
165
166
167
168
169
170
static void alc_update_coefex_idx(struct hda_codec *codec, hda_nid_t nid,
				  unsigned int coef_idx, unsigned int mask,
				  unsigned int bits_set)
{
	unsigned int val = alc_read_coefex_idx(codec, nid, coef_idx);

	if (val != -1)
		alc_write_coefex_idx(codec, nid, coef_idx,
				     (val & ~mask) | bits_set);
}

#define alc_update_coef_idx(codec, coef_idx, mask, bits_set)	\
	alc_update_coefex_idx(codec, 0x20, coef_idx, mask, bits_set)

171
172
173
174
175
176
177
178
179
180
/* a special bypass for COEF 0; read the cached value at the second time */
static unsigned int alc_get_coef0(struct hda_codec *codec)
{
	struct alc_spec *spec = codec->spec;

	if (!spec->coef0)
		spec->coef0 = alc_read_coef_idx(codec, 0);
	return spec->coef0;
}

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* coef writes/updates batch */
struct coef_fw {
	unsigned char nid;
	unsigned char idx;
	unsigned short mask;
	unsigned short val;
};

#define UPDATE_COEFEX(_nid, _idx, _mask, _val) \
	{ .nid = (_nid), .idx = (_idx), .mask = (_mask), .val = (_val) }
#define WRITE_COEFEX(_nid, _idx, _val) UPDATE_COEFEX(_nid, _idx, -1, _val)
#define WRITE_COEF(_idx, _val) WRITE_COEFEX(0x20, _idx, _val)
#define UPDATE_COEF(_idx, _mask, _val) UPDATE_COEFEX(0x20, _idx, _mask, _val)

static void alc_process_coef_fw(struct hda_codec *codec,
				const struct coef_fw *fw)
{
	for (; fw->nid; fw++) {
		if (fw->mask == (unsigned short)-1)
			alc_write_coefex_idx(codec, fw->nid, fw->idx, fw->val);
		else
			alc_update_coefex_idx(codec, fw->nid, fw->idx,
					      fw->mask, fw->val);
	}
}

207
/*
208
209
210
 * 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().
211
 */
212
static void add_mixer(struct alc_spec *spec, const struct snd_kcontrol_new *mix)
213
214
215
216
217
218
{
	if (snd_BUG_ON(spec->num_mixers >= ARRAY_SIZE(spec->mixers)))
		return;
	spec->mixers[spec->num_mixers++] = mix;
}

219
/*
220
 * GPIO setup tables, used in initialization
221
 */
222
/* Enable GPIO mask and set output */
223
static const struct hda_verb alc_gpio1_init_verbs[] = {
224
225
226
227
228
229
	{0x01, AC_VERB_SET_GPIO_MASK, 0x01},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x01},
	{ }
};

230
static const struct hda_verb alc_gpio2_init_verbs[] = {
231
232
233
234
235
236
	{0x01, AC_VERB_SET_GPIO_MASK, 0x02},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x02},
	{ }
};

237
static const struct hda_verb alc_gpio3_init_verbs[] = {
238
239
240
241
242
243
	{0x01, AC_VERB_SET_GPIO_MASK, 0x03},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x03},
	{ }
};

244
245
246
247
248
249
250
251
252
/*
 * 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;

253
254
255
	if (spec->pll_nid)
		alc_update_coefex_idx(codec, spec->pll_nid, spec->pll_coef_idx,
				      1 << spec->pll_coef_bit, 0);
256
257
258
259
260
261
262
263
264
265
266
267
}

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);
}

268
/* update the master volume per volume-knob's unsol event */
269
270
static void alc_update_knob_master(struct hda_codec *codec,
				   struct hda_jack_callback *jack)
271
272
273
274
275
276
277
278
279
280
281
{
	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;
282
	val = snd_hda_codec_read(codec, jack->nid, 0,
283
284
285
286
287
288
289
290
				 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);
}

291
static void alc880_unsol_event(struct hda_codec *codec, unsigned int res)
292
{
293
294
295
	/* For some reason, the res given from ALC880 is broken.
	   Here we adjust it properly. */
	snd_hda_jack_unsol_event(codec, res >> 2);
296
297
}

298
299
300
301
302
303
304
/* Change EAPD to verb control */
static void alc_fill_eapd_coef(struct hda_codec *codec)
{
	int coef;

	coef = alc_get_coef0(codec);

305
	switch (codec->core.vendor_id) {
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
	case 0x10ec0262:
		alc_update_coef_idx(codec, 0x7, 0, 1<<5);
		break;
	case 0x10ec0267:
	case 0x10ec0268:
		alc_update_coef_idx(codec, 0x7, 0, 1<<13);
		break;
	case 0x10ec0269:
		if ((coef & 0x00f0) == 0x0010)
			alc_update_coef_idx(codec, 0xd, 0, 1<<14);
		if ((coef & 0x00f0) == 0x0020)
			alc_update_coef_idx(codec, 0x4, 1<<15, 0);
		if ((coef & 0x00f0) == 0x0030)
			alc_update_coef_idx(codec, 0x10, 1<<9, 0);
		break;
	case 0x10ec0280:
	case 0x10ec0284:
	case 0x10ec0290:
	case 0x10ec0292:
		alc_update_coef_idx(codec, 0x4, 1<<15, 0);
		break;
327
	case 0x10ec0225:
328
329
330
331
332
	case 0x10ec0295:
	case 0x10ec0299:
		alc_update_coef_idx(codec, 0x67, 0xf000, 0x3000);
		/* fallthrough */
	case 0x10ec0215:
333
	case 0x10ec0233:
334
	case 0x10ec0235:
335
	case 0x10ec0236:
336
	case 0x10ec0255:
337
	case 0x10ec0256:
338
	case 0x10ec0257:
339
340
341
342
	case 0x10ec0282:
	case 0x10ec0283:
	case 0x10ec0286:
	case 0x10ec0288:
343
	case 0x10ec0285:
344
	case 0x10ec0298:
345
	case 0x10ec0289:
346
347
		alc_update_coef_idx(codec, 0x10, 1<<9, 0);
		break;
348
349
350
	case 0x10ec0275:
		alc_update_coef_idx(codec, 0xe, 0, 1<<0);
		break;
351
352
353
	case 0x10ec0293:
		alc_update_coef_idx(codec, 0xa, 1<<13, 0);
		break;
354
355
356
	case 0x10ec0234:
	case 0x10ec0274:
	case 0x10ec0294:
357
358
359
	case 0x10ec0700:
	case 0x10ec0701:
	case 0x10ec0703:
360
361
		alc_update_coef_idx(codec, 0x10, 1<<15, 0);
		break;
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
	case 0x10ec0662:
		if ((coef & 0x00f0) == 0x0030)
			alc_update_coef_idx(codec, 0x4, 1<<10, 0); /* EAPD Ctrl */
		break;
	case 0x10ec0272:
	case 0x10ec0273:
	case 0x10ec0663:
	case 0x10ec0665:
	case 0x10ec0670:
	case 0x10ec0671:
	case 0x10ec0672:
		alc_update_coef_idx(codec, 0xd, 0, 1<<14); /* EAPD Ctrl */
		break;
	case 0x10ec0668:
		alc_update_coef_idx(codec, 0x7, 3<<13, 0);
		break;
	case 0x10ec0867:
		alc_update_coef_idx(codec, 0x4, 1<<10, 0);
		break;
	case 0x10ec0888:
		if ((coef & 0x00f0) == 0x0020 || (coef & 0x00f0) == 0x0030)
			alc_update_coef_idx(codec, 0x7, 1<<5, 0);
		break;
	case 0x10ec0892:
		alc_update_coef_idx(codec, 0x7, 1<<5, 0);
		break;
	case 0x10ec0899:
	case 0x10ec0900:
390
	case 0x10ec1168:
391
	case 0x10ec1220:
392
393
394
395
396
		alc_update_coef_idx(codec, 0x7, 1<<1, 0);
		break;
	}
}

397
398
399
/* additional initialization for ALC888 variants */
static void alc888_coef_init(struct hda_codec *codec)
{
400
401
402
403
404
405
406
407
	switch (alc_get_coef0(codec) & 0x00f0) {
	/* alc888-VA */
	case 0x00:
	/* alc888-VB */
	case 0x10:
		alc_update_coef_idx(codec, 7, 0, 0x2030); /* Turn EAPD to High */
		break;
	}
408
409
}

410
411
412
413
414
415
416
417
418
419
/* 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);
}

420
421
422
423
/* 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 */
424
	static hda_nid_t pins[] = {
425
		0x0f, 0x10, 0x14, 0x15, 0x17, 0
426
427
428
429
	};
	hda_nid_t *p;
	for (p = pins; *p; p++)
		set_eapd(codec, *p, on);
430
431
}

432
/* generic shutup callback;
Lars-Peter Clausen's avatar
Lars-Peter Clausen committed
433
 * just turning off EAPD and a little pause for avoiding pop-noise
434
435
436
 */
static void alc_eapd_shutup(struct hda_codec *codec)
{
437
438
	struct alc_spec *spec = codec->spec;

439
	alc_auto_setup_eapd(codec, false);
440
441
	if (!spec->no_depop_delay)
		msleep(200);
442
	snd_hda_shutup_pins(codec);
443
444
}

445
/* generic EAPD initialization */
446
static void alc_auto_init_amp(struct hda_codec *codec, int type)
447
{
448
	alc_fill_eapd_coef(codec);
449
	alc_auto_setup_eapd(codec, true);
450
451
	switch (type) {
	case ALC_INIT_GPIO1:
452
453
		snd_hda_sequence_write(codec, alc_gpio1_init_verbs);
		break;
454
	case ALC_INIT_GPIO2:
455
456
		snd_hda_sequence_write(codec, alc_gpio2_init_verbs);
		break;
457
	case ALC_INIT_GPIO3:
458
459
		snd_hda_sequence_write(codec, alc_gpio3_init_verbs);
		break;
460
	case ALC_INIT_DEFAULT:
461
		switch (codec->core.vendor_id) {
462
		case 0x10ec0260:
463
			alc_update_coefex_idx(codec, 0x1a, 7, 0, 0x2010);
464
465
466
467
468
			break;
		case 0x10ec0880:
		case 0x10ec0882:
		case 0x10ec0883:
		case 0x10ec0885:
469
			alc_update_coef_idx(codec, 7, 0, 0x2030);
470
			break;
471
		case 0x10ec0888:
472
			alc888_coef_init(codec);
473
			break;
474
		}
475
476
477
478
		break;
	}
}

479

480
/*
481
 * Realtek SSID verification
482
 */
483

484
485
486
487
/* Could be any non-zero and even value. When used as fixup, tells
 * the driver to ignore any present sku defines.
 */
#define ALC_FIXUP_SKU_IGNORE (2)
488

489
490
static void alc_fixup_sku_ignore(struct hda_codec *codec,
				 const struct hda_fixup *fix, int action)
491
492
{
	struct alc_spec *spec = codec->spec;
493
494
495
	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
		spec->cdefine.fixup = 1;
		spec->cdefine.sku_cfg = ALC_FIXUP_SKU_IGNORE;
496
497
498
	}
}

499
500
501
502
503
static void alc_fixup_no_depop_delay(struct hda_codec *codec,
				    const struct hda_fixup *fix, int action)
{
	struct alc_spec *spec = codec->spec;

504
	if (action == HDA_FIXUP_ACT_PROBE) {
505
		spec->no_depop_delay = 1;
506
507
		codec->depop_delay = 0;
	}
508
509
}

510
static int alc_auto_parse_customize_define(struct hda_codec *codec)
511
{
512
513
	unsigned int ass, tmp, i;
	unsigned nid = 0;
514
515
	struct alc_spec *spec = codec->spec;

516
	spec->cdefine.enable_pcbeep = 1; /* assume always enabled */
517

518
519
520
521
522
	if (spec->cdefine.fixup) {
		ass = spec->cdefine.sku_cfg;
		if (ass == ALC_FIXUP_SKU_IGNORE)
			return -1;
		goto do_sku;
523
524
	}

525
526
	if (!codec->bus->pci)
		return -1;
527
	ass = codec->core.subsystem_id & 0xffff;
528
529
	if (ass != codec->bus->pci->subsystem_device && (ass & 1))
		goto do_sku;
530

531
	nid = 0x1d;
532
	if (codec->core.vendor_id == 0x10ec0260)
533
534
		nid = 0x17;
	ass = snd_hda_codec_get_pincfg(codec, nid);
535

536
	if (!(ass & 1)) {
537
		codec_info(codec, "%s: SKU not ready 0x%08x\n",
538
			   codec->core.chip_name, ass);
539
		return -1;
540
541
	}

542
543
544
545
546
	/* check sum */
	tmp = 0;
	for (i = 1; i < 16; i++) {
		if ((ass >> i) & 1)
			tmp++;
547
	}
548
549
	if (((ass >> 16) & 0xf) != tmp)
		return -1;
550

551
552
553
554
555
556
557
558
559
560
561
	spec->cdefine.port_connectivity = ass >> 30;
	spec->cdefine.enable_pcbeep = (ass & 0x100000) >> 20;
	spec->cdefine.check_sum = (ass >> 16) & 0xf;
	spec->cdefine.customization = ass >> 8;
do_sku:
	spec->cdefine.sku_cfg = ass;
	spec->cdefine.external_amp = (ass & 0x38) >> 3;
	spec->cdefine.platform_type = (ass & 0x4) >> 2;
	spec->cdefine.swap = (ass & 0x2) >> 1;
	spec->cdefine.override = ass & 0x1;

562
	codec_dbg(codec, "SKU: Nid=0x%x sku_cfg=0x%08x\n",
563
		   nid, spec->cdefine.sku_cfg);
564
	codec_dbg(codec, "SKU: port_connectivity=0x%x\n",
565
		   spec->cdefine.port_connectivity);
566
567
568
569
570
571
572
	codec_dbg(codec, "SKU: enable_pcbeep=0x%x\n", spec->cdefine.enable_pcbeep);
	codec_dbg(codec, "SKU: check_sum=0x%08x\n", spec->cdefine.check_sum);
	codec_dbg(codec, "SKU: customization=0x%08x\n", spec->cdefine.customization);
	codec_dbg(codec, "SKU: external_amp=0x%x\n", spec->cdefine.external_amp);
	codec_dbg(codec, "SKU: platform_type=0x%x\n", spec->cdefine.platform_type);
	codec_dbg(codec, "SKU: swap=0x%x\n", spec->cdefine.swap);
	codec_dbg(codec, "SKU: override=0x%x\n", spec->cdefine.override);
573
574
575
576

	return 0;
}

577
578
579
580
581
582
583
584
585
/* return the position of NID in the list, or -1 if not found */
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;
}
586
/* return true if the given NID is found in the list */
587
588
static bool found_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
{
589
	return find_idx_in_nid_list(nid, list, nums) >= 0;
590
591
}

592
593
594
595
596
597
598
599
600
/* check subsystem ID and set up device-specific initialization;
 * return 1 if initialized, 0 if invalid SSID
 */
/* 32-bit subsystem ID for BIOS loading in HD Audio codec.
 *	31 ~ 16 :	Manufacture ID
 *	15 ~ 8	:	SKU ID
 *	7  ~ 0	:	Assembly ID
 *	port-A --> pin 39/41, port-E --> pin 14/15, port-D --> pin 35/36
 */
601
static int alc_subsystem_id(struct hda_codec *codec, const hda_nid_t *ports)
602
603
604
605
606
{
	unsigned int ass, tmp, i;
	unsigned nid;
	struct alc_spec *spec = codec->spec;

607
608
609
610
611
612
613
	if (spec->cdefine.fixup) {
		ass = spec->cdefine.sku_cfg;
		if (ass == ALC_FIXUP_SKU_IGNORE)
			return 0;
		goto do_sku;
	}

614
	ass = codec->core.subsystem_id & 0xffff;
615
616
	if (codec->bus->pci &&
	    ass != codec->bus->pci->subsystem_device && (ass & 1))
617
618
619
620
		goto do_sku;

	/* invalid SSID, check the special NID pin defcfg instead */
	/*
621
	 * 31~30	: port connectivity
622
623
624
625
626
627
628
	 * 29~21	: reserve
	 * 20		: PCBEEP input
	 * 19~16	: Check sum (15:1)
	 * 15~1		: Custom
	 * 0		: override
	*/
	nid = 0x1d;
629
	if (codec->core.vendor_id == 0x10ec0260)
630
631
		nid = 0x17;
	ass = snd_hda_codec_get_pincfg(codec, nid);
632
633
	codec_dbg(codec,
		  "realtek: No valid SSID, checking pincfg 0x%08x for NID 0x%x\n",
634
		   ass, nid);
635
	if (!(ass & 1))
636
637
638
639
640
641
642
643
644
645
646
647
648
		return 0;
	if ((ass >> 30) != 1)	/* no physical connection */
		return 0;

	/* check sum */
	tmp = 0;
	for (i = 1; i < 16; i++) {
		if ((ass >> i) & 1)
			tmp++;
	}
	if (((ass >> 16) & 0xf) != tmp)
		return 0;
do_sku:
649
	codec_dbg(codec, "realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
650
		   ass & 0xffff, codec->core.vendor_id);
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
	/*
	 * 0 : override
	 * 1 :	Swap Jack
	 * 2 : 0 --> Desktop, 1 --> Laptop
	 * 3~5 : External Amplifier control
	 * 7~6 : Reserved
	*/
	tmp = (ass & 0x38) >> 3;	/* external Amp control */
	switch (tmp) {
	case 1:
		spec->init_amp = ALC_INIT_GPIO1;
		break;
	case 3:
		spec->init_amp = ALC_INIT_GPIO2;
		break;
	case 7:
		spec->init_amp = ALC_INIT_GPIO3;
		break;
	case 5:
670
	default:
671
		spec->init_amp = ALC_INIT_DEFAULT;
672
673
		break;
	}
674

675
	/* is laptop or Desktop and enable the function "Mute internal speaker
676
677
	 * when the external headphone out jack is plugged"
	 */
678
	if (!(ass & 0x8000))
679
		return 1;
680
681
682
683
684
685
686
	/*
	 * 10~8 : Jack location
	 * 12~11: Headphone out -> 00: PortA, 01: PortE, 02: PortD, 03: Resvered
	 * 14~13: Resvered
	 * 15   : 1 --> enable the function "Mute internal speaker
	 *	        when the external headphone out jack is plugged"
	 */
687
688
689
	if (!spec->gen.autocfg.hp_pins[0] &&
	    !(spec->gen.autocfg.line_out_pins[0] &&
	      spec->gen.autocfg.line_out_type == AUTO_PIN_HP_OUT)) {
690
		hda_nid_t nid;
691
		tmp = (ass >> 11) & 0x3;	/* HP to chassis */
692
		nid = ports[tmp];
693
694
		if (found_in_nid_list(nid, spec->gen.autocfg.line_out_pins,
				      spec->gen.autocfg.line_outs))
695
			return 1;
696
		spec->gen.autocfg.hp_pins[0] = nid;
697
	}
698
699
	return 1;
}
700

701
702
703
/* Check the validity of ALC subsystem-id
 * ports contains an array of 4 pin NIDs for port-A, E, D and I */
static void alc_ssid_check(struct hda_codec *codec, const hda_nid_t *ports)
704
{
705
	if (!alc_subsystem_id(codec, ports)) {
706
		struct alc_spec *spec = codec->spec;
707
708
		codec_dbg(codec,
			  "realtek: Enable default setup for auto mode as fallback\n");
709
710
		spec->init_amp = ALC_INIT_DEFAULT;
	}
711
}
712

713
/*
714
 */
715

716
717
static void alc_fixup_inv_dmic(struct hda_codec *codec,
			       const struct hda_fixup *fix, int action)
718
719
{
	struct alc_spec *spec = codec->spec;
720

721
	spec->gen.inv_dmic_split = 1;
722
723
}

724

725
726
727
728
729
#ifdef CONFIG_SND_HDA_INPUT_BEEP
/* additional beep mixers; the actual parameters are overwritten at build */
static const struct snd_kcontrol_new alc_beep_mixer[] = {
	HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_INPUT),
	HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_INPUT),
730
731
	{ } /* end */
};
732
#endif
733

734
static int alc_build_controls(struct hda_codec *codec)
735
736
{
	struct alc_spec *spec = codec->spec;
737
	int i, err;
738

739
740
741
	err = snd_hda_gen_build_controls(codec);
	if (err < 0)
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
742
743
744
745
746
747

	for (i = 0; i < spec->num_mixers; i++) {
		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
		if (err < 0)
			return err;
	}
748

749
#ifdef CONFIG_SND_HDA_INPUT_BEEP
750
751
	/* create beep controls if needed */
	if (spec->beep_amp) {
752
		const struct snd_kcontrol_new *knew;
753
754
755
756
		for (knew = alc_beep_mixer; knew->name; knew++) {
			struct snd_kcontrol *kctl;
			kctl = snd_ctl_new1(knew, codec);
			if (!kctl)
757
758
759
760
761
				return -ENOMEM;
			kctl->private_value = spec->beep_amp;
			err = snd_hda_ctl_add(codec, 0, kctl);
			if (err < 0)
				return err;
762
		}
763
	}
764
#endif
765

766
	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_BUILD);
767
	return 0;
768
769
770
}


771
/*
772
 * Common callbacks
773
 */
774

775
static int alc_init(struct hda_codec *codec)
776
777
{
	struct alc_spec *spec = codec->spec;
778

779
780
	if (spec->init_hook)
		spec->init_hook(codec);
781

782
783
	alc_fix_pll(codec);
	alc_auto_init_amp(codec, spec->init_amp);
784

785
	snd_hda_gen_init(codec);
786

787
	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT);
788

789
790
	return 0;
}
791

792
static inline void alc_shutup(struct hda_codec *codec)
793
794
{
	struct alc_spec *spec = codec->spec;
795

796
797
798
	if (!snd_hda_get_bool_hint(codec, "shutup"))
		return; /* disabled explicitly by hints */

799
800
	if (spec && spec->shutup)
		spec->shutup(codec);
801
802
	else
		snd_hda_shutup_pins(codec);
803
804
}

805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
static void alc_reboot_notify(struct hda_codec *codec)
{
	struct alc_spec *spec = codec->spec;

	if (spec && spec->reboot_notify)
		spec->reboot_notify(codec);
	else
		alc_shutup(codec);
}

/* power down codec to D3 at reboot/shutdown; set as reboot_notify ops */
static void alc_d3_at_reboot(struct hda_codec *codec)
{
	snd_hda_codec_set_power_to_all(codec, codec->core.afg, AC_PWRST_D3);
	snd_hda_codec_write(codec, codec->core.afg, 0,
			    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
	msleep(10);
}

824
#define alc_free	snd_hda_gen_free
825

826
827
#ifdef CONFIG_PM
static void alc_power_eapd(struct hda_codec *codec)
828
{
829
	alc_auto_setup_eapd(codec, false);
830
}
831

832
static int alc_suspend(struct hda_codec *codec)
833
834
{
	struct alc_spec *spec = codec->spec;
835
836
837
	alc_shutup(codec);
	if (spec && spec->power_hook)
		spec->power_hook(codec);
838
839
	return 0;
}
840
#endif
841

842
843
#ifdef CONFIG_PM
static int alc_resume(struct hda_codec *codec)
844
{
845
846
847
848
	struct alc_spec *spec = codec->spec;

	if (!spec->no_depop_delay)
		msleep(150); /* to avoid pop noise */
849
	codec->patch_ops.init(codec);
850
	regcache_sync(codec->core.regmap);
851
852
	hda_call_check_power_status(codec, 0x01);
	return 0;
853
}
854
#endif
855

856
857
/*
 */
858
859
860
861
862
863
864
865
866
static const struct hda_codec_ops alc_patch_ops = {
	.build_controls = alc_build_controls,
	.build_pcms = snd_hda_gen_build_pcms,
	.init = alc_init,
	.free = alc_free,
	.unsol_event = snd_hda_jack_unsol_event,
#ifdef CONFIG_PM
	.resume = alc_resume,
	.suspend = alc_suspend,
867
	.check_power_status = snd_hda_gen_check_power_status,
868
#endif
869
	.reboot_notify = alc_reboot_notify,
870
};
871

872

873
#define alc_codec_rename(codec, name) snd_hda_codec_set_name(codec, name)
874

875
/*
876
 * Rename codecs appropriately from COEF value or subvendor id
877
 */
878
879
880
881
882
883
struct alc_codec_rename_table {
	unsigned int vendor_id;
	unsigned short coef_mask;
	unsigned short coef_bits;
	const char *name;
};
884

885
886
887
888
889
890
891
struct alc_codec_rename_pci_table {
	unsigned int codec_vendor_id;
	unsigned short pci_subvendor;
	unsigned short pci_subdevice;
	const char *name;
};

892
static struct alc_codec_rename_table rename_tbl[] = {
893
	{ 0x10ec0221, 0xf00f, 0x1003, "ALC231" },
894
895
896
897
898
899
900
901
	{ 0x10ec0269, 0xfff0, 0x3010, "ALC277" },
	{ 0x10ec0269, 0xf0f0, 0x2010, "ALC259" },
	{ 0x10ec0269, 0xf0f0, 0x3010, "ALC258" },
	{ 0x10ec0269, 0x00f0, 0x0010, "ALC269VB" },
	{ 0x10ec0269, 0xffff, 0xa023, "ALC259" },
	{ 0x10ec0269, 0xffff, 0x6023, "ALC281X" },
	{ 0x10ec0269, 0x00f0, 0x0020, "ALC269VC" },
	{ 0x10ec0269, 0x00f0, 0x0030, "ALC269VD" },
902
	{ 0x10ec0662, 0xffff, 0x4020, "ALC656" },
903
904
905
906
907
908
909
910
911
	{ 0x10ec0887, 0x00f0, 0x0030, "ALC887-VD" },
	{ 0x10ec0888, 0x00f0, 0x0030, "ALC888-VD" },
	{ 0x10ec0888, 0xf0f0, 0x3020, "ALC886" },
	{ 0x10ec0899, 0x2000, 0x2000, "ALC899" },
	{ 0x10ec0892, 0xffff, 0x8020, "ALC661" },
	{ 0x10ec0892, 0xffff, 0x8011, "ALC661" },
	{ 0x10ec0892, 0xffff, 0x4011, "ALC656" },
	{ } /* terminator */
};
912

913
914
915
916
static struct alc_codec_rename_pci_table rename_pci_tbl[] = {
	{ 0x10ec0280, 0x1028, 0, "ALC3220" },
	{ 0x10ec0282, 0x1028, 0, "ALC3221" },
	{ 0x10ec0283, 0x1028, 0, "ALC3223" },
917
	{ 0x10ec0288, 0x1028, 0, "ALC3263" },
918
	{ 0x10ec0292, 0x1028, 0, "ALC3226" },
919
	{ 0x10ec0293, 0x1028, 0, "ALC3235" },
920
921
	{ 0x10ec0255, 0x1028, 0, "ALC3234" },
	{ 0x10ec0668, 0x1028, 0, "ALC3661" },
922
923
	{ 0x10ec0275, 0x1028, 0, "ALC3260" },
	{ 0x10ec0899, 0x1028, 0, "ALC3861" },
924
	{ 0x10ec0298, 0x1028, 0, "ALC3266" },
925
	{ 0x10ec0236, 0x1028, 0, "ALC3204" },
926
	{ 0x10ec0256, 0x1028, 0, "ALC3246" },
927
	{ 0x10ec0225, 0x1028, 0, "ALC3253" },
928
	{ 0x10ec0295, 0x1028, 0, "ALC3254" },
929
	{ 0x10ec0299, 0x1028, 0, "ALC3271" },
930
931
932
933
934
935
936
937
938
939
940
	{ 0x10ec0670, 0x1025, 0, "ALC669X" },
	{ 0x10ec0676, 0x1025, 0, "ALC679X" },
	{ 0x10ec0282, 0x1043, 0, "ALC3229" },
	{ 0x10ec0233, 0x1043, 0, "ALC3236" },
	{ 0x10ec0280, 0x103c, 0, "ALC3228" },
	{ 0x10ec0282, 0x103c, 0, "ALC3227" },
	{ 0x10ec0286, 0x103c, 0, "ALC3242" },
	{ 0x10ec0290, 0x103c, 0, "ALC3241" },
	{ 0x10ec0668, 0x103c, 0, "ALC3662" },
	{ 0x10ec0283, 0x17aa, 0, "ALC3239" },
	{ 0x10ec0292, 0x17aa, 0, "ALC3232" },
941
942
943
	{ } /* terminator */
};

944
static int alc_codec_rename_from_preset(struct hda_codec *codec)
945
{
946
	const struct alc_codec_rename_table *p;
947
	const struct alc_codec_rename_pci_table *q;
948

949
	for (p = rename_tbl; p->vendor_id; p++) {
950
		if (p->vendor_id != codec->core.vendor_id)
951
952
953
			continue;
		if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
			return alc_codec_rename(codec, p->name);
954
	}
955

956
957
	if (!codec->bus->pci)
		return 0;
958
	for (q = rename_pci_tbl; q->codec_vendor_id; q++) {
959
		if (q->codec_vendor_id != codec->core.vendor_id)
960
961
962
963
964
965
966
967
			continue;
		if (q->pci_subvendor != codec->bus->pci->subsystem_vendor)
			continue;
		if (!q->pci_subdevice ||
		    q->pci_subdevice == codec->bus->pci->subsystem_device)
			return alc_codec_rename(codec, q->name);
	}

968
	return 0;
969
}
970

971

972
973
974
975
976
977
/*
 * Digital-beep handlers
 */
#ifdef CONFIG_SND_HDA_INPUT_BEEP
#define set_beep_amp(spec, nid, idx, dir) \
	((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir))
978

979
static const struct snd_pci_quirk beep_white_list[] = {
980
	SND_PCI_QUIRK(0x1043, 0x103c, "ASUS", 1),
981
	SND_PCI_QUIRK(0x1043, 0x115d, "ASUS", 1),
982
	SND_PCI_QUIRK(0x1043, 0x829f, "ASUS", 1),
983
	SND_PCI_QUIRK(0x1043, 0x8376, "EeePC", 1),
984
985
986
	SND_PCI_QUIRK(0x1043, 0x83ce, "EeePC", 1),
	SND_PCI_QUIRK(0x1043, 0x831a, "EeePC", 1),
	SND_PCI_QUIRK(0x1043, 0x834a, "EeePC", 1),
987
	SND_PCI_QUIRK(0x1458, 0xa002, "GA-MA790X", 1),
988
989
	SND_PCI_QUIRK(0x8086, 0xd613, "Intel", 1),
	{}
990
991
};

992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
static inline int has_cdefine_beep(struct hda_codec *codec)
{
	struct alc_spec *spec = codec->spec;
	const struct snd_pci_quirk *q;
	q = snd_pci_quirk_lookup(codec->bus->pci, beep_white_list);
	if (q)
		return q->value;
	return spec->cdefine.enable_pcbeep;
}
#else
#define set_beep_amp(spec, nid, idx, dir) /* NOP */
#define has_cdefine_beep(codec)		0
#endif
1005

1006
1007
1008
1009
/* parse the BIOS configuration and set up the alc_spec */
/* return 1 if successful, 0 if the proper config is not found,
 * or a negative error code
 */
1010
1011
1012
static int alc_parse_auto_config(struct hda_codec *codec,
				 const hda_nid_t *ignore_nids,
				 const hda_nid_t *ssid_nids)
1013
1014
{
	struct alc_spec *spec = codec->spec;
1015
	struct auto_pin_cfg *cfg = &spec->gen.autocfg;
1016
	int err;
1017

1018
1019
	err = snd_hda_parse_pin_defcfg(codec, cfg, ignore_nids,
				       spec->parse_flags);
1020
1021
	if (err < 0)
		return err;
1022
1023
1024

	if (ssid_nids)
		alc_ssid_check(codec, ssid_nids);
1025

1026
1027
1028
	err = snd_hda_gen_parse_auto_config(codec, cfg);
	if (err < 0)
		return err;
1029

</