patch_realtek.c 337 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds's avatar
Linus Torvalds committed
2
3
4
/*
 * Universal Interface for Intel High Definition Audio Codec
 *
5
 * HD audio interface patch for Realtek ALC codecs
Linus Torvalds's avatar
Linus Torvalds committed
6
 *
7
8
 * Copyright (c) 2004 Kailang Yang <kailang@realtek.com.tw>
 *                    PeiSen Hou <pshou@realtek.com.tw>
Linus Torvalds's avatar
Linus Torvalds committed
9
 *                    Takashi Iwai <tiwai@suse.de>
Jonathan Woithe's avatar
Jonathan Woithe committed
10
 *                    Jonathan Woithe <jwoithe@just42.net>
Linus Torvalds's avatar
Linus Torvalds committed
11
12
13
14
15
16
 */

#include <linux/init.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/pci.h>
17
#include <linux/dmi.h>
18
#include <linux/module.h>
19
#include <linux/input.h>
20
#include <linux/leds.h>
Linus Torvalds's avatar
Linus Torvalds committed
21
#include <sound/core.h>
22
#include <sound/jack.h>
23
#include <sound/hda_codec.h>
Linus Torvalds's avatar
Linus Torvalds committed
24
#include "hda_local.h"
25
#include "hda_auto_parser.h"
26
#include "hda_jack.h"
27
#include "hda_generic.h"
Linus Torvalds's avatar
Linus Torvalds committed
28

29
30
31
/* keep halting ALC5505 DSP, for power saving */
#define HALT_REALTEK_ALC5505

32
33
/* extra amp-initialization sequence types */
enum {
34
	ALC_INIT_UNDEFINED,
35
36
37
38
	ALC_INIT_NONE,
	ALC_INIT_DEFAULT,
};

39
40
41
42
43
44
45
46
47
48
49
50
51
52
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,
};

53
54
55
56
enum {
	ALC_KEY_MICMUTE_INDEX,
};

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
76
struct alc_coef_led {
	unsigned int idx;
	unsigned int mask;
	unsigned int on;
	unsigned int off;
};

Linus Torvalds's avatar
Linus Torvalds committed
77
struct alc_spec {
78
	struct hda_gen_spec gen; /* must be at head */
79

Linus Torvalds's avatar
Linus Torvalds committed
80
	/* codec parameterization */
81
	struct alc_customize_define cdefine;
82
	unsigned int parse_flags; /* flag for snd_hda_parse_pin_defcfg() */
83

84
85
86
87
	/* GPIO bits */
	unsigned int gpio_mask;
	unsigned int gpio_dir;
	unsigned int gpio_data;
88
	bool gpio_write_delay;	/* add a delay before writing gpio_data */
89

90
	/* mute LED for HP laptops, see vref_mute_led_set() */
91
	int mute_led_polarity;
92
	int micmute_led_polarity;
93
	hda_nid_t mute_led_nid;
94
	hda_nid_t cap_mute_led_nid;
95

96
97
	unsigned int gpio_mute_led_mask;
	unsigned int gpio_mic_led_mask;
98
99
	struct alc_coef_led mute_led_coef;
	struct alc_coef_led mic_led_coef;
100

101
102
103
104
105
	hda_nid_t headset_mic_pin;
	hda_nid_t headphone_mic_pin;
	int current_headset_mode;
	int current_headset_type;

106
107
	/* hooks */
	void (*init_hook)(struct hda_codec *codec);
108
#ifdef CONFIG_PM
109
	void (*power_hook)(struct hda_codec *codec);
110
#endif
111
	void (*shutup)(struct hda_codec *codec);
112
	void (*reboot_notify)(struct hda_codec *codec);
113

114
	int init_amp;
115
	int codec_variant;	/* flag for other variants */
116
117
	unsigned int has_alc5505_dsp:1;
	unsigned int no_depop_delay:1;
118
	unsigned int done_hp_init:1;
119
	unsigned int no_shutup_pins:1;
120
	unsigned int ultra_low_power:1;
121
	unsigned int has_hs_key:1;
122
	unsigned int no_internal_mic_pin:1;
123

124
125
126
	/* for PLL fix */
	hda_nid_t pll_nid;
	unsigned int pll_coef_idx, pll_coef_bit;
127
	unsigned int coef0;
128
	struct input_dev *kb_dev;
129
	u8 alc_mute_keycode_map[1];
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
157
158
/*
 * 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)

159
160
161
162
163
164
165
166
167
168
169
170
171
172
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)

173
174
175
176
177
178
179
180
181
182
/* 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;
}

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* 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);
	}
}

209
/*
210
 * GPIO setup tables, used in initialization
211
 */
212

213
/* Enable GPIO mask and set output */
214
215
216
static void alc_setup_gpio(struct hda_codec *codec, unsigned int mask)
{
	struct alc_spec *spec = codec->spec;
217

218
219
220
221
	spec->gpio_mask |= mask;
	spec->gpio_dir |= mask;
	spec->gpio_data |= mask;
}
222

223
224
225
226
227
228
229
230
static void alc_write_gpio_data(struct hda_codec *codec)
{
	struct alc_spec *spec = codec->spec;

	snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
			    spec->gpio_data);
}

