OSDN Git Service

ALSA: hda - Add GPIO-based LED support on HP desktop machines
[android-x86/kernel.git] / sound / pci / hda / patch_realtek.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for Realtek ALC codecs
5  *
6  * Copyright (c) 2004 Kailang Yang <kailang@realtek.com.tw>
7  *                    PeiSen Hou <pshou@realtek.com.tw>
8  *                    Takashi Iwai <tiwai@suse.de>
9  *                    Jonathan Woithe <jwoithe@just42.net>
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
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/pci.h>
30 #include <linux/dmi.h>
31 #include <linux/module.h>
32 #include <sound/core.h>
33 #include <sound/jack.h>
34 #include "hda_codec.h"
35 #include "hda_local.h"
36 #include "hda_auto_parser.h"
37 #include "hda_jack.h"
38 #include "hda_generic.h"
39
40 /* unsol event tags */
41 #define ALC_DCVOL_EVENT         0x08
42
43 /* for GPIO Poll */
44 #define GPIO_MASK       0x03
45
46 /* extra amp-initialization sequence types */
47 enum {
48         ALC_INIT_NONE,
49         ALC_INIT_DEFAULT,
50         ALC_INIT_GPIO1,
51         ALC_INIT_GPIO2,
52         ALC_INIT_GPIO3,
53 };
54
55 struct alc_customize_define {
56         unsigned int  sku_cfg;
57         unsigned char port_connectivity;
58         unsigned char check_sum;
59         unsigned char customization;
60         unsigned char external_amp;
61         unsigned int  enable_pcbeep:1;
62         unsigned int  platform_type:1;
63         unsigned int  swap:1;
64         unsigned int  override:1;
65         unsigned int  fixup:1; /* Means that this sku is set by driver, not read from hw */
66 };
67
68 struct alc_spec {
69         struct hda_gen_spec gen; /* must be at head */
70
71         /* codec parameterization */
72         const struct snd_kcontrol_new *mixers[5];       /* mixer arrays */
73         unsigned int num_mixers;
74         unsigned int beep_amp;  /* beep amp value, set via set_beep_amp() */
75
76         struct alc_customize_define cdefine;
77         unsigned int parse_flags; /* flag for snd_hda_parse_pin_defcfg() */
78
79         /* inverted dmic fix */
80         unsigned int inv_dmic_fixup:1; /* has inverted digital-mic workaround */
81         unsigned int inv_dmic_muted:1; /* R-ch of inv d-mic is muted? */
82         hda_nid_t inv_dmic_pin;
83
84         /* mute LED for HP laptops, see alc269_fixup_mic_mute_hook() */
85         int mute_led_polarity;
86         hda_nid_t mute_led_nid;
87
88         unsigned int gpio_led; /* used for alc269_fixup_hp_gpio_led() */
89
90         /* hooks */
91         void (*init_hook)(struct hda_codec *codec);
92 #ifdef CONFIG_PM
93         void (*power_hook)(struct hda_codec *codec);
94 #endif
95         void (*shutup)(struct hda_codec *codec);
96
97         int init_amp;
98         int codec_variant;      /* flag for other variants */
99
100         /* for PLL fix */
101         hda_nid_t pll_nid;
102         unsigned int pll_coef_idx, pll_coef_bit;
103         unsigned int coef0;
104 };
105
106 /*
107  * Append the given mixer and verb elements for the later use
108  * The mixer array is referred in build_controls(), and init_verbs are
109  * called in init().
110  */
111 static void add_mixer(struct alc_spec *spec, const struct snd_kcontrol_new *mix)
112 {
113         if (snd_BUG_ON(spec->num_mixers >= ARRAY_SIZE(spec->mixers)))
114                 return;
115         spec->mixers[spec->num_mixers++] = mix;
116 }
117
118 /*
119  * GPIO setup tables, used in initialization
120  */
121 /* Enable GPIO mask and set output */
122 static const struct hda_verb alc_gpio1_init_verbs[] = {
123         {0x01, AC_VERB_SET_GPIO_MASK, 0x01},
124         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
125         {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
126         { }
127 };
128
129 static const struct hda_verb alc_gpio2_init_verbs[] = {
130         {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
131         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
132         {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
133         { }
134 };
135
136 static const struct hda_verb alc_gpio3_init_verbs[] = {
137         {0x01, AC_VERB_SET_GPIO_MASK, 0x03},
138         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
139         {0x01, AC_VERB_SET_GPIO_DATA, 0x03},
140         { }
141 };
142
143 /*
144  * Fix hardware PLL issue
145  * On some codecs, the analog PLL gating control must be off while
146  * the default value is 1.
147  */
148 static void alc_fix_pll(struct hda_codec *codec)
149 {
150         struct alc_spec *spec = codec->spec;
151         unsigned int val;
152
153         if (!spec->pll_nid)
154                 return;
155         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
156                             spec->pll_coef_idx);
157         val = snd_hda_codec_read(codec, spec->pll_nid, 0,
158                                  AC_VERB_GET_PROC_COEF, 0);
159         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
160                             spec->pll_coef_idx);
161         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_PROC_COEF,
162                             val & ~(1 << spec->pll_coef_bit));
163 }
164
165 static void alc_fix_pll_init(struct hda_codec *codec, hda_nid_t nid,
166                              unsigned int coef_idx, unsigned int coef_bit)
167 {
168         struct alc_spec *spec = codec->spec;
169         spec->pll_nid = nid;
170         spec->pll_coef_idx = coef_idx;
171         spec->pll_coef_bit = coef_bit;
172         alc_fix_pll(codec);
173 }
174
175 /* update the master volume per volume-knob's unsol event */
176 static void alc_update_knob_master(struct hda_codec *codec, struct hda_jack_tbl *jack)
177 {
178         unsigned int val;
179         struct snd_kcontrol *kctl;
180         struct snd_ctl_elem_value *uctl;
181
182         kctl = snd_hda_find_mixer_ctl(codec, "Master Playback Volume");
183         if (!kctl)
184                 return;
185         uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
186         if (!uctl)
187                 return;
188         val = snd_hda_codec_read(codec, jack->nid, 0,
189                                  AC_VERB_GET_VOLUME_KNOB_CONTROL, 0);
190         val &= HDA_AMP_VOLMASK;
191         uctl->value.integer.value[0] = val;
192         uctl->value.integer.value[1] = val;
193         kctl->put(kctl, uctl);
194         kfree(uctl);
195 }
196
197 static void alc880_unsol_event(struct hda_codec *codec, unsigned int res)
198 {
199         /* For some reason, the res given from ALC880 is broken.
200            Here we adjust it properly. */
201         snd_hda_jack_unsol_event(codec, res >> 2);
202 }
203
204 /* additional initialization for ALC888 variants */
205 static void alc888_coef_init(struct hda_codec *codec)
206 {
207         unsigned int tmp;
208
209         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 0);
210         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
211         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
212         if ((tmp & 0xf0) == 0x20)
213                 /* alc888S-VC */
214                 snd_hda_codec_read(codec, 0x20, 0,
215                                    AC_VERB_SET_PROC_COEF, 0x830);
216          else
217                  /* alc888-VB */
218                  snd_hda_codec_read(codec, 0x20, 0,
219                                     AC_VERB_SET_PROC_COEF, 0x3030);
220 }
221
222 /* additional initialization for ALC889 variants */
223 static void alc889_coef_init(struct hda_codec *codec)
224 {
225         unsigned int tmp;
226
227         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
228         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
229         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
230         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF, tmp|0x2010);
231 }
232
233 /* turn on/off EAPD control (only if available) */
234 static void set_eapd(struct hda_codec *codec, hda_nid_t nid, int on)
235 {
236         if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
237                 return;
238         if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
239                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
240                                     on ? 2 : 0);
241 }
242
243 /* turn on/off EAPD controls of the codec */
244 static void alc_auto_setup_eapd(struct hda_codec *codec, bool on)
245 {
246         /* We currently only handle front, HP */
247         static hda_nid_t pins[] = {
248                 0x0f, 0x10, 0x14, 0x15, 0
249         };
250         hda_nid_t *p;
251         for (p = pins; *p; p++)
252                 set_eapd(codec, *p, on);
253 }
254
255 /* generic shutup callback;
256  * just turning off EPAD and a little pause for avoiding pop-noise
257  */
258 static void alc_eapd_shutup(struct hda_codec *codec)
259 {
260         alc_auto_setup_eapd(codec, false);
261         msleep(200);
262 }
263
264 /* generic EAPD initialization */
265 static void alc_auto_init_amp(struct hda_codec *codec, int type)
266 {
267         unsigned int tmp;
268
269         alc_auto_setup_eapd(codec, true);
270         switch (type) {
271         case ALC_INIT_GPIO1:
272                 snd_hda_sequence_write(codec, alc_gpio1_init_verbs);
273                 break;
274         case ALC_INIT_GPIO2:
275                 snd_hda_sequence_write(codec, alc_gpio2_init_verbs);
276                 break;
277         case ALC_INIT_GPIO3:
278                 snd_hda_sequence_write(codec, alc_gpio3_init_verbs);
279                 break;
280         case ALC_INIT_DEFAULT:
281                 switch (codec->vendor_id) {
282                 case 0x10ec0260:
283                         snd_hda_codec_write(codec, 0x1a, 0,
284                                             AC_VERB_SET_COEF_INDEX, 7);
285                         tmp = snd_hda_codec_read(codec, 0x1a, 0,
286                                                  AC_VERB_GET_PROC_COEF, 0);
287                         snd_hda_codec_write(codec, 0x1a, 0,
288                                             AC_VERB_SET_COEF_INDEX, 7);
289                         snd_hda_codec_write(codec, 0x1a, 0,
290                                             AC_VERB_SET_PROC_COEF,
291                                             tmp | 0x2010);
292                         break;
293                 case 0x10ec0262:
294                 case 0x10ec0880:
295                 case 0x10ec0882:
296                 case 0x10ec0883:
297                 case 0x10ec0885:
298                 case 0x10ec0887:
299                 /*case 0x10ec0889:*/ /* this causes an SPDIF problem */
300                         alc889_coef_init(codec);
301                         break;
302                 case 0x10ec0888:
303                         alc888_coef_init(codec);
304                         break;
305 #if 0 /* XXX: This may cause the silent output on speaker on some machines */
306                 case 0x10ec0267:
307                 case 0x10ec0268:
308                         snd_hda_codec_write(codec, 0x20, 0,
309                                             AC_VERB_SET_COEF_INDEX, 7);
310                         tmp = snd_hda_codec_read(codec, 0x20, 0,
311                                                  AC_VERB_GET_PROC_COEF, 0);
312                         snd_hda_codec_write(codec, 0x20, 0,
313                                             AC_VERB_SET_COEF_INDEX, 7);
314                         snd_hda_codec_write(codec, 0x20, 0,
315                                             AC_VERB_SET_PROC_COEF,
316                                             tmp | 0x3000);
317                         break;
318 #endif /* XXX */
319                 }
320                 break;
321         }
322 }
323
324
325 /*
326  * Realtek SSID verification
327  */
328
329 /* Could be any non-zero and even value. When used as fixup, tells
330  * the driver to ignore any present sku defines.
331  */
332 #define ALC_FIXUP_SKU_IGNORE (2)
333
334 static void alc_fixup_sku_ignore(struct hda_codec *codec,
335                                  const struct hda_fixup *fix, int action)
336 {
337         struct alc_spec *spec = codec->spec;
338         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
339                 spec->cdefine.fixup = 1;
340                 spec->cdefine.sku_cfg = ALC_FIXUP_SKU_IGNORE;
341         }
342 }
343
344 static int alc_auto_parse_customize_define(struct hda_codec *codec)
345 {
346         unsigned int ass, tmp, i;
347         unsigned nid = 0;
348         struct alc_spec *spec = codec->spec;
349
350         spec->cdefine.enable_pcbeep = 1; /* assume always enabled */
351
352         if (spec->cdefine.fixup) {
353                 ass = spec->cdefine.sku_cfg;
354                 if (ass == ALC_FIXUP_SKU_IGNORE)
355                         return -1;
356                 goto do_sku;
357         }
358
359         ass = codec->subsystem_id & 0xffff;
360         if (ass != codec->bus->pci->subsystem_device && (ass & 1))
361                 goto do_sku;
362
363         nid = 0x1d;
364         if (codec->vendor_id == 0x10ec0260)
365                 nid = 0x17;
366         ass = snd_hda_codec_get_pincfg(codec, nid);
367
368         if (!(ass & 1)) {
369                 printk(KERN_INFO "hda_codec: %s: SKU not ready 0x%08x\n",
370                        codec->chip_name, ass);
371                 return -1;
372         }
373
374         /* check sum */
375         tmp = 0;
376         for (i = 1; i < 16; i++) {
377                 if ((ass >> i) & 1)
378                         tmp++;
379         }
380         if (((ass >> 16) & 0xf) != tmp)
381                 return -1;
382
383         spec->cdefine.port_connectivity = ass >> 30;
384         spec->cdefine.enable_pcbeep = (ass & 0x100000) >> 20;
385         spec->cdefine.check_sum = (ass >> 16) & 0xf;
386         spec->cdefine.customization = ass >> 8;
387 do_sku:
388         spec->cdefine.sku_cfg = ass;
389         spec->cdefine.external_amp = (ass & 0x38) >> 3;
390         spec->cdefine.platform_type = (ass & 0x4) >> 2;
391         spec->cdefine.swap = (ass & 0x2) >> 1;
392         spec->cdefine.override = ass & 0x1;
393
394         snd_printd("SKU: Nid=0x%x sku_cfg=0x%08x\n",
395                    nid, spec->cdefine.sku_cfg);
396         snd_printd("SKU: port_connectivity=0x%x\n",
397                    spec->cdefine.port_connectivity);
398         snd_printd("SKU: enable_pcbeep=0x%x\n", spec->cdefine.enable_pcbeep);
399         snd_printd("SKU: check_sum=0x%08x\n", spec->cdefine.check_sum);
400         snd_printd("SKU: customization=0x%08x\n", spec->cdefine.customization);
401         snd_printd("SKU: external_amp=0x%x\n", spec->cdefine.external_amp);
402         snd_printd("SKU: platform_type=0x%x\n", spec->cdefine.platform_type);
403         snd_printd("SKU: swap=0x%x\n", spec->cdefine.swap);
404         snd_printd("SKU: override=0x%x\n", spec->cdefine.override);
405
406         return 0;
407 }
408
409 /* return the position of NID in the list, or -1 if not found */
410 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
411 {
412         int i;
413         for (i = 0; i < nums; i++)
414                 if (list[i] == nid)
415                         return i;
416         return -1;
417 }
418 /* return true if the given NID is found in the list */
419 static bool found_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
420 {
421         return find_idx_in_nid_list(nid, list, nums) >= 0;
422 }
423
424 /* check subsystem ID and set up device-specific initialization;
425  * return 1 if initialized, 0 if invalid SSID
426  */
427 /* 32-bit subsystem ID for BIOS loading in HD Audio codec.
428  *      31 ~ 16 :       Manufacture ID
429  *      15 ~ 8  :       SKU ID
430  *      7  ~ 0  :       Assembly ID
431  *      port-A --> pin 39/41, port-E --> pin 14/15, port-D --> pin 35/36
432  */
433 static int alc_subsystem_id(struct hda_codec *codec,
434                             hda_nid_t porta, hda_nid_t porte,
435                             hda_nid_t portd, hda_nid_t porti)
436 {
437         unsigned int ass, tmp, i;
438         unsigned nid;
439         struct alc_spec *spec = codec->spec;
440
441         if (spec->cdefine.fixup) {
442                 ass = spec->cdefine.sku_cfg;
443                 if (ass == ALC_FIXUP_SKU_IGNORE)
444                         return 0;
445                 goto do_sku;
446         }
447
448         ass = codec->subsystem_id & 0xffff;
449         if ((ass != codec->bus->pci->subsystem_device) && (ass & 1))
450                 goto do_sku;
451
452         /* invalid SSID, check the special NID pin defcfg instead */
453         /*
454          * 31~30        : port connectivity
455          * 29~21        : reserve
456          * 20           : PCBEEP input
457          * 19~16        : Check sum (15:1)
458          * 15~1         : Custom
459          * 0            : override
460         */
461         nid = 0x1d;
462         if (codec->vendor_id == 0x10ec0260)
463                 nid = 0x17;
464         ass = snd_hda_codec_get_pincfg(codec, nid);
465         snd_printd("realtek: No valid SSID, "
466                    "checking pincfg 0x%08x for NID 0x%x\n",
467                    ass, nid);
468         if (!(ass & 1))
469                 return 0;
470         if ((ass >> 30) != 1)   /* no physical connection */
471                 return 0;
472
473         /* check sum */
474         tmp = 0;
475         for (i = 1; i < 16; i++) {
476                 if ((ass >> i) & 1)
477                         tmp++;
478         }
479         if (((ass >> 16) & 0xf) != tmp)
480                 return 0;
481 do_sku:
482         snd_printd("realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
483                    ass & 0xffff, codec->vendor_id);
484         /*
485          * 0 : override
486          * 1 :  Swap Jack
487          * 2 : 0 --> Desktop, 1 --> Laptop
488          * 3~5 : External Amplifier control
489          * 7~6 : Reserved
490         */
491         tmp = (ass & 0x38) >> 3;        /* external Amp control */
492         switch (tmp) {
493         case 1:
494                 spec->init_amp = ALC_INIT_GPIO1;
495                 break;
496         case 3:
497                 spec->init_amp = ALC_INIT_GPIO2;
498                 break;
499         case 7:
500                 spec->init_amp = ALC_INIT_GPIO3;
501                 break;
502         case 5:
503         default:
504                 spec->init_amp = ALC_INIT_DEFAULT;
505                 break;
506         }
507
508         /* is laptop or Desktop and enable the function "Mute internal speaker
509          * when the external headphone out jack is plugged"
510          */
511         if (!(ass & 0x8000))
512                 return 1;
513         /*
514          * 10~8 : Jack location
515          * 12~11: Headphone out -> 00: PortA, 01: PortE, 02: PortD, 03: Resvered
516          * 14~13: Resvered
517          * 15   : 1 --> enable the function "Mute internal speaker
518          *              when the external headphone out jack is plugged"
519          */
520         if (!spec->gen.autocfg.hp_pins[0] &&
521             !(spec->gen.autocfg.line_out_pins[0] &&
522               spec->gen.autocfg.line_out_type == AUTO_PIN_HP_OUT)) {
523                 hda_nid_t nid;
524                 tmp = (ass >> 11) & 0x3;        /* HP to chassis */
525                 if (tmp == 0)
526                         nid = porta;
527                 else if (tmp == 1)
528                         nid = porte;
529                 else if (tmp == 2)
530                         nid = portd;
531                 else if (tmp == 3)
532                         nid = porti;
533                 else
534                         return 1;
535                 if (found_in_nid_list(nid, spec->gen.autocfg.line_out_pins,
536                                       spec->gen.autocfg.line_outs))
537                         return 1;
538                 spec->gen.autocfg.hp_pins[0] = nid;
539         }
540         return 1;
541 }
542
543 /* Check the validity of ALC subsystem-id
544  * ports contains an array of 4 pin NIDs for port-A, E, D and I */
545 static void alc_ssid_check(struct hda_codec *codec, const hda_nid_t *ports)
546 {
547         if (!alc_subsystem_id(codec, ports[0], ports[1], ports[2], ports[3])) {
548                 struct alc_spec *spec = codec->spec;
549                 snd_printd("realtek: "
550                            "Enable default setup for auto mode as fallback\n");
551                 spec->init_amp = ALC_INIT_DEFAULT;
552         }
553 }
554
555 /*
556  * COEF access helper functions
557  */
558 static int alc_read_coef_idx(struct hda_codec *codec,
559                         unsigned int coef_idx)
560 {
561         unsigned int val;
562         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
563                                 coef_idx);
564         val = snd_hda_codec_read(codec, 0x20, 0,
565                                 AC_VERB_GET_PROC_COEF, 0);
566         return val;
567 }
568
569 static void alc_write_coef_idx(struct hda_codec *codec, unsigned int coef_idx,
570                                                         unsigned int coef_val)
571 {
572         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
573                             coef_idx);
574         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF,
575                             coef_val);
576 }
577
578 /* a special bypass for COEF 0; read the cached value at the second time */
579 static unsigned int alc_get_coef0(struct hda_codec *codec)
580 {
581         struct alc_spec *spec = codec->spec;
582         if (!spec->coef0)
583                 spec->coef0 = alc_read_coef_idx(codec, 0);
584         return spec->coef0;
585 }
586
587 /*
588  */
589
590 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
591 {
592         struct hda_gen_spec *spec = codec->spec;
593         if (spec->dyn_adc_switch)
594                 adc_idx = spec->dyn_adc_idx[imux_idx];
595         return spec->adc_nids[adc_idx];
596 }
597
598 static void alc_inv_dmic_sync_adc(struct hda_codec *codec, int adc_idx)
599 {
600         struct alc_spec *spec = codec->spec;
601         struct hda_input_mux *imux = &spec->gen.input_mux;
602         struct nid_path *path;
603         hda_nid_t nid;
604         int i, dir, parm;
605         unsigned int val;
606
607         for (i = 0; i < imux->num_items; i++) {
608                 if (spec->gen.imux_pins[i] == spec->inv_dmic_pin)
609                         break;
610         }
611         if (i >= imux->num_items)
612                 return;
613
614         path = snd_hda_get_nid_path(codec, spec->inv_dmic_pin,
615                                     get_adc_nid(codec, adc_idx, i));
616         val = path->ctls[NID_PATH_MUTE_CTL];
617         if (!val)
618                 return;
619         nid = get_amp_nid_(val);
620         dir = get_amp_direction_(val);
621         parm = AC_AMP_SET_RIGHT |
622                 (dir == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT);
623
624         /* flush all cached amps at first */
625         snd_hda_codec_flush_cache(codec);
626
627         /* we care only right channel */
628         val = snd_hda_codec_amp_read(codec, nid, 1, dir, 0);
629         if (val & 0x80) /* if already muted, we don't need to touch */
630                 return;
631         val |= 0x80;
632         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
633                             parm | val);
634 }
635
636 /*
637  * Inverted digital-mic handling
638  *
639  * First off, it's a bit tricky.  The "Inverted Internal Mic Capture Switch"
640  * gives the additional mute only to the right channel of the digital mic
641  * capture stream.  This is a workaround for avoiding the almost silence
642  * by summing the stereo stream from some (known to be ForteMedia)
643  * digital mic unit.
644  *
645  * The logic is to call alc_inv_dmic_sync() after each action (possibly)
646  * modifying ADC amp.  When the mute flag is set, it mutes the R-channel
647  * without caching so that the cache can still keep the original value.
648  * The cached value is then restored when the flag is set off or any other
649  * than d-mic is used as the current input source.
650  */
651 static void alc_inv_dmic_sync(struct hda_codec *codec, bool force)
652 {
653         struct alc_spec *spec = codec->spec;
654         int src, nums;
655
656         if (!spec->inv_dmic_fixup)
657                 return;
658         if (!spec->inv_dmic_muted && !force)
659                 return;
660         nums = spec->gen.dyn_adc_switch ? 1 : spec->gen.num_adc_nids;
661         for (src = 0; src < nums; src++) {
662                 bool dmic_fixup = false;
663
664                 if (spec->inv_dmic_muted &&
665                     spec->gen.imux_pins[spec->gen.cur_mux[src]] == spec->inv_dmic_pin)
666                         dmic_fixup = true;
667                 if (!dmic_fixup && !force)
668                         continue;
669                 alc_inv_dmic_sync_adc(codec, src);
670         }
671 }
672
673 static void alc_inv_dmic_hook(struct hda_codec *codec,
674                              struct snd_ctl_elem_value *ucontrol)
675 {
676         alc_inv_dmic_sync(codec, false);
677 }
678
679 static int alc_inv_dmic_sw_get(struct snd_kcontrol *kcontrol,
680                                struct snd_ctl_elem_value *ucontrol)
681 {
682         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
683         struct alc_spec *spec = codec->spec;
684
685         ucontrol->value.integer.value[0] = !spec->inv_dmic_muted;
686         return 0;
687 }
688
689 static int alc_inv_dmic_sw_put(struct snd_kcontrol *kcontrol,
690                                struct snd_ctl_elem_value *ucontrol)
691 {
692         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
693         struct alc_spec *spec = codec->spec;
694         unsigned int val = !ucontrol->value.integer.value[0];
695
696         if (val == spec->inv_dmic_muted)
697                 return 0;
698         spec->inv_dmic_muted = val;
699         alc_inv_dmic_sync(codec, true);
700         return 0;
701 }
702
703 static const struct snd_kcontrol_new alc_inv_dmic_sw = {
704         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
705         .name = "Inverted Internal Mic Capture Switch",
706         .info = snd_ctl_boolean_mono_info,
707         .get = alc_inv_dmic_sw_get,
708         .put = alc_inv_dmic_sw_put,
709 };
710
711 static int alc_add_inv_dmic_mixer(struct hda_codec *codec, hda_nid_t nid)
712 {
713         struct alc_spec *spec = codec->spec;
714
715         if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &alc_inv_dmic_sw))
716                 return -ENOMEM;
717         spec->inv_dmic_fixup = 1;
718         spec->inv_dmic_muted = 0;
719         spec->inv_dmic_pin = nid;
720         spec->gen.cap_sync_hook = alc_inv_dmic_hook;
721         return 0;
722 }
723
724 /* typically the digital mic is put at node 0x12 */
725 static void alc_fixup_inv_dmic_0x12(struct hda_codec *codec,
726                                     const struct hda_fixup *fix, int action)
727 {
728         if (action == HDA_FIXUP_ACT_PROBE)
729                 alc_add_inv_dmic_mixer(codec, 0x12);
730 }
731
732
733 #ifdef CONFIG_SND_HDA_INPUT_BEEP
734 /* additional beep mixers; the actual parameters are overwritten at build */
735 static const struct snd_kcontrol_new alc_beep_mixer[] = {
736         HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_INPUT),
737         HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_INPUT),
738         { } /* end */
739 };
740 #endif
741
742 static int alc_build_controls(struct hda_codec *codec)
743 {
744         struct alc_spec *spec = codec->spec;
745         int i, err;
746
747         err = snd_hda_gen_build_controls(codec);
748         if (err < 0)
749                 return err;
750
751         for (i = 0; i < spec->num_mixers; i++) {
752                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
753                 if (err < 0)
754                         return err;
755         }
756
757 #ifdef CONFIG_SND_HDA_INPUT_BEEP
758         /* create beep controls if needed */
759         if (spec->beep_amp) {
760                 const struct snd_kcontrol_new *knew;
761                 for (knew = alc_beep_mixer; knew->name; knew++) {
762                         struct snd_kcontrol *kctl;
763                         kctl = snd_ctl_new1(knew, codec);
764                         if (!kctl)
765                                 return -ENOMEM;
766                         kctl->private_value = spec->beep_amp;
767                         err = snd_hda_ctl_add(codec, 0, kctl);
768                         if (err < 0)
769                                 return err;
770                 }
771         }
772 #endif
773
774         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_BUILD);
775         return 0;
776 }
777
778
779 /*
780  * Common callbacks
781  */
782
783 static int alc_init(struct hda_codec *codec)
784 {
785         struct alc_spec *spec = codec->spec;
786
787         if (spec->init_hook)
788                 spec->init_hook(codec);
789
790         alc_fix_pll(codec);
791         alc_auto_init_amp(codec, spec->init_amp);
792
793         snd_hda_gen_init(codec);
794
795         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT);
796
797         return 0;
798 }
799
800 static inline void alc_shutup(struct hda_codec *codec)
801 {
802         struct alc_spec *spec = codec->spec;
803
804         if (spec && spec->shutup)
805                 spec->shutup(codec);
806         snd_hda_shutup_pins(codec);
807 }
808
809 #define alc_free        snd_hda_gen_free
810
811 #ifdef CONFIG_PM
812 static void alc_power_eapd(struct hda_codec *codec)
813 {
814         alc_auto_setup_eapd(codec, false);
815 }
816
817 static int alc_suspend(struct hda_codec *codec)
818 {
819         struct alc_spec *spec = codec->spec;
820         alc_shutup(codec);
821         if (spec && spec->power_hook)
822                 spec->power_hook(codec);
823         return 0;
824 }
825 #endif
826
827 #ifdef CONFIG_PM
828 static int alc_resume(struct hda_codec *codec)
829 {
830         msleep(150); /* to avoid pop noise */
831         codec->patch_ops.init(codec);
832         snd_hda_codec_resume_amp(codec);
833         snd_hda_codec_resume_cache(codec);
834         alc_inv_dmic_sync(codec, true);
835         hda_call_check_power_status(codec, 0x01);
836         return 0;
837 }
838 #endif
839
840 /*
841  */
842 static const struct hda_codec_ops alc_patch_ops = {
843         .build_controls = alc_build_controls,
844         .build_pcms = snd_hda_gen_build_pcms,
845         .init = alc_init,
846         .free = alc_free,
847         .unsol_event = snd_hda_jack_unsol_event,
848 #ifdef CONFIG_PM
849         .resume = alc_resume,
850         .suspend = alc_suspend,
851         .check_power_status = snd_hda_gen_check_power_status,
852 #endif
853         .reboot_notify = alc_shutup,
854 };
855
856
857 /* replace the codec chip_name with the given string */
858 static int alc_codec_rename(struct hda_codec *codec, const char *name)
859 {
860         kfree(codec->chip_name);
861         codec->chip_name = kstrdup(name, GFP_KERNEL);
862         if (!codec->chip_name) {
863                 alc_free(codec);
864                 return -ENOMEM;
865         }
866         return 0;
867 }
868
869 /*
870  * Rename codecs appropriately from COEF value
871  */
872 struct alc_codec_rename_table {
873         unsigned int vendor_id;
874         unsigned short coef_mask;
875         unsigned short coef_bits;
876         const char *name;
877 };
878
879 static struct alc_codec_rename_table rename_tbl[] = {
880         { 0x10ec0269, 0xfff0, 0x3010, "ALC277" },
881         { 0x10ec0269, 0xf0f0, 0x2010, "ALC259" },
882         { 0x10ec0269, 0xf0f0, 0x3010, "ALC258" },
883         { 0x10ec0269, 0x00f0, 0x0010, "ALC269VB" },
884         { 0x10ec0269, 0xffff, 0xa023, "ALC259" },
885         { 0x10ec0269, 0xffff, 0x6023, "ALC281X" },
886         { 0x10ec0269, 0x00f0, 0x0020, "ALC269VC" },
887         { 0x10ec0269, 0x00f0, 0x0030, "ALC269VD" },
888         { 0x10ec0887, 0x00f0, 0x0030, "ALC887-VD" },
889         { 0x10ec0888, 0x00f0, 0x0030, "ALC888-VD" },
890         { 0x10ec0888, 0xf0f0, 0x3020, "ALC886" },
891         { 0x10ec0899, 0x2000, 0x2000, "ALC899" },
892         { 0x10ec0892, 0xffff, 0x8020, "ALC661" },
893         { 0x10ec0892, 0xffff, 0x8011, "ALC661" },
894         { 0x10ec0892, 0xffff, 0x4011, "ALC656" },
895         { } /* terminator */
896 };
897
898 static int alc_codec_rename_from_preset(struct hda_codec *codec)
899 {
900         const struct alc_codec_rename_table *p;
901
902         for (p = rename_tbl; p->vendor_id; p++) {
903                 if (p->vendor_id != codec->vendor_id)
904                         continue;
905                 if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
906                         return alc_codec_rename(codec, p->name);
907         }
908         return 0;
909 }
910
911
912 /*
913  * Digital-beep handlers
914  */
915 #ifdef CONFIG_SND_HDA_INPUT_BEEP
916 #define set_beep_amp(spec, nid, idx, dir) \
917         ((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir))
918
919 static const struct snd_pci_quirk beep_white_list[] = {
920         SND_PCI_QUIRK(0x1043, 0x103c, "ASUS", 1),
921         SND_PCI_QUIRK(0x1043, 0x829f, "ASUS", 1),
922         SND_PCI_QUIRK(0x1043, 0x8376, "EeePC", 1),
923         SND_PCI_QUIRK(0x1043, 0x83ce, "EeePC", 1),
924         SND_PCI_QUIRK(0x1043, 0x831a, "EeePC", 1),
925         SND_PCI_QUIRK(0x1043, 0x834a, "EeePC", 1),
926         SND_PCI_QUIRK(0x1458, 0xa002, "GA-MA790X", 1),
927         SND_PCI_QUIRK(0x8086, 0xd613, "Intel", 1),
928         {}
929 };
930
931 static inline int has_cdefine_beep(struct hda_codec *codec)
932 {
933         struct alc_spec *spec = codec->spec;
934         const struct snd_pci_quirk *q;
935         q = snd_pci_quirk_lookup(codec->bus->pci, beep_white_list);
936         if (q)
937                 return q->value;
938         return spec->cdefine.enable_pcbeep;
939 }
940 #else
941 #define set_beep_amp(spec, nid, idx, dir) /* NOP */
942 #define has_cdefine_beep(codec)         0
943 #endif
944
945 /* parse the BIOS configuration and set up the alc_spec */
946 /* return 1 if successful, 0 if the proper config is not found,
947  * or a negative error code
948  */
949 static int alc_parse_auto_config(struct hda_codec *codec,
950                                  const hda_nid_t *ignore_nids,
951                                  const hda_nid_t *ssid_nids)
952 {
953         struct alc_spec *spec = codec->spec;
954         struct auto_pin_cfg *cfg = &spec->gen.autocfg;
955         int err;
956
957         err = snd_hda_parse_pin_defcfg(codec, cfg, ignore_nids,
958                                        spec->parse_flags);
959         if (err < 0)
960                 return err;
961
962         if (ssid_nids)
963                 alc_ssid_check(codec, ssid_nids);
964
965         err = snd_hda_gen_parse_auto_config(codec, cfg);
966         if (err < 0)
967                 return err;
968
969         return 1;
970 }
971
972 /* common preparation job for alc_spec */
973 static int alc_alloc_spec(struct hda_codec *codec, hda_nid_t mixer_nid)
974 {
975         struct alc_spec *spec = kzalloc(sizeof(*spec), GFP_KERNEL);
976         int err;
977
978         if (!spec)
979                 return -ENOMEM;
980         codec->spec = spec;
981         snd_hda_gen_spec_init(&spec->gen);
982         spec->gen.mixer_nid = mixer_nid;
983         spec->gen.own_eapd_ctl = 1;
984         codec->single_adc_amp = 1;
985         /* FIXME: do we need this for all Realtek codec models? */
986         codec->spdif_status_reset = 1;
987
988         err = alc_codec_rename_from_preset(codec);
989         if (err < 0) {
990                 kfree(spec);
991                 return err;
992         }
993         return 0;
994 }
995
996 static int alc880_parse_auto_config(struct hda_codec *codec)
997 {
998         static const hda_nid_t alc880_ignore[] = { 0x1d, 0 };
999         static const hda_nid_t alc880_ssids[] = { 0x15, 0x1b, 0x14, 0 };
1000         return alc_parse_auto_config(codec, alc880_ignore, alc880_ssids);
1001 }
1002
1003 /*
1004  * ALC880 fix-ups
1005  */
1006 enum {
1007         ALC880_FIXUP_GPIO1,
1008         ALC880_FIXUP_GPIO2,
1009         ALC880_FIXUP_MEDION_RIM,
1010         ALC880_FIXUP_LG,
1011         ALC880_FIXUP_W810,
1012         ALC880_FIXUP_EAPD_COEF,
1013         ALC880_FIXUP_TCL_S700,
1014         ALC880_FIXUP_VOL_KNOB,
1015         ALC880_FIXUP_FUJITSU,
1016         ALC880_FIXUP_F1734,
1017         ALC880_FIXUP_UNIWILL,
1018         ALC880_FIXUP_UNIWILL_DIG,
1019         ALC880_FIXUP_Z71V,
1020         ALC880_FIXUP_3ST_BASE,
1021         ALC880_FIXUP_3ST,
1022         ALC880_FIXUP_3ST_DIG,
1023         ALC880_FIXUP_5ST_BASE,
1024         ALC880_FIXUP_5ST,
1025         ALC880_FIXUP_5ST_DIG,
1026         ALC880_FIXUP_6ST_BASE,
1027         ALC880_FIXUP_6ST,
1028         ALC880_FIXUP_6ST_DIG,
1029         ALC880_FIXUP_6ST_AUTOMUTE,
1030 };
1031
1032 /* enable the volume-knob widget support on NID 0x21 */
1033 static void alc880_fixup_vol_knob(struct hda_codec *codec,
1034                                   const struct hda_fixup *fix, int action)
1035 {
1036         if (action == HDA_FIXUP_ACT_PROBE)
1037                 snd_hda_jack_detect_enable_callback(codec, 0x21, ALC_DCVOL_EVENT, alc_update_knob_master);
1038 }
1039
1040 static const struct hda_fixup alc880_fixups[] = {
1041         [ALC880_FIXUP_GPIO1] = {
1042                 .type = HDA_FIXUP_VERBS,
1043                 .v.verbs = alc_gpio1_init_verbs,
1044         },
1045         [ALC880_FIXUP_GPIO2] = {
1046                 .type = HDA_FIXUP_VERBS,
1047                 .v.verbs = alc_gpio2_init_verbs,
1048         },
1049         [ALC880_FIXUP_MEDION_RIM] = {
1050                 .type = HDA_FIXUP_VERBS,
1051                 .v.verbs = (const struct hda_verb[]) {
1052                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
1053                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3060 },
1054                         { }
1055                 },
1056                 .chained = true,
1057                 .chain_id = ALC880_FIXUP_GPIO2,
1058         },
1059         [ALC880_FIXUP_LG] = {
1060                 .type = HDA_FIXUP_PINS,
1061                 .v.pins = (const struct hda_pintbl[]) {
1062                         /* disable bogus unused pins */
1063                         { 0x16, 0x411111f0 },
1064                         { 0x18, 0x411111f0 },
1065                         { 0x1a, 0x411111f0 },
1066                         { }
1067                 }
1068         },
1069         [ALC880_FIXUP_W810] = {
1070                 .type = HDA_FIXUP_PINS,
1071                 .v.pins = (const struct hda_pintbl[]) {
1072                         /* disable bogus unused pins */
1073                         { 0x17, 0x411111f0 },
1074                         { }
1075                 },
1076                 .chained = true,
1077                 .chain_id = ALC880_FIXUP_GPIO2,
1078         },
1079         [ALC880_FIXUP_EAPD_COEF] = {
1080                 .type = HDA_FIXUP_VERBS,
1081                 .v.verbs = (const struct hda_verb[]) {
1082                         /* change to EAPD mode */
1083                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
1084                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3060 },
1085                         {}
1086                 },
1087         },
1088         [ALC880_FIXUP_TCL_S700] = {
1089                 .type = HDA_FIXUP_VERBS,
1090                 .v.verbs = (const struct hda_verb[]) {
1091                         /* change to EAPD mode */
1092                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
1093                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3070 },
1094                         {}
1095                 },
1096                 .chained = true,
1097                 .chain_id = ALC880_FIXUP_GPIO2,
1098         },
1099         [ALC880_FIXUP_VOL_KNOB] = {
1100                 .type = HDA_FIXUP_FUNC,
1101                 .v.func = alc880_fixup_vol_knob,
1102         },
1103         [ALC880_FIXUP_FUJITSU] = {
1104                 /* override all pins as BIOS on old Amilo is broken */
1105                 .type = HDA_FIXUP_PINS,
1106                 .v.pins = (const struct hda_pintbl[]) {
1107                         { 0x14, 0x0121411f }, /* HP */
1108                         { 0x15, 0x99030120 }, /* speaker */
1109                         { 0x16, 0x99030130 }, /* bass speaker */
1110                         { 0x17, 0x411111f0 }, /* N/A */
1111                         { 0x18, 0x411111f0 }, /* N/A */
1112                         { 0x19, 0x01a19950 }, /* mic-in */
1113                         { 0x1a, 0x411111f0 }, /* N/A */
1114                         { 0x1b, 0x411111f0 }, /* N/A */
1115                         { 0x1c, 0x411111f0 }, /* N/A */
1116                         { 0x1d, 0x411111f0 }, /* N/A */
1117                         { 0x1e, 0x01454140 }, /* SPDIF out */
1118                         { }
1119                 },
1120                 .chained = true,
1121                 .chain_id = ALC880_FIXUP_VOL_KNOB,
1122         },
1123         [ALC880_FIXUP_F1734] = {
1124                 /* almost compatible with FUJITSU, but no bass and SPDIF */
1125                 .type = HDA_FIXUP_PINS,
1126                 .v.pins = (const struct hda_pintbl[]) {
1127                         { 0x14, 0x0121411f }, /* HP */
1128                         { 0x15, 0x99030120 }, /* speaker */
1129                         { 0x16, 0x411111f0 }, /* N/A */
1130                         { 0x17, 0x411111f0 }, /* N/A */
1131                         { 0x18, 0x411111f0 }, /* N/A */
1132                         { 0x19, 0x01a19950 }, /* mic-in */
1133                         { 0x1a, 0x411111f0 }, /* N/A */
1134                         { 0x1b, 0x411111f0 }, /* N/A */
1135                         { 0x1c, 0x411111f0 }, /* N/A */
1136                         { 0x1d, 0x411111f0 }, /* N/A */
1137                         { 0x1e, 0x411111f0 }, /* N/A */
1138                         { }
1139                 },
1140                 .chained = true,
1141                 .chain_id = ALC880_FIXUP_VOL_KNOB,
1142         },
1143         [ALC880_FIXUP_UNIWILL] = {
1144                 /* need to fix HP and speaker pins to be parsed correctly */
1145                 .type = HDA_FIXUP_PINS,
1146                 .v.pins = (const struct hda_pintbl[]) {
1147                         { 0x14, 0x0121411f }, /* HP */
1148                         { 0x15, 0x99030120 }, /* speaker */
1149                         { 0x16, 0x99030130 }, /* bass speaker */
1150                         { }
1151                 },
1152         },
1153         [ALC880_FIXUP_UNIWILL_DIG] = {
1154                 .type = HDA_FIXUP_PINS,
1155                 .v.pins = (const struct hda_pintbl[]) {
1156                         /* disable bogus unused pins */
1157                         { 0x17, 0x411111f0 },
1158                         { 0x19, 0x411111f0 },
1159                         { 0x1b, 0x411111f0 },
1160                         { 0x1f, 0x411111f0 },
1161                         { }
1162                 }
1163         },
1164         [ALC880_FIXUP_Z71V] = {
1165                 .type = HDA_FIXUP_PINS,
1166                 .v.pins = (const struct hda_pintbl[]) {
1167                         /* set up the whole pins as BIOS is utterly broken */
1168                         { 0x14, 0x99030120 }, /* speaker */
1169                         { 0x15, 0x0121411f }, /* HP */
1170                         { 0x16, 0x411111f0 }, /* N/A */
1171                         { 0x17, 0x411111f0 }, /* N/A */
1172                         { 0x18, 0x01a19950 }, /* mic-in */
1173                         { 0x19, 0x411111f0 }, /* N/A */
1174                         { 0x1a, 0x01813031 }, /* line-in */
1175                         { 0x1b, 0x411111f0 }, /* N/A */
1176                         { 0x1c, 0x411111f0 }, /* N/A */
1177                         { 0x1d, 0x411111f0 }, /* N/A */
1178                         { 0x1e, 0x0144111e }, /* SPDIF */
1179                         { }
1180                 }
1181         },
1182         [ALC880_FIXUP_3ST_BASE] = {
1183                 .type = HDA_FIXUP_PINS,
1184                 .v.pins = (const struct hda_pintbl[]) {
1185                         { 0x14, 0x01014010 }, /* line-out */
1186                         { 0x15, 0x411111f0 }, /* N/A */
1187                         { 0x16, 0x411111f0 }, /* N/A */
1188                         { 0x17, 0x411111f0 }, /* N/A */
1189                         { 0x18, 0x01a19c30 }, /* mic-in */
1190                         { 0x19, 0x0121411f }, /* HP */
1191                         { 0x1a, 0x01813031 }, /* line-in */
1192                         { 0x1b, 0x02a19c40 }, /* front-mic */
1193                         { 0x1c, 0x411111f0 }, /* N/A */
1194                         { 0x1d, 0x411111f0 }, /* N/A */
1195                         /* 0x1e is filled in below */
1196                         { 0x1f, 0x411111f0 }, /* N/A */
1197                         { }
1198                 }
1199         },
1200         [ALC880_FIXUP_3ST] = {
1201                 .type = HDA_FIXUP_PINS,
1202                 .v.pins = (const struct hda_pintbl[]) {
1203                         { 0x1e, 0x411111f0 }, /* N/A */
1204                         { }
1205                 },
1206                 .chained = true,
1207                 .chain_id = ALC880_FIXUP_3ST_BASE,
1208         },
1209         [ALC880_FIXUP_3ST_DIG] = {
1210                 .type = HDA_FIXUP_PINS,
1211                 .v.pins = (const struct hda_pintbl[]) {
1212                         { 0x1e, 0x0144111e }, /* SPDIF */
1213                         { }
1214                 },
1215                 .chained = true,
1216                 .chain_id = ALC880_FIXUP_3ST_BASE,
1217         },
1218         [ALC880_FIXUP_5ST_BASE] = {
1219                 .type = HDA_FIXUP_PINS,
1220                 .v.pins = (const struct hda_pintbl[]) {
1221                         { 0x14, 0x01014010 }, /* front */
1222                         { 0x15, 0x411111f0 }, /* N/A */
1223                         { 0x16, 0x01011411 }, /* CLFE */
1224                         { 0x17, 0x01016412 }, /* surr */
1225                         { 0x18, 0x01a19c30 }, /* mic-in */
1226                         { 0x19, 0x0121411f }, /* HP */
1227                         { 0x1a, 0x01813031 }, /* line-in */
1228                         { 0x1b, 0x02a19c40 }, /* front-mic */
1229                         { 0x1c, 0x411111f0 }, /* N/A */
1230                         { 0x1d, 0x411111f0 }, /* N/A */
1231                         /* 0x1e is filled in below */
1232                         { 0x1f, 0x411111f0 }, /* N/A */
1233                         { }
1234                 }
1235         },
1236         [ALC880_FIXUP_5ST] = {
1237                 .type = HDA_FIXUP_PINS,
1238                 .v.pins = (const struct hda_pintbl[]) {
1239                         { 0x1e, 0x411111f0 }, /* N/A */
1240                         { }
1241                 },
1242                 .chained = true,
1243                 .chain_id = ALC880_FIXUP_5ST_BASE,
1244         },
1245         [ALC880_FIXUP_5ST_DIG] = {
1246                 .type = HDA_FIXUP_PINS,
1247                 .v.pins = (const struct hda_pintbl[]) {
1248                         { 0x1e, 0x0144111e }, /* SPDIF */
1249                         { }
1250                 },
1251                 .chained = true,
1252                 .chain_id = ALC880_FIXUP_5ST_BASE,
1253         },
1254         [ALC880_FIXUP_6ST_BASE] = {
1255                 .type = HDA_FIXUP_PINS,
1256                 .v.pins = (const struct hda_pintbl[]) {
1257                         { 0x14, 0x01014010 }, /* front */
1258                         { 0x15, 0x01016412 }, /* surr */
1259                         { 0x16, 0x01011411 }, /* CLFE */
1260                         { 0x17, 0x01012414 }, /* side */
1261                         { 0x18, 0x01a19c30 }, /* mic-in */
1262                         { 0x19, 0x02a19c40 }, /* front-mic */
1263                         { 0x1a, 0x01813031 }, /* line-in */
1264                         { 0x1b, 0x0121411f }, /* HP */
1265                         { 0x1c, 0x411111f0 }, /* N/A */
1266                         { 0x1d, 0x411111f0 }, /* N/A */
1267                         /* 0x1e is filled in below */
1268                         { 0x1f, 0x411111f0 }, /* N/A */
1269                         { }
1270                 }
1271         },
1272         [ALC880_FIXUP_6ST] = {
1273                 .type = HDA_FIXUP_PINS,
1274                 .v.pins = (const struct hda_pintbl[]) {
1275                         { 0x1e, 0x411111f0 }, /* N/A */
1276                         { }
1277                 },
1278                 .chained = true,
1279                 .chain_id = ALC880_FIXUP_6ST_BASE,
1280         },
1281         [ALC880_FIXUP_6ST_DIG] = {
1282                 .type = HDA_FIXUP_PINS,
1283                 .v.pins = (const struct hda_pintbl[]) {
1284                         { 0x1e, 0x0144111e }, /* SPDIF */
1285                         { }
1286                 },
1287                 .chained = true,
1288                 .chain_id = ALC880_FIXUP_6ST_BASE,
1289         },
1290         [ALC880_FIXUP_6ST_AUTOMUTE] = {
1291                 .type = HDA_FIXUP_PINS,
1292                 .v.pins = (const struct hda_pintbl[]) {
1293                         { 0x1b, 0x0121401f }, /* HP with jack detect */
1294                         { }
1295                 },
1296                 .chained_before = true,
1297                 .chain_id = ALC880_FIXUP_6ST_BASE,
1298         },
1299 };
1300
1301 static const struct snd_pci_quirk alc880_fixup_tbl[] = {
1302         SND_PCI_QUIRK(0x1019, 0x0f69, "Coeus G610P", ALC880_FIXUP_W810),
1303         SND_PCI_QUIRK(0x1043, 0x1964, "ASUS Z71V", ALC880_FIXUP_Z71V),
1304         SND_PCI_QUIRK_VENDOR(0x1043, "ASUS", ALC880_FIXUP_GPIO1),
1305         SND_PCI_QUIRK(0x1558, 0x5401, "Clevo GPIO2", ALC880_FIXUP_GPIO2),
1306         SND_PCI_QUIRK_VENDOR(0x1558, "Clevo", ALC880_FIXUP_EAPD_COEF),
1307         SND_PCI_QUIRK(0x1584, 0x9050, "Uniwill", ALC880_FIXUP_UNIWILL_DIG),
1308         SND_PCI_QUIRK(0x1584, 0x9054, "Uniwill", ALC880_FIXUP_F1734),
1309         SND_PCI_QUIRK(0x1584, 0x9070, "Uniwill", ALC880_FIXUP_UNIWILL),
1310         SND_PCI_QUIRK(0x1584, 0x9077, "Uniwill P53", ALC880_FIXUP_VOL_KNOB),
1311         SND_PCI_QUIRK(0x161f, 0x203d, "W810", ALC880_FIXUP_W810),
1312         SND_PCI_QUIRK(0x161f, 0x205d, "Medion Rim 2150", ALC880_FIXUP_MEDION_RIM),
1313         SND_PCI_QUIRK(0x1631, 0xe011, "PB 13201056", ALC880_FIXUP_6ST_AUTOMUTE),
1314         SND_PCI_QUIRK(0x1734, 0x107c, "FSC F1734", ALC880_FIXUP_F1734),
1315         SND_PCI_QUIRK(0x1734, 0x1094, "FSC Amilo M1451G", ALC880_FIXUP_FUJITSU),
1316         SND_PCI_QUIRK(0x1734, 0x10ac, "FSC AMILO Xi 1526", ALC880_FIXUP_F1734),
1317         SND_PCI_QUIRK(0x1734, 0x10b0, "FSC Amilo Pi1556", ALC880_FIXUP_FUJITSU),
1318         SND_PCI_QUIRK(0x1854, 0x003b, "LG", ALC880_FIXUP_LG),
1319         SND_PCI_QUIRK(0x1854, 0x005f, "LG P1 Express", ALC880_FIXUP_LG),
1320         SND_PCI_QUIRK(0x1854, 0x0068, "LG w1", ALC880_FIXUP_LG),
1321         SND_PCI_QUIRK(0x19db, 0x4188, "TCL S700", ALC880_FIXUP_TCL_S700),
1322
1323         /* Below is the copied entries from alc880_quirks.c.
1324          * It's not quite sure whether BIOS sets the correct pin-config table
1325          * on these machines, thus they are kept to be compatible with
1326          * the old static quirks.  Once when it's confirmed to work without
1327          * these overrides, it'd be better to remove.
1328          */
1329         SND_PCI_QUIRK(0x1019, 0xa880, "ECS", ALC880_FIXUP_5ST_DIG),
1330         SND_PCI_QUIRK(0x1019, 0xa884, "Acer APFV", ALC880_FIXUP_6ST),
1331         SND_PCI_QUIRK(0x1025, 0x0070, "ULI", ALC880_FIXUP_3ST_DIG),
1332         SND_PCI_QUIRK(0x1025, 0x0077, "ULI", ALC880_FIXUP_6ST_DIG),
1333         SND_PCI_QUIRK(0x1025, 0x0078, "ULI", ALC880_FIXUP_6ST_DIG),
1334         SND_PCI_QUIRK(0x1025, 0x0087, "ULI", ALC880_FIXUP_6ST_DIG),
1335         SND_PCI_QUIRK(0x1025, 0xe309, "ULI", ALC880_FIXUP_3ST_DIG),
1336         SND_PCI_QUIRK(0x1025, 0xe310, "ULI", ALC880_FIXUP_3ST),
1337         SND_PCI_QUIRK(0x1039, 0x1234, NULL, ALC880_FIXUP_6ST_DIG),
1338         SND_PCI_QUIRK(0x104d, 0x81a0, "Sony", ALC880_FIXUP_3ST),
1339         SND_PCI_QUIRK(0x104d, 0x81d6, "Sony", ALC880_FIXUP_3ST),
1340         SND_PCI_QUIRK(0x107b, 0x3032, "Gateway", ALC880_FIXUP_5ST),
1341         SND_PCI_QUIRK(0x107b, 0x3033, "Gateway", ALC880_FIXUP_5ST),
1342         SND_PCI_QUIRK(0x107b, 0x4039, "Gateway", ALC880_FIXUP_5ST),
1343         SND_PCI_QUIRK(0x1297, 0xc790, "Shuttle ST20G5", ALC880_FIXUP_6ST_DIG),
1344         SND_PCI_QUIRK(0x1458, 0xa102, "Gigabyte K8", ALC880_FIXUP_6ST_DIG),
1345         SND_PCI_QUIRK(0x1462, 0x1150, "MSI", ALC880_FIXUP_6ST_DIG),
1346         SND_PCI_QUIRK(0x1509, 0x925d, "FIC P4M", ALC880_FIXUP_6ST_DIG),
1347         SND_PCI_QUIRK(0x1565, 0x8202, "Biostar", ALC880_FIXUP_5ST_DIG),
1348         SND_PCI_QUIRK(0x1695, 0x400d, "EPoX", ALC880_FIXUP_5ST_DIG),
1349         SND_PCI_QUIRK(0x1695, 0x4012, "EPox EP-5LDA", ALC880_FIXUP_5ST_DIG),
1350         SND_PCI_QUIRK(0x2668, 0x8086, NULL, ALC880_FIXUP_6ST_DIG), /* broken BIOS */
1351         SND_PCI_QUIRK(0x8086, 0x2668, NULL, ALC880_FIXUP_6ST_DIG),
1352         SND_PCI_QUIRK(0x8086, 0xa100, "Intel mobo", ALC880_FIXUP_5ST_DIG),
1353         SND_PCI_QUIRK(0x8086, 0xd400, "Intel mobo", ALC880_FIXUP_5ST_DIG),
1354         SND_PCI_QUIRK(0x8086, 0xd401, "Intel mobo", ALC880_FIXUP_5ST_DIG),
1355         SND_PCI_QUIRK(0x8086, 0xd402, "Intel mobo", ALC880_FIXUP_3ST_DIG),
1356         SND_PCI_QUIRK(0x8086, 0xe224, "Intel mobo", ALC880_FIXUP_5ST_DIG),
1357         SND_PCI_QUIRK(0x8086, 0xe305, "Intel mobo", ALC880_FIXUP_3ST_DIG),
1358         SND_PCI_QUIRK(0x8086, 0xe308, "Intel mobo", ALC880_FIXUP_3ST_DIG),
1359         SND_PCI_QUIRK(0x8086, 0xe400, "Intel mobo", ALC880_FIXUP_5ST_DIG),
1360         SND_PCI_QUIRK(0x8086, 0xe401, "Intel mobo", ALC880_FIXUP_5ST_DIG),
1361         SND_PCI_QUIRK(0x8086, 0xe402, "Intel mobo", ALC880_FIXUP_5ST_DIG),
1362         /* default Intel */
1363         SND_PCI_QUIRK_VENDOR(0x8086, "Intel mobo", ALC880_FIXUP_3ST),
1364         SND_PCI_QUIRK(0xa0a0, 0x0560, "AOpen i915GMm-HFS", ALC880_FIXUP_5ST_DIG),
1365         SND_PCI_QUIRK(0xe803, 0x1019, NULL, ALC880_FIXUP_6ST_DIG),
1366         {}
1367 };
1368
1369 static const struct hda_model_fixup alc880_fixup_models[] = {
1370         {.id = ALC880_FIXUP_3ST, .name = "3stack"},
1371         {.id = ALC880_FIXUP_3ST_DIG, .name = "3stack-digout"},
1372         {.id = ALC880_FIXUP_5ST, .name = "5stack"},
1373         {.id = ALC880_FIXUP_5ST_DIG, .name = "5stack-digout"},
1374         {.id = ALC880_FIXUP_6ST, .name = "6stack"},
1375         {.id = ALC880_FIXUP_6ST_DIG, .name = "6stack-digout"},
1376         {.id = ALC880_FIXUP_6ST_AUTOMUTE, .name = "6stack-automute"},
1377         {}
1378 };
1379
1380
1381 /*
1382  * OK, here we have finally the patch for ALC880
1383  */
1384 static int patch_alc880(struct hda_codec *codec)
1385 {
1386         struct alc_spec *spec;
1387         int err;
1388
1389         err = alc_alloc_spec(codec, 0x0b);
1390         if (err < 0)
1391                 return err;
1392
1393         spec = codec->spec;
1394         spec->gen.need_dac_fix = 1;
1395         spec->gen.beep_nid = 0x01;
1396
1397         snd_hda_pick_fixup(codec, alc880_fixup_models, alc880_fixup_tbl,
1398                        alc880_fixups);
1399         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1400
1401         /* automatic parse from the BIOS config */
1402         err = alc880_parse_auto_config(codec);
1403         if (err < 0)
1404                 goto error;
1405
1406         if (!spec->gen.no_analog)
1407                 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
1408
1409         codec->patch_ops = alc_patch_ops;
1410         codec->patch_ops.unsol_event = alc880_unsol_event;
1411
1412
1413         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1414
1415         return 0;
1416
1417  error:
1418         alc_free(codec);
1419         return err;
1420 }
1421
1422
1423 /*
1424  * ALC260 support
1425  */
1426 static int alc260_parse_auto_config(struct hda_codec *codec)
1427 {
1428         static const hda_nid_t alc260_ignore[] = { 0x17, 0 };
1429         static const hda_nid_t alc260_ssids[] = { 0x10, 0x15, 0x0f, 0 };
1430         return alc_parse_auto_config(codec, alc260_ignore, alc260_ssids);
1431 }
1432
1433 /*
1434  * Pin config fixes
1435  */
1436 enum {
1437         ALC260_FIXUP_HP_DC5750,
1438         ALC260_FIXUP_HP_PIN_0F,
1439         ALC260_FIXUP_COEF,
1440         ALC260_FIXUP_GPIO1,
1441         ALC260_FIXUP_GPIO1_TOGGLE,
1442         ALC260_FIXUP_REPLACER,
1443         ALC260_FIXUP_HP_B1900,
1444         ALC260_FIXUP_KN1,
1445         ALC260_FIXUP_FSC_S7020,
1446         ALC260_FIXUP_FSC_S7020_JWSE,
1447 };
1448
1449 static void alc260_gpio1_automute(struct hda_codec *codec)
1450 {
1451         struct alc_spec *spec = codec->spec;
1452         snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1453                             spec->gen.hp_jack_present);
1454 }
1455
1456 static void alc260_fixup_gpio1_toggle(struct hda_codec *codec,
1457                                       const struct hda_fixup *fix, int action)
1458 {
1459         struct alc_spec *spec = codec->spec;
1460         if (action == HDA_FIXUP_ACT_PROBE) {
1461                 /* although the machine has only one output pin, we need to
1462                  * toggle GPIO1 according to the jack state
1463                  */
1464                 spec->gen.automute_hook = alc260_gpio1_automute;
1465                 spec->gen.detect_hp = 1;
1466                 spec->gen.automute_speaker = 1;
1467                 spec->gen.autocfg.hp_pins[0] = 0x0f; /* copy it for automute */
1468                 snd_hda_jack_detect_enable_callback(codec, 0x0f, HDA_GEN_HP_EVENT,
1469                                                     snd_hda_gen_hp_automute);
1470                 snd_hda_add_verbs(codec, alc_gpio1_init_verbs);
1471         }
1472 }
1473
1474 static void alc260_fixup_kn1(struct hda_codec *codec,
1475                              const struct hda_fixup *fix, int action)
1476 {
1477         struct alc_spec *spec = codec->spec;
1478         static const struct hda_pintbl pincfgs[] = {
1479                 { 0x0f, 0x02214000 }, /* HP/speaker */
1480                 { 0x12, 0x90a60160 }, /* int mic */
1481                 { 0x13, 0x02a19000 }, /* ext mic */
1482                 { 0x18, 0x01446000 }, /* SPDIF out */
1483                 /* disable bogus I/O pins */
1484                 { 0x10, 0x411111f0 },
1485                 { 0x11, 0x411111f0 },
1486                 { 0x14, 0x411111f0 },
1487                 { 0x15, 0x411111f0 },
1488                 { 0x16, 0x411111f0 },
1489                 { 0x17, 0x411111f0 },
1490                 { 0x19, 0x411111f0 },
1491                 { }
1492         };
1493
1494         switch (action) {
1495         case HDA_FIXUP_ACT_PRE_PROBE:
1496                 snd_hda_apply_pincfgs(codec, pincfgs);
1497                 break;
1498         case HDA_FIXUP_ACT_PROBE:
1499                 spec->init_amp = ALC_INIT_NONE;
1500                 break;
1501         }
1502 }
1503
1504 static void alc260_fixup_fsc_s7020(struct hda_codec *codec,
1505                                    const struct hda_fixup *fix, int action)
1506 {
1507         struct alc_spec *spec = codec->spec;
1508         if (action == HDA_FIXUP_ACT_PROBE)
1509                 spec->init_amp = ALC_INIT_NONE;
1510 }
1511
1512 static void alc260_fixup_fsc_s7020_jwse(struct hda_codec *codec,
1513                                    const struct hda_fixup *fix, int action)
1514 {
1515         struct alc_spec *spec = codec->spec;
1516         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1517                 spec->gen.add_jack_modes = 1;
1518                 spec->gen.hp_mic = 1;
1519         }
1520 }
1521
1522 static const struct hda_fixup alc260_fixups[] = {
1523         [ALC260_FIXUP_HP_DC5750] = {
1524                 .type = HDA_FIXUP_PINS,
1525                 .v.pins = (const struct hda_pintbl[]) {
1526                         { 0x11, 0x90130110 }, /* speaker */
1527                         { }
1528                 }
1529         },
1530         [ALC260_FIXUP_HP_PIN_0F] = {
1531                 .type = HDA_FIXUP_PINS,
1532                 .v.pins = (const struct hda_pintbl[]) {
1533                         { 0x0f, 0x01214000 }, /* HP */
1534                         { }
1535                 }
1536         },
1537         [ALC260_FIXUP_COEF] = {
1538                 .type = HDA_FIXUP_VERBS,
1539                 .v.verbs = (const struct hda_verb[]) {
1540                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
1541                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3040 },
1542                         { }
1543                 },
1544                 .chained = true,
1545                 .chain_id = ALC260_FIXUP_HP_PIN_0F,
1546         },
1547         [ALC260_FIXUP_GPIO1] = {
1548                 .type = HDA_FIXUP_VERBS,
1549                 .v.verbs = alc_gpio1_init_verbs,
1550         },
1551         [ALC260_FIXUP_GPIO1_TOGGLE] = {
1552                 .type = HDA_FIXUP_FUNC,
1553                 .v.func = alc260_fixup_gpio1_toggle,
1554                 .chained = true,
1555                 .chain_id = ALC260_FIXUP_HP_PIN_0F,
1556         },
1557         [ALC260_FIXUP_REPLACER] = {
1558                 .type = HDA_FIXUP_VERBS,
1559                 .v.verbs = (const struct hda_verb[]) {
1560                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
1561                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3050 },
1562                         { }
1563                 },
1564                 .chained = true,
1565                 .chain_id = ALC260_FIXUP_GPIO1_TOGGLE,
1566         },
1567         [ALC260_FIXUP_HP_B1900] = {
1568                 .type = HDA_FIXUP_FUNC,
1569                 .v.func = alc260_fixup_gpio1_toggle,
1570                 .chained = true,
1571                 .chain_id = ALC260_FIXUP_COEF,
1572         },
1573         [ALC260_FIXUP_KN1] = {
1574                 .type = HDA_FIXUP_FUNC,
1575                 .v.func = alc260_fixup_kn1,
1576         },
1577         [ALC260_FIXUP_FSC_S7020] = {
1578                 .type = HDA_FIXUP_FUNC,
1579                 .v.func = alc260_fixup_fsc_s7020,
1580         },
1581         [ALC260_FIXUP_FSC_S7020_JWSE] = {
1582                 .type = HDA_FIXUP_FUNC,
1583                 .v.func = alc260_fixup_fsc_s7020_jwse,
1584                 .chained = true,
1585                 .chain_id = ALC260_FIXUP_FSC_S7020,
1586         },
1587 };
1588
1589 static const struct snd_pci_quirk alc260_fixup_tbl[] = {
1590         SND_PCI_QUIRK(0x1025, 0x007b, "Acer C20x", ALC260_FIXUP_GPIO1),
1591         SND_PCI_QUIRK(0x1025, 0x007f, "Acer Aspire 9500", ALC260_FIXUP_COEF),
1592         SND_PCI_QUIRK(0x1025, 0x008f, "Acer", ALC260_FIXUP_GPIO1),
1593         SND_PCI_QUIRK(0x103c, 0x280a, "HP dc5750", ALC260_FIXUP_HP_DC5750),
1594         SND_PCI_QUIRK(0x103c, 0x30ba, "HP Presario B1900", ALC260_FIXUP_HP_B1900),
1595         SND_PCI_QUIRK(0x10cf, 0x1326, "FSC LifeBook S7020", ALC260_FIXUP_FSC_S7020),
1596         SND_PCI_QUIRK(0x1509, 0x4540, "Favorit 100XS", ALC260_FIXUP_GPIO1),
1597         SND_PCI_QUIRK(0x152d, 0x0729, "Quanta KN1", ALC260_FIXUP_KN1),
1598         SND_PCI_QUIRK(0x161f, 0x2057, "Replacer 672V", ALC260_FIXUP_REPLACER),
1599         SND_PCI_QUIRK(0x1631, 0xc017, "PB V7900", ALC260_FIXUP_COEF),
1600         {}
1601 };
1602
1603 static const struct hda_model_fixup alc260_fixup_models[] = {
1604         {.id = ALC260_FIXUP_GPIO1, .name = "gpio1"},
1605         {.id = ALC260_FIXUP_COEF, .name = "coef"},
1606         {.id = ALC260_FIXUP_FSC_S7020, .name = "fujitsu"},
1607         {.id = ALC260_FIXUP_FSC_S7020_JWSE, .name = "fujitsu-jwse"},
1608         {}
1609 };
1610
1611 /*
1612  */
1613 static int patch_alc260(struct hda_codec *codec)
1614 {
1615         struct alc_spec *spec;
1616         int err;
1617
1618         err = alc_alloc_spec(codec, 0x07);
1619         if (err < 0)
1620                 return err;
1621
1622         spec = codec->spec;
1623         /* as quite a few machines require HP amp for speaker outputs,
1624          * it's easier to enable it unconditionally; even if it's unneeded,
1625          * it's almost harmless.
1626          */
1627         spec->gen.prefer_hp_amp = 1;
1628         spec->gen.beep_nid = 0x01;
1629
1630         snd_hda_pick_fixup(codec, alc260_fixup_models, alc260_fixup_tbl,
1631                            alc260_fixups);
1632         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1633
1634         /* automatic parse from the BIOS config */
1635         err = alc260_parse_auto_config(codec);
1636         if (err < 0)
1637                 goto error;
1638
1639         if (!spec->gen.no_analog)
1640                 set_beep_amp(spec, 0x07, 0x05, HDA_INPUT);
1641
1642         codec->patch_ops = alc_patch_ops;
1643         spec->shutup = alc_eapd_shutup;
1644
1645         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1646
1647         return 0;
1648
1649  error:
1650         alc_free(codec);
1651         return err;
1652 }
1653
1654
1655 /*
1656  * ALC882/883/885/888/889 support
1657  *
1658  * ALC882 is almost identical with ALC880 but has cleaner and more flexible
1659  * configuration.  Each pin widget can choose any input DACs and a mixer.
1660  * Each ADC is connected from a mixer of all inputs.  This makes possible
1661  * 6-channel independent captures.
1662  *
1663  * In addition, an independent DAC for the multi-playback (not used in this
1664  * driver yet).
1665  */
1666
1667 /*
1668  * Pin config fixes
1669  */
1670 enum {
1671         ALC882_FIXUP_ABIT_AW9D_MAX,
1672         ALC882_FIXUP_LENOVO_Y530,
1673         ALC882_FIXUP_PB_M5210,
1674         ALC882_FIXUP_ACER_ASPIRE_7736,
1675         ALC882_FIXUP_ASUS_W90V,
1676         ALC889_FIXUP_CD,
1677         ALC889_FIXUP_VAIO_TT,
1678         ALC888_FIXUP_EEE1601,
1679         ALC882_FIXUP_EAPD,
1680         ALC883_FIXUP_EAPD,
1681         ALC883_FIXUP_ACER_EAPD,
1682         ALC882_FIXUP_GPIO1,
1683         ALC882_FIXUP_GPIO2,
1684         ALC882_FIXUP_GPIO3,
1685         ALC889_FIXUP_COEF,
1686         ALC882_FIXUP_ASUS_W2JC,
1687         ALC882_FIXUP_ACER_ASPIRE_4930G,
1688         ALC882_FIXUP_ACER_ASPIRE_8930G,
1689         ALC882_FIXUP_ASPIRE_8930G_VERBS,
1690         ALC885_FIXUP_MACPRO_GPIO,
1691         ALC889_FIXUP_DAC_ROUTE,
1692         ALC889_FIXUP_MBP_VREF,
1693         ALC889_FIXUP_IMAC91_VREF,
1694         ALC882_FIXUP_INV_DMIC,
1695         ALC882_FIXUP_NO_PRIMARY_HP,
1696 };
1697
1698 static void alc889_fixup_coef(struct hda_codec *codec,
1699                               const struct hda_fixup *fix, int action)
1700 {
1701         if (action != HDA_FIXUP_ACT_INIT)
1702                 return;
1703         alc889_coef_init(codec);
1704 }
1705
1706 /* toggle speaker-output according to the hp-jack state */
1707 static void alc882_gpio_mute(struct hda_codec *codec, int pin, int muted)
1708 {
1709         unsigned int gpiostate, gpiomask, gpiodir;
1710
1711         gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
1712                                        AC_VERB_GET_GPIO_DATA, 0);
1713
1714         if (!muted)
1715                 gpiostate |= (1 << pin);
1716         else
1717                 gpiostate &= ~(1 << pin);
1718
1719         gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
1720                                       AC_VERB_GET_GPIO_MASK, 0);
1721         gpiomask |= (1 << pin);
1722
1723         gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
1724                                      AC_VERB_GET_GPIO_DIRECTION, 0);
1725         gpiodir |= (1 << pin);
1726
1727
1728         snd_hda_codec_write(codec, codec->afg, 0,
1729                             AC_VERB_SET_GPIO_MASK, gpiomask);
1730         snd_hda_codec_write(codec, codec->afg, 0,
1731                             AC_VERB_SET_GPIO_DIRECTION, gpiodir);
1732
1733         msleep(1);
1734
1735         snd_hda_codec_write(codec, codec->afg, 0,
1736                             AC_VERB_SET_GPIO_DATA, gpiostate);
1737 }
1738
1739 /* set up GPIO at initialization */
1740 static void alc885_fixup_macpro_gpio(struct hda_codec *codec,
1741                                      const struct hda_fixup *fix, int action)
1742 {
1743         if (action != HDA_FIXUP_ACT_INIT)
1744                 return;
1745         alc882_gpio_mute(codec, 0, 0);
1746         alc882_gpio_mute(codec, 1, 0);
1747 }
1748
1749 /* Fix the connection of some pins for ALC889:
1750  * At least, Acer Aspire 5935 shows the connections to DAC3/4 don't
1751  * work correctly (bko#42740)
1752  */
1753 static void alc889_fixup_dac_route(struct hda_codec *codec,
1754                                    const struct hda_fixup *fix, int action)
1755 {
1756         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1757                 /* fake the connections during parsing the tree */
1758                 hda_nid_t conn1[2] = { 0x0c, 0x0d };
1759                 hda_nid_t conn2[2] = { 0x0e, 0x0f };
1760                 snd_hda_override_conn_list(codec, 0x14, 2, conn1);
1761                 snd_hda_override_conn_list(codec, 0x15, 2, conn1);
1762                 snd_hda_override_conn_list(codec, 0x18, 2, conn2);
1763                 snd_hda_override_conn_list(codec, 0x1a, 2, conn2);
1764         } else if (action == HDA_FIXUP_ACT_PROBE) {
1765                 /* restore the connections */
1766                 hda_nid_t conn[5] = { 0x0c, 0x0d, 0x0e, 0x0f, 0x26 };
1767                 snd_hda_override_conn_list(codec, 0x14, 5, conn);
1768                 snd_hda_override_conn_list(codec, 0x15, 5, conn);
1769                 snd_hda_override_conn_list(codec, 0x18, 5, conn);
1770                 snd_hda_override_conn_list(codec, 0x1a, 5, conn);
1771         }
1772 }
1773
1774 /* Set VREF on HP pin */
1775 static void alc889_fixup_mbp_vref(struct hda_codec *codec,
1776                                   const struct hda_fixup *fix, int action)
1777 {
1778         struct alc_spec *spec = codec->spec;
1779         static hda_nid_t nids[2] = { 0x14, 0x15 };
1780         int i;
1781
1782         if (action != HDA_FIXUP_ACT_INIT)
1783                 return;
1784         for (i = 0; i < ARRAY_SIZE(nids); i++) {
1785                 unsigned int val = snd_hda_codec_get_pincfg(codec, nids[i]);
1786                 if (get_defcfg_device(val) != AC_JACK_HP_OUT)
1787                         continue;
1788                 val = snd_hda_codec_get_pin_target(codec, nids[i]);
1789                 val |= AC_PINCTL_VREF_80;
1790                 snd_hda_set_pin_ctl(codec, nids[i], val);
1791                 spec->gen.keep_vref_in_automute = 1;
1792                 break;
1793         }
1794 }
1795
1796 /* Set VREF on speaker pins on imac91 */
1797 static void alc889_fixup_imac91_vref(struct hda_codec *codec,
1798                                      const struct hda_fixup *fix, int action)
1799 {
1800         struct alc_spec *spec = codec->spec;
1801         static hda_nid_t nids[2] = { 0x18, 0x1a };
1802         int i;
1803
1804         if (action != HDA_FIXUP_ACT_INIT)
1805                 return;
1806         for (i = 0; i < ARRAY_SIZE(nids); i++) {
1807                 unsigned int val;
1808                 val = snd_hda_codec_get_pin_target(codec, nids[i]);
1809                 val |= AC_PINCTL_VREF_50;
1810                 snd_hda_set_pin_ctl(codec, nids[i], val);
1811         }
1812         spec->gen.keep_vref_in_automute = 1;
1813 }
1814
1815 /* Don't take HP output as primary
1816  * Strangely, the speaker output doesn't work on Vaio Z and some Vaio
1817  * all-in-one desktop PCs (for example VGC-LN51JGB) through DAC 0x05
1818  */
1819 static void alc882_fixup_no_primary_hp(struct hda_codec *codec,
1820                                        const struct hda_fixup *fix, int action)
1821 {
1822         struct alc_spec *spec = codec->spec;
1823         if (action == HDA_FIXUP_ACT_PRE_PROBE)
1824                 spec->gen.no_primary_hp = 1;
1825 }
1826
1827 static const struct hda_fixup alc882_fixups[] = {
1828         [ALC882_FIXUP_ABIT_AW9D_MAX] = {
1829                 .type = HDA_FIXUP_PINS,
1830                 .v.pins = (const struct hda_pintbl[]) {
1831                         { 0x15, 0x01080104 }, /* side */
1832                         { 0x16, 0x01011012 }, /* rear */
1833                         { 0x17, 0x01016011 }, /* clfe */
1834                         { }
1835                 }
1836         },
1837         [ALC882_FIXUP_LENOVO_Y530] = {
1838                 .type = HDA_FIXUP_PINS,
1839                 .v.pins = (const struct hda_pintbl[]) {
1840                         { 0x15, 0x99130112 }, /* rear int speakers */
1841                         { 0x16, 0x99130111 }, /* subwoofer */
1842                         { }
1843                 }
1844         },
1845         [ALC882_FIXUP_PB_M5210] = {
1846                 .type = HDA_FIXUP_PINCTLS,
1847                 .v.pins = (const struct hda_pintbl[]) {
1848                         { 0x19, PIN_VREF50 },
1849                         {}
1850                 }
1851         },
1852         [ALC882_FIXUP_ACER_ASPIRE_7736] = {
1853                 .type = HDA_FIXUP_FUNC,
1854                 .v.func = alc_fixup_sku_ignore,
1855         },
1856         [ALC882_FIXUP_ASUS_W90V] = {
1857                 .type = HDA_FIXUP_PINS,
1858                 .v.pins = (const struct hda_pintbl[]) {
1859                         { 0x16, 0x99130110 }, /* fix sequence for CLFE */
1860                         { }
1861                 }
1862         },
1863         [ALC889_FIXUP_CD] = {
1864                 .type = HDA_FIXUP_PINS,
1865                 .v.pins = (const struct hda_pintbl[]) {
1866                         { 0x1c, 0x993301f0 }, /* CD */
1867                         { }
1868                 }
1869         },
1870         [ALC889_FIXUP_VAIO_TT] = {
1871                 .type = HDA_FIXUP_PINS,
1872                 .v.pins = (const struct hda_pintbl[]) {
1873                         { 0x17, 0x90170111 }, /* hidden surround speaker */
1874                         { }
1875                 }
1876         },
1877         [ALC888_FIXUP_EEE1601] = {
1878                 .type = HDA_FIXUP_VERBS,
1879                 .v.verbs = (const struct hda_verb[]) {
1880                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x0b },
1881                         { 0x20, AC_VERB_SET_PROC_COEF,  0x0838 },
1882                         { }
1883                 }
1884         },
1885         [ALC882_FIXUP_EAPD] = {
1886                 .type = HDA_FIXUP_VERBS,
1887                 .v.verbs = (const struct hda_verb[]) {
1888                         /* change to EAPD mode */
1889                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
1890                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3060 },
1891                         { }
1892                 }
1893         },
1894         [ALC883_FIXUP_EAPD] = {
1895                 .type = HDA_FIXUP_VERBS,
1896                 .v.verbs = (const struct hda_verb[]) {
1897                         /* change to EAPD mode */
1898                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
1899                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3070 },
1900                         { }
1901                 }
1902         },
1903         [ALC883_FIXUP_ACER_EAPD] = {
1904                 .type = HDA_FIXUP_VERBS,
1905                 .v.verbs = (const struct hda_verb[]) {
1906                         /* eanable EAPD on Acer laptops */
1907                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
1908                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
1909                         { }
1910                 }
1911         },
1912         [ALC882_FIXUP_GPIO1] = {
1913                 .type = HDA_FIXUP_VERBS,
1914                 .v.verbs = alc_gpio1_init_verbs,
1915         },
1916         [ALC882_FIXUP_GPIO2] = {
1917                 .type = HDA_FIXUP_VERBS,
1918                 .v.verbs = alc_gpio2_init_verbs,
1919         },
1920         [ALC882_FIXUP_GPIO3] = {
1921                 .type = HDA_FIXUP_VERBS,
1922                 .v.verbs = alc_gpio3_init_verbs,
1923         },
1924         [ALC882_FIXUP_ASUS_W2JC] = {
1925                 .type = HDA_FIXUP_VERBS,
1926                 .v.verbs = alc_gpio1_init_verbs,
1927                 .chained = true,
1928                 .chain_id = ALC882_FIXUP_EAPD,
1929         },
1930         [ALC889_FIXUP_COEF] = {
1931                 .type = HDA_FIXUP_FUNC,
1932                 .v.func = alc889_fixup_coef,
1933         },
1934         [ALC882_FIXUP_ACER_ASPIRE_4930G] = {
1935                 .type = HDA_FIXUP_PINS,
1936                 .v.pins = (const struct hda_pintbl[]) {
1937                         { 0x16, 0x99130111 }, /* CLFE speaker */
1938                         { 0x17, 0x99130112 }, /* surround speaker */
1939                         { }
1940                 },
1941                 .chained = true,
1942                 .chain_id = ALC882_FIXUP_GPIO1,
1943         },
1944         [ALC882_FIXUP_ACER_ASPIRE_8930G] = {
1945                 .type = HDA_FIXUP_PINS,
1946                 .v.pins = (const struct hda_pintbl[]) {
1947                         { 0x16, 0x99130111 }, /* CLFE speaker */
1948                         { 0x1b, 0x99130112 }, /* surround speaker */
1949                         { }
1950                 },
1951                 .chained = true,
1952                 .chain_id = ALC882_FIXUP_ASPIRE_8930G_VERBS,
1953         },
1954         [ALC882_FIXUP_ASPIRE_8930G_VERBS] = {
1955                 /* additional init verbs for Acer Aspire 8930G */
1956                 .type = HDA_FIXUP_VERBS,
1957                 .v.verbs = (const struct hda_verb[]) {
1958                         /* Enable all DACs */
1959                         /* DAC DISABLE/MUTE 1? */
1960                         /*  setting bits 1-5 disables DAC nids 0x02-0x06
1961                          *  apparently. Init=0x38 */
1962                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x03 },
1963                         { 0x20, AC_VERB_SET_PROC_COEF, 0x0000 },
1964                         /* DAC DISABLE/MUTE 2? */
1965                         /*  some bit here disables the other DACs.
1966                          *  Init=0x4900 */
1967                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x08 },
1968                         { 0x20, AC_VERB_SET_PROC_COEF, 0x0000 },
1969                         /* DMIC fix
1970                          * This laptop has a stereo digital microphone.
1971                          * The mics are only 1cm apart which makes the stereo
1972                          * useless. However, either the mic or the ALC889
1973                          * makes the signal become a difference/sum signal
1974                          * instead of standard stereo, which is annoying.
1975                          * So instead we flip this bit which makes the
1976                          * codec replicate the sum signal to both channels,
1977                          * turning it into a normal mono mic.
1978                          */
1979                         /* DMIC_CONTROL? Init value = 0x0001 */
1980                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x0b },
1981                         { 0x20, AC_VERB_SET_PROC_COEF, 0x0003 },
1982                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
1983                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
1984                         { }
1985                 },
1986                 .chained = true,
1987                 .chain_id = ALC882_FIXUP_GPIO1,
1988         },
1989         [ALC885_FIXUP_MACPRO_GPIO] = {
1990                 .type = HDA_FIXUP_FUNC,
1991                 .v.func = alc885_fixup_macpro_gpio,
1992         },
1993         [ALC889_FIXUP_DAC_ROUTE] = {
1994                 .type = HDA_FIXUP_FUNC,
1995                 .v.func = alc889_fixup_dac_route,
1996         },
1997         [ALC889_FIXUP_MBP_VREF] = {
1998                 .type = HDA_FIXUP_FUNC,
1999                 .v.func = alc889_fixup_mbp_vref,
2000                 .chained = true,
2001                 .chain_id = ALC882_FIXUP_GPIO1,
2002         },
2003         [ALC889_FIXUP_IMAC91_VREF] = {
2004                 .type = HDA_FIXUP_FUNC,
2005                 .v.func = alc889_fixup_imac91_vref,
2006                 .chained = true,
2007                 .chain_id = ALC882_FIXUP_GPIO1,
2008         },
2009         [ALC882_FIXUP_INV_DMIC] = {
2010                 .type = HDA_FIXUP_FUNC,
2011                 .v.func = alc_fixup_inv_dmic_0x12,
2012         },
2013         [ALC882_FIXUP_NO_PRIMARY_HP] = {
2014                 .type = HDA_FIXUP_FUNC,
2015                 .v.func = alc882_fixup_no_primary_hp,
2016         },
2017 };
2018
2019 static const struct snd_pci_quirk alc882_fixup_tbl[] = {
2020         SND_PCI_QUIRK(0x1025, 0x006c, "Acer Aspire 9810", ALC883_FIXUP_ACER_EAPD),
2021         SND_PCI_QUIRK(0x1025, 0x0090, "Acer Aspire", ALC883_FIXUP_ACER_EAPD),
2022         SND_PCI_QUIRK(0x1025, 0x010a, "Acer Ferrari 5000", ALC883_FIXUP_ACER_EAPD),
2023         SND_PCI_QUIRK(0x1025, 0x0110, "Acer Aspire", ALC883_FIXUP_ACER_EAPD),
2024         SND_PCI_QUIRK(0x1025, 0x0112, "Acer Aspire 9303", ALC883_FIXUP_ACER_EAPD),
2025         SND_PCI_QUIRK(0x1025, 0x0121, "Acer Aspire 5920G", ALC883_FIXUP_ACER_EAPD),
2026         SND_PCI_QUIRK(0x1025, 0x013e, "Acer Aspire 4930G",
2027                       ALC882_FIXUP_ACER_ASPIRE_4930G),
2028         SND_PCI_QUIRK(0x1025, 0x013f, "Acer Aspire 5930G",
2029                       ALC882_FIXUP_ACER_ASPIRE_4930G),
2030         SND_PCI_QUIRK(0x1025, 0x0145, "Acer Aspire 8930G",
2031                       ALC882_FIXUP_ACER_ASPIRE_8930G),
2032         SND_PCI_QUIRK(0x1025, 0x0146, "Acer Aspire 6935G",
2033                       ALC882_FIXUP_ACER_ASPIRE_8930G),
2034         SND_PCI_QUIRK(0x1025, 0x015e, "Acer Aspire 6930G",
2035                       ALC882_FIXUP_ACER_ASPIRE_4930G),
2036         SND_PCI_QUIRK(0x1025, 0x0166, "Acer Aspire 6530G",
2037                       ALC882_FIXUP_ACER_ASPIRE_4930G),
2038         SND_PCI_QUIRK(0x1025, 0x0142, "Acer Aspire 7730G",
2039                       ALC882_FIXUP_ACER_ASPIRE_4930G),
2040         SND_PCI_QUIRK(0x1025, 0x0155, "Packard-Bell M5120", ALC882_FIXUP_PB_M5210),
2041         SND_PCI_QUIRK(0x1025, 0x021e, "Acer Aspire 5739G",
2042                       ALC882_FIXUP_ACER_ASPIRE_4930G),
2043         SND_PCI_QUIRK(0x1025, 0x0259, "Acer Aspire 5935", ALC889_FIXUP_DAC_ROUTE),
2044         SND_PCI_QUIRK(0x1025, 0x026b, "Acer Aspire 8940G", ALC882_FIXUP_ACER_ASPIRE_8930G),
2045         SND_PCI_QUIRK(0x1025, 0x0296, "Acer Aspire 7736z", ALC882_FIXUP_ACER_ASPIRE_7736),
2046         SND_PCI_QUIRK(0x1043, 0x13c2, "Asus A7M", ALC882_FIXUP_EAPD),
2047         SND_PCI_QUIRK(0x1043, 0x1873, "ASUS W90V", ALC882_FIXUP_ASUS_W90V),
2048         SND_PCI_QUIRK(0x1043, 0x1971, "Asus W2JC", ALC882_FIXUP_ASUS_W2JC),
2049         SND_PCI_QUIRK(0x1043, 0x835f, "Asus Eee 1601", ALC888_FIXUP_EEE1601),
2050         SND_PCI_QUIRK(0x104d, 0x9047, "Sony Vaio TT", ALC889_FIXUP_VAIO_TT),
2051         SND_PCI_QUIRK(0x104d, 0x905a, "Sony Vaio Z", ALC882_FIXUP_NO_PRIMARY_HP),
2052         SND_PCI_QUIRK(0x104d, 0x9043, "Sony Vaio VGC-LN51JGB", ALC882_FIXUP_NO_PRIMARY_HP),
2053
2054         /* All Apple entries are in codec SSIDs */
2055         SND_PCI_QUIRK(0x106b, 0x00a0, "MacBookPro 3,1", ALC889_FIXUP_MBP_VREF),
2056         SND_PCI_QUIRK(0x106b, 0x00a1, "Macbook", ALC889_FIXUP_MBP_VREF),
2057         SND_PCI_QUIRK(0x106b, 0x00a4, "MacbookPro 4,1", ALC889_FIXUP_MBP_VREF),
2058         SND_PCI_QUIRK(0x106b, 0x0c00, "Mac Pro", ALC885_FIXUP_MACPRO_GPIO),
2059         SND_PCI_QUIRK(0x106b, 0x1000, "iMac 24", ALC885_FIXUP_MACPRO_GPIO),
2060         SND_PCI_QUIRK(0x106b, 0x2800, "AppleTV", ALC885_FIXUP_MACPRO_GPIO),
2061         SND_PCI_QUIRK(0x106b, 0x2c00, "MacbookPro rev3", ALC889_FIXUP_MBP_VREF),
2062         SND_PCI_QUIRK(0x106b, 0x3000, "iMac", ALC889_FIXUP_MBP_VREF),
2063         SND_PCI_QUIRK(0x106b, 0x3200, "iMac 7,1 Aluminum", ALC882_FIXUP_EAPD),
2064         SND_PCI_QUIRK(0x106b, 0x3400, "MacBookAir 1,1", ALC889_FIXUP_MBP_VREF),
2065         SND_PCI_QUIRK(0x106b, 0x3500, "MacBookAir 2,1", ALC889_FIXUP_MBP_VREF),
2066         SND_PCI_QUIRK(0x106b, 0x3600, "Macbook 3,1", ALC889_FIXUP_MBP_VREF),
2067         SND_PCI_QUIRK(0x106b, 0x3800, "MacbookPro 4,1", ALC889_FIXUP_MBP_VREF),
2068         SND_PCI_QUIRK(0x106b, 0x3e00, "iMac 24 Aluminum", ALC885_FIXUP_MACPRO_GPIO),
2069         SND_PCI_QUIRK(0x106b, 0x3f00, "Macbook 5,1", ALC889_FIXUP_IMAC91_VREF),
2070         SND_PCI_QUIRK(0x106b, 0x4000, "MacbookPro 5,1", ALC889_FIXUP_IMAC91_VREF),
2071         SND_PCI_QUIRK(0x106b, 0x4100, "Macmini 3,1", ALC889_FIXUP_IMAC91_VREF),
2072         SND_PCI_QUIRK(0x106b, 0x4200, "Mac Pro 5,1", ALC885_FIXUP_MACPRO_GPIO),
2073         SND_PCI_QUIRK(0x106b, 0x4300, "iMac 9,1", ALC889_FIXUP_IMAC91_VREF),
2074         SND_PCI_QUIRK(0x106b, 0x4600, "MacbookPro 5,2", ALC889_FIXUP_IMAC91_VREF),
2075         SND_PCI_QUIRK(0x106b, 0x4900, "iMac 9,1 Aluminum", ALC889_FIXUP_IMAC91_VREF),
2076         SND_PCI_QUIRK(0x106b, 0x4a00, "Macbook 5,2", ALC889_FIXUP_IMAC91_VREF),
2077
2078         SND_PCI_QUIRK(0x1071, 0x8258, "Evesham Voyaeger", ALC882_FIXUP_EAPD),
2079         SND_PCI_QUIRK(0x1462, 0x7350, "MSI-7350", ALC889_FIXUP_CD),
2080         SND_PCI_QUIRK_VENDOR(0x1462, "MSI", ALC882_FIXUP_GPIO3),
2081         SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte EP45-DS3", ALC889_FIXUP_CD),
2082         SND_PCI_QUIRK(0x147b, 0x107a, "Abit AW9D-MAX", ALC882_FIXUP_ABIT_AW9D_MAX),
2083         SND_PCI_QUIRK_VENDOR(0x1558, "Clevo laptop", ALC882_FIXUP_EAPD),
2084         SND_PCI_QUIRK(0x161f, 0x2054, "Medion laptop", ALC883_FIXUP_EAPD),
2085         SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo Y530", ALC882_FIXUP_LENOVO_Y530),
2086         SND_PCI_QUIRK(0x8086, 0x0022, "DX58SO", ALC889_FIXUP_COEF),
2087         {}
2088 };
2089
2090 static const struct hda_model_fixup alc882_fixup_models[] = {
2091         {.id = ALC882_FIXUP_ACER_ASPIRE_4930G, .name = "acer-aspire-4930g"},
2092         {.id = ALC882_FIXUP_ACER_ASPIRE_8930G, .name = "acer-aspire-8930g"},
2093         {.id = ALC883_FIXUP_ACER_EAPD, .name = "acer-aspire"},
2094         {.id = ALC882_FIXUP_INV_DMIC, .name = "inv-dmic"},
2095         {.id = ALC882_FIXUP_NO_PRIMARY_HP, .name = "no-primary-hp"},
2096         {}
2097 };
2098
2099 /*
2100  * BIOS auto configuration
2101  */
2102 /* almost identical with ALC880 parser... */
2103 static int alc882_parse_auto_config(struct hda_codec *codec)
2104 {
2105         static const hda_nid_t alc882_ignore[] = { 0x1d, 0 };
2106         static const hda_nid_t alc882_ssids[] = { 0x15, 0x1b, 0x14, 0 };
2107         return alc_parse_auto_config(codec, alc882_ignore, alc882_ssids);
2108 }
2109
2110 /*
2111  */
2112 static int patch_alc882(struct hda_codec *codec)
2113 {
2114         struct alc_spec *spec;
2115         int err;
2116
2117         err = alc_alloc_spec(codec, 0x0b);
2118         if (err < 0)
2119                 return err;
2120
2121         spec = codec->spec;
2122
2123         switch (codec->vendor_id) {
2124         case 0x10ec0882:
2125         case 0x10ec0885:
2126                 break;
2127         default:
2128                 /* ALC883 and variants */
2129                 alc_fix_pll_init(codec, 0x20, 0x0a, 10);
2130                 break;
2131         }
2132
2133         snd_hda_pick_fixup(codec, alc882_fixup_models, alc882_fixup_tbl,
2134                        alc882_fixups);
2135         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
2136
2137         alc_auto_parse_customize_define(codec);
2138
2139         if (has_cdefine_beep(codec))
2140                 spec->gen.beep_nid = 0x01;
2141
2142         /* automatic parse from the BIOS config */
2143         err = alc882_parse_auto_config(codec);
2144         if (err < 0)
2145                 goto error;
2146
2147         if (!spec->gen.no_analog && spec->gen.beep_nid)
2148                 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
2149
2150         codec->patch_ops = alc_patch_ops;
2151
2152         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
2153
2154         return 0;
2155
2156  error:
2157         alc_free(codec);
2158         return err;
2159 }
2160
2161
2162 /*
2163  * ALC262 support
2164  */
2165 static int alc262_parse_auto_config(struct hda_codec *codec)
2166 {
2167         static const hda_nid_t alc262_ignore[] = { 0x1d, 0 };
2168         static const hda_nid_t alc262_ssids[] = { 0x15, 0x1b, 0x14, 0 };
2169         return alc_parse_auto_config(codec, alc262_ignore, alc262_ssids);
2170 }
2171
2172 /*
2173  * Pin config fixes
2174  */
2175 enum {
2176         ALC262_FIXUP_FSC_H270,
2177         ALC262_FIXUP_FSC_S7110,
2178         ALC262_FIXUP_HP_Z200,
2179         ALC262_FIXUP_TYAN,
2180         ALC262_FIXUP_LENOVO_3000,
2181         ALC262_FIXUP_BENQ,
2182         ALC262_FIXUP_BENQ_T31,
2183         ALC262_FIXUP_INV_DMIC,
2184 };
2185
2186 static const struct hda_fixup alc262_fixups[] = {
2187         [ALC262_FIXUP_FSC_H270] = {
2188                 .type = HDA_FIXUP_PINS,
2189                 .v.pins = (const struct hda_pintbl[]) {
2190                         { 0x14, 0x99130110 }, /* speaker */
2191                         { 0x15, 0x0221142f }, /* front HP */
2192                         { 0x1b, 0x0121141f }, /* rear HP */
2193                         { }
2194                 }
2195         },
2196         [ALC262_FIXUP_FSC_S7110] = {
2197                 .type = HDA_FIXUP_PINS,
2198                 .v.pins = (const struct hda_pintbl[]) {
2199                         { 0x15, 0x90170110 }, /* speaker */
2200                         { }
2201                 },
2202                 .chained = true,
2203                 .chain_id = ALC262_FIXUP_BENQ,
2204         },
2205         [ALC262_FIXUP_HP_Z200] = {
2206                 .type = HDA_FIXUP_PINS,
2207                 .v.pins = (const struct hda_pintbl[]) {
2208                         { 0x16, 0x99130120 }, /* internal speaker */
2209                         { }
2210                 }
2211         },
2212         [ALC262_FIXUP_TYAN] = {
2213                 .type = HDA_FIXUP_PINS,
2214                 .v.pins = (const struct hda_pintbl[]) {
2215                         { 0x14, 0x1993e1f0 }, /* int AUX */
2216                         { }
2217                 }
2218         },
2219         [ALC262_FIXUP_LENOVO_3000] = {
2220                 .type = HDA_FIXUP_PINCTLS,
2221                 .v.pins = (const struct hda_pintbl[]) {
2222                         { 0x19, PIN_VREF50 },
2223                         {}
2224                 },
2225                 .chained = true,
2226                 .chain_id = ALC262_FIXUP_BENQ,
2227         },
2228         [ALC262_FIXUP_BENQ] = {
2229                 .type = HDA_FIXUP_VERBS,
2230                 .v.verbs = (const struct hda_verb[]) {
2231                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
2232                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3070 },
2233                         {}
2234                 }
2235         },
2236         [ALC262_FIXUP_BENQ_T31] = {
2237                 .type = HDA_FIXUP_VERBS,
2238                 .v.verbs = (const struct hda_verb[]) {
2239                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
2240                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
2241                         {}
2242                 }
2243         },
2244         [ALC262_FIXUP_INV_DMIC] = {
2245                 .type = HDA_FIXUP_FUNC,
2246                 .v.func = alc_fixup_inv_dmic_0x12,
2247         },
2248 };
2249
2250 static const struct snd_pci_quirk alc262_fixup_tbl[] = {
2251         SND_PCI_QUIRK(0x103c, 0x170b, "HP Z200", ALC262_FIXUP_HP_Z200),
2252         SND_PCI_QUIRK(0x10cf, 0x1397, "Fujitsu Lifebook S7110", ALC262_FIXUP_FSC_S7110),
2253         SND_PCI_QUIRK(0x10cf, 0x142d, "Fujitsu Lifebook E8410", ALC262_FIXUP_BENQ),
2254         SND_PCI_QUIRK(0x10f1, 0x2915, "Tyan Thunder n6650W", ALC262_FIXUP_TYAN),
2255         SND_PCI_QUIRK(0x1734, 0x1147, "FSC Celsius H270", ALC262_FIXUP_FSC_H270),
2256         SND_PCI_QUIRK(0x17aa, 0x384e, "Lenovo 3000", ALC262_FIXUP_LENOVO_3000),
2257         SND_PCI_QUIRK(0x17ff, 0x0560, "Benq ED8", ALC262_FIXUP_BENQ),
2258         SND_PCI_QUIRK(0x17ff, 0x058d, "Benq T31-16", ALC262_FIXUP_BENQ_T31),
2259         {}
2260 };
2261
2262 static const struct hda_model_fixup alc262_fixup_models[] = {
2263         {.id = ALC262_FIXUP_INV_DMIC, .name = "inv-dmic"},
2264         {}
2265 };
2266
2267 /*
2268  */
2269 static int patch_alc262(struct hda_codec *codec)
2270 {
2271         struct alc_spec *spec;
2272         int err;
2273
2274         err = alc_alloc_spec(codec, 0x0b);
2275         if (err < 0)
2276                 return err;
2277
2278         spec = codec->spec;
2279         spec->gen.shared_mic_vref_pin = 0x18;
2280
2281 #if 0
2282         /* pshou 07/11/05  set a zero PCM sample to DAC when FIFO is
2283          * under-run
2284          */
2285         {
2286         int tmp;
2287         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
2288         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
2289         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
2290         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PROC_COEF, tmp | 0x80);
2291         }
2292 #endif
2293         alc_fix_pll_init(codec, 0x20, 0x0a, 10);
2294
2295         snd_hda_pick_fixup(codec, alc262_fixup_models, alc262_fixup_tbl,
2296                        alc262_fixups);
2297         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
2298
2299         alc_auto_parse_customize_define(codec);
2300
2301         if (has_cdefine_beep(codec))
2302                 spec->gen.beep_nid = 0x01;
2303
2304         /* automatic parse from the BIOS config */
2305         err = alc262_parse_auto_config(codec);
2306         if (err < 0)
2307                 goto error;
2308
2309         if (!spec->gen.no_analog && spec->gen.beep_nid)
2310                 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
2311
2312         codec->patch_ops = alc_patch_ops;
2313         spec->shutup = alc_eapd_shutup;
2314
2315         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
2316
2317         return 0;
2318
2319  error:
2320         alc_free(codec);
2321         return err;
2322 }
2323
2324 /*
2325  *  ALC268
2326  */
2327 /* bind Beep switches of both NID 0x0f and 0x10 */
2328 static const struct hda_bind_ctls alc268_bind_beep_sw = {
2329         .ops = &snd_hda_bind_sw,
2330         .values = {
2331                 HDA_COMPOSE_AMP_VAL(0x0f, 3, 1, HDA_INPUT),
2332                 HDA_COMPOSE_AMP_VAL(0x10, 3, 1, HDA_INPUT),
2333                 0
2334         },
2335 };
2336
2337 static const struct snd_kcontrol_new alc268_beep_mixer[] = {
2338         HDA_CODEC_VOLUME("Beep Playback Volume", 0x1d, 0x0, HDA_INPUT),
2339         HDA_BIND_SW("Beep Playback Switch", &alc268_bind_beep_sw),
2340         { }
2341 };
2342
2343 /* set PCBEEP vol = 0, mute connections */
2344 static const struct hda_verb alc268_beep_init_verbs[] = {
2345         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2346         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2347         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2348         { }
2349 };
2350
2351 enum {
2352         ALC268_FIXUP_INV_DMIC,
2353         ALC268_FIXUP_HP_EAPD,
2354 };
2355
2356 static const struct hda_fixup alc268_fixups[] = {
2357         [ALC268_FIXUP_INV_DMIC] = {
2358                 .type = HDA_FIXUP_FUNC,
2359                 .v.func = alc_fixup_inv_dmic_0x12,
2360         },
2361         [ALC268_FIXUP_HP_EAPD] = {
2362                 .type = HDA_FIXUP_VERBS,
2363                 .v.verbs = (const struct hda_verb[]) {
2364                         {0x15, AC_VERB_SET_EAPD_BTLENABLE, 0},
2365                         {}
2366                 }
2367         },
2368 };
2369
2370 static const struct hda_model_fixup alc268_fixup_models[] = {
2371         {.id = ALC268_FIXUP_INV_DMIC, .name = "inv-dmic"},
2372         {.id = ALC268_FIXUP_HP_EAPD, .name = "hp-eapd"},
2373         {}
2374 };
2375
2376 static const struct snd_pci_quirk alc268_fixup_tbl[] = {
2377         SND_PCI_QUIRK(0x1025, 0x015b, "Acer AOA 150 (ZG5)", ALC268_FIXUP_INV_DMIC),
2378         /* below is codec SSID since multiple Toshiba laptops have the
2379          * same PCI SSID 1179:ff00
2380          */
2381         SND_PCI_QUIRK(0x1179, 0xff06, "Toshiba P200", ALC268_FIXUP_HP_EAPD),
2382         {}
2383 };
2384
2385 /*
2386  * BIOS auto configuration
2387  */
2388 static int alc268_parse_auto_config(struct hda_codec *codec)
2389 {
2390         static const hda_nid_t alc268_ssids[] = { 0x15, 0x1b, 0x14, 0 };
2391         return alc_parse_auto_config(codec, NULL, alc268_ssids);
2392 }
2393
2394 /*
2395  */
2396 static int patch_alc268(struct hda_codec *codec)
2397 {
2398         struct alc_spec *spec;
2399         int err;
2400
2401         /* ALC268 has no aa-loopback mixer */
2402         err = alc_alloc_spec(codec, 0);
2403         if (err < 0)
2404                 return err;
2405
2406         spec = codec->spec;
2407         spec->gen.beep_nid = 0x01;
2408
2409         snd_hda_pick_fixup(codec, alc268_fixup_models, alc268_fixup_tbl, alc268_fixups);
2410         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
2411
2412         /* automatic parse from the BIOS config */
2413         err = alc268_parse_auto_config(codec);
2414         if (err < 0)
2415                 goto error;
2416
2417         if (err > 0 && !spec->gen.no_analog &&
2418             spec->gen.autocfg.speaker_pins[0] != 0x1d) {
2419                 add_mixer(spec, alc268_beep_mixer);
2420                 snd_hda_add_verbs(codec, alc268_beep_init_verbs);
2421                 if (!query_amp_caps(codec, 0x1d, HDA_INPUT))
2422                         /* override the amp caps for beep generator */
2423                         snd_hda_override_amp_caps(codec, 0x1d, HDA_INPUT,
2424                                           (0x0c << AC_AMPCAP_OFFSET_SHIFT) |
2425                                           (0x0c << AC_AMPCAP_NUM_STEPS_SHIFT) |
2426                                           (0x07 << AC_AMPCAP_STEP_SIZE_SHIFT) |
2427                                           (0 << AC_AMPCAP_MUTE_SHIFT));
2428         }
2429
2430         codec->patch_ops = alc_patch_ops;
2431         spec->shutup = alc_eapd_shutup;
2432
2433         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
2434
2435         return 0;
2436
2437  error:
2438         alc_free(codec);
2439         return err;
2440 }
2441
2442 /*
2443  * ALC269
2444  */
2445
2446 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
2447                              struct hda_codec *codec,
2448                              struct snd_pcm_substream *substream)
2449 {
2450         struct hda_gen_spec *spec = codec->spec;
2451         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2452                                              hinfo);
2453 }
2454
2455 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2456                                 struct hda_codec *codec,
2457                                 unsigned int stream_tag,
2458                                 unsigned int format,
2459                                 struct snd_pcm_substream *substream)
2460 {
2461         struct hda_gen_spec *spec = codec->spec;
2462         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2463                                                 stream_tag, format, substream);
2464 }
2465
2466 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2467                                 struct hda_codec *codec,
2468                                 struct snd_pcm_substream *substream)
2469 {
2470         struct hda_gen_spec *spec = codec->spec;
2471         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
2472 }
2473
2474 static const struct hda_pcm_stream alc269_44k_pcm_analog_playback = {
2475         .substreams = 1,
2476         .channels_min = 2,
2477         .channels_max = 8,
2478         .rates = SNDRV_PCM_RATE_44100, /* fixed rate */
2479         /* NID is set in alc_build_pcms */
2480         .ops = {
2481                 .open = playback_pcm_open,
2482                 .prepare = playback_pcm_prepare,
2483                 .cleanup = playback_pcm_cleanup
2484         },
2485 };
2486
2487 static const struct hda_pcm_stream alc269_44k_pcm_analog_capture = {
2488         .substreams = 1,
2489         .channels_min = 2,
2490         .channels_max = 2,
2491         .rates = SNDRV_PCM_RATE_44100, /* fixed rate */
2492         /* NID is set in alc_build_pcms */
2493 };
2494
2495 /* different alc269-variants */
2496 enum {
2497         ALC269_TYPE_ALC269VA,
2498         ALC269_TYPE_ALC269VB,
2499         ALC269_TYPE_ALC269VC,
2500         ALC269_TYPE_ALC269VD,
2501         ALC269_TYPE_ALC280,
2502         ALC269_TYPE_ALC282,
2503         ALC269_TYPE_ALC284,
2504 };
2505
2506 /*
2507  * BIOS auto configuration
2508  */
2509 static int alc269_parse_auto_config(struct hda_codec *codec)
2510 {
2511         static const hda_nid_t alc269_ignore[] = { 0x1d, 0 };
2512         static const hda_nid_t alc269_ssids[] = { 0, 0x1b, 0x14, 0x21 };
2513         static const hda_nid_t alc269va_ssids[] = { 0x15, 0x1b, 0x14, 0 };
2514         struct alc_spec *spec = codec->spec;
2515         const hda_nid_t *ssids;
2516
2517         switch (spec->codec_variant) {
2518         case ALC269_TYPE_ALC269VA:
2519         case ALC269_TYPE_ALC269VC:
2520         case ALC269_TYPE_ALC280:
2521         case ALC269_TYPE_ALC284:
2522                 ssids = alc269va_ssids;
2523                 break;
2524         case ALC269_TYPE_ALC269VB:
2525         case ALC269_TYPE_ALC269VD:
2526         case ALC269_TYPE_ALC282:
2527                 ssids = alc269_ssids;
2528                 break;
2529         default:
2530                 ssids = alc269_ssids;
2531                 break;
2532         }
2533
2534         return alc_parse_auto_config(codec, alc269_ignore, ssids);
2535 }
2536
2537 static void alc269vb_toggle_power_output(struct hda_codec *codec, int power_up)
2538 {
2539         int val = alc_read_coef_idx(codec, 0x04);
2540         if (power_up)
2541                 val |= 1 << 11;
2542         else
2543                 val &= ~(1 << 11);
2544         alc_write_coef_idx(codec, 0x04, val);
2545 }
2546
2547 static void alc269_shutup(struct hda_codec *codec)
2548 {
2549         struct alc_spec *spec = codec->spec;
2550
2551         if (spec->codec_variant != ALC269_TYPE_ALC269VB)
2552                 return;
2553
2554         if (spec->codec_variant == ALC269_TYPE_ALC269VB)
2555                 alc269vb_toggle_power_output(codec, 0);
2556         if (spec->codec_variant == ALC269_TYPE_ALC269VB &&
2557                         (alc_get_coef0(codec) & 0x00ff) == 0x018) {
2558                 msleep(150);
2559         }
2560 }
2561
2562 #ifdef CONFIG_PM
2563 static int alc269_resume(struct hda_codec *codec)
2564 {
2565         struct alc_spec *spec = codec->spec;
2566
2567         if (spec->codec_variant == ALC269_TYPE_ALC269VB)
2568                 alc269vb_toggle_power_output(codec, 0);
2569         if (spec->codec_variant == ALC269_TYPE_ALC269VB &&
2570                         (alc_get_coef0(codec) & 0x00ff) == 0x018) {
2571                 msleep(150);
2572         }
2573
2574         codec->patch_ops.init(codec);
2575
2576         if (spec->codec_variant == ALC269_TYPE_ALC269VB)
2577                 alc269vb_toggle_power_output(codec, 1);
2578         if (spec->codec_variant == ALC269_TYPE_ALC269VB &&
2579                         (alc_get_coef0(codec) & 0x00ff) == 0x017) {
2580                 msleep(200);
2581         }
2582
2583         snd_hda_codec_resume_amp(codec);
2584         snd_hda_codec_resume_cache(codec);
2585         hda_call_check_power_status(codec, 0x01);
2586         return 0;
2587 }
2588 #endif /* CONFIG_PM */
2589
2590 static void alc269_fixup_pincfg_no_hp_to_lineout(struct hda_codec *codec,
2591                                                  const struct hda_fixup *fix, int action)
2592 {
2593         struct alc_spec *spec = codec->spec;
2594
2595         if (action == HDA_FIXUP_ACT_PRE_PROBE)
2596                 spec->parse_flags = HDA_PINCFG_NO_HP_FIXUP;
2597 }
2598
2599 static void alc269_fixup_hweq(struct hda_codec *codec,
2600                                const struct hda_fixup *fix, int action)
2601 {
2602         int coef;
2603
2604         if (action != HDA_FIXUP_ACT_INIT)
2605                 return;
2606         coef = alc_read_coef_idx(codec, 0x1e);
2607         alc_write_coef_idx(codec, 0x1e, coef | 0x80);
2608 }
2609
2610 static void alc271_fixup_dmic(struct hda_codec *codec,
2611                               const struct hda_fixup *fix, int action)
2612 {
2613         static const struct hda_verb verbs[] = {
2614                 {0x20, AC_VERB_SET_COEF_INDEX, 0x0d},
2615                 {0x20, AC_VERB_SET_PROC_COEF, 0x4000},
2616                 {}
2617         };
2618         unsigned int cfg;
2619
2620         if (strcmp(codec->chip_name, "ALC271X"))
2621                 return;
2622         cfg = snd_hda_codec_get_pincfg(codec, 0x12);
2623         if (get_defcfg_connect(cfg) == AC_JACK_PORT_FIXED)
2624                 snd_hda_sequence_write(codec, verbs);
2625 }
2626
2627 static void alc269_fixup_pcm_44k(struct hda_codec *codec,
2628                                  const struct hda_fixup *fix, int action)
2629 {
2630         struct alc_spec *spec = codec->spec;
2631
2632         if (action != HDA_FIXUP_ACT_PROBE)
2633                 return;
2634
2635         /* Due to a hardware problem on Lenovo Ideadpad, we need to
2636          * fix the sample rate of analog I/O to 44.1kHz
2637          */
2638         spec->gen.stream_analog_playback = &alc269_44k_pcm_analog_playback;
2639         spec->gen.stream_analog_capture = &alc269_44k_pcm_analog_capture;
2640 }
2641
2642 static void alc269_fixup_stereo_dmic(struct hda_codec *codec,
2643                                      const struct hda_fixup *fix, int action)
2644 {
2645         int coef;
2646
2647         if (action != HDA_FIXUP_ACT_INIT)
2648                 return;
2649         /* The digital-mic unit sends PDM (differential signal) instead of
2650          * the standard PCM, thus you can't record a valid mono stream as is.
2651          * Below is a workaround specific to ALC269 to control the dmic
2652          * signal source as mono.
2653          */
2654         coef = alc_read_coef_idx(codec, 0x07);
2655         alc_write_coef_idx(codec, 0x07, coef | 0x80);
2656 }
2657
2658 static void alc269_quanta_automute(struct hda_codec *codec)
2659 {
2660         snd_hda_gen_update_outputs(codec);
2661
2662         snd_hda_codec_write(codec, 0x20, 0,
2663                         AC_VERB_SET_COEF_INDEX, 0x0c);
2664         snd_hda_codec_write(codec, 0x20, 0,
2665                         AC_VERB_SET_PROC_COEF, 0x680);
2666
2667         snd_hda_codec_write(codec, 0x20, 0,
2668                         AC_VERB_SET_COEF_INDEX, 0x0c);
2669         snd_hda_codec_write(codec, 0x20, 0,
2670                         AC_VERB_SET_PROC_COEF, 0x480);
2671 }
2672
2673 static void alc269_fixup_quanta_mute(struct hda_codec *codec,
2674                                      const struct hda_fixup *fix, int action)
2675 {
2676         struct alc_spec *spec = codec->spec;
2677         if (action != HDA_FIXUP_ACT_PROBE)
2678                 return;
2679         spec->gen.automute_hook = alc269_quanta_automute;
2680 }
2681
2682 /* update mute-LED according to the speaker mute state via mic VREF pin */
2683 static void alc269_fixup_mic_mute_hook(void *private_data, int enabled)
2684 {
2685         struct hda_codec *codec = private_data;
2686         struct alc_spec *spec = codec->spec;
2687         unsigned int pinval;
2688
2689         if (spec->mute_led_polarity)
2690                 enabled = !enabled;
2691         pinval = AC_PINCTL_IN_EN |
2692                 (enabled ? AC_PINCTL_VREF_HIZ : AC_PINCTL_VREF_80);
2693         if (spec->mute_led_nid)
2694                 snd_hda_set_pin_ctl_cache(codec, spec->mute_led_nid, pinval);
2695 }
2696
2697 static void alc269_fixup_hp_mute_led(struct hda_codec *codec,
2698                                      const struct hda_fixup *fix, int action)
2699 {
2700         struct alc_spec *spec = codec->spec;
2701         const struct dmi_device *dev = NULL;
2702
2703         if (action != HDA_FIXUP_ACT_PRE_PROBE)
2704                 return;
2705
2706         while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2707                 int pol, pin;
2708                 if (sscanf(dev->name, "HP_Mute_LED_%d_%x", &pol, &pin) != 2)
2709                         continue;
2710                 if (pin < 0x0a || pin >= 0x10)
2711                         break;
2712                 spec->mute_led_polarity = pol;
2713                 spec->mute_led_nid = pin - 0x0a + 0x18;
2714                 spec->gen.vmaster_mute.hook = alc269_fixup_mic_mute_hook;
2715                 spec->gen.vmaster_mute_enum = 1;
2716                 snd_printd("Detected mute LED for %x:%d\n", spec->mute_led_nid,
2717                            spec->mute_led_polarity);
2718                 break;
2719         }
2720 }
2721
2722 static void alc269_fixup_hp_mute_led_mic1(struct hda_codec *codec,
2723                                 const struct hda_fixup *fix, int action)
2724 {
2725         struct alc_spec *spec = codec->spec;
2726         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
2727                 spec->mute_led_polarity = 0;
2728                 spec->mute_led_nid = 0x18;
2729                 spec->gen.vmaster_mute.hook = alc269_fixup_mic_mute_hook;
2730                 spec->gen.vmaster_mute_enum = 1;
2731         }
2732 }
2733
2734 static void alc269_fixup_hp_mute_led_mic2(struct hda_codec *codec,
2735                                 const struct hda_fixup *fix, int action)
2736 {
2737         struct alc_spec *spec = codec->spec;
2738         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
2739                 spec->mute_led_polarity = 0;
2740                 spec->mute_led_nid = 0x19;
2741                 spec->gen.vmaster_mute.hook = alc269_fixup_mic_mute_hook;
2742                 spec->gen.vmaster_mute_enum = 1;
2743         }
2744 }
2745
2746 /* turn on/off mute LED per vmaster hook */
2747 static void alc269_fixup_hp_gpio_mute_hook(void *private_data, int enabled)
2748 {
2749         struct hda_codec *codec = private_data;
2750         struct alc_spec *spec = codec->spec;
2751         unsigned int oldval = spec->gpio_led;
2752
2753         if (enabled)
2754                 spec->gpio_led &= ~0x08;
2755         else
2756                 spec->gpio_led |= 0x08;
2757         if (spec->gpio_led != oldval)
2758                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
2759                                     spec->gpio_led);
2760 }
2761
2762 /* turn on/off mic-mute LED per capture hook */
2763 static void alc269_fixup_hp_gpio_mic_mute_hook(struct hda_codec *codec,
2764                                struct snd_ctl_elem_value *ucontrol)
2765 {
2766         struct alc_spec *spec = codec->spec;
2767         unsigned int oldval = spec->gpio_led;
2768
2769         if (!ucontrol)
2770                 return;
2771
2772         if (ucontrol->value.integer.value[0] ||
2773             ucontrol->value.integer.value[1])
2774                 spec->gpio_led &= ~0x10;
2775         else
2776                 spec->gpio_led |= 0x10;
2777         if (spec->gpio_led != oldval)
2778                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
2779                                     spec->gpio_led);
2780 }
2781
2782 static void alc269_fixup_hp_gpio_led(struct hda_codec *codec,
2783                                 const struct hda_fixup *fix, int action)
2784 {
2785         struct alc_spec *spec = codec->spec;
2786         static const struct hda_verb gpio_init[] = {
2787                 { 0x01, AC_VERB_SET_GPIO_MASK, 0x18 },
2788                 { 0x01, AC_VERB_SET_GPIO_DIRECTION, 0x18 },
2789                 {}
2790         };
2791
2792         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
2793                 spec->gen.vmaster_mute.hook = alc269_fixup_hp_gpio_mute_hook;
2794                 spec->gen.cap_sync_hook = alc269_fixup_hp_gpio_mic_mute_hook;
2795                 spec->gpio_led = 0;
2796                 snd_hda_add_verbs(codec, gpio_init);
2797         }
2798 }
2799
2800 static void alc271_hp_gate_mic_jack(struct hda_codec *codec,
2801                                     const struct hda_fixup *fix,
2802                                     int action)
2803 {
2804         struct alc_spec *spec = codec->spec;
2805
2806         if (action == HDA_FIXUP_ACT_PROBE) {
2807                 if (snd_BUG_ON(!spec->gen.am_entry[1].pin ||
2808                                !spec->gen.autocfg.hp_pins[0]))
2809                         return;
2810                 snd_hda_jack_set_gating_jack(codec, spec->gen.am_entry[1].pin,
2811                                              spec->gen.autocfg.hp_pins[0]);
2812         }
2813 }
2814
2815 enum {
2816         ALC269_FIXUP_SONY_VAIO,
2817         ALC275_FIXUP_SONY_VAIO_GPIO2,
2818         ALC269_FIXUP_DELL_M101Z,
2819         ALC269_FIXUP_SKU_IGNORE,
2820         ALC269_FIXUP_ASUS_G73JW,
2821         ALC269_FIXUP_LENOVO_EAPD,
2822         ALC275_FIXUP_SONY_HWEQ,
2823         ALC271_FIXUP_DMIC,
2824         ALC269_FIXUP_PCM_44K,
2825         ALC269_FIXUP_STEREO_DMIC,
2826         ALC269_FIXUP_QUANTA_MUTE,
2827         ALC269_FIXUP_LIFEBOOK,
2828         ALC269_FIXUP_AMIC,
2829         ALC269_FIXUP_DMIC,
2830         ALC269VB_FIXUP_AMIC,
2831         ALC269VB_FIXUP_DMIC,
2832         ALC269_FIXUP_HP_MUTE_LED,
2833         ALC269_FIXUP_HP_MUTE_LED_MIC1,
2834         ALC269_FIXUP_HP_MUTE_LED_MIC2,
2835         ALC269_FIXUP_HP_GPIO_LED,
2836         ALC269_FIXUP_INV_DMIC,
2837         ALC269_FIXUP_LENOVO_DOCK,
2838         ALC269_FIXUP_PINCFG_NO_HP_TO_LINEOUT,
2839         ALC271_FIXUP_AMIC_MIC2,
2840         ALC271_FIXUP_HP_GATE_MIC_JACK,
2841 };
2842
2843 static const struct hda_fixup alc269_fixups[] = {
2844         [ALC269_FIXUP_SONY_VAIO] = {
2845                 .type = HDA_FIXUP_PINCTLS,
2846                 .v.pins = (const struct hda_pintbl[]) {
2847                         {0x19, PIN_VREFGRD},
2848                         {}
2849                 }
2850         },
2851         [ALC275_FIXUP_SONY_VAIO_GPIO2] = {
2852                 .type = HDA_FIXUP_VERBS,
2853                 .v.verbs = (const struct hda_verb[]) {
2854                         {0x01, AC_VERB_SET_GPIO_MASK, 0x04},
2855                         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x04},
2856                         {0x01, AC_VERB_SET_GPIO_DATA, 0x00},
2857                         { }
2858                 },
2859                 .chained = true,
2860                 .chain_id = ALC269_FIXUP_SONY_VAIO
2861         },
2862         [ALC269_FIXUP_DELL_M101Z] = {
2863                 .type = HDA_FIXUP_VERBS,
2864                 .v.verbs = (const struct hda_verb[]) {
2865                         /* Enables internal speaker */
2866                         {0x20, AC_VERB_SET_COEF_INDEX, 13},
2867                         {0x20, AC_VERB_SET_PROC_COEF, 0x4040},
2868                         {}
2869                 }
2870         },
2871         [ALC269_FIXUP_SKU_IGNORE] = {
2872                 .type = HDA_FIXUP_FUNC,
2873                 .v.func = alc_fixup_sku_ignore,
2874         },
2875         [ALC269_FIXUP_ASUS_G73JW] = {
2876                 .type = HDA_FIXUP_PINS,
2877                 .v.pins = (const struct hda_pintbl[]) {
2878                         { 0x17, 0x99130111 }, /* subwoofer */
2879                         { }
2880                 }
2881         },
2882         [ALC269_FIXUP_LENOVO_EAPD] = {
2883                 .type = HDA_FIXUP_VERBS,
2884                 .v.verbs = (const struct hda_verb[]) {
2885                         {0x14, AC_VERB_SET_EAPD_BTLENABLE, 0},
2886                         {}
2887                 }
2888         },
2889         [ALC275_FIXUP_SONY_HWEQ] = {
2890                 .type = HDA_FIXUP_FUNC,
2891                 .v.func = alc269_fixup_hweq,
2892                 .chained = true,
2893                 .chain_id = ALC275_FIXUP_SONY_VAIO_GPIO2
2894         },
2895         [ALC271_FIXUP_DMIC] = {
2896                 .type = HDA_FIXUP_FUNC,
2897                 .v.func = alc271_fixup_dmic,
2898         },
2899         [ALC269_FIXUP_PCM_44K] = {
2900                 .type = HDA_FIXUP_FUNC,
2901                 .v.func = alc269_fixup_pcm_44k,
2902                 .chained = true,
2903                 .chain_id = ALC269_FIXUP_QUANTA_MUTE
2904         },
2905         [ALC269_FIXUP_STEREO_DMIC] = {
2906                 .type = HDA_FIXUP_FUNC,
2907                 .v.func = alc269_fixup_stereo_dmic,
2908         },
2909         [ALC269_FIXUP_QUANTA_MUTE] = {
2910                 .type = HDA_FIXUP_FUNC,
2911                 .v.func = alc269_fixup_quanta_mute,
2912         },
2913         [ALC269_FIXUP_LIFEBOOK] = {
2914                 .type = HDA_FIXUP_PINS,
2915                 .v.pins = (const struct hda_pintbl[]) {
2916                         { 0x1a, 0x2101103f }, /* dock line-out */
2917                         { 0x1b, 0x23a11040 }, /* dock mic-in */
2918                         { }
2919                 },
2920                 .chained = true,
2921                 .chain_id = ALC269_FIXUP_QUANTA_MUTE
2922         },
2923         [ALC269_FIXUP_AMIC] = {
2924                 .type = HDA_FIXUP_PINS,
2925                 .v.pins = (const struct hda_pintbl[]) {
2926                         { 0x14, 0x99130110 }, /* speaker */
2927                         { 0x15, 0x0121401f }, /* HP out */
2928                         { 0x18, 0x01a19c20 }, /* mic */
2929                         { 0x19, 0x99a3092f }, /* int-mic */
2930                         { }
2931                 },
2932         },
2933         [ALC269_FIXUP_DMIC] = {
2934                 .type = HDA_FIXUP_PINS,
2935                 .v.pins = (const struct hda_pintbl[]) {
2936                         { 0x12, 0x99a3092f }, /* int-mic */
2937                         { 0x14, 0x99130110 }, /* speaker */
2938                         { 0x15, 0x0121401f }, /* HP out */
2939                         { 0x18, 0x01a19c20 }, /* mic */
2940                         { }
2941                 },
2942         },
2943         [ALC269VB_FIXUP_AMIC] = {
2944                 .type = HDA_FIXUP_PINS,
2945                 .v.pins = (const struct hda_pintbl[]) {
2946                         { 0x14, 0x99130110 }, /* speaker */
2947                         { 0x18, 0x01a19c20 }, /* mic */
2948                         { 0x19, 0x99a3092f }, /* int-mic */
2949                         { 0x21, 0x0121401f }, /* HP out */
2950                         { }
2951                 },
2952         },
2953         [ALC269VB_FIXUP_DMIC] = {
2954                 .type = HDA_FIXUP_PINS,
2955                 .v.pins = (const struct hda_pintbl[]) {
2956                         { 0x12, 0x99a3092f }, /* int-mic */
2957                         { 0x14, 0x99130110 }, /* speaker */
2958                         { 0x18, 0x01a19c20 }, /* mic */
2959                         { 0x21, 0x0121401f }, /* HP out */
2960                         { }
2961                 },
2962         },
2963         [ALC269_FIXUP_HP_MUTE_LED] = {
2964                 .type = HDA_FIXUP_FUNC,
2965                 .v.func = alc269_fixup_hp_mute_led,
2966         },
2967         [ALC269_FIXUP_HP_MUTE_LED_MIC1] = {
2968                 .type = HDA_FIXUP_FUNC,
2969                 .v.func = alc269_fixup_hp_mute_led_mic1,
2970         },
2971         [ALC269_FIXUP_HP_MUTE_LED_MIC2] = {
2972                 .type = HDA_FIXUP_FUNC,
2973                 .v.func = alc269_fixup_hp_mute_led_mic2,
2974         },
2975         [ALC269_FIXUP_HP_GPIO_LED] = {
2976                 .type = HDA_FIXUP_FUNC,
2977                 .v.func = alc269_fixup_hp_gpio_led,
2978         },
2979         [ALC269_FIXUP_INV_DMIC] = {
2980                 .type = HDA_FIXUP_FUNC,
2981                 .v.func = alc_fixup_inv_dmic_0x12,
2982         },
2983         [ALC269_FIXUP_LENOVO_DOCK] = {
2984                 .type = HDA_FIXUP_PINS,
2985                 .v.pins = (const struct hda_pintbl[]) {
2986                         { 0x19, 0x23a11040 }, /* dock mic */
2987                         { 0x1b, 0x2121103f }, /* dock headphone */
2988                         { }
2989                 },
2990                 .chained = true,
2991                 .chain_id = ALC269_FIXUP_PINCFG_NO_HP_TO_LINEOUT
2992         },
2993         [ALC269_FIXUP_PINCFG_NO_HP_TO_LINEOUT] = {
2994                 .type = HDA_FIXUP_FUNC,
2995                 .v.func = alc269_fixup_pincfg_no_hp_to_lineout,
2996         },
2997         [ALC271_FIXUP_AMIC_MIC2] = {
2998                 .type = HDA_FIXUP_PINS,
2999                 .v.pins = (const struct hda_pintbl[]) {
3000                         { 0x14, 0x99130110 }, /* speaker */
3001                         { 0x19, 0x01a19c20 }, /* mic */
3002                         { 0x1b, 0x99a7012f }, /* int-mic */
3003                         { 0x21, 0x0121401f }, /* HP out */
3004                         { }
3005                 },
3006         },
3007         [ALC271_FIXUP_HP_GATE_MIC_JACK] = {
3008                 .type = HDA_FIXUP_FUNC,
3009                 .v.func = alc271_hp_gate_mic_jack,
3010                 .chained = true,
3011                 .chain_id = ALC271_FIXUP_AMIC_MIC2,
3012         },
3013 };
3014
3015 static const struct snd_pci_quirk alc269_fixup_tbl[] = {
3016         SND_PCI_QUIRK(0x1025, 0x029b, "Acer 1810TZ", ALC269_FIXUP_INV_DMIC),
3017         SND_PCI_QUIRK(0x1025, 0x0349, "Acer AOD260", ALC269_FIXUP_INV_DMIC),
3018         SND_PCI_QUIRK(0x103c, 0x1586, "HP", ALC269_FIXUP_HP_MUTE_LED_MIC2),
3019         SND_PCI_QUIRK(0x103c, 0x18e6, "HP", ALC269_FIXUP_HP_GPIO_LED),
3020         SND_PCI_QUIRK(0x103c, 0x1973, "HP Pavilion", ALC269_FIXUP_HP_MUTE_LED_MIC1),
3021         SND_PCI_QUIRK(0x103c, 0x1983, "HP Pavilion", ALC269_FIXUP_HP_MUTE_LED_MIC1),
3022         SND_PCI_QUIRK_VENDOR(0x103c, "HP", ALC269_FIXUP_HP_MUTE_LED),
3023         SND_PCI_QUIRK(0x1043, 0x1427, "Asus Zenbook UX31E", ALC269VB_FIXUP_DMIC),
3024         SND_PCI_QUIRK(0x1043, 0x1517, "Asus Zenbook UX31A", ALC269VB_FIXUP_DMIC),
3025         SND_PCI_QUIRK(0x1043, 0x1a13, "Asus G73Jw", ALC269_FIXUP_ASUS_G73JW),
3026         SND_PCI_QUIRK(0x1043, 0x1b13, "Asus U41SV", ALC269_FIXUP_INV_DMIC),
3027         SND_PCI_QUIRK(0x1043, 0x16e3, "ASUS UX50", ALC269_FIXUP_STEREO_DMIC),
3028         SND_PCI_QUIRK(0x1043, 0x831a, "ASUS P901", ALC269_FIXUP_STEREO_DMIC),
3029         SND_PCI_QUIRK(0x1043, 0x834a, "ASUS S101", ALC269_FIXUP_STEREO_DMIC),
3030         SND_PCI_QUIRK(0x1043, 0x8398, "ASUS P1005", ALC269_FIXUP_STEREO_DMIC),
3031         SND_PCI_QUIRK(0x1043, 0x83ce, "ASUS P1005", ALC269_FIXUP_STEREO_DMIC),
3032         SND_PCI_QUIRK(0x104d, 0x9073, "Sony VAIO", ALC275_FIXUP_SONY_VAIO_GPIO2),
3033         SND_PCI_QUIRK(0x104d, 0x907b, "Sony VAIO", ALC275_FIXUP_SONY_HWEQ),
3034         SND_PCI_QUIRK(0x104d, 0x9084, "Sony VAIO", ALC275_FIXUP_SONY_HWEQ),
3035         SND_PCI_QUIRK_VENDOR(0x104d, "Sony VAIO", ALC269_FIXUP_SONY_VAIO),
3036         SND_PCI_QUIRK(0x1028, 0x0470, "Dell M101z", ALC269_FIXUP_DELL_M101Z),
3037         SND_PCI_QUIRK(0x1025, 0x0740, "Acer AO725", ALC271_FIXUP_HP_GATE_MIC_JACK),
3038         SND_PCI_QUIRK(0x1025, 0x0742, "Acer AO756", ALC271_FIXUP_HP_GATE_MIC_JACK),
3039         SND_PCI_QUIRK_VENDOR(0x1025, "Acer Aspire", ALC271_FIXUP_DMIC),
3040         SND_PCI_QUIRK(0x10cf, 0x1475, "Lifebook", ALC269_FIXUP_LIFEBOOK),
3041         SND_PCI_QUIRK(0x17aa, 0x20f2, "Thinkpad SL410/510", ALC269_FIXUP_SKU_IGNORE),
3042         SND_PCI_QUIRK(0x17aa, 0x215e, "Thinkpad L512", ALC269_FIXUP_SKU_IGNORE),
3043         SND_PCI_QUIRK(0x17aa, 0x21b8, "Thinkpad Edge 14", ALC269_FIXUP_SKU_IGNORE),
3044         SND_PCI_QUIRK(0x17aa, 0x21ca, "Thinkpad L412", ALC269_FIXUP_SKU_IGNORE),
3045         SND_PCI_QUIRK(0x17aa, 0x21e9, "Thinkpad Edge 15", ALC269_FIXUP_SKU_IGNORE),
3046         SND_PCI_QUIRK(0x17aa, 0x21f6, "Thinkpad T530", ALC269_FIXUP_LENOVO_DOCK),
3047         SND_PCI_QUIRK(0x17aa, 0x21fa, "Thinkpad X230", ALC269_FIXUP_LENOVO_DOCK),
3048         SND_PCI_QUIRK(0x17aa, 0x21f3, "Thinkpad T430", ALC269_FIXUP_LENOVO_DOCK),
3049         SND_PCI_QUIRK(0x17aa, 0x21fb, "Thinkpad T430s", ALC269_FIXUP_LENOVO_DOCK),
3050         SND_PCI_QUIRK(0x17aa, 0x2203, "Thinkpad X230 Tablet", ALC269_FIXUP_LENOVO_DOCK),
3051         SND_PCI_QUIRK(0x17aa, 0x3bf8, "Quanta FL1", ALC269_FIXUP_PCM_44K),
3052         SND_PCI_QUIRK(0x17aa, 0x9e54, "LENOVO NB", ALC269_FIXUP_LENOVO_EAPD),
3053
3054 #if 0
3055         /* Below is a quirk table taken from the old code.
3056          * Basically the device should work as is without the fixup table.
3057          * If BIOS doesn't give a proper info, enable the corresponding
3058          * fixup entry.
3059          */
3060         SND_PCI_QUIRK(0x1043, 0x8330, "ASUS Eeepc P703 P900A",
3061                       ALC269_FIXUP_AMIC),
3062         SND_PCI_QUIRK(0x1043, 0x1013, "ASUS N61Da", ALC269_FIXUP_AMIC),
3063         SND_PCI_QUIRK(0x1043, 0x1143, "ASUS B53f", ALC269_FIXUP_AMIC),
3064         SND_PCI_QUIRK(0x1043, 0x1133, "ASUS UJ20ft", ALC269_FIXUP_AMIC),
3065         SND_PCI_QUIRK(0x1043, 0x1183, "ASUS K72DR", ALC269_FIXUP_AMIC),
3066         SND_PCI_QUIRK(0x1043, 0x11b3, "ASUS K52DR", ALC269_FIXUP_AMIC),
3067         SND_PCI_QUIRK(0x1043, 0x11e3, "ASUS U33Jc", ALC269_FIXUP_AMIC),
3068         SND_PCI_QUIRK(0x1043, 0x1273, "ASUS UL80Jt", ALC269_FIXUP_AMIC),
3069         SND_PCI_QUIRK(0x1043, 0x1283, "ASUS U53Jc", ALC269_FIXUP_AMIC),
3070         SND_PCI_QUIRK(0x1043, 0x12b3, "ASUS N82JV", ALC269_FIXUP_AMIC),
3071         SND_PCI_QUIRK(0x1043, 0x12d3, "ASUS N61Jv", ALC269_FIXUP_AMIC),
3072         SND_PCI_QUIRK(0x1043, 0x13a3, "ASUS UL30Vt", ALC269_FIXUP_AMIC),
3073         SND_PCI_QUIRK(0x1043, 0x1373, "ASUS G73JX", ALC269_FIXUP_AMIC),
3074         SND_PCI_QUIRK(0x1043, 0x1383, "ASUS UJ30Jc", ALC269_FIXUP_AMIC),
3075         SND_PCI_QUIRK(0x1043, 0x13d3, "ASUS N61JA", ALC269_FIXUP_AMIC),
3076         SND_PCI_QUIRK(0x1043, 0x1413, "ASUS UL50", ALC269_FIXUP_AMIC),
3077         SND_PCI_QUIRK(0x1043, 0x1443, "ASUS UL30", ALC269_FIXUP_AMIC),
3078         SND_PCI_QUIRK(0x1043, 0x1453, "ASUS M60Jv", ALC269_FIXUP_AMIC),
3079         SND_PCI_QUIRK(0x1043, 0x1483, "ASUS UL80", ALC269_FIXUP_AMIC),
3080         SND_PCI_QUIRK(0x1043, 0x14f3, "ASUS F83Vf", ALC269_FIXUP_AMIC),
3081         SND_PCI_QUIRK(0x1043, 0x14e3, "ASUS UL20", ALC269_FIXUP_AMIC),
3082         SND_PCI_QUIRK(0x1043, 0x1513, "ASUS UX30", ALC269_FIXUP_AMIC),
3083         SND_PCI_QUIRK(0x1043, 0x1593, "ASUS N51Vn", ALC269_FIXUP_AMIC),
3084         SND_PCI_QUIRK(0x1043, 0x15a3, "ASUS N60Jv", ALC269_FIXUP_AMIC),
3085         SND_PCI_QUIRK(0x1043, 0x15b3, "ASUS N60Dp", ALC269_FIXUP_AMIC),
3086         SND_PCI_QUIRK(0x1043, 0x15c3, "ASUS N70De", ALC269_FIXUP_AMIC),
3087         SND_PCI_QUIRK(0x1043, 0x15e3, "ASUS F83T", ALC269_FIXUP_AMIC),
3088         SND_PCI_QUIRK(0x1043, 0x1643, "ASUS M60J", ALC269_FIXUP_AMIC),
3089         SND_PCI_QUIRK(0x1043, 0x1653, "ASUS U50", ALC269_FIXUP_AMIC),
3090         SND_PCI_QUIRK(0x1043, 0x1693, "ASUS F50N", ALC269_FIXUP_AMIC),
3091         SND_PCI_QUIRK(0x1043, 0x16a3, "ASUS F5Q", ALC269_FIXUP_AMIC),
3092         SND_PCI_QUIRK(0x1043, 0x1723, "ASUS P80", ALC269_FIXUP_AMIC),
3093         SND_PCI_QUIRK(0x1043, 0x1743, "ASUS U80", ALC269_FIXUP_AMIC),
3094         SND_PCI_QUIRK(0x1043, 0x1773, "ASUS U20A", ALC269_FIXUP_AMIC),
3095         SND_PCI_QUIRK(0x1043, 0x1883, "ASUS F81Se", ALC269_FIXUP_AMIC),
3096         SND_PCI_QUIRK(0x152d, 0x1778, "Quanta ON1", ALC269_FIXUP_DMIC),
3097         SND_PCI_QUIRK(0x17aa, 0x3be9, "Quanta Wistron", ALC269_FIXUP_AMIC),
3098         SND_PCI_QUIRK(0x17aa, 0x3bf8, "Quanta FL1", ALC269_FIXUP_AMIC),
3099         SND_PCI_QUIRK(0x17ff, 0x059a, "Quanta EL3", ALC269_FIXUP_DMIC),
3100         SND_PCI_QUIRK(0x17ff, 0x059b, "Quanta JR1", ALC269_FIXUP_DMIC),
3101 #endif
3102         {}
3103 };
3104
3105 static const struct hda_model_fixup alc269_fixup_models[] = {
3106         {.id = ALC269_FIXUP_AMIC, .name = "laptop-amic"},
3107         {.id = ALC269_FIXUP_DMIC, .name = "laptop-dmic"},
3108         {.id = ALC269_FIXUP_STEREO_DMIC, .name = "alc269-dmic"},
3109         {.id = ALC271_FIXUP_DMIC, .name = "alc271-dmic"},
3110         {.id = ALC269_FIXUP_INV_DMIC, .name = "inv-dmic"},
3111         {.id = ALC269_FIXUP_LENOVO_DOCK, .name = "lenovo-dock"},
3112         {.id = ALC269_FIXUP_HP_GPIO_LED, .name = "hp-gpio-led"},
3113         {}
3114 };
3115
3116
3117 static void alc269_fill_coef(struct hda_codec *codec)
3118 {
3119         struct alc_spec *spec = codec->spec;
3120         int val;
3121
3122         if (spec->codec_variant != ALC269_TYPE_ALC269VB)
3123                 return;
3124
3125         if ((alc_get_coef0(codec) & 0x00ff) < 0x015) {
3126                 alc_write_coef_idx(codec, 0xf, 0x960b);
3127                 alc_write_coef_idx(codec, 0xe, 0x8817);
3128         }
3129
3130         if ((alc_get_coef0(codec) & 0x00ff) == 0x016) {
3131                 alc_write_coef_idx(codec, 0xf, 0x960b);
3132                 alc_write_coef_idx(codec, 0xe, 0x8814);
3133         }
3134
3135         if ((alc_get_coef0(codec) & 0x00ff) == 0x017) {
3136                 val = alc_read_coef_idx(codec, 0x04);
3137                 /* Power up output pin */
3138                 alc_write_coef_idx(codec, 0x04, val | (1<<11));
3139         }
3140
3141         if ((alc_get_coef0(codec) & 0x00ff) == 0x018) {
3142                 val = alc_read_coef_idx(codec, 0xd);
3143                 if ((val & 0x0c00) >> 10 != 0x1) {
3144                         /* Capless ramp up clock control */
3145                         alc_write_coef_idx(codec, 0xd, val | (1<<10));
3146                 }
3147                 val = alc_read_coef_idx(codec, 0x17);
3148                 if ((val & 0x01c0) >> 6 != 0x4) {
3149                         /* Class D power on reset */
3150                         alc_write_coef_idx(codec, 0x17, val | (1<<7));
3151                 }
3152         }
3153
3154         val = alc_read_coef_idx(codec, 0xd); /* Class D */
3155         alc_write_coef_idx(codec, 0xd, val | (1<<14));
3156
3157         val = alc_read_coef_idx(codec, 0x4); /* HP */
3158         alc_write_coef_idx(codec, 0x4, val | (1<<11));
3159 }
3160
3161 /*
3162  */
3163 static int patch_alc269(struct hda_codec *codec)
3164 {
3165         struct alc_spec *spec;
3166         int err;
3167
3168         err = alc_alloc_spec(codec, 0x0b);
3169         if (err < 0)
3170                 return err;
3171
3172         spec = codec->spec;
3173         spec->gen.shared_mic_vref_pin = 0x18;
3174
3175         snd_hda_pick_fixup(codec, alc269_fixup_models,
3176                        alc269_fixup_tbl, alc269_fixups);
3177         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
3178
3179         alc_auto_parse_customize_define(codec);
3180
3181         if (has_cdefine_beep(codec))
3182                 spec->gen.beep_nid = 0x01;
3183
3184         switch (codec->vendor_id) {
3185         case 0x10ec0269:
3186                 spec->codec_variant = ALC269_TYPE_ALC269VA;
3187                 switch (alc_get_coef0(codec) & 0x00f0) {
3188                 case 0x0010:
3189                         if (codec->bus->pci->subsystem_vendor == 0x1025 &&
3190                             spec->cdefine.platform_type == 1)
3191                                 err = alc_codec_rename(codec, "ALC271X");
3192                         spec->codec_variant = ALC269_TYPE_ALC269VB;
3193                         break;
3194                 case 0x0020:
3195                         if (codec->bus->pci->subsystem_vendor == 0x17aa &&
3196                             codec->bus->pci->subsystem_device == 0x21f3)
3197                                 err = alc_codec_rename(codec, "ALC3202");
3198                         spec->codec_variant = ALC269_TYPE_ALC269VC;
3199                         break;
3200                 case 0x0030:
3201                         spec->codec_variant = ALC269_TYPE_ALC269VD;
3202                         break;
3203                 default:
3204                         alc_fix_pll_init(codec, 0x20, 0x04, 15);
3205                 }
3206                 if (err < 0)
3207                         goto error;
3208                 spec->init_hook = alc269_fill_coef;
3209                 alc269_fill_coef(codec);
3210                 break;
3211
3212         case 0x10ec0280:
3213         case 0x10ec0290:
3214                 spec->codec_variant = ALC269_TYPE_ALC280;
3215                 break;
3216         case 0x10ec0233:
3217         case 0x10ec0282:
3218         case 0x10ec0283:
3219                 spec->codec_variant = ALC269_TYPE_ALC282;
3220                 break;
3221         case 0x10ec0284:
3222         case 0x10ec0292:
3223                 spec->codec_variant = ALC269_TYPE_ALC284;
3224                 break;
3225         }
3226
3227         /* automatic parse from the BIOS config */
3228         err = alc269_parse_auto_config(codec);
3229         if (err < 0)
3230                 goto error;
3231
3232         if (!spec->gen.no_analog && spec->gen.beep_nid)
3233                 set_beep_amp(spec, 0x0b, 0x04, HDA_INPUT);
3234
3235         codec->patch_ops = alc_patch_ops;
3236 #ifdef CONFIG_PM
3237         codec->patch_ops.resume = alc269_resume;
3238 #endif
3239         spec->shutup = alc269_shutup;
3240
3241         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
3242
3243         return 0;
3244
3245  error:
3246         alc_free(codec);
3247         return err;
3248 }
3249
3250 /*
3251  * ALC861
3252  */
3253
3254 static int alc861_parse_auto_config(struct hda_codec *codec)
3255 {
3256         static const hda_nid_t alc861_ignore[] = { 0x1d, 0 };
3257         static const hda_nid_t alc861_ssids[] = { 0x0e, 0x0f, 0x0b, 0 };
3258         return alc_parse_auto_config(codec, alc861_ignore, alc861_ssids);
3259 }
3260
3261 /* Pin config fixes */
3262 enum {
3263         ALC861_FIXUP_FSC_AMILO_PI1505,
3264         ALC861_FIXUP_AMP_VREF_0F,
3265         ALC861_FIXUP_NO_JACK_DETECT,
3266         ALC861_FIXUP_ASUS_A6RP,
3267 };
3268
3269 /* On some laptops, VREF of pin 0x0f is abused for controlling the main amp */
3270 static void alc861_fixup_asus_amp_vref_0f(struct hda_codec *codec,
3271                         const struct hda_fixup *fix, int action)
3272 {
3273         struct alc_spec *spec = codec->spec;
3274         unsigned int val;
3275
3276         if (action != HDA_FIXUP_ACT_INIT)
3277                 return;
3278         val = snd_hda_codec_get_pin_target(codec, 0x0f);
3279         if (!(val & (AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN)))
3280                 val |= AC_PINCTL_IN_EN;
3281         val |= AC_PINCTL_VREF_50;
3282         snd_hda_set_pin_ctl(codec, 0x0f, val);
3283         spec->gen.keep_vref_in_automute = 1;
3284 }
3285
3286 /* suppress the jack-detection */
3287 static void alc_fixup_no_jack_detect(struct hda_codec *codec,
3288                                      const struct hda_fixup *fix, int action)
3289 {
3290         if (action == HDA_FIXUP_ACT_PRE_PROBE)
3291                 codec->no_jack_detect = 1;
3292 }
3293
3294 static const struct hda_fixup alc861_fixups[] = {
3295         [ALC861_FIXUP_FSC_AMILO_PI1505] = {
3296                 .type = HDA_FIXUP_PINS,
3297                 .v.pins = (const struct hda_pintbl[]) {
3298                         { 0x0b, 0x0221101f }, /* HP */
3299                         { 0x0f, 0x90170310 }, /* speaker */
3300                         { }
3301                 }
3302         },
3303         [ALC861_FIXUP_AMP_VREF_0F] = {
3304                 .type = HDA_FIXUP_FUNC,
3305                 .v.func = alc861_fixup_asus_amp_vref_0f,
3306         },
3307         [ALC861_FIXUP_NO_JACK_DETECT] = {
3308                 .type = HDA_FIXUP_FUNC,
3309                 .v.func = alc_fixup_no_jack_detect,
3310         },
3311         [ALC861_FIXUP_ASUS_A6RP] = {
3312                 .type = HDA_FIXUP_FUNC,
3313                 .v.func = alc861_fixup_asus_amp_vref_0f,
3314                 .chained = true,
3315                 .chain_id = ALC861_FIXUP_NO_JACK_DETECT,
3316         }
3317 };
3318
3319 static const struct snd_pci_quirk alc861_fixup_tbl[] = {
3320         SND_PCI_QUIRK(0x1043, 0x1393, "ASUS A6Rp", ALC861_FIXUP_ASUS_A6RP),
3321         SND_PCI_QUIRK_VENDOR(0x1043, "ASUS laptop", ALC861_FIXUP_AMP_VREF_0F),
3322         SND_PCI_QUIRK(0x1462, 0x7254, "HP DX2200", ALC861_FIXUP_NO_JACK_DETECT),
3323         SND_PCI_QUIRK(0x1584, 0x2b01, "Haier W18", ALC861_FIXUP_AMP_VREF_0F),
3324         SND_PCI_QUIRK(0x1584, 0x0000, "Uniwill ECS M31EI", ALC861_FIXUP_AMP_VREF_0F),
3325         SND_PCI_QUIRK(0x1734, 0x10c7, "FSC Amilo Pi1505", ALC861_FIXUP_FSC_AMILO_PI1505),
3326         {}
3327 };
3328
3329 /*
3330  */
3331 static int patch_alc861(struct hda_codec *codec)
3332 {
3333         struct alc_spec *spec;
3334         int err;
3335
3336         err = alc_alloc_spec(codec, 0x15);
3337         if (err < 0)
3338                 return err;
3339
3340         spec = codec->spec;
3341         spec->gen.beep_nid = 0x23;
3342
3343         snd_hda_pick_fixup(codec, NULL, alc861_fixup_tbl, alc861_fixups);
3344         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
3345
3346         /* automatic parse from the BIOS config */
3347         err = alc861_parse_auto_config(codec);
3348         if (err < 0)
3349                 goto error;
3350
3351         if (!spec->gen.no_analog)
3352                 set_beep_amp(spec, 0x23, 0, HDA_OUTPUT);
3353
3354         codec->patch_ops = alc_patch_ops;
3355 #ifdef CONFIG_PM
3356         spec->power_hook = alc_power_eapd;
3357 #endif
3358
3359         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
3360
3361         return 0;
3362
3363  error:
3364         alc_free(codec);
3365         return err;
3366 }
3367
3368 /*
3369  * ALC861-VD support
3370  *
3371  * Based on ALC882
3372  *
3373  * In addition, an independent DAC
3374  */
3375 static int alc861vd_parse_auto_config(struct hda_codec *codec)
3376 {
3377         static const hda_nid_t alc861vd_ignore[] = { 0x1d, 0 };
3378         static const hda_nid_t alc861vd_ssids[] = { 0x15, 0x1b, 0x14, 0 };
3379         return alc_parse_auto_config(codec, alc861vd_ignore, alc861vd_ssids);
3380 }
3381
3382 enum {
3383         ALC660VD_FIX_ASUS_GPIO1,
3384         ALC861VD_FIX_DALLAS,
3385 };
3386
3387 /* exclude VREF80 */
3388 static void alc861vd_fixup_dallas(struct hda_codec *codec,
3389                                   const struct hda_fixup *fix, int action)
3390 {
3391         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
3392                 snd_hda_override_pin_caps(codec, 0x18, 0x00000734);
3393                 snd_hda_override_pin_caps(codec, 0x19, 0x0000073c);
3394         }
3395 }
3396
3397 static const struct hda_fixup alc861vd_fixups[] = {
3398         [ALC660VD_FIX_ASUS_GPIO1] = {
3399                 .type = HDA_FIXUP_VERBS,
3400                 .v.verbs = (const struct hda_verb[]) {
3401                         /* reset GPIO1 */
3402                         {0x01, AC_VERB_SET_GPIO_MASK, 0x03},
3403                         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
3404                         {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
3405                         { }
3406                 }
3407         },
3408         [ALC861VD_FIX_DALLAS] = {
3409                 .type = HDA_FIXUP_FUNC,
3410                 .v.func = alc861vd_fixup_dallas,
3411         },
3412 };
3413
3414 static const struct snd_pci_quirk alc861vd_fixup_tbl[] = {
3415         SND_PCI_QUIRK(0x103c, 0x30bf, "HP TX1000", ALC861VD_FIX_DALLAS),
3416         SND_PCI_QUIRK(0x1043, 0x1339, "ASUS A7-K", ALC660VD_FIX_ASUS_GPIO1),
3417         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba L30-149", ALC861VD_FIX_DALLAS),
3418         {}
3419 };
3420
3421 /*
3422  */
3423 static int patch_alc861vd(struct hda_codec *codec)
3424 {
3425         struct alc_spec *spec;
3426         int err;
3427
3428         err = alc_alloc_spec(codec, 0x0b);
3429         if (err < 0)
3430                 return err;
3431
3432         spec = codec->spec;
3433         spec->gen.beep_nid = 0x23;
3434
3435         snd_hda_pick_fixup(codec, NULL, alc861vd_fixup_tbl, alc861vd_fixups);
3436         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
3437
3438         /* automatic parse from the BIOS config */
3439         err = alc861vd_parse_auto_config(codec);
3440         if (err < 0)
3441                 goto error;
3442
3443         if (!spec->gen.no_analog)
3444                 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
3445
3446         codec->patch_ops = alc_patch_ops;
3447
3448         spec->shutup = alc_eapd_shutup;
3449
3450         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
3451
3452         return 0;
3453
3454  error:
3455         alc_free(codec);
3456         return err;
3457 }
3458
3459 /*
3460  * ALC662 support
3461  *
3462  * ALC662 is almost identical with ALC880 but has cleaner and more flexible
3463  * configuration.  Each pin widget can choose any input DACs and a mixer.
3464  * Each ADC is connected from a mixer of all inputs.  This makes possible
3465  * 6-channel independent captures.
3466  *
3467  * In addition, an independent DAC for the multi-playback (not used in this
3468  * driver yet).
3469  */
3470
3471 /*
3472  * BIOS auto configuration
3473  */
3474
3475 static int alc662_parse_auto_config(struct hda_codec *codec)
3476 {
3477         static const hda_nid_t alc662_ignore[] = { 0x1d, 0 };
3478         static const hda_nid_t alc663_ssids[] = { 0x15, 0x1b, 0x14, 0x21 };
3479         static const hda_nid_t alc662_ssids[] = { 0x15, 0x1b, 0x14, 0 };
3480         const hda_nid_t *ssids;
3481
3482         if (codec->vendor_id == 0x10ec0272 || codec->vendor_id == 0x10ec0663 ||
3483             codec->vendor_id == 0x10ec0665 || codec->vendor_id == 0x10ec0670)
3484                 ssids = alc663_ssids;
3485         else
3486                 ssids = alc662_ssids;
3487         return alc_parse_auto_config(codec, alc662_ignore, ssids);
3488 }
3489
3490 static void alc272_fixup_mario(struct hda_codec *codec,
3491                                const struct hda_fixup *fix, int action)
3492 {
3493         if (action != HDA_FIXUP_ACT_PRE_PROBE)
3494                 return;
3495         if (snd_hda_override_amp_caps(codec, 0x2, HDA_OUTPUT,
3496                                       (0x3b << AC_AMPCAP_OFFSET_SHIFT) |
3497                                       (0x3b << AC_AMPCAP_NUM_STEPS_SHIFT) |
3498                                       (0x03 << AC_AMPCAP_STEP_SIZE_SHIFT) |
3499                                       (0 << AC_AMPCAP_MUTE_SHIFT)))
3500                 printk(KERN_WARNING
3501                        "hda_codec: failed to override amp caps for NID 0x2\n");
3502 }
3503
3504 enum {
3505         ALC662_FIXUP_ASPIRE,
3506         ALC662_FIXUP_IDEAPAD,
3507         ALC272_FIXUP_MARIO,
3508         ALC662_FIXUP_CZC_P10T,
3509         ALC662_FIXUP_SKU_IGNORE,
3510         ALC662_FIXUP_HP_RP5800,
3511         ALC662_FIXUP_ASUS_MODE1,
3512         ALC662_FIXUP_ASUS_MODE2,
3513         ALC662_FIXUP_ASUS_MODE3,
3514         ALC662_FIXUP_ASUS_MODE4,
3515         ALC662_FIXUP_ASUS_MODE5,
3516         ALC662_FIXUP_ASUS_MODE6,
3517         ALC662_FIXUP_ASUS_MODE7,
3518         ALC662_FIXUP_ASUS_MODE8,
3519         ALC662_FIXUP_NO_JACK_DETECT,
3520         ALC662_FIXUP_ZOTAC_Z68,
3521         ALC662_FIXUP_INV_DMIC,
3522 };
3523
3524 static const struct hda_fixup alc662_fixups[] = {
3525         [ALC662_FIXUP_ASPIRE] = {
3526                 .type = HDA_FIXUP_PINS,
3527                 .v.pins = (const struct hda_pintbl[]) {
3528                         { 0x15, 0x99130112 }, /* subwoofer */
3529                         { }
3530                 }
3531         },
3532         [ALC662_FIXUP_IDEAPAD] = {
3533                 .type = HDA_FIXUP_PINS,
3534                 .v.pins = (const struct hda_pintbl[]) {
3535                         { 0x17, 0x99130112 }, /* subwoofer */
3536                         { }
3537                 }
3538         },
3539         [ALC272_FIXUP_MARIO] = {
3540                 .type = HDA_FIXUP_FUNC,
3541                 .v.func = alc272_fixup_mario,
3542         },
3543         [ALC662_FIXUP_CZC_P10T] = {
3544                 .type = HDA_FIXUP_VERBS,
3545                 .v.verbs = (const struct hda_verb[]) {
3546                         {0x14, AC_VERB_SET_EAPD_BTLENABLE, 0},
3547                         {}
3548                 }
3549         },
3550         [ALC662_FIXUP_SKU_IGNORE] = {
3551                 .type = HDA_FIXUP_FUNC,
3552                 .v.func = alc_fixup_sku_ignore,
3553         },
3554         [ALC662_FIXUP_HP_RP5800] = {
3555                 .type = HDA_FIXUP_PINS,
3556                 .v.pins = (const struct hda_pintbl[]) {
3557                         { 0x14, 0x0221201f }, /* HP out */
3558                         { }
3559                 },
3560                 .chained = true,
3561                 .chain_id = ALC662_FIXUP_SKU_IGNORE
3562         },
3563         [ALC662_FIXUP_ASUS_MODE1] = {
3564                 .type = HDA_FIXUP_PINS,
3565                 .v.pins = (const struct hda_pintbl[]) {
3566                         { 0x14, 0x99130110 }, /* speaker */
3567                         { 0x18, 0x01a19c20 }, /* mic */
3568                         { 0x19, 0x99a3092f }, /* int-mic */
3569                         { 0x21, 0x0121401f }, /* HP out */
3570                         { }
3571                 },
3572                 .chained = true,
3573                 .chain_id = ALC662_FIXUP_SKU_IGNORE
3574         },
3575         [ALC662_FIXUP_ASUS_MODE2] = {
3576                 .type = HDA_FIXUP_PINS,
3577                 .v.pins = (const struct hda_pintbl[]) {
3578                         { 0x14, 0x99130110 }, /* speaker */
3579                         { 0x18, 0x01a19820 }, /* mic */
3580                         { 0x19, 0x99a3092f }, /* int-mic */
3581                         { 0x1b, 0x0121401f }, /* HP out */
3582                         { }
3583                 },
3584                 .chained = true,
3585                 .chain_id = ALC662_FIXUP_SKU_IGNORE
3586         },
3587         [ALC662_FIXUP_ASUS_MODE3] = {
3588                 .type = HDA_FIXUP_PINS,
3589                 .v.pins = (const struct hda_pintbl[]) {
3590                         { 0x14, 0x99130110 }, /* speaker */
3591                         { 0x15, 0x0121441f }, /* HP */
3592                         { 0x18, 0x01a19840 }, /* mic */
3593                         { 0x19, 0x99a3094f }, /* int-mic */
3594                         { 0x21, 0x01211420 }, /* HP2 */
3595                         { }
3596                 },
3597                 .chained = true,
3598                 .chain_id = ALC662_FIXUP_SKU_IGNORE
3599         },
3600         [ALC662_FIXUP_ASUS_MODE4] = {
3601                 .type = HDA_FIXUP_PINS,
3602                 .v.pins = (const struct hda_pintbl[]) {
3603                         { 0x14, 0x99130110 }, /* speaker */
3604                         { 0x16, 0x99130111 }, /* speaker */
3605                         { 0x18, 0x01a19840 }, /* mic */
3606                         { 0x19, 0x99a3094f }, /* int-mic */
3607                         { 0x21, 0x0121441f }, /* HP */
3608                         { }
3609                 },
3610                 .chained = true,
3611                 .chain_id = ALC662_FIXUP_SKU_IGNORE
3612         },
3613         [ALC662_FIXUP_ASUS_MODE5] = {
3614                 .type = HDA_FIXUP_PINS,
3615                 .v.pins = (const struct hda_pintbl[]) {
3616                         { 0x14, 0x99130110 }, /* speaker */
3617                         { 0x15, 0x0121441f }, /* HP */
3618                         { 0x16, 0x99130111 }, /* speaker */
3619                         { 0x18, 0x01a19840 }, /* mic */
3620                         { 0x19, 0x99a3094f }, /* int-mic */
3621                         { }
3622                 },
3623                 .chained = true,
3624                 .chain_id = ALC662_FIXUP_SKU_IGNORE
3625         },
3626         [ALC662_FIXUP_ASUS_MODE6] = {
3627                 .type = HDA_FIXUP_PINS,
3628                 .v.pins = (const struct hda_pintbl[]) {
3629                         { 0x14, 0x99130110 }, /* speaker */
3630                         { 0x15, 0x01211420 }, /* HP2 */
3631                         { 0x18, 0x01a19840 }, /* mic */
3632                         { 0x19, 0x99a3094f }, /* int-mic */
3633                         { 0x1b, 0x0121441f }, /* HP */
3634                         { }
3635                 },
3636                 .chained = true,
3637                 .chain_id = ALC662_FIXUP_SKU_IGNORE
3638         },
3639         [ALC662_FIXUP_ASUS_MODE7] = {
3640                 .type = HDA_FIXUP_PINS,
3641                 .v.pins = (const struct hda_pintbl[]) {
3642                         { 0x14, 0x99130110 }, /* speaker */
3643                         { 0x17, 0x99130111 }, /* speaker */
3644                         { 0x18, 0x01a19840 }, /* mic */
3645                         { 0x19, 0x99a3094f }, /* int-mic */
3646                         { 0x1b, 0x01214020 }, /* HP */
3647                         { 0x21, 0x0121401f }, /* HP */
3648                         { }
3649                 },
3650                 .chained = true,
3651                 .chain_id = ALC662_FIXUP_SKU_IGNORE
3652         },
3653         [ALC662_FIXUP_ASUS_MODE8] = {
3654                 .type = HDA_FIXUP_PINS,
3655                 .v.pins = (const struct hda_pintbl[]) {
3656                         { 0x14, 0x99130110 }, /* speaker */
3657                         { 0x12, 0x99a30970 }, /* int-mic */
3658                         { 0x15, 0x01214020 }, /* HP */
3659                         { 0x17, 0x99130111 }, /* speaker */
3660                         { 0x18, 0x01a19840 }, /* mic */
3661                         { 0x21, 0x0121401f }, /* HP */
3662                         { }
3663                 },
3664                 .chained = true,
3665                 .chain_id = ALC662_FIXUP_SKU_IGNORE
3666         },
3667         [ALC662_FIXUP_NO_JACK_DETECT] = {
3668                 .type = HDA_FIXUP_FUNC,
3669                 .v.func = alc_fixup_no_jack_detect,
3670         },
3671         [ALC662_FIXUP_ZOTAC_Z68] = {
3672                 .type = HDA_FIXUP_PINS,
3673                 .v.pins = (const struct hda_pintbl[]) {
3674                         { 0x1b, 0x02214020 }, /* Front HP */
3675                         { }
3676                 }
3677         },
3678         [ALC662_FIXUP_INV_DMIC] = {
3679                 .type = HDA_FIXUP_FUNC,
3680                 .v.func = alc_fixup_inv_dmic_0x12,
3681         },
3682 };
3683
3684 static const struct snd_pci_quirk alc662_fixup_tbl[] = {
3685         SND_PCI_QUIRK(0x1019, 0x9087, "ECS", ALC662_FIXUP_ASUS_MODE2),
3686         SND_PCI_QUIRK(0x1025, 0x0308, "Acer Aspire 8942G", ALC662_FIXUP_ASPIRE),
3687         SND_PCI_QUIRK(0x1025, 0x031c, "Gateway NV79", ALC662_FIXUP_SKU_IGNORE),
3688         SND_PCI_QUIRK(0x1025, 0x0349, "eMachines eM250", ALC662_FIXUP_INV_DMIC),
3689         SND_PCI_QUIRK(0x1025, 0x038b, "Acer Aspire 8943G", ALC662_FIXUP_ASPIRE),
3690         SND_PCI_QUIRK(0x103c, 0x1632, "HP RP5800", ALC662_FIXUP_HP_RP5800),
3691         SND_PCI_QUIRK(0x1043, 0x8469, "ASUS mobo", ALC662_FIXUP_NO_JACK_DETECT),
3692         SND_PCI_QUIRK(0x105b, 0x0cd6, "Foxconn", ALC662_FIXUP_ASUS_MODE2),
3693         SND_PCI_QUIRK(0x144d, 0xc051, "Samsung R720", ALC662_FIXUP_IDEAPAD),
3694         SND_PCI_QUIRK(0x17aa, 0x38af, "Lenovo Ideapad Y550P", ALC662_FIXUP_IDEAPAD),
3695         SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo Ideapad Y550", ALC662_FIXUP_IDEAPAD),
3696         SND_PCI_QUIRK(0x19da, 0xa130, "Zotac Z68", ALC662_FIXUP_ZOTAC_Z68),
3697         SND_PCI_QUIRK(0x1b35, 0x2206, "CZC P10T", ALC662_FIXUP_CZC_P10T),
3698
3699 #if 0
3700         /* Below is a quirk table taken from the old code.
3701          * Basically the device should work as is without the fixup table.
3702          * If BIOS doesn't give a proper info, enable the corresponding
3703          * fixup entry.
3704          */
3705         SND_PCI_QUIRK(0x1043, 0x1000, "ASUS N50Vm", ALC662_FIXUP_ASUS_MODE1),
3706         SND_PCI_QUIRK(0x1043, 0x1092, "ASUS NB", ALC662_FIXUP_ASUS_MODE3),
3707         SND_PCI_QUIRK(0x1043, 0x1173, "ASUS K73Jn", ALC662_FIXUP_ASUS_MODE1),
3708         SND_PCI_QUIRK(0x1043, 0x11c3, "ASUS M70V", ALC662_FIXUP_ASUS_MODE3),
3709         SND_PCI_QUIRK(0x1043, 0x11d3, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
3710         SND_PCI_QUIRK(0x1043, 0x11f3, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
3711         SND_PCI_QUIRK(0x1043, 0x1203, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
3712         SND_PCI_QUIRK(0x1043, 0x1303, "ASUS G60J", ALC662_FIXUP_ASUS_MODE1),
3713         SND_PCI_QUIRK(0x1043, 0x1333, "ASUS G60Jx", ALC662_FIXUP_ASUS_MODE1),
3714         SND_PCI_QUIRK(0x1043, 0x1339, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
3715         SND_PCI_QUIRK(0x1043, 0x13e3, "ASUS N71JA", ALC662_FIXUP_ASUS_MODE7),
3716         SND_PCI_QUIRK(0x1043, 0x1463, "ASUS N71", ALC662_FIXUP_ASUS_MODE7),
3717         SND_PCI_QUIRK(0x1043, 0x14d3, "ASUS G72", ALC662_FIXUP_ASUS_MODE8),
3718         SND_PCI_QUIRK(0x1043, 0x1563, "ASUS N90", ALC662_FIXUP_ASUS_MODE3),
3719         SND_PCI_QUIRK(0x1043, 0x15d3, "ASUS N50SF F50SF", ALC662_FIXUP_ASUS_MODE1),
3720         SND_PCI_QUIRK(0x1043, 0x16c3, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
3721         SND_PCI_QUIRK(0x1043, 0x16f3, "ASUS K40C K50C", ALC662_FIXUP_ASUS_MODE2),
3722         SND_PCI_QUIRK(0x1043, 0x1733, "ASUS N81De", ALC662_FIXUP_ASUS_MODE1),
3723         SND_PCI_QUIRK(0x1043, 0x1753, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
3724         SND_PCI_QUIRK(0x1043, 0x1763, "ASUS NB", ALC662_FIXUP_ASUS_MODE6),
3725         SND_PCI_QUIRK(0x1043, 0x1765, "ASUS NB", ALC662_FIXUP_ASUS_MODE6),
3726         SND_PCI_QUIRK(0x1043, 0x1783, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
3727         SND_PCI_QUIRK(0x1043, 0x1793, "ASUS F50GX", ALC662_FIXUP_ASUS_MODE1),
3728         SND_PCI_QUIRK(0x1043, 0x17b3, "ASUS F70SL", ALC662_FIXUP_ASUS_MODE3),
3729         SND_PCI_QUIRK(0x1043, 0x17f3, "ASUS X58LE", ALC662_FIXUP_ASUS_MODE2),
3730         SND_PCI_QUIRK(0x1043, 0x1813, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
3731         SND_PCI_QUIRK(0x1043, 0x1823, "ASUS NB", ALC662_FIXUP_ASUS_MODE5),
3732         SND_PCI_QUIRK(0x1043, 0x1833, "ASUS NB", ALC662_FIXUP_ASUS_MODE6),
3733         SND_PCI_QUIRK(0x1043, 0x1843, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
3734         SND_PCI_QUIRK(0x1043, 0x1853, "ASUS F50Z", ALC662_FIXUP_ASUS_MODE1),
3735         SND_PCI_QUIRK(0x1043, 0x1864, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
3736         SND_PCI_QUIRK(0x1043, 0x1876, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
3737         SND_PCI_QUIRK(0x1043, 0x1893, "ASUS M50Vm", ALC662_FIXUP_ASUS_MODE3),
3738         SND_PCI_QUIRK(0x1043, 0x1894, "ASUS X55", ALC662_FIXUP_ASUS_MODE3),
3739         SND_PCI_QUIRK(0x1043, 0x18b3, "ASUS N80Vc", ALC662_FIXUP_ASUS_MODE1),
3740         SND_PCI_QUIRK(0x1043, 0x18c3, "ASUS VX5", ALC662_FIXUP_ASUS_MODE1),
3741         SND_PCI_QUIRK(0x1043, 0x18d3, "ASUS N81Te", ALC662_FIXUP_ASUS_MODE1),
3742         SND_PCI_QUIRK(0x1043, 0x18f3, "ASUS N505Tp", ALC662_FIXUP_ASUS_MODE1),
3743         SND_PCI_QUIRK(0x1043, 0x1903, "ASUS F5GL", ALC662_FIXUP_ASUS_MODE1),
3744         SND_PCI_QUIRK(0x1043, 0x1913, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
3745         SND_PCI_QUIRK(0x1043, 0x1933, "ASUS F80Q", ALC662_FIXUP_ASUS_MODE2),
3746         SND_PCI_QUIRK(0x1043, 0x1943, "ASUS Vx3V", ALC662_FIXUP_ASUS_MODE1),
3747         SND_PCI_QUIRK(0x1043, 0x1953, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
3748         SND_PCI_QUIRK(0x1043, 0x1963, "ASUS X71C", ALC662_FIXUP_ASUS_MODE3),
3749         SND_PCI_QUIRK(0x1043, 0x1983, "ASUS N5051A", ALC662_FIXUP_ASUS_MODE1),
3750         SND_PCI_QUIRK(0x1043, 0x1993, "ASUS N20", ALC662_FIXUP_ASUS_MODE1),
3751         SND_PCI_QUIRK(0x1043, 0x19b3, "ASUS F7Z", ALC662_FIXUP_ASUS_MODE1),
3752         SND_PCI_QUIRK(0x1043, 0x19c3, "ASUS F5Z/F6x", ALC662_FIXUP_ASUS_MODE2),
3753         SND_PCI_QUIRK(0x1043, 0x19e3, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
3754         SND_PCI_QUIRK(0x1043, 0x19f3, "ASUS NB", ALC662_FIXUP_ASUS_MODE4),
3755 #endif
3756         {}
3757 };
3758
3759 static const struct hda_model_fixup alc662_fixup_models[] = {
3760         {.id = ALC272_FIXUP_MARIO, .name = "mario"},
3761         {.id = ALC662_FIXUP_ASUS_MODE1, .name = "asus-mode1"},
3762         {.id = ALC662_FIXUP_ASUS_MODE2, .name = "asus-mode2"},
3763         {.id = ALC662_FIXUP_ASUS_MODE3, .name = "asus-mode3"},
3764         {.id = ALC662_FIXUP_ASUS_MODE4, .name = "asus-mode4"},
3765         {.id = ALC662_FIXUP_ASUS_MODE5, .name = "asus-mode5"},
3766         {.id = ALC662_FIXUP_ASUS_MODE6, .name = "asus-mode6"},
3767         {.id = ALC662_FIXUP_ASUS_MODE7, .name = "asus-mode7"},
3768         {.id = ALC662_FIXUP_ASUS_MODE8, .name = "asus-mode8"},
3769         {.id = ALC662_FIXUP_INV_DMIC, .name = "inv-dmic"},
3770         {}
3771 };
3772
3773 static void alc662_fill_coef(struct hda_codec *codec)
3774 {
3775         int val, coef;
3776
3777         coef = alc_get_coef0(codec);
3778
3779         switch (codec->vendor_id) {
3780         case 0x10ec0662:
3781                 if ((coef & 0x00f0) == 0x0030) {
3782                         val = alc_read_coef_idx(codec, 0x4); /* EAPD Ctrl */
3783                         alc_write_coef_idx(codec, 0x4, val & ~(1<<10));
3784                 }
3785                 break;
3786         case 0x10ec0272:
3787         case 0x10ec0273:
3788         case 0x10ec0663:
3789         case 0x10ec0665:
3790         case 0x10ec0670:
3791         case 0x10ec0671:
3792         case 0x10ec0672:
3793                 val = alc_read_coef_idx(codec, 0xd); /* EAPD Ctrl */
3794                 alc_write_coef_idx(codec, 0xd, val | (1<<14));
3795                 break;
3796         }
3797 }
3798
3799 /*
3800  */
3801 static int patch_alc662(struct hda_codec *codec)
3802 {
3803         struct alc_spec *spec;
3804         int err;
3805
3806         err = alc_alloc_spec(codec, 0x0b);
3807         if (err < 0)
3808                 return err;
3809
3810         spec = codec->spec;
3811
3812         /* handle multiple HPs as is */
3813         spec->parse_flags = HDA_PINCFG_NO_HP_FIXUP;
3814
3815         alc_fix_pll_init(codec, 0x20, 0x04, 15);
3816
3817         spec->init_hook = alc662_fill_coef;
3818         alc662_fill_coef(codec);
3819
3820         snd_hda_pick_fixup(codec, alc662_fixup_models,
3821                        alc662_fixup_tbl, alc662_fixups);
3822         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
3823
3824         alc_auto_parse_customize_define(codec);
3825
3826         if (has_cdefine_beep(codec))
3827                 spec->gen.beep_nid = 0x01;
3828
3829         if ((alc_get_coef0(codec) & (1 << 14)) &&
3830             codec->bus->pci->subsystem_vendor == 0x1025 &&
3831             spec->cdefine.platform_type == 1) {
3832                 if (alc_codec_rename(codec, "ALC272X") < 0)
3833                         goto error;
3834         }
3835
3836         /* automatic parse from the BIOS config */
3837         err = alc662_parse_auto_config(codec);
3838         if (err < 0)
3839                 goto error;
3840
3841         if (!spec->gen.no_analog && spec->gen.beep_nid) {
3842                 switch (codec->vendor_id) {
3843                 case 0x10ec0662:
3844                         set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
3845                         break;
3846                 case 0x10ec0272:
3847                 case 0x10ec0663:
3848                 case 0x10ec0665:
3849                         set_beep_amp(spec, 0x0b, 0x04, HDA_INPUT);
3850                         break;
3851                 case 0x10ec0273:
3852                         set_beep_amp(spec, 0x0b, 0x03, HDA_INPUT);
3853                         break;
3854                 }
3855         }
3856
3857         codec->patch_ops = alc_patch_ops;
3858         spec->shutup = alc_eapd_shutup;
3859
3860         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
3861
3862         return 0;
3863
3864  error:
3865         alc_free(codec);
3866         return err;
3867 }
3868
3869 /*
3870  * ALC680 support
3871  */
3872
3873 static int alc680_parse_auto_config(struct hda_codec *codec)
3874 {
3875         return alc_parse_auto_config(codec, NULL, NULL);
3876 }
3877
3878 /*
3879  */
3880 static int patch_alc680(struct hda_codec *codec)
3881 {
3882         int err;
3883
3884         /* ALC680 has no aa-loopback mixer */
3885         err = alc_alloc_spec(codec, 0);
3886         if (err < 0)
3887                 return err;
3888
3889         /* automatic parse from the BIOS config */
3890         err = alc680_parse_auto_config(codec);
3891         if (err < 0) {
3892                 alc_free(codec);
3893                 return err;
3894         }
3895
3896         codec->patch_ops = alc_patch_ops;
3897
3898         return 0;
3899 }
3900
3901 /*
3902  * patch entries
3903  */
3904 static const struct hda_codec_preset snd_hda_preset_realtek[] = {
3905         { .id = 0x10ec0221, .name = "ALC221", .patch = patch_alc269 },
3906         { .id = 0x10ec0233, .name = "ALC233", .patch = patch_alc269 },
3907         { .id = 0x10ec0260, .name = "ALC260", .patch = patch_alc260 },
3908         { .id = 0x10ec0262, .name = "ALC262", .patch = patch_alc262 },
3909         { .id = 0x10ec0267, .name = "ALC267", .patch = patch_alc268 },
3910         { .id = 0x10ec0268, .name = "ALC268", .patch = patch_alc268 },
3911         { .id = 0x10ec0269, .name = "ALC269", .patch = patch_alc269 },
3912         { .id = 0x10ec0270, .name = "ALC270", .patch = patch_alc269 },
3913         { .id = 0x10ec0272, .name = "ALC272", .patch = patch_alc662 },
3914         { .id = 0x10ec0275, .name = "ALC275", .patch = patch_alc269 },
3915         { .id = 0x10ec0276, .name = "ALC276", .patch = patch_alc269 },
3916         { .id = 0x10ec0280, .name = "ALC280", .patch = patch_alc269 },
3917         { .id = 0x10ec0282, .name = "ALC282", .patch = patch_alc269 },
3918         { .id = 0x10ec0283, .name = "ALC283", .patch = patch_alc269 },
3919         { .id = 0x10ec0284, .name = "ALC284", .patch = patch_alc269 },
3920         { .id = 0x10ec0290, .name = "ALC290", .patch = patch_alc269 },
3921         { .id = 0x10ec0292, .name = "ALC292", .patch = patch_alc269 },
3922         { .id = 0x10ec0861, .rev = 0x100340, .name = "ALC660",
3923           .patch = patch_alc861 },
3924         { .id = 0x10ec0660, .name = "ALC660-VD", .patch = patch_alc861vd },
3925         { .id = 0x10ec0861, .name = "ALC861", .patch = patch_alc861 },
3926         { .id = 0x10ec0862, .name = "ALC861-VD", .patch = patch_alc861vd },
3927         { .id = 0x10ec0662, .rev = 0x100002, .name = "ALC662 rev2",
3928           .patch = patch_alc882 },
3929         { .id = 0x10ec0662, .rev = 0x100101, .name = "ALC662 rev1",
3930           .patch = patch_alc662 },
3931         { .id = 0x10ec0662, .rev = 0x100300, .name = "ALC662 rev3",
3932           .patch = patch_alc662 },
3933         { .id = 0x10ec0663, .name = "ALC663", .patch = patch_alc662 },
3934         { .id = 0x10ec0665, .name = "ALC665", .patch = patch_alc662 },
3935         { .id = 0x10ec0668, .name = "ALC668", .patch = patch_alc662 },
3936         { .id = 0x10ec0670, .name = "ALC670", .patch = patch_alc662 },
3937         { .id = 0x10ec0680, .name = "ALC680", .patch = patch_alc680 },
3938         { .id = 0x10ec0880, .name = "ALC880", .patch = patch_alc880 },
3939         { .id = 0x10ec0882, .name = "ALC882", .patch = patch_alc882 },
3940         { .id = 0x10ec0883, .name = "ALC883", .patch = patch_alc882 },
3941         { .id = 0x10ec0885, .rev = 0x100101, .name = "ALC889A",
3942           .patch = patch_alc882 },
3943         { .id = 0x10ec0885, .rev = 0x100103, .name = "ALC889A",
3944           .patch = patch_alc882 },
3945         { .id = 0x10ec0885, .name = "ALC885", .patch = patch_alc882 },
3946         { .id = 0x10ec0887, .name = "ALC887", .patch = patch_alc882 },
3947         { .id = 0x10ec0888, .rev = 0x100101, .name = "ALC1200",
3948           .patch = patch_alc882 },
3949         { .id = 0x10ec0888, .name = "ALC888", .patch = patch_alc882 },
3950         { .id = 0x10ec0889, .name = "ALC889", .patch = patch_alc882 },
3951         { .id = 0x10ec0892, .name = "ALC892", .patch = patch_alc662 },
3952         { .id = 0x10ec0899, .name = "ALC898", .patch = patch_alc882 },
3953         { .id = 0x10ec0900, .name = "ALC1150", .patch = patch_alc882 },
3954         {} /* terminator */
3955 };
3956
3957 MODULE_ALIAS("snd-hda-codec-id:10ec*");
3958
3959 MODULE_LICENSE("GPL");
3960 MODULE_DESCRIPTION("Realtek HD-audio codec");
3961
3962 static struct hda_codec_preset_list realtek_list = {
3963         .preset = snd_hda_preset_realtek,
3964         .owner = THIS_MODULE,
3965 };
3966
3967 static int __init patch_realtek_init(void)
3968 {
3969         return snd_hda_add_codec_preset(&realtek_list);
3970 }
3971
3972 static void __exit patch_realtek_exit(void)
3973 {
3974         snd_hda_delete_codec_preset(&realtek_list);
3975 }
3976
3977 module_init(patch_realtek_init)
3978 module_exit(patch_realtek_exit)