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