[ALSA] Added model for ASUS M2NPV-VM mobo
[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 static int ad1986a_pcm_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
456                                unsigned int size, unsigned int __user *_tlv)
457 {
458         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
459         struct ad198x_spec *ad = codec->spec;
460
461         mutex_lock(&ad->amp_mutex);
462         snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, _tlv);
463         mutex_unlock(&ad->amp_mutex);
464         return 0;
465 }
466
467
468 #define ad1986a_pcm_amp_sw_info         snd_hda_mixer_amp_switch_info
469
470 static int ad1986a_pcm_amp_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
471 {
472         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
473         struct ad198x_spec *ad = codec->spec;
474
475         mutex_lock(&ad->amp_mutex);
476         snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
477         mutex_unlock(&ad->amp_mutex);
478         return 0;
479 }
480
481 static int ad1986a_pcm_amp_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
482 {
483         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
484         struct ad198x_spec *ad = codec->spec;
485         int i, change = 0;
486
487         mutex_lock(&ad->amp_mutex);
488         for (i = 0; i < ARRAY_SIZE(ad1986a_dac_nids); i++) {
489                 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(ad1986a_dac_nids[i], 3, 0, HDA_OUTPUT);
490                 change |= snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
491         }
492         kcontrol->private_value = HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT);
493         mutex_unlock(&ad->amp_mutex);
494         return change;
495 }
496
497 /*
498  * mixers
499  */
500 static struct snd_kcontrol_new ad1986a_mixers[] = {
501         {
502                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
503                 .name = "PCM Playback Volume",
504                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
505                           SNDRV_CTL_ELEM_ACCESS_TLV_READ |
506                           SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
507                 .info = ad1986a_pcm_amp_vol_info,
508                 .get = ad1986a_pcm_amp_vol_get,
509                 .put = ad1986a_pcm_amp_vol_put,
510                 .tlv.c = ad1986a_pcm_amp_tlv,
511                 .private_value = HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT)
512         },
513         {
514                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
515                 .name = "PCM Playback Switch",
516                 .info = ad1986a_pcm_amp_sw_info,
517                 .get = ad1986a_pcm_amp_sw_get,
518                 .put = ad1986a_pcm_amp_sw_put,
519                 .private_value = HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT)
520         },
521         HDA_CODEC_VOLUME("Front Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
522         HDA_CODEC_MUTE("Front Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
523         HDA_CODEC_VOLUME("Surround Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
524         HDA_CODEC_MUTE("Surround Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
525         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x1d, 1, 0x0, HDA_OUTPUT),
526         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x1d, 2, 0x0, HDA_OUTPUT),
527         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x1d, 1, 0x0, HDA_OUTPUT),
528         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x1d, 2, 0x0, HDA_OUTPUT),
529         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x1a, 0x0, HDA_OUTPUT),
530         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1a, 0x0, HDA_OUTPUT),
531         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
532         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
533         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
534         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
535         HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
536         HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
537         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
538         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
539         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x18, 0x0, HDA_OUTPUT),
540         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x18, 0x0, HDA_OUTPUT),
541         HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
542         HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT),
543         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
544         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
545         {
546                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
547                 .name = "Capture Source",
548                 .info = ad198x_mux_enum_info,
549                 .get = ad198x_mux_enum_get,
550                 .put = ad198x_mux_enum_put,
551         },
552         HDA_CODEC_MUTE("Stereo Downmix Switch", 0x09, 0x0, HDA_OUTPUT),
553         { } /* end */
554 };
555
556 /* additional mixers for 3stack mode */
557 static struct snd_kcontrol_new ad1986a_3st_mixers[] = {
558         {
559                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
560                 .name = "Channel Mode",
561                 .info = ad198x_ch_mode_info,
562                 .get = ad198x_ch_mode_get,
563                 .put = ad198x_ch_mode_put,
564         },
565         { } /* end */
566 };
567
568 /* laptop model - 2ch only */
569 static hda_nid_t ad1986a_laptop_dac_nids[1] = { AD1986A_FRONT_DAC };
570
571 static struct snd_kcontrol_new ad1986a_laptop_mixers[] = {
572         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
573         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
574         HDA_CODEC_VOLUME("Master Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
575         HDA_CODEC_MUTE("Master Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
576         /* HDA_CODEC_VOLUME("Headphone Playback Volume", 0x1a, 0x0, HDA_OUTPUT),
577            HDA_CODEC_MUTE("Headphone Playback Switch", 0x1a, 0x0, HDA_OUTPUT), */
578         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
579         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
580         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
581         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
582         HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
583         HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
584         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
585         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
586         /* HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x18, 0x0, HDA_OUTPUT),
587            HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x18, 0x0, HDA_OUTPUT),
588            HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
589            HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT), */
590         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
591         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
592         {
593                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
594                 .name = "Capture Source",
595                 .info = ad198x_mux_enum_info,
596                 .get = ad198x_mux_enum_get,
597                 .put = ad198x_mux_enum_put,
598         },
599         { } /* end */
600 };
601
602 /* laptop-eapd model - 2ch only */
603
604 /* master controls both pins 0x1a and 0x1b */
605 static int ad1986a_laptop_master_vol_put(struct snd_kcontrol *kcontrol,
606                                          struct snd_ctl_elem_value *ucontrol)
607 {
608         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
609         long *valp = ucontrol->value.integer.value;
610         int change;
611
612         change = snd_hda_codec_amp_update(codec, 0x1a, 0, HDA_OUTPUT, 0,
613                                           0x7f, valp[0] & 0x7f);
614         change |= snd_hda_codec_amp_update(codec, 0x1a, 1, HDA_OUTPUT, 0,
615                                            0x7f, valp[1] & 0x7f);
616         snd_hda_codec_amp_update(codec, 0x1b, 0, HDA_OUTPUT, 0,
617                                  0x7f, valp[0] & 0x7f);
618         snd_hda_codec_amp_update(codec, 0x1b, 1, HDA_OUTPUT, 0,
619                                  0x7f, valp[1] & 0x7f);
620         return change;
621 }
622
623 static int ad1986a_laptop_master_sw_put(struct snd_kcontrol *kcontrol,
624                                         struct snd_ctl_elem_value *ucontrol)
625 {
626         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
627         long *valp = ucontrol->value.integer.value;
628         int change;
629
630         change = snd_hda_codec_amp_update(codec, 0x1a, 0, HDA_OUTPUT, 0,
631                                           0x80, valp[0] ? 0 : 0x80);
632         change |= snd_hda_codec_amp_update(codec, 0x1a, 1, HDA_OUTPUT, 0,
633                                            0x80, valp[1] ? 0 : 0x80);
634         snd_hda_codec_amp_update(codec, 0x1b, 0, HDA_OUTPUT, 0,
635                                  0x80, valp[0] ? 0 : 0x80);
636         snd_hda_codec_amp_update(codec, 0x1b, 1, HDA_OUTPUT, 0,
637                                  0x80, valp[1] ? 0 : 0x80);
638         return change;
639 }
640
641 static struct hda_input_mux ad1986a_laptop_eapd_capture_source = {
642         .num_items = 3,
643         .items = {
644                 { "Mic", 0x0 },
645                 { "Internal Mic", 0x4 },
646                 { "Mix", 0x5 },
647         },
648 };
649
650 static struct snd_kcontrol_new ad1986a_laptop_eapd_mixers[] = {
651         {
652                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
653                 .name = "Master Playback Volume",
654                 .info = snd_hda_mixer_amp_volume_info,
655                 .get = snd_hda_mixer_amp_volume_get,
656                 .put = ad1986a_laptop_master_vol_put,
657                 .private_value = HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
658         },
659         {
660                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
661                 .name = "Master Playback Switch",
662                 .info = snd_hda_mixer_amp_switch_info,
663                 .get = snd_hda_mixer_amp_switch_get,
664                 .put = ad1986a_laptop_master_sw_put,
665                 .private_value = HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
666         },
667         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
668         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
669         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0x0, HDA_OUTPUT),
670         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0x0, HDA_OUTPUT),
671         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
672         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
673         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
674         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
675         {
676                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
677                 .name = "Capture Source",
678                 .info = ad198x_mux_enum_info,
679                 .get = ad198x_mux_enum_get,
680                 .put = ad198x_mux_enum_put,
681         },
682         {
683                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
684                 .name = "External Amplifier",
685                 .info = ad198x_eapd_info,
686                 .get = ad198x_eapd_get,
687                 .put = ad198x_eapd_put,
688                 .private_value = 0x1b | (1 << 8), /* port-D, inversed */
689         },
690         { } /* end */
691 };
692
693 /*
694  * initialization verbs
695  */
696 static struct hda_verb ad1986a_init_verbs[] = {
697         /* Front, Surround, CLFE DAC; mute as default */
698         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
699         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
700         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
701         /* Downmix - off */
702         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
703         /* HP, Line-Out, Surround, CLFE selectors */
704         {0x0a, AC_VERB_SET_CONNECT_SEL, 0x0},
705         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x0},
706         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
707         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
708         /* Mono selector */
709         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x0},
710         /* Mic selector: Mic 1/2 pin */
711         {0x0f, AC_VERB_SET_CONNECT_SEL, 0x0},
712         /* Line-in selector: Line-in */
713         {0x10, AC_VERB_SET_CONNECT_SEL, 0x0},
714         /* Mic 1/2 swap */
715         {0x11, AC_VERB_SET_CONNECT_SEL, 0x0},
716         /* Record selector: mic */
717         {0x12, AC_VERB_SET_CONNECT_SEL, 0x0},
718         /* Mic, Phone, CD, Aux, Line-In amp; mute as default */
719         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
720         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
721         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
722         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
723         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
724         /* PC beep */
725         {0x18, AC_VERB_SET_CONNECT_SEL, 0x0},
726         /* HP, Line-Out, Surround, CLFE, Mono pins; mute as default */
727         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
728         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
729         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
730         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
731         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
732         /* HP Pin */
733         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
734         /* Front, Surround, CLFE Pins */
735         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
736         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
737         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
738         /* Mono Pin */
739         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
740         /* Mic Pin */
741         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
742         /* Line, Aux, CD, Beep-In Pin */
743         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
744         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
745         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
746         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
747         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
748         { } /* end */
749 };
750
751 /* additional verbs for 3-stack model */
752 static struct hda_verb ad1986a_3st_init_verbs[] = {
753         /* Mic and line-in selectors */
754         {0x0f, AC_VERB_SET_CONNECT_SEL, 0x2},
755         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
756         { } /* end */
757 };
758
759 static struct hda_verb ad1986a_ch2_init[] = {
760         /* Surround out -> Line In */
761         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
762         { 0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
763         /* CLFE -> Mic in */
764         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
765         { 0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
766         { } /* end */
767 };
768
769 static struct hda_verb ad1986a_ch4_init[] = {
770         /* Surround out -> Surround */
771         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
772         { 0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
773         /* CLFE -> Mic in */
774         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
775         { 0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
776         { } /* end */
777 };
778
779 static struct hda_verb ad1986a_ch6_init[] = {
780         /* Surround out -> Surround out */
781         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
782         { 0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
783         /* CLFE -> CLFE */
784         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
785         { 0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
786         { } /* end */
787 };
788
789 static struct hda_channel_mode ad1986a_modes[3] = {
790         { 2, ad1986a_ch2_init },
791         { 4, ad1986a_ch4_init },
792         { 6, ad1986a_ch6_init },
793 };
794
795 /* eapd initialization */
796 static struct hda_verb ad1986a_eapd_init_verbs[] = {
797         {0x1b, AC_VERB_SET_EAPD_BTLENABLE, 0x00},
798         {}
799 };
800
801 /* models */
802 enum { AD1986A_6STACK, AD1986A_3STACK, AD1986A_LAPTOP, AD1986A_LAPTOP_EAPD };
803
804 static struct hda_board_config ad1986a_cfg_tbl[] = {
805         { .modelname = "6stack",        .config = AD1986A_6STACK },
806         { .modelname = "3stack",        .config = AD1986A_3STACK },
807         { .pci_subvendor = 0x10de, .pci_subdevice = 0xcb84,
808           .config = AD1986A_3STACK }, /* ASUS A8N-VM CSM */
809         { .pci_subvendor = 0x1043, .pci_subdevice = 0x81b3,
810           .config = AD1986A_3STACK }, /* ASUS P5RD2-VM / P5GPL-X SE */
811         { .pci_subvendor = 0x1043, .pci_subdevice = 0x81cb,
812           .config = AD1986A_3STACK }, /* ASUS M2NPV-VM */
813         { .modelname = "laptop",        .config = AD1986A_LAPTOP },
814         { .pci_subvendor = 0x144d, .pci_subdevice = 0xc01e,
815           .config = AD1986A_LAPTOP }, /* FSC V2060 */
816         { .pci_subvendor = 0x17c0, .pci_subdevice = 0x2017,
817           .config = AD1986A_LAPTOP }, /* Samsung M50 */
818         { .pci_subvendor = 0x1043, .pci_subdevice = 0x818f,
819           .config = AD1986A_LAPTOP }, /* ASUS P5GV-MX */
820         { .modelname = "laptop-eapd",   .config = AD1986A_LAPTOP_EAPD },
821         { .pci_subvendor = 0x144d, .pci_subdevice = 0xc023,
822           .config = AD1986A_LAPTOP_EAPD }, /* Samsung X60 Chane */
823         { .pci_subvendor = 0x144d, .pci_subdevice = 0xc024,
824           .config = AD1986A_LAPTOP_EAPD }, /* Samsung R65-T2300 Charis */
825         { .pci_subvendor = 0x144d, .pci_subdevice = 0xc026,
826           .config = AD1986A_LAPTOP_EAPD }, /* Samsung X10-T2300 Culesa */
827         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1153,
828           .config = AD1986A_LAPTOP_EAPD }, /* ASUS M9 */
829         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1213,
830           .config = AD1986A_LAPTOP_EAPD }, /* ASUS A6J */
831         { .pci_subvendor = 0x1043, .pci_subdevice = 0x11f7,
832           .config = AD1986A_LAPTOP_EAPD }, /* ASUS U5A */
833         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1297,
834           .config = AD1986A_LAPTOP_EAPD }, /* ASUS Z62F */
835         { .pci_subvendor = 0x103c, .pci_subdevice = 0x30af,
836           .config = AD1986A_LAPTOP_EAPD }, /* HP Compaq Presario B2800 */
837         { .pci_subvendor = 0x17aa, .pci_subdevice = 0x2066,
838           .config = AD1986A_LAPTOP_EAPD }, /* Lenovo 3000 N100-07684JU */
839         {}
840 };
841
842 static int patch_ad1986a(struct hda_codec *codec)
843 {
844         struct ad198x_spec *spec;
845         int board_config;
846
847         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
848         if (spec == NULL)
849                 return -ENOMEM;
850
851         mutex_init(&spec->amp_mutex);
852         codec->spec = spec;
853
854         spec->multiout.max_channels = 6;
855         spec->multiout.num_dacs = ARRAY_SIZE(ad1986a_dac_nids);
856         spec->multiout.dac_nids = ad1986a_dac_nids;
857         spec->multiout.dig_out_nid = AD1986A_SPDIF_OUT;
858         spec->num_adc_nids = 1;
859         spec->adc_nids = ad1986a_adc_nids;
860         spec->capsrc_nids = ad1986a_capsrc_nids;
861         spec->input_mux = &ad1986a_capture_source;
862         spec->num_mixers = 1;
863         spec->mixers[0] = ad1986a_mixers;
864         spec->num_init_verbs = 1;
865         spec->init_verbs[0] = ad1986a_init_verbs;
866
867         codec->patch_ops = ad198x_patch_ops;
868
869         /* override some parameters */
870         board_config = snd_hda_check_board_config(codec, ad1986a_cfg_tbl);
871         switch (board_config) {
872         case AD1986A_3STACK:
873                 spec->num_mixers = 2;
874                 spec->mixers[1] = ad1986a_3st_mixers;
875                 spec->num_init_verbs = 3;
876                 spec->init_verbs[1] = ad1986a_3st_init_verbs;
877                 spec->init_verbs[2] = ad1986a_ch2_init;
878                 spec->channel_mode = ad1986a_modes;
879                 spec->num_channel_mode = ARRAY_SIZE(ad1986a_modes);
880                 spec->need_dac_fix = 1;
881                 spec->multiout.max_channels = 2;
882                 spec->multiout.num_dacs = 1;
883                 break;
884         case AD1986A_LAPTOP:
885                 spec->mixers[0] = ad1986a_laptop_mixers;
886                 spec->multiout.max_channels = 2;
887                 spec->multiout.num_dacs = 1;
888                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
889                 break;
890         case AD1986A_LAPTOP_EAPD:
891                 spec->mixers[0] = ad1986a_laptop_eapd_mixers;
892                 spec->num_init_verbs = 2;
893                 spec->init_verbs[1] = ad1986a_eapd_init_verbs;
894                 spec->multiout.max_channels = 2;
895                 spec->multiout.num_dacs = 1;
896                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
897                 spec->multiout.dig_out_nid = 0;
898                 spec->input_mux = &ad1986a_laptop_eapd_capture_source;
899                 break;
900         }
901
902         return 0;
903 }
904
905 /*
906  * AD1983 specific
907  */
908
909 #define AD1983_SPDIF_OUT        0x02
910 #define AD1983_DAC              0x03
911 #define AD1983_ADC              0x04
912
913 static hda_nid_t ad1983_dac_nids[1] = { AD1983_DAC };
914 static hda_nid_t ad1983_adc_nids[1] = { AD1983_ADC };
915 static hda_nid_t ad1983_capsrc_nids[1] = { 0x15 };
916
917 static struct hda_input_mux ad1983_capture_source = {
918         .num_items = 4,
919         .items = {
920                 { "Mic", 0x0 },
921                 { "Line", 0x1 },
922                 { "Mix", 0x2 },
923                 { "Mix Mono", 0x3 },
924         },
925 };
926
927 /*
928  * SPDIF playback route
929  */
930 static int ad1983_spdif_route_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
931 {
932         static char *texts[] = { "PCM", "ADC" };
933
934         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
935         uinfo->count = 1;
936         uinfo->value.enumerated.items = 2;
937         if (uinfo->value.enumerated.item > 1)
938                 uinfo->value.enumerated.item = 1;
939         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
940         return 0;
941 }
942
943 static int ad1983_spdif_route_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
944 {
945         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
946         struct ad198x_spec *spec = codec->spec;
947
948         ucontrol->value.enumerated.item[0] = spec->spdif_route;
949         return 0;
950 }
951
952 static int ad1983_spdif_route_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
953 {
954         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
955         struct ad198x_spec *spec = codec->spec;
956
957         if (spec->spdif_route != ucontrol->value.enumerated.item[0]) {
958                 spec->spdif_route = ucontrol->value.enumerated.item[0];
959                 snd_hda_codec_write(codec, spec->multiout.dig_out_nid, 0,
960                                     AC_VERB_SET_CONNECT_SEL, spec->spdif_route);
961                 return 1;
962         }
963         return 0;
964 }
965
966 static struct snd_kcontrol_new ad1983_mixers[] = {
967         HDA_CODEC_VOLUME("Front Playback Volume", 0x05, 0x0, HDA_OUTPUT),
968         HDA_CODEC_MUTE("Front Playback Switch", 0x05, 0x0, HDA_OUTPUT),
969         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x06, 0x0, HDA_OUTPUT),
970         HDA_CODEC_MUTE("Headphone Playback Switch", 0x06, 0x0, HDA_OUTPUT),
971         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x07, 1, 0x0, HDA_OUTPUT),
972         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x07, 1, 0x0, HDA_OUTPUT),
973         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
974         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
975         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
976         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
977         HDA_CODEC_VOLUME("Line Playback Volume", 0x13, 0x0, HDA_OUTPUT),
978         HDA_CODEC_MUTE("Line Playback Switch", 0x13, 0x0, HDA_OUTPUT),
979         HDA_CODEC_VOLUME_MONO("PC Speaker Playback Volume", 0x10, 1, 0x0, HDA_OUTPUT),
980         HDA_CODEC_MUTE_MONO("PC Speaker Playback Switch", 0x10, 1, 0x0, HDA_OUTPUT),
981         HDA_CODEC_VOLUME("Mic Boost", 0x0c, 0x0, HDA_OUTPUT),
982         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
983         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
984         {
985                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
986                 .name = "Capture Source",
987                 .info = ad198x_mux_enum_info,
988                 .get = ad198x_mux_enum_get,
989                 .put = ad198x_mux_enum_put,
990         },
991         {
992                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
993                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
994                 .info = ad1983_spdif_route_info,
995                 .get = ad1983_spdif_route_get,
996                 .put = ad1983_spdif_route_put,
997         },
998         { } /* end */
999 };
1000
1001 static struct hda_verb ad1983_init_verbs[] = {
1002         /* Front, HP, Mono; mute as default */
1003         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1004         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1005         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1006         /* Beep, PCM, Mic, Line-In: mute */
1007         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1008         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1009         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1010         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1011         /* Front, HP selectors; from Mix */
1012         {0x05, AC_VERB_SET_CONNECT_SEL, 0x01},
1013         {0x06, AC_VERB_SET_CONNECT_SEL, 0x01},
1014         /* Mono selector; from Mix */
1015         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x03},
1016         /* Mic selector; Mic */
1017         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
1018         /* Line-in selector: Line-in */
1019         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
1020         /* Mic boost: 0dB */
1021         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1022         /* Record selector: mic */
1023         {0x15, AC_VERB_SET_CONNECT_SEL, 0x0},
1024         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1025         /* SPDIF route: PCM */
1026         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0},
1027         /* Front Pin */
1028         {0x05, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1029         /* HP Pin */
1030         {0x06, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
1031         /* Mono Pin */
1032         {0x07, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1033         /* Mic Pin */
1034         {0x08, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1035         /* Line Pin */
1036         {0x09, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
1037         { } /* end */
1038 };
1039
1040
1041 static int patch_ad1983(struct hda_codec *codec)
1042 {
1043         struct ad198x_spec *spec;
1044
1045         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1046         if (spec == NULL)
1047                 return -ENOMEM;
1048
1049         mutex_init(&spec->amp_mutex);
1050         codec->spec = spec;
1051
1052         spec->multiout.max_channels = 2;
1053         spec->multiout.num_dacs = ARRAY_SIZE(ad1983_dac_nids);
1054         spec->multiout.dac_nids = ad1983_dac_nids;
1055         spec->multiout.dig_out_nid = AD1983_SPDIF_OUT;
1056         spec->num_adc_nids = 1;
1057         spec->adc_nids = ad1983_adc_nids;
1058         spec->capsrc_nids = ad1983_capsrc_nids;
1059         spec->input_mux = &ad1983_capture_source;
1060         spec->num_mixers = 1;
1061         spec->mixers[0] = ad1983_mixers;
1062         spec->num_init_verbs = 1;
1063         spec->init_verbs[0] = ad1983_init_verbs;
1064         spec->spdif_route = 0;
1065
1066         codec->patch_ops = ad198x_patch_ops;
1067
1068         return 0;
1069 }
1070
1071
1072 /*
1073  * AD1981 HD specific
1074  */
1075
1076 #define AD1981_SPDIF_OUT        0x02
1077 #define AD1981_DAC              0x03
1078 #define AD1981_ADC              0x04
1079
1080 static hda_nid_t ad1981_dac_nids[1] = { AD1981_DAC };
1081 static hda_nid_t ad1981_adc_nids[1] = { AD1981_ADC };
1082 static hda_nid_t ad1981_capsrc_nids[1] = { 0x15 };
1083
1084 /* 0x0c, 0x09, 0x0e, 0x0f, 0x19, 0x05, 0x18, 0x17 */
1085 static struct hda_input_mux ad1981_capture_source = {
1086         .num_items = 7,
1087         .items = {
1088                 { "Front Mic", 0x0 },
1089                 { "Line", 0x1 },
1090                 { "Mix", 0x2 },
1091                 { "Mix Mono", 0x3 },
1092                 { "CD", 0x4 },
1093                 { "Mic", 0x6 },
1094                 { "Aux", 0x7 },
1095         },
1096 };
1097
1098 static struct snd_kcontrol_new ad1981_mixers[] = {
1099         HDA_CODEC_VOLUME("Front Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1100         HDA_CODEC_MUTE("Front Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1101         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1102         HDA_CODEC_MUTE("Headphone Playback Switch", 0x06, 0x0, HDA_OUTPUT),
1103         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x07, 1, 0x0, HDA_OUTPUT),
1104         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x07, 1, 0x0, HDA_OUTPUT),
1105         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1106         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1107         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1108         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1109         HDA_CODEC_VOLUME("Line Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1110         HDA_CODEC_MUTE("Line Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1111         HDA_CODEC_VOLUME("Aux Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
1112         HDA_CODEC_MUTE("Aux Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
1113         HDA_CODEC_VOLUME("Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
1114         HDA_CODEC_MUTE("Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
1115         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1116         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1117         HDA_CODEC_VOLUME_MONO("PC Speaker Playback Volume", 0x0d, 1, 0x0, HDA_OUTPUT),
1118         HDA_CODEC_MUTE_MONO("PC Speaker Playback Switch", 0x0d, 1, 0x0, HDA_OUTPUT),
1119         HDA_CODEC_VOLUME("Front Mic Boost", 0x08, 0x0, HDA_INPUT),
1120         HDA_CODEC_VOLUME("Mic Boost", 0x18, 0x0, HDA_INPUT),
1121         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1122         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1123         {
1124                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1125                 .name = "Capture Source",
1126                 .info = ad198x_mux_enum_info,
1127                 .get = ad198x_mux_enum_get,
1128                 .put = ad198x_mux_enum_put,
1129         },
1130         /* identical with AD1983 */
1131         {
1132                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1133                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1134                 .info = ad1983_spdif_route_info,
1135                 .get = ad1983_spdif_route_get,
1136                 .put = ad1983_spdif_route_put,
1137         },
1138         { } /* end */
1139 };
1140
1141 static struct hda_verb ad1981_init_verbs[] = {
1142         /* Front, HP, Mono; mute as default */
1143         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1144         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1145         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1146         /* Beep, PCM, Front Mic, Line, Rear Mic, Aux, CD-In: mute */
1147         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1148         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1149         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1150         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1151         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1152         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1153         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1154         /* Front, HP selectors; from Mix */
1155         {0x05, AC_VERB_SET_CONNECT_SEL, 0x01},
1156         {0x06, AC_VERB_SET_CONNECT_SEL, 0x01},
1157         /* Mono selector; from Mix */
1158         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x03},
1159         /* Mic Mixer; select Front Mic */
1160         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1161         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1162         /* Mic boost: 0dB */
1163         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1164         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1165         /* Record selector: Front mic */
1166         {0x15, AC_VERB_SET_CONNECT_SEL, 0x0},
1167         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1168         /* SPDIF route: PCM */
1169         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0},
1170         /* Front Pin */
1171         {0x05, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1172         /* HP Pin */
1173         {0x06, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
1174         /* Mono Pin */
1175         {0x07, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1176         /* Front & Rear Mic Pins */
1177         {0x08, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1178         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1179         /* Line Pin */
1180         {0x09, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
1181         /* Digital Beep */
1182         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x00},
1183         /* Line-Out as Input: disabled */
1184         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1185         { } /* end */
1186 };
1187
1188 /*
1189  * Patch for HP nx6320
1190  *
1191  * nx6320 uses EAPD in the reserve way - EAPD-on means the internal
1192  * speaker output enabled _and_ mute-LED off.
1193  */
1194
1195 #define AD1981_HP_EVENT         0x37
1196 #define AD1981_MIC_EVENT        0x38
1197
1198 static struct hda_verb ad1981_hp_init_verbs[] = {
1199         {0x05, AC_VERB_SET_EAPD_BTLENABLE, 0x00 }, /* default off */
1200         /* pin sensing on HP and Mic jacks */
1201         {0x06, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_HP_EVENT},
1202         {0x08, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_MIC_EVENT},
1203         {}
1204 };
1205
1206 /* turn on/off EAPD (+ mute HP) as a master switch */
1207 static int ad1981_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1208                                    struct snd_ctl_elem_value *ucontrol)
1209 {
1210         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1211         struct ad198x_spec *spec = codec->spec;
1212
1213         if (! ad198x_eapd_put(kcontrol, ucontrol))
1214                 return 0;
1215
1216         /* toggle HP mute appropriately */
1217         snd_hda_codec_amp_update(codec, 0x06, 0, HDA_OUTPUT, 0,
1218                                  0x80, spec->cur_eapd ? 0 : 0x80);
1219         snd_hda_codec_amp_update(codec, 0x06, 1, HDA_OUTPUT, 0,
1220                                  0x80, spec->cur_eapd ? 0 : 0x80);
1221         return 1;
1222 }
1223
1224 /* bind volumes of both NID 0x05 and 0x06 */
1225 static int ad1981_hp_master_vol_put(struct snd_kcontrol *kcontrol,
1226                                     struct snd_ctl_elem_value *ucontrol)
1227 {
1228         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1229         long *valp = ucontrol->value.integer.value;
1230         int change;
1231
1232         change = snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1233                                           0x7f, valp[0] & 0x7f);
1234         change |= snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1235                                            0x7f, valp[1] & 0x7f);
1236         snd_hda_codec_amp_update(codec, 0x06, 0, HDA_OUTPUT, 0,
1237                                  0x7f, valp[0] & 0x7f);
1238         snd_hda_codec_amp_update(codec, 0x06, 1, HDA_OUTPUT, 0,
1239                                  0x7f, valp[1] & 0x7f);
1240         return change;
1241 }
1242
1243 /* mute internal speaker if HP is plugged */
1244 static void ad1981_hp_automute(struct hda_codec *codec)
1245 {
1246         unsigned int present;
1247
1248         present = snd_hda_codec_read(codec, 0x06, 0,
1249                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1250         snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1251                                  0x80, present ? 0x80 : 0);
1252         snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1253                                  0x80, present ? 0x80 : 0);
1254 }
1255
1256 /* toggle input of built-in and mic jack appropriately */
1257 static void ad1981_hp_automic(struct hda_codec *codec)
1258 {
1259         static struct hda_verb mic_jack_on[] = {
1260                 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1261                 {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1262                 {}
1263         };
1264         static struct hda_verb mic_jack_off[] = {
1265                 {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1266                 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1267                 {}
1268         };
1269         unsigned int present;
1270
1271         present = snd_hda_codec_read(codec, 0x08, 0,
1272                                  AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1273         if (present)
1274                 snd_hda_sequence_write(codec, mic_jack_on);
1275         else
1276                 snd_hda_sequence_write(codec, mic_jack_off);
1277 }
1278
1279 /* unsolicited event for HP jack sensing */
1280 static void ad1981_hp_unsol_event(struct hda_codec *codec,
1281                                   unsigned int res)
1282 {
1283         res >>= 26;
1284         switch (res) {
1285         case AD1981_HP_EVENT:
1286                 ad1981_hp_automute(codec);
1287                 break;
1288         case AD1981_MIC_EVENT:
1289                 ad1981_hp_automic(codec);
1290                 break;
1291         }
1292 }
1293
1294 static struct hda_input_mux ad1981_hp_capture_source = {
1295         .num_items = 3,
1296         .items = {
1297                 { "Mic", 0x0 },
1298                 { "Docking-Station", 0x1 },
1299                 { "Mix", 0x2 },
1300         },
1301 };
1302
1303 static struct snd_kcontrol_new ad1981_hp_mixers[] = {
1304         {
1305                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1306                 .name = "Master Playback Volume",
1307                 .info = snd_hda_mixer_amp_volume_info,
1308                 .get = snd_hda_mixer_amp_volume_get,
1309                 .put = ad1981_hp_master_vol_put,
1310                 .private_value = HDA_COMPOSE_AMP_VAL(0x05, 3, 0, HDA_OUTPUT),
1311         },
1312         {
1313                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1314                 .name = "Master Playback Switch",
1315                 .info = ad198x_eapd_info,
1316                 .get = ad198x_eapd_get,
1317                 .put = ad1981_hp_master_sw_put,
1318                 .private_value = 0x05,
1319         },
1320         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1321         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1322 #if 0
1323         /* FIXME: analog mic/line loopback doesn't work with my tests...
1324          *        (although recording is OK)
1325          */
1326         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1327         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1328         HDA_CODEC_VOLUME("Docking-Station Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1329         HDA_CODEC_MUTE("Docking-Station Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1330         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
1331         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
1332         /* FIXME: does this laptop have analog CD connection? */
1333         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1334         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1335 #endif
1336         HDA_CODEC_VOLUME("Mic Boost", 0x08, 0x0, HDA_INPUT),
1337         HDA_CODEC_VOLUME("Internal Mic Boost", 0x18, 0x0, HDA_INPUT),
1338         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1339         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1340         {
1341                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1342                 .name = "Capture Source",
1343                 .info = ad198x_mux_enum_info,
1344                 .get = ad198x_mux_enum_get,
1345                 .put = ad198x_mux_enum_put,
1346         },
1347         { } /* end */
1348 };
1349
1350 /* initialize jack-sensing, too */
1351 static int ad1981_hp_init(struct hda_codec *codec)
1352 {
1353         ad198x_init(codec);
1354         ad1981_hp_automute(codec);
1355         ad1981_hp_automic(codec);
1356         return 0;
1357 }
1358
1359 /* configuration for Lenovo Thinkpad T60 */
1360 static struct snd_kcontrol_new ad1981_thinkpad_mixers[] = {
1361         HDA_CODEC_VOLUME("Master Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1362         HDA_CODEC_MUTE("Master Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1363         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1364         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1365         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1366         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1367         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1368         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1369         HDA_CODEC_VOLUME("Mic Boost", 0x08, 0x0, HDA_INPUT),
1370         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1371         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1372         {
1373                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1374                 .name = "Capture Source",
1375                 .info = ad198x_mux_enum_info,
1376                 .get = ad198x_mux_enum_get,
1377                 .put = ad198x_mux_enum_put,
1378         },
1379         /* identical with AD1983 */
1380         {
1381                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1382                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1383                 .info = ad1983_spdif_route_info,
1384                 .get = ad1983_spdif_route_get,
1385                 .put = ad1983_spdif_route_put,
1386         },
1387         { } /* end */
1388 };
1389
1390 static struct hda_input_mux ad1981_thinkpad_capture_source = {
1391         .num_items = 3,
1392         .items = {
1393                 { "Mic", 0x0 },
1394                 { "Mix", 0x2 },
1395                 { "CD", 0x4 },
1396         },
1397 };
1398
1399 /* models */
1400 enum { AD1981_BASIC, AD1981_HP, AD1981_THINKPAD };
1401
1402 static struct hda_board_config ad1981_cfg_tbl[] = {
1403         { .modelname = "hp", .config = AD1981_HP },
1404         /* All HP models */
1405         { .pci_subvendor = 0x103c, .config = AD1981_HP },
1406         { .pci_subvendor = 0x30b0, .pci_subdevice = 0x103c,
1407           .config = AD1981_HP }, /* HP nx6320 (reversed SSID, H/W bug) */
1408         { .modelname = "thinkpad", .config = AD1981_THINKPAD },
1409         /* Lenovo Thinkpad T60/X60/Z6xx */
1410         { .pci_subvendor = 0x17aa, .config = AD1981_THINKPAD },
1411         { .pci_subvendor = 0x1014, .pci_subdevice = 0x0597,
1412           .config = AD1981_THINKPAD }, /* Z60m/t */
1413         { .modelname = "basic", .config = AD1981_BASIC },
1414         {}
1415 };
1416
1417 static int patch_ad1981(struct hda_codec *codec)
1418 {
1419         struct ad198x_spec *spec;
1420         int board_config;
1421
1422         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1423         if (spec == NULL)
1424                 return -ENOMEM;
1425
1426         mutex_init(&spec->amp_mutex);
1427         codec->spec = spec;
1428
1429         spec->multiout.max_channels = 2;
1430         spec->multiout.num_dacs = ARRAY_SIZE(ad1981_dac_nids);
1431         spec->multiout.dac_nids = ad1981_dac_nids;
1432         spec->multiout.dig_out_nid = AD1981_SPDIF_OUT;
1433         spec->num_adc_nids = 1;
1434         spec->adc_nids = ad1981_adc_nids;
1435         spec->capsrc_nids = ad1981_capsrc_nids;
1436         spec->input_mux = &ad1981_capture_source;
1437         spec->num_mixers = 1;
1438         spec->mixers[0] = ad1981_mixers;
1439         spec->num_init_verbs = 1;
1440         spec->init_verbs[0] = ad1981_init_verbs;
1441         spec->spdif_route = 0;
1442
1443         codec->patch_ops = ad198x_patch_ops;
1444
1445         /* override some parameters */
1446         board_config = snd_hda_check_board_config(codec, ad1981_cfg_tbl);
1447         switch (board_config) {
1448         case AD1981_HP:
1449                 spec->mixers[0] = ad1981_hp_mixers;
1450                 spec->num_init_verbs = 2;
1451                 spec->init_verbs[1] = ad1981_hp_init_verbs;
1452                 spec->multiout.dig_out_nid = 0;
1453                 spec->input_mux = &ad1981_hp_capture_source;
1454
1455                 codec->patch_ops.init = ad1981_hp_init;
1456                 codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
1457                 break;
1458         case AD1981_THINKPAD:
1459                 spec->mixers[0] = ad1981_thinkpad_mixers;
1460                 spec->input_mux = &ad1981_thinkpad_capture_source;
1461                 break;
1462         }
1463
1464         return 0;
1465 }
1466
1467
1468 /*
1469  * AD1988
1470  *
1471  * Output pins and routes
1472  *
1473  *        Pin               Mix     Sel     DAC (*)
1474  * port-A 0x11 (mute/hp) <- 0x22 <- 0x37 <- 03/04/06
1475  * port-B 0x14 (mute/hp) <- 0x2b <- 0x30 <- 03/04/06
1476  * port-C 0x15 (mute)    <- 0x2c <- 0x31 <- 05/0a
1477  * port-D 0x12 (mute/hp) <- 0x29         <- 04
1478  * port-E 0x17 (mute/hp) <- 0x26 <- 0x32 <- 05/0a
1479  * port-F 0x16 (mute)    <- 0x2a         <- 06
1480  * port-G 0x24 (mute)    <- 0x27         <- 05
1481  * port-H 0x25 (mute)    <- 0x28         <- 0a
1482  * mono   0x13 (mute/amp)<- 0x1e <- 0x36 <- 03/04/06
1483  *
1484  * DAC0 = 03h, DAC1 = 04h, DAC2 = 05h, DAC3 = 06h, DAC4 = 0ah
1485  * (*) DAC2/3/4 are swapped to DAC3/4/2 on AD198A rev.2 due to a h/w bug.
1486  *
1487  * Input pins and routes
1488  *
1489  *        pin     boost   mix input # / adc input #
1490  * port-A 0x11 -> 0x38 -> mix 2, ADC 0
1491  * port-B 0x14 -> 0x39 -> mix 0, ADC 1
1492  * port-C 0x15 -> 0x3a -> 33:0 - mix 1, ADC 2
1493  * port-D 0x12 -> 0x3d -> mix 3, ADC 8
1494  * port-E 0x17 -> 0x3c -> 34:0 - mix 4, ADC 4
1495  * port-F 0x16 -> 0x3b -> mix 5, ADC 3
1496  * port-G 0x24 -> N/A  -> 33:1 - mix 1, 34:1 - mix 4, ADC 6
1497  * port-H 0x25 -> N/A  -> 33:2 - mix 1, 34:2 - mix 4, ADC 7
1498  *
1499  *
1500  * DAC assignment
1501  *   6stack - front/surr/CLFE/side/opt DACs - 04/06/05/0a/03
1502  *   3stack - front/surr/CLFE/opt DACs - 04/05/0a/03
1503  *
1504  * Inputs of Analog Mix (0x20)
1505  *   0:Port-B (front mic)
1506  *   1:Port-C/G/H (line-in)
1507  *   2:Port-A
1508  *   3:Port-D (line-in/2)
1509  *   4:Port-E/G/H (mic-in)
1510  *   5:Port-F (mic2-in)
1511  *   6:CD
1512  *   7:Beep
1513  *
1514  * ADC selection
1515  *   0:Port-A
1516  *   1:Port-B (front mic-in)
1517  *   2:Port-C (line-in)
1518  *   3:Port-F (mic2-in)
1519  *   4:Port-E (mic-in)
1520  *   5:CD
1521  *   6:Port-G
1522  *   7:Port-H
1523  *   8:Port-D (line-in/2)
1524  *   9:Mix
1525  *
1526  * Proposed pin assignments by the datasheet
1527  *
1528  * 6-stack
1529  * Port-A front headphone
1530  *      B front mic-in
1531  *      C rear line-in
1532  *      D rear front-out
1533  *      E rear mic-in
1534  *      F rear surround
1535  *      G rear CLFE
1536  *      H rear side
1537  *
1538  * 3-stack
1539  * Port-A front headphone
1540  *      B front mic
1541  *      C rear line-in/surround
1542  *      D rear front-out
1543  *      E rear mic-in/CLFE
1544  *
1545  * laptop
1546  * Port-A headphone
1547  *      B mic-in
1548  *      C docking station
1549  *      D internal speaker (with EAPD)
1550  *      E/F quad mic array
1551  */
1552
1553
1554 /* models */
1555 enum {
1556         AD1988_6STACK,
1557         AD1988_6STACK_DIG,
1558         AD1988_3STACK,
1559         AD1988_3STACK_DIG,
1560         AD1988_LAPTOP,
1561         AD1988_LAPTOP_DIG,
1562         AD1988_AUTO,
1563         AD1988_MODEL_LAST,
1564 };
1565
1566 /* reivision id to check workarounds */
1567 #define AD1988A_REV2            0x100200
1568
1569 #define is_rev2(codec) \
1570         ((codec)->vendor_id == 0x11d41988 && \
1571          (codec)->revision_id == AD1988A_REV2)
1572
1573 /*
1574  * mixers
1575  */
1576
1577 static hda_nid_t ad1988_6stack_dac_nids[4] = {
1578         0x04, 0x06, 0x05, 0x0a
1579 };
1580
1581 static hda_nid_t ad1988_3stack_dac_nids[3] = {
1582         0x04, 0x05, 0x0a
1583 };
1584
1585 /* for AD1988A revision-2, DAC2-4 are swapped */
1586 static hda_nid_t ad1988_6stack_dac_nids_rev2[4] = {
1587         0x04, 0x05, 0x0a, 0x06
1588 };
1589
1590 static hda_nid_t ad1988_3stack_dac_nids_rev2[3] = {
1591         0x04, 0x0a, 0x06
1592 };
1593
1594 static hda_nid_t ad1988_adc_nids[3] = {
1595         0x08, 0x09, 0x0f
1596 };
1597
1598 static hda_nid_t ad1988_capsrc_nids[3] = {
1599         0x0c, 0x0d, 0x0e
1600 };
1601
1602 #define AD1988_SPDIF_OUT        0x02
1603 #define AD1988_SPDIF_IN         0x07
1604
1605 static struct hda_input_mux ad1988_6stack_capture_source = {
1606         .num_items = 5,
1607         .items = {
1608                 { "Front Mic", 0x0 },
1609                 { "Line", 0x1 },
1610                 { "Mic", 0x4 },
1611                 { "CD", 0x5 },
1612                 { "Mix", 0x9 },
1613         },
1614 };
1615
1616 static struct hda_input_mux ad1988_laptop_capture_source = {
1617         .num_items = 3,
1618         .items = {
1619                 { "Mic/Line", 0x0 },
1620                 { "CD", 0x5 },
1621                 { "Mix", 0x9 },
1622         },
1623 };
1624
1625 /*
1626  */
1627 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
1628                                struct snd_ctl_elem_info *uinfo)
1629 {
1630         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1631         struct ad198x_spec *spec = codec->spec;
1632         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
1633                                     spec->num_channel_mode);
1634 }
1635
1636 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
1637                               struct snd_ctl_elem_value *ucontrol)
1638 {
1639         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1640         struct ad198x_spec *spec = codec->spec;
1641         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
1642                                    spec->num_channel_mode, spec->multiout.max_channels);
1643 }
1644
1645 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
1646                               struct snd_ctl_elem_value *ucontrol)
1647 {
1648         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1649         struct ad198x_spec *spec = codec->spec;
1650         if (spec->need_dac_fix)
1651                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1652         return snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
1653                                    spec->num_channel_mode, &spec->multiout.max_channels);
1654 }
1655
1656 /* 6-stack mode */
1657 static struct snd_kcontrol_new ad1988_6stack_mixers1[] = {
1658         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1659         HDA_CODEC_VOLUME("Surround Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1660         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
1661         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
1662         HDA_CODEC_VOLUME("Side Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1663         { } /* end */
1664 };
1665
1666 static struct snd_kcontrol_new ad1988_6stack_mixers1_rev2[] = {
1667         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1668         HDA_CODEC_VOLUME("Surround Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1669         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0a, 1, 0x0, HDA_OUTPUT),
1670         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0a, 2, 0x0, HDA_OUTPUT),
1671         HDA_CODEC_VOLUME("Side Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1672         { } /* end */
1673 };
1674
1675 static struct snd_kcontrol_new ad1988_6stack_mixers2[] = {
1676         HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
1677         HDA_BIND_MUTE("Surround Playback Switch", 0x2a, 2, HDA_INPUT),
1678         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x27, 1, 2, HDA_INPUT),
1679         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x27, 2, 2, HDA_INPUT),
1680         HDA_BIND_MUTE("Side Playback Switch", 0x28, 2, HDA_INPUT),
1681         HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
1682         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1683
1684         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1685         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1686         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1687         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1688         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1689         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1690         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
1691         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),
1692
1693         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1694         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1695
1696         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1697         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1698
1699         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1700         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
1701
1702         { } /* end */
1703 };
1704
1705 /* 3-stack mode */
1706 static struct snd_kcontrol_new ad1988_3stack_mixers1[] = {
1707         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1708         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1709         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
1710         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
1711         { } /* end */
1712 };
1713
1714 static struct snd_kcontrol_new ad1988_3stack_mixers1_rev2[] = {
1715         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1716         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1717         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x06, 1, 0x0, HDA_OUTPUT),
1718         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x06, 2, 0x0, HDA_OUTPUT),
1719         { } /* end */
1720 };
1721
1722 static struct snd_kcontrol_new ad1988_3stack_mixers2[] = {
1723         HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
1724         HDA_BIND_MUTE("Surround Playback Switch", 0x2c, 2, HDA_INPUT),
1725         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x26, 1, 2, HDA_INPUT),
1726         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x26, 2, 2, HDA_INPUT),
1727         HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
1728         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1729
1730         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1731         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1732         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1733         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1734         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1735         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1736         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
1737         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),
1738
1739         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1740         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1741
1742         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1743         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1744
1745         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1746         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
1747         {
1748                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1749                 .name = "Channel Mode",
1750                 .info = ad198x_ch_mode_info,
1751                 .get = ad198x_ch_mode_get,
1752                 .put = ad198x_ch_mode_put,
1753         },
1754
1755         { } /* end */
1756 };
1757
1758 /* laptop mode */
1759 static struct snd_kcontrol_new ad1988_laptop_mixers[] = {
1760         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1761         HDA_CODEC_MUTE("PCM Playback Switch", 0x29, 0x0, HDA_INPUT),
1762         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1763
1764         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1765         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1766         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1767         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1768         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1769         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1770
1771         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1772         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1773
1774         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1775         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1776
1777         HDA_CODEC_VOLUME("Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1778
1779         {
1780                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1781                 .name = "External Amplifier",
1782                 .info = ad198x_eapd_info,
1783                 .get = ad198x_eapd_get,
1784                 .put = ad198x_eapd_put,
1785                 .private_value = 0x12 | (1 << 8), /* port-D, inversed */
1786         },
1787
1788         { } /* end */
1789 };
1790
1791 /* capture */
1792 static struct snd_kcontrol_new ad1988_capture_mixers[] = {
1793         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
1794         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
1795         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
1796         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
1797         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x0e, 0x0, HDA_OUTPUT),
1798         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x0e, 0x0, HDA_OUTPUT),
1799         {
1800                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1801                 /* The multiple "Capture Source" controls confuse alsamixer
1802                  * So call somewhat different..
1803                  * FIXME: the controls appear in the "playback" view!
1804                  */
1805                 /* .name = "Capture Source", */
1806                 .name = "Input Source",
1807                 .count = 3,
1808                 .info = ad198x_mux_enum_info,
1809                 .get = ad198x_mux_enum_get,
1810                 .put = ad198x_mux_enum_put,
1811         },
1812         { } /* end */
1813 };
1814
1815 static int ad1988_spdif_playback_source_info(struct snd_kcontrol *kcontrol,
1816                                              struct snd_ctl_elem_info *uinfo)
1817 {
1818         static char *texts[] = {
1819                 "PCM", "ADC1", "ADC2", "ADC3"
1820         };
1821         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1822         uinfo->count = 1;
1823         uinfo->value.enumerated.items = 4;
1824         if (uinfo->value.enumerated.item >= 4)
1825                 uinfo->value.enumerated.item = 3;
1826         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1827         return 0;
1828 }
1829
1830 static int ad1988_spdif_playback_source_get(struct snd_kcontrol *kcontrol,
1831                                             struct snd_ctl_elem_value *ucontrol)
1832 {
1833         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1834         unsigned int sel;
1835
1836         sel = snd_hda_codec_read(codec, 0x02, 0, AC_VERB_GET_CONNECT_SEL, 0);
1837         if (sel > 0) {
1838                 sel = snd_hda_codec_read(codec, 0x0b, 0, AC_VERB_GET_CONNECT_SEL, 0);
1839                 if (sel <= 3)
1840                         sel++;
1841                 else
1842                         sel = 0;
1843         }
1844         ucontrol->value.enumerated.item[0] = sel;
1845         return 0;
1846 }
1847
1848 static int ad1988_spdif_playback_source_put(struct snd_kcontrol *kcontrol,
1849                                             struct snd_ctl_elem_value *ucontrol)
1850 {
1851         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1852         unsigned int sel;
1853         int change;
1854
1855         sel = snd_hda_codec_read(codec, 0x02, 0, AC_VERB_GET_CONNECT_SEL, 0);
1856         if (! ucontrol->value.enumerated.item[0]) {
1857                 change = sel != 0;
1858                 if (change)
1859                         snd_hda_codec_write(codec, 0x02, 0, AC_VERB_SET_CONNECT_SEL, 0);
1860         } else {
1861                 change = sel == 0;
1862                 if (change)
1863                         snd_hda_codec_write(codec, 0x02, 0, AC_VERB_SET_CONNECT_SEL, 1);
1864                 sel = snd_hda_codec_read(codec, 0x0b, 0, AC_VERB_GET_CONNECT_SEL, 0) + 1;
1865                 change |= sel == ucontrol->value.enumerated.item[0];
1866                 if (change)
1867                         snd_hda_codec_write(codec, 0x02, 0, AC_VERB_SET_CONNECT_SEL,
1868                                             ucontrol->value.enumerated.item[0] - 1);
1869         }
1870         return change;
1871 }
1872
1873 static struct snd_kcontrol_new ad1988_spdif_out_mixers[] = {
1874         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
1875         {
1876                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1877                 .name = "IEC958 Playback Source",
1878                 .info = ad1988_spdif_playback_source_info,
1879                 .get = ad1988_spdif_playback_source_get,
1880                 .put = ad1988_spdif_playback_source_put,
1881         },
1882         { } /* end */
1883 };
1884
1885 static struct snd_kcontrol_new ad1988_spdif_in_mixers[] = {
1886         HDA_CODEC_VOLUME("IEC958 Capture Volume", 0x1c, 0x0, HDA_INPUT),
1887         { } /* end */
1888 };
1889
1890
1891 /*
1892  * initialization verbs
1893  */
1894
1895 /*
1896  * for 6-stack (+dig)
1897  */
1898 static struct hda_verb ad1988_6stack_init_verbs[] = {
1899         /* Front, Surround, CLFE, side DAC; unmute as default */
1900         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1901         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1902         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1903         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1904         /* Port-A front headphon path */
1905         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
1906         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1907         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1908         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1909         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1910         /* Port-D line-out path */
1911         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1912         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1913         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1914         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1915         /* Port-F surround path */
1916         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1917         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1918         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1919         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1920         /* Port-G CLFE path */
1921         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1922         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1923         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1924         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1925         /* Port-H side path */
1926         {0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1927         {0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1928         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1929         {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1930         /* Mono out path */
1931         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
1932         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1933         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1934         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1935         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
1936         /* Port-B front mic-in path */
1937         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1938         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1939         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1940         /* Port-C line-in path */
1941         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1942         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1943         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1944         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
1945         /* Port-E mic-in path */
1946         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1947         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1948         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1949         {0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
1950
1951         { }
1952 };
1953
1954 static struct hda_verb ad1988_capture_init_verbs[] = {
1955         /* mute analog mix */
1956         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1957         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1958         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
1959         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
1960         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
1961         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
1962         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
1963         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
1964         /* select ADCs - front-mic */
1965         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
1966         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
1967         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
1968         /* ADCs; muted */
1969         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1970         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1971         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1972
1973         { }
1974 };
1975
1976 static struct hda_verb ad1988_spdif_init_verbs[] = {
1977         /* SPDIF out sel */
1978         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
1979         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x0}, /* ADC1 */
1980         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1981         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1982         /* SPDIF out pin */
1983         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
1984         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x17}, /* 0dB */
1985
1986         { }
1987 };
1988
1989 /*
1990  * verbs for 3stack (+dig)
1991  */
1992 static struct hda_verb ad1988_3stack_ch2_init[] = {
1993         /* set port-C to line-in */
1994         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
1995         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1996         /* set port-E to mic-in */
1997         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
1998         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
1999         { } /* end */
2000 };
2001
2002 static struct hda_verb ad1988_3stack_ch6_init[] = {
2003         /* set port-C to surround out */
2004         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2005         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2006         /* set port-E to CLFE out */
2007         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2008         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2009         { } /* end */
2010 };
2011
2012 static struct hda_channel_mode ad1988_3stack_modes[2] = {
2013         { 2, ad1988_3stack_ch2_init },
2014         { 6, ad1988_3stack_ch6_init },
2015 };
2016
2017 static struct hda_verb ad1988_3stack_init_verbs[] = {
2018         /* Front, Surround, CLFE, side DAC; unmute as default */
2019         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2020         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2021         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2022         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2023         /* Port-A front headphon path */
2024         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
2025         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2026         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2027         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2028         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2029         /* Port-D line-out path */
2030         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2031         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2032         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2033         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2034         /* Mono out path */
2035         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2036         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2037         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2038         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2039         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2040         /* Port-B front mic-in path */
2041         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2042         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2043         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2044         /* Port-C line-in/surround path - 6ch mode as default */
2045         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2046         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2047         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2048         {0x31, AC_VERB_SET_CONNECT_SEL, 0x0}, /* output sel: DAC 0x05 */
2049         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2050         /* Port-E mic-in/CLFE path - 6ch mode as default */
2051         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2052         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2053         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2054         {0x32, AC_VERB_SET_CONNECT_SEL, 0x1}, /* output sel: DAC 0x0a */
2055         {0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
2056         /* mute analog mix */
2057         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2058         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2059         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2060         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2061         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2062         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2063         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2064         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2065         /* select ADCs - front-mic */
2066         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2067         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2068         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2069         /* ADCs; muted */
2070         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2071         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2072         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2073         { }
2074 };
2075
2076 /*
2077  * verbs for laptop mode (+dig)
2078  */
2079 static struct hda_verb ad1988_laptop_hp_on[] = {
2080         /* unmute port-A and mute port-D */
2081         { 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2082         { 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2083         { } /* end */
2084 };
2085 static struct hda_verb ad1988_laptop_hp_off[] = {
2086         /* mute port-A and unmute port-D */
2087         { 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2088         { 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2089         { } /* end */
2090 };
2091
2092 #define AD1988_HP_EVENT 0x01
2093
2094 static struct hda_verb ad1988_laptop_init_verbs[] = {
2095         /* Front, Surround, CLFE, side DAC; unmute as default */
2096         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2097         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2098         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2099         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2100         /* Port-A front headphon path */
2101         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
2102         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2103         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2104         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2105         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2106         /* unsolicited event for pin-sense */
2107         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1988_HP_EVENT },
2108         /* Port-D line-out path + EAPD */
2109         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2110         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2111         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2112         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2113         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x00}, /* EAPD-off */
2114         /* Mono out path */
2115         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2116         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2117         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2118         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2119         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2120         /* Port-B mic-in path */
2121         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2122         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2123         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2124         /* Port-C docking station - try to output */
2125         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2126         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2127         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2128         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2129         /* mute analog mix */
2130         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2131         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2132         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2133         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2134         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2135         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2136         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2137         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2138         /* select ADCs - mic */
2139         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2140         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2141         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2142         /* ADCs; muted */
2143         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2144         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2145         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2146         { }
2147 };
2148
2149 static void ad1988_laptop_unsol_event(struct hda_codec *codec, unsigned int res)
2150 {
2151         if ((res >> 26) != AD1988_HP_EVENT)
2152                 return;
2153         if (snd_hda_codec_read(codec, 0x11, 0, AC_VERB_GET_PIN_SENSE, 0) & (1 << 31))
2154                 snd_hda_sequence_write(codec, ad1988_laptop_hp_on);
2155         else
2156                 snd_hda_sequence_write(codec, ad1988_laptop_hp_off);
2157
2158
2159
2160 /*
2161  * Automatic parse of I/O pins from the BIOS configuration
2162  */
2163
2164 #define NUM_CONTROL_ALLOC       32
2165 #define NUM_VERB_ALLOC          32
2166
2167 enum {
2168         AD_CTL_WIDGET_VOL,
2169         AD_CTL_WIDGET_MUTE,
2170         AD_CTL_BIND_MUTE,
2171 };
2172 static struct snd_kcontrol_new ad1988_control_templates[] = {
2173         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2174         HDA_CODEC_MUTE(NULL, 0, 0, 0),
2175         HDA_BIND_MUTE(NULL, 0, 0, 0),
2176 };
2177
2178 /* add dynamic controls */
2179 static int add_control(struct ad198x_spec *spec, int type, const char *name,
2180                        unsigned long val)
2181 {
2182         struct snd_kcontrol_new *knew;
2183
2184         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
2185                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
2186
2187                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
2188                 if (! knew)
2189                         return -ENOMEM;
2190                 if (spec->kctl_alloc) {
2191                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
2192                         kfree(spec->kctl_alloc);
2193                 }
2194                 spec->kctl_alloc = knew;
2195                 spec->num_kctl_alloc = num;
2196         }
2197
2198         knew = &spec->kctl_alloc[spec->num_kctl_used];
2199         *knew = ad1988_control_templates[type];
2200         knew->name = kstrdup(name, GFP_KERNEL);
2201         if (! knew->name)
2202                 return -ENOMEM;
2203         knew->private_value = val;
2204         spec->num_kctl_used++;
2205         return 0;
2206 }
2207
2208 #define AD1988_PIN_CD_NID               0x18
2209 #define AD1988_PIN_BEEP_NID             0x10
2210
2211 static hda_nid_t ad1988_mixer_nids[8] = {
2212         /* A     B     C     D     E     F     G     H */
2213         0x22, 0x2b, 0x2c, 0x29, 0x26, 0x2a, 0x27, 0x28
2214 };
2215
2216 static inline hda_nid_t ad1988_idx_to_dac(struct hda_codec *codec, int idx)
2217 {
2218         static hda_nid_t idx_to_dac[8] = {
2219                 /* A     B     C     D     E     F     G     H */
2220                 0x04, 0x06, 0x05, 0x04, 0x0a, 0x06, 0x05, 0x0a
2221         };
2222         static hda_nid_t idx_to_dac_rev2[8] = {
2223                 /* A     B     C     D     E     F     G     H */
2224                 0x04, 0x05, 0x0a, 0x04, 0x06, 0x05, 0x0a, 0x06
2225         };
2226         if (is_rev2(codec))
2227                 return idx_to_dac_rev2[idx];
2228         else
2229                 return idx_to_dac[idx];
2230 }
2231
2232 static hda_nid_t ad1988_boost_nids[8] = {
2233         0x38, 0x39, 0x3a, 0x3d, 0x3c, 0x3b, 0, 0
2234 };
2235
2236 static int ad1988_pin_idx(hda_nid_t nid)
2237 {
2238         static hda_nid_t ad1988_io_pins[8] = {
2239                 0x11, 0x14, 0x15, 0x12, 0x17, 0x16, 0x24, 0x25
2240         };
2241         int i;
2242         for (i = 0; i < ARRAY_SIZE(ad1988_io_pins); i++)
2243                 if (ad1988_io_pins[i] == nid)
2244                         return i;
2245         return 0; /* should be -1 */
2246 }
2247
2248 static int ad1988_pin_to_loopback_idx(hda_nid_t nid)
2249 {
2250         static int loopback_idx[8] = {
2251                 2, 0, 1, 3, 4, 5, 1, 4
2252         };
2253         switch (nid) {
2254         case AD1988_PIN_CD_NID:
2255                 return 6;
2256         default:
2257                 return loopback_idx[ad1988_pin_idx(nid)];
2258         }
2259 }
2260
2261 static int ad1988_pin_to_adc_idx(hda_nid_t nid)
2262 {
2263         static int adc_idx[8] = {
2264                 0, 1, 2, 8, 4, 3, 6, 7
2265         };
2266         switch (nid) {
2267         case AD1988_PIN_CD_NID:
2268                 return 5;
2269         default:
2270                 return adc_idx[ad1988_pin_idx(nid)];
2271         }
2272 }
2273
2274 /* fill in the dac_nids table from the parsed pin configuration */
2275 static int ad1988_auto_fill_dac_nids(struct hda_codec *codec,
2276                                      const struct auto_pin_cfg *cfg)
2277 {
2278         struct ad198x_spec *spec = codec->spec;
2279         int i, idx;
2280
2281         spec->multiout.dac_nids = spec->private_dac_nids;
2282
2283         /* check the pins hardwired to audio widget */
2284         for (i = 0; i < cfg->line_outs; i++) {
2285                 idx = ad1988_pin_idx(cfg->line_out_pins[i]);
2286                 spec->multiout.dac_nids[i] = ad1988_idx_to_dac(codec, idx);
2287         }
2288         spec->multiout.num_dacs = cfg->line_outs;
2289         return 0;
2290 }
2291
2292 /* add playback controls from the parsed DAC table */
2293 static int ad1988_auto_create_multi_out_ctls(struct ad198x_spec *spec,
2294                                              const struct auto_pin_cfg *cfg)
2295 {
2296         char name[32];
2297         static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
2298         hda_nid_t nid;
2299         int i, err;
2300
2301         for (i = 0; i < cfg->line_outs; i++) {
2302                 hda_nid_t dac = spec->multiout.dac_nids[i];
2303                 if (! dac)
2304                         continue;
2305                 nid = ad1988_mixer_nids[ad1988_pin_idx(cfg->line_out_pins[i])];
2306                 if (i == 2) {
2307                         /* Center/LFE */
2308                         err = add_control(spec, AD_CTL_WIDGET_VOL,
2309                                           "Center Playback Volume",
2310                                           HDA_COMPOSE_AMP_VAL(dac, 1, 0, HDA_OUTPUT));
2311                         if (err < 0)
2312                                 return err;
2313                         err = add_control(spec, AD_CTL_WIDGET_VOL,
2314                                           "LFE Playback Volume",
2315                                           HDA_COMPOSE_AMP_VAL(dac, 2, 0, HDA_OUTPUT));
2316                         if (err < 0)
2317                                 return err;
2318                         err = add_control(spec, AD_CTL_BIND_MUTE,
2319                                           "Center Playback Switch",
2320                                           HDA_COMPOSE_AMP_VAL(nid, 1, 2, HDA_INPUT));
2321                         if (err < 0)
2322                                 return err;
2323                         err = add_control(spec, AD_CTL_BIND_MUTE,
2324                                           "LFE Playback Switch",
2325                                           HDA_COMPOSE_AMP_VAL(nid, 2, 2, HDA_INPUT));
2326                         if (err < 0)
2327                                 return err;
2328                 } else {
2329                         sprintf(name, "%s Playback Volume", chname[i]);
2330                         err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2331                                           HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT));
2332                         if (err < 0)
2333                                 return err;
2334                         sprintf(name, "%s Playback Switch", chname[i]);
2335                         err = add_control(spec, AD_CTL_BIND_MUTE, name,
2336                                           HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT));
2337                         if (err < 0)
2338                                 return err;
2339                 }
2340         }
2341         return 0;
2342 }
2343
2344 /* add playback controls for speaker and HP outputs */
2345 static int ad1988_auto_create_extra_out(struct hda_codec *codec, hda_nid_t pin,
2346                                         const char *pfx)
2347 {
2348         struct ad198x_spec *spec = codec->spec;
2349         hda_nid_t nid;
2350         int idx, err;
2351         char name[32];
2352
2353         if (! pin)
2354                 return 0;
2355
2356         idx = ad1988_pin_idx(pin);
2357         nid = ad1988_idx_to_dac(codec, idx);
2358         /* specify the DAC as the extra output */
2359         if (! spec->multiout.hp_nid)
2360                 spec->multiout.hp_nid = nid;
2361         else
2362                 spec->multiout.extra_out_nid[0] = nid;
2363         /* control HP volume/switch on the output mixer amp */
2364         sprintf(name, "%s Playback Volume", pfx);
2365         if ((err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2366                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
2367                 return err;
2368         nid = ad1988_mixer_nids[idx];
2369         sprintf(name, "%s Playback Switch", pfx);
2370         if ((err = add_control(spec, AD_CTL_BIND_MUTE, name,
2371                                HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT))) < 0)
2372                 return err;
2373         return 0;
2374 }
2375
2376 /* create input playback/capture controls for the given pin */
2377 static int new_analog_input(struct ad198x_spec *spec, hda_nid_t pin,
2378                             const char *ctlname, int boost)
2379 {
2380         char name[32];
2381         int err, idx;
2382
2383         sprintf(name, "%s Playback Volume", ctlname);
2384         idx = ad1988_pin_to_loopback_idx(pin);
2385         if ((err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2386                                HDA_COMPOSE_AMP_VAL(0x20, 3, idx, HDA_INPUT))) < 0)
2387                 return err;
2388         sprintf(name, "%s Playback Switch", ctlname);
2389         if ((err = add_control(spec, AD_CTL_WIDGET_MUTE, name,
2390                                HDA_COMPOSE_AMP_VAL(0x20, 3, idx, HDA_INPUT))) < 0)
2391                 return err;
2392         if (boost) {
2393                 hda_nid_t bnid;
2394                 idx = ad1988_pin_idx(pin);
2395                 bnid = ad1988_boost_nids[idx];
2396                 if (bnid) {
2397                         sprintf(name, "%s Boost", ctlname);
2398                         return add_control(spec, AD_CTL_WIDGET_VOL, name,
2399                                            HDA_COMPOSE_AMP_VAL(bnid, 3, idx, HDA_OUTPUT));
2400
2401                 }
2402         }
2403         return 0;
2404 }
2405
2406 /* create playback/capture controls for input pins */
2407 static int ad1988_auto_create_analog_input_ctls(struct ad198x_spec *spec,
2408                                                 const struct auto_pin_cfg *cfg)
2409 {
2410         struct hda_input_mux *imux = &spec->private_imux;
2411         int i, err;
2412
2413         for (i = 0; i < AUTO_PIN_LAST; i++) {
2414                 err = new_analog_input(spec, cfg->input_pins[i],
2415                                        auto_pin_cfg_labels[i],
2416                                        i <= AUTO_PIN_FRONT_MIC);
2417                 if (err < 0)
2418                         return err;
2419                 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
2420                 imux->items[imux->num_items].index = ad1988_pin_to_adc_idx(cfg->input_pins[i]);
2421                 imux->num_items++;
2422         }
2423         imux->items[imux->num_items].label = "Mix";
2424         imux->items[imux->num_items].index = 9;
2425         imux->num_items++;
2426
2427         if ((err = add_control(spec, AD_CTL_WIDGET_VOL,
2428                                "Analog Mix Playback Volume",
2429                                HDA_COMPOSE_AMP_VAL(0x21, 3, 0x0, HDA_OUTPUT))) < 0)
2430                 return err;
2431         if ((err = add_control(spec, AD_CTL_WIDGET_MUTE,
2432                                "Analog Mix Playback Switch",
2433                                HDA_COMPOSE_AMP_VAL(0x21, 3, 0x0, HDA_OUTPUT))) < 0)
2434                 return err;
2435
2436         return 0;
2437 }
2438
2439 static void ad1988_auto_set_output_and_unmute(struct hda_codec *codec,
2440                                               hda_nid_t nid, int pin_type,
2441                                               int dac_idx)
2442 {
2443         /* set as output */
2444         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
2445         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
2446         switch (nid) {
2447         case 0x11: /* port-A - DAC 04 */
2448                 snd_hda_codec_write(codec, 0x37, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2449                 break;
2450         case 0x14: /* port-B - DAC 06 */
2451                 snd_hda_codec_write(codec, 0x30, 0, AC_VERB_SET_CONNECT_SEL, 0x02);
2452                 break;
2453         case 0x15: /* port-C - DAC 05 */
2454                 snd_hda_codec_write(codec, 0x31, 0, AC_VERB_SET_CONNECT_SEL, 0x00);
2455                 break;
2456         case 0x17: /* port-E - DAC 0a */
2457                 snd_hda_codec_write(codec, 0x32, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2458                 break;
2459         case 0x13: /* mono - DAC 04 */
2460                 snd_hda_codec_write(codec, 0x36, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2461                 break;
2462         }
2463 }
2464
2465 static void ad1988_auto_init_multi_out(struct hda_codec *codec)
2466 {
2467         struct ad198x_spec *spec = codec->spec;
2468         int i;
2469
2470         for (i = 0; i < spec->autocfg.line_outs; i++) {
2471                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
2472                 ad1988_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
2473         }
2474 }
2475
2476 static void ad1988_auto_init_extra_out(struct hda_codec *codec)
2477 {
2478         struct ad198x_spec *spec = codec->spec;
2479         hda_nid_t pin;
2480
2481         pin = spec->autocfg.speaker_pins[0];
2482         if (pin) /* connect to front */
2483                 ad1988_auto_set_output_and_unmute(codec, pin, PIN_OUT, 0);
2484         pin = spec->autocfg.hp_pin;
2485         if (pin) /* connect to front */
2486                 ad1988_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
2487 }
2488
2489 static void ad1988_auto_init_analog_input(struct hda_codec *codec)
2490 {
2491         struct ad198x_spec *spec = codec->spec;
2492         int i, idx;
2493
2494         for (i = 0; i < AUTO_PIN_LAST; i++) {
2495                 hda_nid_t nid = spec->autocfg.input_pins[i];
2496                 if (! nid)
2497                         continue;
2498                 switch (nid) {
2499                 case 0x15: /* port-C */
2500                         snd_hda_codec_write(codec, 0x33, 0, AC_VERB_SET_CONNECT_SEL, 0x0);
2501                         break;
2502                 case 0x17: /* port-E */
2503                         snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_CONNECT_SEL, 0x0);
2504                         break;
2505                 }
2506                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2507                                     i <= AUTO_PIN_FRONT_MIC ? PIN_VREF80 : PIN_IN);
2508                 if (nid != AD1988_PIN_CD_NID)
2509                         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2510                                             AMP_OUT_MUTE);
2511                 idx = ad1988_pin_idx(nid);
2512                 if (ad1988_boost_nids[idx])
2513                         snd_hda_codec_write(codec, ad1988_boost_nids[idx], 0,
2514                                             AC_VERB_SET_AMP_GAIN_MUTE,
2515                                             AMP_OUT_ZERO);
2516         }
2517 }
2518
2519 /* parse the BIOS configuration and set up the alc_spec */
2520 /* return 1 if successful, 0 if the proper config is not found, or a negative error code */
2521 static int ad1988_parse_auto_config(struct hda_codec *codec)
2522 {
2523         struct ad198x_spec *spec = codec->spec;
2524         int err;
2525
2526         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
2527                 return err;
2528         if ((err = ad1988_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
2529                 return err;
2530         if (! spec->autocfg.line_outs)
2531                 return 0; /* can't find valid BIOS pin config */
2532         if ((err = ad1988_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
2533             (err = ad1988_auto_create_extra_out(codec,
2534                                                 spec->autocfg.speaker_pins[0],
2535                                                 "Speaker")) < 0 ||
2536             (err = ad1988_auto_create_extra_out(codec, spec->autocfg.hp_pin,
2537                                                 "Headphone")) < 0 ||
2538             (err = ad1988_auto_create_analog_input_ctls(spec, &spec->autocfg)) < 0)
2539                 return err;
2540
2541         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2542
2543         if (spec->autocfg.dig_out_pin)
2544                 spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2545         if (spec->autocfg.dig_in_pin)
2546                 spec->dig_in_nid = AD1988_SPDIF_IN;
2547
2548         if (spec->kctl_alloc)
2549                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
2550
2551         spec->init_verbs[spec->num_init_verbs++] = ad1988_6stack_init_verbs;
2552
2553         spec->input_mux = &spec->private_imux;
2554
2555         return 1;
2556 }
2557
2558 /* init callback for auto-configuration model -- overriding the default init */
2559 static int ad1988_auto_init(struct hda_codec *codec)
2560 {
2561         ad198x_init(codec);
2562         ad1988_auto_init_multi_out(codec);
2563         ad1988_auto_init_extra_out(codec);
2564         ad1988_auto_init_analog_input(codec);
2565         return 0;
2566 }
2567
2568
2569 /*
2570  */
2571
2572 static struct hda_board_config ad1988_cfg_tbl[] = {
2573         { .modelname = "6stack",        .config = AD1988_6STACK },
2574         { .modelname = "6stack-dig",    .config = AD1988_6STACK_DIG },
2575         { .modelname = "3stack",        .config = AD1988_3STACK },
2576         { .modelname = "3stack-dig",    .config = AD1988_3STACK_DIG },
2577         { .modelname = "laptop",        .config = AD1988_LAPTOP },
2578         { .modelname = "laptop-dig",    .config = AD1988_LAPTOP_DIG },
2579         { .modelname = "auto",          .config = AD1988_AUTO },
2580         {}
2581 };
2582
2583 static int patch_ad1988(struct hda_codec *codec)
2584 {
2585         struct ad198x_spec *spec;
2586         int board_config;
2587
2588         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2589         if (spec == NULL)
2590                 return -ENOMEM;
2591
2592         mutex_init(&spec->amp_mutex);
2593         codec->spec = spec;
2594
2595         if (is_rev2(codec))
2596                 snd_printk(KERN_INFO "patch_analog: AD1988A rev.2 is detected, enable workarounds\n");
2597
2598         board_config = snd_hda_check_board_config(codec, ad1988_cfg_tbl);
2599         if (board_config < 0 || board_config >= AD1988_MODEL_LAST) {
2600                 printk(KERN_INFO "hda_codec: Unknown model for AD1988, trying auto-probe from BIOS...\n");
2601                 board_config = AD1988_AUTO;
2602         }
2603
2604         if (board_config == AD1988_AUTO) {
2605                 /* automatic parse from the BIOS config */
2606                 int err = ad1988_parse_auto_config(codec);
2607                 if (err < 0) {
2608                         ad198x_free(codec);
2609                         return err;
2610                 } else if (! err) {
2611                         printk(KERN_INFO "hda_codec: Cannot set up configuration from BIOS.  Using 6-stack mode...\n");
2612                         board_config = AD1988_6STACK;
2613                 }
2614         }
2615
2616         switch (board_config) {
2617         case AD1988_6STACK:
2618         case AD1988_6STACK_DIG:
2619                 spec->multiout.max_channels = 8;
2620                 spec->multiout.num_dacs = 4;
2621                 if (is_rev2(codec))
2622                         spec->multiout.dac_nids = ad1988_6stack_dac_nids_rev2;
2623                 else
2624                         spec->multiout.dac_nids = ad1988_6stack_dac_nids;
2625                 spec->input_mux = &ad1988_6stack_capture_source;
2626                 spec->num_mixers = 2;
2627                 if (is_rev2(codec))
2628                         spec->mixers[0] = ad1988_6stack_mixers1_rev2;
2629                 else
2630                         spec->mixers[0] = ad1988_6stack_mixers1;
2631                 spec->mixers[1] = ad1988_6stack_mixers2;
2632                 spec->num_init_verbs = 1;
2633                 spec->init_verbs[0] = ad1988_6stack_init_verbs;
2634                 if (board_config == AD1988_6STACK_DIG) {
2635                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2636                         spec->dig_in_nid = AD1988_SPDIF_IN;
2637                 }
2638                 break;
2639         case AD1988_3STACK:
2640         case AD1988_3STACK_DIG:
2641                 spec->multiout.max_channels = 6;
2642                 spec->multiout.num_dacs = 3;
2643                 if (is_rev2(codec))
2644                         spec->multiout.dac_nids = ad1988_3stack_dac_nids_rev2;
2645                 else
2646                         spec->multiout.dac_nids = ad1988_3stack_dac_nids;
2647                 spec->input_mux = &ad1988_6stack_capture_source;
2648                 spec->channel_mode = ad1988_3stack_modes;
2649                 spec->num_channel_mode = ARRAY_SIZE(ad1988_3stack_modes);
2650                 spec->num_mixers = 2;
2651                 if (is_rev2(codec))
2652                         spec->mixers[0] = ad1988_3stack_mixers1_rev2;
2653                 else
2654                         spec->mixers[0] = ad1988_3stack_mixers1;
2655                 spec->mixers[1] = ad1988_3stack_mixers2;
2656                 spec->num_init_verbs = 1;
2657                 spec->init_verbs[0] = ad1988_3stack_init_verbs;
2658                 if (board_config == AD1988_3STACK_DIG)
2659                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2660                 break;
2661         case AD1988_LAPTOP:
2662         case AD1988_LAPTOP_DIG:
2663                 spec->multiout.max_channels = 2;
2664                 spec->multiout.num_dacs = 1;
2665                 spec->multiout.dac_nids = ad1988_3stack_dac_nids;
2666                 spec->input_mux = &ad1988_laptop_capture_source;
2667                 spec->num_mixers = 1;
2668                 spec->mixers[0] = ad1988_laptop_mixers;
2669                 spec->num_init_verbs = 1;
2670                 spec->init_verbs[0] = ad1988_laptop_init_verbs;
2671                 if (board_config == AD1988_LAPTOP_DIG)
2672                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2673                 break;
2674         }
2675
2676         spec->num_adc_nids = ARRAY_SIZE(ad1988_adc_nids);
2677         spec->adc_nids = ad1988_adc_nids;
2678         spec->capsrc_nids = ad1988_capsrc_nids;
2679         spec->mixers[spec->num_mixers++] = ad1988_capture_mixers;
2680         spec->init_verbs[spec->num_init_verbs++] = ad1988_capture_init_verbs;
2681         if (spec->multiout.dig_out_nid) {
2682                 spec->mixers[spec->num_mixers++] = ad1988_spdif_out_mixers;
2683                 spec->init_verbs[spec->num_init_verbs++] = ad1988_spdif_init_verbs;
2684         }
2685         if (spec->dig_in_nid)
2686                 spec->mixers[spec->num_mixers++] = ad1988_spdif_in_mixers;
2687
2688         codec->patch_ops = ad198x_patch_ops;
2689         switch (board_config) {
2690         case AD1988_AUTO:
2691                 codec->patch_ops.init = ad1988_auto_init;
2692                 break;
2693         case AD1988_LAPTOP:
2694         case AD1988_LAPTOP_DIG:
2695                 codec->patch_ops.unsol_event = ad1988_laptop_unsol_event;
2696                 break;
2697         }
2698
2699         return 0;
2700 }
2701
2702
2703 /*
2704  * patch entries
2705  */
2706 struct hda_codec_preset snd_hda_preset_analog[] = {
2707         { .id = 0x11d41981, .name = "AD1981", .patch = patch_ad1981 },
2708         { .id = 0x11d41983, .name = "AD1983", .patch = patch_ad1983 },
2709         { .id = 0x11d41986, .name = "AD1986A", .patch = patch_ad1986a },
2710         { .id = 0x11d41988, .name = "AD1988", .patch = patch_ad1988 },
2711         { .id = 0x11d4198b, .name = "AD1988B", .patch = patch_ad1988 },
2712         {} /* terminator */
2713 };