231
232
233
234
235
236
237
238
239
240
241
242
243
244
static void alc_update_gpio_data(struct hda_codec *codec, unsigned int mask,
				 bool on)
{
	struct alc_spec *spec = codec->spec;
	unsigned int oldval = spec->gpio_data;

	if (on)
		spec->gpio_data |= mask;
	else
		spec->gpio_data &= ~mask;
	if (oldval != spec->gpio_data)
		alc_write_gpio_data(codec);
}

245
246
247
248
249
250
251
252
253
254
255
static void alc_write_gpio(struct hda_codec *codec)
{
	struct alc_spec *spec = codec->spec;

	if (!spec->gpio_mask)
		return;

	snd_hda_codec_write(codec, codec->core.afg, 0,
			    AC_VERB_SET_GPIO_MASK, spec->gpio_mask);
	snd_hda_codec_write(codec, codec->core.afg, 0,
			    AC_VERB_SET_GPIO_DIRECTION, spec->gpio_dir);
256
257
	if (spec->gpio_write_delay)
		msleep(1);
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
	alc_write_gpio_data(codec);
}

static void alc_fixup_gpio(struct hda_codec *codec, int action,
			   unsigned int mask)
{
	if (action == HDA_FIXUP_ACT_PRE_PROBE)
		alc_setup_gpio(codec, mask);
}

static void alc_fixup_gpio1(struct hda_codec *codec,
			    const struct hda_fixup *fix, int action)
{
	alc_fixup_gpio(codec, action, 0x01);
}

static void alc_fixup_gpio2(struct hda_codec *codec,
			    const struct hda_fixup *fix, int action)
{
	alc_fixup_gpio(codec, action, 0x02);
}

static void alc_fixup_gpio3(struct hda_codec *codec,
			    const struct hda_fixup *fix, int action)
{
	alc_fixup_gpio(codec, action, 0x03);
}
285

286
287
288
289
290
291
static void alc_fixup_gpio4(struct hda_codec *codec,
			    const struct hda_fixup *fix, int action)
{
	alc_fixup_gpio(codec, action, 0x04);
}

292
293
294
static void alc_fixup_micmute_led(struct hda_codec *codec,
				  const struct hda_fixup *fix, int action)
{
295
	if (action == HDA_FIXUP_ACT_PRE_PROBE)
296
297
298
		snd_hda_gen_add_micmute_led_cdev(codec, NULL);
}

299
300
301
302
303
304
305
306
307
/*
 * 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;

308
309
310
	if (spec->pll_nid)
		alc_update_coefex_idx(codec, spec->pll_nid, spec->pll_coef_idx,
				      1 << spec->pll_coef_bit, 0);
311
312
313
314
315
316
317
318
319
320
321
322
}

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

323
/* update the master volume per volume-knob's unsol event */
324
325
static void alc_update_knob_master(struct hda_codec *codec,
				   struct hda_jack_callback *jack)
