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