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

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

43
/* unsol event tags */
44
#define ALC_DCVOL_EVENT		0x08
45

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

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

58
59
60
61
62
63
64
65
66
67
68
69
70
71
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,
};

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

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

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

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

96
97
98
	/* inverted dmic fix */
	unsigned int inv_dmic_fixup:1; /* has inverted digital-mic workaround */
	unsigned int inv_dmic_muted:1; /* R-ch of inv d-mic is muted? */
99
	hda_nid_t inv_dmic_pin;
100

101
102
103
104
	/* mute LED for HP laptops, see alc269_fixup_mic_mute_hook() */
	int mute_led_polarity;
	hda_nid_t mute_led_nid;

105
106
	unsigned int gpio_led; /* used for alc269_fixup_hp_gpio_led() */

107
108
109
110
111
	hda_nid_t headset_mic_pin;
	hda_nid_t headphone_mic_pin;
	int current_headset_mode;
	int current_headset_type;

112
113
	/* hooks */
	void (*init_hook)(struct hda_codec *codec);
114
#ifdef CONFIG_PM
115
	void (*power_hook)(struct hda_codec *codec);
116
#endif
117
	void (*shutup)(struct hda_codec *codec);
118

119
	int init_amp;
120
	int codec_variant;	/* flag for other variants */
121
	bool has_alc5505_dsp;
122

123
124
125
	/* for PLL fix */
	hda_nid_t pll_nid;
	unsigned int pll_coef_idx, pll_coef_bit;
126
	unsigned int coef0;
127
128
};

129
/*
130
131
132
 * 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().
133
 */
134
static void add_mixer(struct alc_spec *spec, const struct snd_kcontrol_new *mix)
135
136
137
138
139
140
{
	if (snd_BUG_ON(spec->num_mixers >= ARRAY_SIZE(spec->mixers)))
		return;
	spec->mixers[spec->num_mixers++] = mix;
}

141
/*
142
 * GPIO setup tables, used in initialization
143
 */
144
/* Enable GPIO mask and set output */
145
static const struct hda_verb alc_gpio1_init_verbs[] = {
146
147
148
149
150
151
	{0x01, AC_VERB_SET_GPIO_MASK, 0x01},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x01},
	{ }
};

152
static const struct hda_verb alc_gpio2_init_verbs[] = {
153
154
155
156
157
158
	{0x01, AC_VERB_SET_GPIO_MASK, 0x02},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x02},
	{ }
};

159
static const struct hda_verb alc_gpio3_init_verbs[] = {
160
161
162
163
164
165
	{0x01, AC_VERB_SET_GPIO_MASK, 0x03},
	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
	{0x01, AC_VERB_SET_GPIO_DATA, 0x03},
	{ }
};

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Fix hardware PLL issue
 * On some codecs, the analog PLL gating control must be off while
 * the default value is 1.
 */
static void alc_fix_pll(struct hda_codec *codec)
{
	struct alc_spec *spec = codec->spec;
	unsigned int val;

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

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

198
/* update the master volume per volume-knob's unsol event */
199
static void alc_update_knob_master(struct hda_codec *codec, struct hda_jack_tbl *jack)
200
201
202
203
204
205
206
207
208
209
210
{
	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;
211
	val = snd_hda_codec_read(codec, jack->nid, 0,
212
213
214
215
216
217
218
219
				 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);
}

220
static void alc880_unsol_event(struct hda_codec *codec, unsigned int res)
221
{
222
223
224
	/* For some reason, the res given from ALC880 is broken.
	   Here we adjust it properly. */
	snd_hda_jack_unsol_event(codec, res >> 2);
225
226
}

227
228
229
230
231
232
233
234
/* additional initialization for ALC888 variants */
static void alc888_coef_init(struct hda_codec *codec)
{
	unsigned int tmp;

	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 0);
	tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
235
	if ((tmp & 0xf0) == 0x20)
236
237
238
239
240
241
242
243
244
		/* alc888S-VC */
		snd_hda_codec_read(codec, 0x20, 0,
				   AC_VERB_SET_PROC_COEF, 0x830);
	 else
		 /* alc888-VB */
		 snd_hda_codec_read(codec, 0x20, 0,
				    AC_VERB_SET_PROC_COEF, 0x3030);
}