326
327
328
329
330
331
332
333
334
335
336
{
	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;
337
	val = snd_hda_codec_read(codec, jack->nid, 0,
338
339
340
341
342
343
344
345
				 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);
}

346
static void alc880_unsol_event(struct hda_codec *codec, unsigned int res)
347
{
348
349
350
	/* For some reason, the res given from ALC880 is broken.
	   Here we adjust it properly. */
	snd_hda_jack_unsol_event(codec, res >> 2);
351
352
}

353
354
355
356
357
358
359
/* Change EAPD to verb control */
static void alc_fill_eapd_coef(struct hda_codec *codec)
{
	int coef;

	coef = alc_get_coef0(codec);

360
	switch (codec->core.vendor_id) {
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
	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;
382
	case 0x10ec0225:
383
384
385
	case 0x10ec0295:
	case 0x10ec0299:
		alc_update_coef_idx(codec, 0x67, 0xf000, 0x3000);
386
		fallthrough;
387
	case 0x10ec0215:
388
	case 0x10ec0230:
389
	case 0x10ec0233:
390
	case 0x10ec0235:
391
	case 0x10ec0236:
392
	case 0x10ec0245:
393
	case 0x10ec0255:
394
	case 0x10ec0256:
395
	case 0x10ec0257:
396
397
398
399
	case 0x10ec0282:
	case 0x10ec0283:
	case 0x10ec0286:
	case 0x10ec0288:
400
	case 0x10ec0285:
401
	case 0x10ec0298:
402
	case 0x10ec0289:
403
	case 0x10ec0300:
404
405
		alc_update_coef_idx(codec, 0x10, 1<<9, 0);
		break;
406
407
408
	case 0x10ec0275:
		alc_update_coef_idx(codec, 0xe, 0, 1<<0);
		break;
409
410
411
412
	case 0x10ec0287:
		alc_update_coef_idx(codec, 0x10, 1<<9, 0);
		alc_write_coef_idx(codec, 0x8, 0x4ab7);
		break;
413
414
415
	case 0x10ec0293:
		alc_update_coef_idx(codec, 0xa, 1<<13, 0);
		break;
416
417
418
	case 0x10ec0234:
	case 0x10ec0274:
	case 0x10ec0294:
419
420
421
	case 0x10ec0700:
	case 0x10ec0701:
	case 0x10ec0703:
422
	case 0x10ec0711:
423
424
		alc_update_coef_idx(codec, 0x10, 1<<15, 0);
		break;
425
426
427
428
429
430
431
432
433
434
435
436
437
	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;
438
	case 0x10ec0222:
439
440
441
	case 0x10ec0623:
		alc_update_coef_idx(codec, 0x19, 1<<13, 0);
		break;
442
443
444
445
446
447
448
449
450
451
452
	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:
453
	case 0x10ec0897:
454
455
456
457
		alc_update_coef_idx(codec, 0x7, 1<<5, 0);
		break;
	case 0x10ec0899:
	case 0x10ec0900:
458
	case 0x10ec0b00:
459
	case 0x10ec1168:
460
	case 0x10ec1220:
461
462
463
464
465
		alc_update_coef_idx(codec, 0x7, 1<<1, 0);
		break;
	}
}

466
467
468
/* additional initialization for ALC888 variants */
static void alc888_coef_init(struct hda_codec *codec)
{
469
470
471
472
473
474
475
476
	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;
	}
477
478
}

479
480
481
482
483
484
485
486
487
488
/* 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);
}

489
490
491
492
/* 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 */
493
	static const hda_nid_t pins[] = {
494
		0x0f, 0x10, 0x14, 0x15, 0x17, 0
495
	};
496
	const hda_nid_t *p;
497
498
	for (p = pins; *p; p++)
		set_eapd(codec, *p, on);
499
500
}

501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
static int find_ext_mic_pin(struct hda_codec *codec);

