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