245
/* additional initialization for ALC889 variants */
246
247
248
249
250
251
252
253
254
255
static void alc889_coef_init(struct hda_codec *codec)
{
	unsigned int tmp;

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

256
257
258
259
260
261
262
263
264
265
/* 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);
}

266
267
268
269
/* 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 */
270
271
272
273
274
275
	static hda_nid_t pins[] = {
		0x0f, 0x10, 0x14, 0x15, 0
	};
	hda_nid_t *p;
	for (p = pins; *p; p++)
		set_eapd(codec, *p, on);
276
277
}

278
279
280
281
282
283
284
/* generic shutup callback;
 * just turning off EPAD and a little pause for avoiding pop-noise
 */
static void alc_eapd_shutup(struct hda_codec *codec)
{
	alc_auto_setup_eapd(codec, false);
	msleep(200);
285
	snd_hda_shutup_pins(codec);
286
287
}

288
/* generic EAPD initialization */
289
static void alc_auto_init_amp(struct hda_codec *codec, int type)
290
{
291
	unsigned int tmp;
292

293
	alc_auto_setup_eapd(codec, true);
294
295
	switch (type) {
	case ALC_INIT_GPIO1:
296
297
		snd_hda_sequence_write(codec, alc_gpio1_init_verbs);
		break;
298
	case ALC_INIT_GPIO2:
299
300
		snd_hda_sequence_write(codec, alc_gpio2_init_verbs);
		break;
301
	case ALC_INIT_GPIO3:
302
303
		snd_hda_sequence_write(codec, alc_gpio3_init_verbs);
		break;
304
	case ALC_INIT_DEFAULT:
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
		switch (codec->vendor_id) {
		case 0x10ec0260:
			snd_hda_codec_write(codec, 0x1a, 0,
					    AC_VERB_SET_COEF_INDEX, 7);
			tmp = snd_hda_codec_read(codec, 0x1a, 0,
						 AC_VERB_GET_PROC_COEF, 0);
			snd_hda_codec_write(codec, 0x1a, 0,
					    AC_VERB_SET_COEF_INDEX, 7);
			snd_hda_codec_write(codec, 0x1a, 0,
					    AC_VERB_SET_PROC_COEF,
					    tmp | 0x2010);
			break;
		case 0x10ec0262:
		case 0x10ec0880:
		case 0x10ec0882:
		case 0x10ec0883:
		case 0x10ec0885:
322
		case 0x10ec0887:
323
		/*case 0x10ec0889:*/ /* this causes an SPDIF problem */
324
			alc889_coef_init(codec);
325
			break;
326
		case 0x10ec0888:
327
			alc888_coef_init(codec);
328
			break;
329
#if 0 /* XXX: This may cause the silent output on speaker on some machines */
330
331
332
333
334
335
336
		case 0x10ec0267:
		case 0x10ec0268:
			snd_hda_codec_write(codec, 0x20, 0,
					    AC_VERB_SET_COEF_INDEX, 7);
			tmp = snd_hda_codec_read(codec, 0x20, 0,
						 AC_VERB_GET_PROC_COEF, 0);
			snd_hda_codec_write(codec, 0x20, 0,
337
					    AC_VERB_SET_COEF_INDEX, 7);
338
339
340
341
			snd_hda_codec_write(codec, 0x20, 0,
					    AC_VERB_SET_PROC_COEF,
					    tmp | 0x3000);
			break;
342
#endif /* XXX */
343
		}
344
345
346
347
		break;
	}
}

348

349
/*
350
 * Realtek SSID verification
351
 */
352

353
354
355
356
/* 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)
357

358
359
static void alc_fixup_sku_ignore(struct hda_codec *codec,
				 const struct hda_fixup *fix, int action)
360
361
{
	struct alc_spec *spec = codec->spec;
362
363
364
	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
		spec->cdefine.fixup = 1;
		spec->cdefine.sku_cfg = ALC_FIXUP_SKU_IGNORE;
365
366
367
	}
}

368
static int alc_auto_parse_customize_define(struct hda_codec *codec)
369
{
370
371
	unsigned int ass, tmp, i;
	unsigned nid = 0;
372
373
	struct alc_spec *spec = codec->spec;

374
	spec->cdefine.enable_pcbeep = 1; /* assume always enabled */
375

376
377
378
379
380
	if (spec->cdefine.fixup) {
		ass = spec->cdefine.sku_cfg;
		if (ass == ALC_FIXUP_SKU_IGNORE)
			return -1;
		goto do_sku;
381
382
	}

