[ALSA] hda-intel - Don't do suspend if already powered down
[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 <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/mutex.h>
28
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include "hda_local.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[4];
76
77 #ifdef CONFIG_SND_HDA_POWER_SAVE
78         struct hda_loopback_check loopback;
79 #endif
80 };
81
82 /*
83  * input MUX handling (common part)
84  */
85 static int ad198x_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
86 {
87         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
88         struct ad198x_spec *spec = codec->spec;
89
90         return snd_hda_input_mux_info(spec->input_mux, uinfo);
91 }
92
93 static int ad198x_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
94 {
95         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
96         struct ad198x_spec *spec = codec->spec;
97         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
98
99         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
100         return 0;
101 }
102
103 static int ad198x_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
104 {
105         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
106         struct ad198x_spec *spec = codec->spec;
107         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
108
109         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
110                                      spec->capsrc_nids[adc_idx],
111                                      &spec->cur_mux[adc_idx]);
112 }
113
114 /*
115  * initialization (common callbacks)
116  */
117 static int ad198x_init(struct hda_codec *codec)
118 {
119         struct ad198x_spec *spec = codec->spec;
120         int i;
121
122         for (i = 0; i < spec->num_init_verbs; i++)
123                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
124         return 0;
125 }
126
127 static int ad198x_build_controls(struct hda_codec *codec)
128 {
129         struct ad198x_spec *spec = codec->spec;
130         unsigned int i;
131         int err;
132
133         for (i = 0; i < spec->num_mixers; i++) {
134                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
135                 if (err < 0)
136                         return err;
137         }
138         if (spec->multiout.dig_out_nid) {
139                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
140                 if (err < 0)
141                         return err;
142         } 
143         if (spec->dig_in_nid) {
144                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
145                 if (err < 0)
146                         return err;
147         }
148         return 0;
149 }
150
151 #ifdef CONFIG_SND_HDA_POWER_SAVE
152 static int ad198x_check_power_status(struct hda_codec *codec, hda_nid_t nid)
153 {
154         struct ad198x_spec *spec = codec->spec;
155         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
156 }
157 #endif
158
159 /*
160  * Analog playback callbacks
161  */
162 static int ad198x_playback_pcm_open(struct hda_pcm_stream *hinfo,
163                                     struct hda_codec *codec,
164                                     struct snd_pcm_substream *substream)
165 {
166         struct ad198x_spec *spec = codec->spec;
167         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
168 }
169
170 static int ad198x_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
171                                        struct hda_codec *codec,
172                                        unsigned int stream_tag,
173                                        unsigned int format,
174                                        struct snd_pcm_substream *substream)
175 {
176         struct ad198x_spec *spec = codec->spec;
177         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
178                                                 format, substream);
179 }
180
181 static int ad198x_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
182                                        struct hda_codec *codec,
183                                        struct snd_pcm_substream *substream)
184 {
185         struct ad198x_spec *spec = codec->spec;
186         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
187 }
188
189 /*
190  * Digital out
191  */
192 static int ad198x_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
193                                         struct hda_codec *codec,
194                                         struct snd_pcm_substream *substream)
195 {
196         struct ad198x_spec *spec = codec->spec;
197         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
198 }
199
200 static int ad198x_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
201                                          struct hda_codec *codec,
202                                          struct snd_pcm_substream *substream)
203 {
204         struct ad198x_spec *spec = codec->spec;
205         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
206 }
207
208 static int ad198x_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
209                                            struct hda_codec *codec,
210                                            unsigned int stream_tag,
211                                            unsigned int format,
212                                            struct snd_pcm_substream *substream)
213 {
214         struct ad198x_spec *spec = codec->spec;
215         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
216                                              format, substream);
217 }
218
219 /*
220  * Analog capture
221  */
222 static int ad198x_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
223                                       struct hda_codec *codec,
224                                       unsigned int stream_tag,
225                                       unsigned int format,
226                                       struct snd_pcm_substream *substream)
227 {
228         struct ad198x_spec *spec = codec->spec;
229         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
230                                    stream_tag, 0, format);
231         return 0;
232 }
233
234 static int ad198x_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
235                                       struct hda_codec *codec,
236                                       struct snd_pcm_substream *substream)
237 {
238         struct ad198x_spec *spec = codec->spec;
239         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
240                                    0, 0, 0);
241         return 0;
242 }
243
244
245 /*
246  */
247 static struct hda_pcm_stream ad198x_pcm_analog_playback = {
248         .substreams = 1,
249         .channels_min = 2,
250         .channels_max = 6, /* changed later */
251         .nid = 0, /* fill later */
252         .ops = {
253                 .open = ad198x_playback_pcm_open,
254                 .prepare = ad198x_playback_pcm_prepare,
255                 .cleanup = ad198x_playback_pcm_cleanup
256         },
257 };
258
259 static struct hda_pcm_stream ad198x_pcm_analog_capture = {
260         .substreams = 1,
261         .channels_min = 2,
262         .channels_max = 2,
263         .nid = 0, /* fill later */
264         .ops = {
265                 .prepare = ad198x_capture_pcm_prepare,
266                 .cleanup = ad198x_capture_pcm_cleanup
267         },
268 };
269
270 static struct hda_pcm_stream ad198x_pcm_digital_playback = {
271         .substreams = 1,
272         .channels_min = 2,
273         .channels_max = 2,
274         .nid = 0, /* fill later */
275         .ops = {
276                 .open = ad198x_dig_playback_pcm_open,
277                 .close = ad198x_dig_playback_pcm_close,
278                 .prepare = ad198x_dig_playback_pcm_prepare
279         },
280 };
281
282 static struct hda_pcm_stream ad198x_pcm_digital_capture = {
283         .substreams = 1,
284         .channels_min = 2,
285         .channels_max = 2,
286         /* NID is set in alc_build_pcms */
287 };
288
289 static int ad198x_build_pcms(struct hda_codec *codec)
290 {
291         struct ad198x_spec *spec = codec->spec;
292         struct hda_pcm *info = spec->pcm_rec;
293
294         codec->num_pcms = 1;
295         codec->pcm_info = info;
296
297         info->name = "AD198x Analog";
298         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ad198x_pcm_analog_playback;
299         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->multiout.max_channels;
300         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
301         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad198x_pcm_analog_capture;
302         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adc_nids;
303         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
304
305         if (spec->multiout.dig_out_nid) {
306                 info++;
307                 codec->num_pcms++;
308                 info->name = "AD198x Digital";
309                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ad198x_pcm_digital_playback;
310                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
311                 if (spec->dig_in_nid) {
312                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad198x_pcm_digital_capture;
313                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
314                 }
315         }
316
317         return 0;
318 }
319
320 static void ad198x_free(struct hda_codec *codec)
321 {
322         struct ad198x_spec *spec = codec->spec;
323         unsigned int i;
324
325         if (spec->kctl_alloc) {
326                 for (i = 0; i < spec->num_kctl_used; i++)
327                         kfree(spec->kctl_alloc[i].name);
328                 kfree(spec->kctl_alloc);
329         }
330         kfree(codec->spec);
331 }
332
333 static struct hda_codec_ops ad198x_patch_ops = {
334         .build_controls = ad198x_build_controls,
335         .build_pcms = ad198x_build_pcms,
336         .init = ad198x_init,
337         .free = ad198x_free,
338 #ifdef CONFIG_SND_HDA_POWER_SAVE
339         .check_power_status = ad198x_check_power_status,
340 #endif
341 };
342
343
344 /*
345  * EAPD control
346  * the private value = nid | (invert << 8)
347  */
348 #define ad198x_eapd_info        snd_ctl_boolean_mono_info
349
350 static int ad198x_eapd_get(struct snd_kcontrol *kcontrol,
351                            struct snd_ctl_elem_value *ucontrol)
352 {
353         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
354         struct ad198x_spec *spec = codec->spec;
355         int invert = (kcontrol->private_value >> 8) & 1;
356         if (invert)
357                 ucontrol->value.integer.value[0] = ! spec->cur_eapd;
358         else
359                 ucontrol->value.integer.value[0] = spec->cur_eapd;
360         return 0;
361 }
362
363 static int ad198x_eapd_put(struct snd_kcontrol *kcontrol,
364                            struct snd_ctl_elem_value *ucontrol)
365 {
366         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
367         struct ad198x_spec *spec = codec->spec;
368         int invert = (kcontrol->private_value >> 8) & 1;
369         hda_nid_t nid = kcontrol->private_value & 0xff;
370         unsigned int eapd;
371         eapd = ucontrol->value.integer.value[0];
372         if (invert)
373                 eapd = !eapd;
374         if (eapd == spec->cur_eapd)
375                 return 0;
376         spec->cur_eapd = eapd;
377         snd_hda_codec_write_cache(codec, nid,
378                                   0, AC_VERB_SET_EAPD_BTLENABLE,
379                                   eapd ? 0x02 : 0x00);
380         return 1;
381 }
382
383 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
384                                struct snd_ctl_elem_info *uinfo);
385 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
386                               struct snd_ctl_elem_value *ucontrol);
387 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
388                               struct snd_ctl_elem_value *ucontrol);
389
390
391 /*
392  * AD1986A specific
393  */
394
395 #define AD1986A_SPDIF_OUT       0x02
396 #define AD1986A_FRONT_DAC       0x03
397 #define AD1986A_SURR_DAC        0x04
398 #define AD1986A_CLFE_DAC        0x05
399 #define AD1986A_ADC             0x06
400
401 static hda_nid_t ad1986a_dac_nids[3] = {
402         AD1986A_FRONT_DAC, AD1986A_SURR_DAC, AD1986A_CLFE_DAC
403 };
404 static hda_nid_t ad1986a_adc_nids[1] = { AD1986A_ADC };
405 static hda_nid_t ad1986a_capsrc_nids[1] = { 0x12 };
406
407 static struct hda_input_mux ad1986a_capture_source = {
408         .num_items = 7,
409         .items = {
410                 { "Mic", 0x0 },
411                 { "CD", 0x1 },
412                 { "Aux", 0x3 },
413                 { "Line", 0x4 },
414                 { "Mix", 0x5 },
415                 { "Mono", 0x6 },
416                 { "Phone", 0x7 },
417         },
418 };
419
420
421 static struct hda_bind_ctls ad1986a_bind_pcm_vol = {
422         .ops = &snd_hda_bind_vol,
423         .values = {
424                 HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT),
425                 HDA_COMPOSE_AMP_VAL(AD1986A_SURR_DAC, 3, 0, HDA_OUTPUT),
426                 HDA_COMPOSE_AMP_VAL(AD1986A_CLFE_DAC, 3, 0, HDA_OUTPUT),
427                 0
428         },
429 };
430
431 static struct hda_bind_ctls ad1986a_bind_pcm_sw = {
432         .ops = &snd_hda_bind_sw,
433         .values = {
434                 HDA_COMPOSE_AMP_VAL(AD1986A_FRONT_DAC, 3, 0, HDA_OUTPUT),
435                 HDA_COMPOSE_AMP_VAL(AD1986A_SURR_DAC, 3, 0, HDA_OUTPUT),
436                 HDA_COMPOSE_AMP_VAL(AD1986A_CLFE_DAC, 3, 0, HDA_OUTPUT),
437                 0
438         },
439 };
440
441 /*
442  * mixers
443  */
444 static struct snd_kcontrol_new ad1986a_mixers[] = {
445         /*
446          * bind volumes/mutes of 3 DACs as a single PCM control for simplicity
447          */
448         HDA_BIND_VOL("PCM Playback Volume", &ad1986a_bind_pcm_vol),
449         HDA_BIND_SW("PCM Playback Switch", &ad1986a_bind_pcm_sw),
450         HDA_CODEC_VOLUME("Front Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
451         HDA_CODEC_MUTE("Front Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
452         HDA_CODEC_VOLUME("Surround Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
453         HDA_CODEC_MUTE("Surround Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
454         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x1d, 1, 0x0, HDA_OUTPUT),
455         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x1d, 2, 0x0, HDA_OUTPUT),
456         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x1d, 1, 0x0, HDA_OUTPUT),
457         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x1d, 2, 0x0, HDA_OUTPUT),
458         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x1a, 0x0, HDA_OUTPUT),
459         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1a, 0x0, HDA_OUTPUT),
460         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
461         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
462         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
463         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
464         HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
465         HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
466         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
467         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
468         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
469         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x18, 0x0, HDA_OUTPUT),
470         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x18, 0x0, HDA_OUTPUT),
471         HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
472         HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT),
473         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
474         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
475         {
476                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
477                 .name = "Capture Source",
478                 .info = ad198x_mux_enum_info,
479                 .get = ad198x_mux_enum_get,
480                 .put = ad198x_mux_enum_put,
481         },
482         HDA_CODEC_MUTE("Stereo Downmix Switch", 0x09, 0x0, HDA_OUTPUT),
483         { } /* end */
484 };
485
486 /* additional mixers for 3stack mode */
487 static struct snd_kcontrol_new ad1986a_3st_mixers[] = {
488         {
489                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
490                 .name = "Channel Mode",
491                 .info = ad198x_ch_mode_info,
492                 .get = ad198x_ch_mode_get,
493                 .put = ad198x_ch_mode_put,
494         },
495         { } /* end */
496 };
497
498 /* laptop model - 2ch only */
499 static hda_nid_t ad1986a_laptop_dac_nids[1] = { AD1986A_FRONT_DAC };
500
501 static struct snd_kcontrol_new ad1986a_laptop_mixers[] = {
502         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
503         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
504         HDA_CODEC_VOLUME("Master Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
505         HDA_CODEC_MUTE("Master Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
506         /* HDA_CODEC_VOLUME("Headphone Playback Volume", 0x1a, 0x0, HDA_OUTPUT),
507            HDA_CODEC_MUTE("Headphone Playback Switch", 0x1a, 0x0, HDA_OUTPUT), */
508         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_OUTPUT),
509         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_OUTPUT),
510         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x0, HDA_OUTPUT),
511         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x0, HDA_OUTPUT),
512         HDA_CODEC_VOLUME("Aux Playback Volume", 0x16, 0x0, HDA_OUTPUT),
513         HDA_CODEC_MUTE("Aux Playback Switch", 0x16, 0x0, HDA_OUTPUT),
514         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
515         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
516         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
517         /* HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x18, 0x0, HDA_OUTPUT),
518            HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x18, 0x0, HDA_OUTPUT),
519            HDA_CODEC_VOLUME("Mono Playback Volume", 0x1e, 0x0, HDA_OUTPUT),
520            HDA_CODEC_MUTE("Mono Playback Switch", 0x1e, 0x0, HDA_OUTPUT), */
521         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
522         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
523         {
524                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
525                 .name = "Capture Source",
526                 .info = ad198x_mux_enum_info,
527                 .get = ad198x_mux_enum_get,
528                 .put = ad198x_mux_enum_put,
529         },
530         { } /* end */
531 };
532
533 /* laptop-eapd model - 2ch only */
534
535 /* master controls both pins 0x1a and 0x1b */
536 static struct hda_bind_ctls ad1986a_laptop_master_vol = {
537         .ops = &snd_hda_bind_vol,
538         .values = {
539                 HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
540                 HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
541                 0,
542         },
543 };
544
545 static struct hda_bind_ctls ad1986a_laptop_master_sw = {
546         .ops = &snd_hda_bind_sw,
547         .values = {
548                 HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT),
549                 HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
550                 0,
551         },
552 };
553
554 static struct hda_input_mux ad1986a_laptop_eapd_capture_source = {
555         .num_items = 3,
556         .items = {
557                 { "Mic", 0x0 },
558                 { "Internal Mic", 0x4 },
559                 { "Mix", 0x5 },
560         },
561 };
562
563 static struct snd_kcontrol_new ad1986a_laptop_eapd_mixers[] = {
564         HDA_BIND_VOL("Master Playback Volume", &ad1986a_laptop_master_vol),
565         HDA_BIND_SW("Master Playback Switch", &ad1986a_laptop_master_sw),
566         HDA_CODEC_VOLUME("PCM Playback Volume", 0x03, 0x0, HDA_OUTPUT),
567         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT),
568         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0x0, HDA_OUTPUT),
569         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0x0, HDA_OUTPUT),
570         HDA_CODEC_VOLUME("Mic Playback Volume", 0x13, 0x0, HDA_OUTPUT),
571         HDA_CODEC_MUTE("Mic Playback Switch", 0x13, 0x0, HDA_OUTPUT),
572         HDA_CODEC_VOLUME("Mic Boost", 0x0f, 0x0, HDA_OUTPUT),
573         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_OUTPUT),
574         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_OUTPUT),
575         {
576                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
577                 .name = "Capture Source",
578                 .info = ad198x_mux_enum_info,
579                 .get = ad198x_mux_enum_get,
580                 .put = ad198x_mux_enum_put,
581         },
582         {
583                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
584                 .name = "External Amplifier",
585                 .info = ad198x_eapd_info,
586                 .get = ad198x_eapd_get,
587                 .put = ad198x_eapd_put,
588                 .private_value = 0x1b | (1 << 8), /* port-D, inversed */
589         },
590         { } /* end */
591 };
592
593 /*
594  * initialization verbs
595  */
596 static struct hda_verb ad1986a_init_verbs[] = {
597         /* Front, Surround, CLFE DAC; mute as default */
598         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
599         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
600         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
601         /* Downmix - off */
602         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
603         /* HP, Line-Out, Surround, CLFE selectors */
604         {0x0a, AC_VERB_SET_CONNECT_SEL, 0x0},
605         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x0},
606         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
607         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
608         /* Mono selector */
609         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x0},
610         /* Mic selector: Mic 1/2 pin */
611         {0x0f, AC_VERB_SET_CONNECT_SEL, 0x0},
612         /* Line-in selector: Line-in */
613         {0x10, AC_VERB_SET_CONNECT_SEL, 0x0},
614         /* Mic 1/2 swap */
615         {0x11, AC_VERB_SET_CONNECT_SEL, 0x0},
616         /* Record selector: mic */
617         {0x12, AC_VERB_SET_CONNECT_SEL, 0x0},
618         /* Mic, Phone, CD, Aux, Line-In amp; mute as default */
619         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
620         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
621         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
622         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
623         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
624         /* PC beep */
625         {0x18, AC_VERB_SET_CONNECT_SEL, 0x0},
626         /* HP, Line-Out, Surround, CLFE, Mono pins; mute as default */
627         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
628         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
629         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
630         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
631         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
632         /* HP Pin */
633         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
634         /* Front, Surround, CLFE Pins */
635         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
636         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
637         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
638         /* Mono Pin */
639         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
640         /* Mic Pin */
641         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
642         /* Line, Aux, CD, Beep-In Pin */
643         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
644         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
645         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
646         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
647         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
648         { } /* end */
649 };
650
651 static struct hda_verb ad1986a_ch2_init[] = {
652         /* Surround out -> Line In */
653         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
654         /* Line-in selectors */
655         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x1 },
656         /* CLFE -> Mic in */
657         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
658         /* Mic selector, mix C/LFE (backmic) and Mic (frontmic) */
659         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x4 },
660         { } /* end */
661 };
662
663 static struct hda_verb ad1986a_ch4_init[] = {
664         /* Surround out -> Surround */
665         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
666         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
667         /* CLFE -> Mic in */
668         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
669         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x4 },
670         { } /* end */
671 };
672
673 static struct hda_verb ad1986a_ch6_init[] = {
674         /* Surround out -> Surround out */
675         { 0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
676         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
677         /* CLFE -> CLFE */
678         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
679         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x0 },
680         { } /* end */
681 };
682
683 static struct hda_channel_mode ad1986a_modes[3] = {
684         { 2, ad1986a_ch2_init },
685         { 4, ad1986a_ch4_init },
686         { 6, ad1986a_ch6_init },
687 };
688
689 /* eapd initialization */
690 static struct hda_verb ad1986a_eapd_init_verbs[] = {
691         {0x1b, AC_VERB_SET_EAPD_BTLENABLE, 0x00 },
692         {}
693 };
694
695 /* Ultra initialization */
696 static struct hda_verb ad1986a_ultra_init[] = {
697         /* eapd initialization */
698         { 0x1b, AC_VERB_SET_EAPD_BTLENABLE, 0x00 },
699         /* CLFE -> Mic in */
700         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x2 },
701         { 0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
702         { 0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080 },
703         { } /* end */
704 };
705
706 /* models */
707 enum {
708         AD1986A_6STACK,
709         AD1986A_3STACK,
710         AD1986A_LAPTOP,
711         AD1986A_LAPTOP_EAPD,
712         AD1986A_ULTRA,
713         AD1986A_MODELS
714 };
715
716 static const char *ad1986a_models[AD1986A_MODELS] = {
717         [AD1986A_6STACK]        = "6stack",
718         [AD1986A_3STACK]        = "3stack",
719         [AD1986A_LAPTOP]        = "laptop",
720         [AD1986A_LAPTOP_EAPD]   = "laptop-eapd",
721         [AD1986A_ULTRA]         = "ultra",
722 };
723
724 static struct snd_pci_quirk ad1986a_cfg_tbl[] = {
725         SND_PCI_QUIRK(0x103c, 0x30af, "HP B2800", AD1986A_LAPTOP_EAPD),
726         SND_PCI_QUIRK(0x10de, 0xcb84, "ASUS A8N-VM", AD1986A_3STACK),
727         SND_PCI_QUIRK(0x1043, 0x1153, "ASUS M9", AD1986A_LAPTOP_EAPD),
728         SND_PCI_QUIRK(0x1043, 0x1213, "ASUS A6J", AD1986A_LAPTOP_EAPD),
729         SND_PCI_QUIRK(0x1043, 0x11f7, "ASUS U5A", AD1986A_LAPTOP_EAPD),
730         SND_PCI_QUIRK(0x1043, 0x1263, "ASUS U5F", AD1986A_LAPTOP_EAPD),
731         SND_PCI_QUIRK(0x1043, 0x1297, "ASUS Z62F", AD1986A_LAPTOP_EAPD),
732         SND_PCI_QUIRK(0x1043, 0x12b3, "ASUS V1j", AD1986A_LAPTOP_EAPD),
733         SND_PCI_QUIRK(0x1043, 0x1302, "ASUS W3j", AD1986A_LAPTOP_EAPD),
734         SND_PCI_QUIRK(0x1043, 0x1447, "ASUS A8J", AD1986A_3STACK),
735         SND_PCI_QUIRK(0x1043, 0x817f, "ASUS P5", AD1986A_3STACK),
736         SND_PCI_QUIRK(0x1043, 0x818f, "ASUS P5", AD1986A_LAPTOP),
737         SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS P5", AD1986A_3STACK),
738         SND_PCI_QUIRK(0x1043, 0x81cb, "ASUS M2N", AD1986A_3STACK),
739         SND_PCI_QUIRK(0x1043, 0x8234, "ASUS M2N", AD1986A_3STACK),
740         SND_PCI_QUIRK(0x144d, 0xb03c, "Samsung R55", AD1986A_3STACK),
741         SND_PCI_QUIRK(0x144d, 0xc01e, "FSC V2060", AD1986A_LAPTOP),
742         SND_PCI_QUIRK(0x144d, 0xc023, "Samsung X60", AD1986A_LAPTOP_EAPD),
743         SND_PCI_QUIRK(0x144d, 0xc024, "Samsung R65", AD1986A_LAPTOP_EAPD),
744         SND_PCI_QUIRK(0x144d, 0xc026, "Samsung X11", AD1986A_LAPTOP_EAPD),
745         SND_PCI_QUIRK(0x144d, 0xc504, "Samsung Q35", AD1986A_3STACK),
746         SND_PCI_QUIRK(0x144d, 0xc027, "Samsung Q1", AD1986A_ULTRA),
747         SND_PCI_QUIRK(0x17aa, 0x1011, "Lenovo M55", AD1986A_LAPTOP),
748         SND_PCI_QUIRK(0x17aa, 0x1017, "Lenovo A60", AD1986A_3STACK),
749         SND_PCI_QUIRK(0x17aa, 0x2066, "Lenovo N100", AD1986A_LAPTOP_EAPD),
750         SND_PCI_QUIRK(0x17c0, 0x2017, "Samsung M50", AD1986A_LAPTOP),
751         {}
752 };
753
754 #ifdef CONFIG_SND_HDA_POWER_SAVE
755 static struct hda_amp_list ad1986a_loopbacks[] = {
756         { 0x13, HDA_OUTPUT, 0 }, /* Mic */
757         { 0x14, HDA_OUTPUT, 0 }, /* Phone */
758         { 0x15, HDA_OUTPUT, 0 }, /* CD */
759         { 0x16, HDA_OUTPUT, 0 }, /* Aux */
760         { 0x17, HDA_OUTPUT, 0 }, /* Line */
761         { } /* end */
762 };
763 #endif
764
765 static int patch_ad1986a(struct hda_codec *codec)
766 {
767         struct ad198x_spec *spec;
768         int board_config;
769
770         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
771         if (spec == NULL)
772                 return -ENOMEM;
773
774         codec->spec = spec;
775
776         spec->multiout.max_channels = 6;
777         spec->multiout.num_dacs = ARRAY_SIZE(ad1986a_dac_nids);
778         spec->multiout.dac_nids = ad1986a_dac_nids;
779         spec->multiout.dig_out_nid = AD1986A_SPDIF_OUT;
780         spec->num_adc_nids = 1;
781         spec->adc_nids = ad1986a_adc_nids;
782         spec->capsrc_nids = ad1986a_capsrc_nids;
783         spec->input_mux = &ad1986a_capture_source;
784         spec->num_mixers = 1;
785         spec->mixers[0] = ad1986a_mixers;
786         spec->num_init_verbs = 1;
787         spec->init_verbs[0] = ad1986a_init_verbs;
788 #ifdef CONFIG_SND_HDA_POWER_SAVE
789         spec->loopback.amplist = ad1986a_loopbacks;
790 #endif
791
792         codec->patch_ops = ad198x_patch_ops;
793
794         /* override some parameters */
795         board_config = snd_hda_check_board_config(codec, AD1986A_MODELS,
796                                                   ad1986a_models,
797                                                   ad1986a_cfg_tbl);
798         switch (board_config) {
799         case AD1986A_3STACK:
800                 spec->num_mixers = 2;
801                 spec->mixers[1] = ad1986a_3st_mixers;
802                 spec->num_init_verbs = 2;
803                 spec->init_verbs[1] = ad1986a_ch2_init;
804                 spec->channel_mode = ad1986a_modes;
805                 spec->num_channel_mode = ARRAY_SIZE(ad1986a_modes);
806                 spec->need_dac_fix = 1;
807                 spec->multiout.max_channels = 2;
808                 spec->multiout.num_dacs = 1;
809                 break;
810         case AD1986A_LAPTOP:
811                 spec->mixers[0] = ad1986a_laptop_mixers;
812                 spec->multiout.max_channels = 2;
813                 spec->multiout.num_dacs = 1;
814                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
815                 break;
816         case AD1986A_LAPTOP_EAPD:
817                 spec->mixers[0] = ad1986a_laptop_eapd_mixers;
818                 spec->num_init_verbs = 2;
819                 spec->init_verbs[1] = ad1986a_eapd_init_verbs;
820                 spec->multiout.max_channels = 2;
821                 spec->multiout.num_dacs = 1;
822                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
823                 spec->multiout.dig_out_nid = 0;
824                 spec->input_mux = &ad1986a_laptop_eapd_capture_source;
825                 break;
826         case AD1986A_ULTRA:
827                 spec->mixers[0] = ad1986a_laptop_eapd_mixers;
828                 spec->num_init_verbs = 2;
829                 spec->init_verbs[1] = ad1986a_ultra_init;
830                 spec->multiout.max_channels = 2;
831                 spec->multiout.num_dacs = 1;
832                 spec->multiout.dac_nids = ad1986a_laptop_dac_nids;
833                 spec->multiout.dig_out_nid = 0;
834                 break;
835         }
836
837         return 0;
838 }
839
840 /*
841  * AD1983 specific
842  */
843
844 #define AD1983_SPDIF_OUT        0x02
845 #define AD1983_DAC              0x03
846 #define AD1983_ADC              0x04
847
848 static hda_nid_t ad1983_dac_nids[1] = { AD1983_DAC };
849 static hda_nid_t ad1983_adc_nids[1] = { AD1983_ADC };
850 static hda_nid_t ad1983_capsrc_nids[1] = { 0x15 };
851
852 static struct hda_input_mux ad1983_capture_source = {
853         .num_items = 4,
854         .items = {
855                 { "Mic", 0x0 },
856                 { "Line", 0x1 },
857                 { "Mix", 0x2 },
858                 { "Mix Mono", 0x3 },
859         },
860 };
861
862 /*
863  * SPDIF playback route
864  */
865 static int ad1983_spdif_route_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
866 {
867         static char *texts[] = { "PCM", "ADC" };
868
869         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
870         uinfo->count = 1;
871         uinfo->value.enumerated.items = 2;
872         if (uinfo->value.enumerated.item > 1)
873                 uinfo->value.enumerated.item = 1;
874         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
875         return 0;
876 }
877
878 static int ad1983_spdif_route_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
879 {
880         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
881         struct ad198x_spec *spec = codec->spec;
882
883         ucontrol->value.enumerated.item[0] = spec->spdif_route;
884         return 0;
885 }
886
887 static int ad1983_spdif_route_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
888 {
889         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
890         struct ad198x_spec *spec = codec->spec;
891
892         if (spec->spdif_route != ucontrol->value.enumerated.item[0]) {
893                 spec->spdif_route = ucontrol->value.enumerated.item[0];
894                 snd_hda_codec_write_cache(codec, spec->multiout.dig_out_nid, 0,
895                                           AC_VERB_SET_CONNECT_SEL,
896                                           spec->spdif_route);
897                 return 1;
898         }
899         return 0;
900 }
901
902 static struct snd_kcontrol_new ad1983_mixers[] = {
903         HDA_CODEC_VOLUME("Front Playback Volume", 0x05, 0x0, HDA_OUTPUT),
904         HDA_CODEC_MUTE("Front Playback Switch", 0x05, 0x0, HDA_OUTPUT),
905         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x06, 0x0, HDA_OUTPUT),
906         HDA_CODEC_MUTE("Headphone Playback Switch", 0x06, 0x0, HDA_OUTPUT),
907         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x07, 1, 0x0, HDA_OUTPUT),
908         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x07, 1, 0x0, HDA_OUTPUT),
909         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
910         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
911         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
912         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
913         HDA_CODEC_VOLUME("Line Playback Volume", 0x13, 0x0, HDA_OUTPUT),
914         HDA_CODEC_MUTE("Line Playback Switch", 0x13, 0x0, HDA_OUTPUT),
915         HDA_CODEC_VOLUME_MONO("PC Speaker Playback Volume", 0x10, 1, 0x0, HDA_OUTPUT),
916         HDA_CODEC_MUTE_MONO("PC Speaker Playback Switch", 0x10, 1, 0x0, HDA_OUTPUT),
917         HDA_CODEC_VOLUME("Mic Boost", 0x0c, 0x0, HDA_OUTPUT),
918         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
919         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
920         {
921                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
922                 .name = "Capture Source",
923                 .info = ad198x_mux_enum_info,
924                 .get = ad198x_mux_enum_get,
925                 .put = ad198x_mux_enum_put,
926         },
927         {
928                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
929                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
930                 .info = ad1983_spdif_route_info,
931                 .get = ad1983_spdif_route_get,
932                 .put = ad1983_spdif_route_put,
933         },
934         { } /* end */
935 };
936
937 static struct hda_verb ad1983_init_verbs[] = {
938         /* Front, HP, Mono; mute as default */
939         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
940         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
941         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
942         /* Beep, PCM, Mic, Line-In: mute */
943         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
944         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
945         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
946         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
947         /* Front, HP selectors; from Mix */
948         {0x05, AC_VERB_SET_CONNECT_SEL, 0x01},
949         {0x06, AC_VERB_SET_CONNECT_SEL, 0x01},
950         /* Mono selector; from Mix */
951         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x03},
952         /* Mic selector; Mic */
953         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x0},
954         /* Line-in selector: Line-in */
955         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x0},
956         /* Mic boost: 0dB */
957         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
958         /* Record selector: mic */
959         {0x15, AC_VERB_SET_CONNECT_SEL, 0x0},
960         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
961         /* SPDIF route: PCM */
962         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0},
963         /* Front Pin */
964         {0x05, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
965         /* HP Pin */
966         {0x06, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
967         /* Mono Pin */
968         {0x07, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
969         /* Mic Pin */
970         {0x08, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
971         /* Line Pin */
972         {0x09, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
973         { } /* end */
974 };
975
976 #ifdef CONFIG_SND_HDA_POWER_SAVE
977 static struct hda_amp_list ad1983_loopbacks[] = {
978         { 0x12, HDA_OUTPUT, 0 }, /* Mic */
979         { 0x13, HDA_OUTPUT, 0 }, /* Line */
980         { } /* end */
981 };
982 #endif
983
984 static int patch_ad1983(struct hda_codec *codec)
985 {
986         struct ad198x_spec *spec;
987
988         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
989         if (spec == NULL)
990                 return -ENOMEM;
991
992         codec->spec = spec;
993
994         spec->multiout.max_channels = 2;
995         spec->multiout.num_dacs = ARRAY_SIZE(ad1983_dac_nids);
996         spec->multiout.dac_nids = ad1983_dac_nids;
997         spec->multiout.dig_out_nid = AD1983_SPDIF_OUT;
998         spec->num_adc_nids = 1;
999         spec->adc_nids = ad1983_adc_nids;
1000         spec->capsrc_nids = ad1983_capsrc_nids;
1001         spec->input_mux = &ad1983_capture_source;
1002         spec->num_mixers = 1;
1003         spec->mixers[0] = ad1983_mixers;
1004         spec->num_init_verbs = 1;
1005         spec->init_verbs[0] = ad1983_init_verbs;
1006         spec->spdif_route = 0;
1007 #ifdef CONFIG_SND_HDA_POWER_SAVE
1008         spec->loopback.amplist = ad1983_loopbacks;
1009 #endif
1010
1011         codec->patch_ops = ad198x_patch_ops;
1012
1013         return 0;
1014 }
1015
1016
1017 /*
1018  * AD1981 HD specific
1019  */
1020
1021 #define AD1981_SPDIF_OUT        0x02
1022 #define AD1981_DAC              0x03
1023 #define AD1981_ADC              0x04
1024
1025 static hda_nid_t ad1981_dac_nids[1] = { AD1981_DAC };
1026 static hda_nid_t ad1981_adc_nids[1] = { AD1981_ADC };
1027 static hda_nid_t ad1981_capsrc_nids[1] = { 0x15 };
1028
1029 /* 0x0c, 0x09, 0x0e, 0x0f, 0x19, 0x05, 0x18, 0x17 */
1030 static struct hda_input_mux ad1981_capture_source = {
1031         .num_items = 7,
1032         .items = {
1033                 { "Front Mic", 0x0 },
1034                 { "Line", 0x1 },
1035                 { "Mix", 0x2 },
1036                 { "Mix Mono", 0x3 },
1037                 { "CD", 0x4 },
1038                 { "Mic", 0x6 },
1039                 { "Aux", 0x7 },
1040         },
1041 };
1042
1043 static struct snd_kcontrol_new ad1981_mixers[] = {
1044         HDA_CODEC_VOLUME("Front Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1045         HDA_CODEC_MUTE("Front Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1046         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1047         HDA_CODEC_MUTE("Headphone Playback Switch", 0x06, 0x0, HDA_OUTPUT),
1048         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x07, 1, 0x0, HDA_OUTPUT),
1049         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x07, 1, 0x0, HDA_OUTPUT),
1050         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1051         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1052         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1053         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1054         HDA_CODEC_VOLUME("Line Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1055         HDA_CODEC_MUTE("Line Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1056         HDA_CODEC_VOLUME("Aux Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
1057         HDA_CODEC_MUTE("Aux Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
1058         HDA_CODEC_VOLUME("Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
1059         HDA_CODEC_MUTE("Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
1060         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1061         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1062         HDA_CODEC_VOLUME_MONO("PC Speaker Playback Volume", 0x0d, 1, 0x0, HDA_OUTPUT),
1063         HDA_CODEC_MUTE_MONO("PC Speaker Playback Switch", 0x0d, 1, 0x0, HDA_OUTPUT),
1064         HDA_CODEC_VOLUME("Front Mic Boost", 0x08, 0x0, HDA_INPUT),
1065         HDA_CODEC_VOLUME("Mic Boost", 0x18, 0x0, HDA_INPUT),
1066         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1067         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1068         {
1069                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1070                 .name = "Capture Source",
1071                 .info = ad198x_mux_enum_info,
1072                 .get = ad198x_mux_enum_get,
1073                 .put = ad198x_mux_enum_put,
1074         },
1075         /* identical with AD1983 */
1076         {
1077                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1078                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1079                 .info = ad1983_spdif_route_info,
1080                 .get = ad1983_spdif_route_get,
1081                 .put = ad1983_spdif_route_put,
1082         },
1083         { } /* end */
1084 };
1085
1086 static struct hda_verb ad1981_init_verbs[] = {
1087         /* Front, HP, Mono; mute as default */
1088         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1089         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1090         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1091         /* Beep, PCM, Front Mic, Line, Rear Mic, Aux, CD-In: mute */
1092         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1093         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1094         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1095         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1096         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1097         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1098         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1099         /* Front, HP selectors; from Mix */
1100         {0x05, AC_VERB_SET_CONNECT_SEL, 0x01},
1101         {0x06, AC_VERB_SET_CONNECT_SEL, 0x01},
1102         /* Mono selector; from Mix */
1103         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x03},
1104         /* Mic Mixer; select Front Mic */
1105         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1106         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1107         /* Mic boost: 0dB */
1108         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1109         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1110         /* Record selector: Front mic */
1111         {0x15, AC_VERB_SET_CONNECT_SEL, 0x0},
1112         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1113         /* SPDIF route: PCM */
1114         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0},
1115         /* Front Pin */
1116         {0x05, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1117         /* HP Pin */
1118         {0x06, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
1119         /* Mono Pin */
1120         {0x07, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
1121         /* Front & Rear Mic Pins */
1122         {0x08, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1123         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
1124         /* Line Pin */
1125         {0x09, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
1126         /* Digital Beep */
1127         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x00},
1128         /* Line-Out as Input: disabled */
1129         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1130         { } /* end */
1131 };
1132
1133 #ifdef CONFIG_SND_HDA_POWER_SAVE
1134 static struct hda_amp_list ad1981_loopbacks[] = {
1135         { 0x12, HDA_OUTPUT, 0 }, /* Front Mic */
1136         { 0x13, HDA_OUTPUT, 0 }, /* Line */
1137         { 0x1b, HDA_OUTPUT, 0 }, /* Aux */
1138         { 0x1c, HDA_OUTPUT, 0 }, /* Mic */
1139         { 0x1d, HDA_OUTPUT, 0 }, /* CD */
1140         { } /* end */
1141 };
1142 #endif
1143
1144 /*
1145  * Patch for HP nx6320
1146  *
1147  * nx6320 uses EAPD in the reverse way - EAPD-on means the internal
1148  * speaker output enabled _and_ mute-LED off.
1149  */
1150
1151 #define AD1981_HP_EVENT         0x37
1152 #define AD1981_MIC_EVENT        0x38
1153
1154 static struct hda_verb ad1981_hp_init_verbs[] = {
1155         {0x05, AC_VERB_SET_EAPD_BTLENABLE, 0x00 }, /* default off */
1156         /* pin sensing on HP and Mic jacks */
1157         {0x06, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_HP_EVENT},
1158         {0x08, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_MIC_EVENT},
1159         {}
1160 };
1161
1162 /* turn on/off EAPD (+ mute HP) as a master switch */
1163 static int ad1981_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1164                                    struct snd_ctl_elem_value *ucontrol)
1165 {
1166         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1167         struct ad198x_spec *spec = codec->spec;
1168
1169         if (! ad198x_eapd_put(kcontrol, ucontrol))
1170                 return 0;
1171
1172         /* toggle HP mute appropriately */
1173         snd_hda_codec_amp_stereo(codec, 0x06, HDA_OUTPUT, 0,
1174                                  HDA_AMP_MUTE,
1175                                  spec->cur_eapd ? 0 : HDA_AMP_MUTE);
1176         return 1;
1177 }
1178
1179 /* bind volumes of both NID 0x05 and 0x06 */
1180 static struct hda_bind_ctls ad1981_hp_bind_master_vol = {
1181         .ops = &snd_hda_bind_vol,
1182         .values = {
1183                 HDA_COMPOSE_AMP_VAL(0x05, 3, 0, HDA_OUTPUT),
1184                 HDA_COMPOSE_AMP_VAL(0x06, 3, 0, HDA_OUTPUT),
1185                 0
1186         },
1187 };
1188
1189 /* mute internal speaker if HP is plugged */
1190 static void ad1981_hp_automute(struct hda_codec *codec)
1191 {
1192         unsigned int present;
1193
1194         present = snd_hda_codec_read(codec, 0x06, 0,
1195                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1196         snd_hda_codec_amp_stereo(codec, 0x05, HDA_OUTPUT, 0,
1197                                  HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
1198 }
1199
1200 /* toggle input of built-in and mic jack appropriately */
1201 static void ad1981_hp_automic(struct hda_codec *codec)
1202 {
1203         static struct hda_verb mic_jack_on[] = {
1204                 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1205                 {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1206                 {}
1207         };
1208         static struct hda_verb mic_jack_off[] = {
1209                 {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
1210                 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
1211                 {}
1212         };
1213         unsigned int present;
1214
1215         present = snd_hda_codec_read(codec, 0x08, 0,
1216                                  AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1217         if (present)
1218                 snd_hda_sequence_write(codec, mic_jack_on);
1219         else
1220                 snd_hda_sequence_write(codec, mic_jack_off);
1221 }
1222
1223 /* unsolicited event for HP jack sensing */
1224 static void ad1981_hp_unsol_event(struct hda_codec *codec,
1225                                   unsigned int res)
1226 {
1227         res >>= 26;
1228         switch (res) {
1229         case AD1981_HP_EVENT:
1230                 ad1981_hp_automute(codec);
1231                 break;
1232         case AD1981_MIC_EVENT:
1233                 ad1981_hp_automic(codec);
1234                 break;
1235         }
1236 }
1237
1238 static struct hda_input_mux ad1981_hp_capture_source = {
1239         .num_items = 3,
1240         .items = {
1241                 { "Mic", 0x0 },
1242                 { "Docking-Station", 0x1 },
1243                 { "Mix", 0x2 },
1244         },
1245 };
1246
1247 static struct snd_kcontrol_new ad1981_hp_mixers[] = {
1248         HDA_BIND_VOL("Master Playback Volume", &ad1981_hp_bind_master_vol),
1249         {
1250                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1251                 .name = "Master Playback Switch",
1252                 .info = ad198x_eapd_info,
1253                 .get = ad198x_eapd_get,
1254                 .put = ad1981_hp_master_sw_put,
1255                 .private_value = 0x05,
1256         },
1257         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1258         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1259 #if 0
1260         /* FIXME: analog mic/line loopback doesn't work with my tests...
1261          *        (although recording is OK)
1262          */
1263         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1264         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1265         HDA_CODEC_VOLUME("Docking-Station Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1266         HDA_CODEC_MUTE("Docking-Station Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1267         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x1c, 0x0, HDA_OUTPUT),
1268         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x1c, 0x0, HDA_OUTPUT),
1269         /* FIXME: does this laptop have analog CD connection? */
1270         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1271         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1272 #endif
1273         HDA_CODEC_VOLUME("Mic Boost", 0x08, 0x0, HDA_INPUT),
1274         HDA_CODEC_VOLUME("Internal Mic Boost", 0x18, 0x0, HDA_INPUT),
1275         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1276         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1277         {
1278                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1279                 .name = "Capture Source",
1280                 .info = ad198x_mux_enum_info,
1281                 .get = ad198x_mux_enum_get,
1282                 .put = ad198x_mux_enum_put,
1283         },
1284         { } /* end */
1285 };
1286
1287 /* initialize jack-sensing, too */
1288 static int ad1981_hp_init(struct hda_codec *codec)
1289 {
1290         ad198x_init(codec);
1291         ad1981_hp_automute(codec);
1292         ad1981_hp_automic(codec);
1293         return 0;
1294 }
1295
1296 /* configuration for Toshiba Laptops */
1297 static struct hda_verb ad1981_toshiba_init_verbs[] = {
1298         {0x05, AC_VERB_SET_EAPD_BTLENABLE, 0x01 }, /* default on */
1299         /* pin sensing on HP and Mic jacks */
1300         {0x06, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_HP_EVENT},
1301         {0x08, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1981_MIC_EVENT},
1302         {}
1303 };
1304
1305 static struct snd_kcontrol_new ad1981_toshiba_mixers[] = {
1306         HDA_CODEC_VOLUME("Amp Volume", 0x1a, 0x0, HDA_OUTPUT),
1307         HDA_CODEC_MUTE("Amp Switch", 0x1a, 0x0, HDA_OUTPUT),
1308         { }
1309 };
1310
1311 /* configuration for Lenovo Thinkpad T60 */
1312 static struct snd_kcontrol_new ad1981_thinkpad_mixers[] = {
1313         HDA_CODEC_VOLUME("Master Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1314         HDA_CODEC_MUTE("Master Playback Switch", 0x05, 0x0, HDA_OUTPUT),
1315         HDA_CODEC_VOLUME("PCM Playback Volume", 0x11, 0x0, HDA_OUTPUT),
1316         HDA_CODEC_MUTE("PCM Playback Switch", 0x11, 0x0, HDA_OUTPUT),
1317         HDA_CODEC_VOLUME("Mic Playback Volume", 0x12, 0x0, HDA_OUTPUT),
1318         HDA_CODEC_MUTE("Mic Playback Switch", 0x12, 0x0, HDA_OUTPUT),
1319         HDA_CODEC_VOLUME("CD Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1320         HDA_CODEC_MUTE("CD Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1321         HDA_CODEC_VOLUME("Mic Boost", 0x08, 0x0, HDA_INPUT),
1322         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
1323         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_OUTPUT),
1324         {
1325                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1326                 .name = "Capture Source",
1327                 .info = ad198x_mux_enum_info,
1328                 .get = ad198x_mux_enum_get,
1329                 .put = ad198x_mux_enum_put,
1330         },
1331         /* identical with AD1983 */
1332         {
1333                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1334                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1335                 .info = ad1983_spdif_route_info,
1336                 .get = ad1983_spdif_route_get,
1337                 .put = ad1983_spdif_route_put,
1338         },
1339         { } /* end */
1340 };
1341
1342 static struct hda_input_mux ad1981_thinkpad_capture_source = {
1343         .num_items = 3,
1344         .items = {
1345                 { "Mic", 0x0 },
1346                 { "Mix", 0x2 },
1347                 { "CD", 0x4 },
1348         },
1349 };
1350
1351 /* models */
1352 enum {
1353         AD1981_BASIC,
1354         AD1981_HP,
1355         AD1981_THINKPAD,
1356         AD1981_TOSHIBA,
1357         AD1981_MODELS
1358 };
1359
1360 static const char *ad1981_models[AD1981_MODELS] = {
1361         [AD1981_HP]             = "hp",
1362         [AD1981_THINKPAD]       = "thinkpad",
1363         [AD1981_BASIC]          = "basic",
1364         [AD1981_TOSHIBA]        = "toshiba"
1365 };
1366
1367 static struct snd_pci_quirk ad1981_cfg_tbl[] = {
1368         /* All HP models */
1369         SND_PCI_QUIRK(0x103c, 0, "HP nx", AD1981_HP),
1370         /* HP nx6320 (reversed SSID, H/W bug) */
1371         SND_PCI_QUIRK(0x30b0, 0x103c, "HP nx6320", AD1981_HP),
1372         /* Lenovo Thinkpad T60/X60/Z6xx */
1373         SND_PCI_QUIRK(0x17aa, 0, "Lenovo Thinkpad", AD1981_THINKPAD),
1374         SND_PCI_QUIRK(0x1014, 0x0597, "Lenovo Z60", AD1981_THINKPAD),
1375         SND_PCI_QUIRK(0x1179, 0x0001, "Toshiba U205", AD1981_TOSHIBA),
1376         {}
1377 };
1378
1379 static int patch_ad1981(struct hda_codec *codec)
1380 {
1381         struct ad198x_spec *spec;
1382         int board_config;
1383
1384         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1385         if (spec == NULL)
1386                 return -ENOMEM;
1387
1388         codec->spec = spec;
1389
1390         spec->multiout.max_channels = 2;
1391         spec->multiout.num_dacs = ARRAY_SIZE(ad1981_dac_nids);
1392         spec->multiout.dac_nids = ad1981_dac_nids;
1393         spec->multiout.dig_out_nid = AD1981_SPDIF_OUT;
1394         spec->num_adc_nids = 1;
1395         spec->adc_nids = ad1981_adc_nids;
1396         spec->capsrc_nids = ad1981_capsrc_nids;
1397         spec->input_mux = &ad1981_capture_source;
1398         spec->num_mixers = 1;
1399         spec->mixers[0] = ad1981_mixers;
1400         spec->num_init_verbs = 1;
1401         spec->init_verbs[0] = ad1981_init_verbs;
1402         spec->spdif_route = 0;
1403 #ifdef CONFIG_SND_HDA_POWER_SAVE
1404         spec->loopback.amplist = ad1981_loopbacks;
1405 #endif
1406
1407         codec->patch_ops = ad198x_patch_ops;
1408
1409         /* override some parameters */
1410         board_config = snd_hda_check_board_config(codec, AD1981_MODELS,
1411                                                   ad1981_models,
1412                                                   ad1981_cfg_tbl);
1413         switch (board_config) {
1414         case AD1981_HP:
1415                 spec->mixers[0] = ad1981_hp_mixers;
1416                 spec->num_init_verbs = 2;
1417                 spec->init_verbs[1] = ad1981_hp_init_verbs;
1418                 spec->multiout.dig_out_nid = 0;
1419                 spec->input_mux = &ad1981_hp_capture_source;
1420
1421                 codec->patch_ops.init = ad1981_hp_init;
1422                 codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
1423                 break;
1424         case AD1981_THINKPAD:
1425                 spec->mixers[0] = ad1981_thinkpad_mixers;
1426                 spec->input_mux = &ad1981_thinkpad_capture_source;
1427                 break;
1428         case AD1981_TOSHIBA:
1429                 spec->mixers[0] = ad1981_hp_mixers;
1430                 spec->mixers[1] = ad1981_toshiba_mixers;
1431                 spec->num_init_verbs = 2;
1432                 spec->init_verbs[1] = ad1981_toshiba_init_verbs;
1433                 spec->multiout.dig_out_nid = 0;
1434                 spec->input_mux = &ad1981_hp_capture_source;
1435                 codec->patch_ops.init = ad1981_hp_init;
1436                 codec->patch_ops.unsol_event = ad1981_hp_unsol_event;
1437                 break;
1438         }
1439         return 0;
1440 }
1441
1442
1443 /*
1444  * AD1988
1445  *
1446  * Output pins and routes
1447  *
1448  *        Pin               Mix     Sel     DAC (*)
1449  * port-A 0x11 (mute/hp) <- 0x22 <- 0x37 <- 03/04/06
1450  * port-B 0x14 (mute/hp) <- 0x2b <- 0x30 <- 03/04/06
1451  * port-C 0x15 (mute)    <- 0x2c <- 0x31 <- 05/0a
1452  * port-D 0x12 (mute/hp) <- 0x29         <- 04
1453  * port-E 0x17 (mute/hp) <- 0x26 <- 0x32 <- 05/0a
1454  * port-F 0x16 (mute)    <- 0x2a         <- 06
1455  * port-G 0x24 (mute)    <- 0x27         <- 05
1456  * port-H 0x25 (mute)    <- 0x28         <- 0a
1457  * mono   0x13 (mute/amp)<- 0x1e <- 0x36 <- 03/04/06
1458  *
1459  * DAC0 = 03h, DAC1 = 04h, DAC2 = 05h, DAC3 = 06h, DAC4 = 0ah
1460  * (*) DAC2/3/4 are swapped to DAC3/4/2 on AD198A rev.2 due to a h/w bug.
1461  *
1462  * Input pins and routes
1463  *
1464  *        pin     boost   mix input # / adc input #
1465  * port-A 0x11 -> 0x38 -> mix 2, ADC 0
1466  * port-B 0x14 -> 0x39 -> mix 0, ADC 1
1467  * port-C 0x15 -> 0x3a -> 33:0 - mix 1, ADC 2
1468  * port-D 0x12 -> 0x3d -> mix 3, ADC 8
1469  * port-E 0x17 -> 0x3c -> 34:0 - mix 4, ADC 4
1470  * port-F 0x16 -> 0x3b -> mix 5, ADC 3
1471  * port-G 0x24 -> N/A  -> 33:1 - mix 1, 34:1 - mix 4, ADC 6
1472  * port-H 0x25 -> N/A  -> 33:2 - mix 1, 34:2 - mix 4, ADC 7
1473  *
1474  *
1475  * DAC assignment
1476  *   6stack - front/surr/CLFE/side/opt DACs - 04/06/05/0a/03
1477  *   3stack - front/surr/CLFE/opt DACs - 04/05/0a/03
1478  *
1479  * Inputs of Analog Mix (0x20)
1480  *   0:Port-B (front mic)
1481  *   1:Port-C/G/H (line-in)
1482  *   2:Port-A
1483  *   3:Port-D (line-in/2)
1484  *   4:Port-E/G/H (mic-in)
1485  *   5:Port-F (mic2-in)
1486  *   6:CD
1487  *   7:Beep
1488  *
1489  * ADC selection
1490  *   0:Port-A
1491  *   1:Port-B (front mic-in)
1492  *   2:Port-C (line-in)
1493  *   3:Port-F (mic2-in)
1494  *   4:Port-E (mic-in)
1495  *   5:CD
1496  *   6:Port-G
1497  *   7:Port-H
1498  *   8:Port-D (line-in/2)
1499  *   9:Mix
1500  *
1501  * Proposed pin assignments by the datasheet
1502  *
1503  * 6-stack
1504  * Port-A front headphone
1505  *      B front mic-in
1506  *      C rear line-in
1507  *      D rear front-out
1508  *      E rear mic-in
1509  *      F rear surround
1510  *      G rear CLFE
1511  *      H rear side
1512  *
1513  * 3-stack
1514  * Port-A front headphone
1515  *      B front mic
1516  *      C rear line-in/surround
1517  *      D rear front-out
1518  *      E rear mic-in/CLFE
1519  *
1520  * laptop
1521  * Port-A headphone
1522  *      B mic-in
1523  *      C docking station
1524  *      D internal speaker (with EAPD)
1525  *      E/F quad mic array
1526  */
1527
1528
1529 /* models */
1530 enum {
1531         AD1988_6STACK,
1532         AD1988_6STACK_DIG,
1533         AD1988_3STACK,
1534         AD1988_3STACK_DIG,
1535         AD1988_LAPTOP,
1536         AD1988_LAPTOP_DIG,
1537         AD1988_AUTO,
1538         AD1988_MODEL_LAST,
1539 };
1540
1541 /* reivision id to check workarounds */
1542 #define AD1988A_REV2            0x100200
1543
1544 #define is_rev2(codec) \
1545         ((codec)->vendor_id == 0x11d41988 && \
1546          (codec)->revision_id == AD1988A_REV2)
1547
1548 /*
1549  * mixers
1550  */
1551
1552 static hda_nid_t ad1988_6stack_dac_nids[4] = {
1553         0x04, 0x06, 0x05, 0x0a
1554 };
1555
1556 static hda_nid_t ad1988_3stack_dac_nids[3] = {
1557         0x04, 0x05, 0x0a
1558 };
1559
1560 /* for AD1988A revision-2, DAC2-4 are swapped */
1561 static hda_nid_t ad1988_6stack_dac_nids_rev2[4] = {
1562         0x04, 0x05, 0x0a, 0x06
1563 };
1564
1565 static hda_nid_t ad1988_3stack_dac_nids_rev2[3] = {
1566         0x04, 0x0a, 0x06
1567 };
1568
1569 static hda_nid_t ad1988_adc_nids[3] = {
1570         0x08, 0x09, 0x0f
1571 };
1572
1573 static hda_nid_t ad1988_capsrc_nids[3] = {
1574         0x0c, 0x0d, 0x0e
1575 };
1576
1577 #define AD1988_SPDIF_OUT        0x02
1578 #define AD1988_SPDIF_IN         0x07
1579
1580 static struct hda_input_mux ad1988_6stack_capture_source = {
1581         .num_items = 5,
1582         .items = {
1583                 { "Front Mic", 0x0 },
1584                 { "Line", 0x1 },
1585                 { "Mic", 0x4 },
1586                 { "CD", 0x5 },
1587                 { "Mix", 0x9 },
1588         },
1589 };
1590
1591 static struct hda_input_mux ad1988_laptop_capture_source = {
1592         .num_items = 3,
1593         .items = {
1594                 { "Mic/Line", 0x0 },
1595                 { "CD", 0x5 },
1596                 { "Mix", 0x9 },
1597         },
1598 };
1599
1600 /*
1601  */
1602 static int ad198x_ch_mode_info(struct snd_kcontrol *kcontrol,
1603                                struct snd_ctl_elem_info *uinfo)
1604 {
1605         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1606         struct ad198x_spec *spec = codec->spec;
1607         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
1608                                     spec->num_channel_mode);
1609 }
1610
1611 static int ad198x_ch_mode_get(struct snd_kcontrol *kcontrol,
1612                               struct snd_ctl_elem_value *ucontrol)
1613 {
1614         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1615         struct ad198x_spec *spec = codec->spec;
1616         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
1617                                    spec->num_channel_mode, spec->multiout.max_channels);
1618 }
1619
1620 static int ad198x_ch_mode_put(struct snd_kcontrol *kcontrol,
1621                               struct snd_ctl_elem_value *ucontrol)
1622 {
1623         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1624         struct ad198x_spec *spec = codec->spec;
1625         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
1626                                       spec->num_channel_mode,
1627                                       &spec->multiout.max_channels);
1628         if (err >= 0 && spec->need_dac_fix)
1629                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1630         return err;
1631 }
1632
1633 /* 6-stack mode */
1634 static struct snd_kcontrol_new ad1988_6stack_mixers1[] = {
1635         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1636         HDA_CODEC_VOLUME("Surround Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1637         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
1638         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
1639         HDA_CODEC_VOLUME("Side Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1640         { } /* end */
1641 };
1642
1643 static struct snd_kcontrol_new ad1988_6stack_mixers1_rev2[] = {
1644         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1645         HDA_CODEC_VOLUME("Surround Playback Volume", 0x05, 0x0, HDA_OUTPUT),
1646         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0a, 1, 0x0, HDA_OUTPUT),
1647         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0a, 2, 0x0, HDA_OUTPUT),
1648         HDA_CODEC_VOLUME("Side Playback Volume", 0x06, 0x0, HDA_OUTPUT),
1649         { } /* end */
1650 };
1651
1652 static struct snd_kcontrol_new ad1988_6stack_mixers2[] = {
1653         HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
1654         HDA_BIND_MUTE("Surround Playback Switch", 0x2a, 2, HDA_INPUT),
1655         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x27, 1, 2, HDA_INPUT),
1656         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x27, 2, 2, HDA_INPUT),
1657         HDA_BIND_MUTE("Side Playback Switch", 0x28, 2, HDA_INPUT),
1658         HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
1659         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1660
1661         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1662         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1663         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1664         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1665         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1666         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1667         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
1668         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),
1669
1670         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1671         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1672
1673         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1674         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1675
1676         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1677         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
1678
1679         { } /* end */
1680 };
1681
1682 /* 3-stack mode */
1683 static struct snd_kcontrol_new ad1988_3stack_mixers1[] = {
1684         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1685         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1686         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
1687         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
1688         { } /* end */
1689 };
1690
1691 static struct snd_kcontrol_new ad1988_3stack_mixers1_rev2[] = {
1692         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1693         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0a, 0x0, HDA_OUTPUT),
1694         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x06, 1, 0x0, HDA_OUTPUT),
1695         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x06, 2, 0x0, HDA_OUTPUT),
1696         { } /* end */
1697 };
1698
1699 static struct snd_kcontrol_new ad1988_3stack_mixers2[] = {
1700         HDA_BIND_MUTE("Front Playback Switch", 0x29, 2, HDA_INPUT),
1701         HDA_BIND_MUTE("Surround Playback Switch", 0x2c, 2, HDA_INPUT),
1702         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x26, 1, 2, HDA_INPUT),
1703         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x26, 2, 2, HDA_INPUT),
1704         HDA_BIND_MUTE("Headphone Playback Switch", 0x22, 2, HDA_INPUT),
1705         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1706
1707         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1708         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1709         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1710         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1711         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1712         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1713         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x4, HDA_INPUT),
1714         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x4, HDA_INPUT),
1715
1716         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1717         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1718
1719         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1720         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1721
1722         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1723         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
1724         {
1725                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1726                 .name = "Channel Mode",
1727                 .info = ad198x_ch_mode_info,
1728                 .get = ad198x_ch_mode_get,
1729                 .put = ad198x_ch_mode_put,
1730         },
1731
1732         { } /* end */
1733 };
1734
1735 /* laptop mode */
1736 static struct snd_kcontrol_new ad1988_laptop_mixers[] = {
1737         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
1738         HDA_CODEC_MUTE("PCM Playback Switch", 0x29, 0x0, HDA_INPUT),
1739         HDA_BIND_MUTE("Mono Playback Switch", 0x1e, 2, HDA_INPUT),
1740
1741         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x6, HDA_INPUT),
1742         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x6, HDA_INPUT),
1743         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x0, HDA_INPUT),
1744         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x0, HDA_INPUT),
1745         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x1, HDA_INPUT),
1746         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x1, HDA_INPUT),
1747
1748         HDA_CODEC_VOLUME("Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
1749         HDA_CODEC_MUTE("Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
1750
1751         HDA_CODEC_VOLUME("Analog Mix Playback Volume", 0x21, 0x0, HDA_OUTPUT),
1752         HDA_CODEC_MUTE("Analog Mix Playback Switch", 0x21, 0x0, HDA_OUTPUT),
1753
1754         HDA_CODEC_VOLUME("Mic Boost", 0x39, 0x0, HDA_OUTPUT),
1755
1756         {
1757                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1758                 .name = "External Amplifier",
1759                 .info = ad198x_eapd_info,
1760                 .get = ad198x_eapd_get,
1761                 .put = ad198x_eapd_put,
1762                 .private_value = 0x12 | (1 << 8), /* port-D, inversed */
1763         },
1764
1765         { } /* end */
1766 };
1767
1768 /* capture */
1769 static struct snd_kcontrol_new ad1988_capture_mixers[] = {
1770         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
1771         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
1772         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
1773         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
1774         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x0e, 0x0, HDA_OUTPUT),
1775         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x0e, 0x0, HDA_OUTPUT),
1776         {
1777                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1778                 /* The multiple "Capture Source" controls confuse alsamixer
1779                  * So call somewhat different..
1780                  * FIXME: the controls appear in the "playback" view!
1781                  */
1782                 /* .name = "Capture Source", */
1783                 .name = "Input Source",
1784                 .count = 3,
1785                 .info = ad198x_mux_enum_info,
1786                 .get = ad198x_mux_enum_get,
1787                 .put = ad198x_mux_enum_put,
1788         },
1789         { } /* end */
1790 };
1791
1792 static int ad1988_spdif_playback_source_info(struct snd_kcontrol *kcontrol,
1793                                              struct snd_ctl_elem_info *uinfo)
1794 {
1795         static char *texts[] = {
1796                 "PCM", "ADC1", "ADC2", "ADC3"
1797         };
1798         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1799         uinfo->count = 1;
1800         uinfo->value.enumerated.items = 4;
1801         if (uinfo->value.enumerated.item >= 4)
1802                 uinfo->value.enumerated.item = 3;
1803         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1804         return 0;
1805 }
1806
1807 static int ad1988_spdif_playback_source_get(struct snd_kcontrol *kcontrol,
1808                                             struct snd_ctl_elem_value *ucontrol)
1809 {
1810         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1811         unsigned int sel;
1812
1813         sel = snd_hda_codec_read(codec, 0x1d, 0, AC_VERB_GET_AMP_GAIN_MUTE,
1814                                  AC_AMP_GET_INPUT);
1815         if (!(sel & 0x80))
1816                 ucontrol->value.enumerated.item[0] = 0;
1817         else {
1818                 sel = snd_hda_codec_read(codec, 0x0b, 0,
1819                                          AC_VERB_GET_CONNECT_SEL, 0);
1820                 if (sel < 3)
1821                         sel++;
1822                 else
1823                         sel = 0;
1824                 ucontrol->value.enumerated.item[0] = sel;
1825         }
1826         return 0;
1827 }
1828
1829 static int ad1988_spdif_playback_source_put(struct snd_kcontrol *kcontrol,
1830                                             struct snd_ctl_elem_value *ucontrol)
1831 {
1832         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1833         unsigned int val, sel;
1834         int change;
1835
1836         val = ucontrol->value.enumerated.item[0];
1837         if (!val) {
1838                 sel = snd_hda_codec_read(codec, 0x1d, 0,
1839                                          AC_VERB_GET_AMP_GAIN_MUTE,
1840                                          AC_AMP_GET_INPUT);
1841                 change = sel & 0x80;
1842                 if (change) {
1843                         snd_hda_codec_write_cache(codec, 0x1d, 0,
1844                                                   AC_VERB_SET_AMP_GAIN_MUTE,
1845                                                   AMP_IN_UNMUTE(0));
1846                         snd_hda_codec_write_cache(codec, 0x1d, 0,
1847                                                   AC_VERB_SET_AMP_GAIN_MUTE,
1848                                                   AMP_IN_MUTE(1));
1849                 }
1850         } else {
1851                 sel = snd_hda_codec_read(codec, 0x1d, 0,
1852                                          AC_VERB_GET_AMP_GAIN_MUTE,
1853                                          AC_AMP_GET_INPUT | 0x01);
1854                 change = sel & 0x80;
1855                 if (change) {
1856                         snd_hda_codec_write_cache(codec, 0x1d, 0,
1857                                                   AC_VERB_SET_AMP_GAIN_MUTE,
1858                                                   AMP_IN_MUTE(0));
1859                         snd_hda_codec_write_cache(codec, 0x1d, 0,
1860                                                   AC_VERB_SET_AMP_GAIN_MUTE,
1861                                                   AMP_IN_UNMUTE(1));
1862                 }
1863                 sel = snd_hda_codec_read(codec, 0x0b, 0,
1864                                          AC_VERB_GET_CONNECT_SEL, 0) + 1;
1865                 change |= sel != val;
1866                 if (change)
1867                         snd_hda_codec_write_cache(codec, 0x0b, 0,
1868                                                   AC_VERB_SET_CONNECT_SEL,
1869                                                   val - 1);
1870         }
1871         return change;
1872 }
1873
1874 static struct snd_kcontrol_new ad1988_spdif_out_mixers[] = {
1875         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
1876         {
1877                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1878                 .name = "IEC958 Playback Source",
1879                 .info = ad1988_spdif_playback_source_info,
1880                 .get = ad1988_spdif_playback_source_get,
1881                 .put = ad1988_spdif_playback_source_put,
1882         },
1883         { } /* end */
1884 };
1885
1886 static struct snd_kcontrol_new ad1988_spdif_in_mixers[] = {
1887         HDA_CODEC_VOLUME("IEC958 Capture Volume", 0x1c, 0x0, HDA_INPUT),
1888         { } /* end */
1889 };
1890
1891
1892 /*
1893  * initialization verbs
1894  */
1895
1896 /*
1897  * for 6-stack (+dig)
1898  */
1899 static struct hda_verb ad1988_6stack_init_verbs[] = {
1900         /* Front, Surround, CLFE, side DAC; unmute as default */
1901         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1902         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1903         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1904         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1905         /* Port-A front headphon path */
1906         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
1907         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1908         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1909         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1910         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1911         /* Port-D line-out path */
1912         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1913         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1914         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1915         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1916         /* Port-F surround path */
1917         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1918         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1919         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1920         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1921         /* Port-G CLFE path */
1922         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1923         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1924         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1925         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1926         /* Port-H side path */
1927         {0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1928         {0x28, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1929         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1930         {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1931         /* Mono out path */
1932         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
1933         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1934         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1935         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1936         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
1937         /* Port-B front mic-in path */
1938         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1939         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1940         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1941         /* Port-C line-in path */
1942         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1943         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1944         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1945         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
1946         /* Port-E mic-in path */
1947         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1948         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1949         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1950         {0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
1951
1952         { }
1953 };
1954
1955 static struct hda_verb ad1988_capture_init_verbs[] = {
1956         /* mute analog mix */
1957         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1958         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1959         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
1960         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
1961         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
1962         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
1963         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
1964         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
1965         /* select ADCs - front-mic */
1966         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
1967         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
1968         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
1969         /* ADCs; muted */
1970         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1971         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1972         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1973
1974         { }
1975 };
1976
1977 static struct hda_verb ad1988_spdif_init_verbs[] = {
1978         /* SPDIF out sel */
1979         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
1980         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x0}, /* ADC1 */
1981         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1982         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1983         /* SPDIF out pin */
1984         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
1985
1986         { }
1987 };
1988
1989 /*
1990  * verbs for 3stack (+dig)
1991  */
1992 static struct hda_verb ad1988_3stack_ch2_init[] = {
1993         /* set port-C to line-in */
1994         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
1995         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1996         /* set port-E to mic-in */
1997         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
1998         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
1999         { } /* end */
2000 };
2001
2002 static struct hda_verb ad1988_3stack_ch6_init[] = {
2003         /* set port-C to surround out */
2004         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2005         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2006         /* set port-E to CLFE out */
2007         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2008         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2009         { } /* end */
2010 };
2011
2012 static struct hda_channel_mode ad1988_3stack_modes[2] = {
2013         { 2, ad1988_3stack_ch2_init },
2014         { 6, ad1988_3stack_ch6_init },
2015 };
2016
2017 static struct hda_verb ad1988_3stack_init_verbs[] = {
2018         /* Front, Surround, CLFE, side DAC; unmute as default */
2019         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2020         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2021         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2022         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2023         /* Port-A front headphon path */
2024         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
2025         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2026         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2027         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2028         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2029         /* Port-D line-out path */
2030         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2031         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2032         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2033         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2034         /* Mono out path */
2035         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2036         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2037         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2038         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2039         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2040         /* Port-B front mic-in path */
2041         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2042         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2043         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2044         /* Port-C line-in/surround path - 6ch mode as default */
2045         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2046         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2047         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2048         {0x31, AC_VERB_SET_CONNECT_SEL, 0x0}, /* output sel: DAC 0x05 */
2049         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2050         /* Port-E mic-in/CLFE path - 6ch mode as default */
2051         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2052         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2053         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2054         {0x32, AC_VERB_SET_CONNECT_SEL, 0x1}, /* output sel: DAC 0x0a */
2055         {0x34, AC_VERB_SET_CONNECT_SEL, 0x0},
2056         /* mute analog mix */
2057         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2058         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2059         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2060         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2061         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2062         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2063         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2064         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2065         /* select ADCs - front-mic */
2066         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2067         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2068         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2069         /* ADCs; muted */
2070         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2071         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2072         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2073         { }
2074 };
2075
2076 /*
2077  * verbs for laptop mode (+dig)
2078  */
2079 static struct hda_verb ad1988_laptop_hp_on[] = {
2080         /* unmute port-A and mute port-D */
2081         { 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2082         { 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2083         { } /* end */
2084 };
2085 static struct hda_verb ad1988_laptop_hp_off[] = {
2086         /* mute port-A and unmute port-D */
2087         { 0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2088         { 0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2089         { } /* end */
2090 };
2091
2092 #define AD1988_HP_EVENT 0x01
2093
2094 static struct hda_verb ad1988_laptop_init_verbs[] = {
2095         /* Front, Surround, CLFE, side DAC; unmute as default */
2096         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2097         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2098         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2099         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2100         /* Port-A front headphon path */
2101         {0x37, AC_VERB_SET_CONNECT_SEL, 0x01}, /* DAC1:04h */
2102         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2103         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2104         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2105         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2106         /* unsolicited event for pin-sense */
2107         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | AD1988_HP_EVENT },
2108         /* Port-D line-out path + EAPD */
2109         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2110         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2111         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2112         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2113         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x00}, /* EAPD-off */
2114         /* Mono out path */
2115         {0x36, AC_VERB_SET_CONNECT_SEL, 0x1}, /* DAC1:04h */
2116         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2117         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2118         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2119         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, 0xb01f}, /* unmute, 0dB */
2120         /* Port-B mic-in path */
2121         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2122         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2123         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2124         /* Port-C docking station - try to output */
2125         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2126         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2127         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2128         {0x33, AC_VERB_SET_CONNECT_SEL, 0x0},
2129         /* mute analog mix */
2130         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2131         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2132         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2133         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2134         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2135         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
2136         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
2137         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
2138         /* select ADCs - mic */
2139         {0x0c, AC_VERB_SET_CONNECT_SEL, 0x1},
2140         {0x0d, AC_VERB_SET_CONNECT_SEL, 0x1},
2141         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2142         /* ADCs; muted */
2143         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2144         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2145         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2146         { }
2147 };
2148
2149 static void ad1988_laptop_unsol_event(struct hda_codec *codec, unsigned int res)
2150 {
2151         if ((res >> 26) != AD1988_HP_EVENT)
2152                 return;
2153         if (snd_hda_codec_read(codec, 0x11, 0, AC_VERB_GET_PIN_SENSE, 0) & (1 << 31))
2154                 snd_hda_sequence_write(codec, ad1988_laptop_hp_on);
2155         else
2156                 snd_hda_sequence_write(codec, ad1988_laptop_hp_off);
2157
2158
2159 #ifdef CONFIG_SND_HDA_POWER_SAVE
2160 static struct hda_amp_list ad1988_loopbacks[] = {
2161         { 0x20, HDA_INPUT, 0 }, /* Front Mic */
2162         { 0x20, HDA_INPUT, 1 }, /* Line */
2163         { 0x20, HDA_INPUT, 4 }, /* Mic */
2164         { 0x20, HDA_INPUT, 6 }, /* CD */
2165         { } /* end */
2166 };
2167 #endif
2168
2169 /*
2170  * Automatic parse of I/O pins from the BIOS configuration
2171  */
2172
2173 #define NUM_CONTROL_ALLOC       32
2174 #define NUM_VERB_ALLOC          32
2175
2176 enum {
2177         AD_CTL_WIDGET_VOL,
2178         AD_CTL_WIDGET_MUTE,
2179         AD_CTL_BIND_MUTE,
2180 };
2181 static struct snd_kcontrol_new ad1988_control_templates[] = {
2182         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2183         HDA_CODEC_MUTE(NULL, 0, 0, 0),
2184         HDA_BIND_MUTE(NULL, 0, 0, 0),
2185 };
2186
2187 /* add dynamic controls */
2188 static int add_control(struct ad198x_spec *spec, int type, const char *name,
2189                        unsigned long val)
2190 {
2191         struct snd_kcontrol_new *knew;
2192
2193         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
2194                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
2195
2196                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
2197                 if (! knew)
2198                         return -ENOMEM;
2199                 if (spec->kctl_alloc) {
2200                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
2201                         kfree(spec->kctl_alloc);
2202                 }
2203                 spec->kctl_alloc = knew;
2204                 spec->num_kctl_alloc = num;
2205         }
2206
2207         knew = &spec->kctl_alloc[spec->num_kctl_used];
2208         *knew = ad1988_control_templates[type];
2209         knew->name = kstrdup(name, GFP_KERNEL);
2210         if (! knew->name)
2211                 return -ENOMEM;
2212         knew->private_value = val;
2213         spec->num_kctl_used++;
2214         return 0;
2215 }
2216
2217 #define AD1988_PIN_CD_NID               0x18
2218 #define AD1988_PIN_BEEP_NID             0x10
2219
2220 static hda_nid_t ad1988_mixer_nids[8] = {
2221         /* A     B     C     D     E     F     G     H */
2222         0x22, 0x2b, 0x2c, 0x29, 0x26, 0x2a, 0x27, 0x28
2223 };
2224
2225 static inline hda_nid_t ad1988_idx_to_dac(struct hda_codec *codec, int idx)
2226 {
2227         static hda_nid_t idx_to_dac[8] = {
2228                 /* A     B     C     D     E     F     G     H */
2229                 0x04, 0x06, 0x05, 0x04, 0x0a, 0x06, 0x05, 0x0a
2230         };
2231         static hda_nid_t idx_to_dac_rev2[8] = {
2232                 /* A     B     C     D     E     F     G     H */
2233                 0x04, 0x05, 0x0a, 0x04, 0x06, 0x05, 0x0a, 0x06
2234         };
2235         if (is_rev2(codec))
2236                 return idx_to_dac_rev2[idx];
2237         else
2238                 return idx_to_dac[idx];
2239 }
2240
2241 static hda_nid_t ad1988_boost_nids[8] = {
2242         0x38, 0x39, 0x3a, 0x3d, 0x3c, 0x3b, 0, 0
2243 };
2244
2245 static int ad1988_pin_idx(hda_nid_t nid)
2246 {
2247         static hda_nid_t ad1988_io_pins[8] = {
2248                 0x11, 0x14, 0x15, 0x12, 0x17, 0x16, 0x24, 0x25
2249         };
2250         int i;
2251         for (i = 0; i < ARRAY_SIZE(ad1988_io_pins); i++)
2252                 if (ad1988_io_pins[i] == nid)
2253                         return i;
2254         return 0; /* should be -1 */
2255 }
2256
2257 static int ad1988_pin_to_loopback_idx(hda_nid_t nid)
2258 {
2259         static int loopback_idx[8] = {
2260                 2, 0, 1, 3, 4, 5, 1, 4
2261         };
2262         switch (nid) {
2263         case AD1988_PIN_CD_NID:
2264                 return 6;
2265         default:
2266                 return loopback_idx[ad1988_pin_idx(nid)];
2267         }
2268 }
2269
2270 static int ad1988_pin_to_adc_idx(hda_nid_t nid)
2271 {
2272         static int adc_idx[8] = {
2273                 0, 1, 2, 8, 4, 3, 6, 7
2274         };
2275         switch (nid) {
2276         case AD1988_PIN_CD_NID:
2277                 return 5;
2278         default:
2279                 return adc_idx[ad1988_pin_idx(nid)];
2280         }
2281 }
2282
2283 /* fill in the dac_nids table from the parsed pin configuration */
2284 static int ad1988_auto_fill_dac_nids(struct hda_codec *codec,
2285                                      const struct auto_pin_cfg *cfg)
2286 {
2287         struct ad198x_spec *spec = codec->spec;
2288         int i, idx;
2289
2290         spec->multiout.dac_nids = spec->private_dac_nids;
2291
2292         /* check the pins hardwired to audio widget */
2293         for (i = 0; i < cfg->line_outs; i++) {
2294                 idx = ad1988_pin_idx(cfg->line_out_pins[i]);
2295                 spec->multiout.dac_nids[i] = ad1988_idx_to_dac(codec, idx);
2296         }
2297         spec->multiout.num_dacs = cfg->line_outs;
2298         return 0;
2299 }
2300
2301 /* add playback controls from the parsed DAC table */
2302 static int ad1988_auto_create_multi_out_ctls(struct ad198x_spec *spec,
2303                                              const struct auto_pin_cfg *cfg)
2304 {
2305         char name[32];
2306         static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
2307         hda_nid_t nid;
2308         int i, err;
2309
2310         for (i = 0; i < cfg->line_outs; i++) {
2311                 hda_nid_t dac = spec->multiout.dac_nids[i];
2312                 if (! dac)
2313                         continue;
2314                 nid = ad1988_mixer_nids[ad1988_pin_idx(cfg->line_out_pins[i])];
2315                 if (i == 2) {
2316                         /* Center/LFE */
2317                         err = add_control(spec, AD_CTL_WIDGET_VOL,
2318                                           "Center Playback Volume",
2319                                           HDA_COMPOSE_AMP_VAL(dac, 1, 0, HDA_OUTPUT));
2320                         if (err < 0)
2321                                 return err;
2322                         err = add_control(spec, AD_CTL_WIDGET_VOL,
2323                                           "LFE Playback Volume",
2324                                           HDA_COMPOSE_AMP_VAL(dac, 2, 0, HDA_OUTPUT));
2325                         if (err < 0)
2326                                 return err;
2327                         err = add_control(spec, AD_CTL_BIND_MUTE,
2328                                           "Center Playback Switch",
2329                                           HDA_COMPOSE_AMP_VAL(nid, 1, 2, HDA_INPUT));
2330                         if (err < 0)
2331                                 return err;
2332                         err = add_control(spec, AD_CTL_BIND_MUTE,
2333                                           "LFE Playback Switch",
2334                                           HDA_COMPOSE_AMP_VAL(nid, 2, 2, HDA_INPUT));
2335                         if (err < 0)
2336                                 return err;
2337                 } else {
2338                         sprintf(name, "%s Playback Volume", chname[i]);
2339                         err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2340                                           HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT));
2341                         if (err < 0)
2342                                 return err;
2343                         sprintf(name, "%s Playback Switch", chname[i]);
2344                         err = add_control(spec, AD_CTL_BIND_MUTE, name,
2345                                           HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT));
2346                         if (err < 0)
2347                                 return err;
2348                 }
2349         }
2350         return 0;
2351 }
2352
2353 /* add playback controls for speaker and HP outputs */
2354 static int ad1988_auto_create_extra_out(struct hda_codec *codec, hda_nid_t pin,
2355                                         const char *pfx)
2356 {
2357         struct ad198x_spec *spec = codec->spec;
2358         hda_nid_t nid;
2359         int idx, err;
2360         char name[32];
2361
2362         if (! pin)
2363                 return 0;
2364
2365         idx = ad1988_pin_idx(pin);
2366         nid = ad1988_idx_to_dac(codec, idx);
2367         /* specify the DAC as the extra output */
2368         if (! spec->multiout.hp_nid)
2369                 spec->multiout.hp_nid = nid;
2370         else
2371                 spec->multiout.extra_out_nid[0] = nid;
2372         /* control HP volume/switch on the output mixer amp */
2373         sprintf(name, "%s Playback Volume", pfx);
2374         if ((err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2375                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
2376                 return err;
2377         nid = ad1988_mixer_nids[idx];
2378         sprintf(name, "%s Playback Switch", pfx);
2379         if ((err = add_control(spec, AD_CTL_BIND_MUTE, name,
2380                                HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT))) < 0)
2381                 return err;
2382         return 0;
2383 }
2384
2385 /* create input playback/capture controls for the given pin */
2386 static int new_analog_input(struct ad198x_spec *spec, hda_nid_t pin,
2387                             const char *ctlname, int boost)
2388 {
2389         char name[32];
2390         int err, idx;
2391
2392         sprintf(name, "%s Playback Volume", ctlname);
2393         idx = ad1988_pin_to_loopback_idx(pin);
2394         if ((err = add_control(spec, AD_CTL_WIDGET_VOL, name,
2395                                HDA_COMPOSE_AMP_VAL(0x20, 3, idx, HDA_INPUT))) < 0)
2396                 return err;
2397         sprintf(name, "%s Playback Switch", ctlname);
2398         if ((err = add_control(spec, AD_CTL_WIDGET_MUTE, name,
2399                                HDA_COMPOSE_AMP_VAL(0x20, 3, idx, HDA_INPUT))) < 0)
2400                 return err;
2401         if (boost) {
2402                 hda_nid_t bnid;
2403                 idx = ad1988_pin_idx(pin);
2404                 bnid = ad1988_boost_nids[idx];
2405                 if (bnid) {
2406                         sprintf(name, "%s Boost", ctlname);
2407                         return add_control(spec, AD_CTL_WIDGET_VOL, name,
2408                                            HDA_COMPOSE_AMP_VAL(bnid, 3, idx, HDA_OUTPUT));
2409
2410                 }
2411         }
2412         return 0;
2413 }
2414
2415 /* create playback/capture controls for input pins */
2416 static int ad1988_auto_create_analog_input_ctls(struct ad198x_spec *spec,
2417                                                 const struct auto_pin_cfg *cfg)
2418 {
2419         struct hda_input_mux *imux = &spec->private_imux;
2420         int i, err;
2421
2422         for (i = 0; i < AUTO_PIN_LAST; i++) {
2423                 err = new_analog_input(spec, cfg->input_pins[i],
2424                                        auto_pin_cfg_labels[i],
2425                                        i <= AUTO_PIN_FRONT_MIC);
2426                 if (err < 0)
2427                         return err;
2428                 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
2429                 imux->items[imux->num_items].index = ad1988_pin_to_adc_idx(cfg->input_pins[i]);
2430                 imux->num_items++;
2431         }
2432         imux->items[imux->num_items].label = "Mix";
2433         imux->items[imux->num_items].index = 9;
2434         imux->num_items++;
2435
2436         if ((err = add_control(spec, AD_CTL_WIDGET_VOL,
2437                                "Analog Mix Playback Volume",
2438                                HDA_COMPOSE_AMP_VAL(0x21, 3, 0x0, HDA_OUTPUT))) < 0)
2439                 return err;
2440         if ((err = add_control(spec, AD_CTL_WIDGET_MUTE,
2441                                "Analog Mix Playback Switch",
2442                                HDA_COMPOSE_AMP_VAL(0x21, 3, 0x0, HDA_OUTPUT))) < 0)
2443                 return err;
2444
2445         return 0;
2446 }
2447
2448 static void ad1988_auto_set_output_and_unmute(struct hda_codec *codec,
2449                                               hda_nid_t nid, int pin_type,
2450                                               int dac_idx)
2451 {
2452         /* set as output */
2453         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
2454         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
2455         switch (nid) {
2456         case 0x11: /* port-A - DAC 04 */
2457                 snd_hda_codec_write(codec, 0x37, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2458                 break;
2459         case 0x14: /* port-B - DAC 06 */
2460                 snd_hda_codec_write(codec, 0x30, 0, AC_VERB_SET_CONNECT_SEL, 0x02);
2461                 break;
2462         case 0x15: /* port-C - DAC 05 */
2463                 snd_hda_codec_write(codec, 0x31, 0, AC_VERB_SET_CONNECT_SEL, 0x00);
2464                 break;
2465         case 0x17: /* port-E - DAC 0a */
2466                 snd_hda_codec_write(codec, 0x32, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2467                 break;
2468         case 0x13: /* mono - DAC 04 */
2469                 snd_hda_codec_write(codec, 0x36, 0, AC_VERB_SET_CONNECT_SEL, 0x01);
2470                 break;
2471         }
2472 }
2473
2474 static void ad1988_auto_init_multi_out(struct hda_codec *codec)
2475 {
2476         struct ad198x_spec *spec = codec->spec;
2477         int i;
2478
2479         for (i = 0; i < spec->autocfg.line_outs; i++) {
2480                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
2481                 ad1988_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
2482         }
2483 }
2484
2485 static void ad1988_auto_init_extra_out(struct hda_codec *codec)
2486 {
2487         struct ad198x_spec *spec = codec->spec;
2488         hda_nid_t pin;
2489
2490         pin = spec->autocfg.speaker_pins[0];
2491         if (pin) /* connect to front */
2492                 ad1988_auto_set_output_and_unmute(codec, pin, PIN_OUT, 0);
2493         pin = spec->autocfg.hp_pins[0];
2494         if (pin) /* connect to front */
2495                 ad1988_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
2496 }
2497
2498 static void ad1988_auto_init_analog_input(struct hda_codec *codec)
2499 {
2500         struct ad198x_spec *spec = codec->spec;
2501         int i, idx;
2502
2503         for (i = 0; i < AUTO_PIN_LAST; i++) {
2504                 hda_nid_t nid = spec->autocfg.input_pins[i];
2505                 if (! nid)
2506                         continue;
2507                 switch (nid) {
2508                 case 0x15: /* port-C */
2509                         snd_hda_codec_write(codec, 0x33, 0, AC_VERB_SET_CONNECT_SEL, 0x0);
2510                         break;
2511                 case 0x17: /* port-E */
2512                         snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_CONNECT_SEL, 0x0);
2513                         break;
2514                 }
2515                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2516                                     i <= AUTO_PIN_FRONT_MIC ? PIN_VREF80 : PIN_IN);
2517                 if (nid != AD1988_PIN_CD_NID)
2518                         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2519                                             AMP_OUT_MUTE);
2520                 idx = ad1988_pin_idx(nid);
2521                 if (ad1988_boost_nids[idx])
2522                         snd_hda_codec_write(codec, ad1988_boost_nids[idx], 0,
2523                                             AC_VERB_SET_AMP_GAIN_MUTE,
2524                                             AMP_OUT_ZERO);
2525         }
2526 }
2527
2528 /* parse the BIOS configuration and set up the alc_spec */
2529 /* return 1 if successful, 0 if the proper config is not found, or a negative error code */
2530 static int ad1988_parse_auto_config(struct hda_codec *codec)
2531 {
2532         struct ad198x_spec *spec = codec->spec;
2533         int err;
2534
2535         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
2536                 return err;
2537         if ((err = ad1988_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
2538                 return err;
2539         if (! spec->autocfg.line_outs)
2540                 return 0; /* can't find valid BIOS pin config */
2541         if ((err = ad1988_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
2542             (err = ad1988_auto_create_extra_out(codec,
2543                                                 spec->autocfg.speaker_pins[0],
2544                                                 "Speaker")) < 0 ||
2545             (err = ad1988_auto_create_extra_out(codec, spec->autocfg.hp_pins[0],
2546                                                 "Headphone")) < 0 ||
2547             (err = ad1988_auto_create_analog_input_ctls(spec, &spec->autocfg)) < 0)
2548                 return err;
2549
2550         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2551
2552         if (spec->autocfg.dig_out_pin)
2553                 spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2554         if (spec->autocfg.dig_in_pin)
2555                 spec->dig_in_nid = AD1988_SPDIF_IN;
2556
2557         if (spec->kctl_alloc)
2558                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
2559
2560         spec->init_verbs[spec->num_init_verbs++] = ad1988_6stack_init_verbs;
2561
2562         spec->input_mux = &spec->private_imux;
2563
2564         return 1;
2565 }
2566
2567 /* init callback for auto-configuration model -- overriding the default init */
2568 static int ad1988_auto_init(struct hda_codec *codec)
2569 {
2570         ad198x_init(codec);
2571         ad1988_auto_init_multi_out(codec);
2572         ad1988_auto_init_extra_out(codec);
2573         ad1988_auto_init_analog_input(codec);
2574         return 0;
2575 }
2576
2577
2578 /*
2579  */
2580
2581 static const char *ad1988_models[AD1988_MODEL_LAST] = {
2582         [AD1988_6STACK]         = "6stack",
2583         [AD1988_6STACK_DIG]     = "6stack-dig",
2584         [AD1988_3STACK]         = "3stack",
2585         [AD1988_3STACK_DIG]     = "3stack-dig",
2586         [AD1988_LAPTOP]         = "laptop",
2587         [AD1988_LAPTOP_DIG]     = "laptop-dig",
2588         [AD1988_AUTO]           = "auto",
2589 };
2590
2591 static struct snd_pci_quirk ad1988_cfg_tbl[] = {
2592         SND_PCI_QUIRK(0x1043, 0x81f6, "Asus M2N-SLI", AD1988_6STACK_DIG),
2593         SND_PCI_QUIRK(0x1043, 0x81ec, "Asus P5B-DLX", AD1988_6STACK_DIG),
2594         {}
2595 };
2596
2597 static int patch_ad1988(struct hda_codec *codec)
2598 {
2599         struct ad198x_spec *spec;
2600         int board_config;
2601
2602         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2603         if (spec == NULL)
2604                 return -ENOMEM;
2605
2606         codec->spec = spec;
2607
2608         if (is_rev2(codec))
2609                 snd_printk(KERN_INFO "patch_analog: AD1988A rev.2 is detected, enable workarounds\n");
2610
2611         board_config = snd_hda_check_board_config(codec, AD1988_MODEL_LAST,
2612                                                   ad1988_models, ad1988_cfg_tbl);
2613         if (board_config < 0) {
2614                 printk(KERN_INFO "hda_codec: Unknown model for AD1988, trying auto-probe from BIOS...\n");
2615                 board_config = AD1988_AUTO;
2616         }
2617
2618         if (board_config == AD1988_AUTO) {
2619                 /* automatic parse from the BIOS config */
2620                 int err = ad1988_parse_auto_config(codec);
2621                 if (err < 0) {
2622                         ad198x_free(codec);
2623                         return err;
2624                 } else if (! err) {
2625                         printk(KERN_INFO "hda_codec: Cannot set up configuration from BIOS.  Using 6-stack mode...\n");
2626                         board_config = AD1988_6STACK;
2627                 }
2628         }
2629
2630         switch (board_config) {
2631         case AD1988_6STACK:
2632         case AD1988_6STACK_DIG:
2633                 spec->multiout.max_channels = 8;
2634                 spec->multiout.num_dacs = 4;
2635                 if (is_rev2(codec))
2636                         spec->multiout.dac_nids = ad1988_6stack_dac_nids_rev2;
2637                 else
2638                         spec->multiout.dac_nids = ad1988_6stack_dac_nids;
2639                 spec->input_mux = &ad1988_6stack_capture_source;
2640                 spec->num_mixers = 2;
2641                 if (is_rev2(codec))
2642                         spec->mixers[0] = ad1988_6stack_mixers1_rev2;
2643                 else
2644                         spec->mixers[0] = ad1988_6stack_mixers1;
2645                 spec->mixers[1] = ad1988_6stack_mixers2;
2646                 spec->num_init_verbs = 1;
2647                 spec->init_verbs[0] = ad1988_6stack_init_verbs;
2648                 if (board_config == AD1988_6STACK_DIG) {
2649                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2650                         spec->dig_in_nid = AD1988_SPDIF_IN;
2651                 }
2652                 break;
2653         case AD1988_3STACK:
2654         case AD1988_3STACK_DIG:
2655                 spec->multiout.max_channels = 6;
2656                 spec->multiout.num_dacs = 3;
2657                 if (is_rev2(codec))
2658                         spec->multiout.dac_nids = ad1988_3stack_dac_nids_rev2;
2659                 else
2660                         spec->multiout.dac_nids = ad1988_3stack_dac_nids;
2661                 spec->input_mux = &ad1988_6stack_capture_source;
2662                 spec->channel_mode = ad1988_3stack_modes;
2663                 spec->num_channel_mode = ARRAY_SIZE(ad1988_3stack_modes);
2664                 spec->num_mixers = 2;
2665                 if (is_rev2(codec))
2666                         spec->mixers[0] = ad1988_3stack_mixers1_rev2;
2667                 else
2668                         spec->mixers[0] = ad1988_3stack_mixers1;
2669                 spec->mixers[1] = ad1988_3stack_mixers2;
2670                 spec->num_init_verbs = 1;
2671                 spec->init_verbs[0] = ad1988_3stack_init_verbs;
2672                 if (board_config == AD1988_3STACK_DIG)
2673                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2674                 break;
2675         case AD1988_LAPTOP:
2676         case AD1988_LAPTOP_DIG:
2677                 spec->multiout.max_channels = 2;
2678                 spec->multiout.num_dacs = 1;
2679                 spec->multiout.dac_nids = ad1988_3stack_dac_nids;
2680                 spec->input_mux = &ad1988_laptop_capture_source;
2681                 spec->num_mixers = 1;
2682                 spec->mixers[0] = ad1988_laptop_mixers;
2683                 spec->num_init_verbs = 1;
2684                 spec->init_verbs[0] = ad1988_laptop_init_verbs;
2685                 if (board_config == AD1988_LAPTOP_DIG)
2686                         spec->multiout.dig_out_nid = AD1988_SPDIF_OUT;
2687                 break;
2688         }
2689
2690         spec->num_adc_nids = ARRAY_SIZE(ad1988_adc_nids);
2691         spec->adc_nids = ad1988_adc_nids;
2692         spec->capsrc_nids = ad1988_capsrc_nids;
2693         spec->mixers[spec->num_mixers++] = ad1988_capture_mixers;
2694         spec->init_verbs[spec->num_init_verbs++] = ad1988_capture_init_verbs;
2695         if (spec->multiout.dig_out_nid) {
2696                 spec->mixers[spec->num_mixers++] = ad1988_spdif_out_mixers;
2697                 spec->init_verbs[spec->num_init_verbs++] = ad1988_spdif_init_verbs;
2698         }
2699         if (spec->dig_in_nid)
2700                 spec->mixers[spec->num_mixers++] = ad1988_spdif_in_mixers;
2701
2702         codec->patch_ops = ad198x_patch_ops;
2703         switch (board_config) {
2704         case AD1988_AUTO:
2705                 codec->patch_ops.init = ad1988_auto_init;
2706                 break;
2707         case AD1988_LAPTOP:
2708         case AD1988_LAPTOP_DIG:
2709                 codec->patch_ops.unsol_event = ad1988_laptop_unsol_event;
2710                 break;
2711         }
2712 #ifdef CONFIG_SND_HDA_POWER_SAVE
2713         spec->loopback.amplist = ad1988_loopbacks;
2714 #endif
2715
2716         return 0;
2717 }
2718
2719
2720 /*
2721  * AD1884 / AD1984
2722  *
2723  * port-B - front line/mic-in
2724  * port-E - aux in/out
2725  * port-F - aux in/out
2726  * port-C - rear line/mic-in
2727  * port-D - rear line/hp-out
2728  * port-A - front line/hp-out
2729  *
2730  * AD1984 = AD1884 + two digital mic-ins
2731  *
2732  * FIXME:
2733  * For simplicity, we share the single DAC for both HP and line-outs
2734  * right now.  The inidividual playbacks could be easily implemented,
2735  * but no build-up framework is given, so far.
2736  */
2737
2738 static hda_nid_t ad1884_dac_nids[1] = {
2739         0x04,
2740 };
2741
2742 static hda_nid_t ad1884_adc_nids[2] = {
2743         0x08, 0x09,
2744 };
2745
2746 static hda_nid_t ad1884_capsrc_nids[2] = {
2747         0x0c, 0x0d,
2748 };
2749
2750 #define AD1884_SPDIF_OUT        0x02
2751
2752 static struct hda_input_mux ad1884_capture_source = {
2753         .num_items = 4,
2754         .items = {
2755                 { "Front Mic", 0x0 },
2756                 { "Mic", 0x1 },
2757                 { "CD", 0x2 },
2758                 { "Mix", 0x3 },
2759         },
2760 };
2761
2762 static struct snd_kcontrol_new ad1884_base_mixers[] = {
2763         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2764         /* HDA_CODEC_VOLUME_IDX("PCM Playback Volume", 1, 0x03, 0x0, HDA_OUTPUT), */
2765         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
2766         HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
2767         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
2768         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
2769         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
2770         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
2771         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
2772         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
2773         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x02, HDA_INPUT),
2774         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x02, HDA_INPUT),
2775         /*
2776         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x20, 0x03, HDA_INPUT),
2777         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x20, 0x03, HDA_INPUT),
2778         HDA_CODEC_VOLUME("Digital Beep Playback Volume", 0x10, 0x0, HDA_OUTPUT),
2779         HDA_CODEC_MUTE("Digital Beep Playback Switch", 0x10, 0x0, HDA_OUTPUT),
2780         */
2781         HDA_CODEC_VOLUME("Mic Boost", 0x15, 0x0, HDA_INPUT),
2782         HDA_CODEC_VOLUME("Front Mic Boost", 0x14, 0x0, HDA_INPUT),
2783         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
2784         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
2785         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
2786         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
2787         {
2788                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2789                 /* The multiple "Capture Source" controls confuse alsamixer
2790                  * So call somewhat different..
2791                  * FIXME: the controls appear in the "playback" view!
2792                  */
2793                 /* .name = "Capture Source", */
2794                 .name = "Input Source",
2795                 .count = 2,
2796                 .info = ad198x_mux_enum_info,
2797                 .get = ad198x_mux_enum_get,
2798                 .put = ad198x_mux_enum_put,
2799         },
2800         /* SPDIF controls */
2801         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
2802         {
2803                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2804                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
2805                 /* identical with ad1983 */
2806                 .info = ad1983_spdif_route_info,
2807                 .get = ad1983_spdif_route_get,
2808                 .put = ad1983_spdif_route_put,
2809         },
2810         { } /* end */
2811 };
2812
2813 static struct snd_kcontrol_new ad1984_dmic_mixers[] = {
2814         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x05, 0x0, HDA_INPUT),
2815         HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x05, 0x0, HDA_INPUT),
2816         HDA_CODEC_VOLUME_IDX("Digital Mic Capture Volume", 1, 0x06, 0x0,
2817                              HDA_INPUT),
2818         HDA_CODEC_MUTE_IDX("Digital Mic Capture Switch", 1, 0x06, 0x0,
2819                            HDA_INPUT),
2820         { } /* end */
2821 };
2822
2823 /*
2824  * initialization verbs
2825  */
2826 static struct hda_verb ad1884_init_verbs[] = {
2827         /* DACs; mute as default */
2828         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2829         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2830         /* Port-A (HP) mixer */
2831         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2832         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2833         /* Port-A pin */
2834         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2835         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2836         /* HP selector - select DAC2 */
2837         {0x22, AC_VERB_SET_CONNECT_SEL, 0x1},
2838         /* Port-D (Line-out) mixer */
2839         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2840         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2841         /* Port-D pin */
2842         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2843         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2844         /* Mono-out mixer */
2845         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2846         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2847         /* Mono-out pin */
2848         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2849         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2850         /* Mono selector */
2851         {0x0e, AC_VERB_SET_CONNECT_SEL, 0x1},
2852         /* Port-B (front mic) pin */
2853         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2854         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2855         /* Port-C (rear mic) pin */
2856         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2857         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2858         /* Analog mixer; mute as default */
2859         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2860         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2861         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2862         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2863         /* Analog Mix output amp */
2864         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
2865         /* SPDIF output selector */
2866         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
2867         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
2868         { } /* end */
2869 };
2870
2871 #ifdef CONFIG_SND_HDA_POWER_SAVE
2872 static struct hda_amp_list ad1884_loopbacks[] = {
2873         { 0x20, HDA_INPUT, 0 }, /* Front Mic */
2874         { 0x20, HDA_INPUT, 1 }, /* Mic */
2875         { 0x20, HDA_INPUT, 2 }, /* CD */
2876         { 0x20, HDA_INPUT, 4 }, /* Docking */
2877         { } /* end */
2878 };
2879 #endif
2880
2881 static int patch_ad1884(struct hda_codec *codec)
2882 {
2883         struct ad198x_spec *spec;
2884
2885         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2886         if (spec == NULL)
2887                 return -ENOMEM;
2888
2889         mutex_init(&spec->amp_mutex);
2890         codec->spec = spec;
2891
2892         spec->multiout.max_channels = 2;
2893         spec->multiout.num_dacs = ARRAY_SIZE(ad1884_dac_nids);
2894         spec->multiout.dac_nids = ad1884_dac_nids;
2895         spec->multiout.dig_out_nid = AD1884_SPDIF_OUT;
2896         spec->num_adc_nids = ARRAY_SIZE(ad1884_adc_nids);
2897         spec->adc_nids = ad1884_adc_nids;
2898         spec->capsrc_nids = ad1884_capsrc_nids;
2899         spec->input_mux = &ad1884_capture_source;
2900         spec->num_mixers = 1;
2901         spec->mixers[0] = ad1884_base_mixers;
2902         spec->num_init_verbs = 1;
2903         spec->init_verbs[0] = ad1884_init_verbs;
2904         spec->spdif_route = 0;
2905 #ifdef CONFIG_SND_HDA_POWER_SAVE
2906         spec->loopback.amplist = ad1884_loopbacks;
2907 #endif
2908
2909         codec->patch_ops = ad198x_patch_ops;
2910
2911         return 0;
2912 }
2913
2914 /*
2915  * Lenovo Thinkpad T61/X61
2916  */
2917 static struct hda_input_mux ad1984_thinkpad_capture_source = {
2918         .num_items = 3,
2919         .items = {
2920                 { "Mic", 0x0 },
2921                 { "Internal Mic", 0x1 },
2922                 { "Mix", 0x3 },
2923         },
2924 };
2925
2926 static struct snd_kcontrol_new ad1984_thinkpad_mixers[] = {
2927         HDA_CODEC_VOLUME("PCM Playback Volume", 0x04, 0x0, HDA_OUTPUT),
2928         /* HDA_CODEC_VOLUME_IDX("PCM Playback Volume", 1, 0x03, 0x0, HDA_OUTPUT), */
2929         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
2930         HDA_CODEC_MUTE("Speaker Playback Switch", 0x12, 0x0, HDA_OUTPUT),
2931         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
2932         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
2933         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
2934         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
2935         HDA_CODEC_VOLUME("Docking Mic Playback Volume", 0x20, 0x04, HDA_INPUT),
2936         HDA_CODEC_MUTE("Docking Mic Playback Switch", 0x20, 0x04, HDA_INPUT),
2937         HDA_CODEC_VOLUME("Mic Boost", 0x14, 0x0, HDA_INPUT),
2938         HDA_CODEC_VOLUME("Internal Mic Boost", 0x15, 0x0, HDA_INPUT),
2939         HDA_CODEC_VOLUME("Docking Mic Boost", 0x25, 0x0, HDA_OUTPUT),
2940         HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x03, HDA_INPUT),
2941         HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x03, HDA_INPUT),
2942         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
2943         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
2944         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
2945         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
2946         {
2947                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2948                 /* The multiple "Capture Source" controls confuse alsamixer
2949                  * So call somewhat different..
2950                  * FIXME: the controls appear in the "playback" view!
2951                  */
2952                 /* .name = "Capture Source", */
2953                 .name = "Input Source",
2954                 .count = 2,
2955                 .info = ad198x_mux_enum_info,
2956                 .get = ad198x_mux_enum_get,
2957                 .put = ad198x_mux_enum_put,
2958         },
2959         { } /* end */
2960 };
2961
2962 /* additional verbs */
2963 static struct hda_verb ad1984_thinkpad_init_verbs[] = {
2964         /* Port-E (docking station mic) pin */
2965         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2966         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2967         /* docking mic boost */
2968         {0x25, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2969         /* Analog mixer - docking mic; mute as default */
2970         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2971         /* enable EAPD bit */
2972         {0x12, AC_VERB_SET_EAPD_BTLENABLE, 0x02},
2973         { } /* end */
2974 };
2975
2976 /* Digial MIC ADC NID 0x05 + 0x06 */
2977 static int ad1984_pcm_dmic_prepare(struct hda_pcm_stream *hinfo,
2978                                    struct hda_codec *codec,
2979                                    unsigned int stream_tag,
2980                                    unsigned int format,
2981                                    struct snd_pcm_substream *substream)
2982 {
2983         snd_hda_codec_setup_stream(codec, 0x05 + substream->number,
2984                                    stream_tag, 0, format);
2985         return 0;
2986 }
2987
2988 static int ad1984_pcm_dmic_cleanup(struct hda_pcm_stream *hinfo,
2989                                    struct hda_codec *codec,
2990                                    struct snd_pcm_substream *substream)
2991 {
2992         snd_hda_codec_setup_stream(codec, 0x05 + substream->number,
2993                                    0, 0, 0);
2994         return 0;
2995 }
2996
2997 static struct hda_pcm_stream ad1984_pcm_dmic_capture = {
2998         .substreams = 2,
2999         .channels_min = 2,
3000         .channels_max = 2,
3001         .nid = 0x05,
3002         .ops = {
3003                 .prepare = ad1984_pcm_dmic_prepare,
3004                 .cleanup = ad1984_pcm_dmic_cleanup
3005         },
3006 };
3007
3008 static int ad1984_build_pcms(struct hda_codec *codec)
3009 {
3010         struct ad198x_spec *spec = codec->spec;
3011         struct hda_pcm *info;
3012         int err;
3013
3014         err = ad198x_build_pcms(codec);
3015         if (err < 0)
3016                 return err;
3017
3018         info = spec->pcm_rec + codec->num_pcms;
3019         codec->num_pcms++;
3020         info->name = "AD1984 Digital Mic";
3021         info->stream[SNDRV_PCM_STREAM_CAPTURE] = ad1984_pcm_dmic_capture;
3022         return 0;
3023 }
3024
3025 /* models */
3026 enum {
3027         AD1984_BASIC,
3028         AD1984_THINKPAD,
3029         AD1984_MODELS
3030 };
3031
3032 static const char *ad1984_models[AD1984_MODELS] = {
3033         [AD1984_BASIC]          = "basic",
3034         [AD1984_THINKPAD]       = "thinkpad",
3035 };
3036
3037 static struct snd_pci_quirk ad1984_cfg_tbl[] = {
3038         /* Lenovo Thinkpad T61/X61 */
3039         SND_PCI_QUIRK(0x17aa, 0, "Lenovo Thinkpad", AD1984_THINKPAD),
3040         {}
3041 };
3042
3043 static int patch_ad1984(struct hda_codec *codec)
3044 {
3045         struct ad198x_spec *spec;
3046         int board_config, err;
3047
3048         err = patch_ad1884(codec);
3049         if (err < 0)
3050                 return err;
3051         spec = codec->spec;
3052         board_config = snd_hda_check_board_config(codec, AD1984_MODELS,
3053                                                   ad1984_models, ad1984_cfg_tbl);
3054         switch (board_config) {
3055         case AD1984_BASIC:
3056                 /* additional digital mics */
3057                 spec->mixers[spec->num_mixers++] = ad1984_dmic_mixers;
3058                 codec->patch_ops.build_pcms = ad1984_build_pcms;
3059                 break;
3060         case AD1984_THINKPAD:
3061                 spec->multiout.dig_out_nid = 0;
3062                 spec->input_mux = &ad1984_thinkpad_capture_source;
3063                 spec->mixers[0] = ad1984_thinkpad_mixers;
3064                 spec->init_verbs[spec->num_init_verbs++] = ad1984_thinkpad_init_verbs;
3065                 break;
3066         }
3067         return 0;
3068 }
3069
3070
3071 /*
3072  * AD1882
3073  *
3074  * port-A - front hp-out
3075  * port-B - front mic-in
3076  * port-C - rear line-in, shared surr-out (3stack)
3077  * port-D - rear line-out
3078  * port-E - rear mic-in, shared clfe-out (3stack)
3079  * port-F - rear surr-out (6stack)
3080  * port-G - rear clfe-out (6stack)
3081  */
3082
3083 static hda_nid_t ad1882_dac_nids[3] = {
3084         0x04, 0x03, 0x05
3085 };
3086
3087 static hda_nid_t ad1882_adc_nids[2] = {
3088         0x08, 0x09,
3089 };
3090
3091 static hda_nid_t ad1882_capsrc_nids[2] = {
3092         0x0c, 0x0d,
3093 };
3094
3095 #define AD1882_SPDIF_OUT        0x02
3096
3097 /* list: 0x11, 0x39, 0x3a, 0x18, 0x3c, 0x3b, 0x12, 0x20 */
3098 static struct hda_input_mux ad1882_capture_source = {
3099         .num_items = 5,
3100         .items = {
3101                 { "Front Mic", 0x1 },
3102                 { "Mic", 0x4 },
3103                 { "Line", 0x2 },
3104                 { "CD", 0x3 },
3105                 { "Mix", 0x7 },
3106         },
3107 };
3108
3109 static struct snd_kcontrol_new ad1882_base_mixers[] = {
3110         HDA_CODEC_VOLUME("Front Playback Volume", 0x04, 0x0, HDA_OUTPUT),
3111         HDA_CODEC_VOLUME("Surround Playback Volume", 0x03, 0x0, HDA_OUTPUT),
3112         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x05, 1, 0x0, HDA_OUTPUT),
3113         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x05, 2, 0x0, HDA_OUTPUT),
3114         HDA_CODEC_MUTE("Headphone Playback Switch", 0x11, 0x0, HDA_OUTPUT),
3115         HDA_CODEC_MUTE("Front Playback Switch", 0x12, 0x0, HDA_OUTPUT),
3116         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
3117         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x13, 1, 0x0, HDA_OUTPUT),
3118         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x20, 0x00, HDA_INPUT),
3119         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x20, 0x00, HDA_INPUT),
3120         HDA_CODEC_VOLUME("Mic Playback Volume", 0x20, 0x01, HDA_INPUT),
3121         HDA_CODEC_MUTE("Mic Playback Switch", 0x20, 0x01, HDA_INPUT),
3122         HDA_CODEC_VOLUME("Line Playback Volume", 0x20, 0x04, HDA_INPUT),
3123         HDA_CODEC_MUTE("Line Playback Switch", 0x20, 0x04, HDA_INPUT),
3124         HDA_CODEC_VOLUME("CD Playback Volume", 0x20, 0x06, HDA_INPUT),
3125         HDA_CODEC_MUTE("CD Playback Switch", 0x20, 0x06, HDA_INPUT),
3126         HDA_CODEC_VOLUME("Beep Playback Volume", 0x20, 0x07, HDA_INPUT),
3127         HDA_CODEC_MUTE("Beep Playback Switch", 0x20, 0x07, HDA_INPUT),
3128         HDA_CODEC_VOLUME("Mic Boost", 0x3c, 0x0, HDA_OUTPUT),
3129         HDA_CODEC_VOLUME("Front Mic Boost", 0x39, 0x0, HDA_OUTPUT),
3130         HDA_CODEC_VOLUME("Line-In Boost", 0x3a, 0x0, HDA_OUTPUT),
3131         HDA_CODEC_VOLUME("Capture Volume", 0x0c, 0x0, HDA_OUTPUT),
3132         HDA_CODEC_MUTE("Capture Switch", 0x0c, 0x0, HDA_OUTPUT),
3133         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x0d, 0x0, HDA_OUTPUT),
3134         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x0d, 0x0, HDA_OUTPUT),
3135         {
3136                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3137                 /* The multiple "Capture Source" controls confuse alsamixer
3138                  * So call somewhat different..
3139                  * FIXME: the controls appear in the "playback" view!
3140                  */
3141                 /* .name = "Capture Source", */
3142                 .name = "Input Source",
3143                 .count = 2,
3144                 .info = ad198x_mux_enum_info,
3145                 .get = ad198x_mux_enum_get,
3146                 .put = ad198x_mux_enum_put,
3147         },
3148         /* SPDIF controls */
3149         HDA_CODEC_VOLUME("IEC958 Playback Volume", 0x1b, 0x0, HDA_OUTPUT),
3150         {
3151                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3152                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
3153                 /* identical with ad1983 */
3154                 .info = ad1983_spdif_route_info,
3155                 .get = ad1983_spdif_route_get,
3156                 .put = ad1983_spdif_route_put,
3157         },
3158         { } /* end */
3159 };
3160
3161 static struct snd_kcontrol_new ad1882_3stack_mixers[] = {
3162         HDA_CODEC_MUTE("Surround Playback Switch", 0x15, 0x0, HDA_OUTPUT),
3163         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x17, 1, 0x0, HDA_OUTPUT),
3164         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x17, 2, 0x0, HDA_OUTPUT),
3165         {
3166                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3167                 .name = "Channel Mode",
3168                 .info = ad198x_ch_mode_info,
3169                 .get = ad198x_ch_mode_get,
3170                 .put = ad198x_ch_mode_put,
3171         },
3172         { } /* end */
3173 };
3174
3175 static struct snd_kcontrol_new ad1882_6stack_mixers[] = {
3176         HDA_CODEC_MUTE("Surround Playback Switch", 0x16, 0x0, HDA_OUTPUT),
3177         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x24, 1, 0x0, HDA_OUTPUT),
3178         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x24, 2, 0x0, HDA_OUTPUT),
3179         { } /* end */
3180 };
3181
3182 static struct hda_verb ad1882_ch2_init[] = {
3183         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3184         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3185         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3186         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3187         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3188         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3189         { } /* end */
3190 };
3191
3192 static struct hda_verb ad1882_ch4_init[] = {
3193         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3194         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3195         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3196         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3197         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3198         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3199         { } /* end */
3200 };
3201
3202 static struct hda_verb ad1882_ch6_init[] = {
3203         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3204         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3205         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3206         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3207         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3208         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3209         { } /* end */
3210 };
3211
3212 static struct hda_channel_mode ad1882_modes[3] = {
3213         { 2, ad1882_ch2_init },
3214         { 4, ad1882_ch4_init },
3215         { 6, ad1882_ch6_init },
3216 };
3217
3218 /*
3219  * initialization verbs
3220  */
3221 static struct hda_verb ad1882_init_verbs[] = {
3222         /* DACs; mute as default */
3223         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3224         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3225         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3226         /* Port-A (HP) mixer */
3227         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3228         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3229         /* Port-A pin */
3230         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3231         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3232         /* HP selector - select DAC2 */
3233         {0x37, AC_VERB_SET_CONNECT_SEL, 0x1},
3234         /* Port-D (Line-out) mixer */
3235         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3236         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3237         /* Port-D pin */
3238         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3239         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3240         /* Mono-out mixer */
3241         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3242         {0x1e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3243         /* Mono-out pin */
3244         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3245         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3246         /* Port-B (front mic) pin */
3247         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3248         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3249         {0x39, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
3250         /* Port-C (line-in) pin */
3251         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3252         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3253         {0x3a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
3254         /* Port-C mixer - mute as input */
3255         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3256         {0x2c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3257         /* Port-E (mic-in) pin */
3258         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3259         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3260         {0x3c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO}, /* boost */
3261         /* Port-E mixer - mute as input */
3262         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3263         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3264         /* Port-F (surround) */
3265         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3266         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3267         /* Port-G (CLFE) */
3268         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3269         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3270         /* Analog mixer; mute as default */
3271         /* list: 0x39, 0x3a, 0x11, 0x12, 0x3c, 0x3b, 0x18, 0x1a */
3272         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3273         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3274         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3275         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3276         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3277         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
3278         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
3279         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
3280         /* Analog Mix output amp */
3281         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x1f}, /* 0dB */
3282         /* SPDIF output selector */
3283         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
3284         {0x02, AC_VERB_SET_CONNECT_SEL, 0x0}, /* PCM */
3285         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x27}, /* 0dB */
3286         { } /* end */
3287 };
3288
3289 #ifdef CONFIG_SND_HDA_POWER_SAVE
3290 static struct hda_amp_list ad1882_loopbacks[] = {
3291         { 0x20, HDA_INPUT, 0 }, /* Front Mic */
3292         { 0x20, HDA_INPUT, 1 }, /* Mic */
3293         { 0x20, HDA_INPUT, 4 }, /* Line */
3294         { 0x20, HDA_INPUT, 6 }, /* CD */
3295         { } /* end */
3296 };
3297 #endif
3298
3299 /* models */
3300 enum {
3301         AD1882_3STACK,
3302         AD1882_6STACK,
3303         AD1882_MODELS
3304 };
3305
3306 static const char *ad1882_models[AD1986A_MODELS] = {
3307         [AD1882_3STACK]         = "3stack",
3308         [AD1882_6STACK]         = "6stack",
3309 };
3310
3311
3312 static int patch_ad1882(struct hda_codec *codec)
3313 {
3314         struct ad198x_spec *spec;
3315         int board_config;
3316
3317         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3318         if (spec == NULL)
3319                 return -ENOMEM;
3320
3321         mutex_init(&spec->amp_mutex);
3322         codec->spec = spec;
3323
3324         spec->multiout.max_channels = 6;
3325         spec->multiout.num_dacs = 3;
3326         spec->multiout.dac_nids = ad1882_dac_nids;
3327         spec->multiout.dig_out_nid = AD1882_SPDIF_OUT;
3328         spec->num_adc_nids = ARRAY_SIZE(ad1882_adc_nids);
3329         spec->adc_nids = ad1882_adc_nids;
3330         spec->capsrc_nids = ad1882_capsrc_nids;
3331         spec->input_mux = &ad1882_capture_source;
3332         spec->num_mixers = 1;
3333         spec->mixers[0] = ad1882_base_mixers;
3334         spec->num_init_verbs = 1;
3335         spec->init_verbs[0] = ad1882_init_verbs;
3336         spec->spdif_route = 0;
3337 #ifdef CONFIG_SND_HDA_POWER_SAVE
3338         spec->loopback.amplist = ad1882_loopbacks;
3339 #endif
3340
3341         codec->patch_ops = ad198x_patch_ops;
3342
3343         /* override some parameters */
3344         board_config = snd_hda_check_board_config(codec, AD1882_MODELS,
3345                                                   ad1882_models, NULL);
3346         switch (board_config) {
3347         default:
3348         case AD1882_3STACK:
3349                 spec->num_mixers = 2;
3350                 spec->mixers[1] = ad1882_3stack_mixers;
3351                 spec->channel_mode = ad1882_modes;
3352                 spec->num_channel_mode = ARRAY_SIZE(ad1882_modes);
3353                 spec->need_dac_fix = 1;
3354                 spec->multiout.max_channels = 2;
3355                 spec->multiout.num_dacs = 1;
3356                 break;
3357         case AD1882_6STACK:
3358                 spec->num_mixers = 2;
3359                 spec->mixers[1] = ad1882_6stack_mixers;
3360                 break;
3361         }
3362         return 0;
3363 }
3364
3365
3366 /*
3367  * patch entries
3368  */
3369 struct hda_codec_preset snd_hda_preset_analog[] = {
3370         { .id = 0x11d41882, .name = "AD1882", .patch = patch_ad1882 },
3371         { .id = 0x11d41884, .name = "AD1884", .patch = patch_ad1884 },
3372         { .id = 0x11d41981, .name = "AD1981", .patch = patch_ad1981 },
3373         { .id = 0x11d41983, .name = "AD1983", .patch = patch_ad1983 },
3374         { .id = 0x11d41984, .name = "AD1984", .patch = patch_ad1984 },
3375         { .id = 0x11d41986, .name = "AD1986A", .patch = patch_ad1986a },
3376         { .id = 0x11d41988, .name = "AD1988", .patch = patch_ad1988 },
3377         { .id = 0x11d4198b, .name = "AD1988B", .patch = patch_ad1988 },
3378         {} /* terminator */
3379 };