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