383
384
385
	ass = codec->subsystem_id & 0xffff;
	if (ass != codec->bus->pci->subsystem_device && (ass & 1))
		goto do_sku;
386

387
388
389
390
	nid = 0x1d;
	if (codec->vendor_id == 0x10ec0260)
		nid = 0x17;
	ass = snd_hda_codec_get_pincfg(codec, nid);
391

392
393
394
395
	if (!(ass & 1)) {
		printk(KERN_INFO "hda_codec: %s: SKU not ready 0x%08x\n",
		       codec->chip_name, ass);
		return -1;
396
397
	}

398
399
400
401
402
	/* check sum */
	tmp = 0;
	for (i = 1; i < 16; i++) {
		if ((ass >> i) & 1)
			tmp++;
403
	}
404
405
	if (((ass >> 16) & 0xf) != tmp)
		return -1;
406

407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
	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;

	snd_printd("SKU: Nid=0x%x sku_cfg=0x%08x\n",
		   nid, spec->cdefine.sku_cfg);
	snd_printd("SKU: port_connectivity=0x%x\n",
		   spec->cdefine.port_connectivity);
	snd_printd("SKU: enable_pcbeep=0x%x\n", spec->cdefine.enable_pcbeep);
	snd_printd("SKU: check_sum=0x%08x\n", spec->cdefine.check_sum);
	snd_printd("SKU: customization=0x%08x\n", spec->cdefine.customization);
	snd_printd("SKU: external_amp=0x%x\n", spec->cdefine.external_amp);
	snd_printd("SKU: platform_type=0x%x\n", spec->cdefine.platform_type);
	snd_printd("SKU: swap=0x%x\n", spec->cdefine.swap);
	snd_printd("SKU: override=0x%x\n", spec->cdefine.override);

	return 0;
}

433
434
435
436
437
438
439
440
441
/* 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;
}
442
/* return true if the given NID is found in the list */
443
444
static bool found_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
{
445
	return find_idx_in_nid_list(nid, list, nums) >= 0;
446
447
}

448
449
450
451
452
453
454
455
456
457
458
/* 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
 */
static int alc_subsystem_id(struct hda_codec *codec,
			    hda_nid_t porta, hda_nid_t porte,
459
			    hda_nid_t portd, hda_nid_t porti)
460
461
462
463
464
{
	unsigned int ass, tmp, i;
	unsigned nid;
	struct alc_spec *spec = codec->spec;

465
466
467
468
469
470
471
	if (spec->cdefine.fixup) {
		ass = spec->cdefine.sku_cfg;
		if (ass == ALC_FIXUP_SKU_IGNORE)
			return 0;
		goto do_sku;
	}

472
473
474
475
476
477
	ass = codec->subsystem_id & 0xffff;
	if ((ass != codec->bus->pci->subsystem_device) && (ass & 1))
		goto do_sku;

	/* invalid SSID, check the special NID pin defcfg instead */
	/*
478
	 * 31~30	: port connectivity
479
480
481
482
483
484
485
486
487
488
489
490
	 * 29~21	: reserve
	 * 20		: PCBEEP input
	 * 19~16	: Check sum (15:1)
	 * 15~1		: Custom
	 * 0		: override
	*/
	nid = 0x1d;
	if (codec->vendor_id == 0x10ec0260)
		nid = 0x17;
	ass = snd_hda_codec_get_pincfg(codec, nid);
	snd_printd("realtek: No valid SSID, "
		   "checking pincfg 0x%08x for NID 0x%x\n",
491
		   ass, nid);
492
	if (!(ass & 1))
493
494
495
496
497
498
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
525
526
		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:
	snd_printd("realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
		   ass & 0xffff, codec->vendor_id);
	/*
	 * 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:
527
	default:
528
		spec->init_amp = ALC_INIT_DEFAULT;
529
530
		break;
	}
531

532
	/* is laptop or Desktop and enable the function "Mute internal speaker
533
534
	 * when the external headphone out jack is plugged"
	 */
535
	if (!(ass & 0x8000))
536
		return 1;
537
538
539
540
541
542
543
	/*
	 * 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"
	 */
544
545
546
	if (!spec->gen.autocfg.hp_pins[0] &&
	    !(spec->gen.autocfg.line_out_pins[0] &&
	      spec->gen.autocfg.line_out_type == AUTO_PIN_HP_OUT)) {
547
		hda_nid_t nid;
548
549
		tmp = (ass >> 11) & 0x3;	/* HP to chassis */
		if (tmp == 0)
550
			nid = porta;
551
		else if (tmp == 1)
552
			nid = porte;
553
		else if (tmp == 2)
554
			nid = portd;
555
556
		else if (tmp == 3)
			nid = porti;
557
558
		if (found_in_nid_list(nid, spec->gen.autocfg.line_out_pins,
				      spec->gen.autocfg.line_outs))
559
			return 1;
560
		spec->gen.autocfg.hp_pins[0] = nid;
561
	}
562
563
	return 1;
}
564

