patch_realtek.c 247 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
	case 0x10ec0300:
347
348
		alc_update_coef_idx(codec, 0x10, 1<<9, 0);
		break;
349
350
351
	case 0x10ec0275:
		alc_update_coef_idx(codec, 0xe, 0, 1<<0);
		break;
352
353
354
	case 0x10ec0293:
		alc_update_coef_idx(codec, 0xa, 1<<13, 0);
		break;
355
356
357
	case 0x10ec0234:
	case 0x10ec0274:
	case 0x10ec0294:
358
359
360
	case 0x10ec0700:
	case 0x10ec0701:
	case 0x10ec0703:
361
362
		alc_update_coef_idx(codec, 0x10, 1<<15, 0);
		break;
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
390
	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:
391
	case 0x10ec1168:
392
	case 0x10ec1220:
393
394
395
396
397
		alc_update_coef_idx(codec, 0x7, 1<<1, 0);
		break;
	}
}

398
399
400
/* additional initialization for ALC888 variants */
static void alc888_coef_init(struct hda_codec *codec)
{
401
402
403
404
405
406
407
408
	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;
	}
409
410
}

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

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

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

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

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

480

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

485
486
487
488
/* 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)
489

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

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

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

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

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

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

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

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

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

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

552
553
554
555
556
557
558
559
560
561
562
	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;

563
	codec_dbg(codec, "SKU: Nid=0x%x sku_cfg=0x%08x\n",
564
		   nid, spec->cdefine.sku_cfg);
565
	codec_dbg(codec, "SKU: port_connectivity=0x%x\n",
566
		   spec->cdefine.port_connectivity);
567
568
569
570
571
572
573
	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);
574
575
576
577

	return 0;
}

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

593
594
595
596
597
598
599
600
601
/* 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
 */
602
static int alc_subsystem_id(struct hda_codec *codec, const hda_nid_t *ports)
603
604
605
606
607
{
	unsigned int ass, tmp, i;
	unsigned nid;
	struct alc_spec *spec = codec->spec;

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

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

	/* invalid SSID, check the special NID pin defcfg instead */
	/*
622
	 * 31~30	: port connectivity
623
624
625
626
627
628
629
	 * 29~21	: reserve
	 * 20		: PCBEEP input
	 * 19~16	: Check sum (15:1)
	 * 15~1		: Custom
	 * 0		: override
	*/
	nid = 0x1d;
630
	if (codec->core.vendor_id == 0x10ec0260)
631
632
		nid = 0x17;
	ass = snd_hda_codec_get_pincfg(codec, nid);
633
634
	codec_dbg(codec,
		  "realtek: No valid SSID, checking pincfg 0x%08x for NID 0x%x\n",
635
		   ass, nid);
636
	if (!(ass & 1))
637
638
639
640
641
642
643
644
645
646
647
648
649
		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:
650
	codec_dbg(codec, "realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
651
		   ass & 0xffff, codec->core.vendor_id);
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
	/*
	 * 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:
671
	default:
672
		spec->init_amp = ALC_INIT_DEFAULT;
673
674
		break;
	}
675

676
	/* is laptop or Desktop and enable the function "Mute internal speaker
677
678
	 * when the external headphone out jack is plugged"
	 */
679
	if (!(ass & 0x8000))
680
		return 1;
681
682
683
684
685
686
687
	/*
	 * 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"
	 */
688
689
690
	if (!spec->gen.autocfg.hp_pins[0] &&
	    !(spec->gen.autocfg.line_out_pins[0] &&
	      spec->gen.autocfg.line_out_type == AUTO_PIN_HP_OUT)) {
691
		hda_nid_t nid;
692
		tmp = (ass >> 11) & 0x3;	/* HP to chassis */
693
		nid = ports[tmp];
694
695
		if (found_in_nid_list(nid, spec->gen.autocfg.line_out_pins,
				      spec->gen.autocfg.line_outs))
696
			return 1;
697
		spec->gen.autocfg.hp_pins[0] = nid;
698
	}
699
700
	return 1;
}
701

702
703
704
/* 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)
705
{
706
	if (!alc_subsystem_id(codec, ports)) {
707
		struct alc_spec *spec = codec->spec;
708
709
		codec_dbg(codec,
			  "realtek: Enable default setup for auto mode as fallback\n");
710
711
		spec->init_amp = ALC_INIT_DEFAULT;
	}
712
}
713

714
/*
715
 */
716

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

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

725

726
727
728
729
730
#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),
731
732
	{ } /* end */
};
733
#endif
734

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

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

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

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

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


772
/*
773
 * Common callbacks
774
 */
775

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

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

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

786
	snd_hda_gen_init(codec);
787

788
	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT);
789

790
791
	return 0;
}
792

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

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

803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
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);
}

822
#define alc_free	snd_hda_gen_free
823

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

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

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

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

854
855
/*
 */
856
857
858
859
860
861
862
863
864
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,
865
	.check_power_status = snd_hda_gen_check_power_status,
866
#endif
867
	.reboot_notify = alc_reboot_notify,
868
};
869

870

871
#define alc_codec_rename(codec, name) snd_hda_codec_set_name(codec, name)
872

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

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

