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