565
566
567
/* 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)
568
{
569
	if (!alc_subsystem_id(codec, ports[0], ports[1], ports[2], ports[3])) {
570
571
572
573
574
		struct alc_spec *spec = codec->spec;
		snd_printd("realtek: "
			   "Enable default setup for auto mode as fallback\n");
		spec->init_amp = ALC_INIT_DEFAULT;
	}
575
}
576

577
578
579
/*
 * COEF access helper functions
 */
580
581
582
583

static int alc_read_coefex_idx(struct hda_codec *codec,
					hda_nid_t nid,
					unsigned int coef_idx)
584
585
{
	unsigned int val;
586
	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_COEF_INDEX,
587
		    		coef_idx);
588
	val = snd_hda_codec_read(codec, nid, 0,
589
590
591
592
			 	AC_VERB_GET_PROC_COEF, 0);
	return val;
}

593
594
595
596
597
#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,
598
599
							unsigned int coef_val)
{
600
	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_COEF_INDEX,
601
			    coef_idx);
602
	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PROC_COEF,
603
604
605
			    coef_val);
}

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

609
610
611
612
613
614
615
616
617
/* 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;
}

618
/*
619
 */
620

621
static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
622
{
623
624
625
626
	struct hda_gen_spec *spec = codec->spec;
	if (spec->dyn_adc_switch)
		adc_idx = spec->dyn_adc_idx[imux_idx];
	return spec->adc_nids[adc_idx];
627
628
}

629
static void alc_inv_dmic_sync_adc(struct hda_codec *codec, int adc_idx)
630
631
{
	struct alc_spec *spec = codec->spec;
632
	struct hda_input_mux *imux = &spec->gen.input_mux;
633
634
635
636
	struct nid_path *path;
	hda_nid_t nid;
	int i, dir, parm;
	unsigned int val;
637

638
	for (i = 0; i < imux->num_items; i++) {
639
		if (spec->gen.imux_pins[i] == spec->inv_dmic_pin)
640
			break;
641
	}
642
643
	if (i >= imux->num_items)
		return;
644

645
646
	path = snd_hda_get_nid_path(codec, spec->inv_dmic_pin,
				    get_adc_nid(codec, adc_idx, i));
647
648
649
650
651
652
653
	val = path->ctls[NID_PATH_MUTE_CTL];
	if (!val)
		return;
	nid = get_amp_nid_(val);
	dir = get_amp_direction_(val);
	parm = AC_AMP_SET_RIGHT |
		(dir == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT);
654

655
	/* flush all cached amps at first */
656
	snd_hda_codec_flush_cache(codec);
657

658
659
660
661
662
663
664
	/* we care only right channel */
	val = snd_hda_codec_amp_read(codec, nid, 1, dir, 0);
	if (val & 0x80) /* if already muted, we don't need to touch */
		return;
	val |= 0x80;
	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
			    parm | val);
665
666
}

667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/*
 * Inverted digital-mic handling
 *
 * First off, it's a bit tricky.  The "Inverted Internal Mic Capture Switch"
 * gives the additional mute only to the right channel of the digital mic
 * capture stream.  This is a workaround for avoiding the almost silence
 * by summing the stereo stream from some (known to be ForteMedia)
 * digital mic unit.
 *
 * The logic is to call alc_inv_dmic_sync() after each action (possibly)
 * modifying ADC amp.  When the mute flag is set, it mutes the R-channel
 * without caching so that the cache can still keep the original value.
 * The cached value is then restored when the flag is set off or any other
 * than d-mic is used as the current input source.
 */
