21011b5199de5d4a24fdfa4d88fff015b4dc67cf
[linux-2.6.git] / sound / pci / hda / patch_analog.c
1 /*
2  * HD audio interface patch for AD1882, AD1884, AD1981HD, AD1983, AD1984,
3  *   AD1986A, AD1988
4  *
5  * Copyright (c) 2005-2007 Takashi Iwai <tiwai@suse.de>
6  *
7  *  This driver is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This driver is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/pci.h>
26
27 #include <sound/core.h>
28 #include "hda_codec.h"
29 #include "hda_local.h"
30 #include "hda_beep.h"
31
32 struct ad198x_spec {
33         struct snd_kcontrol_new *mixers[5];
34         int num_mixers;
35         unsigned int beep_amp;  /* beep amp value, set via set_beep_amp() */
36         const struct hda_verb *init_verbs[5];   /* initialization verbs
37                                                  * don't forget NULL termination!
38                                                  */
39         unsigned int num_init_verbs;
40
41         /* playback */
42         struct hda_multi_out multiout;  /* playback set-up
43                                          * max_channels, dacs must be set
44                                          * dig_out_nid and hp_nid are optional
45                                          */
46         unsigned int cur_eapd;
47         unsigned int need_dac_fix;
48
49         /* capture */
50         unsigned int num_adc_nids;
51         hda_nid_t *adc_nids;
52         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
53
54         /* capture source */
55         const struct hda_input_mux *input_mux;
56         hda_nid_t *capsrc_nids;
57         unsigned int cur_mux[3];
58
59         /* channel model */
60         const struct hda_channel_mode *channel_mode;
61         int num_channel_mode;
62
63         /* PCM information */
64         struct hda_pcm pcm_rec[3];      /* used in alc_build_pcms() */
65
66         unsigned int spdif_route;
67
68         /* dynamic controls, init_verbs and input_mux */
69         struct auto_pin_cfg autocfg;
70         struct snd_array kctls;
71         struct hda_input_mux private_imux;
72         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
73
74         unsigned int jack_present :1;
75         unsigned int inv_jack_detect:1; /* inverted jack-detection */
76         unsigned int inv_eapd:1;        /* inverted EAPD implementation */
77
78 #ifdef CONFIG_SND_HDA_POWER_SAVE
79         struct hda_loopback_check loopback;
80 #endif
81         /* for virtual master */
82         hda_nid_t vmaster_nid;
83         const char **slave_vols;
84         const char **slave_sws;
85 };
86
87 /*
88  * input MUX handling (common part)
89  */
90 static int ad198x_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
91 {
92         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
93         struct ad198x_spec *spec = codec->spec;
94
95         return snd_hda_input_mux_info(spec->input_mux, uinfo);
96 }
97
98 static int ad198x_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
99 {
100         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
101         struct ad198x_spec *spec = codec->spec;
102         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
103
104         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
105         return 0;
106 }
107
108 static int ad198x_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
109 {
110         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
111         struct ad198x_spec *spec = codec->spec;
112         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
113
114         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
115                                      spec->capsrc_nids[adc_idx],
116                                      &spec->cur_mux[adc_idx]);
117 }
118
119 /*
120  * initialization (common callbacks)
121  */
122 static int ad198x_init(struct hda_codec *codec)
123 {
124         struct ad198x_spec *spec = codec->spec;
125         int i;
126
127         for (i = 0; i < spec->num_init_verbs; i++)
128                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
129         return 0;
130 }
131
132 static const char *ad_slave_vols[] = {
133         "Front Playback Volume",
134         "Surround Playback Volume",
135         "Center Playback Volume",
136         "LFE Playback Volume",
137         "Side Playback Volume",
138         "Headphone Playback Volume",
139         "Mono Playback Volume",
140         "Speaker Playback Volume",
141         "IEC958 Playback Volume",
142         NULL
143 };
144
145 static const char *ad_slave_sws[] = {
146         "Front Playback Switch",
147         "Surround Playback Switch",
148         "Center Playback Switch",
149         "LFE Playback Switch",
150         "Side Playback Switch",
151         "Headphone Playback Switch",
152         "Mono Playback Switch",
153         "Speaker Playback Switch",
154         "IEC958 Playback Switch",
155         NULL
156 };
157
158 static void ad198x_free_kctls(struct hda_codec *codec);
159
160 #ifdef CONFIG_SND_HDA_INPUT_BEEP
161 /* additional beep mixers; the actual parameters are overwritten at build */
162 static struct snd_kcontrol_new ad_beep_mixer[] = {
163         HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_OUTPUT),
164         HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_OUTPUT),
165         { } /* end */
166 };
167
168 #define set_beep_amp(spec, nid, idx, dir) \
169         ((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir)) /* mono */
170 #else
171 #define set_beep_amp(spec, nid, idx, dir) /* NOP */
172 #endif
173
174 static int ad198x_build_controls(struct hda_codec *codec)
175 {
176         struct ad198x_spec *spec = codec->spec;
177         struct snd_kcontrol *kctl;
178         unsigned int i;
179         int err;
180
181         for (i = 0; i < spec->num_mixers; i++) {
182                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
183                 if (err < 0)
184                         return err;
185         }
186         if (spec->multiout.dig_out_nid) {
187                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
188                 if (err < 0)
189                         return err;
190                 err = snd_hda_create_spdif_share_sw(codec,
191                                                     &spec->multiout);
192                 if (err < 0)
193                         return err;
194                 spec->multiout.share_spdif = 1;
195         } 
196         if (spec->dig_in_nid) {
197                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
198                 if (err < 0)
199                         return err;
200         }
201
202         /* create beep controls if needed */
203 #ifdef CONFIG_SND_HDA_INPUT_BEEP
204         if (spec->beep_amp) {
205                 struct snd_kcontrol_new *knew;
206                 for (knew = ad_beep_mixer; knew->name; knew++) {
207                         struct snd_kcontrol *kctl;
208                         kctl = snd_ctl_new1(knew, codec);
209                         if (!kctl)
210                                 return -ENOMEM;
211                         kctl->private_value = spec->beep_amp;
212                         err = snd_hda_ctl_add(codec, 0, kctl);
213                         if (err < 0)
214                                 return err;
215                 }
216         }
217 #endif
218
219         /* if we have no master control, let's create it */
220         if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
221                 unsigned int vmaster_tlv[4];
222                 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
223                                         HDA_OUTPUT, vmaster_tlv);
224                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
225                                           vmaster_tlv,
226                                           (spec->slave_vols ?
227                                            spec->slave_vols : ad_slave_vols));
228                 if (err < 0)
229                         return err;
230         }
231         if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
232                 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
233                                           NULL,
234                                           (spec->slave_sws ?
235                                            spec->slave_sws : ad_slave_sws));
236                 if (err < 0)
237                         return err;
238         }
239
240         ad198x_free_kctls(codec); /* no longer needed */
241
242         /* assign Capture Source enums to NID */
243         kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
244         if (!kctl)
245                 kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
246         for (i = 0; kctl && i < kctl->count; i++) {
247                 err = snd_hda_add_nid(codec, kctl, i, spec->capsrc_nids[i]);
248                 if (err < 0)
249                         return err;
250         }
251
252         /* assign IEC958 enums to NID */
253         kctl = snd_hda_find_mixer_ctl(codec,
254                         SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source");
255         if (kctl) {
256                 err = snd_hda_add_nid(codec, kctl, 0,
257                                       spec->multiout.dig_out_nid);
258                 if (err < 0)
259                         return err;
260         }
261
262         return 0;
263 }
264
265 #ifdef CONFIG_SND_HDA_POWER_SAVE
266 static int ad198x_check_power_status(struct hda_codec *codec, hda_nid_t nid)
267 {
268         struct ad198x_spec *spec = codec->spec;
269         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
270 }
271 #endif
272
273 /*
274  * Analog playback callbacks
275  */
276 static int ad198x_playback_pcm_open(struct hda_pcm_stream *hinfo,
277                                     struct hda_codec *codec,
278                                     struct snd_pcm_substream *substream)
279 {
280         struct ad198x_spec *spec = codec->spec;
281         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
282                                              hinfo);
283 }
284
285 static int ad198x_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
286                                        struct hda_codec *codec,
287                                        unsigned int stream_tag,
288                                        unsigned int format,
289                                        struct snd_pcm_substream *substream)
290 {
291         struct ad198x_spec *spec = codec->spec;
292         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
293                                                 format, substream);
294 }
295
296 static int ad198x_playback_pcm_cleanup(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_cleanup(codec, &spec->multiout);
302 }
303
304 /*
305  * Digital out
306  */
307 static int ad198x_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
308                                         struct hda_codec *codec,
309                                         struct snd_pcm_substream *substream)
310 {
311         struct ad198x_spec *spec = codec->spec;
312         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
313 }
314
315 static int ad198x_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
316                                          struct hda_codec *codec,
317                                          struct snd_pcm_substream *substream)
318 {
319         struct ad198x_spec *spec = codec->spec;
320         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
321 }
322
323 static int ad198x_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
324                                            struct hda_codec *codec,
325                                            unsigned int stream_tag,
326                                            unsigned int format,
327                                            struct snd_pcm_substream *substream)
328 {
329         struct ad198x_spec *spec = codec->spec;
330         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
331                                              format, substream);
332 }
333
334 static int ad198x_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
335                                            struct hda_codec *codec,
336                                            struct snd_pcm_substream *substream)
337 {
338         struct ad198x_spec *spec = codec->spec;
339         return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
340 }
341
342 /*
343  * Analog capture
344  */
345 static int ad198x_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
346                                       struct hda_codec *codec,
347                                       unsigned int stream_tag,
348                                       unsigned int format,
349                                       struct snd_pcm_substream *substream)
350 {
351         struct ad198x_spec *spec = codec->spec;
352         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
353                                    stream_tag, 0, format);
354         return 0;
355 }
356
357 static int ad198x_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
358                                       struct hda_codec *codec,
359                                       struct snd_pcm_substream *substream)
360 {
361         struct ad198x_spec *spec = codec->spec;
362         snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
363         return 0;
364 }
365
366
367 /*
368  */
369 static struct hda_pcm_stream ad198x_pcm_analog_playback = {
370         .substreams = 1,
371         .channels_min = 2,
372         .channels_max = 6, /* changed later */
373         .nid = 0, /* fill later */
374         .ops = {
375                 .open = ad198x_playback_pcm_open,
376                 .prepare = ad198x_playback_pcm_prepare,
377                 .cleanup = ad198x_playback_pcm_cleanup
378         },
379 };
380
381 static struct hda_pcm_stream ad198x_pcm_analog_capture = {
382         .substreams = 1,
383         .channels_min = 2,
384         .channels_max = 2,
385         .nid = 0, /* fill later */
386         .ops = {
387                 .prepare = ad198x_capture_pcm_prepare,
388                 .cleanup = ad198x_capture_pcm_cleanup
389         },
390 };
391
392 static struct hda_pcm_stream ad198x_pcm_digital_playback = {
393         .substreams = 1,
394         .channels_min = 2,
395         .channels_max = 2,
396         .nid = 0, /* fill later */
397         .ops = {
398                 .open = ad198x_dig_playback_pcm_open,
399                 .close = ad198x_dig_playback_pcm_close,
400                 .prepare = ad198x_dig_playback_pcm_prepare,
401                 .cleanup = ad198x_dig_playback_pcm_cleanup
402         },
403 };
404
405 static struct hda_pcm_stream ad198x_pcm_digital_capture = {
406         .substreams = 1,
407         .channels_min = 2,
408         .channels_max = 2,
409         /* NID is set in alc_build_pcms */
410 };
411
412 static int ad198x_build_pcms(struct hda_codec *codec)
413 {
414         struct ad198x_spec *spec = codec->spec;
415         struct hda_pcm *info = spec->pcm_rec;
416
417         codec->num_pcms = 1;
418         codec->pcm_info = info;
419
420         info->name = "AD198x Analog";
421         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ad198x_pcm_analog_playback;
422         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->multiout.max_channels;
423         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
424         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad198x_pcm_analog_capture;
425         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adc_nids;
426         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
427
428         if (spec->multiout.dig_out_nid) {
429                 info++;
430                 codec->num_pcms++;
431                 info->name = "AD198x Digital";
432                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
433                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ad198x_pcm_digital_playback;
434                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
435                 if (spec->dig_in_nid) {
436                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad198x_pcm_digital_capture;
437                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
438                 }
439         }
440
441         return 0;
442 }
443
444 static inline void ad198x_shutup(struct hda_codec *codec)
445 {
446         snd_hda_shutup_pins(codec);
447 }
448
449 static void ad198x_free_kctls(struct hda_codec *codec)
450 {
451         struct ad198x_spec *spec = codec->spec;
452
453         if (spec->kctls.list) {
454                 struct snd_kcontrol_new *kctl = spec->kctls.list;
455                 int i;
456                 for (i = 0; i < spec->kctls.used; i++)
457                         kfree(kctl[i].name);
458         }
459         snd_array_free(&spec->kctls);
460 }
461
462 static void ad198x_power_eapd_write(struct hda_codec *codec, hda_nid_t front,
463                                 hda_nid_t hp)
464 {
465         struct ad198x_spec *spec = codec->spec;
466         snd_hda_codec_write(codec, front, 0, AC_VERB_SET_EAPD_BTLENABLE,
467                             !spec->inv_eapd ? 0x00 : 0x02);
468         snd_hda_codec_write(codec, hp, 0, AC_VERB_SET_EAPD_BTLENABLE,
469                             !spec->inv_eapd ? 0x00 : 0x02);
470 }
471
472 static void ad198x_power_eapd(struct hda_codec *codec)
473 {
474         /* We currently only handle front, HP */
475         switch (codec->vendor_id) {
476         case 0x11d41882:
477         case 0x11d4882a:
478         case 0x11d41884:
479         case 0x11d41984:
480         case 0x11d41883:
481         case 0x11d4184a:
482         case 0x11d4194a:
483         case 0x11d4194b:
484                 ad198x_power_eapd_write(codec, 0x12, 0x11);
485                 break;
486         case 0x11d41981:
487         case 0x11d41983:
488                 ad198x_power_eapd_write(codec, 0x05, 0x06);
489                 break;
490         case 0x11d41986:
491                 ad198x_power_eapd_write(codec, 0x1b, 0x1a);
492                 break;
493         case 0x11d41988:
494         case 0x11d4198b:
495         case 0x11d4989a:
496         case 0x11d4989b:
497                 ad198x_power_eapd_write(codec, 0x29, 0x22);
498                 break;
499         }
500 }
501
502 static void ad198x_free(struct hda_codec *codec)
503 {
504         struct ad198x_spec *spec = codec->spec;
505
506         if (!spec)
507                 return;
508
509         ad198x_shutup(codec);
510         ad198x_free_kctls(codec);
511         kfree(spec);
512         snd_hda_detach_beep_device(codec);
513 }
514
515 #ifdef SND_HDA_NEEDS_RESUME
516 static int ad198x_suspend(struct hda_codec *codec, pm_message_t state)
517 {
518         ad198x_shutup(codec);
519         ad198x_power_eapd(codec);
520         return 0;
521 }
522
523 static int ad198x_resume(struct hda_codec *codec)
524 {
525         ad198x_init(codec);
526         snd_hda_codec_resume_amp(codec);
527         snd_hda_codec_resume_cache(codec);
528         return 0;
529 }
530 #endif
531
532 static struct hda_codec_ops ad198x_patch_ops = {
533         .build_controls = ad198x_build_controls,
534         .build_pcms = ad198x_build_pcms,
535         .init = ad198x_init,
536         .free = ad198x_free,
537 #ifdef CONFIG_SND_HDA_POWER_SAVE
538         .check_power_status = ad198x_check_power_status,
539 #endif
540 #ifdef SND_HDA_NEEDS_RESUME
541         .suspend = ad198x_suspend,
542         .resume = ad198x_resume,
543 #endif
544         .reboot_notify = ad198x_shutup,
545 };
546
547
548 /*
549  * EAPD control
550  * the private value = nid
551  */
552 #define ad198x_eapd_info        snd_ctl_boolean_mono_info
553
554 static int ad198x_eapd_get(struct snd_kcontrol *kcontrol,
555                            struct snd_ctl_elem_value *ucontrol)
556 {
557         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
558         struct ad198x_spec *spec = codec->spec;
559         if (spec->inv_eapd)
560                 ucontrol->value.integer.value[0] = ! spec->cur_eapd;
561         else
562                 ucontrol->value.integer.value[0] = spec->cur_eapd;
563         return 0;
564 }
565
566 static int ad198x_eapd_put(struct snd_kcontrol *kcontrol,
567                            struct snd_ctl_elem_value *ucontrol)
568 {
569         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
570         struct ad198x_spec *spec = codec->spec;
571         hda_nid_t nid = kcontrol->private_value & 0xff;
572         unsigned int eapd;
573         eapd = !!ucontrol->value.integer.value[0];
574         if (spec->inv_eapd)
575                 eapd = !eapd;
576         if (eapd == spec->cur_eapd)
577                 return 0;
578         spec->cur_eapd = eapd;
579         snd_hda_codec_write_cache(codec, nid,
580                                   0, AC_VERB_SET_EAPD_BTLENABLE,
581                                   eapd ? 0x02 : 0x00);
582         return 1;
583 }
584
585 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
586                                struct snd_ctl_elem_info *uinfo);
587 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
588                               struct snd_ctl_elem_value *ucontrol);
589 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
590                               struct snd_ctl_elem_value *ucontrol);
591
592
593 /*
594  * AD1986A specific
595  */
596
597 #define AD1986A_SPDIF_OUT       0x02
598 #define AD1986A_FRONT_DAC       0x03
599 #define AD1986A_SURR_DAC        0x04
600 #define AD1986A_CLFE_DAC        0x05
601 #define AD1986A_ADC             0x06
602
603 static hda_nid_t ad1986a_dac_nids[3] = {
604         AD1986A_FRONT_DAC, AD1986A_SURR_DAC, AD1986A_CLFE_DAC
605 };
606 static hda_nid_t ad1986a_adc_nids[1] = { AD1986A_ADC };
607 static hda_nid_t ad1986a_capsrc_nids[1] = { 0x12 };
608
609 static struct hda_input_mux ad1986a_capture_source = {
610         .num_items = 7,
611         .items = {
612                 { "Mic", 0x0 },
613                 { "CD", 0x1 },
614                 { "Aux", 0x3 },
615                 { "Line", 0x4 },
616                 { "Mix", 0x5 },
617                 { "Mono", 0x6 },
618                 { "Phone", 0x7 },
619         },
620 };
621
622
623 static struct hda_bind_ctls ad1986a_bind_pcm_vol = {
624         .ops = &snd_hda_bind_vol,
625         .values = {
626                 HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT),
627                 HDA_COMPOSE_AMP_VAL(AD1986A_SURR_DAC, 3, 0, HDA_OUTPUT),
628                 HDA_COMPOSE_AMP_VAL(AD1986A_CLFE_DAC, 3, 0, HDA_OUTPUT),
629                 0
630         },
631 };
632
633 static struct hda_bind_ctls ad1986a_bind_pcm_sw = {
634         .ops = &snd_hda_bind_sw,
635         .values = {
636                 HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT),
637                 HDA_COMPOSE_AMP_VAL(AD1986A_SURR_DAC, 3, 0, HDA_OUTPUT),
638                 HDA_COMPOSE_AMP_VAL(AD1986A_CLFE_DAC, 3, 0, HDA_OUTPUT),
639                 0
640         },
641 };
642
643 /*
644  * mixers
645  */
646 static struct snd_kcontrol_new ad1986a_mixers[] = {
647         /*
648          * bind volumes/mutes of 3 DACs as a single PCM control for simplicity
649          */
650         HDA_BIND_VOL("PCM Playback Volume", &ad1986a_bind_pcm_vol),
651         HDA_BIND_SW("PCM Playback Switch", &ad1986a_bind_pcm_sw),
652         HDA_CODEC_VOLUME("Front Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
653         HDA_CODEC_MUTE("Front Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
654         HDA_CODEC_VOLUME("Surround Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
655         HDA_CODEC_MUTE("Surround Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
656         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x1d, 1, 0x0, HDA_OUTPUT),
657         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x1d, 2, 0x0, HDA_OUTPUT),
658         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x1d, 1, 0x0, HDA_OUTPUT),
659         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x1d, 2, 0x0, HDA_OUTPUT),
660         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x1a, 0x0, HDA_OUTPUT),
661         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1a, 0x0, HDA_OUTPUT),
662         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
663         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
664         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
665         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
666         HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
667         HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
668         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
669         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
670         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
671         HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
672         HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT),
673         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
674         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
675         {
676                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
677                 .name = "Capture Source",
678                 .info = ad198x_mux_enum_info,
679                 .get = ad198x_mux_enum_get,
680                 .put = ad198x_mux_enum_put,
681         },
682         HDA_CODEC_MUTE("Stereo Downmix Switch", 0x09, 0x0, HDA_OUTPUT),
683         { } /* end */
684 };
685
686 /* additional mixers for 3stack mode */
687 static struct snd_kcontrol_new ad1986a_3st_mixers[] = {
688         {
689                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
690                 .name = "Channel Mode",
691                 .info = ad198x_ch_mode_info,
692                 .get = ad198x_ch_mode_get,
693                 .put = ad198x_ch_mode_put,
694         },
695         { } /* end */
696 };
697
698 /* laptop model - 2ch only */
699 static hda_nid_t ad1986a_laptop_dac_nids[1] = { AD1986A_FRONT_DAC };
700
701 /* master controls both pins 0x1a and 0x1b */
702 static struct hda_bind_ctls ad1986a_laptop_master_vol = {
703         .ops = &snd_hda_bind_vol,
704         .values = {
705                 HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
706                 HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
707                 0,
708         },
709 };
710
711 static struct hda_bind_ctls ad1986a_laptop_master_sw = {
712         .ops = &snd_hda_bind_sw,
713         .values = {
714                 HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
715                 HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
716                 0,
717         },
718 };
719
720 static struct snd_kcontrol_new ad1986a_laptop_mixers[] = {
721         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
722         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
723         HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
724         HDA_BIND_SW("Master Playback Switch", &ad1986a_laptop_master_sw),
725         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
726         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
727         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
728         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
729         HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
730         HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
731         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
732         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
733         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
734         /* 
735            HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
736            HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT), */
737         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
738         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
739         {
740                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
741                 .name = "Capture Source",
742                 .info = ad198x_mux_enum_info,
743                 .get = ad198x_mux_enum_get,
744                 .put = ad198x_mux_enum_put,
745         },
746         { } /* end */
747 };
748
749 /* laptop-eapd model - 2ch only */
750
751 static struct hda_input_mux ad1986a_laptop_eapd_capture_source = {
752         .num_items = 3,
753         .items = {
754                 { "Mic", 0x0 },
755                 { "Internal Mic", 0x4 },
756                 { "Mix", 0x5 },
757         },
758 };
759
760 static struct hda_input_mux ad1986a_automic_capture_source = {
761         .num_items = 2,
762         .items = {
763                 { "Mic", 0x0 },
764                 { "Mix", 0x5 },
765         },
766 };
767
768 static struct snd_kcontrol_new ad1986a_laptop_master_mixers[] = {
769         HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
770         HDA_BIND_SW("Master Playback Switch", &ad1986a_laptop_master_sw),
771         { } /* end */
772 };
773
774 static struct snd_kcontrol_new ad1986a_laptop_eapd_mixers[] = {
775         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
776         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
777         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
778         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
779         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
780         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
781         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
782         {
783                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
784                 .name = "Capture Source",
785                 .info = ad198x_mux_enum_info,
786                 .get = ad198x_mux_enum_get,
787                 .put = ad198x_mux_enum_put,
788         },
789         {
790                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
791                 .name = "External Amplifier",
792                 .subdevice = HDA_SUBDEV_NID_FLAG | 0x1b,
793                 .info = ad198x_eapd_info,
794                 .get = ad198x_eapd_get,
795                 .put = ad198x_eapd_put,
796                 .private_value = 0x1b, /* port-D */
797         },
798         { } /* end */
799 };
800
801 static struct snd_kcontrol_new ad1986a_laptop_intmic_mixers[] = {
802         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0, HDA_OUTPUT),
803         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0, HDA_OUTPUT),
804         { } /* end */
805 };
806
807 /* re-connect the mic boost input according to the jack sensing */
808 static void ad1986a_automic(struct hda_codec *codec)
809 {
810         unsigned int present;
811         present = snd_hda_jack_detect(codec, 0x1f);
812         /* 0 = 0x1f, 2 = 0x1d, 4 = mixed */
813         snd_hda_codec_write(codec, 0x0f, 0, AC_VERB_SET_CONNECT_SEL,
814                             present ? 0 : 2);
815 }
816
817 #define AD1986A_MIC_EVENT               0x36
818
819 static void ad1986a_automic_unsol_event(struct hda_codec *codec,
820                                             unsigned int res)
821 {
822         if ((res >> 26) != AD1986A_MIC_EVENT)
823                 return;
824         ad1986a_automic(codec);
825 }
826
827 static int ad1986a_automic_init(struct hda_codec *codec)
828 {
829         ad198x_init(codec);
830         ad1986a_automic(codec);
831         return 0;
832 }
833
834 /* laptop-automute - 2ch only */
835
836 static void ad1986a_update_hp(struct hda_codec *codec)
837 {
838         struct ad198x_spec *spec = codec->spec;
839         unsigned int mute;
840
841         if (spec->jack_present)
842                 mute = HDA_AMP_MUTE; /* mute internal speaker */
843         else
844                 /* unmute internal speaker if necessary */
845                 mute = snd_hda_codec_amp_read(codec, 0x1a, 0, HDA_OUTPUT, 0);
846         snd_hda_codec_amp_stereo(codec, 0x1b, HDA_OUTPUT, 0,
847                                  HDA_AMP_MUTE, mute);
848 }
849
850 static void ad1986a_hp_automute(struct hda_codec *codec)
851 {
852         struct ad198x_spec *spec = codec->spec;
853
854         spec->jack_present = snd_hda_jack_detect(codec, 0x1a);
855         if (spec->inv_jack_detect)
856                 spec->jack_present = !spec->jack_present;
857         ad1986a_update_hp(codec);
858 }
859
860 #define AD1986A_HP_EVENT                0x37
861
862 static void ad1986a_hp_unsol_event(struct hda_codec *codec, unsigned int res)
863 {
864         if ((res >> 26) != AD1986A_HP_EVENT)
865                 return;
866         ad1986a_hp_automute(codec);
867 }
868
869 static int ad1986a_hp_init(struct hda_codec *codec)
870 {
871         ad198x_init(codec);
872         ad1986a_hp_automute(codec);
873         return 0;
874 }
875
876 /* bind hp and internal speaker mute (with plug check) */
877 static int ad1986a_hp_master_sw_put(struct snd_kcontrol *kcontrol,
878                                     struct snd_ctl_elem_value *ucontrol)
879 {
880         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
881         long *valp = ucontrol->value.integer.value;
882         int change;
883
884         change = snd_hda_codec_amp_update(codec, 0x1a, 0, HDA_OUTPUT, 0,
885                                           HDA_AMP_MUTE,
886                                           valp[0] ? 0 : HDA_AMP_MUTE);
887         change |= snd_hda_codec_amp_update(codec, 0x1a, 1, HDA_OUTPUT, 0,
888                                            HDA_AMP_MUTE,
889                                            valp[1] ? 0 : HDA_AMP_MUTE);
890         if (change)
891                 ad1986a_update_hp(codec);
892         return change;
893 }
894
895 static struct snd_kcontrol_new ad1986a_automute_master_mixers[] = {
896         HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
897         {
898                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
899                 .name = "Master Playback Switch",
900                 .subdevice = HDA_SUBDEV_AMP_FLAG,
901                 .info = snd_hda_mixer_amp_switch_info,
902                 .get = snd_hda_mixer_amp_switch_get,
903                 .put = ad1986a_hp_master_sw_put,
904                 .private_value = HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
905         },
906         { } /* end */
907 };
908
909
910 /*
911  * initialization verbs
912  */
913 static struct hda_verb ad1986a_init_verbs[] = {
914         /* Front, Surround, CLFE DAC; mute as default */
915         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
916         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
917         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
918         /* Downmix - off */
919         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
920         /* HP, Line-Out, Surround, CLFE selectors */
921         {0x0a, AC_VERB_SET_CONNECT_SEL, 0x0},
922         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x0},
923         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
924         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
925         /* Mono selector */
926         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x0},
927         /* Mic selector: Mic 1/2 pin */
928         {0x0f, AC_VERB_SET_CONNECT_SEL, 0x0},
929         /* Line-in selector: Line-in */
930         {0x10, AC_VERB_SET_CONNECT_SEL, 0x0},
931         /* Mic 1/2 swap */
932         {0x11, AC_VERB_SET_CONNECT_SEL, 0x0},
933         /* Record selector: mic */
934         {0x12, AC_VERB_SET_CONNECT_SEL, 0x0},
935         /* Mic, Phone, CD, Aux, Line-In amp; mute as default */
936         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
937         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
938         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
939         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
940         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
941         /* PC beep */
942         {0x18, AC_VERB_SET_CONNECT_SEL, 0x0},
943         /* HP, Line-Out, Surround, CLFE, Mono pins; mute as default */
944         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
945         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
946         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
947         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
948         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
949         /* HP Pin */
950         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
951         /* Front, Surround, CLFE Pins */
952         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
953         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
954         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
955         /* Mono Pin */
956         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
957         /* Mic Pin */
958         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
959         /* Line, Aux, CD, Beep-In Pin */
960         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
961         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
962         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
963         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
964         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
965         { } /* end */
966 };
967
968 static struct hda_verb ad1986a_ch2_init[] = {
969         /* Surround out -> Line In */
970         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
971         /* Line-in selectors */
972         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x1 },
973         /* CLFE -> Mic in */
974         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
975         /* Mic selector, mix C/LFE (backmic) and Mic (frontmic) */
976         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x4 },
977         { } /* end */
978 };
979
980 static struct hda_verb ad1986a_ch4_init[] = {
981         /* Surround out -> Surround */
982         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
983         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
984         /* CLFE -> Mic in */
985         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
986         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x4 },
987         { } /* end */
988 };
989
990 static struct hda_verb ad1986a_ch6_init[] = {
991         /* Surround out -> Surround out */
992         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
993         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
994         /* CLFE -> CLFE */
995         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
996         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x0 },
997         { } /* end */
998 };
999
1000 static struct hda_channel_mode ad1986a_modes[3] = {
1001         { 2, ad1986a_ch2_init },
1002         { 4, ad1986a_ch4_init },
1003         { 6, ad1986a_ch6_init },
1004 };
1005
1006 /* eapd initialization */
1007 static struct hda_verb ad1986a_eapd_init_verbs[] = {
1008         {0x1b, AC_VERB_SET_EAPD_BTLENABLE, 0x00 },
1009         {}
1010 };
1011
1012 static struct hda_verb ad1986a_automic_verbs[] = {
1013         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1014         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1015         /*{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},*/
1016         {0x0f, AC_VERB_SET_CONNECT_SEL, 0x0},
1017         {0x1f, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1986A_MIC_EVENT},
1018         {}
1019 };
1020
1021 /* Ultra initialization */
1022 static struct hda_verb ad1986a_ultra_init[] = {
1023         /* eapd initialization */
1024         { 0x1b, AC_VERB_SET_EAPD_BTLENABLE, 0x00 },
1025         /* CLFE -> Mic in */
1026         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x2 },
1027         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1028         { 0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080 },
1029         { } /* end */
1030 };
1031
1032 /* pin sensing on HP jack */
1033 static struct hda_verb ad1986a_hp_init_verbs[] = {
1034         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1986A_HP_EVENT},
1035         {}
1036 };
1037
1038 static void ad1986a_samsung_p50_unsol_event(struct hda_codec *codec,
1039                                             unsigned int res)
1040 {
1041         switch (res >> 26) {
1042         case AD1986A_HP_EVENT:
1043                 ad1986a_hp_automute(codec);
1044                 break;
1045         case AD1986A_MIC_EVENT:
1046                 ad1986a_automic(codec);
1047                 break;
1048         }
1049 }
1050
1051 static int ad1986a_samsung_p50_init(struct hda_codec *codec)
1052 {
1053         ad198x_init(codec);
1054         ad1986a_hp_automute(codec);
1055         ad1986a_automic(codec);
1056         return 0;
1057 }
1058
1059
1060 /* models */
1061 enum {
1062         AD1986A_6STACK,
1063         AD1986A_3STACK,
1064         AD1986A_LAPTOP,
1065         AD1986A_LAPTOP_EAPD,
1066         AD1986A_LAPTOP_AUTOMUTE,
1067         AD1986A_ULTRA,
1068         AD1986A_SAMSUNG,
1069         AD1986A_SAMSUNG_P50,
1070         AD1986A_MODELS
1071 };
1072
1073 static const char *ad1986a_models[AD1986A_MODELS] = {
1074         [AD1986A_6STACK]        = "6stack",
1075         [AD1986A_3STACK]        = "3stack",
1076         [AD1986A_LAPTOP]        = "laptop",
1077         [AD1986A_LAPTOP_EAPD]   = "laptop-eapd",
1078         [AD1986A_LAPTOP_AUTOMUTE] = "laptop-automute",
1079         [AD1986A_ULTRA]         = "ultra",
1080         [AD1986A_SAMSUNG]       = "samsung",
1081         [AD1986A_SAMSUNG_P50]   = "samsung-p50",
1082 };
1083
1084 static struct snd_pci_quirk ad1986a_cfg_tbl[] = {
1085         SND_PCI_QUIRK(0x103c, 0x30af, "HP B2800", AD1986A_LAPTOP_EAPD),
1086         SND_PCI_QUIRK(0x1043, 0x1153, "ASUS M9", AD1986A_LAPTOP_EAPD),
1087         SND_PCI_QUIRK(0x1043, 0x11f7, "ASUS U5A", AD1986A_LAPTOP_EAPD),
1088         SND_PCI_QUIRK(0x1043, 0x1213, "ASUS A6J", AD1986A_LAPTOP_EAPD),
1089         SND_PCI_QUIRK(0x1043, 0x1263, "ASUS U5F", AD1986A_LAPTOP_EAPD),
1090         SND_PCI_QUIRK(0x1043, 0x1297, "ASUS Z62F", AD1986A_LAPTOP_EAPD),
1091         SND_PCI_QUIRK(0x1043, 0x12b3, "ASUS V1j", AD1986A_LAPTOP_EAPD),
1092         SND_PCI_QUIRK(0x1043, 0x1302, "ASUS W3j", AD1986A_LAPTOP_EAPD),
1093         SND_PCI_QUIRK(0x1043, 0x1443, "ASUS VX1", AD1986A_LAPTOP),
1094         SND_PCI_QUIRK(0x1043, 0x1447, "ASUS A8J", AD1986A_3STACK),
1095         SND_PCI_QUIRK(0x1043, 0x817f, "ASUS P5", AD1986A_3STACK),
1096         SND_PCI_QUIRK(0x1043, 0x818f, "ASUS P5", AD1986A_LAPTOP),
1097         SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS P5", AD1986A_3STACK),
1098         SND_PCI_QUIRK(0x1043, 0x81cb, "ASUS M2N", AD1986A_3STACK),
1099         SND_PCI_QUIRK(0x1043, 0x8234, "ASUS M2N", AD1986A_3STACK),
1100         SND_PCI_QUIRK(0x10de, 0xcb84, "ASUS A8N-VM", AD1986A_3STACK),
1101         SND_PCI_QUIRK(0x1179, 0xff40, "Toshiba", AD1986A_LAPTOP_EAPD),
1102         SND_PCI_QUIRK(0x144d, 0xb03c, "Samsung R55", AD1986A_3STACK),
1103         SND_PCI_QUIRK(0x144d, 0xc01e, "FSC V2060", AD1986A_LAPTOP),
1104         SND_PCI_QUIRK(0x144d, 0xc024, "Samsung P50", AD1986A_SAMSUNG_P50),
1105         SND_PCI_QUIRK(0x144d, 0xc027, "Samsung Q1", AD1986A_ULTRA),
1106         SND_PCI_QUIRK_MASK(0x144d, 0xff00, 0xc000, "Samsung", AD1986A_SAMSUNG),
1107         SND_PCI_QUIRK(0x144d, 0xc504, "Samsung Q35", AD1986A_3STACK),
1108         SND_PCI_QUIRK(0x17aa, 0x1011, "Lenovo M55", AD1986A_LAPTOP),
1109         SND_PCI_QUIRK(0x17aa, 0x1017, "Lenovo A60", AD1986A_3STACK),
1110         SND_PCI_QUIRK(0x17aa, 0x2066, "Lenovo N100", AD1986A_LAPTOP_AUTOMUTE),
1111         SND_PCI_QUIRK(0x17c0, 0x2017, "Samsung M50", AD1986A_LAPTOP),
1112         {}
1113 };
1114
1115 #ifdef CONFIG_SND_HDA_POWER_SAVE
1116 static struct hda_amp_list ad1986a_loopbacks[] = {
1117         { 0x13, HDA_OUTPUT, 0 }, /* Mic */
1118         { 0x14, HDA_OUTPUT, 0 }, /* Phone */
1119         { 0x15, HDA_OUTPUT, 0 }, /* CD */
1120         { 0x16, HDA_OUTPUT, 0 }, /* Aux */
1121         { 0x17, HDA_OUTPUT, 0 }, /* Line */
1122         { } /* end */
1123 };
1124 #endif
1125
1126 static int is_jack_available(struct hda_codec *codec, hda_nid_t nid)
1127 {
1128         unsigned int conf = snd_hda_codec_get_pincfg(codec, nid);
1129         return get_defcfg_connect(conf) != AC_JACK_PORT_NONE;
1130 }
1131
1132 static int patch_ad1986a(struct hda_codec *codec)
1133 {
1134         struct ad198x_spec *spec;
1135         int err, board_config;
1136
1137         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1138         if (spec == NULL)
1139                 return -ENOMEM;
1140
1141         codec->spec = spec;
1142
1143         err = snd_hda_attach_beep_device(codec, 0x19);
1144         if (err < 0) {
1145                 ad198x_free(codec);
1146                 return err;
1147         }
1148         set_beep_amp(spec, 0x18, 0, HDA_OUTPUT);
1149
1150         spec->multiout.max_channels = 6;
1151         spec->multiout.num_dacs = ARRAY_SIZE(ad1986a_dac_nids);
1152         spec->multiout.dac_nids = ad1986a_dac_nids;
1153         spec->multiout.dig_out_nid = AD1986A_SPDIF_OUT;
1154         spec->num_adc_nids = 1;
1155         spec->adc_nids = ad1986a_adc_nids;
1156         spec->capsrc_nids = ad1986a_capsrc_nids;
1157         spec->input_mux = &ad1986a_capture_source;
1158         spec->num_mixers = 1;
1159         spec->mixers[0] = ad1986a_mixers;
1160         spec->num_init_verbs = 1;
1161         spec->init_verbs[0] = ad1986a_init_verbs;
1162 #ifdef CONFIG_SND_HDA_POWER_SAVE
1163         spec->loopback.amplist = ad1986a_loopbacks;
1164 #endif
1165         spec->vmaster_nid = 0x1b;
1166         spec->inv_eapd = 1; /* AD1986A has the inverted EAPD implementation */
1167
1168         codec->patch_ops = ad198x_patch_ops;
1169
1170         /* override some parameters */
1171         board_config = snd_hda_check_board_config(codec, AD1986A_MODELS,
1172                                                   ad1986a_models,
1173                                                   ad1986a_cfg_tbl);
1174         switch (board_config) {
1175         case AD1986A_3STACK:
1176                 spec->num_mixers = 2;
1177                 spec->mixers[1] = ad1986a_3st_mixers;
1178                 spec->num_init_verbs = 2;
1179                 spec->init_verbs[1] = ad1986a_ch2_init;
1180                 spec->channel_mode = ad1986a_modes;
1181                 spec->num_channel_mode = ARRAY_SIZE(ad1986a_modes);
1182                 spec->need_dac_fix = 1;
1183                 spec->multiout.max_channels = 2;
1184                 spec->multiout.num_dacs = 1;
1185                 break;
1186         case AD1986A_LAPTOP:
1187                 spec->mixers[0] = ad1986a_laptop_mixers;
1188                 spec->multiout.max_channels = 2;
1189                 spec->multiout.num_dacs = 1;
1190                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1191                 break;
1192         case AD1986A_LAPTOP_EAPD:
1193                 spec->num_mixers = 3;
1194                 spec->mixers[0] = ad1986a_laptop_master_mixers;
1195                 spec->mixers[1] = ad1986a_laptop_eapd_mixers;
1196                 spec->mixers[2] = ad1986a_laptop_intmic_mixers;
1197                 spec->num_init_verbs = 2;
1198                 spec->init_verbs[1] = ad1986a_eapd_init_verbs;
1199                 spec->multiout.max_channels = 2;
1200                 spec->multiout.num_dacs = 1;
1201                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1202                 if (!is_jack_available(codec, 0x25))
1203                         spec->multiout.dig_out_nid = 0;
1204                 spec->input_mux = &ad1986a_laptop_eapd_capture_source;
1205                 break;
1206         case AD1986A_SAMSUNG:
1207                 spec->num_mixers = 2;
1208                 spec->mixers[0] = ad1986a_laptop_master_mixers;
1209                 spec->mixers[1] = ad1986a_laptop_eapd_mixers;
1210                 spec->num_init_verbs = 3;
1211                 spec->init_verbs[1] = ad1986a_eapd_init_verbs;
1212                 spec->init_verbs[2] = ad1986a_automic_verbs;
1213                 spec->multiout.max_channels = 2;
1214                 spec->multiout.num_dacs = 1;
1215                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1216                 if (!is_jack_available(codec, 0x25))
1217                         spec->multiout.dig_out_nid = 0;
1218                 spec->input_mux = &ad1986a_automic_capture_source;
1219                 codec->patch_ops.unsol_event = ad1986a_automic_unsol_event;
1220                 codec->patch_ops.init = ad1986a_automic_init;
1221                 break;
1222         case AD1986A_SAMSUNG_P50:
1223                 spec->num_mixers = 2;
1224                 spec->mixers[0] = ad1986a_automute_master_mixers;
1225                 spec->mixers[1] = ad1986a_laptop_eapd_mixers;
1226                 spec->num_init_verbs = 4;
1227                 spec->init_verbs[1] = ad1986a_eapd_init_verbs;
1228                 spec->init_verbs[2] = ad1986a_automic_verbs;
1229                 spec->init_verbs[3] = ad1986a_hp_init_verbs;
1230                 spec->multiout.max_channels = 2;
1231                 spec->multiout.num_dacs = 1;
1232                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1233                 if (!is_jack_available(codec, 0x25))
1234                         spec->multiout.dig_out_nid = 0;
1235                 spec->input_mux = &ad1986a_automic_capture_source;
1236                 codec->patch_ops.unsol_event = ad1986a_samsung_p50_unsol_event;
1237                 codec->patch_ops.init = ad1986a_samsung_p50_init;
1238                 break;
1239         case AD1986A_LAPTOP_AUTOMUTE:
1240                 spec->num_mixers = 3;
1241                 spec->mixers[0] = ad1986a_automute_master_mixers;
1242                 spec->mixers[1] = ad1986a_laptop_eapd_mixers;
1243                 spec->mixers[2] = ad1986a_laptop_intmic_mixers;
1244                 spec->num_init_verbs = 3;
1245                 spec->init_verbs[1] = ad1986a_eapd_init_verbs;
1246                 spec->init_verbs[2] = ad1986a_hp_init_verbs;
1247                 spec->multiout.max_channels = 2;
1248                 spec->multiout.num_dacs = 1;
1249                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1250                 if (!is_jack_available(codec, 0x25))
1251                         spec->multiout.dig_out_nid = 0;
1252                 spec->input_mux = &ad1986a_laptop_eapd_capture_source;
1253                 codec->patch_ops.unsol_event = ad1986a_hp_unsol_event;
1254                 codec->patch_ops.init = ad1986a_hp_init;
1255                 /* Lenovo N100 seems to report the reversed bit
1256                  * for HP jack-sensing
1257                  */
1258                 spec->inv_jack_detect = 1;
1259                 break;
1260         case AD1986A_ULTRA:
1261                 spec->mixers[0] = ad1986a_laptop_eapd_mixers;
1262                 spec->num_init_verbs = 2;
1263                 spec->init_verbs[1] = ad1986a_ultra_init;
1264                 spec->multiout.max_channels = 2;
1265                 spec->multiout.num_dacs = 1;
1266                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1267                 spec->multiout.dig_out_nid = 0;
1268                 break;
1269         }
1270
1271         /* AD1986A has a hardware problem that it can't share a stream
1272          * with multiple output pins.  The copy of front to surrounds
1273          * causes noisy or silent outputs at a certain timing, e.g.
1274          * changing the volume.
1275          * So, let's disable the shared stream.
1276          */
1277         spec->multiout.no_share_stream = 1;
1278
1279         codec->no_trigger_sense = 1;
1280
1281         return 0;
1282 }
1283
1284 /*
1285  * AD1983 specific
1286  */
1287
1288 #define AD1983_SPDIF_OUT        0x02
1289 #define AD1983_DAC              0x03
1290 #define AD1983_ADC              0x04
1291
1292 static hda_nid_t ad1983_dac_nids[1] = { AD1983_DAC };
1293 static hda_nid_t ad1983_adc_nids[1] = { AD1983_ADC };
1294 static hda_nid_t ad1983_capsrc_nids[1] = { 0x15 };
1295
1296 static struct hda_input_mux ad1983_capture_source = {
1297         .num_items = 4,
1298         .items = {
1299                 { "Mic", 0x0 },
1300                 { "Line", 0x1 },
1301                 { "Mix", 0x2 },
1302                 { "Mix Mono", 0x3 },
1303         },
1304 };
1305
1306 /*
1307  * SPDIF playback route
1308  */
1309 static int ad1983_spdif_route_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1310 {
1311         static char *texts[] = { "PCM", "ADC" };
1312
1313         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1314         uinfo->count = 1;
1315         uinfo->value.enumerated.items = 2;
1316         if (uinfo->value.enumerated.item > 1)
1317                 uinfo->value.enumerated.item = 1;
1318         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1319         return 0;
1320 }
1321
1322 static int ad1983_spdif_route_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1323 {
1324         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1325         struct ad198x_spec *spec = codec->spec;
1326
1327         ucontrol->value.enumerated.item[0] = spec->spdif_route;
1328         return 0;
1329 }
1330
1331 static int ad1983_spdif_route_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1332 {
1333         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1334         struct ad198x_spec *spec = codec->spec;
1335
1336         if (ucontrol->value.enumerated.item[0] > 1)
1337                 return -EINVAL;
1338         if (spec->spdif_route != ucontrol->value.enumerated.item[0]) {
1339                 spec->spdif_route = ucontrol->value.enumerated.item[0];
1340                 snd_hda_codec_write_cache(codec, spec->multiout.dig_out_nid, 0,
1341                                           AC_VERB_SET_CONNECT_SEL,
1342                                           spec->spdif_route);
1343                 return 1;
1344         }
1345         return 0;
1346 }
1347
1348 static struct snd_kcontrol_new ad1983_mixers[] = {
1349         HDA_CODEC_VOLUME("Front Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1350         HDA_CODEC_MUTE("Front Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1351         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1352         HDA_CODEC_MUTE("Headphone Playback Switch", 0x06, 0x0, HDA_OUTPUT),
1353         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x07, 1, 0x0, HDA_OUTPUT),
1354         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x07, 1, 0x0, HDA_OUTPUT),
1355         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1356         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1357         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1358         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1359         HDA_CODEC_VOLUME("Line Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1360         HDA_CODEC_MUTE("Line Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1361         HDA_CODEC_VOLUME("Mic Boost", 0x0c, 0x0, HDA_OUTPUT),
1362         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1363         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1364         {
1365                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1366                 .name = "Capture Source",
1367                 .info = ad198x_mux_enum_info,
1368                 .get = ad198x_mux_enum_get,
1369                 .put = ad198x_mux_enum_put,
1370         },
1371         {
1372                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1373                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1374                 .info = ad1983_spdif_route_info,
1375                 .get = ad1983_spdif_route_get,
1376                 .put = ad1983_spdif_route_put,
1377         },
1378         { } /* end */
1379 };
1380
1381 static struct hda_verb ad1983_init_verbs[] = {
1382         /* Front, HP, Mono; mute as default */
1383         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1384         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1385         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1386         /* Beep, PCM, Mic, Line-In: mute */
1387         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1388         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1389         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1390         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1391         /* Front, HP selectors; from Mix */
1392         {0x05, AC_VERB_SET_CONNECT_SEL, 0x01},
1393         {0x06, AC_VERB_SET_CONNECT_SEL, 0x01},
1394         /* Mono selector; from Mix */
1395         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x03},
1396         /* Mic selector; Mic */
1397         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
1398         /* Line-in selector: Line-in */
1399         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
1400         /* Mic boost: 0dB */
1401         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1402         /* Record selector: mic */
1403         {0x15, AC_VERB_SET_CONNECT_SEL, 0x0},
1404         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1405         /* SPDIF route: PCM */
1406         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0},
1407         /* Front Pin */
1408         {0x05, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1409         /* HP Pin */
1410         {0x06, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
1411         /* Mono Pin */
1412         {0x07, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1413         /* Mic Pin */
1414         {0x08, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1415         /* Line Pin */
1416         {0x09, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
1417         { } /* end */
1418 };
1419
1420 #ifdef CONFIG_SND_HDA_POWER_SAVE
1421 static struct hda_amp_list ad1983_loopbacks[] = {
1422         { 0x12, HDA_OUTPUT, 0 }, /* Mic */
1423         { 0x13, HDA_OUTPUT, 0 }, /* Line */
1424         { } /* end */
1425 };
1426 #endif
1427
1428 static int patch_ad1983(struct hda_codec *codec)
1429 {
1430         struct ad198x_spec *spec;
1431         int err;
1432
1433         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1434         if (spec == NULL)
1435                 return -ENOMEM;
1436
1437         codec->spec = spec;
1438
1439         err = snd_hda_attach_beep_device(codec, 0x10);
1440         if (err < 0) {
1441                 ad198x_free(codec);
1442                 return err;
1443         }
1444         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
1445
1446         spec->multiout.max_channels = 2;
1447         spec->multiout.num_dacs = ARRAY_SIZE(ad1983_dac_nids);
1448         spec->multiout.dac_nids = ad1983_dac_nids;
1449         spec->multiout.dig_out_nid = AD1983_SPDIF_OUT;
1450         spec->num_adc_nids = 1;
1451         spec->adc_nids = ad1983_adc_nids;
1452         spec->capsrc_nids = ad1983_capsrc_nids;
1453         spec->input_mux = &ad1983_capture_source;
1454         spec->num_mixers = 1;
1455         spec->mixers[0] = ad1983_mixers;
1456         spec->num_init_verbs = 1;
1457         spec->init_verbs[0] = ad1983_init_verbs;
1458         spec->spdif_route = 0;
1459 #ifdef CONFIG_SND_HDA_POWER_SAVE
1460         spec->loopback.amplist = ad1983_loopbacks;
1461 #endif
1462         spec->vmaster_nid = 0x05;
1463
1464         codec->patch_ops = ad198x_patch_ops;
1465
1466         codec->no_trigger_sense = 1;
1467
1468         return 0;
1469 }
1470
1471
1472 /*
1473  * AD1981 HD specific
1474  */
1475
1476 #define AD1981_SPDIF_OUT        0x02
1477 #define AD1981_DAC              0x03
1478 #define AD1981_ADC              0x04
1479
1480 static hda_nid_t ad1981_dac_nids[1] = { AD1981_DAC };
1481 static hda_nid_t ad1981_adc_nids[1] = { AD1981_ADC };
1482 static hda_nid_t ad1981_capsrc_nids[1] = { 0x15 };
1483
1484 /* 0x0c, 0x09, 0x0e, 0x0f, 0x19, 0x05, 0x18, 0x17 */
1485 static struct hda_input_mux ad1981_capture_source = {
1486         .num_items = 7,
1487         .items = {
1488                 { "Front Mic", 0x0 },
1489                 { "Line", 0x1 },
1490                 { "Mix", 0x2 },
1491                 { "Mix Mono", 0x3 },
1492                 { "CD", 0x4 },
1493                 { "Mic", 0x6 },
1494                 { "Aux", 0x7 },
1495         },
1496 };
1497
1498 static struct snd_kcontrol_new ad1981_mixers[] = {
1499         HDA_CODEC_VOLUME("Front Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1500         HDA_CODEC_MUTE("Front Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1501         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1502         HDA_CODEC_MUTE("Headphone Playback Switch", 0x06, 0x0, HDA_OUTPUT),
1503         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x07, 1, 0x0, HDA_OUTPUT),
1504         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x07, 1, 0x0, HDA_OUTPUT),
1505         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1506         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1507         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1508         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1509         HDA_CODEC_VOLUME("Line Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1510         HDA_CODEC_MUTE("Line Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1511         HDA_CODEC_VOLUME("Aux Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
1512         HDA_CODEC_MUTE("Aux Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
1513         HDA_CODEC_VOLUME("Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
1514         HDA_CODEC_MUTE("Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
1515         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1516         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1517         HDA_CODEC_VOLUME("Front Mic Boost", 0x08, 0x0, HDA_INPUT),
1518         HDA_CODEC_VOLUME("Mic Boost", 0x18, 0x0, HDA_INPUT),
1519         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1520         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1521         {
1522                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1523                 .name = "Capture Source",
1524                 .info = ad198x_mux_enum_info,
1525                 .get = ad198x_mux_enum_get,
1526                 .put = ad198x_mux_enum_put,
1527         },
1528         /* identical with AD1983 */
1529         {
1530                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1531                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1532                 .info = ad1983_spdif_route_info,
1533                 .get = ad1983_spdif_route_get,
1534                 .put = ad1983_spdif_route_put,
1535         },
1536         { } /* end */
1537 };
1538
1539 static struct hda_verb ad1981_init_verbs[] = {
1540         /* Front, HP, Mono; mute as default */
1541         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1542         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1543         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1544         /* Beep, PCM, Front Mic, Line, Rear Mic, Aux, CD-In: mute */
1545         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1546         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1547         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1548         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1549         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1550         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1551         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1552         /* Front, HP selectors; from Mix */
1553         {0x05, AC_VERB_SET_CONNECT_SEL, 0x01},
1554         {0x06, AC_VERB_SET_CONNECT_SEL, 0x01},
1555         /* Mono selector; from Mix */
1556         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x03},
1557         /* Mic Mixer; select Front Mic */
1558         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1559         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1560         /* Mic boost: 0dB */
1561         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1562         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1563         /* Record selector: Front mic */
1564         {0x15, AC_VERB_SET_CONNECT_SEL, 0x0},
1565         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1566         /* SPDIF route: PCM */
1567         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0},
1568         /* Front Pin */
1569         {0x05, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1570         /* HP Pin */
1571         {0x06, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
1572         /* Mono Pin */
1573         {0x07, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1574         /* Front & Rear Mic Pins */
1575         {0x08, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1576         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1577         /* Line Pin */
1578         {0x09, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
1579         /* Digital Beep */
1580         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x00},
1581         /* Line-Out as Input: disabled */
1582         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1583         { } /* end */
1584 };
1585
1586 #ifdef CONFIG_SND_HDA_POWER_SAVE
1587 static struct hda_amp_list ad1981_loopbacks[] = {
1588         { 0x12, HDA_OUTPUT, 0 }, /* Front Mic */
1589         { 0x13, HDA_OUTPUT, 0 }, /* Line */
1590         { 0x1b, HDA_OUTPUT, 0 }, /* Aux */
1591         { 0x1c, HDA_OUTPUT, 0 }, /* Mic */
1592         { 0x1d, HDA_OUTPUT, 0 }, /* CD */
1593         { } /* end */
1594 };
1595 #endif
1596
1597 /*
1598  * Patch for HP nx6320
1599  *
1600  * nx6320 uses EAPD in the reverse way - EAPD-on means the internal
1601  * speaker output enabled _and_ mute-LED off.
1602  */
1603
1604 #define AD1981_HP_EVENT         0x37
1605 #define AD1981_MIC_EVENT        0x38
1606
1607 static struct hda_verb ad1981_hp_init_verbs[] = {
1608         {0x05, AC_VERB_SET_EAPD_BTLENABLE, 0x00 }, /* default off */
1609         /* pin sensing on HP and Mic jacks */
1610         {0x06, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_HP_EVENT},
1611         {0x08, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_MIC_EVENT},
1612         {}
1613 };
1614
1615 /* turn on/off EAPD (+ mute HP) as a master switch */
1616 static int ad1981_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1617                                    struct snd_ctl_elem_value *ucontrol)
1618 {
1619         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1620         struct ad198x_spec *spec = codec->spec;
1621
1622         if (! ad198x_eapd_put(kcontrol, ucontrol))
1623                 return 0;
1624         /* change speaker pin appropriately */
1625         snd_hda_codec_write(codec, 0x05, 0,
1626                             AC_VERB_SET_PIN_WIDGET_CONTROL,
1627                             spec->cur_eapd ? PIN_OUT : 0);
1628         /* toggle HP mute appropriately */
1629         snd_hda_codec_amp_stereo(codec, 0x06, HDA_OUTPUT, 0,
1630                                  HDA_AMP_MUTE,
1631                                  spec->cur_eapd ? 0 : HDA_AMP_MUTE);
1632         return 1;
1633 }
1634
1635 /* bind volumes of both NID 0x05 and 0x06 */
1636 static struct hda_bind_ctls ad1981_hp_bind_master_vol = {
1637         .ops = &snd_hda_bind_vol,
1638         .values = {
1639                 HDA_COMPOSE_AMP_VAL(0x05, 3, 0, HDA_OUTPUT),
1640                 HDA_COMPOSE_AMP_VAL(0x06, 3, 0, HDA_OUTPUT),
1641                 0
1642         },
1643 };
1644
1645 /* mute internal speaker if HP is plugged */
1646 static void ad1981_hp_automute(struct hda_codec *codec)
1647 {
1648         unsigned int present;
1649
1650         present = snd_hda_jack_detect(codec, 0x06);
1651         snd_hda_codec_amp_stereo(codec, 0x05, HDA_OUTPUT, 0,
1652                                  HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
1653 }
1654
1655 /* toggle input of built-in and mic jack appropriately */
1656 static void ad1981_hp_automic(struct hda_codec *codec)
1657 {
1658         static struct hda_verb mic_jack_on[] = {
1659                 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1660                 {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1661                 {}
1662         };
1663         static struct hda_verb mic_jack_off[] = {
1664                 {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1665                 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1666                 {}
1667         };
1668         unsigned int present;
1669
1670         present = snd_hda_jack_detect(codec, 0x08);
1671         if (present)
1672                 snd_hda_sequence_write(codec, mic_jack_on);
1673         else
1674                 snd_hda_sequence_write(codec, mic_jack_off);
1675 }
1676
1677 /* unsolicited event for HP jack sensing */
1678 static void ad1981_hp_unsol_event(struct hda_codec *codec,
1679                                   unsigned int res)
1680 {
1681         res >>= 26;
1682         switch (res) {
1683         case AD1981_HP_EVENT:
1684                 ad1981_hp_automute(codec);
1685                 break;
1686         case AD1981_MIC_EVENT:
1687                 ad1981_hp_automic(codec);
1688                 break;
1689         }
1690 }
1691
1692 static struct hda_input_mux ad1981_hp_capture_source = {
1693         .num_items = 3,
1694         .items = {
1695                 { "Mic", 0x0 },
1696                 { "Docking-Station", 0x1 },
1697                 { "Mix", 0x2 },
1698         },
1699 };
1700
1701 static struct snd_kcontrol_new ad1981_hp_mixers[] = {
1702         HDA_BIND_VOL("Master Playback Volume", &ad1981_hp_bind_master_vol),
1703         {
1704                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1705                 .subdevice = HDA_SUBDEV_NID_FLAG | 0x05,
1706                 .name = "Master Playback Switch",
1707                 .info = ad198x_eapd_info,
1708                 .get = ad198x_eapd_get,
1709                 .put = ad1981_hp_master_sw_put,
1710                 .private_value = 0x05,
1711         },
1712         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1713         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1714 #if 0
1715         /* FIXME: analog mic/line loopback doesn't work with my tests...
1716          *        (although recording is OK)
1717          */
1718         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1719         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1720         HDA_CODEC_VOLUME("Docking-Station Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1721         HDA_CODEC_MUTE("Docking-Station Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1722         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
1723         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
1724         /* FIXME: does this laptop have analog CD connection? */
1725         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1726         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1727 #endif
1728         HDA_CODEC_VOLUME("Mic Boost", 0x08, 0x0, HDA_INPUT),
1729         HDA_CODEC_VOLUME("Internal Mic Boost", 0x18, 0x0, HDA_INPUT),
1730         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1731         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1732         {
1733                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1734                 .name = "Capture Source",
1735                 .info = ad198x_mux_enum_info,
1736                 .get = ad198x_mux_enum_get,
1737                 .put = ad198x_mux_enum_put,
1738         },
1739         { } /* end */
1740 };
1741
1742 /* initialize jack-sensing, too */
1743 static int ad1981_hp_init(struct hda_codec *codec)
1744 {
1745         ad198x_init(codec);
1746         ad1981_hp_automute(codec);
1747         ad1981_hp_automic(codec);
1748         return 0;
1749 }
1750
1751 /* configuration for Toshiba Laptops */
1752 static struct hda_verb ad1981_toshiba_init_verbs[] = {
1753         {0x05, AC_VERB_SET_EAPD_BTLENABLE, 0x01 }, /* default on */
1754         /* pin sensing on HP and Mic jacks */
1755         {0x06, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_HP_EVENT},
1756         {0x08, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_MIC_EVENT},
1757         {}
1758 };
1759
1760 static struct snd_kcontrol_new ad1981_toshiba_mixers[] = {
1761         HDA_CODEC_VOLUME("Amp Volume", 0x1a, 0x0, HDA_OUTPUT),
1762         HDA_CODEC_MUTE("Amp Switch", 0x1a, 0x0, HDA_OUTPUT),
1763         { }
1764 };
1765
1766 /* configuration for Lenovo Thinkpad T60 */
1767 static struct snd_kcontrol_new ad1981_thinkpad_mixers[] = {
1768         HDA_CODEC_VOLUME("Master Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1769         HDA_CODEC_MUTE("Master Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1770         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1771         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1772         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1773         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1774         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1775         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1776         HDA_CODEC_VOLUME("Mic Boost", 0x08, 0x0, HDA_INPUT),
1777         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1778         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1779         {
1780                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1781                 .name = "Capture Source",
1782                 .info = ad198x_mux_enum_info,
1783                 .get = ad198x_mux_enum_get,
1784                 .put = ad198x_mux_enum_put,
1785         },
1786         /* identical with AD1983 */
1787         {
1788                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1789                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1790                 .info = ad1983_spdif_route_info,
1791                 .get = ad1983_spdif_route_get,
1792                 .put = ad1983_spdif_route_put,
1793         },
1794         { } /* end */
1795 };
1796
1797 static struct hda_input_mux ad1981_thinkpad_capture_source = {
1798         .num_items = 3,
1799         .items = {
1800                 { "Mic", 0x0 },
1801                 { "Mix", 0x2 },
1802                 { "CD", 0x4 },
1803         },
1804 };
1805
1806 /* models */
1807 enum {
1808         AD1981_BASIC,
1809         AD1981_HP,
1810         AD1981_THINKPAD,
1811         AD1981_TOSHIBA,
1812         AD1981_MODELS
1813 };
1814
1815 static const char *ad1981_models[AD1981_MODELS] = {
1816         [AD1981_HP]             = "hp",
1817         [AD1981_THINKPAD]       = "thinkpad",
1818         [AD1981_BASIC]          = "basic",
1819         [AD1981_TOSHIBA]        = "toshiba"
1820 };
1821
1822 static struct snd_pci_quirk ad1981_cfg_tbl[] = {
1823         SND_PCI_QUIRK(0x1014, 0x0597, "Lenovo Z60", AD1981_THINKPAD),
1824         SND_PCI_QUIRK(0x1014, 0x05b7, "Lenovo Z60m", AD1981_THINKPAD),
1825         /* All HP models */
1826         SND_PCI_QUIRK_VENDOR(0x103c, "HP nx", AD1981_HP),
1827         SND_PCI_QUIRK(0x1179, 0x0001, "Toshiba U205", AD1981_TOSHIBA),
1828         /* Lenovo Thinkpad T60/X60/Z6xx */
1829         SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo Thinkpad", AD1981_THINKPAD),
1830         /* HP nx6320 (reversed SSID, H/W bug) */
1831         SND_PCI_QUIRK(0x30b0, 0x103c, "HP nx6320", AD1981_HP),
1832         {}
1833 };
1834
1835 static int patch_ad1981(struct hda_codec *codec)
1836 {
1837         struct ad198x_spec *spec;
1838         int err, board_config;
1839
1840         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1841         if (spec == NULL)
1842                 return -ENOMEM;
1843
1844         codec->spec = spec;
1845
1846         err = snd_hda_attach_beep_device(codec, 0x10);
1847         if (err < 0) {
1848                 ad198x_free(codec);
1849                 return err;
1850         }
1851         set_beep_amp(spec, 0x0d, 0, HDA_OUTPUT);
1852
1853         spec->multiout.max_channels = 2;
1854         spec->multiout.num_dacs = ARRAY_SIZE(ad1981_dac_nids);
1855         spec->multiout.dac_nids = ad1981_dac_nids;
1856         spec->multiout.dig_out_nid = AD1981_SPDIF_OUT;
1857         spec->num_adc_nids = 1;
1858         spec->adc_nids = ad1981_adc_nids;
1859         spec->capsrc_nids = ad1981_capsrc_nids;
1860         spec->input_mux = &ad1981_capture_source;
1861         spec->num_mixers = 1;
1862         spec->mixers[0] = ad1981_mixers;
1863         spec->num_init_verbs = 1;
1864         spec->init_verbs[0] = ad1981_init_verbs;
1865         spec->spdif_route = 0;
1866 #ifdef CONFIG_SND_HDA_POWER_SAVE
1867         spec->loopback.amplist = ad1981_loopbacks;
1868 #endif
1869         spec->vmaster_nid = 0x05;
1870
1871         codec->patch_ops = ad198x_patch_ops;
1872
1873         /* override some parameters */
1874         board_config = snd_hda_check_board_config(codec, AD1981_MODELS,
1875                                                   ad1981_models,
1876                                                   ad1981_cfg_tbl);
1877         switch (board_config) {
1878         case AD1981_HP:
1879                 spec->mixers[0] = ad1981_hp_mixers;
1880                 spec->num_init_verbs = 2;
1881                 spec->init_verbs[1] = ad1981_hp_init_verbs;
1882                 spec->multiout.dig_out_nid = 0;
1883                 spec->input_mux = &ad1981_hp_capture_source;
1884
1885                 codec->patch_ops.init = ad1981_hp_init;
1886                 codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
1887                 /* set the upper-limit for mixer amp to 0dB for avoiding the
1888                  * possible damage by overloading
1889                  */
1890                 snd_hda_override_amp_caps(codec, 0x11, HDA_INPUT,
1891                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
1892                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1893                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1894                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1895                 break;
1896         case AD1981_THINKPAD:
1897                 spec->mixers[0] = ad1981_thinkpad_mixers;
1898                 spec->input_mux = &ad1981_thinkpad_capture_source;
1899                 break;
1900         case AD1981_TOSHIBA:
1901                 spec->mixers[0] = ad1981_hp_mixers;
1902                 spec->mixers[1] = ad1981_toshiba_mixers;
1903                 spec->num_init_verbs = 2;
1904                 spec->init_verbs[1] = ad1981_toshiba_init_verbs;
1905                 spec->multiout.dig_out_nid = 0;
1906                 spec->input_mux = &ad1981_hp_capture_source;
1907                 codec->patch_ops.init = ad1981_hp_init;
1908                 codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
1909                 break;
1910         }
1911
1912         codec->no_trigger_sense = 1;
1913
1914         return 0;
1915 }
1916
1917
1918 /*
1919  * AD1988
1920  *
1921  * Output pins and routes
1922  *
1923  *        Pin               Mix     Sel     DAC (*)
1924  * port-A 0x11 (mute/hp) <- 0x22 <- 0x37 <- 03/04/06
1925  * port-B 0x14 (mute/hp) <- 0x2b <- 0x30 <- 03/04/06
1926  * port-C 0x15 (mute)    <- 0x2c <- 0x31 <- 05/0a
1927  * port-D 0x12 (mute/hp) <- 0x29         <- 04
1928  * port-E 0x17 (mute/hp) <- 0x26 <- 0x32 <- 05/0a
1929  * port-F 0x16 (mute)    <- 0x2a         <- 06
1930  * port-G 0x24 (mute)    <- 0x27         <- 05
1931  * port-H 0x25 (mute)    <- 0x28         <- 0a
1932  * mono   0x13 (mute/amp)<- 0x1e <- 0x36 <- 03/04/06
1933  *
1934  * DAC0 = 03h, DAC1 = 04h, DAC2 = 05h, DAC3 = 06h, DAC4 = 0ah
1935  * (*) DAC2/3/4 are swapped to DAC3/4/2 on AD198A rev.2 due to a h/w bug.
1936  *
1937  * Input pins and routes
1938  *
1939  *        pin     boost   mix input # / adc input #
1940  * port-A 0x11 -> 0x38 -> mix 2, ADC 0
1941  * port-B 0x14 -> 0x39 -> mix 0, ADC 1
1942  * port-C 0x15 -> 0x3a -> 33:0 - mix 1, ADC 2
1943  * port-D 0x12 -> 0x3d -> mix 3, ADC 8
1944  * port-E 0x17 -> 0x3c -> 34:0 - mix 4, ADC 4
1945  * port-F 0x16 -> 0x3b -> mix 5, ADC 3
1946  * port-G 0x24 -> N/A  -> 33:1 - mix 1, 34:1 - mix 4, ADC 6
1947  * port-H 0x25 -> N/A  -> 33:2 - mix 1, 34:2 - mix 4, ADC 7
1948  *
1949  *
1950  * DAC assignment
1951  *   6stack - front/surr/CLFE/side/opt DACs - 04/06/05/0a/03
1952  *   3stack - front/surr/CLFE/opt DACs - 04/05/0a/03
1953  *
1954  * Inputs of Analog Mix (0x20)
1955  *   0:Port-B (front mic)
1956  *   1:Port-C/G/H (line-in)
1957  *   2:Port-A
1958  *   3:Port-D (line-in/2)
1959  *   4:Port-E/G/H (mic-in)
1960  *   5:Port-F (mic2-in)
1961  *   6:CD
1962  *   7:Beep
1963  *
1964  * ADC selection
1965  *   0:Port-A
1966  *   1:Port-B (front mic-in)
1967  *   2:Port-C (line-in)
1968  *   3:Port-F (mic2-in)
1969  *   4:Port-E (mic-in)
1970  *   5:CD
1971  *   6:Port-G
1972  *   7:Port-H
1973  *   8:Port-D (line-in/2)
1974  *   9:Mix
1975  *
1976  * Proposed pin assignments by the datasheet
1977  *
1978  * 6-stack
1979  * Port-A front headphone
1980  *      B front mic-in
1981  *      C rear line-in
1982  *      D rear front-out
1983  *      E rear mic-in
1984  *      F rear surround
1985  *      G rear CLFE
1986  *      H rear side
1987  *
1988  * 3-stack
1989  * Port-A front headphone
1990  *      B front mic
1991  *      C rear line-in/surround
1992  *      D rear front-out
1993  *      E rear mic-in/CLFE
1994  *
1995  * laptop
1996  * Port-A headphone
1997  *      B mic-in
1998  *      C docking station
1999  *      D internal speaker (with EAPD)
2000  *      E/F quad mic array
2001  */
2002
2003
2004 /* models */
2005 enum {
2006         AD1988_6STACK,
2007         AD1988_6STACK_DIG,
2008         AD1988_3STACK,
2009         AD1988_3STACK_DIG,
2010         AD1988_LAPTOP,
2011         AD1988_LAPTOP_DIG,
2012         AD1988_AUTO,
2013         AD1988_MODEL_LAST,
2014 };
2015
2016 /* reivision id to check workarounds */
2017 #define AD1988A_REV2            0x100200
2018
2019 #define is_rev2(codec) \
2020         ((codec)->vendor_id == 0x11d41988 && \
2021          (codec)->revision_id == AD1988A_REV2)
2022
2023 /*
2024  * mixers
2025  */
2026
2027 static hda_nid_t ad1988_6stack_dac_nids[4] = {
2028         0x04, 0x06, 0x05, 0x0a
2029 };
2030
2031 static hda_nid_t ad1988_3stack_dac_nids[3] = {
2032         0x04, 0x05, 0x0a
2033 };
2034
2035 /* for AD1988A revision-2, DAC2-4 are swapped */
2036 static hda_nid_t ad1988_6stack_dac_nids_rev2[4] = {
2037         0x04, 0x05, 0x0a, 0x06
2038 };
2039
2040 static hda_nid_t ad1988_3stack_dac_nids_rev2[3] = {
2041         0x04, 0x0a, 0x06
2042 };
2043
2044 static hda_nid_t ad1988_adc_nids[3] = {
2045         0x08, 0x09, 0x0f
2046 };
2047
2048 static hda_nid_t ad1988_capsrc_nids[3] = {
2049         0x0c, 0x0d, 0x0e
2050 };
2051
2052 #define AD1988_SPDIF_OUT                0x02
2053 #define AD1988_SPDIF_OUT_HDMI   0x0b
2054 #define AD1988_SPDIF_IN         0x07
2055
2056 static hda_nid_t ad1989b_slave_dig_outs[] = {
2057         AD1988_SPDIF_OUT, AD1988_SPDIF_OUT_HDMI, 0
2058 };
2059
2060 static struct hda_input_mux ad1988_6stack_capture_source = {
2061         .num_items = 5,
2062         .items = {
2063                 { "Front Mic", 0x1 },   /* port-B */
2064                 { "Line", 0x2 },        /* port-C */
2065                 { "Mic", 0x4 },         /* port-E */
2066                 { "CD", 0x5 },
2067                 { "Mix", 0x9 },
2068         },
2069 };
2070
2071 static struct hda_input_mux ad1988_laptop_capture_source = {
2072         .num_items = 3,
2073         .items = {
2074                 { "Mic/Line", 0x1 },    /* port-B */
2075                 { "CD", 0x5 },
2076                 { "Mix", 0x9 },
2077         },
2078 };
2079
2080 /*
2081  */
2082 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
2083                                struct snd_ctl_elem_info *uinfo)
2084 {
2085         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2086         struct ad198x_spec *spec = codec->spec;
2087         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
2088                                     spec->num_channel_mode);
2089 }
2090
2091 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
2092                               struct snd_ctl_elem_value *ucontrol)
2093 {
2094         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2095         struct ad198x_spec *spec = codec->spec;
2096         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
2097                                    spec->num_channel_mode, spec->multiout.max_channels);
2098 }
2099
2100 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
2101                               struct snd_ctl_elem_value *ucontrol)
2102 {
2103         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2104         struct ad198x_spec *spec = codec->spec;
2105         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
2106                                       spec->num_channel_mode,
2107                                       &spec->multiout.max_channels);
2108         if (err >= 0 && spec->need_dac_fix)
2109                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2110         return err;
2111 }
2112
2113 /* 6-stack mode */
2114 static struct snd_kcontrol_new ad1988_6stack_mixers1[] = {
2115         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2116         HDA_CODEC_VOLUME("Surround Playback Volume", 0x06, 0x0, HDA_OUTPUT),
2117         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
2118         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
2119         HDA_CODEC_VOLUME("Side Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
2120         { } /* end */
2121 };
2122
2123 static struct snd_kcontrol_new ad1988_6stack_mixers1_rev2[] = {
2124         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2125         HDA_CODEC_VOLUME("Surround Playback Volume", 0x05, 0x0, HDA_OUTPUT),
2126         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0a, 1, 0x0, HDA_OUTPUT),
2127         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0a, 2, 0x0, HDA_OUTPUT),
2128         HDA_CODEC_VOLUME("Side Playback Volume", 0x06, 0x0, HDA_OUTPUT),
2129         { } /* end */
2130 };
2131
2132 static struct snd_kcontrol_new ad1988_6stack_mixers2[] = {
2133         HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
2134         HDA_BIND_MUTE("Surround Playback Switch", 0x2a, 2, HDA_INPUT),
2135         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x27, 1, 2, HDA_INPUT),
2136         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x27, 2, 2, HDA_INPUT),
2137         HDA_BIND_MUTE("Side Playback Switch", 0x28, 2, HDA_INPUT),
2138         HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
2139         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
2140
2141         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
2142         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
2143         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
2144         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
2145         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
2146         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
2147         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
2148         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),
2149
2150         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
2151         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
2152
2153         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
2154         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
2155
2156         { } /* end */
2157 };
2158
2159 /* 3-stack mode */
2160 static struct snd_kcontrol_new ad1988_3stack_mixers1[] = {
2161         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2162         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
2163         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
2164         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
2165         { } /* end */
2166 };
2167
2168 static struct snd_kcontrol_new ad1988_3stack_mixers1_rev2[] = {
2169         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2170         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
2171         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x06, 1, 0x0, HDA_OUTPUT),
2172         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x06, 2, 0x0, HDA_OUTPUT),
2173         { } /* end */
2174 };
2175
2176 static struct snd_kcontrol_new ad1988_3stack_mixers2[] = {
2177         HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
2178         HDA_BIND_MUTE("Surround Playback Switch", 0x2c, 2, HDA_INPUT),
2179         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x26, 1, 2, HDA_INPUT),
2180         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x26, 2, 2, HDA_INPUT),
2181         HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
2182         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
2183
2184         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
2185         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
2186         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
2187         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
2188         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
2189         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
2190         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
2191         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),
2192
2193         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
2194         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
2195
2196         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
2197         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
2198         {
2199                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2200                 .name = "Channel Mode",
2201                 .info = ad198x_ch_mode_info,
2202                 .get = ad198x_ch_mode_get,
2203                 .put = ad198x_ch_mode_put,
2204         },
2205
2206         { } /* end */
2207 };
2208
2209 /* laptop mode */
2210 static struct snd_kcontrol_new ad1988_laptop_mixers[] = {
2211         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2212         HDA_CODEC_MUTE("PCM Playback Switch", 0x29, 0x0, HDA_INPUT),
2213         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
2214
2215         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
2216         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
2217         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
2218         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
2219         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
2220         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
2221
2222         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
2223         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
2224
2225         HDA_CODEC_VOLUME("Mic Boost", 0x39, 0x0, HDA_OUTPUT),
2226
2227         {
2228                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2229                 .name = "External Amplifier",
2230                 .subdevice = HDA_SUBDEV_NID_FLAG | 0x12,
2231                 .info = ad198x_eapd_info,
2232                 .get = ad198x_eapd_get,
2233                 .put = ad198x_eapd_put,
2234                 .private_value = 0x12, /* port-D */
2235         },
2236
2237         { } /* end */
2238 };
2239
2240 /* capture */
2241 static struct snd_kcontrol_new ad1988_capture_mixers[] = {
2242         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
2243         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
2244         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
2245         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
2246         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x0e, 0x0, HDA_OUTPUT),
2247         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x0e, 0x0, HDA_OUTPUT),
2248         {
2249                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2250                 /* The multiple "Capture Source" controls confuse alsamixer
2251                  * So call somewhat different..
2252                  */
2253                 /* .name = "Capture Source", */
2254                 .name = "Input Source",
2255                 .count = 3,
2256                 .info = ad198x_mux_enum_info,
2257                 .get = ad198x_mux_enum_get,
2258                 .put = ad198x_mux_enum_put,
2259         },
2260         { } /* end */
2261 };
2262
2263 static int ad1988_spdif_playback_source_info(struct snd_kcontrol *kcontrol,
2264                                              struct snd_ctl_elem_info *uinfo)
2265 {
2266         static char *texts[] = {
2267                 "PCM", "ADC1", "ADC2", "ADC3"
2268         };
2269         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2270         uinfo->count = 1;
2271         uinfo->value.enumerated.items = 4;
2272         if (uinfo->value.enumerated.item >= 4)
2273                 uinfo->value.enumerated.item = 3;
2274         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2275         return 0;
2276 }
2277
2278 static int ad1988_spdif_playback_source_get(struct snd_kcontrol *kcontrol,
2279                                             struct snd_ctl_elem_value *ucontrol)
2280 {
2281         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2282         unsigned int sel;
2283
2284         sel = snd_hda_codec_read(codec, 0x1d, 0, AC_VERB_GET_AMP_GAIN_MUTE,
2285                                  AC_AMP_GET_INPUT);
2286         if (!(sel & 0x80))
2287                 ucontrol->value.enumerated.item[0] = 0;
2288         else {
2289                 sel = snd_hda_codec_read(codec, 0x0b, 0,
2290                                          AC_VERB_GET_CONNECT_SEL, 0);
2291                 if (sel < 3)
2292                         sel++;
2293                 else
2294                         sel = 0;
2295                 ucontrol->value.enumerated.item[0] = sel;
2296         }
2297         return 0;
2298 }
2299
2300 static int ad1988_spdif_playback_source_put(struct snd_kcontrol *kcontrol,
2301                                             struct snd_ctl_elem_value *ucontrol)
2302 {
2303         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2304         unsigned int val, sel;
2305         int change;
2306
2307         val = ucontrol->value.enumerated.item[0];
2308         if (val > 3)
2309                 return -EINVAL;
2310         if (!val) {
2311                 sel = snd_hda_codec_read(codec, 0x1d, 0,
2312                                          AC_VERB_GET_AMP_GAIN_MUTE,
2313                                          AC_AMP_GET_INPUT);
2314                 change = sel & 0x80;
2315                 if (change) {
2316                         snd_hda_codec_write_cache(codec, 0x1d, 0,
2317                                                   AC_VERB_SET_AMP_GAIN_MUTE,
2318                                                   AMP_IN_UNMUTE(0));
2319                         snd_hda_codec_write_cache(codec, 0x1d, 0,
2320                                                   AC_VERB_SET_AMP_GAIN_MUTE,
2321                                                   AMP_IN_MUTE(1));
2322                 }
2323         } else {
2324                 sel = snd_hda_codec_read(codec, 0x1d, 0,
2325                                          AC_VERB_GET_AMP_GAIN_MUTE,
2326                                          AC_AMP_GET_INPUT | 0x01);
2327                 change = sel & 0x80;
2328                 if (change) {
2329                         snd_hda_codec_write_cache(codec, 0x1d, 0,
2330                                                   AC_VERB_SET_AMP_GAIN_MUTE,
2331                                                   AMP_IN_MUTE(0));
2332                         snd_hda_codec_write_cache(codec, 0x1d, 0,
2333                                                   AC_VERB_SET_AMP_GAIN_MUTE,
2334                                                   AMP_IN_UNMUTE(1));
2335                 }
2336                 sel = snd_hda_codec_read(codec, 0x0b, 0,
2337                                          AC_VERB_GET_CONNECT_SEL, 0) + 1;
2338                 change |= sel != val;
2339                 if (change)
2340                         snd_hda_codec_write_cache(codec, 0x0b, 0,
2341                                                   AC_VERB_SET_CONNECT_SEL,
2342                                                   val - 1);
2343         }
2344         return change;
2345 }
2346
2347 static struct snd_kcontrol_new ad1988_spdif_out_mixers[] = {
2348         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
2349         {
2350                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2351                 .name = "IEC958 Playback Source",
2352                 .subdevice = HDA_SUBDEV_NID_FLAG | 0x1b,
2353                 .info = ad1988_spdif_playback_source_info,
2354                 .get = ad1988_spdif_playback_source_get,
2355                 .put = ad1988_spdif_playback_source_put,
2356         },
2357         { } /* end */
2358 };
2359
2360 static struct snd_kcontrol_new ad1988_spdif_in_mixers[] = {
2361         HDA_CODEC_VOLUME("IEC958 Capture Volume", 0x1c, 0x0, HDA_INPUT),
2362         { } /* end */
2363 };
2364
2365 static struct snd_kcontrol_new ad1989_spdif_out_mixers[] = {
2366         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
2367         HDA_CODEC_VOLUME("HDMI Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
2368         { } /* end */
2369 };
2370
2371 /*
2372  * initialization verbs
2373  */
2374
2375 /*
2376  * for 6-stack (+dig)
2377  */
2378 static struct hda_verb ad1988_6stack_init_verbs[] = {
2379         /* Front, Surround, CLFE, side DAC; unmute as default */
2380         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2381         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2382         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2383         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2384         /* Port-A front headphon path */
2385         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
2386         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2387         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2388         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2389         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2390         /* Port-D line-out path */
2391         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2392         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2393         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2394         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2395         /* Port-F surround path */
2396         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2397         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2398         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2399         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2400         /* Port-G CLFE path */
2401         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2402         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2403         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2404         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2405         /* Port-H side path */
2406         {0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2407         {0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2408         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2409         {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2410         /* Mono out path */
2411         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2412         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2413         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2414         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2415         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2416         /* Port-B front mic-in path */
2417         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2418         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2419         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2420         /* Port-C line-in path */
2421         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2422         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2423         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2424         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2425         /* Port-E mic-in path */
2426         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2427         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2428         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2429         {0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
2430         /* Analog CD Input */
2431         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2432         /* Analog Mix output amp */
2433         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
2434
2435         { }
2436 };
2437
2438 static struct hda_verb ad1988_capture_init_verbs[] = {
2439         /* mute analog mix */
2440         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2441         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2442         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2443         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2444         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2445         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2446         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2447         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2448         /* select ADCs - front-mic */
2449         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2450         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2451         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2452
2453         { }
2454 };
2455
2456 static struct hda_verb ad1988_spdif_init_verbs[] = {
2457         /* SPDIF out sel */
2458         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
2459         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x0}, /* ADC1 */
2460         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2461         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2462         /* SPDIF out pin */
2463         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
2464
2465         { }
2466 };
2467
2468 /* AD1989 has no ADC -> SPDIF route */
2469 static struct hda_verb ad1989_spdif_init_verbs[] = {
2470         /* SPDIF-1 out pin */
2471         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2472         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
2473         /* SPDIF-2/HDMI out pin */
2474         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2475         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
2476         { }
2477 };
2478
2479 /*
2480  * verbs for 3stack (+dig)
2481  */
2482 static struct hda_verb ad1988_3stack_ch2_init[] = {
2483         /* set port-C to line-in */
2484         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2485         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
2486         /* set port-E to mic-in */
2487         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2488         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
2489         { } /* end */
2490 };
2491
2492 static struct hda_verb ad1988_3stack_ch6_init[] = {
2493         /* set port-C to surround out */
2494         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2495         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2496         /* set port-E to CLFE out */
2497         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2498         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2499         { } /* end */
2500 };
2501
2502 static struct hda_channel_mode ad1988_3stack_modes[2] = {
2503         { 2, ad1988_3stack_ch2_init },
2504         { 6, ad1988_3stack_ch6_init },
2505 };
2506
2507 static struct hda_verb ad1988_3stack_init_verbs[] = {
2508         /* Front, Surround, CLFE, side DAC; unmute as default */
2509         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2510         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2511         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2512         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2513         /* Port-A front headphon path */
2514         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
2515         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2516         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2517         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2518         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2519         /* Port-D line-out path */
2520         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2521         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2522         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2523         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2524         /* Mono out path */
2525         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2526         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2527         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2528         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2529         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2530         /* Port-B front mic-in path */
2531         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2532         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2533         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2534         /* Port-C line-in/surround path - 6ch mode as default */
2535         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2536         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2537         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2538         {0x31, AC_VERB_SET_CONNECT_SEL, 0x0}, /* output sel: DAC 0x05 */
2539         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2540         /* Port-E mic-in/CLFE path - 6ch mode as default */
2541         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2542         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2543         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2544         {0x32, AC_VERB_SET_CONNECT_SEL, 0x1}, /* output sel: DAC 0x0a */
2545         {0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
2546         /* mute analog mix */
2547         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2548         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2549         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2550         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2551         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2552         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2553         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2554         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2555         /* select ADCs - front-mic */
2556         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2557         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2558         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2559         /* Analog Mix output amp */
2560         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
2561         { }
2562 };
2563
2564 /*
2565  * verbs for laptop mode (+dig)
2566  */
2567 static struct hda_verb ad1988_laptop_hp_on[] = {
2568         /* unmute port-A and mute port-D */
2569         { 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2570         { 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2571         { } /* end */
2572 };
2573 static struct hda_verb ad1988_laptop_hp_off[] = {
2574         /* mute port-A and unmute port-D */
2575         { 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2576         { 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2577         { } /* end */
2578 };
2579
2580 #define AD1988_HP_EVENT 0x01
2581
2582 static struct hda_verb ad1988_laptop_init_verbs[] = {
2583         /* Front, Surround, CLFE, side DAC; unmute as default */
2584         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2585         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2586         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2587         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2588         /* Port-A front headphon path */
2589         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
2590         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2591         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2592         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2593         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2594         /* unsolicited event for pin-sense */
2595         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1988_HP_EVENT },
2596         /* Port-D line-out path + EAPD */
2597         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2598         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2599         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2600         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2601         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x00}, /* EAPD-off */
2602         /* Mono out path */
2603         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2604         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2605         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2606         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2607         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2608         /* Port-B mic-in path */
2609         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2610         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2611         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2612         /* Port-C docking station - try to output */
2613         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2614         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2615         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2616         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2617         /* mute analog mix */
2618         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2619         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2620         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2621         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2622         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2623         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2624         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2625         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2626         /* select ADCs - mic */
2627         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2628         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2629         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2630         /* Analog Mix output amp */
2631         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
2632         { }
2633 };
2634
2635 static void ad1988_laptop_unsol_event(struct hda_codec *codec, unsigned int res)
2636 {
2637         if ((res >> 26) != AD1988_HP_EVENT)
2638                 return;
2639         if (snd_hda_jack_detect(codec, 0x11))
2640                 snd_hda_sequence_write(codec, ad1988_laptop_hp_on);
2641         else
2642                 snd_hda_sequence_write(codec, ad1988_laptop_hp_off);
2643
2644
2645 #ifdef CONFIG_SND_HDA_POWER_SAVE
2646 static struct hda_amp_list ad1988_loopbacks[] = {
2647         { 0x20, HDA_INPUT, 0 }, /* Front Mic */
2648         { 0x20, HDA_INPUT, 1 }, /* Line */
2649         { 0x20, HDA_INPUT, 4 }, /* Mic */
2650         { 0x20, HDA_INPUT, 6 }, /* CD */
2651         { } /* end */
2652 };
2653 #endif
2654
2655 /*
2656  * Automatic parse of I/O pins from the BIOS configuration
2657  */
2658
2659 enum {
2660         AD_CTL_WIDGET_VOL,
2661         AD_CTL_WIDGET_MUTE,
2662         AD_CTL_BIND_MUTE,
2663 };
2664 static struct snd_kcontrol_new ad1988_control_templates[] = {
2665         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2666         HDA_CODEC_MUTE(NULL, 0, 0, 0),
2667         HDA_BIND_MUTE(NULL, 0, 0, 0),
2668 };
2669
2670 /* add dynamic controls */
2671 static int add_control(struct ad198x_spec *spec, int type, const char *name,
2672                        unsigned long val)
2673 {
2674         struct snd_kcontrol_new *knew;
2675
2676         snd_array_init(&spec->kctls, sizeof(*knew), 32);
2677         knew = snd_array_new(&spec->kctls);
2678         if (!knew)
2679                 return -ENOMEM;
2680         *knew = ad1988_control_templates[type];
2681         knew->name = kstrdup(name, GFP_KERNEL);
2682         if (! knew->name)
2683                 return -ENOMEM;
2684         if (get_amp_nid_(val))
2685                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2686         knew->private_value = val;
2687         return 0;
2688 }
2689
2690 #define AD1988_PIN_CD_NID               0x18
2691 #define AD1988_PIN_BEEP_NID             0x10
2692
2693 static hda_nid_t ad1988_mixer_nids[8] = {
2694         /* A     B     C     D     E     F     G     H */
2695         0x22, 0x2b, 0x2c, 0x29, 0x26, 0x2a, 0x27, 0x28
2696 };
2697
2698 static inline hda_nid_t ad1988_idx_to_dac(struct hda_codec *codec, int idx)
2699 {
2700         static hda_nid_t idx_to_dac[8] = {
2701                 /* A     B     C     D     E     F     G     H */
2702                 0x04, 0x06, 0x05, 0x04, 0x0a, 0x06, 0x05, 0x0a
2703         };
2704         static hda_nid_t idx_to_dac_rev2[8] = {
2705                 /* A     B     C     D     E     F     G     H */
2706                 0x04, 0x05, 0x0a, 0x04, 0x06, 0x05, 0x0a, 0x06
2707         };
2708         if (is_rev2(codec))
2709                 return idx_to_dac_rev2[idx];
2710         else
2711                 return idx_to_dac[idx];
2712 }
2713
2714 static hda_nid_t ad1988_boost_nids[8] = {
2715         0x38, 0x39, 0x3a, 0x3d, 0x3c, 0x3b, 0, 0
2716 };
2717
2718 static int ad1988_pin_idx(hda_nid_t nid)
2719 {
2720         static hda_nid_t ad1988_io_pins[8] = {
2721                 0x11, 0x14, 0x15, 0x12, 0x17, 0x16, 0x24, 0x25
2722         };
2723         int i;
2724         for (i = 0; i < ARRAY_SIZE(ad1988_io_pins); i++)
2725                 if (ad1988_io_pins[i] == nid)
2726                         return i;
2727         return 0; /* should be -1 */
2728 }
2729
2730 static int ad1988_pin_to_loopback_idx(hda_nid_t nid)
2731 {
2732         static int loopback_idx[8] = {
2733                 2, 0, 1, 3, 4, 5, 1, 4
2734         };
2735         switch (nid) {
2736         case AD1988_PIN_CD_NID:
2737                 return 6;
2738         default:
2739                 return loopback_idx[ad1988_pin_idx(nid)];
2740         }
2741 }
2742
2743 static int ad1988_pin_to_adc_idx(hda_nid_t nid)
2744 {
2745         static int adc_idx[8] = {
2746                 0, 1, 2, 8, 4, 3, 6, 7
2747         };
2748         switch (nid) {
2749         case AD1988_PIN_CD_NID:
2750                 return 5;
2751         default:
2752                 return adc_idx[ad1988_pin_idx(nid)];
2753         }
2754 }
2755
2756 /* fill in the dac_nids table from the parsed pin configuration */
2757 static int ad1988_auto_fill_dac_nids(struct hda_codec *codec,
2758                                      const struct auto_pin_cfg *cfg)
2759 {
2760         struct ad198x_spec *spec = codec->spec;
2761         int i, idx;
2762
2763         spec->multiout.dac_nids = spec->private_dac_nids;
2764
2765         /* check the pins hardwired to audio widget */
2766         for (i = 0; i < cfg->line_outs; i++) {
2767                 idx = ad1988_pin_idx(cfg->line_out_pins[i]);
2768                 spec->multiout.dac_nids[i] = ad1988_idx_to_dac(codec, idx);
2769         }
2770         spec->multiout.num_dacs = cfg->line_outs;
2771         return 0;
2772 }
2773
2774 /* add playback controls from the parsed DAC table */
2775 static int ad1988_auto_create_multi_out_ctls(struct ad198x_spec *spec,
2776                                              const struct auto_pin_cfg *cfg)
2777 {
2778         char name[32];
2779         static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
2780         hda_nid_t nid;
2781         int i, err;
2782
2783         for (i = 0; i < cfg->line_outs; i++) {
2784                 hda_nid_t dac = spec->multiout.dac_nids[i];
2785                 if (! dac)
2786                         continue;
2787                 nid = ad1988_mixer_nids[ad1988_pin_idx(cfg->line_out_pins[i])];
2788                 if (i == 2) {
2789                         /* Center/LFE */
2790                         err = add_control(spec, AD_CTL_WIDGET_VOL,
2791                                           "Center Playback Volume",
2792                                           HDA_COMPOSE_AMP_VAL(dac, 1, 0, HDA_OUTPUT));
2793                         if (err < 0)
2794                                 return err;
2795                         err = add_control(spec, AD_CTL_WIDGET_VOL,
2796                                           "LFE Playback Volume",
2797                                           HDA_COMPOSE_AMP_VAL(dac, 2, 0, HDA_OUTPUT));
2798                         if (err < 0)
2799                                 return err;
2800                         err = add_control(spec, AD_CTL_BIND_MUTE,
2801                                           "Center Playback Switch",
2802                                           HDA_COMPOSE_AMP_VAL(nid, 1, 2, HDA_INPUT));
2803                         if (err < 0)
2804                                 return err;
2805                         err = add_control(spec, AD_CTL_BIND_MUTE,
2806                                           "LFE Playback Switch",
2807                                           HDA_COMPOSE_AMP_VAL(nid, 2, 2, HDA_INPUT));
2808                         if (err < 0)
2809                                 return err;
2810                 } else {
2811                         sprintf(name, "%s Playback Volume", chname[i]);
2812                         err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2813                                           HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT));
2814                         if (err < 0)
2815                                 return err;
2816                         sprintf(name, "%s Playback Switch", chname[i]);
2817                         err = add_control(spec, AD_CTL_BIND_MUTE, name,
2818                                           HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT));
2819                         if (err < 0)
2820                                 return err;
2821                 }
2822         }
2823         return 0;
2824 }
2825
2826 /* add playback controls for speaker and HP outputs */
2827 static int ad1988_auto_create_extra_out(struct hda_codec *codec, hda_nid_t pin,
2828                                         const char *pfx)
2829 {
2830         struct ad198x_spec *spec = codec->spec;
2831         hda_nid_t nid;
2832         int i, idx, err;
2833         char name[32];
2834
2835         if (! pin)
2836                 return 0;
2837
2838         idx = ad1988_pin_idx(pin);
2839         nid = ad1988_idx_to_dac(codec, idx);
2840         /* check whether the corresponding DAC was already taken */
2841         for (i = 0; i < spec->autocfg.line_outs; i++) {
2842                 hda_nid_t pin = spec->autocfg.line_out_pins[i];
2843                 hda_nid_t dac = ad1988_idx_to_dac(codec, ad1988_pin_idx(pin));
2844                 if (dac == nid)
2845                         break;
2846         }
2847         if (i >= spec->autocfg.line_outs) {
2848                 /* specify the DAC as the extra output */
2849                 if (!spec->multiout.hp_nid)
2850                         spec->multiout.hp_nid = nid;
2851                 else
2852                         spec->multiout.extra_out_nid[0] = nid;
2853                 /* control HP volume/switch on the output mixer amp */
2854                 sprintf(name, "%s Playback Volume", pfx);
2855                 err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2856                                   HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT));
2857                 if (err < 0)
2858                         return err;
2859         }
2860         nid = ad1988_mixer_nids[idx];
2861         sprintf(name, "%s Playback Switch", pfx);
2862         if ((err = add_control(spec, AD_CTL_BIND_MUTE, name,
2863                                HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT))) < 0)
2864                 return err;
2865         return 0;
2866 }
2867
2868 /* create input playback/capture controls for the given pin */
2869 static int new_analog_input(struct ad198x_spec *spec, hda_nid_t pin,
2870                             const char *ctlname, int boost)
2871 {
2872         char name[32];
2873         int err, idx;
2874
2875         sprintf(name, "%s Playback Volume", ctlname);
2876         idx = ad1988_pin_to_loopback_idx(pin);
2877         if ((err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2878                                HDA_COMPOSE_AMP_VAL(0x20, 3, idx, HDA_INPUT))) < 0)
2879                 return err;
2880         sprintf(name, "%s Playback Switch", ctlname);
2881         if ((err = add_control(spec, AD_CTL_WIDGET_MUTE, name,
2882                                HDA_COMPOSE_AMP_VAL(0x20, 3, idx, HDA_INPUT))) < 0)
2883                 return err;
2884         if (boost) {
2885                 hda_nid_t bnid;
2886                 idx = ad1988_pin_idx(pin);
2887                 bnid = ad1988_boost_nids[idx];
2888                 if (bnid) {
2889                         sprintf(name, "%s Boost", ctlname);
2890                         return add_control(spec, AD_CTL_WIDGET_VOL, name,
2891                                            HDA_COMPOSE_AMP_VAL(bnid, 3, idx, HDA_OUTPUT));
2892
2893                 }
2894         }
2895         return 0;
2896 }
2897
2898 /* create playback/capture controls for input pins */
2899 static int ad1988_auto_create_analog_input_ctls(struct ad198x_spec *spec,
2900                                                 const struct auto_pin_cfg *cfg)
2901 {
2902         struct hda_input_mux *imux = &spec->private_imux;
2903         int i, err;
2904
2905         for (i = 0; i < AUTO_PIN_LAST; i++) {
2906                 err = new_analog_input(spec, cfg->input_pins[i],
2907                                        auto_pin_cfg_labels[i],
2908                                        i <= AUTO_PIN_FRONT_MIC);
2909                 if (err < 0)
2910                         return err;
2911                 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
2912                 imux->items[imux->num_items].index = ad1988_pin_to_adc_idx(cfg->input_pins[i]);
2913                 imux->num_items++;
2914         }
2915         imux->items[imux->num_items].label = "Mix";
2916         imux->items[imux->num_items].index = 9;
2917         imux->num_items++;
2918
2919         if ((err = add_control(spec, AD_CTL_WIDGET_VOL,
2920                                "Analog Mix Playback Volume",
2921                                HDA_COMPOSE_AMP_VAL(0x21, 3, 0x0, HDA_OUTPUT))) < 0)
2922                 return err;
2923         if ((err = add_control(spec, AD_CTL_WIDGET_MUTE,
2924                                "Analog Mix Playback Switch",
2925                                HDA_COMPOSE_AMP_VAL(0x21, 3, 0x0, HDA_OUTPUT))) < 0)
2926                 return err;
2927
2928         return 0;
2929 }
2930
2931 static void ad1988_auto_set_output_and_unmute(struct hda_codec *codec,
2932                                               hda_nid_t nid, int pin_type,
2933                                               int dac_idx)
2934 {
2935         /* set as output */
2936         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
2937         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
2938         switch (nid) {
2939         case 0x11: /* port-A - DAC 04 */
2940                 snd_hda_codec_write(codec, 0x37, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2941                 break;
2942         case 0x14: /* port-B - DAC 06 */
2943                 snd_hda_codec_write(codec, 0x30, 0, AC_VERB_SET_CONNECT_SEL, 0x02);
2944                 break;
2945         case 0x15: /* port-C - DAC 05 */
2946                 snd_hda_codec_write(codec, 0x31, 0, AC_VERB_SET_CONNECT_SEL, 0x00);
2947                 break;
2948         case 0x17: /* port-E - DAC 0a */
2949                 snd_hda_codec_write(codec, 0x32, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2950                 break;
2951         case 0x13: /* mono - DAC 04 */
2952                 snd_hda_codec_write(codec, 0x36, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2953                 break;
2954         }
2955 }
2956
2957 static void ad1988_auto_init_multi_out(struct hda_codec *codec)
2958 {
2959         struct ad198x_spec *spec = codec->spec;
2960         int i;
2961
2962         for (i = 0; i < spec->autocfg.line_outs; i++) {
2963                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
2964                 ad1988_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
2965         }
2966 }
2967
2968 static void ad1988_auto_init_extra_out(struct hda_codec *codec)
2969 {
2970         struct ad198x_spec *spec = codec->spec;
2971         hda_nid_t pin;
2972
2973         pin = spec->autocfg.speaker_pins[0];
2974         if (pin) /* connect to front */
2975                 ad1988_auto_set_output_and_unmute(codec, pin, PIN_OUT, 0);
2976         pin = spec->autocfg.hp_pins[0];
2977         if (pin) /* connect to front */
2978                 ad1988_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
2979 }
2980
2981 static void ad1988_auto_init_analog_input(struct hda_codec *codec)
2982 {
2983         struct ad198x_spec *spec = codec->spec;
2984         int i, idx;
2985
2986         for (i = 0; i < AUTO_PIN_LAST; i++) {
2987                 hda_nid_t nid = spec->autocfg.input_pins[i];
2988                 if (! nid)
2989                         continue;
2990                 switch (nid) {
2991                 case 0x15: /* port-C */
2992                         snd_hda_codec_write(codec, 0x33, 0, AC_VERB_SET_CONNECT_SEL, 0x0);
2993                         break;
2994                 case 0x17: /* port-E */
2995                         snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_CONNECT_SEL, 0x0);
2996                         break;
2997                 }
2998                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2999                                     i <= AUTO_PIN_FRONT_MIC ? PIN_VREF80 : PIN_IN);
3000                 if (nid != AD1988_PIN_CD_NID)
3001                         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3002                                             AMP_OUT_MUTE);
3003                 idx = ad1988_pin_idx(nid);
3004                 if (ad1988_boost_nids[idx])
3005                         snd_hda_codec_write(codec, ad1988_boost_nids[idx], 0,
3006                                             AC_VERB_SET_AMP_GAIN_MUTE,
3007                                             AMP_OUT_ZERO);
3008         }
3009 }
3010
3011 /* parse the BIOS configuration and set up the alc_spec */
3012 /* return 1 if successful, 0 if the proper config is not found, or a negative error code */
3013 static int ad1988_parse_auto_config(struct hda_codec *codec)
3014 {
3015         struct ad198x_spec *spec = codec->spec;
3016         int err;
3017
3018         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
3019                 return err;
3020         if ((err = ad1988_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
3021                 return err;
3022         if (! spec->autocfg.line_outs)
3023                 return 0; /* can't find valid BIOS pin config */
3024         if ((err = ad1988_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
3025             (err = ad1988_auto_create_extra_out(codec,
3026                                                 spec->autocfg.speaker_pins[0],
3027                                                 "Speaker")) < 0 ||
3028             (err = ad1988_auto_create_extra_out(codec, spec->autocfg.hp_pins[0],
3029                                                 "Headphone")) < 0 ||
3030             (err = ad1988_auto_create_analog_input_ctls(spec, &spec->autocfg)) < 0)
3031                 return err;
3032
3033         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3034
3035         if (spec->autocfg.dig_outs)
3036                 spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
3037         if (spec->autocfg.dig_in_pin)
3038                 spec->dig_in_nid = AD1988_SPDIF_IN;
3039
3040         if (spec->kctls.list)
3041                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
3042
3043         spec->init_verbs[spec->num_init_verbs++] = ad1988_6stack_init_verbs;
3044
3045         spec->input_mux = &spec->private_imux;
3046
3047         return 1;
3048 }
3049
3050 /* init callback for auto-configuration model -- overriding the default init */
3051 static int ad1988_auto_init(struct hda_codec *codec)
3052 {
3053         ad198x_init(codec);
3054         ad1988_auto_init_multi_out(codec);
3055         ad1988_auto_init_extra_out(codec);
3056         ad1988_auto_init_analog_input(codec);
3057         return 0;
3058 }
3059
3060
3061 /*
3062  */
3063
3064 static const char *ad1988_models[AD1988_MODEL_LAST] = {
3065         [AD1988_6STACK]         = "6stack",
3066         [AD1988_6STACK_DIG]     = "6stack-dig",
3067         [AD1988_3STACK]         = "3stack",
3068         [AD1988_3STACK_DIG]     = "3stack-dig",
3069         [AD1988_LAPTOP]         = "laptop",
3070         [AD1988_LAPTOP_DIG]     = "laptop-dig",
3071         [AD1988_AUTO]           = "auto",
3072 };
3073
3074 static struct snd_pci_quirk ad1988_cfg_tbl[] = {
3075         SND_PCI_QUIRK(0x1043, 0x81ec, "Asus P5B-DLX", AD1988_6STACK_DIG),
3076         SND_PCI_QUIRK(0x1043, 0x81f6, "Asus M2N-SLI", AD1988_6STACK_DIG),
3077         SND_PCI_QUIRK(0x1043, 0x8277, "Asus P5K-E/WIFI-AP", AD1988_6STACK_DIG),
3078         SND_PCI_QUIRK(0x1043, 0x8311, "Asus P5Q-Premium/Pro", AD1988_6STACK_DIG),
3079         {}
3080 };
3081
3082 static int patch_ad1988(struct hda_codec *codec)
3083 {
3084         struct ad198x_spec *spec;
3085         int err, board_config;
3086
3087         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3088         if (spec == NULL)
3089                 return -ENOMEM;
3090
3091         codec->spec = spec;
3092
3093         if (is_rev2(codec))
3094                 snd_printk(KERN_INFO "patch_analog: AD1988A rev.2 is detected, enable workarounds\n");
3095
3096         board_config = snd_hda_check_board_config(codec, AD1988_MODEL_LAST,
3097                                                   ad1988_models, ad1988_cfg_tbl);
3098         if (board_config < 0) {
3099                 printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
3100                        codec->chip_name);
3101                 board_config = AD1988_AUTO;
3102         }
3103
3104         if (board_config == AD1988_AUTO) {
3105                 /* automatic parse from the BIOS config */
3106                 err = ad1988_parse_auto_config(codec);
3107                 if (err < 0) {
3108                         ad198x_free(codec);
3109                         return err;
3110                 } else if (! err) {
3111                         printk(KERN_INFO "hda_codec: Cannot set up configuration from BIOS.  Using 6-stack mode...\n");
3112                         board_config = AD1988_6STACK;
3113                 }
3114         }
3115
3116         err = snd_hda_attach_beep_device(codec, 0x10);
3117         if (err < 0) {
3118                 ad198x_free(codec);
3119                 return err;
3120         }
3121         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
3122
3123         switch (board_config) {
3124         case AD1988_6STACK:
3125         case AD1988_6STACK_DIG:
3126                 spec->multiout.max_channels = 8;
3127                 spec->multiout.num_dacs = 4;
3128                 if (is_rev2(codec))
3129                         spec->multiout.dac_nids = ad1988_6stack_dac_nids_rev2;
3130                 else
3131                         spec->multiout.dac_nids = ad1988_6stack_dac_nids;
3132                 spec->input_mux = &ad1988_6stack_capture_source;
3133                 spec->num_mixers = 2;
3134                 if (is_rev2(codec))
3135                         spec->mixers[0] = ad1988_6stack_mixers1_rev2;
3136                 else
3137                         spec->mixers[0] = ad1988_6stack_mixers1;
3138                 spec->mixers[1] = ad1988_6stack_mixers2;
3139                 spec->num_init_verbs = 1;
3140                 spec->init_verbs[0] = ad1988_6stack_init_verbs;
3141                 if (board_config == AD1988_6STACK_DIG) {
3142                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
3143                         spec->dig_in_nid = AD1988_SPDIF_IN;
3144                 }
3145                 break;
3146         case AD1988_3STACK:
3147         case AD1988_3STACK_DIG:
3148                 spec->multiout.max_channels = 6;
3149                 spec->multiout.num_dacs = 3;
3150                 if (is_rev2(codec))
3151                         spec->multiout.dac_nids = ad1988_3stack_dac_nids_rev2;
3152                 else
3153                         spec->multiout.dac_nids = ad1988_3stack_dac_nids;
3154                 spec->input_mux = &ad1988_6stack_capture_source;
3155                 spec->channel_mode = ad1988_3stack_modes;
3156                 spec->num_channel_mode = ARRAY_SIZE(ad1988_3stack_modes);
3157                 spec->num_mixers = 2;
3158                 if (is_rev2(codec))
3159                         spec->mixers[0] = ad1988_3stack_mixers1_rev2;
3160                 else
3161                         spec->mixers[0] = ad1988_3stack_mixers1;
3162                 spec->mixers[1] = ad1988_3stack_mixers2;
3163                 spec->num_init_verbs = 1;
3164                 spec->init_verbs[0] = ad1988_3stack_init_verbs;
3165                 if (board_config == AD1988_3STACK_DIG)
3166                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
3167                 break;
3168         case AD1988_LAPTOP:
3169         case AD1988_LAPTOP_DIG:
3170                 spec->multiout.max_channels = 2;
3171                 spec->multiout.num_dacs = 1;
3172                 spec->multiout.dac_nids = ad1988_3stack_dac_nids;
3173                 spec->input_mux = &ad1988_laptop_capture_source;
3174                 spec->num_mixers = 1;
3175                 spec->mixers[0] = ad1988_laptop_mixers;
3176                 spec->inv_eapd = 1; /* inverted EAPD */
3177                 spec->num_init_verbs = 1;
3178                 spec->init_verbs[0] = ad1988_laptop_init_verbs;
3179                 if (board_config == AD1988_LAPTOP_DIG)
3180                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
3181                 break;
3182         }
3183
3184         spec->num_adc_nids = ARRAY_SIZE(ad1988_adc_nids);
3185         spec->adc_nids = ad1988_adc_nids;
3186         spec->capsrc_nids = ad1988_capsrc_nids;
3187         spec->mixers[spec->num_mixers++] = ad1988_capture_mixers;
3188         spec->init_verbs[spec->num_init_verbs++] = ad1988_capture_init_verbs;
3189         if (spec->multiout.dig_out_nid) {
3190                 if (codec->vendor_id >= 0x11d4989a) {
3191                         spec->mixers[spec->num_mixers++] =
3192                                 ad1989_spdif_out_mixers;
3193                         spec->init_verbs[spec->num_init_verbs++] =
3194                                 ad1989_spdif_init_verbs;
3195                         codec->slave_dig_outs = ad1989b_slave_dig_outs;
3196                 } else {
3197                         spec->mixers[spec->num_mixers++] =
3198                                 ad1988_spdif_out_mixers;
3199                         spec->init_verbs[spec->num_init_verbs++] =
3200                                 ad1988_spdif_init_verbs;
3201                 }
3202         }
3203         if (spec->dig_in_nid && codec->vendor_id < 0x11d4989a)
3204                 spec->mixers[spec->num_mixers++] = ad1988_spdif_in_mixers;
3205
3206         codec->patch_ops = ad198x_patch_ops;
3207         switch (board_config) {
3208         case AD1988_AUTO:
3209                 codec->patch_ops.init = ad1988_auto_init;
3210                 break;
3211         case AD1988_LAPTOP:
3212         case AD1988_LAPTOP_DIG:
3213                 codec->patch_ops.unsol_event = ad1988_laptop_unsol_event;
3214                 break;
3215         }
3216 #ifdef CONFIG_SND_HDA_POWER_SAVE
3217         spec->loopback.amplist = ad1988_loopbacks;
3218 #endif
3219         spec->vmaster_nid = 0x04;
3220
3221         codec->no_trigger_sense = 1;
3222
3223         return 0;
3224 }
3225
3226
3227 /*
3228  * AD1884 / AD1984
3229  *
3230  * port-B - front line/mic-in
3231  * port-E - aux in/out
3232  * port-F - aux in/out
3233  * port-C - rear line/mic-in
3234  * port-D - rear line/hp-out
3235  * port-A - front line/hp-out
3236  *
3237  * AD1984 = AD1884 + two digital mic-ins
3238  *
3239  * FIXME:
3240  * For simplicity, we share the single DAC for both HP and line-outs
3241  * right now.  The inidividual playbacks could be easily implemented,
3242  * but no build-up framework is given, so far.
3243  */
3244
3245 static hda_nid_t ad1884_dac_nids[1] = {
3246         0x04,
3247 };
3248
3249 static hda_nid_t ad1884_adc_nids[2] = {
3250         0x08, 0x09,
3251 };
3252
3253 static hda_nid_t ad1884_capsrc_nids[2] = {
3254         0x0c, 0x0d,
3255 };
3256
3257 #define AD1884_SPDIF_OUT        0x02
3258
3259 static struct hda_input_mux ad1884_capture_source = {
3260         .num_items = 4,
3261         .items = {
3262                 { "Front Mic", 0x0 },
3263                 { "Mic", 0x1 },
3264                 { "CD", 0x2 },
3265                 { "Mix", 0x3 },
3266         },
3267 };
3268
3269 static struct snd_kcontrol_new ad1884_base_mixers[] = {
3270         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
3271         /* HDA_CODEC_VOLUME_IDX("PCM Playback Volume", 1, 0x03, 0x0, HDA_OUTPUT), */
3272         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
3273         HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3274         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
3275         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
3276         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3277         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3278         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
3279         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
3280         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x02, HDA_INPUT),
3281         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x02, HDA_INPUT),
3282         HDA_CODEC_VOLUME("Mic Boost", 0x15, 0x0, HDA_INPUT),
3283         HDA_CODEC_VOLUME("Front Mic Boost", 0x14, 0x0, HDA_INPUT),
3284         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3285         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3286         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3287         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3288         {
3289                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3290                 /* The multiple "Capture Source" controls confuse alsamixer
3291                  * So call somewhat different..
3292                  */
3293                 /* .name = "Capture Source", */
3294                 .name = "Input Source",
3295                 .count = 2,
3296                 .info = ad198x_mux_enum_info,
3297                 .get = ad198x_mux_enum_get,
3298                 .put = ad198x_mux_enum_put,
3299         },
3300         /* SPDIF controls */
3301         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
3302         {
3303                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3304                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
3305                 /* identical with ad1983 */
3306                 .info = ad1983_spdif_route_info,
3307                 .get = ad1983_spdif_route_get,
3308                 .put = ad1983_spdif_route_put,
3309         },
3310         { } /* end */
3311 };
3312
3313 static struct snd_kcontrol_new ad1984_dmic_mixers[] = {
3314         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x05, 0x0, HDA_INPUT),
3315         HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x05, 0x0, HDA_INPUT),
3316         HDA_CODEC_VOLUME_IDX("Digital Mic Capture Volume", 1, 0x06, 0x0,
3317                              HDA_INPUT),
3318         HDA_CODEC_MUTE_IDX("Digital Mic Capture Switch", 1, 0x06, 0x0,
3319                            HDA_INPUT),
3320         { } /* end */
3321 };
3322
3323 /*
3324  * initialization verbs
3325  */
3326 static struct hda_verb ad1884_init_verbs[] = {
3327         /* DACs; mute as default */
3328         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3329         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3330         /* Port-A (HP) mixer */
3331         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3332         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3333         /* Port-A pin */
3334         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3335         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3336         /* HP selector - select DAC2 */
3337         {0x22, AC_VERB_SET_CONNECT_SEL, 0x1},
3338         /* Port-D (Line-out) mixer */
3339         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3340         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3341         /* Port-D pin */
3342         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3343         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3344         /* Mono-out mixer */
3345         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3346         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3347         /* Mono-out pin */
3348         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3349         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3350         /* Mono selector */
3351         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
3352         /* Port-B (front mic) pin */
3353         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3354         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3355         /* Port-C (rear mic) pin */
3356         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3357         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3358         /* Analog mixer; mute as default */
3359         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3360         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3361         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3362         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3363         /* Analog Mix output amp */
3364         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
3365         /* SPDIF output selector */
3366         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
3367         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
3368         { } /* end */
3369 };
3370
3371 #ifdef CONFIG_SND_HDA_POWER_SAVE
3372 static struct hda_amp_list ad1884_loopbacks[] = {
3373         { 0x20, HDA_INPUT, 0 }, /* Front Mic */
3374         { 0x20, HDA_INPUT, 1 }, /* Mic */
3375         { 0x20, HDA_INPUT, 2 }, /* CD */
3376         { 0x20, HDA_INPUT, 4 }, /* Docking */
3377         { } /* end */
3378 };
3379 #endif
3380
3381 static const char *ad1884_slave_vols[] = {
3382         "PCM Playback Volume",
3383         "Mic Playback Volume",
3384         "Mono Playback Volume",
3385         "Front Mic Playback Volume",
3386         "Mic Playback Volume",
3387         "CD Playback Volume",
3388         "Internal Mic Playback Volume",
3389         "Docking Mic Playback Volume",
3390         /* "Beep Playback Volume", */
3391         "IEC958 Playback Volume",
3392         NULL
3393 };
3394
3395 static int patch_ad1884(struct hda_codec *codec)
3396 {
3397         struct ad198x_spec *spec;
3398         int err;
3399
3400         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3401         if (spec == NULL)
3402                 return -ENOMEM;
3403
3404         codec->spec = spec;
3405
3406         err = snd_hda_attach_beep_device(codec, 0x10);
3407         if (err < 0) {
3408                 ad198x_free(codec);
3409                 return err;
3410         }
3411         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
3412
3413         spec->multiout.max_channels = 2;
3414         spec->multiout.num_dacs = ARRAY_SIZE(ad1884_dac_nids);
3415         spec->multiout.dac_nids = ad1884_dac_nids;
3416         spec->multiout.dig_out_nid = AD1884_SPDIF_OUT;
3417         spec->num_adc_nids = ARRAY_SIZE(ad1884_adc_nids);
3418         spec->adc_nids = ad1884_adc_nids;
3419         spec->capsrc_nids = ad1884_capsrc_nids;
3420         spec->input_mux = &ad1884_capture_source;
3421         spec->num_mixers = 1;
3422         spec->mixers[0] = ad1884_base_mixers;
3423         spec->num_init_verbs = 1;
3424         spec->init_verbs[0] = ad1884_init_verbs;
3425         spec->spdif_route = 0;
3426 #ifdef CONFIG_SND_HDA_POWER_SAVE
3427         spec->loopback.amplist = ad1884_loopbacks;
3428 #endif
3429         spec->vmaster_nid = 0x04;
3430         /* we need to cover all playback volumes */
3431         spec->slave_vols = ad1884_slave_vols;
3432
3433         codec->patch_ops = ad198x_patch_ops;
3434
3435         codec->no_trigger_sense = 1;
3436
3437         return 0;
3438 }
3439
3440 /*
3441  * Lenovo Thinkpad T61/X61
3442  */
3443 static struct hda_input_mux ad1984_thinkpad_capture_source = {
3444         .num_items = 4,
3445         .items = {
3446                 { "Mic", 0x0 },
3447                 { "Internal Mic", 0x1 },
3448                 { "Mix", 0x3 },
3449                 { "Docking-Station", 0x4 },
3450         },
3451 };
3452
3453
3454 /*
3455  * Dell Precision T3400
3456  */
3457 static struct hda_input_mux ad1984_dell_desktop_capture_source = {
3458         .num_items = 3,
3459         .items = {
3460                 { "Front Mic", 0x0 },
3461                 { "Line-In", 0x1 },
3462                 { "Mix", 0x3 },
3463         },
3464 };
3465
3466
3467 static struct snd_kcontrol_new ad1984_thinkpad_mixers[] = {
3468         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
3469         /* HDA_CODEC_VOLUME_IDX("PCM Playback Volume", 1, 0x03, 0x0, HDA_OUTPUT), */
3470         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
3471         HDA_CODEC_MUTE("Speaker Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3472         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3473         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3474         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
3475         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
3476         HDA_CODEC_VOLUME("Docking Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
3477         HDA_CODEC_MUTE("Docking Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
3478         HDA_CODEC_VOLUME("Mic Boost", 0x14, 0x0, HDA_INPUT),
3479         HDA_CODEC_VOLUME("Internal Mic Boost", 0x15, 0x0, HDA_INPUT),
3480         HDA_CODEC_VOLUME("Docking Mic Boost", 0x25, 0x0, HDA_OUTPUT),
3481         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3482         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3483         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3484         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3485         {
3486                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3487                 /* The multiple "Capture Source" controls confuse alsamixer
3488                  * So call somewhat different..
3489                  */
3490                 /* .name = "Capture Source", */
3491                 .name = "Input Source",
3492                 .count = 2,
3493                 .info = ad198x_mux_enum_info,
3494                 .get = ad198x_mux_enum_get,
3495                 .put = ad198x_mux_enum_put,
3496         },
3497         /* SPDIF controls */
3498         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
3499         {
3500                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3501                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
3502                 /* identical with ad1983 */
3503                 .info = ad1983_spdif_route_info,
3504                 .get = ad1983_spdif_route_get,
3505                 .put = ad1983_spdif_route_put,
3506         },
3507         { } /* end */
3508 };
3509
3510 /* additional verbs */
3511 static struct hda_verb ad1984_thinkpad_init_verbs[] = {
3512         /* Port-E (docking station mic) pin */
3513         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3514         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3515         /* docking mic boost */
3516         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3517         /* Analog mixer - docking mic; mute as default */
3518         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3519         /* enable EAPD bit */
3520         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x02},
3521         { } /* end */
3522 };
3523
3524 /*
3525  * Dell Precision T3400
3526  */
3527 static struct snd_kcontrol_new ad1984_dell_desktop_mixers[] = {
3528         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
3529         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
3530         HDA_CODEC_MUTE("Speaker Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3531         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
3532         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
3533         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3534         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3535         HDA_CODEC_VOLUME("Line-In Playback Volume", 0x20, 0x01, HDA_INPUT),
3536         HDA_CODEC_MUTE("Line-In Playback Switch", 0x20, 0x01, HDA_INPUT),
3537         HDA_CODEC_VOLUME("Line-In Boost", 0x15, 0x0, HDA_INPUT),
3538         HDA_CODEC_VOLUME("Front Mic Boost", 0x14, 0x0, HDA_INPUT),
3539         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3540         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3541         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3542         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3543         {
3544                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3545                 /* The multiple "Capture Source" controls confuse alsamixer
3546                  * So call somewhat different..
3547                  */
3548                 /* .name = "Capture Source", */
3549                 .name = "Input Source",
3550                 .count = 2,
3551                 .info = ad198x_mux_enum_info,
3552                 .get = ad198x_mux_enum_get,
3553                 .put = ad198x_mux_enum_put,
3554         },
3555         { } /* end */
3556 };
3557
3558 /* Digial MIC ADC NID 0x05 + 0x06 */
3559 static int ad1984_pcm_dmic_prepare(struct hda_pcm_stream *hinfo,
3560                                    struct hda_codec *codec,
3561                                    unsigned int stream_tag,
3562                                    unsigned int format,
3563                                    struct snd_pcm_substream *substream)
3564 {
3565         snd_hda_codec_setup_stream(codec, 0x05 + substream->number,
3566                                    stream_tag, 0, format);
3567         return 0;
3568 }
3569
3570 static int ad1984_pcm_dmic_cleanup(struct hda_pcm_stream *hinfo,
3571                                    struct hda_codec *codec,
3572                                    struct snd_pcm_substream *substream)
3573 {
3574         snd_hda_codec_cleanup_stream(codec, 0x05 + substream->number);
3575         return 0;
3576 }
3577
3578 static struct hda_pcm_stream ad1984_pcm_dmic_capture = {
3579         .substreams = 2,
3580         .channels_min = 2,
3581         .channels_max = 2,
3582         .nid = 0x05,
3583         .ops = {
3584                 .prepare = ad1984_pcm_dmic_prepare,
3585                 .cleanup = ad1984_pcm_dmic_cleanup
3586         },
3587 };
3588
3589 static int ad1984_build_pcms(struct hda_codec *codec)
3590 {
3591         struct ad198x_spec *spec = codec->spec;
3592         struct hda_pcm *info;
3593         int err;
3594
3595         err = ad198x_build_pcms(codec);
3596         if (err < 0)
3597                 return err;
3598
3599         info = spec->pcm_rec + codec->num_pcms;
3600         codec->num_pcms++;
3601         info->name = "AD1984 Digital Mic";
3602         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad1984_pcm_dmic_capture;
3603         return 0;
3604 }
3605
3606 /* models */
3607 enum {
3608         AD1984_BASIC,
3609         AD1984_THINKPAD,
3610         AD1984_DELL_DESKTOP,
3611         AD1984_MODELS
3612 };
3613
3614 static const char *ad1984_models[AD1984_MODELS] = {
3615         [AD1984_BASIC]          = "basic",
3616         [AD1984_THINKPAD]       = "thinkpad",
3617         [AD1984_DELL_DESKTOP]   = "dell_desktop",
3618 };
3619
3620 static struct snd_pci_quirk ad1984_cfg_tbl[] = {
3621         /* Lenovo Thinkpad T61/X61 */
3622         SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo Thinkpad", AD1984_THINKPAD),
3623         SND_PCI_QUIRK(0x1028, 0x0214, "Dell T3400", AD1984_DELL_DESKTOP),
3624         {}
3625 };
3626
3627 static int patch_ad1984(struct hda_codec *codec)
3628 {
3629         struct ad198x_spec *spec;
3630         int board_config, err;
3631
3632         err = patch_ad1884(codec);
3633         if (err < 0)
3634                 return err;
3635         spec = codec->spec;
3636         board_config = snd_hda_check_board_config(codec, AD1984_MODELS,
3637                                                   ad1984_models, ad1984_cfg_tbl);
3638         switch (board_config) {
3639         case AD1984_BASIC:
3640                 /* additional digital mics */
3641                 spec->mixers[spec->num_mixers++] = ad1984_dmic_mixers;
3642                 codec->patch_ops.build_pcms = ad1984_build_pcms;
3643                 break;
3644         case AD1984_THINKPAD:
3645                 spec->multiout.dig_out_nid = AD1884_SPDIF_OUT;
3646                 spec->input_mux = &ad1984_thinkpad_capture_source;
3647                 spec->mixers[0] = ad1984_thinkpad_mixers;
3648                 spec->init_verbs[spec->num_init_verbs++] = ad1984_thinkpad_init_verbs;
3649                 break;
3650         case AD1984_DELL_DESKTOP:
3651                 spec->multiout.dig_out_nid = 0;
3652                 spec->input_mux = &ad1984_dell_desktop_capture_source;
3653                 spec->mixers[0] = ad1984_dell_desktop_mixers;
3654                 break;
3655         }
3656         return 0;
3657 }
3658
3659
3660 /*
3661  * AD1883 / AD1884A / AD1984A / AD1984B
3662  *
3663  * port-B (0x14) - front mic-in
3664  * port-E (0x1c) - rear mic-in
3665  * port-F (0x16) - CD / ext out
3666  * port-C (0x15) - rear line-in
3667  * port-D (0x12) - rear line-out
3668  * port-A (0x11) - front hp-out
3669  *
3670  * AD1984A = AD1884A + digital-mic
3671  * AD1883 = equivalent with AD1984A
3672  * AD1984B = AD1984A + extra SPDIF-out
3673  *
3674  * FIXME:
3675  * We share the single DAC for both HP and line-outs (see AD1884/1984).
3676  */
3677
3678 static hda_nid_t ad1884a_dac_nids[1] = {
3679         0x03,
3680 };
3681
3682 #define ad1884a_adc_nids        ad1884_adc_nids
3683 #define ad1884a_capsrc_nids     ad1884_capsrc_nids
3684
3685 #define AD1884A_SPDIF_OUT       0x02
3686
3687 static struct hda_input_mux ad1884a_capture_source = {
3688         .num_items = 5,
3689         .items = {
3690                 { "Front Mic", 0x0 },
3691                 { "Mic", 0x4 },
3692                 { "Line", 0x1 },
3693                 { "CD", 0x2 },
3694                 { "Mix", 0x3 },
3695         },
3696 };
3697
3698 static struct snd_kcontrol_new ad1884a_base_mixers[] = {
3699         HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
3700         HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),
3701         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
3702         HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3703         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
3704         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
3705         HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
3706         HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
3707         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3708         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3709         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x01, HDA_INPUT),
3710         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x01, HDA_INPUT),
3711         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
3712         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
3713         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x02, HDA_INPUT),
3714         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x02, HDA_INPUT),
3715         HDA_CODEC_VOLUME("Front Mic Boost", 0x14, 0x0, HDA_INPUT),
3716         HDA_CODEC_VOLUME("Line Boost", 0x15, 0x0, HDA_INPUT),
3717         HDA_CODEC_VOLUME("Mic Boost", 0x25, 0x0, HDA_OUTPUT),
3718         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3719         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3720         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3721         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3722         {
3723                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3724                 /* The multiple "Capture Source" controls confuse alsamixer
3725                  * So call somewhat different..
3726                  */
3727                 /* .name = "Capture Source", */
3728                 .name = "Input Source",
3729                 .count = 2,
3730                 .info = ad198x_mux_enum_info,
3731                 .get = ad198x_mux_enum_get,
3732                 .put = ad198x_mux_enum_put,
3733         },
3734         /* SPDIF controls */
3735         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
3736         {
3737                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3738                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
3739                 /* identical with ad1983 */
3740                 .info = ad1983_spdif_route_info,
3741                 .get = ad1983_spdif_route_get,
3742                 .put = ad1983_spdif_route_put,
3743         },
3744         { } /* end */
3745 };
3746
3747 /*
3748  * initialization verbs
3749  */
3750 static struct hda_verb ad1884a_init_verbs[] = {
3751         /* DACs; unmute as default */
3752         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
3753         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
3754         /* Port-A (HP) mixer - route only from analog mixer */
3755         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3756         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3757         /* Port-A pin */
3758         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3759         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3760         /* Port-D (Line-out) mixer - route only from analog mixer */
3761         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3762         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3763         /* Port-D pin */
3764         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3765         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3766         /* Mono-out mixer - route only from analog mixer */
3767         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3768         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3769         /* Mono-out pin */
3770         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3771         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3772         /* Port-B (front mic) pin */
3773         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3774         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3775         /* Port-C (rear line-in) pin */
3776         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3777         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3778         /* Port-E (rear mic) pin */
3779         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3780         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3781         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* no boost */
3782         /* Port-F (CD) pin */
3783         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3784         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3785         /* Analog mixer; mute as default */
3786         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3787         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3788         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3789         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3790         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)}, /* aux */
3791         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
3792         /* Analog Mix output amp */
3793         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3794         /* capture sources */
3795         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
3796         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3797         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
3798         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3799         /* SPDIF output amp */
3800         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
3801         { } /* end */
3802 };
3803
3804 #ifdef CONFIG_SND_HDA_POWER_SAVE
3805 static struct hda_amp_list ad1884a_loopbacks[] = {
3806         { 0x20, HDA_INPUT, 0 }, /* Front Mic */
3807         { 0x20, HDA_INPUT, 1 }, /* Mic */
3808         { 0x20, HDA_INPUT, 2 }, /* CD */
3809         { 0x20, HDA_INPUT, 4 }, /* Docking */
3810         { } /* end */
3811 };
3812 #endif
3813
3814 /*
3815  * Laptop model
3816  *
3817  * Port A: Headphone jack
3818  * Port B: MIC jack
3819  * Port C: Internal MIC
3820  * Port D: Dock Line Out (if enabled)
3821  * Port E: Dock Line In (if enabled)
3822  * Port F: Internal speakers
3823  */
3824
3825 static int ad1884a_mobile_master_sw_put(struct snd_kcontrol *kcontrol,
3826                                         struct snd_ctl_elem_value *ucontrol)
3827 {
3828         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3829         int ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3830         int mute = (!ucontrol->value.integer.value[0] &&
3831                     !ucontrol->value.integer.value[1]);
3832         /* toggle GPIO1 according to the mute state */
3833         snd_hda_codec_write_cache(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
3834                             mute ? 0x02 : 0x0);
3835         return ret;
3836 }
3837
3838 static struct snd_kcontrol_new ad1884a_laptop_mixers[] = {
3839         HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
3840         {
3841                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3842                 .name = "Master Playback Switch",
3843                 .subdevice = HDA_SUBDEV_AMP_FLAG,
3844                 .info = snd_hda_mixer_amp_switch_info,
3845                 .get = snd_hda_mixer_amp_switch_get,
3846                 .put = ad1884a_mobile_master_sw_put,
3847                 .private_value = HDA_COMPOSE_AMP_VAL(0x21, 3, 0, HDA_OUTPUT),
3848         },
3849         HDA_CODEC_MUTE("Dock Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3850         HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
3851         HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
3852         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3853         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3854         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
3855         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
3856         HDA_CODEC_VOLUME("Dock Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
3857         HDA_CODEC_MUTE("Dock Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
3858         HDA_CODEC_VOLUME("Mic Boost", 0x14, 0x0, HDA_INPUT),
3859         HDA_CODEC_VOLUME("Internal Mic Boost", 0x15, 0x0, HDA_INPUT),
3860         HDA_CODEC_VOLUME("Dock Mic Boost", 0x25, 0x0, HDA_OUTPUT),
3861         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3862         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3863         { } /* end */
3864 };
3865
3866 static struct snd_kcontrol_new ad1884a_mobile_mixers[] = {
3867         HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
3868         /*HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),*/
3869         {
3870                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3871                 .name = "Master Playback Switch",
3872                 .subdevice = HDA_SUBDEV_AMP_FLAG,
3873                 .info = snd_hda_mixer_amp_switch_info,
3874                 .get = snd_hda_mixer_amp_switch_get,
3875                 .put = ad1884a_mobile_master_sw_put,
3876                 .private_value = HDA_COMPOSE_AMP_VAL(0x21, 3, 0, HDA_OUTPUT),
3877         },
3878         HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
3879         HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
3880         HDA_CODEC_VOLUME("Mic Capture Volume", 0x14, 0x0, HDA_INPUT),
3881         HDA_CODEC_VOLUME("Internal Mic Capture Volume", 0x15, 0x0, HDA_INPUT),
3882         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3883         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3884         { } /* end */
3885 };
3886
3887 /* mute internal speaker if HP is plugged */
3888 static void ad1884a_hp_automute(struct hda_codec *codec)
3889 {
3890         unsigned int present;
3891
3892         present = snd_hda_jack_detect(codec, 0x11);
3893         snd_hda_codec_amp_stereo(codec, 0x16, HDA_OUTPUT, 0,
3894                                  HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
3895         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_EAPD_BTLENABLE,
3896                             present ? 0x00 : 0x02);
3897 }
3898
3899 /* switch to external mic if plugged */
3900 static void ad1884a_hp_automic(struct hda_codec *codec)
3901 {
3902         unsigned int present;
3903
3904         present = snd_hda_jack_detect(codec, 0x14);
3905         snd_hda_codec_write(codec, 0x0c, 0, AC_VERB_SET_CONNECT_SEL,
3906                             present ? 0 : 1);
3907 }
3908
3909 #define AD1884A_HP_EVENT                0x37
3910 #define AD1884A_MIC_EVENT               0x36
3911
3912 /* unsolicited event for HP jack sensing */
3913 static void ad1884a_hp_unsol_event(struct hda_codec *codec, unsigned int res)
3914 {
3915         switch (res >> 26) {
3916         case AD1884A_HP_EVENT:
3917                 ad1884a_hp_automute(codec);
3918                 break;
3919         case AD1884A_MIC_EVENT:
3920                 ad1884a_hp_automic(codec);
3921                 break;
3922         }
3923 }
3924
3925 /* initialize jack-sensing, too */
3926 static int ad1884a_hp_init(struct hda_codec *codec)
3927 {
3928         ad198x_init(codec);
3929         ad1884a_hp_automute(codec);
3930         ad1884a_hp_automic(codec);
3931         return 0;
3932 }
3933
3934 /* mute internal speaker if HP or docking HP is plugged */
3935 static void ad1884a_laptop_automute(struct hda_codec *codec)
3936 {
3937         unsigned int present;
3938
3939         present = snd_hda_jack_detect(codec, 0x11);
3940         if (!present)
3941                 present = snd_hda_jack_detect(codec, 0x12);
3942         snd_hda_codec_amp_stereo(codec, 0x16, HDA_OUTPUT, 0,
3943                                  HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
3944         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_EAPD_BTLENABLE,
3945                             present ? 0x00 : 0x02);
3946 }
3947
3948 /* switch to external mic if plugged */
3949 static void ad1884a_laptop_automic(struct hda_codec *codec)
3950 {
3951         unsigned int idx;
3952
3953         if (snd_hda_jack_detect(codec, 0x14))
3954                 idx = 0;
3955         else if (snd_hda_jack_detect(codec, 0x1c))
3956                 idx = 4;
3957         else
3958                 idx = 1;
3959         snd_hda_codec_write(codec, 0x0c, 0, AC_VERB_SET_CONNECT_SEL, idx);
3960 }
3961
3962 /* unsolicited event for HP jack sensing */
3963 static void ad1884a_laptop_unsol_event(struct hda_codec *codec,
3964                                        unsigned int res)
3965 {
3966         switch (res >> 26) {
3967         case AD1884A_HP_EVENT:
3968                 ad1884a_laptop_automute(codec);
3969                 break;
3970         case AD1884A_MIC_EVENT:
3971                 ad1884a_laptop_automic(codec);
3972                 break;
3973         }
3974 }
3975
3976 /* initialize jack-sensing, too */
3977 static int ad1884a_laptop_init(struct hda_codec *codec)
3978 {
3979         ad198x_init(codec);
3980         ad1884a_laptop_automute(codec);
3981         ad1884a_laptop_automic(codec);
3982         return 0;
3983 }
3984
3985 /* additional verbs for laptop model */
3986 static struct hda_verb ad1884a_laptop_verbs[] = {
3987         /* Port-A (HP) pin - always unmuted */
3988         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3989         /* Port-F (int speaker) mixer - route only from analog mixer */
3990         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3991         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3992         /* Port-F (int speaker) pin */
3993         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3994         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3995         /* required for compaq 6530s/6531s speaker output */
3996         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3997         /* Port-C pin - internal mic-in */
3998         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3999         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0x7002}, /* raise mic as default */
4000         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0x7002}, /* raise mic as default */
4001         /* Port-D (docking line-out) pin - default unmuted */
4002         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4003         /* analog mix */
4004         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4005         /* unsolicited event for pin-sense */
4006         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
4007         {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
4008         {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_MIC_EVENT},
4009         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_MIC_EVENT},
4010         /* allow to touch GPIO1 (for mute control) */
4011         {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
4012         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
4013         {0x01, AC_VERB_SET_GPIO_DATA, 0x02}, /* first muted */
4014         { } /* end */
4015 };
4016
4017 static struct hda_verb ad1884a_mobile_verbs[] = {
4018         /* DACs; unmute as default */
4019         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
4020         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
4021         /* Port-A (HP) mixer - route only from analog mixer */
4022         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4023         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4024         /* Port-A pin */
4025         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
4026         /* Port-A (HP) pin - always unmuted */
4027         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4028         /* Port-B (mic jack) pin */
4029         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4030         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0x7002}, /* raise mic as default */
4031         /* Port-C (int mic) pin */
4032         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4033         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0x7002}, /* raise mic as default */
4034         /* Port-F (int speaker) mixer - route only from analog mixer */
4035         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4036         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4037         /* Port-F pin */
4038         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
4039         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4040         /* Analog mixer; mute as default */
4041         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4042         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4043         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4044         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4045         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4046         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
4047         /* Analog Mix output amp */
4048         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4049         /* capture sources */
4050         /* {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0}, */ /* set via unsol */
4051         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4052         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
4053         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4054         /* unsolicited event for pin-sense */
4055         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
4056         {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_MIC_EVENT},
4057         /* allow to touch GPIO1 (for mute control) */
4058         {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
4059         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
4060         {0x01, AC_VERB_SET_GPIO_DATA, 0x02}, /* first muted */
4061         { } /* end */
4062 };
4063
4064 /*
4065  * Thinkpad X300
4066  * 0x11 - HP
4067  * 0x12 - speaker
4068  * 0x14 - mic-in
4069  * 0x17 - built-in mic
4070  */
4071
4072 static struct hda_verb ad1984a_thinkpad_verbs[] = {
4073         /* HP unmute */
4074         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4075         /* analog mix */
4076         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4077         /* turn on EAPD */
4078         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x02},
4079         /* unsolicited event for pin-sense */
4080         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
4081         /* internal mic - dmic */
4082         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
4083         /* set magic COEFs for dmic */
4084         {0x01, AC_VERB_SET_COEF_INDEX, 0x13f7},
4085         {0x01, AC_VERB_SET_PROC_COEF, 0x08},
4086         { } /* end */
4087 };
4088
4089 static struct snd_kcontrol_new ad1984a_thinkpad_mixers[] = {
4090         HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
4091         HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),
4092         HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
4093         HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
4094         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
4095         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
4096         HDA_CODEC_VOLUME("Mic Boost", 0x14, 0x0, HDA_INPUT),
4097         HDA_CODEC_VOLUME("Internal Mic Boost", 0x17, 0x0, HDA_INPUT),
4098         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
4099         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
4100         {
4101                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4102                 .name = "Capture Source",
4103                 .info = ad198x_mux_enum_info,
4104                 .get = ad198x_mux_enum_get,
4105                 .put = ad198x_mux_enum_put,
4106         },
4107         { } /* end */
4108 };
4109
4110 static struct hda_input_mux ad1984a_thinkpad_capture_source = {
4111         .num_items = 3,
4112         .items = {
4113                 { "Mic", 0x0 },
4114                 { "Internal Mic", 0x5 },
4115                 { "Mix", 0x3 },
4116         },
4117 };
4118
4119 /* mute internal speaker if HP is plugged */
4120 static void ad1984a_thinkpad_automute(struct hda_codec *codec)
4121 {
4122         unsigned int present;
4123
4124         present = snd_hda_jack_detect(codec, 0x11);
4125         snd_hda_codec_amp_stereo(codec, 0x12, HDA_OUTPUT, 0,
4126                                  HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
4127 }
4128
4129 /* unsolicited event for HP jack sensing */
4130 static void ad1984a_thinkpad_unsol_event(struct hda_codec *codec,
4131                                          unsigned int res)
4132 {
4133         if ((res >> 26) != AD1884A_HP_EVENT)
4134                 return;
4135         ad1984a_thinkpad_automute(codec);
4136 }
4137
4138 /* initialize jack-sensing, too */
4139 static int ad1984a_thinkpad_init(struct hda_codec *codec)
4140 {
4141         ad198x_init(codec);
4142         ad1984a_thinkpad_automute(codec);
4143         return 0;
4144 }
4145
4146 /*
4147  * HP Touchsmart
4148  * port-A (0x11)      - front hp-out
4149  * port-B (0x14)      - unused
4150  * port-C (0x15)      - unused
4151  * port-D (0x12)      - rear line out
4152  * port-E (0x1c)      - front mic-in
4153  * port-F (0x16)      - Internal speakers
4154  * digital-mic (0x17) - Internal mic
4155  */
4156
4157 static struct hda_verb ad1984a_touchsmart_verbs[] = {
4158         /* DACs; unmute as default */
4159         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
4160         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
4161         /* Port-A (HP) mixer - route only from analog mixer */
4162         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4163         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4164         /* Port-A pin */
4165         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
4166         /* Port-A (HP) pin - always unmuted */
4167         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4168         /* Port-E (int speaker) mixer - route only from analog mixer */
4169         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, 0x03},
4170         /* Port-E pin */
4171         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
4172         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4173         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4174         /* Port-F (int speaker) mixer - route only from analog mixer */
4175         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4176         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4177         /* Port-F pin */
4178         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
4179         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4180         /* Analog mixer; mute as default */
4181         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4182         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4183         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4184         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4185         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4186         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
4187         /* Analog Mix output amp */
4188         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4189         /* capture sources */
4190         /* {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0}, */ /* set via unsol */
4191         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4192         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
4193         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4194         /* unsolicited event for pin-sense */
4195         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
4196         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_MIC_EVENT},
4197         /* allow to touch GPIO1 (for mute control) */
4198         {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
4199         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
4200         {0x01, AC_VERB_SET_GPIO_DATA, 0x02}, /* first muted */
4201         /* internal mic - dmic */
4202         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
4203         /* set magic COEFs for dmic */
4204         {0x01, AC_VERB_SET_COEF_INDEX, 0x13f7},
4205         {0x01, AC_VERB_SET_PROC_COEF, 0x08},
4206         { } /* end */
4207 };
4208
4209 static struct snd_kcontrol_new ad1984a_touchsmart_mixers[] = {
4210         HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
4211 /*      HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),*/
4212         {
4213                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4214                 .subdevice = HDA_SUBDEV_AMP_FLAG,
4215                 .name = "Master Playback Switch",
4216                 .info = snd_hda_mixer_amp_switch_info,
4217                 .get = snd_hda_mixer_amp_switch_get,
4218                 .put = ad1884a_mobile_master_sw_put,
4219                 .private_value = HDA_COMPOSE_AMP_VAL(0x21, 3, 0, HDA_OUTPUT),
4220         },
4221         HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
4222         HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
4223         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
4224         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
4225         HDA_CODEC_VOLUME("Mic Boost", 0x25, 0x0, HDA_OUTPUT),
4226         HDA_CODEC_VOLUME("Internal Mic Boost", 0x17, 0x0, HDA_INPUT),
4227         { } /* end */
4228 };
4229
4230 /* switch to external mic if plugged */
4231 static void ad1984a_touchsmart_automic(struct hda_codec *codec)
4232 {
4233         if (snd_hda_jack_detect(codec, 0x1c))
4234                 snd_hda_codec_write(codec, 0x0c, 0,
4235                                      AC_VERB_SET_CONNECT_SEL, 0x4);
4236         else
4237                 snd_hda_codec_write(codec, 0x0c, 0,
4238                                      AC_VERB_SET_CONNECT_SEL, 0x5);
4239 }
4240
4241
4242 /* unsolicited event for HP jack sensing */
4243 static void ad1984a_touchsmart_unsol_event(struct hda_codec *codec,
4244         unsigned int res)
4245 {
4246         switch (res >> 26) {
4247         case AD1884A_HP_EVENT:
4248                 ad1884a_hp_automute(codec);
4249                 break;
4250         case AD1884A_MIC_EVENT:
4251                 ad1984a_touchsmart_automic(codec);
4252                 break;
4253         }
4254 }
4255
4256 /* initialize jack-sensing, too */
4257 static int ad1984a_touchsmart_init(struct hda_codec *codec)
4258 {
4259         ad198x_init(codec);
4260         ad1884a_hp_automute(codec);
4261         ad1984a_touchsmart_automic(codec);
4262         return 0;
4263 }
4264
4265
4266 /*
4267  */
4268
4269 enum {
4270         AD1884A_DESKTOP,
4271         AD1884A_LAPTOP,
4272         AD1884A_MOBILE,
4273         AD1884A_THINKPAD,
4274         AD1984A_TOUCHSMART,
4275         AD1884A_MODELS
4276 };
4277
4278 static const char *ad1884a_models[AD1884A_MODELS] = {
4279         [AD1884A_DESKTOP]       = "desktop",
4280         [AD1884A_LAPTOP]        = "laptop",
4281         [AD1884A_MOBILE]        = "mobile",
4282         [AD1884A_THINKPAD]      = "thinkpad",
4283         [AD1984A_TOUCHSMART]    = "touchsmart",
4284 };
4285
4286 static struct snd_pci_quirk ad1884a_cfg_tbl[] = {
4287         SND_PCI_QUIRK(0x103c, 0x3030, "HP", AD1884A_MOBILE),
4288         SND_PCI_QUIRK(0x103c, 0x3037, "HP 2230s", AD1884A_LAPTOP),
4289         SND_PCI_QUIRK(0x103c, 0x3056, "HP", AD1884A_MOBILE),
4290         SND_PCI_QUIRK_MASK(0x103c, 0xfff0, 0x3070, "HP", AD1884A_MOBILE),
4291         SND_PCI_QUIRK_MASK(0x103c, 0xfff0, 0x30d0, "HP laptop", AD1884A_LAPTOP),
4292         SND_PCI_QUIRK_MASK(0x103c, 0xfff0, 0x30e0, "HP laptop", AD1884A_LAPTOP),
4293         SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3600, "HP laptop", AD1884A_LAPTOP),
4294         SND_PCI_QUIRK_MASK(0x103c, 0xfff0, 0x7010, "HP laptop", AD1884A_MOBILE),
4295         SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X300", AD1884A_THINKPAD),
4296         SND_PCI_QUIRK(0x103c, 0x2a82, "Touchsmart", AD1984A_TOUCHSMART),
4297         {}
4298 };
4299
4300 static int patch_ad1884a(struct hda_codec *codec)
4301 {
4302         struct ad198x_spec *spec;
4303         int err, board_config;
4304
4305         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4306         if (spec == NULL)
4307                 return -ENOMEM;
4308
4309         codec->spec = spec;
4310
4311         err = snd_hda_attach_beep_device(codec, 0x10);
4312         if (err < 0) {
4313                 ad198x_free(codec);
4314                 return err;
4315         }
4316         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
4317
4318         spec->multiout.max_channels = 2;
4319         spec->multiout.num_dacs = ARRAY_SIZE(ad1884a_dac_nids);
4320         spec->multiout.dac_nids = ad1884a_dac_nids;
4321         spec->multiout.dig_out_nid = AD1884A_SPDIF_OUT;
4322         spec->num_adc_nids = ARRAY_SIZE(ad1884a_adc_nids);
4323         spec->adc_nids = ad1884a_adc_nids;
4324         spec->capsrc_nids = ad1884a_capsrc_nids;
4325         spec->input_mux = &ad1884a_capture_source;
4326         spec->num_mixers = 1;
4327         spec->mixers[0] = ad1884a_base_mixers;
4328         spec->num_init_verbs = 1;
4329         spec->init_verbs[0] = ad1884a_init_verbs;
4330         spec->spdif_route = 0;
4331 #ifdef CONFIG_SND_HDA_POWER_SAVE
4332         spec->loopback.amplist = ad1884a_loopbacks;
4333 #endif
4334         codec->patch_ops = ad198x_patch_ops;
4335
4336         /* override some parameters */
4337         board_config = snd_hda_check_board_config(codec, AD1884A_MODELS,
4338                                                   ad1884a_models,
4339                                                   ad1884a_cfg_tbl);
4340         switch (board_config) {
4341         case AD1884A_LAPTOP:
4342                 spec->mixers[0] = ad1884a_laptop_mixers;
4343                 spec->init_verbs[spec->num_init_verbs++] = ad1884a_laptop_verbs;
4344                 spec->multiout.dig_out_nid = 0;
4345                 codec->patch_ops.unsol_event = ad1884a_laptop_unsol_event;
4346                 codec->patch_ops.init = ad1884a_laptop_init;
4347                 /* set the upper-limit for mixer amp to 0dB for avoiding the
4348                  * possible damage by overloading
4349                  */
4350                 snd_hda_override_amp_caps(codec, 0x20, HDA_INPUT,
4351                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
4352                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
4353                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
4354                                           (1 << AC_AMPCAP_MUTE_SHIFT));
4355                 break;
4356         case AD1884A_MOBILE:
4357                 spec->mixers[0] = ad1884a_mobile_mixers;
4358                 spec->init_verbs[0] = ad1884a_mobile_verbs;
4359                 spec->multiout.dig_out_nid = 0;
4360                 codec->patch_ops.unsol_event = ad1884a_hp_unsol_event;
4361                 codec->patch_ops.init = ad1884a_hp_init;
4362                 /* set the upper-limit for mixer amp to 0dB for avoiding the
4363                  * possible damage by overloading
4364                  */
4365                 snd_hda_override_amp_caps(codec, 0x20, HDA_INPUT,
4366                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
4367                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
4368                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
4369                                           (1 << AC_AMPCAP_MUTE_SHIFT));
4370                 break;
4371         case AD1884A_THINKPAD:
4372                 spec->mixers[0] = ad1984a_thinkpad_mixers;
4373                 spec->init_verbs[spec->num_init_verbs++] =
4374                         ad1984a_thinkpad_verbs;
4375                 spec->multiout.dig_out_nid = 0;
4376                 spec->input_mux = &ad1984a_thinkpad_capture_source;
4377                 codec->patch_ops.unsol_event = ad1984a_thinkpad_unsol_event;
4378                 codec->patch_ops.init = ad1984a_thinkpad_init;
4379                 break;
4380         case AD1984A_TOUCHSMART:
4381                 spec->mixers[0] = ad1984a_touchsmart_mixers;
4382                 spec->init_verbs[0] = ad1984a_touchsmart_verbs;
4383                 spec->multiout.dig_out_nid = 0;
4384                 codec->patch_ops.unsol_event = ad1984a_touchsmart_unsol_event;
4385                 codec->patch_ops.init = ad1984a_touchsmart_init;
4386                 /* set the upper-limit for mixer amp to 0dB for avoiding the
4387                  * possible damage by overloading
4388                  */
4389                 snd_hda_override_amp_caps(codec, 0x20, HDA_INPUT,
4390                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
4391                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
4392                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
4393                                           (1 << AC_AMPCAP_MUTE_SHIFT));
4394                 break;
4395         }
4396
4397         codec->no_trigger_sense = 1;
4398
4399         return 0;
4400 }
4401
4402
4403 /*
4404  * AD1882 / AD1882A
4405  *
4406  * port-A - front hp-out
4407  * port-B - front mic-in
4408  * port-C - rear line-in, shared surr-out (3stack)
4409  * port-D - rear line-out
4410  * port-E - rear mic-in, shared clfe-out (3stack)
4411  * port-F - rear surr-out (6stack)
4412  * port-G - rear clfe-out (6stack)
4413  */
4414
4415 static hda_nid_t ad1882_dac_nids[3] = {
4416         0x04, 0x03, 0x05
4417 };
4418
4419 static hda_nid_t ad1882_adc_nids[2] = {
4420         0x08, 0x09,
4421 };
4422
4423 static hda_nid_t ad1882_capsrc_nids[2] = {
4424         0x0c, 0x0d,
4425 };
4426
4427 #define AD1882_SPDIF_OUT        0x02
4428
4429 /* list: 0x11, 0x39, 0x3a, 0x18, 0x3c, 0x3b, 0x12, 0x20 */
4430 static struct hda_input_mux ad1882_capture_source = {
4431         .num_items = 5,
4432         .items = {
4433                 { "Front Mic", 0x1 },
4434                 { "Mic", 0x4 },
4435                 { "Line", 0x2 },
4436                 { "CD", 0x3 },
4437                 { "Mix", 0x7 },
4438         },
4439 };
4440
4441 /* list: 0x11, 0x39, 0x3a, 0x3c, 0x18, 0x1f, 0x12, 0x20 */
4442 static struct hda_input_mux ad1882a_capture_source = {
4443         .num_items = 5,
4444         .items = {
4445                 { "Front Mic", 0x1 },
4446                 { "Mic", 0x4},
4447                 { "Line", 0x2 },
4448                 { "Digital Mic", 0x06 },
4449                 { "Mix", 0x7 },
4450         },
4451 };
4452
4453 static struct snd_kcontrol_new ad1882_base_mixers[] = {
4454         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
4455         HDA_CODEC_VOLUME("Surround Playback Volume", 0x03, 0x0, HDA_OUTPUT),
4456         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
4457         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
4458         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
4459         HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
4460         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
4461         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
4462
4463         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
4464         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
4465         HDA_CODEC_VOLUME("Line-In Boost", 0x3a, 0x0, HDA_OUTPUT),
4466         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
4467         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
4468         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
4469         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
4470         {
4471                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4472                 /* The multiple "Capture Source" controls confuse alsamixer
4473                  * So call somewhat different..
4474                  */
4475                 /* .name = "Capture Source", */
4476                 .name = "Input Source",
4477                 .count = 2,
4478                 .info = ad198x_mux_enum_info,
4479                 .get = ad198x_mux_enum_get,
4480                 .put = ad198x_mux_enum_put,
4481         },
4482         /* SPDIF controls */
4483         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
4484         {
4485                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4486                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
4487                 /* identical with ad1983 */
4488                 .info = ad1983_spdif_route_info,
4489                 .get = ad1983_spdif_route_get,
4490                 .put = ad1983_spdif_route_put,
4491         },
4492         { } /* end */
4493 };
4494
4495 static struct snd_kcontrol_new ad1882_loopback_mixers[] = {
4496         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
4497         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
4498         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
4499         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
4500         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x04, HDA_INPUT),
4501         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x04, HDA_INPUT),
4502         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x06, HDA_INPUT),
4503         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x06, HDA_INPUT),
4504         { } /* end */
4505 };
4506
4507 static struct snd_kcontrol_new ad1882a_loopback_mixers[] = {
4508         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
4509         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
4510         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
4511         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
4512         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x01, HDA_INPUT),
4513         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x01, HDA_INPUT),
4514         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x06, HDA_INPUT),
4515         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x06, HDA_INPUT),
4516         HDA_CODEC_VOLUME("Digital Mic Boost", 0x1f, 0x0, HDA_INPUT),
4517         { } /* end */
4518 };
4519
4520 static struct snd_kcontrol_new ad1882_3stack_mixers[] = {
4521         HDA_CODEC_MUTE("Surround Playback Switch", 0x15, 0x0, HDA_OUTPUT),
4522         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x17, 1, 0x0, HDA_OUTPUT),
4523         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x17, 2, 0x0, HDA_OUTPUT),
4524         {
4525                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4526                 .name = "Channel Mode",
4527                 .info = ad198x_ch_mode_info,
4528                 .get = ad198x_ch_mode_get,
4529                 .put = ad198x_ch_mode_put,
4530         },
4531         { } /* end */
4532 };
4533
4534 static struct snd_kcontrol_new ad1882_6stack_mixers[] = {
4535         HDA_CODEC_MUTE("Surround Playback Switch", 0x16, 0x0, HDA_OUTPUT),
4536         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x24, 1, 0x0, HDA_OUTPUT),
4537         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x24, 2, 0x0, HDA_OUTPUT),
4538         { } /* end */
4539 };
4540
4541 static struct hda_verb ad1882_ch2_init[] = {
4542         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
4543         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4544         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4545         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4546         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4547         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4548         { } /* end */
4549 };
4550
4551 static struct hda_verb ad1882_ch4_init[] = {
4552         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4553         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4554         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4555         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4556         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4557         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4558         { } /* end */
4559 };
4560
4561 static struct hda_verb ad1882_ch6_init[] = {
4562         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4563         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4564         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4565         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4566         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4567         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4568         { } /* end */
4569 };
4570
4571 static struct hda_channel_mode ad1882_modes[3] = {
4572         { 2, ad1882_ch2_init },
4573         { 4, ad1882_ch4_init },
4574         { 6, ad1882_ch6_init },
4575 };
4576
4577 /*
4578  * initialization verbs
4579  */
4580 static struct hda_verb ad1882_init_verbs[] = {
4581         /* DACs; mute as default */
4582         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
4583         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
4584         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
4585         /* Port-A (HP) mixer */
4586         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4587         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4588         /* Port-A pin */
4589         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
4590         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4591         /* HP selector - select DAC2 */
4592         {0x37, AC_VERB_SET_CONNECT_SEL, 0x1},
4593         /* Port-D (Line-out) mixer */
4594         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4595         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4596         /* Port-D pin */
4597         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
4598         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4599         /* Mono-out mixer */
4600         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4601         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4602         /* Mono-out pin */
4603         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
4604         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4605         /* Port-B (front mic) pin */
4606         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4607         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4608         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
4609         /* Port-C (line-in) pin */
4610         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
4611         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4612         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
4613         /* Port-C mixer - mute as input */
4614         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4615         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4616         /* Port-E (mic-in) pin */
4617         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4618         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4619         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
4620         /* Port-E mixer - mute as input */
4621         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4622         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4623         /* Port-F (surround) */
4624         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4625         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4626         /* Port-G (CLFE) */
4627         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4628         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4629         /* Analog mixer; mute as default */
4630         /* list: 0x39, 0x3a, 0x11, 0x12, 0x3c, 0x3b, 0x18, 0x1a */
4631         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4632         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4633         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4634         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4635         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4636         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
4637         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
4638         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
4639         /* Analog Mix output amp */
4640         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
4641         /* SPDIF output selector */
4642         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
4643         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
4644         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
4645         { } /* end */
4646 };
4647
4648 #ifdef CONFIG_SND_HDA_POWER_SAVE
4649 static struct hda_amp_list ad1882_loopbacks[] = {
4650         { 0x20, HDA_INPUT, 0 }, /* Front Mic */
4651         { 0x20, HDA_INPUT, 1 }, /* Mic */
4652         { 0x20, HDA_INPUT, 4 }, /* Line */
4653         { 0x20, HDA_INPUT, 6 }, /* CD */
4654         { } /* end */
4655 };
4656 #endif
4657
4658 /* models */
4659 enum {
4660         AD1882_3STACK,
4661         AD1882_6STACK,
4662         AD1882_MODELS
4663 };
4664
4665 static const char *ad1882_models[AD1986A_MODELS] = {
4666         [AD1882_3STACK]         = "3stack",
4667         [AD1882_6STACK]         = "6stack",
4668 };
4669
4670
4671 static int patch_ad1882(struct hda_codec *codec)
4672 {
4673         struct ad198x_spec *spec;
4674         int err, board_config;
4675
4676         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4677         if (spec == NULL)
4678                 return -ENOMEM;
4679
4680         codec->spec = spec;
4681
4682         err = snd_hda_attach_beep_device(codec, 0x10);
4683         if (err < 0) {
4684                 ad198x_free(codec);
4685                 return err;
4686         }
4687         set_beep_amp(spec, 0x10, 0, HDA_OUTPUT);
4688
4689         spec->multiout.max_channels = 6;
4690         spec->multiout.num_dacs = 3;
4691         spec->multiout.dac_nids = ad1882_dac_nids;
4692         spec->multiout.dig_out_nid = AD1882_SPDIF_OUT;
4693         spec->num_adc_nids = ARRAY_SIZE(ad1882_adc_nids);
4694         spec->adc_nids = ad1882_adc_nids;
4695         spec->capsrc_nids = ad1882_capsrc_nids;
4696         if (codec->vendor_id == 0x11d41882)
4697                 spec->input_mux = &ad1882_capture_source;
4698         else
4699                 spec->input_mux = &ad1882a_capture_source;
4700         spec->num_mixers = 2;
4701         spec->mixers[0] = ad1882_base_mixers;
4702         if (codec->vendor_id == 0x11d41882)
4703                 spec->mixers[1] = ad1882_loopback_mixers;
4704         else
4705                 spec->mixers[1] = ad1882a_loopback_mixers;
4706         spec->num_init_verbs = 1;
4707         spec->init_verbs[0] = ad1882_init_verbs;
4708         spec->spdif_route = 0;
4709 #ifdef CONFIG_SND_HDA_POWER_SAVE
4710         spec->loopback.amplist = ad1882_loopbacks;
4711 #endif
4712         spec->vmaster_nid = 0x04;
4713
4714         codec->patch_ops = ad198x_patch_ops;
4715
4716         /* override some parameters */
4717         board_config = snd_hda_check_board_config(codec, AD1882_MODELS,
4718                                                   ad1882_models, NULL);
4719         switch (board_config) {
4720         default:
4721         case AD1882_3STACK:
4722                 spec->num_mixers = 3;
4723                 spec->mixers[2] = ad1882_3stack_mixers;
4724                 spec->channel_mode = ad1882_modes;
4725                 spec->num_channel_mode = ARRAY_SIZE(ad1882_modes);
4726                 spec->need_dac_fix = 1;
4727                 spec->multiout.max_channels = 2;
4728                 spec->multiout.num_dacs = 1;
4729                 break;
4730         case AD1882_6STACK:
4731                 spec->num_mixers = 3;
4732                 spec->mixers[2] = ad1882_6stack_mixers;
4733                 break;
4734         }
4735
4736         codec->no_trigger_sense = 1;
4737
4738         return 0;
4739 }
4740
4741
4742 /*
4743  * patch entries
4744  */
4745 static struct hda_codec_preset snd_hda_preset_analog[] = {
4746         { .id = 0x11d4184a, .name = "AD1884A", .patch = patch_ad1884a },
4747         { .id = 0x11d41882, .name = "AD1882", .patch = patch_ad1882 },
4748         { .id = 0x11d41883, .name = "AD1883", .patch = patch_ad1884a },
4749         { .id = 0x11d41884, .name = "AD1884", .patch = patch_ad1884 },
4750         { .id = 0x11d4194a, .name = "AD1984A", .patch = patch_ad1884a },
4751         { .id = 0x11d4194b, .name = "AD1984B", .patch = patch_ad1884a },
4752         { .id = 0x11d41981, .name = "AD1981", .patch = patch_ad1981 },
4753         { .id = 0x11d41983, .name = "AD1983", .patch = patch_ad1983 },
4754         { .id = 0x11d41984, .name = "AD1984", .patch = patch_ad1984 },
4755         { .id = 0x11d41986, .name = "AD1986A", .patch = patch_ad1986a },
4756         { .id = 0x11d41988, .name = "AD1988", .patch = patch_ad1988 },
4757         { .id = 0x11d4198b, .name = "AD1988B", .patch = patch_ad1988 },
4758         { .id = 0x11d4882a, .name = "AD1882A", .patch = patch_ad1882 },
4759         { .id = 0x11d4989a, .name = "AD1989A", .patch = patch_ad1988 },
4760         { .id = 0x11d4989b, .name = "AD1989B", .patch = patch_ad1988 },
4761         {} /* terminator */
4762 };
4763
4764 MODULE_ALIAS("snd-hda-codec-id:11d4*");
4765
4766 MODULE_LICENSE("GPL");
4767 MODULE_DESCRIPTION("Analog Devices HD-audio codec");
4768
4769 static struct hda_codec_preset_list analog_list = {
4770         .preset = snd_hda_preset_analog,
4771         .owner = THIS_MODULE,
4772 };
4773
4774 static int __init patch_analog_init(void)
4775 {
4776         return snd_hda_add_codec_preset(&analog_list);
4777 }
4778
4779 static void __exit patch_analog_exit(void)
4780 {
4781         snd_hda_delete_codec_preset(&analog_list);
4782 }
4783
4784 module_init(patch_analog_init)
4785 module_exit(patch_analog_exit)