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