Merge tag 'fbdev-fixes-3.13' of git://git.kernel.org/pub/scm/linux/kernel/git/tomba...
[linux-drm-fsl-dcu.git] / sound / pci / hda / patch_analog.c
1 /*
2  * HD audio interface patch for AD1882, AD1884, AD1981HD, AD1983, AD1984,
3  *   AD1986A, AD1988
4  *
5  * Copyright (c) 2005-2007 Takashi Iwai <tiwai@suse.de>
6  *
7  *  This driver is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This driver is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/pci.h>
25 #include <linux/module.h>
26
27 #include <sound/core.h>
28 #include "hda_codec.h"
29 #include "hda_local.h"
30 #include "hda_auto_parser.h"
31 #include "hda_beep.h"
32 #include "hda_jack.h"
33 #include "hda_generic.h"
34
35
36 struct ad198x_spec {
37         struct hda_gen_spec gen;
38
39         /* for auto parser */
40         int smux_paths[4];
41         unsigned int cur_smux;
42         hda_nid_t eapd_nid;
43
44         unsigned int beep_amp;  /* beep amp value, set via set_beep_amp() */
45 };
46
47
48 #ifdef CONFIG_SND_HDA_INPUT_BEEP
49 /* additional beep mixers; the actual parameters are overwritten at build */
50 static const struct snd_kcontrol_new ad_beep_mixer[] = {
51         HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_OUTPUT),
52         HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_OUTPUT),
53         { } /* end */
54 };
55
56 #define set_beep_amp(spec, nid, idx, dir) \
57         ((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir)) /* mono */
58 #else
59 #define set_beep_amp(spec, nid, idx, dir) /* NOP */
60 #endif
61
62 #ifdef CONFIG_SND_HDA_INPUT_BEEP
63 static int create_beep_ctls(struct hda_codec *codec)
64 {
65         struct ad198x_spec *spec = codec->spec;
66         const struct snd_kcontrol_new *knew;
67
68         if (!spec->beep_amp)
69                 return 0;
70
71         for (knew = ad_beep_mixer ; knew->name; knew++) {
72                 int err;
73                 struct snd_kcontrol *kctl;
74                 kctl = snd_ctl_new1(knew, codec);
75                 if (!kctl)
76                         return -ENOMEM;
77                 kctl->private_value = spec->beep_amp;
78                 err = snd_hda_ctl_add(codec, 0, kctl);
79                 if (err < 0)
80                         return err;
81         }
82         return 0;
83 }
84 #else
85 #define create_beep_ctls(codec)         0
86 #endif
87
88
89 static void ad198x_power_eapd_write(struct hda_codec *codec, hda_nid_t front,
90                                 hda_nid_t hp)
91 {
92         if (snd_hda_query_pin_caps(codec, front) & AC_PINCAP_EAPD)
93                 snd_hda_codec_write(codec, front, 0, AC_VERB_SET_EAPD_BTLENABLE,
94                             !codec->inv_eapd ? 0x00 : 0x02);
95         if (snd_hda_query_pin_caps(codec, hp) & AC_PINCAP_EAPD)
96                 snd_hda_codec_write(codec, hp, 0, AC_VERB_SET_EAPD_BTLENABLE,
97                             !codec->inv_eapd ? 0x00 : 0x02);
98 }
99
100 static void ad198x_power_eapd(struct hda_codec *codec)
101 {
102         /* We currently only handle front, HP */
103         switch (codec->vendor_id) {
104         case 0x11d41882:
105         case 0x11d4882a:
106         case 0x11d41884:
107         case 0x11d41984:
108         case 0x11d41883:
109         case 0x11d4184a:
110         case 0x11d4194a:
111         case 0x11d4194b:
112         case 0x11d41988:
113         case 0x11d4198b:
114         case 0x11d4989a:
115         case 0x11d4989b:
116                 ad198x_power_eapd_write(codec, 0x12, 0x11);
117                 break;
118         case 0x11d41981:
119         case 0x11d41983:
120                 ad198x_power_eapd_write(codec, 0x05, 0x06);
121                 break;
122         case 0x11d41986:
123                 ad198x_power_eapd_write(codec, 0x1b, 0x1a);
124                 break;
125         }
126 }
127
128 static void ad198x_shutup(struct hda_codec *codec)
129 {
130         snd_hda_shutup_pins(codec);
131         ad198x_power_eapd(codec);
132 }
133
134 #ifdef CONFIG_PM
135 static int ad198x_suspend(struct hda_codec *codec)
136 {
137         ad198x_shutup(codec);
138         return 0;
139 }
140 #endif
141
142 /* follow EAPD via vmaster hook */
143 static void ad_vmaster_eapd_hook(void *private_data, int enabled)
144 {
145         struct hda_codec *codec = private_data;
146         struct ad198x_spec *spec = codec->spec;
147
148         if (!spec->eapd_nid)
149                 return;
150         if (codec->inv_eapd)
151                 enabled = !enabled;
152         snd_hda_codec_update_cache(codec, spec->eapd_nid, 0,
153                                    AC_VERB_SET_EAPD_BTLENABLE,
154                                    enabled ? 0x02 : 0x00);
155 }
156
157 /*
158  * Automatic parse of I/O pins from the BIOS configuration
159  */
160
161 static int ad198x_auto_build_controls(struct hda_codec *codec)
162 {
163         int err;
164
165         err = snd_hda_gen_build_controls(codec);
166         if (err < 0)
167                 return err;
168         err = create_beep_ctls(codec);
169         if (err < 0)
170                 return err;
171         return 0;
172 }
173
174 static const struct hda_codec_ops ad198x_auto_patch_ops = {
175         .build_controls = ad198x_auto_build_controls,
176         .build_pcms = snd_hda_gen_build_pcms,
177         .init = snd_hda_gen_init,
178         .free = snd_hda_gen_free,
179         .unsol_event = snd_hda_jack_unsol_event,
180 #ifdef CONFIG_PM
181         .check_power_status = snd_hda_gen_check_power_status,
182         .suspend = ad198x_suspend,
183 #endif
184         .reboot_notify = ad198x_shutup,
185 };
186
187
188 static int ad198x_parse_auto_config(struct hda_codec *codec)
189 {
190         struct ad198x_spec *spec = codec->spec;
191         struct auto_pin_cfg *cfg = &spec->gen.autocfg;
192         int err;
193
194         codec->spdif_status_reset = 1;
195         codec->no_trigger_sense = 1;
196         codec->no_sticky_stream = 1;
197
198         spec->gen.indep_hp = 1;
199
200         err = snd_hda_parse_pin_defcfg(codec, cfg, NULL, 0);
201         if (err < 0)
202                 return err;
203         err = snd_hda_gen_parse_auto_config(codec, cfg);
204         if (err < 0)
205                 return err;
206
207         codec->patch_ops = ad198x_auto_patch_ops;
208
209         return 0;
210 }
211
212 /*
213  * AD1986A specific
214  */
215
216 static int alloc_ad_spec(struct hda_codec *codec)
217 {
218         struct ad198x_spec *spec;
219
220         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
221         if (!spec)
222                 return -ENOMEM;
223         codec->spec = spec;
224         snd_hda_gen_spec_init(&spec->gen);
225         return 0;
226 }
227
228 /*
229  * AD1986A fixup codes
230  */
231
232 /* Lenovo N100 seems to report the reversed bit for HP jack-sensing */
233 static void ad_fixup_inv_jack_detect(struct hda_codec *codec,
234                                      const struct hda_fixup *fix, int action)
235 {
236         struct ad198x_spec *spec = codec->spec;
237
238         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
239                 codec->inv_jack_detect = 1;
240                 spec->gen.keep_eapd_on = 1;
241                 spec->gen.vmaster_mute.hook = ad_vmaster_eapd_hook;
242                 spec->eapd_nid = 0x1b;
243         }
244 }
245
246 enum {
247         AD1986A_FIXUP_INV_JACK_DETECT,
248         AD1986A_FIXUP_ULTRA,
249         AD1986A_FIXUP_SAMSUNG,
250         AD1986A_FIXUP_3STACK,
251         AD1986A_FIXUP_LAPTOP,
252         AD1986A_FIXUP_LAPTOP_IMIC,
253 };
254
255 static const struct hda_fixup ad1986a_fixups[] = {
256         [AD1986A_FIXUP_INV_JACK_DETECT] = {
257                 .type = HDA_FIXUP_FUNC,
258                 .v.func = ad_fixup_inv_jack_detect,
259         },
260         [AD1986A_FIXUP_ULTRA] = {
261                 .type = HDA_FIXUP_PINS,
262                 .v.pins = (const struct hda_pintbl[]) {
263                         { 0x1b, 0x90170110 }, /* speaker */
264                         { 0x1d, 0x90a7013e }, /* int mic */
265                         {}
266                 },
267         },
268         [AD1986A_FIXUP_SAMSUNG] = {
269                 .type = HDA_FIXUP_PINS,
270                 .v.pins = (const struct hda_pintbl[]) {
271                         { 0x1b, 0x90170110 }, /* speaker */
272                         { 0x1d, 0x90a7013e }, /* int mic */
273                         { 0x20, 0x411111f0 }, /* N/A */
274                         { 0x24, 0x411111f0 }, /* N/A */
275                         {}
276                 },
277         },
278         [AD1986A_FIXUP_3STACK] = {
279                 .type = HDA_FIXUP_PINS,
280                 .v.pins = (const struct hda_pintbl[]) {
281                         { 0x1a, 0x02214021 }, /* headphone */
282                         { 0x1b, 0x01014011 }, /* front */
283                         { 0x1c, 0x01013012 }, /* surround */
284                         { 0x1d, 0x01019015 }, /* clfe */
285                         { 0x1e, 0x411111f0 }, /* N/A */
286                         { 0x1f, 0x02a190f0 }, /* mic */
287                         { 0x20, 0x018130f0 }, /* line-in */
288                         {}
289                 },
290         },
291         [AD1986A_FIXUP_LAPTOP] = {
292                 .type = HDA_FIXUP_PINS,
293                 .v.pins = (const struct hda_pintbl[]) {
294                         { 0x1a, 0x02214021 }, /* headphone */
295                         { 0x1b, 0x90170110 }, /* speaker */
296                         { 0x1c, 0x411111f0 }, /* N/A */
297                         { 0x1d, 0x411111f0 }, /* N/A */
298                         { 0x1e, 0x411111f0 }, /* N/A */
299                         { 0x1f, 0x02a191f0 }, /* mic */
300                         { 0x20, 0x411111f0 }, /* N/A */
301                         {}
302                 },
303         },
304         [AD1986A_FIXUP_LAPTOP_IMIC] = {
305                 .type = HDA_FIXUP_PINS,
306                 .v.pins = (const struct hda_pintbl[]) {
307                         { 0x1d, 0x90a7013e }, /* int mic */
308                         {}
309                 },
310                 .chained_before = 1,
311                 .chain_id = AD1986A_FIXUP_LAPTOP,
312         },
313 };
314
315 static const struct snd_pci_quirk ad1986a_fixup_tbl[] = {
316         SND_PCI_QUIRK(0x103c, 0x30af, "HP B2800", AD1986A_FIXUP_LAPTOP_IMIC),
317         SND_PCI_QUIRK_MASK(0x1043, 0xff00, 0x8100, "ASUS P5", AD1986A_FIXUP_3STACK),
318         SND_PCI_QUIRK_MASK(0x1043, 0xff00, 0x8200, "ASUS M2", AD1986A_FIXUP_3STACK),
319         SND_PCI_QUIRK(0x10de, 0xcb84, "ASUS A8N-VM", AD1986A_FIXUP_3STACK),
320         SND_PCI_QUIRK(0x144d, 0xc01e, "FSC V2060", AD1986A_FIXUP_LAPTOP),
321         SND_PCI_QUIRK_MASK(0x144d, 0xff00, 0xc000, "Samsung", AD1986A_FIXUP_SAMSUNG),
322         SND_PCI_QUIRK(0x144d, 0xc027, "Samsung Q1", AD1986A_FIXUP_ULTRA),
323         SND_PCI_QUIRK(0x17aa, 0x2066, "Lenovo N100", AD1986A_FIXUP_INV_JACK_DETECT),
324         SND_PCI_QUIRK(0x17aa, 0x1011, "Lenovo M55", AD1986A_FIXUP_3STACK),
325         SND_PCI_QUIRK(0x17aa, 0x1017, "Lenovo A60", AD1986A_FIXUP_3STACK),
326         {}
327 };
328
329 static const struct hda_model_fixup ad1986a_fixup_models[] = {
330         { .id = AD1986A_FIXUP_3STACK, .name = "3stack" },
331         { .id = AD1986A_FIXUP_LAPTOP, .name = "laptop" },
332         { .id = AD1986A_FIXUP_LAPTOP_IMIC, .name = "laptop-imic" },
333         { .id = AD1986A_FIXUP_LAPTOP_IMIC, .name = "laptop-eapd" }, /* alias */
334         {}
335 };
336
337 /*
338  */
339 static int patch_ad1986a(struct hda_codec *codec)
340 {
341         int err;
342         struct ad198x_spec *spec;
343
344         err = alloc_ad_spec(codec);
345         if (err < 0)
346                 return err;
347         spec = codec->spec;
348
349         /* AD1986A has the inverted EAPD implementation */
350         codec->inv_eapd = 1;
351
352         spec->gen.mixer_nid = 0x07;
353         spec->gen.beep_nid = 0x19;
354         set_beep_amp(spec, 0x18, 0, HDA_OUTPUT);
355
356         /* AD1986A has a hardware problem that it can't share a stream
357          * with multiple output pins.  The copy of front to surrounds
358          * causes noisy or silent outputs at a certain timing, e.g.
359          * changing the volume.
360          * So, let's disable the shared stream.
361          */
362         spec->gen.multiout.no_share_stream = 1;
363
364         /* AD1986A can't manage the dynamic pin on/off smoothly */
365         spec->gen.auto_mute_via_amp = 1;
366
367         snd_hda_pick_fixup(codec, ad1986a_fixup_models, ad1986a_fixup_tbl,
368                            ad1986a_fixups);
369         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
370
371         err = ad198x_parse_auto_config(codec);
372         if (err < 0) {
373                 snd_hda_gen_free(codec);
374                 return err;
375         }
376
377         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
378
379         return 0;
380 }
381
382
383 /*
384  * AD1983 specific
385  */
386
387 /*
388  * SPDIF mux control for AD1983 auto-parser
389  */
390 static int ad1983_auto_smux_enum_info(struct snd_kcontrol *kcontrol,
391                                       struct snd_ctl_elem_info *uinfo)
392 {
393         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
394         struct ad198x_spec *spec = codec->spec;
395         static const char * const texts2[] = { "PCM", "ADC" };
396         static const char * const texts3[] = { "PCM", "ADC1", "ADC2" };
397         hda_nid_t dig_out = spec->gen.multiout.dig_out_nid;
398         int num_conns = snd_hda_get_num_conns(codec, dig_out);
399
400         if (num_conns == 2)
401                 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts2);
402         else if (num_conns == 3)
403                 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
404         else
405                 return -EINVAL;
406 }
407
408 static int ad1983_auto_smux_enum_get(struct snd_kcontrol *kcontrol,
409                                      struct snd_ctl_elem_value *ucontrol)
410 {
411         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
412         struct ad198x_spec *spec = codec->spec;
413
414         ucontrol->value.enumerated.item[0] = spec->cur_smux;
415         return 0;
416 }
417
418 static int ad1983_auto_smux_enum_put(struct snd_kcontrol *kcontrol,
419                                      struct snd_ctl_elem_value *ucontrol)
420 {
421         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
422         struct ad198x_spec *spec = codec->spec;
423         unsigned int val = ucontrol->value.enumerated.item[0];
424         hda_nid_t dig_out = spec->gen.multiout.dig_out_nid;
425         int num_conns = snd_hda_get_num_conns(codec, dig_out);
426
427         if (val >= num_conns)
428                 return -EINVAL;
429         if (spec->cur_smux == val)
430                 return 0;
431         spec->cur_smux = val;
432         snd_hda_codec_write_cache(codec, dig_out, 0,
433                                   AC_VERB_SET_CONNECT_SEL, val);
434         return 1;
435 }
436
437 static struct snd_kcontrol_new ad1983_auto_smux_mixer = {
438         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
439         .name = "IEC958 Playback Source",
440         .info = ad1983_auto_smux_enum_info,
441         .get = ad1983_auto_smux_enum_get,
442         .put = ad1983_auto_smux_enum_put,
443 };
444
445 static int ad1983_add_spdif_mux_ctl(struct hda_codec *codec)
446 {
447         struct ad198x_spec *spec = codec->spec;
448         hda_nid_t dig_out = spec->gen.multiout.dig_out_nid;
449         int num_conns;
450
451         if (!dig_out)
452                 return 0;
453         num_conns = snd_hda_get_num_conns(codec, dig_out);
454         if (num_conns != 2 && num_conns != 3)
455                 return 0;
456         if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &ad1983_auto_smux_mixer))
457                 return -ENOMEM;
458         return 0;
459 }
460
461 static int patch_ad1983(struct hda_codec *codec)
462 {
463         struct ad198x_spec *spec;
464         int err;
465
466         err = alloc_ad_spec(codec);
467         if (err < 0)
468                 return err;
469         spec = codec->spec;
470
471         spec->gen.beep_nid = 0x10;
472         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
473         err = ad198x_parse_auto_config(codec);
474         if (err < 0)
475                 goto error;
476         err = ad1983_add_spdif_mux_ctl(codec);
477         if (err < 0)
478                 goto error;
479         return 0;
480
481  error:
482         snd_hda_gen_free(codec);
483         return err;
484 }
485
486
487 /*
488  * AD1981 HD specific
489  */
490
491 static void ad1981_fixup_hp_eapd(struct hda_codec *codec,
492                                  const struct hda_fixup *fix, int action)
493 {
494         struct ad198x_spec *spec = codec->spec;
495
496         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
497                 spec->gen.vmaster_mute.hook = ad_vmaster_eapd_hook;
498                 spec->eapd_nid = 0x05;
499         }
500 }
501
502 /* set the upper-limit for mixer amp to 0dB for avoiding the possible
503  * damage by overloading
504  */
505 static void ad1981_fixup_amp_override(struct hda_codec *codec,
506                                       const struct hda_fixup *fix, int action)
507 {
508         if (action == HDA_FIXUP_ACT_PRE_PROBE)
509                 snd_hda_override_amp_caps(codec, 0x11, HDA_INPUT,
510                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
511                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
512                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
513                                           (1 << AC_AMPCAP_MUTE_SHIFT));
514 }
515
516 enum {
517         AD1981_FIXUP_AMP_OVERRIDE,
518         AD1981_FIXUP_HP_EAPD,
519 };
520
521 static const struct hda_fixup ad1981_fixups[] = {
522         [AD1981_FIXUP_AMP_OVERRIDE] = {
523                 .type = HDA_FIXUP_FUNC,
524                 .v.func = ad1981_fixup_amp_override,
525         },
526         [AD1981_FIXUP_HP_EAPD] = {
527                 .type = HDA_FIXUP_FUNC,
528                 .v.func = ad1981_fixup_hp_eapd,
529                 .chained = true,
530                 .chain_id = AD1981_FIXUP_AMP_OVERRIDE,
531         },
532 };
533
534 static const struct snd_pci_quirk ad1981_fixup_tbl[] = {
535         SND_PCI_QUIRK_VENDOR(0x1014, "Lenovo", AD1981_FIXUP_AMP_OVERRIDE),
536         SND_PCI_QUIRK_VENDOR(0x103c, "HP", AD1981_FIXUP_HP_EAPD),
537         SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo", AD1981_FIXUP_AMP_OVERRIDE),
538         /* HP nx6320 (reversed SSID, H/W bug) */
539         SND_PCI_QUIRK(0x30b0, 0x103c, "HP nx6320", AD1981_FIXUP_HP_EAPD),
540         {}
541 };
542
543 static int patch_ad1981(struct hda_codec *codec)
544 {
545         struct ad198x_spec *spec;
546         int err;
547
548         err = alloc_ad_spec(codec);
549         if (err < 0)
550                 return -ENOMEM;
551         spec = codec->spec;
552
553         spec->gen.mixer_nid = 0x0e;
554         spec->gen.beep_nid = 0x10;
555         set_beep_amp(spec, 0x0d, 0, HDA_OUTPUT);
556
557         snd_hda_pick_fixup(codec, NULL, ad1981_fixup_tbl, ad1981_fixups);
558         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
559
560         err = ad198x_parse_auto_config(codec);
561         if (err < 0)
562                 goto error;
563         err = ad1983_add_spdif_mux_ctl(codec);
564         if (err < 0)
565                 goto error;
566
567         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
568
569         return 0;
570
571  error:
572         snd_hda_gen_free(codec);
573         return err;
574 }
575
576
577 /*
578  * AD1988
579  *
580  * Output pins and routes
581  *
582  *        Pin               Mix     Sel     DAC (*)
583  * port-A 0x11 (mute/hp) <- 0x22 <- 0x37 <- 03/04/06
584  * port-B 0x14 (mute/hp) <- 0x2b <- 0x30 <- 03/04/06
585  * port-C 0x15 (mute)    <- 0x2c <- 0x31 <- 05/0a
586  * port-D 0x12 (mute/hp) <- 0x29         <- 04
587  * port-E 0x17 (mute/hp) <- 0x26 <- 0x32 <- 05/0a
588  * port-F 0x16 (mute)    <- 0x2a         <- 06
589  * port-G 0x24 (mute)    <- 0x27         <- 05
590  * port-H 0x25 (mute)    <- 0x28         <- 0a
591  * mono   0x13 (mute/amp)<- 0x1e <- 0x36 <- 03/04/06
592  *
593  * DAC0 = 03h, DAC1 = 04h, DAC2 = 05h, DAC3 = 06h, DAC4 = 0ah
594  * (*) DAC2/3/4 are swapped to DAC3/4/2 on AD198A rev.2 due to a h/w bug.
595  *
596  * Input pins and routes
597  *
598  *        pin     boost   mix input # / adc input #
599  * port-A 0x11 -> 0x38 -> mix 2, ADC 0
600  * port-B 0x14 -> 0x39 -> mix 0, ADC 1
601  * port-C 0x15 -> 0x3a -> 33:0 - mix 1, ADC 2
602  * port-D 0x12 -> 0x3d -> mix 3, ADC 8
603  * port-E 0x17 -> 0x3c -> 34:0 - mix 4, ADC 4
604  * port-F 0x16 -> 0x3b -> mix 5, ADC 3
605  * port-G 0x24 -> N/A  -> 33:1 - mix 1, 34:1 - mix 4, ADC 6
606  * port-H 0x25 -> N/A  -> 33:2 - mix 1, 34:2 - mix 4, ADC 7
607  *
608  *
609  * DAC assignment
610  *   6stack - front/surr/CLFE/side/opt DACs - 04/06/05/0a/03
611  *   3stack - front/surr/CLFE/opt DACs - 04/05/0a/03
612  *
613  * Inputs of Analog Mix (0x20)
614  *   0:Port-B (front mic)
615  *   1:Port-C/G/H (line-in)
616  *   2:Port-A
617  *   3:Port-D (line-in/2)
618  *   4:Port-E/G/H (mic-in)
619  *   5:Port-F (mic2-in)
620  *   6:CD
621  *   7:Beep
622  *
623  * ADC selection
624  *   0:Port-A
625  *   1:Port-B (front mic-in)
626  *   2:Port-C (line-in)
627  *   3:Port-F (mic2-in)
628  *   4:Port-E (mic-in)
629  *   5:CD
630  *   6:Port-G
631  *   7:Port-H
632  *   8:Port-D (line-in/2)
633  *   9:Mix
634  *
635  * Proposed pin assignments by the datasheet
636  *
637  * 6-stack
638  * Port-A front headphone
639  *      B front mic-in
640  *      C rear line-in
641  *      D rear front-out
642  *      E rear mic-in
643  *      F rear surround
644  *      G rear CLFE
645  *      H rear side
646  *
647  * 3-stack
648  * Port-A front headphone
649  *      B front mic
650  *      C rear line-in/surround
651  *      D rear front-out
652  *      E rear mic-in/CLFE
653  *
654  * laptop
655  * Port-A headphone
656  *      B mic-in
657  *      C docking station
658  *      D internal speaker (with EAPD)
659  *      E/F quad mic array
660  */
661
662 #ifdef ENABLE_AD_STATIC_QUIRKS
663 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
664                                struct snd_ctl_elem_info *uinfo)
665 {
666         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
667         struct ad198x_spec *spec = codec->spec;
668         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
669                                     spec->num_channel_mode);
670 }
671
672 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
673                               struct snd_ctl_elem_value *ucontrol)
674 {
675         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
676         struct ad198x_spec *spec = codec->spec;
677         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
678                                    spec->num_channel_mode, spec->multiout.max_channels);
679 }
680
681 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
682                               struct snd_ctl_elem_value *ucontrol)
683 {
684         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
685         struct ad198x_spec *spec = codec->spec;
686         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
687                                       spec->num_channel_mode,
688                                       &spec->multiout.max_channels);
689         if (err >= 0 && spec->need_dac_fix)
690                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
691         return err;
692 }
693 #endif /* ENABLE_AD_STATIC_QUIRKS */
694
695 static int ad1988_auto_smux_enum_info(struct snd_kcontrol *kcontrol,
696                                       struct snd_ctl_elem_info *uinfo)
697 {
698         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
699         static const char * const texts[] = {
700                 "PCM", "ADC1", "ADC2", "ADC3",
701         };
702         int num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1;
703         if (num_conns > 4)
704                 num_conns = 4;
705         return snd_hda_enum_helper_info(kcontrol, uinfo, num_conns, texts);
706 }
707
708 static int ad1988_auto_smux_enum_get(struct snd_kcontrol *kcontrol,
709                                      struct snd_ctl_elem_value *ucontrol)
710 {
711         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
712         struct ad198x_spec *spec = codec->spec;
713
714         ucontrol->value.enumerated.item[0] = spec->cur_smux;
715         return 0;
716 }
717
718 static int ad1988_auto_smux_enum_put(struct snd_kcontrol *kcontrol,
719                                      struct snd_ctl_elem_value *ucontrol)
720 {
721         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
722         struct ad198x_spec *spec = codec->spec;
723         unsigned int val = ucontrol->value.enumerated.item[0];
724         struct nid_path *path;
725         int num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1;
726
727         if (val >= num_conns)
728                 return -EINVAL;
729         if (spec->cur_smux == val)
730                 return 0;
731
732         mutex_lock(&codec->control_mutex);
733         codec->cached_write = 1;
734         path = snd_hda_get_path_from_idx(codec,
735                                          spec->smux_paths[spec->cur_smux]);
736         if (path)
737                 snd_hda_activate_path(codec, path, false, true);
738         path = snd_hda_get_path_from_idx(codec, spec->smux_paths[val]);
739         if (path)
740                 snd_hda_activate_path(codec, path, true, true);
741         spec->cur_smux = val;
742         codec->cached_write = 0;
743         mutex_unlock(&codec->control_mutex);
744         snd_hda_codec_flush_cache(codec); /* flush the updates */
745         return 1;
746 }
747
748 static struct snd_kcontrol_new ad1988_auto_smux_mixer = {
749         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
750         .name = "IEC958 Playback Source",
751         .info = ad1988_auto_smux_enum_info,
752         .get = ad1988_auto_smux_enum_get,
753         .put = ad1988_auto_smux_enum_put,
754 };
755
756 static int ad1988_auto_init(struct hda_codec *codec)
757 {
758         struct ad198x_spec *spec = codec->spec;
759         int i, err;
760
761         err = snd_hda_gen_init(codec);
762         if (err < 0)
763                 return err;
764         if (!spec->gen.autocfg.dig_outs)
765                 return 0;
766
767         for (i = 0; i < 4; i++) {
768                 struct nid_path *path;
769                 path = snd_hda_get_path_from_idx(codec, spec->smux_paths[i]);
770                 if (path)
771                         snd_hda_activate_path(codec, path, path->active, false);
772         }
773
774         return 0;
775 }
776
777 static int ad1988_add_spdif_mux_ctl(struct hda_codec *codec)
778 {
779         struct ad198x_spec *spec = codec->spec;
780         int i, num_conns;
781         /* we create four static faked paths, since AD codecs have odd
782          * widget connections regarding the SPDIF out source
783          */
784         static struct nid_path fake_paths[4] = {
785                 {
786                         .depth = 3,
787                         .path = { 0x02, 0x1d, 0x1b },
788                         .idx = { 0, 0, 0 },
789                         .multi = { 0, 0, 0 },
790                 },
791                 {
792                         .depth = 4,
793                         .path = { 0x08, 0x0b, 0x1d, 0x1b },
794                         .idx = { 0, 0, 1, 0 },
795                         .multi = { 0, 1, 0, 0 },
796                 },
797                 {
798                         .depth = 4,
799                         .path = { 0x09, 0x0b, 0x1d, 0x1b },
800                         .idx = { 0, 1, 1, 0 },
801                         .multi = { 0, 1, 0, 0 },
802                 },
803                 {
804                         .depth = 4,
805                         .path = { 0x0f, 0x0b, 0x1d, 0x1b },
806                         .idx = { 0, 2, 1, 0 },
807                         .multi = { 0, 1, 0, 0 },
808                 },
809         };
810
811         /* SPDIF source mux appears to be present only on AD1988A */
812         if (!spec->gen.autocfg.dig_outs ||
813             get_wcaps_type(get_wcaps(codec, 0x1d)) != AC_WID_AUD_MIX)
814                 return 0;
815
816         num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1;
817         if (num_conns != 3 && num_conns != 4)
818                 return 0;
819
820         for (i = 0; i < num_conns; i++) {
821                 struct nid_path *path = snd_array_new(&spec->gen.paths);
822                 if (!path)
823                         return -ENOMEM;
824                 *path = fake_paths[i];
825                 if (!i)
826                         path->active = 1;
827                 spec->smux_paths[i] = snd_hda_get_path_idx(codec, path);
828         }
829
830         if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &ad1988_auto_smux_mixer))
831                 return -ENOMEM;
832
833         codec->patch_ops.init = ad1988_auto_init;
834
835         return 0;
836 }
837
838 /*
839  */
840
841 enum {
842         AD1988_FIXUP_6STACK_DIG,
843 };
844
845 static const struct hda_fixup ad1988_fixups[] = {
846         [AD1988_FIXUP_6STACK_DIG] = {
847                 .type = HDA_FIXUP_PINS,
848                 .v.pins = (const struct hda_pintbl[]) {
849                         { 0x11, 0x02214130 }, /* front-hp */
850                         { 0x12, 0x01014010 }, /* line-out */
851                         { 0x14, 0x02a19122 }, /* front-mic */
852                         { 0x15, 0x01813021 }, /* line-in */
853                         { 0x16, 0x01011012 }, /* line-out */
854                         { 0x17, 0x01a19020 }, /* mic */
855                         { 0x1b, 0x0145f1f0 }, /* SPDIF */
856                         { 0x24, 0x01016011 }, /* line-out */
857                         { 0x25, 0x01012013 }, /* line-out */
858                         { }
859                 }
860         },
861 };
862
863 static const struct hda_model_fixup ad1988_fixup_models[] = {
864         { .id = AD1988_FIXUP_6STACK_DIG, .name = "6stack-dig" },
865         {}
866 };
867
868 static int patch_ad1988(struct hda_codec *codec)
869 {
870         struct ad198x_spec *spec;
871         int err;
872
873         err = alloc_ad_spec(codec);
874         if (err < 0)
875                 return err;
876         spec = codec->spec;
877
878         spec->gen.mixer_nid = 0x20;
879         spec->gen.mixer_merge_nid = 0x21;
880         spec->gen.beep_nid = 0x10;
881         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
882
883         snd_hda_pick_fixup(codec, ad1988_fixup_models, NULL, ad1988_fixups);
884         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
885
886         err = ad198x_parse_auto_config(codec);
887         if (err < 0)
888                 goto error;
889         err = ad1988_add_spdif_mux_ctl(codec);
890         if (err < 0)
891                 goto error;
892
893         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
894
895         return 0;
896
897  error:
898         snd_hda_gen_free(codec);
899         return err;
900 }
901
902
903 /*
904  * AD1884 / AD1984
905  *
906  * port-B - front line/mic-in
907  * port-E - aux in/out
908  * port-F - aux in/out
909  * port-C - rear line/mic-in
910  * port-D - rear line/hp-out
911  * port-A - front line/hp-out
912  *
913  * AD1984 = AD1884 + two digital mic-ins
914  *
915  * AD1883 / AD1884A / AD1984A / AD1984B
916  *
917  * port-B (0x14) - front mic-in
918  * port-E (0x1c) - rear mic-in
919  * port-F (0x16) - CD / ext out
920  * port-C (0x15) - rear line-in
921  * port-D (0x12) - rear line-out
922  * port-A (0x11) - front hp-out
923  *
924  * AD1984A = AD1884A + digital-mic
925  * AD1883 = equivalent with AD1984A
926  * AD1984B = AD1984A + extra SPDIF-out
927  */
928
929 /* set the upper-limit for mixer amp to 0dB for avoiding the possible
930  * damage by overloading
931  */
932 static void ad1884_fixup_amp_override(struct hda_codec *codec,
933                                       const struct hda_fixup *fix, int action)
934 {
935         if (action == HDA_FIXUP_ACT_PRE_PROBE)
936                 snd_hda_override_amp_caps(codec, 0x20, HDA_INPUT,
937                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
938                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
939                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
940                                           (1 << AC_AMPCAP_MUTE_SHIFT));
941 }
942
943 /* toggle GPIO1 according to the mute state */
944 static void ad1884_vmaster_hp_gpio_hook(void *private_data, int enabled)
945 {
946         struct hda_codec *codec = private_data;
947         struct ad198x_spec *spec = codec->spec;
948
949         if (spec->eapd_nid)
950                 ad_vmaster_eapd_hook(private_data, enabled);
951         snd_hda_codec_update_cache(codec, 0x01, 0,
952                                    AC_VERB_SET_GPIO_DATA,
953                                    enabled ? 0x00 : 0x02);
954 }
955
956 static void ad1884_fixup_hp_eapd(struct hda_codec *codec,
957                                  const struct hda_fixup *fix, int action)
958 {
959         struct ad198x_spec *spec = codec->spec;
960         static const struct hda_verb gpio_init_verbs[] = {
961                 {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
962                 {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
963                 {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
964                 {},
965         };
966
967         switch (action) {
968         case HDA_FIXUP_ACT_PRE_PROBE:
969                 spec->gen.vmaster_mute.hook = ad1884_vmaster_hp_gpio_hook;
970                 spec->gen.own_eapd_ctl = 1;
971                 snd_hda_sequence_write_cache(codec, gpio_init_verbs);
972                 break;
973         case HDA_FIXUP_ACT_PROBE:
974                 if (spec->gen.autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
975                         spec->eapd_nid = spec->gen.autocfg.line_out_pins[0];
976                 else
977                         spec->eapd_nid = spec->gen.autocfg.speaker_pins[0];
978                 break;
979         }
980 }
981
982 static void ad1884_fixup_thinkpad(struct hda_codec *codec,
983                                   const struct hda_fixup *fix, int action)
984 {
985         struct ad198x_spec *spec = codec->spec;
986
987         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
988                 spec->gen.keep_eapd_on = 1;
989                 spec->gen.vmaster_mute.hook = ad_vmaster_eapd_hook;
990                 spec->eapd_nid = 0x12;
991         }
992 }
993
994 /* set magic COEFs for dmic */
995 static const struct hda_verb ad1884_dmic_init_verbs[] = {
996         {0x01, AC_VERB_SET_COEF_INDEX, 0x13f7},
997         {0x01, AC_VERB_SET_PROC_COEF, 0x08},
998         {}
999 };
1000
1001 enum {
1002         AD1884_FIXUP_AMP_OVERRIDE,
1003         AD1884_FIXUP_HP_EAPD,
1004         AD1884_FIXUP_DMIC_COEF,
1005         AD1884_FIXUP_THINKPAD,
1006         AD1884_FIXUP_HP_TOUCHSMART,
1007 };
1008
1009 static const struct hda_fixup ad1884_fixups[] = {
1010         [AD1884_FIXUP_AMP_OVERRIDE] = {
1011                 .type = HDA_FIXUP_FUNC,
1012                 .v.func = ad1884_fixup_amp_override,
1013         },
1014         [AD1884_FIXUP_HP_EAPD] = {
1015                 .type = HDA_FIXUP_FUNC,
1016                 .v.func = ad1884_fixup_hp_eapd,
1017                 .chained = true,
1018                 .chain_id = AD1884_FIXUP_AMP_OVERRIDE,
1019         },
1020         [AD1884_FIXUP_DMIC_COEF] = {
1021                 .type = HDA_FIXUP_VERBS,
1022                 .v.verbs = ad1884_dmic_init_verbs,
1023         },
1024         [AD1884_FIXUP_THINKPAD] = {
1025                 .type = HDA_FIXUP_FUNC,
1026                 .v.func = ad1884_fixup_thinkpad,
1027                 .chained = true,
1028                 .chain_id = AD1884_FIXUP_DMIC_COEF,
1029         },
1030         [AD1884_FIXUP_HP_TOUCHSMART] = {
1031                 .type = HDA_FIXUP_VERBS,
1032                 .v.verbs = ad1884_dmic_init_verbs,
1033                 .chained = true,
1034                 .chain_id = AD1884_FIXUP_HP_EAPD,
1035         },
1036 };
1037
1038 static const struct snd_pci_quirk ad1884_fixup_tbl[] = {
1039         SND_PCI_QUIRK(0x103c, 0x2a82, "HP Touchsmart", AD1884_FIXUP_HP_TOUCHSMART),
1040         SND_PCI_QUIRK_VENDOR(0x103c, "HP", AD1884_FIXUP_HP_EAPD),
1041         SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo Thinkpad", AD1884_FIXUP_THINKPAD),
1042         {}
1043 };
1044
1045
1046 static int patch_ad1884(struct hda_codec *codec)
1047 {
1048         struct ad198x_spec *spec;
1049         int err;
1050
1051         err = alloc_ad_spec(codec);
1052         if (err < 0)
1053                 return err;
1054         spec = codec->spec;
1055
1056         spec->gen.mixer_nid = 0x20;
1057         spec->gen.beep_nid = 0x10;
1058         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
1059
1060         snd_hda_pick_fixup(codec, NULL, ad1884_fixup_tbl, ad1884_fixups);
1061         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1062
1063         err = ad198x_parse_auto_config(codec);
1064         if (err < 0)
1065                 goto error;
1066         err = ad1983_add_spdif_mux_ctl(codec);
1067         if (err < 0)
1068                 goto error;
1069
1070         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1071
1072         return 0;
1073
1074  error:
1075         snd_hda_gen_free(codec);
1076         return err;
1077 }
1078
1079 /*
1080  * AD1882 / AD1882A
1081  *
1082  * port-A - front hp-out
1083  * port-B - front mic-in
1084  * port-C - rear line-in, shared surr-out (3stack)
1085  * port-D - rear line-out
1086  * port-E - rear mic-in, shared clfe-out (3stack)
1087  * port-F - rear surr-out (6stack)
1088  * port-G - rear clfe-out (6stack)
1089  */
1090
1091 static int patch_ad1882(struct hda_codec *codec)
1092 {
1093         struct ad198x_spec *spec;
1094         int err;
1095
1096         err = alloc_ad_spec(codec);
1097         if (err < 0)
1098                 return err;
1099         spec = codec->spec;
1100
1101         spec->gen.mixer_nid = 0x20;
1102         spec->gen.mixer_merge_nid = 0x21;
1103         spec->gen.beep_nid = 0x10;
1104         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
1105         err = ad198x_parse_auto_config(codec);
1106         if (err < 0)
1107                 goto error;
1108         err = ad1988_add_spdif_mux_ctl(codec);
1109         if (err < 0)
1110                 goto error;
1111         return 0;
1112
1113  error:
1114         snd_hda_gen_free(codec);
1115         return err;
1116 }
1117
1118
1119 /*
1120  * patch entries
1121  */
1122 static const struct hda_codec_preset snd_hda_preset_analog[] = {
1123         { .id = 0x11d4184a, .name = "AD1884A", .patch = patch_ad1884 },
1124         { .id = 0x11d41882, .name = "AD1882", .patch = patch_ad1882 },
1125         { .id = 0x11d41883, .name = "AD1883", .patch = patch_ad1884 },
1126         { .id = 0x11d41884, .name = "AD1884", .patch = patch_ad1884 },
1127         { .id = 0x11d4194a, .name = "AD1984A", .patch = patch_ad1884 },
1128         { .id = 0x11d4194b, .name = "AD1984B", .patch = patch_ad1884 },
1129         { .id = 0x11d41981, .name = "AD1981", .patch = patch_ad1981 },
1130         { .id = 0x11d41983, .name = "AD1983", .patch = patch_ad1983 },
1131         { .id = 0x11d41984, .name = "AD1984", .patch = patch_ad1884 },
1132         { .id = 0x11d41986, .name = "AD1986A", .patch = patch_ad1986a },
1133         { .id = 0x11d41988, .name = "AD1988", .patch = patch_ad1988 },
1134         { .id = 0x11d4198b, .name = "AD1988B", .patch = patch_ad1988 },
1135         { .id = 0x11d4882a, .name = "AD1882A", .patch = patch_ad1882 },
1136         { .id = 0x11d4989a, .name = "AD1989A", .patch = patch_ad1988 },
1137         { .id = 0x11d4989b, .name = "AD1989B", .patch = patch_ad1988 },
1138         {} /* terminator */
1139 };
1140
1141 MODULE_ALIAS("snd-hda-codec-id:11d4*");
1142
1143 MODULE_LICENSE("GPL");
1144 MODULE_DESCRIPTION("Analog Devices HD-audio codec");
1145
1146 static struct hda_codec_preset_list analog_list = {
1147         .preset = snd_hda_preset_analog,
1148         .owner = THIS_MODULE,
1149 };
1150
1151 static int __init patch_analog_init(void)
1152 {
1153         return snd_hda_add_codec_preset(&analog_list);
1154 }
1155
1156 static void __exit patch_analog_exit(void)
1157 {
1158         snd_hda_delete_codec_preset(&analog_list);
1159 }
1160
1161 module_init(patch_analog_init)
1162 module_exit(patch_analog_exit)