f9390a544ea4abb9de5bbd06d2722b63c6fa8739
[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_stereo(codec, 0x06, HDA_OUTPUT, 0,
1124                                  HDA_AMP_MUTE,
1125                                  spec->cur_eapd ? 0 : HDA_AMP_MUTE);
1126         return 1;
1127 }
1128
1129 /* bind volumes of both NID 0x05 and 0x06 */
1130 static struct hda_bind_ctls ad1981_hp_bind_master_vol = {
1131         .ops = &snd_hda_bind_vol,
1132         .values = {
1133                 HDA_COMPOSE_AMP_VAL(0x05, 3, 0, HDA_OUTPUT),
1134                 HDA_COMPOSE_AMP_VAL(0x06, 3, 0, HDA_OUTPUT),
1135                 0
1136         },
1137 };
1138
1139 /* mute internal speaker if HP is plugged */
1140 static void ad1981_hp_automute(struct hda_codec *codec)
1141 {
1142         unsigned int present;
1143
1144         present = snd_hda_codec_read(codec, 0x06, 0,
1145                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1146         snd_hda_codec_amp_stereo(codec, 0x05, HDA_OUTPUT, 0,
1147                                  HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
1148 }
1149
1150 /* toggle input of built-in and mic jack appropriately */
1151 static void ad1981_hp_automic(struct hda_codec *codec)
1152 {
1153         static struct hda_verb mic_jack_on[] = {
1154                 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1155                 {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1156                 {}
1157         };
1158         static struct hda_verb mic_jack_off[] = {
1159                 {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1160                 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1161                 {}
1162         };
1163         unsigned int present;
1164
1165         present = snd_hda_codec_read(codec, 0x08, 0,
1166                                  AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1167         if (present)
1168                 snd_hda_sequence_write(codec, mic_jack_on);
1169         else
1170                 snd_hda_sequence_write(codec, mic_jack_off);
1171 }
1172
1173 /* unsolicited event for HP jack sensing */
1174 static void ad1981_hp_unsol_event(struct hda_codec *codec,
1175                                   unsigned int res)
1176 {
1177         res >>= 26;
1178         switch (res) {
1179         case AD1981_HP_EVENT:
1180                 ad1981_hp_automute(codec);
1181                 break;
1182         case AD1981_MIC_EVENT:
1183                 ad1981_hp_automic(codec);
1184                 break;
1185         }
1186 }
1187
1188 static struct hda_input_mux ad1981_hp_capture_source = {
1189         .num_items = 3,
1190         .items = {
1191                 { "Mic", 0x0 },
1192                 { "Docking-Station", 0x1 },
1193                 { "Mix", 0x2 },
1194         },
1195 };
1196
1197 static struct snd_kcontrol_new ad1981_hp_mixers[] = {
1198         HDA_BIND_VOL("Master Playback Volume", &ad1981_hp_bind_master_vol),
1199         {
1200                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1201                 .name = "Master Playback Switch",
1202                 .info = ad198x_eapd_info,
1203                 .get = ad198x_eapd_get,
1204                 .put = ad1981_hp_master_sw_put,
1205                 .private_value = 0x05,
1206         },
1207         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1208         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1209 #if 0
1210         /* FIXME: analog mic/line loopback doesn't work with my tests...
1211          *        (although recording is OK)
1212          */
1213         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1214         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1215         HDA_CODEC_VOLUME("Docking-Station Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1216         HDA_CODEC_MUTE("Docking-Station Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1217         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
1218         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
1219         /* FIXME: does this laptop have analog CD connection? */
1220         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1221         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1222 #endif
1223         HDA_CODEC_VOLUME("Mic Boost", 0x08, 0x0, HDA_INPUT),
1224         HDA_CODEC_VOLUME("Internal Mic Boost", 0x18, 0x0, HDA_INPUT),
1225         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1226         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1227         {
1228                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1229                 .name = "Capture Source",
1230                 .info = ad198x_mux_enum_info,
1231                 .get = ad198x_mux_enum_get,
1232                 .put = ad198x_mux_enum_put,
1233         },
1234         { } /* end */
1235 };
1236
1237 /* initialize jack-sensing, too */
1238 static int ad1981_hp_init(struct hda_codec *codec)
1239 {
1240         ad198x_init(codec);
1241         ad1981_hp_automute(codec);
1242         ad1981_hp_automic(codec);
1243         return 0;
1244 }
1245
1246 /* configuration for Toshiba Laptops */
1247 static struct hda_verb ad1981_toshiba_init_verbs[] = {
1248         {0x05, AC_VERB_SET_EAPD_BTLENABLE, 0x01 }, /* default on */
1249         /* pin sensing on HP and Mic jacks */
1250         {0x06, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_HP_EVENT},
1251         {0x08, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_MIC_EVENT},
1252         {}
1253 };
1254
1255 static struct snd_kcontrol_new ad1981_toshiba_mixers[] = {
1256         HDA_CODEC_VOLUME("Amp Volume", 0x1a, 0x0, HDA_OUTPUT),
1257         HDA_CODEC_MUTE("Amp Switch", 0x1a, 0x0, HDA_OUTPUT),
1258         { }
1259 };
1260
1261 /* configuration for Lenovo Thinkpad T60 */
1262 static struct snd_kcontrol_new ad1981_thinkpad_mixers[] = {
1263         HDA_CODEC_VOLUME("Master Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1264         HDA_CODEC_MUTE("Master Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1265         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1266         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1267         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1268         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1269         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1270         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1271         HDA_CODEC_VOLUME("Mic Boost", 0x08, 0x0, HDA_INPUT),
1272         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1273         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1274         {
1275                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1276                 .name = "Capture Source",
1277                 .info = ad198x_mux_enum_info,
1278                 .get = ad198x_mux_enum_get,
1279                 .put = ad198x_mux_enum_put,
1280         },
1281         /* identical with AD1983 */
1282         {
1283                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1284                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1285                 .info = ad1983_spdif_route_info,
1286                 .get = ad1983_spdif_route_get,
1287                 .put = ad1983_spdif_route_put,
1288         },
1289         { } /* end */
1290 };
1291
1292 static struct hda_input_mux ad1981_thinkpad_capture_source = {
1293         .num_items = 3,
1294         .items = {
1295                 { "Mic", 0x0 },
1296                 { "Mix", 0x2 },
1297                 { "CD", 0x4 },
1298         },
1299 };
1300
1301 /* models */
1302 enum {
1303         AD1981_BASIC,
1304         AD1981_HP,
1305         AD1981_THINKPAD,
1306         AD1981_TOSHIBA,
1307         AD1981_MODELS
1308 };
1309
1310 static const char *ad1981_models[AD1981_MODELS] = {
1311         [AD1981_HP]             = "hp",
1312         [AD1981_THINKPAD]       = "thinkpad",
1313         [AD1981_BASIC]          = "basic",
1314         [AD1981_TOSHIBA]        = "toshiba"
1315 };
1316
1317 static struct snd_pci_quirk ad1981_cfg_tbl[] = {
1318         /* All HP models */
1319         SND_PCI_QUIRK(0x103c, 0, "HP nx", AD1981_HP),
1320         /* HP nx6320 (reversed SSID, H/W bug) */
1321         SND_PCI_QUIRK(0x30b0, 0x103c, "HP nx6320", AD1981_HP),
1322         /* Lenovo Thinkpad T60/X60/Z6xx */
1323         SND_PCI_QUIRK(0x17aa, 0, "Lenovo Thinkpad", AD1981_THINKPAD),
1324         SND_PCI_QUIRK(0x1014, 0x0597, "Lenovo Z60", AD1981_THINKPAD),
1325         SND_PCI_QUIRK(0x1179, 0x0001, "Toshiba U205", AD1981_TOSHIBA),
1326         {}
1327 };
1328
1329 static int patch_ad1981(struct hda_codec *codec)
1330 {
1331         struct ad198x_spec *spec;
1332         int board_config;
1333
1334         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1335         if (spec == NULL)
1336                 return -ENOMEM;
1337
1338         codec->spec = spec;
1339
1340         spec->multiout.max_channels = 2;
1341         spec->multiout.num_dacs = ARRAY_SIZE(ad1981_dac_nids);
1342         spec->multiout.dac_nids = ad1981_dac_nids;
1343         spec->multiout.dig_out_nid = AD1981_SPDIF_OUT;
1344         spec->num_adc_nids = 1;
1345         spec->adc_nids = ad1981_adc_nids;
1346         spec->capsrc_nids = ad1981_capsrc_nids;
1347         spec->input_mux = &ad1981_capture_source;
1348         spec->num_mixers = 1;
1349         spec->mixers[0] = ad1981_mixers;
1350         spec->num_init_verbs = 1;
1351         spec->init_verbs[0] = ad1981_init_verbs;
1352         spec->spdif_route = 0;
1353
1354         codec->patch_ops = ad198x_patch_ops;
1355
1356         /* override some parameters */
1357         board_config = snd_hda_check_board_config(codec, AD1981_MODELS,
1358                                                   ad1981_models,
1359                                                   ad1981_cfg_tbl);
1360         switch (board_config) {
1361         case AD1981_HP:
1362                 spec->mixers[0] = ad1981_hp_mixers;
1363                 spec->num_init_verbs = 2;
1364                 spec->init_verbs[1] = ad1981_hp_init_verbs;
1365                 spec->multiout.dig_out_nid = 0;
1366                 spec->input_mux = &ad1981_hp_capture_source;
1367
1368                 codec->patch_ops.init = ad1981_hp_init;
1369                 codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
1370                 break;
1371         case AD1981_THINKPAD:
1372                 spec->mixers[0] = ad1981_thinkpad_mixers;
1373                 spec->input_mux = &ad1981_thinkpad_capture_source;
1374                 break;
1375         case AD1981_TOSHIBA:
1376                 spec->mixers[0] = ad1981_hp_mixers;
1377                 spec->mixers[1] = ad1981_toshiba_mixers;
1378                 spec->num_init_verbs = 2;
1379                 spec->init_verbs[1] = ad1981_toshiba_init_verbs;
1380                 spec->multiout.dig_out_nid = 0;
1381                 spec->input_mux = &ad1981_hp_capture_source;
1382                 codec->patch_ops.init = ad1981_hp_init;
1383                 codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
1384                 break;
1385         }
1386         return 0;
1387 }
1388
1389
1390 /*
1391  * AD1988
1392  *
1393  * Output pins and routes
1394  *
1395  *        Pin               Mix     Sel     DAC (*)
1396  * port-A 0x11 (mute/hp) <- 0x22 <- 0x37 <- 03/04/06
1397  * port-B 0x14 (mute/hp) <- 0x2b <- 0x30 <- 03/04/06
1398  * port-C 0x15 (mute)    <- 0x2c <- 0x31 <- 05/0a
1399  * port-D 0x12 (mute/hp) <- 0x29         <- 04
1400  * port-E 0x17 (mute/hp) <- 0x26 <- 0x32 <- 05/0a
1401  * port-F 0x16 (mute)    <- 0x2a         <- 06
1402  * port-G 0x24 (mute)    <- 0x27         <- 05
1403  * port-H 0x25 (mute)    <- 0x28         <- 0a
1404  * mono   0x13 (mute/amp)<- 0x1e <- 0x36 <- 03/04/06
1405  *
1406  * DAC0 = 03h, DAC1 = 04h, DAC2 = 05h, DAC3 = 06h, DAC4 = 0ah
1407  * (*) DAC2/3/4 are swapped to DAC3/4/2 on AD198A rev.2 due to a h/w bug.
1408  *
1409  * Input pins and routes
1410  *
1411  *        pin     boost   mix input # / adc input #
1412  * port-A 0x11 -> 0x38 -> mix 2, ADC 0
1413  * port-B 0x14 -> 0x39 -> mix 0, ADC 1
1414  * port-C 0x15 -> 0x3a -> 33:0 - mix 1, ADC 2
1415  * port-D 0x12 -> 0x3d -> mix 3, ADC 8
1416  * port-E 0x17 -> 0x3c -> 34:0 - mix 4, ADC 4
1417  * port-F 0x16 -> 0x3b -> mix 5, ADC 3
1418  * port-G 0x24 -> N/A  -> 33:1 - mix 1, 34:1 - mix 4, ADC 6
1419  * port-H 0x25 -> N/A  -> 33:2 - mix 1, 34:2 - mix 4, ADC 7
1420  *
1421  *
1422  * DAC assignment
1423  *   6stack - front/surr/CLFE/side/opt DACs - 04/06/05/0a/03
1424  *   3stack - front/surr/CLFE/opt DACs - 04/05/0a/03
1425  *
1426  * Inputs of Analog Mix (0x20)
1427  *   0:Port-B (front mic)
1428  *   1:Port-C/G/H (line-in)
1429  *   2:Port-A
1430  *   3:Port-D (line-in/2)
1431  *   4:Port-E/G/H (mic-in)
1432  *   5:Port-F (mic2-in)
1433  *   6:CD
1434  *   7:Beep
1435  *
1436  * ADC selection
1437  *   0:Port-A
1438  *   1:Port-B (front mic-in)
1439  *   2:Port-C (line-in)
1440  *   3:Port-F (mic2-in)
1441  *   4:Port-E (mic-in)
1442  *   5:CD
1443  *   6:Port-G
1444  *   7:Port-H
1445  *   8:Port-D (line-in/2)
1446  *   9:Mix
1447  *
1448  * Proposed pin assignments by the datasheet
1449  *
1450  * 6-stack
1451  * Port-A front headphone
1452  *      B front mic-in
1453  *      C rear line-in
1454  *      D rear front-out
1455  *      E rear mic-in
1456  *      F rear surround
1457  *      G rear CLFE
1458  *      H rear side
1459  *
1460  * 3-stack
1461  * Port-A front headphone
1462  *      B front mic
1463  *      C rear line-in/surround
1464  *      D rear front-out
1465  *      E rear mic-in/CLFE
1466  *
1467  * laptop
1468  * Port-A headphone
1469  *      B mic-in
1470  *      C docking station
1471  *      D internal speaker (with EAPD)
1472  *      E/F quad mic array
1473  */
1474
1475
1476 /* models */
1477 enum {
1478         AD1988_6STACK,
1479         AD1988_6STACK_DIG,
1480         AD1988_3STACK,
1481         AD1988_3STACK_DIG,
1482         AD1988_LAPTOP,
1483         AD1988_LAPTOP_DIG,
1484         AD1988_AUTO,
1485         AD1988_MODEL_LAST,
1486 };
1487
1488 /* reivision id to check workarounds */
1489 #define AD1988A_REV2            0x100200
1490
1491 #define is_rev2(codec) \
1492         ((codec)->vendor_id == 0x11d41988 && \
1493          (codec)->revision_id == AD1988A_REV2)
1494
1495 /*
1496  * mixers
1497  */
1498
1499 static hda_nid_t ad1988_6stack_dac_nids[4] = {
1500         0x04, 0x06, 0x05, 0x0a
1501 };
1502
1503 static hda_nid_t ad1988_3stack_dac_nids[3] = {
1504         0x04, 0x05, 0x0a
1505 };
1506
1507 /* for AD1988A revision-2, DAC2-4 are swapped */
1508 static hda_nid_t ad1988_6stack_dac_nids_rev2[4] = {
1509         0x04, 0x05, 0x0a, 0x06
1510 };
1511
1512 static hda_nid_t ad1988_3stack_dac_nids_rev2[3] = {
1513         0x04, 0x0a, 0x06
1514 };
1515
1516 static hda_nid_t ad1988_adc_nids[3] = {
1517         0x08, 0x09, 0x0f
1518 };
1519
1520 static hda_nid_t ad1988_capsrc_nids[3] = {
1521         0x0c, 0x0d, 0x0e
1522 };
1523
1524 #define AD1988_SPDIF_OUT        0x02
1525 #define AD1988_SPDIF_IN         0x07
1526
1527 static struct hda_input_mux ad1988_6stack_capture_source = {
1528         .num_items = 5,
1529         .items = {
1530                 { "Front Mic", 0x0 },
1531                 { "Line", 0x1 },
1532                 { "Mic", 0x4 },
1533                 { "CD", 0x5 },
1534                 { "Mix", 0x9 },
1535         },
1536 };
1537
1538 static struct hda_input_mux ad1988_laptop_capture_source = {
1539         .num_items = 3,
1540         .items = {
1541                 { "Mic/Line", 0x0 },
1542                 { "CD", 0x5 },
1543                 { "Mix", 0x9 },
1544         },
1545 };
1546
1547 /*
1548  */
1549 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
1550                                struct snd_ctl_elem_info *uinfo)
1551 {
1552         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1553         struct ad198x_spec *spec = codec->spec;
1554         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
1555                                     spec->num_channel_mode);
1556 }
1557
1558 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
1559                               struct snd_ctl_elem_value *ucontrol)
1560 {
1561         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1562         struct ad198x_spec *spec = codec->spec;
1563         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
1564                                    spec->num_channel_mode, spec->multiout.max_channels);
1565 }
1566
1567 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
1568                               struct snd_ctl_elem_value *ucontrol)
1569 {
1570         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1571         struct ad198x_spec *spec = codec->spec;
1572         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
1573                                       spec->num_channel_mode,
1574                                       &spec->multiout.max_channels);
1575         if (err >= 0 && spec->need_dac_fix)
1576                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1577         return err;
1578 }
1579
1580 /* 6-stack mode */
1581 static struct snd_kcontrol_new ad1988_6stack_mixers1[] = {
1582         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1583         HDA_CODEC_VOLUME("Surround Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1584         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
1585         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
1586         HDA_CODEC_VOLUME("Side Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1587         { } /* end */
1588 };
1589
1590 static struct snd_kcontrol_new ad1988_6stack_mixers1_rev2[] = {
1591         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1592         HDA_CODEC_VOLUME("Surround Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1593         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0a, 1, 0x0, HDA_OUTPUT),
1594         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0a, 2, 0x0, HDA_OUTPUT),
1595         HDA_CODEC_VOLUME("Side Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1596         { } /* end */
1597 };
1598
1599 static struct snd_kcontrol_new ad1988_6stack_mixers2[] = {
1600         HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
1601         HDA_BIND_MUTE("Surround Playback Switch", 0x2a, 2, HDA_INPUT),
1602         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x27, 1, 2, HDA_INPUT),
1603         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x27, 2, 2, HDA_INPUT),
1604         HDA_BIND_MUTE("Side Playback Switch", 0x28, 2, HDA_INPUT),
1605         HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
1606         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1607
1608         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1609         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1610         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1611         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1612         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1613         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1614         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
1615         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),
1616
1617         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1618         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1619
1620         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1621         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1622
1623         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1624         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
1625
1626         { } /* end */
1627 };
1628
1629 /* 3-stack mode */
1630 static struct snd_kcontrol_new ad1988_3stack_mixers1[] = {
1631         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1632         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1633         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
1634         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
1635         { } /* end */
1636 };
1637
1638 static struct snd_kcontrol_new ad1988_3stack_mixers1_rev2[] = {
1639         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1640         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1641         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x06, 1, 0x0, HDA_OUTPUT),
1642         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x06, 2, 0x0, HDA_OUTPUT),
1643         { } /* end */
1644 };
1645
1646 static struct snd_kcontrol_new ad1988_3stack_mixers2[] = {
1647         HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
1648         HDA_BIND_MUTE("Surround Playback Switch", 0x2c, 2, HDA_INPUT),
1649         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x26, 1, 2, HDA_INPUT),
1650         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x26, 2, 2, HDA_INPUT),
1651         HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
1652         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1653
1654         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1655         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1656         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1657         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1658         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1659         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1660         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
1661         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),
1662
1663         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1664         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1665
1666         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1667         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1668
1669         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1670         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
1671         {
1672                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1673                 .name = "Channel Mode",
1674                 .info = ad198x_ch_mode_info,
1675                 .get = ad198x_ch_mode_get,
1676                 .put = ad198x_ch_mode_put,
1677         },
1678
1679         { } /* end */
1680 };
1681
1682 /* laptop mode */
1683 static struct snd_kcontrol_new ad1988_laptop_mixers[] = {
1684         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1685         HDA_CODEC_MUTE("PCM Playback Switch", 0x29, 0x0, HDA_INPUT),
1686         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1687
1688         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1689         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1690         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1691         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1692         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1693         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1694
1695         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1696         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1697
1698         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1699         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1700
1701         HDA_CODEC_VOLUME("Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1702
1703         {
1704                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1705                 .name = "External Amplifier",
1706                 .info = ad198x_eapd_info,
1707                 .get = ad198x_eapd_get,
1708                 .put = ad198x_eapd_put,
1709                 .private_value = 0x12 | (1 << 8), /* port-D, inversed */
1710         },
1711
1712         { } /* end */
1713 };
1714
1715 /* capture */
1716 static struct snd_kcontrol_new ad1988_capture_mixers[] = {
1717         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
1718         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
1719         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
1720         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
1721         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x0e, 0x0, HDA_OUTPUT),
1722         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x0e, 0x0, HDA_OUTPUT),
1723         {
1724                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1725                 /* The multiple "Capture Source" controls confuse alsamixer
1726                  * So call somewhat different..
1727                  * FIXME: the controls appear in the "playback" view!
1728                  */
1729                 /* .name = "Capture Source", */
1730                 .name = "Input Source",
1731                 .count = 3,
1732                 .info = ad198x_mux_enum_info,
1733                 .get = ad198x_mux_enum_get,
1734                 .put = ad198x_mux_enum_put,
1735         },
1736         { } /* end */
1737 };
1738
1739 static int ad1988_spdif_playback_source_info(struct snd_kcontrol *kcontrol,
1740                                              struct snd_ctl_elem_info *uinfo)
1741 {
1742         static char *texts[] = {
1743                 "PCM", "ADC1", "ADC2", "ADC3"
1744         };
1745         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1746         uinfo->count = 1;
1747         uinfo->value.enumerated.items = 4;
1748         if (uinfo->value.enumerated.item >= 4)
1749                 uinfo->value.enumerated.item = 3;
1750         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1751         return 0;
1752 }
1753
1754 static int ad1988_spdif_playback_source_get(struct snd_kcontrol *kcontrol,
1755                                             struct snd_ctl_elem_value *ucontrol)
1756 {
1757         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1758         unsigned int sel;
1759
1760         sel = snd_hda_codec_read(codec, 0x1d, 0, AC_VERB_GET_AMP_GAIN_MUTE,
1761                                  AC_AMP_GET_INPUT);
1762         if (!(sel & 0x80))
1763                 ucontrol->value.enumerated.item[0] = 0;
1764         else {
1765                 sel = snd_hda_codec_read(codec, 0x0b, 0,
1766                                          AC_VERB_GET_CONNECT_SEL, 0);
1767                 if (sel < 3)
1768                         sel++;
1769                 else
1770                         sel = 0;
1771                 ucontrol->value.enumerated.item[0] = sel;
1772         }
1773         return 0;
1774 }
1775
1776 static int ad1988_spdif_playback_source_put(struct snd_kcontrol *kcontrol,
1777                                             struct snd_ctl_elem_value *ucontrol)
1778 {
1779         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1780         unsigned int val, sel;
1781         int change;
1782
1783         val = ucontrol->value.enumerated.item[0];
1784         if (!val) {
1785                 sel = snd_hda_codec_read(codec, 0x1d, 0,
1786                                          AC_VERB_GET_AMP_GAIN_MUTE,
1787                                          AC_AMP_GET_INPUT);
1788                 change = sel & 0x80;
1789                 if (change) {
1790                         snd_hda_codec_write_cache(codec, 0x1d, 0,
1791                                                   AC_VERB_SET_AMP_GAIN_MUTE,
1792                                                   AMP_IN_UNMUTE(0));
1793                         snd_hda_codec_write_cache(codec, 0x1d, 0,
1794                                                   AC_VERB_SET_AMP_GAIN_MUTE,
1795                                                   AMP_IN_MUTE(1));
1796                 }
1797         } else {
1798                 sel = snd_hda_codec_read(codec, 0x1d, 0,
1799                                          AC_VERB_GET_AMP_GAIN_MUTE,
1800                                          AC_AMP_GET_INPUT | 0x01);
1801                 change = sel & 0x80;
1802                 if (change) {
1803                         snd_hda_codec_write_cache(codec, 0x1d, 0,
1804                                                   AC_VERB_SET_AMP_GAIN_MUTE,
1805                                                   AMP_IN_MUTE(0));
1806                         snd_hda_codec_write_cache(codec, 0x1d, 0,
1807                                                   AC_VERB_SET_AMP_GAIN_MUTE,
1808                                                   AMP_IN_UNMUTE(1));
1809                 }
1810                 sel = snd_hda_codec_read(codec, 0x0b, 0,
1811                                          AC_VERB_GET_CONNECT_SEL, 0) + 1;
1812                 change |= sel != val;
1813                 if (change)
1814                         snd_hda_codec_write_cache(codec, 0x0b, 0,
1815                                                   AC_VERB_SET_CONNECT_SEL,
1816                                                   val - 1);
1817         }
1818         return change;
1819 }
1820
1821 static struct snd_kcontrol_new ad1988_spdif_out_mixers[] = {
1822         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
1823         {
1824                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1825                 .name = "IEC958 Playback Source",
1826                 .info = ad1988_spdif_playback_source_info,
1827                 .get = ad1988_spdif_playback_source_get,
1828                 .put = ad1988_spdif_playback_source_put,
1829         },
1830         { } /* end */
1831 };
1832
1833 static struct snd_kcontrol_new ad1988_spdif_in_mixers[] = {
1834         HDA_CODEC_VOLUME("IEC958 Capture Volume", 0x1c, 0x0, HDA_INPUT),
1835         { } /* end */
1836 };
1837
1838
1839 /*
1840  * initialization verbs
1841  */
1842
1843 /*
1844  * for 6-stack (+dig)
1845  */
1846 static struct hda_verb ad1988_6stack_init_verbs[] = {
1847         /* Front, Surround, CLFE, side DAC; unmute as default */
1848         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1849         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1850         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1851         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1852         /* Port-A front headphon path */
1853         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
1854         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1855         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1856         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1857         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1858         /* Port-D line-out path */
1859         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1860         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1861         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1862         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1863         /* Port-F surround path */
1864         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1865         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1866         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1867         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1868         /* Port-G CLFE path */
1869         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1870         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1871         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1872         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1873         /* Port-H side path */
1874         {0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1875         {0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1876         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1877         {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1878         /* Mono out path */
1879         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
1880         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1881         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1882         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1883         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
1884         /* Port-B front mic-in path */
1885         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1886         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1887         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1888         /* Port-C line-in path */
1889         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1890         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1891         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1892         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
1893         /* Port-E mic-in path */
1894         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1895         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1896         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1897         {0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
1898
1899         { }
1900 };
1901
1902 static struct hda_verb ad1988_capture_init_verbs[] = {
1903         /* mute analog mix */
1904         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1905         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1906         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
1907         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
1908         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
1909         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
1910         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
1911         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
1912         /* select ADCs - front-mic */
1913         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
1914         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
1915         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
1916         /* ADCs; muted */
1917         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1918         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1919         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1920
1921         { }
1922 };
1923
1924 static struct hda_verb ad1988_spdif_init_verbs[] = {
1925         /* SPDIF out sel */
1926         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
1927         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x0}, /* ADC1 */
1928         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1929         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1930         /* SPDIF out pin */
1931         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
1932
1933         { }
1934 };
1935
1936 /*
1937  * verbs for 3stack (+dig)
1938  */
1939 static struct hda_verb ad1988_3stack_ch2_init[] = {
1940         /* set port-C to line-in */
1941         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
1942         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1943         /* set port-E to mic-in */
1944         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
1945         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
1946         { } /* end */
1947 };
1948
1949 static struct hda_verb ad1988_3stack_ch6_init[] = {
1950         /* set port-C to surround out */
1951         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
1952         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
1953         /* set port-E to CLFE out */
1954         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
1955         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
1956         { } /* end */
1957 };
1958
1959 static struct hda_channel_mode ad1988_3stack_modes[2] = {
1960         { 2, ad1988_3stack_ch2_init },
1961         { 6, ad1988_3stack_ch6_init },
1962 };
1963
1964 static struct hda_verb ad1988_3stack_init_verbs[] = {
1965         /* Front, Surround, CLFE, side DAC; unmute as default */
1966         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1967         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1968         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1969         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1970         /* Port-A front headphon path */
1971         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
1972         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1973         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1974         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1975         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1976         /* Port-D line-out path */
1977         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1978         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1979         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1980         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1981         /* Mono out path */
1982         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
1983         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1984         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1985         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1986         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
1987         /* Port-B front mic-in path */
1988         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1989         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1990         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1991         /* Port-C line-in/surround path - 6ch mode as default */
1992         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1993         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1994         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1995         {0x31, AC_VERB_SET_CONNECT_SEL, 0x0}, /* output sel: DAC 0x05 */
1996         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
1997         /* Port-E mic-in/CLFE path - 6ch mode as default */
1998         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1999         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2000         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2001         {0x32, AC_VERB_SET_CONNECT_SEL, 0x1}, /* output sel: DAC 0x0a */
2002         {0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
2003         /* mute analog mix */
2004         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2005         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2006         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2007         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2008         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2009         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2010         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2011         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2012         /* select ADCs - front-mic */
2013         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2014         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2015         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2016         /* ADCs; muted */
2017         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2018         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2019         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2020         { }
2021 };
2022
2023 /*
2024  * verbs for laptop mode (+dig)
2025  */
2026 static struct hda_verb ad1988_laptop_hp_on[] = {
2027         /* unmute port-A and mute port-D */
2028         { 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2029         { 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2030         { } /* end */
2031 };
2032 static struct hda_verb ad1988_laptop_hp_off[] = {
2033         /* mute port-A and unmute port-D */
2034         { 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2035         { 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2036         { } /* end */
2037 };
2038
2039 #define AD1988_HP_EVENT 0x01
2040
2041 static struct hda_verb ad1988_laptop_init_verbs[] = {
2042         /* Front, Surround, CLFE, side DAC; unmute as default */
2043         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2044         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2045         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2046         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2047         /* Port-A front headphon path */
2048         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
2049         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2050         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2051         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2052         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2053         /* unsolicited event for pin-sense */
2054         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1988_HP_EVENT },
2055         /* Port-D line-out path + EAPD */
2056         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2057         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2058         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2059         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2060         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x00}, /* EAPD-off */
2061         /* Mono out path */
2062         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2063         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2064         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2065         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2066         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2067         /* Port-B mic-in path */
2068         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2069         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2070         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2071         /* Port-C docking station - try to output */
2072         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2073         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2074         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2075         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2076         /* mute analog mix */
2077         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2078         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2079         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2080         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2081         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2082         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2083         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2084         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2085         /* select ADCs - mic */
2086         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2087         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2088         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2089         /* ADCs; muted */
2090         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2091         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2092         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2093         { }
2094 };
2095
2096 static void ad1988_laptop_unsol_event(struct hda_codec *codec, unsigned int res)
2097 {
2098         if ((res >> 26) != AD1988_HP_EVENT)
2099                 return;
2100         if (snd_hda_codec_read(codec, 0x11, 0, AC_VERB_GET_PIN_SENSE, 0) & (1 << 31))
2101                 snd_hda_sequence_write(codec, ad1988_laptop_hp_on);
2102         else
2103                 snd_hda_sequence_write(codec, ad1988_laptop_hp_off);
2104
2105
2106
2107 /*
2108  * Automatic parse of I/O pins from the BIOS configuration
2109  */
2110
2111 #define NUM_CONTROL_ALLOC       32
2112 #define NUM_VERB_ALLOC          32
2113
2114 enum {
2115         AD_CTL_WIDGET_VOL,
2116         AD_CTL_WIDGET_MUTE,
2117         AD_CTL_BIND_MUTE,
2118 };
2119 static struct snd_kcontrol_new ad1988_control_templates[] = {
2120         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2121         HDA_CODEC_MUTE(NULL, 0, 0, 0),
2122         HDA_BIND_MUTE(NULL, 0, 0, 0),
2123 };
2124
2125 /* add dynamic controls */
2126 static int add_control(struct ad198x_spec *spec, int type, const char *name,
2127                        unsigned long val)
2128 {
2129         struct snd_kcontrol_new *knew;
2130
2131         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
2132                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
2133
2134                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
2135                 if (! knew)
2136                         return -ENOMEM;
2137                 if (spec->kctl_alloc) {
2138                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
2139                         kfree(spec->kctl_alloc);
2140                 }
2141                 spec->kctl_alloc = knew;
2142                 spec->num_kctl_alloc = num;
2143         }
2144
2145         knew = &spec->kctl_alloc[spec->num_kctl_used];
2146         *knew = ad1988_control_templates[type];
2147         knew->name = kstrdup(name, GFP_KERNEL);
2148         if (! knew->name)
2149                 return -ENOMEM;
2150         knew->private_value = val;
2151         spec->num_kctl_used++;
2152         return 0;
2153 }
2154
2155 #define AD1988_PIN_CD_NID               0x18
2156 #define AD1988_PIN_BEEP_NID             0x10
2157
2158 static hda_nid_t ad1988_mixer_nids[8] = {
2159         /* A     B     C     D     E     F     G     H */
2160         0x22, 0x2b, 0x2c, 0x29, 0x26, 0x2a, 0x27, 0x28
2161 };
2162
2163 static inline hda_nid_t ad1988_idx_to_dac(struct hda_codec *codec, int idx)
2164 {
2165         static hda_nid_t idx_to_dac[8] = {
2166                 /* A     B     C     D     E     F     G     H */
2167                 0x04, 0x06, 0x05, 0x04, 0x0a, 0x06, 0x05, 0x0a
2168         };
2169         static hda_nid_t idx_to_dac_rev2[8] = {
2170                 /* A     B     C     D     E     F     G     H */
2171                 0x04, 0x05, 0x0a, 0x04, 0x06, 0x05, 0x0a, 0x06
2172         };
2173         if (is_rev2(codec))
2174                 return idx_to_dac_rev2[idx];
2175         else
2176                 return idx_to_dac[idx];
2177 }
2178
2179 static hda_nid_t ad1988_boost_nids[8] = {
2180         0x38, 0x39, 0x3a, 0x3d, 0x3c, 0x3b, 0, 0
2181 };
2182
2183 static int ad1988_pin_idx(hda_nid_t nid)
2184 {
2185         static hda_nid_t ad1988_io_pins[8] = {
2186                 0x11, 0x14, 0x15, 0x12, 0x17, 0x16, 0x24, 0x25
2187         };
2188         int i;
2189         for (i = 0; i < ARRAY_SIZE(ad1988_io_pins); i++)
2190                 if (ad1988_io_pins[i] == nid)
2191                         return i;
2192         return 0; /* should be -1 */
2193 }
2194
2195 static int ad1988_pin_to_loopback_idx(hda_nid_t nid)
2196 {
2197         static int loopback_idx[8] = {
2198                 2, 0, 1, 3, 4, 5, 1, 4
2199         };
2200         switch (nid) {
2201         case AD1988_PIN_CD_NID:
2202                 return 6;
2203         default:
2204                 return loopback_idx[ad1988_pin_idx(nid)];
2205         }
2206 }
2207
2208 static int ad1988_pin_to_adc_idx(hda_nid_t nid)
2209 {
2210         static int adc_idx[8] = {
2211                 0, 1, 2, 8, 4, 3, 6, 7
2212         };
2213         switch (nid) {
2214         case AD1988_PIN_CD_NID:
2215                 return 5;
2216         default:
2217                 return adc_idx[ad1988_pin_idx(nid)];
2218         }
2219 }
2220
2221 /* fill in the dac_nids table from the parsed pin configuration */
2222 static int ad1988_auto_fill_dac_nids(struct hda_codec *codec,
2223                                      const struct auto_pin_cfg *cfg)
2224 {
2225         struct ad198x_spec *spec = codec->spec;
2226         int i, idx;
2227
2228         spec->multiout.dac_nids = spec->private_dac_nids;
2229
2230         /* check the pins hardwired to audio widget */
2231         for (i = 0; i < cfg->line_outs; i++) {
2232                 idx = ad1988_pin_idx(cfg->line_out_pins[i]);
2233                 spec->multiout.dac_nids[i] = ad1988_idx_to_dac(codec, idx);
2234         }
2235         spec->multiout.num_dacs = cfg->line_outs;
2236         return 0;
2237 }
2238
2239 /* add playback controls from the parsed DAC table */
2240 static int ad1988_auto_create_multi_out_ctls(struct ad198x_spec *spec,
2241                                              const struct auto_pin_cfg *cfg)
2242 {
2243         char name[32];
2244         static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
2245         hda_nid_t nid;
2246         int i, err;
2247
2248         for (i = 0; i < cfg->line_outs; i++) {
2249                 hda_nid_t dac = spec->multiout.dac_nids[i];
2250                 if (! dac)
2251                         continue;
2252                 nid = ad1988_mixer_nids[ad1988_pin_idx(cfg->line_out_pins[i])];
2253                 if (i == 2) {
2254                         /* Center/LFE */
2255                         err = add_control(spec, AD_CTL_WIDGET_VOL,
2256                                           "Center Playback Volume",
2257                                           HDA_COMPOSE_AMP_VAL(dac, 1, 0, HDA_OUTPUT));
2258                         if (err < 0)
2259                                 return err;
2260                         err = add_control(spec, AD_CTL_WIDGET_VOL,
2261                                           "LFE Playback Volume",
2262                                           HDA_COMPOSE_AMP_VAL(dac, 2, 0, HDA_OUTPUT));
2263                         if (err < 0)
2264                                 return err;
2265                         err = add_control(spec, AD_CTL_BIND_MUTE,
2266                                           "Center Playback Switch",
2267                                           HDA_COMPOSE_AMP_VAL(nid, 1, 2, HDA_INPUT));
2268                         if (err < 0)
2269                                 return err;
2270                         err = add_control(spec, AD_CTL_BIND_MUTE,
2271                                           "LFE Playback Switch",
2272                                           HDA_COMPOSE_AMP_VAL(nid, 2, 2, HDA_INPUT));
2273                         if (err < 0)
2274                                 return err;
2275                 } else {
2276                         sprintf(name, "%s Playback Volume", chname[i]);
2277                         err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2278                                           HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT));
2279                         if (err < 0)
2280                                 return err;
2281                         sprintf(name, "%s Playback Switch", chname[i]);
2282                         err = add_control(spec, AD_CTL_BIND_MUTE, name,
2283                                           HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT));
2284                         if (err < 0)
2285                                 return err;
2286                 }
2287         }
2288         return 0;
2289 }
2290
2291 /* add playback controls for speaker and HP outputs */
2292 static int ad1988_auto_create_extra_out(struct hda_codec *codec, hda_nid_t pin,
2293                                         const char *pfx)
2294 {
2295         struct ad198x_spec *spec = codec->spec;
2296         hda_nid_t nid;
2297         int idx, err;
2298         char name[32];
2299
2300         if (! pin)
2301                 return 0;
2302
2303         idx = ad1988_pin_idx(pin);
2304         nid = ad1988_idx_to_dac(codec, idx);
2305         /* specify the DAC as the extra output */
2306         if (! spec->multiout.hp_nid)
2307                 spec->multiout.hp_nid = nid;
2308         else
2309                 spec->multiout.extra_out_nid[0] = nid;
2310         /* control HP volume/switch on the output mixer amp */
2311         sprintf(name, "%s Playback Volume", pfx);
2312         if ((err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2313                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
2314                 return err;
2315         nid = ad1988_mixer_nids[idx];
2316         sprintf(name, "%s Playback Switch", pfx);
2317         if ((err = add_control(spec, AD_CTL_BIND_MUTE, name,
2318                                HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT))) < 0)
2319                 return err;
2320         return 0;
2321 }
2322
2323 /* create input playback/capture controls for the given pin */
2324 static int new_analog_input(struct ad198x_spec *spec, hda_nid_t pin,
2325                             const char *ctlname, int boost)
2326 {
2327         char name[32];
2328         int err, idx;
2329
2330         sprintf(name, "%s Playback Volume", ctlname);
2331         idx = ad1988_pin_to_loopback_idx(pin);
2332         if ((err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2333                                HDA_COMPOSE_AMP_VAL(0x20, 3, idx, HDA_INPUT))) < 0)
2334                 return err;
2335         sprintf(name, "%s Playback Switch", ctlname);
2336         if ((err = add_control(spec, AD_CTL_WIDGET_MUTE, name,
2337                                HDA_COMPOSE_AMP_VAL(0x20, 3, idx, HDA_INPUT))) < 0)
2338                 return err;
2339         if (boost) {
2340                 hda_nid_t bnid;
2341                 idx = ad1988_pin_idx(pin);
2342                 bnid = ad1988_boost_nids[idx];
2343                 if (bnid) {
2344                         sprintf(name, "%s Boost", ctlname);
2345                         return add_control(spec, AD_CTL_WIDGET_VOL, name,
2346                                            HDA_COMPOSE_AMP_VAL(bnid, 3, idx, HDA_OUTPUT));
2347
2348                 }
2349         }
2350         return 0;
2351 }
2352
2353 /* create playback/capture controls for input pins */
2354 static int ad1988_auto_create_analog_input_ctls(struct ad198x_spec *spec,
2355                                                 const struct auto_pin_cfg *cfg)
2356 {
2357         struct hda_input_mux *imux = &spec->private_imux;
2358         int i, err;
2359
2360         for (i = 0; i < AUTO_PIN_LAST; i++) {
2361                 err = new_analog_input(spec, cfg->input_pins[i],
2362                                        auto_pin_cfg_labels[i],
2363                                        i <= AUTO_PIN_FRONT_MIC);
2364                 if (err < 0)
2365                         return err;
2366                 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
2367                 imux->items[imux->num_items].index = ad1988_pin_to_adc_idx(cfg->input_pins[i]);
2368                 imux->num_items++;
2369         }
2370         imux->items[imux->num_items].label = "Mix";
2371         imux->items[imux->num_items].index = 9;
2372         imux->num_items++;
2373
2374         if ((err = add_control(spec, AD_CTL_WIDGET_VOL,
2375                                "Analog Mix Playback Volume",
2376                                HDA_COMPOSE_AMP_VAL(0x21, 3, 0x0, HDA_OUTPUT))) < 0)
2377                 return err;
2378         if ((err = add_control(spec, AD_CTL_WIDGET_MUTE,
2379                                "Analog Mix Playback Switch",
2380                                HDA_COMPOSE_AMP_VAL(0x21, 3, 0x0, HDA_OUTPUT))) < 0)
2381                 return err;
2382
2383         return 0;
2384 }
2385
2386 static void ad1988_auto_set_output_and_unmute(struct hda_codec *codec,
2387                                               hda_nid_t nid, int pin_type,
2388                                               int dac_idx)
2389 {
2390         /* set as output */
2391         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
2392         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
2393         switch (nid) {
2394         case 0x11: /* port-A - DAC 04 */
2395                 snd_hda_codec_write(codec, 0x37, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2396                 break;
2397         case 0x14: /* port-B - DAC 06 */
2398                 snd_hda_codec_write(codec, 0x30, 0, AC_VERB_SET_CONNECT_SEL, 0x02);
2399                 break;
2400         case 0x15: /* port-C - DAC 05 */
2401                 snd_hda_codec_write(codec, 0x31, 0, AC_VERB_SET_CONNECT_SEL, 0x00);
2402                 break;
2403         case 0x17: /* port-E - DAC 0a */
2404                 snd_hda_codec_write(codec, 0x32, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2405                 break;
2406         case 0x13: /* mono - DAC 04 */
2407                 snd_hda_codec_write(codec, 0x36, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2408                 break;
2409         }
2410 }
2411
2412 static void ad1988_auto_init_multi_out(struct hda_codec *codec)
2413 {
2414         struct ad198x_spec *spec = codec->spec;
2415         int i;
2416
2417         for (i = 0; i < spec->autocfg.line_outs; i++) {
2418                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
2419                 ad1988_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
2420         }
2421 }
2422
2423 static void ad1988_auto_init_extra_out(struct hda_codec *codec)
2424 {
2425         struct ad198x_spec *spec = codec->spec;
2426         hda_nid_t pin;
2427
2428         pin = spec->autocfg.speaker_pins[0];
2429         if (pin) /* connect to front */
2430                 ad1988_auto_set_output_and_unmute(codec, pin, PIN_OUT, 0);
2431         pin = spec->autocfg.hp_pins[0];
2432         if (pin) /* connect to front */
2433                 ad1988_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
2434 }
2435
2436 static void ad1988_auto_init_analog_input(struct hda_codec *codec)
2437 {
2438         struct ad198x_spec *spec = codec->spec;
2439         int i, idx;
2440
2441         for (i = 0; i < AUTO_PIN_LAST; i++) {
2442                 hda_nid_t nid = spec->autocfg.input_pins[i];
2443                 if (! nid)
2444                         continue;
2445                 switch (nid) {
2446                 case 0x15: /* port-C */
2447                         snd_hda_codec_write(codec, 0x33, 0, AC_VERB_SET_CONNECT_SEL, 0x0);
2448                         break;
2449                 case 0x17: /* port-E */
2450                         snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_CONNECT_SEL, 0x0);
2451                         break;
2452                 }
2453                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2454                                     i <= AUTO_PIN_FRONT_MIC ? PIN_VREF80 : PIN_IN);
2455                 if (nid != AD1988_PIN_CD_NID)
2456                         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2457                                             AMP_OUT_MUTE);
2458                 idx = ad1988_pin_idx(nid);
2459                 if (ad1988_boost_nids[idx])
2460                         snd_hda_codec_write(codec, ad1988_boost_nids[idx], 0,
2461                                             AC_VERB_SET_AMP_GAIN_MUTE,
2462                                             AMP_OUT_ZERO);
2463         }
2464 }
2465
2466 /* parse the BIOS configuration and set up the alc_spec */
2467 /* return 1 if successful, 0 if the proper config is not found, or a negative error code */
2468 static int ad1988_parse_auto_config(struct hda_codec *codec)
2469 {
2470         struct ad198x_spec *spec = codec->spec;
2471         int err;
2472
2473         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
2474                 return err;
2475         if ((err = ad1988_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
2476                 return err;
2477         if (! spec->autocfg.line_outs)
2478                 return 0; /* can't find valid BIOS pin config */
2479         if ((err = ad1988_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
2480             (err = ad1988_auto_create_extra_out(codec,
2481                                                 spec->autocfg.speaker_pins[0],
2482                                                 "Speaker")) < 0 ||
2483             (err = ad1988_auto_create_extra_out(codec, spec->autocfg.hp_pins[0],
2484                                                 "Headphone")) < 0 ||
2485             (err = ad1988_auto_create_analog_input_ctls(spec, &spec->autocfg)) < 0)
2486                 return err;
2487
2488         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2489
2490         if (spec->autocfg.dig_out_pin)
2491                 spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2492         if (spec->autocfg.dig_in_pin)
2493                 spec->dig_in_nid = AD1988_SPDIF_IN;
2494
2495         if (spec->kctl_alloc)
2496                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
2497
2498         spec->init_verbs[spec->num_init_verbs++] = ad1988_6stack_init_verbs;
2499
2500         spec->input_mux = &spec->private_imux;
2501
2502         return 1;
2503 }
2504
2505 /* init callback for auto-configuration model -- overriding the default init */
2506 static int ad1988_auto_init(struct hda_codec *codec)
2507 {
2508         ad198x_init(codec);
2509         ad1988_auto_init_multi_out(codec);
2510         ad1988_auto_init_extra_out(codec);
2511         ad1988_auto_init_analog_input(codec);
2512         return 0;
2513 }
2514
2515
2516 /*
2517  */
2518
2519 static const char *ad1988_models[AD1988_MODEL_LAST] = {
2520         [AD1988_6STACK]         = "6stack",
2521         [AD1988_6STACK_DIG]     = "6stack-dig",
2522         [AD1988_3STACK]         = "3stack",
2523         [AD1988_3STACK_DIG]     = "3stack-dig",
2524         [AD1988_LAPTOP]         = "laptop",
2525         [AD1988_LAPTOP_DIG]     = "laptop-dig",
2526         [AD1988_AUTO]           = "auto",
2527 };
2528
2529 static struct snd_pci_quirk ad1988_cfg_tbl[] = {
2530         SND_PCI_QUIRK(0x1043, 0x81f6, "Asus M2N-SLI", AD1988_6STACK_DIG),
2531         SND_PCI_QUIRK(0x1043, 0x81ec, "Asus P5B-DLX", AD1988_6STACK_DIG),
2532         {}
2533 };
2534
2535 static int patch_ad1988(struct hda_codec *codec)
2536 {
2537         struct ad198x_spec *spec;
2538         int board_config;
2539
2540         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2541         if (spec == NULL)
2542                 return -ENOMEM;
2543
2544         codec->spec = spec;
2545
2546         if (is_rev2(codec))
2547                 snd_printk(KERN_INFO "patch_analog: AD1988A rev.2 is detected, enable workarounds\n");
2548
2549         board_config = snd_hda_check_board_config(codec, AD1988_MODEL_LAST,
2550                                                   ad1988_models, ad1988_cfg_tbl);
2551         if (board_config < 0) {
2552                 printk(KERN_INFO "hda_codec: Unknown model for AD1988, trying auto-probe from BIOS...\n");
2553                 board_config = AD1988_AUTO;
2554         }
2555
2556         if (board_config == AD1988_AUTO) {
2557                 /* automatic parse from the BIOS config */
2558                 int err = ad1988_parse_auto_config(codec);
2559                 if (err < 0) {
2560                         ad198x_free(codec);
2561                         return err;
2562                 } else if (! err) {
2563                         printk(KERN_INFO "hda_codec: Cannot set up configuration from BIOS.  Using 6-stack mode...\n");
2564                         board_config = AD1988_6STACK;
2565                 }
2566         }
2567
2568         switch (board_config) {
2569         case AD1988_6STACK:
2570         case AD1988_6STACK_DIG:
2571                 spec->multiout.max_channels = 8;
2572                 spec->multiout.num_dacs = 4;
2573                 if (is_rev2(codec))
2574                         spec->multiout.dac_nids = ad1988_6stack_dac_nids_rev2;
2575                 else
2576                         spec->multiout.dac_nids = ad1988_6stack_dac_nids;
2577                 spec->input_mux = &ad1988_6stack_capture_source;
2578                 spec->num_mixers = 2;
2579                 if (is_rev2(codec))
2580                         spec->mixers[0] = ad1988_6stack_mixers1_rev2;
2581                 else
2582                         spec->mixers[0] = ad1988_6stack_mixers1;
2583                 spec->mixers[1] = ad1988_6stack_mixers2;
2584                 spec->num_init_verbs = 1;
2585                 spec->init_verbs[0] = ad1988_6stack_init_verbs;
2586                 if (board_config == AD1988_6STACK_DIG) {
2587                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2588                         spec->dig_in_nid = AD1988_SPDIF_IN;
2589                 }
2590                 break;
2591         case AD1988_3STACK:
2592         case AD1988_3STACK_DIG:
2593                 spec->multiout.max_channels = 6;
2594                 spec->multiout.num_dacs = 3;
2595                 if (is_rev2(codec))
2596                         spec->multiout.dac_nids = ad1988_3stack_dac_nids_rev2;
2597                 else
2598                         spec->multiout.dac_nids = ad1988_3stack_dac_nids;
2599                 spec->input_mux = &ad1988_6stack_capture_source;
2600                 spec->channel_mode = ad1988_3stack_modes;
2601                 spec->num_channel_mode = ARRAY_SIZE(ad1988_3stack_modes);
2602                 spec->num_mixers = 2;
2603                 if (is_rev2(codec))
2604                         spec->mixers[0] = ad1988_3stack_mixers1_rev2;
2605                 else
2606                         spec->mixers[0] = ad1988_3stack_mixers1;
2607                 spec->mixers[1] = ad1988_3stack_mixers2;
2608                 spec->num_init_verbs = 1;
2609                 spec->init_verbs[0] = ad1988_3stack_init_verbs;
2610                 if (board_config == AD1988_3STACK_DIG)
2611                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2612                 break;
2613         case AD1988_LAPTOP:
2614         case AD1988_LAPTOP_DIG:
2615                 spec->multiout.max_channels = 2;
2616                 spec->multiout.num_dacs = 1;
2617                 spec->multiout.dac_nids = ad1988_3stack_dac_nids;
2618                 spec->input_mux = &ad1988_laptop_capture_source;
2619                 spec->num_mixers = 1;
2620                 spec->mixers[0] = ad1988_laptop_mixers;
2621                 spec->num_init_verbs = 1;
2622                 spec->init_verbs[0] = ad1988_laptop_init_verbs;
2623                 if (board_config == AD1988_LAPTOP_DIG)
2624                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2625                 break;
2626         }
2627
2628         spec->num_adc_nids = ARRAY_SIZE(ad1988_adc_nids);
2629         spec->adc_nids = ad1988_adc_nids;
2630         spec->capsrc_nids = ad1988_capsrc_nids;
2631         spec->mixers[spec->num_mixers++] = ad1988_capture_mixers;
2632         spec->init_verbs[spec->num_init_verbs++] = ad1988_capture_init_verbs;
2633         if (spec->multiout.dig_out_nid) {
2634                 spec->mixers[spec->num_mixers++] = ad1988_spdif_out_mixers;
2635                 spec->init_verbs[spec->num_init_verbs++] = ad1988_spdif_init_verbs;
2636         }
2637         if (spec->dig_in_nid)
2638                 spec->mixers[spec->num_mixers++] = ad1988_spdif_in_mixers;
2639
2640         codec->patch_ops = ad198x_patch_ops;
2641         switch (board_config) {
2642         case AD1988_AUTO:
2643                 codec->patch_ops.init = ad1988_auto_init;
2644                 break;
2645         case AD1988_LAPTOP:
2646         case AD1988_LAPTOP_DIG:
2647                 codec->patch_ops.unsol_event = ad1988_laptop_unsol_event;
2648                 break;
2649         }
2650
2651         return 0;
2652 }
2653
2654
2655 /*
2656  * AD1884 / AD1984
2657  *
2658  * port-B - front line/mic-in
2659  * port-E - aux in/out
2660  * port-F - aux in/out
2661  * port-C - rear line/mic-in
2662  * port-D - rear line/hp-out
2663  * port-A - front line/hp-out
2664  *
2665  * AD1984 = AD1884 + two digital mic-ins
2666  *
2667  * FIXME:
2668  * For simplicity, we share the single DAC for both HP and line-outs
2669  * right now.  The inidividual playbacks could be easily implemented,
2670  * but no build-up framework is given, so far.
2671  */
2672
2673 static hda_nid_t ad1884_dac_nids[1] = {
2674         0x04,
2675 };
2676
2677 static hda_nid_t ad1884_adc_nids[2] = {
2678         0x08, 0x09,
2679 };
2680
2681 static hda_nid_t ad1884_capsrc_nids[2] = {
2682         0x0c, 0x0d,
2683 };
2684
2685 #define AD1884_SPDIF_OUT        0x02
2686
2687 static struct hda_input_mux ad1884_capture_source = {
2688         .num_items = 4,
2689         .items = {
2690                 { "Front Mic", 0x0 },
2691                 { "Mic", 0x1 },
2692                 { "CD", 0x2 },
2693                 { "Mix", 0x3 },
2694         },
2695 };
2696
2697 static struct snd_kcontrol_new ad1884_base_mixers[] = {
2698         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2699         /* HDA_CODEC_VOLUME_IDX("PCM Playback Volume", 1, 0x03, 0x0, HDA_OUTPUT), */
2700         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
2701         HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
2702         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
2703         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
2704         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
2705         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
2706         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
2707         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
2708         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x02, HDA_INPUT),
2709         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x02, HDA_INPUT),
2710         /*
2711         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x20, 0x03, HDA_INPUT),
2712         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x20, 0x03, HDA_INPUT),
2713         HDA_CODEC_VOLUME("Digital Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
2714         HDA_CODEC_MUTE("Digital Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
2715         */
2716         HDA_CODEC_VOLUME("Mic Boost", 0x15, 0x0, HDA_INPUT),
2717         HDA_CODEC_VOLUME("Front Mic Boost", 0x14, 0x0, HDA_INPUT),
2718         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
2719         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
2720         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
2721         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
2722         {
2723                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2724                 /* The multiple "Capture Source" controls confuse alsamixer
2725                  * So call somewhat different..
2726                  * FIXME: the controls appear in the "playback" view!
2727                  */
2728                 /* .name = "Capture Source", */
2729                 .name = "Input Source",
2730                 .count = 2,
2731                 .info = ad198x_mux_enum_info,
2732                 .get = ad198x_mux_enum_get,
2733                 .put = ad198x_mux_enum_put,
2734         },
2735         /* SPDIF controls */
2736         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
2737         {
2738                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2739                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
2740                 /* identical with ad1983 */
2741                 .info = ad1983_spdif_route_info,
2742                 .get = ad1983_spdif_route_get,
2743                 .put = ad1983_spdif_route_put,
2744         },
2745         { } /* end */
2746 };
2747
2748 static struct snd_kcontrol_new ad1984_dmic_mixers[] = {
2749         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x05, 0x0, HDA_INPUT),
2750         HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x05, 0x0, HDA_INPUT),
2751         HDA_CODEC_VOLUME_IDX("Digital Mic Capture Volume", 1, 0x06, 0x0,
2752                              HDA_INPUT),
2753         HDA_CODEC_MUTE_IDX("Digital Mic Capture Switch", 1, 0x06, 0x0,
2754                            HDA_INPUT),
2755         { } /* end */
2756 };
2757
2758 /*
2759  * initialization verbs
2760  */
2761 static struct hda_verb ad1884_init_verbs[] = {
2762         /* DACs; mute as default */
2763         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2764         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2765         /* Port-A (HP) mixer */
2766         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2767         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2768         /* Port-A pin */
2769         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2770         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2771         /* HP selector - select DAC2 */
2772         {0x22, AC_VERB_SET_CONNECT_SEL, 0x1},
2773         /* Port-D (Line-out) mixer */
2774         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2775         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2776         /* Port-D pin */
2777         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2778         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2779         /* Mono-out mixer */
2780         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2781         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2782         /* Mono-out pin */
2783         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2784         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2785         /* Mono selector */
2786         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2787         /* Port-B (front mic) pin */
2788         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2789         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2790         /* Port-C (rear mic) pin */
2791         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2792         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2793         /* Analog mixer; mute as default */
2794         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2795         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2796         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2797         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2798         /* Analog Mix output amp */
2799         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
2800         /* SPDIF output selector */
2801         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
2802         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
2803         { } /* end */
2804 };
2805
2806 static int patch_ad1884(struct hda_codec *codec)
2807 {
2808         struct ad198x_spec *spec;
2809
2810         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2811         if (spec == NULL)
2812                 return -ENOMEM;
2813
2814         mutex_init(&spec->amp_mutex);
2815         codec->spec = spec;
2816
2817         spec->multiout.max_channels = 2;
2818         spec->multiout.num_dacs = ARRAY_SIZE(ad1884_dac_nids);
2819         spec->multiout.dac_nids = ad1884_dac_nids;
2820         spec->multiout.dig_out_nid = AD1884_SPDIF_OUT;
2821         spec->num_adc_nids = ARRAY_SIZE(ad1884_adc_nids);
2822         spec->adc_nids = ad1884_adc_nids;
2823         spec->capsrc_nids = ad1884_capsrc_nids;
2824         spec->input_mux = &ad1884_capture_source;
2825         spec->num_mixers = 1;
2826         spec->mixers[0] = ad1884_base_mixers;
2827         spec->num_init_verbs = 1;
2828         spec->init_verbs[0] = ad1884_init_verbs;
2829         spec->spdif_route = 0;
2830
2831         codec->patch_ops = ad198x_patch_ops;
2832
2833         return 0;
2834 }
2835
2836 /*
2837  * Lenovo Thinkpad T61/X61
2838  */
2839 static struct hda_input_mux ad1984_thinkpad_capture_source = {
2840         .num_items = 3,
2841         .items = {
2842                 { "Mic", 0x0 },
2843                 { "Internal Mic", 0x1 },
2844                 { "Mix", 0x3 },
2845         },
2846 };
2847
2848 static struct snd_kcontrol_new ad1984_thinkpad_mixers[] = {
2849         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2850         /* HDA_CODEC_VOLUME_IDX("PCM Playback Volume", 1, 0x03, 0x0, HDA_OUTPUT), */
2851         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
2852         HDA_CODEC_MUTE("Speaker Playback Switch", 0x12, 0x0, HDA_OUTPUT),
2853         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
2854         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
2855         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
2856         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
2857         HDA_CODEC_VOLUME("Docking Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
2858         HDA_CODEC_MUTE("Docking Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
2859         HDA_CODEC_VOLUME("Mic Boost", 0x14, 0x0, HDA_INPUT),
2860         HDA_CODEC_VOLUME("Internal Mic Boost", 0x15, 0x0, HDA_INPUT),
2861         HDA_CODEC_VOLUME("Docking Mic Boost", 0x25, 0x0, HDA_OUTPUT),
2862         HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x03, HDA_INPUT),
2863         HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x03, HDA_INPUT),
2864         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
2865         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
2866         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
2867         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
2868         {
2869                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2870                 /* The multiple "Capture Source" controls confuse alsamixer
2871                  * So call somewhat different..
2872                  * FIXME: the controls appear in the "playback" view!
2873                  */
2874                 /* .name = "Capture Source", */
2875                 .name = "Input Source",
2876                 .count = 2,
2877                 .info = ad198x_mux_enum_info,
2878                 .get = ad198x_mux_enum_get,
2879                 .put = ad198x_mux_enum_put,
2880         },
2881         { } /* end */
2882 };
2883
2884 /* additional verbs */
2885 static struct hda_verb ad1984_thinkpad_init_verbs[] = {
2886         /* Port-E (docking station mic) pin */
2887         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2888         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2889         /* docking mic boost */
2890         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2891         /* Analog mixer - docking mic; mute as default */
2892         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2893         /* enable EAPD bit */
2894         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x02},
2895         { } /* end */
2896 };
2897
2898 /* Digial MIC ADC NID 0x05 + 0x06 */
2899 static int ad1984_pcm_dmic_prepare(struct hda_pcm_stream *hinfo,
2900                                    struct hda_codec *codec,
2901                                    unsigned int stream_tag,
2902                                    unsigned int format,
2903                                    struct snd_pcm_substream *substream)
2904 {
2905         snd_hda_codec_setup_stream(codec, 0x05 + substream->number,
2906                                    stream_tag, 0, format);
2907         return 0;
2908 }
2909
2910 static int ad1984_pcm_dmic_cleanup(struct hda_pcm_stream *hinfo,
2911                                    struct hda_codec *codec,
2912                                    struct snd_pcm_substream *substream)
2913 {
2914         snd_hda_codec_setup_stream(codec, 0x05 + substream->number,
2915                                    0, 0, 0);
2916         return 0;
2917 }
2918
2919 static struct hda_pcm_stream ad1984_pcm_dmic_capture = {
2920         .substreams = 2,
2921         .channels_min = 2,
2922         .channels_max = 2,
2923         .nid = 0x05,
2924         .ops = {
2925                 .prepare = ad1984_pcm_dmic_prepare,
2926                 .cleanup = ad1984_pcm_dmic_cleanup
2927         },
2928 };
2929
2930 static int ad1984_build_pcms(struct hda_codec *codec)
2931 {
2932         struct ad198x_spec *spec = codec->spec;
2933         struct hda_pcm *info;
2934         int err;
2935
2936         err = ad198x_build_pcms(codec);
2937         if (err < 0)
2938                 return err;
2939
2940         info = spec->pcm_rec + codec->num_pcms;
2941         codec->num_pcms++;
2942         info->name = "AD1984 Digital Mic";
2943         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad1984_pcm_dmic_capture;
2944         return 0;
2945 }
2946
2947 /* models */
2948 enum {
2949         AD1984_BASIC,
2950         AD1984_THINKPAD,
2951         AD1984_MODELS
2952 };
2953
2954 static const char *ad1984_models[AD1984_MODELS] = {
2955         [AD1984_BASIC]          = "basic",
2956         [AD1984_THINKPAD]       = "thinkpad",
2957 };
2958
2959 static struct snd_pci_quirk ad1984_cfg_tbl[] = {
2960         /* Lenovo Thinkpad T61/X61 */
2961         SND_PCI_QUIRK(0x17aa, 0, "Lenovo Thinkpad", AD1984_THINKPAD),
2962         {}
2963 };
2964
2965 static int patch_ad1984(struct hda_codec *codec)
2966 {
2967         struct ad198x_spec *spec;
2968         int board_config, err;
2969
2970         err = patch_ad1884(codec);
2971         if (err < 0)
2972                 return err;
2973         spec = codec->spec;
2974         board_config = snd_hda_check_board_config(codec, AD1984_MODELS,
2975                                                   ad1984_models, ad1984_cfg_tbl);
2976         switch (board_config) {
2977         case AD1984_BASIC:
2978                 /* additional digital mics */
2979                 spec->mixers[spec->num_mixers++] = ad1984_dmic_mixers;
2980                 codec->patch_ops.build_pcms = ad1984_build_pcms;
2981                 break;
2982         case AD1984_THINKPAD:
2983                 spec->multiout.dig_out_nid = 0;
2984                 spec->input_mux = &ad1984_thinkpad_capture_source;
2985                 spec->mixers[0] = ad1984_thinkpad_mixers;
2986                 spec->init_verbs[spec->num_init_verbs++] = ad1984_thinkpad_init_verbs;
2987                 break;
2988         }
2989         return 0;
2990 }
2991
2992
2993 /*
2994  * AD1882
2995  *
2996  * port-A - front hp-out
2997  * port-B - front mic-in
2998  * port-C - rear line-in, shared surr-out (3stack)
2999  * port-D - rear line-out
3000  * port-E - rear mic-in, shared clfe-out (3stack)
3001  * port-F - rear surr-out (6stack)
3002  * port-G - rear clfe-out (6stack)
3003  */
3004
3005 static hda_nid_t ad1882_dac_nids[3] = {
3006         0x04, 0x03, 0x05
3007 };
3008
3009 static hda_nid_t ad1882_adc_nids[2] = {
3010         0x08, 0x09,
3011 };
3012
3013 static hda_nid_t ad1882_capsrc_nids[2] = {
3014         0x0c, 0x0d,
3015 };
3016
3017 #define AD1882_SPDIF_OUT        0x02
3018
3019 /* list: 0x11, 0x39, 0x3a, 0x18, 0x3c, 0x3b, 0x12, 0x20 */
3020 static struct hda_input_mux ad1882_capture_source = {
3021         .num_items = 5,
3022         .items = {
3023                 { "Front Mic", 0x1 },
3024                 { "Mic", 0x4 },
3025                 { "Line", 0x2 },
3026                 { "CD", 0x3 },
3027                 { "Mix", 0x7 },
3028         },
3029 };
3030
3031 static struct snd_kcontrol_new ad1882_base_mixers[] = {
3032         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
3033         HDA_CODEC_VOLUME("Surround Playback Volume", 0x03, 0x0, HDA_OUTPUT),
3034         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
3035         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
3036         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
3037         HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3038         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
3039         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
3040         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3041         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3042         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
3043         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
3044         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x04, HDA_INPUT),
3045         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x04, HDA_INPUT),
3046         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x06, HDA_INPUT),
3047         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x06, HDA_INPUT),
3048         HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x07, HDA_INPUT),
3049         HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x07, HDA_INPUT),
3050         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
3051         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
3052         HDA_CODEC_VOLUME("Line-In Boost", 0x3a, 0x0, HDA_OUTPUT),
3053         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3054         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3055         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3056         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3057         {
3058                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3059                 /* The multiple "Capture Source" controls confuse alsamixer
3060                  * So call somewhat different..
3061                  * FIXME: the controls appear in the "playback" view!
3062                  */
3063                 /* .name = "Capture Source", */
3064                 .name = "Input Source",
3065                 .count = 2,
3066                 .info = ad198x_mux_enum_info,
3067                 .get = ad198x_mux_enum_get,
3068                 .put = ad198x_mux_enum_put,
3069         },
3070         /* SPDIF controls */
3071         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
3072         {
3073                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3074                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
3075                 /* identical with ad1983 */
3076                 .info = ad1983_spdif_route_info,
3077                 .get = ad1983_spdif_route_get,
3078                 .put = ad1983_spdif_route_put,
3079         },
3080         { } /* end */
3081 };
3082
3083 static struct snd_kcontrol_new ad1882_3stack_mixers[] = {
3084         HDA_CODEC_MUTE("Surround Playback Switch", 0x15, 0x0, HDA_OUTPUT),
3085         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x17, 1, 0x0, HDA_OUTPUT),
3086         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x17, 2, 0x0, HDA_OUTPUT),
3087         {
3088                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3089                 .name = "Channel Mode",
3090                 .info = ad198x_ch_mode_info,
3091                 .get = ad198x_ch_mode_get,
3092                 .put = ad198x_ch_mode_put,
3093         },
3094         { } /* end */
3095 };
3096
3097 static struct snd_kcontrol_new ad1882_6stack_mixers[] = {
3098         HDA_CODEC_MUTE("Surround Playback Switch", 0x16, 0x0, HDA_OUTPUT),
3099         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x24, 1, 0x0, HDA_OUTPUT),
3100         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x24, 2, 0x0, HDA_OUTPUT),
3101         { } /* end */
3102 };
3103
3104 static struct hda_verb ad1882_ch2_init[] = {
3105         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3106         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3107         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3108         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3109         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3110         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3111         { } /* end */
3112 };
3113
3114 static struct hda_verb ad1882_ch4_init[] = {
3115         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3116         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3117         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3118         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3119         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3120         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3121         { } /* end */
3122 };
3123
3124 static struct hda_verb ad1882_ch6_init[] = {
3125         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3126         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3127         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3128         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3129         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3130         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3131         { } /* end */
3132 };
3133
3134 static struct hda_channel_mode ad1882_modes[3] = {
3135         { 2, ad1882_ch2_init },
3136         { 4, ad1882_ch4_init },
3137         { 6, ad1882_ch6_init },
3138 };
3139
3140 /*
3141  * initialization verbs
3142  */
3143 static struct hda_verb ad1882_init_verbs[] = {
3144         /* DACs; mute as default */
3145         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3146         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3147         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3148         /* Port-A (HP) mixer */
3149         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3150         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3151         /* Port-A pin */
3152         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3153         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3154         /* HP selector - select DAC2 */
3155         {0x37, AC_VERB_SET_CONNECT_SEL, 0x1},
3156         /* Port-D (Line-out) mixer */
3157         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3158         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3159         /* Port-D pin */
3160         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3161         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3162         /* Mono-out mixer */
3163         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3164         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3165         /* Mono-out pin */
3166         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3167         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3168         /* Port-B (front mic) pin */
3169         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3170         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3171         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
3172         /* Port-C (line-in) pin */
3173         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3174         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3175         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
3176         /* Port-C mixer - mute as input */
3177         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3178         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3179         /* Port-E (mic-in) pin */
3180         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3181         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3182         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
3183         /* Port-E mixer - mute as input */
3184         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3185         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3186         /* Port-F (surround) */
3187         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3188         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3189         /* Port-G (CLFE) */
3190         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3191         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3192         /* Analog mixer; mute as default */
3193         /* list: 0x39, 0x3a, 0x11, 0x12, 0x3c, 0x3b, 0x18, 0x1a */
3194         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3195         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3196         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3197         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3198         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3199         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
3200         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
3201         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
3202         /* Analog Mix output amp */
3203         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
3204         /* SPDIF output selector */
3205         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
3206         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
3207         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
3208         { } /* end */
3209 };
3210
3211 /* models */
3212 enum {
3213         AD1882_3STACK,
3214         AD1882_6STACK,
3215         AD1882_MODELS
3216 };
3217
3218 static const char *ad1882_models[AD1986A_MODELS] = {
3219         [AD1882_3STACK]         = "3stack",
3220         [AD1882_6STACK]         = "6stack",
3221 };
3222
3223
3224 static int patch_ad1882(struct hda_codec *codec)
3225 {
3226         struct ad198x_spec *spec;
3227         int board_config;
3228
3229         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3230         if (spec == NULL)
3231                 return -ENOMEM;
3232
3233         mutex_init(&spec->amp_mutex);
3234         codec->spec = spec;
3235
3236         spec->multiout.max_channels = 6;
3237         spec->multiout.num_dacs = 3;
3238         spec->multiout.dac_nids = ad1882_dac_nids;
3239         spec->multiout.dig_out_nid = AD1882_SPDIF_OUT;
3240         spec->num_adc_nids = ARRAY_SIZE(ad1882_adc_nids);
3241         spec->adc_nids = ad1882_adc_nids;
3242         spec->capsrc_nids = ad1882_capsrc_nids;
3243         spec->input_mux = &ad1882_capture_source;
3244         spec->num_mixers = 1;
3245         spec->mixers[0] = ad1882_base_mixers;
3246         spec->num_init_verbs = 1;
3247         spec->init_verbs[0] = ad1882_init_verbs;
3248         spec->spdif_route = 0;
3249
3250         codec->patch_ops = ad198x_patch_ops;
3251
3252         /* override some parameters */
3253         board_config = snd_hda_check_board_config(codec, AD1882_MODELS,
3254                                                   ad1882_models, NULL);
3255         switch (board_config) {
3256         default:
3257         case AD1882_3STACK:
3258                 spec->num_mixers = 2;
3259                 spec->mixers[1] = ad1882_3stack_mixers;
3260                 spec->channel_mode = ad1882_modes;
3261                 spec->num_channel_mode = ARRAY_SIZE(ad1882_modes);
3262                 spec->need_dac_fix = 1;
3263                 spec->multiout.max_channels = 2;
3264                 spec->multiout.num_dacs = 1;
3265                 break;
3266         case AD1882_6STACK:
3267                 spec->num_mixers = 2;
3268                 spec->mixers[1] = ad1882_6stack_mixers;
3269                 break;
3270         }
3271         return 0;
3272 }
3273
3274
3275 /*
3276  * patch entries
3277  */
3278 struct hda_codec_preset snd_hda_preset_analog[] = {
3279         { .id = 0x11d41882, .name = "AD1882", .patch = patch_ad1882 },
3280         { .id = 0x11d41884, .name = "AD1884", .patch = patch_ad1884 },
3281         { .id = 0x11d41981, .name = "AD1981", .patch = patch_ad1981 },
3282         { .id = 0x11d41983, .name = "AD1983", .patch = patch_ad1983 },
3283         { .id = 0x11d41984, .name = "AD1984", .patch = patch_ad1984 },
3284         { .id = 0x11d41986, .name = "AD1986A", .patch = patch_ad1986a },
3285         { .id = 0x11d41988, .name = "AD1988", .patch = patch_ad1988 },
3286         { .id = 0x11d4198b, .name = "AD1988B", .patch = patch_ad1988 },
3287         {} /* terminator */
3288 };