static void alc_headset_mic_no_shutup(struct hda_codec *codec)
{
	const struct hda_pincfg *pin;
	int mic_pin = find_ext_mic_pin(codec);
	int i;

	/* don't shut up pins when unloading the driver; otherwise it breaks
	 * the default pin setup at the next load of the driver
	 */
	if (codec->bus->shutdown)
		return;

	snd_array_for_each(&codec->init_pins, i, pin) {
		/* use read here for syncing after issuing each verb */
		if (pin->nid != mic_pin)
			snd_hda_codec_read(codec, pin->nid, 0,
					AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
	}

	codec->pins_shutup = 1;
}

525
526
527
528
static void alc_shutup_pins(struct hda_codec *codec)
{
	struct alc_spec *spec = codec->spec;

529
	switch (codec->core.vendor_id) {
530
	case 0x10ec0283:
531
532
533
534
535
536
537
538
539
540
	case 0x10ec0286:
	case 0x10ec0288:
	case 0x10ec0298:
		alc_headset_mic_no_shutup(codec);
		break;
	default:
		if (!spec->no_shutup_pins)
			snd_hda_shutup_pins(codec);
		break;
	}
541
542
}

543
/* generic shutup callback;
Lars-Peter Clausen's avatar
Lars-Peter Clausen committed
544
 * just turning off EAPD and a little pause for avoiding pop-noise
545
546
547
 */
static void alc_eapd_shutup(struct hda_codec *codec)
{
548
549
	struct alc_spec *spec = codec->spec;

550
	alc_auto_setup_eapd(codec, false);
551
552
	if (!spec->no_depop_delay)
		msleep(200);
553
	alc_shutup_pins(codec);
554
555
}

556
/* generic EAPD initialization */
557
static void alc_auto_init_amp(struct hda_codec *codec, int type)
558
{
559
	alc_auto_setup_eapd(codec, true);
560
	alc_write_gpio(codec);
561
562
	switch (type) {
	case ALC_INIT_DEFAULT:
563
		switch (codec->core.vendor_id) {
564
		case 0x10ec0260:
565
			alc_update_coefex_idx(codec, 0x1a, 7, 0, 0x2010);
566
567
568
569
570
			break;
		case 0x10ec0880:
		case 0x10ec0882:
		case 0x10ec0883:
		case 0x10ec0885:
571
			alc_update_coef_idx(codec, 7, 0, 0x2030);
572
			break;
573
		case 0x10ec0888:
574
			alc888_coef_init(codec);
575
			break;
576
		}
577
578
579
580
		break;
	}
}

581
582
583
584
585
586
587
588
589
/* get a primary headphone pin if available */
static hda_nid_t alc_get_hp_pin(struct alc_spec *spec)
{
	if (spec->gen.autocfg.hp_pins[0])
		return spec->gen.autocfg.hp_pins[0];
	if (spec->gen.autocfg.line_out_type == AC_JACK_HP_OUT)
		return spec->gen.autocfg.line_out_pins[0];
	return 0;
}
590

591
/*
592
 * Realtek SSID verification
593
 */
594

595
596
597
598
/* 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)
599

600
601
static void alc_fixup_sku_ignore(struct hda_codec *codec,
				 const struct hda_fixup *fix, int action)
602
603
{
	struct alc_spec *spec = codec->spec;
604
605
606
	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
		spec->cdefine.fixup = 1;
		spec->cdefine.sku_cfg = ALC_FIXUP_SKU_IGNORE;
607
608
609
	}
}

610
611
612
613
614
static void alc_fixup_no_depop_delay(struct hda_codec *codec,
				    const struct hda_fixup *fix, int action)
{
	struct alc_spec *spec = codec->spec;

615
	if (action == HDA_FIXUP_ACT_PROBE) {
616
		spec->no_depop_delay = 1;
617
618
		codec->depop_delay = 0;
	}
619
620
}

621
static int alc_auto_parse_customize_define(struct hda_codec *codec)
622
{
623
624
	unsigned int ass, tmp, i;
	unsigned nid = 0;
625
626
	struct alc_spec *spec = codec->spec;

627
	spec->cdefine.enable_pcbeep = 1; /* assume always enabled */
628

629
630
631
632
633
	if (spec->cdefine.fixup) {
		ass = spec->cdefine.sku_cfg;
		if (ass == ALC_FIXUP_SKU_IGNORE)
			return -1;
		goto do_sku;
634
635
	}

636
637
	if (!codec->bus->pci)
		return -1;
638
	ass = codec->core.subsystem_id & 0xffff;
639
640
	if (ass != codec->bus->pci->subsystem_device && (ass & 1))
		goto do_sku;
641

642
	nid = 0x1d;
643
	if (codec->core.vendor_id == 0x10ec0260)
644
645
		nid = 0x17;
	ass = snd_hda_codec_get_pincfg(codec, nid);
646

647
	if (!(ass & 1)) {
648
		codec_info(codec, "%s: SKU not ready 0x%08x\n",
649
			   codec->core.chip_name, ass);
650
		return -1;
651
652
	}

653
654
655
656
657
	/* check sum */
	tmp = 0;
	for (i = 1; i < 16; i++) {
		if ((ass >> i) & 1)
			tmp++;
658
	}
659
660
	if (((ass >> 16) & 0xf) != tmp)
		return -1;
661

662
663
664
665
666
667
668
669
670
671
672
	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;

673
	codec_dbg(codec, "SKU: Nid=0x%x sku_cfg=0x%08x\n",
674
		   nid, spec->cdefine.sku_cfg);
675
	codec_dbg(codec, "SKU: port_connectivity=0x%x\n",
676
		   spec->cdefine.port_connectivity);
677
678
679
680
681
682
683
	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);