static void alc_inv_dmic_sync(struct hda_codec *codec, bool force)
{
	struct alc_spec *spec = codec->spec;
685
	int src, nums;
686
687
688
689
690

	if (!spec->inv_dmic_fixup)
		return;
	if (!spec->inv_dmic_muted && !force)
		return;
691
	nums = spec->gen.dyn_adc_switch ? 1 : spec->gen.num_adc_nids;
692
	for (src = 0; src < nums; src++) {
693
694
695
		bool dmic_fixup = false;

		if (spec->inv_dmic_muted &&
696
		    spec->gen.imux_pins[spec->gen.cur_mux[src]] == spec->inv_dmic_pin)
697
698
699
			dmic_fixup = true;
		if (!dmic_fixup && !force)
			continue;
700
		alc_inv_dmic_sync_adc(codec, src);
701
702
703
	}
}

704
705
static void alc_inv_dmic_hook(struct hda_codec *codec,
			     struct snd_ctl_elem_value *ucontrol)
706
707
708
709
{
	alc_inv_dmic_sync(codec, false);
}

710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
static int alc_inv_dmic_sw_get(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct alc_spec *spec = codec->spec;

	ucontrol->value.integer.value[0] = !spec->inv_dmic_muted;
	return 0;
}

static int alc_inv_dmic_sw_put(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct alc_spec *spec = codec->spec;
	unsigned int val = !ucontrol->value.integer.value[0];

	if (val == spec->inv_dmic_muted)
		return 0;
	spec->inv_dmic_muted = val;
	alc_inv_dmic_sync(codec, true);
	return 0;
}

static const struct snd_kcontrol_new alc_inv_dmic_sw = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
736
	.name = "Inverted Internal Mic Capture Switch",
737
738
739
740
741
742
743
744
	.info = snd_ctl_boolean_mono_info,
	.get = alc_inv_dmic_sw_get,
	.put = alc_inv_dmic_sw_put,
};

static int alc_add_inv_dmic_mixer(struct hda_codec *codec, hda_nid_t nid)
{
	struct alc_spec *spec = codec->spec;
745

746
	if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &alc_inv_dmic_sw))
747
748
749
750
		return -ENOMEM;
	spec->inv_dmic_fixup = 1;
	spec->inv_dmic_muted = 0;
	spec->inv_dmic_pin = nid;
751
	spec->gen.cap_sync_hook = alc_inv_dmic_hook;
752
753
754
	return 0;
}

755
756
/* typically the digital mic is put at node 0x12 */
static void alc_fixup_inv_dmic_0x12(struct hda_codec *codec,
757
				    const struct hda_fixup *fix, int action)
758
{
759
	if (action == HDA_FIXUP_ACT_PROBE)
760
761
762
		alc_add_inv_dmic_mixer(codec, 0x12);
}

763

764
765
766
767
768
#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),
769
770
	{ } /* end */
};
771
#endif
772

773
static int alc_build_controls(struct hda_codec *codec)
774
775
{
	struct alc_spec *spec = codec->spec;
776
	int i, err;
777

778
779
780
	err = snd_hda_gen_build_controls(codec);
	if (err < 0)
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
781
782
783
784
785
786

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

788
#ifdef CONFIG_SND_HDA_INPUT_BEEP
789
790
	/* create beep controls if needed */
	if (spec->beep_amp) {
791
		const struct snd_kcontrol_new *knew;
792
793
794
795
		for (knew = alc_beep_mixer; knew->name; knew++) {
			struct snd_kcontrol *kctl;
			kctl = snd_ctl_new1(knew, codec);
			if (!kctl)
796
797
798
799
800
				return -ENOMEM;
			kctl->private_value = spec->beep_amp;
			err = snd_hda_ctl_add(codec, 0, kctl);
			if (err < 0)
				return err;
801
		}
802
	}
803
#endif
804

805
	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_BUILD);
806
	return 0;
807
808
809
}


810
/*
811
 * Common callbacks
812
 */
813

814
static int alc_init(struct hda_codec *codec)
815
816
{
	struct alc_spec *spec = codec->spec;
817

818
819
	if (spec->init_hook)
		spec->init_hook(codec);
820

821
822
	alc_fix_pll(codec);
	alc_auto_init_amp(codec, spec->init_amp);
823

824
	snd_hda_gen_init(codec);
825

826
	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT);
827

828
829
	return 0;
}
830

831
static inline void alc_shutup(struct hda_codec *codec)
832
833
{
	struct alc_spec *spec = codec->spec;
834

835
836
	if (spec && spec->shutup)
		spec->shutup(codec);
837
838
	else
		snd_hda_shutup_pins(codec);
839
840
}

