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