684
685
686
687

	return 0;
}

688
689
690
691
692
693
694
695
696
/* 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;
}
697
/* return true if the given NID is found in the list */
698
699
static bool found_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
{
700
	return find_idx_in_nid_list(nid, list, nums) >= 0;
701
702
}

703
704
705
706
707
708
709
710
711
/* 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
 */
712
static int alc_subsystem_id(struct hda_codec *codec, const hda_nid_t *ports)
713
714
715
716
717
{
	unsigned int ass, tmp, i;
	unsigned nid;
	struct alc_spec *spec = codec->spec;

718
719
720
721
722
723
724
	if (spec->cdefine.fixup) {
		ass = spec->cdefine.sku_cfg;
		if (ass == ALC_FIXUP_SKU_IGNORE)
			return 0;
		goto do_sku;
	}

725
	ass = codec->core.subsystem_id & 0xffff;
726
727
	if (codec->bus->pci &&
	    ass != codec->bus->pci->subsystem_device && (ass & 1))
728
729
730
731
		goto do_sku;

	/* invalid SSID, check the special NID pin defcfg instead */
	/*
732
	 * 31~30	: port connectivity
733
734
735
736
737
738
739
	 * 29~21	: reserve
	 * 20		: PCBEEP input
	 * 19~16	: Check sum (15:1)
	 * 15~1		: Custom
	 * 0		: override
	*/
	nid = 0x1d;
740
	if (codec->core.vendor_id == 0x10ec0260)
741
742
		nid = 0x17;
	ass = snd_hda_codec_get_pincfg(codec, nid);
743
744
	codec_dbg(codec,
		  "realtek: No valid SSID, checking pincfg 0x%08x for NID 0x%x\n",
745
		   ass, nid);
746
	if (!(ass & 1))
747
748
749
750
751
752
753
754
755
756
757
758
759
		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:
760
	codec_dbg(codec, "realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
761
		   ass & 0xffff, codec->core.vendor_id);
762
763
764
765
766
767
768
769
	/*
	 * 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 */
770
771
772
	if (spec->init_amp == ALC_INIT_UNDEFINED) {
		switch (tmp) {
		case 1:
773
			alc_setup_gpio(codec, 0x01);
774
775
			break;
		case 3:
776
			alc_setup_gpio(codec, 0x02);
777
778
			break;
		case 7:
779
			alc_setup_gpio(codec, 0x03);
780
781
782
783
784
785
			break;
		case 5:
		default:
			spec->init_amp = ALC_INIT_DEFAULT;
			break;
		}
786
	}
787

788
	/* is laptop or Desktop and enable the function "Mute internal speaker
789
790
	 * when the external headphone out jack is plugged"
	 */
791
	if (!(ass & 0x8000))
792
		return 1;
793
794
795
796
797
798
799
	/*
	 * 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"
	 */
800
	if (!alc_get_hp_pin(spec)) {
801
		hda_nid_t nid;
802
		tmp = (ass >> 11) & 0x3;	/* HP to chassis */
803
		nid = ports[tmp];
804
805
		if (found_in_nid_list(nid, spec->gen.autocfg.line_out_pins,
				      spec->gen.autocfg.line_outs))
806
			return 1;
807
		spec->gen.autocfg.hp_pins[0] = nid;
808
	}
809
810
	return 1;
}
811

