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