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