812
813
814
/* 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)
815
{
816
	if (!alc_subsystem_id(codec, ports)) {
817
		struct alc_spec *spec = codec->spec;
818
819
820
821
822
		if (spec->init_amp == ALC_INIT_UNDEFINED) {
			codec_dbg(codec,
				  "realtek: Enable default setup for auto mode as fallback\n");
			spec->init_amp = ALC_INIT_DEFAULT;
		}
823
	}
824
}
825

826
/*
827
 */
828

829
830
static void alc_fixup_inv_dmic(struct hda_codec *codec,
			       const struct hda_fixup *fix, int action)
831
832
{
	struct alc_spec *spec = codec->spec;
833

834
	spec->gen.inv_dmic_split = 1;
835
836
}

837

838
static int alc_build_controls(struct hda_codec *codec)
839
{
840
	int err;
841

842
843
844
	err = snd_hda_gen_build_controls(codec);
	if (err < 0)
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
845

846
	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_BUILD);
847
	return 0;
848
849
850
}


851
/*
852
 * Common callbacks
853
 */
854

855
856
857
858
859
static void alc_pre_init(struct hda_codec *codec)
{
	alc_fill_eapd_coef(codec);
}

860
861
#define is_s3_resume(codec) \
	((codec)->core.dev.power.power_state.event == PM_EVENT_RESUME)
862
863
864
#define is_s4_resume(codec) \
	((codec)->core.dev.power.power_state.event == PM_EVENT_RESTORE)

865
static int alc_init(struct hda_codec *codec)
866
867
{
	struct alc_spec *spec = codec->spec;
868

869
870
871
872
	/* hibernation resume needs the full chip initialization */
	if (is_s4_resume(codec))
		alc_pre_init(codec);

873
874
	if (spec->init_hook)
		spec->init_hook(codec);
875

876
	spec->gen.skip_verbs = 1; /* applied in below */
877
	snd_hda_gen_init(codec);
878
879
	alc_fix_pll(codec);
	alc_auto_init_amp(codec, spec->init_amp);
880
	snd_hda_apply_verbs(codec); /* apply verbs here after own init */
881

882
	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT);
883

884
885
	return 0;
}
886

887
static inline void alc_shutup(struct hda_codec *codec)
888
889
{
	struct alc_spec *spec = codec->spec;
890

891
892
893
	if (!snd_hda_get_bool_hint(codec, "shutup"))
		return; /* disabled explicitly by hints */

894
895
	if (spec && spec->shutup)
		spec->shutup(codec);
896
	else
897
		alc_shutup_pins(codec);
898
899
}

900
901
902
903
904
905
906
907
908
909
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);
}

910
#define alc_free	snd_hda_gen_free
911

912
913
#ifdef CONFIG_PM
static void alc_power_eapd(struct hda_codec *codec)
914
{
915
	alc_auto_setup_eapd(codec, false);
916
}
917

918
static int alc_suspend(struct hda_codec *codec)
919
920
{
	struct alc_spec *spec = codec->spec;
921
922
923
	alc_shutup(codec);
	if (spec && spec->power_hook)
		spec->power_hook(codec);
924
925
	return 0;
}
926
#endif
927

