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