Merge git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[linux-2.6] / sound / pci / hda / patch_analog.c
1 /*
2  * HD audio interface patch for AD1882, AD1884, AD1981HD, AD1983, AD1984,
3  *   AD1986A, AD1988
4  *
5  * Copyright (c) 2005-2007 Takashi Iwai <tiwai@suse.de>
6  *
7  *  This driver is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This driver is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21
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 #include "hda_patch.h"
32
33 struct ad198x_spec {
34         struct snd_kcontrol_new *mixers[5];
35         int num_mixers;
36
37         const struct hda_verb *init_verbs[5];   /* initialization verbs
38                                                  * don't forget NULL termination!
39                                                  */
40         unsigned int num_init_verbs;
41
42         /* playback */
43         struct hda_multi_out multiout;  /* playback set-up
44                                          * max_channels, dacs must be set
45                                          * dig_out_nid and hp_nid are optional
46                                          */
47         unsigned int cur_eapd;
48         unsigned int need_dac_fix;
49
50         /* capture */
51         unsigned int num_adc_nids;
52         hda_nid_t *adc_nids;
53         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
54
55         /* capture source */
56         const struct hda_input_mux *input_mux;
57         hda_nid_t *capsrc_nids;
58         unsigned int cur_mux[3];
59
60         /* channel model */
61         const struct hda_channel_mode *channel_mode;
62         int num_channel_mode;
63
64         /* PCM information */
65         struct hda_pcm pcm_rec[3];      /* used in alc_build_pcms() */
66
67         struct mutex amp_mutex; /* PCM volume/mute control mutex */
68         unsigned int spdif_route;
69
70         /* dynamic controls, init_verbs and input_mux */
71         struct auto_pin_cfg autocfg;
72         unsigned int num_kctl_alloc, num_kctl_used;
73         struct snd_kcontrol_new *kctl_alloc;
74         struct hda_input_mux private_imux;
75         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
76
77         unsigned int jack_present :1;
78
79 #ifdef CONFIG_SND_HDA_POWER_SAVE
80         struct hda_loopback_check loopback;
81 #endif
82         /* for virtual master */
83         hda_nid_t vmaster_nid;
84         const char **slave_vols;
85         const char **slave_sws;
86 };
87
88 /*
89  * input MUX handling (common part)
90  */
91 static int ad198x_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
92 {
93         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
94         struct ad198x_spec *spec = codec->spec;
95
96         return snd_hda_input_mux_info(spec->input_mux, uinfo);
97 }
98
99 static int ad198x_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
100 {
101         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
102         struct ad198x_spec *spec = codec->spec;
103         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
104
105         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
106         return 0;
107 }
108
109 static int ad198x_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
110 {
111         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
112         struct ad198x_spec *spec = codec->spec;
113         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
114
115         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
116                                      spec->capsrc_nids[adc_idx],
117                                      &spec->cur_mux[adc_idx]);
118 }
119
120 /*
121  * initialization (common callbacks)
122  */
123 static int ad198x_init(struct hda_codec *codec)
124 {
125         struct ad198x_spec *spec = codec->spec;
126         int i;
127
128         for (i = 0; i < spec->num_init_verbs; i++)
129                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
130         return 0;
131 }
132
133 static const char *ad_slave_vols[] = {
134         "Front Playback Volume",
135         "Surround Playback Volume",
136         "Center Playback Volume",
137         "LFE Playback Volume",
138         "Side Playback Volume",
139         "Headphone Playback Volume",
140         "Mono Playback Volume",
141         "Speaker Playback Volume",
142         "IEC958 Playback Volume",
143         NULL
144 };
145
146 static const char *ad_slave_sws[] = {
147         "Front Playback Switch",
148         "Surround Playback Switch",
149         "Center Playback Switch",
150         "LFE Playback Switch",
151         "Side Playback Switch",
152         "Headphone Playback Switch",
153         "Mono Playback Switch",
154         "Speaker Playback Switch",
155         "IEC958 Playback Switch",
156         NULL
157 };
158
159 static int ad198x_build_controls(struct hda_codec *codec)
160 {
161         struct ad198x_spec *spec = codec->spec;
162         unsigned int i;
163         int err;
164
165         for (i = 0; i < spec->num_mixers; i++) {
166                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
167                 if (err < 0)
168                         return err;
169         }
170         if (spec->multiout.dig_out_nid) {
171                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
172                 if (err < 0)
173                         return err;
174                 err = snd_hda_create_spdif_share_sw(codec,
175                                                     &spec->multiout);
176                 if (err < 0)
177                         return err;
178                 spec->multiout.share_spdif = 1;
179         } 
180         if (spec->dig_in_nid) {
181                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
182                 if (err < 0)
183                         return err;
184         }
185
186         /* if we have no master control, let's create it */
187         if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
188                 unsigned int vmaster_tlv[4];
189                 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
190                                         HDA_OUTPUT, vmaster_tlv);
191                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
192                                           vmaster_tlv,
193                                           (spec->slave_vols ?
194                                            spec->slave_vols : ad_slave_vols));
195                 if (err < 0)
196                         return err;
197         }
198         if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
199                 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
200                                           NULL,
201                                           (spec->slave_sws ?
202                                            spec->slave_sws : ad_slave_sws));
203                 if (err < 0)
204                         return err;
205         }
206
207         return 0;
208 }
209
210 #ifdef CONFIG_SND_HDA_POWER_SAVE
211 static int ad198x_check_power_status(struct hda_codec *codec, hda_nid_t nid)
212 {
213         struct ad198x_spec *spec = codec->spec;
214         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
215 }
216 #endif
217
218 /*
219  * Analog playback callbacks
220  */
221 static int ad198x_playback_pcm_open(struct hda_pcm_stream *hinfo,
222                                     struct hda_codec *codec,
223                                     struct snd_pcm_substream *substream)
224 {
225         struct ad198x_spec *spec = codec->spec;
226         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
227                                              hinfo);
228 }
229
230 static int ad198x_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
231                                        struct hda_codec *codec,
232                                        unsigned int stream_tag,
233                                        unsigned int format,
234                                        struct snd_pcm_substream *substream)
235 {
236         struct ad198x_spec *spec = codec->spec;
237         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
238                                                 format, substream);
239 }
240
241 static int ad198x_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
242                                        struct hda_codec *codec,
243                                        struct snd_pcm_substream *substream)
244 {
245         struct ad198x_spec *spec = codec->spec;
246         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
247 }
248
249 /*
250  * Digital out
251  */
252 static int ad198x_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
253                                         struct hda_codec *codec,
254                                         struct snd_pcm_substream *substream)
255 {
256         struct ad198x_spec *spec = codec->spec;
257         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
258 }
259
260 static int ad198x_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
261                                          struct hda_codec *codec,
262                                          struct snd_pcm_substream *substream)
263 {
264         struct ad198x_spec *spec = codec->spec;
265         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
266 }
267
268 static int ad198x_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
269                                            struct hda_codec *codec,
270                                            unsigned int stream_tag,
271                                            unsigned int format,
272                                            struct snd_pcm_substream *substream)
273 {
274         struct ad198x_spec *spec = codec->spec;
275         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
276                                              format, substream);
277 }
278
279 /*
280  * Analog capture
281  */
282 static int ad198x_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
283                                       struct hda_codec *codec,
284                                       unsigned int stream_tag,
285                                       unsigned int format,
286                                       struct snd_pcm_substream *substream)
287 {
288         struct ad198x_spec *spec = codec->spec;
289         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
290                                    stream_tag, 0, format);
291         return 0;
292 }
293
294 static int ad198x_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
295                                       struct hda_codec *codec,
296                                       struct snd_pcm_substream *substream)
297 {
298         struct ad198x_spec *spec = codec->spec;
299         snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
300         return 0;
301 }
302
303
304 /*
305  */
306 static struct hda_pcm_stream ad198x_pcm_analog_playback = {
307         .substreams = 1,
308         .channels_min = 2,
309         .channels_max = 6, /* changed later */
310         .nid = 0, /* fill later */
311         .ops = {
312                 .open = ad198x_playback_pcm_open,
313                 .prepare = ad198x_playback_pcm_prepare,
314                 .cleanup = ad198x_playback_pcm_cleanup
315         },
316 };
317
318 static struct hda_pcm_stream ad198x_pcm_analog_capture = {
319         .substreams = 1,
320         .channels_min = 2,
321         .channels_max = 2,
322         .nid = 0, /* fill later */
323         .ops = {
324                 .prepare = ad198x_capture_pcm_prepare,
325                 .cleanup = ad198x_capture_pcm_cleanup
326         },
327 };
328
329 static struct hda_pcm_stream ad198x_pcm_digital_playback = {
330         .substreams = 1,
331         .channels_min = 2,
332         .channels_max = 2,
333         .nid = 0, /* fill later */
334         .ops = {
335                 .open = ad198x_dig_playback_pcm_open,
336                 .close = ad198x_dig_playback_pcm_close,
337                 .prepare = ad198x_dig_playback_pcm_prepare
338         },
339 };
340
341 static struct hda_pcm_stream ad198x_pcm_digital_capture = {
342         .substreams = 1,
343         .channels_min = 2,
344         .channels_max = 2,
345         /* NID is set in alc_build_pcms */
346 };
347
348 static int ad198x_build_pcms(struct hda_codec *codec)
349 {
350         struct ad198x_spec *spec = codec->spec;
351         struct hda_pcm *info = spec->pcm_rec;
352
353         codec->num_pcms = 1;
354         codec->pcm_info = info;
355
356         info->name = "AD198x Analog";
357         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ad198x_pcm_analog_playback;
358         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->multiout.max_channels;
359         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
360         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad198x_pcm_analog_capture;
361         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adc_nids;
362         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
363
364         if (spec->multiout.dig_out_nid) {
365                 info++;
366                 codec->num_pcms++;
367                 info->name = "AD198x Digital";
368                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
369                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ad198x_pcm_digital_playback;
370                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
371                 if (spec->dig_in_nid) {
372                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad198x_pcm_digital_capture;
373                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
374                 }
375         }
376
377         return 0;
378 }
379
380 static void ad198x_free(struct hda_codec *codec)
381 {
382         struct ad198x_spec *spec = codec->spec;
383         unsigned int i;
384
385         if (spec->kctl_alloc) {
386                 for (i = 0; i < spec->num_kctl_used; i++)
387                         kfree(spec->kctl_alloc[i].name);
388                 kfree(spec->kctl_alloc);
389         }
390         kfree(codec->spec);
391 }
392
393 static struct hda_codec_ops ad198x_patch_ops = {
394         .build_controls = ad198x_build_controls,
395         .build_pcms = ad198x_build_pcms,
396         .init = ad198x_init,
397         .free = ad198x_free,
398 #ifdef CONFIG_SND_HDA_POWER_SAVE
399         .check_power_status = ad198x_check_power_status,
400 #endif
401 };
402
403
404 /*
405  * EAPD control
406  * the private value = nid | (invert << 8)
407  */
408 #define ad198x_eapd_info        snd_ctl_boolean_mono_info
409
410 static int ad198x_eapd_get(struct snd_kcontrol *kcontrol,
411                            struct snd_ctl_elem_value *ucontrol)
412 {
413         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
414         struct ad198x_spec *spec = codec->spec;
415         int invert = (kcontrol->private_value >> 8) & 1;
416         if (invert)
417                 ucontrol->value.integer.value[0] = ! spec->cur_eapd;
418         else
419                 ucontrol->value.integer.value[0] = spec->cur_eapd;
420         return 0;
421 }
422
423 static int ad198x_eapd_put(struct snd_kcontrol *kcontrol,
424                            struct snd_ctl_elem_value *ucontrol)
425 {
426         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
427         struct ad198x_spec *spec = codec->spec;
428         int invert = (kcontrol->private_value >> 8) & 1;
429         hda_nid_t nid = kcontrol->private_value & 0xff;
430         unsigned int eapd;
431         eapd = !!ucontrol->value.integer.value[0];
432         if (invert)
433                 eapd = !eapd;
434         if (eapd == spec->cur_eapd)
435                 return 0;
436         spec->cur_eapd = eapd;
437         snd_hda_codec_write_cache(codec, nid,
438                                   0, AC_VERB_SET_EAPD_BTLENABLE,
439                                   eapd ? 0x02 : 0x00);
440         return 1;
441 }
442
443 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
444                                struct snd_ctl_elem_info *uinfo);
445 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
446                               struct snd_ctl_elem_value *ucontrol);
447 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
448                               struct snd_ctl_elem_value *ucontrol);
449
450
451 /*
452  * AD1986A specific
453  */
454
455 #define AD1986A_SPDIF_OUT       0x02
456 #define AD1986A_FRONT_DAC       0x03
457 #define AD1986A_SURR_DAC        0x04
458 #define AD1986A_CLFE_DAC        0x05
459 #define AD1986A_ADC             0x06
460
461 static hda_nid_t ad1986a_dac_nids[3] = {
462         AD1986A_FRONT_DAC, AD1986A_SURR_DAC, AD1986A_CLFE_DAC
463 };
464 static hda_nid_t ad1986a_adc_nids[1] = { AD1986A_ADC };
465 static hda_nid_t ad1986a_capsrc_nids[1] = { 0x12 };
466
467 static struct hda_input_mux ad1986a_capture_source = {
468         .num_items = 7,
469         .items = {
470                 { "Mic", 0x0 },
471                 { "CD", 0x1 },
472                 { "Aux", 0x3 },
473                 { "Line", 0x4 },
474                 { "Mix", 0x5 },
475                 { "Mono", 0x6 },
476                 { "Phone", 0x7 },
477         },
478 };
479
480
481 static struct hda_bind_ctls ad1986a_bind_pcm_vol = {
482         .ops = &snd_hda_bind_vol,
483         .values = {
484                 HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT),
485                 HDA_COMPOSE_AMP_VAL(AD1986A_SURR_DAC, 3, 0, HDA_OUTPUT),
486                 HDA_COMPOSE_AMP_VAL(AD1986A_CLFE_DAC, 3, 0, HDA_OUTPUT),
487                 0
488         },
489 };
490
491 static struct hda_bind_ctls ad1986a_bind_pcm_sw = {
492         .ops = &snd_hda_bind_sw,
493         .values = {
494                 HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT),
495                 HDA_COMPOSE_AMP_VAL(AD1986A_SURR_DAC, 3, 0, HDA_OUTPUT),
496                 HDA_COMPOSE_AMP_VAL(AD1986A_CLFE_DAC, 3, 0, HDA_OUTPUT),
497                 0
498         },
499 };
500
501 /*
502  * mixers
503  */
504 static struct snd_kcontrol_new ad1986a_mixers[] = {
505         /*
506          * bind volumes/mutes of 3 DACs as a single PCM control for simplicity
507          */
508         HDA_BIND_VOL("PCM Playback Volume", &ad1986a_bind_pcm_vol),
509         HDA_BIND_SW("PCM Playback Switch", &ad1986a_bind_pcm_sw),
510         HDA_CODEC_VOLUME("Front Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
511         HDA_CODEC_MUTE("Front Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
512         HDA_CODEC_VOLUME("Surround Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
513         HDA_CODEC_MUTE("Surround Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
514         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x1d, 1, 0x0, HDA_OUTPUT),
515         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x1d, 2, 0x0, HDA_OUTPUT),
516         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x1d, 1, 0x0, HDA_OUTPUT),
517         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x1d, 2, 0x0, HDA_OUTPUT),
518         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x1a, 0x0, HDA_OUTPUT),
519         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1a, 0x0, HDA_OUTPUT),
520         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
521         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
522         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
523         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
524         HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
525         HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
526         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
527         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
528         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
529         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x18, 0x0, HDA_OUTPUT),
530         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x18, 0x0, HDA_OUTPUT),
531         HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
532         HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT),
533         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
534         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
535         {
536                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
537                 .name = "Capture Source",
538                 .info = ad198x_mux_enum_info,
539                 .get = ad198x_mux_enum_get,
540                 .put = ad198x_mux_enum_put,
541         },
542         HDA_CODEC_MUTE("Stereo Downmix Switch", 0x09, 0x0, HDA_OUTPUT),
543         { } /* end */
544 };
545
546 /* additional mixers for 3stack mode */
547 static struct snd_kcontrol_new ad1986a_3st_mixers[] = {
548         {
549                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
550                 .name = "Channel Mode",
551                 .info = ad198x_ch_mode_info,
552                 .get = ad198x_ch_mode_get,
553                 .put = ad198x_ch_mode_put,
554         },
555         { } /* end */
556 };
557
558 /* laptop model - 2ch only */
559 static hda_nid_t ad1986a_laptop_dac_nids[1] = { AD1986A_FRONT_DAC };
560
561 /* master controls both pins 0x1a and 0x1b */
562 static struct hda_bind_ctls ad1986a_laptop_master_vol = {
563         .ops = &snd_hda_bind_vol,
564         .values = {
565                 HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
566                 HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
567                 0,
568         },
569 };
570
571 static struct hda_bind_ctls ad1986a_laptop_master_sw = {
572         .ops = &snd_hda_bind_sw,
573         .values = {
574                 HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
575                 HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
576                 0,
577         },
578 };
579
580 static struct snd_kcontrol_new ad1986a_laptop_mixers[] = {
581         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
582         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
583         HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
584         HDA_BIND_SW("Master Playback Switch", &ad1986a_laptop_master_sw),
585         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
586         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
587         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
588         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
589         HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
590         HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
591         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
592         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
593         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
594         /* HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x18, 0x0, HDA_OUTPUT),
595            HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x18, 0x0, HDA_OUTPUT),
596            HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
597            HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT), */
598         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
599         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
600         {
601                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
602                 .name = "Capture Source",
603                 .info = ad198x_mux_enum_info,
604                 .get = ad198x_mux_enum_get,
605                 .put = ad198x_mux_enum_put,
606         },
607         { } /* end */
608 };
609
610 /* laptop-eapd model - 2ch only */
611
612 static struct hda_input_mux ad1986a_laptop_eapd_capture_source = {
613         .num_items = 3,
614         .items = {
615                 { "Mic", 0x0 },
616                 { "Internal Mic", 0x4 },
617                 { "Mix", 0x5 },
618         },
619 };
620
621 static struct hda_input_mux ad1986a_automic_capture_source = {
622         .num_items = 2,
623         .items = {
624                 { "Mic", 0x0 },
625                 { "Mix", 0x5 },
626         },
627 };
628
629 static struct snd_kcontrol_new ad1986a_laptop_eapd_mixers[] = {
630         HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
631         HDA_BIND_SW("Master Playback Switch", &ad1986a_laptop_master_sw),
632         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
633         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
634         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
635         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
636         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
637         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
638         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
639         {
640                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
641                 .name = "Capture Source",
642                 .info = ad198x_mux_enum_info,
643                 .get = ad198x_mux_enum_get,
644                 .put = ad198x_mux_enum_put,
645         },
646         {
647                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
648                 .name = "External Amplifier",
649                 .info = ad198x_eapd_info,
650                 .get = ad198x_eapd_get,
651                 .put = ad198x_eapd_put,
652                 .private_value = 0x1b | (1 << 8), /* port-D, inversed */
653         },
654         { } /* end */
655 };
656
657 /* re-connect the mic boost input according to the jack sensing */
658 static void ad1986a_automic(struct hda_codec *codec)
659 {
660         unsigned int present;
661         present = snd_hda_codec_read(codec, 0x1f, 0, AC_VERB_GET_PIN_SENSE, 0);
662         /* 0 = 0x1f, 2 = 0x1d, 4 = mixed */
663         snd_hda_codec_write(codec, 0x0f, 0, AC_VERB_SET_CONNECT_SEL,
664                             (present & AC_PINSENSE_PRESENCE) ? 0 : 2);
665 }
666
667 #define AD1986A_MIC_EVENT               0x36
668
669 static void ad1986a_automic_unsol_event(struct hda_codec *codec,
670                                             unsigned int res)
671 {
672         if ((res >> 26) != AD1986A_MIC_EVENT)
673                 return;
674         ad1986a_automic(codec);
675 }
676
677 static int ad1986a_automic_init(struct hda_codec *codec)
678 {
679         ad198x_init(codec);
680         ad1986a_automic(codec);
681         return 0;
682 }
683
684 /* laptop-automute - 2ch only */
685
686 static void ad1986a_update_hp(struct hda_codec *codec)
687 {
688         struct ad198x_spec *spec = codec->spec;
689         unsigned int mute;
690
691         if (spec->jack_present)
692                 mute = HDA_AMP_MUTE; /* mute internal speaker */
693         else
694                 /* unmute internal speaker if necessary */
695                 mute = snd_hda_codec_amp_read(codec, 0x1a, 0, HDA_OUTPUT, 0);
696         snd_hda_codec_amp_stereo(codec, 0x1b, HDA_OUTPUT, 0,
697                                  HDA_AMP_MUTE, mute);
698 }
699
700 static void ad1986a_hp_automute(struct hda_codec *codec)
701 {
702         struct ad198x_spec *spec = codec->spec;
703         unsigned int present;
704
705         present = snd_hda_codec_read(codec, 0x1a, 0, AC_VERB_GET_PIN_SENSE, 0);
706         /* Lenovo N100 seems to report the reversed bit for HP jack-sensing */
707         spec->jack_present = !(present & 0x80000000);
708         ad1986a_update_hp(codec);
709 }
710
711 #define AD1986A_HP_EVENT                0x37
712
713 static void ad1986a_hp_unsol_event(struct hda_codec *codec, unsigned int res)
714 {
715         if ((res >> 26) != AD1986A_HP_EVENT)
716                 return;
717         ad1986a_hp_automute(codec);
718 }
719
720 static int ad1986a_hp_init(struct hda_codec *codec)
721 {
722         ad198x_init(codec);
723         ad1986a_hp_automute(codec);
724         return 0;
725 }
726
727 /* bind hp and internal speaker mute (with plug check) */
728 static int ad1986a_hp_master_sw_put(struct snd_kcontrol *kcontrol,
729                                     struct snd_ctl_elem_value *ucontrol)
730 {
731         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
732         long *valp = ucontrol->value.integer.value;
733         int change;
734
735         change = snd_hda_codec_amp_update(codec, 0x1a, 0, HDA_OUTPUT, 0,
736                                           HDA_AMP_MUTE,
737                                           valp[0] ? 0 : HDA_AMP_MUTE);
738         change |= snd_hda_codec_amp_update(codec, 0x1a, 1, HDA_OUTPUT, 0,
739                                            HDA_AMP_MUTE,
740                                            valp[1] ? 0 : HDA_AMP_MUTE);
741         if (change)
742                 ad1986a_update_hp(codec);
743         return change;
744 }
745
746 static struct snd_kcontrol_new ad1986a_laptop_automute_mixers[] = {
747         HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
748         {
749                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
750                 .name = "Master Playback Switch",
751                 .info = snd_hda_mixer_amp_switch_info,
752                 .get = snd_hda_mixer_amp_switch_get,
753                 .put = ad1986a_hp_master_sw_put,
754                 .private_value = HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
755         },
756         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
757         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
758         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0x0, HDA_OUTPUT),
759         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0x0, HDA_OUTPUT),
760         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
761         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
762         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
763         HDA_CODEC_VOLUME("Beep Playback Volume", 0x18, 0x0, HDA_OUTPUT),
764         HDA_CODEC_MUTE("Beep Playback Switch", 0x18, 0x0, HDA_OUTPUT),
765         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
766         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
767         {
768                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
769                 .name = "Capture Source",
770                 .info = ad198x_mux_enum_info,
771                 .get = ad198x_mux_enum_get,
772                 .put = ad198x_mux_enum_put,
773         },
774         {
775                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
776                 .name = "External Amplifier",
777                 .info = ad198x_eapd_info,
778                 .get = ad198x_eapd_get,
779                 .put = ad198x_eapd_put,
780                 .private_value = 0x1b | (1 << 8), /* port-D, inversed */
781         },
782         { } /* end */
783 };
784
785 /*
786  * initialization verbs
787  */
788 static struct hda_verb ad1986a_init_verbs[] = {
789         /* Front, Surround, CLFE DAC; mute as default */
790         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
791         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
792         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
793         /* Downmix - off */
794         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
795         /* HP, Line-Out, Surround, CLFE selectors */
796         {0x0a, AC_VERB_SET_CONNECT_SEL, 0x0},
797         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x0},
798         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
799         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
800         /* Mono selector */
801         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x0},
802         /* Mic selector: Mic 1/2 pin */
803         {0x0f, AC_VERB_SET_CONNECT_SEL, 0x0},
804         /* Line-in selector: Line-in */
805         {0x10, AC_VERB_SET_CONNECT_SEL, 0x0},
806         /* Mic 1/2 swap */
807         {0x11, AC_VERB_SET_CONNECT_SEL, 0x0},
808         /* Record selector: mic */
809         {0x12, AC_VERB_SET_CONNECT_SEL, 0x0},
810         /* Mic, Phone, CD, Aux, Line-In amp; mute as default */
811         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
812         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
813         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
814         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
815         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
816         /* PC beep */
817         {0x18, AC_VERB_SET_CONNECT_SEL, 0x0},
818         /* HP, Line-Out, Surround, CLFE, Mono pins; mute as default */
819         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
820         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
821         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
822         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
823         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
824         /* HP Pin */
825         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
826         /* Front, Surround, CLFE Pins */
827         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
828         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
829         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
830         /* Mono Pin */
831         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
832         /* Mic Pin */
833         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
834         /* Line, Aux, CD, Beep-In Pin */
835         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
836         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
837         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
838         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
839         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
840         { } /* end */
841 };
842
843 static struct hda_verb ad1986a_ch2_init[] = {
844         /* Surround out -> Line In */
845         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
846         /* Line-in selectors */
847         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x1 },
848         /* CLFE -> Mic in */
849         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
850         /* Mic selector, mix C/LFE (backmic) and Mic (frontmic) */
851         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x4 },
852         { } /* end */
853 };
854
855 static struct hda_verb ad1986a_ch4_init[] = {
856         /* Surround out -> Surround */
857         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
858         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
859         /* CLFE -> Mic in */
860         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
861         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x4 },
862         { } /* end */
863 };
864
865 static struct hda_verb ad1986a_ch6_init[] = {
866         /* Surround out -> Surround out */
867         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
868         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
869         /* CLFE -> CLFE */
870         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
871         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x0 },
872         { } /* end */
873 };
874
875 static struct hda_channel_mode ad1986a_modes[3] = {
876         { 2, ad1986a_ch2_init },
877         { 4, ad1986a_ch4_init },
878         { 6, ad1986a_ch6_init },
879 };
880
881 /* eapd initialization */
882 static struct hda_verb ad1986a_eapd_init_verbs[] = {
883         {0x1b, AC_VERB_SET_EAPD_BTLENABLE, 0x00 },
884         {}
885 };
886
887 static struct hda_verb ad1986a_automic_verbs[] = {
888         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
889         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
890         /*{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},*/
891         {0x0f, AC_VERB_SET_CONNECT_SEL, 0x0},
892         {0x1f, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1986A_MIC_EVENT},
893         {}
894 };
895
896 /* Ultra initialization */
897 static struct hda_verb ad1986a_ultra_init[] = {
898         /* eapd initialization */
899         { 0x1b, AC_VERB_SET_EAPD_BTLENABLE, 0x00 },
900         /* CLFE -> Mic in */
901         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x2 },
902         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
903         { 0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080 },
904         { } /* end */
905 };
906
907 /* pin sensing on HP jack */
908 static struct hda_verb ad1986a_hp_init_verbs[] = {
909         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1986A_HP_EVENT},
910         {}
911 };
912
913
914 /* models */
915 enum {
916         AD1986A_6STACK,
917         AD1986A_3STACK,
918         AD1986A_LAPTOP,
919         AD1986A_LAPTOP_EAPD,
920         AD1986A_LAPTOP_AUTOMUTE,
921         AD1986A_ULTRA,
922         AD1986A_MODELS
923 };
924
925 static const char *ad1986a_models[AD1986A_MODELS] = {
926         [AD1986A_6STACK]        = "6stack",
927         [AD1986A_3STACK]        = "3stack",
928         [AD1986A_LAPTOP]        = "laptop",
929         [AD1986A_LAPTOP_EAPD]   = "laptop-eapd",
930         [AD1986A_LAPTOP_AUTOMUTE] = "laptop-automute",
931         [AD1986A_ULTRA]         = "ultra",
932 };
933
934 static struct snd_pci_quirk ad1986a_cfg_tbl[] = {
935         SND_PCI_QUIRK(0x103c, 0x30af, "HP B2800", AD1986A_LAPTOP_EAPD),
936         SND_PCI_QUIRK(0x1043, 0x1153, "ASUS M9", AD1986A_LAPTOP_EAPD),
937         SND_PCI_QUIRK(0x1043, 0x11f7, "ASUS U5A", AD1986A_LAPTOP_EAPD),
938         SND_PCI_QUIRK(0x1043, 0x1213, "ASUS A6J", AD1986A_LAPTOP_EAPD),
939         SND_PCI_QUIRK(0x1043, 0x1263, "ASUS U5F", AD1986A_LAPTOP_EAPD),
940         SND_PCI_QUIRK(0x1043, 0x1297, "ASUS Z62F", AD1986A_LAPTOP_EAPD),
941         SND_PCI_QUIRK(0x1043, 0x12b3, "ASUS V1j", AD1986A_LAPTOP_EAPD),
942         SND_PCI_QUIRK(0x1043, 0x1302, "ASUS W3j", AD1986A_LAPTOP_EAPD),
943         SND_PCI_QUIRK(0x1043, 0x1443, "ASUS VX1", AD1986A_LAPTOP),
944         SND_PCI_QUIRK(0x1043, 0x1447, "ASUS A8J", AD1986A_3STACK),
945         SND_PCI_QUIRK(0x1043, 0x817f, "ASUS P5", AD1986A_3STACK),
946         SND_PCI_QUIRK(0x1043, 0x818f, "ASUS P5", AD1986A_LAPTOP),
947         SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS P5", AD1986A_3STACK),
948         SND_PCI_QUIRK(0x1043, 0x81cb, "ASUS M2N", AD1986A_3STACK),
949         SND_PCI_QUIRK(0x1043, 0x8234, "ASUS M2N", AD1986A_3STACK),
950         SND_PCI_QUIRK(0x10de, 0xcb84, "ASUS A8N-VM", AD1986A_3STACK),
951         SND_PCI_QUIRK(0x1179, 0xff40, "Toshiba", AD1986A_LAPTOP_EAPD),
952         SND_PCI_QUIRK(0x144d, 0xb03c, "Samsung R55", AD1986A_3STACK),
953         SND_PCI_QUIRK(0x144d, 0xc01e, "FSC V2060", AD1986A_LAPTOP),
954         SND_PCI_QUIRK(0x144d, 0xc023, "Samsung X60", AD1986A_LAPTOP_EAPD),
955         SND_PCI_QUIRK(0x144d, 0xc024, "Samsung R65", AD1986A_LAPTOP_EAPD),
956         SND_PCI_QUIRK(0x144d, 0xc026, "Samsung X11", AD1986A_LAPTOP_EAPD),
957         SND_PCI_QUIRK(0x144d, 0xc027, "Samsung Q1", AD1986A_ULTRA),
958         SND_PCI_QUIRK(0x144d, 0xc504, "Samsung Q35", AD1986A_3STACK),
959         SND_PCI_QUIRK(0x17aa, 0x1011, "Lenovo M55", AD1986A_LAPTOP),
960         SND_PCI_QUIRK(0x17aa, 0x1017, "Lenovo A60", AD1986A_3STACK),
961         SND_PCI_QUIRK(0x17aa, 0x2066, "Lenovo N100", AD1986A_LAPTOP_AUTOMUTE),
962         SND_PCI_QUIRK(0x17c0, 0x2017, "Samsung M50", AD1986A_LAPTOP),
963         {}
964 };
965
966 #ifdef CONFIG_SND_HDA_POWER_SAVE
967 static struct hda_amp_list ad1986a_loopbacks[] = {
968         { 0x13, HDA_OUTPUT, 0 }, /* Mic */
969         { 0x14, HDA_OUTPUT, 0 }, /* Phone */
970         { 0x15, HDA_OUTPUT, 0 }, /* CD */
971         { 0x16, HDA_OUTPUT, 0 }, /* Aux */
972         { 0x17, HDA_OUTPUT, 0 }, /* Line */
973         { } /* end */
974 };
975 #endif
976
977 static int is_jack_available(struct hda_codec *codec, hda_nid_t nid)
978 {
979         unsigned int conf = snd_hda_codec_read(codec, nid, 0,
980                                                AC_VERB_GET_CONFIG_DEFAULT, 0);
981         return get_defcfg_connect(conf) != AC_JACK_PORT_NONE;
982 }
983
984 static int patch_ad1986a(struct hda_codec *codec)
985 {
986         struct ad198x_spec *spec;
987         int board_config;
988
989         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
990         if (spec == NULL)
991                 return -ENOMEM;
992
993         codec->spec = spec;
994
995         spec->multiout.max_channels = 6;
996         spec->multiout.num_dacs = ARRAY_SIZE(ad1986a_dac_nids);
997         spec->multiout.dac_nids = ad1986a_dac_nids;
998         spec->multiout.dig_out_nid = AD1986A_SPDIF_OUT;
999         spec->num_adc_nids = 1;
1000         spec->adc_nids = ad1986a_adc_nids;
1001         spec->capsrc_nids = ad1986a_capsrc_nids;
1002         spec->input_mux = &ad1986a_capture_source;
1003         spec->num_mixers = 1;
1004         spec->mixers[0] = ad1986a_mixers;
1005         spec->num_init_verbs = 1;
1006         spec->init_verbs[0] = ad1986a_init_verbs;
1007 #ifdef CONFIG_SND_HDA_POWER_SAVE
1008         spec->loopback.amplist = ad1986a_loopbacks;
1009 #endif
1010         spec->vmaster_nid = 0x1b;
1011
1012         codec->patch_ops = ad198x_patch_ops;
1013
1014         /* override some parameters */
1015         board_config = snd_hda_check_board_config(codec, AD1986A_MODELS,
1016                                                   ad1986a_models,
1017                                                   ad1986a_cfg_tbl);
1018         switch (board_config) {
1019         case AD1986A_3STACK:
1020                 spec->num_mixers = 2;
1021                 spec->mixers[1] = ad1986a_3st_mixers;
1022                 spec->num_init_verbs = 2;
1023                 spec->init_verbs[1] = ad1986a_ch2_init;
1024                 spec->channel_mode = ad1986a_modes;
1025                 spec->num_channel_mode = ARRAY_SIZE(ad1986a_modes);
1026                 spec->need_dac_fix = 1;
1027                 spec->multiout.max_channels = 2;
1028                 spec->multiout.num_dacs = 1;
1029                 break;
1030         case AD1986A_LAPTOP:
1031                 spec->mixers[0] = ad1986a_laptop_mixers;
1032                 spec->multiout.max_channels = 2;
1033                 spec->multiout.num_dacs = 1;
1034                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1035                 break;
1036         case AD1986A_LAPTOP_EAPD:
1037                 spec->mixers[0] = ad1986a_laptop_eapd_mixers;
1038                 spec->num_init_verbs = 3;
1039                 spec->init_verbs[1] = ad1986a_eapd_init_verbs;
1040                 spec->init_verbs[2] = ad1986a_automic_verbs;
1041                 spec->multiout.max_channels = 2;
1042                 spec->multiout.num_dacs = 1;
1043                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1044                 if (!is_jack_available(codec, 0x25))
1045                         spec->multiout.dig_out_nid = 0;
1046                 spec->input_mux = &ad1986a_automic_capture_source;
1047                 codec->patch_ops.unsol_event = ad1986a_automic_unsol_event;
1048                 codec->patch_ops.init = ad1986a_automic_init;
1049                 break;
1050         case AD1986A_LAPTOP_AUTOMUTE:
1051                 spec->mixers[0] = ad1986a_laptop_automute_mixers;
1052                 spec->num_init_verbs = 3;
1053                 spec->init_verbs[1] = ad1986a_eapd_init_verbs;
1054                 spec->init_verbs[2] = ad1986a_hp_init_verbs;
1055                 spec->multiout.max_channels = 2;
1056                 spec->multiout.num_dacs = 1;
1057                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1058                 if (!is_jack_available(codec, 0x25))
1059                         spec->multiout.dig_out_nid = 0;
1060                 spec->input_mux = &ad1986a_laptop_eapd_capture_source;
1061                 codec->patch_ops.unsol_event = ad1986a_hp_unsol_event;
1062                 codec->patch_ops.init = ad1986a_hp_init;
1063                 break;
1064         case AD1986A_ULTRA:
1065                 spec->mixers[0] = ad1986a_laptop_eapd_mixers;
1066                 spec->num_init_verbs = 2;
1067                 spec->init_verbs[1] = ad1986a_ultra_init;
1068                 spec->multiout.max_channels = 2;
1069                 spec->multiout.num_dacs = 1;
1070                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
1071                 spec->multiout.dig_out_nid = 0;
1072                 break;
1073         }
1074
1075         /* AD1986A has a hardware problem that it can't share a stream
1076          * with multiple output pins.  The copy of front to surrounds
1077          * causes noisy or silent outputs at a certain timing, e.g.
1078          * changing the volume.
1079          * So, let's disable the shared stream.
1080          */
1081         spec->multiout.no_share_stream = 1;
1082
1083         return 0;
1084 }
1085
1086 /*
1087  * AD1983 specific
1088  */
1089
1090 #define AD1983_SPDIF_OUT        0x02
1091 #define AD1983_DAC              0x03
1092 #define AD1983_ADC              0x04
1093
1094 static hda_nid_t ad1983_dac_nids[1] = { AD1983_DAC };
1095 static hda_nid_t ad1983_adc_nids[1] = { AD1983_ADC };
1096 static hda_nid_t ad1983_capsrc_nids[1] = { 0x15 };
1097
1098 static struct hda_input_mux ad1983_capture_source = {
1099         .num_items = 4,
1100         .items = {
1101                 { "Mic", 0x0 },
1102                 { "Line", 0x1 },
1103                 { "Mix", 0x2 },
1104                 { "Mix Mono", 0x3 },
1105         },
1106 };
1107
1108 /*
1109  * SPDIF playback route
1110  */
1111 static int ad1983_spdif_route_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1112 {
1113         static char *texts[] = { "PCM", "ADC" };
1114
1115         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1116         uinfo->count = 1;
1117         uinfo->value.enumerated.items = 2;
1118         if (uinfo->value.enumerated.item > 1)
1119                 uinfo->value.enumerated.item = 1;
1120         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1121         return 0;
1122 }
1123
1124 static int ad1983_spdif_route_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1125 {
1126         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1127         struct ad198x_spec *spec = codec->spec;
1128
1129         ucontrol->value.enumerated.item[0] = spec->spdif_route;
1130         return 0;
1131 }
1132
1133 static int ad1983_spdif_route_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1134 {
1135         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1136         struct ad198x_spec *spec = codec->spec;
1137
1138         if (ucontrol->value.enumerated.item[0] > 1)
1139                 return -EINVAL;
1140         if (spec->spdif_route != ucontrol->value.enumerated.item[0]) {
1141                 spec->spdif_route = ucontrol->value.enumerated.item[0];
1142                 snd_hda_codec_write_cache(codec, spec->multiout.dig_out_nid, 0,
1143                                           AC_VERB_SET_CONNECT_SEL,
1144                                           spec->spdif_route);
1145                 return 1;
1146         }
1147         return 0;
1148 }
1149
1150 static struct snd_kcontrol_new ad1983_mixers[] = {
1151         HDA_CODEC_VOLUME("Front Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1152         HDA_CODEC_MUTE("Front Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1153         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1154         HDA_CODEC_MUTE("Headphone Playback Switch", 0x06, 0x0, HDA_OUTPUT),
1155         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x07, 1, 0x0, HDA_OUTPUT),
1156         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x07, 1, 0x0, HDA_OUTPUT),
1157         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1158         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1159         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1160         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1161         HDA_CODEC_VOLUME("Line Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1162         HDA_CODEC_MUTE("Line Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1163         HDA_CODEC_VOLUME_MONO("PC Speaker Playback Volume", 0x10, 1, 0x0, HDA_OUTPUT),
1164         HDA_CODEC_MUTE_MONO("PC Speaker Playback Switch", 0x10, 1, 0x0, HDA_OUTPUT),
1165         HDA_CODEC_VOLUME("Mic Boost", 0x0c, 0x0, HDA_OUTPUT),
1166         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1167         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1168         {
1169                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1170                 .name = "Capture Source",
1171                 .info = ad198x_mux_enum_info,
1172                 .get = ad198x_mux_enum_get,
1173                 .put = ad198x_mux_enum_put,
1174         },
1175         {
1176                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1177                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1178                 .info = ad1983_spdif_route_info,
1179                 .get = ad1983_spdif_route_get,
1180                 .put = ad1983_spdif_route_put,
1181         },
1182         { } /* end */
1183 };
1184
1185 static struct hda_verb ad1983_init_verbs[] = {
1186         /* Front, HP, Mono; mute as default */
1187         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1188         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1189         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1190         /* Beep, PCM, Mic, Line-In: mute */
1191         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1192         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1193         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1194         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1195         /* Front, HP selectors; from Mix */
1196         {0x05, AC_VERB_SET_CONNECT_SEL, 0x01},
1197         {0x06, AC_VERB_SET_CONNECT_SEL, 0x01},
1198         /* Mono selector; from Mix */
1199         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x03},
1200         /* Mic selector; Mic */
1201         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
1202         /* Line-in selector: Line-in */
1203         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
1204         /* Mic boost: 0dB */
1205         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1206         /* Record selector: mic */
1207         {0x15, AC_VERB_SET_CONNECT_SEL, 0x0},
1208         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1209         /* SPDIF route: PCM */
1210         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0},
1211         /* Front Pin */
1212         {0x05, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1213         /* HP Pin */
1214         {0x06, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
1215         /* Mono Pin */
1216         {0x07, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1217         /* Mic Pin */
1218         {0x08, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1219         /* Line Pin */
1220         {0x09, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
1221         { } /* end */
1222 };
1223
1224 #ifdef CONFIG_SND_HDA_POWER_SAVE
1225 static struct hda_amp_list ad1983_loopbacks[] = {
1226         { 0x12, HDA_OUTPUT, 0 }, /* Mic */
1227         { 0x13, HDA_OUTPUT, 0 }, /* Line */
1228         { } /* end */
1229 };
1230 #endif
1231
1232 static int patch_ad1983(struct hda_codec *codec)
1233 {
1234         struct ad198x_spec *spec;
1235
1236         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1237         if (spec == NULL)
1238                 return -ENOMEM;
1239
1240         codec->spec = spec;
1241
1242         spec->multiout.max_channels = 2;
1243         spec->multiout.num_dacs = ARRAY_SIZE(ad1983_dac_nids);
1244         spec->multiout.dac_nids = ad1983_dac_nids;
1245         spec->multiout.dig_out_nid = AD1983_SPDIF_OUT;
1246         spec->num_adc_nids = 1;
1247         spec->adc_nids = ad1983_adc_nids;
1248         spec->capsrc_nids = ad1983_capsrc_nids;
1249         spec->input_mux = &ad1983_capture_source;
1250         spec->num_mixers = 1;
1251         spec->mixers[0] = ad1983_mixers;
1252         spec->num_init_verbs = 1;
1253         spec->init_verbs[0] = ad1983_init_verbs;
1254         spec->spdif_route = 0;
1255 #ifdef CONFIG_SND_HDA_POWER_SAVE
1256         spec->loopback.amplist = ad1983_loopbacks;
1257 #endif
1258         spec->vmaster_nid = 0x05;
1259
1260         codec->patch_ops = ad198x_patch_ops;
1261
1262         return 0;
1263 }
1264
1265
1266 /*
1267  * AD1981 HD specific
1268  */
1269
1270 #define AD1981_SPDIF_OUT        0x02
1271 #define AD1981_DAC              0x03
1272 #define AD1981_ADC              0x04
1273
1274 static hda_nid_t ad1981_dac_nids[1] = { AD1981_DAC };
1275 static hda_nid_t ad1981_adc_nids[1] = { AD1981_ADC };
1276 static hda_nid_t ad1981_capsrc_nids[1] = { 0x15 };
1277
1278 /* 0x0c, 0x09, 0x0e, 0x0f, 0x19, 0x05, 0x18, 0x17 */
1279 static struct hda_input_mux ad1981_capture_source = {
1280         .num_items = 7,
1281         .items = {
1282                 { "Front Mic", 0x0 },
1283                 { "Line", 0x1 },
1284                 { "Mix", 0x2 },
1285                 { "Mix Mono", 0x3 },
1286                 { "CD", 0x4 },
1287                 { "Mic", 0x6 },
1288                 { "Aux", 0x7 },
1289         },
1290 };
1291
1292 static struct snd_kcontrol_new ad1981_mixers[] = {
1293         HDA_CODEC_VOLUME("Front Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1294         HDA_CODEC_MUTE("Front Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1295         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1296         HDA_CODEC_MUTE("Headphone Playback Switch", 0x06, 0x0, HDA_OUTPUT),
1297         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x07, 1, 0x0, HDA_OUTPUT),
1298         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x07, 1, 0x0, HDA_OUTPUT),
1299         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1300         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1301         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1302         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1303         HDA_CODEC_VOLUME("Line Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1304         HDA_CODEC_MUTE("Line Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1305         HDA_CODEC_VOLUME("Aux Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
1306         HDA_CODEC_MUTE("Aux Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
1307         HDA_CODEC_VOLUME("Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
1308         HDA_CODEC_MUTE("Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
1309         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1310         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1311         HDA_CODEC_VOLUME_MONO("PC Speaker Playback Volume", 0x0d, 1, 0x0, HDA_OUTPUT),
1312         HDA_CODEC_MUTE_MONO("PC Speaker Playback Switch", 0x0d, 1, 0x0, HDA_OUTPUT),
1313         HDA_CODEC_VOLUME("Front Mic Boost", 0x08, 0x0, HDA_INPUT),
1314         HDA_CODEC_VOLUME("Mic Boost", 0x18, 0x0, HDA_INPUT),
1315         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1316         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1317         {
1318                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1319                 .name = "Capture Source",
1320                 .info = ad198x_mux_enum_info,
1321                 .get = ad198x_mux_enum_get,
1322                 .put = ad198x_mux_enum_put,
1323         },
1324         /* identical with AD1983 */
1325         {
1326                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1327                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1328                 .info = ad1983_spdif_route_info,
1329                 .get = ad1983_spdif_route_get,
1330                 .put = ad1983_spdif_route_put,
1331         },
1332         { } /* end */
1333 };
1334
1335 static struct hda_verb ad1981_init_verbs[] = {
1336         /* Front, HP, Mono; mute as default */
1337         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1338         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1339         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1340         /* Beep, PCM, Front Mic, Line, Rear Mic, Aux, CD-In: mute */
1341         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1342         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1343         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1344         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1345         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1346         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1347         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1348         /* Front, HP selectors; from Mix */
1349         {0x05, AC_VERB_SET_CONNECT_SEL, 0x01},
1350         {0x06, AC_VERB_SET_CONNECT_SEL, 0x01},
1351         /* Mono selector; from Mix */
1352         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x03},
1353         /* Mic Mixer; select Front Mic */
1354         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1355         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1356         /* Mic boost: 0dB */
1357         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1358         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1359         /* Record selector: Front mic */
1360         {0x15, AC_VERB_SET_CONNECT_SEL, 0x0},
1361         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1362         /* SPDIF route: PCM */
1363         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0},
1364         /* Front Pin */
1365         {0x05, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1366         /* HP Pin */
1367         {0x06, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
1368         /* Mono Pin */
1369         {0x07, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1370         /* Front & Rear Mic Pins */
1371         {0x08, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1372         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1373         /* Line Pin */
1374         {0x09, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
1375         /* Digital Beep */
1376         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x00},
1377         /* Line-Out as Input: disabled */
1378         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1379         { } /* end */
1380 };
1381
1382 #ifdef CONFIG_SND_HDA_POWER_SAVE
1383 static struct hda_amp_list ad1981_loopbacks[] = {
1384         { 0x12, HDA_OUTPUT, 0 }, /* Front Mic */
1385         { 0x13, HDA_OUTPUT, 0 }, /* Line */
1386         { 0x1b, HDA_OUTPUT, 0 }, /* Aux */
1387         { 0x1c, HDA_OUTPUT, 0 }, /* Mic */
1388         { 0x1d, HDA_OUTPUT, 0 }, /* CD */
1389         { } /* end */
1390 };
1391 #endif
1392
1393 /*
1394  * Patch for HP nx6320
1395  *
1396  * nx6320 uses EAPD in the reverse way - EAPD-on means the internal
1397  * speaker output enabled _and_ mute-LED off.
1398  */
1399
1400 #define AD1981_HP_EVENT         0x37
1401 #define AD1981_MIC_EVENT        0x38
1402
1403 static struct hda_verb ad1981_hp_init_verbs[] = {
1404         {0x05, AC_VERB_SET_EAPD_BTLENABLE, 0x00 }, /* default off */
1405         /* pin sensing on HP and Mic jacks */
1406         {0x06, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_HP_EVENT},
1407         {0x08, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_MIC_EVENT},
1408         {}
1409 };
1410
1411 /* turn on/off EAPD (+ mute HP) as a master switch */
1412 static int ad1981_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1413                                    struct snd_ctl_elem_value *ucontrol)
1414 {
1415         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1416         struct ad198x_spec *spec = codec->spec;
1417
1418         if (! ad198x_eapd_put(kcontrol, ucontrol))
1419                 return 0;
1420         /* change speaker pin appropriately */
1421         snd_hda_codec_write(codec, 0x05, 0,
1422                             AC_VERB_SET_PIN_WIDGET_CONTROL,
1423                             spec->cur_eapd ? PIN_OUT : 0);
1424         /* toggle HP mute appropriately */
1425         snd_hda_codec_amp_stereo(codec, 0x06, HDA_OUTPUT, 0,
1426                                  HDA_AMP_MUTE,
1427                                  spec->cur_eapd ? 0 : HDA_AMP_MUTE);
1428         return 1;
1429 }
1430
1431 /* bind volumes of both NID 0x05 and 0x06 */
1432 static struct hda_bind_ctls ad1981_hp_bind_master_vol = {
1433         .ops = &snd_hda_bind_vol,
1434         .values = {
1435                 HDA_COMPOSE_AMP_VAL(0x05, 3, 0, HDA_OUTPUT),
1436                 HDA_COMPOSE_AMP_VAL(0x06, 3, 0, HDA_OUTPUT),
1437                 0
1438         },
1439 };
1440
1441 /* mute internal speaker if HP is plugged */
1442 static void ad1981_hp_automute(struct hda_codec *codec)
1443 {
1444         unsigned int present;
1445
1446         present = snd_hda_codec_read(codec, 0x06, 0,
1447                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1448         snd_hda_codec_amp_stereo(codec, 0x05, HDA_OUTPUT, 0,
1449                                  HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
1450 }
1451
1452 /* toggle input of built-in and mic jack appropriately */
1453 static void ad1981_hp_automic(struct hda_codec *codec)
1454 {
1455         static struct hda_verb mic_jack_on[] = {
1456                 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1457                 {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1458                 {}
1459         };
1460         static struct hda_verb mic_jack_off[] = {
1461                 {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1462                 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1463                 {}
1464         };
1465         unsigned int present;
1466
1467         present = snd_hda_codec_read(codec, 0x08, 0,
1468                                  AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1469         if (present)
1470                 snd_hda_sequence_write(codec, mic_jack_on);
1471         else
1472                 snd_hda_sequence_write(codec, mic_jack_off);
1473 }
1474
1475 /* unsolicited event for HP jack sensing */
1476 static void ad1981_hp_unsol_event(struct hda_codec *codec,
1477                                   unsigned int res)
1478 {
1479         res >>= 26;
1480         switch (res) {
1481         case AD1981_HP_EVENT:
1482                 ad1981_hp_automute(codec);
1483                 break;
1484         case AD1981_MIC_EVENT:
1485                 ad1981_hp_automic(codec);
1486                 break;
1487         }
1488 }
1489
1490 static struct hda_input_mux ad1981_hp_capture_source = {
1491         .num_items = 3,
1492         .items = {
1493                 { "Mic", 0x0 },
1494                 { "Docking-Station", 0x1 },
1495                 { "Mix", 0x2 },
1496         },
1497 };
1498
1499 static struct snd_kcontrol_new ad1981_hp_mixers[] = {
1500         HDA_BIND_VOL("Master Playback Volume", &ad1981_hp_bind_master_vol),
1501         {
1502                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1503                 .name = "Master Playback Switch",
1504                 .info = ad198x_eapd_info,
1505                 .get = ad198x_eapd_get,
1506                 .put = ad1981_hp_master_sw_put,
1507                 .private_value = 0x05,
1508         },
1509         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1510         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1511 #if 0
1512         /* FIXME: analog mic/line loopback doesn't work with my tests...
1513          *        (although recording is OK)
1514          */
1515         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1516         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1517         HDA_CODEC_VOLUME("Docking-Station Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1518         HDA_CODEC_MUTE("Docking-Station Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1519         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
1520         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
1521         /* FIXME: does this laptop have analog CD connection? */
1522         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1523         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1524 #endif
1525         HDA_CODEC_VOLUME("Mic Boost", 0x08, 0x0, HDA_INPUT),
1526         HDA_CODEC_VOLUME("Internal Mic Boost", 0x18, 0x0, HDA_INPUT),
1527         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1528         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1529         {
1530                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1531                 .name = "Capture Source",
1532                 .info = ad198x_mux_enum_info,
1533                 .get = ad198x_mux_enum_get,
1534                 .put = ad198x_mux_enum_put,
1535         },
1536         { } /* end */
1537 };
1538
1539 /* initialize jack-sensing, too */
1540 static int ad1981_hp_init(struct hda_codec *codec)
1541 {
1542         ad198x_init(codec);
1543         ad1981_hp_automute(codec);
1544         ad1981_hp_automic(codec);
1545         return 0;
1546 }
1547
1548 /* configuration for Toshiba Laptops */
1549 static struct hda_verb ad1981_toshiba_init_verbs[] = {
1550         {0x05, AC_VERB_SET_EAPD_BTLENABLE, 0x01 }, /* default on */
1551         /* pin sensing on HP and Mic jacks */
1552         {0x06, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_HP_EVENT},
1553         {0x08, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_MIC_EVENT},
1554         {}
1555 };
1556
1557 static struct snd_kcontrol_new ad1981_toshiba_mixers[] = {
1558         HDA_CODEC_VOLUME("Amp Volume", 0x1a, 0x0, HDA_OUTPUT),
1559         HDA_CODEC_MUTE("Amp Switch", 0x1a, 0x0, HDA_OUTPUT),
1560         { }
1561 };
1562
1563 /* configuration for Lenovo Thinkpad T60 */
1564 static struct snd_kcontrol_new ad1981_thinkpad_mixers[] = {
1565         HDA_CODEC_VOLUME("Master Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1566         HDA_CODEC_MUTE("Master Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1567         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1568         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1569         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1570         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1571         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1572         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1573         HDA_CODEC_VOLUME("Mic Boost", 0x08, 0x0, HDA_INPUT),
1574         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1575         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1576         {
1577                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1578                 .name = "Capture Source",
1579                 .info = ad198x_mux_enum_info,
1580                 .get = ad198x_mux_enum_get,
1581                 .put = ad198x_mux_enum_put,
1582         },
1583         /* identical with AD1983 */
1584         {
1585                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1586                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1587                 .info = ad1983_spdif_route_info,
1588                 .get = ad1983_spdif_route_get,
1589                 .put = ad1983_spdif_route_put,
1590         },
1591         { } /* end */
1592 };
1593
1594 static struct hda_input_mux ad1981_thinkpad_capture_source = {
1595         .num_items = 3,
1596         .items = {
1597                 { "Mic", 0x0 },
1598                 { "Mix", 0x2 },
1599                 { "CD", 0x4 },
1600         },
1601 };
1602
1603 /* models */
1604 enum {
1605         AD1981_BASIC,
1606         AD1981_HP,
1607         AD1981_THINKPAD,
1608         AD1981_TOSHIBA,
1609         AD1981_MODELS
1610 };
1611
1612 static const char *ad1981_models[AD1981_MODELS] = {
1613         [AD1981_HP]             = "hp",
1614         [AD1981_THINKPAD]       = "thinkpad",
1615         [AD1981_BASIC]          = "basic",
1616         [AD1981_TOSHIBA]        = "toshiba"
1617 };
1618
1619 static struct snd_pci_quirk ad1981_cfg_tbl[] = {
1620         SND_PCI_QUIRK(0x1014, 0x0597, "Lenovo Z60", AD1981_THINKPAD),
1621         /* All HP models */
1622         SND_PCI_QUIRK(0x103c, 0, "HP nx", AD1981_HP),
1623         SND_PCI_QUIRK(0x1179, 0x0001, "Toshiba U205", AD1981_TOSHIBA),
1624         /* Lenovo Thinkpad T60/X60/Z6xx */
1625         SND_PCI_QUIRK(0x17aa, 0, "Lenovo Thinkpad", AD1981_THINKPAD),
1626         /* HP nx6320 (reversed SSID, H/W bug) */
1627         SND_PCI_QUIRK(0x30b0, 0x103c, "HP nx6320", AD1981_HP),
1628         {}
1629 };
1630
1631 static int patch_ad1981(struct hda_codec *codec)
1632 {
1633         struct ad198x_spec *spec;
1634         int board_config;
1635
1636         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1637         if (spec == NULL)
1638                 return -ENOMEM;
1639
1640         codec->spec = spec;
1641
1642         spec->multiout.max_channels = 2;
1643         spec->multiout.num_dacs = ARRAY_SIZE(ad1981_dac_nids);
1644         spec->multiout.dac_nids = ad1981_dac_nids;
1645         spec->multiout.dig_out_nid = AD1981_SPDIF_OUT;
1646         spec->num_adc_nids = 1;
1647         spec->adc_nids = ad1981_adc_nids;
1648         spec->capsrc_nids = ad1981_capsrc_nids;
1649         spec->input_mux = &ad1981_capture_source;
1650         spec->num_mixers = 1;
1651         spec->mixers[0] = ad1981_mixers;
1652         spec->num_init_verbs = 1;
1653         spec->init_verbs[0] = ad1981_init_verbs;
1654         spec->spdif_route = 0;
1655 #ifdef CONFIG_SND_HDA_POWER_SAVE
1656         spec->loopback.amplist = ad1981_loopbacks;
1657 #endif
1658         spec->vmaster_nid = 0x05;
1659
1660         codec->patch_ops = ad198x_patch_ops;
1661
1662         /* override some parameters */
1663         board_config = snd_hda_check_board_config(codec, AD1981_MODELS,
1664                                                   ad1981_models,
1665                                                   ad1981_cfg_tbl);
1666         switch (board_config) {
1667         case AD1981_HP:
1668                 spec->mixers[0] = ad1981_hp_mixers;
1669                 spec->num_init_verbs = 2;
1670                 spec->init_verbs[1] = ad1981_hp_init_verbs;
1671                 spec->multiout.dig_out_nid = 0;
1672                 spec->input_mux = &ad1981_hp_capture_source;
1673
1674                 codec->patch_ops.init = ad1981_hp_init;
1675                 codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
1676                 break;
1677         case AD1981_THINKPAD:
1678                 spec->mixers[0] = ad1981_thinkpad_mixers;
1679                 spec->input_mux = &ad1981_thinkpad_capture_source;
1680                 break;
1681         case AD1981_TOSHIBA:
1682                 spec->mixers[0] = ad1981_hp_mixers;
1683                 spec->mixers[1] = ad1981_toshiba_mixers;
1684                 spec->num_init_verbs = 2;
1685                 spec->init_verbs[1] = ad1981_toshiba_init_verbs;
1686                 spec->multiout.dig_out_nid = 0;
1687                 spec->input_mux = &ad1981_hp_capture_source;
1688                 codec->patch_ops.init = ad1981_hp_init;
1689                 codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
1690                 break;
1691         }
1692         return 0;
1693 }
1694
1695
1696 /*
1697  * AD1988
1698  *
1699  * Output pins and routes
1700  *
1701  *        Pin               Mix     Sel     DAC (*)
1702  * port-A 0x11 (mute/hp) <- 0x22 <- 0x37 <- 03/04/06
1703  * port-B 0x14 (mute/hp) <- 0x2b <- 0x30 <- 03/04/06
1704  * port-C 0x15 (mute)    <- 0x2c <- 0x31 <- 05/0a
1705  * port-D 0x12 (mute/hp) <- 0x29         <- 04
1706  * port-E 0x17 (mute/hp) <- 0x26 <- 0x32 <- 05/0a
1707  * port-F 0x16 (mute)    <- 0x2a         <- 06
1708  * port-G 0x24 (mute)    <- 0x27         <- 05
1709  * port-H 0x25 (mute)    <- 0x28         <- 0a
1710  * mono   0x13 (mute/amp)<- 0x1e <- 0x36 <- 03/04/06
1711  *
1712  * DAC0 = 03h, DAC1 = 04h, DAC2 = 05h, DAC3 = 06h, DAC4 = 0ah
1713  * (*) DAC2/3/4 are swapped to DAC3/4/2 on AD198A rev.2 due to a h/w bug.
1714  *
1715  * Input pins and routes
1716  *
1717  *        pin     boost   mix input # / adc input #
1718  * port-A 0x11 -> 0x38 -> mix 2, ADC 0
1719  * port-B 0x14 -> 0x39 -> mix 0, ADC 1
1720  * port-C 0x15 -> 0x3a -> 33:0 - mix 1, ADC 2
1721  * port-D 0x12 -> 0x3d -> mix 3, ADC 8
1722  * port-E 0x17 -> 0x3c -> 34:0 - mix 4, ADC 4
1723  * port-F 0x16 -> 0x3b -> mix 5, ADC 3
1724  * port-G 0x24 -> N/A  -> 33:1 - mix 1, 34:1 - mix 4, ADC 6
1725  * port-H 0x25 -> N/A  -> 33:2 - mix 1, 34:2 - mix 4, ADC 7
1726  *
1727  *
1728  * DAC assignment
1729  *   6stack - front/surr/CLFE/side/opt DACs - 04/06/05/0a/03
1730  *   3stack - front/surr/CLFE/opt DACs - 04/05/0a/03
1731  *
1732  * Inputs of Analog Mix (0x20)
1733  *   0:Port-B (front mic)
1734  *   1:Port-C/G/H (line-in)
1735  *   2:Port-A
1736  *   3:Port-D (line-in/2)
1737  *   4:Port-E/G/H (mic-in)
1738  *   5:Port-F (mic2-in)
1739  *   6:CD
1740  *   7:Beep
1741  *
1742  * ADC selection
1743  *   0:Port-A
1744  *   1:Port-B (front mic-in)
1745  *   2:Port-C (line-in)
1746  *   3:Port-F (mic2-in)
1747  *   4:Port-E (mic-in)
1748  *   5:CD
1749  *   6:Port-G
1750  *   7:Port-H
1751  *   8:Port-D (line-in/2)
1752  *   9:Mix
1753  *
1754  * Proposed pin assignments by the datasheet
1755  *
1756  * 6-stack
1757  * Port-A front headphone
1758  *      B front mic-in
1759  *      C rear line-in
1760  *      D rear front-out
1761  *      E rear mic-in
1762  *      F rear surround
1763  *      G rear CLFE
1764  *      H rear side
1765  *
1766  * 3-stack
1767  * Port-A front headphone
1768  *      B front mic
1769  *      C rear line-in/surround
1770  *      D rear front-out
1771  *      E rear mic-in/CLFE
1772  *
1773  * laptop
1774  * Port-A headphone
1775  *      B mic-in
1776  *      C docking station
1777  *      D internal speaker (with EAPD)
1778  *      E/F quad mic array
1779  */
1780
1781
1782 /* models */
1783 enum {
1784         AD1988_6STACK,
1785         AD1988_6STACK_DIG,
1786         AD1988_3STACK,
1787         AD1988_3STACK_DIG,
1788         AD1988_LAPTOP,
1789         AD1988_LAPTOP_DIG,
1790         AD1988_AUTO,
1791         AD1988_MODEL_LAST,
1792 };
1793
1794 /* reivision id to check workarounds */
1795 #define AD1988A_REV2            0x100200
1796
1797 #define is_rev2(codec) \
1798         ((codec)->vendor_id == 0x11d41988 && \
1799          (codec)->revision_id == AD1988A_REV2)
1800
1801 /*
1802  * mixers
1803  */
1804
1805 static hda_nid_t ad1988_6stack_dac_nids[4] = {
1806         0x04, 0x06, 0x05, 0x0a
1807 };
1808
1809 static hda_nid_t ad1988_3stack_dac_nids[3] = {
1810         0x04, 0x05, 0x0a
1811 };
1812
1813 /* for AD1988A revision-2, DAC2-4 are swapped */
1814 static hda_nid_t ad1988_6stack_dac_nids_rev2[4] = {
1815         0x04, 0x05, 0x0a, 0x06
1816 };
1817
1818 static hda_nid_t ad1988_3stack_dac_nids_rev2[3] = {
1819         0x04, 0x0a, 0x06
1820 };
1821
1822 static hda_nid_t ad1988_adc_nids[3] = {
1823         0x08, 0x09, 0x0f
1824 };
1825
1826 static hda_nid_t ad1988_capsrc_nids[3] = {
1827         0x0c, 0x0d, 0x0e
1828 };
1829
1830 #define AD1988_SPDIF_OUT        0x02
1831 #define AD1988_SPDIF_IN         0x07
1832
1833 static struct hda_input_mux ad1988_6stack_capture_source = {
1834         .num_items = 5,
1835         .items = {
1836                 { "Front Mic", 0x1 },   /* port-B */
1837                 { "Line", 0x2 },        /* port-C */
1838                 { "Mic", 0x4 },         /* port-E */
1839                 { "CD", 0x5 },
1840                 { "Mix", 0x9 },
1841         },
1842 };
1843
1844 static struct hda_input_mux ad1988_laptop_capture_source = {
1845         .num_items = 3,
1846         .items = {
1847                 { "Mic/Line", 0x1 },    /* port-B */
1848                 { "CD", 0x5 },
1849                 { "Mix", 0x9 },
1850         },
1851 };
1852
1853 /*
1854  */
1855 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
1856                                struct snd_ctl_elem_info *uinfo)
1857 {
1858         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1859         struct ad198x_spec *spec = codec->spec;
1860         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
1861                                     spec->num_channel_mode);
1862 }
1863
1864 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
1865                               struct snd_ctl_elem_value *ucontrol)
1866 {
1867         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1868         struct ad198x_spec *spec = codec->spec;
1869         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
1870                                    spec->num_channel_mode, spec->multiout.max_channels);
1871 }
1872
1873 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
1874                               struct snd_ctl_elem_value *ucontrol)
1875 {
1876         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1877         struct ad198x_spec *spec = codec->spec;
1878         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
1879                                       spec->num_channel_mode,
1880                                       &spec->multiout.max_channels);
1881         if (err >= 0 && spec->need_dac_fix)
1882                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1883         return err;
1884 }
1885
1886 /* 6-stack mode */
1887 static struct snd_kcontrol_new ad1988_6stack_mixers1[] = {
1888         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1889         HDA_CODEC_VOLUME("Surround Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1890         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
1891         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
1892         HDA_CODEC_VOLUME("Side Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1893         { } /* end */
1894 };
1895
1896 static struct snd_kcontrol_new ad1988_6stack_mixers1_rev2[] = {
1897         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1898         HDA_CODEC_VOLUME("Surround Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1899         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0a, 1, 0x0, HDA_OUTPUT),
1900         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0a, 2, 0x0, HDA_OUTPUT),
1901         HDA_CODEC_VOLUME("Side Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1902         { } /* end */
1903 };
1904
1905 static struct snd_kcontrol_new ad1988_6stack_mixers2[] = {
1906         HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
1907         HDA_BIND_MUTE("Surround Playback Switch", 0x2a, 2, HDA_INPUT),
1908         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x27, 1, 2, HDA_INPUT),
1909         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x27, 2, 2, HDA_INPUT),
1910         HDA_BIND_MUTE("Side Playback Switch", 0x28, 2, HDA_INPUT),
1911         HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
1912         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1913
1914         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1915         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1916         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1917         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1918         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1919         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1920         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
1921         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),
1922
1923         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1924         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1925
1926         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1927         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1928
1929         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1930         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
1931
1932         { } /* end */
1933 };
1934
1935 /* 3-stack mode */
1936 static struct snd_kcontrol_new ad1988_3stack_mixers1[] = {
1937         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1938         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1939         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
1940         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
1941         { } /* end */
1942 };
1943
1944 static struct snd_kcontrol_new ad1988_3stack_mixers1_rev2[] = {
1945         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1946         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1947         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x06, 1, 0x0, HDA_OUTPUT),
1948         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x06, 2, 0x0, HDA_OUTPUT),
1949         { } /* end */
1950 };
1951
1952 static struct snd_kcontrol_new ad1988_3stack_mixers2[] = {
1953         HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
1954         HDA_BIND_MUTE("Surround Playback Switch", 0x2c, 2, HDA_INPUT),
1955         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x26, 1, 2, HDA_INPUT),
1956         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x26, 2, 2, HDA_INPUT),
1957         HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
1958         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1959
1960         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1961         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1962         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1963         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1964         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1965         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1966         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
1967         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),
1968
1969         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1970         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1971
1972         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1973         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1974
1975         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1976         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
1977         {
1978                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1979                 .name = "Channel Mode",
1980                 .info = ad198x_ch_mode_info,
1981                 .get = ad198x_ch_mode_get,
1982                 .put = ad198x_ch_mode_put,
1983         },
1984
1985         { } /* end */
1986 };
1987
1988 /* laptop mode */
1989 static struct snd_kcontrol_new ad1988_laptop_mixers[] = {
1990         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1991         HDA_CODEC_MUTE("PCM Playback Switch", 0x29, 0x0, HDA_INPUT),
1992         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1993
1994         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1995         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1996         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1997         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1998         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1999         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
2000
2001         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
2002         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
2003
2004         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
2005         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
2006
2007         HDA_CODEC_VOLUME("Mic Boost", 0x39, 0x0, HDA_OUTPUT),
2008
2009         {
2010                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2011                 .name = "External Amplifier",
2012                 .info = ad198x_eapd_info,
2013                 .get = ad198x_eapd_get,
2014                 .put = ad198x_eapd_put,
2015                 .private_value = 0x12 | (1 << 8), /* port-D, inversed */
2016         },
2017
2018         { } /* end */
2019 };
2020
2021 /* capture */
2022 static struct snd_kcontrol_new ad1988_capture_mixers[] = {
2023         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
2024         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
2025         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
2026         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
2027         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x0e, 0x0, HDA_OUTPUT),
2028         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x0e, 0x0, HDA_OUTPUT),
2029         {
2030                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2031                 /* The multiple "Capture Source" controls confuse alsamixer
2032                  * So call somewhat different..
2033                  */
2034                 /* .name = "Capture Source", */
2035                 .name = "Input Source",
2036                 .count = 3,
2037                 .info = ad198x_mux_enum_info,
2038                 .get = ad198x_mux_enum_get,
2039                 .put = ad198x_mux_enum_put,
2040         },
2041         { } /* end */
2042 };
2043
2044 static int ad1988_spdif_playback_source_info(struct snd_kcontrol *kcontrol,
2045                                              struct snd_ctl_elem_info *uinfo)
2046 {
2047         static char *texts[] = {
2048                 "PCM", "ADC1", "ADC2", "ADC3"
2049         };
2050         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2051         uinfo->count = 1;
2052         uinfo->value.enumerated.items = 4;
2053         if (uinfo->value.enumerated.item >= 4)
2054                 uinfo->value.enumerated.item = 3;
2055         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2056         return 0;
2057 }
2058
2059 static int ad1988_spdif_playback_source_get(struct snd_kcontrol *kcontrol,
2060                                             struct snd_ctl_elem_value *ucontrol)
2061 {
2062         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2063         unsigned int sel;
2064
2065         sel = snd_hda_codec_read(codec, 0x1d, 0, AC_VERB_GET_AMP_GAIN_MUTE,
2066                                  AC_AMP_GET_INPUT);
2067         if (!(sel & 0x80))
2068                 ucontrol->value.enumerated.item[0] = 0;
2069         else {
2070                 sel = snd_hda_codec_read(codec, 0x0b, 0,
2071                                          AC_VERB_GET_CONNECT_SEL, 0);
2072                 if (sel < 3)
2073                         sel++;
2074                 else
2075                         sel = 0;
2076                 ucontrol->value.enumerated.item[0] = sel;
2077         }
2078         return 0;
2079 }
2080
2081 static int ad1988_spdif_playback_source_put(struct snd_kcontrol *kcontrol,
2082                                             struct snd_ctl_elem_value *ucontrol)
2083 {
2084         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2085         unsigned int val, sel;
2086         int change;
2087
2088         val = ucontrol->value.enumerated.item[0];
2089         if (val > 3)
2090                 return -EINVAL;
2091         if (!val) {
2092                 sel = snd_hda_codec_read(codec, 0x1d, 0,
2093                                          AC_VERB_GET_AMP_GAIN_MUTE,
2094                                          AC_AMP_GET_INPUT);
2095                 change = sel & 0x80;
2096                 if (change) {
2097                         snd_hda_codec_write_cache(codec, 0x1d, 0,
2098                                                   AC_VERB_SET_AMP_GAIN_MUTE,
2099                                                   AMP_IN_UNMUTE(0));
2100                         snd_hda_codec_write_cache(codec, 0x1d, 0,
2101                                                   AC_VERB_SET_AMP_GAIN_MUTE,
2102                                                   AMP_IN_MUTE(1));
2103                 }
2104         } else {
2105                 sel = snd_hda_codec_read(codec, 0x1d, 0,
2106                                          AC_VERB_GET_AMP_GAIN_MUTE,
2107                                          AC_AMP_GET_INPUT | 0x01);
2108                 change = sel & 0x80;
2109                 if (change) {
2110                         snd_hda_codec_write_cache(codec, 0x1d, 0,
2111                                                   AC_VERB_SET_AMP_GAIN_MUTE,
2112                                                   AMP_IN_MUTE(0));
2113                         snd_hda_codec_write_cache(codec, 0x1d, 0,
2114                                                   AC_VERB_SET_AMP_GAIN_MUTE,
2115                                                   AMP_IN_UNMUTE(1));
2116                 }
2117                 sel = snd_hda_codec_read(codec, 0x0b, 0,
2118                                          AC_VERB_GET_CONNECT_SEL, 0) + 1;
2119                 change |= sel != val;
2120                 if (change)
2121                         snd_hda_codec_write_cache(codec, 0x0b, 0,
2122                                                   AC_VERB_SET_CONNECT_SEL,
2123                                                   val - 1);
2124         }
2125         return change;
2126 }
2127
2128 static struct snd_kcontrol_new ad1988_spdif_out_mixers[] = {
2129         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
2130         {
2131                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2132                 .name = "IEC958 Playback Source",
2133                 .info = ad1988_spdif_playback_source_info,
2134                 .get = ad1988_spdif_playback_source_get,
2135                 .put = ad1988_spdif_playback_source_put,
2136         },
2137         { } /* end */
2138 };
2139
2140 static struct snd_kcontrol_new ad1988_spdif_in_mixers[] = {
2141         HDA_CODEC_VOLUME("IEC958 Capture Volume", 0x1c, 0x0, HDA_INPUT),
2142         { } /* end */
2143 };
2144
2145 static struct snd_kcontrol_new ad1989_spdif_out_mixers[] = {
2146         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
2147         { } /* end */
2148 };
2149
2150 /*
2151  * initialization verbs
2152  */
2153
2154 /*
2155  * for 6-stack (+dig)
2156  */
2157 static struct hda_verb ad1988_6stack_init_verbs[] = {
2158         /* Front, Surround, CLFE, side DAC; unmute as default */
2159         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2160         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2161         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2162         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2163         /* Port-A front headphon path */
2164         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
2165         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2166         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2167         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2168         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2169         /* Port-D line-out path */
2170         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2171         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2172         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2173         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2174         /* Port-F surround path */
2175         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2176         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2177         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2178         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2179         /* Port-G CLFE path */
2180         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2181         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2182         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2183         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2184         /* Port-H side path */
2185         {0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2186         {0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2187         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2188         {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2189         /* Mono out path */
2190         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2191         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2192         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2193         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2194         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2195         /* Port-B front mic-in path */
2196         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2197         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2198         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2199         /* Port-C line-in path */
2200         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2201         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2202         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2203         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2204         /* Port-E mic-in path */
2205         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2206         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2207         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2208         {0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
2209         /* Analog CD Input */
2210         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2211
2212         { }
2213 };
2214
2215 static struct hda_verb ad1988_capture_init_verbs[] = {
2216         /* mute analog mix */
2217         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2218         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2219         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2220         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2221         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2222         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2223         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2224         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2225         /* select ADCs - front-mic */
2226         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2227         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2228         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2229         /* ADCs; muted */
2230         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2231         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2232         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2233
2234         { }
2235 };
2236
2237 static struct hda_verb ad1988_spdif_init_verbs[] = {
2238         /* SPDIF out sel */
2239         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
2240         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x0}, /* ADC1 */
2241         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2242         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2243         /* SPDIF out pin */
2244         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
2245
2246         { }
2247 };
2248
2249 /* AD1989 has no ADC -> SPDIF route */
2250 static struct hda_verb ad1989_spdif_init_verbs[] = {
2251         /* SPDIF out pin */
2252         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
2253         { }
2254 };
2255
2256 /*
2257  * verbs for 3stack (+dig)
2258  */
2259 static struct hda_verb ad1988_3stack_ch2_init[] = {
2260         /* set port-C to line-in */
2261         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2262         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
2263         /* set port-E to mic-in */
2264         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2265         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
2266         { } /* end */
2267 };
2268
2269 static struct hda_verb ad1988_3stack_ch6_init[] = {
2270         /* set port-C to surround out */
2271         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2272         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2273         /* set port-E to CLFE out */
2274         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2275         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2276         { } /* end */
2277 };
2278
2279 static struct hda_channel_mode ad1988_3stack_modes[2] = {
2280         { 2, ad1988_3stack_ch2_init },
2281         { 6, ad1988_3stack_ch6_init },
2282 };
2283
2284 static struct hda_verb ad1988_3stack_init_verbs[] = {
2285         /* Front, Surround, CLFE, side DAC; unmute as default */
2286         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2287         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2288         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2289         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2290         /* Port-A front headphon path */
2291         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
2292         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2293         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2294         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2295         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2296         /* Port-D line-out path */
2297         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2298         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2299         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2300         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2301         /* Mono out path */
2302         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2303         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2304         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2305         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2306         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2307         /* Port-B front mic-in path */
2308         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2309         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2310         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2311         /* Port-C line-in/surround path - 6ch mode as default */
2312         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2313         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2314         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2315         {0x31, AC_VERB_SET_CONNECT_SEL, 0x0}, /* output sel: DAC 0x05 */
2316         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2317         /* Port-E mic-in/CLFE path - 6ch mode as default */
2318         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2319         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2320         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2321         {0x32, AC_VERB_SET_CONNECT_SEL, 0x1}, /* output sel: DAC 0x0a */
2322         {0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
2323         /* mute analog mix */
2324         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2325         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2326         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2327         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2328         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2329         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2330         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2331         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2332         /* select ADCs - front-mic */
2333         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2334         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2335         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2336         /* ADCs; muted */
2337         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2338         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2339         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2340         { }
2341 };
2342
2343 /*
2344  * verbs for laptop mode (+dig)
2345  */
2346 static struct hda_verb ad1988_laptop_hp_on[] = {
2347         /* unmute port-A and mute port-D */
2348         { 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2349         { 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2350         { } /* end */
2351 };
2352 static struct hda_verb ad1988_laptop_hp_off[] = {
2353         /* mute port-A and unmute port-D */
2354         { 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2355         { 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2356         { } /* end */
2357 };
2358
2359 #define AD1988_HP_EVENT 0x01
2360
2361 static struct hda_verb ad1988_laptop_init_verbs[] = {
2362         /* Front, Surround, CLFE, side DAC; unmute as default */
2363         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2364         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2365         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2366         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2367         /* Port-A front headphon path */
2368         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
2369         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2370         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2371         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2372         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2373         /* unsolicited event for pin-sense */
2374         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1988_HP_EVENT },
2375         /* Port-D line-out path + EAPD */
2376         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2377         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2378         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2379         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2380         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x00}, /* EAPD-off */
2381         /* Mono out path */
2382         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2383         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2384         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2385         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2386         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2387         /* Port-B mic-in path */
2388         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2389         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2390         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2391         /* Port-C docking station - try to output */
2392         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2393         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2394         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2395         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2396         /* mute analog mix */
2397         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2398         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2399         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2400         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2401         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2402         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2403         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2404         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2405         /* select ADCs - mic */
2406         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2407         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2408         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2409         /* ADCs; muted */
2410         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2411         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2412         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2413         { }
2414 };
2415
2416 static void ad1988_laptop_unsol_event(struct hda_codec *codec, unsigned int res)
2417 {
2418         if ((res >> 26) != AD1988_HP_EVENT)
2419                 return;
2420         if (snd_hda_codec_read(codec, 0x11, 0, AC_VERB_GET_PIN_SENSE, 0) & (1 << 31))
2421                 snd_hda_sequence_write(codec, ad1988_laptop_hp_on);
2422         else
2423                 snd_hda_sequence_write(codec, ad1988_laptop_hp_off);
2424
2425
2426 #ifdef CONFIG_SND_HDA_POWER_SAVE
2427 static struct hda_amp_list ad1988_loopbacks[] = {
2428         { 0x20, HDA_INPUT, 0 }, /* Front Mic */
2429         { 0x20, HDA_INPUT, 1 }, /* Line */
2430         { 0x20, HDA_INPUT, 4 }, /* Mic */
2431         { 0x20, HDA_INPUT, 6 }, /* CD */
2432         { } /* end */
2433 };
2434 #endif
2435
2436 /*
2437  * Automatic parse of I/O pins from the BIOS configuration
2438  */
2439
2440 #define NUM_CONTROL_ALLOC       32
2441 #define NUM_VERB_ALLOC          32
2442
2443 enum {
2444         AD_CTL_WIDGET_VOL,
2445         AD_CTL_WIDGET_MUTE,
2446         AD_CTL_BIND_MUTE,
2447 };
2448 static struct snd_kcontrol_new ad1988_control_templates[] = {
2449         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2450         HDA_CODEC_MUTE(NULL, 0, 0, 0),
2451         HDA_BIND_MUTE(NULL, 0, 0, 0),
2452 };
2453
2454 /* add dynamic controls */
2455 static int add_control(struct ad198x_spec *spec, int type, const char *name,
2456                        unsigned long val)
2457 {
2458         struct snd_kcontrol_new *knew;
2459
2460         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
2461                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
2462
2463                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
2464                 if (! knew)
2465                         return -ENOMEM;
2466                 if (spec->kctl_alloc) {
2467                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
2468                         kfree(spec->kctl_alloc);
2469                 }
2470                 spec->kctl_alloc = knew;
2471                 spec->num_kctl_alloc = num;
2472         }
2473
2474         knew = &spec->kctl_alloc[spec->num_kctl_used];
2475         *knew = ad1988_control_templates[type];
2476         knew->name = kstrdup(name, GFP_KERNEL);
2477         if (! knew->name)
2478                 return -ENOMEM;
2479         knew->private_value = val;
2480         spec->num_kctl_used++;
2481         return 0;
2482 }
2483
2484 #define AD1988_PIN_CD_NID               0x18
2485 #define AD1988_PIN_BEEP_NID             0x10
2486
2487 static hda_nid_t ad1988_mixer_nids[8] = {
2488         /* A     B     C     D     E     F     G     H */
2489         0x22, 0x2b, 0x2c, 0x29, 0x26, 0x2a, 0x27, 0x28
2490 };
2491
2492 static inline hda_nid_t ad1988_idx_to_dac(struct hda_codec *codec, int idx)
2493 {
2494         static hda_nid_t idx_to_dac[8] = {
2495                 /* A     B     C     D     E     F     G     H */
2496                 0x04, 0x06, 0x05, 0x04, 0x0a, 0x06, 0x05, 0x0a
2497         };
2498         static hda_nid_t idx_to_dac_rev2[8] = {
2499                 /* A     B     C     D     E     F     G     H */
2500                 0x04, 0x05, 0x0a, 0x04, 0x06, 0x05, 0x0a, 0x06
2501         };
2502         if (is_rev2(codec))
2503                 return idx_to_dac_rev2[idx];
2504         else
2505                 return idx_to_dac[idx];
2506 }
2507
2508 static hda_nid_t ad1988_boost_nids[8] = {
2509         0x38, 0x39, 0x3a, 0x3d, 0x3c, 0x3b, 0, 0
2510 };
2511
2512 static int ad1988_pin_idx(hda_nid_t nid)
2513 {
2514         static hda_nid_t ad1988_io_pins[8] = {
2515                 0x11, 0x14, 0x15, 0x12, 0x17, 0x16, 0x24, 0x25
2516         };
2517         int i;
2518         for (i = 0; i < ARRAY_SIZE(ad1988_io_pins); i++)
2519                 if (ad1988_io_pins[i] == nid)
2520                         return i;
2521         return 0; /* should be -1 */
2522 }
2523
2524 static int ad1988_pin_to_loopback_idx(hda_nid_t nid)
2525 {
2526         static int loopback_idx[8] = {
2527                 2, 0, 1, 3, 4, 5, 1, 4
2528         };
2529         switch (nid) {
2530         case AD1988_PIN_CD_NID:
2531                 return 6;
2532         default:
2533                 return loopback_idx[ad1988_pin_idx(nid)];
2534         }
2535 }
2536
2537 static int ad1988_pin_to_adc_idx(hda_nid_t nid)
2538 {
2539         static int adc_idx[8] = {
2540                 0, 1, 2, 8, 4, 3, 6, 7
2541         };
2542         switch (nid) {
2543         case AD1988_PIN_CD_NID:
2544                 return 5;
2545         default:
2546                 return adc_idx[ad1988_pin_idx(nid)];
2547         }
2548 }
2549
2550 /* fill in the dac_nids table from the parsed pin configuration */
2551 static int ad1988_auto_fill_dac_nids(struct hda_codec *codec,
2552                                      const struct auto_pin_cfg *cfg)
2553 {
2554         struct ad198x_spec *spec = codec->spec;
2555         int i, idx;
2556
2557         spec->multiout.dac_nids = spec->private_dac_nids;
2558
2559         /* check the pins hardwired to audio widget */
2560         for (i = 0; i < cfg->line_outs; i++) {
2561                 idx = ad1988_pin_idx(cfg->line_out_pins[i]);
2562                 spec->multiout.dac_nids[i] = ad1988_idx_to_dac(codec, idx);
2563         }
2564         spec->multiout.num_dacs = cfg->line_outs;
2565         return 0;
2566 }
2567
2568 /* add playback controls from the parsed DAC table */
2569 static int ad1988_auto_create_multi_out_ctls(struct ad198x_spec *spec,
2570                                              const struct auto_pin_cfg *cfg)
2571 {
2572         char name[32];
2573         static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
2574         hda_nid_t nid;
2575         int i, err;
2576
2577         for (i = 0; i < cfg->line_outs; i++) {
2578                 hda_nid_t dac = spec->multiout.dac_nids[i];
2579                 if (! dac)
2580                         continue;
2581                 nid = ad1988_mixer_nids[ad1988_pin_idx(cfg->line_out_pins[i])];
2582                 if (i == 2) {
2583                         /* Center/LFE */
2584                         err = add_control(spec, AD_CTL_WIDGET_VOL,
2585                                           "Center Playback Volume",
2586                                           HDA_COMPOSE_AMP_VAL(dac, 1, 0, HDA_OUTPUT));
2587                         if (err < 0)
2588                                 return err;
2589                         err = add_control(spec, AD_CTL_WIDGET_VOL,
2590                                           "LFE Playback Volume",
2591                                           HDA_COMPOSE_AMP_VAL(dac, 2, 0, HDA_OUTPUT));
2592                         if (err < 0)
2593                                 return err;
2594                         err = add_control(spec, AD_CTL_BIND_MUTE,
2595                                           "Center Playback Switch",
2596                                           HDA_COMPOSE_AMP_VAL(nid, 1, 2, HDA_INPUT));
2597                         if (err < 0)
2598                                 return err;
2599                         err = add_control(spec, AD_CTL_BIND_MUTE,
2600                                           "LFE Playback Switch",
2601                                           HDA_COMPOSE_AMP_VAL(nid, 2, 2, HDA_INPUT));
2602                         if (err < 0)
2603                                 return err;
2604                 } else {
2605                         sprintf(name, "%s Playback Volume", chname[i]);
2606                         err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2607                                           HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT));
2608                         if (err < 0)
2609                                 return err;
2610                         sprintf(name, "%s Playback Switch", chname[i]);
2611                         err = add_control(spec, AD_CTL_BIND_MUTE, name,
2612                                           HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT));
2613                         if (err < 0)
2614                                 return err;
2615                 }
2616         }
2617         return 0;
2618 }
2619
2620 /* add playback controls for speaker and HP outputs */
2621 static int ad1988_auto_create_extra_out(struct hda_codec *codec, hda_nid_t pin,
2622                                         const char *pfx)
2623 {
2624         struct ad198x_spec *spec = codec->spec;
2625         hda_nid_t nid;
2626         int idx, err;
2627         char name[32];
2628
2629         if (! pin)
2630                 return 0;
2631
2632         idx = ad1988_pin_idx(pin);
2633         nid = ad1988_idx_to_dac(codec, idx);
2634         /* specify the DAC as the extra output */
2635         if (! spec->multiout.hp_nid)
2636                 spec->multiout.hp_nid = nid;
2637         else
2638                 spec->multiout.extra_out_nid[0] = nid;
2639         /* control HP volume/switch on the output mixer amp */
2640         sprintf(name, "%s Playback Volume", pfx);
2641         if ((err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2642                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
2643                 return err;
2644         nid = ad1988_mixer_nids[idx];
2645         sprintf(name, "%s Playback Switch", pfx);
2646         if ((err = add_control(spec, AD_CTL_BIND_MUTE, name,
2647                                HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT))) < 0)
2648                 return err;
2649         return 0;
2650 }
2651
2652 /* create input playback/capture controls for the given pin */
2653 static int new_analog_input(struct ad198x_spec *spec, hda_nid_t pin,
2654                             const char *ctlname, int boost)
2655 {
2656         char name[32];
2657         int err, idx;
2658
2659         sprintf(name, "%s Playback Volume", ctlname);
2660         idx = ad1988_pin_to_loopback_idx(pin);
2661         if ((err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2662                                HDA_COMPOSE_AMP_VAL(0x20, 3, idx, HDA_INPUT))) < 0)
2663                 return err;
2664         sprintf(name, "%s Playback Switch", ctlname);
2665         if ((err = add_control(spec, AD_CTL_WIDGET_MUTE, name,
2666                                HDA_COMPOSE_AMP_VAL(0x20, 3, idx, HDA_INPUT))) < 0)
2667                 return err;
2668         if (boost) {
2669                 hda_nid_t bnid;
2670                 idx = ad1988_pin_idx(pin);
2671                 bnid = ad1988_boost_nids[idx];
2672                 if (bnid) {
2673                         sprintf(name, "%s Boost", ctlname);
2674                         return add_control(spec, AD_CTL_WIDGET_VOL, name,
2675                                            HDA_COMPOSE_AMP_VAL(bnid, 3, idx, HDA_OUTPUT));
2676
2677                 }
2678         }
2679         return 0;
2680 }
2681
2682 /* create playback/capture controls for input pins */
2683 static int ad1988_auto_create_analog_input_ctls(struct ad198x_spec *spec,
2684                                                 const struct auto_pin_cfg *cfg)
2685 {
2686         struct hda_input_mux *imux = &spec->private_imux;
2687         int i, err;
2688
2689         for (i = 0; i < AUTO_PIN_LAST; i++) {
2690                 err = new_analog_input(spec, cfg->input_pins[i],
2691                                        auto_pin_cfg_labels[i],
2692                                        i <= AUTO_PIN_FRONT_MIC);
2693                 if (err < 0)
2694                         return err;
2695                 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
2696                 imux->items[imux->num_items].index = ad1988_pin_to_adc_idx(cfg->input_pins[i]);
2697                 imux->num_items++;
2698         }
2699         imux->items[imux->num_items].label = "Mix";
2700         imux->items[imux->num_items].index = 9;
2701         imux->num_items++;
2702
2703         if ((err = add_control(spec, AD_CTL_WIDGET_VOL,
2704                                "Analog Mix Playback Volume",
2705                                HDA_COMPOSE_AMP_VAL(0x21, 3, 0x0, HDA_OUTPUT))) < 0)
2706                 return err;
2707         if ((err = add_control(spec, AD_CTL_WIDGET_MUTE,
2708                                "Analog Mix Playback Switch",
2709                                HDA_COMPOSE_AMP_VAL(0x21, 3, 0x0, HDA_OUTPUT))) < 0)
2710                 return err;
2711
2712         return 0;
2713 }
2714
2715 static void ad1988_auto_set_output_and_unmute(struct hda_codec *codec,
2716                                               hda_nid_t nid, int pin_type,
2717                                               int dac_idx)
2718 {
2719         /* set as output */
2720         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
2721         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
2722         switch (nid) {
2723         case 0x11: /* port-A - DAC 04 */
2724                 snd_hda_codec_write(codec, 0x37, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2725                 break;
2726         case 0x14: /* port-B - DAC 06 */
2727                 snd_hda_codec_write(codec, 0x30, 0, AC_VERB_SET_CONNECT_SEL, 0x02);
2728                 break;
2729         case 0x15: /* port-C - DAC 05 */
2730                 snd_hda_codec_write(codec, 0x31, 0, AC_VERB_SET_CONNECT_SEL, 0x00);
2731                 break;
2732         case 0x17: /* port-E - DAC 0a */
2733                 snd_hda_codec_write(codec, 0x32, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2734                 break;
2735         case 0x13: /* mono - DAC 04 */
2736                 snd_hda_codec_write(codec, 0x36, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2737                 break;
2738         }
2739 }
2740
2741 static void ad1988_auto_init_multi_out(struct hda_codec *codec)
2742 {
2743         struct ad198x_spec *spec = codec->spec;
2744         int i;
2745
2746         for (i = 0; i < spec->autocfg.line_outs; i++) {
2747                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
2748                 ad1988_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
2749         }
2750 }
2751
2752 static void ad1988_auto_init_extra_out(struct hda_codec *codec)
2753 {
2754         struct ad198x_spec *spec = codec->spec;
2755         hda_nid_t pin;
2756
2757         pin = spec->autocfg.speaker_pins[0];
2758         if (pin) /* connect to front */
2759                 ad1988_auto_set_output_and_unmute(codec, pin, PIN_OUT, 0);
2760         pin = spec->autocfg.hp_pins[0];
2761         if (pin) /* connect to front */
2762                 ad1988_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
2763 }
2764
2765 static void ad1988_auto_init_analog_input(struct hda_codec *codec)
2766 {
2767         struct ad198x_spec *spec = codec->spec;
2768         int i, idx;
2769
2770         for (i = 0; i < AUTO_PIN_LAST; i++) {
2771                 hda_nid_t nid = spec->autocfg.input_pins[i];
2772                 if (! nid)
2773                         continue;
2774                 switch (nid) {
2775                 case 0x15: /* port-C */
2776                         snd_hda_codec_write(codec, 0x33, 0, AC_VERB_SET_CONNECT_SEL, 0x0);
2777                         break;
2778                 case 0x17: /* port-E */
2779                         snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_CONNECT_SEL, 0x0);
2780                         break;
2781                 }
2782                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2783                                     i <= AUTO_PIN_FRONT_MIC ? PIN_VREF80 : PIN_IN);
2784                 if (nid != AD1988_PIN_CD_NID)
2785                         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2786                                             AMP_OUT_MUTE);
2787                 idx = ad1988_pin_idx(nid);
2788                 if (ad1988_boost_nids[idx])
2789                         snd_hda_codec_write(codec, ad1988_boost_nids[idx], 0,
2790                                             AC_VERB_SET_AMP_GAIN_MUTE,
2791                                             AMP_OUT_ZERO);
2792         }
2793 }
2794
2795 /* parse the BIOS configuration and set up the alc_spec */
2796 /* return 1 if successful, 0 if the proper config is not found, or a negative error code */
2797 static int ad1988_parse_auto_config(struct hda_codec *codec)
2798 {
2799         struct ad198x_spec *spec = codec->spec;
2800         int err;
2801
2802         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
2803                 return err;
2804         if ((err = ad1988_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
2805                 return err;
2806         if (! spec->autocfg.line_outs)
2807                 return 0; /* can't find valid BIOS pin config */
2808         if ((err = ad1988_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
2809             (err = ad1988_auto_create_extra_out(codec,
2810                                                 spec->autocfg.speaker_pins[0],
2811                                                 "Speaker")) < 0 ||
2812             (err = ad1988_auto_create_extra_out(codec, spec->autocfg.hp_pins[0],
2813                                                 "Headphone")) < 0 ||
2814             (err = ad1988_auto_create_analog_input_ctls(spec, &spec->autocfg)) < 0)
2815                 return err;
2816
2817         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2818
2819         if (spec->autocfg.dig_out_pin)
2820                 spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2821         if (spec->autocfg.dig_in_pin)
2822                 spec->dig_in_nid = AD1988_SPDIF_IN;
2823
2824         if (spec->kctl_alloc)
2825                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
2826
2827         spec->init_verbs[spec->num_init_verbs++] = ad1988_6stack_init_verbs;
2828
2829         spec->input_mux = &spec->private_imux;
2830
2831         return 1;
2832 }
2833
2834 /* init callback for auto-configuration model -- overriding the default init */
2835 static int ad1988_auto_init(struct hda_codec *codec)
2836 {
2837         ad198x_init(codec);
2838         ad1988_auto_init_multi_out(codec);
2839         ad1988_auto_init_extra_out(codec);
2840         ad1988_auto_init_analog_input(codec);
2841         return 0;
2842 }
2843
2844
2845 /*
2846  */
2847
2848 static const char *ad1988_models[AD1988_MODEL_LAST] = {
2849         [AD1988_6STACK]         = "6stack",
2850         [AD1988_6STACK_DIG]     = "6stack-dig",
2851         [AD1988_3STACK]         = "3stack",
2852         [AD1988_3STACK_DIG]     = "3stack-dig",
2853         [AD1988_LAPTOP]         = "laptop",
2854         [AD1988_LAPTOP_DIG]     = "laptop-dig",
2855         [AD1988_AUTO]           = "auto",
2856 };
2857
2858 static struct snd_pci_quirk ad1988_cfg_tbl[] = {
2859         SND_PCI_QUIRK(0x1043, 0x81ec, "Asus P5B-DLX", AD1988_6STACK_DIG),
2860         SND_PCI_QUIRK(0x1043, 0x81f6, "Asus M2N-SLI", AD1988_6STACK_DIG),
2861         {}
2862 };
2863
2864 static int patch_ad1988(struct hda_codec *codec)
2865 {
2866         struct ad198x_spec *spec;
2867         int board_config;
2868
2869         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2870         if (spec == NULL)
2871                 return -ENOMEM;
2872
2873         codec->spec = spec;
2874
2875         if (is_rev2(codec))
2876                 snd_printk(KERN_INFO "patch_analog: AD1988A rev.2 is detected, enable workarounds\n");
2877
2878         board_config = snd_hda_check_board_config(codec, AD1988_MODEL_LAST,
2879                                                   ad1988_models, ad1988_cfg_tbl);
2880         if (board_config < 0) {
2881                 printk(KERN_INFO "hda_codec: Unknown model for AD1988, trying auto-probe from BIOS...\n");
2882                 board_config = AD1988_AUTO;
2883         }
2884
2885         if (board_config == AD1988_AUTO) {
2886                 /* automatic parse from the BIOS config */
2887                 int err = ad1988_parse_auto_config(codec);
2888                 if (err < 0) {
2889                         ad198x_free(codec);
2890                         return err;
2891                 } else if (! err) {
2892                         printk(KERN_INFO "hda_codec: Cannot set up configuration from BIOS.  Using 6-stack mode...\n");
2893                         board_config = AD1988_6STACK;
2894                 }
2895         }
2896
2897         switch (board_config) {
2898         case AD1988_6STACK:
2899         case AD1988_6STACK_DIG:
2900                 spec->multiout.max_channels = 8;
2901                 spec->multiout.num_dacs = 4;
2902                 if (is_rev2(codec))
2903                         spec->multiout.dac_nids = ad1988_6stack_dac_nids_rev2;
2904                 else
2905                         spec->multiout.dac_nids = ad1988_6stack_dac_nids;
2906                 spec->input_mux = &ad1988_6stack_capture_source;
2907                 spec->num_mixers = 2;
2908                 if (is_rev2(codec))
2909                         spec->mixers[0] = ad1988_6stack_mixers1_rev2;
2910                 else
2911                         spec->mixers[0] = ad1988_6stack_mixers1;
2912                 spec->mixers[1] = ad1988_6stack_mixers2;
2913                 spec->num_init_verbs = 1;
2914                 spec->init_verbs[0] = ad1988_6stack_init_verbs;
2915                 if (board_config == AD1988_6STACK_DIG) {
2916                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2917                         spec->dig_in_nid = AD1988_SPDIF_IN;
2918                 }
2919                 break;
2920         case AD1988_3STACK:
2921         case AD1988_3STACK_DIG:
2922                 spec->multiout.max_channels = 6;
2923                 spec->multiout.num_dacs = 3;
2924                 if (is_rev2(codec))
2925                         spec->multiout.dac_nids = ad1988_3stack_dac_nids_rev2;
2926                 else
2927                         spec->multiout.dac_nids = ad1988_3stack_dac_nids;
2928                 spec->input_mux = &ad1988_6stack_capture_source;
2929                 spec->channel_mode = ad1988_3stack_modes;
2930                 spec->num_channel_mode = ARRAY_SIZE(ad1988_3stack_modes);
2931                 spec->num_mixers = 2;
2932                 if (is_rev2(codec))
2933                         spec->mixers[0] = ad1988_3stack_mixers1_rev2;
2934                 else
2935                         spec->mixers[0] = ad1988_3stack_mixers1;
2936                 spec->mixers[1] = ad1988_3stack_mixers2;
2937                 spec->num_init_verbs = 1;
2938                 spec->init_verbs[0] = ad1988_3stack_init_verbs;
2939                 if (board_config == AD1988_3STACK_DIG)
2940                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2941                 break;
2942         case AD1988_LAPTOP:
2943         case AD1988_LAPTOP_DIG:
2944                 spec->multiout.max_channels = 2;
2945                 spec->multiout.num_dacs = 1;
2946                 spec->multiout.dac_nids = ad1988_3stack_dac_nids;
2947                 spec->input_mux = &ad1988_laptop_capture_source;
2948                 spec->num_mixers = 1;
2949                 spec->mixers[0] = ad1988_laptop_mixers;
2950                 spec->num_init_verbs = 1;
2951                 spec->init_verbs[0] = ad1988_laptop_init_verbs;
2952                 if (board_config == AD1988_LAPTOP_DIG)
2953                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2954                 break;
2955         }
2956
2957         spec->num_adc_nids = ARRAY_SIZE(ad1988_adc_nids);
2958         spec->adc_nids = ad1988_adc_nids;
2959         spec->capsrc_nids = ad1988_capsrc_nids;
2960         spec->mixers[spec->num_mixers++] = ad1988_capture_mixers;
2961         spec->init_verbs[spec->num_init_verbs++] = ad1988_capture_init_verbs;
2962         if (spec->multiout.dig_out_nid) {
2963                 if (codec->vendor_id >= 0x11d4989a) {
2964                         spec->mixers[spec->num_mixers++] =
2965                                 ad1989_spdif_out_mixers;
2966                         spec->init_verbs[spec->num_init_verbs++] =
2967                                 ad1989_spdif_init_verbs;
2968                 } else {
2969                         spec->mixers[spec->num_mixers++] =
2970                                 ad1988_spdif_out_mixers;
2971                         spec->init_verbs[spec->num_init_verbs++] =
2972                                 ad1988_spdif_init_verbs;
2973                 }
2974         }
2975         if (spec->dig_in_nid && codec->vendor_id < 0x11d4989a)
2976                 spec->mixers[spec->num_mixers++] = ad1988_spdif_in_mixers;
2977
2978         codec->patch_ops = ad198x_patch_ops;
2979         switch (board_config) {
2980         case AD1988_AUTO:
2981                 codec->patch_ops.init = ad1988_auto_init;
2982                 break;
2983         case AD1988_LAPTOP:
2984         case AD1988_LAPTOP_DIG:
2985                 codec->patch_ops.unsol_event = ad1988_laptop_unsol_event;
2986                 break;
2987         }
2988 #ifdef CONFIG_SND_HDA_POWER_SAVE
2989         spec->loopback.amplist = ad1988_loopbacks;
2990 #endif
2991         spec->vmaster_nid = 0x04;
2992
2993         return 0;
2994 }
2995
2996
2997 /*
2998  * AD1884 / AD1984
2999  *
3000  * port-B - front line/mic-in
3001  * port-E - aux in/out
3002  * port-F - aux in/out
3003  * port-C - rear line/mic-in
3004  * port-D - rear line/hp-out
3005  * port-A - front line/hp-out
3006  *
3007  * AD1984 = AD1884 + two digital mic-ins
3008  *
3009  * FIXME:
3010  * For simplicity, we share the single DAC for both HP and line-outs
3011  * right now.  The inidividual playbacks could be easily implemented,
3012  * but no build-up framework is given, so far.
3013  */
3014
3015 static hda_nid_t ad1884_dac_nids[1] = {
3016         0x04,
3017 };
3018
3019 static hda_nid_t ad1884_adc_nids[2] = {
3020         0x08, 0x09,
3021 };
3022
3023 static hda_nid_t ad1884_capsrc_nids[2] = {
3024         0x0c, 0x0d,
3025 };
3026
3027 #define AD1884_SPDIF_OUT        0x02
3028
3029 static struct hda_input_mux ad1884_capture_source = {
3030         .num_items = 4,
3031         .items = {
3032                 { "Front Mic", 0x0 },
3033                 { "Mic", 0x1 },
3034                 { "CD", 0x2 },
3035                 { "Mix", 0x3 },
3036         },
3037 };
3038
3039 static struct snd_kcontrol_new ad1884_base_mixers[] = {
3040         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
3041         /* HDA_CODEC_VOLUME_IDX("PCM Playback Volume", 1, 0x03, 0x0, HDA_OUTPUT), */
3042         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
3043         HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3044         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
3045         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
3046         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3047         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3048         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
3049         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
3050         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x02, HDA_INPUT),
3051         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x02, HDA_INPUT),
3052         /*
3053         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x20, 0x03, HDA_INPUT),
3054         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x20, 0x03, HDA_INPUT),
3055         HDA_CODEC_VOLUME("Digital Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
3056         HDA_CODEC_MUTE("Digital Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
3057         */
3058         HDA_CODEC_VOLUME("Mic Boost", 0x15, 0x0, HDA_INPUT),
3059         HDA_CODEC_VOLUME("Front Mic Boost", 0x14, 0x0, HDA_INPUT),
3060         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3061         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3062         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3063         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3064         {
3065                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3066                 /* The multiple "Capture Source" controls confuse alsamixer
3067                  * So call somewhat different..
3068                  */
3069                 /* .name = "Capture Source", */
3070                 .name = "Input Source",
3071                 .count = 2,
3072                 .info = ad198x_mux_enum_info,
3073                 .get = ad198x_mux_enum_get,
3074                 .put = ad198x_mux_enum_put,
3075         },
3076         /* SPDIF controls */
3077         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
3078         {
3079                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3080                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
3081                 /* identical with ad1983 */
3082                 .info = ad1983_spdif_route_info,
3083                 .get = ad1983_spdif_route_get,
3084                 .put = ad1983_spdif_route_put,
3085         },
3086         { } /* end */
3087 };
3088
3089 static struct snd_kcontrol_new ad1984_dmic_mixers[] = {
3090         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x05, 0x0, HDA_INPUT),
3091         HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x05, 0x0, HDA_INPUT),
3092         HDA_CODEC_VOLUME_IDX("Digital Mic Capture Volume", 1, 0x06, 0x0,
3093                              HDA_INPUT),
3094         HDA_CODEC_MUTE_IDX("Digital Mic Capture Switch", 1, 0x06, 0x0,
3095                            HDA_INPUT),
3096         { } /* end */
3097 };
3098
3099 /*
3100  * initialization verbs
3101  */
3102 static struct hda_verb ad1884_init_verbs[] = {
3103         /* DACs; mute as default */
3104         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3105         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3106         /* Port-A (HP) mixer */
3107         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3108         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3109         /* Port-A pin */
3110         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3111         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3112         /* HP selector - select DAC2 */
3113         {0x22, AC_VERB_SET_CONNECT_SEL, 0x1},
3114         /* Port-D (Line-out) mixer */
3115         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3116         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3117         /* Port-D pin */
3118         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3119         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3120         /* Mono-out mixer */
3121         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3122         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3123         /* Mono-out pin */
3124         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3125         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3126         /* Mono selector */
3127         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
3128         /* Port-B (front mic) pin */
3129         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3130         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3131         /* Port-C (rear mic) pin */
3132         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3133         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3134         /* Analog mixer; mute as default */
3135         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3136         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3137         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3138         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3139         /* Analog Mix output amp */
3140         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
3141         /* SPDIF output selector */
3142         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
3143         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
3144         { } /* end */
3145 };
3146
3147 #ifdef CONFIG_SND_HDA_POWER_SAVE
3148 static struct hda_amp_list ad1884_loopbacks[] = {
3149         { 0x20, HDA_INPUT, 0 }, /* Front Mic */
3150         { 0x20, HDA_INPUT, 1 }, /* Mic */
3151         { 0x20, HDA_INPUT, 2 }, /* CD */
3152         { 0x20, HDA_INPUT, 4 }, /* Docking */
3153         { } /* end */
3154 };
3155 #endif
3156
3157 static const char *ad1884_slave_vols[] = {
3158         "PCM Playback Volume",
3159         "Mic Playback Volume",
3160         "Mono Playback Volume",
3161         "Front Mic Playback Volume",
3162         "Mic Playback Volume",
3163         "CD Playback Volume",
3164         "Internal Mic Playback Volume",
3165         "Docking Mic Playback Volume"
3166         "Beep Playback Volume",
3167         "IEC958 Playback Volume",
3168         NULL
3169 };
3170
3171 static int patch_ad1884(struct hda_codec *codec)
3172 {
3173         struct ad198x_spec *spec;
3174
3175         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3176         if (spec == NULL)
3177                 return -ENOMEM;
3178
3179         mutex_init(&spec->amp_mutex);
3180         codec->spec = spec;
3181
3182         spec->multiout.max_channels = 2;
3183         spec->multiout.num_dacs = ARRAY_SIZE(ad1884_dac_nids);
3184         spec->multiout.dac_nids = ad1884_dac_nids;
3185         spec->multiout.dig_out_nid = AD1884_SPDIF_OUT;
3186         spec->num_adc_nids = ARRAY_SIZE(ad1884_adc_nids);
3187         spec->adc_nids = ad1884_adc_nids;
3188         spec->capsrc_nids = ad1884_capsrc_nids;
3189         spec->input_mux = &ad1884_capture_source;
3190         spec->num_mixers = 1;
3191         spec->mixers[0] = ad1884_base_mixers;
3192         spec->num_init_verbs = 1;
3193         spec->init_verbs[0] = ad1884_init_verbs;
3194         spec->spdif_route = 0;
3195 #ifdef CONFIG_SND_HDA_POWER_SAVE
3196         spec->loopback.amplist = ad1884_loopbacks;
3197 #endif
3198         spec->vmaster_nid = 0x04;
3199         /* we need to cover all playback volumes */
3200         spec->slave_vols = ad1884_slave_vols;
3201
3202         codec->patch_ops = ad198x_patch_ops;
3203
3204         return 0;
3205 }
3206
3207 /*
3208  * Lenovo Thinkpad T61/X61
3209  */
3210 static struct hda_input_mux ad1984_thinkpad_capture_source = {
3211         .num_items = 4,
3212         .items = {
3213                 { "Mic", 0x0 },
3214                 { "Internal Mic", 0x1 },
3215                 { "Mix", 0x3 },
3216                 { "Docking-Station", 0x4 },
3217         },
3218 };
3219
3220
3221 /*
3222  * Dell Precision T3400
3223  */
3224 static struct hda_input_mux ad1984_dell_desktop_capture_source = {
3225         .num_items = 3,
3226         .items = {
3227                 { "Front Mic", 0x0 },
3228                 { "Line-In", 0x1 },
3229                 { "Mix", 0x3 },
3230         },
3231 };
3232
3233
3234 static struct snd_kcontrol_new ad1984_thinkpad_mixers[] = {
3235         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
3236         /* HDA_CODEC_VOLUME_IDX("PCM Playback Volume", 1, 0x03, 0x0, HDA_OUTPUT), */
3237         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
3238         HDA_CODEC_MUTE("Speaker Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3239         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3240         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3241         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
3242         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
3243         HDA_CODEC_VOLUME("Docking Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
3244         HDA_CODEC_MUTE("Docking Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
3245         HDA_CODEC_VOLUME("Mic Boost", 0x14, 0x0, HDA_INPUT),
3246         HDA_CODEC_VOLUME("Internal Mic Boost", 0x15, 0x0, HDA_INPUT),
3247         HDA_CODEC_VOLUME("Docking Mic Boost", 0x25, 0x0, HDA_OUTPUT),
3248         HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x03, HDA_INPUT),
3249         HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x03, HDA_INPUT),
3250         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3251         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3252         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3253         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3254         {
3255                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3256                 /* The multiple "Capture Source" controls confuse alsamixer
3257                  * So call somewhat different..
3258                  */
3259                 /* .name = "Capture Source", */
3260                 .name = "Input Source",
3261                 .count = 2,
3262                 .info = ad198x_mux_enum_info,
3263                 .get = ad198x_mux_enum_get,
3264                 .put = ad198x_mux_enum_put,
3265         },
3266         /* SPDIF controls */
3267         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
3268         {
3269                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3270                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
3271                 /* identical with ad1983 */
3272                 .info = ad1983_spdif_route_info,
3273                 .get = ad1983_spdif_route_get,
3274                 .put = ad1983_spdif_route_put,
3275         },
3276         { } /* end */
3277 };
3278
3279 /* additional verbs */
3280 static struct hda_verb ad1984_thinkpad_init_verbs[] = {
3281         /* Port-E (docking station mic) pin */
3282         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3283         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3284         /* docking mic boost */
3285         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3286         /* Analog mixer - docking mic; mute as default */
3287         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3288         /* enable EAPD bit */
3289         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x02},
3290         { } /* end */
3291 };
3292
3293 /*
3294  * Dell Precision T3400
3295  */
3296 static struct snd_kcontrol_new ad1984_dell_desktop_mixers[] = {
3297         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
3298         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
3299         HDA_CODEC_MUTE("Speaker Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3300         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
3301         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
3302         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3303         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3304         HDA_CODEC_VOLUME("Line-In Playback Volume", 0x20, 0x01, HDA_INPUT),
3305         HDA_CODEC_MUTE("Line-In Playback Switch", 0x20, 0x01, HDA_INPUT),
3306         /*
3307         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x20, 0x03, HDA_INPUT),
3308         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x20, 0x03, HDA_INPUT),
3309         */
3310         HDA_CODEC_VOLUME("Line-In Boost", 0x15, 0x0, HDA_INPUT),
3311         HDA_CODEC_VOLUME("Front Mic Boost", 0x14, 0x0, HDA_INPUT),
3312         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3313         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3314         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3315         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3316         {
3317                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3318                 /* The multiple "Capture Source" controls confuse alsamixer
3319                  * So call somewhat different..
3320                  */
3321                 /* .name = "Capture Source", */
3322                 .name = "Input Source",
3323                 .count = 2,
3324                 .info = ad198x_mux_enum_info,
3325                 .get = ad198x_mux_enum_get,
3326                 .put = ad198x_mux_enum_put,
3327         },
3328         { } /* end */
3329 };
3330
3331 /* Digial MIC ADC NID 0x05 + 0x06 */
3332 static int ad1984_pcm_dmic_prepare(struct hda_pcm_stream *hinfo,
3333                                    struct hda_codec *codec,
3334                                    unsigned int stream_tag,
3335                                    unsigned int format,
3336                                    struct snd_pcm_substream *substream)
3337 {
3338         snd_hda_codec_setup_stream(codec, 0x05 + substream->number,
3339                                    stream_tag, 0, format);
3340         return 0;
3341 }
3342
3343 static int ad1984_pcm_dmic_cleanup(struct hda_pcm_stream *hinfo,
3344                                    struct hda_codec *codec,
3345                                    struct snd_pcm_substream *substream)
3346 {
3347         snd_hda_codec_cleanup_stream(codec, 0x05 + substream->number);
3348         return 0;
3349 }
3350
3351 static struct hda_pcm_stream ad1984_pcm_dmic_capture = {
3352         .substreams = 2,
3353         .channels_min = 2,
3354         .channels_max = 2,
3355         .nid = 0x05,
3356         .ops = {
3357                 .prepare = ad1984_pcm_dmic_prepare,
3358                 .cleanup = ad1984_pcm_dmic_cleanup
3359         },
3360 };
3361
3362 static int ad1984_build_pcms(struct hda_codec *codec)
3363 {
3364         struct ad198x_spec *spec = codec->spec;
3365         struct hda_pcm *info;
3366         int err;
3367
3368         err = ad198x_build_pcms(codec);
3369         if (err < 0)
3370                 return err;
3371
3372         info = spec->pcm_rec + codec->num_pcms;
3373         codec->num_pcms++;
3374         info->name = "AD1984 Digital Mic";
3375         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad1984_pcm_dmic_capture;
3376         return 0;
3377 }
3378
3379 /* models */
3380 enum {
3381         AD1984_BASIC,
3382         AD1984_THINKPAD,
3383         AD1984_DELL_DESKTOP,
3384         AD1984_MODELS
3385 };
3386
3387 static const char *ad1984_models[AD1984_MODELS] = {
3388         [AD1984_BASIC]          = "basic",
3389         [AD1984_THINKPAD]       = "thinkpad",
3390         [AD1984_DELL_DESKTOP]   = "dell_desktop",
3391 };
3392
3393 static struct snd_pci_quirk ad1984_cfg_tbl[] = {
3394         /* Lenovo Thinkpad T61/X61 */
3395         SND_PCI_QUIRK(0x17aa, 0, "Lenovo Thinkpad", AD1984_THINKPAD),
3396         SND_PCI_QUIRK(0x1028, 0x0214, "Dell T3400", AD1984_DELL_DESKTOP),
3397         {}
3398 };
3399
3400 static int patch_ad1984(struct hda_codec *codec)
3401 {
3402         struct ad198x_spec *spec;
3403         int board_config, err;
3404
3405         err = patch_ad1884(codec);
3406         if (err < 0)
3407                 return err;
3408         spec = codec->spec;
3409         board_config = snd_hda_check_board_config(codec, AD1984_MODELS,
3410                                                   ad1984_models, ad1984_cfg_tbl);
3411         switch (board_config) {
3412         case AD1984_BASIC:
3413                 /* additional digital mics */
3414                 spec->mixers[spec->num_mixers++] = ad1984_dmic_mixers;
3415                 codec->patch_ops.build_pcms = ad1984_build_pcms;
3416                 break;
3417         case AD1984_THINKPAD:
3418                 spec->multiout.dig_out_nid = AD1884_SPDIF_OUT;
3419                 spec->input_mux = &ad1984_thinkpad_capture_source;
3420                 spec->mixers[0] = ad1984_thinkpad_mixers;
3421                 spec->init_verbs[spec->num_init_verbs++] = ad1984_thinkpad_init_verbs;
3422                 break;
3423         case AD1984_DELL_DESKTOP:
3424                 spec->multiout.dig_out_nid = 0;
3425                 spec->input_mux = &ad1984_dell_desktop_capture_source;
3426                 spec->mixers[0] = ad1984_dell_desktop_mixers;
3427                 break;
3428         }
3429         return 0;
3430 }
3431
3432
3433 /*
3434  * AD1883 / AD1884A / AD1984A / AD1984B
3435  *
3436  * port-B (0x14) - front mic-in
3437  * port-E (0x1c) - rear mic-in
3438  * port-F (0x16) - CD / ext out
3439  * port-C (0x15) - rear line-in
3440  * port-D (0x12) - rear line-out
3441  * port-A (0x11) - front hp-out
3442  *
3443  * AD1984A = AD1884A + digital-mic
3444  * AD1883 = equivalent with AD1984A
3445  * AD1984B = AD1984A + extra SPDIF-out
3446  *
3447  * FIXME:
3448  * We share the single DAC for both HP and line-outs (see AD1884/1984).
3449  */
3450
3451 static hda_nid_t ad1884a_dac_nids[1] = {
3452         0x03,
3453 };
3454
3455 #define ad1884a_adc_nids        ad1884_adc_nids
3456 #define ad1884a_capsrc_nids     ad1884_capsrc_nids
3457
3458 #define AD1884A_SPDIF_OUT       0x02
3459
3460 static struct hda_input_mux ad1884a_capture_source = {
3461         .num_items = 5,
3462         .items = {
3463                 { "Front Mic", 0x0 },
3464                 { "Mic", 0x4 },
3465                 { "Line", 0x1 },
3466                 { "CD", 0x2 },
3467                 { "Mix", 0x3 },
3468         },
3469 };
3470
3471 static struct snd_kcontrol_new ad1884a_base_mixers[] = {
3472         HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
3473         HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),
3474         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
3475         HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3476         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
3477         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
3478         HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
3479         HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
3480         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3481         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3482         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x01, HDA_INPUT),
3483         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x01, HDA_INPUT),
3484         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
3485         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
3486         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x02, HDA_INPUT),
3487         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x02, HDA_INPUT),
3488         HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x03, HDA_INPUT),
3489         HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x03, HDA_INPUT),
3490         HDA_CODEC_VOLUME("Front Mic Boost", 0x14, 0x0, HDA_INPUT),
3491         HDA_CODEC_VOLUME("Line Boost", 0x15, 0x0, HDA_INPUT),
3492         HDA_CODEC_VOLUME("Mic Boost", 0x25, 0x0, HDA_OUTPUT),
3493         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3494         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3495         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3496         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3497         {
3498                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3499                 /* The multiple "Capture Source" controls confuse alsamixer
3500                  * So call somewhat different..
3501                  */
3502                 /* .name = "Capture Source", */
3503                 .name = "Input Source",
3504                 .count = 2,
3505                 .info = ad198x_mux_enum_info,
3506                 .get = ad198x_mux_enum_get,
3507                 .put = ad198x_mux_enum_put,
3508         },
3509         /* SPDIF controls */
3510         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
3511         {
3512                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3513                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
3514                 /* identical with ad1983 */
3515                 .info = ad1983_spdif_route_info,
3516                 .get = ad1983_spdif_route_get,
3517                 .put = ad1983_spdif_route_put,
3518         },
3519         { } /* end */
3520 };
3521
3522 /*
3523  * initialization verbs
3524  */
3525 static struct hda_verb ad1884a_init_verbs[] = {
3526         /* DACs; unmute as default */
3527         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
3528         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0x27}, /* 0dB */
3529         /* Port-A (HP) mixer - route only from analog mixer */
3530         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3531         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3532         /* Port-A pin */
3533         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3534         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3535         /* Port-D (Line-out) mixer - route only from analog mixer */
3536         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3537         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3538         /* Port-D pin */
3539         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3540         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3541         /* Mono-out mixer - route only from analog mixer */
3542         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3543         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3544         /* Mono-out pin */
3545         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3546         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3547         /* Port-B (front mic) pin */
3548         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3549         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3550         /* Port-C (rear line-in) pin */
3551         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3552         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3553         /* Port-E (rear mic) pin */
3554         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3555         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3556         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* no boost */
3557         /* Port-F (CD) pin */
3558         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3559         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3560         /* Analog mixer; mute as default */
3561         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3562         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3563         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3564         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3565         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)}, /* aux */
3566         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
3567         /* Analog Mix output amp */
3568         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3569         /* capture sources */
3570         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
3571         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3572         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
3573         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3574         /* SPDIF output amp */
3575         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
3576         { } /* end */
3577 };
3578
3579 #ifdef CONFIG_SND_HDA_POWER_SAVE
3580 static struct hda_amp_list ad1884a_loopbacks[] = {
3581         { 0x20, HDA_INPUT, 0 }, /* Front Mic */
3582         { 0x20, HDA_INPUT, 1 }, /* Mic */
3583         { 0x20, HDA_INPUT, 2 }, /* CD */
3584         { 0x20, HDA_INPUT, 4 }, /* Docking */
3585         { } /* end */
3586 };
3587 #endif
3588
3589 /*
3590  * Laptop model
3591  *
3592  * Port A: Headphone jack
3593  * Port B: MIC jack
3594  * Port C: Internal MIC
3595  * Port D: Dock Line Out (if enabled)
3596  * Port E: Dock Line In (if enabled)
3597  * Port F: Internal speakers
3598  */
3599
3600 static struct hda_input_mux ad1884a_laptop_capture_source = {
3601         .num_items = 4,
3602         .items = {
3603                 { "Mic", 0x0 },         /* port-B */
3604                 { "Internal Mic", 0x1 }, /* port-C */
3605                 { "Dock Mic", 0x4 },    /* port-E */
3606                 { "Mix", 0x3 },
3607         },
3608 };
3609
3610 static struct snd_kcontrol_new ad1884a_laptop_mixers[] = {
3611         HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
3612         HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),
3613         HDA_CODEC_MUTE("Dock Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3614         HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
3615         HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
3616         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3617         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3618         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
3619         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
3620         HDA_CODEC_VOLUME("Dock Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
3621         HDA_CODEC_MUTE("Dock Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
3622         HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x03, HDA_INPUT),
3623         HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x03, HDA_INPUT),
3624         HDA_CODEC_VOLUME("Mic Boost", 0x14, 0x0, HDA_INPUT),
3625         HDA_CODEC_VOLUME("Internal Mic Boost", 0x15, 0x0, HDA_INPUT),
3626         HDA_CODEC_VOLUME("Dock Mic Boost", 0x25, 0x0, HDA_OUTPUT),
3627         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3628         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3629         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3630         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3631         {
3632                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3633                 /* The multiple "Capture Source" controls confuse alsamixer
3634                  * So call somewhat different..
3635                  */
3636                 /* .name = "Capture Source", */
3637                 .name = "Input Source",
3638                 .count = 2,
3639                 .info = ad198x_mux_enum_info,
3640                 .get = ad198x_mux_enum_get,
3641                 .put = ad198x_mux_enum_put,
3642         },
3643         { } /* end */
3644 };
3645
3646 static struct hda_input_mux ad1884a_mobile_capture_source = {
3647         .num_items = 2,
3648         .items = {
3649                 { "Mic", 0x1 }, /* port-C */
3650                 { "Mix", 0x3 },
3651         },
3652 };
3653
3654 static struct snd_kcontrol_new ad1884a_mobile_mixers[] = {
3655         HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
3656         HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),
3657         HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
3658         HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
3659         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
3660         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
3661         HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x03, HDA_INPUT),
3662         HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x03, HDA_INPUT),
3663         HDA_CODEC_VOLUME("Mic Boost", 0x15, 0x0, HDA_INPUT),
3664         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3665         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3666         {
3667                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3668                 .name = "Capture Source",
3669                 .info = ad198x_mux_enum_info,
3670                 .get = ad198x_mux_enum_get,
3671                 .put = ad198x_mux_enum_put,
3672         },
3673         { } /* end */
3674 };
3675
3676 /* mute internal speaker if HP is plugged */
3677 static void ad1884a_hp_automute(struct hda_codec *codec)
3678 {
3679         unsigned int present;
3680
3681         present = snd_hda_codec_read(codec, 0x11, 0,
3682                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
3683         snd_hda_codec_amp_stereo(codec, 0x16, HDA_OUTPUT, 0,
3684                                  HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
3685         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_EAPD_BTLENABLE,
3686                             present ? 0x00 : 0x02);
3687 }
3688
3689 #define AD1884A_HP_EVENT                0x37
3690
3691 /* unsolicited event for HP jack sensing */
3692 static void ad1884a_hp_unsol_event(struct hda_codec *codec, unsigned int res)
3693 {
3694         if ((res >> 26) != AD1884A_HP_EVENT)
3695                 return;
3696         ad1884a_hp_automute(codec);
3697 }
3698
3699 /* initialize jack-sensing, too */
3700 static int ad1884a_hp_init(struct hda_codec *codec)
3701 {
3702         ad198x_init(codec);
3703         ad1884a_hp_automute(codec);
3704         return 0;
3705 }
3706
3707 /* additional verbs for laptop model */
3708 static struct hda_verb ad1884a_laptop_verbs[] = {
3709         /* Port-A (HP) pin - always unmuted */
3710         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3711         /* Port-F (int speaker) mixer - route only from analog mixer */
3712         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3713         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3714         /* Port-F pin */
3715         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3716         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3717         /* analog mix */
3718         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3719         /* unsolicited event for pin-sense */
3720         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
3721         { } /* end */
3722 };
3723
3724 /*
3725  * Thinkpad X300
3726  * 0x11 - HP
3727  * 0x12 - speaker
3728  * 0x14 - mic-in
3729  * 0x17 - built-in mic
3730  */
3731
3732 static struct hda_verb ad1984a_thinkpad_verbs[] = {
3733         /* HP unmute */
3734         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3735         /* analog mix */
3736         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3737         /* turn on EAPD */
3738         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x02},
3739         /* unsolicited event for pin-sense */
3740         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1884A_HP_EVENT},
3741         /* internal mic - dmic */
3742         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3743         /* set magic COEFs for dmic */
3744         {0x01, AC_VERB_SET_COEF_INDEX, 0x13f7},
3745         {0x01, AC_VERB_SET_PROC_COEF, 0x08},
3746         { } /* end */
3747 };
3748
3749 static struct snd_kcontrol_new ad1984a_thinkpad_mixers[] = {
3750         HDA_CODEC_VOLUME("Master Playback Volume", 0x21, 0x0, HDA_OUTPUT),
3751         HDA_CODEC_MUTE("Master Playback Switch", 0x21, 0x0, HDA_OUTPUT),
3752         HDA_CODEC_VOLUME("PCM Playback Volume", 0x20, 0x5, HDA_INPUT),
3753         HDA_CODEC_MUTE("PCM Playback Switch", 0x20, 0x5, HDA_INPUT),
3754         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3755         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3756         HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x03, HDA_INPUT),
3757         HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x03, HDA_INPUT),
3758         HDA_CODEC_VOLUME("Mic Boost", 0x14, 0x0, HDA_INPUT),
3759         HDA_CODEC_VOLUME("Internal Mic Boost", 0x17, 0x0, HDA_INPUT),
3760         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3761         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3762         {
3763                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3764                 .name = "Capture Source",
3765                 .info = ad198x_mux_enum_info,
3766                 .get = ad198x_mux_enum_get,
3767                 .put = ad198x_mux_enum_put,
3768         },
3769         { } /* end */
3770 };
3771
3772 static struct hda_input_mux ad1984a_thinkpad_capture_source = {
3773         .num_items = 3,
3774         .items = {
3775                 { "Mic", 0x0 },
3776                 { "Internal Mic", 0x5 },
3777                 { "Mix", 0x3 },
3778         },
3779 };
3780
3781 /* mute internal speaker if HP is plugged */
3782 static void ad1984a_thinkpad_automute(struct hda_codec *codec)
3783 {
3784         unsigned int present;
3785
3786         present = snd_hda_codec_read(codec, 0x11, 0, AC_VERB_GET_PIN_SENSE, 0)
3787                 & AC_PINSENSE_PRESENCE;
3788         snd_hda_codec_amp_stereo(codec, 0x12, HDA_OUTPUT, 0,
3789                                  HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
3790 }
3791
3792 /* unsolicited event for HP jack sensing */
3793 static void ad1984a_thinkpad_unsol_event(struct hda_codec *codec,
3794                                          unsigned int res)
3795 {
3796         if ((res >> 26) != AD1884A_HP_EVENT)
3797                 return;
3798         ad1984a_thinkpad_automute(codec);
3799 }
3800
3801 /* initialize jack-sensing, too */
3802 static int ad1984a_thinkpad_init(struct hda_codec *codec)
3803 {
3804         ad198x_init(codec);
3805         ad1984a_thinkpad_automute(codec);
3806         return 0;
3807 }
3808
3809 /*
3810  */
3811
3812 enum {
3813         AD1884A_DESKTOP,
3814         AD1884A_LAPTOP,
3815         AD1884A_MOBILE,
3816         AD1884A_THINKPAD,
3817         AD1884A_MODELS
3818 };
3819
3820 static const char *ad1884a_models[AD1884A_MODELS] = {
3821         [AD1884A_DESKTOP]       = "desktop",
3822         [AD1884A_LAPTOP]        = "laptop",
3823         [AD1884A_MOBILE]        = "mobile",
3824         [AD1884A_THINKPAD]      = "thinkpad",
3825 };
3826
3827 static struct snd_pci_quirk ad1884a_cfg_tbl[] = {
3828         SND_PCI_QUIRK(0x103c, 0x3030, "HP", AD1884A_MOBILE),
3829         SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X300", AD1884A_THINKPAD),
3830         {}
3831 };
3832
3833 static int patch_ad1884a(struct hda_codec *codec)
3834 {
3835         struct ad198x_spec *spec;
3836         int board_config;
3837
3838         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3839         if (spec == NULL)
3840                 return -ENOMEM;
3841
3842         mutex_init(&spec->amp_mutex);
3843         codec->spec = spec;
3844
3845         spec->multiout.max_channels = 2;
3846         spec->multiout.num_dacs = ARRAY_SIZE(ad1884a_dac_nids);
3847         spec->multiout.dac_nids = ad1884a_dac_nids;
3848         spec->multiout.dig_out_nid = AD1884A_SPDIF_OUT;
3849         spec->num_adc_nids = ARRAY_SIZE(ad1884a_adc_nids);
3850         spec->adc_nids = ad1884a_adc_nids;
3851         spec->capsrc_nids = ad1884a_capsrc_nids;
3852         spec->input_mux = &ad1884a_capture_source;
3853         spec->num_mixers = 1;
3854         spec->mixers[0] = ad1884a_base_mixers;
3855         spec->num_init_verbs = 1;
3856         spec->init_verbs[0] = ad1884a_init_verbs;
3857         spec->spdif_route = 0;
3858 #ifdef CONFIG_SND_HDA_POWER_SAVE
3859         spec->loopback.amplist = ad1884a_loopbacks;
3860 #endif
3861         codec->patch_ops = ad198x_patch_ops;
3862
3863         /* override some parameters */
3864         board_config = snd_hda_check_board_config(codec, AD1884A_MODELS,
3865                                                   ad1884a_models,
3866                                                   ad1884a_cfg_tbl);
3867         switch (board_config) {
3868         case AD1884A_LAPTOP:
3869                 spec->mixers[0] = ad1884a_laptop_mixers;
3870                 spec->init_verbs[spec->num_init_verbs++] = ad1884a_laptop_verbs;
3871                 spec->multiout.dig_out_nid = 0;
3872                 spec->input_mux = &ad1884a_laptop_capture_source;
3873                 codec->patch_ops.unsol_event = ad1884a_hp_unsol_event;
3874                 codec->patch_ops.init = ad1884a_hp_init;
3875                 break;
3876         case AD1884A_MOBILE:
3877                 spec->mixers[0] = ad1884a_mobile_mixers;
3878                 spec->init_verbs[spec->num_init_verbs++] = ad1884a_laptop_verbs;
3879                 spec->multiout.dig_out_nid = 0;
3880                 spec->input_mux = &ad1884a_mobile_capture_source;
3881                 codec->patch_ops.unsol_event = ad1884a_hp_unsol_event;
3882                 codec->patch_ops.init = ad1884a_hp_init;
3883                 break;
3884         case AD1884A_THINKPAD:
3885                 spec->mixers[0] = ad1984a_thinkpad_mixers;
3886                 spec->init_verbs[spec->num_init_verbs++] =
3887                         ad1984a_thinkpad_verbs;
3888                 spec->multiout.dig_out_nid = 0;
3889                 spec->input_mux = &ad1984a_thinkpad_capture_source;
3890                 codec->patch_ops.unsol_event = ad1984a_thinkpad_unsol_event;
3891                 codec->patch_ops.init = ad1984a_thinkpad_init;
3892                 break;
3893         }
3894
3895         return 0;
3896 }
3897
3898
3899 /*
3900  * AD1882
3901  *
3902  * port-A - front hp-out
3903  * port-B - front mic-in
3904  * port-C - rear line-in, shared surr-out (3stack)
3905  * port-D - rear line-out
3906  * port-E - rear mic-in, shared clfe-out (3stack)
3907  * port-F - rear surr-out (6stack)
3908  * port-G - rear clfe-out (6stack)
3909  */
3910
3911 static hda_nid_t ad1882_dac_nids[3] = {
3912         0x04, 0x03, 0x05
3913 };
3914
3915 static hda_nid_t ad1882_adc_nids[2] = {
3916         0x08, 0x09,
3917 };
3918
3919 static hda_nid_t ad1882_capsrc_nids[2] = {
3920         0x0c, 0x0d,
3921 };
3922
3923 #define AD1882_SPDIF_OUT        0x02
3924
3925 /* list: 0x11, 0x39, 0x3a, 0x18, 0x3c, 0x3b, 0x12, 0x20 */
3926 static struct hda_input_mux ad1882_capture_source = {
3927         .num_items = 5,
3928         .items = {
3929                 { "Front Mic", 0x1 },
3930                 { "Mic", 0x4 },
3931                 { "Line", 0x2 },
3932                 { "CD", 0x3 },
3933                 { "Mix", 0x7 },
3934         },
3935 };
3936
3937 static struct snd_kcontrol_new ad1882_base_mixers[] = {
3938         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
3939         HDA_CODEC_VOLUME("Surround Playback Volume", 0x03, 0x0, HDA_OUTPUT),
3940         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
3941         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
3942         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
3943         HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3944         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
3945         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
3946         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3947         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3948         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
3949         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
3950         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x04, HDA_INPUT),
3951         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x04, HDA_INPUT),
3952         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x06, HDA_INPUT),
3953         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x06, HDA_INPUT),
3954         HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x07, HDA_INPUT),
3955         HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x07, HDA_INPUT),
3956         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
3957         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
3958         HDA_CODEC_VOLUME("Line-In Boost", 0x3a, 0x0, HDA_OUTPUT),
3959         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3960         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3961         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3962         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3963         {
3964                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3965                 /* The multiple "Capture Source" controls confuse alsamixer
3966                  * So call somewhat different..
3967                  */
3968                 /* .name = "Capture Source", */
3969                 .name = "Input Source",
3970                 .count = 2,
3971                 .info = ad198x_mux_enum_info,
3972                 .get = ad198x_mux_enum_get,
3973                 .put = ad198x_mux_enum_put,
3974         },
3975         /* SPDIF controls */
3976         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
3977         {
3978                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3979                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
3980                 /* identical with ad1983 */
3981                 .info = ad1983_spdif_route_info,
3982                 .get = ad1983_spdif_route_get,
3983                 .put = ad1983_spdif_route_put,
3984         },
3985         { } /* end */
3986 };
3987
3988 static struct snd_kcontrol_new ad1882_3stack_mixers[] = {
3989         HDA_CODEC_MUTE("Surround Playback Switch", 0x15, 0x0, HDA_OUTPUT),
3990         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x17, 1, 0x0, HDA_OUTPUT),
3991         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x17, 2, 0x0, HDA_OUTPUT),
3992         {
3993                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3994                 .name = "Channel Mode",
3995                 .info = ad198x_ch_mode_info,
3996                 .get = ad198x_ch_mode_get,
3997                 .put = ad198x_ch_mode_put,
3998         },
3999         { } /* end */
4000 };
4001
4002 static struct snd_kcontrol_new ad1882_6stack_mixers[] = {
4003         HDA_CODEC_MUTE("Surround Playback Switch", 0x16, 0x0, HDA_OUTPUT),
4004         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x24, 1, 0x0, HDA_OUTPUT),
4005         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x24, 2, 0x0, HDA_OUTPUT),
4006         { } /* end */
4007 };
4008
4009 static struct hda_verb ad1882_ch2_init[] = {
4010         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
4011         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4012         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4013         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4014         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4015         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4016         { } /* end */
4017 };
4018
4019 static struct hda_verb ad1882_ch4_init[] = {
4020         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4021         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4022         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4023         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4024         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4025         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4026         { } /* end */
4027 };
4028
4029 static struct hda_verb ad1882_ch6_init[] = {
4030         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4031         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4032         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4033         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4034         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4035         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4036         { } /* end */
4037 };
4038
4039 static struct hda_channel_mode ad1882_modes[3] = {
4040         { 2, ad1882_ch2_init },
4041         { 4, ad1882_ch4_init },
4042         { 6, ad1882_ch6_init },
4043 };
4044
4045 /*
4046  * initialization verbs
4047  */
4048 static struct hda_verb ad1882_init_verbs[] = {
4049         /* DACs; mute as default */
4050         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
4051         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
4052         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
4053         /* Port-A (HP) mixer */
4054         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4055         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4056         /* Port-A pin */
4057         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
4058         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4059         /* HP selector - select DAC2 */
4060         {0x37, AC_VERB_SET_CONNECT_SEL, 0x1},
4061         /* Port-D (Line-out) mixer */
4062         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4063         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4064         /* Port-D pin */
4065         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
4066         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4067         /* Mono-out mixer */
4068         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4069         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4070         /* Mono-out pin */
4071         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
4072         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4073         /* Port-B (front mic) pin */
4074         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4075         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4076         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
4077         /* Port-C (line-in) pin */
4078         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
4079         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4080         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
4081         /* Port-C mixer - mute as input */
4082         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4083         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4084         /* Port-E (mic-in) pin */
4085         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4086         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4087         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
4088         /* Port-E mixer - mute as input */
4089         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4090         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4091         /* Port-F (surround) */
4092         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4093         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4094         /* Port-G (CLFE) */
4095         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4096         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4097         /* Analog mixer; mute as default */
4098         /* list: 0x39, 0x3a, 0x11, 0x12, 0x3c, 0x3b, 0x18, 0x1a */
4099         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4100         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4101         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4102         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4103         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4104         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
4105         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
4106         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
4107         /* Analog Mix output amp */
4108         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
4109         /* SPDIF output selector */
4110         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
4111         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
4112         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
4113         { } /* end */
4114 };
4115
4116 #ifdef CONFIG_SND_HDA_POWER_SAVE
4117 static struct hda_amp_list ad1882_loopbacks[] = {
4118         { 0x20, HDA_INPUT, 0 }, /* Front Mic */
4119         { 0x20, HDA_INPUT, 1 }, /* Mic */
4120         { 0x20, HDA_INPUT, 4 }, /* Line */
4121         { 0x20, HDA_INPUT, 6 }, /* CD */
4122         { } /* end */
4123 };
4124 #endif
4125
4126 /* models */
4127 enum {
4128         AD1882_3STACK,
4129         AD1882_6STACK,
4130         AD1882_MODELS
4131 };
4132
4133 static const char *ad1882_models[AD1986A_MODELS] = {
4134         [AD1882_3STACK]         = "3stack",
4135         [AD1882_6STACK]         = "6stack",
4136 };
4137
4138
4139 static int patch_ad1882(struct hda_codec *codec)
4140 {
4141         struct ad198x_spec *spec;
4142         int board_config;
4143
4144         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4145         if (spec == NULL)
4146                 return -ENOMEM;
4147
4148         mutex_init(&spec->amp_mutex);
4149         codec->spec = spec;
4150
4151         spec->multiout.max_channels = 6;
4152         spec->multiout.num_dacs = 3;
4153         spec->multiout.dac_nids = ad1882_dac_nids;
4154         spec->multiout.dig_out_nid = AD1882_SPDIF_OUT;
4155         spec->num_adc_nids = ARRAY_SIZE(ad1882_adc_nids);
4156         spec->adc_nids = ad1882_adc_nids;
4157         spec->capsrc_nids = ad1882_capsrc_nids;
4158         spec->input_mux = &ad1882_capture_source;
4159         spec->num_mixers = 1;
4160         spec->mixers[0] = ad1882_base_mixers;
4161         spec->num_init_verbs = 1;
4162         spec->init_verbs[0] = ad1882_init_verbs;
4163         spec->spdif_route = 0;
4164 #ifdef CONFIG_SND_HDA_POWER_SAVE
4165         spec->loopback.amplist = ad1882_loopbacks;
4166 #endif
4167         spec->vmaster_nid = 0x04;
4168
4169         codec->patch_ops = ad198x_patch_ops;
4170
4171         /* override some parameters */
4172         board_config = snd_hda_check_board_config(codec, AD1882_MODELS,
4173                                                   ad1882_models, NULL);
4174         switch (board_config) {
4175         default:
4176         case AD1882_3STACK:
4177                 spec->num_mixers = 2;
4178                 spec->mixers[1] = ad1882_3stack_mixers;
4179                 spec->channel_mode = ad1882_modes;
4180                 spec->num_channel_mode = ARRAY_SIZE(ad1882_modes);
4181                 spec->need_dac_fix = 1;
4182                 spec->multiout.max_channels = 2;
4183                 spec->multiout.num_dacs = 1;
4184                 break;
4185         case AD1882_6STACK:
4186                 spec->num_mixers = 2;
4187                 spec->mixers[1] = ad1882_6stack_mixers;
4188                 break;
4189         }
4190         return 0;
4191 }
4192
4193
4194 /*
4195  * patch entries
4196  */
4197 struct hda_codec_preset snd_hda_preset_analog[] = {
4198         { .id = 0x11d4184a, .name = "AD1884A", .patch = patch_ad1884a },
4199         { .id = 0x11d41882, .name = "AD1882", .patch = patch_ad1882 },
4200         { .id = 0x11d41883, .name = "AD1883", .patch = patch_ad1884a },
4201         { .id = 0x11d41884, .name = "AD1884", .patch = patch_ad1884 },
4202         { .id = 0x11d4194a, .name = "AD1984A", .patch = patch_ad1884a },
4203         { .id = 0x11d4194b, .name = "AD1984B", .patch = patch_ad1884a },
4204         { .id = 0x11d41981, .name = "AD1981", .patch = patch_ad1981 },
4205         { .id = 0x11d41983, .name = "AD1983", .patch = patch_ad1983 },
4206         { .id = 0x11d41984, .name = "AD1984", .patch = patch_ad1984 },
4207         { .id = 0x11d41986, .name = "AD1986A", .patch = patch_ad1986a },
4208         { .id = 0x11d41988, .name = "AD1988", .patch = patch_ad1988 },
4209         { .id = 0x11d4198b, .name = "AD1988B", .patch = patch_ad1988 },
4210         { .id = 0x11d4989a, .name = "AD1989A", .patch = patch_ad1988 },
4211         { .id = 0x11d4989b, .name = "AD1989B", .patch = patch_ad1988 },
4212         {} /* terminator */
4213 };