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