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