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