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