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