841
842
843
844
845
static void alc_free(struct hda_codec *codec)
{
	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
	snd_hda_gen_free(codec);
}
846

847
848
#ifdef CONFIG_PM
static void alc_power_eapd(struct hda_codec *codec)
849
{
850
	alc_auto_setup_eapd(codec, false);
851
}
852

853
static int alc_suspend(struct hda_codec *codec)
854
855
{
	struct alc_spec *spec = codec->spec;
856
857
858
	alc_shutup(codec);
	if (spec && spec->power_hook)
		spec->power_hook(codec);
859
860
	return 0;
}
861
#endif
862

863
864
#ifdef CONFIG_PM
static int alc_resume(struct hda_codec *codec)
865
{
866
867
868
869
870
871
872
	msleep(150); /* to avoid pop noise */
	codec->patch_ops.init(codec);
	snd_hda_codec_resume_amp(codec);
	snd_hda_codec_resume_cache(codec);
	alc_inv_dmic_sync(codec, true);
	hda_call_check_power_status(codec, 0x01);
	return 0;
873
}
874
#endif
875

876
877
/*
 */
878
879
880
881
882
883
884
885
886
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,
887
	.check_power_status = snd_hda_gen_check_power_status,
888
889
890
#endif
	.reboot_notify = alc_shutup,
};
891

892

893
894
/* replace the codec chip_name with the given string */
static int alc_codec_rename(struct hda_codec *codec, const char *name)
895
{
896
897
898
899
900
	kfree(codec->chip_name);
	codec->chip_name = kstrdup(name, GFP_KERNEL);
	if (!codec->chip_name) {
		alc_free(codec);
		return -ENOMEM;
901
	}
902
	return 0;
903
}
904

905
/*
906
 * Rename codecs appropriately from COEF value
907
 */
908
909
910
911
912
913
struct alc_codec_rename_table {
	unsigned int vendor_id;
	unsigned short coef_mask;
	unsigned short coef_bits;
	const char *name;
};
914

