50b2427f19ca8cc86356a1bfabb13ac5cd9baa76
[cascardo/linux.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, bool indep_hp)
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 = indep_hp;
199         spec->gen.add_stereo_mix_input = 1;
200
201         err = snd_hda_parse_pin_defcfg(codec, cfg, NULL, 0);
202         if (err < 0)
203                 return err;
204         err = snd_hda_gen_parse_auto_config(codec, cfg);
205         if (err < 0)
206                 return err;
207
208         codec->patch_ops = ad198x_auto_patch_ops;
209
210         return 0;
211 }
212
213 /*
214  * AD1986A specific
215  */
216
217 static int alloc_ad_spec(struct hda_codec *codec)
218 {
219         struct ad198x_spec *spec;
220
221         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
222         if (!spec)
223                 return -ENOMEM;
224         codec->spec = spec;
225         snd_hda_gen_spec_init(&spec->gen);
226         return 0;
227 }
228
229 /*
230  * AD1986A fixup codes
231  */
232
233 /* Lenovo N100 seems to report the reversed bit for HP jack-sensing */
234 static void ad_fixup_inv_jack_detect(struct hda_codec *codec,
235                                      const struct hda_fixup *fix, int action)
236 {
237         struct ad198x_spec *spec = codec->spec;
238
239         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
240                 codec->inv_jack_detect = 1;
241                 spec->gen.keep_eapd_on = 1;
242                 spec->gen.vmaster_mute.hook = ad_vmaster_eapd_hook;
243                 spec->eapd_nid = 0x1b;
244         }
245 }
246
247 /* Toshiba Satellite L40 implements EAPD in a standard way unlike others */
248 static void ad1986a_fixup_eapd(struct hda_codec *codec,
249                                const struct hda_fixup *fix, int action)
250 {
251         struct ad198x_spec *spec = codec->spec;
252
253         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
254                 codec->inv_eapd = 0;
255                 spec->gen.keep_eapd_on = 1;
256                 spec->eapd_nid = 0x1b;
257         }
258 }
259
260 enum {
261         AD1986A_FIXUP_INV_JACK_DETECT,
262         AD1986A_FIXUP_ULTRA,
263         AD1986A_FIXUP_SAMSUNG,
264         AD1986A_FIXUP_3STACK,
265         AD1986A_FIXUP_LAPTOP,
266         AD1986A_FIXUP_LAPTOP_IMIC,
267         AD1986A_FIXUP_EAPD,
268 };
269
270 static const struct hda_fixup ad1986a_fixups[] = {
271         [AD1986A_FIXUP_INV_JACK_DETECT] = {
272                 .type = HDA_FIXUP_FUNC,
273                 .v.func = ad_fixup_inv_jack_detect,
274         },
275         [AD1986A_FIXUP_ULTRA] = {
276                 .type = HDA_FIXUP_PINS,
277                 .v.pins = (const struct hda_pintbl[]) {
278                         { 0x1b, 0x90170110 }, /* speaker */
279                         { 0x1d, 0x90a7013e }, /* int mic */
280                         {}
281                 },
282         },
283         [AD1986A_FIXUP_SAMSUNG] = {
284                 .type = HDA_FIXUP_PINS,
285                 .v.pins = (const struct hda_pintbl[]) {
286                         { 0x1b, 0x90170110 }, /* speaker */
287                         { 0x1d, 0x90a7013e }, /* int mic */
288                         { 0x20, 0x411111f0 }, /* N/A */
289                         { 0x24, 0x411111f0 }, /* N/A */
290                         {}
291                 },
292         },
293         [AD1986A_FIXUP_3STACK] = {
294                 .type = HDA_FIXUP_PINS,
295                 .v.pins = (const struct hda_pintbl[]) {
296                         { 0x1a, 0x02214021 }, /* headphone */
297                         { 0x1b, 0x01014011 }, /* front */
298                         { 0x1c, 0x01813030 }, /* line-in */
299                         { 0x1d, 0x01a19020 }, /* rear mic */
300                         { 0x1e, 0x411111f0 }, /* N/A */
301                         { 0x1f, 0x02a190f0 }, /* mic */
302                         { 0x20, 0x411111f0 }, /* N/A */
303                         {}
304                 },
305         },
306         [AD1986A_FIXUP_LAPTOP] = {
307                 .type = HDA_FIXUP_PINS,
308                 .v.pins = (const struct hda_pintbl[]) {
309                         { 0x1a, 0x02214021 }, /* headphone */
310                         { 0x1b, 0x90170110 }, /* speaker */
311                         { 0x1c, 0x411111f0 }, /* N/A */
312                         { 0x1d, 0x411111f0 }, /* N/A */
313                         { 0x1e, 0x411111f0 }, /* N/A */
314                         { 0x1f, 0x02a191f0 }, /* mic */
315                         { 0x20, 0x411111f0 }, /* N/A */
316                         {}
317                 },
318         },
319         [AD1986A_FIXUP_LAPTOP_IMIC] = {
320                 .type = HDA_FIXUP_PINS,
321                 .v.pins = (const struct hda_pintbl[]) {
322                         { 0x1d, 0x90a7013e }, /* int mic */
323                         {}
324                 },
325                 .chained_before = 1,
326                 .chain_id = AD1986A_FIXUP_LAPTOP,
327         },
328         [AD1986A_FIXUP_EAPD] = {
329                 .type = HDA_FIXUP_FUNC,
330                 .v.func = ad1986a_fixup_eapd,
331         },
332 };
333
334 static const struct snd_pci_quirk ad1986a_fixup_tbl[] = {
335         SND_PCI_QUIRK(0x103c, 0x30af, "HP B2800", AD1986A_FIXUP_LAPTOP_IMIC),
336         SND_PCI_QUIRK_MASK(0x1043, 0xff00, 0x8100, "ASUS P5", AD1986A_FIXUP_3STACK),
337         SND_PCI_QUIRK_MASK(0x1043, 0xff00, 0x8200, "ASUS M2", AD1986A_FIXUP_3STACK),
338         SND_PCI_QUIRK(0x10de, 0xcb84, "ASUS A8N-VM", AD1986A_FIXUP_3STACK),
339         SND_PCI_QUIRK(0x1179, 0xff40, "Toshiba Satellite L40", AD1986A_FIXUP_EAPD),
340         SND_PCI_QUIRK(0x144d, 0xc01e, "FSC V2060", AD1986A_FIXUP_LAPTOP),
341         SND_PCI_QUIRK_MASK(0x144d, 0xff00, 0xc000, "Samsung", AD1986A_FIXUP_SAMSUNG),
342         SND_PCI_QUIRK(0x144d, 0xc027, "Samsung Q1", AD1986A_FIXUP_ULTRA),
343         SND_PCI_QUIRK(0x17aa, 0x2066, "Lenovo N100", AD1986A_FIXUP_INV_JACK_DETECT),
344         SND_PCI_QUIRK(0x17aa, 0x1011, "Lenovo M55", AD1986A_FIXUP_3STACK),
345         SND_PCI_QUIRK(0x17aa, 0x1017, "Lenovo A60", AD1986A_FIXUP_3STACK),
346         {}
347 };
348
349 static const struct hda_model_fixup ad1986a_fixup_models[] = {
350         { .id = AD1986A_FIXUP_3STACK, .name = "3stack" },
351         { .id = AD1986A_FIXUP_LAPTOP, .name = "laptop" },
352         { .id = AD1986A_FIXUP_LAPTOP_IMIC, .name = "laptop-imic" },
353         { .id = AD1986A_FIXUP_LAPTOP_IMIC, .name = "laptop-eapd" }, /* alias */
354         {}
355 };
356
357 /*
358  */
359 static int patch_ad1986a(struct hda_codec *codec)
360 {
361         int err;
362         struct ad198x_spec *spec;
363         static hda_nid_t preferred_pairs[] = {
364                 0x1a, 0x03,
365                 0x1b, 0x03,
366                 0x1c, 0x04,
367                 0x1d, 0x05,
368                 0x1e, 0x03,
369                 0
370         };
371
372         err = alloc_ad_spec(codec);
373         if (err < 0)
374                 return err;
375         spec = codec->spec;
376
377         /* AD1986A has the inverted EAPD implementation */
378         codec->inv_eapd = 1;
379
380         spec->gen.mixer_nid = 0x07;
381         spec->gen.beep_nid = 0x19;
382         set_beep_amp(spec, 0x18, 0, HDA_OUTPUT);
383
384         /* AD1986A has a hardware problem that it can't share a stream
385          * with multiple output pins.  The copy of front to surrounds
386          * causes noisy or silent outputs at a certain timing, e.g.
387          * changing the volume.
388          * So, let's disable the shared stream.
389          */
390         spec->gen.multiout.no_share_stream = 1;
391         /* give fixed DAC/pin pairs */
392         spec->gen.preferred_dacs = preferred_pairs;
393
394         /* AD1986A can't manage the dynamic pin on/off smoothly */
395         spec->gen.auto_mute_via_amp = 1;
396
397         snd_hda_pick_fixup(codec, ad1986a_fixup_models, ad1986a_fixup_tbl,
398                            ad1986a_fixups);
399         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
400
401         err = ad198x_parse_auto_config(codec, false);
402         if (err < 0) {
403                 snd_hda_gen_free(codec);
404                 return err;
405         }
406
407         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
408
409         return 0;
410 }
411
412
413 /*
414  * AD1983 specific
415  */
416
417 /*
418  * SPDIF mux control for AD1983 auto-parser
419  */
420 static int ad1983_auto_smux_enum_info(struct snd_kcontrol *kcontrol,
421                                       struct snd_ctl_elem_info *uinfo)
422 {
423         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
424         struct ad198x_spec *spec = codec->spec;
425         static const char * const texts2[] = { "PCM", "ADC" };
426         static const char * const texts3[] = { "PCM", "ADC1", "ADC2" };
427         hda_nid_t dig_out = spec->gen.multiout.dig_out_nid;
428         int num_conns = snd_hda_get_num_conns(codec, dig_out);
429
430         if (num_conns == 2)
431                 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts2);
432         else if (num_conns == 3)
433                 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
434         else
435                 return -EINVAL;
436 }
437
438 static int ad1983_auto_smux_enum_get(struct snd_kcontrol *kcontrol,
439                                      struct snd_ctl_elem_value *ucontrol)
440 {
441         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
442         struct ad198x_spec *spec = codec->spec;
443
444         ucontrol->value.enumerated.item[0] = spec->cur_smux;
445         return 0;
446 }
447
448 static int ad1983_auto_smux_enum_put(struct snd_kcontrol *kcontrol,
449                                      struct snd_ctl_elem_value *ucontrol)
450 {
451         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
452         struct ad198x_spec *spec = codec->spec;
453         unsigned int val = ucontrol->value.enumerated.item[0];
454         hda_nid_t dig_out = spec->gen.multiout.dig_out_nid;
455         int num_conns = snd_hda_get_num_conns(codec, dig_out);
456
457         if (val >= num_conns)
458                 return -EINVAL;
459         if (spec->cur_smux == val)
460                 return 0;
461         spec->cur_smux = val;
462         snd_hda_codec_write_cache(codec, dig_out, 0,
463                                   AC_VERB_SET_CONNECT_SEL, val);
464         return 1;
465 }
466
467 static struct snd_kcontrol_new ad1983_auto_smux_mixer = {
468         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
469         .name = "IEC958 Playback Source",
470         .info = ad1983_auto_smux_enum_info,
471         .get = ad1983_auto_smux_enum_get,
472         .put = ad1983_auto_smux_enum_put,
473 };
474
475 static int ad1983_add_spdif_mux_ctl(struct hda_codec *codec)
476 {
477         struct ad198x_spec *spec = codec->spec;
478         hda_nid_t dig_out = spec->gen.multiout.dig_out_nid;
479         int num_conns;
480
481         if (!dig_out)
482                 return 0;
483         num_conns = snd_hda_get_num_conns(codec, dig_out);
484         if (num_conns != 2 && num_conns != 3)
485                 return 0;
486         if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &ad1983_auto_smux_mixer))
487                 return -ENOMEM;
488         return 0;
489 }
490
491 static int patch_ad1983(struct hda_codec *codec)
492 {
493         struct ad198x_spec *spec;
494         int err;
495
496         err = alloc_ad_spec(codec);
497         if (err < 0)
498                 return err;
499         spec = codec->spec;
500
501         spec->gen.beep_nid = 0x10;
502         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
503         err = ad198x_parse_auto_config(codec, false);
504         if (err < 0)
505                 goto error;
506         err = ad1983_add_spdif_mux_ctl(codec);
507         if (err < 0)
508                 goto error;
509         return 0;
510
511  error:
512         snd_hda_gen_free(codec);
513         return err;
514 }
515
516
517 /*
518  * AD1981 HD specific
519  */
520
521 static void ad1981_fixup_hp_eapd(struct hda_codec *codec,
522                                  const struct hda_fixup *fix, int action)
523 {
524         struct ad198x_spec *spec = codec->spec;
525
526         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
527                 spec->gen.vmaster_mute.hook = ad_vmaster_eapd_hook;
528                 spec->eapd_nid = 0x05;
529         }
530 }
531
532 /* set the upper-limit for mixer amp to 0dB for avoiding the possible
533  * damage by overloading
534  */
535 static void ad1981_fixup_amp_override(struct hda_codec *codec,
536                                       const struct hda_fixup *fix, int action)
537 {
538         if (action == HDA_FIXUP_ACT_PRE_PROBE)
539                 snd_hda_override_amp_caps(codec, 0x11, HDA_INPUT,
540                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
541                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
542                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
543                                           (1 << AC_AMPCAP_MUTE_SHIFT));
544 }
545
546 enum {
547         AD1981_FIXUP_AMP_OVERRIDE,
548         AD1981_FIXUP_HP_EAPD,
549 };
550
551 static const struct hda_fixup ad1981_fixups[] = {
552         [AD1981_FIXUP_AMP_OVERRIDE] = {
553                 .type = HDA_FIXUP_FUNC,
554                 .v.func = ad1981_fixup_amp_override,
555         },
556         [AD1981_FIXUP_HP_EAPD] = {
557                 .type = HDA_FIXUP_FUNC,
558                 .v.func = ad1981_fixup_hp_eapd,
559                 .chained = true,
560                 .chain_id = AD1981_FIXUP_AMP_OVERRIDE,
561         },
562 };
563
564 static const struct snd_pci_quirk ad1981_fixup_tbl[] = {
565         SND_PCI_QUIRK_VENDOR(0x1014, "Lenovo", AD1981_FIXUP_AMP_OVERRIDE),
566         SND_PCI_QUIRK_VENDOR(0x103c, "HP", AD1981_FIXUP_HP_EAPD),
567         SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo", AD1981_FIXUP_AMP_OVERRIDE),
568         /* HP nx6320 (reversed SSID, H/W bug) */
569         SND_PCI_QUIRK(0x30b0, 0x103c, "HP nx6320", AD1981_FIXUP_HP_EAPD),
570         {}
571 };
572
573 static int patch_ad1981(struct hda_codec *codec)
574 {
575         struct ad198x_spec *spec;
576         int err;
577
578         err = alloc_ad_spec(codec);
579         if (err < 0)
580                 return -ENOMEM;
581         spec = codec->spec;
582
583         spec->gen.mixer_nid = 0x0e;
584         spec->gen.beep_nid = 0x10;
585         set_beep_amp(spec, 0x0d, 0, HDA_OUTPUT);
586
587         snd_hda_pick_fixup(codec, NULL, ad1981_fixup_tbl, ad1981_fixups);
588         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
589
590         err = ad198x_parse_auto_config(codec, false);
591         if (err < 0)
592                 goto error;
593         err = ad1983_add_spdif_mux_ctl(codec);
594         if (err < 0)
595                 goto error;
596
597         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
598
599         return 0;
600
601  error:
602         snd_hda_gen_free(codec);
603         return err;
604 }
605
606
607 /*
608  * AD1988
609  *
610  * Output pins and routes
611  *
612  *        Pin               Mix     Sel     DAC (*)
613  * port-A 0x11 (mute/hp) <- 0x22 <- 0x37 <- 03/04/06
614  * port-B 0x14 (mute/hp) <- 0x2b <- 0x30 <- 03/04/06
615  * port-C 0x15 (mute)    <- 0x2c <- 0x31 <- 05/0a
616  * port-D 0x12 (mute/hp) <- 0x29         <- 04
617  * port-E 0x17 (mute/hp) <- 0x26 <- 0x32 <- 05/0a
618  * port-F 0x16 (mute)    <- 0x2a         <- 06
619  * port-G 0x24 (mute)    <- 0x27         <- 05
620  * port-H 0x25 (mute)    <- 0x28         <- 0a
621  * mono   0x13 (mute/amp)<- 0x1e <- 0x36 <- 03/04/06
622  *
623  * DAC0 = 03h, DAC1 = 04h, DAC2 = 05h, DAC3 = 06h, DAC4 = 0ah
624  * (*) DAC2/3/4 are swapped to DAC3/4/2 on AD198A rev.2 due to a h/w bug.
625  *
626  * Input pins and routes
627  *
628  *        pin     boost   mix input # / adc input #
629  * port-A 0x11 -> 0x38 -> mix 2, ADC 0
630  * port-B 0x14 -> 0x39 -> mix 0, ADC 1
631  * port-C 0x15 -> 0x3a -> 33:0 - mix 1, ADC 2
632  * port-D 0x12 -> 0x3d -> mix 3, ADC 8
633  * port-E 0x17 -> 0x3c -> 34:0 - mix 4, ADC 4
634  * port-F 0x16 -> 0x3b -> mix 5, ADC 3
635  * port-G 0x24 -> N/A  -> 33:1 - mix 1, 34:1 - mix 4, ADC 6
636  * port-H 0x25 -> N/A  -> 33:2 - mix 1, 34:2 - mix 4, ADC 7
637  *
638  *
639  * DAC assignment
640  *   6stack - front/surr/CLFE/side/opt DACs - 04/06/05/0a/03
641  *   3stack - front/surr/CLFE/opt DACs - 04/05/0a/03
642  *
643  * Inputs of Analog Mix (0x20)
644  *   0:Port-B (front mic)
645  *   1:Port-C/G/H (line-in)
646  *   2:Port-A
647  *   3:Port-D (line-in/2)
648  *   4:Port-E/G/H (mic-in)
649  *   5:Port-F (mic2-in)
650  *   6:CD
651  *   7:Beep
652  *
653  * ADC selection
654  *   0:Port-A
655  *   1:Port-B (front mic-in)
656  *   2:Port-C (line-in)
657  *   3:Port-F (mic2-in)
658  *   4:Port-E (mic-in)
659  *   5:CD
660  *   6:Port-G
661  *   7:Port-H
662  *   8:Port-D (line-in/2)
663  *   9:Mix
664  *
665  * Proposed pin assignments by the datasheet
666  *
667  * 6-stack
668  * Port-A front headphone
669  *      B front mic-in
670  *      C rear line-in
671  *      D rear front-out
672  *      E rear mic-in
673  *      F rear surround
674  *      G rear CLFE
675  *      H rear side
676  *
677  * 3-stack
678  * Port-A front headphone
679  *      B front mic
680  *      C rear line-in/surround
681  *      D rear front-out
682  *      E rear mic-in/CLFE
683  *
684  * laptop
685  * Port-A headphone
686  *      B mic-in
687  *      C docking station
688  *      D internal speaker (with EAPD)
689  *      E/F quad mic array
690  */
691
692 #ifdef ENABLE_AD_STATIC_QUIRKS
693 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
694                                struct snd_ctl_elem_info *uinfo)
695 {
696         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
697         struct ad198x_spec *spec = codec->spec;
698         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
699                                     spec->num_channel_mode);
700 }
701
702 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
703                               struct snd_ctl_elem_value *ucontrol)
704 {
705         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
706         struct ad198x_spec *spec = codec->spec;
707         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
708                                    spec->num_channel_mode, spec->multiout.max_channels);
709 }
710
711 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
712                               struct snd_ctl_elem_value *ucontrol)
713 {
714         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
715         struct ad198x_spec *spec = codec->spec;
716         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
717                                       spec->num_channel_mode,
718                                       &spec->multiout.max_channels);
719         if (err >= 0 && spec->need_dac_fix)
720                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
721         return err;
722 }
723 #endif /* ENABLE_AD_STATIC_QUIRKS */
724
725 static int ad1988_auto_smux_enum_info(struct snd_kcontrol *kcontrol,
726                                       struct snd_ctl_elem_info *uinfo)
727 {
728         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
729         static const char * const texts[] = {
730                 "PCM", "ADC1", "ADC2", "ADC3",
731         };
732         int num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1;
733         if (num_conns > 4)
734                 num_conns = 4;
735         return snd_hda_enum_helper_info(kcontrol, uinfo, num_conns, texts);
736 }
737
738 static int ad1988_auto_smux_enum_get(struct snd_kcontrol *kcontrol,
739                                      struct snd_ctl_elem_value *ucontrol)
740 {
741         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
742         struct ad198x_spec *spec = codec->spec;
743
744         ucontrol->value.enumerated.item[0] = spec->cur_smux;
745         return 0;
746 }
747
748 static int ad1988_auto_smux_enum_put(struct snd_kcontrol *kcontrol,
749                                      struct snd_ctl_elem_value *ucontrol)
750 {
751         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
752         struct ad198x_spec *spec = codec->spec;
753         unsigned int val = ucontrol->value.enumerated.item[0];
754         struct nid_path *path;
755         int num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1;
756
757         if (val >= num_conns)
758                 return -EINVAL;
759         if (spec->cur_smux == val)
760                 return 0;
761
762         mutex_lock(&codec->control_mutex);
763         codec->cached_write = 1;
764         path = snd_hda_get_path_from_idx(codec,
765                                          spec->smux_paths[spec->cur_smux]);
766         if (path)
767                 snd_hda_activate_path(codec, path, false, true);
768         path = snd_hda_get_path_from_idx(codec, spec->smux_paths[val]);
769         if (path)
770                 snd_hda_activate_path(codec, path, true, true);
771         spec->cur_smux = val;
772         codec->cached_write = 0;
773         mutex_unlock(&codec->control_mutex);
774         snd_hda_codec_flush_cache(codec); /* flush the updates */
775         return 1;
776 }
777
778 static struct snd_kcontrol_new ad1988_auto_smux_mixer = {
779         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
780         .name = "IEC958 Playback Source",
781         .info = ad1988_auto_smux_enum_info,
782         .get = ad1988_auto_smux_enum_get,
783         .put = ad1988_auto_smux_enum_put,
784 };
785
786 static int ad1988_auto_init(struct hda_codec *codec)
787 {
788         struct ad198x_spec *spec = codec->spec;
789         int i, err;
790
791         err = snd_hda_gen_init(codec);
792         if (err < 0)
793                 return err;
794         if (!spec->gen.autocfg.dig_outs)
795                 return 0;
796
797         for (i = 0; i < 4; i++) {
798                 struct nid_path *path;
799                 path = snd_hda_get_path_from_idx(codec, spec->smux_paths[i]);
800                 if (path)
801                         snd_hda_activate_path(codec, path, path->active, false);
802         }
803
804         return 0;
805 }
806
807 static int ad1988_add_spdif_mux_ctl(struct hda_codec *codec)
808 {
809         struct ad198x_spec *spec = codec->spec;
810         int i, num_conns;
811         /* we create four static faked paths, since AD codecs have odd
812          * widget connections regarding the SPDIF out source
813          */
814         static struct nid_path fake_paths[4] = {
815                 {
816                         .depth = 3,
817                         .path = { 0x02, 0x1d, 0x1b },
818                         .idx = { 0, 0, 0 },
819                         .multi = { 0, 0, 0 },
820                 },
821                 {
822                         .depth = 4,
823                         .path = { 0x08, 0x0b, 0x1d, 0x1b },
824                         .idx = { 0, 0, 1, 0 },
825                         .multi = { 0, 1, 0, 0 },
826                 },
827                 {
828                         .depth = 4,
829                         .path = { 0x09, 0x0b, 0x1d, 0x1b },
830                         .idx = { 0, 1, 1, 0 },
831                         .multi = { 0, 1, 0, 0 },
832                 },
833                 {
834                         .depth = 4,
835                         .path = { 0x0f, 0x0b, 0x1d, 0x1b },
836                         .idx = { 0, 2, 1, 0 },
837                         .multi = { 0, 1, 0, 0 },
838                 },
839         };
840
841         /* SPDIF source mux appears to be present only on AD1988A */
842         if (!spec->gen.autocfg.dig_outs ||
843             get_wcaps_type(get_wcaps(codec, 0x1d)) != AC_WID_AUD_MIX)
844                 return 0;
845
846         num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1;
847         if (num_conns != 3 && num_conns != 4)
848                 return 0;
849
850         for (i = 0; i < num_conns; i++) {
851                 struct nid_path *path = snd_array_new(&spec->gen.paths);
852                 if (!path)
853                         return -ENOMEM;
854                 *path = fake_paths[i];
855                 if (!i)
856                         path->active = 1;
857                 spec->smux_paths[i] = snd_hda_get_path_idx(codec, path);
858         }
859
860         if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &ad1988_auto_smux_mixer))
861                 return -ENOMEM;
862
863         codec->patch_ops.init = ad1988_auto_init;
864
865         return 0;
866 }
867
868 /*
869  */
870
871 enum {
872         AD1988_FIXUP_6STACK_DIG,
873 };
874
875 static const struct hda_fixup ad1988_fixups[] = {
876         [AD1988_FIXUP_6STACK_DIG] = {
877                 .type = HDA_FIXUP_PINS,
878                 .v.pins = (const struct hda_pintbl[]) {
879                         { 0x11, 0x02214130 }, /* front-hp */
880                         { 0x12, 0x01014010 }, /* line-out */
881                         { 0x14, 0x02a19122 }, /* front-mic */
882                         { 0x15, 0x01813021 }, /* line-in */
883                         { 0x16, 0x01011012 }, /* line-out */
884                         { 0x17, 0x01a19020 }, /* mic */
885                         { 0x1b, 0x0145f1f0 }, /* SPDIF */
886                         { 0x24, 0x01016011 }, /* line-out */
887                         { 0x25, 0x01012013 }, /* line-out */
888                         { }
889                 }
890         },
891 };
892
893 static const struct hda_model_fixup ad1988_fixup_models[] = {
894         { .id = AD1988_FIXUP_6STACK_DIG, .name = "6stack-dig" },
895         {}
896 };
897
898 static int patch_ad1988(struct hda_codec *codec)
899 {
900         struct ad198x_spec *spec;
901         int err;
902
903         err = alloc_ad_spec(codec);
904         if (err < 0)
905                 return err;
906         spec = codec->spec;
907
908         spec->gen.mixer_nid = 0x20;
909         spec->gen.mixer_merge_nid = 0x21;
910         spec->gen.beep_nid = 0x10;
911         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
912
913         snd_hda_pick_fixup(codec, ad1988_fixup_models, NULL, ad1988_fixups);
914         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
915
916         err = ad198x_parse_auto_config(codec, true);
917         if (err < 0)
918                 goto error;
919         err = ad1988_add_spdif_mux_ctl(codec);
920         if (err < 0)
921                 goto error;
922
923         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
924
925         return 0;
926
927  error:
928         snd_hda_gen_free(codec);
929         return err;
930 }
931
932
933 /*
934  * AD1884 / AD1984
935  *
936  * port-B - front line/mic-in
937  * port-E - aux in/out
938  * port-F - aux in/out
939  * port-C - rear line/mic-in
940  * port-D - rear line/hp-out
941  * port-A - front line/hp-out
942  *
943  * AD1984 = AD1884 + two digital mic-ins
944  *
945  * AD1883 / AD1884A / AD1984A / AD1984B
946  *
947  * port-B (0x14) - front mic-in
948  * port-E (0x1c) - rear mic-in
949  * port-F (0x16) - CD / ext out
950  * port-C (0x15) - rear line-in
951  * port-D (0x12) - rear line-out
952  * port-A (0x11) - front hp-out
953  *
954  * AD1984A = AD1884A + digital-mic
955  * AD1883 = equivalent with AD1984A
956  * AD1984B = AD1984A + extra SPDIF-out
957  */
958
959 /* set the upper-limit for mixer amp to 0dB for avoiding the possible
960  * damage by overloading
961  */
962 static void ad1884_fixup_amp_override(struct hda_codec *codec,
963                                       const struct hda_fixup *fix, int action)
964 {
965         if (action == HDA_FIXUP_ACT_PRE_PROBE)
966                 snd_hda_override_amp_caps(codec, 0x20, HDA_INPUT,
967                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
968                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
969                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
970                                           (1 << AC_AMPCAP_MUTE_SHIFT));
971 }
972
973 /* toggle GPIO1 according to the mute state */
974 static void ad1884_vmaster_hp_gpio_hook(void *private_data, int enabled)
975 {
976         struct hda_codec *codec = private_data;
977         struct ad198x_spec *spec = codec->spec;
978
979         if (spec->eapd_nid)
980                 ad_vmaster_eapd_hook(private_data, enabled);
981         snd_hda_codec_update_cache(codec, 0x01, 0,
982                                    AC_VERB_SET_GPIO_DATA,
983                                    enabled ? 0x00 : 0x02);
984 }
985
986 static void ad1884_fixup_hp_eapd(struct hda_codec *codec,
987                                  const struct hda_fixup *fix, int action)
988 {
989         struct ad198x_spec *spec = codec->spec;
990         static const struct hda_verb gpio_init_verbs[] = {
991                 {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
992                 {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
993                 {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
994                 {},
995         };
996
997         switch (action) {
998         case HDA_FIXUP_ACT_PRE_PROBE:
999                 spec->gen.vmaster_mute.hook = ad1884_vmaster_hp_gpio_hook;
1000                 spec->gen.own_eapd_ctl = 1;
1001                 snd_hda_sequence_write_cache(codec, gpio_init_verbs);
1002                 break;
1003         case HDA_FIXUP_ACT_PROBE:
1004                 if (spec->gen.autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
1005                         spec->eapd_nid = spec->gen.autocfg.line_out_pins[0];
1006                 else
1007                         spec->eapd_nid = spec->gen.autocfg.speaker_pins[0];
1008                 break;
1009         }
1010 }
1011
1012 static void ad1884_fixup_thinkpad(struct hda_codec *codec,
1013                                   const struct hda_fixup *fix, int action)
1014 {
1015         struct ad198x_spec *spec = codec->spec;
1016
1017         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1018                 spec->gen.keep_eapd_on = 1;
1019                 spec->gen.vmaster_mute.hook = ad_vmaster_eapd_hook;
1020                 spec->eapd_nid = 0x12;
1021         }
1022 }
1023
1024 /* set magic COEFs for dmic */
1025 static const struct hda_verb ad1884_dmic_init_verbs[] = {
1026         {0x01, AC_VERB_SET_COEF_INDEX, 0x13f7},
1027         {0x01, AC_VERB_SET_PROC_COEF, 0x08},
1028         {}
1029 };
1030
1031 enum {
1032         AD1884_FIXUP_AMP_OVERRIDE,
1033         AD1884_FIXUP_HP_EAPD,
1034         AD1884_FIXUP_DMIC_COEF,
1035         AD1884_FIXUP_THINKPAD,
1036         AD1884_FIXUP_HP_TOUCHSMART,
1037 };
1038
1039 static const struct hda_fixup ad1884_fixups[] = {
1040         [AD1884_FIXUP_AMP_OVERRIDE] = {
1041                 .type = HDA_FIXUP_FUNC,
1042                 .v.func = ad1884_fixup_amp_override,
1043         },
1044         [AD1884_FIXUP_HP_EAPD] = {
1045                 .type = HDA_FIXUP_FUNC,
1046                 .v.func = ad1884_fixup_hp_eapd,
1047                 .chained = true,
1048                 .chain_id = AD1884_FIXUP_AMP_OVERRIDE,
1049         },
1050         [AD1884_FIXUP_DMIC_COEF] = {
1051                 .type = HDA_FIXUP_VERBS,
1052                 .v.verbs = ad1884_dmic_init_verbs,
1053         },
1054         [AD1884_FIXUP_THINKPAD] = {
1055                 .type = HDA_FIXUP_FUNC,
1056                 .v.func = ad1884_fixup_thinkpad,
1057                 .chained = true,
1058                 .chain_id = AD1884_FIXUP_DMIC_COEF,
1059         },
1060         [AD1884_FIXUP_HP_TOUCHSMART] = {
1061                 .type = HDA_FIXUP_VERBS,
1062                 .v.verbs = ad1884_dmic_init_verbs,
1063                 .chained = true,
1064                 .chain_id = AD1884_FIXUP_HP_EAPD,
1065         },
1066 };
1067
1068 static const struct snd_pci_quirk ad1884_fixup_tbl[] = {
1069         SND_PCI_QUIRK(0x103c, 0x2a82, "HP Touchsmart", AD1884_FIXUP_HP_TOUCHSMART),
1070         SND_PCI_QUIRK_VENDOR(0x103c, "HP", AD1884_FIXUP_HP_EAPD),
1071         SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo Thinkpad", AD1884_FIXUP_THINKPAD),
1072         {}
1073 };
1074
1075
1076 static int patch_ad1884(struct hda_codec *codec)
1077 {
1078         struct ad198x_spec *spec;
1079         int err;
1080
1081         err = alloc_ad_spec(codec);
1082         if (err < 0)
1083                 return err;
1084         spec = codec->spec;
1085
1086         spec->gen.mixer_nid = 0x20;
1087         spec->gen.beep_nid = 0x10;
1088         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
1089
1090         snd_hda_pick_fixup(codec, NULL, ad1884_fixup_tbl, ad1884_fixups);
1091         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1092
1093         err = ad198x_parse_auto_config(codec, true);
1094         if (err < 0)
1095                 goto error;
1096         err = ad1983_add_spdif_mux_ctl(codec);
1097         if (err < 0)
1098                 goto error;
1099
1100         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1101
1102         return 0;
1103
1104  error:
1105         snd_hda_gen_free(codec);
1106         return err;
1107 }
1108
1109 /*
1110  * AD1882 / AD1882A
1111  *
1112  * port-A - front hp-out
1113  * port-B - front mic-in
1114  * port-C - rear line-in, shared surr-out (3stack)
1115  * port-D - rear line-out
1116  * port-E - rear mic-in, shared clfe-out (3stack)
1117  * port-F - rear surr-out (6stack)
1118  * port-G - rear clfe-out (6stack)
1119  */
1120
1121 static int patch_ad1882(struct hda_codec *codec)
1122 {
1123         struct ad198x_spec *spec;
1124         int err;
1125
1126         err = alloc_ad_spec(codec);
1127         if (err < 0)
1128                 return err;
1129         spec = codec->spec;
1130
1131         spec->gen.mixer_nid = 0x20;
1132         spec->gen.mixer_merge_nid = 0x21;
1133         spec->gen.beep_nid = 0x10;
1134         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
1135         err = ad198x_parse_auto_config(codec, true);
1136         if (err < 0)
1137                 goto error;
1138         err = ad1988_add_spdif_mux_ctl(codec);
1139         if (err < 0)
1140                 goto error;
1141         return 0;
1142
1143  error:
1144         snd_hda_gen_free(codec);
1145         return err;
1146 }
1147
1148
1149 /*
1150  * patch entries
1151  */
1152 static const struct hda_codec_preset snd_hda_preset_analog[] = {
1153         { .id = 0x11d4184a, .name = "AD1884A", .patch = patch_ad1884 },
1154         { .id = 0x11d41882, .name = "AD1882", .patch = patch_ad1882 },
1155         { .id = 0x11d41883, .name = "AD1883", .patch = patch_ad1884 },
1156         { .id = 0x11d41884, .name = "AD1884", .patch = patch_ad1884 },
1157         { .id = 0x11d4194a, .name = "AD1984A", .patch = patch_ad1884 },
1158         { .id = 0x11d4194b, .name = "AD1984B", .patch = patch_ad1884 },
1159         { .id = 0x11d41981, .name = "AD1981", .patch = patch_ad1981 },
1160         { .id = 0x11d41983, .name = "AD1983", .patch = patch_ad1983 },
1161         { .id = 0x11d41984, .name = "AD1984", .patch = patch_ad1884 },
1162         { .id = 0x11d41986, .name = "AD1986A", .patch = patch_ad1986a },
1163         { .id = 0x11d41988, .name = "AD1988", .patch = patch_ad1988 },
1164         { .id = 0x11d4198b, .name = "AD1988B", .patch = patch_ad1988 },
1165         { .id = 0x11d4882a, .name = "AD1882A", .patch = patch_ad1882 },
1166         { .id = 0x11d4989a, .name = "AD1989A", .patch = patch_ad1988 },
1167         { .id = 0x11d4989b, .name = "AD1989B", .patch = patch_ad1988 },
1168         {} /* terminator */
1169 };
1170
1171 MODULE_ALIAS("snd-hda-codec-id:11d4*");
1172
1173 MODULE_LICENSE("GPL");
1174 MODULE_DESCRIPTION("Analog Devices HD-audio codec");
1175
1176 static struct hda_codec_preset_list analog_list = {
1177         .preset = snd_hda_preset_analog,
1178         .owner = THIS_MODULE,
1179 };
1180
1181 static int __init patch_analog_init(void)
1182 {
1183         return snd_hda_add_codec_preset(&analog_list);
1184 }
1185
1186 static void __exit patch_analog_exit(void)
1187 {
1188         snd_hda_delete_codec_preset(&analog_list);
1189 }
1190
1191 module_init(patch_analog_init)
1192 module_exit(patch_analog_exit)