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