915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
static struct alc_codec_rename_table rename_tbl[] = {
	{ 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" },
	{ 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 */
};
933

934
static int alc_codec_rename_from_preset(struct hda_codec *codec)
935
{
936
	const struct alc_codec_rename_table *p;
937

938
939
940
941
942
	for (p = rename_tbl; p->vendor_id; p++) {
		if (p->vendor_id != codec->vendor_id)
			continue;
		if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
			return alc_codec_rename(codec, p->name);
943
	}
944
	return 0;
945
}
946

947

948
949
950
951
952
953
/*
 * 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))
954

955
static const struct snd_pci_quirk beep_white_list[] = {
956
	SND_PCI_QUIRK(0x1043, 0x103c, "ASUS", 1),
957
	SND_PCI_QUIRK(0x1043, 0x829f, "ASUS", 1),
958
	SND_PCI_QUIRK(0x1043, 0x8376, "EeePC", 1),
959
960
961
	SND_PCI_QUIRK(0x1043, 0x83ce, "EeePC", 1),
	SND_PCI_QUIRK(0x1043, 0x831a, "EeePC", 1),
	SND_PCI_QUIRK(0x1043, 0x834a, "EeePC", 1),
962
	SND_PCI_QUIRK(0x1458, 0xa002, "GA-MA790X", 1),
963
964
	SND_PCI_QUIRK(0x8086, 0xd613, "Intel", 1),
	{}
965
966
};

967
968
969
970
971
972
973
974
975
976
977
978
979
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
980

981
982
983
984
/* 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
 */
985
986
987
static int alc_parse_auto_config(struct hda_codec *codec,
				 const hda_nid_t *ignore_nids,
				 const hda_nid_t *ssid_nids)
988
989
{
	struct alc_spec *spec = codec->spec;
990
	struct auto_pin_cfg *cfg = &spec->gen.autocfg;
991
	int err;
992

993
994
	err = snd_hda_parse_pin_defcfg(codec, cfg, ignore_nids,
				       spec->parse_flags);
995
996
	if (err < 0)
		return err;
997
998
999

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

1001
1002
1003
	err = snd_hda_gen_parse_auto_config(codec, cfg);
	if (err < 0)
		return err;
1004

1005
	return 1;
1006
}
1007

1008
1009
1010
1011
1012
1013
1014
1015
1016
/* 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;
1017
1018
1019
	snd_hda_gen_spec_init(&spec->gen);
	spec->gen.mixer_nid = mixer_nid;
	spec->gen.own_eapd_ctl = 1;
1020
	codec->single_adc_amp = 1;
1021
1022
	/* FIXME: do we need this for all Realtek codec models? */
	codec->spdif_status_reset = 1;
1023
1024
1025
1026
1027
1028
1029
1030
1031

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

1032
1033
1034
static int alc880_parse_auto_config(struct hda_codec *codec)
{
	static const hda_nid_t alc880_ignore[] = { 0x1d, 0 };
1035
	static const hda_nid_t alc880_ssids[] = { 0x15, 0x1b, 0x14, 0 };
1036
1037
1038
	return alc_parse_auto_config(codec, alc880_ignore, alc880_ssids);
}

1039
1040
1041
1042
/*
 * ALC880 fix-ups
 */
enum {
1043
	ALC880_FIXUP_GPIO1,
1044
1045
	ALC880_FIXUP_GPIO2,
	ALC880_FIXUP_MEDION_RIM,
1046
	ALC880_FIXUP_LG,
1047
	ALC880_FIXUP_LG_LW25,
1048
	ALC880_FIXUP_W810,
1049
	ALC880_FIXUP_EAPD_COEF,
1050
	ALC880_FIXUP_TCL_S700,
1051
1052
	ALC880_FIXUP_VOL_KNOB,
	ALC880_FIXUP_FUJITSU,
1053
	ALC880_FIXUP_F1734,
1054
	ALC880_FIXUP_UNIWILL,
1055
	ALC880_FIXUP_UNIWILL_DIG,
1056
	ALC880_FIXUP_Z71V,
1057
	ALC880_FIXUP_ASUS_W5A,
1058
1059
1060
1061
1062
1063
1064
1065
1066
	ALC880_FIXUP_3ST_BASE,
	ALC880_FIXUP_3ST,
	ALC880_FIXUP_3ST_DIG,
	ALC880_FIXUP_5ST_BASE,
	ALC880_FIXUP_5ST,
	ALC880_FIXUP_5ST_DIG,
	ALC880_FIXUP_6ST_BASE,
	ALC880_FIXUP_6ST,
	ALC880_FIXUP_6ST_DIG,
1067
	ALC880_FIXUP_6ST_AUTOMUTE,
1068
1069
};

1070
1071
/* enable the volume-knob widget support on NID 0x21 */
static void alc880_fixup_vol_knob(struct hda_codec *codec,
1072
				  const struct hda_fixup *fix, int action)
1073
{
1074
	if (action == HDA_FIXUP_ACT_PROBE)
1075
		snd_hda_jack_detect_enable_callback(codec, 0x21, ALC_DCVOL_EVENT, alc_update_knob_master);
1076
1077
}

1078
static const struct hda_fixup alc880_fixups[] = {
1079
	[ALC880_FIXUP_GPIO1] = {
1080
		.type = HDA_FIXUP_VERBS,
1081
1082
		.v.verbs = alc_gpio1_init_verbs,
	},
1083
	[ALC880_FIXUP_GPIO2] = {
1084
		.type = HDA_FIXUP_VERBS,
1085
1086
1087
		.v.verbs = alc_gpio2_init_verbs,
	},
	[ALC880_FIXUP_MEDION_RIM] = {
1088
		.type = HDA_FIXUP_VERBS,
1089
1090
1091
1092
1093
1094
1095
1096
		.v.verbs = (const struct hda_verb[]) {
			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
			{ 0x20, AC_VERB_SET_PROC_COEF,  0x3060 },
			{ }
		},
		.chained = true,
		.chain_id = ALC880_FIXUP_GPIO2,
	},
1097
	[ALC880_FIXUP_LG] = {
1098
1099
		.type = HDA_FIXUP_PINS,
		.v.pins = (const struct hda_pintbl[]) {
1100
1101
1102
1103
1104
1105
1106
			/* disable bogus unused pins */
			{ 0x16, 0x411111f0 },
			{ 0x18, 0x411111f0 },
			{ 0x1a, 0x411111f0 },
			{ }
		}
	},
1107
1108
1109
1110
1111
1112
1113
1114
	[ALC880_FIXUP_LG_LW25] = {
		.type = HDA_FIXUP_PINS,
		.v.pins = (const struct hda_pintbl[]) {
			{ 0x1a, 0x0181344f }, /* line-in */
			{ 0x1b, 0x0321403f }, /* headphone */
			{ }
		}