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