ALSA: hda - Drop a few other static quirks for AD1986A
[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 #define ENABLE_AD_STATIC_QUIRKS
36
37 struct ad198x_spec {
38         struct hda_gen_spec gen;
39
40         /* for auto parser */
41         int smux_paths[4];
42         unsigned int cur_smux;
43         hda_nid_t eapd_nid;
44
45         unsigned int beep_amp;  /* beep amp value, set via set_beep_amp() */
46
47 #ifdef ENABLE_AD_STATIC_QUIRKS
48         const struct snd_kcontrol_new *mixers[6];
49         int num_mixers;
50         const struct hda_verb *init_verbs[6];   /* initialization verbs
51                                                  * don't forget NULL termination!
52                                                  */
53         unsigned int num_init_verbs;
54
55         /* playback */
56         struct hda_multi_out multiout;  /* playback set-up
57                                          * max_channels, dacs must be set
58                                          * dig_out_nid and hp_nid are optional
59                                          */
60         unsigned int cur_eapd;
61         unsigned int need_dac_fix;
62
63         /* capture */
64         unsigned int num_adc_nids;
65         const hda_nid_t *adc_nids;
66         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
67
68         /* capture source */
69         const struct hda_input_mux *input_mux;
70         const hda_nid_t *capsrc_nids;
71         unsigned int cur_mux[3];
72
73         /* channel model */
74         const struct hda_channel_mode *channel_mode;
75         int num_channel_mode;
76
77         /* PCM information */
78         struct hda_pcm pcm_rec[3];      /* used in alc_build_pcms() */
79
80         unsigned int spdif_route;
81
82         unsigned int jack_present: 1;
83         unsigned int inv_jack_detect: 1;/* inverted jack-detection */
84         unsigned int analog_beep: 1;    /* analog beep input present */
85         unsigned int avoid_init_slave_vol:1;
86
87 #ifdef CONFIG_PM
88         struct hda_loopback_check loopback;
89 #endif
90         /* for virtual master */
91         hda_nid_t vmaster_nid;
92         const char * const *slave_vols;
93         const char * const *slave_sws;
94 #endif /* ENABLE_AD_STATIC_QUIRKS */
95 };
96
97 #ifdef ENABLE_AD_STATIC_QUIRKS
98 /*
99  * input MUX handling (common part)
100  */
101 static int ad198x_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
102 {
103         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
104         struct ad198x_spec *spec = codec->spec;
105
106         return snd_hda_input_mux_info(spec->input_mux, uinfo);
107 }
108
109 static int ad198x_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
110 {
111         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
112         struct ad198x_spec *spec = codec->spec;
113         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
114
115         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
116         return 0;
117 }
118
119 static int ad198x_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
120 {
121         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
122         struct ad198x_spec *spec = codec->spec;
123         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
124
125         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
126                                      spec->capsrc_nids[adc_idx],
127                                      &spec->cur_mux[adc_idx]);
128 }
129
130 /*
131  * initialization (common callbacks)
132  */
133 static int ad198x_init(struct hda_codec *codec)
134 {
135         struct ad198x_spec *spec = codec->spec;
136         int i;
137
138         for (i = 0; i < spec->num_init_verbs; i++)
139                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
140         return 0;
141 }
142
143 static const char * const ad_slave_pfxs[] = {
144         "Front", "Surround", "Center", "LFE", "Side",
145         "Headphone", "Mono", "Speaker", "IEC958",
146         NULL
147 };
148
149 static const char * const ad1988_6stack_fp_slave_pfxs[] = {
150         "Front", "Surround", "Center", "LFE", "Side", "IEC958",
151         NULL
152 };
153 #endif /* ENABLE_AD_STATIC_QUIRKS */
154
155 #ifdef CONFIG_SND_HDA_INPUT_BEEP
156 /* additional beep mixers; the actual parameters are overwritten at build */
157 static const struct snd_kcontrol_new ad_beep_mixer[] = {
158         HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_OUTPUT),
159         HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_OUTPUT),
160         { } /* end */
161 };
162
163 static const struct snd_kcontrol_new ad_beep2_mixer[] = {
164         HDA_CODEC_VOLUME("Digital Beep Playback Volume", 0, 0, HDA_OUTPUT),
165         HDA_CODEC_MUTE_BEEP("Digital Beep Playback Switch", 0, 0, HDA_OUTPUT),
166         { } /* end */
167 };
168
169 #define set_beep_amp(spec, nid, idx, dir) \
170         ((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir)) /* mono */
171 #else
172 #define set_beep_amp(spec, nid, idx, dir) /* NOP */
173 #endif
174
175 #ifdef CONFIG_SND_HDA_INPUT_BEEP
176 static int create_beep_ctls(struct hda_codec *codec)
177 {
178         struct ad198x_spec *spec = codec->spec;
179         const struct snd_kcontrol_new *knew;
180
181         if (!spec->beep_amp)
182                 return 0;
183
184         knew = spec->analog_beep ? ad_beep2_mixer : ad_beep_mixer;
185         for ( ; knew->name; knew++) {
186                 int err;
187                 struct snd_kcontrol *kctl;
188                 kctl = snd_ctl_new1(knew, codec);
189                 if (!kctl)
190                         return -ENOMEM;
191                 kctl->private_value = spec->beep_amp;
192                 err = snd_hda_ctl_add(codec, 0, kctl);
193                 if (err < 0)
194                         return err;
195         }
196         return 0;
197 }
198 #else
199 #define create_beep_ctls(codec)         0
200 #endif
201
202 #ifdef ENABLE_AD_STATIC_QUIRKS
203 static int ad198x_build_controls(struct hda_codec *codec)
204 {
205         struct ad198x_spec *spec = codec->spec;
206         struct snd_kcontrol *kctl;
207         unsigned int i;
208         int err;
209
210         for (i = 0; i < spec->num_mixers; i++) {
211                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
212                 if (err < 0)
213                         return err;
214         }
215         if (spec->multiout.dig_out_nid) {
216                 err = snd_hda_create_spdif_out_ctls(codec,
217                                                     spec->multiout.dig_out_nid,
218                                                     spec->multiout.dig_out_nid);
219                 if (err < 0)
220                         return err;
221                 err = snd_hda_create_spdif_share_sw(codec,
222                                                     &spec->multiout);
223                 if (err < 0)
224                         return err;
225                 spec->multiout.share_spdif = 1;
226         } 
227         if (spec->dig_in_nid) {
228                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
229                 if (err < 0)
230                         return err;
231         }
232
233         /* create beep controls if needed */
234         err = create_beep_ctls(codec);
235         if (err < 0)
236                 return err;
237
238         /* if we have no master control, let's create it */
239         if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
240                 unsigned int vmaster_tlv[4];
241                 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
242                                         HDA_OUTPUT, vmaster_tlv);
243                 err = __snd_hda_add_vmaster(codec, "Master Playback Volume",
244                                           vmaster_tlv,
245                                           (spec->slave_vols ?
246                                            spec->slave_vols : ad_slave_pfxs),
247                                           "Playback Volume",
248                                           !spec->avoid_init_slave_vol, NULL);
249                 if (err < 0)
250                         return err;
251         }
252         if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
253                 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
254                                           NULL,
255                                           (spec->slave_sws ?
256                                            spec->slave_sws : ad_slave_pfxs),
257                                           "Playback Switch");
258                 if (err < 0)
259                         return err;
260         }
261
262         /* assign Capture Source enums to NID */
263         kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
264         if (!kctl)
265                 kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
266         for (i = 0; kctl && i < kctl->count; i++) {
267                 err = snd_hda_add_nid(codec, kctl, i, spec->capsrc_nids[i]);
268                 if (err < 0)
269                         return err;
270         }
271
272         /* assign IEC958 enums to NID */
273         kctl = snd_hda_find_mixer_ctl(codec,
274                         SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source");
275         if (kctl) {
276                 err = snd_hda_add_nid(codec, kctl, 0,
277                                       spec->multiout.dig_out_nid);
278                 if (err < 0)
279                         return err;
280         }
281
282         return 0;
283 }
284
285 #ifdef CONFIG_PM
286 static int ad198x_check_power_status(struct hda_codec *codec, hda_nid_t nid)
287 {
288         struct ad198x_spec *spec = codec->spec;
289         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
290 }
291 #endif
292
293 /*
294  * Analog playback callbacks
295  */
296 static int ad198x_playback_pcm_open(struct hda_pcm_stream *hinfo,
297                                     struct hda_codec *codec,
298                                     struct snd_pcm_substream *substream)
299 {
300         struct ad198x_spec *spec = codec->spec;
301         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
302                                              hinfo);
303 }
304
305 static int ad198x_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
306                                        struct hda_codec *codec,
307                                        unsigned int stream_tag,
308                                        unsigned int format,
309                                        struct snd_pcm_substream *substream)
310 {
311         struct ad198x_spec *spec = codec->spec;
312         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
313                                                 format, substream);
314 }
315
316 static int ad198x_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
317                                        struct hda_codec *codec,
318                                        struct snd_pcm_substream *substream)
319 {
320         struct ad198x_spec *spec = codec->spec;
321         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
322 }
323
324 /*
325  * Digital out
326  */
327 static int ad198x_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
328                                         struct hda_codec *codec,
329                                         struct snd_pcm_substream *substream)
330 {
331         struct ad198x_spec *spec = codec->spec;
332         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
333 }
334
335 static int ad198x_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
336                                          struct hda_codec *codec,
337                                          struct snd_pcm_substream *substream)
338 {
339         struct ad198x_spec *spec = codec->spec;
340         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
341 }
342
343 static int ad198x_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
344                                            struct hda_codec *codec,
345                                            unsigned int stream_tag,
346                                            unsigned int format,
347                                            struct snd_pcm_substream *substream)
348 {
349         struct ad198x_spec *spec = codec->spec;
350         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
351                                              format, substream);
352 }
353
354 static int ad198x_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
355                                            struct hda_codec *codec,
356                                            struct snd_pcm_substream *substream)
357 {
358         struct ad198x_spec *spec = codec->spec;
359         return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
360 }
361
362 /*
363  * Analog capture
364  */
365 static int ad198x_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
366                                       struct hda_codec *codec,
367                                       unsigned int stream_tag,
368                                       unsigned int format,
369                                       struct snd_pcm_substream *substream)
370 {
371         struct ad198x_spec *spec = codec->spec;
372         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
373                                    stream_tag, 0, format);
374         return 0;
375 }
376
377 static int ad198x_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
378                                       struct hda_codec *codec,
379                                       struct snd_pcm_substream *substream)
380 {
381         struct ad198x_spec *spec = codec->spec;
382         snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
383         return 0;
384 }
385
386 /*
387  */
388 static const struct hda_pcm_stream ad198x_pcm_analog_playback = {
389         .substreams = 1,
390         .channels_min = 2,
391         .channels_max = 6, /* changed later */
392         .nid = 0, /* fill later */
393         .ops = {
394                 .open = ad198x_playback_pcm_open,
395                 .prepare = ad198x_playback_pcm_prepare,
396                 .cleanup = ad198x_playback_pcm_cleanup,
397         },
398 };
399
400 static const struct hda_pcm_stream ad198x_pcm_analog_capture = {
401         .substreams = 1,
402         .channels_min = 2,
403         .channels_max = 2,
404         .nid = 0, /* fill later */
405         .ops = {
406                 .prepare = ad198x_capture_pcm_prepare,
407                 .cleanup = ad198x_capture_pcm_cleanup
408         },
409 };
410
411 static const struct hda_pcm_stream ad198x_pcm_digital_playback = {
412         .substreams = 1,
413         .channels_min = 2,
414         .channels_max = 2,
415         .nid = 0, /* fill later */
416         .ops = {
417                 .open = ad198x_dig_playback_pcm_open,
418                 .close = ad198x_dig_playback_pcm_close,
419                 .prepare = ad198x_dig_playback_pcm_prepare,
420                 .cleanup = ad198x_dig_playback_pcm_cleanup
421         },
422 };
423
424 static const struct hda_pcm_stream ad198x_pcm_digital_capture = {
425         .substreams = 1,
426         .channels_min = 2,
427         .channels_max = 2,
428         /* NID is set in alc_build_pcms */
429 };
430
431 static int ad198x_build_pcms(struct hda_codec *codec)
432 {
433         struct ad198x_spec *spec = codec->spec;
434         struct hda_pcm *info = spec->pcm_rec;
435
436         codec->num_pcms = 1;
437         codec->pcm_info = info;
438
439         info->name = "AD198x Analog";
440         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ad198x_pcm_analog_playback;
441         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->multiout.max_channels;
442         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
443         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad198x_pcm_analog_capture;
444         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adc_nids;
445         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
446
447         if (spec->multiout.dig_out_nid) {
448                 info++;
449                 codec->num_pcms++;
450                 codec->spdif_status_reset = 1;
451                 info->name = "AD198x Digital";
452                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
453                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ad198x_pcm_digital_playback;
454                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
455                 if (spec->dig_in_nid) {
456                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad198x_pcm_digital_capture;
457                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
458                 }
459         }
460
461         return 0;
462 }
463 #endif /* ENABLE_AD_STATIC_QUIRKS */
464
465 static void ad198x_power_eapd_write(struct hda_codec *codec, hda_nid_t front,
466                                 hda_nid_t hp)
467 {
468         if (snd_hda_query_pin_caps(codec, front) & AC_PINCAP_EAPD)
469                 snd_hda_codec_write(codec, front, 0, AC_VERB_SET_EAPD_BTLENABLE,
470                             !codec->inv_eapd ? 0x00 : 0x02);
471         if (snd_hda_query_pin_caps(codec, hp) & AC_PINCAP_EAPD)
472                 snd_hda_codec_write(codec, hp, 0, AC_VERB_SET_EAPD_BTLENABLE,
473                             !codec->inv_eapd ? 0x00 : 0x02);
474 }
475
476 static void ad198x_power_eapd(struct hda_codec *codec)
477 {
478         /* We currently only handle front, HP */
479         switch (codec->vendor_id) {
480         case 0x11d41882:
481         case 0x11d4882a:
482         case 0x11d41884:
483         case 0x11d41984:
484         case 0x11d41883:
485         case 0x11d4184a:
486         case 0x11d4194a:
487         case 0x11d4194b:
488         case 0x11d41988:
489         case 0x11d4198b:
490         case 0x11d4989a:
491         case 0x11d4989b:
492                 ad198x_power_eapd_write(codec, 0x12, 0x11);
493                 break;
494         case 0x11d41981:
495         case 0x11d41983:
496                 ad198x_power_eapd_write(codec, 0x05, 0x06);
497                 break;
498         case 0x11d41986:
499                 ad198x_power_eapd_write(codec, 0x1b, 0x1a);
500                 break;
501         }
502 }
503
504 static void ad198x_shutup(struct hda_codec *codec)
505 {
506         snd_hda_shutup_pins(codec);
507         ad198x_power_eapd(codec);
508 }
509
510 static void ad198x_free(struct hda_codec *codec)
511 {
512         struct ad198x_spec *spec = codec->spec;
513
514         if (!spec)
515                 return;
516
517         snd_hda_gen_spec_free(&spec->gen);
518         kfree(spec);
519         snd_hda_detach_beep_device(codec);
520 }
521
522 #ifdef CONFIG_PM
523 static int ad198x_suspend(struct hda_codec *codec)
524 {
525         ad198x_shutup(codec);
526         return 0;
527 }
528 #endif
529
530 #ifdef ENABLE_AD_STATIC_QUIRKS
531 static const struct hda_codec_ops ad198x_patch_ops = {
532         .build_controls = ad198x_build_controls,
533         .build_pcms = ad198x_build_pcms,
534         .init = ad198x_init,
535         .free = ad198x_free,
536 #ifdef CONFIG_PM
537         .check_power_status = ad198x_check_power_status,
538         .suspend = ad198x_suspend,
539 #endif
540         .reboot_notify = ad198x_shutup,
541 };
542
543
544 /*
545  * EAPD control
546  * the private value = nid
547  */
548 #define ad198x_eapd_info        snd_ctl_boolean_mono_info
549
550 static int ad198x_eapd_get(struct snd_kcontrol *kcontrol,
551                            struct snd_ctl_elem_value *ucontrol)
552 {
553         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
554         struct ad198x_spec *spec = codec->spec;
555         if (codec->inv_eapd)
556                 ucontrol->value.integer.value[0] = ! spec->cur_eapd;
557         else
558                 ucontrol->value.integer.value[0] = spec->cur_eapd;
559         return 0;
560 }
561
562 static int ad198x_eapd_put(struct snd_kcontrol *kcontrol,
563                            struct snd_ctl_elem_value *ucontrol)
564 {
565         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
566         struct ad198x_spec *spec = codec->spec;
567         hda_nid_t nid = kcontrol->private_value & 0xff;
568         unsigned int eapd;
569         eapd = !!ucontrol->value.integer.value[0];
570         if (codec->inv_eapd)
571                 eapd = !eapd;
572         if (eapd == spec->cur_eapd)
573                 return 0;
574         spec->cur_eapd = eapd;
575         snd_hda_codec_write_cache(codec, nid,
576                                   0, AC_VERB_SET_EAPD_BTLENABLE,
577                                   eapd ? 0x02 : 0x00);
578         return 1;
579 }
580
581 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
582                                struct snd_ctl_elem_info *uinfo);
583 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
584                               struct snd_ctl_elem_value *ucontrol);
585 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
586                               struct snd_ctl_elem_value *ucontrol);
587 #endif /* ENABLE_AD_STATIC_QUIRKS */
588
589
590 /*
591  * Automatic parse of I/O pins from the BIOS configuration
592  */
593
594 static int ad198x_auto_build_controls(struct hda_codec *codec)
595 {
596         int err;
597
598         err = snd_hda_gen_build_controls(codec);
599         if (err < 0)
600                 return err;
601         err = create_beep_ctls(codec);
602         if (err < 0)
603                 return err;
604         return 0;
605 }
606
607 static const struct hda_codec_ops ad198x_auto_patch_ops = {
608         .build_controls = ad198x_auto_build_controls,
609         .build_pcms = snd_hda_gen_build_pcms,
610         .init = snd_hda_gen_init,
611         .free = snd_hda_gen_free,
612         .unsol_event = snd_hda_jack_unsol_event,
613 #ifdef CONFIG_PM
614         .check_power_status = snd_hda_gen_check_power_status,
615         .suspend = ad198x_suspend,
616 #endif
617         .reboot_notify = ad198x_shutup,
618 };
619
620
621 static int ad198x_parse_auto_config(struct hda_codec *codec)
622 {
623         struct ad198x_spec *spec = codec->spec;
624         struct auto_pin_cfg *cfg = &spec->gen.autocfg;
625         int err;
626
627         codec->spdif_status_reset = 1;
628         codec->no_trigger_sense = 1;
629         codec->no_sticky_stream = 1;
630
631         spec->gen.indep_hp = 1;
632
633         err = snd_hda_parse_pin_defcfg(codec, cfg, NULL, 0);
634         if (err < 0)
635                 return err;
636         err = snd_hda_gen_parse_auto_config(codec, cfg);
637         if (err < 0)
638                 return err;
639
640         codec->patch_ops = ad198x_auto_patch_ops;
641
642         return 0;
643 }
644
645 /*
646  * AD1986A specific
647  */
648
649 #ifdef ENABLE_AD_STATIC_QUIRKS
650 #define AD1986A_SPDIF_OUT       0x02
651 #define AD1986A_FRONT_DAC       0x03
652 #define AD1986A_SURR_DAC        0x04
653 #define AD1986A_CLFE_DAC        0x05
654 #define AD1986A_ADC             0x06
655
656 static const hda_nid_t ad1986a_dac_nids[3] = {
657         AD1986A_FRONT_DAC, AD1986A_SURR_DAC, AD1986A_CLFE_DAC
658 };
659 static const hda_nid_t ad1986a_adc_nids[1] = { AD1986A_ADC };
660 static const hda_nid_t ad1986a_capsrc_nids[1] = { 0x12 };
661
662 static const struct hda_input_mux ad1986a_capture_source = {
663         .num_items = 7,
664         .items = {
665                 { "Mic", 0x0 },
666                 { "CD", 0x1 },
667                 { "Aux", 0x3 },
668                 { "Line", 0x4 },
669                 { "Mix", 0x5 },
670                 { "Mono", 0x6 },
671                 { "Phone", 0x7 },
672         },
673 };
674
675
676 static const struct hda_bind_ctls ad1986a_bind_pcm_vol = {
677         .ops = &snd_hda_bind_vol,
678         .values = {
679                 HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT),
680                 HDA_COMPOSE_AMP_VAL(AD1986A_SURR_DAC, 3, 0, HDA_OUTPUT),
681                 HDA_COMPOSE_AMP_VAL(AD1986A_CLFE_DAC, 3, 0, HDA_OUTPUT),
682                 0
683         },
684 };
685
686 static const struct hda_bind_ctls ad1986a_bind_pcm_sw = {
687         .ops = &snd_hda_bind_sw,
688         .values = {
689                 HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT),
690                 HDA_COMPOSE_AMP_VAL(AD1986A_SURR_DAC, 3, 0, HDA_OUTPUT),
691                 HDA_COMPOSE_AMP_VAL(AD1986A_CLFE_DAC, 3, 0, HDA_OUTPUT),
692                 0
693         },
694 };
695
696 /*
697  * mixers
698  */
699 static const struct snd_kcontrol_new ad1986a_mixers[] = {
700         /*
701          * bind volumes/mutes of 3 DACs as a single PCM control for simplicity
702          */
703         HDA_BIND_VOL("PCM Playback Volume", &ad1986a_bind_pcm_vol),
704         HDA_BIND_SW("PCM Playback Switch", &ad1986a_bind_pcm_sw),
705         HDA_CODEC_VOLUME("Front Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
706         HDA_CODEC_MUTE("Front Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
707         HDA_CODEC_VOLUME("Surround Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
708         HDA_CODEC_MUTE("Surround Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
709         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x1d, 1, 0x0, HDA_OUTPUT),
710         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x1d, 2, 0x0, HDA_OUTPUT),
711         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x1d, 1, 0x0, HDA_OUTPUT),
712         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x1d, 2, 0x0, HDA_OUTPUT),
713         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x1a, 0x0, HDA_OUTPUT),
714         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1a, 0x0, HDA_OUTPUT),
715         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
716         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
717         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
718         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
719         HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
720         HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
721         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
722         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
723         HDA_CODEC_VOLUME("Mic Boost Volume", 0x0f, 0x0, HDA_OUTPUT),
724         HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
725         HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT),
726         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
727         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
728         {
729                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
730                 .name = "Capture Source",
731                 .info = ad198x_mux_enum_info,
732                 .get = ad198x_mux_enum_get,
733                 .put = ad198x_mux_enum_put,
734         },
735         HDA_CODEC_MUTE("Stereo Downmix Switch", 0x09, 0x0, HDA_OUTPUT),
736         { } /* end */
737 };
738
739 /* additional mixers for 3stack mode */
740 static const struct snd_kcontrol_new ad1986a_3st_mixers[] = {
741         {
742                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
743                 .name = "Channel Mode",
744                 .info = ad198x_ch_mode_info,
745                 .get = ad198x_ch_mode_get,
746                 .put = ad198x_ch_mode_put,
747         },
748         { } /* end */
749 };
750
751 /* laptop model - 2ch only */
752 static const hda_nid_t ad1986a_laptop_dac_nids[1] = { AD1986A_FRONT_DAC };
753
754 /* master controls both pins 0x1a and 0x1b */
755 static const struct hda_bind_ctls ad1986a_laptop_master_vol = {
756         .ops = &snd_hda_bind_vol,
757         .values = {
758                 HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
759                 HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
760                 0,
761         },
762 };
763
764 static const struct hda_bind_ctls ad1986a_laptop_master_sw = {
765         .ops = &snd_hda_bind_sw,
766         .values = {
767                 HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
768                 HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
769                 0,
770         },
771 };
772
773 static const struct snd_kcontrol_new ad1986a_laptop_mixers[] = {
774         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
775         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
776         HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
777         HDA_BIND_SW("Master Playback Switch", &ad1986a_laptop_master_sw),
778         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
779         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
780         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
781         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
782         HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
783         HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
784         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
785         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
786         HDA_CODEC_VOLUME("Mic Boost Volume", 0x0f, 0x0, HDA_OUTPUT),
787         /* 
788            HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
789            HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT), */
790         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
791         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
792         {
793                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
794                 .name = "Capture Source",
795                 .info = ad198x_mux_enum_info,
796                 .get = ad198x_mux_enum_get,
797                 .put = ad198x_mux_enum_put,
798         },
799         { } /* end */
800 };
801
802 /* laptop-eapd model - 2ch only */
803
804 static const struct hda_input_mux ad1986a_laptop_eapd_capture_source = {
805         .num_items = 3,
806         .items = {
807                 { "Mic", 0x0 },
808                 { "Internal Mic", 0x4 },
809                 { "Mix", 0x5 },
810         },
811 };
812
813 static const struct hda_input_mux ad1986a_automic_capture_source = {
814         .num_items = 2,
815         .items = {
816                 { "Mic", 0x0 },
817                 { "Mix", 0x5 },
818         },
819 };
820
821 static const struct snd_kcontrol_new ad1986a_laptop_master_mixers[] = {
822         HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
823         HDA_BIND_SW("Master Playback Switch", &ad1986a_laptop_master_sw),
824         { } /* end */
825 };
826
827 static const struct snd_kcontrol_new ad1986a_laptop_eapd_mixers[] = {
828         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
829         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
830         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
831         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
832         HDA_CODEC_VOLUME("Mic Boost Volume", 0x0f, 0x0, HDA_OUTPUT),
833         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
834         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
835         {
836                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
837                 .name = "Capture Source",
838                 .info = ad198x_mux_enum_info,
839                 .get = ad198x_mux_enum_get,
840                 .put = ad198x_mux_enum_put,
841         },
842         {
843                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
844                 .name = "External Amplifier",
845                 .subdevice = HDA_SUBDEV_NID_FLAG | 0x1b,
846                 .info = ad198x_eapd_info,
847                 .get = ad198x_eapd_get,
848                 .put = ad198x_eapd_put,
849                 .private_value = 0x1b, /* port-D */
850         },
851         { } /* end */
852 };
853
854 static const struct snd_kcontrol_new ad1986a_laptop_intmic_mixers[] = {
855         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0, HDA_OUTPUT),
856         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0, HDA_OUTPUT),
857         { } /* end */
858 };
859
860 /* laptop-automute - 2ch only */
861
862 static void ad1986a_update_hp(struct hda_codec *codec)
863 {
864         struct ad198x_spec *spec = codec->spec;
865         unsigned int mute;
866
867         if (spec->jack_present)
868                 mute = HDA_AMP_MUTE; /* mute internal speaker */
869         else
870                 /* unmute internal speaker if necessary */
871                 mute = snd_hda_codec_amp_read(codec, 0x1a, 0, HDA_OUTPUT, 0);
872         snd_hda_codec_amp_stereo(codec, 0x1b, HDA_OUTPUT, 0,
873                                  HDA_AMP_MUTE, mute);
874 }
875
876 static void ad1986a_hp_automute(struct hda_codec *codec)
877 {
878         struct ad198x_spec *spec = codec->spec;
879
880         spec->jack_present = snd_hda_jack_detect(codec, 0x1a);
881         if (spec->inv_jack_detect)
882                 spec->jack_present = !spec->jack_present;
883         ad1986a_update_hp(codec);
884 }
885
886 #define AD1986A_HP_EVENT                0x37
887
888 static void ad1986a_hp_unsol_event(struct hda_codec *codec, unsigned int res)
889 {
890         if ((res >> 26) != AD1986A_HP_EVENT)
891                 return;
892         ad1986a_hp_automute(codec);
893 }
894
895 static int ad1986a_hp_init(struct hda_codec *codec)
896 {
897         ad198x_init(codec);
898         ad1986a_hp_automute(codec);
899         return 0;
900 }
901
902 /* bind hp and internal speaker mute (with plug check) */
903 static int ad1986a_hp_master_sw_put(struct snd_kcontrol *kcontrol,
904                                     struct snd_ctl_elem_value *ucontrol)
905 {
906         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
907         int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
908         if (change)
909                 ad1986a_update_hp(codec);
910         return change;
911 }
912
913 static const struct snd_kcontrol_new ad1986a_automute_master_mixers[] = {
914         HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
915         {
916                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
917                 .name = "Master Playback Switch",
918                 .subdevice = HDA_SUBDEV_AMP_FLAG,
919                 .info = snd_hda_mixer_amp_switch_info,
920                 .get = snd_hda_mixer_amp_switch_get,
921                 .put = ad1986a_hp_master_sw_put,
922                 .private_value = HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
923         },
924         { } /* end */
925 };
926
927
928 /*
929  * initialization verbs
930  */
931 static const struct hda_verb ad1986a_init_verbs[] = {
932         /* Front, Surround, CLFE DAC; mute as default */
933         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
934         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
935         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
936         /* Downmix - off */
937         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
938         /* HP, Line-Out, Surround, CLFE selectors */
939         {0x0a, AC_VERB_SET_CONNECT_SEL, 0x0},
940         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x0},
941         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
942         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
943         /* Mono selector */
944         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x0},
945         /* Mic selector: Mic 1/2 pin */
946         {0x0f, AC_VERB_SET_CONNECT_SEL, 0x0},
947         /* Line-in selector: Line-in */
948         {0x10, AC_VERB_SET_CONNECT_SEL, 0x0},
949         /* Mic 1/2 swap */
950         {0x11, AC_VERB_SET_CONNECT_SEL, 0x0},
951         /* Record selector: mic */
952         {0x12, AC_VERB_SET_CONNECT_SEL, 0x0},
953         /* Mic, Phone, CD, Aux, Line-In amp; mute as default */
954         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
955         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
956         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
957         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
958         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
959         /* PC beep */
960         {0x18, AC_VERB_SET_CONNECT_SEL, 0x0},
961         /* HP, Line-Out, Surround, CLFE, Mono pins; mute as default */
962         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
963         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
964         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
965         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
966         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
967         /* HP Pin */
968         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
969         /* Front, Surround, CLFE Pins */
970         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
971         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
972         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
973         /* Mono Pin */
974         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
975         /* Mic Pin */
976         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
977         /* Line, Aux, CD, Beep-In Pin */
978         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
979         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
980         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
981         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
982         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
983         { } /* end */
984 };
985
986 static const struct hda_verb ad1986a_ch2_init[] = {
987         /* Surround out -> Line In */
988         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
989         /* Line-in selectors */
990         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x1 },
991         /* CLFE -> Mic in */
992         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
993         /* Mic selector, mix C/LFE (backmic) and Mic (frontmic) */
994         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x4 },
995         { } /* end */
996 };
997
998 static const struct hda_verb ad1986a_ch4_init[] = {
999         /* Surround out -> Surround */
1000         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
1001         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
1002         /* CLFE -> Mic in */
1003         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
1004         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x4 },
1005         { } /* end */
1006 };
1007
1008 static const struct hda_verb ad1986a_ch6_init[] = {
1009         /* Surround out -> Surround out */
1010         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
1011         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
1012         /* CLFE -> CLFE */
1013         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
1014         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x0 },
1015         { } /* end */
1016 };
1017
1018 static const struct hda_channel_mode ad1986a_modes[3] = {
1019         { 2, ad1986a_ch2_init },
1020         { 4, ad1986a_ch4_init },
1021         { 6, ad1986a_ch6_init },
1022 };
1023
1024 /* eapd initialization */
1025 static const struct hda_verb ad1986a_eapd_init_verbs[] = {
1026         {0x1b, AC_VERB_SET_EAPD_BTLENABLE, 0x00 },
1027         {}
1028 };
1029
1030 /* pin sensing on HP jack */
1031 static const struct hda_verb ad1986a_hp_init_verbs[] = {
1032         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1986A_HP_EVENT},
1033         {}
1034 };
1035
1036
1037 /* models */
1038 enum {
1039         AD1986A_AUTO,
1040         AD1986A_6STACK,
1041         AD1986A_3STACK,
1042         AD1986A_LAPTOP,
1043         AD1986A_LAPTOP_EAPD,
1044         AD1986A_LAPTOP_AUTOMUTE,
1045         AD1986A_MODELS
1046 };
1047
1048 static const char * const ad1986a_models[AD1986A_MODELS] = {
1049         [AD1986A_AUTO]          = "auto",
1050         [AD1986A_6STACK]        = "6stack",
1051         [AD1986A_3STACK]        = "3stack",
1052         [AD1986A_LAPTOP]        = "laptop",
1053         [AD1986A_LAPTOP_EAPD]   = "laptop-eapd",
1054         [AD1986A_LAPTOP_AUTOMUTE] = "laptop-automute",
1055 };
1056
1057 static const struct snd_pci_quirk ad1986a_cfg_tbl[] = {
1058         SND_PCI_QUIRK(0x103c, 0x30af, "HP B2800", AD1986A_LAPTOP_EAPD),
1059         SND_PCI_QUIRK(0x1043, 0x817f, "ASUS P5", AD1986A_3STACK),
1060         SND_PCI_QUIRK(0x1043, 0x818f, "ASUS P5", AD1986A_LAPTOP),
1061         SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS P5", AD1986A_3STACK),
1062         SND_PCI_QUIRK(0x1043, 0x81cb, "ASUS M2N", AD1986A_3STACK),
1063         SND_PCI_QUIRK(0x1043, 0x8234, "ASUS M2N", AD1986A_3STACK),
1064         SND_PCI_QUIRK(0x10de, 0xcb84, "ASUS A8N-VM", AD1986A_3STACK),
1065         SND_PCI_QUIRK(0x144d, 0xc01e, "FSC V2060", AD1986A_LAPTOP),
1066         SND_PCI_QUIRK(0x17aa, 0x1011, "Lenovo M55", AD1986A_LAPTOP),
1067         SND_PCI_QUIRK(0x17aa, 0x1017, "Lenovo A60", AD1986A_3STACK),
1068         {}
1069 };
1070
1071 #ifdef CONFIG_PM
1072 static const struct hda_amp_list ad1986a_loopbacks[] = {
1073         { 0x13, HDA_OUTPUT, 0 }, /* Mic */
1074         { 0x14, HDA_OUTPUT, 0 }, /* Phone */
1075         { 0x15, HDA_OUTPUT, 0 }, /* CD */
1076         { 0x16, HDA_OUTPUT, 0 }, /* Aux */
1077         { 0x17, HDA_OUTPUT, 0 }, /* Line */
1078         { } /* end */
1079 };
1080 #endif
1081
1082 static int is_jack_available(struct hda_codec *codec, hda_nid_t nid)
1083 {
1084         unsigned int conf = snd_hda_codec_get_pincfg(codec, nid);
1085         return get_defcfg_connect(conf) != AC_JACK_PORT_NONE;
1086 }
1087 #endif /* ENABLE_AD_STATIC_QUIRKS */
1088
1089 static int alloc_ad_spec(struct hda_codec *codec)
1090 {
1091         struct ad198x_spec *spec;
1092
1093         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1094         if (!spec)
1095                 return -ENOMEM;
1096         codec->spec = spec;
1097         snd_hda_gen_spec_init(&spec->gen);
1098         return 0;
1099 }
1100
1101 /*
1102  * AD1986A fixup codes
1103  */
1104
1105 /* Lenovo N100 seems to report the reversed bit for HP jack-sensing */
1106 static void ad_fixup_inv_jack_detect(struct hda_codec *codec,
1107                                      const struct hda_fixup *fix, int action)
1108 {
1109         if (action == HDA_FIXUP_ACT_PRE_PROBE)
1110                 codec->inv_jack_detect = 1;
1111 }
1112
1113 enum {
1114         AD1986A_FIXUP_INV_JACK_DETECT,
1115         AD1986A_FIXUP_ULTRA,
1116         AD1986A_FIXUP_SAMSUNG,
1117 };
1118
1119 static const struct hda_fixup ad1986a_fixups[] = {
1120         [AD1986A_FIXUP_INV_JACK_DETECT] = {
1121                 .type = HDA_FIXUP_FUNC,
1122                 .v.func = ad_fixup_inv_jack_detect,
1123         },
1124         [AD1986A_FIXUP_ULTRA] = {
1125                 .type = HDA_FIXUP_PINS,
1126                 .v.pins = (const struct hda_pintbl[]) {
1127                         { 0x1b, 0x90170110 }, /* speaker */
1128                         { 0x1d, 0x90a7013e }, /* int mic */
1129                         {}
1130                 },
1131         },
1132         [AD1986A_FIXUP_SAMSUNG] = {
1133                 .type = HDA_FIXUP_PINS,
1134                 .v.pins = (const struct hda_pintbl[]) {
1135                         { 0x1b, 0x90170110 }, /* speaker */
1136                         { 0x1d, 0x90a7013e }, /* int mic */
1137                         { 0x20, 0x411111f0 }, /* N/A */
1138                         { 0x24, 0x411111f0 }, /* N/A */
1139                         {}
1140                 },
1141         },
1142 };
1143
1144 static const struct snd_pci_quirk ad1986a_fixup_tbl[] = {
1145         SND_PCI_QUIRK_MASK(0x144d, 0xff00, 0xc000, "Samsung", AD1986A_FIXUP_SAMSUNG),
1146         SND_PCI_QUIRK(0x144d, 0xc027, "Samsung Q1", AD1986A_FIXUP_ULTRA),
1147         SND_PCI_QUIRK(0x17aa, 0x2066, "Lenovo N100", AD1986A_FIXUP_INV_JACK_DETECT),
1148         {}
1149 };
1150
1151 /*
1152  */
1153 static int ad1986a_parse_auto_config(struct hda_codec *codec)
1154 {
1155         int err;
1156         struct ad198x_spec *spec;
1157
1158         err = alloc_ad_spec(codec);
1159         if (err < 0)
1160                 return err;
1161         spec = codec->spec;
1162
1163         /* AD1986A has the inverted EAPD implementation */
1164         codec->inv_eapd = 1;
1165
1166         spec->gen.mixer_nid = 0x07;
1167         spec->gen.beep_nid = 0x19;
1168         set_beep_amp(spec, 0x18, 0, HDA_OUTPUT);
1169
1170         /* AD1986A has a hardware problem that it can't share a stream
1171          * with multiple output pins.  The copy of front to surrounds
1172          * causes noisy or silent outputs at a certain timing, e.g.
1173          * changing the volume.
1174          * So, let's disable the shared stream.
1175          */
1176         spec->gen.multiout.no_share_stream = 1;
1177
1178         snd_hda_pick_fixup(codec, NULL, ad1986a_fixup_tbl, ad1986a_fixups);
1179         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1180
1181         err = ad198x_parse_auto_config(codec);
1182         if (err < 0) {
1183                 snd_hda_gen_free(codec);
1184                 return err;
1185         }
1186
1187         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1188
1189         return 0;
1190 }
1191
1192 #ifdef ENABLE_AD_STATIC_QUIRKS
1193 static int patch_ad1986a(struct hda_codec *codec)
1194 {
1195         struct ad198x_spec *spec;
1196         int err, board_config;
1197
1198         board_config = snd_hda_check_board_config(codec, AD1986A_MODELS,
1199                                                   ad1986a_models,
1200                                                   ad1986a_cfg_tbl);
1201         if (board_config < 0) {
1202                 printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
1203                        codec->chip_name);
1204                 board_config = AD1986A_AUTO;
1205         }
1206
1207         if (board_config == AD1986A_AUTO)
1208                 return ad1986a_parse_auto_config(codec);
1209
1210         err = alloc_ad_spec(codec);
1211         if (err < 0)
1212                 return err;
1213         spec = codec->spec;
1214
1215         err = snd_hda_attach_beep_device(codec, 0x19);
1216         if (err < 0) {
1217                 ad198x_free(codec);
1218                 return err;
1219         }
1220         set_beep_amp(spec, 0x18, 0, HDA_OUTPUT);
1221
1222         spec->multiout.max_channels = 6;
1223         spec->multiout.num_dacs = ARRAY_SIZE(ad1986a_dac_nids);
1224         spec->multiout.dac_nids = ad1986a_dac_nids;
1225         spec->multiout.dig_out_nid = AD1986A_SPDIF_OUT;
1226         spec->num_adc_nids = 1;
1227         spec->adc_nids = ad1986a_adc_nids;
1228         spec->capsrc_nids = ad1986a_capsrc_nids;
1229         spec->input_mux = &ad1986a_capture_source;
1230         spec->num_mixers = 1;
1231         spec->mixers[0] = ad1986a_mixers;
1232         spec->num_init_verbs = 1;
1233         spec->init_verbs[0] = ad1986a_init_verbs;
1234 #ifdef CONFIG_PM
1235         spec->loopback.amplist = ad1986a_loopbacks;
1236 #endif
1237         spec->vmaster_nid = 0x1b;
1238         codec->inv_eapd = 1; /* AD1986A has the inverted EAPD implementation */
1239
1240         codec->patch_ops = ad198x_patch_ops;
1241
1242         /* override some parameters */
1243         switch (board_config) {
1244         case AD1986A_3STACK:
1245                 spec->num_mixers = 2;
1246                 spec->mixers[1] = ad1986a_3st_mixers;
1247                 spec->num_init_verbs = 2;
1248                 spec->init_verbs[1] = ad1986a_ch2_init;
1249                 spec->channel_mode = ad1986a_modes;
1250                 spec->num_channel_mode = ARRAY_SIZE(ad1986a_modes);
1251                 spec->need_dac_fix = 1;
1252                 spec->multiout.max_channels = 2;
1253                 spec->multiout.num_dacs = 1;
1254                 break;
1255         case AD1986A_LAPTOP:
1256                 spec->mixers[0] = ad1986a_laptop_mixers;
1257                 spec->multiout.max_channels = 2;
1258                 spec->multiout.num_dacs = 1;
1259                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1260                 break;
1261         case AD1986A_LAPTOP_EAPD:
1262                 spec->num_mixers = 3;
1263                 spec->mixers[0] = ad1986a_laptop_master_mixers;
1264                 spec->mixers[1] = ad1986a_laptop_eapd_mixers;
1265                 spec->mixers[2] = ad1986a_laptop_intmic_mixers;
1266                 spec->num_init_verbs = 2;
1267                 spec->init_verbs[1] = ad1986a_eapd_init_verbs;
1268                 spec->multiout.max_channels = 2;
1269                 spec->multiout.num_dacs = 1;
1270                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1271                 if (!is_jack_available(codec, 0x25))
1272                         spec->multiout.dig_out_nid = 0;
1273                 spec->input_mux = &ad1986a_laptop_eapd_capture_source;
1274                 break;
1275         case AD1986A_LAPTOP_AUTOMUTE:
1276                 spec->num_mixers = 3;
1277                 spec->mixers[0] = ad1986a_automute_master_mixers;
1278                 spec->mixers[1] = ad1986a_laptop_eapd_mixers;
1279                 spec->mixers[2] = ad1986a_laptop_intmic_mixers;
1280                 spec->num_init_verbs = 3;
1281                 spec->init_verbs[1] = ad1986a_eapd_init_verbs;
1282                 spec->init_verbs[2] = ad1986a_hp_init_verbs;
1283                 spec->multiout.max_channels = 2;
1284                 spec->multiout.num_dacs = 1;
1285                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1286                 if (!is_jack_available(codec, 0x25))
1287                         spec->multiout.dig_out_nid = 0;
1288                 spec->input_mux = &ad1986a_laptop_eapd_capture_source;
1289                 codec->patch_ops.unsol_event = ad1986a_hp_unsol_event;
1290                 codec->patch_ops.init = ad1986a_hp_init;
1291                 /* Lenovo N100 seems to report the reversed bit
1292                  * for HP jack-sensing
1293                  */
1294                 spec->inv_jack_detect = 1;
1295                 break;
1296         }
1297
1298         /* AD1986A has a hardware problem that it can't share a stream
1299          * with multiple output pins.  The copy of front to surrounds
1300          * causes noisy or silent outputs at a certain timing, e.g.
1301          * changing the volume.
1302          * So, let's disable the shared stream.
1303          */
1304         spec->multiout.no_share_stream = 1;
1305
1306         codec->no_trigger_sense = 1;
1307         codec->no_sticky_stream = 1;
1308
1309         return 0;
1310 }
1311 #else /* ENABLE_AD_STATIC_QUIRKS */
1312 #define patch_ad1986a   ad1986a_parse_auto_config
1313 #endif /* ENABLE_AD_STATIC_QUIRKS */
1314
1315 /*
1316  * AD1983 specific
1317  */
1318
1319 /*
1320  * SPDIF mux control for AD1983 auto-parser
1321  */
1322 static int ad1983_auto_smux_enum_info(struct snd_kcontrol *kcontrol,
1323                                       struct snd_ctl_elem_info *uinfo)
1324 {
1325         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1326         struct ad198x_spec *spec = codec->spec;
1327         static const char * const texts2[] = { "PCM", "ADC" };
1328         static const char * const texts3[] = { "PCM", "ADC1", "ADC2" };
1329         hda_nid_t dig_out = spec->gen.multiout.dig_out_nid;
1330         int num_conns = snd_hda_get_num_conns(codec, dig_out);
1331
1332         if (num_conns == 2)
1333                 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts2);
1334         else if (num_conns == 3)
1335                 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
1336         else
1337                 return -EINVAL;
1338 }
1339
1340 static int ad1983_auto_smux_enum_get(struct snd_kcontrol *kcontrol,
1341                                      struct snd_ctl_elem_value *ucontrol)
1342 {
1343         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1344         struct ad198x_spec *spec = codec->spec;
1345
1346         ucontrol->value.enumerated.item[0] = spec->cur_smux;
1347         return 0;
1348 }
1349
1350 static int ad1983_auto_smux_enum_put(struct snd_kcontrol *kcontrol,
1351                                      struct snd_ctl_elem_value *ucontrol)
1352 {
1353         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1354         struct ad198x_spec *spec = codec->spec;
1355         unsigned int val = ucontrol->value.enumerated.item[0];
1356         hda_nid_t dig_out = spec->gen.multiout.dig_out_nid;
1357         int num_conns = snd_hda_get_num_conns(codec, dig_out);
1358
1359         if (val >= num_conns)
1360                 return -EINVAL;
1361         if (spec->cur_smux == val)
1362                 return 0;
1363         spec->cur_smux = val;
1364         snd_hda_codec_write_cache(codec, dig_out, 0,
1365                                   AC_VERB_SET_CONNECT_SEL, val);
1366         return 1;
1367 }
1368
1369 static struct snd_kcontrol_new ad1983_auto_smux_mixer = {
1370         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1371         .name = "IEC958 Playback Source",
1372         .info = ad1983_auto_smux_enum_info,
1373         .get = ad1983_auto_smux_enum_get,
1374         .put = ad1983_auto_smux_enum_put,
1375 };
1376
1377 static int ad1983_add_spdif_mux_ctl(struct hda_codec *codec)
1378 {
1379         struct ad198x_spec *spec = codec->spec;
1380         hda_nid_t dig_out = spec->gen.multiout.dig_out_nid;
1381         int num_conns;
1382
1383         if (!dig_out)
1384                 return 0;
1385         num_conns = snd_hda_get_num_conns(codec, dig_out);
1386         if (num_conns != 2 && num_conns != 3)
1387                 return 0;
1388         if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &ad1983_auto_smux_mixer))
1389                 return -ENOMEM;
1390         return 0;
1391 }
1392
1393 static int patch_ad1983(struct hda_codec *codec)
1394 {
1395         struct ad198x_spec *spec;
1396         int err;
1397
1398         err = alloc_ad_spec(codec);
1399         if (err < 0)
1400                 return err;
1401         spec = codec->spec;
1402
1403         spec->gen.beep_nid = 0x10;
1404         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
1405         err = ad198x_parse_auto_config(codec);
1406         if (err < 0)
1407                 goto error;
1408         err = ad1983_add_spdif_mux_ctl(codec);
1409         if (err < 0)
1410                 goto error;
1411         return 0;
1412
1413  error:
1414         snd_hda_gen_free(codec);
1415         return err;
1416 }
1417
1418
1419 /*
1420  * AD1981 HD specific
1421  */
1422
1423 /* follow EAPD via vmaster hook */
1424 static void ad_vmaster_eapd_hook(void *private_data, int enabled)
1425 {
1426         struct hda_codec *codec = private_data;
1427         struct ad198x_spec *spec = codec->spec;
1428
1429         if (!spec->eapd_nid)
1430                 return;
1431         snd_hda_codec_update_cache(codec, spec->eapd_nid, 0,
1432                                    AC_VERB_SET_EAPD_BTLENABLE,
1433                                    enabled ? 0x02 : 0x00);
1434 }
1435
1436 static void ad1981_fixup_hp_eapd(struct hda_codec *codec,
1437                                  const struct hda_fixup *fix, int action)
1438 {
1439         struct ad198x_spec *spec = codec->spec;
1440
1441         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1442                 spec->gen.vmaster_mute.hook = ad_vmaster_eapd_hook;
1443                 spec->eapd_nid = 0x05;
1444         }
1445 }
1446
1447 /* set the upper-limit for mixer amp to 0dB for avoiding the possible
1448  * damage by overloading
1449  */
1450 static void ad1981_fixup_amp_override(struct hda_codec *codec,
1451                                       const struct hda_fixup *fix, int action)
1452 {
1453         if (action == HDA_FIXUP_ACT_PRE_PROBE)
1454                 snd_hda_override_amp_caps(codec, 0x11, HDA_INPUT,
1455                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
1456                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1457                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1458                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1459 }
1460
1461 enum {
1462         AD1981_FIXUP_AMP_OVERRIDE,
1463         AD1981_FIXUP_HP_EAPD,
1464 };
1465
1466 static const struct hda_fixup ad1981_fixups[] = {
1467         [AD1981_FIXUP_AMP_OVERRIDE] = {
1468                 .type = HDA_FIXUP_FUNC,
1469                 .v.func = ad1981_fixup_amp_override,
1470         },
1471         [AD1981_FIXUP_HP_EAPD] = {
1472                 .type = HDA_FIXUP_FUNC,
1473                 .v.func = ad1981_fixup_hp_eapd,
1474                 .chained = true,
1475                 .chain_id = AD1981_FIXUP_AMP_OVERRIDE,
1476         },
1477 };
1478
1479 static const struct snd_pci_quirk ad1981_fixup_tbl[] = {
1480         SND_PCI_QUIRK_VENDOR(0x1014, "Lenovo", AD1981_FIXUP_AMP_OVERRIDE),
1481         SND_PCI_QUIRK_VENDOR(0x103c, "HP", AD1981_FIXUP_HP_EAPD),
1482         SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo", AD1981_FIXUP_AMP_OVERRIDE),
1483         /* HP nx6320 (reversed SSID, H/W bug) */
1484         SND_PCI_QUIRK(0x30b0, 0x103c, "HP nx6320", AD1981_FIXUP_HP_EAPD),
1485         {}
1486 };
1487
1488 static int patch_ad1981(struct hda_codec *codec)
1489 {
1490         struct ad198x_spec *spec;
1491         int err;
1492
1493         err = alloc_ad_spec(codec);
1494         if (err < 0)
1495                 return -ENOMEM;
1496         spec = codec->spec;
1497
1498         spec->gen.mixer_nid = 0x0e;
1499         spec->gen.beep_nid = 0x10;
1500         set_beep_amp(spec, 0x0d, 0, HDA_OUTPUT);
1501
1502         snd_hda_pick_fixup(codec, NULL, ad1981_fixup_tbl, ad1981_fixups);
1503         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1504
1505         err = ad198x_parse_auto_config(codec);
1506         if (err < 0)
1507                 goto error;
1508         err = ad1983_add_spdif_mux_ctl(codec);
1509         if (err < 0)
1510                 goto error;
1511
1512         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1513
1514         return 0;
1515
1516  error:
1517         snd_hda_gen_free(codec);
1518         return err;
1519 }
1520
1521
1522 /*
1523  * AD1988
1524  *
1525  * Output pins and routes
1526  *
1527  *        Pin               Mix     Sel     DAC (*)
1528  * port-A 0x11 (mute/hp) <- 0x22 <- 0x37 <- 03/04/06
1529  * port-B 0x14 (mute/hp) <- 0x2b <- 0x30 <- 03/04/06
1530  * port-C 0x15 (mute)    <- 0x2c <- 0x31 <- 05/0a
1531  * port-D 0x12 (mute/hp) <- 0x29         <- 04
1532  * port-E 0x17 (mute/hp) <- 0x26 <- 0x32 <- 05/0a
1533  * port-F 0x16 (mute)    <- 0x2a         <- 06
1534  * port-G 0x24 (mute)    <- 0x27         <- 05
1535  * port-H 0x25 (mute)    <- 0x28         <- 0a
1536  * mono   0x13 (mute/amp)<- 0x1e <- 0x36 <- 03/04/06
1537  *
1538  * DAC0 = 03h, DAC1 = 04h, DAC2 = 05h, DAC3 = 06h, DAC4 = 0ah
1539  * (*) DAC2/3/4 are swapped to DAC3/4/2 on AD198A rev.2 due to a h/w bug.
1540  *
1541  * Input pins and routes
1542  *
1543  *        pin     boost   mix input # / adc input #
1544  * port-A 0x11 -> 0x38 -> mix 2, ADC 0
1545  * port-B 0x14 -> 0x39 -> mix 0, ADC 1
1546  * port-C 0x15 -> 0x3a -> 33:0 - mix 1, ADC 2
1547  * port-D 0x12 -> 0x3d -> mix 3, ADC 8
1548  * port-E 0x17 -> 0x3c -> 34:0 - mix 4, ADC 4
1549  * port-F 0x16 -> 0x3b -> mix 5, ADC 3
1550  * port-G 0x24 -> N/A  -> 33:1 - mix 1, 34:1 - mix 4, ADC 6
1551  * port-H 0x25 -> N/A  -> 33:2 - mix 1, 34:2 - mix 4, ADC 7
1552  *
1553  *
1554  * DAC assignment
1555  *   6stack - front/surr/CLFE/side/opt DACs - 04/06/05/0a/03
1556  *   3stack - front/surr/CLFE/opt DACs - 04/05/0a/03
1557  *
1558  * Inputs of Analog Mix (0x20)
1559  *   0:Port-B (front mic)
1560  *   1:Port-C/G/H (line-in)
1561  *   2:Port-A
1562  *   3:Port-D (line-in/2)
1563  *   4:Port-E/G/H (mic-in)
1564  *   5:Port-F (mic2-in)
1565  *   6:CD
1566  *   7:Beep
1567  *
1568  * ADC selection
1569  *   0:Port-A
1570  *   1:Port-B (front mic-in)
1571  *   2:Port-C (line-in)
1572  *   3:Port-F (mic2-in)
1573  *   4:Port-E (mic-in)
1574  *   5:CD
1575  *   6:Port-G
1576  *   7:Port-H
1577  *   8:Port-D (line-in/2)
1578  *   9:Mix
1579  *
1580  * Proposed pin assignments by the datasheet
1581  *
1582  * 6-stack
1583  * Port-A front headphone
1584  *      B front mic-in
1585  *      C rear line-in
1586  *      D rear front-out
1587  *      E rear mic-in
1588  *      F rear surround
1589  *      G rear CLFE
1590  *      H rear side
1591  *
1592  * 3-stack
1593  * Port-A front headphone
1594  *      B front mic
1595  *      C rear line-in/surround
1596  *      D rear front-out
1597  *      E rear mic-in/CLFE
1598  *
1599  * laptop
1600  * Port-A headphone
1601  *      B mic-in
1602  *      C docking station
1603  *      D internal speaker (with EAPD)
1604  *      E/F quad mic array
1605  */
1606
1607 #ifdef ENABLE_AD_STATIC_QUIRKS
1608 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
1609                                struct snd_ctl_elem_info *uinfo)
1610 {
1611         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1612         struct ad198x_spec *spec = codec->spec;
1613         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
1614                                     spec->num_channel_mode);
1615 }
1616
1617 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
1618                               struct snd_ctl_elem_value *ucontrol)
1619 {
1620         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1621         struct ad198x_spec *spec = codec->spec;
1622         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
1623                                    spec->num_channel_mode, spec->multiout.max_channels);
1624 }
1625
1626 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
1627                               struct snd_ctl_elem_value *ucontrol)
1628 {
1629         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1630         struct ad198x_spec *spec = codec->spec;
1631         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
1632                                       spec->num_channel_mode,
1633                                       &spec->multiout.max_channels);
1634         if (err >= 0 && spec->need_dac_fix)
1635                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1636         return err;
1637 }
1638 #endif /* ENABLE_AD_STATIC_QUIRKS */
1639
1640 static int ad1988_auto_smux_enum_info(struct snd_kcontrol *kcontrol,
1641                                       struct snd_ctl_elem_info *uinfo)
1642 {
1643         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1644         static const char * const texts[] = {
1645                 "PCM", "ADC1", "ADC2", "ADC3",
1646         };
1647         int num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1;
1648         if (num_conns > 4)
1649                 num_conns = 4;
1650         return snd_hda_enum_helper_info(kcontrol, uinfo, num_conns, texts);
1651 }
1652
1653 static int ad1988_auto_smux_enum_get(struct snd_kcontrol *kcontrol,
1654                                      struct snd_ctl_elem_value *ucontrol)
1655 {
1656         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1657         struct ad198x_spec *spec = codec->spec;
1658
1659         ucontrol->value.enumerated.item[0] = spec->cur_smux;
1660         return 0;
1661 }
1662
1663 static int ad1988_auto_smux_enum_put(struct snd_kcontrol *kcontrol,
1664                                      struct snd_ctl_elem_value *ucontrol)
1665 {
1666         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1667         struct ad198x_spec *spec = codec->spec;
1668         unsigned int val = ucontrol->value.enumerated.item[0];
1669         struct nid_path *path;
1670         int num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1;
1671
1672         if (val >= num_conns)
1673                 return -EINVAL;
1674         if (spec->cur_smux == val)
1675                 return 0;
1676
1677         mutex_lock(&codec->control_mutex);
1678         codec->cached_write = 1;
1679         path = snd_hda_get_path_from_idx(codec,
1680                                          spec->smux_paths[spec->cur_smux]);
1681         if (path)
1682                 snd_hda_activate_path(codec, path, false, true);
1683         path = snd_hda_get_path_from_idx(codec, spec->smux_paths[val]);
1684         if (path)
1685                 snd_hda_activate_path(codec, path, true, true);
1686         spec->cur_smux = val;
1687         codec->cached_write = 0;
1688         mutex_unlock(&codec->control_mutex);
1689         snd_hda_codec_flush_cache(codec); /* flush the updates */
1690         return 1;
1691 }
1692
1693 static struct snd_kcontrol_new ad1988_auto_smux_mixer = {
1694         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1695         .name = "IEC958 Playback Source",
1696         .info = ad1988_auto_smux_enum_info,
1697         .get = ad1988_auto_smux_enum_get,
1698         .put = ad1988_auto_smux_enum_put,
1699 };
1700
1701 static int ad1988_auto_init(struct hda_codec *codec)
1702 {
1703         struct ad198x_spec *spec = codec->spec;
1704         int i, err;
1705
1706         err = snd_hda_gen_init(codec);
1707         if (err < 0)
1708                 return err;
1709         if (!spec->gen.autocfg.dig_outs)
1710                 return 0;
1711
1712         for (i = 0; i < 4; i++) {
1713                 struct nid_path *path;
1714                 path = snd_hda_get_path_from_idx(codec, spec->smux_paths[i]);
1715                 if (path)
1716                         snd_hda_activate_path(codec, path, path->active, false);
1717         }
1718
1719         return 0;
1720 }
1721
1722 static int ad1988_add_spdif_mux_ctl(struct hda_codec *codec)
1723 {
1724         struct ad198x_spec *spec = codec->spec;
1725         int i, num_conns;
1726         /* we create four static faked paths, since AD codecs have odd
1727          * widget connections regarding the SPDIF out source
1728          */
1729         static struct nid_path fake_paths[4] = {
1730                 {
1731                         .depth = 3,
1732                         .path = { 0x02, 0x1d, 0x1b },
1733                         .idx = { 0, 0, 0 },
1734                         .multi = { 0, 0, 0 },
1735                 },
1736                 {
1737                         .depth = 4,
1738                         .path = { 0x08, 0x0b, 0x1d, 0x1b },
1739                         .idx = { 0, 0, 1, 0 },
1740                         .multi = { 0, 1, 0, 0 },
1741                 },
1742                 {
1743                         .depth = 4,
1744                         .path = { 0x09, 0x0b, 0x1d, 0x1b },
1745                         .idx = { 0, 1, 1, 0 },
1746                         .multi = { 0, 1, 0, 0 },
1747                 },
1748                 {
1749                         .depth = 4,
1750                         .path = { 0x0f, 0x0b, 0x1d, 0x1b },
1751                         .idx = { 0, 2, 1, 0 },
1752                         .multi = { 0, 1, 0, 0 },
1753                 },
1754         };
1755
1756         /* SPDIF source mux appears to be present only on AD1988A */
1757         if (!spec->gen.autocfg.dig_outs ||
1758             get_wcaps_type(get_wcaps(codec, 0x1d)) != AC_WID_AUD_MIX)
1759                 return 0;
1760
1761         num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1;
1762         if (num_conns != 3 && num_conns != 4)
1763                 return 0;
1764
1765         for (i = 0; i < num_conns; i++) {
1766                 struct nid_path *path = snd_array_new(&spec->gen.paths);
1767                 if (!path)
1768                         return -ENOMEM;
1769                 *path = fake_paths[i];
1770                 if (!i)
1771                         path->active = 1;
1772                 spec->smux_paths[i] = snd_hda_get_path_idx(codec, path);
1773         }
1774
1775         if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &ad1988_auto_smux_mixer))
1776                 return -ENOMEM;
1777
1778         codec->patch_ops.init = ad1988_auto_init;
1779
1780         return 0;
1781 }
1782
1783 /*
1784  */
1785
1786 enum {
1787         AD1988_FIXUP_6STACK_DIG,
1788 };
1789
1790 static const struct hda_fixup ad1988_fixups[] = {
1791         [AD1988_FIXUP_6STACK_DIG] = {
1792                 .type = HDA_FIXUP_PINS,
1793                 .v.pins = (const struct hda_pintbl[]) {
1794                         { 0x11, 0x02214130 }, /* front-hp */
1795                         { 0x12, 0x01014010 }, /* line-out */
1796                         { 0x14, 0x02a19122 }, /* front-mic */
1797                         { 0x15, 0x01813021 }, /* line-in */
1798                         { 0x16, 0x01011012 }, /* line-out */
1799                         { 0x17, 0x01a19020 }, /* mic */
1800                         { 0x1b, 0x0145f1f0 }, /* SPDIF */
1801                         { 0x24, 0x01016011 }, /* line-out */
1802                         { 0x25, 0x01012013 }, /* line-out */
1803                         { }
1804                 }
1805         },
1806 };
1807
1808 static const struct hda_model_fixup ad1988_fixup_models[] = {
1809         { .id = AD1988_FIXUP_6STACK_DIG, .name = "6stack-dig" },
1810         {}
1811 };
1812
1813 static int patch_ad1988(struct hda_codec *codec)
1814 {
1815         struct ad198x_spec *spec;
1816         int err;
1817
1818         err = alloc_ad_spec(codec);
1819         if (err < 0)
1820                 return err;
1821         spec = codec->spec;
1822
1823         spec->gen.mixer_nid = 0x20;
1824         spec->gen.mixer_merge_nid = 0x21;
1825         spec->gen.beep_nid = 0x10;
1826         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
1827
1828         snd_hda_pick_fixup(codec, ad1988_fixup_models, NULL, ad1988_fixups);
1829         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1830
1831         err = ad198x_parse_auto_config(codec);
1832         if (err < 0)
1833                 goto error;
1834         err = ad1988_add_spdif_mux_ctl(codec);
1835         if (err < 0)
1836                 goto error;
1837
1838         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1839
1840         return 0;
1841
1842  error:
1843         snd_hda_gen_free(codec);
1844         return err;
1845 }
1846
1847
1848 /*
1849  * AD1884 / AD1984
1850  *
1851  * port-B - front line/mic-in
1852  * port-E - aux in/out
1853  * port-F - aux in/out
1854  * port-C - rear line/mic-in
1855  * port-D - rear line/hp-out
1856  * port-A - front line/hp-out
1857  *
1858  * AD1984 = AD1884 + two digital mic-ins
1859  *
1860  * AD1883 / AD1884A / AD1984A / AD1984B
1861  *
1862  * port-B (0x14) - front mic-in
1863  * port-E (0x1c) - rear mic-in
1864  * port-F (0x16) - CD / ext out
1865  * port-C (0x15) - rear line-in
1866  * port-D (0x12) - rear line-out
1867  * port-A (0x11) - front hp-out
1868  *
1869  * AD1984A = AD1884A + digital-mic
1870  * AD1883 = equivalent with AD1984A
1871  * AD1984B = AD1984A + extra SPDIF-out
1872  */
1873
1874 /* set the upper-limit for mixer amp to 0dB for avoiding the possible
1875  * damage by overloading
1876  */
1877 static void ad1884_fixup_amp_override(struct hda_codec *codec,
1878                                       const struct hda_fixup *fix, int action)
1879 {
1880         if (action == HDA_FIXUP_ACT_PRE_PROBE)
1881                 snd_hda_override_amp_caps(codec, 0x20, HDA_INPUT,
1882                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
1883                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1884                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1885                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1886 }
1887
1888 /* toggle GPIO1 according to the mute state */
1889 static void ad1884_vmaster_hp_gpio_hook(void *private_data, int enabled)
1890 {
1891         struct hda_codec *codec = private_data;
1892         struct ad198x_spec *spec = codec->spec;
1893
1894         if (spec->eapd_nid)
1895                 ad_vmaster_eapd_hook(private_data, enabled);
1896         snd_hda_codec_update_cache(codec, 0x01, 0,
1897                                    AC_VERB_SET_GPIO_DATA,
1898                                    enabled ? 0x00 : 0x02);
1899 }
1900
1901 static void ad1884_fixup_hp_eapd(struct hda_codec *codec,
1902                                  const struct hda_fixup *fix, int action)
1903 {
1904         struct ad198x_spec *spec = codec->spec;
1905         static const struct hda_verb gpio_init_verbs[] = {
1906                 {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
1907                 {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
1908                 {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
1909                 {},
1910         };
1911
1912         switch (action) {
1913         case HDA_FIXUP_ACT_PRE_PROBE:
1914                 spec->gen.vmaster_mute.hook = ad1884_vmaster_hp_gpio_hook;
1915                 snd_hda_sequence_write_cache(codec, gpio_init_verbs);
1916                 break;
1917         case HDA_FIXUP_ACT_PROBE:
1918                 if (spec->gen.autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
1919                         spec->eapd_nid = spec->gen.autocfg.line_out_pins[0];
1920                 else
1921                         spec->eapd_nid = spec->gen.autocfg.speaker_pins[0];
1922                 break;
1923         }
1924 }
1925
1926 /* set magic COEFs for dmic */
1927 static const struct hda_verb ad1884_dmic_init_verbs[] = {
1928         {0x01, AC_VERB_SET_COEF_INDEX, 0x13f7},
1929         {0x01, AC_VERB_SET_PROC_COEF, 0x08},
1930         {}
1931 };
1932
1933 enum {
1934         AD1884_FIXUP_AMP_OVERRIDE,
1935         AD1884_FIXUP_HP_EAPD,
1936         AD1884_FIXUP_DMIC_COEF,
1937         AD1884_FIXUP_HP_TOUCHSMART,
1938 };
1939
1940 static const struct hda_fixup ad1884_fixups[] = {
1941         [AD1884_FIXUP_AMP_OVERRIDE] = {
1942                 .type = HDA_FIXUP_FUNC,
1943                 .v.func = ad1884_fixup_amp_override,
1944         },
1945         [AD1884_FIXUP_HP_EAPD] = {
1946                 .type = HDA_FIXUP_FUNC,
1947                 .v.func = ad1884_fixup_hp_eapd,
1948                 .chained = true,
1949                 .chain_id = AD1884_FIXUP_AMP_OVERRIDE,
1950         },
1951         [AD1884_FIXUP_DMIC_COEF] = {
1952                 .type = HDA_FIXUP_VERBS,
1953                 .v.verbs = ad1884_dmic_init_verbs,
1954         },
1955         [AD1884_FIXUP_HP_TOUCHSMART] = {
1956                 .type = HDA_FIXUP_VERBS,
1957                 .v.verbs = ad1884_dmic_init_verbs,
1958                 .chained = true,
1959                 .chain_id = AD1884_FIXUP_HP_EAPD,
1960         },
1961 };
1962
1963 static const struct snd_pci_quirk ad1884_fixup_tbl[] = {
1964         SND_PCI_QUIRK(0x103c, 0x2a82, "HP Touchsmart", AD1884_FIXUP_HP_TOUCHSMART),
1965         SND_PCI_QUIRK_VENDOR(0x103c, "HP", AD1884_FIXUP_HP_EAPD),
1966         SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo Thinkpad", AD1884_FIXUP_DMIC_COEF),
1967         {}
1968 };
1969
1970
1971 static int patch_ad1884(struct hda_codec *codec)
1972 {
1973         struct ad198x_spec *spec;
1974         int err;
1975
1976         err = alloc_ad_spec(codec);
1977         if (err < 0)
1978                 return err;
1979         spec = codec->spec;
1980
1981         spec->gen.mixer_nid = 0x20;
1982         spec->gen.beep_nid = 0x10;
1983         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
1984
1985         snd_hda_pick_fixup(codec, NULL, ad1884_fixup_tbl, ad1884_fixups);
1986         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1987
1988         err = ad198x_parse_auto_config(codec);
1989         if (err < 0)
1990                 goto error;
1991         err = ad1983_add_spdif_mux_ctl(codec);
1992         if (err < 0)
1993                 goto error;
1994
1995         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1996
1997         return 0;
1998
1999  error:
2000         snd_hda_gen_free(codec);
2001         return err;
2002 }
2003
2004 /*
2005  * AD1882 / AD1882A
2006  *
2007  * port-A - front hp-out
2008  * port-B - front mic-in
2009  * port-C - rear line-in, shared surr-out (3stack)
2010  * port-D - rear line-out
2011  * port-E - rear mic-in, shared clfe-out (3stack)
2012  * port-F - rear surr-out (6stack)
2013  * port-G - rear clfe-out (6stack)
2014  */
2015
2016 static int patch_ad1882(struct hda_codec *codec)
2017 {
2018         struct ad198x_spec *spec;
2019         int err;
2020
2021         err = alloc_ad_spec(codec);
2022         if (err < 0)
2023                 return err;
2024         spec = codec->spec;
2025
2026         spec->gen.mixer_nid = 0x20;
2027         spec->gen.mixer_merge_nid = 0x21;
2028         spec->gen.beep_nid = 0x10;
2029         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
2030         err = ad198x_parse_auto_config(codec);
2031         if (err < 0)
2032                 goto error;
2033         err = ad1988_add_spdif_mux_ctl(codec);
2034         if (err < 0)
2035                 goto error;
2036         return 0;
2037
2038  error:
2039         snd_hda_gen_free(codec);
2040         return err;
2041 }
2042
2043
2044 /*
2045  * patch entries
2046  */
2047 static const struct hda_codec_preset snd_hda_preset_analog[] = {
2048         { .id = 0x11d4184a, .name = "AD1884A", .patch = patch_ad1884 },
2049         { .id = 0x11d41882, .name = "AD1882", .patch = patch_ad1882 },
2050         { .id = 0x11d41883, .name = "AD1883", .patch = patch_ad1884 },
2051         { .id = 0x11d41884, .name = "AD1884", .patch = patch_ad1884 },
2052         { .id = 0x11d4194a, .name = "AD1984A", .patch = patch_ad1884 },
2053         { .id = 0x11d4194b, .name = "AD1984B", .patch = patch_ad1884 },
2054         { .id = 0x11d41981, .name = "AD1981", .patch = patch_ad1981 },
2055         { .id = 0x11d41983, .name = "AD1983", .patch = patch_ad1983 },
2056         { .id = 0x11d41984, .name = "AD1984", .patch = patch_ad1884 },
2057         { .id = 0x11d41986, .name = "AD1986A", .patch = patch_ad1986a },
2058         { .id = 0x11d41988, .name = "AD1988", .patch = patch_ad1988 },
2059         { .id = 0x11d4198b, .name = "AD1988B", .patch = patch_ad1988 },
2060         { .id = 0x11d4882a, .name = "AD1882A", .patch = patch_ad1882 },
2061         { .id = 0x11d4989a, .name = "AD1989A", .patch = patch_ad1988 },
2062         { .id = 0x11d4989b, .name = "AD1989B", .patch = patch_ad1988 },
2063         {} /* terminator */
2064 };
2065
2066 MODULE_ALIAS("snd-hda-codec-id:11d4*");
2067
2068 MODULE_LICENSE("GPL");
2069 MODULE_DESCRIPTION("Analog Devices HD-audio codec");
2070
2071 static struct hda_codec_preset_list analog_list = {
2072         .preset = snd_hda_preset_analog,
2073         .owner = THIS_MODULE,
2074 };
2075
2076 static int __init patch_analog_init(void)
2077 {
2078         return snd_hda_add_codec_preset(&analog_list);
2079 }
2080
2081 static void __exit patch_analog_exit(void)
2082 {
2083         snd_hda_delete_codec_preset(&analog_list);
2084 }
2085
2086 module_init(patch_analog_init)
2087 module_exit(patch_analog_exit)