]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - sound/pci/hda/patch_realtek.c
ALSA: hda - Add inv-dmic model to possible 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 /* typically the digital mic is put at node 0x12 */
1788 static void alc_fixup_inv_dmic_0x12(struct hda_codec *codec,
1789 const struct alc_fixup *fix, int action)
1790 {
1791 if (action == ALC_FIXUP_ACT_PROBE)
1792 alc_add_inv_dmic_mixer(codec, 0x12);
1793 }
1794
1795 /*
1796 * virtual master controls
1797 */
1798
1799 /*
1800 * slave controls for virtual master
1801 */
1802 static const char * const alc_slave_pfxs[] = {
1803 "Front", "Surround", "Center", "LFE", "Side",
1804 "Headphone", "Speaker", "Mono", "Line Out",
1805 "CLFE", "Bass Speaker", "PCM",
1806 NULL,
1807 };
1808
1809 /*
1810 * build control elements
1811 */
1812
1813 #define NID_MAPPING (-1)
1814
1815 #define SUBDEV_SPEAKER_ (0 << 6)
1816 #define SUBDEV_HP_ (1 << 6)
1817 #define SUBDEV_LINE_ (2 << 6)
1818 #define SUBDEV_SPEAKER(x) (SUBDEV_SPEAKER_ | ((x) & 0x3f))
1819 #define SUBDEV_HP(x) (SUBDEV_HP_ | ((x) & 0x3f))
1820 #define SUBDEV_LINE(x) (SUBDEV_LINE_ | ((x) & 0x3f))
1821
1822 static void alc_free_kctls(struct hda_codec *codec);
1823
1824 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1825 /* additional beep mixers; the actual parameters are overwritten at build */
1826 static const struct snd_kcontrol_new alc_beep_mixer[] = {
1827 HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_INPUT),
1828 HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_INPUT),
1829 { } /* end */
1830 };
1831 #endif
1832
1833 static int __alc_build_controls(struct hda_codec *codec)
1834 {
1835 struct alc_spec *spec = codec->spec;
1836 struct snd_kcontrol *kctl = NULL;
1837 const struct snd_kcontrol_new *knew;
1838 int i, j, err;
1839 unsigned int u;
1840 hda_nid_t nid;
1841
1842 for (i = 0; i < spec->num_mixers; i++) {
1843 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1844 if (err < 0)
1845 return err;
1846 }
1847 if (spec->cap_mixer) {
1848 err = snd_hda_add_new_ctls(codec, spec->cap_mixer);
1849 if (err < 0)
1850 return err;
1851 }
1852 if (spec->multiout.dig_out_nid) {
1853 err = snd_hda_create_spdif_out_ctls(codec,
1854 spec->multiout.dig_out_nid,
1855 spec->multiout.dig_out_nid);
1856 if (err < 0)
1857 return err;
1858 if (!spec->no_analog) {
1859 err = snd_hda_create_spdif_share_sw(codec,
1860 &spec->multiout);
1861 if (err < 0)
1862 return err;
1863 spec->multiout.share_spdif = 1;
1864 }
1865 }
1866 if (spec->dig_in_nid) {
1867 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1868 if (err < 0)
1869 return err;
1870 }
1871
1872 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1873 /* create beep controls if needed */
1874 if (spec->beep_amp) {
1875 const struct snd_kcontrol_new *knew;
1876 for (knew = alc_beep_mixer; knew->name; knew++) {
1877 struct snd_kcontrol *kctl;
1878 kctl = snd_ctl_new1(knew, codec);
1879 if (!kctl)
1880 return -ENOMEM;
1881 kctl->private_value = spec->beep_amp;
1882 err = snd_hda_ctl_add(codec, 0, kctl);
1883 if (err < 0)
1884 return err;
1885 }
1886 }
1887 #endif
1888
1889 /* if we have no master control, let's create it */
1890 if (!spec->no_analog &&
1891 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1892 unsigned int vmaster_tlv[4];
1893 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1894 HDA_OUTPUT, vmaster_tlv);
1895 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1896 vmaster_tlv, alc_slave_pfxs,
1897 "Playback Volume");
1898 if (err < 0)
1899 return err;
1900 }
1901 if (!spec->no_analog &&
1902 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1903 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
1904 NULL, alc_slave_pfxs,
1905 "Playback Switch",
1906 true, &spec->vmaster_mute.sw_kctl);
1907 if (err < 0)
1908 return err;
1909 }
1910
1911 /* assign Capture Source enums to NID */
1912 if (spec->capsrc_nids || spec->adc_nids) {
1913 kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
1914 if (!kctl)
1915 kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1916 for (i = 0; kctl && i < kctl->count; i++) {
1917 err = snd_hda_add_nid(codec, kctl, i,
1918 get_capsrc(spec, i));
1919 if (err < 0)
1920 return err;
1921 }
1922 }
1923 if (spec->cap_mixer && spec->adc_nids) {
1924 const char *kname = kctl ? kctl->id.name : NULL;
1925 for (knew = spec->cap_mixer; knew->name; knew++) {
1926 if (kname && strcmp(knew->name, kname) == 0)
1927 continue;
1928 kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1929 for (i = 0; kctl && i < kctl->count; i++) {
1930 err = snd_hda_add_nid(codec, kctl, i,
1931 spec->adc_nids[i]);
1932 if (err < 0)
1933 return err;
1934 }
1935 }
1936 }
1937
1938 /* other nid->control mapping */
1939 for (i = 0; i < spec->num_mixers; i++) {
1940 for (knew = spec->mixers[i]; knew->name; knew++) {
1941 if (knew->iface != NID_MAPPING)
1942 continue;
1943 kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1944 if (kctl == NULL)
1945 continue;
1946 u = knew->subdevice;
1947 for (j = 0; j < 4; j++, u >>= 8) {
1948 nid = u & 0x3f;
1949 if (nid == 0)
1950 continue;
1951 switch (u & 0xc0) {
1952 case SUBDEV_SPEAKER_:
1953 nid = spec->autocfg.speaker_pins[nid];
1954 break;
1955 case SUBDEV_LINE_:
1956 nid = spec->autocfg.line_out_pins[nid];
1957 break;
1958 case SUBDEV_HP_:
1959 nid = spec->autocfg.hp_pins[nid];
1960 break;
1961 default:
1962 continue;
1963 }
1964 err = snd_hda_add_nid(codec, kctl, 0, nid);
1965 if (err < 0)
1966 return err;
1967 }
1968 u = knew->private_value;
1969 for (j = 0; j < 4; j++, u >>= 8) {
1970 nid = u & 0xff;
1971 if (nid == 0)
1972 continue;
1973 err = snd_hda_add_nid(codec, kctl, 0, nid);
1974 if (err < 0)
1975 return err;
1976 }
1977 }
1978 }
1979
1980 alc_free_kctls(codec); /* no longer needed */
1981
1982 return 0;
1983 }
1984
1985 static int alc_build_controls(struct hda_codec *codec)
1986 {
1987 struct alc_spec *spec = codec->spec;
1988 int err = __alc_build_controls(codec);
1989 if (err < 0)
1990 return err;
1991 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1992 if (err < 0)
1993 return err;
1994 alc_apply_fixup(codec, ALC_FIXUP_ACT_BUILD);
1995 return 0;
1996 }
1997
1998
1999 /*
2000 * Common callbacks
2001 */
2002
2003 static void alc_init_special_input_src(struct hda_codec *codec);
2004 static void alc_auto_init_std(struct hda_codec *codec);
2005
2006 static int alc_init(struct hda_codec *codec)
2007 {
2008 struct alc_spec *spec = codec->spec;
2009
2010 if (spec->init_hook)
2011 spec->init_hook(codec);
2012
2013 alc_fix_pll(codec);
2014 alc_auto_init_amp(codec, spec->init_amp);
2015
2016 snd_hda_gen_apply_verbs(codec);
2017 alc_init_special_input_src(codec);
2018 alc_auto_init_std(codec);
2019
2020 alc_apply_fixup(codec, ALC_FIXUP_ACT_INIT);
2021
2022 snd_hda_jack_report_sync(codec);
2023
2024 hda_call_check_power_status(codec, 0x01);
2025 return 0;
2026 }
2027
2028 static void alc_unsol_event(struct hda_codec *codec, unsigned int res)
2029 {
2030 struct alc_spec *spec = codec->spec;
2031
2032 if (spec->unsol_event)
2033 spec->unsol_event(codec, res);
2034 }
2035
2036 #ifdef CONFIG_SND_HDA_POWER_SAVE
2037 static int alc_check_power_status(struct hda_codec *codec, hda_nid_t nid)
2038 {
2039 struct alc_spec *spec = codec->spec;
2040 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
2041 }
2042 #endif
2043
2044 /*
2045 * Analog playback callbacks
2046 */
2047 static int alc_playback_pcm_open(struct hda_pcm_stream *hinfo,
2048 struct hda_codec *codec,
2049 struct snd_pcm_substream *substream)
2050 {
2051 struct alc_spec *spec = codec->spec;
2052 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2053 hinfo);
2054 }
2055
2056 static int alc_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2057 struct hda_codec *codec,
2058 unsigned int stream_tag,
2059 unsigned int format,
2060 struct snd_pcm_substream *substream)
2061 {
2062 struct alc_spec *spec = codec->spec;
2063 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2064 stream_tag, format, substream);
2065 }
2066
2067 static int alc_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2068 struct hda_codec *codec,
2069 struct snd_pcm_substream *substream)
2070 {
2071 struct alc_spec *spec = codec->spec;
2072 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
2073 }
2074
2075 /*
2076 * Digital out
2077 */
2078 static int alc_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2079 struct hda_codec *codec,
2080 struct snd_pcm_substream *substream)
2081 {
2082 struct alc_spec *spec = codec->spec;
2083 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2084 }
2085
2086 static int alc_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2087 struct hda_codec *codec,
2088 unsigned int stream_tag,
2089 unsigned int format,
2090 struct snd_pcm_substream *substream)
2091 {
2092 struct alc_spec *spec = codec->spec;
2093 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2094 stream_tag, format, substream);
2095 }
2096
2097 static int alc_dig_playback_pcm_cleanup(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_cleanup(codec, &spec->multiout);
2103 }
2104
2105 static int alc_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2106 struct hda_codec *codec,
2107 struct snd_pcm_substream *substream)
2108 {
2109 struct alc_spec *spec = codec->spec;
2110 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2111 }
2112
2113 /*
2114 * Analog capture
2115 */
2116 static int alc_alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2117 struct hda_codec *codec,
2118 unsigned int stream_tag,
2119 unsigned int format,
2120 struct snd_pcm_substream *substream)
2121 {
2122 struct alc_spec *spec = codec->spec;
2123
2124 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
2125 stream_tag, 0, format);
2126 return 0;
2127 }
2128
2129 static int alc_alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2130 struct hda_codec *codec,
2131 struct snd_pcm_substream *substream)
2132 {
2133 struct alc_spec *spec = codec->spec;
2134
2135 snd_hda_codec_cleanup_stream(codec,
2136 spec->adc_nids[substream->number + 1]);
2137 return 0;
2138 }
2139
2140 /* analog capture with dynamic dual-adc changes */
2141 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2142 struct hda_codec *codec,
2143 unsigned int stream_tag,
2144 unsigned int format,
2145 struct snd_pcm_substream *substream)
2146 {
2147 struct alc_spec *spec = codec->spec;
2148 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
2149 spec->cur_adc_stream_tag = stream_tag;
2150 spec->cur_adc_format = format;
2151 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
2152 return 0;
2153 }
2154
2155 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2156 struct hda_codec *codec,
2157 struct snd_pcm_substream *substream)
2158 {
2159 struct alc_spec *spec = codec->spec;
2160 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
2161 spec->cur_adc = 0;
2162 return 0;
2163 }
2164
2165 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
2166 .substreams = 1,
2167 .channels_min = 2,
2168 .channels_max = 2,
2169 .nid = 0, /* fill later */
2170 .ops = {
2171 .prepare = dyn_adc_capture_pcm_prepare,
2172 .cleanup = dyn_adc_capture_pcm_cleanup
2173 },
2174 };
2175
2176 /*
2177 */
2178 static const struct hda_pcm_stream alc_pcm_analog_playback = {
2179 .substreams = 1,
2180 .channels_min = 2,
2181 .channels_max = 8,
2182 /* NID is set in alc_build_pcms */
2183 .ops = {
2184 .open = alc_playback_pcm_open,
2185 .prepare = alc_playback_pcm_prepare,
2186 .cleanup = alc_playback_pcm_cleanup
2187 },
2188 };
2189
2190 static const struct hda_pcm_stream alc_pcm_analog_capture = {
2191 .substreams = 1,
2192 .channels_min = 2,
2193 .channels_max = 2,
2194 /* NID is set in alc_build_pcms */
2195 };
2196
2197 static const struct hda_pcm_stream alc_pcm_analog_alt_playback = {
2198 .substreams = 1,
2199 .channels_min = 2,
2200 .channels_max = 2,
2201 /* NID is set in alc_build_pcms */
2202 };
2203
2204 static const struct hda_pcm_stream alc_pcm_analog_alt_capture = {
2205 .substreams = 2, /* can be overridden */
2206 .channels_min = 2,
2207 .channels_max = 2,
2208 /* NID is set in alc_build_pcms */
2209 .ops = {
2210 .prepare = alc_alt_capture_pcm_prepare,
2211 .cleanup = alc_alt_capture_pcm_cleanup
2212 },
2213 };
2214
2215 static const struct hda_pcm_stream alc_pcm_digital_playback = {
2216 .substreams = 1,
2217 .channels_min = 2,
2218 .channels_max = 2,
2219 /* NID is set in alc_build_pcms */
2220 .ops = {
2221 .open = alc_dig_playback_pcm_open,
2222 .close = alc_dig_playback_pcm_close,
2223 .prepare = alc_dig_playback_pcm_prepare,
2224 .cleanup = alc_dig_playback_pcm_cleanup
2225 },
2226 };
2227
2228 static const struct hda_pcm_stream alc_pcm_digital_capture = {
2229 .substreams = 1,
2230 .channels_min = 2,
2231 .channels_max = 2,
2232 /* NID is set in alc_build_pcms */
2233 };
2234
2235 /* Used by alc_build_pcms to flag that a PCM has no playback stream */
2236 static const struct hda_pcm_stream alc_pcm_null_stream = {
2237 .substreams = 0,
2238 .channels_min = 0,
2239 .channels_max = 0,
2240 };
2241
2242 static int alc_build_pcms(struct hda_codec *codec)
2243 {
2244 struct alc_spec *spec = codec->spec;
2245 struct hda_pcm *info = spec->pcm_rec;
2246 const struct hda_pcm_stream *p;
2247 bool have_multi_adcs;
2248 int i;
2249
2250 codec->num_pcms = 1;
2251 codec->pcm_info = info;
2252
2253 if (spec->no_analog)
2254 goto skip_analog;
2255
2256 snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
2257 "%s Analog", codec->chip_name);
2258 info->name = spec->stream_name_analog;
2259
2260 if (spec->multiout.num_dacs > 0) {
2261 p = spec->stream_analog_playback;
2262 if (!p)
2263 p = &alc_pcm_analog_playback;
2264 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2265 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
2266 }
2267 if (spec->adc_nids) {
2268 p = spec->stream_analog_capture;
2269 if (!p) {
2270 if (spec->dyn_adc_switch)
2271 p = &dyn_adc_pcm_analog_capture;
2272 else
2273 p = &alc_pcm_analog_capture;
2274 }
2275 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2276 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
2277 }
2278
2279 if (spec->channel_mode) {
2280 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
2281 for (i = 0; i < spec->num_channel_mode; i++) {
2282 if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
2283 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
2284 }
2285 }
2286 }
2287
2288 skip_analog:
2289 /* SPDIF for stream index #1 */
2290 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
2291 snprintf(spec->stream_name_digital,
2292 sizeof(spec->stream_name_digital),
2293 "%s Digital", codec->chip_name);
2294 codec->num_pcms = 2;
2295 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
2296 info = spec->pcm_rec + 1;
2297 info->name = spec->stream_name_digital;
2298 if (spec->dig_out_type)
2299 info->pcm_type = spec->dig_out_type;
2300 else
2301 info->pcm_type = HDA_PCM_TYPE_SPDIF;
2302 if (spec->multiout.dig_out_nid) {
2303 p = spec->stream_digital_playback;
2304 if (!p)
2305 p = &alc_pcm_digital_playback;
2306 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2307 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
2308 }
2309 if (spec->dig_in_nid) {
2310 p = spec->stream_digital_capture;
2311 if (!p)
2312 p = &alc_pcm_digital_capture;
2313 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2314 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
2315 }
2316 /* FIXME: do we need this for all Realtek codec models? */
2317 codec->spdif_status_reset = 1;
2318 }
2319
2320 if (spec->no_analog)
2321 return 0;
2322
2323 /* If the use of more than one ADC is requested for the current
2324 * model, configure a second analog capture-only PCM.
2325 */
2326 have_multi_adcs = (spec->num_adc_nids > 1) &&
2327 !spec->dyn_adc_switch && !spec->auto_mic &&
2328 (!spec->input_mux || spec->input_mux->num_items > 1);
2329 /* Additional Analaog capture for index #2 */
2330 if (spec->alt_dac_nid || have_multi_adcs) {
2331 codec->num_pcms = 3;
2332 info = spec->pcm_rec + 2;
2333 info->name = spec->stream_name_analog;
2334 if (spec->alt_dac_nid) {
2335 p = spec->stream_analog_alt_playback;
2336 if (!p)
2337 p = &alc_pcm_analog_alt_playback;
2338 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2339 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
2340 spec->alt_dac_nid;
2341 } else {
2342 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
2343 alc_pcm_null_stream;
2344 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
2345 }
2346 if (have_multi_adcs) {
2347 p = spec->stream_analog_alt_capture;
2348 if (!p)
2349 p = &alc_pcm_analog_alt_capture;
2350 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2351 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
2352 spec->adc_nids[1];
2353 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
2354 spec->num_adc_nids - 1;
2355 } else {
2356 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
2357 alc_pcm_null_stream;
2358 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
2359 }
2360 }
2361
2362 return 0;
2363 }
2364
2365 static inline void alc_shutup(struct hda_codec *codec)
2366 {
2367 struct alc_spec *spec = codec->spec;
2368
2369 if (spec && spec->shutup)
2370 spec->shutup(codec);
2371 snd_hda_shutup_pins(codec);
2372 }
2373
2374 static void alc_free_kctls(struct hda_codec *codec)
2375 {
2376 struct alc_spec *spec = codec->spec;
2377
2378 if (spec->kctls.list) {
2379 struct snd_kcontrol_new *kctl = spec->kctls.list;
2380 int i;
2381 for (i = 0; i < spec->kctls.used; i++)
2382 kfree(kctl[i].name);
2383 }
2384 snd_array_free(&spec->kctls);
2385 }
2386
2387 static void alc_free_bind_ctls(struct hda_codec *codec)
2388 {
2389 struct alc_spec *spec = codec->spec;
2390 if (spec->bind_ctls.list) {
2391 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
2392 int i;
2393 for (i = 0; i < spec->bind_ctls.used; i++)
2394 kfree(ctl[i]);
2395 }
2396 snd_array_free(&spec->bind_ctls);
2397 }
2398
2399 static void alc_free(struct hda_codec *codec)
2400 {
2401 struct alc_spec *spec = codec->spec;
2402
2403 if (!spec)
2404 return;
2405
2406 alc_shutup(codec);
2407 alc_free_kctls(codec);
2408 alc_free_bind_ctls(codec);
2409 kfree(spec);
2410 snd_hda_detach_beep_device(codec);
2411 }
2412
2413 #ifdef CONFIG_SND_HDA_POWER_SAVE
2414 static void alc_power_eapd(struct hda_codec *codec)
2415 {
2416 alc_auto_setup_eapd(codec, false);
2417 }
2418
2419 static int alc_suspend(struct hda_codec *codec, pm_message_t state)
2420 {
2421 struct alc_spec *spec = codec->spec;
2422 alc_shutup(codec);
2423 if (spec && spec->power_hook)
2424 spec->power_hook(codec);
2425 return 0;
2426 }
2427 #endif
2428
2429 #ifdef CONFIG_PM
2430 static int alc_resume(struct hda_codec *codec)
2431 {
2432 msleep(150); /* to avoid pop noise */
2433 codec->patch_ops.init(codec);
2434 snd_hda_codec_resume_amp(codec);
2435 snd_hda_codec_resume_cache(codec);
2436 alc_inv_dmic_sync(codec, true);
2437 hda_call_check_power_status(codec, 0x01);
2438 return 0;
2439 }
2440 #endif
2441
2442 /*
2443 */
2444 static const struct hda_codec_ops alc_patch_ops = {
2445 .build_controls = alc_build_controls,
2446 .build_pcms = alc_build_pcms,
2447 .init = alc_init,
2448 .free = alc_free,
2449 .unsol_event = alc_unsol_event,
2450 #ifdef CONFIG_PM
2451 .resume = alc_resume,
2452 #endif
2453 #ifdef CONFIG_SND_HDA_POWER_SAVE
2454 .suspend = alc_suspend,
2455 .check_power_status = alc_check_power_status,
2456 #endif
2457 .reboot_notify = alc_shutup,
2458 };
2459
2460 /* replace the codec chip_name with the given string */
2461 static int alc_codec_rename(struct hda_codec *codec, const char *name)
2462 {
2463 kfree(codec->chip_name);
2464 codec->chip_name = kstrdup(name, GFP_KERNEL);
2465 if (!codec->chip_name) {
2466 alc_free(codec);
2467 return -ENOMEM;
2468 }
2469 return 0;
2470 }
2471
2472 /*
2473 * Rename codecs appropriately from COEF value
2474 */
2475 struct alc_codec_rename_table {
2476 unsigned int vendor_id;
2477 unsigned short coef_mask;
2478 unsigned short coef_bits;
2479 const char *name;
2480 };
2481
2482 static struct alc_codec_rename_table rename_tbl[] = {
2483 { 0x10ec0269, 0xfff0, 0x3010, "ALC277" },
2484 { 0x10ec0269, 0xf0f0, 0x2010, "ALC259" },
2485 { 0x10ec0269, 0xf0f0, 0x3010, "ALC258" },
2486 { 0x10ec0269, 0x00f0, 0x0010, "ALC269VB" },
2487 { 0x10ec0269, 0xffff, 0xa023, "ALC259" },
2488 { 0x10ec0269, 0xffff, 0x6023, "ALC281X" },
2489 { 0x10ec0269, 0x00f0, 0x0020, "ALC269VC" },
2490 { 0x10ec0269, 0x00f0, 0x0030, "ALC269VD" },
2491 { 0x10ec0887, 0x00f0, 0x0030, "ALC887-VD" },
2492 { 0x10ec0888, 0x00f0, 0x0030, "ALC888-VD" },
2493 { 0x10ec0888, 0xf0f0, 0x3020, "ALC886" },
2494 { 0x10ec0899, 0x2000, 0x2000, "ALC899" },
2495 { 0x10ec0892, 0xffff, 0x8020, "ALC661" },
2496 { 0x10ec0892, 0xffff, 0x8011, "ALC661" },
2497 { 0x10ec0892, 0xffff, 0x4011, "ALC656" },
2498 { } /* terminator */
2499 };
2500
2501 static int alc_codec_rename_from_preset(struct hda_codec *codec)
2502 {
2503 const struct alc_codec_rename_table *p;
2504
2505 for (p = rename_tbl; p->vendor_id; p++) {
2506 if (p->vendor_id != codec->vendor_id)
2507 continue;
2508 if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
2509 return alc_codec_rename(codec, p->name);
2510 }
2511 return 0;
2512 }
2513
2514 /*
2515 * Automatic parse of I/O pins from the BIOS configuration
2516 */
2517
2518 enum {
2519 ALC_CTL_WIDGET_VOL,
2520 ALC_CTL_WIDGET_MUTE,
2521 ALC_CTL_BIND_MUTE,
2522 ALC_CTL_BIND_VOL,
2523 ALC_CTL_BIND_SW,
2524 };
2525 static const struct snd_kcontrol_new alc_control_templates[] = {
2526 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2527 HDA_CODEC_MUTE(NULL, 0, 0, 0),
2528 HDA_BIND_MUTE(NULL, 0, 0, 0),
2529 HDA_BIND_VOL(NULL, 0),
2530 HDA_BIND_SW(NULL, 0),
2531 };
2532
2533 /* add dynamic controls */
2534 static int add_control(struct alc_spec *spec, int type, const char *name,
2535 int cidx, unsigned long val)
2536 {
2537 struct snd_kcontrol_new *knew;
2538
2539 knew = alc_kcontrol_new(spec);
2540 if (!knew)
2541 return -ENOMEM;
2542 *knew = alc_control_templates[type];
2543 knew->name = kstrdup(name, GFP_KERNEL);
2544 if (!knew->name)
2545 return -ENOMEM;
2546 knew->index = cidx;
2547 if (get_amp_nid_(val))
2548 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2549 knew->private_value = val;
2550 return 0;
2551 }
2552
2553 static int add_control_with_pfx(struct alc_spec *spec, int type,
2554 const char *pfx, const char *dir,
2555 const char *sfx, int cidx, unsigned long val)
2556 {
2557 char name[32];
2558 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
2559 return add_control(spec, type, name, cidx, val);
2560 }
2561
2562 #define add_pb_vol_ctrl(spec, type, pfx, val) \
2563 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
2564 #define add_pb_sw_ctrl(spec, type, pfx, val) \
2565 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
2566 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
2567 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
2568 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
2569 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
2570
2571 static const char * const channel_name[4] = {
2572 "Front", "Surround", "CLFE", "Side"
2573 };
2574
2575 static const char *alc_get_line_out_pfx(struct alc_spec *spec, int ch,
2576 bool can_be_master, int *index)
2577 {
2578 struct auto_pin_cfg *cfg = &spec->autocfg;
2579
2580 *index = 0;
2581 if (cfg->line_outs == 1 && !spec->multi_ios &&
2582 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
2583 return "Master";
2584
2585 switch (cfg->line_out_type) {
2586 case AUTO_PIN_SPEAKER_OUT:
2587 if (cfg->line_outs == 1)
2588 return "Speaker";
2589 if (cfg->line_outs == 2)
2590 return ch ? "Bass Speaker" : "Speaker";
2591 break;
2592 case AUTO_PIN_HP_OUT:
2593 /* for multi-io case, only the primary out */
2594 if (ch && spec->multi_ios)
2595 break;
2596 *index = ch;
2597 return "Headphone";
2598 default:
2599 if (cfg->line_outs == 1 && !spec->multi_ios)
2600 return "PCM";
2601 break;
2602 }
2603 if (snd_BUG_ON(ch >= ARRAY_SIZE(channel_name)))
2604 return "PCM";
2605
2606 return channel_name[ch];
2607 }
2608
2609 #ifdef CONFIG_SND_HDA_POWER_SAVE
2610 /* add the powersave loopback-list entry */
2611 static void add_loopback_list(struct alc_spec *spec, hda_nid_t mix, int idx)
2612 {
2613 struct hda_amp_list *list;
2614
2615 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2616 return;
2617 list = spec->loopback_list + spec->num_loopbacks;
2618 list->nid = mix;
2619 list->dir = HDA_INPUT;
2620 list->idx = idx;
2621 spec->num_loopbacks++;
2622 spec->loopback.amplist = spec->loopback_list;
2623 }
2624 #else
2625 #define add_loopback_list(spec, mix, idx) /* NOP */
2626 #endif
2627
2628 /* create input playback/capture controls for the given pin */
2629 static int new_analog_input(struct alc_spec *spec, hda_nid_t pin,
2630 const char *ctlname, int ctlidx,
2631 int idx, hda_nid_t mix_nid)
2632 {
2633 int err;
2634
2635 err = __add_pb_vol_ctrl(spec, ALC_CTL_WIDGET_VOL, ctlname, ctlidx,
2636 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2637 if (err < 0)
2638 return err;
2639 err = __add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, ctlname, ctlidx,
2640 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2641 if (err < 0)
2642 return err;
2643 add_loopback_list(spec, mix_nid, idx);
2644 return 0;
2645 }
2646
2647 static int alc_is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2648 {
2649 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2650 return (pincap & AC_PINCAP_IN) != 0;
2651 }
2652
2653 /* Parse the codec tree and retrieve ADCs and corresponding capsrc MUXs */
2654 static int alc_auto_fill_adc_caps(struct hda_codec *codec)
2655 {
2656 struct alc_spec *spec = codec->spec;
2657 hda_nid_t nid;
2658 hda_nid_t *adc_nids = spec->private_adc_nids;
2659 hda_nid_t *cap_nids = spec->private_capsrc_nids;
2660 int max_nums = ARRAY_SIZE(spec->private_adc_nids);
2661 int i, nums = 0;
2662
2663 nid = codec->start_nid;
2664 for (i = 0; i < codec->num_nodes; i++, nid++) {
2665 hda_nid_t src;
2666 unsigned int caps = get_wcaps(codec, nid);
2667 int type = get_wcaps_type(caps);
2668
2669 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2670 continue;
2671 adc_nids[nums] = nid;
2672 cap_nids[nums] = nid;
2673 src = nid;
2674 for (;;) {
2675 int n;
2676 type = get_wcaps_type(get_wcaps(codec, src));
2677 if (type == AC_WID_PIN)
2678 break;
2679 if (type == AC_WID_AUD_SEL) {
2680 cap_nids[nums] = src;
2681 break;
2682 }
2683 n = snd_hda_get_num_conns(codec, src);
2684 if (n > 1) {
2685 cap_nids[nums] = src;
2686 break;
2687 } else if (n != 1)
2688 break;
2689 if (snd_hda_get_connections(codec, src, &src, 1) != 1)
2690 break;
2691 }
2692 if (++nums >= max_nums)
2693 break;
2694 }
2695 spec->adc_nids = spec->private_adc_nids;
2696 spec->capsrc_nids = spec->private_capsrc_nids;
2697 spec->num_adc_nids = nums;
2698 return nums;
2699 }
2700
2701 /* create playback/capture controls for input pins */
2702 static int alc_auto_create_input_ctls(struct hda_codec *codec)
2703 {
2704 struct alc_spec *spec = codec->spec;
2705 const struct auto_pin_cfg *cfg = &spec->autocfg;
2706 hda_nid_t mixer = spec->mixer_nid;
2707 struct hda_input_mux *imux = &spec->private_imux[0];
2708 int num_adcs;
2709 int i, c, err, idx, type_idx = 0;
2710 const char *prev_label = NULL;
2711
2712 num_adcs = alc_auto_fill_adc_caps(codec);
2713 if (num_adcs < 0)
2714 return 0;
2715
2716 for (i = 0; i < cfg->num_inputs; i++) {
2717 hda_nid_t pin;
2718 const char *label;
2719
2720 pin = cfg->inputs[i].pin;
2721 if (!alc_is_input_pin(codec, pin))
2722 continue;
2723
2724 label = hda_get_autocfg_input_label(codec, cfg, i);
2725 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2726 label = "Headphone Mic";
2727 if (prev_label && !strcmp(label, prev_label))
2728 type_idx++;
2729 else
2730 type_idx = 0;
2731 prev_label = label;
2732
2733 if (mixer) {
2734 idx = get_connection_index(codec, mixer, pin);
2735 if (idx >= 0) {
2736 err = new_analog_input(spec, pin,
2737 label, type_idx,
2738 idx, mixer);
2739 if (err < 0)
2740 return err;
2741 }
2742 }
2743
2744 for (c = 0; c < num_adcs; c++) {
2745 hda_nid_t cap = get_capsrc(spec, c);
2746 idx = get_connection_index(codec, cap, pin);
2747 if (idx >= 0) {
2748 spec->imux_pins[imux->num_items] = pin;
2749 snd_hda_add_imux_item(imux, label, idx, NULL);
2750 break;
2751 }
2752 }
2753 }
2754
2755 spec->num_mux_defs = 1;
2756 spec->input_mux = imux;
2757
2758 return 0;
2759 }
2760
2761 /* create a shared input with the headphone out */
2762 static int alc_auto_create_shared_input(struct hda_codec *codec)
2763 {
2764 struct alc_spec *spec = codec->spec;
2765 struct auto_pin_cfg *cfg = &spec->autocfg;
2766 unsigned int defcfg;
2767 hda_nid_t nid;
2768
2769 /* only one internal input pin? */
2770 if (cfg->num_inputs != 1)
2771 return 0;
2772 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2773 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2774 return 0;
2775
2776 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2777 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2778 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2779 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2780 else
2781 return 0; /* both not available */
2782
2783 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2784 return 0; /* no input */
2785
2786 cfg->inputs[1].pin = nid;
2787 cfg->inputs[1].type = AUTO_PIN_MIC;
2788 cfg->num_inputs = 2;
2789 spec->shared_mic_hp = 1;
2790 snd_printdd("realtek: Enable shared I/O jack on NID 0x%x\n", nid);
2791 return 0;
2792 }
2793
2794 static void alc_set_pin_output(struct hda_codec *codec, hda_nid_t nid,
2795 unsigned int pin_type)
2796 {
2797 snd_hda_set_pin_ctl(codec, nid, pin_type);
2798 /* unmute pin */
2799 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2800 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2801 AMP_OUT_UNMUTE);
2802 }
2803
2804 static int get_pin_type(int line_out_type)
2805 {
2806 if (line_out_type == AUTO_PIN_HP_OUT)
2807 return PIN_HP;
2808 else
2809 return PIN_OUT;
2810 }
2811
2812 static void alc_auto_init_analog_input(struct hda_codec *codec)
2813 {
2814 struct alc_spec *spec = codec->spec;
2815 struct auto_pin_cfg *cfg = &spec->autocfg;
2816 int i;
2817
2818 for (i = 0; i < cfg->num_inputs; i++) {
2819 hda_nid_t nid = cfg->inputs[i].pin;
2820 if (alc_is_input_pin(codec, nid)) {
2821 alc_set_input_pin(codec, nid, cfg->inputs[i].type);
2822 if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
2823 snd_hda_codec_write(codec, nid, 0,
2824 AC_VERB_SET_AMP_GAIN_MUTE,
2825 AMP_OUT_MUTE);
2826 }
2827 }
2828
2829 /* mute all loopback inputs */
2830 if (spec->mixer_nid) {
2831 int nums = snd_hda_get_num_conns(codec, spec->mixer_nid);
2832 for (i = 0; i < nums; i++)
2833 snd_hda_codec_write(codec, spec->mixer_nid, 0,
2834 AC_VERB_SET_AMP_GAIN_MUTE,
2835 AMP_IN_MUTE(i));
2836 }
2837 }
2838
2839 /* convert from MIX nid to DAC */
2840 static hda_nid_t alc_auto_mix_to_dac(struct hda_codec *codec, hda_nid_t nid)
2841 {
2842 hda_nid_t list[5];
2843 int i, num;
2844
2845 if (get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_AUD_OUT)
2846 return nid;
2847 num = snd_hda_get_connections(codec, nid, list, ARRAY_SIZE(list));
2848 for (i = 0; i < num; i++) {
2849 if (get_wcaps_type(get_wcaps(codec, list[i])) == AC_WID_AUD_OUT)
2850 return list[i];
2851 }
2852 return 0;
2853 }
2854
2855 /* go down to the selector widget before the mixer */
2856 static hda_nid_t alc_go_down_to_selector(struct hda_codec *codec, hda_nid_t pin)
2857 {
2858 hda_nid_t srcs[5];
2859 int num = snd_hda_get_connections(codec, pin, srcs,
2860 ARRAY_SIZE(srcs));
2861 if (num != 1 ||
2862 get_wcaps_type(get_wcaps(codec, srcs[0])) != AC_WID_AUD_SEL)
2863 return pin;
2864 return srcs[0];
2865 }
2866
2867 /* get MIX nid connected to the given pin targeted to DAC */
2868 static hda_nid_t alc_auto_dac_to_mix(struct hda_codec *codec, hda_nid_t pin,
2869 hda_nid_t dac)
2870 {
2871 hda_nid_t mix[5];
2872 int i, num;
2873
2874 pin = alc_go_down_to_selector(codec, pin);
2875 num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2876 for (i = 0; i < num; i++) {
2877 if (alc_auto_mix_to_dac(codec, mix[i]) == dac)
2878 return mix[i];
2879 }
2880 return 0;
2881 }
2882
2883 /* select the connection from pin to DAC if needed */
2884 static int alc_auto_select_dac(struct hda_codec *codec, hda_nid_t pin,
2885 hda_nid_t dac)
2886 {
2887 hda_nid_t mix[5];
2888 int i, num;
2889
2890 pin = alc_go_down_to_selector(codec, pin);
2891 num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2892 if (num < 2)
2893 return 0;
2894 for (i = 0; i < num; i++) {
2895 if (alc_auto_mix_to_dac(codec, mix[i]) == dac) {
2896 snd_hda_codec_update_cache(codec, pin, 0,
2897 AC_VERB_SET_CONNECT_SEL, i);
2898 return 0;
2899 }
2900 }
2901 return 0;
2902 }
2903
2904 static bool alc_is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
2905 {
2906 struct alc_spec *spec = codec->spec;
2907 int i;
2908 if (found_in_nid_list(nid, spec->multiout.dac_nids,
2909 ARRAY_SIZE(spec->private_dac_nids)) ||
2910 found_in_nid_list(nid, spec->multiout.hp_out_nid,
2911 ARRAY_SIZE(spec->multiout.hp_out_nid)) ||
2912 found_in_nid_list(nid, spec->multiout.extra_out_nid,
2913 ARRAY_SIZE(spec->multiout.extra_out_nid)))
2914 return true;
2915 for (i = 0; i < spec->multi_ios; i++) {
2916 if (spec->multi_io[i].dac == nid)
2917 return true;
2918 }
2919 return false;
2920 }
2921
2922 /* look for an empty DAC slot */
2923 static hda_nid_t alc_auto_look_for_dac(struct hda_codec *codec, hda_nid_t pin)
2924 {
2925 hda_nid_t srcs[5];
2926 int i, num;
2927
2928 pin = alc_go_down_to_selector(codec, pin);
2929 num = snd_hda_get_connections(codec, pin, srcs, ARRAY_SIZE(srcs));
2930 for (i = 0; i < num; i++) {
2931 hda_nid_t nid = alc_auto_mix_to_dac(codec, srcs[i]);
2932 if (!nid)
2933 continue;
2934 if (!alc_is_dac_already_used(codec, nid))
2935 return nid;
2936 }
2937 return 0;
2938 }
2939
2940 /* check whether the DAC is reachable from the pin */
2941 static bool alc_auto_is_dac_reachable(struct hda_codec *codec,
2942 hda_nid_t pin, hda_nid_t dac)
2943 {
2944 hda_nid_t srcs[5];
2945 int i, num;
2946
2947 if (!pin || !dac)
2948 return false;
2949 pin = alc_go_down_to_selector(codec, pin);
2950 num = snd_hda_get_connections(codec, pin, srcs, ARRAY_SIZE(srcs));
2951 for (i = 0; i < num; i++) {
2952 hda_nid_t nid = alc_auto_mix_to_dac(codec, srcs[i]);
2953 if (nid == dac)
2954 return true;
2955 }
2956 return false;
2957 }
2958
2959 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
2960 {
2961 struct alc_spec *spec = codec->spec;
2962 hda_nid_t sel = alc_go_down_to_selector(codec, pin);
2963 hda_nid_t nid, nid_found, srcs[5];
2964 int i, num = snd_hda_get_connections(codec, sel, srcs,
2965 ARRAY_SIZE(srcs));
2966 if (num == 1)
2967 return alc_auto_look_for_dac(codec, pin);
2968 nid_found = 0;
2969 for (i = 0; i < num; i++) {
2970 if (srcs[i] == spec->mixer_nid)
2971 continue;
2972 nid = alc_auto_mix_to_dac(codec, srcs[i]);
2973 if (nid && !alc_is_dac_already_used(codec, nid)) {
2974 if (nid_found)
2975 return 0;
2976 nid_found = nid;
2977 }
2978 }
2979 return nid_found;
2980 }
2981
2982 /* mark up volume and mute control NIDs: used during badness parsing and
2983 * at creating actual controls
2984 */
2985 static inline unsigned int get_ctl_pos(unsigned int data)
2986 {
2987 hda_nid_t nid = get_amp_nid_(data);
2988 unsigned int dir;
2989 if (snd_BUG_ON(nid >= MAX_VOL_NIDS))
2990 return 0;
2991 dir = get_amp_direction_(data);
2992 return (nid << 1) | dir;
2993 }
2994
2995 #define is_ctl_used(bits, data) \
2996 test_bit(get_ctl_pos(data), bits)
2997 #define mark_ctl_usage(bits, data) \
2998 set_bit(get_ctl_pos(data), bits)
2999
3000 static void clear_vol_marks(struct hda_codec *codec)
3001 {
3002 struct alc_spec *spec = codec->spec;
3003 memset(spec->vol_ctls, 0, sizeof(spec->vol_ctls));
3004 memset(spec->sw_ctls, 0, sizeof(spec->sw_ctls));
3005 }
3006
3007 /* badness definition */
3008 enum {
3009 /* No primary DAC is found for the main output */
3010 BAD_NO_PRIMARY_DAC = 0x10000,
3011 /* No DAC is found for the extra output */
3012 BAD_NO_DAC = 0x4000,
3013 /* No possible multi-ios */
3014 BAD_MULTI_IO = 0x103,
3015 /* No individual DAC for extra output */
3016 BAD_NO_EXTRA_DAC = 0x102,
3017 /* No individual DAC for extra surrounds */
3018 BAD_NO_EXTRA_SURR_DAC = 0x101,
3019 /* Primary DAC shared with main surrounds */
3020 BAD_SHARED_SURROUND = 0x100,
3021 /* Primary DAC shared with main CLFE */
3022 BAD_SHARED_CLFE = 0x10,
3023 /* Primary DAC shared with extra surrounds */
3024 BAD_SHARED_EXTRA_SURROUND = 0x10,
3025 /* Volume widget is shared */
3026 BAD_SHARED_VOL = 0x10,
3027 };
3028
3029 static hda_nid_t alc_look_for_out_mute_nid(struct hda_codec *codec,
3030 hda_nid_t pin, hda_nid_t dac);
3031 static hda_nid_t alc_look_for_out_vol_nid(struct hda_codec *codec,
3032 hda_nid_t pin, hda_nid_t dac);
3033
3034 static int eval_shared_vol_badness(struct hda_codec *codec, hda_nid_t pin,
3035 hda_nid_t dac)
3036 {
3037 struct alc_spec *spec = codec->spec;
3038 hda_nid_t nid;
3039 unsigned int val;
3040 int badness = 0;
3041
3042 nid = alc_look_for_out_vol_nid(codec, pin, dac);
3043 if (nid) {
3044 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3045 if (is_ctl_used(spec->vol_ctls, nid))
3046 badness += BAD_SHARED_VOL;
3047 else
3048 mark_ctl_usage(spec->vol_ctls, val);
3049 } else
3050 badness += BAD_SHARED_VOL;
3051 nid = alc_look_for_out_mute_nid(codec, pin, dac);
3052 if (nid) {
3053 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
3054 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT)
3055 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3056 else
3057 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
3058 if (is_ctl_used(spec->sw_ctls, val))
3059 badness += BAD_SHARED_VOL;
3060 else
3061 mark_ctl_usage(spec->sw_ctls, val);
3062 } else
3063 badness += BAD_SHARED_VOL;
3064 return badness;
3065 }
3066
3067 struct badness_table {
3068 int no_primary_dac; /* no primary DAC */
3069 int no_dac; /* no secondary DACs */
3070 int shared_primary; /* primary DAC is shared with main output */
3071 int shared_surr; /* secondary DAC shared with main or primary */
3072 int shared_clfe; /* third DAC shared with main or primary */
3073 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
3074 };
3075
3076 static struct badness_table main_out_badness = {
3077 .no_primary_dac = BAD_NO_PRIMARY_DAC,
3078 .no_dac = BAD_NO_DAC,
3079 .shared_primary = BAD_NO_PRIMARY_DAC,
3080 .shared_surr = BAD_SHARED_SURROUND,
3081 .shared_clfe = BAD_SHARED_CLFE,
3082 .shared_surr_main = BAD_SHARED_SURROUND,
3083 };
3084
3085 static struct badness_table extra_out_badness = {
3086 .no_primary_dac = BAD_NO_DAC,
3087 .no_dac = BAD_NO_DAC,
3088 .shared_primary = BAD_NO_EXTRA_DAC,
3089 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
3090 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
3091 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
3092 };
3093
3094 /* try to assign DACs to pins and return the resultant badness */
3095 static int alc_auto_fill_dacs(struct hda_codec *codec, int num_outs,
3096 const hda_nid_t *pins, hda_nid_t *dacs,
3097 const struct badness_table *bad)
3098 {
3099 struct alc_spec *spec = codec->spec;
3100 struct auto_pin_cfg *cfg = &spec->autocfg;
3101 int i, j;
3102 int badness = 0;
3103 hda_nid_t dac;
3104
3105 if (!num_outs)
3106 return 0;
3107
3108 for (i = 0; i < num_outs; i++) {
3109 hda_nid_t pin = pins[i];
3110 if (!dacs[i])
3111 dacs[i] = alc_auto_look_for_dac(codec, pin);
3112 if (!dacs[i] && !i) {
3113 for (j = 1; j < num_outs; j++) {
3114 if (alc_auto_is_dac_reachable(codec, pin, dacs[j])) {
3115 dacs[0] = dacs[j];
3116 dacs[j] = 0;
3117 break;
3118 }
3119 }
3120 }
3121 dac = dacs[i];
3122 if (!dac) {
3123 if (alc_auto_is_dac_reachable(codec, pin, dacs[0]))
3124 dac = dacs[0];
3125 else if (cfg->line_outs > i &&
3126 alc_auto_is_dac_reachable(codec, pin,
3127 spec->private_dac_nids[i]))
3128 dac = spec->private_dac_nids[i];
3129 if (dac) {
3130 if (!i)
3131 badness += bad->shared_primary;
3132 else if (i == 1)
3133 badness += bad->shared_surr;
3134 else
3135 badness += bad->shared_clfe;
3136 } else if (alc_auto_is_dac_reachable(codec, pin,
3137 spec->private_dac_nids[0])) {
3138 dac = spec->private_dac_nids[0];
3139 badness += bad->shared_surr_main;
3140 } else if (!i)
3141 badness += bad->no_primary_dac;
3142 else
3143 badness += bad->no_dac;
3144 }
3145 if (dac)
3146 badness += eval_shared_vol_badness(codec, pin, dac);
3147 }
3148
3149 return badness;
3150 }
3151
3152 static int alc_auto_fill_multi_ios(struct hda_codec *codec,
3153 hda_nid_t reference_pin,
3154 bool hardwired, int offset);
3155
3156 static bool alc_map_singles(struct hda_codec *codec, int outs,
3157 const hda_nid_t *pins, hda_nid_t *dacs)
3158 {
3159 int i;
3160 bool found = false;
3161 for (i = 0; i < outs; i++) {
3162 if (dacs[i])
3163 continue;
3164 dacs[i] = get_dac_if_single(codec, pins[i]);
3165 if (dacs[i])
3166 found = true;
3167 }
3168 return found;
3169 }
3170
3171 /* fill in the dac_nids table from the parsed pin configuration */
3172 static int fill_and_eval_dacs(struct hda_codec *codec,
3173 bool fill_hardwired,
3174 bool fill_mio_first)
3175 {
3176 struct alc_spec *spec = codec->spec;
3177 struct auto_pin_cfg *cfg = &spec->autocfg;
3178 int i, err, badness;
3179
3180 /* set num_dacs once to full for alc_auto_look_for_dac() */
3181 spec->multiout.num_dacs = cfg->line_outs;
3182 spec->multiout.dac_nids = spec->private_dac_nids;
3183 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
3184 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
3185 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
3186 spec->multi_ios = 0;
3187 clear_vol_marks(codec);
3188 badness = 0;
3189
3190 /* fill hard-wired DACs first */
3191 if (fill_hardwired) {
3192 bool mapped;
3193 do {
3194 mapped = alc_map_singles(codec, cfg->line_outs,
3195 cfg->line_out_pins,
3196 spec->private_dac_nids);
3197 mapped |= alc_map_singles(codec, cfg->hp_outs,
3198 cfg->hp_pins,
3199 spec->multiout.hp_out_nid);
3200 mapped |= alc_map_singles(codec, cfg->speaker_outs,
3201 cfg->speaker_pins,
3202 spec->multiout.extra_out_nid);
3203 if (fill_mio_first && cfg->line_outs == 1 &&
3204 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3205 err = alc_auto_fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
3206 if (!err)
3207 mapped = true;
3208 }
3209 } while (mapped);
3210 }
3211
3212 badness += alc_auto_fill_dacs(codec, cfg->line_outs, cfg->line_out_pins,
3213 spec->private_dac_nids,
3214 &main_out_badness);
3215
3216 /* re-count num_dacs and squash invalid entries */
3217 spec->multiout.num_dacs = 0;
3218 for (i = 0; i < cfg->line_outs; i++) {
3219 if (spec->private_dac_nids[i])
3220 spec->multiout.num_dacs++;
3221 else {
3222 memmove(spec->private_dac_nids + i,
3223 spec->private_dac_nids + i + 1,
3224 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
3225 spec->private_dac_nids[cfg->line_outs - 1] = 0;
3226 }
3227 }
3228
3229 if (fill_mio_first &&
3230 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3231 /* try to fill multi-io first */
3232 err = alc_auto_fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
3233 if (err < 0)
3234 return err;
3235 /* we don't count badness at this stage yet */
3236 }
3237
3238 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3239 err = alc_auto_fill_dacs(codec, cfg->hp_outs, cfg->hp_pins,
3240 spec->multiout.hp_out_nid,
3241 &extra_out_badness);
3242 if (err < 0)
3243 return err;
3244 badness += err;
3245 }
3246 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3247 err = alc_auto_fill_dacs(codec, cfg->speaker_outs,
3248 cfg->speaker_pins,
3249 spec->multiout.extra_out_nid,
3250 &extra_out_badness);
3251 if (err < 0)
3252 return err;
3253 badness += err;
3254 }
3255 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3256 err = alc_auto_fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
3257 if (err < 0)
3258 return err;
3259 badness += err;
3260 }
3261 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3262 /* try multi-ios with HP + inputs */
3263 int offset = 0;
3264 if (cfg->line_outs >= 3)
3265 offset = 1;
3266 err = alc_auto_fill_multi_ios(codec, cfg->hp_pins[0], false,
3267 offset);
3268 if (err < 0)
3269 return err;
3270 badness += err;
3271 }
3272
3273 if (spec->multi_ios == 2) {
3274 for (i = 0; i < 2; i++)
3275 spec->private_dac_nids[spec->multiout.num_dacs++] =
3276 spec->multi_io[i].dac;
3277 spec->ext_channel_count = 2;
3278 } else if (spec->multi_ios) {
3279 spec->multi_ios = 0;
3280 badness += BAD_MULTI_IO;
3281 }
3282
3283 return badness;
3284 }
3285
3286 #define DEBUG_BADNESS
3287
3288 #ifdef DEBUG_BADNESS
3289 #define debug_badness snd_printdd
3290 #else
3291 #define debug_badness(...)
3292 #endif
3293
3294 static void debug_show_configs(struct alc_spec *spec, struct auto_pin_cfg *cfg)
3295 {
3296 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
3297 cfg->line_out_pins[0], cfg->line_out_pins[1],
3298 cfg->line_out_pins[2], cfg->line_out_pins[2],
3299 spec->multiout.dac_nids[0],
3300 spec->multiout.dac_nids[1],
3301 spec->multiout.dac_nids[2],
3302 spec->multiout.dac_nids[3]);
3303 if (spec->multi_ios > 0)
3304 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
3305 spec->multi_ios,
3306 spec->multi_io[0].pin, spec->multi_io[1].pin,
3307 spec->multi_io[0].dac, spec->multi_io[1].dac);
3308 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
3309 cfg->hp_pins[0], cfg->hp_pins[1],
3310 cfg->hp_pins[2], cfg->hp_pins[2],
3311 spec->multiout.hp_out_nid[0],
3312 spec->multiout.hp_out_nid[1],
3313 spec->multiout.hp_out_nid[2],
3314 spec->multiout.hp_out_nid[3]);
3315 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
3316 cfg->speaker_pins[0], cfg->speaker_pins[1],
3317 cfg->speaker_pins[2], cfg->speaker_pins[3],
3318 spec->multiout.extra_out_nid[0],
3319 spec->multiout.extra_out_nid[1],
3320 spec->multiout.extra_out_nid[2],
3321 spec->multiout.extra_out_nid[3]);
3322 }
3323
3324 static int alc_auto_fill_dac_nids(struct hda_codec *codec)
3325 {
3326 struct alc_spec *spec = codec->spec;
3327 struct auto_pin_cfg *cfg = &spec->autocfg;
3328 struct auto_pin_cfg *best_cfg;
3329 int best_badness = INT_MAX;
3330 int badness;
3331 bool fill_hardwired = true, fill_mio_first = true;
3332 bool best_wired = true, best_mio = true;
3333 bool hp_spk_swapped = false;
3334
3335 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
3336 if (!best_cfg)
3337 return -ENOMEM;
3338 *best_cfg = *cfg;
3339
3340 for (;;) {
3341 badness = fill_and_eval_dacs(codec, fill_hardwired,
3342 fill_mio_first);
3343 if (badness < 0) {
3344 kfree(best_cfg);
3345 return badness;
3346 }
3347 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
3348 cfg->line_out_type, fill_hardwired, fill_mio_first,
3349 badness);
3350 debug_show_configs(spec, cfg);
3351 if (badness < best_badness) {
3352 best_badness = badness;
3353 *best_cfg = *cfg;
3354 best_wired = fill_hardwired;
3355 best_mio = fill_mio_first;
3356 }
3357 if (!badness)
3358 break;
3359 fill_mio_first = !fill_mio_first;
3360 if (!fill_mio_first)
3361 continue;
3362 fill_hardwired = !fill_hardwired;
3363 if (!fill_hardwired)
3364 continue;
3365 if (hp_spk_swapped)
3366 break;
3367 hp_spk_swapped = true;
3368 if (cfg->speaker_outs > 0 &&
3369 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3370 cfg->hp_outs = cfg->line_outs;
3371 memcpy(cfg->hp_pins, cfg->line_out_pins,
3372 sizeof(cfg->hp_pins));
3373 cfg->line_outs = cfg->speaker_outs;
3374 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3375 sizeof(cfg->speaker_pins));
3376 cfg->speaker_outs = 0;
3377 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3378 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3379 fill_hardwired = true;
3380 continue;
3381 }
3382 if (cfg->hp_outs > 0 &&
3383 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3384 cfg->speaker_outs = cfg->line_outs;
3385 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3386 sizeof(cfg->speaker_pins));
3387 cfg->line_outs = cfg->hp_outs;
3388 memcpy(cfg->line_out_pins, cfg->hp_pins,
3389 sizeof(cfg->hp_pins));
3390 cfg->hp_outs = 0;
3391 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3392 cfg->line_out_type = AUTO_PIN_HP_OUT;
3393 fill_hardwired = true;
3394 continue;
3395 }
3396 break;
3397 }
3398
3399 if (badness) {
3400 *cfg = *best_cfg;
3401 fill_and_eval_dacs(codec, best_wired, best_mio);
3402 }
3403 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
3404 cfg->line_out_type, best_wired, best_mio);
3405 debug_show_configs(spec, cfg);
3406
3407 if (cfg->line_out_pins[0])
3408 spec->vmaster_nid =
3409 alc_look_for_out_vol_nid(codec, cfg->line_out_pins[0],
3410 spec->multiout.dac_nids[0]);
3411
3412 /* clear the bitmap flags for creating controls */
3413 clear_vol_marks(codec);
3414 kfree(best_cfg);
3415 return 0;
3416 }
3417
3418 static int alc_auto_add_vol_ctl(struct hda_codec *codec,
3419 const char *pfx, int cidx,
3420 hda_nid_t nid, unsigned int chs)
3421 {
3422 struct alc_spec *spec = codec->spec;
3423 unsigned int val;
3424 if (!nid)
3425 return 0;
3426 val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT);
3427 if (is_ctl_used(spec->vol_ctls, val) && chs != 2) /* exclude LFE */
3428 return 0;
3429 mark_ctl_usage(spec->vol_ctls, val);
3430 return __add_pb_vol_ctrl(codec->spec, ALC_CTL_WIDGET_VOL, pfx, cidx,
3431 val);
3432 }
3433
3434 static int alc_auto_add_stereo_vol(struct hda_codec *codec,
3435 const char *pfx, int cidx,
3436 hda_nid_t nid)
3437 {
3438 int chs = 1;
3439 if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
3440 chs = 3;
3441 return alc_auto_add_vol_ctl(codec, pfx, cidx, nid, chs);
3442 }
3443
3444 /* create a mute-switch for the given mixer widget;
3445 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
3446 */
3447 static int alc_auto_add_sw_ctl(struct hda_codec *codec,
3448 const char *pfx, int cidx,
3449 hda_nid_t nid, unsigned int chs)
3450 {
3451 struct alc_spec *spec = codec->spec;
3452 int wid_type;
3453 int type;
3454 unsigned long val;
3455 if (!nid)
3456 return 0;
3457 wid_type = get_wcaps_type(get_wcaps(codec, nid));
3458 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT) {
3459 type = ALC_CTL_WIDGET_MUTE;
3460 val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT);
3461 } else if (snd_hda_get_num_conns(codec, nid) == 1) {
3462 type = ALC_CTL_WIDGET_MUTE;
3463 val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_INPUT);
3464 } else {
3465 type = ALC_CTL_BIND_MUTE;
3466 val = HDA_COMPOSE_AMP_VAL(nid, chs, 2, HDA_INPUT);
3467 }
3468 if (is_ctl_used(spec->sw_ctls, val) && chs != 2) /* exclude LFE */
3469 return 0;
3470 mark_ctl_usage(spec->sw_ctls, val);
3471 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
3472 }
3473
3474 static int alc_auto_add_stereo_sw(struct hda_codec *codec, const char *pfx,
3475 int cidx, hda_nid_t nid)
3476 {
3477 int chs = 1;
3478 if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
3479 chs = 3;
3480 return alc_auto_add_sw_ctl(codec, pfx, cidx, nid, chs);
3481 }
3482
3483 static hda_nid_t alc_look_for_out_mute_nid(struct hda_codec *codec,
3484 hda_nid_t pin, hda_nid_t dac)
3485 {
3486 hda_nid_t mix = alc_auto_dac_to_mix(codec, pin, dac);
3487 if (nid_has_mute(codec, pin, HDA_OUTPUT))
3488 return pin;
3489 else if (mix && nid_has_mute(codec, mix, HDA_INPUT))
3490 return mix;
3491 else if (nid_has_mute(codec, dac, HDA_OUTPUT))
3492 return dac;
3493 return 0;
3494 }
3495
3496 static hda_nid_t alc_look_for_out_vol_nid(struct hda_codec *codec,
3497 hda_nid_t pin, hda_nid_t dac)
3498 {
3499 hda_nid_t mix = alc_auto_dac_to_mix(codec, pin, dac);
3500 if (nid_has_volume(codec, dac, HDA_OUTPUT))
3501 return dac;
3502 else if (nid_has_volume(codec, mix, HDA_OUTPUT))
3503 return mix;
3504 else if (nid_has_volume(codec, pin, HDA_OUTPUT))
3505 return pin;
3506 return 0;
3507 }
3508
3509 /* add playback controls from the parsed DAC table */
3510 static int alc_auto_create_multi_out_ctls(struct hda_codec *codec,
3511 const struct auto_pin_cfg *cfg)
3512 {
3513 struct alc_spec *spec = codec->spec;
3514 int i, err, noutputs;
3515
3516 noutputs = cfg->line_outs;
3517 if (spec->multi_ios > 0 && cfg->line_outs < 3)
3518 noutputs += spec->multi_ios;
3519
3520 for (i = 0; i < noutputs; i++) {
3521 const char *name;
3522 int index;
3523 hda_nid_t dac, pin;
3524 hda_nid_t sw, vol;
3525
3526 dac = spec->multiout.dac_nids[i];
3527 if (!dac)
3528 continue;
3529 if (i >= cfg->line_outs) {
3530 pin = spec->multi_io[i - 1].pin;
3531 index = 0;
3532 name = channel_name[i];
3533 } else {
3534 pin = cfg->line_out_pins[i];
3535 name = alc_get_line_out_pfx(spec, i, true, &index);
3536 }
3537
3538 sw = alc_look_for_out_mute_nid(codec, pin, dac);
3539 vol = alc_look_for_out_vol_nid(codec, pin, dac);
3540 if (!name || !strcmp(name, "CLFE")) {
3541 /* Center/LFE */
3542 err = alc_auto_add_vol_ctl(codec, "Center", 0, vol, 1);
3543 if (err < 0)
3544 return err;
3545 err = alc_auto_add_vol_ctl(codec, "LFE", 0, vol, 2);
3546 if (err < 0)
3547 return err;
3548 err = alc_auto_add_sw_ctl(codec, "Center", 0, sw, 1);
3549 if (err < 0)
3550 return err;
3551 err = alc_auto_add_sw_ctl(codec, "LFE", 0, sw, 2);
3552 if (err < 0)
3553 return err;
3554 } else {
3555 err = alc_auto_add_stereo_vol(codec, name, index, vol);
3556 if (err < 0)
3557 return err;
3558 err = alc_auto_add_stereo_sw(codec, name, index, sw);
3559 if (err < 0)
3560 return err;
3561 }
3562 }
3563 return 0;
3564 }
3565
3566 static int alc_auto_create_extra_out(struct hda_codec *codec, hda_nid_t pin,
3567 hda_nid_t dac, const char *pfx,
3568 int cidx)
3569 {
3570 struct alc_spec *spec = codec->spec;
3571 hda_nid_t sw, vol;
3572 int err;
3573
3574 if (!dac) {
3575 unsigned int val;
3576 /* the corresponding DAC is already occupied */
3577 if (!(get_wcaps(codec, pin) & AC_WCAP_OUT_AMP))
3578 return 0; /* no way */
3579 /* create a switch only */
3580 val = HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT);
3581 if (is_ctl_used(spec->sw_ctls, val))
3582 return 0; /* already created */
3583 mark_ctl_usage(spec->sw_ctls, val);
3584 return __add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, pfx, cidx, val);
3585 }
3586
3587 sw = alc_look_for_out_mute_nid(codec, pin, dac);
3588 vol = alc_look_for_out_vol_nid(codec, pin, dac);
3589 err = alc_auto_add_stereo_vol(codec, pfx, cidx, vol);
3590 if (err < 0)
3591 return err;
3592 err = alc_auto_add_stereo_sw(codec, pfx, cidx, sw);
3593 if (err < 0)
3594 return err;
3595 return 0;
3596 }
3597
3598 static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
3599 unsigned int nums,
3600 struct hda_ctl_ops *ops)
3601 {
3602 struct alc_spec *spec = codec->spec;
3603 struct hda_bind_ctls **ctlp, *ctl;
3604 snd_array_init(&spec->bind_ctls, sizeof(ctl), 8);
3605 ctlp = snd_array_new(&spec->bind_ctls);
3606 if (!ctlp)
3607 return NULL;
3608 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
3609 *ctlp = ctl;
3610 if (ctl)
3611 ctl->ops = ops;
3612 return ctl;
3613 }
3614
3615 /* add playback controls for speaker and HP outputs */
3616 static int alc_auto_create_extra_outs(struct hda_codec *codec, int num_pins,
3617 const hda_nid_t *pins,
3618 const hda_nid_t *dacs,
3619 const char *pfx)
3620 {
3621 struct alc_spec *spec = codec->spec;
3622 struct hda_bind_ctls *ctl;
3623 char name[32];
3624 int i, n, err;
3625
3626 if (!num_pins || !pins[0])
3627 return 0;
3628
3629 if (num_pins == 1) {
3630 hda_nid_t dac = *dacs;
3631 if (!dac)
3632 dac = spec->multiout.dac_nids[0];
3633 return alc_auto_create_extra_out(codec, *pins, dac, pfx, 0);
3634 }
3635
3636 for (i = 0; i < num_pins; i++) {
3637 hda_nid_t dac;
3638 if (dacs[num_pins - 1])
3639 dac = dacs[i]; /* with individual volumes */
3640 else
3641 dac = 0;
3642 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
3643 err = alc_auto_create_extra_out(codec, pins[i], dac,
3644 "Bass Speaker", 0);
3645 } else if (num_pins >= 3) {
3646 snprintf(name, sizeof(name), "%s %s",
3647 pfx, channel_name[i]);
3648 err = alc_auto_create_extra_out(codec, pins[i], dac,
3649 name, 0);
3650 } else {
3651 err = alc_auto_create_extra_out(codec, pins[i], dac,
3652 pfx, i);
3653 }
3654 if (err < 0)
3655 return err;
3656 }
3657 if (dacs[num_pins - 1])
3658 return 0;
3659
3660 /* Let's create a bind-controls for volumes */
3661 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
3662 if (!ctl)
3663 return -ENOMEM;
3664 n = 0;
3665 for (i = 0; i < num_pins; i++) {
3666 hda_nid_t vol;
3667 if (!pins[i] || !dacs[i])
3668 continue;
3669 vol = alc_look_for_out_vol_nid(codec, pins[i], dacs[i]);
3670 if (vol)
3671 ctl->values[n++] =
3672 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
3673 }
3674 if (n) {
3675 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
3676 err = add_control(spec, ALC_CTL_BIND_VOL, name, 0, (long)ctl);
3677 if (err < 0)
3678 return err;
3679 }
3680 return 0;
3681 }
3682
3683 static int alc_auto_create_hp_out(struct hda_codec *codec)
3684 {
3685 struct alc_spec *spec = codec->spec;
3686 return alc_auto_create_extra_outs(codec, spec->autocfg.hp_outs,
3687 spec->autocfg.hp_pins,
3688 spec->multiout.hp_out_nid,
3689 "Headphone");
3690 }
3691
3692 static int alc_auto_create_speaker_out(struct hda_codec *codec)
3693 {
3694 struct alc_spec *spec = codec->spec;
3695 return alc_auto_create_extra_outs(codec, spec->autocfg.speaker_outs,
3696 spec->autocfg.speaker_pins,
3697 spec->multiout.extra_out_nid,
3698 "Speaker");
3699 }
3700
3701 static void alc_auto_set_output_and_unmute(struct hda_codec *codec,
3702 hda_nid_t pin, int pin_type,
3703 hda_nid_t dac)
3704 {
3705 int i, num;
3706 hda_nid_t nid, mix = 0;
3707 hda_nid_t srcs[HDA_MAX_CONNECTIONS];
3708
3709 alc_set_pin_output(codec, pin, pin_type);
3710 nid = alc_go_down_to_selector(codec, pin);
3711 num = snd_hda_get_connections(codec, nid, srcs, ARRAY_SIZE(srcs));
3712 for (i = 0; i < num; i++) {
3713 if (alc_auto_mix_to_dac(codec, srcs[i]) != dac)
3714 continue;
3715 mix = srcs[i];
3716 break;
3717 }
3718 if (!mix)
3719 return;
3720
3721 /* need the manual connection? */
3722 if (num > 1)
3723 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, i);
3724 /* unmute mixer widget inputs */
3725 if (nid_has_mute(codec, mix, HDA_INPUT)) {
3726 snd_hda_codec_write(codec, mix, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3727 AMP_IN_UNMUTE(0));
3728 snd_hda_codec_write(codec, mix, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3729 AMP_IN_UNMUTE(1));
3730 }
3731 /* initialize volume */
3732 nid = alc_look_for_out_vol_nid(codec, pin, dac);
3733 if (nid)
3734 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3735 AMP_OUT_ZERO);
3736
3737 /* unmute DAC if it's not assigned to a mixer */
3738 nid = alc_look_for_out_mute_nid(codec, pin, dac);
3739 if (nid == mix && nid_has_mute(codec, dac, HDA_OUTPUT))
3740 snd_hda_codec_write(codec, dac, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3741 AMP_OUT_ZERO);
3742 }
3743
3744 static void alc_auto_init_multi_out(struct hda_codec *codec)
3745 {
3746 struct alc_spec *spec = codec->spec;
3747 int pin_type = get_pin_type(spec->autocfg.line_out_type);
3748 int i;
3749
3750 for (i = 0; i <= HDA_SIDE; i++) {
3751 hda_nid_t nid = spec->autocfg.line_out_pins[i];
3752 if (nid)
3753 alc_auto_set_output_and_unmute(codec, nid, pin_type,
3754 spec->multiout.dac_nids[i]);
3755 }
3756 }
3757
3758 static void alc_auto_init_extra_out(struct hda_codec *codec)
3759 {
3760 struct alc_spec *spec = codec->spec;
3761 int i;
3762 hda_nid_t pin, dac;
3763
3764 for (i = 0; i < spec->autocfg.hp_outs; i++) {
3765 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3766 break;
3767 pin = spec->autocfg.hp_pins[i];
3768 if (!pin)
3769 break;
3770 dac = spec->multiout.hp_out_nid[i];
3771 if (!dac) {
3772 if (i > 0 && spec->multiout.hp_out_nid[0])
3773 dac = spec->multiout.hp_out_nid[0];
3774 else
3775 dac = spec->multiout.dac_nids[0];
3776 }
3777 alc_auto_set_output_and_unmute(codec, pin, PIN_HP, dac);
3778 }
3779 for (i = 0; i < spec->autocfg.speaker_outs; i++) {
3780 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3781 break;
3782 pin = spec->autocfg.speaker_pins[i];
3783 if (!pin)
3784 break;
3785 dac = spec->multiout.extra_out_nid[i];
3786 if (!dac) {
3787 if (i > 0 && spec->multiout.extra_out_nid[0])
3788 dac = spec->multiout.extra_out_nid[0];
3789 else
3790 dac = spec->multiout.dac_nids[0];
3791 }
3792 alc_auto_set_output_and_unmute(codec, pin, PIN_OUT, dac);
3793 }
3794 }
3795
3796 /* check whether the given pin can be a multi-io pin */
3797 static bool can_be_multiio_pin(struct hda_codec *codec,
3798 unsigned int location, hda_nid_t nid)
3799 {
3800 unsigned int defcfg, caps;
3801
3802 defcfg = snd_hda_codec_get_pincfg(codec, nid);
3803 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
3804 return false;
3805 if (location && get_defcfg_location(defcfg) != location)
3806 return false;
3807 caps = snd_hda_query_pin_caps(codec, nid);
3808 if (!(caps & AC_PINCAP_OUT))
3809 return false;
3810 return true;
3811 }
3812
3813 /*
3814 * multi-io helper
3815 *
3816 * When hardwired is set, try to fill ony hardwired pins, and returns
3817 * zero if any pins are filled, non-zero if nothing found.
3818 * When hardwired is off, try to fill possible input pins, and returns
3819 * the badness value.
3820 */
3821 static int alc_auto_fill_multi_ios(struct hda_codec *codec,
3822 hda_nid_t reference_pin,
3823 bool hardwired, int offset)
3824 {
3825 struct alc_spec *spec = codec->spec;
3826 struct auto_pin_cfg *cfg = &spec->autocfg;
3827 int type, i, j, dacs, num_pins, old_pins;
3828 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
3829 unsigned int location = get_defcfg_location(defcfg);
3830 int badness = 0;
3831
3832 old_pins = spec->multi_ios;
3833 if (old_pins >= 2)
3834 goto end_fill;
3835
3836 num_pins = 0;
3837 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
3838 for (i = 0; i < cfg->num_inputs; i++) {
3839 if (cfg->inputs[i].type != type)
3840 continue;
3841 if (can_be_multiio_pin(codec, location,
3842 cfg->inputs[i].pin))
3843 num_pins++;
3844 }
3845 }
3846 if (num_pins < 2)
3847 goto end_fill;
3848
3849 dacs = spec->multiout.num_dacs;
3850 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
3851 for (i = 0; i < cfg->num_inputs; i++) {
3852 hda_nid_t nid = cfg->inputs[i].pin;
3853 hda_nid_t dac = 0;
3854
3855 if (cfg->inputs[i].type != type)
3856 continue;
3857 if (!can_be_multiio_pin(codec, location, nid))
3858 continue;
3859 for (j = 0; j < spec->multi_ios; j++) {
3860 if (nid == spec->multi_io[j].pin)
3861 break;
3862 }
3863 if (j < spec->multi_ios)
3864 continue;
3865
3866 if (offset && offset + spec->multi_ios < dacs) {
3867 dac = spec->private_dac_nids[offset + spec->multi_ios];
3868 if (!alc_auto_is_dac_reachable(codec, nid, dac))
3869 dac = 0;
3870 }
3871 if (hardwired)
3872 dac = get_dac_if_single(codec, nid);
3873 else if (!dac)
3874 dac = alc_auto_look_for_dac(codec, nid);
3875 if (!dac) {
3876 badness++;
3877 continue;
3878 }
3879 spec->multi_io[spec->multi_ios].pin = nid;
3880 spec->multi_io[spec->multi_ios].dac = dac;
3881 spec->multi_ios++;
3882 if (spec->multi_ios >= 2)
3883 break;
3884 }
3885 }
3886 end_fill:
3887 if (badness)
3888 badness = BAD_MULTI_IO;
3889 if (old_pins == spec->multi_ios) {
3890 if (hardwired)
3891 return 1; /* nothing found */
3892 else
3893 return badness; /* no badness if nothing found */
3894 }
3895 if (!hardwired && spec->multi_ios < 2) {
3896 spec->multi_ios = old_pins;
3897 return badness;
3898 }
3899
3900 return 0;
3901 }
3902
3903 static int alc_auto_ch_mode_info(struct snd_kcontrol *kcontrol,
3904 struct snd_ctl_elem_info *uinfo)
3905 {
3906 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3907 struct alc_spec *spec = codec->spec;
3908
3909 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3910 uinfo->count = 1;
3911 uinfo->value.enumerated.items = spec->multi_ios + 1;
3912 if (uinfo->value.enumerated.item > spec->multi_ios)
3913 uinfo->value.enumerated.item = spec->multi_ios;
3914 sprintf(uinfo->value.enumerated.name, "%dch",
3915 (uinfo->value.enumerated.item + 1) * 2);
3916 return 0;
3917 }
3918
3919 static int alc_auto_ch_mode_get(struct snd_kcontrol *kcontrol,
3920 struct snd_ctl_elem_value *ucontrol)
3921 {
3922 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3923 struct alc_spec *spec = codec->spec;
3924 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
3925 return 0;
3926 }
3927
3928 static int alc_set_multi_io(struct hda_codec *codec, int idx, bool output)
3929 {
3930 struct alc_spec *spec = codec->spec;
3931 hda_nid_t nid = spec->multi_io[idx].pin;
3932
3933 if (!spec->multi_io[idx].ctl_in)
3934 spec->multi_io[idx].ctl_in =
3935 snd_hda_codec_read(codec, nid, 0,
3936 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3937 if (output) {
3938 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
3939 if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
3940 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3941 HDA_AMP_MUTE, 0);
3942 alc_auto_select_dac(codec, nid, spec->multi_io[idx].dac);
3943 } else {
3944 if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
3945 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3946 HDA_AMP_MUTE, HDA_AMP_MUTE);
3947 snd_hda_set_pin_ctl_cache(codec, nid,
3948 spec->multi_io[idx].ctl_in);
3949 }
3950 return 0;
3951 }
3952
3953 static int alc_auto_ch_mode_put(struct snd_kcontrol *kcontrol,
3954 struct snd_ctl_elem_value *ucontrol)
3955 {
3956 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3957 struct alc_spec *spec = codec->spec;
3958 int i, ch;
3959
3960 ch = ucontrol->value.enumerated.item[0];
3961 if (ch < 0 || ch > spec->multi_ios)
3962 return -EINVAL;
3963 if (ch == (spec->ext_channel_count - 1) / 2)
3964 return 0;
3965 spec->ext_channel_count = (ch + 1) * 2;
3966 for (i = 0; i < spec->multi_ios; i++)
3967 alc_set_multi_io(codec, i, i < ch);
3968 spec->multiout.max_channels = spec->ext_channel_count;
3969 if (spec->need_dac_fix && !spec->const_channel_count)
3970 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
3971 return 1;
3972 }
3973
3974 static const struct snd_kcontrol_new alc_auto_channel_mode_enum = {
3975 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3976 .name = "Channel Mode",
3977 .info = alc_auto_ch_mode_info,
3978 .get = alc_auto_ch_mode_get,
3979 .put = alc_auto_ch_mode_put,
3980 };
3981
3982 static int alc_auto_add_multi_channel_mode(struct hda_codec *codec)
3983 {
3984 struct alc_spec *spec = codec->spec;
3985
3986 if (spec->multi_ios > 0) {
3987 struct snd_kcontrol_new *knew;
3988
3989 knew = alc_kcontrol_new(spec);
3990 if (!knew)
3991 return -ENOMEM;
3992 *knew = alc_auto_channel_mode_enum;
3993 knew->name = kstrdup("Channel Mode", GFP_KERNEL);
3994 if (!knew->name)
3995 return -ENOMEM;
3996 }
3997 return 0;
3998 }
3999
4000 /* filter out invalid adc_nids (and capsrc_nids) that don't give all
4001 * active input pins
4002 */
4003 static void alc_remove_invalid_adc_nids(struct hda_codec *codec)
4004 {
4005 struct alc_spec *spec = codec->spec;
4006 const struct hda_input_mux *imux;
4007 hda_nid_t adc_nids[ARRAY_SIZE(spec->private_adc_nids)];
4008 hda_nid_t capsrc_nids[ARRAY_SIZE(spec->private_adc_nids)];
4009 int i, n, nums;
4010
4011 imux = spec->input_mux;
4012 if (!imux)
4013 return;
4014 if (spec->dyn_adc_switch)
4015 return;
4016
4017 again:
4018 nums = 0;
4019 for (n = 0; n < spec->num_adc_nids; n++) {
4020 hda_nid_t cap = spec->private_capsrc_nids[n];
4021 int num_conns = snd_hda_get_num_conns(codec, cap);
4022 for (i = 0; i < imux->num_items; i++) {
4023 hda_nid_t pin = spec->imux_pins[i];
4024 if (pin) {
4025 if (get_connection_index(codec, cap, pin) < 0)
4026 break;
4027 } else if (num_conns <= imux->items[i].index)
4028 break;
4029 }
4030 if (i >= imux->num_items) {
4031 adc_nids[nums] = spec->private_adc_nids[n];
4032 capsrc_nids[nums++] = cap;
4033 }
4034 }
4035 if (!nums) {
4036 /* check whether ADC-switch is possible */
4037 if (!alc_check_dyn_adc_switch(codec)) {
4038 if (spec->shared_mic_hp) {
4039 spec->shared_mic_hp = 0;
4040 spec->private_imux[0].num_items = 1;
4041 goto again;
4042 }
4043 printk(KERN_WARNING "hda_codec: %s: no valid ADC found;"
4044 " using fallback 0x%x\n",
4045 codec->chip_name, spec->private_adc_nids[0]);
4046 spec->num_adc_nids = 1;
4047 spec->auto_mic = 0;
4048 return;
4049 }
4050 } else if (nums != spec->num_adc_nids) {
4051 memcpy(spec->private_adc_nids, adc_nids,
4052 nums * sizeof(hda_nid_t));
4053 memcpy(spec->private_capsrc_nids, capsrc_nids,
4054 nums * sizeof(hda_nid_t));
4055 spec->num_adc_nids = nums;
4056 }
4057
4058 if (spec->auto_mic)
4059 alc_auto_mic_check_imux(codec); /* check auto-mic setups */
4060 else if (spec->input_mux->num_items == 1 || spec->shared_mic_hp)
4061 spec->num_adc_nids = 1; /* reduce to a single ADC */
4062 }
4063
4064 /*
4065 * initialize ADC paths
4066 */
4067 static void alc_auto_init_adc(struct hda_codec *codec, int adc_idx)
4068 {
4069 struct alc_spec *spec = codec->spec;
4070 hda_nid_t nid;
4071
4072 nid = spec->adc_nids[adc_idx];
4073 /* mute ADC */
4074 if (nid_has_mute(codec, nid, HDA_INPUT)) {
4075 snd_hda_codec_write(codec, nid, 0,
4076 AC_VERB_SET_AMP_GAIN_MUTE,
4077 AMP_IN_MUTE(0));
4078 return;
4079 }
4080 if (!spec->capsrc_nids)
4081 return;
4082 nid = spec->capsrc_nids[adc_idx];
4083 if (nid_has_mute(codec, nid, HDA_OUTPUT))
4084 snd_hda_codec_write(codec, nid, 0,
4085 AC_VERB_SET_AMP_GAIN_MUTE,
4086 AMP_OUT_MUTE);
4087 }
4088
4089 static void alc_auto_init_input_src(struct hda_codec *codec)
4090 {
4091 struct alc_spec *spec = codec->spec;
4092 int c, nums;
4093
4094 for (c = 0; c < spec->num_adc_nids; c++)
4095 alc_auto_init_adc(codec, c);
4096 if (spec->dyn_adc_switch)
4097 nums = 1;
4098 else
4099 nums = spec->num_adc_nids;
4100 for (c = 0; c < nums; c++)
4101 alc_mux_select(codec, c, spec->cur_mux[c], true);
4102 }
4103
4104 /* add mic boosts if needed */
4105 static int alc_auto_add_mic_boost(struct hda_codec *codec)
4106 {
4107 struct alc_spec *spec = codec->spec;
4108 struct auto_pin_cfg *cfg = &spec->autocfg;
4109 int i, err;
4110 int type_idx = 0;
4111 hda_nid_t nid;
4112 const char *prev_label = NULL;
4113
4114 for (i = 0; i < cfg->num_inputs; i++) {
4115 if (cfg->inputs[i].type > AUTO_PIN_MIC)
4116 break;
4117 nid = cfg->inputs[i].pin;
4118 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
4119 const char *label;
4120 char boost_label[32];
4121
4122 label = hda_get_autocfg_input_label(codec, cfg, i);
4123 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
4124 label = "Headphone Mic";
4125 if (prev_label && !strcmp(label, prev_label))
4126 type_idx++;
4127 else
4128 type_idx = 0;
4129 prev_label = label;
4130
4131 snprintf(boost_label, sizeof(boost_label),
4132 "%s Boost Volume", label);
4133 err = add_control(spec, ALC_CTL_WIDGET_VOL,
4134 boost_label, type_idx,
4135 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT));
4136 if (err < 0)
4137 return err;
4138 }
4139 }
4140 return 0;
4141 }
4142
4143 /* select or unmute the given capsrc route */
4144 static void select_or_unmute_capsrc(struct hda_codec *codec, hda_nid_t cap,
4145 int idx)
4146 {
4147 if (get_wcaps_type(get_wcaps(codec, cap)) == AC_WID_AUD_MIX) {
4148 snd_hda_codec_amp_stereo(codec, cap, HDA_INPUT, idx,
4149 HDA_AMP_MUTE, 0);
4150 } else if (snd_hda_get_num_conns(codec, cap) > 1) {
4151 snd_hda_codec_write_cache(codec, cap, 0,
4152 AC_VERB_SET_CONNECT_SEL, idx);
4153 }
4154 }
4155
4156 /* set the default connection to that pin */
4157 static int init_capsrc_for_pin(struct hda_codec *codec, hda_nid_t pin)
4158 {
4159 struct alc_spec *spec = codec->spec;
4160 int i;
4161
4162 if (!pin)
4163 return 0;
4164 for (i = 0; i < spec->num_adc_nids; i++) {
4165 hda_nid_t cap = get_capsrc(spec, i);
4166 int idx;
4167
4168 idx = get_connection_index(codec, cap, pin);
4169 if (idx < 0)
4170 continue;
4171 select_or_unmute_capsrc(codec, cap, idx);
4172 return i; /* return the found index */
4173 }
4174 return -1; /* not found */
4175 }
4176
4177 /* initialize some special cases for input sources */
4178 static void alc_init_special_input_src(struct hda_codec *codec)
4179 {
4180 struct alc_spec *spec = codec->spec;
4181 int i;
4182
4183 for (i = 0; i < spec->autocfg.num_inputs; i++)
4184 init_capsrc_for_pin(codec, spec->autocfg.inputs[i].pin);
4185 }
4186
4187 /* assign appropriate capture mixers */
4188 static void set_capture_mixer(struct hda_codec *codec)
4189 {
4190 struct alc_spec *spec = codec->spec;
4191 static const struct snd_kcontrol_new *caps[2][3] = {
4192 { alc_capture_mixer_nosrc1,
4193 alc_capture_mixer_nosrc2,
4194 alc_capture_mixer_nosrc3 },
4195 { alc_capture_mixer1,
4196 alc_capture_mixer2,
4197 alc_capture_mixer3 },
4198 };
4199
4200 /* check whether either of ADC or MUX has a volume control */
4201 if (!nid_has_volume(codec, spec->adc_nids[0], HDA_INPUT)) {
4202 if (!spec->capsrc_nids)
4203 return; /* no volume */
4204 if (!nid_has_volume(codec, spec->capsrc_nids[0], HDA_OUTPUT))
4205 return; /* no volume in capsrc, too */
4206 spec->vol_in_capsrc = 1;
4207 }
4208
4209 if (spec->num_adc_nids > 0) {
4210 int mux = 0;
4211 int num_adcs = 0;
4212
4213 if (spec->input_mux && spec->input_mux->num_items > 1)
4214 mux = 1;
4215 if (spec->auto_mic) {
4216 num_adcs = 1;
4217 mux = 0;
4218 } else if (spec->dyn_adc_switch)
4219 num_adcs = 1;
4220 if (!num_adcs) {
4221 if (spec->num_adc_nids > 3)
4222 spec->num_adc_nids = 3;
4223 else if (!spec->num_adc_nids)
4224 return;
4225 num_adcs = spec->num_adc_nids;
4226 }
4227 spec->cap_mixer = caps[mux][num_adcs - 1];
4228 }
4229 }
4230
4231 /*
4232 * standard auto-parser initializations
4233 */
4234 static void alc_auto_init_std(struct hda_codec *codec)
4235 {
4236 struct alc_spec *spec = codec->spec;
4237 alc_auto_init_multi_out(codec);
4238 alc_auto_init_extra_out(codec);
4239 alc_auto_init_analog_input(codec);
4240 alc_auto_init_input_src(codec);
4241 alc_auto_init_digital(codec);
4242 if (spec->unsol_event)
4243 alc_inithook(codec);
4244 }
4245
4246 /*
4247 * Digital-beep handlers
4248 */
4249 #ifdef CONFIG_SND_HDA_INPUT_BEEP
4250 #define set_beep_amp(spec, nid, idx, dir) \
4251 ((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir))
4252
4253 static const struct snd_pci_quirk beep_white_list[] = {
4254 SND_PCI_QUIRK(0x1043, 0x829f, "ASUS", 1),
4255 SND_PCI_QUIRK(0x1043, 0x83ce, "EeePC", 1),
4256 SND_PCI_QUIRK(0x1043, 0x831a, "EeePC", 1),
4257 SND_PCI_QUIRK(0x1043, 0x834a, "EeePC", 1),
4258 SND_PCI_QUIRK(0x1458, 0xa002, "GA-MA790X", 1),
4259 SND_PCI_QUIRK(0x8086, 0xd613, "Intel", 1),
4260 {}
4261 };
4262
4263 static inline int has_cdefine_beep(struct hda_codec *codec)
4264 {
4265 struct alc_spec *spec = codec->spec;
4266 const struct snd_pci_quirk *q;
4267 q = snd_pci_quirk_lookup(codec->bus->pci, beep_white_list);
4268 if (q)
4269 return q->value;
4270 return spec->cdefine.enable_pcbeep;
4271 }
4272 #else
4273 #define set_beep_amp(spec, nid, idx, dir) /* NOP */
4274 #define has_cdefine_beep(codec) 0
4275 #endif
4276
4277 /* parse the BIOS configuration and set up the alc_spec */
4278 /* return 1 if successful, 0 if the proper config is not found,
4279 * or a negative error code
4280 */
4281 static int alc_parse_auto_config(struct hda_codec *codec,
4282 const hda_nid_t *ignore_nids,
4283 const hda_nid_t *ssid_nids)
4284 {
4285 struct alc_spec *spec = codec->spec;
4286 struct auto_pin_cfg *cfg = &spec->autocfg;
4287 int err;
4288
4289 err = snd_hda_parse_pin_defcfg(codec, cfg, ignore_nids,
4290 spec->parse_flags);
4291 if (err < 0)
4292 return err;
4293 if (!cfg->line_outs) {
4294 if (cfg->dig_outs || cfg->dig_in_pin) {
4295 spec->multiout.max_channels = 2;
4296 spec->no_analog = 1;
4297 goto dig_only;
4298 }
4299 return 0; /* can't find valid BIOS pin config */
4300 }
4301
4302 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4303 cfg->line_outs <= cfg->hp_outs) {
4304 /* use HP as primary out */
4305 cfg->speaker_outs = cfg->line_outs;
4306 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4307 sizeof(cfg->speaker_pins));
4308 cfg->line_outs = cfg->hp_outs;
4309 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4310 cfg->hp_outs = 0;
4311 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4312 cfg->line_out_type = AUTO_PIN_HP_OUT;
4313 }
4314
4315 err = alc_auto_fill_dac_nids(codec);
4316 if (err < 0)
4317 return err;
4318 err = alc_auto_add_multi_channel_mode(codec);
4319 if (err < 0)
4320 return err;
4321 err = alc_auto_create_multi_out_ctls(codec, cfg);
4322 if (err < 0)
4323 return err;
4324 err = alc_auto_create_hp_out(codec);
4325 if (err < 0)
4326 return err;
4327 err = alc_auto_create_speaker_out(codec);
4328 if (err < 0)
4329 return err;
4330 err = alc_auto_create_shared_input(codec);
4331 if (err < 0)
4332 return err;
4333 err = alc_auto_create_input_ctls(codec);
4334 if (err < 0)
4335 return err;
4336
4337 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4338
4339 dig_only:
4340 alc_auto_parse_digital(codec);
4341
4342 if (!spec->no_analog)
4343 alc_remove_invalid_adc_nids(codec);
4344
4345 if (ssid_nids)
4346 alc_ssid_check(codec, ssid_nids);
4347
4348 if (!spec->no_analog) {
4349 alc_auto_check_switches(codec);
4350 err = alc_auto_add_mic_boost(codec);
4351 if (err < 0)
4352 return err;
4353 }
4354
4355 if (spec->kctls.list)
4356 add_mixer(spec, spec->kctls.list);
4357
4358 if (!spec->no_analog && !spec->cap_mixer)
4359 set_capture_mixer(codec);
4360
4361 return 1;
4362 }
4363
4364 /* common preparation job for alc_spec */
4365 static int alc_alloc_spec(struct hda_codec *codec, hda_nid_t mixer_nid)
4366 {
4367 struct alc_spec *spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4368 int err;
4369
4370 if (!spec)
4371 return -ENOMEM;
4372 codec->spec = spec;
4373 spec->mixer_nid = mixer_nid;
4374
4375 err = alc_codec_rename_from_preset(codec);
4376 if (err < 0) {
4377 kfree(spec);
4378 return err;
4379 }
4380 return 0;
4381 }
4382
4383 static int alc880_parse_auto_config(struct hda_codec *codec)
4384 {
4385 static const hda_nid_t alc880_ignore[] = { 0x1d, 0 };
4386 static const hda_nid_t alc880_ssids[] = { 0x15, 0x1b, 0x14, 0 };
4387 return alc_parse_auto_config(codec, alc880_ignore, alc880_ssids);
4388 }
4389
4390 /*
4391 * ALC880 fix-ups
4392 */
4393 enum {
4394 ALC880_FIXUP_GPIO1,
4395 ALC880_FIXUP_GPIO2,
4396 ALC880_FIXUP_MEDION_RIM,
4397 ALC880_FIXUP_LG,
4398 ALC880_FIXUP_W810,
4399 ALC880_FIXUP_EAPD_COEF,
4400 ALC880_FIXUP_TCL_S700,
4401 ALC880_FIXUP_VOL_KNOB,
4402 ALC880_FIXUP_FUJITSU,
4403 ALC880_FIXUP_F1734,
4404 ALC880_FIXUP_UNIWILL,
4405 ALC880_FIXUP_UNIWILL_DIG,
4406 ALC880_FIXUP_Z71V,
4407 ALC880_FIXUP_3ST_BASE,
4408 ALC880_FIXUP_3ST,
4409 ALC880_FIXUP_3ST_DIG,
4410 ALC880_FIXUP_5ST_BASE,
4411 ALC880_FIXUP_5ST,
4412 ALC880_FIXUP_5ST_DIG,
4413 ALC880_FIXUP_6ST_BASE,
4414 ALC880_FIXUP_6ST,
4415 ALC880_FIXUP_6ST_DIG,
4416 };
4417
4418 /* enable the volume-knob widget support on NID 0x21 */
4419 static void alc880_fixup_vol_knob(struct hda_codec *codec,
4420 const struct alc_fixup *fix, int action)
4421 {
4422 if (action == ALC_FIXUP_ACT_PROBE)
4423 snd_hda_jack_detect_enable(codec, 0x21, ALC_DCVOL_EVENT);
4424 }
4425
4426 static const struct alc_fixup alc880_fixups[] = {
4427 [ALC880_FIXUP_GPIO1] = {
4428 .type = ALC_FIXUP_VERBS,
4429 .v.verbs = alc_gpio1_init_verbs,
4430 },
4431 [ALC880_FIXUP_GPIO2] = {
4432 .type = ALC_FIXUP_VERBS,
4433 .v.verbs = alc_gpio2_init_verbs,
4434 },
4435 [ALC880_FIXUP_MEDION_RIM] = {
4436 .type = ALC_FIXUP_VERBS,
4437 .v.verbs = (const struct hda_verb[]) {
4438 { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4439 { 0x20, AC_VERB_SET_PROC_COEF, 0x3060 },
4440 { }
4441 },
4442 .chained = true,
4443 .chain_id = ALC880_FIXUP_GPIO2,
4444 },
4445 [ALC880_FIXUP_LG] = {
4446 .type = ALC_FIXUP_PINS,
4447 .v.pins = (const struct alc_pincfg[]) {
4448 /* disable bogus unused pins */
4449 { 0x16, 0x411111f0 },
4450 { 0x18, 0x411111f0 },
4451 { 0x1a, 0x411111f0 },
4452 { }
4453 }
4454 },
4455 [ALC880_FIXUP_W810] = {
4456 .type = ALC_FIXUP_PINS,
4457 .v.pins = (const struct alc_pincfg[]) {
4458 /* disable bogus unused pins */
4459 { 0x17, 0x411111f0 },
4460 { }
4461 },
4462 .chained = true,
4463 .chain_id = ALC880_FIXUP_GPIO2,
4464 },
4465 [ALC880_FIXUP_EAPD_COEF] = {
4466 .type = ALC_FIXUP_VERBS,
4467 .v.verbs = (const struct hda_verb[]) {
4468 /* change to EAPD mode */
4469 { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4470 { 0x20, AC_VERB_SET_PROC_COEF, 0x3060 },
4471 {}
4472 },
4473 },
4474 [ALC880_FIXUP_TCL_S700] = {
4475 .type = ALC_FIXUP_VERBS,
4476 .v.verbs = (const struct hda_verb[]) {
4477 /* change to EAPD mode */
4478 { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4479 { 0x20, AC_VERB_SET_PROC_COEF, 0x3070 },
4480 {}
4481 },
4482 .chained = true,
4483 .chain_id = ALC880_FIXUP_GPIO2,
4484 },
4485 [ALC880_FIXUP_VOL_KNOB] = {
4486 .type = ALC_FIXUP_FUNC,
4487 .v.func = alc880_fixup_vol_knob,
4488 },
4489 [ALC880_FIXUP_FUJITSU] = {
4490 /* override all pins as BIOS on old Amilo is broken */
4491 .type = ALC_FIXUP_PINS,
4492 .v.pins = (const struct alc_pincfg[]) {
4493 { 0x14, 0x0121411f }, /* HP */
4494 { 0x15, 0x99030120 }, /* speaker */
4495 { 0x16, 0x99030130 }, /* bass speaker */
4496 { 0x17, 0x411111f0 }, /* N/A */
4497 { 0x18, 0x411111f0 }, /* N/A */
4498 { 0x19, 0x01a19950 }, /* mic-in */
4499 { 0x1a, 0x411111f0 }, /* N/A */
4500 { 0x1b, 0x411111f0 }, /* N/A */
4501 { 0x1c, 0x411111f0 }, /* N/A */
4502 { 0x1d, 0x411111f0 }, /* N/A */
4503 { 0x1e, 0x01454140 }, /* SPDIF out */
4504 { }
4505 },
4506 .chained = true,
4507 .chain_id = ALC880_FIXUP_VOL_KNOB,
4508 },
4509 [ALC880_FIXUP_F1734] = {
4510 /* almost compatible with FUJITSU, but no bass and SPDIF */
4511 .type = ALC_FIXUP_PINS,
4512 .v.pins = (const struct alc_pincfg[]) {
4513 { 0x14, 0x0121411f }, /* HP */
4514 { 0x15, 0x99030120 }, /* speaker */
4515 { 0x16, 0x411111f0 }, /* N/A */
4516 { 0x17, 0x411111f0 }, /* N/A */
4517 { 0x18, 0x411111f0 }, /* N/A */
4518 { 0x19, 0x01a19950 }, /* mic-in */
4519 { 0x1a, 0x411111f0 }, /* N/A */
4520 { 0x1b, 0x411111f0 }, /* N/A */
4521 { 0x1c, 0x411111f0 }, /* N/A */
4522 { 0x1d, 0x411111f0 }, /* N/A */
4523 { 0x1e, 0x411111f0 }, /* N/A */
4524 { }
4525 },
4526 .chained = true,
4527 .chain_id = ALC880_FIXUP_VOL_KNOB,
4528 },
4529 [ALC880_FIXUP_UNIWILL] = {
4530 /* need to fix HP and speaker pins to be parsed correctly */
4531 .type = ALC_FIXUP_PINS,
4532 .v.pins = (const struct alc_pincfg[]) {
4533 { 0x14, 0x0121411f }, /* HP */
4534 { 0x15, 0x99030120 }, /* speaker */
4535 { 0x16, 0x99030130 }, /* bass speaker */
4536 { }
4537 },
4538 },
4539 [ALC880_FIXUP_UNIWILL_DIG] = {
4540 .type = ALC_FIXUP_PINS,
4541 .v.pins = (const struct alc_pincfg[]) {
4542 /* disable bogus unused pins */
4543 { 0x17, 0x411111f0 },
4544 { 0x19, 0x411111f0 },
4545 { 0x1b, 0x411111f0 },
4546 { 0x1f, 0x411111f0 },
4547 { }
4548 }
4549 },
4550 [ALC880_FIXUP_Z71V] = {
4551 .type = ALC_FIXUP_PINS,
4552 .v.pins = (const struct alc_pincfg[]) {
4553 /* set up the whole pins as BIOS is utterly broken */
4554 { 0x14, 0x99030120 }, /* speaker */
4555 { 0x15, 0x0121411f }, /* HP */
4556 { 0x16, 0x411111f0 }, /* N/A */
4557 { 0x17, 0x411111f0 }, /* N/A */
4558 { 0x18, 0x01a19950 }, /* mic-in */
4559 { 0x19, 0x411111f0 }, /* N/A */
4560 { 0x1a, 0x01813031 }, /* line-in */
4561 { 0x1b, 0x411111f0 }, /* N/A */
4562 { 0x1c, 0x411111f0 }, /* N/A */
4563 { 0x1d, 0x411111f0 }, /* N/A */
4564 { 0x1e, 0x0144111e }, /* SPDIF */
4565 { }
4566 }
4567 },
4568 [ALC880_FIXUP_3ST_BASE] = {
4569 .type = ALC_FIXUP_PINS,
4570 .v.pins = (const struct alc_pincfg[]) {
4571 { 0x14, 0x01014010 }, /* line-out */
4572 { 0x15, 0x411111f0 }, /* N/A */
4573 { 0x16, 0x411111f0 }, /* N/A */
4574 { 0x17, 0x411111f0 }, /* N/A */
4575 { 0x18, 0x01a19c30 }, /* mic-in */
4576 { 0x19, 0x0121411f }, /* HP */
4577 { 0x1a, 0x01813031 }, /* line-in */
4578 { 0x1b, 0x02a19c40 }, /* front-mic */
4579 { 0x1c, 0x411111f0 }, /* N/A */
4580 { 0x1d, 0x411111f0 }, /* N/A */
4581 /* 0x1e is filled in below */
4582 { 0x1f, 0x411111f0 }, /* N/A */
4583 { }
4584 }
4585 },
4586 [ALC880_FIXUP_3ST] = {
4587 .type = ALC_FIXUP_PINS,
4588 .v.pins = (const struct alc_pincfg[]) {
4589 { 0x1e, 0x411111f0 }, /* N/A */
4590 { }
4591 },
4592 .chained = true,
4593 .chain_id = ALC880_FIXUP_3ST_BASE,
4594 },
4595 [ALC880_FIXUP_3ST_DIG] = {
4596 .type = ALC_FIXUP_PINS,
4597 .v.pins = (const struct alc_pincfg[]) {
4598 { 0x1e, 0x0144111e }, /* SPDIF */
4599 { }
4600 },
4601 .chained = true,
4602 .chain_id = ALC880_FIXUP_3ST_BASE,
4603 },
4604 [ALC880_FIXUP_5ST_BASE] = {
4605 .type = ALC_FIXUP_PINS,
4606 .v.pins = (const struct alc_pincfg[]) {
4607 { 0x14, 0x01014010 }, /* front */
4608 { 0x15, 0x411111f0 }, /* N/A */
4609 { 0x16, 0x01011411 }, /* CLFE */
4610 { 0x17, 0x01016412 }, /* surr */
4611 { 0x18, 0x01a19c30 }, /* mic-in */
4612 { 0x19, 0x0121411f }, /* HP */
4613 { 0x1a, 0x01813031 }, /* line-in */
4614 { 0x1b, 0x02a19c40 }, /* front-mic */
4615 { 0x1c, 0x411111f0 }, /* N/A */
4616 { 0x1d, 0x411111f0 }, /* N/A */
4617 /* 0x1e is filled in below */
4618 { 0x1f, 0x411111f0 }, /* N/A */
4619 { }
4620 }
4621 },
4622 [ALC880_FIXUP_5ST] = {
4623 .type = ALC_FIXUP_PINS,
4624 .v.pins = (const struct alc_pincfg[]) {
4625 { 0x1e, 0x411111f0 }, /* N/A */
4626 { }
4627 },
4628 .chained = true,
4629 .chain_id = ALC880_FIXUP_5ST_BASE,
4630 },
4631 [ALC880_FIXUP_5ST_DIG] = {
4632 .type = ALC_FIXUP_PINS,
4633 .v.pins = (const struct alc_pincfg[]) {
4634 { 0x1e, 0x0144111e }, /* SPDIF */
4635 { }
4636 },
4637 .chained = true,
4638 .chain_id = ALC880_FIXUP_5ST_BASE,
4639 },
4640 [ALC880_FIXUP_6ST_BASE] = {
4641 .type = ALC_FIXUP_PINS,
4642 .v.pins = (const struct alc_pincfg[]) {
4643 { 0x14, 0x01014010 }, /* front */
4644 { 0x15, 0x01016412 }, /* surr */
4645 { 0x16, 0x01011411 }, /* CLFE */
4646 { 0x17, 0x01012414 }, /* side */
4647 { 0x18, 0x01a19c30 }, /* mic-in */
4648 { 0x19, 0x02a19c40 }, /* front-mic */
4649 { 0x1a, 0x01813031 }, /* line-in */
4650 { 0x1b, 0x0121411f }, /* HP */
4651 { 0x1c, 0x411111f0 }, /* N/A */
4652 { 0x1d, 0x411111f0 }, /* N/A */
4653 /* 0x1e is filled in below */
4654 { 0x1f, 0x411111f0 }, /* N/A */
4655 { }
4656 }
4657 },
4658 [ALC880_FIXUP_6ST] = {
4659 .type = ALC_FIXUP_PINS,
4660 .v.pins = (const struct alc_pincfg[]) {
4661 { 0x1e, 0x411111f0 }, /* N/A */
4662 { }
4663 },
4664 .chained = true,
4665 .chain_id = ALC880_FIXUP_6ST_BASE,
4666 },
4667 [ALC880_FIXUP_6ST_DIG] = {
4668 .type = ALC_FIXUP_PINS,
4669 .v.pins = (const struct alc_pincfg[]) {
4670 { 0x1e, 0x0144111e }, /* SPDIF */
4671 { }
4672 },
4673 .chained = true,
4674 .chain_id = ALC880_FIXUP_6ST_BASE,
4675 },
4676 };
4677
4678 static const struct snd_pci_quirk alc880_fixup_tbl[] = {
4679 SND_PCI_QUIRK(0x1019, 0x0f69, "Coeus G610P", ALC880_FIXUP_W810),
4680 SND_PCI_QUIRK(0x1043, 0x1964, "ASUS Z71V", ALC880_FIXUP_Z71V),
4681 SND_PCI_QUIRK_VENDOR(0x1043, "ASUS", ALC880_FIXUP_GPIO1),
4682 SND_PCI_QUIRK(0x1558, 0x5401, "Clevo GPIO2", ALC880_FIXUP_GPIO2),
4683 SND_PCI_QUIRK_VENDOR(0x1558, "Clevo", ALC880_FIXUP_EAPD_COEF),
4684 SND_PCI_QUIRK(0x1584, 0x9050, "Uniwill", ALC880_FIXUP_UNIWILL_DIG),
4685 SND_PCI_QUIRK(0x1584, 0x9054, "Uniwill", ALC880_FIXUP_F1734),
4686 SND_PCI_QUIRK(0x1584, 0x9070, "Uniwill", ALC880_FIXUP_UNIWILL),
4687 SND_PCI_QUIRK(0x1584, 0x9077, "Uniwill P53", ALC880_FIXUP_VOL_KNOB),
4688 SND_PCI_QUIRK(0x161f, 0x203d, "W810", ALC880_FIXUP_W810),
4689 SND_PCI_QUIRK(0x161f, 0x205d, "Medion Rim 2150", ALC880_FIXUP_MEDION_RIM),
4690 SND_PCI_QUIRK(0x1734, 0x107c, "FSC F1734", ALC880_FIXUP_F1734),
4691 SND_PCI_QUIRK(0x1734, 0x1094, "FSC Amilo M1451G", ALC880_FIXUP_FUJITSU),
4692 SND_PCI_QUIRK(0x1734, 0x10ac, "FSC AMILO Xi 1526", ALC880_FIXUP_F1734),
4693 SND_PCI_QUIRK(0x1734, 0x10b0, "FSC Amilo Pi1556", ALC880_FIXUP_FUJITSU),
4694 SND_PCI_QUIRK(0x1854, 0x003b, "LG", ALC880_FIXUP_LG),
4695 SND_PCI_QUIRK(0x1854, 0x005f, "LG P1 Express", ALC880_FIXUP_LG),
4696 SND_PCI_QUIRK(0x1854, 0x0068, "LG w1", ALC880_FIXUP_LG),
4697 SND_PCI_QUIRK(0x19db, 0x4188, "TCL S700", ALC880_FIXUP_TCL_S700),
4698
4699 /* Below is the copied entries from alc880_quirks.c.
4700 * It's not quite sure whether BIOS sets the correct pin-config table
4701 * on these machines, thus they are kept to be compatible with
4702 * the old static quirks. Once when it's confirmed to work without
4703 * these overrides, it'd be better to remove.
4704 */
4705 SND_PCI_QUIRK(0x1019, 0xa880, "ECS", ALC880_FIXUP_5ST_DIG),
4706 SND_PCI_QUIRK(0x1019, 0xa884, "Acer APFV", ALC880_FIXUP_6ST),
4707 SND_PCI_QUIRK(0x1025, 0x0070, "ULI", ALC880_FIXUP_3ST_DIG),
4708 SND_PCI_QUIRK(0x1025, 0x0077, "ULI", ALC880_FIXUP_6ST_DIG),
4709 SND_PCI_QUIRK(0x1025, 0x0078, "ULI", ALC880_FIXUP_6ST_DIG),
4710 SND_PCI_QUIRK(0x1025, 0x0087, "ULI", ALC880_FIXUP_6ST_DIG),
4711 SND_PCI_QUIRK(0x1025, 0xe309, "ULI", ALC880_FIXUP_3ST_DIG),
4712 SND_PCI_QUIRK(0x1025, 0xe310, "ULI", ALC880_FIXUP_3ST),
4713 SND_PCI_QUIRK(0x1039, 0x1234, NULL, ALC880_FIXUP_6ST_DIG),
4714 SND_PCI_QUIRK(0x104d, 0x81a0, "Sony", ALC880_FIXUP_3ST),
4715 SND_PCI_QUIRK(0x104d, 0x81d6, "Sony", ALC880_FIXUP_3ST),
4716 SND_PCI_QUIRK(0x107b, 0x3032, "Gateway", ALC880_FIXUP_5ST),
4717 SND_PCI_QUIRK(0x107b, 0x3033, "Gateway", ALC880_FIXUP_5ST),
4718 SND_PCI_QUIRK(0x107b, 0x4039, "Gateway", ALC880_FIXUP_5ST),
4719 SND_PCI_QUIRK(0x1297, 0xc790, "Shuttle ST20G5", ALC880_FIXUP_6ST_DIG),
4720 SND_PCI_QUIRK(0x1458, 0xa102, "Gigabyte K8", ALC880_FIXUP_6ST_DIG),
4721 SND_PCI_QUIRK(0x1462, 0x1150, "MSI", ALC880_FIXUP_6ST_DIG),
4722 SND_PCI_QUIRK(0x1509, 0x925d, "FIC P4M", ALC880_FIXUP_6ST_DIG),
4723 SND_PCI_QUIRK(0x1565, 0x8202, "Biostar", ALC880_FIXUP_5ST_DIG),
4724 SND_PCI_QUIRK(0x1695, 0x400d, "EPoX", ALC880_FIXUP_5ST_DIG),
4725 SND_PCI_QUIRK(0x1695, 0x4012, "EPox EP-5LDA", ALC880_FIXUP_5ST_DIG),
4726 SND_PCI_QUIRK(0x2668, 0x8086, NULL, ALC880_FIXUP_6ST_DIG), /* broken BIOS */
4727 SND_PCI_QUIRK(0x8086, 0x2668, NULL, ALC880_FIXUP_6ST_DIG),
4728 SND_PCI_QUIRK(0x8086, 0xa100, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4729 SND_PCI_QUIRK(0x8086, 0xd400, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4730 SND_PCI_QUIRK(0x8086, 0xd401, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4731 SND_PCI_QUIRK(0x8086, 0xd402, "Intel mobo", ALC880_FIXUP_3ST_DIG),
4732 SND_PCI_QUIRK(0x8086, 0xe224, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4733 SND_PCI_QUIRK(0x8086, 0xe305, "Intel mobo", ALC880_FIXUP_3ST_DIG),
4734 SND_PCI_QUIRK(0x8086, 0xe308, "Intel mobo", ALC880_FIXUP_3ST_DIG),
4735 SND_PCI_QUIRK(0x8086, 0xe400, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4736 SND_PCI_QUIRK(0x8086, 0xe401, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4737 SND_PCI_QUIRK(0x8086, 0xe402, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4738 /* default Intel */
4739 SND_PCI_QUIRK_VENDOR(0x8086, "Intel mobo", ALC880_FIXUP_3ST),
4740 SND_PCI_QUIRK(0xa0a0, 0x0560, "AOpen i915GMm-HFS", ALC880_FIXUP_5ST_DIG),
4741 SND_PCI_QUIRK(0xe803, 0x1019, NULL, ALC880_FIXUP_6ST_DIG),
4742 {}
4743 };
4744
4745 static const struct alc_model_fixup alc880_fixup_models[] = {
4746 {.id = ALC880_FIXUP_3ST, .name = "3stack"},
4747 {.id = ALC880_FIXUP_3ST_DIG, .name = "3stack-digout"},
4748 {.id = ALC880_FIXUP_5ST, .name = "5stack"},
4749 {.id = ALC880_FIXUP_5ST_DIG, .name = "5stack-digout"},
4750 {.id = ALC880_FIXUP_6ST, .name = "6stack"},
4751 {.id = ALC880_FIXUP_6ST_DIG, .name = "6stack-digout"},
4752 {}
4753 };
4754
4755
4756 /*
4757 * OK, here we have finally the patch for ALC880
4758 */
4759 static int patch_alc880(struct hda_codec *codec)
4760 {
4761 struct alc_spec *spec;
4762 int err;
4763
4764 err = alc_alloc_spec(codec, 0x0b);
4765 if (err < 0)
4766 return err;
4767
4768 spec = codec->spec;
4769 spec->need_dac_fix = 1;
4770
4771 alc_pick_fixup(codec, alc880_fixup_models, alc880_fixup_tbl,
4772 alc880_fixups);
4773 alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
4774
4775 /* automatic parse from the BIOS config */
4776 err = alc880_parse_auto_config(codec);
4777 if (err < 0)
4778 goto error;
4779
4780 if (!spec->no_analog) {
4781 err = snd_hda_attach_beep_device(codec, 0x1);
4782 if (err < 0)
4783 goto error;
4784 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
4785 }
4786
4787 codec->patch_ops = alc_patch_ops;
4788
4789 alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
4790
4791 return 0;
4792
4793 error:
4794 alc_free(codec);
4795 return err;
4796 }
4797
4798
4799 /*
4800 * ALC260 support
4801 */
4802 static int alc260_parse_auto_config(struct hda_codec *codec)
4803 {
4804 static const hda_nid_t alc260_ignore[] = { 0x17, 0 };
4805 static const hda_nid_t alc260_ssids[] = { 0x10, 0x15, 0x0f, 0 };
4806 return alc_parse_auto_config(codec, alc260_ignore, alc260_ssids);
4807 }
4808
4809 /*
4810 * Pin config fixes
4811 */
4812 enum {
4813 ALC260_FIXUP_HP_DC5750,
4814 ALC260_FIXUP_HP_PIN_0F,
4815 ALC260_FIXUP_COEF,
4816 ALC260_FIXUP_GPIO1,
4817 ALC260_FIXUP_GPIO1_TOGGLE,
4818 ALC260_FIXUP_REPLACER,
4819 ALC260_FIXUP_HP_B1900,
4820 ALC260_FIXUP_KN1,
4821 };
4822
4823 static void alc260_gpio1_automute(struct hda_codec *codec)
4824 {
4825 struct alc_spec *spec = codec->spec;
4826 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
4827 spec->hp_jack_present);
4828 }
4829
4830 static void alc260_fixup_gpio1_toggle(struct hda_codec *codec,
4831 const struct alc_fixup *fix, int action)
4832 {
4833 struct alc_spec *spec = codec->spec;
4834 if (action == ALC_FIXUP_ACT_PROBE) {
4835 /* although the machine has only one output pin, we need to
4836 * toggle GPIO1 according to the jack state
4837 */
4838 spec->automute_hook = alc260_gpio1_automute;
4839 spec->detect_hp = 1;
4840 spec->automute_speaker = 1;
4841 spec->autocfg.hp_pins[0] = 0x0f; /* copy it for automute */
4842 snd_hda_jack_detect_enable(codec, 0x0f, ALC_HP_EVENT);
4843 spec->unsol_event = alc_sku_unsol_event;
4844 snd_hda_gen_add_verbs(&spec->gen, alc_gpio1_init_verbs);
4845 }
4846 }
4847
4848 static void alc260_fixup_kn1(struct hda_codec *codec,
4849 const struct alc_fixup *fix, int action)
4850 {
4851 struct alc_spec *spec = codec->spec;
4852 static const struct alc_pincfg pincfgs[] = {
4853 { 0x0f, 0x02214000 }, /* HP/speaker */
4854 { 0x12, 0x90a60160 }, /* int mic */
4855 { 0x13, 0x02a19000 }, /* ext mic */
4856 { 0x18, 0x01446000 }, /* SPDIF out */
4857 /* disable bogus I/O pins */
4858 { 0x10, 0x411111f0 },
4859 { 0x11, 0x411111f0 },
4860 { 0x14, 0x411111f0 },
4861 { 0x15, 0x411111f0 },
4862 { 0x16, 0x411111f0 },
4863 { 0x17, 0x411111f0 },
4864 { 0x19, 0x411111f0 },
4865 { }
4866 };
4867
4868 switch (action) {
4869 case ALC_FIXUP_ACT_PRE_PROBE:
4870 alc_apply_pincfgs(codec, pincfgs);
4871 break;
4872 case ALC_FIXUP_ACT_PROBE:
4873 spec->init_amp = ALC_INIT_NONE;
4874 break;
4875 }
4876 }
4877
4878 static const struct alc_fixup alc260_fixups[] = {
4879 [ALC260_FIXUP_HP_DC5750] = {
4880 .type = ALC_FIXUP_PINS,
4881 .v.pins = (const struct alc_pincfg[]) {
4882 { 0x11, 0x90130110 }, /* speaker */
4883 { }
4884 }
4885 },
4886 [ALC260_FIXUP_HP_PIN_0F] = {
4887 .type = ALC_FIXUP_PINS,
4888 .v.pins = (const struct alc_pincfg[]) {
4889 { 0x0f, 0x01214000 }, /* HP */
4890 { }
4891 }
4892 },
4893 [ALC260_FIXUP_COEF] = {
4894 .type = ALC_FIXUP_VERBS,
4895 .v.verbs = (const struct hda_verb[]) {
4896 { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4897 { 0x20, AC_VERB_SET_PROC_COEF, 0x3040 },
4898 { }
4899 },
4900 .chained = true,
4901 .chain_id = ALC260_FIXUP_HP_PIN_0F,
4902 },
4903 [ALC260_FIXUP_GPIO1] = {
4904 .type = ALC_FIXUP_VERBS,
4905 .v.verbs = alc_gpio1_init_verbs,
4906 },
4907 [ALC260_FIXUP_GPIO1_TOGGLE] = {
4908 .type = ALC_FIXUP_FUNC,
4909 .v.func = alc260_fixup_gpio1_toggle,
4910 .chained = true,
4911 .chain_id = ALC260_FIXUP_HP_PIN_0F,
4912 },
4913 [ALC260_FIXUP_REPLACER] = {
4914 .type = ALC_FIXUP_VERBS,
4915 .v.verbs = (const struct hda_verb[]) {
4916 { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4917 { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
4918 { }
4919 },
4920 .chained = true,
4921 .chain_id = ALC260_FIXUP_GPIO1_TOGGLE,
4922 },
4923 [ALC260_FIXUP_HP_B1900] = {
4924 .type = ALC_FIXUP_FUNC,
4925 .v.func = alc260_fixup_gpio1_toggle,
4926 .chained = true,
4927 .chain_id = ALC260_FIXUP_COEF,
4928 },
4929 [ALC260_FIXUP_KN1] = {
4930 .type = ALC_FIXUP_FUNC,
4931 .v.func = alc260_fixup_kn1,
4932 },
4933 };
4934
4935 static const struct snd_pci_quirk alc260_fixup_tbl[] = {
4936 SND_PCI_QUIRK(0x1025, 0x007b, "Acer C20x", ALC260_FIXUP_GPIO1),
4937 SND_PCI_QUIRK(0x1025, 0x007f, "Acer Aspire 9500", ALC260_FIXUP_COEF),
4938 SND_PCI_QUIRK(0x1025, 0x008f, "Acer", ALC260_FIXUP_GPIO1),
4939 SND_PCI_QUIRK(0x103c, 0x280a, "HP dc5750", ALC260_FIXUP_HP_DC5750),
4940 SND_PCI_QUIRK(0x103c, 0x30ba, "HP Presario B1900", ALC260_FIXUP_HP_B1900),
4941 SND_PCI_QUIRK(0x1509, 0x4540, "Favorit 100XS", ALC260_FIXUP_GPIO1),
4942 SND_PCI_QUIRK(0x152d, 0x0729, "Quanta KN1", ALC260_FIXUP_KN1),
4943 SND_PCI_QUIRK(0x161f, 0x2057, "Replacer 672V", ALC260_FIXUP_REPLACER),
4944 SND_PCI_QUIRK(0x1631, 0xc017, "PB V7900", ALC260_FIXUP_COEF),
4945 {}
4946 };
4947
4948 /*
4949 */
4950 static int patch_alc260(struct hda_codec *codec)
4951 {
4952 struct alc_spec *spec;
4953 int err;
4954
4955 err = alc_alloc_spec(codec, 0x07);
4956 if (err < 0)
4957 return err;
4958
4959 spec = codec->spec;
4960
4961 alc_pick_fixup(codec, NULL, alc260_fixup_tbl, alc260_fixups);
4962 alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
4963
4964 /* automatic parse from the BIOS config */
4965 err = alc260_parse_auto_config(codec);
4966 if (err < 0)
4967 goto error;
4968
4969 if (!spec->no_analog) {
4970 err = snd_hda_attach_beep_device(codec, 0x1);
4971 if (err < 0)
4972 goto error;
4973 set_beep_amp(spec, 0x07, 0x05, HDA_INPUT);
4974 }
4975
4976 codec->patch_ops = alc_patch_ops;
4977 spec->shutup = alc_eapd_shutup;
4978
4979 alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
4980
4981 return 0;
4982
4983 error:
4984 alc_free(codec);
4985 return err;
4986 }
4987
4988
4989 /*
4990 * ALC882/883/885/888/889 support
4991 *
4992 * ALC882 is almost identical with ALC880 but has cleaner and more flexible
4993 * configuration. Each pin widget can choose any input DACs and a mixer.
4994 * Each ADC is connected from a mixer of all inputs. This makes possible
4995 * 6-channel independent captures.
4996 *
4997 * In addition, an independent DAC for the multi-playback (not used in this
4998 * driver yet).
4999 */
5000
5001 /*
5002 * Pin config fixes
5003 */
5004 enum {
5005 ALC882_FIXUP_ABIT_AW9D_MAX,
5006 ALC882_FIXUP_LENOVO_Y530,
5007 ALC882_FIXUP_PB_M5210,
5008 ALC882_FIXUP_ACER_ASPIRE_7736,
5009 ALC882_FIXUP_ASUS_W90V,
5010 ALC889_FIXUP_CD,
5011 ALC889_FIXUP_VAIO_TT,
5012 ALC888_FIXUP_EEE1601,
5013 ALC882_FIXUP_EAPD,
5014 ALC883_FIXUP_EAPD,
5015 ALC883_FIXUP_ACER_EAPD,
5016 ALC882_FIXUP_GPIO1,
5017 ALC882_FIXUP_GPIO2,
5018 ALC882_FIXUP_GPIO3,
5019 ALC889_FIXUP_COEF,
5020 ALC882_FIXUP_ASUS_W2JC,
5021 ALC882_FIXUP_ACER_ASPIRE_4930G,
5022 ALC882_FIXUP_ACER_ASPIRE_8930G,
5023 ALC882_FIXUP_ASPIRE_8930G_VERBS,
5024 ALC885_FIXUP_MACPRO_GPIO,
5025 ALC889_FIXUP_DAC_ROUTE,
5026 ALC889_FIXUP_MBP_VREF,
5027 ALC889_FIXUP_IMAC91_VREF,
5028 ALC882_FIXUP_INV_DMIC,
5029 };
5030
5031 static void alc889_fixup_coef(struct hda_codec *codec,
5032 const struct alc_fixup *fix, int action)
5033 {
5034 if (action != ALC_FIXUP_ACT_INIT)
5035 return;
5036 alc889_coef_init(codec);
5037 }
5038
5039 /* toggle speaker-output according to the hp-jack state */
5040 static void alc882_gpio_mute(struct hda_codec *codec, int pin, int muted)
5041 {
5042 unsigned int gpiostate, gpiomask, gpiodir;
5043
5044 gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
5045 AC_VERB_GET_GPIO_DATA, 0);
5046
5047 if (!muted)
5048 gpiostate |= (1 << pin);
5049 else
5050 gpiostate &= ~(1 << pin);
5051
5052 gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
5053 AC_VERB_GET_GPIO_MASK, 0);
5054 gpiomask |= (1 << pin);
5055
5056 gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
5057 AC_VERB_GET_GPIO_DIRECTION, 0);
5058 gpiodir |= (1 << pin);
5059
5060
5061 snd_hda_codec_write(codec, codec->afg, 0,
5062 AC_VERB_SET_GPIO_MASK, gpiomask);
5063 snd_hda_codec_write(codec, codec->afg, 0,
5064 AC_VERB_SET_GPIO_DIRECTION, gpiodir);
5065
5066 msleep(1);
5067
5068 snd_hda_codec_write(codec, codec->afg, 0,
5069 AC_VERB_SET_GPIO_DATA, gpiostate);
5070 }
5071
5072 /* set up GPIO at initialization */
5073 static void alc885_fixup_macpro_gpio(struct hda_codec *codec,
5074 const struct alc_fixup *fix, int action)
5075 {
5076 if (action != ALC_FIXUP_ACT_INIT)
5077 return;
5078 alc882_gpio_mute(codec, 0, 0);
5079 alc882_gpio_mute(codec, 1, 0);
5080 }
5081
5082 /* Fix the connection of some pins for ALC889:
5083 * At least, Acer Aspire 5935 shows the connections to DAC3/4 don't
5084 * work correctly (bko#42740)
5085 */
5086 static void alc889_fixup_dac_route(struct hda_codec *codec,
5087 const struct alc_fixup *fix, int action)
5088 {
5089 if (action == ALC_FIXUP_ACT_PRE_PROBE) {
5090 /* fake the connections during parsing the tree */
5091 hda_nid_t conn1[2] = { 0x0c, 0x0d };
5092 hda_nid_t conn2[2] = { 0x0e, 0x0f };
5093 snd_hda_override_conn_list(codec, 0x14, 2, conn1);
5094 snd_hda_override_conn_list(codec, 0x15, 2, conn1);
5095 snd_hda_override_conn_list(codec, 0x18, 2, conn2);
5096 snd_hda_override_conn_list(codec, 0x1a, 2, conn2);
5097 } else if (action == ALC_FIXUP_ACT_PROBE) {
5098 /* restore the connections */
5099 hda_nid_t conn[5] = { 0x0c, 0x0d, 0x0e, 0x0f, 0x26 };
5100 snd_hda_override_conn_list(codec, 0x14, 5, conn);
5101 snd_hda_override_conn_list(codec, 0x15, 5, conn);
5102 snd_hda_override_conn_list(codec, 0x18, 5, conn);
5103 snd_hda_override_conn_list(codec, 0x1a, 5, conn);
5104 }
5105 }
5106
5107 /* Set VREF on HP pin */
5108 static void alc889_fixup_mbp_vref(struct hda_codec *codec,
5109 const struct alc_fixup *fix, int action)
5110 {
5111 struct alc_spec *spec = codec->spec;
5112 static hda_nid_t nids[2] = { 0x14, 0x15 };
5113 int i;
5114
5115 if (action != ALC_FIXUP_ACT_INIT)
5116 return;
5117 for (i = 0; i < ARRAY_SIZE(nids); i++) {
5118 unsigned int val = snd_hda_codec_get_pincfg(codec, nids[i]);
5119 if (get_defcfg_device(val) != AC_JACK_HP_OUT)
5120 continue;
5121 val = snd_hda_codec_read(codec, nids[i], 0,
5122 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5123 val |= AC_PINCTL_VREF_80;
5124 snd_hda_set_pin_ctl(codec, nids[i], val);
5125 spec->keep_vref_in_automute = 1;
5126 break;
5127 }
5128 }
5129
5130 /* Set VREF on speaker pins on imac91 */
5131 static void alc889_fixup_imac91_vref(struct hda_codec *codec,
5132 const struct alc_fixup *fix, int action)
5133 {
5134 struct alc_spec *spec = codec->spec;
5135 static hda_nid_t nids[2] = { 0x18, 0x1a };
5136 int i;
5137
5138 if (action != ALC_FIXUP_ACT_INIT)
5139 return;
5140 for (i = 0; i < ARRAY_SIZE(nids); i++) {
5141 unsigned int val;
5142 val = snd_hda_codec_read(codec, nids[i], 0,
5143 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5144 val |= AC_PINCTL_VREF_50;
5145 snd_hda_set_pin_ctl(codec, nids[i], val);
5146 }
5147 spec->keep_vref_in_automute = 1;
5148 }
5149
5150 static const struct alc_fixup alc882_fixups[] = {
5151 [ALC882_FIXUP_ABIT_AW9D_MAX] = {
5152 .type = ALC_FIXUP_PINS,
5153 .v.pins = (const struct alc_pincfg[]) {
5154 { 0x15, 0x01080104 }, /* side */
5155 { 0x16, 0x01011012 }, /* rear */
5156 { 0x17, 0x01016011 }, /* clfe */
5157 { }
5158 }
5159 },
5160 [ALC882_FIXUP_LENOVO_Y530] = {
5161 .type = ALC_FIXUP_PINS,
5162 .v.pins = (const struct alc_pincfg[]) {
5163 { 0x15, 0x99130112 }, /* rear int speakers */
5164 { 0x16, 0x99130111 }, /* subwoofer */
5165 { }
5166 }
5167 },
5168 [ALC882_FIXUP_PB_M5210] = {
5169 .type = ALC_FIXUP_VERBS,
5170 .v.verbs = (const struct hda_verb[]) {
5171 { 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF50 },
5172 {}
5173 }
5174 },
5175 [ALC882_FIXUP_ACER_ASPIRE_7736] = {
5176 .type = ALC_FIXUP_FUNC,
5177 .v.func = alc_fixup_sku_ignore,
5178 },
5179 [ALC882_FIXUP_ASUS_W90V] = {
5180 .type = ALC_FIXUP_PINS,
5181 .v.pins = (const struct alc_pincfg[]) {
5182 { 0x16, 0x99130110 }, /* fix sequence for CLFE */
5183 { }
5184 }
5185 },
5186 [ALC889_FIXUP_CD] = {
5187 .type = ALC_FIXUP_PINS,
5188 .v.pins = (const struct alc_pincfg[]) {
5189 { 0x1c, 0x993301f0 }, /* CD */
5190 { }
5191 }
5192 },
5193 [ALC889_FIXUP_VAIO_TT] = {
5194 .type = ALC_FIXUP_PINS,
5195 .v.pins = (const struct alc_pincfg[]) {
5196 { 0x17, 0x90170111 }, /* hidden surround speaker */
5197 { }
5198 }
5199 },
5200 [ALC888_FIXUP_EEE1601] = {
5201 .type = ALC_FIXUP_VERBS,
5202 .v.verbs = (const struct hda_verb[]) {
5203 { 0x20, AC_VERB_SET_COEF_INDEX, 0x0b },
5204 { 0x20, AC_VERB_SET_PROC_COEF, 0x0838 },
5205 { }
5206 }
5207 },
5208 [ALC882_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, 0x3060 },
5214 { }
5215 }
5216 },
5217 [ALC883_FIXUP_EAPD] = {
5218 .type = ALC_FIXUP_VERBS,
5219 .v.verbs = (const struct hda_verb[]) {
5220 /* change to EAPD mode */
5221 { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5222 { 0x20, AC_VERB_SET_PROC_COEF, 0x3070 },
5223 { }
5224 }
5225 },
5226 [ALC883_FIXUP_ACER_EAPD] = {
5227 .type = ALC_FIXUP_VERBS,
5228 .v.verbs = (const struct hda_verb[]) {
5229 /* eanable EAPD on Acer laptops */
5230 { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5231 { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
5232 { }
5233 }
5234 },
5235 [ALC882_FIXUP_GPIO1] = {
5236 .type = ALC_FIXUP_VERBS,
5237 .v.verbs = alc_gpio1_init_verbs,
5238 },
5239 [ALC882_FIXUP_GPIO2] = {
5240 .type = ALC_FIXUP_VERBS,
5241 .v.verbs = alc_gpio2_init_verbs,
5242 },
5243 [ALC882_FIXUP_GPIO3] = {
5244 .type = ALC_FIXUP_VERBS,
5245 .v.verbs = alc_gpio3_init_verbs,
5246 },
5247 [ALC882_FIXUP_ASUS_W2JC] = {
5248 .type = ALC_FIXUP_VERBS,
5249 .v.verbs = alc_gpio1_init_verbs,
5250 .chained = true,
5251 .chain_id = ALC882_FIXUP_EAPD,
5252 },
5253 [ALC889_FIXUP_COEF] = {
5254 .type = ALC_FIXUP_FUNC,
5255 .v.func = alc889_fixup_coef,
5256 },
5257 [ALC882_FIXUP_ACER_ASPIRE_4930G] = {
5258 .type = ALC_FIXUP_PINS,
5259 .v.pins = (const struct alc_pincfg[]) {
5260 { 0x16, 0x99130111 }, /* CLFE speaker */
5261 { 0x17, 0x99130112 }, /* surround speaker */
5262 { }
5263 },
5264 .chained = true,
5265 .chain_id = ALC882_FIXUP_GPIO1,
5266 },
5267 [ALC882_FIXUP_ACER_ASPIRE_8930G] = {
5268 .type = ALC_FIXUP_PINS,
5269 .v.pins = (const struct alc_pincfg[]) {
5270 { 0x16, 0x99130111 }, /* CLFE speaker */
5271 { 0x1b, 0x99130112 }, /* surround speaker */
5272 { }
5273 },
5274 .chained = true,
5275 .chain_id = ALC882_FIXUP_ASPIRE_8930G_VERBS,
5276 },
5277 [ALC882_FIXUP_ASPIRE_8930G_VERBS] = {
5278 /* additional init verbs for Acer Aspire 8930G */
5279 .type = ALC_FIXUP_VERBS,
5280 .v.verbs = (const struct hda_verb[]) {
5281 /* Enable all DACs */
5282 /* DAC DISABLE/MUTE 1? */
5283 /* setting bits 1-5 disables DAC nids 0x02-0x06
5284 * apparently. Init=0x38 */
5285 { 0x20, AC_VERB_SET_COEF_INDEX, 0x03 },
5286 { 0x20, AC_VERB_SET_PROC_COEF, 0x0000 },
5287 /* DAC DISABLE/MUTE 2? */
5288 /* some bit here disables the other DACs.
5289 * Init=0x4900 */
5290 { 0x20, AC_VERB_SET_COEF_INDEX, 0x08 },
5291 { 0x20, AC_VERB_SET_PROC_COEF, 0x0000 },
5292 /* DMIC fix
5293 * This laptop has a stereo digital microphone.
5294 * The mics are only 1cm apart which makes the stereo
5295 * useless. However, either the mic or the ALC889
5296 * makes the signal become a difference/sum signal
5297 * instead of standard stereo, which is annoying.
5298 * So instead we flip this bit which makes the
5299 * codec replicate the sum signal to both channels,
5300 * turning it into a normal mono mic.
5301 */
5302 /* DMIC_CONTROL? Init value = 0x0001 */
5303 { 0x20, AC_VERB_SET_COEF_INDEX, 0x0b },
5304 { 0x20, AC_VERB_SET_PROC_COEF, 0x0003 },
5305 { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5306 { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
5307 { }
5308 },
5309 .chained = true,
5310 .chain_id = ALC882_FIXUP_GPIO1,
5311 },
5312 [ALC885_FIXUP_MACPRO_GPIO] = {
5313 .type = ALC_FIXUP_FUNC,
5314 .v.func = alc885_fixup_macpro_gpio,
5315 },
5316 [ALC889_FIXUP_DAC_ROUTE] = {
5317 .type = ALC_FIXUP_FUNC,
5318 .v.func = alc889_fixup_dac_route,
5319 },
5320 [ALC889_FIXUP_MBP_VREF] = {
5321 .type = ALC_FIXUP_FUNC,
5322 .v.func = alc889_fixup_mbp_vref,
5323 .chained = true,
5324 .chain_id = ALC882_FIXUP_GPIO1,
5325 },
5326 [ALC889_FIXUP_IMAC91_VREF] = {
5327 .type = ALC_FIXUP_FUNC,
5328 .v.func = alc889_fixup_imac91_vref,
5329 .chained = true,
5330 .chain_id = ALC882_FIXUP_GPIO1,
5331 },
5332 [ALC882_FIXUP_INV_DMIC] = {
5333 .type = ALC_FIXUP_FUNC,
5334 .v.func = alc_fixup_inv_dmic_0x12,
5335 },
5336 };
5337
5338 static const struct snd_pci_quirk alc882_fixup_tbl[] = {
5339 SND_PCI_QUIRK(0x1025, 0x006c, "Acer Aspire 9810", ALC883_FIXUP_ACER_EAPD),
5340 SND_PCI_QUIRK(0x1025, 0x0090, "Acer Aspire", ALC883_FIXUP_ACER_EAPD),
5341 SND_PCI_QUIRK(0x1025, 0x010a, "Acer Ferrari 5000", ALC883_FIXUP_ACER_EAPD),
5342 SND_PCI_QUIRK(0x1025, 0x0110, "Acer Aspire", ALC883_FIXUP_ACER_EAPD),
5343 SND_PCI_QUIRK(0x1025, 0x0112, "Acer Aspire 9303", ALC883_FIXUP_ACER_EAPD),
5344 SND_PCI_QUIRK(0x1025, 0x0121, "Acer Aspire 5920G", ALC883_FIXUP_ACER_EAPD),
5345 SND_PCI_QUIRK(0x1025, 0x013e, "Acer Aspire 4930G",
5346 ALC882_FIXUP_ACER_ASPIRE_4930G),
5347 SND_PCI_QUIRK(0x1025, 0x013f, "Acer Aspire 5930G",
5348 ALC882_FIXUP_ACER_ASPIRE_4930G),
5349 SND_PCI_QUIRK(0x1025, 0x0145, "Acer Aspire 8930G",
5350 ALC882_FIXUP_ACER_ASPIRE_8930G),
5351 SND_PCI_QUIRK(0x1025, 0x0146, "Acer Aspire 6935G",
5352 ALC882_FIXUP_ACER_ASPIRE_8930G),
5353 SND_PCI_QUIRK(0x1025, 0x015e, "Acer Aspire 6930G",
5354 ALC882_FIXUP_ACER_ASPIRE_4930G),
5355 SND_PCI_QUIRK(0x1025, 0x0166, "Acer Aspire 6530G",
5356 ALC882_FIXUP_ACER_ASPIRE_4930G),
5357 SND_PCI_QUIRK(0x1025, 0x0142, "Acer Aspire 7730G",
5358 ALC882_FIXUP_ACER_ASPIRE_4930G),
5359 SND_PCI_QUIRK(0x1025, 0x0155, "Packard-Bell M5120", ALC882_FIXUP_PB_M5210),
5360 SND_PCI_QUIRK(0x1025, 0x021e, "Acer Aspire 5739G",
5361 ALC882_FIXUP_ACER_ASPIRE_4930G),
5362 SND_PCI_QUIRK(0x1025, 0x0259, "Acer Aspire 5935", ALC889_FIXUP_DAC_ROUTE),
5363 SND_PCI_QUIRK(0x1025, 0x026b, "Acer Aspire 8940G", ALC882_FIXUP_ACER_ASPIRE_8930G),
5364 SND_PCI_QUIRK(0x1025, 0x0296, "Acer Aspire 7736z", ALC882_FIXUP_ACER_ASPIRE_7736),
5365 SND_PCI_QUIRK(0x1043, 0x13c2, "Asus A7M", ALC882_FIXUP_EAPD),
5366 SND_PCI_QUIRK(0x1043, 0x1873, "ASUS W90V", ALC882_FIXUP_ASUS_W90V),
5367 SND_PCI_QUIRK(0x1043, 0x1971, "Asus W2JC", ALC882_FIXUP_ASUS_W2JC),
5368 SND_PCI_QUIRK(0x1043, 0x835f, "Asus Eee 1601", ALC888_FIXUP_EEE1601),
5369 SND_PCI_QUIRK(0x104d, 0x9047, "Sony Vaio TT", ALC889_FIXUP_VAIO_TT),
5370
5371 /* All Apple entries are in codec SSIDs */
5372 SND_PCI_QUIRK(0x106b, 0x00a0, "MacBookPro 3,1", ALC889_FIXUP_MBP_VREF),
5373 SND_PCI_QUIRK(0x106b, 0x00a1, "Macbook", ALC889_FIXUP_MBP_VREF),
5374 SND_PCI_QUIRK(0x106b, 0x00a4, "MacbookPro 4,1", ALC889_FIXUP_MBP_VREF),
5375 SND_PCI_QUIRK(0x106b, 0x0c00, "Mac Pro", ALC885_FIXUP_MACPRO_GPIO),
5376 SND_PCI_QUIRK(0x106b, 0x1000, "iMac 24", ALC885_FIXUP_MACPRO_GPIO),
5377 SND_PCI_QUIRK(0x106b, 0x2800, "AppleTV", ALC885_FIXUP_MACPRO_GPIO),
5378 SND_PCI_QUIRK(0x106b, 0x2c00, "MacbookPro rev3", ALC889_FIXUP_MBP_VREF),
5379 SND_PCI_QUIRK(0x106b, 0x3000, "iMac", ALC889_FIXUP_MBP_VREF),
5380 SND_PCI_QUIRK(0x106b, 0x3200, "iMac 7,1 Aluminum", ALC882_FIXUP_EAPD),
5381 SND_PCI_QUIRK(0x106b, 0x3400, "MacBookAir 1,1", ALC889_FIXUP_MBP_VREF),
5382 SND_PCI_QUIRK(0x106b, 0x3500, "MacBookAir 2,1", ALC889_FIXUP_MBP_VREF),
5383 SND_PCI_QUIRK(0x106b, 0x3600, "Macbook 3,1", ALC889_FIXUP_MBP_VREF),
5384 SND_PCI_QUIRK(0x106b, 0x3800, "MacbookPro 4,1", ALC889_FIXUP_MBP_VREF),
5385 SND_PCI_QUIRK(0x106b, 0x3e00, "iMac 24 Aluminum", ALC885_FIXUP_MACPRO_GPIO),
5386 SND_PCI_QUIRK(0x106b, 0x3f00, "Macbook 5,1", ALC889_FIXUP_IMAC91_VREF),
5387 SND_PCI_QUIRK(0x106b, 0x4000, "MacbookPro 5,1", ALC889_FIXUP_IMAC91_VREF),
5388 SND_PCI_QUIRK(0x106b, 0x4100, "Macmini 3,1", ALC889_FIXUP_IMAC91_VREF),
5389 SND_PCI_QUIRK(0x106b, 0x4200, "Mac Pro 5,1", ALC885_FIXUP_MACPRO_GPIO),
5390 SND_PCI_QUIRK(0x106b, 0x4600, "MacbookPro 5,2", ALC889_FIXUP_IMAC91_VREF),
5391 SND_PCI_QUIRK(0x106b, 0x4900, "iMac 9,1 Aluminum", ALC889_FIXUP_IMAC91_VREF),
5392 SND_PCI_QUIRK(0x106b, 0x4a00, "Macbook 5,2", ALC889_FIXUP_IMAC91_VREF),
5393
5394 SND_PCI_QUIRK(0x1071, 0x8258, "Evesham Voyaeger", ALC882_FIXUP_EAPD),
5395 SND_PCI_QUIRK(0x1462, 0x7350, "MSI-7350", ALC889_FIXUP_CD),
5396 SND_PCI_QUIRK_VENDOR(0x1462, "MSI", ALC882_FIXUP_GPIO3),
5397 SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte EP45-DS3", ALC889_FIXUP_CD),
5398 SND_PCI_QUIRK(0x147b, 0x107a, "Abit AW9D-MAX", ALC882_FIXUP_ABIT_AW9D_MAX),
5399 SND_PCI_QUIRK_VENDOR(0x1558, "Clevo laptop", ALC882_FIXUP_EAPD),
5400 SND_PCI_QUIRK(0x161f, 0x2054, "Medion laptop", ALC883_FIXUP_EAPD),
5401 SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo Y530", ALC882_FIXUP_LENOVO_Y530),
5402 SND_PCI_QUIRK(0x8086, 0x0022, "DX58SO", ALC889_FIXUP_COEF),
5403 {}
5404 };
5405
5406 static const struct alc_model_fixup alc882_fixup_models[] = {
5407 {.id = ALC882_FIXUP_ACER_ASPIRE_4930G, .name = "acer-aspire-4930g"},
5408 {.id = ALC882_FIXUP_ACER_ASPIRE_8930G, .name = "acer-aspire-8930g"},
5409 {.id = ALC883_FIXUP_ACER_EAPD, .name = "acer-aspire"},
5410 {.id = ALC882_FIXUP_INV_DMIC, .name = "inv-dmic"},
5411 {}
5412 };
5413
5414 /*
5415 * BIOS auto configuration
5416 */
5417 /* almost identical with ALC880 parser... */
5418 static int alc882_parse_auto_config(struct hda_codec *codec)
5419 {
5420 static const hda_nid_t alc882_ignore[] = { 0x1d, 0 };
5421 static const hda_nid_t alc882_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5422 return alc_parse_auto_config(codec, alc882_ignore, alc882_ssids);
5423 }
5424
5425 /*
5426 */
5427 static int patch_alc882(struct hda_codec *codec)
5428 {
5429 struct alc_spec *spec;
5430 int err;
5431
5432 err = alc_alloc_spec(codec, 0x0b);
5433 if (err < 0)
5434 return err;
5435
5436 spec = codec->spec;
5437
5438 switch (codec->vendor_id) {
5439 case 0x10ec0882:
5440 case 0x10ec0885:
5441 break;
5442 default:
5443 /* ALC883 and variants */
5444 alc_fix_pll_init(codec, 0x20, 0x0a, 10);
5445 break;
5446 }
5447
5448 alc_pick_fixup(codec, alc882_fixup_models, alc882_fixup_tbl,
5449 alc882_fixups);
5450 alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
5451
5452 alc_auto_parse_customize_define(codec);
5453
5454 /* automatic parse from the BIOS config */
5455 err = alc882_parse_auto_config(codec);
5456 if (err < 0)
5457 goto error;
5458
5459 if (!spec->no_analog && has_cdefine_beep(codec)) {
5460 err = snd_hda_attach_beep_device(codec, 0x1);
5461 if (err < 0)
5462 goto error;
5463 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
5464 }
5465
5466 codec->patch_ops = alc_patch_ops;
5467
5468 alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
5469
5470 return 0;
5471
5472 error:
5473 alc_free(codec);
5474 return err;
5475 }
5476
5477
5478 /*
5479 * ALC262 support
5480 */
5481 static int alc262_parse_auto_config(struct hda_codec *codec)
5482 {
5483 static const hda_nid_t alc262_ignore[] = { 0x1d, 0 };
5484 static const hda_nid_t alc262_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5485 return alc_parse_auto_config(codec, alc262_ignore, alc262_ssids);
5486 }
5487
5488 /*
5489 * Pin config fixes
5490 */
5491 enum {
5492 ALC262_FIXUP_FSC_H270,
5493 ALC262_FIXUP_HP_Z200,
5494 ALC262_FIXUP_TYAN,
5495 ALC262_FIXUP_LENOVO_3000,
5496 ALC262_FIXUP_BENQ,
5497 ALC262_FIXUP_BENQ_T31,
5498 ALC262_FIXUP_INV_DMIC,
5499 };
5500
5501 static const struct alc_fixup alc262_fixups[] = {
5502 [ALC262_FIXUP_FSC_H270] = {
5503 .type = ALC_FIXUP_PINS,
5504 .v.pins = (const struct alc_pincfg[]) {
5505 { 0x14, 0x99130110 }, /* speaker */
5506 { 0x15, 0x0221142f }, /* front HP */
5507 { 0x1b, 0x0121141f }, /* rear HP */
5508 { }
5509 }
5510 },
5511 [ALC262_FIXUP_HP_Z200] = {
5512 .type = ALC_FIXUP_PINS,
5513 .v.pins = (const struct alc_pincfg[]) {
5514 { 0x16, 0x99130120 }, /* internal speaker */
5515 { }
5516 }
5517 },
5518 [ALC262_FIXUP_TYAN] = {
5519 .type = ALC_FIXUP_PINS,
5520 .v.pins = (const struct alc_pincfg[]) {
5521 { 0x14, 0x1993e1f0 }, /* int AUX */
5522 { }
5523 }
5524 },
5525 [ALC262_FIXUP_LENOVO_3000] = {
5526 .type = ALC_FIXUP_VERBS,
5527 .v.verbs = (const struct hda_verb[]) {
5528 { 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF50 },
5529 {}
5530 },
5531 .chained = true,
5532 .chain_id = ALC262_FIXUP_BENQ,
5533 },
5534 [ALC262_FIXUP_BENQ] = {
5535 .type = ALC_FIXUP_VERBS,
5536 .v.verbs = (const struct hda_verb[]) {
5537 { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5538 { 0x20, AC_VERB_SET_PROC_COEF, 0x3070 },
5539 {}
5540 }
5541 },
5542 [ALC262_FIXUP_BENQ_T31] = {
5543 .type = ALC_FIXUP_VERBS,
5544 .v.verbs = (const struct hda_verb[]) {
5545 { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5546 { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
5547 {}
5548 }
5549 },
5550 [ALC262_FIXUP_INV_DMIC] = {
5551 .type = ALC_FIXUP_FUNC,
5552 .v.func = alc_fixup_inv_dmic_0x12,
5553 },
5554 };
5555
5556 static const struct snd_pci_quirk alc262_fixup_tbl[] = {
5557 SND_PCI_QUIRK(0x103c, 0x170b, "HP Z200", ALC262_FIXUP_HP_Z200),
5558 SND_PCI_QUIRK(0x10cf, 0x1397, "Fujitsu", ALC262_FIXUP_BENQ),
5559 SND_PCI_QUIRK(0x10cf, 0x142d, "Fujitsu Lifebook E8410", ALC262_FIXUP_BENQ),
5560 SND_PCI_QUIRK(0x10f1, 0x2915, "Tyan Thunder n6650W", ALC262_FIXUP_TYAN),
5561 SND_PCI_QUIRK(0x1734, 0x1147, "FSC Celsius H270", ALC262_FIXUP_FSC_H270),
5562 SND_PCI_QUIRK(0x17aa, 0x384e, "Lenovo 3000", ALC262_FIXUP_LENOVO_3000),
5563 SND_PCI_QUIRK(0x17ff, 0x0560, "Benq ED8", ALC262_FIXUP_BENQ),
5564 SND_PCI_QUIRK(0x17ff, 0x058d, "Benq T31-16", ALC262_FIXUP_BENQ_T31),
5565 {}
5566 };
5567
5568 static const struct alc_model_fixup alc262_fixup_models[] = {
5569 {.id = ALC262_FIXUP_INV_DMIC, .name = "inv-dmic"},
5570 {}
5571 };
5572
5573 /*
5574 */
5575 static int patch_alc262(struct hda_codec *codec)
5576 {
5577 struct alc_spec *spec;
5578 int err;
5579
5580 err = alc_alloc_spec(codec, 0x0b);
5581 if (err < 0)
5582 return err;
5583
5584 spec = codec->spec;
5585
5586 #if 0
5587 /* pshou 07/11/05 set a zero PCM sample to DAC when FIFO is
5588 * under-run
5589 */
5590 {
5591 int tmp;
5592 snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
5593 tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
5594 snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
5595 snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PROC_COEF, tmp | 0x80);
5596 }
5597 #endif
5598 alc_fix_pll_init(codec, 0x20, 0x0a, 10);
5599
5600 alc_pick_fixup(codec, alc262_fixup_models, alc262_fixup_tbl,
5601 alc262_fixups);
5602 alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
5603
5604 alc_auto_parse_customize_define(codec);
5605
5606 /* automatic parse from the BIOS config */
5607 err = alc262_parse_auto_config(codec);
5608 if (err < 0)
5609 goto error;
5610
5611 if (!spec->no_analog && has_cdefine_beep(codec)) {
5612 err = snd_hda_attach_beep_device(codec, 0x1);
5613 if (err < 0)
5614 goto error;
5615 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
5616 }
5617
5618 codec->patch_ops = alc_patch_ops;
5619 spec->shutup = alc_eapd_shutup;
5620
5621 alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
5622
5623 return 0;
5624
5625 error:
5626 alc_free(codec);
5627 return err;
5628 }
5629
5630 /*
5631 * ALC268
5632 */
5633 /* bind Beep switches of both NID 0x0f and 0x10 */
5634 static const struct hda_bind_ctls alc268_bind_beep_sw = {
5635 .ops = &snd_hda_bind_sw,
5636 .values = {
5637 HDA_COMPOSE_AMP_VAL(0x0f, 3, 1, HDA_INPUT),
5638 HDA_COMPOSE_AMP_VAL(0x10, 3, 1, HDA_INPUT),
5639 0
5640 },
5641 };
5642
5643 static const struct snd_kcontrol_new alc268_beep_mixer[] = {
5644 HDA_CODEC_VOLUME("Beep Playback Volume", 0x1d, 0x0, HDA_INPUT),
5645 HDA_BIND_SW("Beep Playback Switch", &alc268_bind_beep_sw),
5646 { }
5647 };
5648
5649 /* set PCBEEP vol = 0, mute connections */
5650 static const struct hda_verb alc268_beep_init_verbs[] = {
5651 {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5652 {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5653 {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5654 { }
5655 };
5656
5657 enum {
5658 ALC268_FIXUP_INV_DMIC,
5659 };
5660
5661 static const struct alc_fixup alc268_fixups[] = {
5662 [ALC268_FIXUP_INV_DMIC] = {
5663 .type = ALC_FIXUP_FUNC,
5664 .v.func = alc_fixup_inv_dmic_0x12,
5665 },
5666 };
5667
5668 static const struct alc_model_fixup alc268_fixup_models[] = {
5669 {.id = ALC268_FIXUP_INV_DMIC, .name = "inv-dmic"},
5670 {}
5671 };
5672
5673 /*
5674 * BIOS auto configuration
5675 */
5676 static int alc268_parse_auto_config(struct hda_codec *codec)
5677 {
5678 static const hda_nid_t alc268_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5679 struct alc_spec *spec = codec->spec;
5680 int err = alc_parse_auto_config(codec, NULL, alc268_ssids);
5681 if (err > 0) {
5682 if (!spec->no_analog && spec->autocfg.speaker_pins[0] != 0x1d) {
5683 add_mixer(spec, alc268_beep_mixer);
5684 snd_hda_gen_add_verbs(&spec->gen, alc268_beep_init_verbs);
5685 }
5686 }
5687 return err;
5688 }
5689
5690 /*
5691 */
5692 static int patch_alc268(struct hda_codec *codec)
5693 {
5694 struct alc_spec *spec;
5695 int i, has_beep, err;
5696
5697 /* ALC268 has no aa-loopback mixer */
5698 err = alc_alloc_spec(codec, 0);
5699 if (err < 0)
5700 return err;
5701
5702 spec = codec->spec;
5703
5704 alc_pick_fixup(codec, alc268_fixup_models, NULL, alc268_fixups);
5705 alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
5706
5707 /* automatic parse from the BIOS config */
5708 err = alc268_parse_auto_config(codec);
5709 if (err < 0)
5710 goto error;
5711
5712 has_beep = 0;
5713 for (i = 0; i < spec->num_mixers; i++) {
5714 if (spec->mixers[i] == alc268_beep_mixer) {
5715 has_beep = 1;
5716 break;
5717 }
5718 }
5719
5720 if (has_beep) {
5721 err = snd_hda_attach_beep_device(codec, 0x1);
5722 if (err < 0)
5723 goto error;
5724 if (!query_amp_caps(codec, 0x1d, HDA_INPUT))
5725 /* override the amp caps for beep generator */
5726 snd_hda_override_amp_caps(codec, 0x1d, HDA_INPUT,
5727 (0x0c << AC_AMPCAP_OFFSET_SHIFT) |
5728 (0x0c << AC_AMPCAP_NUM_STEPS_SHIFT) |
5729 (0x07 << AC_AMPCAP_STEP_SIZE_SHIFT) |
5730 (0 << AC_AMPCAP_MUTE_SHIFT));
5731 }
5732
5733 codec->patch_ops = alc_patch_ops;
5734 spec->shutup = alc_eapd_shutup;
5735
5736 alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
5737
5738 return 0;
5739
5740 error:
5741 alc_free(codec);
5742 return err;
5743 }
5744
5745 /*
5746 * ALC269
5747 */
5748 static const struct hda_pcm_stream alc269_44k_pcm_analog_playback = {
5749 .substreams = 1,
5750 .channels_min = 2,
5751 .channels_max = 8,
5752 .rates = SNDRV_PCM_RATE_44100, /* fixed rate */
5753 /* NID is set in alc_build_pcms */
5754 .ops = {
5755 .open = alc_playback_pcm_open,
5756 .prepare = alc_playback_pcm_prepare,
5757 .cleanup = alc_playback_pcm_cleanup
5758 },
5759 };
5760
5761 static const struct hda_pcm_stream alc269_44k_pcm_analog_capture = {
5762 .substreams = 1,
5763 .channels_min = 2,
5764 .channels_max = 2,
5765 .rates = SNDRV_PCM_RATE_44100, /* fixed rate */
5766 /* NID is set in alc_build_pcms */
5767 };
5768
5769 /* different alc269-variants */
5770 enum {
5771 ALC269_TYPE_ALC269VA,
5772 ALC269_TYPE_ALC269VB,
5773 ALC269_TYPE_ALC269VC,
5774 ALC269_TYPE_ALC269VD,
5775 };
5776
5777 /*
5778 * BIOS auto configuration
5779 */
5780 static int alc269_parse_auto_config(struct hda_codec *codec)
5781 {
5782 static const hda_nid_t alc269_ignore[] = { 0x1d, 0 };
5783 static const hda_nid_t alc269_ssids[] = { 0, 0x1b, 0x14, 0x21 };
5784 static const hda_nid_t alc269va_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5785 struct alc_spec *spec = codec->spec;
5786 const hda_nid_t *ssids;
5787
5788 switch (spec->codec_variant) {
5789 case ALC269_TYPE_ALC269VA:
5790 case ALC269_TYPE_ALC269VC:
5791 ssids = alc269va_ssids;
5792 break;
5793 case ALC269_TYPE_ALC269VB:
5794 case ALC269_TYPE_ALC269VD:
5795 ssids = alc269_ssids;
5796 break;
5797 default:
5798 ssids = alc269_ssids;
5799 break;
5800 }
5801
5802 return alc_parse_auto_config(codec, alc269_ignore, ssids);
5803 }
5804
5805 static void alc269_toggle_power_output(struct hda_codec *codec, int power_up)
5806 {
5807 int val = alc_read_coef_idx(codec, 0x04);
5808 if (power_up)
5809 val |= 1 << 11;
5810 else
5811 val &= ~(1 << 11);
5812 alc_write_coef_idx(codec, 0x04, val);
5813 }
5814
5815 static void alc269_shutup(struct hda_codec *codec)
5816 {
5817 struct alc_spec *spec = codec->spec;
5818
5819 if (spec->codec_variant != ALC269_TYPE_ALC269VB)
5820 return;
5821
5822 if ((alc_get_coef0(codec) & 0x00ff) == 0x017)
5823 alc269_toggle_power_output(codec, 0);
5824 if ((alc_get_coef0(codec) & 0x00ff) == 0x018) {
5825 alc269_toggle_power_output(codec, 0);
5826 msleep(150);
5827 }
5828 }
5829
5830 #ifdef CONFIG_PM
5831 static int alc269_resume(struct hda_codec *codec)
5832 {
5833 struct alc_spec *spec = codec->spec;
5834
5835 if (spec->codec_variant == ALC269_TYPE_ALC269VB ||
5836 (alc_get_coef0(codec) & 0x00ff) == 0x018) {
5837 alc269_toggle_power_output(codec, 0);
5838 msleep(150);
5839 }
5840
5841 codec->patch_ops.init(codec);
5842
5843 if (spec->codec_variant == ALC269_TYPE_ALC269VB ||
5844 (alc_get_coef0(codec) & 0x00ff) == 0x017) {
5845 alc269_toggle_power_output(codec, 1);
5846 msleep(200);
5847 }
5848
5849 if (spec->codec_variant == ALC269_TYPE_ALC269VB ||
5850 (alc_get_coef0(codec) & 0x00ff) == 0x018)
5851 alc269_toggle_power_output(codec, 1);
5852
5853 snd_hda_codec_resume_amp(codec);
5854 snd_hda_codec_resume_cache(codec);
5855 hda_call_check_power_status(codec, 0x01);
5856 return 0;
5857 }
5858 #endif /* CONFIG_PM */
5859
5860 static void alc269_fixup_hweq(struct hda_codec *codec,
5861 const struct alc_fixup *fix, int action)
5862 {
5863 int coef;
5864
5865 if (action != ALC_FIXUP_ACT_INIT)
5866 return;
5867 coef = alc_read_coef_idx(codec, 0x1e);
5868 alc_write_coef_idx(codec, 0x1e, coef | 0x80);
5869 }
5870
5871 static void alc271_fixup_dmic(struct hda_codec *codec,
5872 const struct alc_fixup *fix, int action)
5873 {
5874 static const struct hda_verb verbs[] = {
5875 {0x20, AC_VERB_SET_COEF_INDEX, 0x0d},
5876 {0x20, AC_VERB_SET_PROC_COEF, 0x4000},
5877 {}
5878 };
5879 unsigned int cfg;
5880
5881 if (strcmp(codec->chip_name, "ALC271X"))
5882 return;
5883 cfg = snd_hda_codec_get_pincfg(codec, 0x12);
5884 if (get_defcfg_connect(cfg) == AC_JACK_PORT_FIXED)
5885 snd_hda_sequence_write(codec, verbs);
5886 }
5887
5888 static void alc269_fixup_pcm_44k(struct hda_codec *codec,
5889 const struct alc_fixup *fix, int action)
5890 {
5891 struct alc_spec *spec = codec->spec;
5892
5893 if (action != ALC_FIXUP_ACT_PROBE)
5894 return;
5895
5896 /* Due to a hardware problem on Lenovo Ideadpad, we need to
5897 * fix the sample rate of analog I/O to 44.1kHz
5898 */
5899 spec->stream_analog_playback = &alc269_44k_pcm_analog_playback;
5900 spec->stream_analog_capture = &alc269_44k_pcm_analog_capture;
5901 }
5902
5903 static void alc269_fixup_stereo_dmic(struct hda_codec *codec,
5904 const struct alc_fixup *fix, int action)
5905 {
5906 int coef;
5907
5908 if (action != ALC_FIXUP_ACT_INIT)
5909 return;
5910 /* The digital-mic unit sends PDM (differential signal) instead of
5911 * the standard PCM, thus you can't record a valid mono stream as is.
5912 * Below is a workaround specific to ALC269 to control the dmic
5913 * signal source as mono.
5914 */
5915 coef = alc_read_coef_idx(codec, 0x07);
5916 alc_write_coef_idx(codec, 0x07, coef | 0x80);
5917 }
5918
5919 static void alc269_quanta_automute(struct hda_codec *codec)
5920 {
5921 update_outputs(codec);
5922
5923 snd_hda_codec_write(codec, 0x20, 0,
5924 AC_VERB_SET_COEF_INDEX, 0x0c);
5925 snd_hda_codec_write(codec, 0x20, 0,
5926 AC_VERB_SET_PROC_COEF, 0x680);
5927
5928 snd_hda_codec_write(codec, 0x20, 0,
5929 AC_VERB_SET_COEF_INDEX, 0x0c);
5930 snd_hda_codec_write(codec, 0x20, 0,
5931 AC_VERB_SET_PROC_COEF, 0x480);
5932 }
5933
5934 static void alc269_fixup_quanta_mute(struct hda_codec *codec,
5935 const struct alc_fixup *fix, int action)
5936 {
5937 struct alc_spec *spec = codec->spec;
5938 if (action != ALC_FIXUP_ACT_PROBE)
5939 return;
5940 spec->automute_hook = alc269_quanta_automute;
5941 }
5942
5943 /* update mute-LED according to the speaker mute state via mic2 VREF pin */
5944 static void alc269_fixup_mic2_mute_hook(void *private_data, int enabled)
5945 {
5946 struct hda_codec *codec = private_data;
5947 unsigned int pinval = enabled ? 0x20 : 0x24;
5948 snd_hda_set_pin_ctl_cache(codec, 0x19, pinval);
5949 }
5950
5951 static void alc269_fixup_mic2_mute(struct hda_codec *codec,
5952 const struct alc_fixup *fix, int action)
5953 {
5954 struct alc_spec *spec = codec->spec;
5955 switch (action) {
5956 case ALC_FIXUP_ACT_BUILD:
5957 spec->vmaster_mute.hook = alc269_fixup_mic2_mute_hook;
5958 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute, true);
5959 /* fallthru */
5960 case ALC_FIXUP_ACT_INIT:
5961 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5962 break;
5963 }
5964 }
5965
5966
5967 enum {
5968 ALC269_FIXUP_SONY_VAIO,
5969 ALC275_FIXUP_SONY_VAIO_GPIO2,
5970 ALC269_FIXUP_DELL_M101Z,
5971 ALC269_FIXUP_SKU_IGNORE,
5972 ALC269_FIXUP_ASUS_G73JW,
5973 ALC269_FIXUP_LENOVO_EAPD,
5974 ALC275_FIXUP_SONY_HWEQ,
5975 ALC271_FIXUP_DMIC,
5976 ALC269_FIXUP_PCM_44K,
5977 ALC269_FIXUP_STEREO_DMIC,
5978 ALC269_FIXUP_QUANTA_MUTE,
5979 ALC269_FIXUP_LIFEBOOK,
5980 ALC269_FIXUP_AMIC,
5981 ALC269_FIXUP_DMIC,
5982 ALC269VB_FIXUP_AMIC,
5983 ALC269VB_FIXUP_DMIC,
5984 ALC269_FIXUP_MIC2_MUTE_LED,
5985 ALC269_FIXUP_INV_DMIC,
5986 };
5987
5988 static const struct alc_fixup alc269_fixups[] = {
5989 [ALC269_FIXUP_SONY_VAIO] = {
5990 .type = ALC_FIXUP_VERBS,
5991 .v.verbs = (const struct hda_verb[]) {
5992 {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREFGRD},
5993 {}
5994 }
5995 },
5996 [ALC275_FIXUP_SONY_VAIO_GPIO2] = {
5997 .type = ALC_FIXUP_VERBS,
5998 .v.verbs = (const struct hda_verb[]) {
5999 {0x01, AC_VERB_SET_GPIO_MASK, 0x04},
6000 {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x04},
6001 {0x01, AC_VERB_SET_GPIO_DATA, 0x00},
6002 { }
6003 },
6004 .chained = true,
6005 .chain_id = ALC269_FIXUP_SONY_VAIO
6006 },
6007 [ALC269_FIXUP_DELL_M101Z] = {
6008 .type = ALC_FIXUP_VERBS,
6009 .v.verbs = (const struct hda_verb[]) {
6010 /* Enables internal speaker */
6011 {0x20, AC_VERB_SET_COEF_INDEX, 13},
6012 {0x20, AC_VERB_SET_PROC_COEF, 0x4040},
6013 {}
6014 }
6015 },
6016 [ALC269_FIXUP_SKU_IGNORE] = {
6017 .type = ALC_FIXUP_FUNC,
6018 .v.func = alc_fixup_sku_ignore,
6019 },
6020 [ALC269_FIXUP_ASUS_G73JW] = {
6021 .type = ALC_FIXUP_PINS,
6022 .v.pins = (const struct alc_pincfg[]) {
6023 { 0x17, 0x99130111 }, /* subwoofer */
6024 { }
6025 }
6026 },
6027 [ALC269_FIXUP_LENOVO_EAPD] = {
6028 .type = ALC_FIXUP_VERBS,
6029 .v.verbs = (const struct hda_verb[]) {
6030 {0x14, AC_VERB_SET_EAPD_BTLENABLE, 0},
6031 {}
6032 }
6033 },
6034 [ALC275_FIXUP_SONY_HWEQ] = {
6035 .type = ALC_FIXUP_FUNC,
6036 .v.func = alc269_fixup_hweq,
6037 .chained = true,
6038 .chain_id = ALC275_FIXUP_SONY_VAIO_GPIO2
6039 },
6040 [ALC271_FIXUP_DMIC] = {
6041 .type = ALC_FIXUP_FUNC,
6042 .v.func = alc271_fixup_dmic,
6043 },
6044 [ALC269_FIXUP_PCM_44K] = {
6045 .type = ALC_FIXUP_FUNC,
6046 .v.func = alc269_fixup_pcm_44k,
6047 },
6048 [ALC269_FIXUP_STEREO_DMIC] = {
6049 .type = ALC_FIXUP_FUNC,
6050 .v.func = alc269_fixup_stereo_dmic,
6051 },
6052 [ALC269_FIXUP_QUANTA_MUTE] = {
6053 .type = ALC_FIXUP_FUNC,
6054 .v.func = alc269_fixup_quanta_mute,
6055 },
6056 [ALC269_FIXUP_LIFEBOOK] = {
6057 .type = ALC_FIXUP_PINS,
6058 .v.pins = (const struct alc_pincfg[]) {
6059 { 0x1a, 0x2101103f }, /* dock line-out */
6060 { 0x1b, 0x23a11040 }, /* dock mic-in */
6061 { }
6062 },
6063 .chained = true,
6064 .chain_id = ALC269_FIXUP_QUANTA_MUTE
6065 },
6066 [ALC269_FIXUP_AMIC] = {
6067 .type = ALC_FIXUP_PINS,
6068 .v.pins = (const struct alc_pincfg[]) {
6069 { 0x14, 0x99130110 }, /* speaker */
6070 { 0x15, 0x0121401f }, /* HP out */
6071 { 0x18, 0x01a19c20 }, /* mic */
6072 { 0x19, 0x99a3092f }, /* int-mic */
6073 { }
6074 },
6075 },
6076 [ALC269_FIXUP_DMIC] = {
6077 .type = ALC_FIXUP_PINS,
6078 .v.pins = (const struct alc_pincfg[]) {
6079 { 0x12, 0x99a3092f }, /* int-mic */
6080 { 0x14, 0x99130110 }, /* speaker */
6081 { 0x15, 0x0121401f }, /* HP out */
6082 { 0x18, 0x01a19c20 }, /* mic */
6083 { }
6084 },
6085 },
6086 [ALC269VB_FIXUP_AMIC] = {
6087 .type = ALC_FIXUP_PINS,
6088 .v.pins = (const struct alc_pincfg[]) {
6089 { 0x14, 0x99130110 }, /* speaker */
6090 { 0x18, 0x01a19c20 }, /* mic */
6091 { 0x19, 0x99a3092f }, /* int-mic */
6092 { 0x21, 0x0121401f }, /* HP out */
6093 { }
6094 },
6095 },
6096 [ALC269VB_FIXUP_DMIC] = {
6097 .type = ALC_FIXUP_PINS,
6098 .v.pins = (const struct alc_pincfg[]) {
6099 { 0x12, 0x99a3092f }, /* int-mic */
6100 { 0x14, 0x99130110 }, /* speaker */
6101 { 0x18, 0x01a19c20 }, /* mic */
6102 { 0x21, 0x0121401f }, /* HP out */
6103 { }
6104 },
6105 },
6106 [ALC269_FIXUP_MIC2_MUTE_LED] = {
6107 .type = ALC_FIXUP_FUNC,
6108 .v.func = alc269_fixup_mic2_mute,
6109 },
6110 [ALC269_FIXUP_INV_DMIC] = {
6111 .type = ALC_FIXUP_FUNC,
6112 .v.func = alc_fixup_inv_dmic_0x12,
6113 },
6114 };
6115
6116 static const struct snd_pci_quirk alc269_fixup_tbl[] = {
6117 SND_PCI_QUIRK(0x1025, 0x029b, "Acer 1810TZ", ALC269_FIXUP_INV_DMIC),
6118 SND_PCI_QUIRK(0x1025, 0x0349, "Acer AOD260", ALC269_FIXUP_INV_DMIC),
6119 SND_PCI_QUIRK(0x103c, 0x1586, "HP", ALC269_FIXUP_MIC2_MUTE_LED),
6120 SND_PCI_QUIRK(0x1043, 0x1427, "Asus Zenbook UX31E", ALC269VB_FIXUP_DMIC),
6121 SND_PCI_QUIRK(0x1043, 0x1a13, "Asus G73Jw", ALC269_FIXUP_ASUS_G73JW),
6122 SND_PCI_QUIRK(0x1043, 0x1b13, "Asus U41SV", ALC269_FIXUP_INV_DMIC),
6123 SND_PCI_QUIRK(0x1043, 0x16e3, "ASUS UX50", ALC269_FIXUP_STEREO_DMIC),
6124 SND_PCI_QUIRK(0x1043, 0x831a, "ASUS P901", ALC269_FIXUP_STEREO_DMIC),
6125 SND_PCI_QUIRK(0x1043, 0x834a, "ASUS S101", ALC269_FIXUP_STEREO_DMIC),
6126 SND_PCI_QUIRK(0x1043, 0x8398, "ASUS P1005", ALC269_FIXUP_STEREO_DMIC),
6127 SND_PCI_QUIRK(0x1043, 0x83ce, "ASUS P1005", ALC269_FIXUP_STEREO_DMIC),
6128 SND_PCI_QUIRK(0x104d, 0x9073, "Sony VAIO", ALC275_FIXUP_SONY_VAIO_GPIO2),
6129 SND_PCI_QUIRK(0x104d, 0x907b, "Sony VAIO", ALC275_FIXUP_SONY_HWEQ),
6130 SND_PCI_QUIRK(0x104d, 0x9084, "Sony VAIO", ALC275_FIXUP_SONY_HWEQ),
6131 SND_PCI_QUIRK_VENDOR(0x104d, "Sony VAIO", ALC269_FIXUP_SONY_VAIO),
6132 SND_PCI_QUIRK(0x1028, 0x0470, "Dell M101z", ALC269_FIXUP_DELL_M101Z),
6133 SND_PCI_QUIRK_VENDOR(0x1025, "Acer Aspire", ALC271_FIXUP_DMIC),
6134 SND_PCI_QUIRK(0x10cf, 0x1475, "Lifebook", ALC269_FIXUP_LIFEBOOK),
6135 SND_PCI_QUIRK(0x17aa, 0x20f2, "Thinkpad SL410/510", ALC269_FIXUP_SKU_IGNORE),
6136 SND_PCI_QUIRK(0x17aa, 0x215e, "Thinkpad L512", ALC269_FIXUP_SKU_IGNORE),
6137 SND_PCI_QUIRK(0x17aa, 0x21b8, "Thinkpad Edge 14", ALC269_FIXUP_SKU_IGNORE),
6138 SND_PCI_QUIRK(0x17aa, 0x21ca, "Thinkpad L412", ALC269_FIXUP_SKU_IGNORE),
6139 SND_PCI_QUIRK(0x17aa, 0x21e9, "Thinkpad Edge 15", ALC269_FIXUP_SKU_IGNORE),
6140 SND_PCI_QUIRK(0x17aa, 0x3bf8, "Quanta FL1", ALC269_FIXUP_QUANTA_MUTE),
6141 SND_PCI_QUIRK(0x17aa, 0x3bf8, "Lenovo Ideapd", ALC269_FIXUP_PCM_44K),
6142 SND_PCI_QUIRK(0x17aa, 0x9e54, "LENOVO NB", ALC269_FIXUP_LENOVO_EAPD),
6143
6144 #if 0
6145 /* Below is a quirk table taken from the old code.
6146 * Basically the device should work as is without the fixup table.
6147 * If BIOS doesn't give a proper info, enable the corresponding
6148 * fixup entry.
6149 */
6150 SND_PCI_QUIRK(0x1043, 0x8330, "ASUS Eeepc P703 P900A",
6151 ALC269_FIXUP_AMIC),
6152 SND_PCI_QUIRK(0x1043, 0x1013, "ASUS N61Da", ALC269_FIXUP_AMIC),
6153 SND_PCI_QUIRK(0x1043, 0x1143, "ASUS B53f", ALC269_FIXUP_AMIC),
6154 SND_PCI_QUIRK(0x1043, 0x1133, "ASUS UJ20ft", ALC269_FIXUP_AMIC),
6155 SND_PCI_QUIRK(0x1043, 0x1183, "ASUS K72DR", ALC269_FIXUP_AMIC),
6156 SND_PCI_QUIRK(0x1043, 0x11b3, "ASUS K52DR", ALC269_FIXUP_AMIC),
6157 SND_PCI_QUIRK(0x1043, 0x11e3, "ASUS U33Jc", ALC269_FIXUP_AMIC),
6158 SND_PCI_QUIRK(0x1043, 0x1273, "ASUS UL80Jt", ALC269_FIXUP_AMIC),
6159 SND_PCI_QUIRK(0x1043, 0x1283, "ASUS U53Jc", ALC269_FIXUP_AMIC),
6160 SND_PCI_QUIRK(0x1043, 0x12b3, "ASUS N82JV", ALC269_FIXUP_AMIC),
6161 SND_PCI_QUIRK(0x1043, 0x12d3, "ASUS N61Jv", ALC269_FIXUP_AMIC),
6162 SND_PCI_QUIRK(0x1043, 0x13a3, "ASUS UL30Vt", ALC269_FIXUP_AMIC),
6163 SND_PCI_QUIRK(0x1043, 0x1373, "ASUS G73JX", ALC269_FIXUP_AMIC),
6164 SND_PCI_QUIRK(0x1043, 0x1383, "ASUS UJ30Jc", ALC269_FIXUP_AMIC),
6165 SND_PCI_QUIRK(0x1043, 0x13d3, "ASUS N61JA", ALC269_FIXUP_AMIC),
6166 SND_PCI_QUIRK(0x1043, 0x1413, "ASUS UL50", ALC269_FIXUP_AMIC),
6167 SND_PCI_QUIRK(0x1043, 0x1443, "ASUS UL30", ALC269_FIXUP_AMIC),
6168 SND_PCI_QUIRK(0x1043, 0x1453, "ASUS M60Jv", ALC269_FIXUP_AMIC),
6169 SND_PCI_QUIRK(0x1043, 0x1483, "ASUS UL80", ALC269_FIXUP_AMIC),
6170 SND_PCI_QUIRK(0x1043, 0x14f3, "ASUS F83Vf", ALC269_FIXUP_AMIC),
6171 SND_PCI_QUIRK(0x1043, 0x14e3, "ASUS UL20", ALC269_FIXUP_AMIC),
6172 SND_PCI_QUIRK(0x1043, 0x1513, "ASUS UX30", ALC269_FIXUP_AMIC),
6173 SND_PCI_QUIRK(0x1043, 0x1593, "ASUS N51Vn", ALC269_FIXUP_AMIC),
6174 SND_PCI_QUIRK(0x1043, 0x15a3, "ASUS N60Jv", ALC269_FIXUP_AMIC),
6175 SND_PCI_QUIRK(0x1043, 0x15b3, "ASUS N60Dp", ALC269_FIXUP_AMIC),
6176 SND_PCI_QUIRK(0x1043, 0x15c3, "ASUS N70De", ALC269_FIXUP_AMIC),
6177 SND_PCI_QUIRK(0x1043, 0x15e3, "ASUS F83T", ALC269_FIXUP_AMIC),
6178 SND_PCI_QUIRK(0x1043, 0x1643, "ASUS M60J", ALC269_FIXUP_AMIC),
6179 SND_PCI_QUIRK(0x1043, 0x1653, "ASUS U50", ALC269_FIXUP_AMIC),
6180 SND_PCI_QUIRK(0x1043, 0x1693, "ASUS F50N", ALC269_FIXUP_AMIC),
6181 SND_PCI_QUIRK(0x1043, 0x16a3, "ASUS F5Q", ALC269_FIXUP_AMIC),
6182 SND_PCI_QUIRK(0x1043, 0x1723, "ASUS P80", ALC269_FIXUP_AMIC),
6183 SND_PCI_QUIRK(0x1043, 0x1743, "ASUS U80", ALC269_FIXUP_AMIC),
6184 SND_PCI_QUIRK(0x1043, 0x1773, "ASUS U20A", ALC269_FIXUP_AMIC),
6185 SND_PCI_QUIRK(0x1043, 0x1883, "ASUS F81Se", ALC269_FIXUP_AMIC),
6186 SND_PCI_QUIRK(0x152d, 0x1778, "Quanta ON1", ALC269_FIXUP_DMIC),
6187 SND_PCI_QUIRK(0x17aa, 0x3be9, "Quanta Wistron", ALC269_FIXUP_AMIC),
6188 SND_PCI_QUIRK(0x17aa, 0x3bf8, "Quanta FL1", ALC269_FIXUP_AMIC),
6189 SND_PCI_QUIRK(0x17ff, 0x059a, "Quanta EL3", ALC269_FIXUP_DMIC),
6190 SND_PCI_QUIRK(0x17ff, 0x059b, "Quanta JR1", ALC269_FIXUP_DMIC),
6191 #endif
6192 {}
6193 };
6194
6195 static const struct alc_model_fixup alc269_fixup_models[] = {
6196 {.id = ALC269_FIXUP_AMIC, .name = "laptop-amic"},
6197 {.id = ALC269_FIXUP_DMIC, .name = "laptop-dmic"},
6198 {.id = ALC269_FIXUP_STEREO_DMIC, .name = "alc269-dmic"},
6199 {.id = ALC271_FIXUP_DMIC, .name = "alc271-dmic"},
6200 {.id = ALC269_FIXUP_INV_DMIC, .name = "inv-dmic"},
6201 {}
6202 };
6203
6204
6205 static void alc269_fill_coef(struct hda_codec *codec)
6206 {
6207 struct alc_spec *spec = codec->spec;
6208 int val;
6209
6210 if (spec->codec_variant != ALC269_TYPE_ALC269VB)
6211 return;
6212
6213 if ((alc_get_coef0(codec) & 0x00ff) < 0x015) {
6214 alc_write_coef_idx(codec, 0xf, 0x960b);
6215 alc_write_coef_idx(codec, 0xe, 0x8817);
6216 }
6217
6218 if ((alc_get_coef0(codec) & 0x00ff) == 0x016) {
6219 alc_write_coef_idx(codec, 0xf, 0x960b);
6220 alc_write_coef_idx(codec, 0xe, 0x8814);
6221 }
6222
6223 if ((alc_get_coef0(codec) & 0x00ff) == 0x017) {
6224 val = alc_read_coef_idx(codec, 0x04);
6225 /* Power up output pin */
6226 alc_write_coef_idx(codec, 0x04, val | (1<<11));
6227 }
6228
6229 if ((alc_get_coef0(codec) & 0x00ff) == 0x018) {
6230 val = alc_read_coef_idx(codec, 0xd);
6231 if ((val & 0x0c00) >> 10 != 0x1) {
6232 /* Capless ramp up clock control */
6233 alc_write_coef_idx(codec, 0xd, val | (1<<10));
6234 }
6235 val = alc_read_coef_idx(codec, 0x17);
6236 if ((val & 0x01c0) >> 6 != 0x4) {
6237 /* Class D power on reset */
6238 alc_write_coef_idx(codec, 0x17, val | (1<<7));
6239 }
6240 }
6241
6242 val = alc_read_coef_idx(codec, 0xd); /* Class D */
6243 alc_write_coef_idx(codec, 0xd, val | (1<<14));
6244
6245 val = alc_read_coef_idx(codec, 0x4); /* HP */
6246 alc_write_coef_idx(codec, 0x4, val | (1<<11));
6247 }
6248
6249 /*
6250 */
6251 static int patch_alc269(struct hda_codec *codec)
6252 {
6253 struct alc_spec *spec;
6254 int err;
6255
6256 err = alc_alloc_spec(codec, 0x0b);
6257 if (err < 0)
6258 return err;
6259
6260 spec = codec->spec;
6261
6262 if (codec->vendor_id == 0x10ec0269) {
6263 spec->codec_variant = ALC269_TYPE_ALC269VA;
6264 switch (alc_get_coef0(codec) & 0x00f0) {
6265 case 0x0010:
6266 if (codec->bus->pci->subsystem_vendor == 0x1025 &&
6267 spec->cdefine.platform_type == 1)
6268 err = alc_codec_rename(codec, "ALC271X");
6269 spec->codec_variant = ALC269_TYPE_ALC269VB;
6270 break;
6271 case 0x0020:
6272 if (codec->bus->pci->subsystem_vendor == 0x17aa &&
6273 codec->bus->pci->subsystem_device == 0x21f3)
6274 err = alc_codec_rename(codec, "ALC3202");
6275 spec->codec_variant = ALC269_TYPE_ALC269VC;
6276 break;
6277 case 0x0030:
6278 spec->codec_variant = ALC269_TYPE_ALC269VD;
6279 break;
6280 default:
6281 alc_fix_pll_init(codec, 0x20, 0x04, 15);
6282 }
6283 if (err < 0)
6284 goto error;
6285 spec->init_hook = alc269_fill_coef;
6286 alc269_fill_coef(codec);
6287 }
6288
6289 alc_pick_fixup(codec, alc269_fixup_models,
6290 alc269_fixup_tbl, alc269_fixups);
6291 alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
6292
6293 alc_auto_parse_customize_define(codec);
6294
6295 /* automatic parse from the BIOS config */
6296 err = alc269_parse_auto_config(codec);
6297 if (err < 0)
6298 goto error;
6299
6300 if (!spec->no_analog && has_cdefine_beep(codec)) {
6301 err = snd_hda_attach_beep_device(codec, 0x1);
6302 if (err < 0)
6303 goto error;
6304 set_beep_amp(spec, 0x0b, 0x04, HDA_INPUT);
6305 }
6306
6307 codec->patch_ops = alc_patch_ops;
6308 #ifdef CONFIG_PM
6309 codec->patch_ops.resume = alc269_resume;
6310 #endif
6311 spec->shutup = alc269_shutup;
6312
6313 alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
6314
6315 return 0;
6316
6317 error:
6318 alc_free(codec);
6319 return err;
6320 }
6321
6322 /*
6323 * ALC861
6324 */
6325
6326 static int alc861_parse_auto_config(struct hda_codec *codec)
6327 {
6328 static const hda_nid_t alc861_ignore[] = { 0x1d, 0 };
6329 static const hda_nid_t alc861_ssids[] = { 0x0e, 0x0f, 0x0b, 0 };
6330 return alc_parse_auto_config(codec, alc861_ignore, alc861_ssids);
6331 }
6332
6333 /* Pin config fixes */
6334 enum {
6335 ALC861_FIXUP_FSC_AMILO_PI1505,
6336 ALC861_FIXUP_AMP_VREF_0F,
6337 ALC861_FIXUP_NO_JACK_DETECT,
6338 ALC861_FIXUP_ASUS_A6RP,
6339 };
6340
6341 /* On some laptops, VREF of pin 0x0f is abused for controlling the main amp */
6342 static void alc861_fixup_asus_amp_vref_0f(struct hda_codec *codec,
6343 const struct alc_fixup *fix, int action)
6344 {
6345 struct alc_spec *spec = codec->spec;
6346 unsigned int val;
6347
6348 if (action != ALC_FIXUP_ACT_INIT)
6349 return;
6350 val = snd_hda_codec_read(codec, 0x0f, 0,
6351 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
6352 if (!(val & (AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN)))
6353 val |= AC_PINCTL_IN_EN;
6354 val |= AC_PINCTL_VREF_50;
6355 snd_hda_set_pin_ctl(codec, 0x0f, val);
6356 spec->keep_vref_in_automute = 1;
6357 }
6358
6359 /* suppress the jack-detection */
6360 static void alc_fixup_no_jack_detect(struct hda_codec *codec,
6361 const struct alc_fixup *fix, int action)
6362 {
6363 if (action == ALC_FIXUP_ACT_PRE_PROBE)
6364 codec->no_jack_detect = 1;
6365 }
6366
6367 static const struct alc_fixup alc861_fixups[] = {
6368 [ALC861_FIXUP_FSC_AMILO_PI1505] = {
6369 .type = ALC_FIXUP_PINS,
6370 .v.pins = (const struct alc_pincfg[]) {
6371 { 0x0b, 0x0221101f }, /* HP */
6372 { 0x0f, 0x90170310 }, /* speaker */
6373 { }
6374 }
6375 },
6376 [ALC861_FIXUP_AMP_VREF_0F] = {
6377 .type = ALC_FIXUP_FUNC,
6378 .v.func = alc861_fixup_asus_amp_vref_0f,
6379 },
6380 [ALC861_FIXUP_NO_JACK_DETECT] = {
6381 .type = ALC_FIXUP_FUNC,
6382 .v.func = alc_fixup_no_jack_detect,
6383 },
6384 [ALC861_FIXUP_ASUS_A6RP] = {
6385 .type = ALC_FIXUP_FUNC,
6386 .v.func = alc861_fixup_asus_amp_vref_0f,
6387 .chained = true,
6388 .chain_id = ALC861_FIXUP_NO_JACK_DETECT,
6389 }
6390 };
6391
6392 static const struct snd_pci_quirk alc861_fixup_tbl[] = {
6393 SND_PCI_QUIRK(0x1043, 0x1393, "ASUS A6Rp", ALC861_FIXUP_ASUS_A6RP),
6394 SND_PCI_QUIRK_VENDOR(0x1043, "ASUS laptop", ALC861_FIXUP_AMP_VREF_0F),
6395 SND_PCI_QUIRK(0x1462, 0x7254, "HP DX2200", ALC861_FIXUP_NO_JACK_DETECT),
6396 SND_PCI_QUIRK(0x1584, 0x2b01, "Haier W18", ALC861_FIXUP_AMP_VREF_0F),
6397 SND_PCI_QUIRK(0x1584, 0x0000, "Uniwill ECS M31EI", ALC861_FIXUP_AMP_VREF_0F),
6398 SND_PCI_QUIRK(0x1734, 0x10c7, "FSC Amilo Pi1505", ALC861_FIXUP_FSC_AMILO_PI1505),
6399 {}
6400 };
6401
6402 /*
6403 */
6404 static int patch_alc861(struct hda_codec *codec)
6405 {
6406 struct alc_spec *spec;
6407 int err;
6408
6409 err = alc_alloc_spec(codec, 0x15);
6410 if (err < 0)
6411 return err;
6412
6413 spec = codec->spec;
6414
6415 alc_pick_fixup(codec, NULL, alc861_fixup_tbl, alc861_fixups);
6416 alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
6417
6418 /* automatic parse from the BIOS config */
6419 err = alc861_parse_auto_config(codec);
6420 if (err < 0)
6421 goto error;
6422
6423 if (!spec->no_analog) {
6424 err = snd_hda_attach_beep_device(codec, 0x23);
6425 if (err < 0)
6426 goto error;
6427 set_beep_amp(spec, 0x23, 0, HDA_OUTPUT);
6428 }
6429
6430 codec->patch_ops = alc_patch_ops;
6431 #ifdef CONFIG_SND_HDA_POWER_SAVE
6432 spec->power_hook = alc_power_eapd;
6433 #endif
6434
6435 alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
6436
6437 return 0;
6438
6439 error:
6440 alc_free(codec);
6441 return err;
6442 }
6443
6444 /*
6445 * ALC861-VD support
6446 *
6447 * Based on ALC882
6448 *
6449 * In addition, an independent DAC
6450 */
6451 static int alc861vd_parse_auto_config(struct hda_codec *codec)
6452 {
6453 static const hda_nid_t alc861vd_ignore[] = { 0x1d, 0 };
6454 static const hda_nid_t alc861vd_ssids[] = { 0x15, 0x1b, 0x14, 0 };
6455 return alc_parse_auto_config(codec, alc861vd_ignore, alc861vd_ssids);
6456 }
6457
6458 enum {
6459 ALC660VD_FIX_ASUS_GPIO1,
6460 ALC861VD_FIX_DALLAS,
6461 };
6462
6463 /* exclude VREF80 */
6464 static void alc861vd_fixup_dallas(struct hda_codec *codec,
6465 const struct alc_fixup *fix, int action)
6466 {
6467 if (action == ALC_FIXUP_ACT_PRE_PROBE) {
6468 snd_hda_override_pin_caps(codec, 0x18, 0x00001714);
6469 snd_hda_override_pin_caps(codec, 0x19, 0x0000171c);
6470 }
6471 }
6472
6473 static const struct alc_fixup alc861vd_fixups[] = {
6474 [ALC660VD_FIX_ASUS_GPIO1] = {
6475 .type = ALC_FIXUP_VERBS,
6476 .v.verbs = (const struct hda_verb[]) {
6477 /* reset GPIO1 */
6478 {0x01, AC_VERB_SET_GPIO_MASK, 0x03},
6479 {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
6480 {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
6481 { }
6482 }
6483 },
6484 [ALC861VD_FIX_DALLAS] = {
6485 .type = ALC_FIXUP_FUNC,
6486 .v.func = alc861vd_fixup_dallas,
6487 },
6488 };
6489
6490 static const struct snd_pci_quirk alc861vd_fixup_tbl[] = {
6491 SND_PCI_QUIRK(0x103c, 0x30bf, "HP TX1000", ALC861VD_FIX_DALLAS),
6492 SND_PCI_QUIRK(0x1043, 0x1339, "ASUS A7-K", ALC660VD_FIX_ASUS_GPIO1),
6493 SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba L30-149", ALC861VD_FIX_DALLAS),
6494 {}
6495 };
6496
6497 static const struct hda_verb alc660vd_eapd_verbs[] = {
6498 {0x14, AC_VERB_SET_EAPD_BTLENABLE, 2},
6499 {0x15, AC_VERB_SET_EAPD_BTLENABLE, 2},
6500 { }
6501 };
6502
6503 /*
6504 */
6505 static int patch_alc861vd(struct hda_codec *codec)
6506 {
6507 struct alc_spec *spec;
6508 int err;
6509
6510 err = alc_alloc_spec(codec, 0x0b);
6511 if (err < 0)
6512 return err;
6513
6514 spec = codec->spec;
6515
6516 alc_pick_fixup(codec, NULL, alc861vd_fixup_tbl, alc861vd_fixups);
6517 alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
6518
6519 /* automatic parse from the BIOS config */
6520 err = alc861vd_parse_auto_config(codec);
6521 if (err < 0)
6522 goto error;
6523
6524 if (codec->vendor_id == 0x10ec0660) {
6525 /* always turn on EAPD */
6526 snd_hda_gen_add_verbs(&spec->gen, alc660vd_eapd_verbs);
6527 }
6528
6529 if (!spec->no_analog) {
6530 err = snd_hda_attach_beep_device(codec, 0x23);
6531 if (err < 0)
6532 goto error;
6533 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
6534 }
6535
6536 codec->patch_ops = alc_patch_ops;
6537
6538 spec->shutup = alc_eapd_shutup;
6539
6540 alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
6541
6542 return 0;
6543
6544 error:
6545 alc_free(codec);
6546 return err;
6547 }
6548
6549 /*
6550 * ALC662 support
6551 *
6552 * ALC662 is almost identical with ALC880 but has cleaner and more flexible
6553 * configuration. Each pin widget can choose any input DACs and a mixer.
6554 * Each ADC is connected from a mixer of all inputs. This makes possible
6555 * 6-channel independent captures.
6556 *
6557 * In addition, an independent DAC for the multi-playback (not used in this
6558 * driver yet).
6559 */
6560
6561 /*
6562 * BIOS auto configuration
6563 */
6564
6565 static int alc662_parse_auto_config(struct hda_codec *codec)
6566 {
6567 static const hda_nid_t alc662_ignore[] = { 0x1d, 0 };
6568 static const hda_nid_t alc663_ssids[] = { 0x15, 0x1b, 0x14, 0x21 };
6569 static const hda_nid_t alc662_ssids[] = { 0x15, 0x1b, 0x14, 0 };
6570 const hda_nid_t *ssids;
6571
6572 if (codec->vendor_id == 0x10ec0272 || codec->vendor_id == 0x10ec0663 ||
6573 codec->vendor_id == 0x10ec0665 || codec->vendor_id == 0x10ec0670)
6574 ssids = alc663_ssids;
6575 else
6576 ssids = alc662_ssids;
6577 return alc_parse_auto_config(codec, alc662_ignore, ssids);
6578 }
6579
6580 static void alc272_fixup_mario(struct hda_codec *codec,
6581 const struct alc_fixup *fix, int action)
6582 {
6583 if (action != ALC_FIXUP_ACT_PROBE)
6584 return;
6585 if (snd_hda_override_amp_caps(codec, 0x2, HDA_OUTPUT,
6586 (0x3b << AC_AMPCAP_OFFSET_SHIFT) |
6587 (0x3b << AC_AMPCAP_NUM_STEPS_SHIFT) |
6588 (0x03 << AC_AMPCAP_STEP_SIZE_SHIFT) |
6589 (0 << AC_AMPCAP_MUTE_SHIFT)))
6590 printk(KERN_WARNING
6591 "hda_codec: failed to override amp caps for NID 0x2\n");
6592 }
6593
6594 enum {
6595 ALC662_FIXUP_ASPIRE,
6596 ALC662_FIXUP_IDEAPAD,
6597 ALC272_FIXUP_MARIO,
6598 ALC662_FIXUP_CZC_P10T,
6599 ALC662_FIXUP_SKU_IGNORE,
6600 ALC662_FIXUP_HP_RP5800,
6601 ALC662_FIXUP_ASUS_MODE1,
6602 ALC662_FIXUP_ASUS_MODE2,
6603 ALC662_FIXUP_ASUS_MODE3,
6604 ALC662_FIXUP_ASUS_MODE4,
6605 ALC662_FIXUP_ASUS_MODE5,
6606 ALC662_FIXUP_ASUS_MODE6,
6607 ALC662_FIXUP_ASUS_MODE7,
6608 ALC662_FIXUP_ASUS_MODE8,
6609 ALC662_FIXUP_NO_JACK_DETECT,
6610 ALC662_FIXUP_ZOTAC_Z68,
6611 ALC662_FIXUP_INV_DMIC,
6612 };
6613
6614 static const struct alc_fixup alc662_fixups[] = {
6615 [ALC662_FIXUP_ASPIRE] = {
6616 .type = ALC_FIXUP_PINS,
6617 .v.pins = (const struct alc_pincfg[]) {
6618 { 0x15, 0x99130112 }, /* subwoofer */
6619 { }
6620 }
6621 },
6622 [ALC662_FIXUP_IDEAPAD] = {
6623 .type = ALC_FIXUP_PINS,
6624 .v.pins = (const struct alc_pincfg[]) {
6625 { 0x17, 0x99130112 }, /* subwoofer */
6626 { }
6627 }
6628 },
6629 [ALC272_FIXUP_MARIO] = {
6630 .type = ALC_FIXUP_FUNC,
6631 .v.func = alc272_fixup_mario,
6632 },
6633 [ALC662_FIXUP_CZC_P10T] = {
6634 .type = ALC_FIXUP_VERBS,
6635 .v.verbs = (const struct hda_verb[]) {
6636 {0x14, AC_VERB_SET_EAPD_BTLENABLE, 0},
6637 {}
6638 }
6639 },
6640 [ALC662_FIXUP_SKU_IGNORE] = {
6641 .type = ALC_FIXUP_FUNC,
6642 .v.func = alc_fixup_sku_ignore,
6643 },
6644 [ALC662_FIXUP_HP_RP5800] = {
6645 .type = ALC_FIXUP_PINS,
6646 .v.pins = (const struct alc_pincfg[]) {
6647 { 0x14, 0x0221201f }, /* HP out */
6648 { }
6649 },
6650 .chained = true,
6651 .chain_id = ALC662_FIXUP_SKU_IGNORE
6652 },
6653 [ALC662_FIXUP_ASUS_MODE1] = {
6654 .type = ALC_FIXUP_PINS,
6655 .v.pins = (const struct alc_pincfg[]) {
6656 { 0x14, 0x99130110 }, /* speaker */
6657 { 0x18, 0x01a19c20 }, /* mic */
6658 { 0x19, 0x99a3092f }, /* int-mic */
6659 { 0x21, 0x0121401f }, /* HP out */
6660 { }
6661 },
6662 .chained = true,
6663 .chain_id = ALC662_FIXUP_SKU_IGNORE
6664 },
6665 [ALC662_FIXUP_ASUS_MODE2] = {
6666 .type = ALC_FIXUP_PINS,
6667 .v.pins = (const struct alc_pincfg[]) {
6668 { 0x14, 0x99130110 }, /* speaker */
6669 { 0x18, 0x01a19820 }, /* mic */
6670 { 0x19, 0x99a3092f }, /* int-mic */
6671 { 0x1b, 0x0121401f }, /* HP out */
6672 { }
6673 },
6674 .chained = true,
6675 .chain_id = ALC662_FIXUP_SKU_IGNORE
6676 },
6677 [ALC662_FIXUP_ASUS_MODE3] = {
6678 .type = ALC_FIXUP_PINS,
6679 .v.pins = (const struct alc_pincfg[]) {
6680 { 0x14, 0x99130110 }, /* speaker */
6681 { 0x15, 0x0121441f }, /* HP */
6682 { 0x18, 0x01a19840 }, /* mic */
6683 { 0x19, 0x99a3094f }, /* int-mic */
6684 { 0x21, 0x01211420 }, /* HP2 */
6685 { }
6686 },
6687 .chained = true,
6688 .chain_id = ALC662_FIXUP_SKU_IGNORE
6689 },
6690 [ALC662_FIXUP_ASUS_MODE4] = {
6691 .type = ALC_FIXUP_PINS,
6692 .v.pins = (const struct alc_pincfg[]) {
6693 { 0x14, 0x99130110 }, /* speaker */
6694 { 0x16, 0x99130111 }, /* speaker */
6695 { 0x18, 0x01a19840 }, /* mic */
6696 { 0x19, 0x99a3094f }, /* int-mic */
6697 { 0x21, 0x0121441f }, /* HP */
6698 { }
6699 },
6700 .chained = true,
6701 .chain_id = ALC662_FIXUP_SKU_IGNORE
6702 },
6703 [ALC662_FIXUP_ASUS_MODE5] = {
6704 .type = ALC_FIXUP_PINS,
6705 .v.pins = (const struct alc_pincfg[]) {
6706 { 0x14, 0x99130110 }, /* speaker */
6707 { 0x15, 0x0121441f }, /* HP */
6708 { 0x16, 0x99130111 }, /* speaker */
6709 { 0x18, 0x01a19840 }, /* mic */
6710 { 0x19, 0x99a3094f }, /* int-mic */
6711 { }
6712 },
6713 .chained = true,
6714 .chain_id = ALC662_FIXUP_SKU_IGNORE
6715 },
6716 [ALC662_FIXUP_ASUS_MODE6] = {
6717 .type = ALC_FIXUP_PINS,
6718 .v.pins = (const struct alc_pincfg[]) {
6719 { 0x14, 0x99130110 }, /* speaker */
6720 { 0x15, 0x01211420 }, /* HP2 */
6721 { 0x18, 0x01a19840 }, /* mic */
6722 { 0x19, 0x99a3094f }, /* int-mic */
6723 { 0x1b, 0x0121441f }, /* HP */
6724 { }
6725 },
6726 .chained = true,
6727 .chain_id = ALC662_FIXUP_SKU_IGNORE
6728 },
6729 [ALC662_FIXUP_ASUS_MODE7] = {
6730 .type = ALC_FIXUP_PINS,
6731 .v.pins = (const struct alc_pincfg[]) {
6732 { 0x14, 0x99130110 }, /* speaker */
6733 { 0x17, 0x99130111 }, /* speaker */
6734 { 0x18, 0x01a19840 }, /* mic */
6735 { 0x19, 0x99a3094f }, /* int-mic */
6736 { 0x1b, 0x01214020 }, /* HP */
6737 { 0x21, 0x0121401f }, /* HP */
6738 { }
6739 },
6740 .chained = true,
6741 .chain_id = ALC662_FIXUP_SKU_IGNORE
6742 },
6743 [ALC662_FIXUP_ASUS_MODE8] = {
6744 .type = ALC_FIXUP_PINS,
6745 .v.pins = (const struct alc_pincfg[]) {
6746 { 0x14, 0x99130110 }, /* speaker */
6747 { 0x12, 0x99a30970 }, /* int-mic */
6748 { 0x15, 0x01214020 }, /* HP */
6749 { 0x17, 0x99130111 }, /* speaker */
6750 { 0x18, 0x01a19840 }, /* mic */
6751 { 0x21, 0x0121401f }, /* HP */
6752 { }
6753 },
6754 .chained = true,
6755 .chain_id = ALC662_FIXUP_SKU_IGNORE
6756 },
6757 [ALC662_FIXUP_NO_JACK_DETECT] = {
6758 .type = ALC_FIXUP_FUNC,
6759 .v.func = alc_fixup_no_jack_detect,
6760 },
6761 [ALC662_FIXUP_ZOTAC_Z68] = {
6762 .type = ALC_FIXUP_PINS,
6763 .v.pins = (const struct alc_pincfg[]) {
6764 { 0x1b, 0x02214020 }, /* Front HP */
6765 { }
6766 }
6767 },
6768 [ALC662_FIXUP_INV_DMIC] = {
6769 .type = ALC_FIXUP_FUNC,
6770 .v.func = alc_fixup_inv_dmic_0x12,
6771 },
6772 };
6773
6774 static const struct snd_pci_quirk alc662_fixup_tbl[] = {
6775 SND_PCI_QUIRK(0x1019, 0x9087, "ECS", ALC662_FIXUP_ASUS_MODE2),
6776 SND_PCI_QUIRK(0x1025, 0x0308, "Acer Aspire 8942G", ALC662_FIXUP_ASPIRE),
6777 SND_PCI_QUIRK(0x1025, 0x031c, "Gateway NV79", ALC662_FIXUP_SKU_IGNORE),
6778 SND_PCI_QUIRK(0x1025, 0x0349, "eMachines eM250", ALC662_FIXUP_INV_DMIC),
6779 SND_PCI_QUIRK(0x1025, 0x038b, "Acer Aspire 8943G", ALC662_FIXUP_ASPIRE),
6780 SND_PCI_QUIRK(0x103c, 0x1632, "HP RP5800", ALC662_FIXUP_HP_RP5800),
6781 SND_PCI_QUIRK(0x1043, 0x8469, "ASUS mobo", ALC662_FIXUP_NO_JACK_DETECT),
6782 SND_PCI_QUIRK(0x105b, 0x0cd6, "Foxconn", ALC662_FIXUP_ASUS_MODE2),
6783 SND_PCI_QUIRK(0x144d, 0xc051, "Samsung R720", ALC662_FIXUP_IDEAPAD),
6784 SND_PCI_QUIRK(0x17aa, 0x38af, "Lenovo Ideapad Y550P", ALC662_FIXUP_IDEAPAD),
6785 SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo Ideapad Y550", ALC662_FIXUP_IDEAPAD),
6786 SND_PCI_QUIRK(0x19da, 0xa130, "Zotac Z68", ALC662_FIXUP_ZOTAC_Z68),
6787 SND_PCI_QUIRK(0x1b35, 0x2206, "CZC P10T", ALC662_FIXUP_CZC_P10T),
6788
6789 #if 0
6790 /* Below is a quirk table taken from the old code.
6791 * Basically the device should work as is without the fixup table.
6792 * If BIOS doesn't give a proper info, enable the corresponding
6793 * fixup entry.
6794 */
6795 SND_PCI_QUIRK(0x1043, 0x1000, "ASUS N50Vm", ALC662_FIXUP_ASUS_MODE1),
6796 SND_PCI_QUIRK(0x1043, 0x1092, "ASUS NB", ALC662_FIXUP_ASUS_MODE3),
6797 SND_PCI_QUIRK(0x1043, 0x1173, "ASUS K73Jn", ALC662_FIXUP_ASUS_MODE1),
6798 SND_PCI_QUIRK(0x1043, 0x11c3, "ASUS M70V", ALC662_FIXUP_ASUS_MODE3),
6799 SND_PCI_QUIRK(0x1043, 0x11d3, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
6800 SND_PCI_QUIRK(0x1043, 0x11f3, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6801 SND_PCI_QUIRK(0x1043, 0x1203, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
6802 SND_PCI_QUIRK(0x1043, 0x1303, "ASUS G60J", ALC662_FIXUP_ASUS_MODE1),
6803 SND_PCI_QUIRK(0x1043, 0x1333, "ASUS G60Jx", ALC662_FIXUP_ASUS_MODE1),
6804 SND_PCI_QUIRK(0x1043, 0x1339, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6805 SND_PCI_QUIRK(0x1043, 0x13e3, "ASUS N71JA", ALC662_FIXUP_ASUS_MODE7),
6806 SND_PCI_QUIRK(0x1043, 0x1463, "ASUS N71", ALC662_FIXUP_ASUS_MODE7),
6807 SND_PCI_QUIRK(0x1043, 0x14d3, "ASUS G72", ALC662_FIXUP_ASUS_MODE8),
6808 SND_PCI_QUIRK(0x1043, 0x1563, "ASUS N90", ALC662_FIXUP_ASUS_MODE3),
6809 SND_PCI_QUIRK(0x1043, 0x15d3, "ASUS N50SF F50SF", ALC662_FIXUP_ASUS_MODE1),
6810 SND_PCI_QUIRK(0x1043, 0x16c3, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6811 SND_PCI_QUIRK(0x1043, 0x16f3, "ASUS K40C K50C", ALC662_FIXUP_ASUS_MODE2),
6812 SND_PCI_QUIRK(0x1043, 0x1733, "ASUS N81De", ALC662_FIXUP_ASUS_MODE1),
6813 SND_PCI_QUIRK(0x1043, 0x1753, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6814 SND_PCI_QUIRK(0x1043, 0x1763, "ASUS NB", ALC662_FIXUP_ASUS_MODE6),
6815 SND_PCI_QUIRK(0x1043, 0x1765, "ASUS NB", ALC662_FIXUP_ASUS_MODE6),
6816 SND_PCI_QUIRK(0x1043, 0x1783, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6817 SND_PCI_QUIRK(0x1043, 0x1793, "ASUS F50GX", ALC662_FIXUP_ASUS_MODE1),
6818 SND_PCI_QUIRK(0x1043, 0x17b3, "ASUS F70SL", ALC662_FIXUP_ASUS_MODE3),
6819 SND_PCI_QUIRK(0x1043, 0x17f3, "ASUS X58LE", ALC662_FIXUP_ASUS_MODE2),
6820 SND_PCI_QUIRK(0x1043, 0x1813, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6821 SND_PCI_QUIRK(0x1043, 0x1823, "ASUS NB", ALC662_FIXUP_ASUS_MODE5),
6822 SND_PCI_QUIRK(0x1043, 0x1833, "ASUS NB", ALC662_FIXUP_ASUS_MODE6),
6823 SND_PCI_QUIRK(0x1043, 0x1843, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6824 SND_PCI_QUIRK(0x1043, 0x1853, "ASUS F50Z", ALC662_FIXUP_ASUS_MODE1),
6825 SND_PCI_QUIRK(0x1043, 0x1864, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6826 SND_PCI_QUIRK(0x1043, 0x1876, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6827 SND_PCI_QUIRK(0x1043, 0x1893, "ASUS M50Vm", ALC662_FIXUP_ASUS_MODE3),
6828 SND_PCI_QUIRK(0x1043, 0x1894, "ASUS X55", ALC662_FIXUP_ASUS_MODE3),
6829 SND_PCI_QUIRK(0x1043, 0x18b3, "ASUS N80Vc", ALC662_FIXUP_ASUS_MODE1),
6830 SND_PCI_QUIRK(0x1043, 0x18c3, "ASUS VX5", ALC662_FIXUP_ASUS_MODE1),
6831 SND_PCI_QUIRK(0x1043, 0x18d3, "ASUS N81Te", ALC662_FIXUP_ASUS_MODE1),
6832 SND_PCI_QUIRK(0x1043, 0x18f3, "ASUS N505Tp", ALC662_FIXUP_ASUS_MODE1),
6833 SND_PCI_QUIRK(0x1043, 0x1903, "ASUS F5GL", ALC662_FIXUP_ASUS_MODE1),
6834 SND_PCI_QUIRK(0x1043, 0x1913, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6835 SND_PCI_QUIRK(0x1043, 0x1933, "ASUS F80Q", ALC662_FIXUP_ASUS_MODE2),
6836 SND_PCI_QUIRK(0x1043, 0x1943, "ASUS Vx3V", ALC662_FIXUP_ASUS_MODE1),
6837 SND_PCI_QUIRK(0x1043, 0x1953, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
6838 SND_PCI_QUIRK(0x1043, 0x1963, "ASUS X71C", ALC662_FIXUP_ASUS_MODE3),
6839 SND_PCI_QUIRK(0x1043, 0x1983, "ASUS N5051A", ALC662_FIXUP_ASUS_MODE1),
6840 SND_PCI_QUIRK(0x1043, 0x1993, "ASUS N20", ALC662_FIXUP_ASUS_MODE1),
6841 SND_PCI_QUIRK(0x1043, 0x19b3, "ASUS F7Z", ALC662_FIXUP_ASUS_MODE1),
6842 SND_PCI_QUIRK(0x1043, 0x19c3, "ASUS F5Z/F6x", ALC662_FIXUP_ASUS_MODE2),
6843 SND_PCI_QUIRK(0x1043, 0x19e3, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
6844 SND_PCI_QUIRK(0x1043, 0x19f3, "ASUS NB", ALC662_FIXUP_ASUS_MODE4),
6845 #endif
6846 {}
6847 };
6848
6849 static const struct alc_model_fixup alc662_fixup_models[] = {
6850 {.id = ALC272_FIXUP_MARIO, .name = "mario"},
6851 {.id = ALC662_FIXUP_ASUS_MODE1, .name = "asus-mode1"},
6852 {.id = ALC662_FIXUP_ASUS_MODE2, .name = "asus-mode2"},
6853 {.id = ALC662_FIXUP_ASUS_MODE3, .name = "asus-mode3"},
6854 {.id = ALC662_FIXUP_ASUS_MODE4, .name = "asus-mode4"},
6855 {.id = ALC662_FIXUP_ASUS_MODE5, .name = "asus-mode5"},
6856 {.id = ALC662_FIXUP_ASUS_MODE6, .name = "asus-mode6"},
6857 {.id = ALC662_FIXUP_ASUS_MODE7, .name = "asus-mode7"},
6858 {.id = ALC662_FIXUP_ASUS_MODE8, .name = "asus-mode8"},
6859 {.id = ALC662_FIXUP_INV_DMIC, .name = "inv-dmic"},
6860 {}
6861 };
6862
6863
6864 /*
6865 */
6866 static int patch_alc662(struct hda_codec *codec)
6867 {
6868 struct alc_spec *spec;
6869 int err;
6870
6871 err = alc_alloc_spec(codec, 0x0b);
6872 if (err < 0)
6873 return err;
6874
6875 spec = codec->spec;
6876
6877 /* handle multiple HPs as is */
6878 spec->parse_flags = HDA_PINCFG_NO_HP_FIXUP;
6879
6880 alc_fix_pll_init(codec, 0x20, 0x04, 15);
6881
6882 if ((alc_get_coef0(codec) & (1 << 14)) &&
6883 codec->bus->pci->subsystem_vendor == 0x1025 &&
6884 spec->cdefine.platform_type == 1) {
6885 if (alc_codec_rename(codec, "ALC272X") < 0)
6886 goto error;
6887 }
6888
6889 alc_pick_fixup(codec, alc662_fixup_models,
6890 alc662_fixup_tbl, alc662_fixups);
6891 alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
6892
6893 alc_auto_parse_customize_define(codec);
6894
6895 /* automatic parse from the BIOS config */
6896 err = alc662_parse_auto_config(codec);
6897 if (err < 0)
6898 goto error;
6899
6900 if (!spec->no_analog && has_cdefine_beep(codec)) {
6901 err = snd_hda_attach_beep_device(codec, 0x1);
6902 if (err < 0)
6903 goto error;
6904 switch (codec->vendor_id) {
6905 case 0x10ec0662:
6906 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
6907 break;
6908 case 0x10ec0272:
6909 case 0x10ec0663:
6910 case 0x10ec0665:
6911 set_beep_amp(spec, 0x0b, 0x04, HDA_INPUT);
6912 break;
6913 case 0x10ec0273:
6914 set_beep_amp(spec, 0x0b, 0x03, HDA_INPUT);
6915 break;
6916 }
6917 }
6918
6919 codec->patch_ops = alc_patch_ops;
6920 spec->shutup = alc_eapd_shutup;
6921
6922 alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
6923
6924 return 0;
6925
6926 error:
6927 alc_free(codec);
6928 return err;
6929 }
6930
6931 /*
6932 * ALC680 support
6933 */
6934
6935 static int alc680_parse_auto_config(struct hda_codec *codec)
6936 {
6937 return alc_parse_auto_config(codec, NULL, NULL);
6938 }
6939
6940 /*
6941 */
6942 static int patch_alc680(struct hda_codec *codec)
6943 {
6944 int err;
6945
6946 /* ALC680 has no aa-loopback mixer */
6947 err = alc_alloc_spec(codec, 0);
6948 if (err < 0)
6949 return err;
6950
6951 /* automatic parse from the BIOS config */
6952 err = alc680_parse_auto_config(codec);
6953 if (err < 0) {
6954 alc_free(codec);
6955 return err;
6956 }
6957
6958 codec->patch_ops = alc_patch_ops;
6959
6960 return 0;
6961 }
6962
6963 /*
6964 * patch entries
6965 */
6966 static const struct hda_codec_preset snd_hda_preset_realtek[] = {
6967 { .id = 0x10ec0221, .name = "ALC221", .patch = patch_alc269 },
6968 { .id = 0x10ec0260, .name = "ALC260", .patch = patch_alc260 },
6969 { .id = 0x10ec0262, .name = "ALC262", .patch = patch_alc262 },
6970 { .id = 0x10ec0267, .name = "ALC267", .patch = patch_alc268 },
6971 { .id = 0x10ec0268, .name = "ALC268", .patch = patch_alc268 },
6972 { .id = 0x10ec0269, .name = "ALC269", .patch = patch_alc269 },
6973 { .id = 0x10ec0270, .name = "ALC270", .patch = patch_alc269 },
6974 { .id = 0x10ec0272, .name = "ALC272", .patch = patch_alc662 },
6975 { .id = 0x10ec0275, .name = "ALC275", .patch = patch_alc269 },
6976 { .id = 0x10ec0276, .name = "ALC276", .patch = patch_alc269 },
6977 { .id = 0x10ec0861, .rev = 0x100340, .name = "ALC660",
6978 .patch = patch_alc861 },
6979 { .id = 0x10ec0660, .name = "ALC660-VD", .patch = patch_alc861vd },
6980 { .id = 0x10ec0861, .name = "ALC861", .patch = patch_alc861 },
6981 { .id = 0x10ec0862, .name = "ALC861-VD", .patch = patch_alc861vd },
6982 { .id = 0x10ec0662, .rev = 0x100002, .name = "ALC662 rev2",
6983 .patch = patch_alc882 },
6984 { .id = 0x10ec0662, .rev = 0x100101, .name = "ALC662 rev1",
6985 .patch = patch_alc662 },
6986 { .id = 0x10ec0662, .rev = 0x100300, .name = "ALC662 rev3",
6987 .patch = patch_alc662 },
6988 { .id = 0x10ec0663, .name = "ALC663", .patch = patch_alc662 },
6989 { .id = 0x10ec0665, .name = "ALC665", .patch = patch_alc662 },
6990 { .id = 0x10ec0670, .name = "ALC670", .patch = patch_alc662 },
6991 { .id = 0x10ec0680, .name = "ALC680", .patch = patch_alc680 },
6992 { .id = 0x10ec0880, .name = "ALC880", .patch = patch_alc880 },
6993 { .id = 0x10ec0882, .name = "ALC882", .patch = patch_alc882 },
6994 { .id = 0x10ec0883, .name = "ALC883", .patch = patch_alc882 },
6995 { .id = 0x10ec0885, .rev = 0x100101, .name = "ALC889A",
6996 .patch = patch_alc882 },
6997 { .id = 0x10ec0885, .rev = 0x100103, .name = "ALC889A",
6998 .patch = patch_alc882 },
6999 { .id = 0x10ec0885, .name = "ALC885", .patch = patch_alc882 },
7000 { .id = 0x10ec0887, .name = "ALC887", .patch = patch_alc882 },
7001 { .id = 0x10ec0888, .rev = 0x100101, .name = "ALC1200",
7002 .patch = patch_alc882 },
7003 { .id = 0x10ec0888, .name = "ALC888", .patch = patch_alc882 },
7004 { .id = 0x10ec0889, .name = "ALC889", .patch = patch_alc882 },
7005 { .id = 0x10ec0892, .name = "ALC892", .patch = patch_alc662 },
7006 { .id = 0x10ec0899, .name = "ALC898", .patch = patch_alc882 },
7007 {} /* terminator */
7008 };
7009
7010 MODULE_ALIAS("snd-hda-codec-id:10ec*");
7011
7012 MODULE_LICENSE("GPL");
7013 MODULE_DESCRIPTION("Realtek HD-audio codec");
7014
7015 static struct hda_codec_preset_list realtek_list = {
7016 .preset = snd_hda_preset_realtek,
7017 .owner = THIS_MODULE,
7018 };
7019
7020 static int __init patch_realtek_init(void)
7021 {
7022 return snd_hda_add_codec_preset(&realtek_list);
7023 }
7024
7025 static void __exit patch_realtek_exit(void)
7026 {
7027 snd_hda_delete_codec_preset(&realtek_list);
7028 }
7029
7030 module_init(patch_realtek_init)
7031 module_exit(patch_realtek_exit)