890
static struct alc_codec_rename_table rename_tbl[] = {
891
	{ 0x10ec0221, 0xf00f, 0x1003, "ALC231" },
892
893
894
895
896
897
898
899
	{ 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" },
900
	{ 0x10ec0662, 0xffff, 0x4020, "ALC656" },
901
902
903
904
905
906
907
908
909
	{ 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 */
};
910

911
912
913
914
static struct alc_codec_rename_pci_table rename_pci_tbl[] = {
	{ 0x10ec0280, 0x1028, 0, "ALC3220" },
	{ 0x10ec0282, 0x1028, 0, "ALC3221" },
	{ 0x10ec0283, 0x1028, 0, "ALC3223" },
915
	{ 0x10ec0288, 0x1028, 0, "ALC3263" },
916
	{ 0x10ec0292, 0x1028, 0, "ALC3226" },
917
	{ 0x10ec0293, 0x1028, 0, "ALC3235" },
918
919
	{ 0x10ec0255, 0x1028, 0, "ALC3234" },
	{ 0x10ec0668, 0x1028, 0, "ALC3661" },
920
921
	{ 0x10ec0275, 0x1028, 0, "ALC3260" },
	{ 0x10ec0899, 0x1028, 0, "ALC3861" },
922
	{ 0x10ec0298, 0x1028, 0, "ALC3266" },
923
	{ 0x10ec0236, 0x1028, 0, "ALC3204" },
924
	{ 0x10ec0256, 0x1028, 0, "ALC3246" },
925
	{ 0x10ec0225, 0x1028, 0, "ALC3253" },
926
	{ 0x10ec0295, 0x1028, 0, "ALC3254" },
927
	{ 0x10ec0299, 0x1028, 0, "ALC3271" },
928
929
930
931
932
933
934
935
936
937
938
	{ 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" },
939
940
941
	{ } /* terminator */
};

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

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

954
955
	if (!codec->bus->pci)
		return 0;
956
	for (q = rename_pci_tbl; q->codec_vendor_id; q++) {
957
		if (q->codec_vendor_id != codec->core.vendor_id)
958
959
960
961
962
963
964
965
			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);
	}

966
	return 0;
967
}
968

969

970
971
972
973
974
975
/*
 * 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))
976

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

990
991
992
993
994
995
996
997
998
999
1000
1001
1002
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
1003

1004
1005
1006
1007
/* 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
 */
1008
1009
1010
static int alc_parse_auto_config(struct hda_codec *codec,
				 const hda_nid_t *ignore_nids,
				 const hda_nid_t *ssid_nids)
1011
1012
{
	struct alc_spec *spec = codec->spec;
1013
	struct auto_pin_cfg *cfg = &spec->gen.autocfg;
1014
	int err;
1015

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

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

1024
1025
1026
	err = snd_hda_gen_parse_auto_config(codec, cfg);
	if (err < 0)
		return err;
1027

1028
	return 1;
1029
}
1030

1031
1032
1033
1034
1035
1036
1037
1038
1039
/* common preparation job for alc_spec */
static int alc_alloc_spec(struct hda_codec *codec, hda_nid_t mixer_nid)
{
	struct alc_spec *spec = kzalloc(sizeof(*spec), GFP_KERNEL);
	int err;

	if (!spec)
		return -ENOMEM;
	codec->spec = spec;
1040
1041
1042
	snd_hda_gen_spec_init(&spec->gen);
	spec->gen.mixer_nid = mixer_nid;
	spec->gen.own_eapd_ctl = 1;
1043
	codec->single_adc_amp = 1;
1044
1045
	/* FIXME: do we need this for all Realtek codec models? */
	codec->spdif_status_reset = 1;
1046
	codec->patch_ops = alc_patch_ops;
1047
1048
1049
1050
1051
1052
1053
1054
1055

	err = alc_codec_rename_from_preset(codec);
	if (err < 0) {
		kfree(spec);
		return err;
	}
	return 0;
}

1056
1057
1058
static int alc880_parse_auto_config(struct hda_codec *codec)
{
	static const hda_nid_t alc880_ignore[] = { 0x1d, 0 };
1059
	static const hda_nid_t alc880_ssids[] = { 0x15, 0x1b, 0x14, 0 };
1060
1061
1062
	return alc_parse_auto_config(codec, alc880_ignore, alc880_ssids);
}

1063
1064
1065
1066
/*
 * ALC880 fix-ups
 */
enum {
1067
	ALC880_FIXUP_GPIO1,
1068
1069
	ALC880_FIXUP_GPIO2,
	ALC880_FIXUP_MEDION_RIM,
1070
	ALC880_FIXUP_LG,
1071
	ALC880_FIXUP_LG_LW25,
1072
	ALC880_FIXUP_W810,
1073
	ALC880_FIXUP_EAPD_COEF,
1074
	ALC880_FIXUP_TCL_S700,
1075
1076
	ALC880_FIXUP_VOL_KNOB,
	ALC880_FIXUP_FUJITSU,
1077
	ALC880_FIXUP_F1734,
1078
	ALC880_FIXUP_UNIWILL,
1079
	ALC880_FIXUP_UNIWILL_DIG,
1080
	ALC880_FIXUP_Z71V,
1081