928
929
#ifdef CONFIG_PM
static int alc_resume(struct hda_codec *codec)
930
{
931
932
933
934
	struct alc_spec *spec = codec->spec;

	if (!spec->no_depop_delay)
		msleep(150); /* to avoid pop noise */
935
	codec->patch_ops.init(codec);
936
	snd_hda_regmap_sync(codec);
937
938
	hda_call_check_power_status(codec, 0x01);
	return 0;
939
}
940
#endif
941

942
943
/*
 */
944
945
946
947
948
949
950
951
952
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,
953
	.check_power_status = snd_hda_gen_check_power_status,
954
#endif
955
	.reboot_notify = alc_reboot_notify,
956
};
957

958

959
#define alc_codec_rename(codec, name) snd_hda_codec_set_name(codec, name)
960

961
/*
962
 * Rename codecs appropriately from COEF value or subvendor id
963
 */
964
965
966
967
968
969
struct alc_codec_rename_table {
	unsigned int vendor_id;
	unsigned short coef_mask;
	unsigned short coef_bits;
	const char *name;
};
970

971
972
973
974
975
976
977
struct alc_codec_rename_pci_table {
	unsigned int codec_vendor_id;
	unsigned short pci_subvendor;
	unsigned short pci_subdevice;
	const char *name;
};

978
static const struct alc_codec_rename_table rename_tbl[] = {
979
	{ 0x10ec0221, 0xf00f, 0x1003, "ALC231" },
980
981
982
983
984
985
986
987
	{ 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" },
988
	{ 0x10ec0662, 0xffff, 0x4020, "ALC656" },
989
990
991
992
993
994
995
996
997
	{ 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 */
};
998

999
static const struct alc_codec_rename_pci_table rename_pci_tbl[] = {
1000
1001
1002
	{ 0x10ec0280, 0x1028, 0, "ALC3220" },
	{ 0x10ec0282, 0x1028, 0, "ALC3221" },
	{ 0x10ec0283, 0x1028, 0, "ALC3223" },
1003
	{ 0x10ec0288, 0x1028, 0, "ALC3263" },
1004
	{ 0x10ec0292, 0x1028, 0, "ALC3226" },
1005
	{ 0x10ec0293, 0x1028, 0, "ALC3235" },
1006
1007
	{ 0x10ec0255, 0x1028, 0, "ALC3234" },
	{ 0x10ec0668, 0x1028, 0, "ALC3661" },
1008
1009
	{ 0x10ec0275, 0x1028, 0, "ALC3260" },
	{ 0x10ec0899, 0x1028, 0, "ALC3861" },
1010
	{ 0x10ec0298, 0x1028, 0, "ALC3266" },
1011
	{ 0x10ec0236, 0x1028, 0, "ALC3204" },
1012
	{ 0x10ec0256, 0x1028, 0, "ALC3246" },
1013
	{ 0x10ec0225, 0x1028, 0, "ALC3253" },
1014
	{ 0x10ec0295, 0x1028, 0, "ALC3254" },
1015
	{ 0x10ec0299, 0x1028, 0, "ALC3271" },
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
	{ 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" },
1027
1028
1029
	{ } /* terminator */
};

1030
static int alc_codec_rename_from_preset(struct hda_codec *codec)
1031
{
1032
	const struct alc_codec_rename_table *p;
1033
	const struct alc_codec_rename_pci_table *q;
1034

1035
	for (p = rename_tbl; p->vendor_id; p++) {
1036
		if (p->vendor_id != codec->core.vendor_id)
1037
1038
1039
			continue;
		if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
			return alc_codec_rename(codec, p->name);
1040
	}
1041

1042
1043
	if (!codec->bus->pci)
		return 0;
1044
	for (q = rename_pci_tbl; q->codec_vendor_id; q++) {
1045
		if (q->codec_vendor_id != codec->core.vendor_id)
1046
1047
1048
1049
1050
1051
1052
1053
			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);
	}

1054
	return 0;
1055
}
1056

1057

1058
1059