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