[ALSA] hda-codec - optimize resume using caches
[linux-2.6.git] / sound / pci / hda / patch_analog.c
1 /*
2  * HD audio interface patch for AD1882, AD1884, AD1981HD, AD1983, AD1984,
3  *   AD1986A, AD1988
4  *
5  * Copyright (c) 2005-2007 Takashi Iwai <tiwai@suse.de>
6  *
7  *  This driver is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This driver is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/mutex.h>
28
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include "hda_local.h"
32
33 struct ad198x_spec {
34         struct snd_kcontrol_new *mixers[5];
35         int num_mixers;
36
37         const struct hda_verb *init_verbs[5];   /* initialization verbs
38                                                  * don't forget NULL termination!
39                                                  */
40         unsigned int num_init_verbs;
41
42         /* playback */
43         struct hda_multi_out multiout;  /* playback set-up
44                                          * max_channels, dacs must be set
45                                          * dig_out_nid and hp_nid are optional
46                                          */
47         unsigned int cur_eapd;
48         unsigned int need_dac_fix;
49
50         /* capture */
51         unsigned int num_adc_nids;
52         hda_nid_t *adc_nids;
53         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
54
55         /* capture source */
56         const struct hda_input_mux *input_mux;
57         hda_nid_t *capsrc_nids;
58         unsigned int cur_mux[3];
59
60         /* channel model */
61         const struct hda_channel_mode *channel_mode;
62         int num_channel_mode;
63
64         /* PCM information */
65         struct hda_pcm pcm_rec[3];      /* used in alc_build_pcms() */
66
67         struct mutex amp_mutex; /* PCM volume/mute control mutex */
68         unsigned int spdif_route;
69
70         /* dynamic controls, init_verbs and input_mux */
71         struct auto_pin_cfg autocfg;
72         unsigned int num_kctl_alloc, num_kctl_used;
73         struct snd_kcontrol_new *kctl_alloc;
74         struct hda_input_mux private_imux;
75         hda_nid_t private_dac_nids[4];
76 };
77
78 /*
79  * input MUX handling (common part)
80  */
81 static int ad198x_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
82 {
83         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
84         struct ad198x_spec *spec = codec->spec;
85
86         return snd_hda_input_mux_info(spec->input_mux, uinfo);
87 }
88
89 static int ad198x_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
90 {
91         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
92         struct ad198x_spec *spec = codec->spec;
93         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
94
95         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
96         return 0;
97 }
98
99 static int ad198x_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
100 {
101         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
102         struct ad198x_spec *spec = codec->spec;
103         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
104
105         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
106                                      spec->capsrc_nids[adc_idx],
107                                      &spec->cur_mux[adc_idx]);
108 }
109
110 /*
111  * initialization (common callbacks)
112  */
113 static int ad198x_init(struct hda_codec *codec)
114 {
115         struct ad198x_spec *spec = codec->spec;
116         int i;
117
118         for (i = 0; i < spec->num_init_verbs; i++)
119                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
120         return 0;
121 }
122
123 static int ad198x_build_controls(struct hda_codec *codec)
124 {
125         struct ad198x_spec *spec = codec->spec;
126         unsigned int i;
127         int err;
128
129         for (i = 0; i < spec->num_mixers; i++) {
130                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
131                 if (err < 0)
132                         return err;
133         }
134         if (spec->multiout.dig_out_nid) {
135                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
136                 if (err < 0)
137                         return err;
138         } 
139         if (spec->dig_in_nid) {
140                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
141                 if (err < 0)
142                         return err;
143         }
144         return 0;
145 }
146
147 /*
148  * Analog playback callbacks
149  */
150 static int ad198x_playback_pcm_open(struct hda_pcm_stream *hinfo,
151                                     struct hda_codec *codec,
152                                     struct snd_pcm_substream *substream)
153 {
154         struct ad198x_spec *spec = codec->spec;
155         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
156 }
157
158 static int ad198x_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
159                                        struct hda_codec *codec,
160                                        unsigned int stream_tag,
161                                        unsigned int format,
162                                        struct snd_pcm_substream *substream)
163 {
164         struct ad198x_spec *spec = codec->spec;
165         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
166                                                 format, substream);
167 }
168
169 static int ad198x_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
170                                        struct hda_codec *codec,
171                                        struct snd_pcm_substream *substream)
172 {
173         struct ad198x_spec *spec = codec->spec;
174         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
175 }
176
177 /*
178  * Digital out
179  */
180 static int ad198x_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
181                                         struct hda_codec *codec,
182                                         struct snd_pcm_substream *substream)
183 {
184         struct ad198x_spec *spec = codec->spec;
185         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
186 }
187
188 static int ad198x_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
189                                          struct hda_codec *codec,
190                                          struct snd_pcm_substream *substream)
191 {
192         struct ad198x_spec *spec = codec->spec;
193         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
194 }
195
196 static int ad198x_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
197                                            struct hda_codec *codec,
198                                            unsigned int stream_tag,
199                                            unsigned int format,
200                                            struct snd_pcm_substream *substream)
201 {
202         struct ad198x_spec *spec = codec->spec;
203         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
204                                              format, substream);
205 }
206
207 /*
208  * Analog capture
209  */
210 static int ad198x_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
211                                       struct hda_codec *codec,
212                                       unsigned int stream_tag,
213                                       unsigned int format,
214                                       struct snd_pcm_substream *substream)
215 {
216         struct ad198x_spec *spec = codec->spec;
217         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
218                                    stream_tag, 0, format);
219         return 0;
220 }
221
222 static int ad198x_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
223                                       struct hda_codec *codec,
224                                       struct snd_pcm_substream *substream)
225 {
226         struct ad198x_spec *spec = codec->spec;
227         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
228                                    0, 0, 0);
229         return 0;
230 }
231
232
233 /*
234  */
235 static struct hda_pcm_stream ad198x_pcm_analog_playback = {
236         .substreams = 1,
237         .channels_min = 2,
238         .channels_max = 6, /* changed later */
239         .nid = 0, /* fill later */
240         .ops = {
241                 .open = ad198x_playback_pcm_open,
242                 .prepare = ad198x_playback_pcm_prepare,
243                 .cleanup = ad198x_playback_pcm_cleanup
244         },
245 };
246
247 static struct hda_pcm_stream ad198x_pcm_analog_capture = {
248         .substreams = 1,
249         .channels_min = 2,
250         .channels_max = 2,
251         .nid = 0, /* fill later */
252         .ops = {
253                 .prepare = ad198x_capture_pcm_prepare,
254                 .cleanup = ad198x_capture_pcm_cleanup
255         },
256 };
257
258 static struct hda_pcm_stream ad198x_pcm_digital_playback = {
259         .substreams = 1,
260         .channels_min = 2,
261         .channels_max = 2,
262         .nid = 0, /* fill later */
263         .ops = {
264                 .open = ad198x_dig_playback_pcm_open,
265                 .close = ad198x_dig_playback_pcm_close,
266                 .prepare = ad198x_dig_playback_pcm_prepare
267         },
268 };
269
270 static struct hda_pcm_stream ad198x_pcm_digital_capture = {
271         .substreams = 1,
272         .channels_min = 2,
273         .channels_max = 2,
274         /* NID is set in alc_build_pcms */
275 };
276
277 static int ad198x_build_pcms(struct hda_codec *codec)
278 {
279         struct ad198x_spec *spec = codec->spec;
280         struct hda_pcm *info = spec->pcm_rec;
281
282         codec->num_pcms = 1;
283         codec->pcm_info = info;
284
285         info->name = "AD198x Analog";
286         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ad198x_pcm_analog_playback;
287         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->multiout.max_channels;
288         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
289         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad198x_pcm_analog_capture;
290         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adc_nids;
291         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
292
293         if (spec->multiout.dig_out_nid) {
294                 info++;
295                 codec->num_pcms++;
296                 info->name = "AD198x Digital";
297                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ad198x_pcm_digital_playback;
298                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
299                 if (spec->dig_in_nid) {
300                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad198x_pcm_digital_capture;
301                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
302                 }
303         }
304
305         return 0;
306 }
307
308 static void ad198x_free(struct hda_codec *codec)
309 {
310         struct ad198x_spec *spec = codec->spec;
311         unsigned int i;
312
313         if (spec->kctl_alloc) {
314                 for (i = 0; i < spec->num_kctl_used; i++)
315                         kfree(spec->kctl_alloc[i].name);
316                 kfree(spec->kctl_alloc);
317         }
318         kfree(codec->spec);
319 }
320
321 static struct hda_codec_ops ad198x_patch_ops = {
322         .build_controls = ad198x_build_controls,
323         .build_pcms = ad198x_build_pcms,
324         .init = ad198x_init,
325         .free = ad198x_free,
326 };
327
328
329 /*
330  * EAPD control
331  * the private value = nid | (invert << 8)
332  */
333 #define ad198x_eapd_info        snd_ctl_boolean_mono_info
334
335 static int ad198x_eapd_get(struct snd_kcontrol *kcontrol,
336                            struct snd_ctl_elem_value *ucontrol)
337 {
338         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
339         struct ad198x_spec *spec = codec->spec;
340         int invert = (kcontrol->private_value >> 8) & 1;
341         if (invert)
342                 ucontrol->value.integer.value[0] = ! spec->cur_eapd;
343         else
344                 ucontrol->value.integer.value[0] = spec->cur_eapd;
345         return 0;
346 }
347
348 static int ad198x_eapd_put(struct snd_kcontrol *kcontrol,
349                            struct snd_ctl_elem_value *ucontrol)
350 {
351         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
352         struct ad198x_spec *spec = codec->spec;
353         int invert = (kcontrol->private_value >> 8) & 1;
354         hda_nid_t nid = kcontrol->private_value & 0xff;
355         unsigned int eapd;
356         eapd = ucontrol->value.integer.value[0];
357         if (invert)
358                 eapd = !eapd;
359         if (eapd == spec->cur_eapd)
360                 return 0;
361         spec->cur_eapd = eapd;
362         snd_hda_codec_write_cache(codec, nid,
363                                   0, AC_VERB_SET_EAPD_BTLENABLE,
364                                   eapd ? 0x02 : 0x00);
365         return 1;
366 }
367
368 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
369                                struct snd_ctl_elem_info *uinfo);
370 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
371                               struct snd_ctl_elem_value *ucontrol);
372 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
373                               struct snd_ctl_elem_value *ucontrol);
374
375
376 /*
377  * AD1986A specific
378  */
379
380 #define AD1986A_SPDIF_OUT       0x02
381 #define AD1986A_FRONT_DAC       0x03
382 #define AD1986A_SURR_DAC        0x04
383 #define AD1986A_CLFE_DAC        0x05
384 #define AD1986A_ADC             0x06
385
386 static hda_nid_t ad1986a_dac_nids[3] = {
387         AD1986A_FRONT_DAC, AD1986A_SURR_DAC, AD1986A_CLFE_DAC
388 };
389 static hda_nid_t ad1986a_adc_nids[1] = { AD1986A_ADC };
390 static hda_nid_t ad1986a_capsrc_nids[1] = { 0x12 };
391
392 static struct hda_input_mux ad1986a_capture_source = {
393         .num_items = 7,
394         .items = {
395                 { "Mic", 0x0 },
396                 { "CD", 0x1 },
397                 { "Aux", 0x3 },
398                 { "Line", 0x4 },
399                 { "Mix", 0x5 },
400                 { "Mono", 0x6 },
401                 { "Phone", 0x7 },
402         },
403 };
404
405
406 static struct hda_bind_ctls ad1986a_bind_pcm_vol = {
407         .ops = &snd_hda_bind_vol,
408         .values = {
409                 HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT),
410                 HDA_COMPOSE_AMP_VAL(AD1986A_SURR_DAC, 3, 0, HDA_OUTPUT),
411                 HDA_COMPOSE_AMP_VAL(AD1986A_CLFE_DAC, 3, 0, HDA_OUTPUT),
412                 0
413         },
414 };
415
416 static struct hda_bind_ctls ad1986a_bind_pcm_sw = {
417         .ops = &snd_hda_bind_sw,
418         .values = {
419                 HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT),
420                 HDA_COMPOSE_AMP_VAL(AD1986A_SURR_DAC, 3, 0, HDA_OUTPUT),
421                 HDA_COMPOSE_AMP_VAL(AD1986A_CLFE_DAC, 3, 0, HDA_OUTPUT),
422                 0
423         },
424 };
425
426 /*
427  * mixers
428  */
429 static struct snd_kcontrol_new ad1986a_mixers[] = {
430         /*
431          * bind volumes/mutes of 3 DACs as a single PCM control for simplicity
432          */
433         HDA_BIND_VOL("PCM Playback Volume", &ad1986a_bind_pcm_vol),
434         HDA_BIND_SW("PCM Playback Switch", &ad1986a_bind_pcm_sw),
435         HDA_CODEC_VOLUME("Front Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
436         HDA_CODEC_MUTE("Front Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
437         HDA_CODEC_VOLUME("Surround Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
438         HDA_CODEC_MUTE("Surround Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
439         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x1d, 1, 0x0, HDA_OUTPUT),
440         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x1d, 2, 0x0, HDA_OUTPUT),
441         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x1d, 1, 0x0, HDA_OUTPUT),
442         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x1d, 2, 0x0, HDA_OUTPUT),
443         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x1a, 0x0, HDA_OUTPUT),
444         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1a, 0x0, HDA_OUTPUT),
445         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
446         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
447         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
448         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
449         HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
450         HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
451         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
452         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
453         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
454         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x18, 0x0, HDA_OUTPUT),
455         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x18, 0x0, HDA_OUTPUT),
456         HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
457         HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT),
458         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
459         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
460         {
461                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
462                 .name = "Capture Source",
463                 .info = ad198x_mux_enum_info,
464                 .get = ad198x_mux_enum_get,
465                 .put = ad198x_mux_enum_put,
466         },
467         HDA_CODEC_MUTE("Stereo Downmix Switch", 0x09, 0x0, HDA_OUTPUT),
468         { } /* end */
469 };
470
471 /* additional mixers for 3stack mode */
472 static struct snd_kcontrol_new ad1986a_3st_mixers[] = {
473         {
474                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
475                 .name = "Channel Mode",
476                 .info = ad198x_ch_mode_info,
477                 .get = ad198x_ch_mode_get,
478                 .put = ad198x_ch_mode_put,
479         },
480         { } /* end */
481 };
482
483 /* laptop model - 2ch only */
484 static hda_nid_t ad1986a_laptop_dac_nids[1] = { AD1986A_FRONT_DAC };
485
486 static struct snd_kcontrol_new ad1986a_laptop_mixers[] = {
487         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
488         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
489         HDA_CODEC_VOLUME("Master Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
490         HDA_CODEC_MUTE("Master Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
491         /* HDA_CODEC_VOLUME("Headphone Playback Volume", 0x1a, 0x0, HDA_OUTPUT),
492            HDA_CODEC_MUTE("Headphone Playback Switch", 0x1a, 0x0, HDA_OUTPUT), */
493         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
494         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
495         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
496         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
497         HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
498         HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
499         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
500         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
501         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
502         /* HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x18, 0x0, HDA_OUTPUT),
503            HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x18, 0x0, HDA_OUTPUT),
504            HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
505            HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT), */
506         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
507         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
508         {
509                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
510                 .name = "Capture Source",
511                 .info = ad198x_mux_enum_info,
512                 .get = ad198x_mux_enum_get,
513                 .put = ad198x_mux_enum_put,
514         },
515         { } /* end */
516 };
517
518 /* laptop-eapd model - 2ch only */
519
520 /* master controls both pins 0x1a and 0x1b */
521 static struct hda_bind_ctls ad1986a_laptop_master_vol = {
522         .ops = &snd_hda_bind_vol,
523         .values = {
524                 HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
525                 HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
526                 0,
527         },
528 };
529
530 static struct hda_bind_ctls ad1986a_laptop_master_sw = {
531         .ops = &snd_hda_bind_sw,
532         .values = {
533                 HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
534                 HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
535                 0,
536         },
537 };
538
539 static struct hda_input_mux ad1986a_laptop_eapd_capture_source = {
540         .num_items = 3,
541         .items = {
542                 { "Mic", 0x0 },
543                 { "Internal Mic", 0x4 },
544                 { "Mix", 0x5 },
545         },
546 };
547
548 static struct snd_kcontrol_new ad1986a_laptop_eapd_mixers[] = {
549         HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
550         HDA_BIND_SW("Master Playback Switch", &ad1986a_laptop_master_sw),
551         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
552         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
553         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0x0, HDA_OUTPUT),
554         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0x0, HDA_OUTPUT),
555         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
556         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
557         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
558         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
559         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
560         {
561                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
562                 .name = "Capture Source",
563                 .info = ad198x_mux_enum_info,
564                 .get = ad198x_mux_enum_get,
565                 .put = ad198x_mux_enum_put,
566         },
567         {
568                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
569                 .name = "External Amplifier",
570                 .info = ad198x_eapd_info,
571                 .get = ad198x_eapd_get,
572                 .put = ad198x_eapd_put,
573                 .private_value = 0x1b | (1 << 8), /* port-D, inversed */
574         },
575         { } /* end */
576 };
577
578 /*
579  * initialization verbs
580  */
581 static struct hda_verb ad1986a_init_verbs[] = {
582         /* Front, Surround, CLFE DAC; mute as default */
583         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
584         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
585         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
586         /* Downmix - off */
587         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
588         /* HP, Line-Out, Surround, CLFE selectors */
589         {0x0a, AC_VERB_SET_CONNECT_SEL, 0x0},
590         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x0},
591         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
592         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
593         /* Mono selector */
594         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x0},
595         /* Mic selector: Mic 1/2 pin */
596         {0x0f, AC_VERB_SET_CONNECT_SEL, 0x0},
597         /* Line-in selector: Line-in */
598         {0x10, AC_VERB_SET_CONNECT_SEL, 0x0},
599         /* Mic 1/2 swap */
600         {0x11, AC_VERB_SET_CONNECT_SEL, 0x0},
601         /* Record selector: mic */
602         {0x12, AC_VERB_SET_CONNECT_SEL, 0x0},
603         /* Mic, Phone, CD, Aux, Line-In amp; mute as default */
604         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
605         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
606         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
607         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
608         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
609         /* PC beep */
610         {0x18, AC_VERB_SET_CONNECT_SEL, 0x0},
611         /* HP, Line-Out, Surround, CLFE, Mono pins; mute as default */
612         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
613         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
614         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
615         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
616         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
617         /* HP Pin */
618         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
619         /* Front, Surround, CLFE Pins */
620         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
621         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
622         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
623         /* Mono Pin */
624         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
625         /* Mic Pin */
626         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
627         /* Line, Aux, CD, Beep-In Pin */
628         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
629         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
630         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
631         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
632         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
633         { } /* end */
634 };
635
636 static struct hda_verb ad1986a_ch2_init[] = {
637         /* Surround out -> Line In */
638         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
639         /* Line-in selectors */
640         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x1 },
641         /* CLFE -> Mic in */
642         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
643         /* Mic selector, mix C/LFE (backmic) and Mic (frontmic) */
644         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x4 },
645         { } /* end */
646 };
647
648 static struct hda_verb ad1986a_ch4_init[] = {
649         /* Surround out -> Surround */
650         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
651         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
652         /* CLFE -> Mic in */
653         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
654         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x4 },
655         { } /* end */
656 };
657
658 static struct hda_verb ad1986a_ch6_init[] = {
659         /* Surround out -> Surround out */
660         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
661         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
662         /* CLFE -> CLFE */
663         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
664         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x0 },
665         { } /* end */
666 };
667
668 static struct hda_channel_mode ad1986a_modes[3] = {
669         { 2, ad1986a_ch2_init },
670         { 4, ad1986a_ch4_init },
671         { 6, ad1986a_ch6_init },
672 };
673
674 /* eapd initialization */
675 static struct hda_verb ad1986a_eapd_init_verbs[] = {
676         {0x1b, AC_VERB_SET_EAPD_BTLENABLE, 0x00 },
677         {}
678 };
679
680 /* Ultra initialization */
681 static struct hda_verb ad1986a_ultra_init[] = {
682         /* eapd initialization */
683         { 0x1b, AC_VERB_SET_EAPD_BTLENABLE, 0x00 },
684         /* CLFE -> Mic in */
685         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x2 },
686         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
687         { 0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080 },
688         { } /* end */
689 };
690
691 /* models */
692 enum {
693         AD1986A_6STACK,
694         AD1986A_3STACK,
695         AD1986A_LAPTOP,
696         AD1986A_LAPTOP_EAPD,
697         AD1986A_ULTRA,
698         AD1986A_MODELS
699 };
700
701 static const char *ad1986a_models[AD1986A_MODELS] = {
702         [AD1986A_6STACK]        = "6stack",
703         [AD1986A_3STACK]        = "3stack",
704         [AD1986A_LAPTOP]        = "laptop",
705         [AD1986A_LAPTOP_EAPD]   = "laptop-eapd",
706         [AD1986A_ULTRA]         = "ultra",
707 };
708
709 static struct snd_pci_quirk ad1986a_cfg_tbl[] = {
710         SND_PCI_QUIRK(0x103c, 0x30af, "HP B2800", AD1986A_LAPTOP_EAPD),
711         SND_PCI_QUIRK(0x10de, 0xcb84, "ASUS A8N-VM", AD1986A_3STACK),
712         SND_PCI_QUIRK(0x1043, 0x1153, "ASUS M9", AD1986A_LAPTOP_EAPD),
713         SND_PCI_QUIRK(0x1043, 0x1213, "ASUS A6J", AD1986A_LAPTOP_EAPD),
714         SND_PCI_QUIRK(0x1043, 0x11f7, "ASUS U5A", AD1986A_LAPTOP_EAPD),
715         SND_PCI_QUIRK(0x1043, 0x1263, "ASUS U5F", AD1986A_LAPTOP_EAPD),
716         SND_PCI_QUIRK(0x1043, 0x1297, "ASUS Z62F", AD1986A_LAPTOP_EAPD),
717         SND_PCI_QUIRK(0x1043, 0x12b3, "ASUS V1j", AD1986A_LAPTOP_EAPD),
718         SND_PCI_QUIRK(0x1043, 0x1302, "ASUS W3j", AD1986A_LAPTOP_EAPD),
719         SND_PCI_QUIRK(0x1043, 0x1447, "ASUS A8J", AD1986A_3STACK),
720         SND_PCI_QUIRK(0x1043, 0x817f, "ASUS P5", AD1986A_3STACK),
721         SND_PCI_QUIRK(0x1043, 0x818f, "ASUS P5", AD1986A_LAPTOP),
722         SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS P5", AD1986A_3STACK),
723         SND_PCI_QUIRK(0x1043, 0x81cb, "ASUS M2N", AD1986A_3STACK),
724         SND_PCI_QUIRK(0x1043, 0x8234, "ASUS M2N", AD1986A_3STACK),
725         SND_PCI_QUIRK(0x144d, 0xb03c, "Samsung R55", AD1986A_3STACK),
726         SND_PCI_QUIRK(0x144d, 0xc01e, "FSC V2060", AD1986A_LAPTOP),
727         SND_PCI_QUIRK(0x144d, 0xc023, "Samsung X60", AD1986A_LAPTOP_EAPD),
728         SND_PCI_QUIRK(0x144d, 0xc024, "Samsung R65", AD1986A_LAPTOP_EAPD),
729         SND_PCI_QUIRK(0x144d, 0xc026, "Samsung X11", AD1986A_LAPTOP_EAPD),
730         SND_PCI_QUIRK(0x144d, 0xc504, "Samsung Q35", AD1986A_3STACK),
731         SND_PCI_QUIRK(0x144d, 0xc027, "Samsung Q1", AD1986A_ULTRA),
732         SND_PCI_QUIRK(0x17aa, 0x1011, "Lenovo M55", AD1986A_LAPTOP),
733         SND_PCI_QUIRK(0x17aa, 0x1017, "Lenovo A60", AD1986A_3STACK),
734         SND_PCI_QUIRK(0x17aa, 0x2066, "Lenovo N100", AD1986A_LAPTOP_EAPD),
735         SND_PCI_QUIRK(0x17c0, 0x2017, "Samsung M50", AD1986A_LAPTOP),
736         {}
737 };
738
739 static int patch_ad1986a(struct hda_codec *codec)
740 {
741         struct ad198x_spec *spec;
742         int board_config;
743
744         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
745         if (spec == NULL)
746                 return -ENOMEM;
747
748         codec->spec = spec;
749
750         spec->multiout.max_channels = 6;
751         spec->multiout.num_dacs = ARRAY_SIZE(ad1986a_dac_nids);
752         spec->multiout.dac_nids = ad1986a_dac_nids;
753         spec->multiout.dig_out_nid = AD1986A_SPDIF_OUT;
754         spec->num_adc_nids = 1;
755         spec->adc_nids = ad1986a_adc_nids;
756         spec->capsrc_nids = ad1986a_capsrc_nids;
757         spec->input_mux = &ad1986a_capture_source;
758         spec->num_mixers = 1;
759         spec->mixers[0] = ad1986a_mixers;
760         spec->num_init_verbs = 1;
761         spec->init_verbs[0] = ad1986a_init_verbs;
762
763         codec->patch_ops = ad198x_patch_ops;
764
765         /* override some parameters */
766         board_config = snd_hda_check_board_config(codec, AD1986A_MODELS,
767                                                   ad1986a_models,
768                                                   ad1986a_cfg_tbl);
769         switch (board_config) {
770         case AD1986A_3STACK:
771                 spec->num_mixers = 2;
772                 spec->mixers[1] = ad1986a_3st_mixers;
773                 spec->num_init_verbs = 2;
774                 spec->init_verbs[1] = ad1986a_ch2_init;
775                 spec->channel_mode = ad1986a_modes;
776                 spec->num_channel_mode = ARRAY_SIZE(ad1986a_modes);
777                 spec->need_dac_fix = 1;
778                 spec->multiout.max_channels = 2;
779                 spec->multiout.num_dacs = 1;
780                 break;
781         case AD1986A_LAPTOP:
782                 spec->mixers[0] = ad1986a_laptop_mixers;
783                 spec->multiout.max_channels = 2;
784                 spec->multiout.num_dacs = 1;
785                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
786                 break;
787         case AD1986A_LAPTOP_EAPD:
788                 spec->mixers[0] = ad1986a_laptop_eapd_mixers;
789                 spec->num_init_verbs = 2;
790                 spec->init_verbs[1] = ad1986a_eapd_init_verbs;
791                 spec->multiout.max_channels = 2;
792                 spec->multiout.num_dacs = 1;
793                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
794                 spec->multiout.dig_out_nid = 0;
795                 spec->input_mux = &ad1986a_laptop_eapd_capture_source;
796                 break;
797         case AD1986A_ULTRA:
798                 spec->mixers[0] = ad1986a_laptop_eapd_mixers;
799                 spec->num_init_verbs = 2;
800                 spec->init_verbs[1] = ad1986a_ultra_init;
801                 spec->multiout.max_channels = 2;
802                 spec->multiout.num_dacs = 1;
803                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
804                 spec->multiout.dig_out_nid = 0;
805                 break;
806         }
807
808         return 0;
809 }
810
811 /*
812  * AD1983 specific
813  */
814
815 #define AD1983_SPDIF_OUT        0x02
816 #define AD1983_DAC              0x03
817 #define AD1983_ADC              0x04
818
819 static hda_nid_t ad1983_dac_nids[1] = { AD1983_DAC };
820 static hda_nid_t ad1983_adc_nids[1] = { AD1983_ADC };
821 static hda_nid_t ad1983_capsrc_nids[1] = { 0x15 };
822
823 static struct hda_input_mux ad1983_capture_source = {
824         .num_items = 4,
825         .items = {
826                 { "Mic", 0x0 },
827                 { "Line", 0x1 },
828                 { "Mix", 0x2 },
829                 { "Mix Mono", 0x3 },
830         },
831 };
832
833 /*
834  * SPDIF playback route
835  */
836 static int ad1983_spdif_route_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
837 {
838         static char *texts[] = { "PCM", "ADC" };
839
840         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
841         uinfo->count = 1;
842         uinfo->value.enumerated.items = 2;
843         if (uinfo->value.enumerated.item > 1)
844                 uinfo->value.enumerated.item = 1;
845         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
846         return 0;
847 }
848
849 static int ad1983_spdif_route_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
850 {
851         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
852         struct ad198x_spec *spec = codec->spec;
853
854         ucontrol->value.enumerated.item[0] = spec->spdif_route;
855         return 0;
856 }
857
858 static int ad1983_spdif_route_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
859 {
860         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
861         struct ad198x_spec *spec = codec->spec;
862
863         if (spec->spdif_route != ucontrol->value.enumerated.item[0]) {
864                 spec->spdif_route = ucontrol->value.enumerated.item[0];
865                 snd_hda_codec_write_cache(codec, spec->multiout.dig_out_nid, 0,
866                                           AC_VERB_SET_CONNECT_SEL,
867                                           spec->spdif_route);
868                 return 1;
869         }
870         return 0;
871 }
872
873 static struct snd_kcontrol_new ad1983_mixers[] = {
874         HDA_CODEC_VOLUME("Front Playback Volume", 0x05, 0x0, HDA_OUTPUT),
875         HDA_CODEC_MUTE("Front Playback Switch", 0x05, 0x0, HDA_OUTPUT),
876         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x06, 0x0, HDA_OUTPUT),
877         HDA_CODEC_MUTE("Headphone Playback Switch", 0x06, 0x0, HDA_OUTPUT),
878         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x07, 1, 0x0, HDA_OUTPUT),
879         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x07, 1, 0x0, HDA_OUTPUT),
880         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
881         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
882         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
883         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
884         HDA_CODEC_VOLUME("Line Playback Volume", 0x13, 0x0, HDA_OUTPUT),
885         HDA_CODEC_MUTE("Line Playback Switch", 0x13, 0x0, HDA_OUTPUT),
886         HDA_CODEC_VOLUME_MONO("PC Speaker Playback Volume", 0x10, 1, 0x0, HDA_OUTPUT),
887         HDA_CODEC_MUTE_MONO("PC Speaker Playback Switch", 0x10, 1, 0x0, HDA_OUTPUT),
888         HDA_CODEC_VOLUME("Mic Boost", 0x0c, 0x0, HDA_OUTPUT),
889         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
890         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
891         {
892                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
893                 .name = "Capture Source",
894                 .info = ad198x_mux_enum_info,
895                 .get = ad198x_mux_enum_get,
896                 .put = ad198x_mux_enum_put,
897         },
898         {
899                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
900                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
901                 .info = ad1983_spdif_route_info,
902                 .get = ad1983_spdif_route_get,
903                 .put = ad1983_spdif_route_put,
904         },
905         { } /* end */
906 };
907
908 static struct hda_verb ad1983_init_verbs[] = {
909         /* Front, HP, Mono; mute as default */
910         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
911         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
912         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
913         /* Beep, PCM, Mic, Line-In: mute */
914         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
915         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
916         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
917         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
918         /* Front, HP selectors; from Mix */
919         {0x05, AC_VERB_SET_CONNECT_SEL, 0x01},
920         {0x06, AC_VERB_SET_CONNECT_SEL, 0x01},
921         /* Mono selector; from Mix */
922         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x03},
923         /* Mic selector; Mic */
924         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
925         /* Line-in selector: Line-in */
926         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
927         /* Mic boost: 0dB */
928         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
929         /* Record selector: mic */
930         {0x15, AC_VERB_SET_CONNECT_SEL, 0x0},
931         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
932         /* SPDIF route: PCM */
933         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0},
934         /* Front Pin */
935         {0x05, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
936         /* HP Pin */
937         {0x06, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
938         /* Mono Pin */
939         {0x07, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
940         /* Mic Pin */
941         {0x08, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
942         /* Line Pin */
943         {0x09, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
944         { } /* end */
945 };
946
947
948 static int patch_ad1983(struct hda_codec *codec)
949 {
950         struct ad198x_spec *spec;
951
952         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
953         if (spec == NULL)
954                 return -ENOMEM;
955
956         codec->spec = spec;
957
958         spec->multiout.max_channels = 2;
959         spec->multiout.num_dacs = ARRAY_SIZE(ad1983_dac_nids);
960         spec->multiout.dac_nids = ad1983_dac_nids;
961         spec->multiout.dig_out_nid = AD1983_SPDIF_OUT;
962         spec->num_adc_nids = 1;
963         spec->adc_nids = ad1983_adc_nids;
964         spec->capsrc_nids = ad1983_capsrc_nids;
965         spec->input_mux = &ad1983_capture_source;
966         spec->num_mixers = 1;
967         spec->mixers[0] = ad1983_mixers;
968         spec->num_init_verbs = 1;
969         spec->init_verbs[0] = ad1983_init_verbs;
970         spec->spdif_route = 0;
971
972         codec->patch_ops = ad198x_patch_ops;
973
974         return 0;
975 }
976
977
978 /*
979  * AD1981 HD specific
980  */
981
982 #define AD1981_SPDIF_OUT        0x02
983 #define AD1981_DAC              0x03
984 #define AD1981_ADC              0x04
985
986 static hda_nid_t ad1981_dac_nids[1] = { AD1981_DAC };
987 static hda_nid_t ad1981_adc_nids[1] = { AD1981_ADC };
988 static hda_nid_t ad1981_capsrc_nids[1] = { 0x15 };
989
990 /* 0x0c, 0x09, 0x0e, 0x0f, 0x19, 0x05, 0x18, 0x17 */
991 static struct hda_input_mux ad1981_capture_source = {
992         .num_items = 7,
993         .items = {
994                 { "Front Mic", 0x0 },
995                 { "Line", 0x1 },
996                 { "Mix", 0x2 },
997                 { "Mix Mono", 0x3 },
998                 { "CD", 0x4 },
999                 { "Mic", 0x6 },
1000                 { "Aux", 0x7 },
1001         },
1002 };
1003
1004 static struct snd_kcontrol_new ad1981_mixers[] = {
1005         HDA_CODEC_VOLUME("Front Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1006         HDA_CODEC_MUTE("Front Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1007         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1008         HDA_CODEC_MUTE("Headphone Playback Switch", 0x06, 0x0, HDA_OUTPUT),
1009         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x07, 1, 0x0, HDA_OUTPUT),
1010         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x07, 1, 0x0, HDA_OUTPUT),
1011         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1012         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1013         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1014         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1015         HDA_CODEC_VOLUME("Line Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1016         HDA_CODEC_MUTE("Line Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1017         HDA_CODEC_VOLUME("Aux Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
1018         HDA_CODEC_MUTE("Aux Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
1019         HDA_CODEC_VOLUME("Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
1020         HDA_CODEC_MUTE("Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
1021         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1022         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1023         HDA_CODEC_VOLUME_MONO("PC Speaker Playback Volume", 0x0d, 1, 0x0, HDA_OUTPUT),
1024         HDA_CODEC_MUTE_MONO("PC Speaker Playback Switch", 0x0d, 1, 0x0, HDA_OUTPUT),
1025         HDA_CODEC_VOLUME("Front Mic Boost", 0x08, 0x0, HDA_INPUT),
1026         HDA_CODEC_VOLUME("Mic Boost", 0x18, 0x0, HDA_INPUT),
1027         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1028         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1029         {
1030                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1031                 .name = "Capture Source",
1032                 .info = ad198x_mux_enum_info,
1033                 .get = ad198x_mux_enum_get,
1034                 .put = ad198x_mux_enum_put,
1035         },
1036         /* identical with AD1983 */
1037         {
1038                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1039                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1040                 .info = ad1983_spdif_route_info,
1041                 .get = ad1983_spdif_route_get,
1042                 .put = ad1983_spdif_route_put,
1043         },
1044         { } /* end */
1045 };
1046
1047 static struct hda_verb ad1981_init_verbs[] = {
1048         /* Front, HP, Mono; mute as default */
1049         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1050         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1051         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1052         /* Beep, PCM, Front Mic, Line, Rear Mic, Aux, CD-In: mute */
1053         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1054         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1055         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1056         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1057         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1058         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1059         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1060         /* Front, HP selectors; from Mix */
1061         {0x05, AC_VERB_SET_CONNECT_SEL, 0x01},
1062         {0x06, AC_VERB_SET_CONNECT_SEL, 0x01},
1063         /* Mono selector; from Mix */
1064         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x03},
1065         /* Mic Mixer; select Front Mic */
1066         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1067         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1068         /* Mic boost: 0dB */
1069         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1070         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1071         /* Record selector: Front mic */
1072         {0x15, AC_VERB_SET_CONNECT_SEL, 0x0},
1073         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1074         /* SPDIF route: PCM */
1075         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0},
1076         /* Front Pin */
1077         {0x05, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1078         /* HP Pin */
1079         {0x06, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
1080         /* Mono Pin */
1081         {0x07, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1082         /* Front & Rear Mic Pins */
1083         {0x08, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1084         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1085         /* Line Pin */
1086         {0x09, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
1087         /* Digital Beep */
1088         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x00},
1089         /* Line-Out as Input: disabled */
1090         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1091         { } /* end */
1092 };
1093
1094 /*
1095  * Patch for HP nx6320
1096  *
1097  * nx6320 uses EAPD in the reverse way - EAPD-on means the internal
1098  * speaker output enabled _and_ mute-LED off.
1099  */
1100
1101 #define AD1981_HP_EVENT         0x37
1102 #define AD1981_MIC_EVENT        0x38
1103
1104 static struct hda_verb ad1981_hp_init_verbs[] = {
1105         {0x05, AC_VERB_SET_EAPD_BTLENABLE, 0x00 }, /* default off */
1106         /* pin sensing on HP and Mic jacks */
1107         {0x06, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_HP_EVENT},
1108         {0x08, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_MIC_EVENT},
1109         {}
1110 };
1111
1112 /* turn on/off EAPD (+ mute HP) as a master switch */
1113 static int ad1981_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1114                                    struct snd_ctl_elem_value *ucontrol)
1115 {
1116         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1117         struct ad198x_spec *spec = codec->spec;
1118
1119         if (! ad198x_eapd_put(kcontrol, ucontrol))
1120                 return 0;
1121
1122         /* toggle HP mute appropriately */
1123         snd_hda_codec_amp_update(codec, 0x06, 0, HDA_OUTPUT, 0,
1124                                  0x80, spec->cur_eapd ? 0 : 0x80);
1125         snd_hda_codec_amp_update(codec, 0x06, 1, HDA_OUTPUT, 0,
1126                                  0x80, spec->cur_eapd ? 0 : 0x80);
1127         return 1;
1128 }
1129
1130 /* bind volumes of both NID 0x05 and 0x06 */
1131 static int ad1981_hp_master_vol_put(struct snd_kcontrol *kcontrol,
1132                                     struct snd_ctl_elem_value *ucontrol)
1133 {
1134         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1135         long *valp = ucontrol->value.integer.value;
1136         int change;
1137
1138         change = snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1139                                           0x7f, valp[0] & 0x7f);
1140         change |= snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1141                                            0x7f, valp[1] & 0x7f);
1142         snd_hda_codec_amp_update(codec, 0x06, 0, HDA_OUTPUT, 0,
1143                                  0x7f, valp[0] & 0x7f);
1144         snd_hda_codec_amp_update(codec, 0x06, 1, HDA_OUTPUT, 0,
1145                                  0x7f, valp[1] & 0x7f);
1146         return change;
1147 }
1148
1149 /* mute internal speaker if HP is plugged */
1150 static void ad1981_hp_automute(struct hda_codec *codec)
1151 {
1152         unsigned int present;
1153
1154         present = snd_hda_codec_read(codec, 0x06, 0,
1155                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1156         snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1157                                  0x80, present ? 0x80 : 0);
1158         snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1159                                  0x80, present ? 0x80 : 0);
1160 }
1161
1162 /* toggle input of built-in and mic jack appropriately */
1163 static void ad1981_hp_automic(struct hda_codec *codec)
1164 {
1165         static struct hda_verb mic_jack_on[] = {
1166                 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1167                 {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1168                 {}
1169         };
1170         static struct hda_verb mic_jack_off[] = {
1171                 {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1172                 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1173                 {}
1174         };
1175         unsigned int present;
1176
1177         present = snd_hda_codec_read(codec, 0x08, 0,
1178                                  AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1179         if (present)
1180                 snd_hda_sequence_write(codec, mic_jack_on);
1181         else
1182                 snd_hda_sequence_write(codec, mic_jack_off);
1183 }
1184
1185 /* unsolicited event for HP jack sensing */
1186 static void ad1981_hp_unsol_event(struct hda_codec *codec,
1187                                   unsigned int res)
1188 {
1189         res >>= 26;
1190         switch (res) {
1191         case AD1981_HP_EVENT:
1192                 ad1981_hp_automute(codec);
1193                 break;
1194         case AD1981_MIC_EVENT:
1195                 ad1981_hp_automic(codec);
1196                 break;
1197         }
1198 }
1199
1200 static struct hda_input_mux ad1981_hp_capture_source = {
1201         .num_items = 3,
1202         .items = {
1203                 { "Mic", 0x0 },
1204                 { "Docking-Station", 0x1 },
1205                 { "Mix", 0x2 },
1206         },
1207 };
1208
1209 static struct snd_kcontrol_new ad1981_hp_mixers[] = {
1210         {
1211                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1212                 .name = "Master Playback Volume",
1213                 .info = snd_hda_mixer_amp_volume_info,
1214                 .get = snd_hda_mixer_amp_volume_get,
1215                 .put = ad1981_hp_master_vol_put,
1216                 .private_value = HDA_COMPOSE_AMP_VAL(0x05, 3, 0, HDA_OUTPUT),
1217         },
1218         {
1219                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1220                 .name = "Master Playback Switch",
1221                 .info = ad198x_eapd_info,
1222                 .get = ad198x_eapd_get,
1223                 .put = ad1981_hp_master_sw_put,
1224                 .private_value = 0x05,
1225         },
1226         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1227         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1228 #if 0
1229         /* FIXME: analog mic/line loopback doesn't work with my tests...
1230          *        (although recording is OK)
1231          */
1232         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1233         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1234         HDA_CODEC_VOLUME("Docking-Station Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1235         HDA_CODEC_MUTE("Docking-Station Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1236         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
1237         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
1238         /* FIXME: does this laptop have analog CD connection? */
1239         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1240         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1241 #endif
1242         HDA_CODEC_VOLUME("Mic Boost", 0x08, 0x0, HDA_INPUT),
1243         HDA_CODEC_VOLUME("Internal Mic Boost", 0x18, 0x0, HDA_INPUT),
1244         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1245         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1246         {
1247                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1248                 .name = "Capture Source",
1249                 .info = ad198x_mux_enum_info,
1250                 .get = ad198x_mux_enum_get,
1251                 .put = ad198x_mux_enum_put,
1252         },
1253         { } /* end */
1254 };
1255
1256 /* initialize jack-sensing, too */
1257 static int ad1981_hp_init(struct hda_codec *codec)
1258 {
1259         ad198x_init(codec);
1260         ad1981_hp_automute(codec);
1261         ad1981_hp_automic(codec);
1262         return 0;
1263 }
1264
1265 /* configuration for Toshiba Laptops */
1266 static struct hda_verb ad1981_toshiba_init_verbs[] = {
1267         {0x05, AC_VERB_SET_EAPD_BTLENABLE, 0x01 }, /* default on */
1268         /* pin sensing on HP and Mic jacks */
1269         {0x06, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_HP_EVENT},
1270         {0x08, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_MIC_EVENT},
1271         {}
1272 };
1273
1274 static struct snd_kcontrol_new ad1981_toshiba_mixers[] = {
1275         HDA_CODEC_VOLUME("Amp Volume", 0x1a, 0x0, HDA_OUTPUT),
1276         HDA_CODEC_MUTE("Amp Switch", 0x1a, 0x0, HDA_OUTPUT),
1277         { }
1278 };
1279
1280 /* configuration for Lenovo Thinkpad T60 */
1281 static struct snd_kcontrol_new ad1981_thinkpad_mixers[] = {
1282         HDA_CODEC_VOLUME("Master Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1283         HDA_CODEC_MUTE("Master Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1284         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1285         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1286         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1287         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1288         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1289         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1290         HDA_CODEC_VOLUME("Mic Boost", 0x08, 0x0, HDA_INPUT),
1291         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1292         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1293         {
1294                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1295                 .name = "Capture Source",
1296                 .info = ad198x_mux_enum_info,
1297                 .get = ad198x_mux_enum_get,
1298                 .put = ad198x_mux_enum_put,
1299         },
1300         /* identical with AD1983 */
1301         {
1302                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1303                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1304                 .info = ad1983_spdif_route_info,
1305                 .get = ad1983_spdif_route_get,
1306                 .put = ad1983_spdif_route_put,
1307         },
1308         { } /* end */
1309 };
1310
1311 static struct hda_input_mux ad1981_thinkpad_capture_source = {
1312         .num_items = 3,
1313         .items = {
1314                 { "Mic", 0x0 },
1315                 { "Mix", 0x2 },
1316                 { "CD", 0x4 },
1317         },
1318 };
1319
1320 /* models */
1321 enum {
1322         AD1981_BASIC,
1323         AD1981_HP,
1324         AD1981_THINKPAD,
1325         AD1981_TOSHIBA,
1326         AD1981_MODELS
1327 };
1328
1329 static const char *ad1981_models[AD1981_MODELS] = {
1330         [AD1981_HP]             = "hp",
1331         [AD1981_THINKPAD]       = "thinkpad",
1332         [AD1981_BASIC]          = "basic",
1333         [AD1981_TOSHIBA]        = "toshiba"
1334 };
1335
1336 static struct snd_pci_quirk ad1981_cfg_tbl[] = {
1337         /* All HP models */
1338         SND_PCI_QUIRK(0x103c, 0, "HP nx", AD1981_HP),
1339         /* HP nx6320 (reversed SSID, H/W bug) */
1340         SND_PCI_QUIRK(0x30b0, 0x103c, "HP nx6320", AD1981_HP),
1341         /* Lenovo Thinkpad T60/X60/Z6xx */
1342         SND_PCI_QUIRK(0x17aa, 0, "Lenovo Thinkpad", AD1981_THINKPAD),
1343         SND_PCI_QUIRK(0x1014, 0x0597, "Lenovo Z60", AD1981_THINKPAD),
1344         SND_PCI_QUIRK(0x1179, 0x0001, "Toshiba U205", AD1981_TOSHIBA),
1345         {}
1346 };
1347
1348 static int patch_ad1981(struct hda_codec *codec)
1349 {
1350         struct ad198x_spec *spec;
1351         int board_config;
1352
1353         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1354         if (spec == NULL)
1355                 return -ENOMEM;
1356
1357         codec->spec = spec;
1358
1359         spec->multiout.max_channels = 2;
1360         spec->multiout.num_dacs = ARRAY_SIZE(ad1981_dac_nids);
1361         spec->multiout.dac_nids = ad1981_dac_nids;
1362         spec->multiout.dig_out_nid = AD1981_SPDIF_OUT;
1363         spec->num_adc_nids = 1;
1364         spec->adc_nids = ad1981_adc_nids;
1365         spec->capsrc_nids = ad1981_capsrc_nids;
1366         spec->input_mux = &ad1981_capture_source;
1367         spec->num_mixers = 1;
1368         spec->mixers[0] = ad1981_mixers;
1369         spec->num_init_verbs = 1;
1370         spec->init_verbs[0] = ad1981_init_verbs;
1371         spec->spdif_route = 0;
1372
1373         codec->patch_ops = ad198x_patch_ops;
1374
1375         /* override some parameters */
1376         board_config = snd_hda_check_board_config(codec, AD1981_MODELS,
1377                                                   ad1981_models,
1378                                                   ad1981_cfg_tbl);
1379         switch (board_config) {
1380         case AD1981_HP:
1381                 spec->mixers[0] = ad1981_hp_mixers;
1382                 spec->num_init_verbs = 2;
1383                 spec->init_verbs[1] = ad1981_hp_init_verbs;
1384                 spec->multiout.dig_out_nid = 0;
1385                 spec->input_mux = &ad1981_hp_capture_source;
1386
1387                 codec->patch_ops.init = ad1981_hp_init;
1388                 codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
1389                 break;
1390         case AD1981_THINKPAD:
1391                 spec->mixers[0] = ad1981_thinkpad_mixers;
1392                 spec->input_mux = &ad1981_thinkpad_capture_source;
1393                 break;
1394         case AD1981_TOSHIBA:
1395                 spec->mixers[0] = ad1981_hp_mixers;
1396                 spec->mixers[1] = ad1981_toshiba_mixers;
1397                 spec->num_init_verbs = 2;
1398                 spec->init_verbs[1] = ad1981_toshiba_init_verbs;
1399                 spec->multiout.dig_out_nid = 0;
1400                 spec->input_mux = &ad1981_hp_capture_source;
1401                 codec->patch_ops.init = ad1981_hp_init;
1402                 codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
1403                 break;
1404         }
1405         return 0;
1406 }
1407
1408
1409 /*
1410  * AD1988
1411  *
1412  * Output pins and routes
1413  *
1414  *        Pin               Mix     Sel     DAC (*)
1415  * port-A 0x11 (mute/hp) <- 0x22 <- 0x37 <- 03/04/06
1416  * port-B 0x14 (mute/hp) <- 0x2b <- 0x30 <- 03/04/06
1417  * port-C 0x15 (mute)    <- 0x2c <- 0x31 <- 05/0a
1418  * port-D 0x12 (mute/hp) <- 0x29         <- 04
1419  * port-E 0x17 (mute/hp) <- 0x26 <- 0x32 <- 05/0a
1420  * port-F 0x16 (mute)    <- 0x2a         <- 06
1421  * port-G 0x24 (mute)    <- 0x27         <- 05
1422  * port-H 0x25 (mute)    <- 0x28         <- 0a
1423  * mono   0x13 (mute/amp)<- 0x1e <- 0x36 <- 03/04/06
1424  *
1425  * DAC0 = 03h, DAC1 = 04h, DAC2 = 05h, DAC3 = 06h, DAC4 = 0ah
1426  * (*) DAC2/3/4 are swapped to DAC3/4/2 on AD198A rev.2 due to a h/w bug.
1427  *
1428  * Input pins and routes
1429  *
1430  *        pin     boost   mix input # / adc input #
1431  * port-A 0x11 -> 0x38 -> mix 2, ADC 0
1432  * port-B 0x14 -> 0x39 -> mix 0, ADC 1
1433  * port-C 0x15 -> 0x3a -> 33:0 - mix 1, ADC 2
1434  * port-D 0x12 -> 0x3d -> mix 3, ADC 8
1435  * port-E 0x17 -> 0x3c -> 34:0 - mix 4, ADC 4
1436  * port-F 0x16 -> 0x3b -> mix 5, ADC 3
1437  * port-G 0x24 -> N/A  -> 33:1 - mix 1, 34:1 - mix 4, ADC 6
1438  * port-H 0x25 -> N/A  -> 33:2 - mix 1, 34:2 - mix 4, ADC 7
1439  *
1440  *
1441  * DAC assignment
1442  *   6stack - front/surr/CLFE/side/opt DACs - 04/06/05/0a/03
1443  *   3stack - front/surr/CLFE/opt DACs - 04/05/0a/03
1444  *
1445  * Inputs of Analog Mix (0x20)
1446  *   0:Port-B (front mic)
1447  *   1:Port-C/G/H (line-in)
1448  *   2:Port-A
1449  *   3:Port-D (line-in/2)
1450  *   4:Port-E/G/H (mic-in)
1451  *   5:Port-F (mic2-in)
1452  *   6:CD
1453  *   7:Beep
1454  *
1455  * ADC selection
1456  *   0:Port-A
1457  *   1:Port-B (front mic-in)
1458  *   2:Port-C (line-in)
1459  *   3:Port-F (mic2-in)
1460  *   4:Port-E (mic-in)
1461  *   5:CD
1462  *   6:Port-G
1463  *   7:Port-H
1464  *   8:Port-D (line-in/2)
1465  *   9:Mix
1466  *
1467  * Proposed pin assignments by the datasheet
1468  *
1469  * 6-stack
1470  * Port-A front headphone
1471  *      B front mic-in
1472  *      C rear line-in
1473  *      D rear front-out
1474  *      E rear mic-in
1475  *      F rear surround
1476  *      G rear CLFE
1477  *      H rear side
1478  *
1479  * 3-stack
1480  * Port-A front headphone
1481  *      B front mic
1482  *      C rear line-in/surround
1483  *      D rear front-out
1484  *      E rear mic-in/CLFE
1485  *
1486  * laptop
1487  * Port-A headphone
1488  *      B mic-in
1489  *      C docking station
1490  *      D internal speaker (with EAPD)
1491  *      E/F quad mic array
1492  */
1493
1494
1495 /* models */
1496 enum {
1497         AD1988_6STACK,
1498         AD1988_6STACK_DIG,
1499         AD1988_3STACK,
1500         AD1988_3STACK_DIG,
1501         AD1988_LAPTOP,
1502         AD1988_LAPTOP_DIG,
1503         AD1988_AUTO,
1504         AD1988_MODEL_LAST,
1505 };
1506
1507 /* reivision id to check workarounds */
1508 #define AD1988A_REV2            0x100200
1509
1510 #define is_rev2(codec) \
1511         ((codec)->vendor_id == 0x11d41988 && \
1512          (codec)->revision_id == AD1988A_REV2)
1513
1514 /*
1515  * mixers
1516  */
1517
1518 static hda_nid_t ad1988_6stack_dac_nids[4] = {
1519         0x04, 0x06, 0x05, 0x0a
1520 };
1521
1522 static hda_nid_t ad1988_3stack_dac_nids[3] = {
1523         0x04, 0x05, 0x0a
1524 };
1525
1526 /* for AD1988A revision-2, DAC2-4 are swapped */
1527 static hda_nid_t ad1988_6stack_dac_nids_rev2[4] = {
1528         0x04, 0x05, 0x0a, 0x06
1529 };
1530
1531 static hda_nid_t ad1988_3stack_dac_nids_rev2[3] = {
1532         0x04, 0x0a, 0x06
1533 };
1534
1535 static hda_nid_t ad1988_adc_nids[3] = {
1536         0x08, 0x09, 0x0f
1537 };
1538
1539 static hda_nid_t ad1988_capsrc_nids[3] = {
1540         0x0c, 0x0d, 0x0e
1541 };
1542
1543 #define AD1988_SPDIF_OUT        0x02
1544 #define AD1988_SPDIF_IN         0x07
1545
1546 static struct hda_input_mux ad1988_6stack_capture_source = {
1547         .num_items = 5,
1548         .items = {
1549                 { "Front Mic", 0x0 },
1550                 { "Line", 0x1 },
1551                 { "Mic", 0x4 },
1552                 { "CD", 0x5 },
1553                 { "Mix", 0x9 },
1554         },
1555 };
1556
1557 static struct hda_input_mux ad1988_laptop_capture_source = {
1558         .num_items = 3,
1559         .items = {
1560                 { "Mic/Line", 0x0 },
1561                 { "CD", 0x5 },
1562                 { "Mix", 0x9 },
1563         },
1564 };
1565
1566 /*
1567  */
1568 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
1569                                struct snd_ctl_elem_info *uinfo)
1570 {
1571         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1572         struct ad198x_spec *spec = codec->spec;
1573         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
1574                                     spec->num_channel_mode);
1575 }
1576
1577 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
1578                               struct snd_ctl_elem_value *ucontrol)
1579 {
1580         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1581         struct ad198x_spec *spec = codec->spec;
1582         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
1583                                    spec->num_channel_mode, spec->multiout.max_channels);
1584 }
1585
1586 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
1587                               struct snd_ctl_elem_value *ucontrol)
1588 {
1589         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1590         struct ad198x_spec *spec = codec->spec;
1591         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
1592                                       spec->num_channel_mode,
1593                                       &spec->multiout.max_channels);
1594         if (err >= 0 && spec->need_dac_fix)
1595                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1596         return err;
1597 }
1598
1599 /* 6-stack mode */
1600 static struct snd_kcontrol_new ad1988_6stack_mixers1[] = {
1601         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1602         HDA_CODEC_VOLUME("Surround Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1603         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
1604         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
1605         HDA_CODEC_VOLUME("Side Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1606         { } /* end */
1607 };
1608
1609 static struct snd_kcontrol_new ad1988_6stack_mixers1_rev2[] = {
1610         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1611         HDA_CODEC_VOLUME("Surround Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1612         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0a, 1, 0x0, HDA_OUTPUT),
1613         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0a, 2, 0x0, HDA_OUTPUT),
1614         HDA_CODEC_VOLUME("Side Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1615         { } /* end */
1616 };
1617
1618 static struct snd_kcontrol_new ad1988_6stack_mixers2[] = {
1619         HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
1620         HDA_BIND_MUTE("Surround Playback Switch", 0x2a, 2, HDA_INPUT),
1621         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x27, 1, 2, HDA_INPUT),
1622         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x27, 2, 2, HDA_INPUT),
1623         HDA_BIND_MUTE("Side Playback Switch", 0x28, 2, HDA_INPUT),
1624         HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
1625         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1626
1627         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1628         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1629         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1630         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1631         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1632         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1633         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
1634         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),
1635
1636         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1637         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1638
1639         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1640         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1641
1642         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1643         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
1644
1645         { } /* end */
1646 };
1647
1648 /* 3-stack mode */
1649 static struct snd_kcontrol_new ad1988_3stack_mixers1[] = {
1650         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1651         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1652         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
1653         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
1654         { } /* end */
1655 };
1656
1657 static struct snd_kcontrol_new ad1988_3stack_mixers1_rev2[] = {
1658         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1659         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1660         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x06, 1, 0x0, HDA_OUTPUT),
1661         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x06, 2, 0x0, HDA_OUTPUT),
1662         { } /* end */
1663 };
1664
1665 static struct snd_kcontrol_new ad1988_3stack_mixers2[] = {
1666         HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
1667         HDA_BIND_MUTE("Surround Playback Switch", 0x2c, 2, HDA_INPUT),
1668         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x26, 1, 2, HDA_INPUT),
1669         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x26, 2, 2, HDA_INPUT),
1670         HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
1671         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1672
1673         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1674         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1675         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1676         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1677         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1678         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1679         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
1680         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),
1681
1682         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1683         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1684
1685         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1686         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1687
1688         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1689         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
1690         {
1691                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1692                 .name = "Channel Mode",
1693                 .info = ad198x_ch_mode_info,
1694                 .get = ad198x_ch_mode_get,
1695                 .put = ad198x_ch_mode_put,
1696         },
1697
1698         { } /* end */
1699 };
1700
1701 /* laptop mode */
1702 static struct snd_kcontrol_new ad1988_laptop_mixers[] = {
1703         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1704         HDA_CODEC_MUTE("PCM Playback Switch", 0x29, 0x0, HDA_INPUT),
1705         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1706
1707         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1708         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1709         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1710         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1711         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1712         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1713
1714         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1715         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1716
1717         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1718         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1719
1720         HDA_CODEC_VOLUME("Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1721
1722         {
1723                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1724                 .name = "External Amplifier",
1725                 .info = ad198x_eapd_info,
1726                 .get = ad198x_eapd_get,
1727                 .put = ad198x_eapd_put,
1728                 .private_value = 0x12 | (1 << 8), /* port-D, inversed */
1729         },
1730
1731         { } /* end */
1732 };
1733
1734 /* capture */
1735 static struct snd_kcontrol_new ad1988_capture_mixers[] = {
1736         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
1737         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
1738         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
1739         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
1740         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x0e, 0x0, HDA_OUTPUT),
1741         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x0e, 0x0, HDA_OUTPUT),
1742         {
1743                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1744                 /* The multiple "Capture Source" controls confuse alsamixer
1745                  * So call somewhat different..
1746                  * FIXME: the controls appear in the "playback" view!
1747                  */
1748                 /* .name = "Capture Source", */
1749                 .name = "Input Source",
1750                 .count = 3,
1751                 .info = ad198x_mux_enum_info,
1752                 .get = ad198x_mux_enum_get,
1753                 .put = ad198x_mux_enum_put,
1754         },
1755         { } /* end */
1756 };
1757
1758 static int ad1988_spdif_playback_source_info(struct snd_kcontrol *kcontrol,
1759                                              struct snd_ctl_elem_info *uinfo)
1760 {
1761         static char *texts[] = {
1762                 "PCM", "ADC1", "ADC2", "ADC3"
1763         };
1764         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1765         uinfo->count = 1;
1766         uinfo->value.enumerated.items = 4;
1767         if (uinfo->value.enumerated.item >= 4)
1768                 uinfo->value.enumerated.item = 3;
1769         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1770         return 0;
1771 }
1772
1773 static int ad1988_spdif_playback_source_get(struct snd_kcontrol *kcontrol,
1774                                             struct snd_ctl_elem_value *ucontrol)
1775 {
1776         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1777         unsigned int sel;
1778
1779         sel = snd_hda_codec_read(codec, 0x1d, 0, AC_VERB_GET_AMP_GAIN_MUTE,
1780                                  AC_AMP_GET_INPUT);
1781         if (!(sel & 0x80))
1782                 ucontrol->value.enumerated.item[0] = 0;
1783         else {
1784                 sel = snd_hda_codec_read(codec, 0x0b, 0,
1785                                          AC_VERB_GET_CONNECT_SEL, 0);
1786                 if (sel < 3)
1787                         sel++;
1788                 else
1789                         sel = 0;
1790                 ucontrol->value.enumerated.item[0] = sel;
1791         }
1792         return 0;
1793 }
1794
1795 static int ad1988_spdif_playback_source_put(struct snd_kcontrol *kcontrol,
1796                                             struct snd_ctl_elem_value *ucontrol)
1797 {
1798         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1799         unsigned int val, sel;
1800         int change;
1801
1802         val = ucontrol->value.enumerated.item[0];
1803         if (!val) {
1804                 sel = snd_hda_codec_read(codec, 0x1d, 0,
1805                                          AC_VERB_GET_AMP_GAIN_MUTE,
1806                                          AC_AMP_GET_INPUT);
1807                 change = sel & 0x80;
1808                 if (change) {
1809                         snd_hda_codec_write_cache(codec, 0x1d, 0,
1810                                                   AC_VERB_SET_AMP_GAIN_MUTE,
1811                                                   AMP_IN_UNMUTE(0));
1812                         snd_hda_codec_write_cache(codec, 0x1d, 0,
1813                                                   AC_VERB_SET_AMP_GAIN_MUTE,
1814                                                   AMP_IN_MUTE(1));
1815                 }
1816         } else {
1817                 sel = snd_hda_codec_read(codec, 0x1d, 0,
1818                                          AC_VERB_GET_AMP_GAIN_MUTE,
1819                                          AC_AMP_GET_INPUT | 0x01);
1820                 change = sel & 0x80;
1821                 if (change) {
1822                         snd_hda_codec_write_cache(codec, 0x1d, 0,
1823                                                   AC_VERB_SET_AMP_GAIN_MUTE,
1824                                                   AMP_IN_MUTE(0));
1825                         snd_hda_codec_write_cache(codec, 0x1d, 0,
1826                                                   AC_VERB_SET_AMP_GAIN_MUTE,
1827                                                   AMP_IN_UNMUTE(1));
1828                 }
1829                 sel = snd_hda_codec_read(codec, 0x0b, 0,
1830                                          AC_VERB_GET_CONNECT_SEL, 0) + 1;
1831                 change |= sel != val;
1832                 if (change)
1833                         snd_hda_codec_write_cache(codec, 0x0b, 0,
1834                                                   AC_VERB_SET_CONNECT_SEL,
1835                                                   val - 1);
1836         }
1837         return change;
1838 }
1839
1840 static struct snd_kcontrol_new ad1988_spdif_out_mixers[] = {
1841         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
1842         {
1843                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1844                 .name = "IEC958 Playback Source",
1845                 .info = ad1988_spdif_playback_source_info,
1846                 .get = ad1988_spdif_playback_source_get,
1847                 .put = ad1988_spdif_playback_source_put,
1848         },
1849         { } /* end */
1850 };
1851
1852 static struct snd_kcontrol_new ad1988_spdif_in_mixers[] = {
1853         HDA_CODEC_VOLUME("IEC958 Capture Volume", 0x1c, 0x0, HDA_INPUT),
1854         { } /* end */
1855 };
1856
1857
1858 /*
1859  * initialization verbs
1860  */
1861
1862 /*
1863  * for 6-stack (+dig)
1864  */
1865 static struct hda_verb ad1988_6stack_init_verbs[] = {
1866         /* Front, Surround, CLFE, side DAC; unmute as default */
1867         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1868         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1869         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1870         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1871         /* Port-A front headphon path */
1872         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
1873         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1874         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1875         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1876         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1877         /* Port-D line-out path */
1878         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1879         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1880         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1881         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1882         /* Port-F surround path */
1883         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1884         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1885         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1886         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1887         /* Port-G CLFE path */
1888         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1889         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1890         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1891         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1892         /* Port-H side path */
1893         {0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1894         {0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1895         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1896         {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1897         /* Mono out path */
1898         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
1899         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1900         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1901         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1902         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
1903         /* Port-B front mic-in path */
1904         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1905         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1906         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1907         /* Port-C line-in path */
1908         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1909         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1910         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1911         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
1912         /* Port-E mic-in path */
1913         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1914         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1915         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1916         {0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
1917
1918         { }
1919 };
1920
1921 static struct hda_verb ad1988_capture_init_verbs[] = {
1922         /* mute analog mix */
1923         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1924         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1925         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
1926         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
1927         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
1928         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
1929         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
1930         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
1931         /* select ADCs - front-mic */
1932         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
1933         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
1934         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
1935         /* ADCs; muted */
1936         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1937         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1938         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1939
1940         { }
1941 };
1942
1943 static struct hda_verb ad1988_spdif_init_verbs[] = {
1944         /* SPDIF out sel */
1945         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
1946         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x0}, /* ADC1 */
1947         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1948         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1949         /* SPDIF out pin */
1950         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
1951
1952         { }
1953 };
1954
1955 /*
1956  * verbs for 3stack (+dig)
1957  */
1958 static struct hda_verb ad1988_3stack_ch2_init[] = {
1959         /* set port-C to line-in */
1960         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
1961         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1962         /* set port-E to mic-in */
1963         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
1964         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
1965         { } /* end */
1966 };
1967
1968 static struct hda_verb ad1988_3stack_ch6_init[] = {
1969         /* set port-C to surround out */
1970         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
1971         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
1972         /* set port-E to CLFE out */
1973         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
1974         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
1975         { } /* end */
1976 };
1977
1978 static struct hda_channel_mode ad1988_3stack_modes[2] = {
1979         { 2, ad1988_3stack_ch2_init },
1980         { 6, ad1988_3stack_ch6_init },
1981 };
1982
1983 static struct hda_verb ad1988_3stack_init_verbs[] = {
1984         /* Front, Surround, CLFE, side DAC; unmute as default */
1985         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1986         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1987         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1988         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1989         /* Port-A front headphon path */
1990         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
1991         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1992         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1993         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1994         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1995         /* Port-D line-out path */
1996         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1997         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1998         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1999         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2000         /* Mono out path */
2001         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2002         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2003         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2004         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2005         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2006         /* Port-B front mic-in path */
2007         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2008         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2009         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2010         /* Port-C line-in/surround path - 6ch mode as default */
2011         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2012         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2013         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2014         {0x31, AC_VERB_SET_CONNECT_SEL, 0x0}, /* output sel: DAC 0x05 */
2015         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2016         /* Port-E mic-in/CLFE path - 6ch mode as default */
2017         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2018         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2019         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2020         {0x32, AC_VERB_SET_CONNECT_SEL, 0x1}, /* output sel: DAC 0x0a */
2021         {0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
2022         /* mute analog mix */
2023         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2024         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2025         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2026         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2027         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2028         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2029         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2030         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2031         /* select ADCs - front-mic */
2032         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2033         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2034         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2035         /* ADCs; muted */
2036         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2037         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2038         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2039         { }
2040 };
2041
2042 /*
2043  * verbs for laptop mode (+dig)
2044  */
2045 static struct hda_verb ad1988_laptop_hp_on[] = {
2046         /* unmute port-A and mute port-D */
2047         { 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2048         { 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2049         { } /* end */
2050 };
2051 static struct hda_verb ad1988_laptop_hp_off[] = {
2052         /* mute port-A and unmute port-D */
2053         { 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2054         { 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2055         { } /* end */
2056 };
2057
2058 #define AD1988_HP_EVENT 0x01
2059
2060 static struct hda_verb ad1988_laptop_init_verbs[] = {
2061         /* Front, Surround, CLFE, side DAC; unmute as default */
2062         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2063         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2064         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2065         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2066         /* Port-A front headphon path */
2067         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
2068         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2069         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2070         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2071         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2072         /* unsolicited event for pin-sense */
2073         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1988_HP_EVENT },
2074         /* Port-D line-out path + EAPD */
2075         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2076         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2077         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2078         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2079         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x00}, /* EAPD-off */
2080         /* Mono out path */
2081         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2082         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2083         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2084         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2085         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2086         /* Port-B mic-in path */
2087         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2088         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2089         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2090         /* Port-C docking station - try to output */
2091         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2092         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2093         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2094         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2095         /* mute analog mix */
2096         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2097         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2098         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2099         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2100         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2101         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2102         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2103         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2104         /* select ADCs - mic */
2105         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2106         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2107         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2108         /* ADCs; muted */
2109         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2110         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2111         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2112         { }
2113 };
2114
2115 static void ad1988_laptop_unsol_event(struct hda_codec *codec, unsigned int res)
2116 {
2117         if ((res >> 26) != AD1988_HP_EVENT)
2118                 return;
2119         if (snd_hda_codec_read(codec, 0x11, 0, AC_VERB_GET_PIN_SENSE, 0) & (1 << 31))
2120                 snd_hda_sequence_write(codec, ad1988_laptop_hp_on);
2121         else
2122                 snd_hda_sequence_write(codec, ad1988_laptop_hp_off);
2123
2124
2125
2126 /*
2127  * Automatic parse of I/O pins from the BIOS configuration
2128  */
2129
2130 #define NUM_CONTROL_ALLOC       32
2131 #define NUM_VERB_ALLOC          32
2132
2133 enum {
2134         AD_CTL_WIDGET_VOL,
2135         AD_CTL_WIDGET_MUTE,
2136         AD_CTL_BIND_MUTE,
2137 };
2138 static struct snd_kcontrol_new ad1988_control_templates[] = {
2139         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2140         HDA_CODEC_MUTE(NULL, 0, 0, 0),
2141         HDA_BIND_MUTE(NULL, 0, 0, 0),
2142 };
2143
2144 /* add dynamic controls */
2145 static int add_control(struct ad198x_spec *spec, int type, const char *name,
2146                        unsigned long val)
2147 {
2148         struct snd_kcontrol_new *knew;
2149
2150         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
2151                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
2152
2153                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
2154                 if (! knew)
2155                         return -ENOMEM;
2156                 if (spec->kctl_alloc) {
2157                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
2158                         kfree(spec->kctl_alloc);
2159                 }
2160                 spec->kctl_alloc = knew;
2161                 spec->num_kctl_alloc = num;
2162         }
2163
2164         knew = &spec->kctl_alloc[spec->num_kctl_used];
2165         *knew = ad1988_control_templates[type];
2166         knew->name = kstrdup(name, GFP_KERNEL);
2167         if (! knew->name)
2168                 return -ENOMEM;
2169         knew->private_value = val;
2170         spec->num_kctl_used++;
2171         return 0;
2172 }
2173
2174 #define AD1988_PIN_CD_NID               0x18
2175 #define AD1988_PIN_BEEP_NID             0x10
2176
2177 static hda_nid_t ad1988_mixer_nids[8] = {
2178         /* A     B     C     D     E     F     G     H */
2179         0x22, 0x2b, 0x2c, 0x29, 0x26, 0x2a, 0x27, 0x28
2180 };
2181
2182 static inline hda_nid_t ad1988_idx_to_dac(struct hda_codec *codec, int idx)
2183 {
2184         static hda_nid_t idx_to_dac[8] = {
2185                 /* A     B     C     D     E     F     G     H */
2186                 0x04, 0x06, 0x05, 0x04, 0x0a, 0x06, 0x05, 0x0a
2187         };
2188         static hda_nid_t idx_to_dac_rev2[8] = {
2189                 /* A     B     C     D     E     F     G     H */
2190                 0x04, 0x05, 0x0a, 0x04, 0x06, 0x05, 0x0a, 0x06
2191         };
2192         if (is_rev2(codec))
2193                 return idx_to_dac_rev2[idx];
2194         else
2195                 return idx_to_dac[idx];
2196 }
2197
2198 static hda_nid_t ad1988_boost_nids[8] = {
2199         0x38, 0x39, 0x3a, 0x3d, 0x3c, 0x3b, 0, 0
2200 };
2201
2202 static int ad1988_pin_idx(hda_nid_t nid)
2203 {
2204         static hda_nid_t ad1988_io_pins[8] = {
2205                 0x11, 0x14, 0x15, 0x12, 0x17, 0x16, 0x24, 0x25
2206         };
2207         int i;
2208         for (i = 0; i < ARRAY_SIZE(ad1988_io_pins); i++)
2209                 if (ad1988_io_pins[i] == nid)
2210                         return i;
2211         return 0; /* should be -1 */
2212 }
2213
2214 static int ad1988_pin_to_loopback_idx(hda_nid_t nid)
2215 {
2216         static int loopback_idx[8] = {
2217                 2, 0, 1, 3, 4, 5, 1, 4
2218         };
2219         switch (nid) {
2220         case AD1988_PIN_CD_NID:
2221                 return 6;
2222         default:
2223                 return loopback_idx[ad1988_pin_idx(nid)];
2224         }
2225 }
2226
2227 static int ad1988_pin_to_adc_idx(hda_nid_t nid)
2228 {
2229         static int adc_idx[8] = {
2230                 0, 1, 2, 8, 4, 3, 6, 7
2231         };
2232         switch (nid) {
2233         case AD1988_PIN_CD_NID:
2234                 return 5;
2235         default:
2236                 return adc_idx[ad1988_pin_idx(nid)];
2237         }
2238 }
2239
2240 /* fill in the dac_nids table from the parsed pin configuration */
2241 static int ad1988_auto_fill_dac_nids(struct hda_codec *codec,
2242                                      const struct auto_pin_cfg *cfg)
2243 {
2244         struct ad198x_spec *spec = codec->spec;
2245         int i, idx;
2246
2247         spec->multiout.dac_nids = spec->private_dac_nids;
2248
2249         /* check the pins hardwired to audio widget */
2250         for (i = 0; i < cfg->line_outs; i++) {
2251                 idx = ad1988_pin_idx(cfg->line_out_pins[i]);
2252                 spec->multiout.dac_nids[i] = ad1988_idx_to_dac(codec, idx);
2253         }
2254         spec->multiout.num_dacs = cfg->line_outs;
2255         return 0;
2256 }
2257
2258 /* add playback controls from the parsed DAC table */
2259 static int ad1988_auto_create_multi_out_ctls(struct ad198x_spec *spec,
2260                                              const struct auto_pin_cfg *cfg)
2261 {
2262         char name[32];
2263         static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
2264         hda_nid_t nid;
2265         int i, err;
2266
2267         for (i = 0; i < cfg->line_outs; i++) {
2268                 hda_nid_t dac = spec->multiout.dac_nids[i];
2269                 if (! dac)
2270                         continue;
2271                 nid = ad1988_mixer_nids[ad1988_pin_idx(cfg->line_out_pins[i])];
2272                 if (i == 2) {
2273                         /* Center/LFE */
2274                         err = add_control(spec, AD_CTL_WIDGET_VOL,
2275                                           "Center Playback Volume",
2276                                           HDA_COMPOSE_AMP_VAL(dac, 1, 0, HDA_OUTPUT));
2277                         if (err < 0)
2278                                 return err;
2279                         err = add_control(spec, AD_CTL_WIDGET_VOL,
2280                                           "LFE Playback Volume",
2281                                           HDA_COMPOSE_AMP_VAL(dac, 2, 0, HDA_OUTPUT));
2282                         if (err < 0)
2283                                 return err;
2284                         err = add_control(spec, AD_CTL_BIND_MUTE,
2285                                           "Center Playback Switch",
2286                                           HDA_COMPOSE_AMP_VAL(nid, 1, 2, HDA_INPUT));
2287                         if (err < 0)
2288                                 return err;
2289                         err = add_control(spec, AD_CTL_BIND_MUTE,
2290                                           "LFE Playback Switch",
2291                                           HDA_COMPOSE_AMP_VAL(nid, 2, 2, HDA_INPUT));
2292                         if (err < 0)
2293                                 return err;
2294                 } else {
2295                         sprintf(name, "%s Playback Volume", chname[i]);
2296                         err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2297                                           HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT));
2298                         if (err < 0)
2299                                 return err;
2300                         sprintf(name, "%s Playback Switch", chname[i]);
2301                         err = add_control(spec, AD_CTL_BIND_MUTE, name,
2302                                           HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT));
2303                         if (err < 0)
2304                                 return err;
2305                 }
2306         }
2307         return 0;
2308 }
2309
2310 /* add playback controls for speaker and HP outputs */
2311 static int ad1988_auto_create_extra_out(struct hda_codec *codec, hda_nid_t pin,
2312                                         const char *pfx)
2313 {
2314         struct ad198x_spec *spec = codec->spec;
2315         hda_nid_t nid;
2316         int idx, err;
2317         char name[32];
2318
2319         if (! pin)
2320                 return 0;
2321
2322         idx = ad1988_pin_idx(pin);
2323         nid = ad1988_idx_to_dac(codec, idx);
2324         /* specify the DAC as the extra output */
2325         if (! spec->multiout.hp_nid)
2326                 spec->multiout.hp_nid = nid;
2327         else
2328                 spec->multiout.extra_out_nid[0] = nid;
2329         /* control HP volume/switch on the output mixer amp */
2330         sprintf(name, "%s Playback Volume", pfx);
2331         if ((err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2332                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
2333                 return err;
2334         nid = ad1988_mixer_nids[idx];
2335         sprintf(name, "%s Playback Switch", pfx);
2336         if ((err = add_control(spec, AD_CTL_BIND_MUTE, name,
2337                                HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT))) < 0)
2338                 return err;
2339         return 0;
2340 }
2341
2342 /* create input playback/capture controls for the given pin */
2343 static int new_analog_input(struct ad198x_spec *spec, hda_nid_t pin,
2344                             const char *ctlname, int boost)
2345 {
2346         char name[32];
2347         int err, idx;
2348
2349         sprintf(name, "%s Playback Volume", ctlname);
2350         idx = ad1988_pin_to_loopback_idx(pin);
2351         if ((err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2352                                HDA_COMPOSE_AMP_VAL(0x20, 3, idx, HDA_INPUT))) < 0)
2353                 return err;
2354         sprintf(name, "%s Playback Switch", ctlname);
2355         if ((err = add_control(spec, AD_CTL_WIDGET_MUTE, name,
2356                                HDA_COMPOSE_AMP_VAL(0x20, 3, idx, HDA_INPUT))) < 0)
2357                 return err;
2358         if (boost) {
2359                 hda_nid_t bnid;
2360                 idx = ad1988_pin_idx(pin);
2361                 bnid = ad1988_boost_nids[idx];
2362                 if (bnid) {
2363                         sprintf(name, "%s Boost", ctlname);
2364                         return add_control(spec, AD_CTL_WIDGET_VOL, name,
2365                                            HDA_COMPOSE_AMP_VAL(bnid, 3, idx, HDA_OUTPUT));
2366
2367                 }
2368         }
2369         return 0;
2370 }
2371
2372 /* create playback/capture controls for input pins */
2373 static int ad1988_auto_create_analog_input_ctls(struct ad198x_spec *spec,
2374                                                 const struct auto_pin_cfg *cfg)
2375 {
2376         struct hda_input_mux *imux = &spec->private_imux;
2377         int i, err;
2378
2379         for (i = 0; i < AUTO_PIN_LAST; i++) {
2380                 err = new_analog_input(spec, cfg->input_pins[i],
2381                                        auto_pin_cfg_labels[i],
2382                                        i <= AUTO_PIN_FRONT_MIC);
2383                 if (err < 0)
2384                         return err;
2385                 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
2386                 imux->items[imux->num_items].index = ad1988_pin_to_adc_idx(cfg->input_pins[i]);
2387                 imux->num_items++;
2388         }
2389         imux->items[imux->num_items].label = "Mix";
2390         imux->items[imux->num_items].index = 9;
2391         imux->num_items++;
2392
2393         if ((err = add_control(spec, AD_CTL_WIDGET_VOL,
2394                                "Analog Mix Playback Volume",
2395                                HDA_COMPOSE_AMP_VAL(0x21, 3, 0x0, HDA_OUTPUT))) < 0)
2396                 return err;
2397         if ((err = add_control(spec, AD_CTL_WIDGET_MUTE,
2398                                "Analog Mix Playback Switch",
2399                                HDA_COMPOSE_AMP_VAL(0x21, 3, 0x0, HDA_OUTPUT))) < 0)
2400                 return err;
2401
2402         return 0;
2403 }
2404
2405 static void ad1988_auto_set_output_and_unmute(struct hda_codec *codec,
2406                                               hda_nid_t nid, int pin_type,
2407                                               int dac_idx)
2408 {
2409         /* set as output */
2410         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
2411         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
2412         switch (nid) {
2413         case 0x11: /* port-A - DAC 04 */
2414                 snd_hda_codec_write(codec, 0x37, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2415                 break;
2416         case 0x14: /* port-B - DAC 06 */
2417                 snd_hda_codec_write(codec, 0x30, 0, AC_VERB_SET_CONNECT_SEL, 0x02);
2418                 break;
2419         case 0x15: /* port-C - DAC 05 */
2420                 snd_hda_codec_write(codec, 0x31, 0, AC_VERB_SET_CONNECT_SEL, 0x00);
2421                 break;
2422         case 0x17: /* port-E - DAC 0a */
2423                 snd_hda_codec_write(codec, 0x32, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2424                 break;
2425         case 0x13: /* mono - DAC 04 */
2426                 snd_hda_codec_write(codec, 0x36, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2427                 break;
2428         }
2429 }
2430
2431 static void ad1988_auto_init_multi_out(struct hda_codec *codec)
2432 {
2433         struct ad198x_spec *spec = codec->spec;
2434         int i;
2435
2436         for (i = 0; i < spec->autocfg.line_outs; i++) {
2437                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
2438                 ad1988_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
2439         }
2440 }
2441
2442 static void ad1988_auto_init_extra_out(struct hda_codec *codec)
2443 {
2444         struct ad198x_spec *spec = codec->spec;
2445         hda_nid_t pin;
2446
2447         pin = spec->autocfg.speaker_pins[0];
2448         if (pin) /* connect to front */
2449                 ad1988_auto_set_output_and_unmute(codec, pin, PIN_OUT, 0);
2450         pin = spec->autocfg.hp_pins[0];
2451         if (pin) /* connect to front */
2452                 ad1988_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
2453 }
2454
2455 static void ad1988_auto_init_analog_input(struct hda_codec *codec)
2456 {
2457         struct ad198x_spec *spec = codec->spec;
2458         int i, idx;
2459
2460         for (i = 0; i < AUTO_PIN_LAST; i++) {
2461                 hda_nid_t nid = spec->autocfg.input_pins[i];
2462                 if (! nid)
2463                         continue;
2464                 switch (nid) {
2465                 case 0x15: /* port-C */
2466                         snd_hda_codec_write(codec, 0x33, 0, AC_VERB_SET_CONNECT_SEL, 0x0);
2467                         break;
2468                 case 0x17: /* port-E */
2469                         snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_CONNECT_SEL, 0x0);
2470                         break;
2471                 }
2472                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2473                                     i <= AUTO_PIN_FRONT_MIC ? PIN_VREF80 : PIN_IN);
2474                 if (nid != AD1988_PIN_CD_NID)
2475                         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2476                                             AMP_OUT_MUTE);
2477                 idx = ad1988_pin_idx(nid);
2478                 if (ad1988_boost_nids[idx])
2479                         snd_hda_codec_write(codec, ad1988_boost_nids[idx], 0,
2480                                             AC_VERB_SET_AMP_GAIN_MUTE,
2481                                             AMP_OUT_ZERO);
2482         }
2483 }
2484
2485 /* parse the BIOS configuration and set up the alc_spec */
2486 /* return 1 if successful, 0 if the proper config is not found, or a negative error code */
2487 static int ad1988_parse_auto_config(struct hda_codec *codec)
2488 {
2489         struct ad198x_spec *spec = codec->spec;
2490         int err;
2491
2492         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
2493                 return err;
2494         if ((err = ad1988_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
2495                 return err;
2496         if (! spec->autocfg.line_outs)
2497                 return 0; /* can't find valid BIOS pin config */
2498         if ((err = ad1988_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
2499             (err = ad1988_auto_create_extra_out(codec,
2500                                                 spec->autocfg.speaker_pins[0],
2501                                                 "Speaker")) < 0 ||
2502             (err = ad1988_auto_create_extra_out(codec, spec->autocfg.hp_pins[0],
2503                                                 "Headphone")) < 0 ||
2504             (err = ad1988_auto_create_analog_input_ctls(spec, &spec->autocfg)) < 0)
2505                 return err;
2506
2507         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2508
2509         if (spec->autocfg.dig_out_pin)
2510                 spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2511         if (spec->autocfg.dig_in_pin)
2512                 spec->dig_in_nid = AD1988_SPDIF_IN;
2513
2514         if (spec->kctl_alloc)
2515                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
2516
2517         spec->init_verbs[spec->num_init_verbs++] = ad1988_6stack_init_verbs;
2518
2519         spec->input_mux = &spec->private_imux;
2520
2521         return 1;
2522 }
2523
2524 /* init callback for auto-configuration model -- overriding the default init */
2525 static int ad1988_auto_init(struct hda_codec *codec)
2526 {
2527         ad198x_init(codec);
2528         ad1988_auto_init_multi_out(codec);
2529         ad1988_auto_init_extra_out(codec);
2530         ad1988_auto_init_analog_input(codec);
2531         return 0;
2532 }
2533
2534
2535 /*
2536  */
2537
2538 static const char *ad1988_models[AD1988_MODEL_LAST] = {
2539         [AD1988_6STACK]         = "6stack",
2540         [AD1988_6STACK_DIG]     = "6stack-dig",
2541         [AD1988_3STACK]         = "3stack",
2542         [AD1988_3STACK_DIG]     = "3stack-dig",
2543         [AD1988_LAPTOP]         = "laptop",
2544         [AD1988_LAPTOP_DIG]     = "laptop-dig",
2545         [AD1988_AUTO]           = "auto",
2546 };
2547
2548 static struct snd_pci_quirk ad1988_cfg_tbl[] = {
2549         SND_PCI_QUIRK(0x1043, 0x81f6, "Asus M2N-SLI", AD1988_6STACK_DIG),
2550         SND_PCI_QUIRK(0x1043, 0x81ec, "Asus P5B-DLX", AD1988_6STACK_DIG),
2551         {}
2552 };
2553
2554 static int patch_ad1988(struct hda_codec *codec)
2555 {
2556         struct ad198x_spec *spec;
2557         int board_config;
2558
2559         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2560         if (spec == NULL)
2561                 return -ENOMEM;
2562
2563         codec->spec = spec;
2564
2565         if (is_rev2(codec))
2566                 snd_printk(KERN_INFO "patch_analog: AD1988A rev.2 is detected, enable workarounds\n");
2567
2568         board_config = snd_hda_check_board_config(codec, AD1988_MODEL_LAST,
2569                                                   ad1988_models, ad1988_cfg_tbl);
2570         if (board_config < 0) {
2571                 printk(KERN_INFO "hda_codec: Unknown model for AD1988, trying auto-probe from BIOS...\n");
2572                 board_config = AD1988_AUTO;
2573         }
2574
2575         if (board_config == AD1988_AUTO) {
2576                 /* automatic parse from the BIOS config */
2577                 int err = ad1988_parse_auto_config(codec);
2578                 if (err < 0) {
2579                         ad198x_free(codec);
2580                         return err;
2581                 } else if (! err) {
2582                         printk(KERN_INFO "hda_codec: Cannot set up configuration from BIOS.  Using 6-stack mode...\n");
2583                         board_config = AD1988_6STACK;
2584                 }
2585         }
2586
2587         switch (board_config) {
2588         case AD1988_6STACK:
2589         case AD1988_6STACK_DIG:
2590                 spec->multiout.max_channels = 8;
2591                 spec->multiout.num_dacs = 4;
2592                 if (is_rev2(codec))
2593                         spec->multiout.dac_nids = ad1988_6stack_dac_nids_rev2;
2594                 else
2595                         spec->multiout.dac_nids = ad1988_6stack_dac_nids;
2596                 spec->input_mux = &ad1988_6stack_capture_source;
2597                 spec->num_mixers = 2;
2598                 if (is_rev2(codec))
2599                         spec->mixers[0] = ad1988_6stack_mixers1_rev2;
2600                 else
2601                         spec->mixers[0] = ad1988_6stack_mixers1;
2602                 spec->mixers[1] = ad1988_6stack_mixers2;
2603                 spec->num_init_verbs = 1;
2604                 spec->init_verbs[0] = ad1988_6stack_init_verbs;
2605                 if (board_config == AD1988_6STACK_DIG) {
2606                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2607                         spec->dig_in_nid = AD1988_SPDIF_IN;
2608                 }
2609                 break;
2610         case AD1988_3STACK:
2611         case AD1988_3STACK_DIG:
2612                 spec->multiout.max_channels = 6;
2613                 spec->multiout.num_dacs = 3;
2614                 if (is_rev2(codec))
2615                         spec->multiout.dac_nids = ad1988_3stack_dac_nids_rev2;
2616                 else
2617                         spec->multiout.dac_nids = ad1988_3stack_dac_nids;
2618                 spec->input_mux = &ad1988_6stack_capture_source;
2619                 spec->channel_mode = ad1988_3stack_modes;
2620                 spec->num_channel_mode = ARRAY_SIZE(ad1988_3stack_modes);
2621                 spec->num_mixers = 2;
2622                 if (is_rev2(codec))
2623                         spec->mixers[0] = ad1988_3stack_mixers1_rev2;
2624                 else
2625                         spec->mixers[0] = ad1988_3stack_mixers1;
2626                 spec->mixers[1] = ad1988_3stack_mixers2;
2627                 spec->num_init_verbs = 1;
2628                 spec->init_verbs[0] = ad1988_3stack_init_verbs;
2629                 if (board_config == AD1988_3STACK_DIG)
2630                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2631                 break;
2632         case AD1988_LAPTOP:
2633         case AD1988_LAPTOP_DIG:
2634                 spec->multiout.max_channels = 2;
2635                 spec->multiout.num_dacs = 1;
2636                 spec->multiout.dac_nids = ad1988_3stack_dac_nids;
2637                 spec->input_mux = &ad1988_laptop_capture_source;
2638                 spec->num_mixers = 1;
2639                 spec->mixers[0] = ad1988_laptop_mixers;
2640                 spec->num_init_verbs = 1;
2641                 spec->init_verbs[0] = ad1988_laptop_init_verbs;
2642                 if (board_config == AD1988_LAPTOP_DIG)
2643                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2644                 break;
2645         }
2646
2647         spec->num_adc_nids = ARRAY_SIZE(ad1988_adc_nids);
2648         spec->adc_nids = ad1988_adc_nids;
2649         spec->capsrc_nids = ad1988_capsrc_nids;
2650         spec->mixers[spec->num_mixers++] = ad1988_capture_mixers;
2651         spec->init_verbs[spec->num_init_verbs++] = ad1988_capture_init_verbs;
2652         if (spec->multiout.dig_out_nid) {
2653                 spec->mixers[spec->num_mixers++] = ad1988_spdif_out_mixers;
2654                 spec->init_verbs[spec->num_init_verbs++] = ad1988_spdif_init_verbs;
2655         }
2656         if (spec->dig_in_nid)
2657                 spec->mixers[spec->num_mixers++] = ad1988_spdif_in_mixers;
2658
2659         codec->patch_ops = ad198x_patch_ops;
2660         switch (board_config) {
2661         case AD1988_AUTO:
2662                 codec->patch_ops.init = ad1988_auto_init;
2663                 break;
2664         case AD1988_LAPTOP:
2665         case AD1988_LAPTOP_DIG:
2666                 codec->patch_ops.unsol_event = ad1988_laptop_unsol_event;
2667                 break;
2668         }
2669
2670         return 0;
2671 }
2672
2673
2674 /*
2675  * AD1884 / AD1984
2676  *
2677  * port-B - front line/mic-in
2678  * port-E - aux in/out
2679  * port-F - aux in/out
2680  * port-C - rear line/mic-in
2681  * port-D - rear line/hp-out
2682  * port-A - front line/hp-out
2683  *
2684  * AD1984 = AD1884 + two digital mic-ins
2685  *
2686  * FIXME:
2687  * For simplicity, we share the single DAC for both HP and line-outs
2688  * right now.  The inidividual playbacks could be easily implemented,
2689  * but no build-up framework is given, so far.
2690  */
2691
2692 static hda_nid_t ad1884_dac_nids[1] = {
2693         0x04,
2694 };
2695
2696 static hda_nid_t ad1884_adc_nids[2] = {
2697         0x08, 0x09,
2698 };
2699
2700 static hda_nid_t ad1884_capsrc_nids[2] = {
2701         0x0c, 0x0d,
2702 };
2703
2704 #define AD1884_SPDIF_OUT        0x02
2705
2706 static struct hda_input_mux ad1884_capture_source = {
2707         .num_items = 4,
2708         .items = {
2709                 { "Front Mic", 0x0 },
2710                 { "Mic", 0x1 },
2711                 { "CD", 0x2 },
2712                 { "Mix", 0x3 },
2713         },
2714 };
2715
2716 static struct snd_kcontrol_new ad1884_base_mixers[] = {
2717         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2718         /* HDA_CODEC_VOLUME_IDX("PCM Playback Volume", 1, 0x03, 0x0, HDA_OUTPUT), */
2719         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
2720         HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
2721         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
2722         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
2723         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
2724         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
2725         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
2726         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
2727         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x02, HDA_INPUT),
2728         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x02, HDA_INPUT),
2729         /*
2730         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x20, 0x03, HDA_INPUT),
2731         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x20, 0x03, HDA_INPUT),
2732         HDA_CODEC_VOLUME("Digital Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
2733         HDA_CODEC_MUTE("Digital Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
2734         */
2735         HDA_CODEC_VOLUME("Mic Boost", 0x15, 0x0, HDA_INPUT),
2736         HDA_CODEC_VOLUME("Front Mic Boost", 0x14, 0x0, HDA_INPUT),
2737         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
2738         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
2739         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
2740         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
2741         {
2742                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2743                 /* The multiple "Capture Source" controls confuse alsamixer
2744                  * So call somewhat different..
2745                  * FIXME: the controls appear in the "playback" view!
2746                  */
2747                 /* .name = "Capture Source", */
2748                 .name = "Input Source",
2749                 .count = 2,
2750                 .info = ad198x_mux_enum_info,
2751                 .get = ad198x_mux_enum_get,
2752                 .put = ad198x_mux_enum_put,
2753         },
2754         /* SPDIF controls */
2755         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
2756         {
2757                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2758                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
2759                 /* identical with ad1983 */
2760                 .info = ad1983_spdif_route_info,
2761                 .get = ad1983_spdif_route_get,
2762                 .put = ad1983_spdif_route_put,
2763         },
2764         { } /* end */
2765 };
2766
2767 static struct snd_kcontrol_new ad1984_dmic_mixers[] = {
2768         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x05, 0x0, HDA_INPUT),
2769         HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x05, 0x0, HDA_INPUT),
2770         HDA_CODEC_VOLUME_IDX("Digital Mic Capture Volume", 1, 0x06, 0x0,
2771                              HDA_INPUT),
2772         HDA_CODEC_MUTE_IDX("Digital Mic Capture Switch", 1, 0x06, 0x0,
2773                            HDA_INPUT),
2774         { } /* end */
2775 };
2776
2777 /*
2778  * initialization verbs
2779  */
2780 static struct hda_verb ad1884_init_verbs[] = {
2781         /* DACs; mute as default */
2782         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2783         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2784         /* Port-A (HP) mixer */
2785         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2786         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2787         /* Port-A pin */
2788         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2789         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2790         /* HP selector - select DAC2 */
2791         {0x22, AC_VERB_SET_CONNECT_SEL, 0x1},
2792         /* Port-D (Line-out) mixer */
2793         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2794         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2795         /* Port-D pin */
2796         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2797         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2798         /* Mono-out mixer */
2799         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2800         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2801         /* Mono-out pin */
2802         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2803         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2804         /* Mono selector */
2805         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2806         /* Port-B (front mic) pin */
2807         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2808         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2809         /* Port-C (rear mic) pin */
2810         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2811         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2812         /* Analog mixer; mute as default */
2813         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2814         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2815         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2816         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2817         /* Analog Mix output amp */
2818         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
2819         /* SPDIF output selector */
2820         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
2821         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
2822         { } /* end */
2823 };
2824
2825 static int patch_ad1884(struct hda_codec *codec)
2826 {
2827         struct ad198x_spec *spec;
2828
2829         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2830         if (spec == NULL)
2831                 return -ENOMEM;
2832
2833         mutex_init(&spec->amp_mutex);
2834         codec->spec = spec;
2835
2836         spec->multiout.max_channels = 2;
2837         spec->multiout.num_dacs = ARRAY_SIZE(ad1884_dac_nids);
2838         spec->multiout.dac_nids = ad1884_dac_nids;
2839         spec->multiout.dig_out_nid = AD1884_SPDIF_OUT;
2840         spec->num_adc_nids = ARRAY_SIZE(ad1884_adc_nids);
2841         spec->adc_nids = ad1884_adc_nids;
2842         spec->capsrc_nids = ad1884_capsrc_nids;
2843         spec->input_mux = &ad1884_capture_source;
2844         spec->num_mixers = 1;
2845         spec->mixers[0] = ad1884_base_mixers;
2846         spec->num_init_verbs = 1;
2847         spec->init_verbs[0] = ad1884_init_verbs;
2848         spec->spdif_route = 0;
2849
2850         codec->patch_ops = ad198x_patch_ops;
2851
2852         return 0;
2853 }
2854
2855 /*
2856  * Lenovo Thinkpad T61/X61
2857  */
2858 static struct hda_input_mux ad1984_thinkpad_capture_source = {
2859         .num_items = 3,
2860         .items = {
2861                 { "Mic", 0x0 },
2862                 { "Internal Mic", 0x1 },
2863                 { "Mix", 0x3 },
2864         },
2865 };
2866
2867 static struct snd_kcontrol_new ad1984_thinkpad_mixers[] = {
2868         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2869         /* HDA_CODEC_VOLUME_IDX("PCM Playback Volume", 1, 0x03, 0x0, HDA_OUTPUT), */
2870         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
2871         HDA_CODEC_MUTE("Speaker Playback Switch", 0x12, 0x0, HDA_OUTPUT),
2872         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
2873         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
2874         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
2875         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
2876         HDA_CODEC_VOLUME("Docking Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
2877         HDA_CODEC_MUTE("Docking Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
2878         HDA_CODEC_VOLUME("Mic Boost", 0x14, 0x0, HDA_INPUT),
2879         HDA_CODEC_VOLUME("Internal Mic Boost", 0x15, 0x0, HDA_INPUT),
2880         HDA_CODEC_VOLUME("Docking Mic Boost", 0x25, 0x0, HDA_OUTPUT),
2881         HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x03, HDA_INPUT),
2882         HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x03, HDA_INPUT),
2883         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
2884         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
2885         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
2886         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
2887         {
2888                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2889                 /* The multiple "Capture Source" controls confuse alsamixer
2890                  * So call somewhat different..
2891                  * FIXME: the controls appear in the "playback" view!
2892                  */
2893                 /* .name = "Capture Source", */
2894                 .name = "Input Source",
2895                 .count = 2,
2896                 .info = ad198x_mux_enum_info,
2897                 .get = ad198x_mux_enum_get,
2898                 .put = ad198x_mux_enum_put,
2899         },
2900         { } /* end */
2901 };
2902
2903 /* additional verbs */
2904 static struct hda_verb ad1984_thinkpad_init_verbs[] = {
2905         /* Port-E (docking station mic) pin */
2906         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2907         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2908         /* docking mic boost */
2909         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2910         /* Analog mixer - docking mic; mute as default */
2911         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2912         /* enable EAPD bit */
2913         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x02},
2914         { } /* end */
2915 };
2916
2917 /* Digial MIC ADC NID 0x05 + 0x06 */
2918 static int ad1984_pcm_dmic_prepare(struct hda_pcm_stream *hinfo,
2919                                    struct hda_codec *codec,
2920                                    unsigned int stream_tag,
2921                                    unsigned int format,
2922                                    struct snd_pcm_substream *substream)
2923 {
2924         snd_hda_codec_setup_stream(codec, 0x05 + substream->number,
2925                                    stream_tag, 0, format);
2926         return 0;
2927 }
2928
2929 static int ad1984_pcm_dmic_cleanup(struct hda_pcm_stream *hinfo,
2930                                    struct hda_codec *codec,
2931                                    struct snd_pcm_substream *substream)
2932 {
2933         snd_hda_codec_setup_stream(codec, 0x05 + substream->number,
2934                                    0, 0, 0);
2935         return 0;
2936 }
2937
2938 static struct hda_pcm_stream ad1984_pcm_dmic_capture = {
2939         .substreams = 2,
2940         .channels_min = 2,
2941         .channels_max = 2,
2942         .nid = 0x05,
2943         .ops = {
2944                 .prepare = ad1984_pcm_dmic_prepare,
2945                 .cleanup = ad1984_pcm_dmic_cleanup
2946         },
2947 };
2948
2949 static int ad1984_build_pcms(struct hda_codec *codec)
2950 {
2951         struct ad198x_spec *spec = codec->spec;
2952         struct hda_pcm *info;
2953         int err;
2954
2955         err = ad198x_build_pcms(codec);
2956         if (err < 0)
2957                 return err;
2958
2959         info = spec->pcm_rec + codec->num_pcms;
2960         codec->num_pcms++;
2961         info->name = "AD1984 Digital Mic";
2962         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad1984_pcm_dmic_capture;
2963         return 0;
2964 }
2965
2966 /* models */
2967 enum {
2968         AD1984_BASIC,
2969         AD1984_THINKPAD,
2970         AD1984_MODELS
2971 };
2972
2973 static const char *ad1984_models[AD1984_MODELS] = {
2974         [AD1984_BASIC]          = "basic",
2975         [AD1984_THINKPAD]       = "thinkpad",
2976 };
2977
2978 static struct snd_pci_quirk ad1984_cfg_tbl[] = {
2979         /* Lenovo Thinkpad T61/X61 */
2980         SND_PCI_QUIRK(0x17aa, 0, "Lenovo Thinkpad", AD1984_THINKPAD),
2981         {}
2982 };
2983
2984 static int patch_ad1984(struct hda_codec *codec)
2985 {
2986         struct ad198x_spec *spec;
2987         int board_config, err;
2988
2989         err = patch_ad1884(codec);
2990         if (err < 0)
2991                 return err;
2992         spec = codec->spec;
2993         board_config = snd_hda_check_board_config(codec, AD1984_MODELS,
2994                                                   ad1984_models, ad1984_cfg_tbl);
2995         switch (board_config) {
2996         case AD1984_BASIC:
2997                 /* additional digital mics */
2998                 spec->mixers[spec->num_mixers++] = ad1984_dmic_mixers;
2999                 codec->patch_ops.build_pcms = ad1984_build_pcms;
3000                 break;
3001         case AD1984_THINKPAD:
3002                 spec->multiout.dig_out_nid = 0;
3003                 spec->input_mux = &ad1984_thinkpad_capture_source;
3004                 spec->mixers[0] = ad1984_thinkpad_mixers;
3005                 spec->init_verbs[spec->num_init_verbs++] = ad1984_thinkpad_init_verbs;
3006                 break;
3007         }
3008         return 0;
3009 }
3010
3011
3012 /*
3013  * AD1882
3014  *
3015  * port-A - front hp-out
3016  * port-B - front mic-in
3017  * port-C - rear line-in, shared surr-out (3stack)
3018  * port-D - rear line-out
3019  * port-E - rear mic-in, shared clfe-out (3stack)
3020  * port-F - rear surr-out (6stack)
3021  * port-G - rear clfe-out (6stack)
3022  */
3023
3024 static hda_nid_t ad1882_dac_nids[3] = {
3025         0x04, 0x03, 0x05
3026 };
3027
3028 static hda_nid_t ad1882_adc_nids[2] = {
3029         0x08, 0x09,
3030 };
3031
3032 static hda_nid_t ad1882_capsrc_nids[2] = {
3033         0x0c, 0x0d,
3034 };
3035
3036 #define AD1882_SPDIF_OUT        0x02
3037
3038 /* list: 0x11, 0x39, 0x3a, 0x18, 0x3c, 0x3b, 0x12, 0x20 */
3039 static struct hda_input_mux ad1882_capture_source = {
3040         .num_items = 5,
3041         .items = {
3042                 { "Front Mic", 0x1 },
3043                 { "Mic", 0x4 },
3044                 { "Line", 0x2 },
3045                 { "CD", 0x3 },
3046                 { "Mix", 0x7 },
3047         },
3048 };
3049
3050 static struct snd_kcontrol_new ad1882_base_mixers[] = {
3051         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
3052         HDA_CODEC_VOLUME("Surround Playback Volume", 0x03, 0x0, HDA_OUTPUT),
3053         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
3054         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
3055         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
3056         HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3057         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
3058         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
3059         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3060         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3061         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
3062         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
3063         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x04, HDA_INPUT),
3064         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x04, HDA_INPUT),
3065         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x06, HDA_INPUT),
3066         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x06, HDA_INPUT),
3067         HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x07, HDA_INPUT),
3068         HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x07, HDA_INPUT),
3069         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
3070         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
3071         HDA_CODEC_VOLUME("Line-In Boost", 0x3a, 0x0, HDA_OUTPUT),
3072         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3073         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3074         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3075         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3076         {
3077                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3078                 /* The multiple "Capture Source" controls confuse alsamixer
3079                  * So call somewhat different..
3080                  * FIXME: the controls appear in the "playback" view!
3081                  */
3082                 /* .name = "Capture Source", */
3083                 .name = "Input Source",
3084                 .count = 2,
3085                 .info = ad198x_mux_enum_info,
3086                 .get = ad198x_mux_enum_get,
3087                 .put = ad198x_mux_enum_put,
3088         },
3089         /* SPDIF controls */
3090         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
3091         {
3092                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3093                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
3094                 /* identical with ad1983 */
3095                 .info = ad1983_spdif_route_info,
3096                 .get = ad1983_spdif_route_get,
3097                 .put = ad1983_spdif_route_put,
3098         },
3099         { } /* end */
3100 };
3101
3102 static struct snd_kcontrol_new ad1882_3stack_mixers[] = {
3103         HDA_CODEC_MUTE("Surround Playback Switch", 0x15, 0x0, HDA_OUTPUT),
3104         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x17, 1, 0x0, HDA_OUTPUT),
3105         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x17, 2, 0x0, HDA_OUTPUT),
3106         {
3107                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3108                 .name = "Channel Mode",
3109                 .info = ad198x_ch_mode_info,
3110                 .get = ad198x_ch_mode_get,
3111                 .put = ad198x_ch_mode_put,
3112         },
3113         { } /* end */
3114 };
3115
3116 static struct snd_kcontrol_new ad1882_6stack_mixers[] = {
3117         HDA_CODEC_MUTE("Surround Playback Switch", 0x16, 0x0, HDA_OUTPUT),
3118         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x24, 1, 0x0, HDA_OUTPUT),
3119         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x24, 2, 0x0, HDA_OUTPUT),
3120         { } /* end */
3121 };
3122
3123 static struct hda_verb ad1882_ch2_init[] = {
3124         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3125         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3126         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3127         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3128         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3129         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3130         { } /* end */
3131 };
3132
3133 static struct hda_verb ad1882_ch4_init[] = {
3134         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3135         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3136         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3137         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3138         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3139         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3140         { } /* end */
3141 };
3142
3143 static struct hda_verb ad1882_ch6_init[] = {
3144         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3145         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3146         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3147         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3148         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3149         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3150         { } /* end */
3151 };
3152
3153 static struct hda_channel_mode ad1882_modes[3] = {
3154         { 2, ad1882_ch2_init },
3155         { 4, ad1882_ch4_init },
3156         { 6, ad1882_ch6_init },
3157 };
3158
3159 /*
3160  * initialization verbs
3161  */
3162 static struct hda_verb ad1882_init_verbs[] = {
3163         /* DACs; mute as default */
3164         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3165         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3166         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3167         /* Port-A (HP) mixer */
3168         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3169         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3170         /* Port-A pin */
3171         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3172         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3173         /* HP selector - select DAC2 */
3174         {0x37, AC_VERB_SET_CONNECT_SEL, 0x1},
3175         /* Port-D (Line-out) mixer */
3176         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3177         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3178         /* Port-D pin */
3179         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3180         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3181         /* Mono-out mixer */
3182         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3183         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3184         /* Mono-out pin */
3185         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3186         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3187         /* Port-B (front mic) pin */
3188         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3189         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3190         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
3191         /* Port-C (line-in) pin */
3192         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3193         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3194         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
3195         /* Port-C mixer - mute as input */
3196         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3197         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3198         /* Port-E (mic-in) pin */
3199         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3200         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3201         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
3202         /* Port-E mixer - mute as input */
3203         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3204         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3205         /* Port-F (surround) */
3206         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3207         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3208         /* Port-G (CLFE) */
3209         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3210         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3211         /* Analog mixer; mute as default */
3212         /* list: 0x39, 0x3a, 0x11, 0x12, 0x3c, 0x3b, 0x18, 0x1a */
3213         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3214         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3215         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3216         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3217         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3218         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
3219         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
3220         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
3221         /* Analog Mix output amp */
3222         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
3223         /* SPDIF output selector */
3224         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
3225         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
3226         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
3227         { } /* end */
3228 };
3229
3230 /* models */
3231 enum {
3232         AD1882_3STACK,
3233         AD1882_6STACK,
3234         AD1882_MODELS
3235 };
3236
3237 static const char *ad1882_models[AD1986A_MODELS] = {
3238         [AD1882_3STACK]         = "3stack",
3239         [AD1882_6STACK]         = "6stack",
3240 };
3241
3242
3243 static int patch_ad1882(struct hda_codec *codec)
3244 {
3245         struct ad198x_spec *spec;
3246         int board_config;
3247
3248         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3249         if (spec == NULL)
3250                 return -ENOMEM;
3251
3252         mutex_init(&spec->amp_mutex);
3253         codec->spec = spec;
3254
3255         spec->multiout.max_channels = 6;
3256         spec->multiout.num_dacs = 3;
3257         spec->multiout.dac_nids = ad1882_dac_nids;
3258         spec->multiout.dig_out_nid = AD1882_SPDIF_OUT;
3259         spec->num_adc_nids = ARRAY_SIZE(ad1882_adc_nids);
3260         spec->adc_nids = ad1882_adc_nids;
3261         spec->capsrc_nids = ad1882_capsrc_nids;
3262         spec->input_mux = &ad1882_capture_source;
3263         spec->num_mixers = 1;
3264         spec->mixers[0] = ad1882_base_mixers;
3265         spec->num_init_verbs = 1;
3266         spec->init_verbs[0] = ad1882_init_verbs;
3267         spec->spdif_route = 0;
3268
3269         codec->patch_ops = ad198x_patch_ops;
3270
3271         /* override some parameters */
3272         board_config = snd_hda_check_board_config(codec, AD1882_MODELS,
3273                                                   ad1882_models, NULL);
3274         switch (board_config) {
3275         default:
3276         case AD1882_3STACK:
3277                 spec->num_mixers = 2;
3278                 spec->mixers[1] = ad1882_3stack_mixers;
3279                 spec->channel_mode = ad1882_modes;
3280                 spec->num_channel_mode = ARRAY_SIZE(ad1882_modes);
3281                 spec->need_dac_fix = 1;
3282                 spec->multiout.max_channels = 2;
3283                 spec->multiout.num_dacs = 1;
3284                 break;
3285         case AD1882_6STACK:
3286                 spec->num_mixers = 2;
3287                 spec->mixers[1] = ad1882_6stack_mixers;
3288                 break;
3289         }
3290         return 0;
3291 }
3292
3293
3294 /*
3295  * patch entries
3296  */
3297 struct hda_codec_preset snd_hda_preset_analog[] = {
3298         { .id = 0x11d41882, .name = "AD1882", .patch = patch_ad1882 },
3299         { .id = 0x11d41884, .name = "AD1884", .patch = patch_ad1884 },
3300         { .id = 0x11d41981, .name = "AD1981", .patch = patch_ad1981 },
3301         { .id = 0x11d41983, .name = "AD1983", .patch = patch_ad1983 },
3302         { .id = 0x11d41984, .name = "AD1984", .patch = patch_ad1984 },
3303         { .id = 0x11d41986, .name = "AD1986A", .patch = patch_ad1986a },
3304         { .id = 0x11d41988, .name = "AD1988", .patch = patch_ad1988 },
3305         { .id = 0x11d4198b, .name = "AD1988B", .patch = patch_ad1988 },
3306         {} /* terminator */
3307 };