xref: /linux/sound/pci/hda/patch_realtek.c (revision f49f4ab95c301dbccad0efe85296d908b8ae7ad4)
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for Realtek ALC codecs
5  *
6  * Copyright (c) 2004 Kailang Yang <kailang@realtek.com.tw>
7  *                    PeiSen Hou <pshou@realtek.com.tw>
8  *                    Takashi Iwai <tiwai@suse.de>
9  *                    Jonathan Woithe <jwoithe@just42.net>
10  *
11  *  This driver is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This driver is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24  */
25 
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/pci.h>
30 #include <linux/module.h>
31 #include <sound/core.h>
32 #include <sound/jack.h>
33 #include "hda_codec.h"
34 #include "hda_local.h"
35 #include "hda_auto_parser.h"
36 #include "hda_beep.h"
37 #include "hda_jack.h"
38 
39 /* unsol event tags */
40 #define ALC_FRONT_EVENT		0x01
41 #define ALC_DCVOL_EVENT		0x02
42 #define ALC_HP_EVENT		0x04
43 #define ALC_MIC_EVENT		0x08
44 
45 /* for GPIO Poll */
46 #define GPIO_MASK	0x03
47 
48 /* extra amp-initialization sequence types */
49 enum {
50 	ALC_INIT_NONE,
51 	ALC_INIT_DEFAULT,
52 	ALC_INIT_GPIO1,
53 	ALC_INIT_GPIO2,
54 	ALC_INIT_GPIO3,
55 };
56 
57 struct alc_customize_define {
58 	unsigned int  sku_cfg;
59 	unsigned char port_connectivity;
60 	unsigned char check_sum;
61 	unsigned char customization;
62 	unsigned char external_amp;
63 	unsigned int  enable_pcbeep:1;
64 	unsigned int  platform_type:1;
65 	unsigned int  swap:1;
66 	unsigned int  override:1;
67 	unsigned int  fixup:1; /* Means that this sku is set by driver, not read from hw */
68 };
69 
70 struct alc_multi_io {
71 	hda_nid_t pin;		/* multi-io widget pin NID */
72 	hda_nid_t dac;		/* DAC to be connected */
73 	unsigned int ctl_in;	/* cached input-pin control value */
74 };
75 
76 enum {
77 	ALC_AUTOMUTE_PIN,	/* change the pin control */
78 	ALC_AUTOMUTE_AMP,	/* mute/unmute the pin AMP */
79 	ALC_AUTOMUTE_MIXER,	/* mute/unmute mixer widget AMP */
80 };
81 
82 #define MAX_VOL_NIDS	0x40
83 
84 /* make compatible with old code */
85 #define alc_apply_pincfgs	snd_hda_apply_pincfgs
86 #define alc_apply_fixup		snd_hda_apply_fixup
87 #define alc_pick_fixup		snd_hda_pick_fixup
88 #define alc_fixup		hda_fixup
89 #define alc_pincfg		hda_pintbl
90 #define alc_model_fixup		hda_model_fixup
91 
92 #define ALC_FIXUP_PINS	HDA_FIXUP_PINS
93 #define ALC_FIXUP_VERBS	HDA_FIXUP_VERBS
94 #define ALC_FIXUP_FUNC	HDA_FIXUP_FUNC
95 
96 #define ALC_FIXUP_ACT_PRE_PROBE	HDA_FIXUP_ACT_PRE_PROBE
97 #define ALC_FIXUP_ACT_PROBE	HDA_FIXUP_ACT_PROBE
98 #define ALC_FIXUP_ACT_INIT	HDA_FIXUP_ACT_INIT
99 #define ALC_FIXUP_ACT_BUILD	HDA_FIXUP_ACT_BUILD
100 
101 
102 struct alc_spec {
103 	struct hda_gen_spec gen;
104 
105 	/* codec parameterization */
106 	const struct snd_kcontrol_new *mixers[5];	/* mixer arrays */
107 	unsigned int num_mixers;
108 	const struct snd_kcontrol_new *cap_mixer;	/* capture mixer */
109 	unsigned int beep_amp;	/* beep amp value, set via set_beep_amp() */
110 
111 	char stream_name_analog[32];	/* analog PCM stream */
112 	const struct hda_pcm_stream *stream_analog_playback;
113 	const struct hda_pcm_stream *stream_analog_capture;
114 	const struct hda_pcm_stream *stream_analog_alt_playback;
115 	const struct hda_pcm_stream *stream_analog_alt_capture;
116 
117 	char stream_name_digital[32];	/* digital PCM stream */
118 	const struct hda_pcm_stream *stream_digital_playback;
119 	const struct hda_pcm_stream *stream_digital_capture;
120 
121 	/* playback */
122 	struct hda_multi_out multiout;	/* playback set-up
123 					 * max_channels, dacs must be set
124 					 * dig_out_nid and hp_nid are optional
125 					 */
126 	hda_nid_t alt_dac_nid;
127 	hda_nid_t slave_dig_outs[3];	/* optional - for auto-parsing */
128 	int dig_out_type;
129 
130 	/* capture */
131 	unsigned int num_adc_nids;
132 	const hda_nid_t *adc_nids;
133 	const hda_nid_t *capsrc_nids;
134 	hda_nid_t dig_in_nid;		/* digital-in NID; optional */
135 	hda_nid_t mixer_nid;		/* analog-mixer NID */
136 	DECLARE_BITMAP(vol_ctls, MAX_VOL_NIDS << 1);
137 	DECLARE_BITMAP(sw_ctls, MAX_VOL_NIDS << 1);
138 
139 	/* capture setup for dynamic dual-adc switch */
140 	hda_nid_t cur_adc;
141 	unsigned int cur_adc_stream_tag;
142 	unsigned int cur_adc_format;
143 
144 	/* capture source */
145 	unsigned int num_mux_defs;
146 	const struct hda_input_mux *input_mux;
147 	unsigned int cur_mux[3];
148 	hda_nid_t ext_mic_pin;
149 	hda_nid_t dock_mic_pin;
150 	hda_nid_t int_mic_pin;
151 
152 	/* channel model */
153 	const struct hda_channel_mode *channel_mode;
154 	int num_channel_mode;
155 	int need_dac_fix;
156 	int const_channel_count;
157 	int ext_channel_count;
158 
159 	/* PCM information */
160 	struct hda_pcm pcm_rec[3];	/* used in alc_build_pcms() */
161 
162 	/* dynamic controls, init_verbs and input_mux */
163 	struct auto_pin_cfg autocfg;
164 	struct alc_customize_define cdefine;
165 	struct snd_array kctls;
166 	struct hda_input_mux private_imux[3];
167 	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
168 	hda_nid_t private_adc_nids[AUTO_CFG_MAX_OUTS];
169 	hda_nid_t private_capsrc_nids[AUTO_CFG_MAX_OUTS];
170 	hda_nid_t imux_pins[HDA_MAX_NUM_INPUTS];
171 	unsigned int dyn_adc_idx[HDA_MAX_NUM_INPUTS];
172 	int int_mic_idx, ext_mic_idx, dock_mic_idx; /* for auto-mic */
173 	hda_nid_t inv_dmic_pin;
174 
175 	/* hooks */
176 	void (*init_hook)(struct hda_codec *codec);
177 #ifdef CONFIG_PM
178 	void (*power_hook)(struct hda_codec *codec);
179 #endif
180 	void (*shutup)(struct hda_codec *codec);
181 	void (*automute_hook)(struct hda_codec *codec);
182 
183 	/* for pin sensing */
184 	unsigned int hp_jack_present:1;
185 	unsigned int line_jack_present:1;
186 	unsigned int master_mute:1;
187 	unsigned int auto_mic:1;
188 	unsigned int auto_mic_valid_imux:1;	/* valid imux for auto-mic */
189 	unsigned int automute_speaker:1; /* automute speaker outputs */
190 	unsigned int automute_lo:1; /* automute LO outputs */
191 	unsigned int detect_hp:1;	/* Headphone detection enabled */
192 	unsigned int detect_lo:1;	/* Line-out detection enabled */
193 	unsigned int automute_speaker_possible:1; /* there are speakers and either LO or HP */
194 	unsigned int automute_lo_possible:1;	  /* there are line outs and HP */
195 	unsigned int keep_vref_in_automute:1; /* Don't clear VREF in automute */
196 
197 	/* other flags */
198 	unsigned int no_analog :1; /* digital I/O only */
199 	unsigned int dyn_adc_switch:1; /* switch ADCs (for ALC275) */
200 	unsigned int single_input_src:1;
201 	unsigned int vol_in_capsrc:1; /* use capsrc volume (ADC has no vol) */
202 	unsigned int parse_flags; /* passed to snd_hda_parse_pin_defcfg() */
203 	unsigned int shared_mic_hp:1; /* HP/Mic-in sharing */
204 	unsigned int inv_dmic_fixup:1; /* has inverted digital-mic workaround */
205 	unsigned int inv_dmic_muted:1; /* R-ch of inv d-mic is muted? */
206 	unsigned int no_primary_hp:1; /* Don't prefer HP pins to speaker pins */
207 
208 	/* auto-mute control */
209 	int automute_mode;
210 	hda_nid_t automute_mixer_nid[AUTO_CFG_MAX_OUTS];
211 
212 	int init_amp;
213 	int codec_variant;	/* flag for other variants */
214 
215 	/* for virtual master */
216 	hda_nid_t vmaster_nid;
217 	struct hda_vmaster_mute_hook vmaster_mute;
218 #ifdef CONFIG_PM
219 	struct hda_loopback_check loopback;
220 	int num_loopbacks;
221 	struct hda_amp_list loopback_list[8];
222 #endif
223 
224 	/* for PLL fix */
225 	hda_nid_t pll_nid;
226 	unsigned int pll_coef_idx, pll_coef_bit;
227 	unsigned int coef0;
228 
229 	/* multi-io */
230 	int multi_ios;
231 	struct alc_multi_io multi_io[4];
232 
233 	/* bind volumes */
234 	struct snd_array bind_ctls;
235 };
236 
237 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
238 			   int dir, unsigned int bits)
239 {
240 	if (!nid)
241 		return false;
242 	if (get_wcaps(codec, nid) & (1 << (dir + 1)))
243 		if (query_amp_caps(codec, nid, dir) & bits)
244 			return true;
245 	return false;
246 }
247 
248 #define nid_has_mute(codec, nid, dir) \
249 	check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
250 #define nid_has_volume(codec, nid, dir) \
251 	check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
252 
253 /*
254  * input MUX handling
255  */
256 static int alc_mux_enum_info(struct snd_kcontrol *kcontrol,
257 			     struct snd_ctl_elem_info *uinfo)
258 {
259 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
260 	struct alc_spec *spec = codec->spec;
261 	unsigned int mux_idx = snd_ctl_get_ioffidx(kcontrol, &uinfo->id);
262 	if (mux_idx >= spec->num_mux_defs)
263 		mux_idx = 0;
264 	if (!spec->input_mux[mux_idx].num_items && mux_idx > 0)
265 		mux_idx = 0;
266 	return snd_hda_input_mux_info(&spec->input_mux[mux_idx], uinfo);
267 }
268 
269 static int alc_mux_enum_get(struct snd_kcontrol *kcontrol,
270 			    struct snd_ctl_elem_value *ucontrol)
271 {
272 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
273 	struct alc_spec *spec = codec->spec;
274 	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
275 
276 	ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
277 	return 0;
278 }
279 
280 static bool alc_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
281 {
282 	struct alc_spec *spec = codec->spec;
283 	hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
284 
285 	if (spec->cur_adc && spec->cur_adc != new_adc) {
286 		/* stream is running, let's swap the current ADC */
287 		__snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
288 		spec->cur_adc = new_adc;
289 		snd_hda_codec_setup_stream(codec, new_adc,
290 					   spec->cur_adc_stream_tag, 0,
291 					   spec->cur_adc_format);
292 		return true;
293 	}
294 	return false;
295 }
296 
297 static inline hda_nid_t get_capsrc(struct alc_spec *spec, int idx)
298 {
299 	return spec->capsrc_nids ?
300 		spec->capsrc_nids[idx] : spec->adc_nids[idx];
301 }
302 
303 static void call_update_outputs(struct hda_codec *codec);
304 static void alc_inv_dmic_sync(struct hda_codec *codec, bool force);
305 
306 /* for shared I/O, change the pin-control accordingly */
307 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
308 {
309 	struct alc_spec *spec = codec->spec;
310 	unsigned int val;
311 	hda_nid_t pin = spec->autocfg.inputs[1].pin;
312 	/* NOTE: this assumes that there are only two inputs, the
313 	 * first is the real internal mic and the second is HP/mic jack.
314 	 */
315 
316 	val = snd_hda_get_default_vref(codec, pin);
317 
318 	/* This pin does not have vref caps - let's enable vref on pin 0x18
319 	   instead, as suggested by Realtek */
320 	if (val == AC_PINCTL_VREF_HIZ) {
321 		const hda_nid_t vref_pin = 0x18;
322 		/* Sanity check pin 0x18 */
323 		if (get_wcaps_type(get_wcaps(codec, vref_pin)) == AC_WID_PIN &&
324 		    get_defcfg_connect(snd_hda_codec_get_pincfg(codec, vref_pin)) == AC_JACK_PORT_NONE) {
325 			unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
326 			if (vref_val != AC_PINCTL_VREF_HIZ)
327 				snd_hda_set_pin_ctl(codec, vref_pin, PIN_IN | (set_as_mic ? vref_val : 0));
328 		}
329 	}
330 
331 	val = set_as_mic ? val | PIN_IN : PIN_HP;
332 	snd_hda_set_pin_ctl(codec, pin, val);
333 
334 	spec->automute_speaker = !set_as_mic;
335 	call_update_outputs(codec);
336 }
337 
338 /* select the given imux item; either unmute exclusively or select the route */
339 static int alc_mux_select(struct hda_codec *codec, unsigned int adc_idx,
340 			  unsigned int idx, bool force)
341 {
342 	struct alc_spec *spec = codec->spec;
343 	const struct hda_input_mux *imux;
344 	unsigned int mux_idx;
345 	int i, type, num_conns;
346 	hda_nid_t nid;
347 
348 	if (!spec->input_mux)
349 		return 0;
350 
351 	mux_idx = adc_idx >= spec->num_mux_defs ? 0 : adc_idx;
352 	imux = &spec->input_mux[mux_idx];
353 	if (!imux->num_items && mux_idx > 0)
354 		imux = &spec->input_mux[0];
355 	if (!imux->num_items)
356 		return 0;
357 
358 	if (idx >= imux->num_items)
359 		idx = imux->num_items - 1;
360 	if (spec->cur_mux[adc_idx] == idx && !force)
361 		return 0;
362 	spec->cur_mux[adc_idx] = idx;
363 
364 	if (spec->shared_mic_hp)
365 		update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
366 
367 	if (spec->dyn_adc_switch) {
368 		alc_dyn_adc_pcm_resetup(codec, idx);
369 		adc_idx = spec->dyn_adc_idx[idx];
370 	}
371 
372 	nid = get_capsrc(spec, adc_idx);
373 
374 	/* no selection? */
375 	num_conns = snd_hda_get_num_conns(codec, nid);
376 	if (num_conns <= 1)
377 		return 1;
378 
379 	type = get_wcaps_type(get_wcaps(codec, nid));
380 	if (type == AC_WID_AUD_MIX) {
381 		/* Matrix-mixer style (e.g. ALC882) */
382 		int active = imux->items[idx].index;
383 		for (i = 0; i < num_conns; i++) {
384 			unsigned int v = (i == active) ? 0 : HDA_AMP_MUTE;
385 			snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, i,
386 						 HDA_AMP_MUTE, v);
387 		}
388 	} else {
389 		/* MUX style (e.g. ALC880) */
390 		snd_hda_codec_write_cache(codec, nid, 0,
391 					  AC_VERB_SET_CONNECT_SEL,
392 					  imux->items[idx].index);
393 	}
394 	alc_inv_dmic_sync(codec, true);
395 	return 1;
396 }
397 
398 static int alc_mux_enum_put(struct snd_kcontrol *kcontrol,
399 			    struct snd_ctl_elem_value *ucontrol)
400 {
401 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
402 	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
403 	return alc_mux_select(codec, adc_idx,
404 			      ucontrol->value.enumerated.item[0], false);
405 }
406 
407 /*
408  * set up the input pin config (depending on the given auto-pin type)
409  */
410 static void alc_set_input_pin(struct hda_codec *codec, hda_nid_t nid,
411 			      int auto_pin_type)
412 {
413 	unsigned int val = PIN_IN;
414 	if (auto_pin_type == AUTO_PIN_MIC)
415 		val |= snd_hda_get_default_vref(codec, nid);
416 	snd_hda_set_pin_ctl(codec, nid, val);
417 }
418 
419 /*
420  * Append the given mixer and verb elements for the later use
421  * The mixer array is referred in build_controls(), and init_verbs are
422  * called in init().
423  */
424 static void add_mixer(struct alc_spec *spec, const struct snd_kcontrol_new *mix)
425 {
426 	if (snd_BUG_ON(spec->num_mixers >= ARRAY_SIZE(spec->mixers)))
427 		return;
428 	spec->mixers[spec->num_mixers++] = mix;
429 }
430 
431 /*
432  * GPIO setup tables, used in initialization
433  */
434 /* Enable GPIO mask and set output */
435 static const struct hda_verb alc_gpio1_init_verbs[] = {
436 	{0x01, AC_VERB_SET_GPIO_MASK, 0x01},
437 	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
438 	{0x01, AC_VERB_SET_GPIO_DATA, 0x01},
439 	{ }
440 };
441 
442 static const struct hda_verb alc_gpio2_init_verbs[] = {
443 	{0x01, AC_VERB_SET_GPIO_MASK, 0x02},
444 	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
445 	{0x01, AC_VERB_SET_GPIO_DATA, 0x02},
446 	{ }
447 };
448 
449 static const struct hda_verb alc_gpio3_init_verbs[] = {
450 	{0x01, AC_VERB_SET_GPIO_MASK, 0x03},
451 	{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
452 	{0x01, AC_VERB_SET_GPIO_DATA, 0x03},
453 	{ }
454 };
455 
456 /*
457  * Fix hardware PLL issue
458  * On some codecs, the analog PLL gating control must be off while
459  * the default value is 1.
460  */
461 static void alc_fix_pll(struct hda_codec *codec)
462 {
463 	struct alc_spec *spec = codec->spec;
464 	unsigned int val;
465 
466 	if (!spec->pll_nid)
467 		return;
468 	snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
469 			    spec->pll_coef_idx);
470 	val = snd_hda_codec_read(codec, spec->pll_nid, 0,
471 				 AC_VERB_GET_PROC_COEF, 0);
472 	snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
473 			    spec->pll_coef_idx);
474 	snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_PROC_COEF,
475 			    val & ~(1 << spec->pll_coef_bit));
476 }
477 
478 static void alc_fix_pll_init(struct hda_codec *codec, hda_nid_t nid,
479 			     unsigned int coef_idx, unsigned int coef_bit)
480 {
481 	struct alc_spec *spec = codec->spec;
482 	spec->pll_nid = nid;
483 	spec->pll_coef_idx = coef_idx;
484 	spec->pll_coef_bit = coef_bit;
485 	alc_fix_pll(codec);
486 }
487 
488 /*
489  * Jack detections for HP auto-mute and mic-switch
490  */
491 
492 /* check each pin in the given array; returns true if any of them is plugged */
493 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
494 {
495 	int i, present = 0;
496 
497 	for (i = 0; i < num_pins; i++) {
498 		hda_nid_t nid = pins[i];
499 		if (!nid)
500 			break;
501 		present |= snd_hda_jack_detect(codec, nid);
502 	}
503 	return present;
504 }
505 
506 /* standard HP/line-out auto-mute helper */
507 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
508 			bool mute, bool hp_out)
509 {
510 	struct alc_spec *spec = codec->spec;
511 	unsigned int mute_bits = mute ? HDA_AMP_MUTE : 0;
512 	unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
513 	int i;
514 
515 	for (i = 0; i < num_pins; i++) {
516 		hda_nid_t nid = pins[i];
517 		unsigned int val;
518 		if (!nid)
519 			break;
520 		switch (spec->automute_mode) {
521 		case ALC_AUTOMUTE_PIN:
522 			/* don't reset VREF value in case it's controlling
523 			 * the amp (see alc861_fixup_asus_amp_vref_0f())
524 			 */
525 			if (spec->keep_vref_in_automute) {
526 				val = snd_hda_codec_read(codec, nid, 0,
527 					AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
528 				val &= ~PIN_HP;
529 			} else
530 				val = 0;
531 			val |= pin_bits;
532 			snd_hda_set_pin_ctl(codec, nid, val);
533 			break;
534 		case ALC_AUTOMUTE_AMP:
535 			snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
536 						 HDA_AMP_MUTE, mute_bits);
537 			break;
538 		case ALC_AUTOMUTE_MIXER:
539 			nid = spec->automute_mixer_nid[i];
540 			if (!nid)
541 				break;
542 			snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
543 						 HDA_AMP_MUTE, mute_bits);
544 			snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 1,
545 						 HDA_AMP_MUTE, mute_bits);
546 			break;
547 		}
548 	}
549 }
550 
551 /* Toggle outputs muting */
552 static void update_outputs(struct hda_codec *codec)
553 {
554 	struct alc_spec *spec = codec->spec;
555 	int on;
556 
557 	/* Control HP pins/amps depending on master_mute state;
558 	 * in general, HP pins/amps control should be enabled in all cases,
559 	 * but currently set only for master_mute, just to be safe
560 	 */
561 	if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
562 		do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
563 		    spec->autocfg.hp_pins, spec->master_mute, true);
564 
565 	if (!spec->automute_speaker)
566 		on = 0;
567 	else
568 		on = spec->hp_jack_present | spec->line_jack_present;
569 	on |= spec->master_mute;
570 	do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
571 		    spec->autocfg.speaker_pins, on, false);
572 
573 	/* toggle line-out mutes if needed, too */
574 	/* if LO is a copy of either HP or Speaker, don't need to handle it */
575 	if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
576 	    spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
577 		return;
578 	if (!spec->automute_lo)
579 		on = 0;
580 	else
581 		on = spec->hp_jack_present;
582 	on |= spec->master_mute;
583 	do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
584 		    spec->autocfg.line_out_pins, on, false);
585 }
586 
587 static void call_update_outputs(struct hda_codec *codec)
588 {
589 	struct alc_spec *spec = codec->spec;
590 	if (spec->automute_hook)
591 		spec->automute_hook(codec);
592 	else
593 		update_outputs(codec);
594 }
595 
596 /* standard HP-automute helper */
597 static void alc_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
598 {
599 	struct alc_spec *spec = codec->spec;
600 
601 	spec->hp_jack_present =
602 		detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
603 			     spec->autocfg.hp_pins);
604 	if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
605 		return;
606 	call_update_outputs(codec);
607 }
608 
609 /* standard line-out-automute helper */
610 static void alc_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
611 {
612 	struct alc_spec *spec = codec->spec;
613 
614 	if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
615 		return;
616 	/* check LO jack only when it's different from HP */
617 	if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
618 		return;
619 
620 	spec->line_jack_present =
621 		detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
622 			     spec->autocfg.line_out_pins);
623 	if (!spec->automute_speaker || !spec->detect_lo)
624 		return;
625 	call_update_outputs(codec);
626 }
627 
628 #define get_connection_index(codec, mux, nid) \
629 	snd_hda_get_conn_index(codec, mux, nid, 0)
630 
631 /* standard mic auto-switch helper */
632 static void alc_mic_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
633 {
634 	struct alc_spec *spec = codec->spec;
635 	hda_nid_t *pins = spec->imux_pins;
636 
637 	if (!spec->auto_mic || !spec->auto_mic_valid_imux)
638 		return;
639 	if (snd_BUG_ON(!spec->adc_nids))
640 		return;
641 	if (snd_BUG_ON(spec->int_mic_idx < 0 || spec->ext_mic_idx < 0))
642 		return;
643 
644 	if (snd_hda_jack_detect(codec, pins[spec->ext_mic_idx]))
645 		alc_mux_select(codec, 0, spec->ext_mic_idx, false);
646 	else if (spec->dock_mic_idx >= 0 &&
647 		   snd_hda_jack_detect(codec, pins[spec->dock_mic_idx]))
648 		alc_mux_select(codec, 0, spec->dock_mic_idx, false);
649 	else
650 		alc_mux_select(codec, 0, spec->int_mic_idx, false);
651 }
652 
653 /* update the master volume per volume-knob's unsol event */
654 static void alc_update_knob_master(struct hda_codec *codec, struct hda_jack_tbl *jack)
655 {
656 	unsigned int val;
657 	struct snd_kcontrol *kctl;
658 	struct snd_ctl_elem_value *uctl;
659 
660 	kctl = snd_hda_find_mixer_ctl(codec, "Master Playback Volume");
661 	if (!kctl)
662 		return;
663 	uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
664 	if (!uctl)
665 		return;
666 	val = snd_hda_codec_read(codec, jack->nid, 0,
667 				 AC_VERB_GET_VOLUME_KNOB_CONTROL, 0);
668 	val &= HDA_AMP_VOLMASK;
669 	uctl->value.integer.value[0] = val;
670 	uctl->value.integer.value[1] = val;
671 	kctl->put(kctl, uctl);
672 	kfree(uctl);
673 }
674 
675 static void alc880_unsol_event(struct hda_codec *codec, unsigned int res)
676 {
677 	/* For some reason, the res given from ALC880 is broken.
678 	   Here we adjust it properly. */
679 	snd_hda_jack_unsol_event(codec, res >> 2);
680 }
681 
682 /* call init functions of standard auto-mute helpers */
683 static void alc_inithook(struct hda_codec *codec)
684 {
685 	alc_hp_automute(codec, NULL);
686 	alc_line_automute(codec, NULL);
687 	alc_mic_automute(codec, NULL);
688 }
689 
690 /* additional initialization for ALC888 variants */
691 static void alc888_coef_init(struct hda_codec *codec)
692 {
693 	unsigned int tmp;
694 
695 	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 0);
696 	tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
697 	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
698 	if ((tmp & 0xf0) == 0x20)
699 		/* alc888S-VC */
700 		snd_hda_codec_read(codec, 0x20, 0,
701 				   AC_VERB_SET_PROC_COEF, 0x830);
702 	 else
703 		 /* alc888-VB */
704 		 snd_hda_codec_read(codec, 0x20, 0,
705 				    AC_VERB_SET_PROC_COEF, 0x3030);
706 }
707 
708 /* additional initialization for ALC889 variants */
709 static void alc889_coef_init(struct hda_codec *codec)
710 {
711 	unsigned int tmp;
712 
713 	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
714 	tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
715 	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
716 	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF, tmp|0x2010);
717 }
718 
719 /* turn on/off EAPD control (only if available) */
720 static void set_eapd(struct hda_codec *codec, hda_nid_t nid, int on)
721 {
722 	if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
723 		return;
724 	if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
725 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
726 				    on ? 2 : 0);
727 }
728 
729 /* turn on/off EAPD controls of the codec */
730 static void alc_auto_setup_eapd(struct hda_codec *codec, bool on)
731 {
732 	/* We currently only handle front, HP */
733 	static hda_nid_t pins[] = {
734 		0x0f, 0x10, 0x14, 0x15, 0
735 	};
736 	hda_nid_t *p;
737 	for (p = pins; *p; p++)
738 		set_eapd(codec, *p, on);
739 }
740 
741 /* generic shutup callback;
742  * just turning off EPAD and a little pause for avoiding pop-noise
743  */
744 static void alc_eapd_shutup(struct hda_codec *codec)
745 {
746 	alc_auto_setup_eapd(codec, false);
747 	msleep(200);
748 }
749 
750 /* generic EAPD initialization */
751 static void alc_auto_init_amp(struct hda_codec *codec, int type)
752 {
753 	unsigned int tmp;
754 
755 	alc_auto_setup_eapd(codec, true);
756 	switch (type) {
757 	case ALC_INIT_GPIO1:
758 		snd_hda_sequence_write(codec, alc_gpio1_init_verbs);
759 		break;
760 	case ALC_INIT_GPIO2:
761 		snd_hda_sequence_write(codec, alc_gpio2_init_verbs);
762 		break;
763 	case ALC_INIT_GPIO3:
764 		snd_hda_sequence_write(codec, alc_gpio3_init_verbs);
765 		break;
766 	case ALC_INIT_DEFAULT:
767 		switch (codec->vendor_id) {
768 		case 0x10ec0260:
769 			snd_hda_codec_write(codec, 0x1a, 0,
770 					    AC_VERB_SET_COEF_INDEX, 7);
771 			tmp = snd_hda_codec_read(codec, 0x1a, 0,
772 						 AC_VERB_GET_PROC_COEF, 0);
773 			snd_hda_codec_write(codec, 0x1a, 0,
774 					    AC_VERB_SET_COEF_INDEX, 7);
775 			snd_hda_codec_write(codec, 0x1a, 0,
776 					    AC_VERB_SET_PROC_COEF,
777 					    tmp | 0x2010);
778 			break;
779 		case 0x10ec0262:
780 		case 0x10ec0880:
781 		case 0x10ec0882:
782 		case 0x10ec0883:
783 		case 0x10ec0885:
784 		case 0x10ec0887:
785 		/*case 0x10ec0889:*/ /* this causes an SPDIF problem */
786 			alc889_coef_init(codec);
787 			break;
788 		case 0x10ec0888:
789 			alc888_coef_init(codec);
790 			break;
791 #if 0 /* XXX: This may cause the silent output on speaker on some machines */
792 		case 0x10ec0267:
793 		case 0x10ec0268:
794 			snd_hda_codec_write(codec, 0x20, 0,
795 					    AC_VERB_SET_COEF_INDEX, 7);
796 			tmp = snd_hda_codec_read(codec, 0x20, 0,
797 						 AC_VERB_GET_PROC_COEF, 0);
798 			snd_hda_codec_write(codec, 0x20, 0,
799 					    AC_VERB_SET_COEF_INDEX, 7);
800 			snd_hda_codec_write(codec, 0x20, 0,
801 					    AC_VERB_SET_PROC_COEF,
802 					    tmp | 0x3000);
803 			break;
804 #endif /* XXX */
805 		}
806 		break;
807 	}
808 }
809 
810 /*
811  * Auto-Mute mode mixer enum support
812  */
813 static int alc_automute_mode_info(struct snd_kcontrol *kcontrol,
814 				  struct snd_ctl_elem_info *uinfo)
815 {
816 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
817 	struct alc_spec *spec = codec->spec;
818 	static const char * const texts2[] = {
819 		"Disabled", "Enabled"
820 	};
821 	static const char * const texts3[] = {
822 		"Disabled", "Speaker Only", "Line Out+Speaker"
823 	};
824 	const char * const *texts;
825 
826 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
827 	uinfo->count = 1;
828 	if (spec->automute_speaker_possible && spec->automute_lo_possible) {
829 		uinfo->value.enumerated.items = 3;
830 		texts = texts3;
831 	} else {
832 		uinfo->value.enumerated.items = 2;
833 		texts = texts2;
834 	}
835 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
836 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
837 	strcpy(uinfo->value.enumerated.name,
838 	       texts[uinfo->value.enumerated.item]);
839 	return 0;
840 }
841 
842 static int alc_automute_mode_get(struct snd_kcontrol *kcontrol,
843 				 struct snd_ctl_elem_value *ucontrol)
844 {
845 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
846 	struct alc_spec *spec = codec->spec;
847 	unsigned int val = 0;
848 	if (spec->automute_speaker)
849 		val++;
850 	if (spec->automute_lo)
851 		val++;
852 
853 	ucontrol->value.enumerated.item[0] = val;
854 	return 0;
855 }
856 
857 static int alc_automute_mode_put(struct snd_kcontrol *kcontrol,
858 				 struct snd_ctl_elem_value *ucontrol)
859 {
860 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
861 	struct alc_spec *spec = codec->spec;
862 
863 	switch (ucontrol->value.enumerated.item[0]) {
864 	case 0:
865 		if (!spec->automute_speaker && !spec->automute_lo)
866 			return 0;
867 		spec->automute_speaker = 0;
868 		spec->automute_lo = 0;
869 		break;
870 	case 1:
871 		if (spec->automute_speaker_possible) {
872 			if (!spec->automute_lo && spec->automute_speaker)
873 				return 0;
874 			spec->automute_speaker = 1;
875 			spec->automute_lo = 0;
876 		} else if (spec->automute_lo_possible) {
877 			if (spec->automute_lo)
878 				return 0;
879 			spec->automute_lo = 1;
880 		} else
881 			return -EINVAL;
882 		break;
883 	case 2:
884 		if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
885 			return -EINVAL;
886 		if (spec->automute_speaker && spec->automute_lo)
887 			return 0;
888 		spec->automute_speaker = 1;
889 		spec->automute_lo = 1;
890 		break;
891 	default:
892 		return -EINVAL;
893 	}
894 	call_update_outputs(codec);
895 	return 1;
896 }
897 
898 static const struct snd_kcontrol_new alc_automute_mode_enum = {
899 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
900 	.name = "Auto-Mute Mode",
901 	.info = alc_automute_mode_info,
902 	.get = alc_automute_mode_get,
903 	.put = alc_automute_mode_put,
904 };
905 
906 static struct snd_kcontrol_new *alc_kcontrol_new(struct alc_spec *spec)
907 {
908 	snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
909 	return snd_array_new(&spec->kctls);
910 }
911 
912 static int alc_add_automute_mode_enum(struct hda_codec *codec)
913 {
914 	struct alc_spec *spec = codec->spec;
915 	struct snd_kcontrol_new *knew;
916 
917 	knew = alc_kcontrol_new(spec);
918 	if (!knew)
919 		return -ENOMEM;
920 	*knew = alc_automute_mode_enum;
921 	knew->name = kstrdup("Auto-Mute Mode", GFP_KERNEL);
922 	if (!knew->name)
923 		return -ENOMEM;
924 	return 0;
925 }
926 
927 /*
928  * Check the availability of HP/line-out auto-mute;
929  * Set up appropriately if really supported
930  */
931 static void alc_init_automute(struct hda_codec *codec)
932 {
933 	struct alc_spec *spec = codec->spec;
934 	struct auto_pin_cfg *cfg = &spec->autocfg;
935 	int present = 0;
936 	int i;
937 
938 	if (cfg->hp_pins[0])
939 		present++;
940 	if (cfg->line_out_pins[0])
941 		present++;
942 	if (cfg->speaker_pins[0])
943 		present++;
944 	if (present < 2) /* need two different output types */
945 		return;
946 
947 	if (!cfg->speaker_pins[0] &&
948 	    cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
949 		memcpy(cfg->speaker_pins, cfg->line_out_pins,
950 		       sizeof(cfg->speaker_pins));
951 		cfg->speaker_outs = cfg->line_outs;
952 	}
953 
954 	if (!cfg->hp_pins[0] &&
955 	    cfg->line_out_type == AUTO_PIN_HP_OUT) {
956 		memcpy(cfg->hp_pins, cfg->line_out_pins,
957 		       sizeof(cfg->hp_pins));
958 		cfg->hp_outs = cfg->line_outs;
959 	}
960 
961 	spec->automute_mode = ALC_AUTOMUTE_PIN;
962 
963 	for (i = 0; i < cfg->hp_outs; i++) {
964 		hda_nid_t nid = cfg->hp_pins[i];
965 		if (!is_jack_detectable(codec, nid))
966 			continue;
967 		snd_printdd("realtek: Enable HP auto-muting on NID 0x%x\n",
968 			    nid);
969 		snd_hda_jack_detect_enable_callback(codec, nid, ALC_HP_EVENT,
970 						    alc_hp_automute);
971 		spec->detect_hp = 1;
972 	}
973 
974 	if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
975 		if (cfg->speaker_outs)
976 			for (i = 0; i < cfg->line_outs; i++) {
977 				hda_nid_t nid = cfg->line_out_pins[i];
978 				if (!is_jack_detectable(codec, nid))
979 					continue;
980 				snd_printdd("realtek: Enable Line-Out "
981 					    "auto-muting on NID 0x%x\n", nid);
982 				snd_hda_jack_detect_enable_callback(codec, nid, ALC_FRONT_EVENT,
983 								    alc_line_automute);
984 				spec->detect_lo = 1;
985 			}
986 		spec->automute_lo_possible = spec->detect_hp;
987 	}
988 
989 	spec->automute_speaker_possible = cfg->speaker_outs &&
990 		(spec->detect_hp || spec->detect_lo);
991 
992 	spec->automute_lo = spec->automute_lo_possible;
993 	spec->automute_speaker = spec->automute_speaker_possible;
994 
995 	if (spec->automute_speaker_possible || spec->automute_lo_possible)
996 		/* create a control for automute mode */
997 		alc_add_automute_mode_enum(codec);
998 }
999 
1000 /* return the position of NID in the list, or -1 if not found */
1001 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
1002 {
1003 	int i;
1004 	for (i = 0; i < nums; i++)
1005 		if (list[i] == nid)
1006 			return i;
1007 	return -1;
1008 }
1009 
1010 /* check whether dynamic ADC-switching is available */
1011 static bool alc_check_dyn_adc_switch(struct hda_codec *codec)
1012 {
1013 	struct alc_spec *spec = codec->spec;
1014 	struct hda_input_mux *imux = &spec->private_imux[0];
1015 	int i, n, idx;
1016 	hda_nid_t cap, pin;
1017 
1018 	if (imux != spec->input_mux) /* no dynamic imux? */
1019 		return false;
1020 
1021 	for (n = 0; n < spec->num_adc_nids; n++) {
1022 		cap = spec->private_capsrc_nids[n];
1023 		for (i = 0; i < imux->num_items; i++) {
1024 			pin = spec->imux_pins[i];
1025 			if (!pin)
1026 				return false;
1027 			if (get_connection_index(codec, cap, pin) < 0)
1028 				break;
1029 		}
1030 		if (i >= imux->num_items)
1031 			return true; /* no ADC-switch is needed */
1032 	}
1033 
1034 	for (i = 0; i < imux->num_items; i++) {
1035 		pin = spec->imux_pins[i];
1036 		for (n = 0; n < spec->num_adc_nids; n++) {
1037 			cap = spec->private_capsrc_nids[n];
1038 			idx = get_connection_index(codec, cap, pin);
1039 			if (idx >= 0) {
1040 				imux->items[i].index = idx;
1041 				spec->dyn_adc_idx[i] = n;
1042 				break;
1043 			}
1044 		}
1045 	}
1046 
1047 	snd_printdd("realtek: enabling ADC switching\n");
1048 	spec->dyn_adc_switch = 1;
1049 	return true;
1050 }
1051 
1052 /* check whether all auto-mic pins are valid; setup indices if OK */
1053 static bool alc_auto_mic_check_imux(struct hda_codec *codec)
1054 {
1055 	struct alc_spec *spec = codec->spec;
1056 	const struct hda_input_mux *imux;
1057 
1058 	if (!spec->auto_mic)
1059 		return false;
1060 	if (spec->auto_mic_valid_imux)
1061 		return true; /* already checked */
1062 
1063 	/* fill up imux indices */
1064 	if (!alc_check_dyn_adc_switch(codec)) {
1065 		spec->auto_mic = 0;
1066 		return false;
1067 	}
1068 
1069 	imux = spec->input_mux;
1070 	spec->ext_mic_idx = find_idx_in_nid_list(spec->ext_mic_pin,
1071 					spec->imux_pins, imux->num_items);
1072 	spec->int_mic_idx = find_idx_in_nid_list(spec->int_mic_pin,
1073 					spec->imux_pins, imux->num_items);
1074 	spec->dock_mic_idx = find_idx_in_nid_list(spec->dock_mic_pin,
1075 					spec->imux_pins, imux->num_items);
1076 	if (spec->ext_mic_idx < 0 || spec->int_mic_idx < 0) {
1077 		spec->auto_mic = 0;
1078 		return false; /* no corresponding imux */
1079 	}
1080 
1081 	snd_hda_jack_detect_enable_callback(codec, spec->ext_mic_pin,
1082 					    ALC_MIC_EVENT, alc_mic_automute);
1083 	if (spec->dock_mic_pin)
1084 		snd_hda_jack_detect_enable_callback(codec, spec->dock_mic_pin,
1085 						    ALC_MIC_EVENT,
1086 						    alc_mic_automute);
1087 
1088 	spec->auto_mic_valid_imux = 1;
1089 	spec->auto_mic = 1;
1090 	return true;
1091 }
1092 
1093 /*
1094  * Check the availability of auto-mic switch;
1095  * Set up if really supported
1096  */
1097 static void alc_init_auto_mic(struct hda_codec *codec)
1098 {
1099 	struct alc_spec *spec = codec->spec;
1100 	struct auto_pin_cfg *cfg = &spec->autocfg;
1101 	hda_nid_t fixed, ext, dock;
1102 	int i;
1103 
1104 	if (spec->shared_mic_hp)
1105 		return; /* no auto-mic for the shared I/O */
1106 
1107 	spec->ext_mic_idx = spec->int_mic_idx = spec->dock_mic_idx = -1;
1108 
1109 	fixed = ext = dock = 0;
1110 	for (i = 0; i < cfg->num_inputs; i++) {
1111 		hda_nid_t nid = cfg->inputs[i].pin;
1112 		unsigned int defcfg;
1113 		defcfg = snd_hda_codec_get_pincfg(codec, nid);
1114 		switch (snd_hda_get_input_pin_attr(defcfg)) {
1115 		case INPUT_PIN_ATTR_INT:
1116 			if (fixed)
1117 				return; /* already occupied */
1118 			if (cfg->inputs[i].type != AUTO_PIN_MIC)
1119 				return; /* invalid type */
1120 			fixed = nid;
1121 			break;
1122 		case INPUT_PIN_ATTR_UNUSED:
1123 			return; /* invalid entry */
1124 		case INPUT_PIN_ATTR_DOCK:
1125 			if (dock)
1126 				return; /* already occupied */
1127 			if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
1128 				return; /* invalid type */
1129 			dock = nid;
1130 			break;
1131 		default:
1132 			if (ext)
1133 				return; /* already occupied */
1134 			if (cfg->inputs[i].type != AUTO_PIN_MIC)
1135 				return; /* invalid type */
1136 			ext = nid;
1137 			break;
1138 		}
1139 	}
1140 	if (!ext && dock) {
1141 		ext = dock;
1142 		dock = 0;
1143 	}
1144 	if (!ext || !fixed)
1145 		return;
1146 	if (!is_jack_detectable(codec, ext))
1147 		return; /* no unsol support */
1148 	if (dock && !is_jack_detectable(codec, dock))
1149 		return; /* no unsol support */
1150 
1151 	/* check imux indices */
1152 	spec->ext_mic_pin = ext;
1153 	spec->int_mic_pin = fixed;
1154 	spec->dock_mic_pin = dock;
1155 
1156 	spec->auto_mic = 1;
1157 	if (!alc_auto_mic_check_imux(codec))
1158 		return;
1159 
1160 	snd_printdd("realtek: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
1161 		    ext, fixed, dock);
1162 }
1163 
1164 /* check the availabilities of auto-mute and auto-mic switches */
1165 static void alc_auto_check_switches(struct hda_codec *codec)
1166 {
1167 	alc_init_automute(codec);
1168 	alc_init_auto_mic(codec);
1169 }
1170 
1171 /*
1172  * Realtek SSID verification
1173  */
1174 
1175 /* Could be any non-zero and even value. When used as fixup, tells
1176  * the driver to ignore any present sku defines.
1177  */
1178 #define ALC_FIXUP_SKU_IGNORE (2)
1179 
1180 static void alc_fixup_sku_ignore(struct hda_codec *codec,
1181 				 const struct hda_fixup *fix, int action)
1182 {
1183 	struct alc_spec *spec = codec->spec;
1184 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1185 		spec->cdefine.fixup = 1;
1186 		spec->cdefine.sku_cfg = ALC_FIXUP_SKU_IGNORE;
1187 	}
1188 }
1189 
1190 static int alc_auto_parse_customize_define(struct hda_codec *codec)
1191 {
1192 	unsigned int ass, tmp, i;
1193 	unsigned nid = 0;
1194 	struct alc_spec *spec = codec->spec;
1195 
1196 	spec->cdefine.enable_pcbeep = 1; /* assume always enabled */
1197 
1198 	if (spec->cdefine.fixup) {
1199 		ass = spec->cdefine.sku_cfg;
1200 		if (ass == ALC_FIXUP_SKU_IGNORE)
1201 			return -1;
1202 		goto do_sku;
1203 	}
1204 
1205 	ass = codec->subsystem_id & 0xffff;
1206 	if (ass != codec->bus->pci->subsystem_device && (ass & 1))
1207 		goto do_sku;
1208 
1209 	nid = 0x1d;
1210 	if (codec->vendor_id == 0x10ec0260)
1211 		nid = 0x17;
1212 	ass = snd_hda_codec_get_pincfg(codec, nid);
1213 
1214 	if (!(ass & 1)) {
1215 		printk(KERN_INFO "hda_codec: %s: SKU not ready 0x%08x\n",
1216 		       codec->chip_name, ass);
1217 		return -1;
1218 	}
1219 
1220 	/* check sum */
1221 	tmp = 0;
1222 	for (i = 1; i < 16; i++) {
1223 		if ((ass >> i) & 1)
1224 			tmp++;
1225 	}
1226 	if (((ass >> 16) & 0xf) != tmp)
1227 		return -1;
1228 
1229 	spec->cdefine.port_connectivity = ass >> 30;
1230 	spec->cdefine.enable_pcbeep = (ass & 0x100000) >> 20;
1231 	spec->cdefine.check_sum = (ass >> 16) & 0xf;
1232 	spec->cdefine.customization = ass >> 8;
1233 do_sku:
1234 	spec->cdefine.sku_cfg = ass;
1235 	spec->cdefine.external_amp = (ass & 0x38) >> 3;
1236 	spec->cdefine.platform_type = (ass & 0x4) >> 2;
1237 	spec->cdefine.swap = (ass & 0x2) >> 1;
1238 	spec->cdefine.override = ass & 0x1;
1239 
1240 	snd_printd("SKU: Nid=0x%x sku_cfg=0x%08x\n",
1241 		   nid, spec->cdefine.sku_cfg);
1242 	snd_printd("SKU: port_connectivity=0x%x\n",
1243 		   spec->cdefine.port_connectivity);
1244 	snd_printd("SKU: enable_pcbeep=0x%x\n", spec->cdefine.enable_pcbeep);
1245 	snd_printd("SKU: check_sum=0x%08x\n", spec->cdefine.check_sum);
1246 	snd_printd("SKU: customization=0x%08x\n", spec->cdefine.customization);
1247 	snd_printd("SKU: external_amp=0x%x\n", spec->cdefine.external_amp);
1248 	snd_printd("SKU: platform_type=0x%x\n", spec->cdefine.platform_type);
1249 	snd_printd("SKU: swap=0x%x\n", spec->cdefine.swap);
1250 	snd_printd("SKU: override=0x%x\n", spec->cdefine.override);
1251 
1252 	return 0;
1253 }
1254 
1255 /* return true if the given NID is found in the list */
1256 static bool found_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
1257 {
1258 	return find_idx_in_nid_list(nid, list, nums) >= 0;
1259 }
1260 
1261 /* check subsystem ID and set up device-specific initialization;
1262  * return 1 if initialized, 0 if invalid SSID
1263  */
1264 /* 32-bit subsystem ID for BIOS loading in HD Audio codec.
1265  *	31 ~ 16 :	Manufacture ID
1266  *	15 ~ 8	:	SKU ID
1267  *	7  ~ 0	:	Assembly ID
1268  *	port-A --> pin 39/41, port-E --> pin 14/15, port-D --> pin 35/36
1269  */
1270 static int alc_subsystem_id(struct hda_codec *codec,
1271 			    hda_nid_t porta, hda_nid_t porte,
1272 			    hda_nid_t portd, hda_nid_t porti)
1273 {
1274 	unsigned int ass, tmp, i;
1275 	unsigned nid;
1276 	struct alc_spec *spec = codec->spec;
1277 
1278 	if (spec->cdefine.fixup) {
1279 		ass = spec->cdefine.sku_cfg;
1280 		if (ass == ALC_FIXUP_SKU_IGNORE)
1281 			return 0;
1282 		goto do_sku;
1283 	}
1284 
1285 	ass = codec->subsystem_id & 0xffff;
1286 	if ((ass != codec->bus->pci->subsystem_device) && (ass & 1))
1287 		goto do_sku;
1288 
1289 	/* invalid SSID, check the special NID pin defcfg instead */
1290 	/*
1291 	 * 31~30	: port connectivity
1292 	 * 29~21	: reserve
1293 	 * 20		: PCBEEP input
1294 	 * 19~16	: Check sum (15:1)
1295 	 * 15~1		: Custom
1296 	 * 0		: override
1297 	*/
1298 	nid = 0x1d;
1299 	if (codec->vendor_id == 0x10ec0260)
1300 		nid = 0x17;
1301 	ass = snd_hda_codec_get_pincfg(codec, nid);
1302 	snd_printd("realtek: No valid SSID, "
1303 		   "checking pincfg 0x%08x for NID 0x%x\n",
1304 		   ass, nid);
1305 	if (!(ass & 1))
1306 		return 0;
1307 	if ((ass >> 30) != 1)	/* no physical connection */
1308 		return 0;
1309 
1310 	/* check sum */
1311 	tmp = 0;
1312 	for (i = 1; i < 16; i++) {
1313 		if ((ass >> i) & 1)
1314 			tmp++;
1315 	}
1316 	if (((ass >> 16) & 0xf) != tmp)
1317 		return 0;
1318 do_sku:
1319 	snd_printd("realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
1320 		   ass & 0xffff, codec->vendor_id);
1321 	/*
1322 	 * 0 : override
1323 	 * 1 :	Swap Jack
1324 	 * 2 : 0 --> Desktop, 1 --> Laptop
1325 	 * 3~5 : External Amplifier control
1326 	 * 7~6 : Reserved
1327 	*/
1328 	tmp = (ass & 0x38) >> 3;	/* external Amp control */
1329 	switch (tmp) {
1330 	case 1:
1331 		spec->init_amp = ALC_INIT_GPIO1;
1332 		break;
1333 	case 3:
1334 		spec->init_amp = ALC_INIT_GPIO2;
1335 		break;
1336 	case 7:
1337 		spec->init_amp = ALC_INIT_GPIO3;
1338 		break;
1339 	case 5:
1340 	default:
1341 		spec->init_amp = ALC_INIT_DEFAULT;
1342 		break;
1343 	}
1344 
1345 	/* is laptop or Desktop and enable the function "Mute internal speaker
1346 	 * when the external headphone out jack is plugged"
1347 	 */
1348 	if (!(ass & 0x8000))
1349 		return 1;
1350 	/*
1351 	 * 10~8 : Jack location
1352 	 * 12~11: Headphone out -> 00: PortA, 01: PortE, 02: PortD, 03: Resvered
1353 	 * 14~13: Resvered
1354 	 * 15   : 1 --> enable the function "Mute internal speaker
1355 	 *	        when the external headphone out jack is plugged"
1356 	 */
1357 	if (!spec->autocfg.hp_pins[0] &&
1358 	    !(spec->autocfg.line_out_pins[0] &&
1359 	      spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)) {
1360 		hda_nid_t nid;
1361 		tmp = (ass >> 11) & 0x3;	/* HP to chassis */
1362 		if (tmp == 0)
1363 			nid = porta;
1364 		else if (tmp == 1)
1365 			nid = porte;
1366 		else if (tmp == 2)
1367 			nid = portd;
1368 		else if (tmp == 3)
1369 			nid = porti;
1370 		else
1371 			return 1;
1372 		if (found_in_nid_list(nid, spec->autocfg.line_out_pins,
1373 				      spec->autocfg.line_outs))
1374 			return 1;
1375 		spec->autocfg.hp_pins[0] = nid;
1376 	}
1377 	return 1;
1378 }
1379 
1380 /* Check the validity of ALC subsystem-id
1381  * ports contains an array of 4 pin NIDs for port-A, E, D and I */
1382 static void alc_ssid_check(struct hda_codec *codec, const hda_nid_t *ports)
1383 {
1384 	if (!alc_subsystem_id(codec, ports[0], ports[1], ports[2], ports[3])) {
1385 		struct alc_spec *spec = codec->spec;
1386 		snd_printd("realtek: "
1387 			   "Enable default setup for auto mode as fallback\n");
1388 		spec->init_amp = ALC_INIT_DEFAULT;
1389 	}
1390 }
1391 
1392 /*
1393  * COEF access helper functions
1394  */
1395 static int alc_read_coef_idx(struct hda_codec *codec,
1396 			unsigned int coef_idx)
1397 {
1398 	unsigned int val;
1399 	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
1400 		    		coef_idx);
1401 	val = snd_hda_codec_read(codec, 0x20, 0,
1402 			 	AC_VERB_GET_PROC_COEF, 0);
1403 	return val;
1404 }
1405 
1406 static void alc_write_coef_idx(struct hda_codec *codec, unsigned int coef_idx,
1407 							unsigned int coef_val)
1408 {
1409 	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
1410 			    coef_idx);
1411 	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF,
1412 			    coef_val);
1413 }
1414 
1415 /* a special bypass for COEF 0; read the cached value at the second time */
1416 static unsigned int alc_get_coef0(struct hda_codec *codec)
1417 {
1418 	struct alc_spec *spec = codec->spec;
1419 	if (!spec->coef0)
1420 		spec->coef0 = alc_read_coef_idx(codec, 0);
1421 	return spec->coef0;
1422 }
1423 
1424 /*
1425  * Digital I/O handling
1426  */
1427 
1428 /* set right pin controls for digital I/O */
1429 static void alc_auto_init_digital(struct hda_codec *codec)
1430 {
1431 	struct alc_spec *spec = codec->spec;
1432 	int i;
1433 	hda_nid_t pin, dac;
1434 
1435 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
1436 		pin = spec->autocfg.dig_out_pins[i];
1437 		if (!pin)
1438 			continue;
1439 		snd_hda_set_pin_ctl(codec, pin, PIN_OUT);
1440 		if (!i)
1441 			dac = spec->multiout.dig_out_nid;
1442 		else
1443 			dac = spec->slave_dig_outs[i - 1];
1444 		if (!dac || !(get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
1445 			continue;
1446 		snd_hda_codec_write(codec, dac, 0,
1447 				    AC_VERB_SET_AMP_GAIN_MUTE,
1448 				    AMP_OUT_UNMUTE);
1449 	}
1450 	pin = spec->autocfg.dig_in_pin;
1451 	if (pin)
1452 		snd_hda_set_pin_ctl(codec, pin, PIN_IN);
1453 }
1454 
1455 /* parse digital I/Os and set up NIDs in BIOS auto-parse mode */
1456 static void alc_auto_parse_digital(struct hda_codec *codec)
1457 {
1458 	struct alc_spec *spec = codec->spec;
1459 	int i, err, nums;
1460 	hda_nid_t dig_nid;
1461 
1462 	/* support multiple SPDIFs; the secondary is set up as a slave */
1463 	nums = 0;
1464 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
1465 		hda_nid_t conn[4];
1466 		err = snd_hda_get_connections(codec,
1467 					      spec->autocfg.dig_out_pins[i],
1468 					      conn, ARRAY_SIZE(conn));
1469 		if (err <= 0)
1470 			continue;
1471 		dig_nid = conn[0]; /* assume the first element is audio-out */
1472 		if (!nums) {
1473 			spec->multiout.dig_out_nid = dig_nid;
1474 			spec->dig_out_type = spec->autocfg.dig_out_type[0];
1475 		} else {
1476 			spec->multiout.slave_dig_outs = spec->slave_dig_outs;
1477 			if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
1478 				break;
1479 			spec->slave_dig_outs[nums - 1] = dig_nid;
1480 		}
1481 		nums++;
1482 	}
1483 
1484 	if (spec->autocfg.dig_in_pin) {
1485 		dig_nid = codec->start_nid;
1486 		for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
1487 			unsigned int wcaps = get_wcaps(codec, dig_nid);
1488 			if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
1489 				continue;
1490 			if (!(wcaps & AC_WCAP_DIGITAL))
1491 				continue;
1492 			if (!(wcaps & AC_WCAP_CONN_LIST))
1493 				continue;
1494 			err = get_connection_index(codec, dig_nid,
1495 						   spec->autocfg.dig_in_pin);
1496 			if (err >= 0) {
1497 				spec->dig_in_nid = dig_nid;
1498 				break;
1499 			}
1500 		}
1501 	}
1502 }
1503 
1504 /*
1505  * capture mixer elements
1506  */
1507 static int alc_cap_vol_info(struct snd_kcontrol *kcontrol,
1508 			    struct snd_ctl_elem_info *uinfo)
1509 {
1510 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1511 	struct alc_spec *spec = codec->spec;
1512 	unsigned long val;
1513 	int err;
1514 
1515 	mutex_lock(&codec->control_mutex);
1516 	if (spec->vol_in_capsrc)
1517 		val = HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[0], 3, 0, HDA_OUTPUT);
1518 	else
1519 		val = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0, HDA_INPUT);
1520 	kcontrol->private_value = val;
1521 	err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
1522 	mutex_unlock(&codec->control_mutex);
1523 	return err;
1524 }
1525 
1526 static int alc_cap_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1527 			   unsigned int size, unsigned int __user *tlv)
1528 {
1529 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1530 	struct alc_spec *spec = codec->spec;
1531 	unsigned long val;
1532 	int err;
1533 
1534 	mutex_lock(&codec->control_mutex);
1535 	if (spec->vol_in_capsrc)
1536 		val = HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[0], 3, 0, HDA_OUTPUT);
1537 	else
1538 		val = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0, HDA_INPUT);
1539 	kcontrol->private_value = val;
1540 	err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
1541 	mutex_unlock(&codec->control_mutex);
1542 	return err;
1543 }
1544 
1545 typedef int (*getput_call_t)(struct snd_kcontrol *kcontrol,
1546 			     struct snd_ctl_elem_value *ucontrol);
1547 
1548 static int alc_cap_getput_caller(struct snd_kcontrol *kcontrol,
1549 				 struct snd_ctl_elem_value *ucontrol,
1550 				 getput_call_t func, bool is_put)
1551 {
1552 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1553 	struct alc_spec *spec = codec->spec;
1554 	int i, err = 0;
1555 
1556 	mutex_lock(&codec->control_mutex);
1557 	if (is_put && spec->dyn_adc_switch) {
1558 		for (i = 0; i < spec->num_adc_nids; i++) {
1559 			kcontrol->private_value =
1560 				HDA_COMPOSE_AMP_VAL(spec->adc_nids[i],
1561 						    3, 0, HDA_INPUT);
1562 			err = func(kcontrol, ucontrol);
1563 			if (err < 0)
1564 				goto error;
1565 		}
1566 	} else {
1567 		i = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1568 		if (spec->vol_in_capsrc)
1569 			kcontrol->private_value =
1570 				HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[i],
1571 						    3, 0, HDA_OUTPUT);
1572 		else
1573 			kcontrol->private_value =
1574 				HDA_COMPOSE_AMP_VAL(spec->adc_nids[i],
1575 						    3, 0, HDA_INPUT);
1576 		err = func(kcontrol, ucontrol);
1577 	}
1578 	if (err >= 0 && is_put)
1579 		alc_inv_dmic_sync(codec, false);
1580  error:
1581 	mutex_unlock(&codec->control_mutex);
1582 	return err;
1583 }
1584 
1585 static int alc_cap_vol_get(struct snd_kcontrol *kcontrol,
1586 			   struct snd_ctl_elem_value *ucontrol)
1587 {
1588 	return alc_cap_getput_caller(kcontrol, ucontrol,
1589 				     snd_hda_mixer_amp_volume_get, false);
1590 }
1591 
1592 static int alc_cap_vol_put(struct snd_kcontrol *kcontrol,
1593 			   struct snd_ctl_elem_value *ucontrol)
1594 {
1595 	return alc_cap_getput_caller(kcontrol, ucontrol,
1596 				     snd_hda_mixer_amp_volume_put, true);
1597 }
1598 
1599 /* capture mixer elements */
1600 #define alc_cap_sw_info		snd_ctl_boolean_stereo_info
1601 
1602 static int alc_cap_sw_get(struct snd_kcontrol *kcontrol,
1603 			  struct snd_ctl_elem_value *ucontrol)
1604 {
1605 	return alc_cap_getput_caller(kcontrol, ucontrol,
1606 				     snd_hda_mixer_amp_switch_get, false);
1607 }
1608 
1609 static int alc_cap_sw_put(struct snd_kcontrol *kcontrol,
1610 			  struct snd_ctl_elem_value *ucontrol)
1611 {
1612 	return alc_cap_getput_caller(kcontrol, ucontrol,
1613 				     snd_hda_mixer_amp_switch_put, true);
1614 }
1615 
1616 #define _DEFINE_CAPMIX(num) \
1617 	{ \
1618 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1619 		.name = "Capture Switch", \
1620 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
1621 		.count = num, \
1622 		.info = alc_cap_sw_info, \
1623 		.get = alc_cap_sw_get, \
1624 		.put = alc_cap_sw_put, \
1625 	}, \
1626 	{ \
1627 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1628 		.name = "Capture Volume", \
1629 		.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | \
1630 			   SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
1631 			   SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK), \
1632 		.count = num, \
1633 		.info = alc_cap_vol_info, \
1634 		.get = alc_cap_vol_get, \
1635 		.put = alc_cap_vol_put, \
1636 		.tlv = { .c = alc_cap_vol_tlv }, \
1637 	}
1638 
1639 #define _DEFINE_CAPSRC(num) \
1640 	{ \
1641 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1642 		/* .name = "Capture Source", */ \
1643 		.name = "Input Source", \
1644 		.count = num, \
1645 		.info = alc_mux_enum_info, \
1646 		.get = alc_mux_enum_get, \
1647 		.put = alc_mux_enum_put, \
1648 	}
1649 
1650 #define DEFINE_CAPMIX(num) \
1651 static const struct snd_kcontrol_new alc_capture_mixer ## num[] = { \
1652 	_DEFINE_CAPMIX(num),				      \
1653 	_DEFINE_CAPSRC(num),				      \
1654 	{ } /* end */					      \
1655 }
1656 
1657 #define DEFINE_CAPMIX_NOSRC(num) \
1658 static const struct snd_kcontrol_new alc_capture_mixer_nosrc ## num[] = { \
1659 	_DEFINE_CAPMIX(num),					    \
1660 	{ } /* end */						    \
1661 }
1662 
1663 /* up to three ADCs */
1664 DEFINE_CAPMIX(1);
1665 DEFINE_CAPMIX(2);
1666 DEFINE_CAPMIX(3);
1667 DEFINE_CAPMIX_NOSRC(1);
1668 DEFINE_CAPMIX_NOSRC(2);
1669 DEFINE_CAPMIX_NOSRC(3);
1670 
1671 /*
1672  * Inverted digital-mic handling
1673  *
1674  * First off, it's a bit tricky.  The "Inverted Internal Mic Capture Switch"
1675  * gives the additional mute only to the right channel of the digital mic
1676  * capture stream.  This is a workaround for avoiding the almost silence
1677  * by summing the stereo stream from some (known to be ForteMedia)
1678  * digital mic unit.
1679  *
1680  * The logic is to call alc_inv_dmic_sync() after each action (possibly)
1681  * modifying ADC amp.  When the mute flag is set, it mutes the R-channel
1682  * without caching so that the cache can still keep the original value.
1683  * The cached value is then restored when the flag is set off or any other
1684  * than d-mic is used as the current input source.
1685  */
1686 static void alc_inv_dmic_sync(struct hda_codec *codec, bool force)
1687 {
1688 	struct alc_spec *spec = codec->spec;
1689 	int i;
1690 
1691 	if (!spec->inv_dmic_fixup)
1692 		return;
1693 	if (!spec->inv_dmic_muted && !force)
1694 		return;
1695 	for (i = 0; i < spec->num_adc_nids; i++) {
1696 		int src = spec->dyn_adc_switch ? 0 : i;
1697 		bool dmic_fixup = false;
1698 		hda_nid_t nid;
1699 		int parm, dir, v;
1700 
1701 		if (spec->inv_dmic_muted &&
1702 		    spec->imux_pins[spec->cur_mux[src]] == spec->inv_dmic_pin)
1703 			dmic_fixup = true;
1704 		if (!dmic_fixup && !force)
1705 			continue;
1706 		if (spec->vol_in_capsrc) {
1707 			nid = spec->capsrc_nids[i];
1708 			parm = AC_AMP_SET_RIGHT | AC_AMP_SET_OUTPUT;
1709 			dir = HDA_OUTPUT;
1710 		} else {
1711 			nid = spec->adc_nids[i];
1712 			parm = AC_AMP_SET_RIGHT | AC_AMP_SET_INPUT;
1713 			dir = HDA_INPUT;
1714 		}
1715 		/* we care only right channel */
1716 		v = snd_hda_codec_amp_read(codec, nid, 1, dir, 0);
1717 		if (v & 0x80) /* if already muted, we don't need to touch */
1718 			continue;
1719 		if (dmic_fixup) /* add mute for d-mic */
1720 			v |= 0x80;
1721 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1722 				    parm | v);
1723 	}
1724 }
1725 
1726 static int alc_inv_dmic_sw_get(struct snd_kcontrol *kcontrol,
1727 			       struct snd_ctl_elem_value *ucontrol)
1728 {
1729 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1730 	struct alc_spec *spec = codec->spec;
1731 
1732 	ucontrol->value.integer.value[0] = !spec->inv_dmic_muted;
1733 	return 0;
1734 }
1735 
1736 static int alc_inv_dmic_sw_put(struct snd_kcontrol *kcontrol,
1737 			       struct snd_ctl_elem_value *ucontrol)
1738 {
1739 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1740 	struct alc_spec *spec = codec->spec;
1741 	unsigned int val = !ucontrol->value.integer.value[0];
1742 
1743 	if (val == spec->inv_dmic_muted)
1744 		return 0;
1745 	spec->inv_dmic_muted = val;
1746 	alc_inv_dmic_sync(codec, true);
1747 	return 0;
1748 }
1749 
1750 static const struct snd_kcontrol_new alc_inv_dmic_sw = {
1751 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1752 	.info = snd_ctl_boolean_mono_info,
1753 	.get = alc_inv_dmic_sw_get,
1754 	.put = alc_inv_dmic_sw_put,
1755 };
1756 
1757 static int alc_add_inv_dmic_mixer(struct hda_codec *codec, hda_nid_t nid)
1758 {
1759 	struct alc_spec *spec = codec->spec;
1760 	struct snd_kcontrol_new *knew = alc_kcontrol_new(spec);
1761 	if (!knew)
1762 		return -ENOMEM;
1763 	*knew = alc_inv_dmic_sw;
1764 	knew->name = kstrdup("Inverted Internal Mic Capture Switch", GFP_KERNEL);
1765 	if (!knew->name)
1766 		return -ENOMEM;
1767 	spec->inv_dmic_fixup = 1;
1768 	spec->inv_dmic_muted = 0;
1769 	spec->inv_dmic_pin = nid;
1770 	return 0;
1771 }
1772 
1773 /* typically the digital mic is put at node 0x12 */
1774 static void alc_fixup_inv_dmic_0x12(struct hda_codec *codec,
1775 				    const struct alc_fixup *fix, int action)
1776 {
1777 	if (action == ALC_FIXUP_ACT_PROBE)
1778 		alc_add_inv_dmic_mixer(codec, 0x12);
1779 }
1780 
1781 /*
1782  * virtual master controls
1783  */
1784 
1785 /*
1786  * slave controls for virtual master
1787  */
1788 static const char * const alc_slave_pfxs[] = {
1789 	"Front", "Surround", "Center", "LFE", "Side",
1790 	"Headphone", "Speaker", "Mono", "Line Out",
1791 	"CLFE", "Bass Speaker", "PCM",
1792 	NULL,
1793 };
1794 
1795 /*
1796  * build control elements
1797  */
1798 
1799 #define NID_MAPPING		(-1)
1800 
1801 #define SUBDEV_SPEAKER_		(0 << 6)
1802 #define SUBDEV_HP_		(1 << 6)
1803 #define SUBDEV_LINE_		(2 << 6)
1804 #define SUBDEV_SPEAKER(x)	(SUBDEV_SPEAKER_ | ((x) & 0x3f))
1805 #define SUBDEV_HP(x)		(SUBDEV_HP_ | ((x) & 0x3f))
1806 #define SUBDEV_LINE(x)		(SUBDEV_LINE_ | ((x) & 0x3f))
1807 
1808 static void alc_free_kctls(struct hda_codec *codec);
1809 
1810 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1811 /* additional beep mixers; the actual parameters are overwritten at build */
1812 static const struct snd_kcontrol_new alc_beep_mixer[] = {
1813 	HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_INPUT),
1814 	HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_INPUT),
1815 	{ } /* end */
1816 };
1817 #endif
1818 
1819 static int __alc_build_controls(struct hda_codec *codec)
1820 {
1821 	struct alc_spec *spec = codec->spec;
1822 	struct snd_kcontrol *kctl = NULL;
1823 	const struct snd_kcontrol_new *knew;
1824 	int i, j, err;
1825 	unsigned int u;
1826 	hda_nid_t nid;
1827 
1828 	for (i = 0; i < spec->num_mixers; i++) {
1829 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1830 		if (err < 0)
1831 			return err;
1832 	}
1833 	if (spec->cap_mixer) {
1834 		err = snd_hda_add_new_ctls(codec, spec->cap_mixer);
1835 		if (err < 0)
1836 			return err;
1837 	}
1838 	if (spec->multiout.dig_out_nid) {
1839 		err = snd_hda_create_spdif_out_ctls(codec,
1840 						    spec->multiout.dig_out_nid,
1841 						    spec->multiout.dig_out_nid);
1842 		if (err < 0)
1843 			return err;
1844 		if (!spec->no_analog) {
1845 			err = snd_hda_create_spdif_share_sw(codec,
1846 							    &spec->multiout);
1847 			if (err < 0)
1848 				return err;
1849 			spec->multiout.share_spdif = 1;
1850 		}
1851 	}
1852 	if (spec->dig_in_nid) {
1853 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1854 		if (err < 0)
1855 			return err;
1856 	}
1857 
1858 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1859 	/* create beep controls if needed */
1860 	if (spec->beep_amp) {
1861 		const struct snd_kcontrol_new *knew;
1862 		for (knew = alc_beep_mixer; knew->name; knew++) {
1863 			struct snd_kcontrol *kctl;
1864 			kctl = snd_ctl_new1(knew, codec);
1865 			if (!kctl)
1866 				return -ENOMEM;
1867 			kctl->private_value = spec->beep_amp;
1868 			err = snd_hda_ctl_add(codec, 0, kctl);
1869 			if (err < 0)
1870 				return err;
1871 		}
1872 	}
1873 #endif
1874 
1875 	/* if we have no master control, let's create it */
1876 	if (!spec->no_analog &&
1877 	    !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1878 		unsigned int vmaster_tlv[4];
1879 		snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1880 					HDA_OUTPUT, vmaster_tlv);
1881 		err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1882 					  vmaster_tlv, alc_slave_pfxs,
1883 					  "Playback Volume");
1884 		if (err < 0)
1885 			return err;
1886 	}
1887 	if (!spec->no_analog &&
1888 	    !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1889 		err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
1890 					    NULL, alc_slave_pfxs,
1891 					    "Playback Switch",
1892 					    true, &spec->vmaster_mute.sw_kctl);
1893 		if (err < 0)
1894 			return err;
1895 	}
1896 
1897 	/* assign Capture Source enums to NID */
1898 	if (spec->capsrc_nids || spec->adc_nids) {
1899 		kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
1900 		if (!kctl)
1901 			kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1902 		for (i = 0; kctl && i < kctl->count; i++) {
1903 			err = snd_hda_add_nid(codec, kctl, i,
1904 					      get_capsrc(spec, i));
1905 			if (err < 0)
1906 				return err;
1907 		}
1908 	}
1909 	if (spec->cap_mixer && spec->adc_nids) {
1910 		const char *kname = kctl ? kctl->id.name : NULL;
1911 		for (knew = spec->cap_mixer; knew->name; knew++) {
1912 			if (kname && strcmp(knew->name, kname) == 0)
1913 				continue;
1914 			kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1915 			for (i = 0; kctl && i < kctl->count; i++) {
1916 				err = snd_hda_add_nid(codec, kctl, i,
1917 						      spec->adc_nids[i]);
1918 				if (err < 0)
1919 					return err;
1920 			}
1921 		}
1922 	}
1923 
1924 	/* other nid->control mapping */
1925 	for (i = 0; i < spec->num_mixers; i++) {
1926 		for (knew = spec->mixers[i]; knew->name; knew++) {
1927 			if (knew->iface != NID_MAPPING)
1928 				continue;
1929 			kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1930 			if (kctl == NULL)
1931 				continue;
1932 			u = knew->subdevice;
1933 			for (j = 0; j < 4; j++, u >>= 8) {
1934 				nid = u & 0x3f;
1935 				if (nid == 0)
1936 					continue;
1937 				switch (u & 0xc0) {
1938 				case SUBDEV_SPEAKER_:
1939 					nid = spec->autocfg.speaker_pins[nid];
1940 					break;
1941 				case SUBDEV_LINE_:
1942 					nid = spec->autocfg.line_out_pins[nid];
1943 					break;
1944 				case SUBDEV_HP_:
1945 					nid = spec->autocfg.hp_pins[nid];
1946 					break;
1947 				default:
1948 					continue;
1949 				}
1950 				err = snd_hda_add_nid(codec, kctl, 0, nid);
1951 				if (err < 0)
1952 					return err;
1953 			}
1954 			u = knew->private_value;
1955 			for (j = 0; j < 4; j++, u >>= 8) {
1956 				nid = u & 0xff;
1957 				if (nid == 0)
1958 					continue;
1959 				err = snd_hda_add_nid(codec, kctl, 0, nid);
1960 				if (err < 0)
1961 					return err;
1962 			}
1963 		}
1964 	}
1965 
1966 	alc_free_kctls(codec); /* no longer needed */
1967 
1968 	return 0;
1969 }
1970 
1971 static int alc_build_jacks(struct hda_codec *codec)
1972 {
1973 	struct alc_spec *spec = codec->spec;
1974 
1975 	if (spec->shared_mic_hp) {
1976 		int err;
1977 		int nid = spec->autocfg.inputs[1].pin;
1978 		err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
1979 		if (err < 0)
1980 			return err;
1981 		err = snd_hda_jack_detect_enable(codec, nid, 0);
1982 		if (err < 0)
1983 			return err;
1984 	}
1985 
1986 	return snd_hda_jack_add_kctls(codec, &spec->autocfg);
1987 }
1988 
1989 static int alc_build_controls(struct hda_codec *codec)
1990 {
1991 	int err = __alc_build_controls(codec);
1992 	if (err < 0)
1993 		return err;
1994 
1995 	err = alc_build_jacks(codec);
1996 	if (err < 0)
1997 		return err;
1998 	alc_apply_fixup(codec, ALC_FIXUP_ACT_BUILD);
1999 	return 0;
2000 }
2001 
2002 
2003 /*
2004  * Common callbacks
2005  */
2006 
2007 static void alc_init_special_input_src(struct hda_codec *codec);
2008 static void alc_auto_init_std(struct hda_codec *codec);
2009 
2010 static int alc_init(struct hda_codec *codec)
2011 {
2012 	struct alc_spec *spec = codec->spec;
2013 
2014 	if (spec->init_hook)
2015 		spec->init_hook(codec);
2016 
2017 	alc_fix_pll(codec);
2018 	alc_auto_init_amp(codec, spec->init_amp);
2019 
2020 	snd_hda_gen_apply_verbs(codec);
2021 	alc_init_special_input_src(codec);
2022 	alc_auto_init_std(codec);
2023 
2024 	alc_apply_fixup(codec, ALC_FIXUP_ACT_INIT);
2025 
2026 	hda_call_check_power_status(codec, 0x01);
2027 	return 0;
2028 }
2029 
2030 #ifdef CONFIG_PM
2031 static int alc_check_power_status(struct hda_codec *codec, hda_nid_t nid)
2032 {
2033 	struct alc_spec *spec = codec->spec;
2034 	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
2035 }
2036 #endif
2037 
2038 /*
2039  * Analog playback callbacks
2040  */
2041 static int alc_playback_pcm_open(struct hda_pcm_stream *hinfo,
2042 				    struct hda_codec *codec,
2043 				    struct snd_pcm_substream *substream)
2044 {
2045 	struct alc_spec *spec = codec->spec;
2046 	return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2047 					     hinfo);
2048 }
2049 
2050 static int alc_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2051 				       struct hda_codec *codec,
2052 				       unsigned int stream_tag,
2053 				       unsigned int format,
2054 				       struct snd_pcm_substream *substream)
2055 {
2056 	struct alc_spec *spec = codec->spec;
2057 	return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2058 						stream_tag, format, substream);
2059 }
2060 
2061 static int alc_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2062 				       struct hda_codec *codec,
2063 				       struct snd_pcm_substream *substream)
2064 {
2065 	struct alc_spec *spec = codec->spec;
2066 	return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
2067 }
2068 
2069 /*
2070  * Digital out
2071  */
2072 static int alc_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2073 					struct hda_codec *codec,
2074 					struct snd_pcm_substream *substream)
2075 {
2076 	struct alc_spec *spec = codec->spec;
2077 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2078 }
2079 
2080 static int alc_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2081 					   struct hda_codec *codec,
2082 					   unsigned int stream_tag,
2083 					   unsigned int format,
2084 					   struct snd_pcm_substream *substream)
2085 {
2086 	struct alc_spec *spec = codec->spec;
2087 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2088 					     stream_tag, format, substream);
2089 }
2090 
2091 static int alc_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2092 					   struct hda_codec *codec,
2093 					   struct snd_pcm_substream *substream)
2094 {
2095 	struct alc_spec *spec = codec->spec;
2096 	return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
2097 }
2098 
2099 static int alc_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2100 					 struct hda_codec *codec,
2101 					 struct snd_pcm_substream *substream)
2102 {
2103 	struct alc_spec *spec = codec->spec;
2104 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2105 }
2106 
2107 /*
2108  * Analog capture
2109  */
2110 static int alc_alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2111 				      struct hda_codec *codec,
2112 				      unsigned int stream_tag,
2113 				      unsigned int format,
2114 				      struct snd_pcm_substream *substream)
2115 {
2116 	struct alc_spec *spec = codec->spec;
2117 
2118 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
2119 				   stream_tag, 0, format);
2120 	return 0;
2121 }
2122 
2123 static int alc_alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2124 				      struct hda_codec *codec,
2125 				      struct snd_pcm_substream *substream)
2126 {
2127 	struct alc_spec *spec = codec->spec;
2128 
2129 	snd_hda_codec_cleanup_stream(codec,
2130 				     spec->adc_nids[substream->number + 1]);
2131 	return 0;
2132 }
2133 
2134 /* analog capture with dynamic dual-adc changes */
2135 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2136 				       struct hda_codec *codec,
2137 				       unsigned int stream_tag,
2138 				       unsigned int format,
2139 				       struct snd_pcm_substream *substream)
2140 {
2141 	struct alc_spec *spec = codec->spec;
2142 	spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
2143 	spec->cur_adc_stream_tag = stream_tag;
2144 	spec->cur_adc_format = format;
2145 	snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
2146 	return 0;
2147 }
2148 
2149 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2150 				       struct hda_codec *codec,
2151 				       struct snd_pcm_substream *substream)
2152 {
2153 	struct alc_spec *spec = codec->spec;
2154 	snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
2155 	spec->cur_adc = 0;
2156 	return 0;
2157 }
2158 
2159 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
2160 	.substreams = 1,
2161 	.channels_min = 2,
2162 	.channels_max = 2,
2163 	.nid = 0, /* fill later */
2164 	.ops = {
2165 		.prepare = dyn_adc_capture_pcm_prepare,
2166 		.cleanup = dyn_adc_capture_pcm_cleanup
2167 	},
2168 };
2169 
2170 /*
2171  */
2172 static const struct hda_pcm_stream alc_pcm_analog_playback = {
2173 	.substreams = 1,
2174 	.channels_min = 2,
2175 	.channels_max = 8,
2176 	/* NID is set in alc_build_pcms */
2177 	.ops = {
2178 		.open = alc_playback_pcm_open,
2179 		.prepare = alc_playback_pcm_prepare,
2180 		.cleanup = alc_playback_pcm_cleanup
2181 	},
2182 };
2183 
2184 static const struct hda_pcm_stream alc_pcm_analog_capture = {
2185 	.substreams = 1,
2186 	.channels_min = 2,
2187 	.channels_max = 2,
2188 	/* NID is set in alc_build_pcms */
2189 };
2190 
2191 static const struct hda_pcm_stream alc_pcm_analog_alt_playback = {
2192 	.substreams = 1,
2193 	.channels_min = 2,
2194 	.channels_max = 2,
2195 	/* NID is set in alc_build_pcms */
2196 };
2197 
2198 static const struct hda_pcm_stream alc_pcm_analog_alt_capture = {
2199 	.substreams = 2, /* can be overridden */
2200 	.channels_min = 2,
2201 	.channels_max = 2,
2202 	/* NID is set in alc_build_pcms */
2203 	.ops = {
2204 		.prepare = alc_alt_capture_pcm_prepare,
2205 		.cleanup = alc_alt_capture_pcm_cleanup
2206 	},
2207 };
2208 
2209 static const struct hda_pcm_stream alc_pcm_digital_playback = {
2210 	.substreams = 1,
2211 	.channels_min = 2,
2212 	.channels_max = 2,
2213 	/* NID is set in alc_build_pcms */
2214 	.ops = {
2215 		.open = alc_dig_playback_pcm_open,
2216 		.close = alc_dig_playback_pcm_close,
2217 		.prepare = alc_dig_playback_pcm_prepare,
2218 		.cleanup = alc_dig_playback_pcm_cleanup
2219 	},
2220 };
2221 
2222 static const struct hda_pcm_stream alc_pcm_digital_capture = {
2223 	.substreams = 1,
2224 	.channels_min = 2,
2225 	.channels_max = 2,
2226 	/* NID is set in alc_build_pcms */
2227 };
2228 
2229 /* Used by alc_build_pcms to flag that a PCM has no playback stream */
2230 static const struct hda_pcm_stream alc_pcm_null_stream = {
2231 	.substreams = 0,
2232 	.channels_min = 0,
2233 	.channels_max = 0,
2234 };
2235 
2236 static int alc_build_pcms(struct hda_codec *codec)
2237 {
2238 	struct alc_spec *spec = codec->spec;
2239 	struct hda_pcm *info = spec->pcm_rec;
2240 	const struct hda_pcm_stream *p;
2241 	bool have_multi_adcs;
2242 	int i;
2243 
2244 	codec->num_pcms = 1;
2245 	codec->pcm_info = info;
2246 
2247 	if (spec->no_analog)
2248 		goto skip_analog;
2249 
2250 	snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
2251 		 "%s Analog", codec->chip_name);
2252 	info->name = spec->stream_name_analog;
2253 
2254 	if (spec->multiout.num_dacs > 0) {
2255 		p = spec->stream_analog_playback;
2256 		if (!p)
2257 			p = &alc_pcm_analog_playback;
2258 		info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2259 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
2260 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
2261 			spec->multiout.max_channels;
2262 	}
2263 	if (spec->adc_nids) {
2264 		p = spec->stream_analog_capture;
2265 		if (!p) {
2266 			if (spec->dyn_adc_switch)
2267 				p = &dyn_adc_pcm_analog_capture;
2268 			else
2269 				p = &alc_pcm_analog_capture;
2270 		}
2271 		info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2272 		info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
2273 	}
2274 
2275 	if (spec->channel_mode) {
2276 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
2277 		for (i = 0; i < spec->num_channel_mode; i++) {
2278 			if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
2279 				info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
2280 			}
2281 		}
2282 	}
2283 
2284  skip_analog:
2285 	/* SPDIF for stream index #1 */
2286 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
2287 		snprintf(spec->stream_name_digital,
2288 			 sizeof(spec->stream_name_digital),
2289 			 "%s Digital", codec->chip_name);
2290 		codec->num_pcms = 2;
2291 	        codec->slave_dig_outs = spec->multiout.slave_dig_outs;
2292 		info = spec->pcm_rec + 1;
2293 		info->name = spec->stream_name_digital;
2294 		if (spec->dig_out_type)
2295 			info->pcm_type = spec->dig_out_type;
2296 		else
2297 			info->pcm_type = HDA_PCM_TYPE_SPDIF;
2298 		if (spec->multiout.dig_out_nid) {
2299 			p = spec->stream_digital_playback;
2300 			if (!p)
2301 				p = &alc_pcm_digital_playback;
2302 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2303 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
2304 		}
2305 		if (spec->dig_in_nid) {
2306 			p = spec->stream_digital_capture;
2307 			if (!p)
2308 				p = &alc_pcm_digital_capture;
2309 			info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2310 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
2311 		}
2312 		/* FIXME: do we need this for all Realtek codec models? */
2313 		codec->spdif_status_reset = 1;
2314 	}
2315 
2316 	if (spec->no_analog)
2317 		return 0;
2318 
2319 	/* If the use of more than one ADC is requested for the current
2320 	 * model, configure a second analog capture-only PCM.
2321 	 */
2322 	have_multi_adcs = (spec->num_adc_nids > 1) &&
2323 		!spec->dyn_adc_switch && !spec->auto_mic &&
2324 		(!spec->input_mux || spec->input_mux->num_items > 1);
2325 	/* Additional Analaog capture for index #2 */
2326 	if (spec->alt_dac_nid || have_multi_adcs) {
2327 		codec->num_pcms = 3;
2328 		info = spec->pcm_rec + 2;
2329 		info->name = spec->stream_name_analog;
2330 		if (spec->alt_dac_nid) {
2331 			p = spec->stream_analog_alt_playback;
2332 			if (!p)
2333 				p = &alc_pcm_analog_alt_playback;
2334 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2335 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
2336 				spec->alt_dac_nid;
2337 		} else {
2338 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
2339 				alc_pcm_null_stream;
2340 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
2341 		}
2342 		if (have_multi_adcs) {
2343 			p = spec->stream_analog_alt_capture;
2344 			if (!p)
2345 				p = &alc_pcm_analog_alt_capture;
2346 			info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2347 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
2348 				spec->adc_nids[1];
2349 			info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
2350 				spec->num_adc_nids - 1;
2351 		} else {
2352 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
2353 				alc_pcm_null_stream;
2354 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
2355 		}
2356 	}
2357 
2358 	return 0;
2359 }
2360 
2361 static inline void alc_shutup(struct hda_codec *codec)
2362 {
2363 	struct alc_spec *spec = codec->spec;
2364 
2365 	if (spec && spec->shutup)
2366 		spec->shutup(codec);
2367 	snd_hda_shutup_pins(codec);
2368 }
2369 
2370 static void alc_free_kctls(struct hda_codec *codec)
2371 {
2372 	struct alc_spec *spec = codec->spec;
2373 
2374 	if (spec->kctls.list) {
2375 		struct snd_kcontrol_new *kctl = spec->kctls.list;
2376 		int i;
2377 		for (i = 0; i < spec->kctls.used; i++)
2378 			kfree(kctl[i].name);
2379 	}
2380 	snd_array_free(&spec->kctls);
2381 }
2382 
2383 static void alc_free_bind_ctls(struct hda_codec *codec)
2384 {
2385 	struct alc_spec *spec = codec->spec;
2386 	if (spec->bind_ctls.list) {
2387 		struct hda_bind_ctls **ctl = spec->bind_ctls.list;
2388 		int i;
2389 		for (i = 0; i < spec->bind_ctls.used; i++)
2390 			kfree(ctl[i]);
2391 	}
2392 	snd_array_free(&spec->bind_ctls);
2393 }
2394 
2395 static void alc_free(struct hda_codec *codec)
2396 {
2397 	struct alc_spec *spec = codec->spec;
2398 
2399 	if (!spec)
2400 		return;
2401 
2402 	alc_shutup(codec);
2403 	alc_free_kctls(codec);
2404 	alc_free_bind_ctls(codec);
2405 	snd_hda_gen_free(&spec->gen);
2406 	kfree(spec);
2407 	snd_hda_detach_beep_device(codec);
2408 }
2409 
2410 #ifdef CONFIG_PM
2411 static void alc_power_eapd(struct hda_codec *codec)
2412 {
2413 	alc_auto_setup_eapd(codec, false);
2414 }
2415 
2416 static int alc_suspend(struct hda_codec *codec)
2417 {
2418 	struct alc_spec *spec = codec->spec;
2419 	alc_shutup(codec);
2420 	if (spec && spec->power_hook)
2421 		spec->power_hook(codec);
2422 	return 0;
2423 }
2424 #endif
2425 
2426 #ifdef CONFIG_PM
2427 static int alc_resume(struct hda_codec *codec)
2428 {
2429 	msleep(150); /* to avoid pop noise */
2430 	codec->patch_ops.init(codec);
2431 	snd_hda_codec_resume_amp(codec);
2432 	snd_hda_codec_resume_cache(codec);
2433 	alc_inv_dmic_sync(codec, true);
2434 	hda_call_check_power_status(codec, 0x01);
2435 	return 0;
2436 }
2437 #endif
2438 
2439 /*
2440  */
2441 static const struct hda_codec_ops alc_patch_ops = {
2442 	.build_controls = alc_build_controls,
2443 	.build_pcms = alc_build_pcms,
2444 	.init = alc_init,
2445 	.free = alc_free,
2446 	.unsol_event = snd_hda_jack_unsol_event,
2447 #ifdef CONFIG_PM
2448 	.resume = alc_resume,
2449 #endif
2450 #ifdef CONFIG_PM
2451 	.suspend = alc_suspend,
2452 	.check_power_status = alc_check_power_status,
2453 #endif
2454 	.reboot_notify = alc_shutup,
2455 };
2456 
2457 
2458 /* replace the codec chip_name with the given string */
2459 static int alc_codec_rename(struct hda_codec *codec, const char *name)
2460 {
2461 	kfree(codec->chip_name);
2462 	codec->chip_name = kstrdup(name, GFP_KERNEL);
2463 	if (!codec->chip_name) {
2464 		alc_free(codec);
2465 		return -ENOMEM;
2466 	}
2467 	return 0;
2468 }
2469 
2470 /*
2471  * Rename codecs appropriately from COEF value
2472  */
2473 struct alc_codec_rename_table {
2474 	unsigned int vendor_id;
2475 	unsigned short coef_mask;
2476 	unsigned short coef_bits;
2477 	const char *name;
2478 };
2479 
2480 static struct alc_codec_rename_table rename_tbl[] = {
2481 	{ 0x10ec0269, 0xfff0, 0x3010, "ALC277" },
2482 	{ 0x10ec0269, 0xf0f0, 0x2010, "ALC259" },
2483 	{ 0x10ec0269, 0xf0f0, 0x3010, "ALC258" },
2484 	{ 0x10ec0269, 0x00f0, 0x0010, "ALC269VB" },
2485 	{ 0x10ec0269, 0xffff, 0xa023, "ALC259" },
2486 	{ 0x10ec0269, 0xffff, 0x6023, "ALC281X" },
2487 	{ 0x10ec0269, 0x00f0, 0x0020, "ALC269VC" },
2488 	{ 0x10ec0269, 0x00f0, 0x0030, "ALC269VD" },
2489 	{ 0x10ec0887, 0x00f0, 0x0030, "ALC887-VD" },
2490 	{ 0x10ec0888, 0x00f0, 0x0030, "ALC888-VD" },
2491 	{ 0x10ec0888, 0xf0f0, 0x3020, "ALC886" },
2492 	{ 0x10ec0899, 0x2000, 0x2000, "ALC899" },
2493 	{ 0x10ec0892, 0xffff, 0x8020, "ALC661" },
2494 	{ 0x10ec0892, 0xffff, 0x8011, "ALC661" },
2495 	{ 0x10ec0892, 0xffff, 0x4011, "ALC656" },
2496 	{ } /* terminator */
2497 };
2498 
2499 static int alc_codec_rename_from_preset(struct hda_codec *codec)
2500 {
2501 	const struct alc_codec_rename_table *p;
2502 
2503 	for (p = rename_tbl; p->vendor_id; p++) {
2504 		if (p->vendor_id != codec->vendor_id)
2505 			continue;
2506 		if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
2507 			return alc_codec_rename(codec, p->name);
2508 	}
2509 	return 0;
2510 }
2511 
2512 /*
2513  * Automatic parse of I/O pins from the BIOS configuration
2514  */
2515 
2516 enum {
2517 	ALC_CTL_WIDGET_VOL,
2518 	ALC_CTL_WIDGET_MUTE,
2519 	ALC_CTL_BIND_MUTE,
2520 	ALC_CTL_BIND_VOL,
2521 	ALC_CTL_BIND_SW,
2522 };
2523 static const struct snd_kcontrol_new alc_control_templates[] = {
2524 	HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2525 	HDA_CODEC_MUTE(NULL, 0, 0, 0),
2526 	HDA_BIND_MUTE(NULL, 0, 0, 0),
2527 	HDA_BIND_VOL(NULL, 0),
2528 	HDA_BIND_SW(NULL, 0),
2529 };
2530 
2531 /* add dynamic controls */
2532 static int add_control(struct alc_spec *spec, int type, const char *name,
2533 		       int cidx, unsigned long val)
2534 {
2535 	struct snd_kcontrol_new *knew;
2536 
2537 	knew = alc_kcontrol_new(spec);
2538 	if (!knew)
2539 		return -ENOMEM;
2540 	*knew = alc_control_templates[type];
2541 	knew->name = kstrdup(name, GFP_KERNEL);
2542 	if (!knew->name)
2543 		return -ENOMEM;
2544 	knew->index = cidx;
2545 	if (get_amp_nid_(val))
2546 		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2547 	knew->private_value = val;
2548 	return 0;
2549 }
2550 
2551 static int add_control_with_pfx(struct alc_spec *spec, int type,
2552 				const char *pfx, const char *dir,
2553 				const char *sfx, int cidx, unsigned long val)
2554 {
2555 	char name[32];
2556 	snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
2557 	return add_control(spec, type, name, cidx, val);
2558 }
2559 
2560 #define add_pb_vol_ctrl(spec, type, pfx, val)			\
2561 	add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
2562 #define add_pb_sw_ctrl(spec, type, pfx, val)			\
2563 	add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
2564 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val)			\
2565 	add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
2566 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val)			\
2567 	add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
2568 
2569 static const char * const channel_name[4] = {
2570 	"Front", "Surround", "CLFE", "Side"
2571 };
2572 
2573 static const char *alc_get_line_out_pfx(struct alc_spec *spec, int ch,
2574 					bool can_be_master, int *index)
2575 {
2576 	struct auto_pin_cfg *cfg = &spec->autocfg;
2577 
2578 	*index = 0;
2579 	if (cfg->line_outs == 1 && !spec->multi_ios &&
2580 	    !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
2581 		return "Master";
2582 
2583 	switch (cfg->line_out_type) {
2584 	case AUTO_PIN_SPEAKER_OUT:
2585 		if (cfg->line_outs == 1)
2586 			return "Speaker";
2587 		if (cfg->line_outs == 2)
2588 			return ch ? "Bass Speaker" : "Speaker";
2589 		break;
2590 	case AUTO_PIN_HP_OUT:
2591 		/* for multi-io case, only the primary out */
2592 		if (ch && spec->multi_ios)
2593 			break;
2594 		*index = ch;
2595 		return "Headphone";
2596 	default:
2597 		if (cfg->line_outs == 1 && !spec->multi_ios)
2598 			return "PCM";
2599 		break;
2600 	}
2601 	if (ch >= ARRAY_SIZE(channel_name)) {
2602 		snd_BUG();
2603 		return "PCM";
2604 	}
2605 
2606 	return channel_name[ch];
2607 }
2608 
2609 #ifdef CONFIG_PM
2610 /* add the powersave loopback-list entry */
2611 static void add_loopback_list(struct alc_spec *spec, hda_nid_t mix, int idx)
2612 {
2613 	struct hda_amp_list *list;
2614 
2615 	if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2616 		return;
2617 	list = spec->loopback_list + spec->num_loopbacks;
2618 	list->nid = mix;
2619 	list->dir = HDA_INPUT;
2620 	list->idx = idx;
2621 	spec->num_loopbacks++;
2622 	spec->loopback.amplist = spec->loopback_list;
2623 }
2624 #else
2625 #define add_loopback_list(spec, mix, idx) /* NOP */
2626 #endif
2627 
2628 /* create input playback/capture controls for the given pin */
2629 static int new_analog_input(struct alc_spec *spec, hda_nid_t pin,
2630 			    const char *ctlname, int ctlidx,
2631 			    int idx, hda_nid_t mix_nid)
2632 {
2633 	int err;
2634 
2635 	err = __add_pb_vol_ctrl(spec, ALC_CTL_WIDGET_VOL, ctlname, ctlidx,
2636 			  HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2637 	if (err < 0)
2638 		return err;
2639 	err = __add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, ctlname, ctlidx,
2640 			  HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2641 	if (err < 0)
2642 		return err;
2643 	add_loopback_list(spec, mix_nid, idx);
2644 	return 0;
2645 }
2646 
2647 static int alc_is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2648 {
2649 	unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2650 	return (pincap & AC_PINCAP_IN) != 0;
2651 }
2652 
2653 /* Parse the codec tree and retrieve ADCs and corresponding capsrc MUXs */
2654 static int alc_auto_fill_adc_caps(struct hda_codec *codec)
2655 {
2656 	struct alc_spec *spec = codec->spec;
2657 	hda_nid_t nid;
2658 	hda_nid_t *adc_nids = spec->private_adc_nids;
2659 	hda_nid_t *cap_nids = spec->private_capsrc_nids;
2660 	int max_nums = ARRAY_SIZE(spec->private_adc_nids);
2661 	int i, nums = 0;
2662 
2663 	nid = codec->start_nid;
2664 	for (i = 0; i < codec->num_nodes; i++, nid++) {
2665 		hda_nid_t src;
2666 		unsigned int caps = get_wcaps(codec, nid);
2667 		int type = get_wcaps_type(caps);
2668 
2669 		if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2670 			continue;
2671 		adc_nids[nums] = nid;
2672 		cap_nids[nums] = nid;
2673 		src = nid;
2674 		for (;;) {
2675 			int n;
2676 			type = get_wcaps_type(get_wcaps(codec, src));
2677 			if (type == AC_WID_PIN)
2678 				break;
2679 			if (type == AC_WID_AUD_SEL) {
2680 				cap_nids[nums] = src;
2681 				break;
2682 			}
2683 			n = snd_hda_get_num_conns(codec, src);
2684 			if (n > 1) {
2685 				cap_nids[nums] = src;
2686 				break;
2687 			} else if (n != 1)
2688 				break;
2689 			if (snd_hda_get_connections(codec, src, &src, 1) != 1)
2690 				break;
2691 		}
2692 		if (++nums >= max_nums)
2693 			break;
2694 	}
2695 	spec->adc_nids = spec->private_adc_nids;
2696 	spec->capsrc_nids = spec->private_capsrc_nids;
2697 	spec->num_adc_nids = nums;
2698 	return nums;
2699 }
2700 
2701 /* create playback/capture controls for input pins */
2702 static int alc_auto_create_input_ctls(struct hda_codec *codec)
2703 {
2704 	struct alc_spec *spec = codec->spec;
2705 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2706 	hda_nid_t mixer = spec->mixer_nid;
2707 	struct hda_input_mux *imux = &spec->private_imux[0];
2708 	int num_adcs;
2709 	int i, c, err, idx, type_idx = 0;
2710 	const char *prev_label = NULL;
2711 
2712 	num_adcs = alc_auto_fill_adc_caps(codec);
2713 	if (num_adcs < 0)
2714 		return 0;
2715 
2716 	for (i = 0; i < cfg->num_inputs; i++) {
2717 		hda_nid_t pin;
2718 		const char *label;
2719 
2720 		pin = cfg->inputs[i].pin;
2721 		if (!alc_is_input_pin(codec, pin))
2722 			continue;
2723 
2724 		label = hda_get_autocfg_input_label(codec, cfg, i);
2725 		if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2726 			label = "Headphone Mic";
2727 		if (prev_label && !strcmp(label, prev_label))
2728 			type_idx++;
2729 		else
2730 			type_idx = 0;
2731 		prev_label = label;
2732 
2733 		if (mixer) {
2734 			idx = get_connection_index(codec, mixer, pin);
2735 			if (idx >= 0) {
2736 				err = new_analog_input(spec, pin,
2737 						       label, type_idx,
2738 						       idx, mixer);
2739 				if (err < 0)
2740 					return err;
2741 			}
2742 		}
2743 
2744 		for (c = 0; c < num_adcs; c++) {
2745 			hda_nid_t cap = get_capsrc(spec, c);
2746 			idx = get_connection_index(codec, cap, pin);
2747 			if (idx >= 0) {
2748 				spec->imux_pins[imux->num_items] = pin;
2749 				snd_hda_add_imux_item(imux, label, idx, NULL);
2750 				break;
2751 			}
2752 		}
2753 	}
2754 
2755 	spec->num_mux_defs = 1;
2756 	spec->input_mux = imux;
2757 
2758 	return 0;
2759 }
2760 
2761 /* create a shared input with the headphone out */
2762 static int alc_auto_create_shared_input(struct hda_codec *codec)
2763 {
2764 	struct alc_spec *spec = codec->spec;
2765 	struct auto_pin_cfg *cfg = &spec->autocfg;
2766 	unsigned int defcfg;
2767 	hda_nid_t nid;
2768 
2769 	/* only one internal input pin? */
2770 	if (cfg->num_inputs != 1)
2771 		return 0;
2772 	defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2773 	if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2774 		return 0;
2775 
2776 	if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2777 		nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2778 	else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2779 		nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2780 	else
2781 		return 0; /* both not available */
2782 
2783 	if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2784 		return 0; /* no input */
2785 
2786 	cfg->inputs[1].pin = nid;
2787 	cfg->inputs[1].type = AUTO_PIN_MIC;
2788 	cfg->num_inputs = 2;
2789 	spec->shared_mic_hp = 1;
2790 	snd_printdd("realtek: Enable shared I/O jack on NID 0x%x\n", nid);
2791 	return 0;
2792 }
2793 
2794 static void alc_set_pin_output(struct hda_codec *codec, hda_nid_t nid,
2795 			       unsigned int pin_type)
2796 {
2797 	snd_hda_set_pin_ctl(codec, nid, pin_type);
2798 	/* unmute pin */
2799 	if (nid_has_mute(codec, nid, HDA_OUTPUT))
2800 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2801 			    AMP_OUT_UNMUTE);
2802 }
2803 
2804 static int get_pin_type(int line_out_type)
2805 {
2806 	if (line_out_type == AUTO_PIN_HP_OUT)
2807 		return PIN_HP;
2808 	else
2809 		return PIN_OUT;
2810 }
2811 
2812 static void alc_auto_init_analog_input(struct hda_codec *codec)
2813 {
2814 	struct alc_spec *spec = codec->spec;
2815 	struct auto_pin_cfg *cfg = &spec->autocfg;
2816 	int i;
2817 
2818 	for (i = 0; i < cfg->num_inputs; i++) {
2819 		hda_nid_t nid = cfg->inputs[i].pin;
2820 		if (alc_is_input_pin(codec, nid)) {
2821 			alc_set_input_pin(codec, nid, cfg->inputs[i].type);
2822 			if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
2823 				snd_hda_codec_write(codec, nid, 0,
2824 						    AC_VERB_SET_AMP_GAIN_MUTE,
2825 						    AMP_OUT_MUTE);
2826 		}
2827 	}
2828 
2829 	/* mute all loopback inputs */
2830 	if (spec->mixer_nid) {
2831 		int nums = snd_hda_get_num_conns(codec, spec->mixer_nid);
2832 		for (i = 0; i < nums; i++)
2833 			snd_hda_codec_write(codec, spec->mixer_nid, 0,
2834 					    AC_VERB_SET_AMP_GAIN_MUTE,
2835 					    AMP_IN_MUTE(i));
2836 	}
2837 }
2838 
2839 /* convert from MIX nid to DAC */
2840 static hda_nid_t alc_auto_mix_to_dac(struct hda_codec *codec, hda_nid_t nid)
2841 {
2842 	hda_nid_t list[5];
2843 	int i, num;
2844 
2845 	if (get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_AUD_OUT)
2846 		return nid;
2847 	num = snd_hda_get_connections(codec, nid, list, ARRAY_SIZE(list));
2848 	for (i = 0; i < num; i++) {
2849 		if (get_wcaps_type(get_wcaps(codec, list[i])) == AC_WID_AUD_OUT)
2850 			return list[i];
2851 	}
2852 	return 0;
2853 }
2854 
2855 /* go down to the selector widget before the mixer */
2856 static hda_nid_t alc_go_down_to_selector(struct hda_codec *codec, hda_nid_t pin)
2857 {
2858 	hda_nid_t srcs[5];
2859 	int num = snd_hda_get_connections(codec, pin, srcs,
2860 					  ARRAY_SIZE(srcs));
2861 	if (num != 1 ||
2862 	    get_wcaps_type(get_wcaps(codec, srcs[0])) != AC_WID_AUD_SEL)
2863 		return pin;
2864 	return srcs[0];
2865 }
2866 
2867 /* get MIX nid connected to the given pin targeted to DAC */
2868 static hda_nid_t alc_auto_dac_to_mix(struct hda_codec *codec, hda_nid_t pin,
2869 				   hda_nid_t dac)
2870 {
2871 	hda_nid_t mix[5];
2872 	int i, num;
2873 
2874 	pin = alc_go_down_to_selector(codec, pin);
2875 	num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2876 	for (i = 0; i < num; i++) {
2877 		if (alc_auto_mix_to_dac(codec, mix[i]) == dac)
2878 			return mix[i];
2879 	}
2880 	return 0;
2881 }
2882 
2883 /* select the connection from pin to DAC if needed */
2884 static int alc_auto_select_dac(struct hda_codec *codec, hda_nid_t pin,
2885 			       hda_nid_t dac)
2886 {
2887 	hda_nid_t mix[5];
2888 	int i, num;
2889 
2890 	pin = alc_go_down_to_selector(codec, pin);
2891 	num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2892 	if (num < 2)
2893 		return 0;
2894 	for (i = 0; i < num; i++) {
2895 		if (alc_auto_mix_to_dac(codec, mix[i]) == dac) {
2896 			snd_hda_codec_update_cache(codec, pin, 0,
2897 						   AC_VERB_SET_CONNECT_SEL, i);
2898 			return 0;
2899 		}
2900 	}
2901 	return 0;
2902 }
2903 
2904 static bool alc_is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
2905 {
2906 	struct alc_spec *spec = codec->spec;
2907 	int i;
2908 	if (found_in_nid_list(nid, spec->multiout.dac_nids,
2909 			      ARRAY_SIZE(spec->private_dac_nids)) ||
2910 	    found_in_nid_list(nid, spec->multiout.hp_out_nid,
2911 			      ARRAY_SIZE(spec->multiout.hp_out_nid)) ||
2912 	    found_in_nid_list(nid, spec->multiout.extra_out_nid,
2913 			      ARRAY_SIZE(spec->multiout.extra_out_nid)))
2914 		return true;
2915 	for (i = 0; i < spec->multi_ios; i++) {
2916 		if (spec->multi_io[i].dac == nid)
2917 			return true;
2918 	}
2919 	return false;
2920 }
2921 
2922 /* look for an empty DAC slot */
2923 static hda_nid_t alc_auto_look_for_dac(struct hda_codec *codec, hda_nid_t pin)
2924 {
2925 	hda_nid_t srcs[5];
2926 	int i, num;
2927 
2928 	pin = alc_go_down_to_selector(codec, pin);
2929 	num = snd_hda_get_connections(codec, pin, srcs, ARRAY_SIZE(srcs));
2930 	for (i = 0; i < num; i++) {
2931 		hda_nid_t nid = alc_auto_mix_to_dac(codec, srcs[i]);
2932 		if (!nid)
2933 			continue;
2934 		if (!alc_is_dac_already_used(codec, nid))
2935 			return nid;
2936 	}
2937 	return 0;
2938 }
2939 
2940 /* check whether the DAC is reachable from the pin */
2941 static bool alc_auto_is_dac_reachable(struct hda_codec *codec,
2942 				      hda_nid_t pin, hda_nid_t dac)
2943 {
2944 	hda_nid_t srcs[5];
2945 	int i, num;
2946 
2947 	if (!pin || !dac)
2948 		return false;
2949 	pin = alc_go_down_to_selector(codec, pin);
2950 	num = snd_hda_get_connections(codec, pin, srcs, ARRAY_SIZE(srcs));
2951 	for (i = 0; i < num; i++) {
2952 		hda_nid_t nid = alc_auto_mix_to_dac(codec, srcs[i]);
2953 		if (nid == dac)
2954 			return true;
2955 	}
2956 	return false;
2957 }
2958 
2959 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
2960 {
2961 	struct alc_spec *spec = codec->spec;
2962 	hda_nid_t sel = alc_go_down_to_selector(codec, pin);
2963 	hda_nid_t nid, nid_found, srcs[5];
2964 	int i, num = snd_hda_get_connections(codec, sel, srcs,
2965 					  ARRAY_SIZE(srcs));
2966 	if (num == 1)
2967 		return alc_auto_look_for_dac(codec, pin);
2968 	nid_found = 0;
2969 	for (i = 0; i < num; i++) {
2970 		if (srcs[i] == spec->mixer_nid)
2971 			continue;
2972 		nid = alc_auto_mix_to_dac(codec, srcs[i]);
2973 		if (nid && !alc_is_dac_already_used(codec, nid)) {
2974 			if (nid_found)
2975 				return 0;
2976 			nid_found = nid;
2977 		}
2978 	}
2979 	return nid_found;
2980 }
2981 
2982 /* mark up volume and mute control NIDs: used during badness parsing and
2983  * at creating actual controls
2984  */
2985 static inline unsigned int get_ctl_pos(unsigned int data)
2986 {
2987 	hda_nid_t nid = get_amp_nid_(data);
2988 	unsigned int dir;
2989 	if (snd_BUG_ON(nid >= MAX_VOL_NIDS))
2990 		return 0;
2991 	dir = get_amp_direction_(data);
2992 	return (nid << 1) | dir;
2993 }
2994 
2995 #define is_ctl_used(bits, data) \
2996 	test_bit(get_ctl_pos(data), bits)
2997 #define mark_ctl_usage(bits, data) \
2998 	set_bit(get_ctl_pos(data), bits)
2999 
3000 static void clear_vol_marks(struct hda_codec *codec)
3001 {
3002 	struct alc_spec *spec = codec->spec;
3003 	memset(spec->vol_ctls, 0, sizeof(spec->vol_ctls));
3004 	memset(spec->sw_ctls, 0, sizeof(spec->sw_ctls));
3005 }
3006 
3007 /* badness definition */
3008 enum {
3009 	/* No primary DAC is found for the main output */
3010 	BAD_NO_PRIMARY_DAC = 0x10000,
3011 	/* No DAC is found for the extra output */
3012 	BAD_NO_DAC = 0x4000,
3013 	/* No possible multi-ios */
3014 	BAD_MULTI_IO = 0x103,
3015 	/* No individual DAC for extra output */
3016 	BAD_NO_EXTRA_DAC = 0x102,
3017 	/* No individual DAC for extra surrounds */
3018 	BAD_NO_EXTRA_SURR_DAC = 0x101,
3019 	/* Primary DAC shared with main surrounds */
3020 	BAD_SHARED_SURROUND = 0x100,
3021 	/* Primary DAC shared with main CLFE */
3022 	BAD_SHARED_CLFE = 0x10,
3023 	/* Primary DAC shared with extra surrounds */
3024 	BAD_SHARED_EXTRA_SURROUND = 0x10,
3025 	/* Volume widget is shared */
3026 	BAD_SHARED_VOL = 0x10,
3027 };
3028 
3029 static hda_nid_t alc_look_for_out_mute_nid(struct hda_codec *codec,
3030 					   hda_nid_t pin, hda_nid_t dac);
3031 static hda_nid_t alc_look_for_out_vol_nid(struct hda_codec *codec,
3032 					  hda_nid_t pin, hda_nid_t dac);
3033 
3034 static int eval_shared_vol_badness(struct hda_codec *codec, hda_nid_t pin,
3035 				   hda_nid_t dac)
3036 {
3037 	struct alc_spec *spec = codec->spec;
3038 	hda_nid_t nid;
3039 	unsigned int val;
3040 	int badness = 0;
3041 
3042 	nid = alc_look_for_out_vol_nid(codec, pin, dac);
3043 	if (nid) {
3044 		val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3045 		if (is_ctl_used(spec->vol_ctls, nid))
3046 			badness += BAD_SHARED_VOL;
3047 		else
3048 			mark_ctl_usage(spec->vol_ctls, val);
3049 	} else
3050 		badness += BAD_SHARED_VOL;
3051 	nid = alc_look_for_out_mute_nid(codec, pin, dac);
3052 	if (nid) {
3053 		unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
3054 		if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT)
3055 			val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3056 		else
3057 			val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
3058 		if (is_ctl_used(spec->sw_ctls, val))
3059 			badness += BAD_SHARED_VOL;
3060 		else
3061 			mark_ctl_usage(spec->sw_ctls, val);
3062 	} else
3063 		badness += BAD_SHARED_VOL;
3064 	return badness;
3065 }
3066 
3067 struct badness_table {
3068 	int no_primary_dac;	/* no primary DAC */
3069 	int no_dac;		/* no secondary DACs */
3070 	int shared_primary;	/* primary DAC is shared with main output */
3071 	int shared_surr;	/* secondary DAC shared with main or primary */
3072 	int shared_clfe;	/* third DAC shared with main or primary */
3073 	int shared_surr_main;	/* secondary DAC sahred with main/DAC0 */
3074 };
3075 
3076 static struct badness_table main_out_badness = {
3077 	.no_primary_dac = BAD_NO_PRIMARY_DAC,
3078 	.no_dac = BAD_NO_DAC,
3079 	.shared_primary = BAD_NO_PRIMARY_DAC,
3080 	.shared_surr = BAD_SHARED_SURROUND,
3081 	.shared_clfe = BAD_SHARED_CLFE,
3082 	.shared_surr_main = BAD_SHARED_SURROUND,
3083 };
3084 
3085 static struct badness_table extra_out_badness = {
3086 	.no_primary_dac = BAD_NO_DAC,
3087 	.no_dac = BAD_NO_DAC,
3088 	.shared_primary = BAD_NO_EXTRA_DAC,
3089 	.shared_surr = BAD_SHARED_EXTRA_SURROUND,
3090 	.shared_clfe = BAD_SHARED_EXTRA_SURROUND,
3091 	.shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
3092 };
3093 
3094 /* try to assign DACs to pins and return the resultant badness */
3095 static int alc_auto_fill_dacs(struct hda_codec *codec, int num_outs,
3096 			      const hda_nid_t *pins, hda_nid_t *dacs,
3097 			      const struct badness_table *bad)
3098 {
3099 	struct alc_spec *spec = codec->spec;
3100 	struct auto_pin_cfg *cfg = &spec->autocfg;
3101 	int i, j;
3102 	int badness = 0;
3103 	hda_nid_t dac;
3104 
3105 	if (!num_outs)
3106 		return 0;
3107 
3108 	for (i = 0; i < num_outs; i++) {
3109 		hda_nid_t pin = pins[i];
3110 		if (!dacs[i])
3111 			dacs[i] = alc_auto_look_for_dac(codec, pin);
3112 		if (!dacs[i] && !i) {
3113 			for (j = 1; j < num_outs; j++) {
3114 				if (alc_auto_is_dac_reachable(codec, pin, dacs[j])) {
3115 					dacs[0] = dacs[j];
3116 					dacs[j] = 0;
3117 					break;
3118 				}
3119 			}
3120 		}
3121 		dac = dacs[i];
3122 		if (!dac) {
3123 			if (alc_auto_is_dac_reachable(codec, pin, dacs[0]))
3124 				dac = dacs[0];
3125 			else if (cfg->line_outs > i &&
3126 				 alc_auto_is_dac_reachable(codec, pin,
3127 					spec->private_dac_nids[i]))
3128 				dac = spec->private_dac_nids[i];
3129 			if (dac) {
3130 				if (!i)
3131 					badness += bad->shared_primary;
3132 				else if (i == 1)
3133 					badness += bad->shared_surr;
3134 				else
3135 					badness += bad->shared_clfe;
3136 			} else if (alc_auto_is_dac_reachable(codec, pin,
3137 					spec->private_dac_nids[0])) {
3138 				dac = spec->private_dac_nids[0];
3139 				badness += bad->shared_surr_main;
3140 			} else if (!i)
3141 				badness += bad->no_primary_dac;
3142 			else
3143 				badness += bad->no_dac;
3144 		}
3145 		if (dac)
3146 			badness += eval_shared_vol_badness(codec, pin, dac);
3147 	}
3148 
3149 	return badness;
3150 }
3151 
3152 static int alc_auto_fill_multi_ios(struct hda_codec *codec,
3153 				   hda_nid_t reference_pin,
3154 				   bool hardwired, int offset);
3155 
3156 static bool alc_map_singles(struct hda_codec *codec, int outs,
3157 			    const hda_nid_t *pins, hda_nid_t *dacs)
3158 {
3159 	int i;
3160 	bool found = false;
3161 	for (i = 0; i < outs; i++) {
3162 		if (dacs[i])
3163 			continue;
3164 		dacs[i] = get_dac_if_single(codec, pins[i]);
3165 		if (dacs[i])
3166 			found = true;
3167 	}
3168 	return found;
3169 }
3170 
3171 /* fill in the dac_nids table from the parsed pin configuration */
3172 static int fill_and_eval_dacs(struct hda_codec *codec,
3173 			      bool fill_hardwired,
3174 			      bool fill_mio_first)
3175 {
3176 	struct alc_spec *spec = codec->spec;
3177 	struct auto_pin_cfg *cfg = &spec->autocfg;
3178 	int i, err, badness;
3179 
3180 	/* set num_dacs once to full for alc_auto_look_for_dac() */
3181 	spec->multiout.num_dacs = cfg->line_outs;
3182 	spec->multiout.dac_nids = spec->private_dac_nids;
3183 	memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
3184 	memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
3185 	memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
3186 	spec->multi_ios = 0;
3187 	clear_vol_marks(codec);
3188 	badness = 0;
3189 
3190 	/* fill hard-wired DACs first */
3191 	if (fill_hardwired) {
3192 		bool mapped;
3193 		do {
3194 			mapped = alc_map_singles(codec, cfg->line_outs,
3195 						 cfg->line_out_pins,
3196 						 spec->private_dac_nids);
3197 			mapped |= alc_map_singles(codec, cfg->hp_outs,
3198 						  cfg->hp_pins,
3199 						  spec->multiout.hp_out_nid);
3200 			mapped |= alc_map_singles(codec, cfg->speaker_outs,
3201 						  cfg->speaker_pins,
3202 						  spec->multiout.extra_out_nid);
3203 			if (fill_mio_first && cfg->line_outs == 1 &&
3204 			    cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3205 				err = alc_auto_fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
3206 				if (!err)
3207 					mapped = true;
3208 			}
3209 		} while (mapped);
3210 	}
3211 
3212 	badness += alc_auto_fill_dacs(codec, cfg->line_outs, cfg->line_out_pins,
3213 				      spec->private_dac_nids,
3214 				      &main_out_badness);
3215 
3216 	/* re-count num_dacs and squash invalid entries */
3217 	spec->multiout.num_dacs = 0;
3218 	for (i = 0; i < cfg->line_outs; i++) {
3219 		if (spec->private_dac_nids[i])
3220 			spec->multiout.num_dacs++;
3221 		else {
3222 			memmove(spec->private_dac_nids + i,
3223 				spec->private_dac_nids + i + 1,
3224 				sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
3225 			spec->private_dac_nids[cfg->line_outs - 1] = 0;
3226 		}
3227 	}
3228 
3229 	if (fill_mio_first &&
3230 	    cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3231 		/* try to fill multi-io first */
3232 		err = alc_auto_fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
3233 		if (err < 0)
3234 			return err;
3235 		/* we don't count badness at this stage yet */
3236 	}
3237 
3238 	if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3239 		err = alc_auto_fill_dacs(codec, cfg->hp_outs, cfg->hp_pins,
3240 					 spec->multiout.hp_out_nid,
3241 					 &extra_out_badness);
3242 		if (err < 0)
3243 			return err;
3244 		badness += err;
3245 	}
3246 	if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3247 		err = alc_auto_fill_dacs(codec, cfg->speaker_outs,
3248 					 cfg->speaker_pins,
3249 					 spec->multiout.extra_out_nid,
3250 					 &extra_out_badness);
3251 		if (err < 0)
3252 			return err;
3253 		badness += err;
3254 	}
3255 	if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3256 		err = alc_auto_fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
3257 		if (err < 0)
3258 			return err;
3259 		badness += err;
3260 	}
3261 	if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3262 		/* try multi-ios with HP + inputs */
3263 		int offset = 0;
3264 		if (cfg->line_outs >= 3)
3265 			offset = 1;
3266 		err = alc_auto_fill_multi_ios(codec, cfg->hp_pins[0], false,
3267 					      offset);
3268 		if (err < 0)
3269 			return err;
3270 		badness += err;
3271 	}
3272 
3273 	if (spec->multi_ios == 2) {
3274 		for (i = 0; i < 2; i++)
3275 			spec->private_dac_nids[spec->multiout.num_dacs++] =
3276 				spec->multi_io[i].dac;
3277 		spec->ext_channel_count = 2;
3278 	} else if (spec->multi_ios) {
3279 		spec->multi_ios = 0;
3280 		badness += BAD_MULTI_IO;
3281 	}
3282 
3283 	return badness;
3284 }
3285 
3286 #define DEBUG_BADNESS
3287 
3288 #ifdef DEBUG_BADNESS
3289 #define debug_badness	snd_printdd
3290 #else
3291 #define debug_badness(...)
3292 #endif
3293 
3294 static void debug_show_configs(struct alc_spec *spec, struct auto_pin_cfg *cfg)
3295 {
3296 	debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
3297 		      cfg->line_out_pins[0], cfg->line_out_pins[1],
3298 		      cfg->line_out_pins[2], cfg->line_out_pins[2],
3299 		      spec->multiout.dac_nids[0],
3300 		      spec->multiout.dac_nids[1],
3301 		      spec->multiout.dac_nids[2],
3302 		      spec->multiout.dac_nids[3]);
3303 	if (spec->multi_ios > 0)
3304 		debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
3305 			      spec->multi_ios,
3306 			      spec->multi_io[0].pin, spec->multi_io[1].pin,
3307 			      spec->multi_io[0].dac, spec->multi_io[1].dac);
3308 	debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
3309 		      cfg->hp_pins[0], cfg->hp_pins[1],
3310 		      cfg->hp_pins[2], cfg->hp_pins[2],
3311 		      spec->multiout.hp_out_nid[0],
3312 		      spec->multiout.hp_out_nid[1],
3313 		      spec->multiout.hp_out_nid[2],
3314 		      spec->multiout.hp_out_nid[3]);
3315 	debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
3316 		      cfg->speaker_pins[0], cfg->speaker_pins[1],
3317 		      cfg->speaker_pins[2], cfg->speaker_pins[3],
3318 		      spec->multiout.extra_out_nid[0],
3319 		      spec->multiout.extra_out_nid[1],
3320 		      spec->multiout.extra_out_nid[2],
3321 		      spec->multiout.extra_out_nid[3]);
3322 }
3323 
3324 static int alc_auto_fill_dac_nids(struct hda_codec *codec)
3325 {
3326 	struct alc_spec *spec = codec->spec;
3327 	struct auto_pin_cfg *cfg = &spec->autocfg;
3328 	struct auto_pin_cfg *best_cfg;
3329 	int best_badness = INT_MAX;
3330 	int badness;
3331 	bool fill_hardwired = true, fill_mio_first = true;
3332 	bool best_wired = true, best_mio = true;
3333 	bool hp_spk_swapped = false;
3334 
3335 	best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
3336 	if (!best_cfg)
3337 		return -ENOMEM;
3338 	*best_cfg = *cfg;
3339 
3340 	for (;;) {
3341 		badness = fill_and_eval_dacs(codec, fill_hardwired,
3342 					     fill_mio_first);
3343 		if (badness < 0) {
3344 			kfree(best_cfg);
3345 			return badness;
3346 		}
3347 		debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
3348 			      cfg->line_out_type, fill_hardwired, fill_mio_first,
3349 			      badness);
3350 		debug_show_configs(spec, cfg);
3351 		if (badness < best_badness) {
3352 			best_badness = badness;
3353 			*best_cfg = *cfg;
3354 			best_wired = fill_hardwired;
3355 			best_mio = fill_mio_first;
3356 		}
3357 		if (!badness)
3358 			break;
3359 		fill_mio_first = !fill_mio_first;
3360 		if (!fill_mio_first)
3361 			continue;
3362 		fill_hardwired = !fill_hardwired;
3363 		if (!fill_hardwired)
3364 			continue;
3365 		if (hp_spk_swapped)
3366 			break;
3367 		hp_spk_swapped = true;
3368 		if (cfg->speaker_outs > 0 &&
3369 		    cfg->line_out_type == AUTO_PIN_HP_OUT) {
3370 			cfg->hp_outs = cfg->line_outs;
3371 			memcpy(cfg->hp_pins, cfg->line_out_pins,
3372 			       sizeof(cfg->hp_pins));
3373 			cfg->line_outs = cfg->speaker_outs;
3374 			memcpy(cfg->line_out_pins, cfg->speaker_pins,
3375 			       sizeof(cfg->speaker_pins));
3376 			cfg->speaker_outs = 0;
3377 			memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3378 			cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3379 			fill_hardwired = true;
3380 			continue;
3381 		}
3382 		if (cfg->hp_outs > 0 &&
3383 		    cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3384 			cfg->speaker_outs = cfg->line_outs;
3385 			memcpy(cfg->speaker_pins, cfg->line_out_pins,
3386 			       sizeof(cfg->speaker_pins));
3387 			cfg->line_outs = cfg->hp_outs;
3388 			memcpy(cfg->line_out_pins, cfg->hp_pins,
3389 			       sizeof(cfg->hp_pins));
3390 			cfg->hp_outs = 0;
3391 			memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3392 			cfg->line_out_type = AUTO_PIN_HP_OUT;
3393 			fill_hardwired = true;
3394 			continue;
3395 		}
3396 		break;
3397 	}
3398 
3399 	if (badness) {
3400 		*cfg = *best_cfg;
3401 		fill_and_eval_dacs(codec, best_wired, best_mio);
3402 	}
3403 	debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
3404 		      cfg->line_out_type, best_wired, best_mio);
3405 	debug_show_configs(spec, cfg);
3406 
3407 	if (cfg->line_out_pins[0])
3408 		spec->vmaster_nid =
3409 			alc_look_for_out_vol_nid(codec, cfg->line_out_pins[0],
3410 						 spec->multiout.dac_nids[0]);
3411 
3412 	/* clear the bitmap flags for creating controls */
3413 	clear_vol_marks(codec);
3414 	kfree(best_cfg);
3415 	return 0;
3416 }
3417 
3418 static int alc_auto_add_vol_ctl(struct hda_codec *codec,
3419 			      const char *pfx, int cidx,
3420 			      hda_nid_t nid, unsigned int chs)
3421 {
3422 	struct alc_spec *spec = codec->spec;
3423 	unsigned int val;
3424 	if (!nid)
3425 		return 0;
3426 	val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT);
3427 	if (is_ctl_used(spec->vol_ctls, val) && chs != 2) /* exclude LFE */
3428 		return 0;
3429 	mark_ctl_usage(spec->vol_ctls, val);
3430 	return __add_pb_vol_ctrl(codec->spec, ALC_CTL_WIDGET_VOL, pfx, cidx,
3431 				 val);
3432 }
3433 
3434 static int alc_auto_add_stereo_vol(struct hda_codec *codec,
3435 				   const char *pfx, int cidx,
3436 				   hda_nid_t nid)
3437 {
3438 	int chs = 1;
3439 	if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
3440 		chs = 3;
3441 	return alc_auto_add_vol_ctl(codec, pfx, cidx, nid, chs);
3442 }
3443 
3444 /* create a mute-switch for the given mixer widget;
3445  * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
3446  */
3447 static int alc_auto_add_sw_ctl(struct hda_codec *codec,
3448 			     const char *pfx, int cidx,
3449 			     hda_nid_t nid, unsigned int chs)
3450 {
3451 	struct alc_spec *spec = codec->spec;
3452 	int wid_type;
3453 	int type;
3454 	unsigned long val;
3455 	if (!nid)
3456 		return 0;
3457 	wid_type = get_wcaps_type(get_wcaps(codec, nid));
3458 	if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT) {
3459 		type = ALC_CTL_WIDGET_MUTE;
3460 		val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT);
3461 	} else if (snd_hda_get_num_conns(codec, nid) == 1) {
3462 		type = ALC_CTL_WIDGET_MUTE;
3463 		val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_INPUT);
3464 	} else {
3465 		type = ALC_CTL_BIND_MUTE;
3466 		val = HDA_COMPOSE_AMP_VAL(nid, chs, 2, HDA_INPUT);
3467 	}
3468 	if (is_ctl_used(spec->sw_ctls, val) && chs != 2) /* exclude LFE */
3469 		return 0;
3470 	mark_ctl_usage(spec->sw_ctls, val);
3471 	return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
3472 }
3473 
3474 static int alc_auto_add_stereo_sw(struct hda_codec *codec, const char *pfx,
3475 				  int cidx, hda_nid_t nid)
3476 {
3477 	int chs = 1;
3478 	if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
3479 		chs = 3;
3480 	return alc_auto_add_sw_ctl(codec, pfx, cidx, nid, chs);
3481 }
3482 
3483 static hda_nid_t alc_look_for_out_mute_nid(struct hda_codec *codec,
3484 					   hda_nid_t pin, hda_nid_t dac)
3485 {
3486 	hda_nid_t mix = alc_auto_dac_to_mix(codec, pin, dac);
3487 	if (nid_has_mute(codec, pin, HDA_OUTPUT))
3488 		return pin;
3489 	else if (mix && nid_has_mute(codec, mix, HDA_INPUT))
3490 		return mix;
3491 	else if (nid_has_mute(codec, dac, HDA_OUTPUT))
3492 		return dac;
3493 	return 0;
3494 }
3495 
3496 static hda_nid_t alc_look_for_out_vol_nid(struct hda_codec *codec,
3497 					  hda_nid_t pin, hda_nid_t dac)
3498 {
3499 	hda_nid_t mix = alc_auto_dac_to_mix(codec, pin, dac);
3500 	if (nid_has_volume(codec, dac, HDA_OUTPUT))
3501 		return dac;
3502 	else if (nid_has_volume(codec, mix, HDA_OUTPUT))
3503 		return mix;
3504 	else if (nid_has_volume(codec, pin, HDA_OUTPUT))
3505 		return pin;
3506 	return 0;
3507 }
3508 
3509 /* add playback controls from the parsed DAC table */
3510 static int alc_auto_create_multi_out_ctls(struct hda_codec *codec,
3511 					     const struct auto_pin_cfg *cfg)
3512 {
3513 	struct alc_spec *spec = codec->spec;
3514 	int i, err, noutputs;
3515 
3516 	noutputs = cfg->line_outs;
3517 	if (spec->multi_ios > 0 && cfg->line_outs < 3)
3518 		noutputs += spec->multi_ios;
3519 
3520 	for (i = 0; i < noutputs; i++) {
3521 		const char *name;
3522 		int index;
3523 		hda_nid_t dac, pin;
3524 		hda_nid_t sw, vol;
3525 
3526 		dac = spec->multiout.dac_nids[i];
3527 		if (!dac)
3528 			continue;
3529 		if (i >= cfg->line_outs) {
3530 			pin = spec->multi_io[i - 1].pin;
3531 			index = 0;
3532 			name = channel_name[i];
3533 		} else {
3534 			pin = cfg->line_out_pins[i];
3535 			name = alc_get_line_out_pfx(spec, i, true, &index);
3536 		}
3537 
3538 		sw = alc_look_for_out_mute_nid(codec, pin, dac);
3539 		vol = alc_look_for_out_vol_nid(codec, pin, dac);
3540 		if (!name || !strcmp(name, "CLFE")) {
3541 			/* Center/LFE */
3542 			err = alc_auto_add_vol_ctl(codec, "Center", 0, vol, 1);
3543 			if (err < 0)
3544 				return err;
3545 			err = alc_auto_add_vol_ctl(codec, "LFE", 0, vol, 2);
3546 			if (err < 0)
3547 				return err;
3548 			err = alc_auto_add_sw_ctl(codec, "Center", 0, sw, 1);
3549 			if (err < 0)
3550 				return err;
3551 			err = alc_auto_add_sw_ctl(codec, "LFE", 0, sw, 2);
3552 			if (err < 0)
3553 				return err;
3554 		} else {
3555 			err = alc_auto_add_stereo_vol(codec, name, index, vol);
3556 			if (err < 0)
3557 				return err;
3558 			err = alc_auto_add_stereo_sw(codec, name, index, sw);
3559 			if (err < 0)
3560 				return err;
3561 		}
3562 	}
3563 	return 0;
3564 }
3565 
3566 static int alc_auto_create_extra_out(struct hda_codec *codec, hda_nid_t pin,
3567 				     hda_nid_t dac, const char *pfx,
3568 				     int cidx)
3569 {
3570 	struct alc_spec *spec = codec->spec;
3571 	hda_nid_t sw, vol;
3572 	int err;
3573 
3574 	if (!dac) {
3575 		unsigned int val;
3576 		/* the corresponding DAC is already occupied */
3577 		if (!(get_wcaps(codec, pin) & AC_WCAP_OUT_AMP))
3578 			return 0; /* no way */
3579 		/* create a switch only */
3580 		val = HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT);
3581 		if (is_ctl_used(spec->sw_ctls, val))
3582 			return 0; /* already created */
3583 		mark_ctl_usage(spec->sw_ctls, val);
3584 		return __add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, pfx, cidx, val);
3585 	}
3586 
3587 	sw = alc_look_for_out_mute_nid(codec, pin, dac);
3588 	vol = alc_look_for_out_vol_nid(codec, pin, dac);
3589 	err = alc_auto_add_stereo_vol(codec, pfx, cidx, vol);
3590 	if (err < 0)
3591 		return err;
3592 	err = alc_auto_add_stereo_sw(codec, pfx, cidx, sw);
3593 	if (err < 0)
3594 		return err;
3595 	return 0;
3596 }
3597 
3598 static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
3599 					  unsigned int nums,
3600 					  struct hda_ctl_ops *ops)
3601 {
3602 	struct alc_spec *spec = codec->spec;
3603 	struct hda_bind_ctls **ctlp, *ctl;
3604 	snd_array_init(&spec->bind_ctls, sizeof(ctl), 8);
3605 	ctlp = snd_array_new(&spec->bind_ctls);
3606 	if (!ctlp)
3607 		return NULL;
3608 	ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
3609 	*ctlp = ctl;
3610 	if (ctl)
3611 		ctl->ops = ops;
3612 	return ctl;
3613 }
3614 
3615 /* add playback controls for speaker and HP outputs */
3616 static int alc_auto_create_extra_outs(struct hda_codec *codec, int num_pins,
3617 				      const hda_nid_t *pins,
3618 				      const hda_nid_t *dacs,
3619 				      const char *pfx)
3620 {
3621 	struct alc_spec *spec = codec->spec;
3622 	struct hda_bind_ctls *ctl;
3623 	char name[32];
3624 	int i, n, err;
3625 
3626 	if (!num_pins || !pins[0])
3627 		return 0;
3628 
3629 	if (num_pins == 1) {
3630 		hda_nid_t dac = *dacs;
3631 		if (!dac)
3632 			dac = spec->multiout.dac_nids[0];
3633 		return alc_auto_create_extra_out(codec, *pins, dac, pfx, 0);
3634 	}
3635 
3636 	for (i = 0; i < num_pins; i++) {
3637 		hda_nid_t dac;
3638 		if (dacs[num_pins - 1])
3639 			dac = dacs[i]; /* with individual volumes */
3640 		else
3641 			dac = 0;
3642 		if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
3643 			err = alc_auto_create_extra_out(codec, pins[i], dac,
3644 							"Bass Speaker", 0);
3645 		} else if (num_pins >= 3) {
3646 			snprintf(name, sizeof(name), "%s %s",
3647 				 pfx, channel_name[i]);
3648 			err = alc_auto_create_extra_out(codec, pins[i], dac,
3649 							name, 0);
3650 		} else {
3651 			err = alc_auto_create_extra_out(codec, pins[i], dac,
3652 							pfx, i);
3653 		}
3654 		if (err < 0)
3655 			return err;
3656 	}
3657 	if (dacs[num_pins - 1])
3658 		return 0;
3659 
3660 	/* Let's create a bind-controls for volumes */
3661 	ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
3662 	if (!ctl)
3663 		return -ENOMEM;
3664 	n = 0;
3665 	for (i = 0; i < num_pins; i++) {
3666 		hda_nid_t vol;
3667 		if (!pins[i] || !dacs[i])
3668 			continue;
3669 		vol = alc_look_for_out_vol_nid(codec, pins[i], dacs[i]);
3670 		if (vol)
3671 			ctl->values[n++] =
3672 				HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
3673 	}
3674 	if (n) {
3675 		snprintf(name, sizeof(name), "%s Playback Volume", pfx);
3676 		err = add_control(spec, ALC_CTL_BIND_VOL, name, 0, (long)ctl);
3677 		if (err < 0)
3678 			return err;
3679 	}
3680 	return 0;
3681 }
3682 
3683 static int alc_auto_create_hp_out(struct hda_codec *codec)
3684 {
3685 	struct alc_spec *spec = codec->spec;
3686 	return alc_auto_create_extra_outs(codec, spec->autocfg.hp_outs,
3687 					  spec->autocfg.hp_pins,
3688 					  spec->multiout.hp_out_nid,
3689 					  "Headphone");
3690 }
3691 
3692 static int alc_auto_create_speaker_out(struct hda_codec *codec)
3693 {
3694 	struct alc_spec *spec = codec->spec;
3695 	return alc_auto_create_extra_outs(codec, spec->autocfg.speaker_outs,
3696 					  spec->autocfg.speaker_pins,
3697 					  spec->multiout.extra_out_nid,
3698 					  "Speaker");
3699 }
3700 
3701 static void alc_auto_set_output_and_unmute(struct hda_codec *codec,
3702 					      hda_nid_t pin, int pin_type,
3703 					      hda_nid_t dac)
3704 {
3705 	int i, num;
3706 	hda_nid_t nid, mix = 0;
3707 	hda_nid_t srcs[HDA_MAX_CONNECTIONS];
3708 
3709 	alc_set_pin_output(codec, pin, pin_type);
3710 	nid = alc_go_down_to_selector(codec, pin);
3711 	num = snd_hda_get_connections(codec, nid, srcs, ARRAY_SIZE(srcs));
3712 	for (i = 0; i < num; i++) {
3713 		if (alc_auto_mix_to_dac(codec, srcs[i]) != dac)
3714 			continue;
3715 		mix = srcs[i];
3716 		break;
3717 	}
3718 	if (!mix)
3719 		return;
3720 
3721 	/* need the manual connection? */
3722 	if (num > 1)
3723 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, i);
3724 	/* unmute mixer widget inputs */
3725 	if (nid_has_mute(codec, mix, HDA_INPUT)) {
3726 		snd_hda_codec_write(codec, mix, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3727 			    AMP_IN_UNMUTE(0));
3728 		snd_hda_codec_write(codec, mix, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3729 			    AMP_IN_UNMUTE(1));
3730 	}
3731 	/* initialize volume */
3732 	nid = alc_look_for_out_vol_nid(codec, pin, dac);
3733 	if (nid)
3734 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3735 				    AMP_OUT_ZERO);
3736 
3737 	/* unmute DAC if it's not assigned to a mixer */
3738 	nid = alc_look_for_out_mute_nid(codec, pin, dac);
3739 	if (nid == mix && nid_has_mute(codec, dac, HDA_OUTPUT))
3740 		snd_hda_codec_write(codec, dac, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3741 				    AMP_OUT_ZERO);
3742 }
3743 
3744 static void alc_auto_init_multi_out(struct hda_codec *codec)
3745 {
3746 	struct alc_spec *spec = codec->spec;
3747 	int pin_type = get_pin_type(spec->autocfg.line_out_type);
3748 	int i;
3749 
3750 	for (i = 0; i <= HDA_SIDE; i++) {
3751 		hda_nid_t nid = spec->autocfg.line_out_pins[i];
3752 		if (nid)
3753 			alc_auto_set_output_and_unmute(codec, nid, pin_type,
3754 					spec->multiout.dac_nids[i]);
3755 	}
3756 }
3757 
3758 static void alc_auto_init_extra_out(struct hda_codec *codec)
3759 {
3760 	struct alc_spec *spec = codec->spec;
3761 	int i;
3762 	hda_nid_t pin, dac;
3763 
3764 	for (i = 0; i < spec->autocfg.hp_outs; i++) {
3765 		if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3766 			break;
3767 		pin = spec->autocfg.hp_pins[i];
3768 		if (!pin)
3769 			break;
3770 		dac = spec->multiout.hp_out_nid[i];
3771 		if (!dac) {
3772 			if (i > 0 && spec->multiout.hp_out_nid[0])
3773 				dac = spec->multiout.hp_out_nid[0];
3774 			else
3775 				dac = spec->multiout.dac_nids[0];
3776 		}
3777 		alc_auto_set_output_and_unmute(codec, pin, PIN_HP, dac);
3778 	}
3779 	for (i = 0; i < spec->autocfg.speaker_outs; i++) {
3780 		if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3781 			break;
3782 		pin = spec->autocfg.speaker_pins[i];
3783 		if (!pin)
3784 			break;
3785 		dac = spec->multiout.extra_out_nid[i];
3786 		if (!dac) {
3787 			if (i > 0 && spec->multiout.extra_out_nid[0])
3788 				dac = spec->multiout.extra_out_nid[0];
3789 			else
3790 				dac = spec->multiout.dac_nids[0];
3791 		}
3792 		alc_auto_set_output_and_unmute(codec, pin, PIN_OUT, dac);
3793 	}
3794 }
3795 
3796 /* check whether the given pin can be a multi-io pin */
3797 static bool can_be_multiio_pin(struct hda_codec *codec,
3798 			       unsigned int location, hda_nid_t nid)
3799 {
3800 	unsigned int defcfg, caps;
3801 
3802 	defcfg = snd_hda_codec_get_pincfg(codec, nid);
3803 	if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
3804 		return false;
3805 	if (location && get_defcfg_location(defcfg) != location)
3806 		return false;
3807 	caps = snd_hda_query_pin_caps(codec, nid);
3808 	if (!(caps & AC_PINCAP_OUT))
3809 		return false;
3810 	return true;
3811 }
3812 
3813 /*
3814  * multi-io helper
3815  *
3816  * When hardwired is set, try to fill ony hardwired pins, and returns
3817  * zero if any pins are filled, non-zero if nothing found.
3818  * When hardwired is off, try to fill possible input pins, and returns
3819  * the badness value.
3820  */
3821 static int alc_auto_fill_multi_ios(struct hda_codec *codec,
3822 				   hda_nid_t reference_pin,
3823 				   bool hardwired, int offset)
3824 {
3825 	struct alc_spec *spec = codec->spec;
3826 	struct auto_pin_cfg *cfg = &spec->autocfg;
3827 	int type, i, j, dacs, num_pins, old_pins;
3828 	unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
3829 	unsigned int location = get_defcfg_location(defcfg);
3830 	int badness = 0;
3831 
3832 	old_pins = spec->multi_ios;
3833 	if (old_pins >= 2)
3834 		goto end_fill;
3835 
3836 	num_pins = 0;
3837 	for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
3838 		for (i = 0; i < cfg->num_inputs; i++) {
3839 			if (cfg->inputs[i].type != type)
3840 				continue;
3841 			if (can_be_multiio_pin(codec, location,
3842 					       cfg->inputs[i].pin))
3843 				num_pins++;
3844 		}
3845 	}
3846 	if (num_pins < 2)
3847 		goto end_fill;
3848 
3849 	dacs = spec->multiout.num_dacs;
3850 	for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
3851 		for (i = 0; i < cfg->num_inputs; i++) {
3852 			hda_nid_t nid = cfg->inputs[i].pin;
3853 			hda_nid_t dac = 0;
3854 
3855 			if (cfg->inputs[i].type != type)
3856 				continue;
3857 			if (!can_be_multiio_pin(codec, location, nid))
3858 				continue;
3859 			for (j = 0; j < spec->multi_ios; j++) {
3860 				if (nid == spec->multi_io[j].pin)
3861 					break;
3862 			}
3863 			if (j < spec->multi_ios)
3864 				continue;
3865 
3866 			if (offset && offset + spec->multi_ios < dacs) {
3867 				dac = spec->private_dac_nids[offset + spec->multi_ios];
3868 				if (!alc_auto_is_dac_reachable(codec, nid, dac))
3869 					dac = 0;
3870 			}
3871 			if (hardwired)
3872 				dac = get_dac_if_single(codec, nid);
3873 			else if (!dac)
3874 				dac = alc_auto_look_for_dac(codec, nid);
3875 			if (!dac) {
3876 				badness++;
3877 				continue;
3878 			}
3879 			spec->multi_io[spec->multi_ios].pin = nid;
3880 			spec->multi_io[spec->multi_ios].dac = dac;
3881 			spec->multi_ios++;
3882 			if (spec->multi_ios >= 2)
3883 				break;
3884 		}
3885 	}
3886  end_fill:
3887 	if (badness)
3888 		badness = BAD_MULTI_IO;
3889 	if (old_pins == spec->multi_ios) {
3890 		if (hardwired)
3891 			return 1; /* nothing found */
3892 		else
3893 			return badness; /* no badness if nothing found */
3894 	}
3895 	if (!hardwired && spec->multi_ios < 2) {
3896 		spec->multi_ios = old_pins;
3897 		return badness;
3898 	}
3899 
3900 	return 0;
3901 }
3902 
3903 static int alc_auto_ch_mode_info(struct snd_kcontrol *kcontrol,
3904 				 struct snd_ctl_elem_info *uinfo)
3905 {
3906 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3907 	struct alc_spec *spec = codec->spec;
3908 
3909 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3910 	uinfo->count = 1;
3911 	uinfo->value.enumerated.items = spec->multi_ios + 1;
3912 	if (uinfo->value.enumerated.item > spec->multi_ios)
3913 		uinfo->value.enumerated.item = spec->multi_ios;
3914 	sprintf(uinfo->value.enumerated.name, "%dch",
3915 		(uinfo->value.enumerated.item + 1) * 2);
3916 	return 0;
3917 }
3918 
3919 static int alc_auto_ch_mode_get(struct snd_kcontrol *kcontrol,
3920 				struct snd_ctl_elem_value *ucontrol)
3921 {
3922 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3923 	struct alc_spec *spec = codec->spec;
3924 	ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
3925 	return 0;
3926 }
3927 
3928 static int alc_set_multi_io(struct hda_codec *codec, int idx, bool output)
3929 {
3930 	struct alc_spec *spec = codec->spec;
3931 	hda_nid_t nid = spec->multi_io[idx].pin;
3932 
3933 	if (!spec->multi_io[idx].ctl_in)
3934 		spec->multi_io[idx].ctl_in =
3935 			snd_hda_codec_read(codec, nid, 0,
3936 					   AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3937 	if (output) {
3938 		snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
3939 		if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
3940 			snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3941 						 HDA_AMP_MUTE, 0);
3942 		alc_auto_select_dac(codec, nid, spec->multi_io[idx].dac);
3943 	} else {
3944 		if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
3945 			snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3946 						 HDA_AMP_MUTE, HDA_AMP_MUTE);
3947 		snd_hda_set_pin_ctl_cache(codec, nid,
3948 					  spec->multi_io[idx].ctl_in);
3949 	}
3950 	return 0;
3951 }
3952 
3953 static int alc_auto_ch_mode_put(struct snd_kcontrol *kcontrol,
3954 				struct snd_ctl_elem_value *ucontrol)
3955 {
3956 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3957 	struct alc_spec *spec = codec->spec;
3958 	int i, ch;
3959 
3960 	ch = ucontrol->value.enumerated.item[0];
3961 	if (ch < 0 || ch > spec->multi_ios)
3962 		return -EINVAL;
3963 	if (ch == (spec->ext_channel_count - 1) / 2)
3964 		return 0;
3965 	spec->ext_channel_count = (ch + 1) * 2;
3966 	for (i = 0; i < spec->multi_ios; i++)
3967 		alc_set_multi_io(codec, i, i < ch);
3968 	spec->multiout.max_channels = spec->ext_channel_count;
3969 	if (spec->need_dac_fix && !spec->const_channel_count)
3970 		spec->multiout.num_dacs = spec->multiout.max_channels / 2;
3971 	return 1;
3972 }
3973 
3974 static const struct snd_kcontrol_new alc_auto_channel_mode_enum = {
3975 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3976 	.name = "Channel Mode",
3977 	.info = alc_auto_ch_mode_info,
3978 	.get = alc_auto_ch_mode_get,
3979 	.put = alc_auto_ch_mode_put,
3980 };
3981 
3982 static int alc_auto_add_multi_channel_mode(struct hda_codec *codec)
3983 {
3984 	struct alc_spec *spec = codec->spec;
3985 
3986 	if (spec->multi_ios > 0) {
3987 		struct snd_kcontrol_new *knew;
3988 
3989 		knew = alc_kcontrol_new(spec);
3990 		if (!knew)
3991 			return -ENOMEM;
3992 		*knew = alc_auto_channel_mode_enum;
3993 		knew->name = kstrdup("Channel Mode", GFP_KERNEL);
3994 		if (!knew->name)
3995 			return -ENOMEM;
3996 	}
3997 	return 0;
3998 }
3999 
4000 /* filter out invalid adc_nids (and capsrc_nids) that don't give all
4001  * active input pins
4002  */
4003 static void alc_remove_invalid_adc_nids(struct hda_codec *codec)
4004 {
4005 	struct alc_spec *spec = codec->spec;
4006 	const struct hda_input_mux *imux;
4007 	hda_nid_t adc_nids[ARRAY_SIZE(spec->private_adc_nids)];
4008 	hda_nid_t capsrc_nids[ARRAY_SIZE(spec->private_adc_nids)];
4009 	int i, n, nums;
4010 
4011 	imux = spec->input_mux;
4012 	if (!imux)
4013 		return;
4014 	if (spec->dyn_adc_switch)
4015 		return;
4016 
4017  again:
4018 	nums = 0;
4019 	for (n = 0; n < spec->num_adc_nids; n++) {
4020 		hda_nid_t cap = spec->private_capsrc_nids[n];
4021 		int num_conns = snd_hda_get_num_conns(codec, cap);
4022 		for (i = 0; i < imux->num_items; i++) {
4023 			hda_nid_t pin = spec->imux_pins[i];
4024 			if (pin) {
4025 				if (get_connection_index(codec, cap, pin) < 0)
4026 					break;
4027 			} else if (num_conns <= imux->items[i].index)
4028 				break;
4029 		}
4030 		if (i >= imux->num_items) {
4031 			adc_nids[nums] = spec->private_adc_nids[n];
4032 			capsrc_nids[nums++] = cap;
4033 		}
4034 	}
4035 	if (!nums) {
4036 		/* check whether ADC-switch is possible */
4037 		if (!alc_check_dyn_adc_switch(codec)) {
4038 			if (spec->shared_mic_hp) {
4039 				spec->shared_mic_hp = 0;
4040 				spec->private_imux[0].num_items = 1;
4041 				goto again;
4042 			}
4043 			printk(KERN_WARNING "hda_codec: %s: no valid ADC found;"
4044 			       " using fallback 0x%x\n",
4045 			       codec->chip_name, spec->private_adc_nids[0]);
4046 			spec->num_adc_nids = 1;
4047 			spec->auto_mic = 0;
4048 			return;
4049 		}
4050 	} else if (nums != spec->num_adc_nids) {
4051 		memcpy(spec->private_adc_nids, adc_nids,
4052 		       nums * sizeof(hda_nid_t));
4053 		memcpy(spec->private_capsrc_nids, capsrc_nids,
4054 		       nums * sizeof(hda_nid_t));
4055 		spec->num_adc_nids = nums;
4056 	}
4057 
4058 	if (spec->auto_mic)
4059 		alc_auto_mic_check_imux(codec); /* check auto-mic setups */
4060 	else if (spec->input_mux->num_items == 1 || spec->shared_mic_hp)
4061 		spec->num_adc_nids = 1; /* reduce to a single ADC */
4062 }
4063 
4064 /*
4065  * initialize ADC paths
4066  */
4067 static void alc_auto_init_adc(struct hda_codec *codec, int adc_idx)
4068 {
4069 	struct alc_spec *spec = codec->spec;
4070 	hda_nid_t nid;
4071 
4072 	nid = spec->adc_nids[adc_idx];
4073 	/* mute ADC */
4074 	if (nid_has_mute(codec, nid, HDA_INPUT)) {
4075 		snd_hda_codec_write(codec, nid, 0,
4076 				    AC_VERB_SET_AMP_GAIN_MUTE,
4077 				    AMP_IN_MUTE(0));
4078 		return;
4079 	}
4080 	if (!spec->capsrc_nids)
4081 		return;
4082 	nid = spec->capsrc_nids[adc_idx];
4083 	if (nid_has_mute(codec, nid, HDA_OUTPUT))
4084 		snd_hda_codec_write(codec, nid, 0,
4085 				    AC_VERB_SET_AMP_GAIN_MUTE,
4086 				    AMP_OUT_MUTE);
4087 }
4088 
4089 static void alc_auto_init_input_src(struct hda_codec *codec)
4090 {
4091 	struct alc_spec *spec = codec->spec;
4092 	int c, nums;
4093 
4094 	for (c = 0; c < spec->num_adc_nids; c++)
4095 		alc_auto_init_adc(codec, c);
4096 	if (spec->dyn_adc_switch)
4097 		nums = 1;
4098 	else
4099 		nums = spec->num_adc_nids;
4100 	for (c = 0; c < nums; c++)
4101 		alc_mux_select(codec, c, spec->cur_mux[c], true);
4102 }
4103 
4104 /* add mic boosts if needed */
4105 static int alc_auto_add_mic_boost(struct hda_codec *codec)
4106 {
4107 	struct alc_spec *spec = codec->spec;
4108 	struct auto_pin_cfg *cfg = &spec->autocfg;
4109 	int i, err;
4110 	int type_idx = 0;
4111 	hda_nid_t nid;
4112 	const char *prev_label = NULL;
4113 
4114 	for (i = 0; i < cfg->num_inputs; i++) {
4115 		if (cfg->inputs[i].type > AUTO_PIN_MIC)
4116 			break;
4117 		nid = cfg->inputs[i].pin;
4118 		if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
4119 			const char *label;
4120 			char boost_label[32];
4121 
4122 			label = hda_get_autocfg_input_label(codec, cfg, i);
4123 			if (spec->shared_mic_hp && !strcmp(label, "Misc"))
4124 				label = "Headphone Mic";
4125 			if (prev_label && !strcmp(label, prev_label))
4126 				type_idx++;
4127 			else
4128 				type_idx = 0;
4129 			prev_label = label;
4130 
4131 			snprintf(boost_label, sizeof(boost_label),
4132 				 "%s Boost Volume", label);
4133 			err = add_control(spec, ALC_CTL_WIDGET_VOL,
4134 					  boost_label, type_idx,
4135 				  HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT));
4136 			if (err < 0)
4137 				return err;
4138 		}
4139 	}
4140 	return 0;
4141 }
4142 
4143 /* select or unmute the given capsrc route */
4144 static void select_or_unmute_capsrc(struct hda_codec *codec, hda_nid_t cap,
4145 				    int idx)
4146 {
4147 	if (get_wcaps_type(get_wcaps(codec, cap)) == AC_WID_AUD_MIX) {
4148 		snd_hda_codec_amp_stereo(codec, cap, HDA_INPUT, idx,
4149 					 HDA_AMP_MUTE, 0);
4150 	} else if (snd_hda_get_num_conns(codec, cap) > 1) {
4151 		snd_hda_codec_write_cache(codec, cap, 0,
4152 					  AC_VERB_SET_CONNECT_SEL, idx);
4153 	}
4154 }
4155 
4156 /* set the default connection to that pin */
4157 static int init_capsrc_for_pin(struct hda_codec *codec, hda_nid_t pin)
4158 {
4159 	struct alc_spec *spec = codec->spec;
4160 	int i;
4161 
4162 	if (!pin)
4163 		return 0;
4164 	for (i = 0; i < spec->num_adc_nids; i++) {
4165 		hda_nid_t cap = get_capsrc(spec, i);
4166 		int idx;
4167 
4168 		idx = get_connection_index(codec, cap, pin);
4169 		if (idx < 0)
4170 			continue;
4171 		select_or_unmute_capsrc(codec, cap, idx);
4172 		return i; /* return the found index */
4173 	}
4174 	return -1; /* not found */
4175 }
4176 
4177 /* initialize some special cases for input sources */
4178 static void alc_init_special_input_src(struct hda_codec *codec)
4179 {
4180 	struct alc_spec *spec = codec->spec;
4181 	int i;
4182 
4183 	for (i = 0; i < spec->autocfg.num_inputs; i++)
4184 		init_capsrc_for_pin(codec, spec->autocfg.inputs[i].pin);
4185 }
4186 
4187 /* assign appropriate capture mixers */
4188 static void set_capture_mixer(struct hda_codec *codec)
4189 {
4190 	struct alc_spec *spec = codec->spec;
4191 	static const struct snd_kcontrol_new *caps[2][3] = {
4192 		{ alc_capture_mixer_nosrc1,
4193 		  alc_capture_mixer_nosrc2,
4194 		  alc_capture_mixer_nosrc3 },
4195 		{ alc_capture_mixer1,
4196 		  alc_capture_mixer2,
4197 		  alc_capture_mixer3 },
4198 	};
4199 
4200 	/* check whether either of ADC or MUX has a volume control */
4201 	if (!nid_has_volume(codec, spec->adc_nids[0], HDA_INPUT)) {
4202 		if (!spec->capsrc_nids)
4203 			return; /* no volume */
4204 		if (!nid_has_volume(codec, spec->capsrc_nids[0], HDA_OUTPUT))
4205 			return; /* no volume in capsrc, too */
4206 		spec->vol_in_capsrc = 1;
4207 	}
4208 
4209 	if (spec->num_adc_nids > 0) {
4210 		int mux = 0;
4211 		int num_adcs = 0;
4212 
4213 		if (spec->input_mux && spec->input_mux->num_items > 1)
4214 			mux = 1;
4215 		if (spec->auto_mic) {
4216 			num_adcs = 1;
4217 			mux = 0;
4218 		} else if (spec->dyn_adc_switch)
4219 			num_adcs = 1;
4220 		if (!num_adcs) {
4221 			if (spec->num_adc_nids > 3)
4222 				spec->num_adc_nids = 3;
4223 			else if (!spec->num_adc_nids)
4224 				return;
4225 			num_adcs = spec->num_adc_nids;
4226 		}
4227 		spec->cap_mixer = caps[mux][num_adcs - 1];
4228 	}
4229 }
4230 
4231 /*
4232  * standard auto-parser initializations
4233  */
4234 static void alc_auto_init_std(struct hda_codec *codec)
4235 {
4236 	alc_auto_init_multi_out(codec);
4237 	alc_auto_init_extra_out(codec);
4238 	alc_auto_init_analog_input(codec);
4239 	alc_auto_init_input_src(codec);
4240 	alc_auto_init_digital(codec);
4241 	alc_inithook(codec);
4242 }
4243 
4244 /*
4245  * Digital-beep handlers
4246  */
4247 #ifdef CONFIG_SND_HDA_INPUT_BEEP
4248 #define set_beep_amp(spec, nid, idx, dir) \
4249 	((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir))
4250 
4251 static const struct snd_pci_quirk beep_white_list[] = {
4252 	SND_PCI_QUIRK(0x1043, 0x103c, "ASUS", 1),
4253 	SND_PCI_QUIRK(0x1043, 0x829f, "ASUS", 1),
4254 	SND_PCI_QUIRK(0x1043, 0x83ce, "EeePC", 1),
4255 	SND_PCI_QUIRK(0x1043, 0x831a, "EeePC", 1),
4256 	SND_PCI_QUIRK(0x1043, 0x834a, "EeePC", 1),
4257 	SND_PCI_QUIRK(0x1458, 0xa002, "GA-MA790X", 1),
4258 	SND_PCI_QUIRK(0x8086, 0xd613, "Intel", 1),
4259 	{}
4260 };
4261 
4262 static inline int has_cdefine_beep(struct hda_codec *codec)
4263 {
4264 	struct alc_spec *spec = codec->spec;
4265 	const struct snd_pci_quirk *q;
4266 	q = snd_pci_quirk_lookup(codec->bus->pci, beep_white_list);
4267 	if (q)
4268 		return q->value;
4269 	return spec->cdefine.enable_pcbeep;
4270 }
4271 #else
4272 #define set_beep_amp(spec, nid, idx, dir) /* NOP */
4273 #define has_cdefine_beep(codec)		0
4274 #endif
4275 
4276 /* parse the BIOS configuration and set up the alc_spec */
4277 /* return 1 if successful, 0 if the proper config is not found,
4278  * or a negative error code
4279  */
4280 static int alc_parse_auto_config(struct hda_codec *codec,
4281 				 const hda_nid_t *ignore_nids,
4282 				 const hda_nid_t *ssid_nids)
4283 {
4284 	struct alc_spec *spec = codec->spec;
4285 	struct auto_pin_cfg *cfg = &spec->autocfg;
4286 	int err;
4287 
4288 	err = snd_hda_parse_pin_defcfg(codec, cfg, ignore_nids,
4289 				       spec->parse_flags);
4290 	if (err < 0)
4291 		return err;
4292 	if (!cfg->line_outs) {
4293 		if (cfg->dig_outs || cfg->dig_in_pin) {
4294 			spec->multiout.max_channels = 2;
4295 			spec->no_analog = 1;
4296 			goto dig_only;
4297 		}
4298 		return 0; /* can't find valid BIOS pin config */
4299 	}
4300 
4301 	if (!spec->no_primary_hp &&
4302 	    cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4303 	    cfg->line_outs <= cfg->hp_outs) {
4304 		/* use HP as primary out */
4305 		cfg->speaker_outs = cfg->line_outs;
4306 		memcpy(cfg->speaker_pins, cfg->line_out_pins,
4307 		       sizeof(cfg->speaker_pins));
4308 		cfg->line_outs = cfg->hp_outs;
4309 		memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4310 		cfg->hp_outs = 0;
4311 		memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4312 		cfg->line_out_type = AUTO_PIN_HP_OUT;
4313 	}
4314 
4315 	err = alc_auto_fill_dac_nids(codec);
4316 	if (err < 0)
4317 		return err;
4318 	err = alc_auto_add_multi_channel_mode(codec);
4319 	if (err < 0)
4320 		return err;
4321 	err = alc_auto_create_multi_out_ctls(codec, cfg);
4322 	if (err < 0)
4323 		return err;
4324 	err = alc_auto_create_hp_out(codec);
4325 	if (err < 0)
4326 		return err;
4327 	err = alc_auto_create_speaker_out(codec);
4328 	if (err < 0)
4329 		return err;
4330 	err = alc_auto_create_shared_input(codec);
4331 	if (err < 0)
4332 		return err;
4333 	err = alc_auto_create_input_ctls(codec);
4334 	if (err < 0)
4335 		return err;
4336 
4337 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4338 
4339  dig_only:
4340 	alc_auto_parse_digital(codec);
4341 
4342 	if (!spec->no_analog)
4343 		alc_remove_invalid_adc_nids(codec);
4344 
4345 	if (ssid_nids)
4346 		alc_ssid_check(codec, ssid_nids);
4347 
4348 	if (!spec->no_analog) {
4349 		alc_auto_check_switches(codec);
4350 		err = alc_auto_add_mic_boost(codec);
4351 		if (err < 0)
4352 			return err;
4353 	}
4354 
4355 	if (spec->kctls.list)
4356 		add_mixer(spec, spec->kctls.list);
4357 
4358 	if (!spec->no_analog && !spec->cap_mixer)
4359 		set_capture_mixer(codec);
4360 
4361 	return 1;
4362 }
4363 
4364 /* common preparation job for alc_spec */
4365 static int alc_alloc_spec(struct hda_codec *codec, hda_nid_t mixer_nid)
4366 {
4367 	struct alc_spec *spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4368 	int err;
4369 
4370 	if (!spec)
4371 		return -ENOMEM;
4372 	codec->spec = spec;
4373 	spec->mixer_nid = mixer_nid;
4374 	snd_hda_gen_init(&spec->gen);
4375 
4376 	err = alc_codec_rename_from_preset(codec);
4377 	if (err < 0) {
4378 		kfree(spec);
4379 		return err;
4380 	}
4381 	return 0;
4382 }
4383 
4384 static int alc880_parse_auto_config(struct hda_codec *codec)
4385 {
4386 	static const hda_nid_t alc880_ignore[] = { 0x1d, 0 };
4387 	static const hda_nid_t alc880_ssids[] = { 0x15, 0x1b, 0x14, 0 };
4388 	return alc_parse_auto_config(codec, alc880_ignore, alc880_ssids);
4389 }
4390 
4391 /*
4392  * ALC880 fix-ups
4393  */
4394 enum {
4395 	ALC880_FIXUP_GPIO1,
4396 	ALC880_FIXUP_GPIO2,
4397 	ALC880_FIXUP_MEDION_RIM,
4398 	ALC880_FIXUP_LG,
4399 	ALC880_FIXUP_W810,
4400 	ALC880_FIXUP_EAPD_COEF,
4401 	ALC880_FIXUP_TCL_S700,
4402 	ALC880_FIXUP_VOL_KNOB,
4403 	ALC880_FIXUP_FUJITSU,
4404 	ALC880_FIXUP_F1734,
4405 	ALC880_FIXUP_UNIWILL,
4406 	ALC880_FIXUP_UNIWILL_DIG,
4407 	ALC880_FIXUP_Z71V,
4408 	ALC880_FIXUP_3ST_BASE,
4409 	ALC880_FIXUP_3ST,
4410 	ALC880_FIXUP_3ST_DIG,
4411 	ALC880_FIXUP_5ST_BASE,
4412 	ALC880_FIXUP_5ST,
4413 	ALC880_FIXUP_5ST_DIG,
4414 	ALC880_FIXUP_6ST_BASE,
4415 	ALC880_FIXUP_6ST,
4416 	ALC880_FIXUP_6ST_DIG,
4417 };
4418 
4419 /* enable the volume-knob widget support on NID 0x21 */
4420 static void alc880_fixup_vol_knob(struct hda_codec *codec,
4421 				  const struct alc_fixup *fix, int action)
4422 {
4423 	if (action == ALC_FIXUP_ACT_PROBE)
4424 		snd_hda_jack_detect_enable_callback(codec, 0x21, ALC_DCVOL_EVENT, alc_update_knob_master);
4425 }
4426 
4427 static const struct alc_fixup alc880_fixups[] = {
4428 	[ALC880_FIXUP_GPIO1] = {
4429 		.type = ALC_FIXUP_VERBS,
4430 		.v.verbs = alc_gpio1_init_verbs,
4431 	},
4432 	[ALC880_FIXUP_GPIO2] = {
4433 		.type = ALC_FIXUP_VERBS,
4434 		.v.verbs = alc_gpio2_init_verbs,
4435 	},
4436 	[ALC880_FIXUP_MEDION_RIM] = {
4437 		.type = ALC_FIXUP_VERBS,
4438 		.v.verbs = (const struct hda_verb[]) {
4439 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4440 			{ 0x20, AC_VERB_SET_PROC_COEF,  0x3060 },
4441 			{ }
4442 		},
4443 		.chained = true,
4444 		.chain_id = ALC880_FIXUP_GPIO2,
4445 	},
4446 	[ALC880_FIXUP_LG] = {
4447 		.type = ALC_FIXUP_PINS,
4448 		.v.pins = (const struct alc_pincfg[]) {
4449 			/* disable bogus unused pins */
4450 			{ 0x16, 0x411111f0 },
4451 			{ 0x18, 0x411111f0 },
4452 			{ 0x1a, 0x411111f0 },
4453 			{ }
4454 		}
4455 	},
4456 	[ALC880_FIXUP_W810] = {
4457 		.type = ALC_FIXUP_PINS,
4458 		.v.pins = (const struct alc_pincfg[]) {
4459 			/* disable bogus unused pins */
4460 			{ 0x17, 0x411111f0 },
4461 			{ }
4462 		},
4463 		.chained = true,
4464 		.chain_id = ALC880_FIXUP_GPIO2,
4465 	},
4466 	[ALC880_FIXUP_EAPD_COEF] = {
4467 		.type = ALC_FIXUP_VERBS,
4468 		.v.verbs = (const struct hda_verb[]) {
4469 			/* change to EAPD mode */
4470 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4471 			{ 0x20, AC_VERB_SET_PROC_COEF,  0x3060 },
4472 			{}
4473 		},
4474 	},
4475 	[ALC880_FIXUP_TCL_S700] = {
4476 		.type = ALC_FIXUP_VERBS,
4477 		.v.verbs = (const struct hda_verb[]) {
4478 			/* change to EAPD mode */
4479 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4480 			{ 0x20, AC_VERB_SET_PROC_COEF,  0x3070 },
4481 			{}
4482 		},
4483 		.chained = true,
4484 		.chain_id = ALC880_FIXUP_GPIO2,
4485 	},
4486 	[ALC880_FIXUP_VOL_KNOB] = {
4487 		.type = ALC_FIXUP_FUNC,
4488 		.v.func = alc880_fixup_vol_knob,
4489 	},
4490 	[ALC880_FIXUP_FUJITSU] = {
4491 		/* override all pins as BIOS on old Amilo is broken */
4492 		.type = ALC_FIXUP_PINS,
4493 		.v.pins = (const struct alc_pincfg[]) {
4494 			{ 0x14, 0x0121411f }, /* HP */
4495 			{ 0x15, 0x99030120 }, /* speaker */
4496 			{ 0x16, 0x99030130 }, /* bass speaker */
4497 			{ 0x17, 0x411111f0 }, /* N/A */
4498 			{ 0x18, 0x411111f0 }, /* N/A */
4499 			{ 0x19, 0x01a19950 }, /* mic-in */
4500 			{ 0x1a, 0x411111f0 }, /* N/A */
4501 			{ 0x1b, 0x411111f0 }, /* N/A */
4502 			{ 0x1c, 0x411111f0 }, /* N/A */
4503 			{ 0x1d, 0x411111f0 }, /* N/A */
4504 			{ 0x1e, 0x01454140 }, /* SPDIF out */
4505 			{ }
4506 		},
4507 		.chained = true,
4508 		.chain_id = ALC880_FIXUP_VOL_KNOB,
4509 	},
4510 	[ALC880_FIXUP_F1734] = {
4511 		/* almost compatible with FUJITSU, but no bass and SPDIF */
4512 		.type = ALC_FIXUP_PINS,
4513 		.v.pins = (const struct alc_pincfg[]) {
4514 			{ 0x14, 0x0121411f }, /* HP */
4515 			{ 0x15, 0x99030120 }, /* speaker */
4516 			{ 0x16, 0x411111f0 }, /* N/A */
4517 			{ 0x17, 0x411111f0 }, /* N/A */
4518 			{ 0x18, 0x411111f0 }, /* N/A */
4519 			{ 0x19, 0x01a19950 }, /* mic-in */
4520 			{ 0x1a, 0x411111f0 }, /* N/A */
4521 			{ 0x1b, 0x411111f0 }, /* N/A */
4522 			{ 0x1c, 0x411111f0 }, /* N/A */
4523 			{ 0x1d, 0x411111f0 }, /* N/A */
4524 			{ 0x1e, 0x411111f0 }, /* N/A */
4525 			{ }
4526 		},
4527 		.chained = true,
4528 		.chain_id = ALC880_FIXUP_VOL_KNOB,
4529 	},
4530 	[ALC880_FIXUP_UNIWILL] = {
4531 		/* need to fix HP and speaker pins to be parsed correctly */
4532 		.type = ALC_FIXUP_PINS,
4533 		.v.pins = (const struct alc_pincfg[]) {
4534 			{ 0x14, 0x0121411f }, /* HP */
4535 			{ 0x15, 0x99030120 }, /* speaker */
4536 			{ 0x16, 0x99030130 }, /* bass speaker */
4537 			{ }
4538 		},
4539 	},
4540 	[ALC880_FIXUP_UNIWILL_DIG] = {
4541 		.type = ALC_FIXUP_PINS,
4542 		.v.pins = (const struct alc_pincfg[]) {
4543 			/* disable bogus unused pins */
4544 			{ 0x17, 0x411111f0 },
4545 			{ 0x19, 0x411111f0 },
4546 			{ 0x1b, 0x411111f0 },
4547 			{ 0x1f, 0x411111f0 },
4548 			{ }
4549 		}
4550 	},
4551 	[ALC880_FIXUP_Z71V] = {
4552 		.type = ALC_FIXUP_PINS,
4553 		.v.pins = (const struct alc_pincfg[]) {
4554 			/* set up the whole pins as BIOS is utterly broken */
4555 			{ 0x14, 0x99030120 }, /* speaker */
4556 			{ 0x15, 0x0121411f }, /* HP */
4557 			{ 0x16, 0x411111f0 }, /* N/A */
4558 			{ 0x17, 0x411111f0 }, /* N/A */
4559 			{ 0x18, 0x01a19950 }, /* mic-in */
4560 			{ 0x19, 0x411111f0 }, /* N/A */
4561 			{ 0x1a, 0x01813031 }, /* line-in */
4562 			{ 0x1b, 0x411111f0 }, /* N/A */
4563 			{ 0x1c, 0x411111f0 }, /* N/A */
4564 			{ 0x1d, 0x411111f0 }, /* N/A */
4565 			{ 0x1e, 0x0144111e }, /* SPDIF */
4566 			{ }
4567 		}
4568 	},
4569 	[ALC880_FIXUP_3ST_BASE] = {
4570 		.type = ALC_FIXUP_PINS,
4571 		.v.pins = (const struct alc_pincfg[]) {
4572 			{ 0x14, 0x01014010 }, /* line-out */
4573 			{ 0x15, 0x411111f0 }, /* N/A */
4574 			{ 0x16, 0x411111f0 }, /* N/A */
4575 			{ 0x17, 0x411111f0 }, /* N/A */
4576 			{ 0x18, 0x01a19c30 }, /* mic-in */
4577 			{ 0x19, 0x0121411f }, /* HP */
4578 			{ 0x1a, 0x01813031 }, /* line-in */
4579 			{ 0x1b, 0x02a19c40 }, /* front-mic */
4580 			{ 0x1c, 0x411111f0 }, /* N/A */
4581 			{ 0x1d, 0x411111f0 }, /* N/A */
4582 			/* 0x1e is filled in below */
4583 			{ 0x1f, 0x411111f0 }, /* N/A */
4584 			{ }
4585 		}
4586 	},
4587 	[ALC880_FIXUP_3ST] = {
4588 		.type = ALC_FIXUP_PINS,
4589 		.v.pins = (const struct alc_pincfg[]) {
4590 			{ 0x1e, 0x411111f0 }, /* N/A */
4591 			{ }
4592 		},
4593 		.chained = true,
4594 		.chain_id = ALC880_FIXUP_3ST_BASE,
4595 	},
4596 	[ALC880_FIXUP_3ST_DIG] = {
4597 		.type = ALC_FIXUP_PINS,
4598 		.v.pins = (const struct alc_pincfg[]) {
4599 			{ 0x1e, 0x0144111e }, /* SPDIF */
4600 			{ }
4601 		},
4602 		.chained = true,
4603 		.chain_id = ALC880_FIXUP_3ST_BASE,
4604 	},
4605 	[ALC880_FIXUP_5ST_BASE] = {
4606 		.type = ALC_FIXUP_PINS,
4607 		.v.pins = (const struct alc_pincfg[]) {
4608 			{ 0x14, 0x01014010 }, /* front */
4609 			{ 0x15, 0x411111f0 }, /* N/A */
4610 			{ 0x16, 0x01011411 }, /* CLFE */
4611 			{ 0x17, 0x01016412 }, /* surr */
4612 			{ 0x18, 0x01a19c30 }, /* mic-in */
4613 			{ 0x19, 0x0121411f }, /* HP */
4614 			{ 0x1a, 0x01813031 }, /* line-in */
4615 			{ 0x1b, 0x02a19c40 }, /* front-mic */
4616 			{ 0x1c, 0x411111f0 }, /* N/A */
4617 			{ 0x1d, 0x411111f0 }, /* N/A */
4618 			/* 0x1e is filled in below */
4619 			{ 0x1f, 0x411111f0 }, /* N/A */
4620 			{ }
4621 		}
4622 	},
4623 	[ALC880_FIXUP_5ST] = {
4624 		.type = ALC_FIXUP_PINS,
4625 		.v.pins = (const struct alc_pincfg[]) {
4626 			{ 0x1e, 0x411111f0 }, /* N/A */
4627 			{ }
4628 		},
4629 		.chained = true,
4630 		.chain_id = ALC880_FIXUP_5ST_BASE,
4631 	},
4632 	[ALC880_FIXUP_5ST_DIG] = {
4633 		.type = ALC_FIXUP_PINS,
4634 		.v.pins = (const struct alc_pincfg[]) {
4635 			{ 0x1e, 0x0144111e }, /* SPDIF */
4636 			{ }
4637 		},
4638 		.chained = true,
4639 		.chain_id = ALC880_FIXUP_5ST_BASE,
4640 	},
4641 	[ALC880_FIXUP_6ST_BASE] = {
4642 		.type = ALC_FIXUP_PINS,
4643 		.v.pins = (const struct alc_pincfg[]) {
4644 			{ 0x14, 0x01014010 }, /* front */
4645 			{ 0x15, 0x01016412 }, /* surr */
4646 			{ 0x16, 0x01011411 }, /* CLFE */
4647 			{ 0x17, 0x01012414 }, /* side */
4648 			{ 0x18, 0x01a19c30 }, /* mic-in */
4649 			{ 0x19, 0x02a19c40 }, /* front-mic */
4650 			{ 0x1a, 0x01813031 }, /* line-in */
4651 			{ 0x1b, 0x0121411f }, /* HP */
4652 			{ 0x1c, 0x411111f0 }, /* N/A */
4653 			{ 0x1d, 0x411111f0 }, /* N/A */
4654 			/* 0x1e is filled in below */
4655 			{ 0x1f, 0x411111f0 }, /* N/A */
4656 			{ }
4657 		}
4658 	},
4659 	[ALC880_FIXUP_6ST] = {
4660 		.type = ALC_FIXUP_PINS,
4661 		.v.pins = (const struct alc_pincfg[]) {
4662 			{ 0x1e, 0x411111f0 }, /* N/A */
4663 			{ }
4664 		},
4665 		.chained = true,
4666 		.chain_id = ALC880_FIXUP_6ST_BASE,
4667 	},
4668 	[ALC880_FIXUP_6ST_DIG] = {
4669 		.type = ALC_FIXUP_PINS,
4670 		.v.pins = (const struct alc_pincfg[]) {
4671 			{ 0x1e, 0x0144111e }, /* SPDIF */
4672 			{ }
4673 		},
4674 		.chained = true,
4675 		.chain_id = ALC880_FIXUP_6ST_BASE,
4676 	},
4677 };
4678 
4679 static const struct snd_pci_quirk alc880_fixup_tbl[] = {
4680 	SND_PCI_QUIRK(0x1019, 0x0f69, "Coeus G610P", ALC880_FIXUP_W810),
4681 	SND_PCI_QUIRK(0x1043, 0x1964, "ASUS Z71V", ALC880_FIXUP_Z71V),
4682 	SND_PCI_QUIRK_VENDOR(0x1043, "ASUS", ALC880_FIXUP_GPIO1),
4683 	SND_PCI_QUIRK(0x1558, 0x5401, "Clevo GPIO2", ALC880_FIXUP_GPIO2),
4684 	SND_PCI_QUIRK_VENDOR(0x1558, "Clevo", ALC880_FIXUP_EAPD_COEF),
4685 	SND_PCI_QUIRK(0x1584, 0x9050, "Uniwill", ALC880_FIXUP_UNIWILL_DIG),
4686 	SND_PCI_QUIRK(0x1584, 0x9054, "Uniwill", ALC880_FIXUP_F1734),
4687 	SND_PCI_QUIRK(0x1584, 0x9070, "Uniwill", ALC880_FIXUP_UNIWILL),
4688 	SND_PCI_QUIRK(0x1584, 0x9077, "Uniwill P53", ALC880_FIXUP_VOL_KNOB),
4689 	SND_PCI_QUIRK(0x161f, 0x203d, "W810", ALC880_FIXUP_W810),
4690 	SND_PCI_QUIRK(0x161f, 0x205d, "Medion Rim 2150", ALC880_FIXUP_MEDION_RIM),
4691 	SND_PCI_QUIRK(0x1734, 0x107c, "FSC F1734", ALC880_FIXUP_F1734),
4692 	SND_PCI_QUIRK(0x1734, 0x1094, "FSC Amilo M1451G", ALC880_FIXUP_FUJITSU),
4693 	SND_PCI_QUIRK(0x1734, 0x10ac, "FSC AMILO Xi 1526", ALC880_FIXUP_F1734),
4694 	SND_PCI_QUIRK(0x1734, 0x10b0, "FSC Amilo Pi1556", ALC880_FIXUP_FUJITSU),
4695 	SND_PCI_QUIRK(0x1854, 0x003b, "LG", ALC880_FIXUP_LG),
4696 	SND_PCI_QUIRK(0x1854, 0x005f, "LG P1 Express", ALC880_FIXUP_LG),
4697 	SND_PCI_QUIRK(0x1854, 0x0068, "LG w1", ALC880_FIXUP_LG),
4698 	SND_PCI_QUIRK(0x19db, 0x4188, "TCL S700", ALC880_FIXUP_TCL_S700),
4699 
4700 	/* Below is the copied entries from alc880_quirks.c.
4701 	 * It's not quite sure whether BIOS sets the correct pin-config table
4702 	 * on these machines, thus they are kept to be compatible with
4703 	 * the old static quirks.  Once when it's confirmed to work without
4704 	 * these overrides, it'd be better to remove.
4705 	 */
4706 	SND_PCI_QUIRK(0x1019, 0xa880, "ECS", ALC880_FIXUP_5ST_DIG),
4707 	SND_PCI_QUIRK(0x1019, 0xa884, "Acer APFV", ALC880_FIXUP_6ST),
4708 	SND_PCI_QUIRK(0x1025, 0x0070, "ULI", ALC880_FIXUP_3ST_DIG),
4709 	SND_PCI_QUIRK(0x1025, 0x0077, "ULI", ALC880_FIXUP_6ST_DIG),
4710 	SND_PCI_QUIRK(0x1025, 0x0078, "ULI", ALC880_FIXUP_6ST_DIG),
4711 	SND_PCI_QUIRK(0x1025, 0x0087, "ULI", ALC880_FIXUP_6ST_DIG),
4712 	SND_PCI_QUIRK(0x1025, 0xe309, "ULI", ALC880_FIXUP_3ST_DIG),
4713 	SND_PCI_QUIRK(0x1025, 0xe310, "ULI", ALC880_FIXUP_3ST),
4714 	SND_PCI_QUIRK(0x1039, 0x1234, NULL, ALC880_FIXUP_6ST_DIG),
4715 	SND_PCI_QUIRK(0x104d, 0x81a0, "Sony", ALC880_FIXUP_3ST),
4716 	SND_PCI_QUIRK(0x104d, 0x81d6, "Sony", ALC880_FIXUP_3ST),
4717 	SND_PCI_QUIRK(0x107b, 0x3032, "Gateway", ALC880_FIXUP_5ST),
4718 	SND_PCI_QUIRK(0x107b, 0x3033, "Gateway", ALC880_FIXUP_5ST),
4719 	SND_PCI_QUIRK(0x107b, 0x4039, "Gateway", ALC880_FIXUP_5ST),
4720 	SND_PCI_QUIRK(0x1297, 0xc790, "Shuttle ST20G5", ALC880_FIXUP_6ST_DIG),
4721 	SND_PCI_QUIRK(0x1458, 0xa102, "Gigabyte K8", ALC880_FIXUP_6ST_DIG),
4722 	SND_PCI_QUIRK(0x1462, 0x1150, "MSI", ALC880_FIXUP_6ST_DIG),
4723 	SND_PCI_QUIRK(0x1509, 0x925d, "FIC P4M", ALC880_FIXUP_6ST_DIG),
4724 	SND_PCI_QUIRK(0x1565, 0x8202, "Biostar", ALC880_FIXUP_5ST_DIG),
4725 	SND_PCI_QUIRK(0x1695, 0x400d, "EPoX", ALC880_FIXUP_5ST_DIG),
4726 	SND_PCI_QUIRK(0x1695, 0x4012, "EPox EP-5LDA", ALC880_FIXUP_5ST_DIG),
4727 	SND_PCI_QUIRK(0x2668, 0x8086, NULL, ALC880_FIXUP_6ST_DIG), /* broken BIOS */
4728 	SND_PCI_QUIRK(0x8086, 0x2668, NULL, ALC880_FIXUP_6ST_DIG),
4729 	SND_PCI_QUIRK(0x8086, 0xa100, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4730 	SND_PCI_QUIRK(0x8086, 0xd400, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4731 	SND_PCI_QUIRK(0x8086, 0xd401, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4732 	SND_PCI_QUIRK(0x8086, 0xd402, "Intel mobo", ALC880_FIXUP_3ST_DIG),
4733 	SND_PCI_QUIRK(0x8086, 0xe224, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4734 	SND_PCI_QUIRK(0x8086, 0xe305, "Intel mobo", ALC880_FIXUP_3ST_DIG),
4735 	SND_PCI_QUIRK(0x8086, 0xe308, "Intel mobo", ALC880_FIXUP_3ST_DIG),
4736 	SND_PCI_QUIRK(0x8086, 0xe400, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4737 	SND_PCI_QUIRK(0x8086, 0xe401, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4738 	SND_PCI_QUIRK(0x8086, 0xe402, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4739 	/* default Intel */
4740 	SND_PCI_QUIRK_VENDOR(0x8086, "Intel mobo", ALC880_FIXUP_3ST),
4741 	SND_PCI_QUIRK(0xa0a0, 0x0560, "AOpen i915GMm-HFS", ALC880_FIXUP_5ST_DIG),
4742 	SND_PCI_QUIRK(0xe803, 0x1019, NULL, ALC880_FIXUP_6ST_DIG),
4743 	{}
4744 };
4745 
4746 static const struct alc_model_fixup alc880_fixup_models[] = {
4747 	{.id = ALC880_FIXUP_3ST, .name = "3stack"},
4748 	{.id = ALC880_FIXUP_3ST_DIG, .name = "3stack-digout"},
4749 	{.id = ALC880_FIXUP_5ST, .name = "5stack"},
4750 	{.id = ALC880_FIXUP_5ST_DIG, .name = "5stack-digout"},
4751 	{.id = ALC880_FIXUP_6ST, .name = "6stack"},
4752 	{.id = ALC880_FIXUP_6ST_DIG, .name = "6stack-digout"},
4753 	{}
4754 };
4755 
4756 
4757 /*
4758  * OK, here we have finally the patch for ALC880
4759  */
4760 static int patch_alc880(struct hda_codec *codec)
4761 {
4762 	struct alc_spec *spec;
4763 	int err;
4764 
4765 	err = alc_alloc_spec(codec, 0x0b);
4766 	if (err < 0)
4767 		return err;
4768 
4769 	spec = codec->spec;
4770 	spec->need_dac_fix = 1;
4771 
4772 	alc_pick_fixup(codec, alc880_fixup_models, alc880_fixup_tbl,
4773 		       alc880_fixups);
4774 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
4775 
4776 	/* automatic parse from the BIOS config */
4777 	err = alc880_parse_auto_config(codec);
4778 	if (err < 0)
4779 		goto error;
4780 
4781 	if (!spec->no_analog) {
4782 		err = snd_hda_attach_beep_device(codec, 0x1);
4783 		if (err < 0)
4784 			goto error;
4785 		set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
4786 	}
4787 
4788 	codec->patch_ops = alc_patch_ops;
4789 	codec->patch_ops.unsol_event = alc880_unsol_event;
4790 
4791 
4792 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
4793 
4794 	return 0;
4795 
4796  error:
4797 	alc_free(codec);
4798 	return err;
4799 }
4800 
4801 
4802 /*
4803  * ALC260 support
4804  */
4805 static int alc260_parse_auto_config(struct hda_codec *codec)
4806 {
4807 	static const hda_nid_t alc260_ignore[] = { 0x17, 0 };
4808 	static const hda_nid_t alc260_ssids[] = { 0x10, 0x15, 0x0f, 0 };
4809 	return alc_parse_auto_config(codec, alc260_ignore, alc260_ssids);
4810 }
4811 
4812 /*
4813  * Pin config fixes
4814  */
4815 enum {
4816 	ALC260_FIXUP_HP_DC5750,
4817 	ALC260_FIXUP_HP_PIN_0F,
4818 	ALC260_FIXUP_COEF,
4819 	ALC260_FIXUP_GPIO1,
4820 	ALC260_FIXUP_GPIO1_TOGGLE,
4821 	ALC260_FIXUP_REPLACER,
4822 	ALC260_FIXUP_HP_B1900,
4823 	ALC260_FIXUP_KN1,
4824 };
4825 
4826 static void alc260_gpio1_automute(struct hda_codec *codec)
4827 {
4828 	struct alc_spec *spec = codec->spec;
4829 	snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
4830 			    spec->hp_jack_present);
4831 }
4832 
4833 static void alc260_fixup_gpio1_toggle(struct hda_codec *codec,
4834 				      const struct alc_fixup *fix, int action)
4835 {
4836 	struct alc_spec *spec = codec->spec;
4837 	if (action == ALC_FIXUP_ACT_PROBE) {
4838 		/* although the machine has only one output pin, we need to
4839 		 * toggle GPIO1 according to the jack state
4840 		 */
4841 		spec->automute_hook = alc260_gpio1_automute;
4842 		spec->detect_hp = 1;
4843 		spec->automute_speaker = 1;
4844 		spec->autocfg.hp_pins[0] = 0x0f; /* copy it for automute */
4845 		snd_hda_jack_detect_enable_callback(codec, 0x0f, ALC_HP_EVENT,
4846 						    alc_hp_automute);
4847 		snd_hda_gen_add_verbs(&spec->gen, alc_gpio1_init_verbs);
4848 	}
4849 }
4850 
4851 static void alc260_fixup_kn1(struct hda_codec *codec,
4852 			     const struct alc_fixup *fix, int action)
4853 {
4854 	struct alc_spec *spec = codec->spec;
4855 	static const struct alc_pincfg pincfgs[] = {
4856 		{ 0x0f, 0x02214000 }, /* HP/speaker */
4857 		{ 0x12, 0x90a60160 }, /* int mic */
4858 		{ 0x13, 0x02a19000 }, /* ext mic */
4859 		{ 0x18, 0x01446000 }, /* SPDIF out */
4860 		/* disable bogus I/O pins */
4861 		{ 0x10, 0x411111f0 },
4862 		{ 0x11, 0x411111f0 },
4863 		{ 0x14, 0x411111f0 },
4864 		{ 0x15, 0x411111f0 },
4865 		{ 0x16, 0x411111f0 },
4866 		{ 0x17, 0x411111f0 },
4867 		{ 0x19, 0x411111f0 },
4868 		{ }
4869 	};
4870 
4871 	switch (action) {
4872 	case ALC_FIXUP_ACT_PRE_PROBE:
4873 		alc_apply_pincfgs(codec, pincfgs);
4874 		break;
4875 	case ALC_FIXUP_ACT_PROBE:
4876 		spec->init_amp = ALC_INIT_NONE;
4877 		break;
4878 	}
4879 }
4880 
4881 static const struct alc_fixup alc260_fixups[] = {
4882 	[ALC260_FIXUP_HP_DC5750] = {
4883 		.type = ALC_FIXUP_PINS,
4884 		.v.pins = (const struct alc_pincfg[]) {
4885 			{ 0x11, 0x90130110 }, /* speaker */
4886 			{ }
4887 		}
4888 	},
4889 	[ALC260_FIXUP_HP_PIN_0F] = {
4890 		.type = ALC_FIXUP_PINS,
4891 		.v.pins = (const struct alc_pincfg[]) {
4892 			{ 0x0f, 0x01214000 }, /* HP */
4893 			{ }
4894 		}
4895 	},
4896 	[ALC260_FIXUP_COEF] = {
4897 		.type = ALC_FIXUP_VERBS,
4898 		.v.verbs = (const struct hda_verb[]) {
4899 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4900 			{ 0x20, AC_VERB_SET_PROC_COEF,  0x3040 },
4901 			{ }
4902 		},
4903 		.chained = true,
4904 		.chain_id = ALC260_FIXUP_HP_PIN_0F,
4905 	},
4906 	[ALC260_FIXUP_GPIO1] = {
4907 		.type = ALC_FIXUP_VERBS,
4908 		.v.verbs = alc_gpio1_init_verbs,
4909 	},
4910 	[ALC260_FIXUP_GPIO1_TOGGLE] = {
4911 		.type = ALC_FIXUP_FUNC,
4912 		.v.func = alc260_fixup_gpio1_toggle,
4913 		.chained = true,
4914 		.chain_id = ALC260_FIXUP_HP_PIN_0F,
4915 	},
4916 	[ALC260_FIXUP_REPLACER] = {
4917 		.type = ALC_FIXUP_VERBS,
4918 		.v.verbs = (const struct hda_verb[]) {
4919 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4920 			{ 0x20, AC_VERB_SET_PROC_COEF,  0x3050 },
4921 			{ }
4922 		},
4923 		.chained = true,
4924 		.chain_id = ALC260_FIXUP_GPIO1_TOGGLE,
4925 	},
4926 	[ALC260_FIXUP_HP_B1900] = {
4927 		.type = ALC_FIXUP_FUNC,
4928 		.v.func = alc260_fixup_gpio1_toggle,
4929 		.chained = true,
4930 		.chain_id = ALC260_FIXUP_COEF,
4931 	},
4932 	[ALC260_FIXUP_KN1] = {
4933 		.type = ALC_FIXUP_FUNC,
4934 		.v.func = alc260_fixup_kn1,
4935 	},
4936 };
4937 
4938 static const struct snd_pci_quirk alc260_fixup_tbl[] = {
4939 	SND_PCI_QUIRK(0x1025, 0x007b, "Acer C20x", ALC260_FIXUP_GPIO1),
4940 	SND_PCI_QUIRK(0x1025, 0x007f, "Acer Aspire 9500", ALC260_FIXUP_COEF),
4941 	SND_PCI_QUIRK(0x1025, 0x008f, "Acer", ALC260_FIXUP_GPIO1),
4942 	SND_PCI_QUIRK(0x103c, 0x280a, "HP dc5750", ALC260_FIXUP_HP_DC5750),
4943 	SND_PCI_QUIRK(0x103c, 0x30ba, "HP Presario B1900", ALC260_FIXUP_HP_B1900),
4944 	SND_PCI_QUIRK(0x1509, 0x4540, "Favorit 100XS", ALC260_FIXUP_GPIO1),
4945 	SND_PCI_QUIRK(0x152d, 0x0729, "Quanta KN1", ALC260_FIXUP_KN1),
4946 	SND_PCI_QUIRK(0x161f, 0x2057, "Replacer 672V", ALC260_FIXUP_REPLACER),
4947 	SND_PCI_QUIRK(0x1631, 0xc017, "PB V7900", ALC260_FIXUP_COEF),
4948 	{}
4949 };
4950 
4951 /*
4952  */
4953 static int patch_alc260(struct hda_codec *codec)
4954 {
4955 	struct alc_spec *spec;
4956 	int err;
4957 
4958 	err = alc_alloc_spec(codec, 0x07);
4959 	if (err < 0)
4960 		return err;
4961 
4962 	spec = codec->spec;
4963 
4964 	alc_pick_fixup(codec, NULL, alc260_fixup_tbl, alc260_fixups);
4965 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
4966 
4967 	/* automatic parse from the BIOS config */
4968 	err = alc260_parse_auto_config(codec);
4969 	if (err < 0)
4970 		goto error;
4971 
4972 	if (!spec->no_analog) {
4973 		err = snd_hda_attach_beep_device(codec, 0x1);
4974 		if (err < 0)
4975 			goto error;
4976 		set_beep_amp(spec, 0x07, 0x05, HDA_INPUT);
4977 	}
4978 
4979 	codec->patch_ops = alc_patch_ops;
4980 	spec->shutup = alc_eapd_shutup;
4981 
4982 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
4983 
4984 	return 0;
4985 
4986  error:
4987 	alc_free(codec);
4988 	return err;
4989 }
4990 
4991 
4992 /*
4993  * ALC882/883/885/888/889 support
4994  *
4995  * ALC882 is almost identical with ALC880 but has cleaner and more flexible
4996  * configuration.  Each pin widget can choose any input DACs and a mixer.
4997  * Each ADC is connected from a mixer of all inputs.  This makes possible
4998  * 6-channel independent captures.
4999  *
5000  * In addition, an independent DAC for the multi-playback (not used in this
5001  * driver yet).
5002  */
5003 
5004 /*
5005  * Pin config fixes
5006  */
5007 enum {
5008 	ALC882_FIXUP_ABIT_AW9D_MAX,
5009 	ALC882_FIXUP_LENOVO_Y530,
5010 	ALC882_FIXUP_PB_M5210,
5011 	ALC882_FIXUP_ACER_ASPIRE_7736,
5012 	ALC882_FIXUP_ASUS_W90V,
5013 	ALC889_FIXUP_CD,
5014 	ALC889_FIXUP_VAIO_TT,
5015 	ALC888_FIXUP_EEE1601,
5016 	ALC882_FIXUP_EAPD,
5017 	ALC883_FIXUP_EAPD,
5018 	ALC883_FIXUP_ACER_EAPD,
5019 	ALC882_FIXUP_GPIO1,
5020 	ALC882_FIXUP_GPIO2,
5021 	ALC882_FIXUP_GPIO3,
5022 	ALC889_FIXUP_COEF,
5023 	ALC882_FIXUP_ASUS_W2JC,
5024 	ALC882_FIXUP_ACER_ASPIRE_4930G,
5025 	ALC882_FIXUP_ACER_ASPIRE_8930G,
5026 	ALC882_FIXUP_ASPIRE_8930G_VERBS,
5027 	ALC885_FIXUP_MACPRO_GPIO,
5028 	ALC889_FIXUP_DAC_ROUTE,
5029 	ALC889_FIXUP_MBP_VREF,
5030 	ALC889_FIXUP_IMAC91_VREF,
5031 	ALC882_FIXUP_INV_DMIC,
5032 	ALC882_FIXUP_NO_PRIMARY_HP,
5033 };
5034 
5035 static void alc889_fixup_coef(struct hda_codec *codec,
5036 			      const struct alc_fixup *fix, int action)
5037 {
5038 	if (action != ALC_FIXUP_ACT_INIT)
5039 		return;
5040 	alc889_coef_init(codec);
5041 }
5042 
5043 /* toggle speaker-output according to the hp-jack state */
5044 static void alc882_gpio_mute(struct hda_codec *codec, int pin, int muted)
5045 {
5046 	unsigned int gpiostate, gpiomask, gpiodir;
5047 
5048 	gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
5049 				       AC_VERB_GET_GPIO_DATA, 0);
5050 
5051 	if (!muted)
5052 		gpiostate |= (1 << pin);
5053 	else
5054 		gpiostate &= ~(1 << pin);
5055 
5056 	gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
5057 				      AC_VERB_GET_GPIO_MASK, 0);
5058 	gpiomask |= (1 << pin);
5059 
5060 	gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
5061 				     AC_VERB_GET_GPIO_DIRECTION, 0);
5062 	gpiodir |= (1 << pin);
5063 
5064 
5065 	snd_hda_codec_write(codec, codec->afg, 0,
5066 			    AC_VERB_SET_GPIO_MASK, gpiomask);
5067 	snd_hda_codec_write(codec, codec->afg, 0,
5068 			    AC_VERB_SET_GPIO_DIRECTION, gpiodir);
5069 
5070 	msleep(1);
5071 
5072 	snd_hda_codec_write(codec, codec->afg, 0,
5073 			    AC_VERB_SET_GPIO_DATA, gpiostate);
5074 }
5075 
5076 /* set up GPIO at initialization */
5077 static void alc885_fixup_macpro_gpio(struct hda_codec *codec,
5078 				     const struct alc_fixup *fix, int action)
5079 {
5080 	if (action != ALC_FIXUP_ACT_INIT)
5081 		return;
5082 	alc882_gpio_mute(codec, 0, 0);
5083 	alc882_gpio_mute(codec, 1, 0);
5084 }
5085 
5086 /* Fix the connection of some pins for ALC889:
5087  * At least, Acer Aspire 5935 shows the connections to DAC3/4 don't
5088  * work correctly (bko#42740)
5089  */
5090 static void alc889_fixup_dac_route(struct hda_codec *codec,
5091 				   const struct alc_fixup *fix, int action)
5092 {
5093 	if (action == ALC_FIXUP_ACT_PRE_PROBE) {
5094 		/* fake the connections during parsing the tree */
5095 		hda_nid_t conn1[2] = { 0x0c, 0x0d };
5096 		hda_nid_t conn2[2] = { 0x0e, 0x0f };
5097 		snd_hda_override_conn_list(codec, 0x14, 2, conn1);
5098 		snd_hda_override_conn_list(codec, 0x15, 2, conn1);
5099 		snd_hda_override_conn_list(codec, 0x18, 2, conn2);
5100 		snd_hda_override_conn_list(codec, 0x1a, 2, conn2);
5101 	} else if (action == ALC_FIXUP_ACT_PROBE) {
5102 		/* restore the connections */
5103 		hda_nid_t conn[5] = { 0x0c, 0x0d, 0x0e, 0x0f, 0x26 };
5104 		snd_hda_override_conn_list(codec, 0x14, 5, conn);
5105 		snd_hda_override_conn_list(codec, 0x15, 5, conn);
5106 		snd_hda_override_conn_list(codec, 0x18, 5, conn);
5107 		snd_hda_override_conn_list(codec, 0x1a, 5, conn);
5108 	}
5109 }
5110 
5111 /* Set VREF on HP pin */
5112 static void alc889_fixup_mbp_vref(struct hda_codec *codec,
5113 				  const struct alc_fixup *fix, int action)
5114 {
5115 	struct alc_spec *spec = codec->spec;
5116 	static hda_nid_t nids[2] = { 0x14, 0x15 };
5117 	int i;
5118 
5119 	if (action != ALC_FIXUP_ACT_INIT)
5120 		return;
5121 	for (i = 0; i < ARRAY_SIZE(nids); i++) {
5122 		unsigned int val = snd_hda_codec_get_pincfg(codec, nids[i]);
5123 		if (get_defcfg_device(val) != AC_JACK_HP_OUT)
5124 			continue;
5125 		val = snd_hda_codec_read(codec, nids[i], 0,
5126 					 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5127 		val |= AC_PINCTL_VREF_80;
5128 		snd_hda_set_pin_ctl(codec, nids[i], val);
5129 		spec->keep_vref_in_automute = 1;
5130 		break;
5131 	}
5132 }
5133 
5134 /* Set VREF on speaker pins on imac91 */
5135 static void alc889_fixup_imac91_vref(struct hda_codec *codec,
5136 				     const struct alc_fixup *fix, int action)
5137 {
5138 	struct alc_spec *spec = codec->spec;
5139 	static hda_nid_t nids[2] = { 0x18, 0x1a };
5140 	int i;
5141 
5142 	if (action != ALC_FIXUP_ACT_INIT)
5143 		return;
5144 	for (i = 0; i < ARRAY_SIZE(nids); i++) {
5145 		unsigned int val;
5146 		val = snd_hda_codec_read(codec, nids[i], 0,
5147 					 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5148 		val |= AC_PINCTL_VREF_50;
5149 		snd_hda_set_pin_ctl(codec, nids[i], val);
5150 	}
5151 	spec->keep_vref_in_automute = 1;
5152 }
5153 
5154 /* Don't take HP output as primary
5155  * strangely, the speaker output doesn't work on VAIO Z through DAC 0x05
5156  */
5157 static void alc882_fixup_no_primary_hp(struct hda_codec *codec,
5158 				       const struct alc_fixup *fix, int action)
5159 {
5160 	struct alc_spec *spec = codec->spec;
5161 	if (action == ALC_FIXUP_ACT_PRE_PROBE)
5162 		spec->no_primary_hp = 1;
5163 }
5164 
5165 static const struct alc_fixup alc882_fixups[] = {
5166 	[ALC882_FIXUP_ABIT_AW9D_MAX] = {
5167 		.type = ALC_FIXUP_PINS,
5168 		.v.pins = (const struct alc_pincfg[]) {
5169 			{ 0x15, 0x01080104 }, /* side */
5170 			{ 0x16, 0x01011012 }, /* rear */
5171 			{ 0x17, 0x01016011 }, /* clfe */
5172 			{ }
5173 		}
5174 	},
5175 	[ALC882_FIXUP_LENOVO_Y530] = {
5176 		.type = ALC_FIXUP_PINS,
5177 		.v.pins = (const struct alc_pincfg[]) {
5178 			{ 0x15, 0x99130112 }, /* rear int speakers */
5179 			{ 0x16, 0x99130111 }, /* subwoofer */
5180 			{ }
5181 		}
5182 	},
5183 	[ALC882_FIXUP_PB_M5210] = {
5184 		.type = ALC_FIXUP_VERBS,
5185 		.v.verbs = (const struct hda_verb[]) {
5186 			{ 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF50 },
5187 			{}
5188 		}
5189 	},
5190 	[ALC882_FIXUP_ACER_ASPIRE_7736] = {
5191 		.type = ALC_FIXUP_FUNC,
5192 		.v.func = alc_fixup_sku_ignore,
5193 	},
5194 	[ALC882_FIXUP_ASUS_W90V] = {
5195 		.type = ALC_FIXUP_PINS,
5196 		.v.pins = (const struct alc_pincfg[]) {
5197 			{ 0x16, 0x99130110 }, /* fix sequence for CLFE */
5198 			{ }
5199 		}
5200 	},
5201 	[ALC889_FIXUP_CD] = {
5202 		.type = ALC_FIXUP_PINS,
5203 		.v.pins = (const struct alc_pincfg[]) {
5204 			{ 0x1c, 0x993301f0 }, /* CD */
5205 			{ }
5206 		}
5207 	},
5208 	[ALC889_FIXUP_VAIO_TT] = {
5209 		.type = ALC_FIXUP_PINS,
5210 		.v.pins = (const struct alc_pincfg[]) {
5211 			{ 0x17, 0x90170111 }, /* hidden surround speaker */
5212 			{ }
5213 		}
5214 	},
5215 	[ALC888_FIXUP_EEE1601] = {
5216 		.type = ALC_FIXUP_VERBS,
5217 		.v.verbs = (const struct hda_verb[]) {
5218 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x0b },
5219 			{ 0x20, AC_VERB_SET_PROC_COEF,  0x0838 },
5220 			{ }
5221 		}
5222 	},
5223 	[ALC882_FIXUP_EAPD] = {
5224 		.type = ALC_FIXUP_VERBS,
5225 		.v.verbs = (const struct hda_verb[]) {
5226 			/* change to EAPD mode */
5227 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5228 			{ 0x20, AC_VERB_SET_PROC_COEF, 0x3060 },
5229 			{ }
5230 		}
5231 	},
5232 	[ALC883_FIXUP_EAPD] = {
5233 		.type = ALC_FIXUP_VERBS,
5234 		.v.verbs = (const struct hda_verb[]) {
5235 			/* change to EAPD mode */
5236 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5237 			{ 0x20, AC_VERB_SET_PROC_COEF, 0x3070 },
5238 			{ }
5239 		}
5240 	},
5241 	[ALC883_FIXUP_ACER_EAPD] = {
5242 		.type = ALC_FIXUP_VERBS,
5243 		.v.verbs = (const struct hda_verb[]) {
5244 			/* eanable EAPD on Acer laptops */
5245 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5246 			{ 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
5247 			{ }
5248 		}
5249 	},
5250 	[ALC882_FIXUP_GPIO1] = {
5251 		.type = ALC_FIXUP_VERBS,
5252 		.v.verbs = alc_gpio1_init_verbs,
5253 	},
5254 	[ALC882_FIXUP_GPIO2] = {
5255 		.type = ALC_FIXUP_VERBS,
5256 		.v.verbs = alc_gpio2_init_verbs,
5257 	},
5258 	[ALC882_FIXUP_GPIO3] = {
5259 		.type = ALC_FIXUP_VERBS,
5260 		.v.verbs = alc_gpio3_init_verbs,
5261 	},
5262 	[ALC882_FIXUP_ASUS_W2JC] = {
5263 		.type = ALC_FIXUP_VERBS,
5264 		.v.verbs = alc_gpio1_init_verbs,
5265 		.chained = true,
5266 		.chain_id = ALC882_FIXUP_EAPD,
5267 	},
5268 	[ALC889_FIXUP_COEF] = {
5269 		.type = ALC_FIXUP_FUNC,
5270 		.v.func = alc889_fixup_coef,
5271 	},
5272 	[ALC882_FIXUP_ACER_ASPIRE_4930G] = {
5273 		.type = ALC_FIXUP_PINS,
5274 		.v.pins = (const struct alc_pincfg[]) {
5275 			{ 0x16, 0x99130111 }, /* CLFE speaker */
5276 			{ 0x17, 0x99130112 }, /* surround speaker */
5277 			{ }
5278 		},
5279 		.chained = true,
5280 		.chain_id = ALC882_FIXUP_GPIO1,
5281 	},
5282 	[ALC882_FIXUP_ACER_ASPIRE_8930G] = {
5283 		.type = ALC_FIXUP_PINS,
5284 		.v.pins = (const struct alc_pincfg[]) {
5285 			{ 0x16, 0x99130111 }, /* CLFE speaker */
5286 			{ 0x1b, 0x99130112 }, /* surround speaker */
5287 			{ }
5288 		},
5289 		.chained = true,
5290 		.chain_id = ALC882_FIXUP_ASPIRE_8930G_VERBS,
5291 	},
5292 	[ALC882_FIXUP_ASPIRE_8930G_VERBS] = {
5293 		/* additional init verbs for Acer Aspire 8930G */
5294 		.type = ALC_FIXUP_VERBS,
5295 		.v.verbs = (const struct hda_verb[]) {
5296 			/* Enable all DACs */
5297 			/* DAC DISABLE/MUTE 1? */
5298 			/*  setting bits 1-5 disables DAC nids 0x02-0x06
5299 			 *  apparently. Init=0x38 */
5300 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x03 },
5301 			{ 0x20, AC_VERB_SET_PROC_COEF, 0x0000 },
5302 			/* DAC DISABLE/MUTE 2? */
5303 			/*  some bit here disables the other DACs.
5304 			 *  Init=0x4900 */
5305 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x08 },
5306 			{ 0x20, AC_VERB_SET_PROC_COEF, 0x0000 },
5307 			/* DMIC fix
5308 			 * This laptop has a stereo digital microphone.
5309 			 * The mics are only 1cm apart which makes the stereo
5310 			 * useless. However, either the mic or the ALC889
5311 			 * makes the signal become a difference/sum signal
5312 			 * instead of standard stereo, which is annoying.
5313 			 * So instead we flip this bit which makes the
5314 			 * codec replicate the sum signal to both channels,
5315 			 * turning it into a normal mono mic.
5316 			 */
5317 			/* DMIC_CONTROL? Init value = 0x0001 */
5318 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x0b },
5319 			{ 0x20, AC_VERB_SET_PROC_COEF, 0x0003 },
5320 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5321 			{ 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
5322 			{ }
5323 		},
5324 		.chained = true,
5325 		.chain_id = ALC882_FIXUP_GPIO1,
5326 	},
5327 	[ALC885_FIXUP_MACPRO_GPIO] = {
5328 		.type = ALC_FIXUP_FUNC,
5329 		.v.func = alc885_fixup_macpro_gpio,
5330 	},
5331 	[ALC889_FIXUP_DAC_ROUTE] = {
5332 		.type = ALC_FIXUP_FUNC,
5333 		.v.func = alc889_fixup_dac_route,
5334 	},
5335 	[ALC889_FIXUP_MBP_VREF] = {
5336 		.type = ALC_FIXUP_FUNC,
5337 		.v.func = alc889_fixup_mbp_vref,
5338 		.chained = true,
5339 		.chain_id = ALC882_FIXUP_GPIO1,
5340 	},
5341 	[ALC889_FIXUP_IMAC91_VREF] = {
5342 		.type = ALC_FIXUP_FUNC,
5343 		.v.func = alc889_fixup_imac91_vref,
5344 		.chained = true,
5345 		.chain_id = ALC882_FIXUP_GPIO1,
5346 	},
5347 	[ALC882_FIXUP_INV_DMIC] = {
5348 		.type = ALC_FIXUP_FUNC,
5349 		.v.func = alc_fixup_inv_dmic_0x12,
5350 	},
5351 	[ALC882_FIXUP_NO_PRIMARY_HP] = {
5352 		.type = ALC_FIXUP_FUNC,
5353 		.v.func = alc882_fixup_no_primary_hp,
5354 	},
5355 };
5356 
5357 static const struct snd_pci_quirk alc882_fixup_tbl[] = {
5358 	SND_PCI_QUIRK(0x1025, 0x006c, "Acer Aspire 9810", ALC883_FIXUP_ACER_EAPD),
5359 	SND_PCI_QUIRK(0x1025, 0x0090, "Acer Aspire", ALC883_FIXUP_ACER_EAPD),
5360 	SND_PCI_QUIRK(0x1025, 0x010a, "Acer Ferrari 5000", ALC883_FIXUP_ACER_EAPD),
5361 	SND_PCI_QUIRK(0x1025, 0x0110, "Acer Aspire", ALC883_FIXUP_ACER_EAPD),
5362 	SND_PCI_QUIRK(0x1025, 0x0112, "Acer Aspire 9303", ALC883_FIXUP_ACER_EAPD),
5363 	SND_PCI_QUIRK(0x1025, 0x0121, "Acer Aspire 5920G", ALC883_FIXUP_ACER_EAPD),
5364 	SND_PCI_QUIRK(0x1025, 0x013e, "Acer Aspire 4930G",
5365 		      ALC882_FIXUP_ACER_ASPIRE_4930G),
5366 	SND_PCI_QUIRK(0x1025, 0x013f, "Acer Aspire 5930G",
5367 		      ALC882_FIXUP_ACER_ASPIRE_4930G),
5368 	SND_PCI_QUIRK(0x1025, 0x0145, "Acer Aspire 8930G",
5369 		      ALC882_FIXUP_ACER_ASPIRE_8930G),
5370 	SND_PCI_QUIRK(0x1025, 0x0146, "Acer Aspire 6935G",
5371 		      ALC882_FIXUP_ACER_ASPIRE_8930G),
5372 	SND_PCI_QUIRK(0x1025, 0x015e, "Acer Aspire 6930G",
5373 		      ALC882_FIXUP_ACER_ASPIRE_4930G),
5374 	SND_PCI_QUIRK(0x1025, 0x0166, "Acer Aspire 6530G",
5375 		      ALC882_FIXUP_ACER_ASPIRE_4930G),
5376 	SND_PCI_QUIRK(0x1025, 0x0142, "Acer Aspire 7730G",
5377 		      ALC882_FIXUP_ACER_ASPIRE_4930G),
5378 	SND_PCI_QUIRK(0x1025, 0x0155, "Packard-Bell M5120", ALC882_FIXUP_PB_M5210),
5379 	SND_PCI_QUIRK(0x1025, 0x021e, "Acer Aspire 5739G",
5380 		      ALC882_FIXUP_ACER_ASPIRE_4930G),
5381 	SND_PCI_QUIRK(0x1025, 0x0259, "Acer Aspire 5935", ALC889_FIXUP_DAC_ROUTE),
5382 	SND_PCI_QUIRK(0x1025, 0x026b, "Acer Aspire 8940G", ALC882_FIXUP_ACER_ASPIRE_8930G),
5383 	SND_PCI_QUIRK(0x1025, 0x0296, "Acer Aspire 7736z", ALC882_FIXUP_ACER_ASPIRE_7736),
5384 	SND_PCI_QUIRK(0x1043, 0x13c2, "Asus A7M", ALC882_FIXUP_EAPD),
5385 	SND_PCI_QUIRK(0x1043, 0x1873, "ASUS W90V", ALC882_FIXUP_ASUS_W90V),
5386 	SND_PCI_QUIRK(0x1043, 0x1971, "Asus W2JC", ALC882_FIXUP_ASUS_W2JC),
5387 	SND_PCI_QUIRK(0x1043, 0x835f, "Asus Eee 1601", ALC888_FIXUP_EEE1601),
5388 	SND_PCI_QUIRK(0x104d, 0x9047, "Sony Vaio TT", ALC889_FIXUP_VAIO_TT),
5389 	SND_PCI_QUIRK(0x104d, 0x905a, "Sony Vaio Z", ALC882_FIXUP_NO_PRIMARY_HP),
5390 
5391 	/* All Apple entries are in codec SSIDs */
5392 	SND_PCI_QUIRK(0x106b, 0x00a0, "MacBookPro 3,1", ALC889_FIXUP_MBP_VREF),
5393 	SND_PCI_QUIRK(0x106b, 0x00a1, "Macbook", ALC889_FIXUP_MBP_VREF),
5394 	SND_PCI_QUIRK(0x106b, 0x00a4, "MacbookPro 4,1", ALC889_FIXUP_MBP_VREF),
5395 	SND_PCI_QUIRK(0x106b, 0x0c00, "Mac Pro", ALC885_FIXUP_MACPRO_GPIO),
5396 	SND_PCI_QUIRK(0x106b, 0x1000, "iMac 24", ALC885_FIXUP_MACPRO_GPIO),
5397 	SND_PCI_QUIRK(0x106b, 0x2800, "AppleTV", ALC885_FIXUP_MACPRO_GPIO),
5398 	SND_PCI_QUIRK(0x106b, 0x2c00, "MacbookPro rev3", ALC889_FIXUP_MBP_VREF),
5399 	SND_PCI_QUIRK(0x106b, 0x3000, "iMac", ALC889_FIXUP_MBP_VREF),
5400 	SND_PCI_QUIRK(0x106b, 0x3200, "iMac 7,1 Aluminum", ALC882_FIXUP_EAPD),
5401 	SND_PCI_QUIRK(0x106b, 0x3400, "MacBookAir 1,1", ALC889_FIXUP_MBP_VREF),
5402 	SND_PCI_QUIRK(0x106b, 0x3500, "MacBookAir 2,1", ALC889_FIXUP_MBP_VREF),
5403 	SND_PCI_QUIRK(0x106b, 0x3600, "Macbook 3,1", ALC889_FIXUP_MBP_VREF),
5404 	SND_PCI_QUIRK(0x106b, 0x3800, "MacbookPro 4,1", ALC889_FIXUP_MBP_VREF),
5405 	SND_PCI_QUIRK(0x106b, 0x3e00, "iMac 24 Aluminum", ALC885_FIXUP_MACPRO_GPIO),
5406 	SND_PCI_QUIRK(0x106b, 0x3f00, "Macbook 5,1", ALC889_FIXUP_IMAC91_VREF),
5407 	SND_PCI_QUIRK(0x106b, 0x4000, "MacbookPro 5,1", ALC889_FIXUP_IMAC91_VREF),
5408 	SND_PCI_QUIRK(0x106b, 0x4100, "Macmini 3,1", ALC889_FIXUP_IMAC91_VREF),
5409 	SND_PCI_QUIRK(0x106b, 0x4200, "Mac Pro 5,1", ALC885_FIXUP_MACPRO_GPIO),
5410 	SND_PCI_QUIRK(0x106b, 0x4600, "MacbookPro 5,2", ALC889_FIXUP_IMAC91_VREF),
5411 	SND_PCI_QUIRK(0x106b, 0x4900, "iMac 9,1 Aluminum", ALC889_FIXUP_IMAC91_VREF),
5412 	SND_PCI_QUIRK(0x106b, 0x4a00, "Macbook 5,2", ALC889_FIXUP_IMAC91_VREF),
5413 
5414 	SND_PCI_QUIRK(0x1071, 0x8258, "Evesham Voyaeger", ALC882_FIXUP_EAPD),
5415 	SND_PCI_QUIRK(0x1462, 0x7350, "MSI-7350", ALC889_FIXUP_CD),
5416 	SND_PCI_QUIRK_VENDOR(0x1462, "MSI", ALC882_FIXUP_GPIO3),
5417 	SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte EP45-DS3", ALC889_FIXUP_CD),
5418 	SND_PCI_QUIRK(0x147b, 0x107a, "Abit AW9D-MAX", ALC882_FIXUP_ABIT_AW9D_MAX),
5419 	SND_PCI_QUIRK_VENDOR(0x1558, "Clevo laptop", ALC882_FIXUP_EAPD),
5420 	SND_PCI_QUIRK(0x161f, 0x2054, "Medion laptop", ALC883_FIXUP_EAPD),
5421 	SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo Y530", ALC882_FIXUP_LENOVO_Y530),
5422 	SND_PCI_QUIRK(0x8086, 0x0022, "DX58SO", ALC889_FIXUP_COEF),
5423 	{}
5424 };
5425 
5426 static const struct alc_model_fixup alc882_fixup_models[] = {
5427 	{.id = ALC882_FIXUP_ACER_ASPIRE_4930G, .name = "acer-aspire-4930g"},
5428 	{.id = ALC882_FIXUP_ACER_ASPIRE_8930G, .name = "acer-aspire-8930g"},
5429 	{.id = ALC883_FIXUP_ACER_EAPD, .name = "acer-aspire"},
5430 	{.id = ALC882_FIXUP_INV_DMIC, .name = "inv-dmic"},
5431 	{.id = ALC882_FIXUP_NO_PRIMARY_HP, .name = "no-primary-hp"},
5432 	{}
5433 };
5434 
5435 /*
5436  * BIOS auto configuration
5437  */
5438 /* almost identical with ALC880 parser... */
5439 static int alc882_parse_auto_config(struct hda_codec *codec)
5440 {
5441 	static const hda_nid_t alc882_ignore[] = { 0x1d, 0 };
5442 	static const hda_nid_t alc882_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5443 	return alc_parse_auto_config(codec, alc882_ignore, alc882_ssids);
5444 }
5445 
5446 /*
5447  */
5448 static int patch_alc882(struct hda_codec *codec)
5449 {
5450 	struct alc_spec *spec;
5451 	int err;
5452 
5453 	err = alc_alloc_spec(codec, 0x0b);
5454 	if (err < 0)
5455 		return err;
5456 
5457 	spec = codec->spec;
5458 
5459 	switch (codec->vendor_id) {
5460 	case 0x10ec0882:
5461 	case 0x10ec0885:
5462 		break;
5463 	default:
5464 		/* ALC883 and variants */
5465 		alc_fix_pll_init(codec, 0x20, 0x0a, 10);
5466 		break;
5467 	}
5468 
5469 	alc_pick_fixup(codec, alc882_fixup_models, alc882_fixup_tbl,
5470 		       alc882_fixups);
5471 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
5472 
5473 	alc_auto_parse_customize_define(codec);
5474 
5475 	/* automatic parse from the BIOS config */
5476 	err = alc882_parse_auto_config(codec);
5477 	if (err < 0)
5478 		goto error;
5479 
5480 	if (!spec->no_analog && has_cdefine_beep(codec)) {
5481 		err = snd_hda_attach_beep_device(codec, 0x1);
5482 		if (err < 0)
5483 			goto error;
5484 		set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
5485 	}
5486 
5487 	codec->patch_ops = alc_patch_ops;
5488 
5489 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
5490 
5491 	return 0;
5492 
5493  error:
5494 	alc_free(codec);
5495 	return err;
5496 }
5497 
5498 
5499 /*
5500  * ALC262 support
5501  */
5502 static int alc262_parse_auto_config(struct hda_codec *codec)
5503 {
5504 	static const hda_nid_t alc262_ignore[] = { 0x1d, 0 };
5505 	static const hda_nid_t alc262_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5506 	return alc_parse_auto_config(codec, alc262_ignore, alc262_ssids);
5507 }
5508 
5509 /*
5510  * Pin config fixes
5511  */
5512 enum {
5513 	ALC262_FIXUP_FSC_H270,
5514 	ALC262_FIXUP_HP_Z200,
5515 	ALC262_FIXUP_TYAN,
5516 	ALC262_FIXUP_LENOVO_3000,
5517 	ALC262_FIXUP_BENQ,
5518 	ALC262_FIXUP_BENQ_T31,
5519 	ALC262_FIXUP_INV_DMIC,
5520 };
5521 
5522 static const struct alc_fixup alc262_fixups[] = {
5523 	[ALC262_FIXUP_FSC_H270] = {
5524 		.type = ALC_FIXUP_PINS,
5525 		.v.pins = (const struct alc_pincfg[]) {
5526 			{ 0x14, 0x99130110 }, /* speaker */
5527 			{ 0x15, 0x0221142f }, /* front HP */
5528 			{ 0x1b, 0x0121141f }, /* rear HP */
5529 			{ }
5530 		}
5531 	},
5532 	[ALC262_FIXUP_HP_Z200] = {
5533 		.type = ALC_FIXUP_PINS,
5534 		.v.pins = (const struct alc_pincfg[]) {
5535 			{ 0x16, 0x99130120 }, /* internal speaker */
5536 			{ }
5537 		}
5538 	},
5539 	[ALC262_FIXUP_TYAN] = {
5540 		.type = ALC_FIXUP_PINS,
5541 		.v.pins = (const struct alc_pincfg[]) {
5542 			{ 0x14, 0x1993e1f0 }, /* int AUX */
5543 			{ }
5544 		}
5545 	},
5546 	[ALC262_FIXUP_LENOVO_3000] = {
5547 		.type = ALC_FIXUP_VERBS,
5548 		.v.verbs = (const struct hda_verb[]) {
5549 			{ 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF50 },
5550 			{}
5551 		},
5552 		.chained = true,
5553 		.chain_id = ALC262_FIXUP_BENQ,
5554 	},
5555 	[ALC262_FIXUP_BENQ] = {
5556 		.type = ALC_FIXUP_VERBS,
5557 		.v.verbs = (const struct hda_verb[]) {
5558 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5559 			{ 0x20, AC_VERB_SET_PROC_COEF, 0x3070 },
5560 			{}
5561 		}
5562 	},
5563 	[ALC262_FIXUP_BENQ_T31] = {
5564 		.type = ALC_FIXUP_VERBS,
5565 		.v.verbs = (const struct hda_verb[]) {
5566 			{ 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5567 			{ 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
5568 			{}
5569 		}
5570 	},
5571 	[ALC262_FIXUP_INV_DMIC] = {
5572 		.type = ALC_FIXUP_FUNC,
5573 		.v.func = alc_fixup_inv_dmic_0x12,
5574 	},
5575 };
5576 
5577 static const struct snd_pci_quirk alc262_fixup_tbl[] = {
5578 	SND_PCI_QUIRK(0x103c, 0x170b, "HP Z200", ALC262_FIXUP_HP_Z200),
5579 	SND_PCI_QUIRK(0x10cf, 0x1397, "Fujitsu", ALC262_FIXUP_BENQ),
5580 	SND_PCI_QUIRK(0x10cf, 0x142d, "Fujitsu Lifebook E8410", ALC262_FIXUP_BENQ),
5581 	SND_PCI_QUIRK(0x10f1, 0x2915, "Tyan Thunder n6650W", ALC262_FIXUP_TYAN),
5582 	SND_PCI_QUIRK(0x1734, 0x1147, "FSC Celsius H270", ALC262_FIXUP_FSC_H270),
5583 	SND_PCI_QUIRK(0x17aa, 0x384e, "Lenovo 3000", ALC262_FIXUP_LENOVO_3000),
5584 	SND_PCI_QUIRK(0x17ff, 0x0560, "Benq ED8", ALC262_FIXUP_BENQ),
5585 	SND_PCI_QUIRK(0x17ff, 0x058d, "Benq T31-16", ALC262_FIXUP_BENQ_T31),
5586 	{}
5587 };
5588 
5589 static const struct alc_model_fixup alc262_fixup_models[] = {
5590 	{.id = ALC262_FIXUP_INV_DMIC, .name = "inv-dmic"},
5591 	{}
5592 };
5593 
5594 /*
5595  */
5596 static int patch_alc262(struct hda_codec *codec)
5597 {
5598 	struct alc_spec *spec;
5599 	int err;
5600 
5601 	err = alc_alloc_spec(codec, 0x0b);
5602 	if (err < 0)
5603 		return err;
5604 
5605 	spec = codec->spec;
5606 
5607 #if 0
5608 	/* pshou 07/11/05  set a zero PCM sample to DAC when FIFO is
5609 	 * under-run
5610 	 */
5611 	{
5612 	int tmp;
5613 	snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
5614 	tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
5615 	snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
5616 	snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PROC_COEF, tmp | 0x80);
5617 	}
5618 #endif
5619 	alc_fix_pll_init(codec, 0x20, 0x0a, 10);
5620 
5621 	alc_pick_fixup(codec, alc262_fixup_models, alc262_fixup_tbl,
5622 		       alc262_fixups);
5623 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
5624 
5625 	alc_auto_parse_customize_define(codec);
5626 
5627 	/* automatic parse from the BIOS config */
5628 	err = alc262_parse_auto_config(codec);
5629 	if (err < 0)
5630 		goto error;
5631 
5632 	if (!spec->no_analog && has_cdefine_beep(codec)) {
5633 		err = snd_hda_attach_beep_device(codec, 0x1);
5634 		if (err < 0)
5635 			goto error;
5636 		set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
5637 	}
5638 
5639 	codec->patch_ops = alc_patch_ops;
5640 	spec->shutup = alc_eapd_shutup;
5641 
5642 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
5643 
5644 	return 0;
5645 
5646  error:
5647 	alc_free(codec);
5648 	return err;
5649 }
5650 
5651 /*
5652  *  ALC268
5653  */
5654 /* bind Beep switches of both NID 0x0f and 0x10 */
5655 static const struct hda_bind_ctls alc268_bind_beep_sw = {
5656 	.ops = &snd_hda_bind_sw,
5657 	.values = {
5658 		HDA_COMPOSE_AMP_VAL(0x0f, 3, 1, HDA_INPUT),
5659 		HDA_COMPOSE_AMP_VAL(0x10, 3, 1, HDA_INPUT),
5660 		0
5661 	},
5662 };
5663 
5664 static const struct snd_kcontrol_new alc268_beep_mixer[] = {
5665 	HDA_CODEC_VOLUME("Beep Playback Volume", 0x1d, 0x0, HDA_INPUT),
5666 	HDA_BIND_SW("Beep Playback Switch", &alc268_bind_beep_sw),
5667 	{ }
5668 };
5669 
5670 /* set PCBEEP vol = 0, mute connections */
5671 static const struct hda_verb alc268_beep_init_verbs[] = {
5672 	{0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5673 	{0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5674 	{0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5675 	{ }
5676 };
5677 
5678 enum {
5679 	ALC268_FIXUP_INV_DMIC,
5680 	ALC268_FIXUP_HP_EAPD,
5681 };
5682 
5683 static const struct alc_fixup alc268_fixups[] = {
5684 	[ALC268_FIXUP_INV_DMIC] = {
5685 		.type = ALC_FIXUP_FUNC,
5686 		.v.func = alc_fixup_inv_dmic_0x12,
5687 	},
5688 	[ALC268_FIXUP_HP_EAPD] = {
5689 		.type = ALC_FIXUP_VERBS,
5690 		.v.verbs = (const struct hda_verb[]) {
5691 			{0x15, AC_VERB_SET_EAPD_BTLENABLE, 0},
5692 			{}
5693 		}
5694 	},
5695 };
5696 
5697 static const struct alc_model_fixup alc268_fixup_models[] = {
5698 	{.id = ALC268_FIXUP_INV_DMIC, .name = "inv-dmic"},
5699 	{.id = ALC268_FIXUP_HP_EAPD, .name = "hp-eapd"},
5700 	{}
5701 };
5702 
5703 static const struct snd_pci_quirk alc268_fixup_tbl[] = {
5704 	/* below is codec SSID since multiple Toshiba laptops have the
5705 	 * same PCI SSID 1179:ff00
5706 	 */
5707 	SND_PCI_QUIRK(0x1179, 0xff06, "Toshiba P200", ALC268_FIXUP_HP_EAPD),
5708 	{}
5709 };
5710 
5711 /*
5712  * BIOS auto configuration
5713  */
5714 static int alc268_parse_auto_config(struct hda_codec *codec)
5715 {
5716 	static const hda_nid_t alc268_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5717 	struct alc_spec *spec = codec->spec;
5718 	int err = alc_parse_auto_config(codec, NULL, alc268_ssids);
5719 	if (err > 0) {
5720 		if (!spec->no_analog && spec->autocfg.speaker_pins[0] != 0x1d) {
5721 			add_mixer(spec, alc268_beep_mixer);
5722 			snd_hda_gen_add_verbs(&spec->gen, alc268_beep_init_verbs);
5723 		}
5724 	}
5725 	return err;
5726 }
5727 
5728 /*
5729  */
5730 static int patch_alc268(struct hda_codec *codec)
5731 {
5732 	struct alc_spec *spec;
5733 	int i, has_beep, err;
5734 
5735 	/* ALC268 has no aa-loopback mixer */
5736 	err = alc_alloc_spec(codec, 0);
5737 	if (err < 0)
5738 		return err;
5739 
5740 	spec = codec->spec;
5741 
5742 	alc_pick_fixup(codec, alc268_fixup_models, alc268_fixup_tbl, alc268_fixups);
5743 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
5744 
5745 	/* automatic parse from the BIOS config */
5746 	err = alc268_parse_auto_config(codec);
5747 	if (err < 0)
5748 		goto error;
5749 
5750 	has_beep = 0;
5751 	for (i = 0; i < spec->num_mixers; i++) {
5752 		if (spec->mixers[i] == alc268_beep_mixer) {
5753 			has_beep = 1;
5754 			break;
5755 		}
5756 	}
5757 
5758 	if (has_beep) {
5759 		err = snd_hda_attach_beep_device(codec, 0x1);
5760 		if (err < 0)
5761 			goto error;
5762 		if (!query_amp_caps(codec, 0x1d, HDA_INPUT))
5763 			/* override the amp caps for beep generator */
5764 			snd_hda_override_amp_caps(codec, 0x1d, HDA_INPUT,
5765 					  (0x0c << AC_AMPCAP_OFFSET_SHIFT) |
5766 					  (0x0c << AC_AMPCAP_NUM_STEPS_SHIFT) |
5767 					  (0x07 << AC_AMPCAP_STEP_SIZE_SHIFT) |
5768 					  (0 << AC_AMPCAP_MUTE_SHIFT));
5769 	}
5770 
5771 	codec->patch_ops = alc_patch_ops;
5772 	spec->shutup = alc_eapd_shutup;
5773 
5774 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
5775 
5776 	return 0;
5777 
5778  error:
5779 	alc_free(codec);
5780 	return err;
5781 }
5782 
5783 /*
5784  * ALC269
5785  */
5786 static const struct hda_pcm_stream alc269_44k_pcm_analog_playback = {
5787 	.substreams = 1,
5788 	.channels_min = 2,
5789 	.channels_max = 8,
5790 	.rates = SNDRV_PCM_RATE_44100, /* fixed rate */
5791 	/* NID is set in alc_build_pcms */
5792 	.ops = {
5793 		.open = alc_playback_pcm_open,
5794 		.prepare = alc_playback_pcm_prepare,
5795 		.cleanup = alc_playback_pcm_cleanup
5796 	},
5797 };
5798 
5799 static const struct hda_pcm_stream alc269_44k_pcm_analog_capture = {
5800 	.substreams = 1,
5801 	.channels_min = 2,
5802 	.channels_max = 2,
5803 	.rates = SNDRV_PCM_RATE_44100, /* fixed rate */
5804 	/* NID is set in alc_build_pcms */
5805 };
5806 
5807 /* different alc269-variants */
5808 enum {
5809 	ALC269_TYPE_ALC269VA,
5810 	ALC269_TYPE_ALC269VB,
5811 	ALC269_TYPE_ALC269VC,
5812 	ALC269_TYPE_ALC269VD,
5813 };
5814 
5815 /*
5816  * BIOS auto configuration
5817  */
5818 static int alc269_parse_auto_config(struct hda_codec *codec)
5819 {
5820 	static const hda_nid_t alc269_ignore[] = { 0x1d, 0 };
5821 	static const hda_nid_t alc269_ssids[] = { 0, 0x1b, 0x14, 0x21 };
5822 	static const hda_nid_t alc269va_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5823 	struct alc_spec *spec = codec->spec;
5824 	const hda_nid_t *ssids;
5825 
5826 	switch (spec->codec_variant) {
5827 	case ALC269_TYPE_ALC269VA:
5828 	case ALC269_TYPE_ALC269VC:
5829 		ssids = alc269va_ssids;
5830 		break;
5831 	case ALC269_TYPE_ALC269VB:
5832 	case ALC269_TYPE_ALC269VD:
5833 		ssids = alc269_ssids;
5834 		break;
5835 	default:
5836 		ssids = alc269_ssids;
5837 		break;
5838 	}
5839 
5840 	return alc_parse_auto_config(codec, alc269_ignore, ssids);
5841 }
5842 
5843 static void alc269_toggle_power_output(struct hda_codec *codec, int power_up)
5844 {
5845 	int val = alc_read_coef_idx(codec, 0x04);
5846 	if (power_up)
5847 		val |= 1 << 11;
5848 	else
5849 		val &= ~(1 << 11);
5850 	alc_write_coef_idx(codec, 0x04, val);
5851 }
5852 
5853 static void alc269_shutup(struct hda_codec *codec)
5854 {
5855 	struct alc_spec *spec = codec->spec;
5856 
5857 	if (spec->codec_variant != ALC269_TYPE_ALC269VB)
5858 		return;
5859 
5860 	if ((alc_get_coef0(codec) & 0x00ff) == 0x017)
5861 		alc269_toggle_power_output(codec, 0);
5862 	if ((alc_get_coef0(codec) & 0x00ff) == 0x018) {
5863 		alc269_toggle_power_output(codec, 0);
5864 		msleep(150);
5865 	}
5866 }
5867 
5868 #ifdef CONFIG_PM
5869 static int alc269_resume(struct hda_codec *codec)
5870 {
5871 	struct alc_spec *spec = codec->spec;
5872 
5873 	if (spec->codec_variant == ALC269_TYPE_ALC269VB ||
5874 			(alc_get_coef0(codec) & 0x00ff) == 0x018) {
5875 		alc269_toggle_power_output(codec, 0);
5876 		msleep(150);
5877 	}
5878 
5879 	codec->patch_ops.init(codec);
5880 
5881 	if (spec->codec_variant == ALC269_TYPE_ALC269VB ||
5882 			(alc_get_coef0(codec) & 0x00ff) == 0x017) {
5883 		alc269_toggle_power_output(codec, 1);
5884 		msleep(200);
5885 	}
5886 
5887 	if (spec->codec_variant == ALC269_TYPE_ALC269VB ||
5888 			(alc_get_coef0(codec) & 0x00ff) == 0x018)
5889 		alc269_toggle_power_output(codec, 1);
5890 
5891 	snd_hda_codec_resume_amp(codec);
5892 	snd_hda_codec_resume_cache(codec);
5893 	hda_call_check_power_status(codec, 0x01);
5894 	return 0;
5895 }
5896 #endif /* CONFIG_PM */
5897 
5898 static void alc269_fixup_pincfg_no_hp_to_lineout(struct hda_codec *codec,
5899 						 const struct alc_fixup *fix, int action)
5900 {
5901 	struct alc_spec *spec = codec->spec;
5902 
5903 	if (action == ALC_FIXUP_ACT_PRE_PROBE)
5904 		spec->parse_flags = HDA_PINCFG_NO_HP_FIXUP;
5905 }
5906 
5907 static void alc269_fixup_hweq(struct hda_codec *codec,
5908 			       const struct alc_fixup *fix, int action)
5909 {
5910 	int coef;
5911 
5912 	if (action != ALC_FIXUP_ACT_INIT)
5913 		return;
5914 	coef = alc_read_coef_idx(codec, 0x1e);
5915 	alc_write_coef_idx(codec, 0x1e, coef | 0x80);
5916 }
5917 
5918 static void alc271_fixup_dmic(struct hda_codec *codec,
5919 			      const struct alc_fixup *fix, int action)
5920 {
5921 	static const struct hda_verb verbs[] = {
5922 		{0x20, AC_VERB_SET_COEF_INDEX, 0x0d},
5923 		{0x20, AC_VERB_SET_PROC_COEF, 0x4000},
5924 		{}
5925 	};
5926 	unsigned int cfg;
5927 
5928 	if (strcmp(codec->chip_name, "ALC271X"))
5929 		return;
5930 	cfg = snd_hda_codec_get_pincfg(codec, 0x12);
5931 	if (get_defcfg_connect(cfg) == AC_JACK_PORT_FIXED)
5932 		snd_hda_sequence_write(codec, verbs);
5933 }
5934 
5935 static void alc269_fixup_pcm_44k(struct hda_codec *codec,
5936 				 const struct alc_fixup *fix, int action)
5937 {
5938 	struct alc_spec *spec = codec->spec;
5939 
5940 	if (action != ALC_FIXUP_ACT_PROBE)
5941 		return;
5942 
5943 	/* Due to a hardware problem on Lenovo Ideadpad, we need to
5944 	 * fix the sample rate of analog I/O to 44.1kHz
5945 	 */
5946 	spec->stream_analog_playback = &alc269_44k_pcm_analog_playback;
5947 	spec->stream_analog_capture = &alc269_44k_pcm_analog_capture;
5948 }
5949 
5950 static void alc269_fixup_stereo_dmic(struct hda_codec *codec,
5951 				     const struct alc_fixup *fix, int action)
5952 {
5953 	int coef;
5954 
5955 	if (action != ALC_FIXUP_ACT_INIT)
5956 		return;
5957 	/* The digital-mic unit sends PDM (differential signal) instead of
5958 	 * the standard PCM, thus you can't record a valid mono stream as is.
5959 	 * Below is a workaround specific to ALC269 to control the dmic
5960 	 * signal source as mono.
5961 	 */
5962 	coef = alc_read_coef_idx(codec, 0x07);
5963 	alc_write_coef_idx(codec, 0x07, coef | 0x80);
5964 }
5965 
5966 static void alc269_quanta_automute(struct hda_codec *codec)
5967 {
5968 	update_outputs(codec);
5969 
5970 	snd_hda_codec_write(codec, 0x20, 0,
5971 			AC_VERB_SET_COEF_INDEX, 0x0c);
5972 	snd_hda_codec_write(codec, 0x20, 0,
5973 			AC_VERB_SET_PROC_COEF, 0x680);
5974 
5975 	snd_hda_codec_write(codec, 0x20, 0,
5976 			AC_VERB_SET_COEF_INDEX, 0x0c);
5977 	snd_hda_codec_write(codec, 0x20, 0,
5978 			AC_VERB_SET_PROC_COEF, 0x480);
5979 }
5980 
5981 static void alc269_fixup_quanta_mute(struct hda_codec *codec,
5982 				     const struct alc_fixup *fix, int action)
5983 {
5984 	struct alc_spec *spec = codec->spec;
5985 	if (action != ALC_FIXUP_ACT_PROBE)
5986 		return;
5987 	spec->automute_hook = alc269_quanta_automute;
5988 }
5989 
5990 /* update mute-LED according to the speaker mute state via mic2 VREF pin */
5991 static void alc269_fixup_mic2_mute_hook(void *private_data, int enabled)
5992 {
5993 	struct hda_codec *codec = private_data;
5994 	unsigned int pinval = enabled ? 0x20 : 0x24;
5995 	snd_hda_set_pin_ctl_cache(codec, 0x19, pinval);
5996 }
5997 
5998 static void alc269_fixup_mic2_mute(struct hda_codec *codec,
5999 				   const struct alc_fixup *fix, int action)
6000 {
6001 	struct alc_spec *spec = codec->spec;
6002 	switch (action) {
6003 	case ALC_FIXUP_ACT_BUILD:
6004 		spec->vmaster_mute.hook = alc269_fixup_mic2_mute_hook;
6005 		snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute, true);
6006 		/* fallthru */
6007 	case ALC_FIXUP_ACT_INIT:
6008 		snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
6009 		break;
6010 	}
6011 }
6012 
6013 
6014 enum {
6015 	ALC269_FIXUP_SONY_VAIO,
6016 	ALC275_FIXUP_SONY_VAIO_GPIO2,
6017 	ALC269_FIXUP_DELL_M101Z,
6018 	ALC269_FIXUP_SKU_IGNORE,
6019 	ALC269_FIXUP_ASUS_G73JW,
6020 	ALC269_FIXUP_LENOVO_EAPD,
6021 	ALC275_FIXUP_SONY_HWEQ,
6022 	ALC271_FIXUP_DMIC,
6023 	ALC269_FIXUP_PCM_44K,
6024 	ALC269_FIXUP_STEREO_DMIC,
6025 	ALC269_FIXUP_QUANTA_MUTE,
6026 	ALC269_FIXUP_LIFEBOOK,
6027 	ALC269_FIXUP_AMIC,
6028 	ALC269_FIXUP_DMIC,
6029 	ALC269VB_FIXUP_AMIC,
6030 	ALC269VB_FIXUP_DMIC,
6031 	ALC269_FIXUP_MIC2_MUTE_LED,
6032 	ALC269_FIXUP_INV_DMIC,
6033 	ALC269_FIXUP_LENOVO_DOCK,
6034 	ALC269_FIXUP_PINCFG_NO_HP_TO_LINEOUT,
6035 };
6036 
6037 static const struct alc_fixup alc269_fixups[] = {
6038 	[ALC269_FIXUP_SONY_VAIO] = {
6039 		.type = ALC_FIXUP_VERBS,
6040 		.v.verbs = (const struct hda_verb[]) {
6041 			{0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREFGRD},
6042 			{}
6043 		}
6044 	},
6045 	[ALC275_FIXUP_SONY_VAIO_GPIO2] = {
6046 		.type = ALC_FIXUP_VERBS,
6047 		.v.verbs = (const struct hda_verb[]) {
6048 			{0x01, AC_VERB_SET_GPIO_MASK, 0x04},
6049 			{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x04},
6050 			{0x01, AC_VERB_SET_GPIO_DATA, 0x00},
6051 			{ }
6052 		},
6053 		.chained = true,
6054 		.chain_id = ALC269_FIXUP_SONY_VAIO
6055 	},
6056 	[ALC269_FIXUP_DELL_M101Z] = {
6057 		.type = ALC_FIXUP_VERBS,
6058 		.v.verbs = (const struct hda_verb[]) {
6059 			/* Enables internal speaker */
6060 			{0x20, AC_VERB_SET_COEF_INDEX, 13},
6061 			{0x20, AC_VERB_SET_PROC_COEF, 0x4040},
6062 			{}
6063 		}
6064 	},
6065 	[ALC269_FIXUP_SKU_IGNORE] = {
6066 		.type = ALC_FIXUP_FUNC,
6067 		.v.func = alc_fixup_sku_ignore,
6068 	},
6069 	[ALC269_FIXUP_ASUS_G73JW] = {
6070 		.type = ALC_FIXUP_PINS,
6071 		.v.pins = (const struct alc_pincfg[]) {
6072 			{ 0x17, 0x99130111 }, /* subwoofer */
6073 			{ }
6074 		}
6075 	},
6076 	[ALC269_FIXUP_LENOVO_EAPD] = {
6077 		.type = ALC_FIXUP_VERBS,
6078 		.v.verbs = (const struct hda_verb[]) {
6079 			{0x14, AC_VERB_SET_EAPD_BTLENABLE, 0},
6080 			{}
6081 		}
6082 	},
6083 	[ALC275_FIXUP_SONY_HWEQ] = {
6084 		.type = ALC_FIXUP_FUNC,
6085 		.v.func = alc269_fixup_hweq,
6086 		.chained = true,
6087 		.chain_id = ALC275_FIXUP_SONY_VAIO_GPIO2
6088 	},
6089 	[ALC271_FIXUP_DMIC] = {
6090 		.type = ALC_FIXUP_FUNC,
6091 		.v.func = alc271_fixup_dmic,
6092 	},
6093 	[ALC269_FIXUP_PCM_44K] = {
6094 		.type = ALC_FIXUP_FUNC,
6095 		.v.func = alc269_fixup_pcm_44k,
6096 		.chained = true,
6097 		.chain_id = ALC269_FIXUP_QUANTA_MUTE
6098 	},
6099 	[ALC269_FIXUP_STEREO_DMIC] = {
6100 		.type = ALC_FIXUP_FUNC,
6101 		.v.func = alc269_fixup_stereo_dmic,
6102 	},
6103 	[ALC269_FIXUP_QUANTA_MUTE] = {
6104 		.type = ALC_FIXUP_FUNC,
6105 		.v.func = alc269_fixup_quanta_mute,
6106 	},
6107 	[ALC269_FIXUP_LIFEBOOK] = {
6108 		.type = ALC_FIXUP_PINS,
6109 		.v.pins = (const struct alc_pincfg[]) {
6110 			{ 0x1a, 0x2101103f }, /* dock line-out */
6111 			{ 0x1b, 0x23a11040 }, /* dock mic-in */
6112 			{ }
6113 		},
6114 		.chained = true,
6115 		.chain_id = ALC269_FIXUP_QUANTA_MUTE
6116 	},
6117 	[ALC269_FIXUP_AMIC] = {
6118 		.type = ALC_FIXUP_PINS,
6119 		.v.pins = (const struct alc_pincfg[]) {
6120 			{ 0x14, 0x99130110 }, /* speaker */
6121 			{ 0x15, 0x0121401f }, /* HP out */
6122 			{ 0x18, 0x01a19c20 }, /* mic */
6123 			{ 0x19, 0x99a3092f }, /* int-mic */
6124 			{ }
6125 		},
6126 	},
6127 	[ALC269_FIXUP_DMIC] = {
6128 		.type = ALC_FIXUP_PINS,
6129 		.v.pins = (const struct alc_pincfg[]) {
6130 			{ 0x12, 0x99a3092f }, /* int-mic */
6131 			{ 0x14, 0x99130110 }, /* speaker */
6132 			{ 0x15, 0x0121401f }, /* HP out */
6133 			{ 0x18, 0x01a19c20 }, /* mic */
6134 			{ }
6135 		},
6136 	},
6137 	[ALC269VB_FIXUP_AMIC] = {
6138 		.type = ALC_FIXUP_PINS,
6139 		.v.pins = (const struct alc_pincfg[]) {
6140 			{ 0x14, 0x99130110 }, /* speaker */
6141 			{ 0x18, 0x01a19c20 }, /* mic */
6142 			{ 0x19, 0x99a3092f }, /* int-mic */
6143 			{ 0x21, 0x0121401f }, /* HP out */
6144 			{ }
6145 		},
6146 	},
6147 	[ALC269VB_FIXUP_DMIC] = {
6148 		.type = ALC_FIXUP_PINS,
6149 		.v.pins = (const struct alc_pincfg[]) {
6150 			{ 0x12, 0x99a3092f }, /* int-mic */
6151 			{ 0x14, 0x99130110 }, /* speaker */
6152 			{ 0x18, 0x01a19c20 }, /* mic */
6153 			{ 0x21, 0x0121401f }, /* HP out */
6154 			{ }
6155 		},
6156 	},
6157 	[ALC269_FIXUP_MIC2_MUTE_LED] = {
6158 		.type = ALC_FIXUP_FUNC,
6159 		.v.func = alc269_fixup_mic2_mute,
6160 	},
6161 	[ALC269_FIXUP_INV_DMIC] = {
6162 		.type = ALC_FIXUP_FUNC,
6163 		.v.func = alc_fixup_inv_dmic_0x12,
6164 	},
6165 	[ALC269_FIXUP_LENOVO_DOCK] = {
6166 		.type = ALC_FIXUP_PINS,
6167 		.v.pins = (const struct alc_pincfg[]) {
6168 			{ 0x19, 0x23a11040 }, /* dock mic */
6169 			{ 0x1b, 0x2121103f }, /* dock headphone */
6170 			{ }
6171 		},
6172 		.chained = true,
6173 		.chain_id = ALC269_FIXUP_PINCFG_NO_HP_TO_LINEOUT
6174 	},
6175 	[ALC269_FIXUP_PINCFG_NO_HP_TO_LINEOUT] = {
6176 		.type = ALC_FIXUP_FUNC,
6177 		.v.func = alc269_fixup_pincfg_no_hp_to_lineout,
6178 	},
6179 };
6180 
6181 static const struct snd_pci_quirk alc269_fixup_tbl[] = {
6182 	SND_PCI_QUIRK(0x1025, 0x029b, "Acer 1810TZ", ALC269_FIXUP_INV_DMIC),
6183 	SND_PCI_QUIRK(0x1025, 0x0349, "Acer AOD260", ALC269_FIXUP_INV_DMIC),
6184 	SND_PCI_QUIRK(0x103c, 0x1586, "HP", ALC269_FIXUP_MIC2_MUTE_LED),
6185 	SND_PCI_QUIRK(0x1043, 0x1427, "Asus Zenbook UX31E", ALC269VB_FIXUP_DMIC),
6186 	SND_PCI_QUIRK(0x1043, 0x1517, "Asus Zenbook UX31A", ALC269VB_FIXUP_DMIC),
6187 	SND_PCI_QUIRK(0x1043, 0x1a13, "Asus G73Jw", ALC269_FIXUP_ASUS_G73JW),
6188 	SND_PCI_QUIRK(0x1043, 0x1b13, "Asus U41SV", ALC269_FIXUP_INV_DMIC),
6189 	SND_PCI_QUIRK(0x1043, 0x16e3, "ASUS UX50", ALC269_FIXUP_STEREO_DMIC),
6190 	SND_PCI_QUIRK(0x1043, 0x831a, "ASUS P901", ALC269_FIXUP_STEREO_DMIC),
6191 	SND_PCI_QUIRK(0x1043, 0x834a, "ASUS S101", ALC269_FIXUP_STEREO_DMIC),
6192 	SND_PCI_QUIRK(0x1043, 0x8398, "ASUS P1005", ALC269_FIXUP_STEREO_DMIC),
6193 	SND_PCI_QUIRK(0x1043, 0x83ce, "ASUS P1005", ALC269_FIXUP_STEREO_DMIC),
6194 	SND_PCI_QUIRK(0x104d, 0x9073, "Sony VAIO", ALC275_FIXUP_SONY_VAIO_GPIO2),
6195 	SND_PCI_QUIRK(0x104d, 0x907b, "Sony VAIO", ALC275_FIXUP_SONY_HWEQ),
6196 	SND_PCI_QUIRK(0x104d, 0x9084, "Sony VAIO", ALC275_FIXUP_SONY_HWEQ),
6197 	SND_PCI_QUIRK_VENDOR(0x104d, "Sony VAIO", ALC269_FIXUP_SONY_VAIO),
6198 	SND_PCI_QUIRK(0x1028, 0x0470, "Dell M101z", ALC269_FIXUP_DELL_M101Z),
6199 	SND_PCI_QUIRK_VENDOR(0x1025, "Acer Aspire", ALC271_FIXUP_DMIC),
6200 	SND_PCI_QUIRK(0x10cf, 0x1475, "Lifebook", ALC269_FIXUP_LIFEBOOK),
6201 	SND_PCI_QUIRK(0x17aa, 0x20f2, "Thinkpad SL410/510", ALC269_FIXUP_SKU_IGNORE),
6202 	SND_PCI_QUIRK(0x17aa, 0x215e, "Thinkpad L512", ALC269_FIXUP_SKU_IGNORE),
6203 	SND_PCI_QUIRK(0x17aa, 0x21b8, "Thinkpad Edge 14", ALC269_FIXUP_SKU_IGNORE),
6204 	SND_PCI_QUIRK(0x17aa, 0x21ca, "Thinkpad L412", ALC269_FIXUP_SKU_IGNORE),
6205 	SND_PCI_QUIRK(0x17aa, 0x21e9, "Thinkpad Edge 15", ALC269_FIXUP_SKU_IGNORE),
6206 	SND_PCI_QUIRK(0x17aa, 0x21f6, "Thinkpad T530", ALC269_FIXUP_LENOVO_DOCK),
6207 	SND_PCI_QUIRK(0x17aa, 0x21fa, "Thinkpad X230", ALC269_FIXUP_LENOVO_DOCK),
6208 	SND_PCI_QUIRK(0x17aa, 0x21f3, "Thinkpad T430", ALC269_FIXUP_LENOVO_DOCK),
6209 	SND_PCI_QUIRK(0x17aa, 0x21fb, "Thinkpad T430s", ALC269_FIXUP_LENOVO_DOCK),
6210 	SND_PCI_QUIRK(0x17aa, 0x2203, "Thinkpad X230 Tablet", ALC269_FIXUP_LENOVO_DOCK),
6211 	SND_PCI_QUIRK(0x17aa, 0x3bf8, "Quanta FL1", ALC269_FIXUP_PCM_44K),
6212 	SND_PCI_QUIRK(0x17aa, 0x9e54, "LENOVO NB", ALC269_FIXUP_LENOVO_EAPD),
6213 
6214 #if 0
6215 	/* Below is a quirk table taken from the old code.
6216 	 * Basically the device should work as is without the fixup table.
6217 	 * If BIOS doesn't give a proper info, enable the corresponding
6218 	 * fixup entry.
6219 	 */
6220 	SND_PCI_QUIRK(0x1043, 0x8330, "ASUS Eeepc P703 P900A",
6221 		      ALC269_FIXUP_AMIC),
6222 	SND_PCI_QUIRK(0x1043, 0x1013, "ASUS N61Da", ALC269_FIXUP_AMIC),
6223 	SND_PCI_QUIRK(0x1043, 0x1143, "ASUS B53f", ALC269_FIXUP_AMIC),
6224 	SND_PCI_QUIRK(0x1043, 0x1133, "ASUS UJ20ft", ALC269_FIXUP_AMIC),
6225 	SND_PCI_QUIRK(0x1043, 0x1183, "ASUS K72DR", ALC269_FIXUP_AMIC),
6226 	SND_PCI_QUIRK(0x1043, 0x11b3, "ASUS K52DR", ALC269_FIXUP_AMIC),
6227 	SND_PCI_QUIRK(0x1043, 0x11e3, "ASUS U33Jc", ALC269_FIXUP_AMIC),
6228 	SND_PCI_QUIRK(0x1043, 0x1273, "ASUS UL80Jt", ALC269_FIXUP_AMIC),
6229 	SND_PCI_QUIRK(0x1043, 0x1283, "ASUS U53Jc", ALC269_FIXUP_AMIC),
6230 	SND_PCI_QUIRK(0x1043, 0x12b3, "ASUS N82JV", ALC269_FIXUP_AMIC),
6231 	SND_PCI_QUIRK(0x1043, 0x12d3, "ASUS N61Jv", ALC269_FIXUP_AMIC),
6232 	SND_PCI_QUIRK(0x1043, 0x13a3, "ASUS UL30Vt", ALC269_FIXUP_AMIC),
6233 	SND_PCI_QUIRK(0x1043, 0x1373, "ASUS G73JX", ALC269_FIXUP_AMIC),
6234 	SND_PCI_QUIRK(0x1043, 0x1383, "ASUS UJ30Jc", ALC269_FIXUP_AMIC),
6235 	SND_PCI_QUIRK(0x1043, 0x13d3, "ASUS N61JA", ALC269_FIXUP_AMIC),
6236 	SND_PCI_QUIRK(0x1043, 0x1413, "ASUS UL50", ALC269_FIXUP_AMIC),
6237 	SND_PCI_QUIRK(0x1043, 0x1443, "ASUS UL30", ALC269_FIXUP_AMIC),
6238 	SND_PCI_QUIRK(0x1043, 0x1453, "ASUS M60Jv", ALC269_FIXUP_AMIC),
6239 	SND_PCI_QUIRK(0x1043, 0x1483, "ASUS UL80", ALC269_FIXUP_AMIC),
6240 	SND_PCI_QUIRK(0x1043, 0x14f3, "ASUS F83Vf", ALC269_FIXUP_AMIC),
6241 	SND_PCI_QUIRK(0x1043, 0x14e3, "ASUS UL20", ALC269_FIXUP_AMIC),
6242 	SND_PCI_QUIRK(0x1043, 0x1513, "ASUS UX30", ALC269_FIXUP_AMIC),
6243 	SND_PCI_QUIRK(0x1043, 0x1593, "ASUS N51Vn", ALC269_FIXUP_AMIC),
6244 	SND_PCI_QUIRK(0x1043, 0x15a3, "ASUS N60Jv", ALC269_FIXUP_AMIC),
6245 	SND_PCI_QUIRK(0x1043, 0x15b3, "ASUS N60Dp", ALC269_FIXUP_AMIC),
6246 	SND_PCI_QUIRK(0x1043, 0x15c3, "ASUS N70De", ALC269_FIXUP_AMIC),
6247 	SND_PCI_QUIRK(0x1043, 0x15e3, "ASUS F83T", ALC269_FIXUP_AMIC),
6248 	SND_PCI_QUIRK(0x1043, 0x1643, "ASUS M60J", ALC269_FIXUP_AMIC),
6249 	SND_PCI_QUIRK(0x1043, 0x1653, "ASUS U50", ALC269_FIXUP_AMIC),
6250 	SND_PCI_QUIRK(0x1043, 0x1693, "ASUS F50N", ALC269_FIXUP_AMIC),
6251 	SND_PCI_QUIRK(0x1043, 0x16a3, "ASUS F5Q", ALC269_FIXUP_AMIC),
6252 	SND_PCI_QUIRK(0x1043, 0x1723, "ASUS P80", ALC269_FIXUP_AMIC),
6253 	SND_PCI_QUIRK(0x1043, 0x1743, "ASUS U80", ALC269_FIXUP_AMIC),
6254 	SND_PCI_QUIRK(0x1043, 0x1773, "ASUS U20A", ALC269_FIXUP_AMIC),
6255 	SND_PCI_QUIRK(0x1043, 0x1883, "ASUS F81Se", ALC269_FIXUP_AMIC),
6256 	SND_PCI_QUIRK(0x152d, 0x1778, "Quanta ON1", ALC269_FIXUP_DMIC),
6257 	SND_PCI_QUIRK(0x17aa, 0x3be9, "Quanta Wistron", ALC269_FIXUP_AMIC),
6258 	SND_PCI_QUIRK(0x17aa, 0x3bf8, "Quanta FL1", ALC269_FIXUP_AMIC),
6259 	SND_PCI_QUIRK(0x17ff, 0x059a, "Quanta EL3", ALC269_FIXUP_DMIC),
6260 	SND_PCI_QUIRK(0x17ff, 0x059b, "Quanta JR1", ALC269_FIXUP_DMIC),
6261 #endif
6262 	{}
6263 };
6264 
6265 static const struct alc_model_fixup alc269_fixup_models[] = {
6266 	{.id = ALC269_FIXUP_AMIC, .name = "laptop-amic"},
6267 	{.id = ALC269_FIXUP_DMIC, .name = "laptop-dmic"},
6268 	{.id = ALC269_FIXUP_STEREO_DMIC, .name = "alc269-dmic"},
6269 	{.id = ALC271_FIXUP_DMIC, .name = "alc271-dmic"},
6270 	{.id = ALC269_FIXUP_INV_DMIC, .name = "inv-dmic"},
6271 	{.id = ALC269_FIXUP_LENOVO_DOCK, .name = "lenovo-dock"},
6272 	{}
6273 };
6274 
6275 
6276 static void alc269_fill_coef(struct hda_codec *codec)
6277 {
6278 	struct alc_spec *spec = codec->spec;
6279 	int val;
6280 
6281 	if (spec->codec_variant != ALC269_TYPE_ALC269VB)
6282 		return;
6283 
6284 	if ((alc_get_coef0(codec) & 0x00ff) < 0x015) {
6285 		alc_write_coef_idx(codec, 0xf, 0x960b);
6286 		alc_write_coef_idx(codec, 0xe, 0x8817);
6287 	}
6288 
6289 	if ((alc_get_coef0(codec) & 0x00ff) == 0x016) {
6290 		alc_write_coef_idx(codec, 0xf, 0x960b);
6291 		alc_write_coef_idx(codec, 0xe, 0x8814);
6292 	}
6293 
6294 	if ((alc_get_coef0(codec) & 0x00ff) == 0x017) {
6295 		val = alc_read_coef_idx(codec, 0x04);
6296 		/* Power up output pin */
6297 		alc_write_coef_idx(codec, 0x04, val | (1<<11));
6298 	}
6299 
6300 	if ((alc_get_coef0(codec) & 0x00ff) == 0x018) {
6301 		val = alc_read_coef_idx(codec, 0xd);
6302 		if ((val & 0x0c00) >> 10 != 0x1) {
6303 			/* Capless ramp up clock control */
6304 			alc_write_coef_idx(codec, 0xd, val | (1<<10));
6305 		}
6306 		val = alc_read_coef_idx(codec, 0x17);
6307 		if ((val & 0x01c0) >> 6 != 0x4) {
6308 			/* Class D power on reset */
6309 			alc_write_coef_idx(codec, 0x17, val | (1<<7));
6310 		}
6311 	}
6312 
6313 	val = alc_read_coef_idx(codec, 0xd); /* Class D */
6314 	alc_write_coef_idx(codec, 0xd, val | (1<<14));
6315 
6316 	val = alc_read_coef_idx(codec, 0x4); /* HP */
6317 	alc_write_coef_idx(codec, 0x4, val | (1<<11));
6318 }
6319 
6320 /*
6321  */
6322 static int patch_alc269(struct hda_codec *codec)
6323 {
6324 	struct alc_spec *spec;
6325 	int err;
6326 
6327 	err = alc_alloc_spec(codec, 0x0b);
6328 	if (err < 0)
6329 		return err;
6330 
6331 	spec = codec->spec;
6332 
6333 	alc_pick_fixup(codec, alc269_fixup_models,
6334 		       alc269_fixup_tbl, alc269_fixups);
6335 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
6336 
6337 	alc_auto_parse_customize_define(codec);
6338 
6339 	if (codec->vendor_id == 0x10ec0269) {
6340 		spec->codec_variant = ALC269_TYPE_ALC269VA;
6341 		switch (alc_get_coef0(codec) & 0x00f0) {
6342 		case 0x0010:
6343 			if (codec->bus->pci->subsystem_vendor == 0x1025 &&
6344 			    spec->cdefine.platform_type == 1)
6345 				err = alc_codec_rename(codec, "ALC271X");
6346 			spec->codec_variant = ALC269_TYPE_ALC269VB;
6347 			break;
6348 		case 0x0020:
6349 			if (codec->bus->pci->subsystem_vendor == 0x17aa &&
6350 			    codec->bus->pci->subsystem_device == 0x21f3)
6351 				err = alc_codec_rename(codec, "ALC3202");
6352 			spec->codec_variant = ALC269_TYPE_ALC269VC;
6353 			break;
6354 		case 0x0030:
6355 			spec->codec_variant = ALC269_TYPE_ALC269VD;
6356 			break;
6357 		default:
6358 			alc_fix_pll_init(codec, 0x20, 0x04, 15);
6359 		}
6360 		if (err < 0)
6361 			goto error;
6362 		spec->init_hook = alc269_fill_coef;
6363 		alc269_fill_coef(codec);
6364 	}
6365 
6366 	/* automatic parse from the BIOS config */
6367 	err = alc269_parse_auto_config(codec);
6368 	if (err < 0)
6369 		goto error;
6370 
6371 	if (!spec->no_analog && has_cdefine_beep(codec)) {
6372 		err = snd_hda_attach_beep_device(codec, 0x1);
6373 		if (err < 0)
6374 			goto error;
6375 		set_beep_amp(spec, 0x0b, 0x04, HDA_INPUT);
6376 	}
6377 
6378 	codec->patch_ops = alc_patch_ops;
6379 #ifdef CONFIG_PM
6380 	codec->patch_ops.resume = alc269_resume;
6381 #endif
6382 	spec->shutup = alc269_shutup;
6383 
6384 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
6385 
6386 	return 0;
6387 
6388  error:
6389 	alc_free(codec);
6390 	return err;
6391 }
6392 
6393 /*
6394  * ALC861
6395  */
6396 
6397 static int alc861_parse_auto_config(struct hda_codec *codec)
6398 {
6399 	static const hda_nid_t alc861_ignore[] = { 0x1d, 0 };
6400 	static const hda_nid_t alc861_ssids[] = { 0x0e, 0x0f, 0x0b, 0 };
6401 	return alc_parse_auto_config(codec, alc861_ignore, alc861_ssids);
6402 }
6403 
6404 /* Pin config fixes */
6405 enum {
6406 	ALC861_FIXUP_FSC_AMILO_PI1505,
6407 	ALC861_FIXUP_AMP_VREF_0F,
6408 	ALC861_FIXUP_NO_JACK_DETECT,
6409 	ALC861_FIXUP_ASUS_A6RP,
6410 };
6411 
6412 /* On some laptops, VREF of pin 0x0f is abused for controlling the main amp */
6413 static void alc861_fixup_asus_amp_vref_0f(struct hda_codec *codec,
6414 			const struct alc_fixup *fix, int action)
6415 {
6416 	struct alc_spec *spec = codec->spec;
6417 	unsigned int val;
6418 
6419 	if (action != ALC_FIXUP_ACT_INIT)
6420 		return;
6421 	val = snd_hda_codec_read(codec, 0x0f, 0,
6422 				 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
6423 	if (!(val & (AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN)))
6424 		val |= AC_PINCTL_IN_EN;
6425 	val |= AC_PINCTL_VREF_50;
6426 	snd_hda_set_pin_ctl(codec, 0x0f, val);
6427 	spec->keep_vref_in_automute = 1;
6428 }
6429 
6430 /* suppress the jack-detection */
6431 static void alc_fixup_no_jack_detect(struct hda_codec *codec,
6432 				     const struct alc_fixup *fix, int action)
6433 {
6434 	if (action == ALC_FIXUP_ACT_PRE_PROBE)
6435 		codec->no_jack_detect = 1;
6436 }
6437 
6438 static const struct alc_fixup alc861_fixups[] = {
6439 	[ALC861_FIXUP_FSC_AMILO_PI1505] = {
6440 		.type = ALC_FIXUP_PINS,
6441 		.v.pins = (const struct alc_pincfg[]) {
6442 			{ 0x0b, 0x0221101f }, /* HP */
6443 			{ 0x0f, 0x90170310 }, /* speaker */
6444 			{ }
6445 		}
6446 	},
6447 	[ALC861_FIXUP_AMP_VREF_0F] = {
6448 		.type = ALC_FIXUP_FUNC,
6449 		.v.func = alc861_fixup_asus_amp_vref_0f,
6450 	},
6451 	[ALC861_FIXUP_NO_JACK_DETECT] = {
6452 		.type = ALC_FIXUP_FUNC,
6453 		.v.func = alc_fixup_no_jack_detect,
6454 	},
6455 	[ALC861_FIXUP_ASUS_A6RP] = {
6456 		.type = ALC_FIXUP_FUNC,
6457 		.v.func = alc861_fixup_asus_amp_vref_0f,
6458 		.chained = true,
6459 		.chain_id = ALC861_FIXUP_NO_JACK_DETECT,
6460 	}
6461 };
6462 
6463 static const struct snd_pci_quirk alc861_fixup_tbl[] = {
6464 	SND_PCI_QUIRK(0x1043, 0x1393, "ASUS A6Rp", ALC861_FIXUP_ASUS_A6RP),
6465 	SND_PCI_QUIRK_VENDOR(0x1043, "ASUS laptop", ALC861_FIXUP_AMP_VREF_0F),
6466 	SND_PCI_QUIRK(0x1462, 0x7254, "HP DX2200", ALC861_FIXUP_NO_JACK_DETECT),
6467 	SND_PCI_QUIRK(0x1584, 0x2b01, "Haier W18", ALC861_FIXUP_AMP_VREF_0F),
6468 	SND_PCI_QUIRK(0x1584, 0x0000, "Uniwill ECS M31EI", ALC861_FIXUP_AMP_VREF_0F),
6469 	SND_PCI_QUIRK(0x1734, 0x10c7, "FSC Amilo Pi1505", ALC861_FIXUP_FSC_AMILO_PI1505),
6470 	{}
6471 };
6472 
6473 /*
6474  */
6475 static int patch_alc861(struct hda_codec *codec)
6476 {
6477 	struct alc_spec *spec;
6478 	int err;
6479 
6480 	err = alc_alloc_spec(codec, 0x15);
6481 	if (err < 0)
6482 		return err;
6483 
6484 	spec = codec->spec;
6485 
6486 	alc_pick_fixup(codec, NULL, alc861_fixup_tbl, alc861_fixups);
6487 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
6488 
6489 	/* automatic parse from the BIOS config */
6490 	err = alc861_parse_auto_config(codec);
6491 	if (err < 0)
6492 		goto error;
6493 
6494 	if (!spec->no_analog) {
6495 		err = snd_hda_attach_beep_device(codec, 0x23);
6496 		if (err < 0)
6497 			goto error;
6498 		set_beep_amp(spec, 0x23, 0, HDA_OUTPUT);
6499 	}
6500 
6501 	codec->patch_ops = alc_patch_ops;
6502 #ifdef CONFIG_PM
6503 	spec->power_hook = alc_power_eapd;
6504 #endif
6505 
6506 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
6507 
6508 	return 0;
6509 
6510  error:
6511 	alc_free(codec);
6512 	return err;
6513 }
6514 
6515 /*
6516  * ALC861-VD support
6517  *
6518  * Based on ALC882
6519  *
6520  * In addition, an independent DAC
6521  */
6522 static int alc861vd_parse_auto_config(struct hda_codec *codec)
6523 {
6524 	static const hda_nid_t alc861vd_ignore[] = { 0x1d, 0 };
6525 	static const hda_nid_t alc861vd_ssids[] = { 0x15, 0x1b, 0x14, 0 };
6526 	return alc_parse_auto_config(codec, alc861vd_ignore, alc861vd_ssids);
6527 }
6528 
6529 enum {
6530 	ALC660VD_FIX_ASUS_GPIO1,
6531 	ALC861VD_FIX_DALLAS,
6532 };
6533 
6534 /* exclude VREF80 */
6535 static void alc861vd_fixup_dallas(struct hda_codec *codec,
6536 				  const struct alc_fixup *fix, int action)
6537 {
6538 	if (action == ALC_FIXUP_ACT_PRE_PROBE) {
6539 		snd_hda_override_pin_caps(codec, 0x18, 0x00001714);
6540 		snd_hda_override_pin_caps(codec, 0x19, 0x0000171c);
6541 	}
6542 }
6543 
6544 static const struct alc_fixup alc861vd_fixups[] = {
6545 	[ALC660VD_FIX_ASUS_GPIO1] = {
6546 		.type = ALC_FIXUP_VERBS,
6547 		.v.verbs = (const struct hda_verb[]) {
6548 			/* reset GPIO1 */
6549 			{0x01, AC_VERB_SET_GPIO_MASK, 0x03},
6550 			{0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
6551 			{0x01, AC_VERB_SET_GPIO_DATA, 0x01},
6552 			{ }
6553 		}
6554 	},
6555 	[ALC861VD_FIX_DALLAS] = {
6556 		.type = ALC_FIXUP_FUNC,
6557 		.v.func = alc861vd_fixup_dallas,
6558 	},
6559 };
6560 
6561 static const struct snd_pci_quirk alc861vd_fixup_tbl[] = {
6562 	SND_PCI_QUIRK(0x103c, 0x30bf, "HP TX1000", ALC861VD_FIX_DALLAS),
6563 	SND_PCI_QUIRK(0x1043, 0x1339, "ASUS A7-K", ALC660VD_FIX_ASUS_GPIO1),
6564 	SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba L30-149", ALC861VD_FIX_DALLAS),
6565 	{}
6566 };
6567 
6568 /*
6569  */
6570 static int patch_alc861vd(struct hda_codec *codec)
6571 {
6572 	struct alc_spec *spec;
6573 	int err;
6574 
6575 	err = alc_alloc_spec(codec, 0x0b);
6576 	if (err < 0)
6577 		return err;
6578 
6579 	spec = codec->spec;
6580 
6581 	alc_pick_fixup(codec, NULL, alc861vd_fixup_tbl, alc861vd_fixups);
6582 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
6583 
6584 	/* automatic parse from the BIOS config */
6585 	err = alc861vd_parse_auto_config(codec);
6586 	if (err < 0)
6587 		goto error;
6588 
6589 	if (!spec->no_analog) {
6590 		err = snd_hda_attach_beep_device(codec, 0x23);
6591 		if (err < 0)
6592 			goto error;
6593 		set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
6594 	}
6595 
6596 	codec->patch_ops = alc_patch_ops;
6597 
6598 	spec->shutup = alc_eapd_shutup;
6599 
6600 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
6601 
6602 	return 0;
6603 
6604  error:
6605 	alc_free(codec);
6606 	return err;
6607 }
6608 
6609 /*
6610  * ALC662 support
6611  *
6612  * ALC662 is almost identical with ALC880 but has cleaner and more flexible
6613  * configuration.  Each pin widget can choose any input DACs and a mixer.
6614  * Each ADC is connected from a mixer of all inputs.  This makes possible
6615  * 6-channel independent captures.
6616  *
6617  * In addition, an independent DAC for the multi-playback (not used in this
6618  * driver yet).
6619  */
6620 
6621 /*
6622  * BIOS auto configuration
6623  */
6624 
6625 static int alc662_parse_auto_config(struct hda_codec *codec)
6626 {
6627 	static const hda_nid_t alc662_ignore[] = { 0x1d, 0 };
6628 	static const hda_nid_t alc663_ssids[] = { 0x15, 0x1b, 0x14, 0x21 };
6629 	static const hda_nid_t alc662_ssids[] = { 0x15, 0x1b, 0x14, 0 };
6630 	const hda_nid_t *ssids;
6631 
6632 	if (codec->vendor_id == 0x10ec0272 || codec->vendor_id == 0x10ec0663 ||
6633 	    codec->vendor_id == 0x10ec0665 || codec->vendor_id == 0x10ec0670)
6634 		ssids = alc663_ssids;
6635 	else
6636 		ssids = alc662_ssids;
6637 	return alc_parse_auto_config(codec, alc662_ignore, ssids);
6638 }
6639 
6640 static void alc272_fixup_mario(struct hda_codec *codec,
6641 			       const struct alc_fixup *fix, int action)
6642 {
6643 	if (action != ALC_FIXUP_ACT_PROBE)
6644 		return;
6645 	if (snd_hda_override_amp_caps(codec, 0x2, HDA_OUTPUT,
6646 				      (0x3b << AC_AMPCAP_OFFSET_SHIFT) |
6647 				      (0x3b << AC_AMPCAP_NUM_STEPS_SHIFT) |
6648 				      (0x03 << AC_AMPCAP_STEP_SIZE_SHIFT) |
6649 				      (0 << AC_AMPCAP_MUTE_SHIFT)))
6650 		printk(KERN_WARNING
6651 		       "hda_codec: failed to override amp caps for NID 0x2\n");
6652 }
6653 
6654 enum {
6655 	ALC662_FIXUP_ASPIRE,
6656 	ALC662_FIXUP_IDEAPAD,
6657 	ALC272_FIXUP_MARIO,
6658 	ALC662_FIXUP_CZC_P10T,
6659 	ALC662_FIXUP_SKU_IGNORE,
6660 	ALC662_FIXUP_HP_RP5800,
6661 	ALC662_FIXUP_ASUS_MODE1,
6662 	ALC662_FIXUP_ASUS_MODE2,
6663 	ALC662_FIXUP_ASUS_MODE3,
6664 	ALC662_FIXUP_ASUS_MODE4,
6665 	ALC662_FIXUP_ASUS_MODE5,
6666 	ALC662_FIXUP_ASUS_MODE6,
6667 	ALC662_FIXUP_ASUS_MODE7,
6668 	ALC662_FIXUP_ASUS_MODE8,
6669 	ALC662_FIXUP_NO_JACK_DETECT,
6670 	ALC662_FIXUP_ZOTAC_Z68,
6671 	ALC662_FIXUP_INV_DMIC,
6672 };
6673 
6674 static const struct alc_fixup alc662_fixups[] = {
6675 	[ALC662_FIXUP_ASPIRE] = {
6676 		.type = ALC_FIXUP_PINS,
6677 		.v.pins = (const struct alc_pincfg[]) {
6678 			{ 0x15, 0x99130112 }, /* subwoofer */
6679 			{ }
6680 		}
6681 	},
6682 	[ALC662_FIXUP_IDEAPAD] = {
6683 		.type = ALC_FIXUP_PINS,
6684 		.v.pins = (const struct alc_pincfg[]) {
6685 			{ 0x17, 0x99130112 }, /* subwoofer */
6686 			{ }
6687 		}
6688 	},
6689 	[ALC272_FIXUP_MARIO] = {
6690 		.type = ALC_FIXUP_FUNC,
6691 		.v.func = alc272_fixup_mario,
6692 	},
6693 	[ALC662_FIXUP_CZC_P10T] = {
6694 		.type = ALC_FIXUP_VERBS,
6695 		.v.verbs = (const struct hda_verb[]) {
6696 			{0x14, AC_VERB_SET_EAPD_BTLENABLE, 0},
6697 			{}
6698 		}
6699 	},
6700 	[ALC662_FIXUP_SKU_IGNORE] = {
6701 		.type = ALC_FIXUP_FUNC,
6702 		.v.func = alc_fixup_sku_ignore,
6703 	},
6704 	[ALC662_FIXUP_HP_RP5800] = {
6705 		.type = ALC_FIXUP_PINS,
6706 		.v.pins = (const struct alc_pincfg[]) {
6707 			{ 0x14, 0x0221201f }, /* HP out */
6708 			{ }
6709 		},
6710 		.chained = true,
6711 		.chain_id = ALC662_FIXUP_SKU_IGNORE
6712 	},
6713 	[ALC662_FIXUP_ASUS_MODE1] = {
6714 		.type = ALC_FIXUP_PINS,
6715 		.v.pins = (const struct alc_pincfg[]) {
6716 			{ 0x14, 0x99130110 }, /* speaker */
6717 			{ 0x18, 0x01a19c20 }, /* mic */
6718 			{ 0x19, 0x99a3092f }, /* int-mic */
6719 			{ 0x21, 0x0121401f }, /* HP out */
6720 			{ }
6721 		},
6722 		.chained = true,
6723 		.chain_id = ALC662_FIXUP_SKU_IGNORE
6724 	},
6725 	[ALC662_FIXUP_ASUS_MODE2] = {
6726 		.type = ALC_FIXUP_PINS,
6727 		.v.pins = (const struct alc_pincfg[]) {
6728 			{ 0x14, 0x99130110 }, /* speaker */
6729 			{ 0x18, 0x01a19820 }, /* mic */
6730 			{ 0x19, 0x99a3092f }, /* int-mic */
6731 			{ 0x1b, 0x0121401f }, /* HP out */
6732 			{ }
6733 		},
6734 		.chained = true,
6735 		.chain_id = ALC662_FIXUP_SKU_IGNORE
6736 	},
6737 	[ALC662_FIXUP_ASUS_MODE3] = {
6738 		.type = ALC_FIXUP_PINS,
6739 		.v.pins = (const struct alc_pincfg[]) {
6740 			{ 0x14, 0x99130110 }, /* speaker */
6741 			{ 0x15, 0x0121441f }, /* HP */
6742 			{ 0x18, 0x01a19840 }, /* mic */
6743 			{ 0x19, 0x99a3094f }, /* int-mic */
6744 			{ 0x21, 0x01211420 }, /* HP2 */
6745 			{ }
6746 		},
6747 		.chained = true,
6748 		.chain_id = ALC662_FIXUP_SKU_IGNORE
6749 	},
6750 	[ALC662_FIXUP_ASUS_MODE4] = {
6751 		.type = ALC_FIXUP_PINS,
6752 		.v.pins = (const struct alc_pincfg[]) {
6753 			{ 0x14, 0x99130110 }, /* speaker */
6754 			{ 0x16, 0x99130111 }, /* speaker */
6755 			{ 0x18, 0x01a19840 }, /* mic */
6756 			{ 0x19, 0x99a3094f }, /* int-mic */
6757 			{ 0x21, 0x0121441f }, /* HP */
6758 			{ }
6759 		},
6760 		.chained = true,
6761 		.chain_id = ALC662_FIXUP_SKU_IGNORE
6762 	},
6763 	[ALC662_FIXUP_ASUS_MODE5] = {
6764 		.type = ALC_FIXUP_PINS,
6765 		.v.pins = (const struct alc_pincfg[]) {
6766 			{ 0x14, 0x99130110 }, /* speaker */
6767 			{ 0x15, 0x0121441f }, /* HP */
6768 			{ 0x16, 0x99130111 }, /* speaker */
6769 			{ 0x18, 0x01a19840 }, /* mic */
6770 			{ 0x19, 0x99a3094f }, /* int-mic */
6771 			{ }
6772 		},
6773 		.chained = true,
6774 		.chain_id = ALC662_FIXUP_SKU_IGNORE
6775 	},
6776 	[ALC662_FIXUP_ASUS_MODE6] = {
6777 		.type = ALC_FIXUP_PINS,
6778 		.v.pins = (const struct alc_pincfg[]) {
6779 			{ 0x14, 0x99130110 }, /* speaker */
6780 			{ 0x15, 0x01211420 }, /* HP2 */
6781 			{ 0x18, 0x01a19840 }, /* mic */
6782 			{ 0x19, 0x99a3094f }, /* int-mic */
6783 			{ 0x1b, 0x0121441f }, /* HP */
6784 			{ }
6785 		},
6786 		.chained = true,
6787 		.chain_id = ALC662_FIXUP_SKU_IGNORE
6788 	},
6789 	[ALC662_FIXUP_ASUS_MODE7] = {
6790 		.type = ALC_FIXUP_PINS,
6791 		.v.pins = (const struct alc_pincfg[]) {
6792 			{ 0x14, 0x99130110 }, /* speaker */
6793 			{ 0x17, 0x99130111 }, /* speaker */
6794 			{ 0x18, 0x01a19840 }, /* mic */
6795 			{ 0x19, 0x99a3094f }, /* int-mic */
6796 			{ 0x1b, 0x01214020 }, /* HP */
6797 			{ 0x21, 0x0121401f }, /* HP */
6798 			{ }
6799 		},
6800 		.chained = true,
6801 		.chain_id = ALC662_FIXUP_SKU_IGNORE
6802 	},
6803 	[ALC662_FIXUP_ASUS_MODE8] = {
6804 		.type = ALC_FIXUP_PINS,
6805 		.v.pins = (const struct alc_pincfg[]) {
6806 			{ 0x14, 0x99130110 }, /* speaker */
6807 			{ 0x12, 0x99a30970 }, /* int-mic */
6808 			{ 0x15, 0x01214020 }, /* HP */
6809 			{ 0x17, 0x99130111 }, /* speaker */
6810 			{ 0x18, 0x01a19840 }, /* mic */
6811 			{ 0x21, 0x0121401f }, /* HP */
6812 			{ }
6813 		},
6814 		.chained = true,
6815 		.chain_id = ALC662_FIXUP_SKU_IGNORE
6816 	},
6817 	[ALC662_FIXUP_NO_JACK_DETECT] = {
6818 		.type = ALC_FIXUP_FUNC,
6819 		.v.func = alc_fixup_no_jack_detect,
6820 	},
6821 	[ALC662_FIXUP_ZOTAC_Z68] = {
6822 		.type = ALC_FIXUP_PINS,
6823 		.v.pins = (const struct alc_pincfg[]) {
6824 			{ 0x1b, 0x02214020 }, /* Front HP */
6825 			{ }
6826 		}
6827 	},
6828 	[ALC662_FIXUP_INV_DMIC] = {
6829 		.type = ALC_FIXUP_FUNC,
6830 		.v.func = alc_fixup_inv_dmic_0x12,
6831 	},
6832 };
6833 
6834 static const struct snd_pci_quirk alc662_fixup_tbl[] = {
6835 	SND_PCI_QUIRK(0x1019, 0x9087, "ECS", ALC662_FIXUP_ASUS_MODE2),
6836 	SND_PCI_QUIRK(0x1025, 0x0308, "Acer Aspire 8942G", ALC662_FIXUP_ASPIRE),
6837 	SND_PCI_QUIRK(0x1025, 0x031c, "Gateway NV79", ALC662_FIXUP_SKU_IGNORE),
6838 	SND_PCI_QUIRK(0x1025, 0x0349, "eMachines eM250", ALC662_FIXUP_INV_DMIC),
6839 	SND_PCI_QUIRK(0x1025, 0x038b, "Acer Aspire 8943G", ALC662_FIXUP_ASPIRE),
6840 	SND_PCI_QUIRK(0x103c, 0x1632, "HP RP5800", ALC662_FIXUP_HP_RP5800),
6841 	SND_PCI_QUIRK(0x1043, 0x8469, "ASUS mobo", ALC662_FIXUP_NO_JACK_DETECT),
6842 	SND_PCI_QUIRK(0x105b, 0x0cd6, "Foxconn", ALC662_FIXUP_ASUS_MODE2),
6843 	SND_PCI_QUIRK(0x144d, 0xc051, "Samsung R720", ALC662_FIXUP_IDEAPAD),
6844 	SND_PCI_QUIRK(0x17aa, 0x38af, "Lenovo Ideapad Y550P", ALC662_FIXUP_IDEAPAD),
6845 	SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo Ideapad Y550", ALC662_FIXUP_IDEAPAD),
6846 	SND_PCI_QUIRK(0x19da, 0xa130, "Zotac Z68", ALC662_FIXUP_ZOTAC_Z68),
6847 	SND_PCI_QUIRK(0x1b35, 0x2206, "CZC P10T", ALC662_FIXUP_CZC_P10T),
6848 
6849 #if 0
6850 	/* Below is a quirk table taken from the old code.
6851 	 * Basically the device should work as is without the fixup table.
6852 	 * If BIOS doesn't give a proper info, enable the corresponding
6853 	 * fixup entry.
6854 	 */
6855 	SND_PCI_QUIRK(0x1043, 0x1000, "ASUS N50Vm", ALC662_FIXUP_ASUS_MODE1),
6856 	SND_PCI_QUIRK(0x1043, 0x1092, "ASUS NB", ALC662_FIXUP_ASUS_MODE3),
6857 	SND_PCI_QUIRK(0x1043, 0x1173, "ASUS K73Jn", ALC662_FIXUP_ASUS_MODE1),
6858 	SND_PCI_QUIRK(0x1043, 0x11c3, "ASUS M70V", ALC662_FIXUP_ASUS_MODE3),
6859 	SND_PCI_QUIRK(0x1043, 0x11d3, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
6860 	SND_PCI_QUIRK(0x1043, 0x11f3, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6861 	SND_PCI_QUIRK(0x1043, 0x1203, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
6862 	SND_PCI_QUIRK(0x1043, 0x1303, "ASUS G60J", ALC662_FIXUP_ASUS_MODE1),
6863 	SND_PCI_QUIRK(0x1043, 0x1333, "ASUS G60Jx", ALC662_FIXUP_ASUS_MODE1),
6864 	SND_PCI_QUIRK(0x1043, 0x1339, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6865 	SND_PCI_QUIRK(0x1043, 0x13e3, "ASUS N71JA", ALC662_FIXUP_ASUS_MODE7),
6866 	SND_PCI_QUIRK(0x1043, 0x1463, "ASUS N71", ALC662_FIXUP_ASUS_MODE7),
6867 	SND_PCI_QUIRK(0x1043, 0x14d3, "ASUS G72", ALC662_FIXUP_ASUS_MODE8),
6868 	SND_PCI_QUIRK(0x1043, 0x1563, "ASUS N90", ALC662_FIXUP_ASUS_MODE3),
6869 	SND_PCI_QUIRK(0x1043, 0x15d3, "ASUS N50SF F50SF", ALC662_FIXUP_ASUS_MODE1),
6870 	SND_PCI_QUIRK(0x1043, 0x16c3, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6871 	SND_PCI_QUIRK(0x1043, 0x16f3, "ASUS K40C K50C", ALC662_FIXUP_ASUS_MODE2),
6872 	SND_PCI_QUIRK(0x1043, 0x1733, "ASUS N81De", ALC662_FIXUP_ASUS_MODE1),
6873 	SND_PCI_QUIRK(0x1043, 0x1753, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6874 	SND_PCI_QUIRK(0x1043, 0x1763, "ASUS NB", ALC662_FIXUP_ASUS_MODE6),
6875 	SND_PCI_QUIRK(0x1043, 0x1765, "ASUS NB", ALC662_FIXUP_ASUS_MODE6),
6876 	SND_PCI_QUIRK(0x1043, 0x1783, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6877 	SND_PCI_QUIRK(0x1043, 0x1793, "ASUS F50GX", ALC662_FIXUP_ASUS_MODE1),
6878 	SND_PCI_QUIRK(0x1043, 0x17b3, "ASUS F70SL", ALC662_FIXUP_ASUS_MODE3),
6879 	SND_PCI_QUIRK(0x1043, 0x17f3, "ASUS X58LE", ALC662_FIXUP_ASUS_MODE2),
6880 	SND_PCI_QUIRK(0x1043, 0x1813, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6881 	SND_PCI_QUIRK(0x1043, 0x1823, "ASUS NB", ALC662_FIXUP_ASUS_MODE5),
6882 	SND_PCI_QUIRK(0x1043, 0x1833, "ASUS NB", ALC662_FIXUP_ASUS_MODE6),
6883 	SND_PCI_QUIRK(0x1043, 0x1843, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6884 	SND_PCI_QUIRK(0x1043, 0x1853, "ASUS F50Z", ALC662_FIXUP_ASUS_MODE1),
6885 	SND_PCI_QUIRK(0x1043, 0x1864, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6886 	SND_PCI_QUIRK(0x1043, 0x1876, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6887 	SND_PCI_QUIRK(0x1043, 0x1893, "ASUS M50Vm", ALC662_FIXUP_ASUS_MODE3),
6888 	SND_PCI_QUIRK(0x1043, 0x1894, "ASUS X55", ALC662_FIXUP_ASUS_MODE3),
6889 	SND_PCI_QUIRK(0x1043, 0x18b3, "ASUS N80Vc", ALC662_FIXUP_ASUS_MODE1),
6890 	SND_PCI_QUIRK(0x1043, 0x18c3, "ASUS VX5", ALC662_FIXUP_ASUS_MODE1),
6891 	SND_PCI_QUIRK(0x1043, 0x18d3, "ASUS N81Te", ALC662_FIXUP_ASUS_MODE1),
6892 	SND_PCI_QUIRK(0x1043, 0x18f3, "ASUS N505Tp", ALC662_FIXUP_ASUS_MODE1),
6893 	SND_PCI_QUIRK(0x1043, 0x1903, "ASUS F5GL", ALC662_FIXUP_ASUS_MODE1),
6894 	SND_PCI_QUIRK(0x1043, 0x1913, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6895 	SND_PCI_QUIRK(0x1043, 0x1933, "ASUS F80Q", ALC662_FIXUP_ASUS_MODE2),
6896 	SND_PCI_QUIRK(0x1043, 0x1943, "ASUS Vx3V", ALC662_FIXUP_ASUS_MODE1),
6897 	SND_PCI_QUIRK(0x1043, 0x1953, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
6898 	SND_PCI_QUIRK(0x1043, 0x1963, "ASUS X71C", ALC662_FIXUP_ASUS_MODE3),
6899 	SND_PCI_QUIRK(0x1043, 0x1983, "ASUS N5051A", ALC662_FIXUP_ASUS_MODE1),
6900 	SND_PCI_QUIRK(0x1043, 0x1993, "ASUS N20", ALC662_FIXUP_ASUS_MODE1),
6901 	SND_PCI_QUIRK(0x1043, 0x19b3, "ASUS F7Z", ALC662_FIXUP_ASUS_MODE1),
6902 	SND_PCI_QUIRK(0x1043, 0x19c3, "ASUS F5Z/F6x", ALC662_FIXUP_ASUS_MODE2),
6903 	SND_PCI_QUIRK(0x1043, 0x19e3, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
6904 	SND_PCI_QUIRK(0x1043, 0x19f3, "ASUS NB", ALC662_FIXUP_ASUS_MODE4),
6905 #endif
6906 	{}
6907 };
6908 
6909 static const struct alc_model_fixup alc662_fixup_models[] = {
6910 	{.id = ALC272_FIXUP_MARIO, .name = "mario"},
6911 	{.id = ALC662_FIXUP_ASUS_MODE1, .name = "asus-mode1"},
6912 	{.id = ALC662_FIXUP_ASUS_MODE2, .name = "asus-mode2"},
6913 	{.id = ALC662_FIXUP_ASUS_MODE3, .name = "asus-mode3"},
6914 	{.id = ALC662_FIXUP_ASUS_MODE4, .name = "asus-mode4"},
6915 	{.id = ALC662_FIXUP_ASUS_MODE5, .name = "asus-mode5"},
6916 	{.id = ALC662_FIXUP_ASUS_MODE6, .name = "asus-mode6"},
6917 	{.id = ALC662_FIXUP_ASUS_MODE7, .name = "asus-mode7"},
6918 	{.id = ALC662_FIXUP_ASUS_MODE8, .name = "asus-mode8"},
6919 	{.id = ALC662_FIXUP_INV_DMIC, .name = "inv-dmic"},
6920 	{}
6921 };
6922 
6923 static void alc662_fill_coef(struct hda_codec *codec)
6924 {
6925 	int val, coef;
6926 
6927 	coef = alc_get_coef0(codec);
6928 
6929 	switch (codec->vendor_id) {
6930 	case 0x10ec0662:
6931 		if ((coef & 0x00f0) == 0x0030) {
6932 			val = alc_read_coef_idx(codec, 0x4); /* EAPD Ctrl */
6933 			alc_write_coef_idx(codec, 0x4, val & ~(1<<10));
6934 		}
6935 		break;
6936 	case 0x10ec0272:
6937 	case 0x10ec0273:
6938 	case 0x10ec0663:
6939 	case 0x10ec0665:
6940 	case 0x10ec0670:
6941 	case 0x10ec0671:
6942 	case 0x10ec0672:
6943 		val = alc_read_coef_idx(codec, 0xd); /* EAPD Ctrl */
6944 		alc_write_coef_idx(codec, 0xd, val | (1<<14));
6945 		break;
6946 	}
6947 }
6948 
6949 /*
6950  */
6951 static int patch_alc662(struct hda_codec *codec)
6952 {
6953 	struct alc_spec *spec;
6954 	int err;
6955 
6956 	err = alc_alloc_spec(codec, 0x0b);
6957 	if (err < 0)
6958 		return err;
6959 
6960 	spec = codec->spec;
6961 
6962 	/* handle multiple HPs as is */
6963 	spec->parse_flags = HDA_PINCFG_NO_HP_FIXUP;
6964 
6965 	alc_fix_pll_init(codec, 0x20, 0x04, 15);
6966 
6967 	spec->init_hook = alc662_fill_coef;
6968 	alc662_fill_coef(codec);
6969 
6970 	alc_pick_fixup(codec, alc662_fixup_models,
6971 		       alc662_fixup_tbl, alc662_fixups);
6972 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
6973 
6974 	alc_auto_parse_customize_define(codec);
6975 
6976 	if ((alc_get_coef0(codec) & (1 << 14)) &&
6977 	    codec->bus->pci->subsystem_vendor == 0x1025 &&
6978 	    spec->cdefine.platform_type == 1) {
6979 		if (alc_codec_rename(codec, "ALC272X") < 0)
6980 			goto error;
6981 	}
6982 
6983 	/* automatic parse from the BIOS config */
6984 	err = alc662_parse_auto_config(codec);
6985 	if (err < 0)
6986 		goto error;
6987 
6988 	if (!spec->no_analog && has_cdefine_beep(codec)) {
6989 		err = snd_hda_attach_beep_device(codec, 0x1);
6990 		if (err < 0)
6991 			goto error;
6992 		switch (codec->vendor_id) {
6993 		case 0x10ec0662:
6994 			set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
6995 			break;
6996 		case 0x10ec0272:
6997 		case 0x10ec0663:
6998 		case 0x10ec0665:
6999 			set_beep_amp(spec, 0x0b, 0x04, HDA_INPUT);
7000 			break;
7001 		case 0x10ec0273:
7002 			set_beep_amp(spec, 0x0b, 0x03, HDA_INPUT);
7003 			break;
7004 		}
7005 	}
7006 
7007 	codec->patch_ops = alc_patch_ops;
7008 	spec->shutup = alc_eapd_shutup;
7009 
7010 	alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
7011 
7012 	return 0;
7013 
7014  error:
7015 	alc_free(codec);
7016 	return err;
7017 }
7018 
7019 /*
7020  * ALC680 support
7021  */
7022 
7023 static int alc680_parse_auto_config(struct hda_codec *codec)
7024 {
7025 	return alc_parse_auto_config(codec, NULL, NULL);
7026 }
7027 
7028 /*
7029  */
7030 static int patch_alc680(struct hda_codec *codec)
7031 {
7032 	int err;
7033 
7034 	/* ALC680 has no aa-loopback mixer */
7035 	err = alc_alloc_spec(codec, 0);
7036 	if (err < 0)
7037 		return err;
7038 
7039 	/* automatic parse from the BIOS config */
7040 	err = alc680_parse_auto_config(codec);
7041 	if (err < 0) {
7042 		alc_free(codec);
7043 		return err;
7044 	}
7045 
7046 	codec->patch_ops = alc_patch_ops;
7047 
7048 	return 0;
7049 }
7050 
7051 /*
7052  * patch entries
7053  */
7054 static const struct hda_codec_preset snd_hda_preset_realtek[] = {
7055 	{ .id = 0x10ec0221, .name = "ALC221", .patch = patch_alc269 },
7056 	{ .id = 0x10ec0260, .name = "ALC260", .patch = patch_alc260 },
7057 	{ .id = 0x10ec0262, .name = "ALC262", .patch = patch_alc262 },
7058 	{ .id = 0x10ec0267, .name = "ALC267", .patch = patch_alc268 },
7059 	{ .id = 0x10ec0268, .name = "ALC268", .patch = patch_alc268 },
7060 	{ .id = 0x10ec0269, .name = "ALC269", .patch = patch_alc269 },
7061 	{ .id = 0x10ec0270, .name = "ALC270", .patch = patch_alc269 },
7062 	{ .id = 0x10ec0272, .name = "ALC272", .patch = patch_alc662 },
7063 	{ .id = 0x10ec0275, .name = "ALC275", .patch = patch_alc269 },
7064 	{ .id = 0x10ec0276, .name = "ALC276", .patch = patch_alc269 },
7065 	{ .id = 0x10ec0280, .name = "ALC280", .patch = patch_alc269 },
7066 	{ .id = 0x10ec0282, .name = "ALC282", .patch = patch_alc269 },
7067 	{ .id = 0x10ec0283, .name = "ALC283", .patch = patch_alc269 },
7068 	{ .id = 0x10ec0290, .name = "ALC290", .patch = patch_alc269 },
7069 	{ .id = 0x10ec0861, .rev = 0x100340, .name = "ALC660",
7070 	  .patch = patch_alc861 },
7071 	{ .id = 0x10ec0660, .name = "ALC660-VD", .patch = patch_alc861vd },
7072 	{ .id = 0x10ec0861, .name = "ALC861", .patch = patch_alc861 },
7073 	{ .id = 0x10ec0862, .name = "ALC861-VD", .patch = patch_alc861vd },
7074 	{ .id = 0x10ec0662, .rev = 0x100002, .name = "ALC662 rev2",
7075 	  .patch = patch_alc882 },
7076 	{ .id = 0x10ec0662, .rev = 0x100101, .name = "ALC662 rev1",
7077 	  .patch = patch_alc662 },
7078 	{ .id = 0x10ec0662, .rev = 0x100300, .name = "ALC662 rev3",
7079 	  .patch = patch_alc662 },
7080 	{ .id = 0x10ec0663, .name = "ALC663", .patch = patch_alc662 },
7081 	{ .id = 0x10ec0665, .name = "ALC665", .patch = patch_alc662 },
7082 	{ .id = 0x10ec0670, .name = "ALC670", .patch = patch_alc662 },
7083 	{ .id = 0x10ec0680, .name = "ALC680", .patch = patch_alc680 },
7084 	{ .id = 0x10ec0880, .name = "ALC880", .patch = patch_alc880 },
7085 	{ .id = 0x10ec0882, .name = "ALC882", .patch = patch_alc882 },
7086 	{ .id = 0x10ec0883, .name = "ALC883", .patch = patch_alc882 },
7087 	{ .id = 0x10ec0885, .rev = 0x100101, .name = "ALC889A",
7088 	  .patch = patch_alc882 },
7089 	{ .id = 0x10ec0885, .rev = 0x100103, .name = "ALC889A",
7090 	  .patch = patch_alc882 },
7091 	{ .id = 0x10ec0885, .name = "ALC885", .patch = patch_alc882 },
7092 	{ .id = 0x10ec0887, .name = "ALC887", .patch = patch_alc882 },
7093 	{ .id = 0x10ec0888, .rev = 0x100101, .name = "ALC1200",
7094 	  .patch = patch_alc882 },
7095 	{ .id = 0x10ec0888, .name = "ALC888", .patch = patch_alc882 },
7096 	{ .id = 0x10ec0889, .name = "ALC889", .patch = patch_alc882 },
7097 	{ .id = 0x10ec0892, .name = "ALC892", .patch = patch_alc662 },
7098 	{ .id = 0x10ec0899, .name = "ALC898", .patch = patch_alc882 },
7099 	{} /* terminator */
7100 };
7101 
7102 MODULE_ALIAS("snd-hda-codec-id:10ec*");
7103 
7104 MODULE_LICENSE("GPL");
7105 MODULE_DESCRIPTION("Realtek HD-audio codec");
7106 
7107 static struct hda_codec_preset_list realtek_list = {
7108 	.preset = snd_hda_preset_realtek,
7109 	.owner = THIS_MODULE,
7110 };
7111 
7112 static int __init patch_realtek_init(void)
7113 {
7114 	return snd_hda_add_codec_preset(&realtek_list);
7115 }
7116 
7117 static void __exit patch_realtek_exit(void)
7118 {
7119 	snd_hda_delete_codec_preset(&realtek_list);
7120 }
7121 
7122 module_init(patch_realtek_init)
7123 module_exit(patch_realtek_exit)
7124