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