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