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