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