xref: /openbmc/linux/sound/pci/emu10k1/emumixer.c (revision 64c70b1c)
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
3  *                   Takashi Iwai <tiwai@suse.de>
4  *                   Creative Labs, Inc.
5  *  Routines for control of EMU10K1 chips / mixer routines
6  *  Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
7  *
8  *  Copyright (c) by James Courtier-Dutton <James@superbug.co.uk>
9  *  	Added EMU 1010 support.
10  *
11  *  BUGS:
12  *    --
13  *
14  *  TODO:
15  *    --
16  *
17  *   This program is free software; you can redistribute it and/or modify
18  *   it under the terms of the GNU General Public License as published by
19  *   the Free Software Foundation; either version 2 of the License, or
20  *   (at your option) any later version.
21  *
22  *   This program is distributed in the hope that it will be useful,
23  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *   GNU General Public License for more details.
26  *
27  *   You should have received a copy of the GNU General Public License
28  *   along with this program; if not, write to the Free Software
29  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
30  *
31  */
32 
33 #include <sound/driver.h>
34 #include <linux/time.h>
35 #include <linux/init.h>
36 #include <sound/core.h>
37 #include <sound/emu10k1.h>
38 #include <linux/delay.h>
39 #include <sound/tlv.h>
40 
41 #include "p17v.h"
42 
43 #define AC97_ID_STAC9758	0x83847658
44 
45 static const DECLARE_TLV_DB_SCALE(snd_audigy_db_scale2, -10350, 50, 1); /* WM8775 gain scale */
46 
47 static int snd_emu10k1_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
48 {
49 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
50 	uinfo->count = 1;
51 	return 0;
52 }
53 
54 static int snd_emu10k1_spdif_get(struct snd_kcontrol *kcontrol,
55                                  struct snd_ctl_elem_value *ucontrol)
56 {
57 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
58 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
59 	unsigned long flags;
60 
61 	spin_lock_irqsave(&emu->reg_lock, flags);
62 	ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
63 	ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
64 	ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
65 	ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
66 	spin_unlock_irqrestore(&emu->reg_lock, flags);
67 	return 0;
68 }
69 
70 static int snd_emu10k1_spdif_get_mask(struct snd_kcontrol *kcontrol,
71 				      struct snd_ctl_elem_value *ucontrol)
72 {
73 	ucontrol->value.iec958.status[0] = 0xff;
74 	ucontrol->value.iec958.status[1] = 0xff;
75 	ucontrol->value.iec958.status[2] = 0xff;
76 	ucontrol->value.iec958.status[3] = 0xff;
77 	return 0;
78 }
79 
80 static char *emu1010_src_texts[] = {
81 	"Silence",
82 	"Dock Mic A",
83 	"Dock Mic B",
84 	"Dock ADC1 Left",
85 	"Dock ADC1 Right",
86 	"Dock ADC2 Left",
87 	"Dock ADC2 Right",
88 	"Dock ADC3 Left",
89 	"Dock ADC3 Right",
90 	"0202 ADC Left",
91 	"0202 ADC Right",
92 	"0202 SPDIF Left",
93 	"0202 SPDIF Right",
94 	"ADAT 0",
95 	"ADAT 1",
96 	"ADAT 2",
97 	"ADAT 3",
98 	"ADAT 4",
99 	"ADAT 5",
100 	"ADAT 6",
101 	"ADAT 7",
102 	"DSP 0",
103 	"DSP 1",
104 	"DSP 2",
105 	"DSP 3",
106 	"DSP 4",
107 	"DSP 5",
108 	"DSP 6",
109 	"DSP 7",
110 	"DSP 8",
111 	"DSP 9",
112 	"DSP 10",
113 	"DSP 11",
114 	"DSP 12",
115 	"DSP 13",
116 	"DSP 14",
117 	"DSP 15",
118 	"DSP 16",
119 	"DSP 17",
120 	"DSP 18",
121 	"DSP 19",
122 	"DSP 20",
123 	"DSP 21",
124 	"DSP 22",
125 	"DSP 23",
126 	"DSP 24",
127 	"DSP 25",
128 	"DSP 26",
129 	"DSP 27",
130 	"DSP 28",
131 	"DSP 29",
132 	"DSP 30",
133 	"DSP 31",
134 };
135 
136 static unsigned int emu1010_src_regs[] = {
137 	EMU_SRC_SILENCE,/* 0 */
138 	EMU_SRC_DOCK_MIC_A1, /* 1 */
139 	EMU_SRC_DOCK_MIC_B1, /* 2 */
140 	EMU_SRC_DOCK_ADC1_LEFT1, /* 3 */
141 	EMU_SRC_DOCK_ADC1_RIGHT1, /* 4 */
142 	EMU_SRC_DOCK_ADC2_LEFT1, /* 5 */
143 	EMU_SRC_DOCK_ADC2_RIGHT1, /* 6 */
144 	EMU_SRC_DOCK_ADC3_LEFT1, /* 7 */
145 	EMU_SRC_DOCK_ADC3_RIGHT1, /* 8 */
146 	EMU_SRC_HAMOA_ADC_LEFT1, /* 9 */
147 	EMU_SRC_HAMOA_ADC_RIGHT1, /* 10 */
148 	EMU_SRC_HANA_SPDIF_LEFT1, /* 11 */
149 	EMU_SRC_HANA_SPDIF_RIGHT1, /* 12 */
150 	EMU_SRC_HANA_ADAT, /* 13 */
151 	EMU_SRC_HANA_ADAT+1, /* 14 */
152 	EMU_SRC_HANA_ADAT+2, /* 15 */
153 	EMU_SRC_HANA_ADAT+3, /* 16 */
154 	EMU_SRC_HANA_ADAT+4, /* 17 */
155 	EMU_SRC_HANA_ADAT+5, /* 18 */
156 	EMU_SRC_HANA_ADAT+6, /* 19 */
157 	EMU_SRC_HANA_ADAT+7, /* 20 */
158 	EMU_SRC_ALICE_EMU32A, /* 21 */
159 	EMU_SRC_ALICE_EMU32A+1, /* 22 */
160 	EMU_SRC_ALICE_EMU32A+2, /* 23 */
161 	EMU_SRC_ALICE_EMU32A+3, /* 24 */
162 	EMU_SRC_ALICE_EMU32A+4, /* 25 */
163 	EMU_SRC_ALICE_EMU32A+5, /* 26 */
164 	EMU_SRC_ALICE_EMU32A+6, /* 27 */
165 	EMU_SRC_ALICE_EMU32A+7, /* 28 */
166 	EMU_SRC_ALICE_EMU32A+8, /* 29 */
167 	EMU_SRC_ALICE_EMU32A+9, /* 30 */
168 	EMU_SRC_ALICE_EMU32A+0xa, /* 31 */
169 	EMU_SRC_ALICE_EMU32A+0xb, /* 32 */
170 	EMU_SRC_ALICE_EMU32A+0xc, /* 33 */
171 	EMU_SRC_ALICE_EMU32A+0xd, /* 34 */
172 	EMU_SRC_ALICE_EMU32A+0xe, /* 35 */
173 	EMU_SRC_ALICE_EMU32A+0xf, /* 36 */
174 	EMU_SRC_ALICE_EMU32B, /* 37 */
175 	EMU_SRC_ALICE_EMU32B+1, /* 38 */
176 	EMU_SRC_ALICE_EMU32B+2, /* 39 */
177 	EMU_SRC_ALICE_EMU32B+3, /* 40 */
178 	EMU_SRC_ALICE_EMU32B+4, /* 41 */
179 	EMU_SRC_ALICE_EMU32B+5, /* 42 */
180 	EMU_SRC_ALICE_EMU32B+6, /* 43 */
181 	EMU_SRC_ALICE_EMU32B+7, /* 44 */
182 	EMU_SRC_ALICE_EMU32B+8, /* 45 */
183 	EMU_SRC_ALICE_EMU32B+9, /* 46 */
184 	EMU_SRC_ALICE_EMU32B+0xa, /* 47 */
185 	EMU_SRC_ALICE_EMU32B+0xb, /* 48 */
186 	EMU_SRC_ALICE_EMU32B+0xc, /* 49 */
187 	EMU_SRC_ALICE_EMU32B+0xd, /* 50 */
188 	EMU_SRC_ALICE_EMU32B+0xe, /* 51 */
189 	EMU_SRC_ALICE_EMU32B+0xf, /* 52 */
190 };
191 
192 static unsigned int emu1010_output_dst[] = {
193 	EMU_DST_DOCK_DAC1_LEFT1, /* 0 */
194 	EMU_DST_DOCK_DAC1_RIGHT1, /* 1 */
195 	EMU_DST_DOCK_DAC2_LEFT1, /* 2 */
196 	EMU_DST_DOCK_DAC2_RIGHT1, /* 3 */
197 	EMU_DST_DOCK_DAC3_LEFT1, /* 4 */
198 	EMU_DST_DOCK_DAC3_RIGHT1, /* 5 */
199 	EMU_DST_DOCK_DAC4_LEFT1, /* 6 */
200 	EMU_DST_DOCK_DAC4_RIGHT1, /* 7 */
201 	EMU_DST_DOCK_PHONES_LEFT1, /* 8 */
202 	EMU_DST_DOCK_PHONES_RIGHT1, /* 9 */
203 	EMU_DST_DOCK_SPDIF_LEFT1, /* 10 */
204 	EMU_DST_DOCK_SPDIF_RIGHT1, /* 11 */
205 	EMU_DST_HANA_SPDIF_LEFT1, /* 12 */
206 	EMU_DST_HANA_SPDIF_RIGHT1, /* 13 */
207 	EMU_DST_HAMOA_DAC_LEFT1, /* 14 */
208 	EMU_DST_HAMOA_DAC_RIGHT1, /* 15 */
209 	EMU_DST_HANA_ADAT, /* 16 */
210 	EMU_DST_HANA_ADAT+1, /* 17 */
211 	EMU_DST_HANA_ADAT+2, /* 18 */
212 	EMU_DST_HANA_ADAT+3, /* 19 */
213 	EMU_DST_HANA_ADAT+4, /* 20 */
214 	EMU_DST_HANA_ADAT+5, /* 21 */
215 	EMU_DST_HANA_ADAT+6, /* 22 */
216 	EMU_DST_HANA_ADAT+7, /* 23 */
217 };
218 
219 static unsigned int emu1010_input_dst[] = {
220 	EMU_DST_ALICE2_EMU32_0,
221 	EMU_DST_ALICE2_EMU32_1,
222 	EMU_DST_ALICE2_EMU32_2,
223 	EMU_DST_ALICE2_EMU32_3,
224 	EMU_DST_ALICE2_EMU32_4,
225 	EMU_DST_ALICE2_EMU32_5,
226 	EMU_DST_ALICE2_EMU32_6,
227 	EMU_DST_ALICE2_EMU32_7,
228 	EMU_DST_ALICE2_EMU32_8,
229 	EMU_DST_ALICE2_EMU32_9,
230 	EMU_DST_ALICE2_EMU32_A,
231 	EMU_DST_ALICE2_EMU32_B,
232 	EMU_DST_ALICE2_EMU32_C,
233 	EMU_DST_ALICE2_EMU32_D,
234 	EMU_DST_ALICE2_EMU32_E,
235 	EMU_DST_ALICE2_EMU32_F,
236 	EMU_DST_ALICE_I2S0_LEFT,
237 	EMU_DST_ALICE_I2S0_RIGHT,
238 	EMU_DST_ALICE_I2S1_LEFT,
239 	EMU_DST_ALICE_I2S1_RIGHT,
240 	EMU_DST_ALICE_I2S2_LEFT,
241 	EMU_DST_ALICE_I2S2_RIGHT,
242 };
243 
244 static int snd_emu1010_input_output_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
245 {
246 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
247 	uinfo->count = 1;
248 	uinfo->value.enumerated.items = 53;
249 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
250 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
251 	strcpy(uinfo->value.enumerated.name, emu1010_src_texts[uinfo->value.enumerated.item]);
252 	return 0;
253 }
254 
255 static int snd_emu1010_output_source_get(struct snd_kcontrol *kcontrol,
256                                  struct snd_ctl_elem_value *ucontrol)
257 {
258 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
259 	int channel;
260 
261 	channel = (kcontrol->private_value) & 0xff;
262 	ucontrol->value.enumerated.item[0] = emu->emu1010.output_source[channel];
263 	return 0;
264 }
265 
266 static int snd_emu1010_output_source_put(struct snd_kcontrol *kcontrol,
267                                  struct snd_ctl_elem_value *ucontrol)
268 {
269 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
270 	int change = 0;
271 	unsigned int val;
272 	int channel;
273 
274 	channel = (kcontrol->private_value) & 0xff;
275 	if (emu->emu1010.output_source[channel] != ucontrol->value.enumerated.item[0]) {
276 		val = emu->emu1010.output_source[channel] = ucontrol->value.enumerated.item[0];
277 		change = 1;
278 		snd_emu1010_fpga_link_dst_src_write(emu,
279 			emu1010_output_dst[channel], emu1010_src_regs[val]);
280 	}
281 	return change;
282 }
283 
284 static int snd_emu1010_input_source_get(struct snd_kcontrol *kcontrol,
285                                  struct snd_ctl_elem_value *ucontrol)
286 {
287 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
288 	int channel;
289 
290 	channel = (kcontrol->private_value) & 0xff;
291 	ucontrol->value.enumerated.item[0] = emu->emu1010.input_source[channel];
292 	return 0;
293 }
294 
295 static int snd_emu1010_input_source_put(struct snd_kcontrol *kcontrol,
296                                  struct snd_ctl_elem_value *ucontrol)
297 {
298 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
299 	int change = 0;
300 	unsigned int val;
301 	int channel;
302 
303 	channel = (kcontrol->private_value) & 0xff;
304 	if (emu->emu1010.input_source[channel] != ucontrol->value.enumerated.item[0]) {
305 		val = emu->emu1010.input_source[channel] = ucontrol->value.enumerated.item[0];
306 		change = 1;
307 		snd_emu1010_fpga_link_dst_src_write(emu,
308 			emu1010_input_dst[channel], emu1010_src_regs[val]);
309 	}
310 	return change;
311 }
312 
313 #define EMU1010_SOURCE_OUTPUT(xname,chid) \
314 {								\
315 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
316 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
317 	.info =  snd_emu1010_input_output_source_info,		\
318 	.get =   snd_emu1010_output_source_get,			\
319 	.put =   snd_emu1010_output_source_put,			\
320 	.private_value = chid					\
321 }
322 
323 static struct snd_kcontrol_new snd_emu1010_output_enum_ctls[] __devinitdata = {
324 	EMU1010_SOURCE_OUTPUT("Dock DAC1 Left Playback Enum", 0),
325 	EMU1010_SOURCE_OUTPUT("Dock DAC1 Right Playback Enum", 1),
326 	EMU1010_SOURCE_OUTPUT("Dock DAC2 Left Playback Enum", 2),
327 	EMU1010_SOURCE_OUTPUT("Dock DAC2 Right Playback Enum", 3),
328 	EMU1010_SOURCE_OUTPUT("Dock DAC3 Left Playback Enum", 4),
329 	EMU1010_SOURCE_OUTPUT("Dock DAC3 Right Playback Enum", 5),
330 	EMU1010_SOURCE_OUTPUT("Dock DAC4 Left Playback Enum", 6),
331 	EMU1010_SOURCE_OUTPUT("Dock DAC4 Right Playback Enum", 7),
332 	EMU1010_SOURCE_OUTPUT("Dock Phones Left Playback Enum", 8),
333 	EMU1010_SOURCE_OUTPUT("Dock Phones Right Playback Enum", 9),
334 	EMU1010_SOURCE_OUTPUT("Dock SPDIF Left Playback Enum", 0xa),
335 	EMU1010_SOURCE_OUTPUT("Dock SPDIF Right Playback Enum", 0xb),
336 	EMU1010_SOURCE_OUTPUT("1010 SPDIF Left Playback Enum", 0xc),
337 	EMU1010_SOURCE_OUTPUT("1010 SPDIF Right Playback Enum", 0xd),
338 	EMU1010_SOURCE_OUTPUT("0202 DAC Left Playback Enum", 0xe),
339 	EMU1010_SOURCE_OUTPUT("0202 DAC Right Playback Enum", 0xf),
340 	EMU1010_SOURCE_OUTPUT("1010 ADAT 0 Playback Enum", 0x10),
341 	EMU1010_SOURCE_OUTPUT("1010 ADAT 1 Playback Enum", 0x11),
342 	EMU1010_SOURCE_OUTPUT("1010 ADAT 2 Playback Enum", 0x12),
343 	EMU1010_SOURCE_OUTPUT("1010 ADAT 3 Playback Enum", 0x13),
344 	EMU1010_SOURCE_OUTPUT("1010 ADAT 4 Playback Enum", 0x14),
345 	EMU1010_SOURCE_OUTPUT("1010 ADAT 5 Playback Enum", 0x15),
346 	EMU1010_SOURCE_OUTPUT("1010 ADAT 6 Playback Enum", 0x16),
347 	EMU1010_SOURCE_OUTPUT("1010 ADAT 7 Playback Enum", 0x17),
348 };
349 
350 #define EMU1010_SOURCE_INPUT(xname,chid) \
351 {								\
352 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
353 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
354 	.info =  snd_emu1010_input_output_source_info,		\
355 	.get =   snd_emu1010_input_source_get,			\
356 	.put =   snd_emu1010_input_source_put,			\
357 	.private_value = chid					\
358 }
359 
360 static struct snd_kcontrol_new snd_emu1010_input_enum_ctls[] __devinitdata = {
361 	EMU1010_SOURCE_INPUT("DSP 0 Capture Enum", 0),
362 	EMU1010_SOURCE_INPUT("DSP 1 Capture Enum", 1),
363 	EMU1010_SOURCE_INPUT("DSP 2 Capture Enum", 2),
364 	EMU1010_SOURCE_INPUT("DSP 3 Capture Enum", 3),
365 	EMU1010_SOURCE_INPUT("DSP 4 Capture Enum", 4),
366 	EMU1010_SOURCE_INPUT("DSP 5 Capture Enum", 5),
367 	EMU1010_SOURCE_INPUT("DSP 6 Capture Enum", 6),
368 	EMU1010_SOURCE_INPUT("DSP 7 Capture Enum", 7),
369 	EMU1010_SOURCE_INPUT("DSP 8 Capture Enum", 8),
370 	EMU1010_SOURCE_INPUT("DSP 9 Capture Enum", 9),
371 	EMU1010_SOURCE_INPUT("DSP A Capture Enum", 0xa),
372 	EMU1010_SOURCE_INPUT("DSP B Capture Enum", 0xb),
373 	EMU1010_SOURCE_INPUT("DSP C Capture Enum", 0xc),
374 	EMU1010_SOURCE_INPUT("DSP D Capture Enum", 0xd),
375 	EMU1010_SOURCE_INPUT("DSP E Capture Enum", 0xe),
376 	EMU1010_SOURCE_INPUT("DSP F Capture Enum", 0xf),
377 	EMU1010_SOURCE_INPUT("DSP 10 Capture Enum", 0x10),
378 	EMU1010_SOURCE_INPUT("DSP 11 Capture Enum", 0x11),
379 	EMU1010_SOURCE_INPUT("DSP 12 Capture Enum", 0x12),
380 	EMU1010_SOURCE_INPUT("DSP 13 Capture Enum", 0x13),
381 	EMU1010_SOURCE_INPUT("DSP 14 Capture Enum", 0x14),
382 	EMU1010_SOURCE_INPUT("DSP 15 Capture Enum", 0x15),
383 };
384 
385 
386 
387 
388 static int snd_emu1010_adc_pads_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
389 {
390 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
391 	uinfo->count = 1;
392 	uinfo->value.integer.min = 0;
393 	uinfo->value.integer.max = 1;
394 	return 0;
395 }
396 
397 static int snd_emu1010_adc_pads_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
398 {
399 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
400 	unsigned int mask = kcontrol->private_value & 0xff;
401 	ucontrol->value.integer.value[0] = (emu->emu1010.adc_pads & mask) ? 1 : 0;
402 	return 0;
403 }
404 
405 static int snd_emu1010_adc_pads_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
406 {
407 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
408 	unsigned int mask = kcontrol->private_value & 0xff;
409 	unsigned int val, cache;
410 	val = ucontrol->value.integer.value[0];
411 	cache = emu->emu1010.adc_pads;
412 	if (val == 1)
413 		cache = cache | mask;
414 	else
415 		cache = cache & ~mask;
416 	if (cache != emu->emu1010.adc_pads) {
417 		snd_emu1010_fpga_write(emu, EMU_HANA_ADC_PADS, cache );
418 	        emu->emu1010.adc_pads = cache;
419 	}
420 
421 	return 0;
422 }
423 
424 
425 
426 #define EMU1010_ADC_PADS(xname,chid) \
427 {								\
428 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
429 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
430 	.info =  snd_emu1010_adc_pads_info,			\
431 	.get =   snd_emu1010_adc_pads_get,			\
432 	.put =   snd_emu1010_adc_pads_put,			\
433 	.private_value = chid					\
434 }
435 
436 static struct snd_kcontrol_new snd_emu1010_adc_pads[] __devinitdata = {
437 	EMU1010_ADC_PADS("ADC1 14dB PAD Audio Dock Capture Switch", EMU_HANA_DOCK_ADC_PAD1),
438 	EMU1010_ADC_PADS("ADC2 14dB PAD Audio Dock Capture Switch", EMU_HANA_DOCK_ADC_PAD2),
439 	EMU1010_ADC_PADS("ADC3 14dB PAD Audio Dock Capture Switch", EMU_HANA_DOCK_ADC_PAD3),
440 	EMU1010_ADC_PADS("ADC1 14dB PAD 0202 Capture Switch", EMU_HANA_0202_ADC_PAD1),
441 };
442 
443 static int snd_emu1010_dac_pads_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
444 {
445 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
446 	uinfo->count = 1;
447 	uinfo->value.integer.min = 0;
448 	uinfo->value.integer.max = 1;
449 	return 0;
450 }
451 
452 static int snd_emu1010_dac_pads_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
453 {
454 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
455 	unsigned int mask = kcontrol->private_value & 0xff;
456 	ucontrol->value.integer.value[0] = (emu->emu1010.dac_pads & mask) ? 1 : 0;
457 	return 0;
458 }
459 
460 static int snd_emu1010_dac_pads_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
461 {
462 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
463 	unsigned int mask = kcontrol->private_value & 0xff;
464 	unsigned int val, cache;
465 	val = ucontrol->value.integer.value[0];
466 	cache = emu->emu1010.dac_pads;
467 	if (val == 1)
468 		cache = cache | mask;
469 	else
470 		cache = cache & ~mask;
471 	if (cache != emu->emu1010.dac_pads) {
472 		snd_emu1010_fpga_write(emu, EMU_HANA_DAC_PADS, cache );
473 	        emu->emu1010.dac_pads = cache;
474 	}
475 
476 	return 0;
477 }
478 
479 
480 
481 #define EMU1010_DAC_PADS(xname,chid) \
482 {								\
483 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
484 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
485 	.info =  snd_emu1010_dac_pads_info,			\
486 	.get =   snd_emu1010_dac_pads_get,			\
487 	.put =   snd_emu1010_dac_pads_put,			\
488 	.private_value = chid					\
489 }
490 
491 static struct snd_kcontrol_new snd_emu1010_dac_pads[] __devinitdata = {
492 	EMU1010_DAC_PADS("DAC1 Audio Dock 14dB PAD Playback Switch", EMU_HANA_DOCK_DAC_PAD1),
493 	EMU1010_DAC_PADS("DAC2 Audio Dock 14dB PAD Playback Switch", EMU_HANA_DOCK_DAC_PAD2),
494 	EMU1010_DAC_PADS("DAC3 Audio Dock 14dB PAD Playback Switch", EMU_HANA_DOCK_DAC_PAD3),
495 	EMU1010_DAC_PADS("DAC4 Audio Dock 14dB PAD Playback Switch", EMU_HANA_DOCK_DAC_PAD4),
496 	EMU1010_DAC_PADS("DAC1 0202 14dB PAD Playback Switch", EMU_HANA_0202_DAC_PAD1),
497 };
498 
499 
500 static int snd_emu1010_internal_clock_info(struct snd_kcontrol *kcontrol,
501 					  struct snd_ctl_elem_info *uinfo)
502 {
503 	static char *texts[2] = {
504 		"44100", "48000"
505 	};
506 
507 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
508 	uinfo->count = 1;
509 	uinfo->value.enumerated.items = 2;
510 	if (uinfo->value.enumerated.item > 1)
511                 uinfo->value.enumerated.item = 1;
512 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
513 	return 0;
514 }
515 
516 static int snd_emu1010_internal_clock_get(struct snd_kcontrol *kcontrol,
517 					struct snd_ctl_elem_value *ucontrol)
518 {
519 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
520 
521 	ucontrol->value.enumerated.item[0] = emu->emu1010.internal_clock;
522 	return 0;
523 }
524 
525 static int snd_emu1010_internal_clock_put(struct snd_kcontrol *kcontrol,
526 					struct snd_ctl_elem_value *ucontrol)
527 {
528 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
529 	unsigned int val;
530 	int change = 0;
531 
532 	val = ucontrol->value.enumerated.item[0] ;
533 	change = (emu->emu1010.internal_clock != val);
534 	if (change) {
535 		emu->emu1010.internal_clock = val;
536 		switch (val) {
537 		case 0:
538 			/* 44100 */
539 			/* Mute all */
540 			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_MUTE );
541 			/* Default fallback clock 48kHz */
542 			snd_emu1010_fpga_write(emu, EMU_HANA_DEFCLOCK, EMU_HANA_DEFCLOCK_44_1K );
543 			/* Word Clock source, Internal 44.1kHz x1 */
544 			snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK,
545 			EMU_HANA_WCLOCK_INT_44_1K | EMU_HANA_WCLOCK_1X );
546 			/* Set LEDs on Audio Dock */
547 			snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_LEDS_2,
548 				EMU_HANA_DOCK_LEDS_2_44K | EMU_HANA_DOCK_LEDS_2_LOCK );
549 			/* Allow DLL to settle */
550 			msleep(10);
551 			/* Unmute all */
552 			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_UNMUTE );
553 			break;
554 		case 1:
555 			/* 48000 */
556 			/* Mute all */
557 			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_MUTE );
558 			/* Default fallback clock 48kHz */
559 			snd_emu1010_fpga_write(emu, EMU_HANA_DEFCLOCK, EMU_HANA_DEFCLOCK_48K );
560 			/* Word Clock source, Internal 48kHz x1 */
561 			snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK,
562 				EMU_HANA_WCLOCK_INT_48K | EMU_HANA_WCLOCK_1X );
563 			/* Set LEDs on Audio Dock */
564 			snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_LEDS_2,
565 				EMU_HANA_DOCK_LEDS_2_48K | EMU_HANA_DOCK_LEDS_2_LOCK );
566 			/* Allow DLL to settle */
567 			msleep(10);
568 			/* Unmute all */
569 			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_UNMUTE );
570 			break;
571 		}
572 	}
573         return change;
574 }
575 
576 static struct snd_kcontrol_new snd_emu1010_internal_clock =
577 {
578 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
579 	.iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
580 	.name =         "Clock Internal Rate",
581 	.count =	1,
582 	.info =         snd_emu1010_internal_clock_info,
583 	.get =          snd_emu1010_internal_clock_get,
584 	.put =          snd_emu1010_internal_clock_put
585 };
586 
587 static int snd_audigy_i2c_capture_source_info(struct snd_kcontrol *kcontrol,
588 					  struct snd_ctl_elem_info *uinfo)
589 {
590 #if 0
591 	static char *texts[4] = {
592 		"Unknown1", "Unknown2", "Mic", "Line"
593 	};
594 #endif
595 	static char *texts[2] = {
596 		"Mic", "Line"
597 	};
598 
599 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
600 	uinfo->count = 1;
601 	uinfo->value.enumerated.items = 2;
602 	if (uinfo->value.enumerated.item > 1)
603                 uinfo->value.enumerated.item = 1;
604 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
605 	return 0;
606 }
607 
608 static int snd_audigy_i2c_capture_source_get(struct snd_kcontrol *kcontrol,
609 					struct snd_ctl_elem_value *ucontrol)
610 {
611 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
612 
613 	ucontrol->value.enumerated.item[0] = emu->i2c_capture_source;
614 	return 0;
615 }
616 
617 static int snd_audigy_i2c_capture_source_put(struct snd_kcontrol *kcontrol,
618 					struct snd_ctl_elem_value *ucontrol)
619 {
620 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
621 	unsigned int source_id;
622 	unsigned int ngain, ogain;
623 	u32 gpio;
624 	int change = 0;
625 	unsigned long flags;
626 	u32 source;
627 	/* If the capture source has changed,
628 	 * update the capture volume from the cached value
629 	 * for the particular source.
630 	 */
631 	source_id = ucontrol->value.enumerated.item[0]; /* Use 2 and 3 */
632 	change = (emu->i2c_capture_source != source_id);
633 	if (change) {
634 		snd_emu10k1_i2c_write(emu, ADC_MUX, 0); /* Mute input */
635 		spin_lock_irqsave(&emu->emu_lock, flags);
636 		gpio = inl(emu->port + A_IOCFG);
637 		if (source_id==0)
638 			outl(gpio | 0x4, emu->port + A_IOCFG);
639 		else
640 			outl(gpio & ~0x4, emu->port + A_IOCFG);
641 		spin_unlock_irqrestore(&emu->emu_lock, flags);
642 
643 		ngain = emu->i2c_capture_volume[source_id][0]; /* Left */
644 		ogain = emu->i2c_capture_volume[emu->i2c_capture_source][0]; /* Left */
645 		if (ngain != ogain)
646 			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff));
647 		ngain = emu->i2c_capture_volume[source_id][1]; /* Right */
648 		ogain = emu->i2c_capture_volume[emu->i2c_capture_source][1]; /* Right */
649 		if (ngain != ogain)
650 			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
651 
652 		source = 1 << (source_id + 2);
653 		snd_emu10k1_i2c_write(emu, ADC_MUX, source); /* Set source */
654 		emu->i2c_capture_source = source_id;
655 	}
656         return change;
657 }
658 
659 static struct snd_kcontrol_new snd_audigy_i2c_capture_source =
660 {
661 		.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
662 		.name =		"Capture Source",
663 		.info =		snd_audigy_i2c_capture_source_info,
664 		.get =		snd_audigy_i2c_capture_source_get,
665 		.put =		snd_audigy_i2c_capture_source_put
666 };
667 
668 static int snd_audigy_i2c_volume_info(struct snd_kcontrol *kcontrol,
669 				  struct snd_ctl_elem_info *uinfo)
670 {
671 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
672 	uinfo->count = 2;
673 	uinfo->value.integer.min = 0;
674 	uinfo->value.integer.max = 255;
675 	return 0;
676 }
677 
678 static int snd_audigy_i2c_volume_get(struct snd_kcontrol *kcontrol,
679 				 struct snd_ctl_elem_value *ucontrol)
680 {
681 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
682 	int source_id;
683 
684 	source_id = kcontrol->private_value;
685 
686 	ucontrol->value.integer.value[0] = emu->i2c_capture_volume[source_id][0];
687 	ucontrol->value.integer.value[1] = emu->i2c_capture_volume[source_id][1];
688 	return 0;
689 }
690 
691 static int snd_audigy_i2c_volume_put(struct snd_kcontrol *kcontrol,
692 				 struct snd_ctl_elem_value *ucontrol)
693 {
694 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
695 	unsigned int ogain;
696 	unsigned int ngain;
697 	int source_id;
698 	int change = 0;
699 
700 	source_id = kcontrol->private_value;
701 	ogain = emu->i2c_capture_volume[source_id][0]; /* Left */
702 	ngain = ucontrol->value.integer.value[0];
703 	if (ngain > 0xff)
704 		return 0;
705 	if (ogain != ngain) {
706 		if (emu->i2c_capture_source == source_id)
707 			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff) );
708 		emu->i2c_capture_volume[source_id][0] = ucontrol->value.integer.value[0];
709 		change = 1;
710 	}
711 	ogain = emu->i2c_capture_volume[source_id][1]; /* Right */
712 	ngain = ucontrol->value.integer.value[1];
713 	if (ngain > 0xff)
714 		return 0;
715 	if (ogain != ngain) {
716 		if (emu->i2c_capture_source == source_id)
717 			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
718 		emu->i2c_capture_volume[source_id][1] = ucontrol->value.integer.value[1];
719 		change = 1;
720 	}
721 
722 	return change;
723 }
724 
725 #define I2C_VOLUME(xname,chid) \
726 {								\
727 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
728 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |		\
729 	          SNDRV_CTL_ELEM_ACCESS_TLV_READ,		\
730 	.info =  snd_audigy_i2c_volume_info,			\
731 	.get =   snd_audigy_i2c_volume_get,			\
732 	.put =   snd_audigy_i2c_volume_put,			\
733 	.tlv = { .p = snd_audigy_db_scale2 },			\
734 	.private_value = chid					\
735 }
736 
737 
738 static struct snd_kcontrol_new snd_audigy_i2c_volume_ctls[] __devinitdata = {
739 	I2C_VOLUME("Mic Capture Volume", 0),
740 	I2C_VOLUME("Line Capture Volume", 0)
741 };
742 
743 #if 0
744 static int snd_audigy_spdif_output_rate_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
745 {
746 	static char *texts[] = {"44100", "48000", "96000"};
747 
748 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
749 	uinfo->count = 1;
750 	uinfo->value.enumerated.items = 3;
751 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
752 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
753 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
754 	return 0;
755 }
756 
757 static int snd_audigy_spdif_output_rate_get(struct snd_kcontrol *kcontrol,
758                                  struct snd_ctl_elem_value *ucontrol)
759 {
760 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
761 	unsigned int tmp;
762 	unsigned long flags;
763 
764 
765 	spin_lock_irqsave(&emu->reg_lock, flags);
766 	tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
767 	switch (tmp & A_SPDIF_RATE_MASK) {
768 	case A_SPDIF_44100:
769 		ucontrol->value.enumerated.item[0] = 0;
770 		break;
771 	case A_SPDIF_48000:
772 		ucontrol->value.enumerated.item[0] = 1;
773 		break;
774 	case A_SPDIF_96000:
775 		ucontrol->value.enumerated.item[0] = 2;
776 		break;
777 	default:
778 		ucontrol->value.enumerated.item[0] = 1;
779 	}
780 	spin_unlock_irqrestore(&emu->reg_lock, flags);
781 	return 0;
782 }
783 
784 static int snd_audigy_spdif_output_rate_put(struct snd_kcontrol *kcontrol,
785                                  struct snd_ctl_elem_value *ucontrol)
786 {
787 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
788 	int change;
789 	unsigned int reg, val, tmp;
790 	unsigned long flags;
791 
792 	switch(ucontrol->value.enumerated.item[0]) {
793 	case 0:
794 		val = A_SPDIF_44100;
795 		break;
796 	case 1:
797 		val = A_SPDIF_48000;
798 		break;
799 	case 2:
800 		val = A_SPDIF_96000;
801 		break;
802 	default:
803 		val = A_SPDIF_48000;
804 		break;
805 	}
806 
807 
808 	spin_lock_irqsave(&emu->reg_lock, flags);
809 	reg = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
810 	tmp = reg & ~A_SPDIF_RATE_MASK;
811 	tmp |= val;
812 	if ((change = (tmp != reg)))
813 		snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
814 	spin_unlock_irqrestore(&emu->reg_lock, flags);
815 	return change;
816 }
817 
818 static struct snd_kcontrol_new snd_audigy_spdif_output_rate =
819 {
820 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
821 	.iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
822 	.name =         "Audigy SPDIF Output Sample Rate",
823 	.count =	1,
824 	.info =         snd_audigy_spdif_output_rate_info,
825 	.get =          snd_audigy_spdif_output_rate_get,
826 	.put =          snd_audigy_spdif_output_rate_put
827 };
828 #endif
829 
830 static int snd_emu10k1_spdif_put(struct snd_kcontrol *kcontrol,
831                                  struct snd_ctl_elem_value *ucontrol)
832 {
833 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
834 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
835 	int change;
836 	unsigned int val;
837 	unsigned long flags;
838 
839 	val = (ucontrol->value.iec958.status[0] << 0) |
840 	      (ucontrol->value.iec958.status[1] << 8) |
841 	      (ucontrol->value.iec958.status[2] << 16) |
842 	      (ucontrol->value.iec958.status[3] << 24);
843 	spin_lock_irqsave(&emu->reg_lock, flags);
844 	change = val != emu->spdif_bits[idx];
845 	if (change) {
846 		snd_emu10k1_ptr_write(emu, SPCS0 + idx, 0, val);
847 		emu->spdif_bits[idx] = val;
848 	}
849 	spin_unlock_irqrestore(&emu->reg_lock, flags);
850 	return change;
851 }
852 
853 static struct snd_kcontrol_new snd_emu10k1_spdif_mask_control =
854 {
855 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
856 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
857 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
858 	.count =	4,
859 	.info =         snd_emu10k1_spdif_info,
860 	.get =          snd_emu10k1_spdif_get_mask
861 };
862 
863 static struct snd_kcontrol_new snd_emu10k1_spdif_control =
864 {
865 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
866 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
867 	.count =	4,
868 	.info =         snd_emu10k1_spdif_info,
869 	.get =          snd_emu10k1_spdif_get,
870 	.put =          snd_emu10k1_spdif_put
871 };
872 
873 
874 static void update_emu10k1_fxrt(struct snd_emu10k1 *emu, int voice, unsigned char *route)
875 {
876 	if (emu->audigy) {
877 		snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
878 				      snd_emu10k1_compose_audigy_fxrt1(route));
879 		snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
880 				      snd_emu10k1_compose_audigy_fxrt2(route));
881 	} else {
882 		snd_emu10k1_ptr_write(emu, FXRT, voice,
883 				      snd_emu10k1_compose_send_routing(route));
884 	}
885 }
886 
887 static void update_emu10k1_send_volume(struct snd_emu10k1 *emu, int voice, unsigned char *volume)
888 {
889 	snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_A, voice, volume[0]);
890 	snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_B, voice, volume[1]);
891 	snd_emu10k1_ptr_write(emu, PSST_FXSENDAMOUNT_C, voice, volume[2]);
892 	snd_emu10k1_ptr_write(emu, DSL_FXSENDAMOUNT_D, voice, volume[3]);
893 	if (emu->audigy) {
894 		unsigned int val = ((unsigned int)volume[4] << 24) |
895 			((unsigned int)volume[5] << 16) |
896 			((unsigned int)volume[6] << 8) |
897 			(unsigned int)volume[7];
898 		snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice, val);
899 	}
900 }
901 
902 /* PCM stream controls */
903 
904 static int snd_emu10k1_send_routing_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
905 {
906 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
907 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
908 	uinfo->count = emu->audigy ? 3*8 : 3*4;
909 	uinfo->value.integer.min = 0;
910 	uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
911 	return 0;
912 }
913 
914 static int snd_emu10k1_send_routing_get(struct snd_kcontrol *kcontrol,
915                                         struct snd_ctl_elem_value *ucontrol)
916 {
917 	unsigned long flags;
918 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
919 	struct snd_emu10k1_pcm_mixer *mix =
920 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
921 	int voice, idx;
922 	int num_efx = emu->audigy ? 8 : 4;
923 	int mask = emu->audigy ? 0x3f : 0x0f;
924 
925 	spin_lock_irqsave(&emu->reg_lock, flags);
926 	for (voice = 0; voice < 3; voice++)
927 		for (idx = 0; idx < num_efx; idx++)
928 			ucontrol->value.integer.value[(voice * num_efx) + idx] =
929 				mix->send_routing[voice][idx] & mask;
930 	spin_unlock_irqrestore(&emu->reg_lock, flags);
931 	return 0;
932 }
933 
934 static int snd_emu10k1_send_routing_put(struct snd_kcontrol *kcontrol,
935                                         struct snd_ctl_elem_value *ucontrol)
936 {
937 	unsigned long flags;
938 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
939 	struct snd_emu10k1_pcm_mixer *mix =
940 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
941 	int change = 0, voice, idx, val;
942 	int num_efx = emu->audigy ? 8 : 4;
943 	int mask = emu->audigy ? 0x3f : 0x0f;
944 
945 	spin_lock_irqsave(&emu->reg_lock, flags);
946 	for (voice = 0; voice < 3; voice++)
947 		for (idx = 0; idx < num_efx; idx++) {
948 			val = ucontrol->value.integer.value[(voice * num_efx) + idx] & mask;
949 			if (mix->send_routing[voice][idx] != val) {
950 				mix->send_routing[voice][idx] = val;
951 				change = 1;
952 			}
953 		}
954 	if (change && mix->epcm) {
955 		if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
956 			update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
957 					    &mix->send_routing[1][0]);
958 			update_emu10k1_fxrt(emu, mix->epcm->voices[1]->number,
959 					    &mix->send_routing[2][0]);
960 		} else if (mix->epcm->voices[0]) {
961 			update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
962 					    &mix->send_routing[0][0]);
963 		}
964 	}
965 	spin_unlock_irqrestore(&emu->reg_lock, flags);
966 	return change;
967 }
968 
969 static struct snd_kcontrol_new snd_emu10k1_send_routing_control =
970 {
971 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
972 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
973 	.name =         "EMU10K1 PCM Send Routing",
974 	.count =	32,
975 	.info =         snd_emu10k1_send_routing_info,
976 	.get =          snd_emu10k1_send_routing_get,
977 	.put =          snd_emu10k1_send_routing_put
978 };
979 
980 static int snd_emu10k1_send_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
981 {
982 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
983 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
984 	uinfo->count = emu->audigy ? 3*8 : 3*4;
985 	uinfo->value.integer.min = 0;
986 	uinfo->value.integer.max = 255;
987 	return 0;
988 }
989 
990 static int snd_emu10k1_send_volume_get(struct snd_kcontrol *kcontrol,
991                                        struct snd_ctl_elem_value *ucontrol)
992 {
993 	unsigned long flags;
994 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
995 	struct snd_emu10k1_pcm_mixer *mix =
996 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
997 	int idx;
998 	int num_efx = emu->audigy ? 8 : 4;
999 
1000 	spin_lock_irqsave(&emu->reg_lock, flags);
1001 	for (idx = 0; idx < 3*num_efx; idx++)
1002 		ucontrol->value.integer.value[idx] = mix->send_volume[idx/num_efx][idx%num_efx];
1003 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1004 	return 0;
1005 }
1006 
1007 static int snd_emu10k1_send_volume_put(struct snd_kcontrol *kcontrol,
1008                                        struct snd_ctl_elem_value *ucontrol)
1009 {
1010 	unsigned long flags;
1011 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1012 	struct snd_emu10k1_pcm_mixer *mix =
1013 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1014 	int change = 0, idx, val;
1015 	int num_efx = emu->audigy ? 8 : 4;
1016 
1017 	spin_lock_irqsave(&emu->reg_lock, flags);
1018 	for (idx = 0; idx < 3*num_efx; idx++) {
1019 		val = ucontrol->value.integer.value[idx] & 255;
1020 		if (mix->send_volume[idx/num_efx][idx%num_efx] != val) {
1021 			mix->send_volume[idx/num_efx][idx%num_efx] = val;
1022 			change = 1;
1023 		}
1024 	}
1025 	if (change && mix->epcm) {
1026 		if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
1027 			update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
1028 						   &mix->send_volume[1][0]);
1029 			update_emu10k1_send_volume(emu, mix->epcm->voices[1]->number,
1030 						   &mix->send_volume[2][0]);
1031 		} else if (mix->epcm->voices[0]) {
1032 			update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
1033 						   &mix->send_volume[0][0]);
1034 		}
1035 	}
1036 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1037 	return change;
1038 }
1039 
1040 static struct snd_kcontrol_new snd_emu10k1_send_volume_control =
1041 {
1042 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1043 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1044 	.name =         "EMU10K1 PCM Send Volume",
1045 	.count =	32,
1046 	.info =         snd_emu10k1_send_volume_info,
1047 	.get =          snd_emu10k1_send_volume_get,
1048 	.put =          snd_emu10k1_send_volume_put
1049 };
1050 
1051 static int snd_emu10k1_attn_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1052 {
1053 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1054 	uinfo->count = 3;
1055 	uinfo->value.integer.min = 0;
1056 	uinfo->value.integer.max = 0xffff;
1057 	return 0;
1058 }
1059 
1060 static int snd_emu10k1_attn_get(struct snd_kcontrol *kcontrol,
1061                                 struct snd_ctl_elem_value *ucontrol)
1062 {
1063 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1064 	struct snd_emu10k1_pcm_mixer *mix =
1065 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1066 	unsigned long flags;
1067 	int idx;
1068 
1069 	spin_lock_irqsave(&emu->reg_lock, flags);
1070 	for (idx = 0; idx < 3; idx++)
1071 		ucontrol->value.integer.value[idx] = mix->attn[idx];
1072 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1073 	return 0;
1074 }
1075 
1076 static int snd_emu10k1_attn_put(struct snd_kcontrol *kcontrol,
1077 				struct snd_ctl_elem_value *ucontrol)
1078 {
1079 	unsigned long flags;
1080 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1081 	struct snd_emu10k1_pcm_mixer *mix =
1082 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1083 	int change = 0, idx, val;
1084 
1085 	spin_lock_irqsave(&emu->reg_lock, flags);
1086 	for (idx = 0; idx < 3; idx++) {
1087 		val = ucontrol->value.integer.value[idx] & 0xffff;
1088 		if (mix->attn[idx] != val) {
1089 			mix->attn[idx] = val;
1090 			change = 1;
1091 		}
1092 	}
1093 	if (change && mix->epcm) {
1094 		if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
1095 			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[1]);
1096 			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[1]->number, mix->attn[2]);
1097 		} else if (mix->epcm->voices[0]) {
1098 			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[0]);
1099 		}
1100 	}
1101 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1102 	return change;
1103 }
1104 
1105 static struct snd_kcontrol_new snd_emu10k1_attn_control =
1106 {
1107 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1108 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1109 	.name =         "EMU10K1 PCM Volume",
1110 	.count =	32,
1111 	.info =         snd_emu10k1_attn_info,
1112 	.get =          snd_emu10k1_attn_get,
1113 	.put =          snd_emu10k1_attn_put
1114 };
1115 
1116 /* Mutichannel PCM stream controls */
1117 
1118 static int snd_emu10k1_efx_send_routing_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1119 {
1120 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1121 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1122 	uinfo->count = emu->audigy ? 8 : 4;
1123 	uinfo->value.integer.min = 0;
1124 	uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
1125 	return 0;
1126 }
1127 
1128 static int snd_emu10k1_efx_send_routing_get(struct snd_kcontrol *kcontrol,
1129                                         struct snd_ctl_elem_value *ucontrol)
1130 {
1131 	unsigned long flags;
1132 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1133 	struct snd_emu10k1_pcm_mixer *mix =
1134 		&emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1135 	int idx;
1136 	int num_efx = emu->audigy ? 8 : 4;
1137 	int mask = emu->audigy ? 0x3f : 0x0f;
1138 
1139 	spin_lock_irqsave(&emu->reg_lock, flags);
1140 	for (idx = 0; idx < num_efx; idx++)
1141 		ucontrol->value.integer.value[idx] =
1142 			mix->send_routing[0][idx] & mask;
1143 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1144 	return 0;
1145 }
1146 
1147 static int snd_emu10k1_efx_send_routing_put(struct snd_kcontrol *kcontrol,
1148                                         struct snd_ctl_elem_value *ucontrol)
1149 {
1150 	unsigned long flags;
1151 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1152 	int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1153 	struct snd_emu10k1_pcm_mixer *mix = &emu->efx_pcm_mixer[ch];
1154 	int change = 0, idx, val;
1155 	int num_efx = emu->audigy ? 8 : 4;
1156 	int mask = emu->audigy ? 0x3f : 0x0f;
1157 
1158 	spin_lock_irqsave(&emu->reg_lock, flags);
1159 	for (idx = 0; idx < num_efx; idx++) {
1160 		val = ucontrol->value.integer.value[idx] & mask;
1161 		if (mix->send_routing[0][idx] != val) {
1162 			mix->send_routing[0][idx] = val;
1163 			change = 1;
1164 		}
1165 	}
1166 
1167 	if (change && mix->epcm) {
1168 		if (mix->epcm->voices[ch]) {
1169 			update_emu10k1_fxrt(emu, mix->epcm->voices[ch]->number,
1170 					&mix->send_routing[0][0]);
1171 		}
1172 	}
1173 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1174 	return change;
1175 }
1176 
1177 static struct snd_kcontrol_new snd_emu10k1_efx_send_routing_control =
1178 {
1179 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1180 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1181 	.name =         "Multichannel PCM Send Routing",
1182 	.count =	16,
1183 	.info =         snd_emu10k1_efx_send_routing_info,
1184 	.get =          snd_emu10k1_efx_send_routing_get,
1185 	.put =          snd_emu10k1_efx_send_routing_put
1186 };
1187 
1188 static int snd_emu10k1_efx_send_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1189 {
1190 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1191 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1192 	uinfo->count = emu->audigy ? 8 : 4;
1193 	uinfo->value.integer.min = 0;
1194 	uinfo->value.integer.max = 255;
1195 	return 0;
1196 }
1197 
1198 static int snd_emu10k1_efx_send_volume_get(struct snd_kcontrol *kcontrol,
1199                                        struct snd_ctl_elem_value *ucontrol)
1200 {
1201 	unsigned long flags;
1202 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1203 	struct snd_emu10k1_pcm_mixer *mix =
1204 		&emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1205 	int idx;
1206 	int num_efx = emu->audigy ? 8 : 4;
1207 
1208 	spin_lock_irqsave(&emu->reg_lock, flags);
1209 	for (idx = 0; idx < num_efx; idx++)
1210 		ucontrol->value.integer.value[idx] = mix->send_volume[0][idx];
1211 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1212 	return 0;
1213 }
1214 
1215 static int snd_emu10k1_efx_send_volume_put(struct snd_kcontrol *kcontrol,
1216                                        struct snd_ctl_elem_value *ucontrol)
1217 {
1218 	unsigned long flags;
1219 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1220 	int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1221 	struct snd_emu10k1_pcm_mixer *mix = &emu->efx_pcm_mixer[ch];
1222 	int change = 0, idx, val;
1223 	int num_efx = emu->audigy ? 8 : 4;
1224 
1225 	spin_lock_irqsave(&emu->reg_lock, flags);
1226 	for (idx = 0; idx < num_efx; idx++) {
1227 		val = ucontrol->value.integer.value[idx] & 255;
1228 		if (mix->send_volume[0][idx] != val) {
1229 			mix->send_volume[0][idx] = val;
1230 			change = 1;
1231 		}
1232 	}
1233 	if (change && mix->epcm) {
1234 		if (mix->epcm->voices[ch]) {
1235 			update_emu10k1_send_volume(emu, mix->epcm->voices[ch]->number,
1236 						   &mix->send_volume[0][0]);
1237 		}
1238 	}
1239 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1240 	return change;
1241 }
1242 
1243 
1244 static struct snd_kcontrol_new snd_emu10k1_efx_send_volume_control =
1245 {
1246 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1247 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1248 	.name =         "Multichannel PCM Send Volume",
1249 	.count =	16,
1250 	.info =         snd_emu10k1_efx_send_volume_info,
1251 	.get =          snd_emu10k1_efx_send_volume_get,
1252 	.put =          snd_emu10k1_efx_send_volume_put
1253 };
1254 
1255 static int snd_emu10k1_efx_attn_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1256 {
1257 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1258 	uinfo->count = 1;
1259 	uinfo->value.integer.min = 0;
1260 	uinfo->value.integer.max = 0xffff;
1261 	return 0;
1262 }
1263 
1264 static int snd_emu10k1_efx_attn_get(struct snd_kcontrol *kcontrol,
1265                                 struct snd_ctl_elem_value *ucontrol)
1266 {
1267 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1268 	struct snd_emu10k1_pcm_mixer *mix =
1269 		&emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1270 	unsigned long flags;
1271 
1272 	spin_lock_irqsave(&emu->reg_lock, flags);
1273 	ucontrol->value.integer.value[0] = mix->attn[0];
1274 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1275 	return 0;
1276 }
1277 
1278 static int snd_emu10k1_efx_attn_put(struct snd_kcontrol *kcontrol,
1279 				struct snd_ctl_elem_value *ucontrol)
1280 {
1281 	unsigned long flags;
1282 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1283 	int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1284 	struct snd_emu10k1_pcm_mixer *mix = &emu->efx_pcm_mixer[ch];
1285 	int change = 0, val;
1286 
1287 	spin_lock_irqsave(&emu->reg_lock, flags);
1288 	val = ucontrol->value.integer.value[0] & 0xffff;
1289 	if (mix->attn[0] != val) {
1290 		mix->attn[0] = val;
1291 		change = 1;
1292 	}
1293 	if (change && mix->epcm) {
1294 		if (mix->epcm->voices[ch]) {
1295 			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[ch]->number, mix->attn[0]);
1296 		}
1297 	}
1298 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1299 	return change;
1300 }
1301 
1302 static struct snd_kcontrol_new snd_emu10k1_efx_attn_control =
1303 {
1304 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1305 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1306 	.name =         "Multichannel PCM Volume",
1307 	.count =	16,
1308 	.info =         snd_emu10k1_efx_attn_info,
1309 	.get =          snd_emu10k1_efx_attn_get,
1310 	.put =          snd_emu10k1_efx_attn_put
1311 };
1312 
1313 static int snd_emu10k1_shared_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1314 {
1315 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1316 	uinfo->count = 1;
1317 	uinfo->value.integer.min = 0;
1318 	uinfo->value.integer.max = 1;
1319 	return 0;
1320 }
1321 
1322 static int snd_emu10k1_shared_spdif_get(struct snd_kcontrol *kcontrol,
1323 					struct snd_ctl_elem_value *ucontrol)
1324 {
1325 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1326 
1327 	if (emu->audigy)
1328 		ucontrol->value.integer.value[0] = inl(emu->port + A_IOCFG) & A_IOCFG_GPOUT0 ? 1 : 0;
1329 	else
1330 		ucontrol->value.integer.value[0] = inl(emu->port + HCFG) & HCFG_GPOUT0 ? 1 : 0;
1331 	return 0;
1332 }
1333 
1334 static int snd_emu10k1_shared_spdif_put(struct snd_kcontrol *kcontrol,
1335 					struct snd_ctl_elem_value *ucontrol)
1336 {
1337 	unsigned long flags;
1338 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1339 	unsigned int reg, val;
1340 	int change = 0;
1341 
1342 	spin_lock_irqsave(&emu->reg_lock, flags);
1343 	if ( emu->card_capabilities->i2c_adc) {
1344 		/* Do nothing for Audigy 2 ZS Notebook */
1345 	} else if (emu->audigy) {
1346 		reg = inl(emu->port + A_IOCFG);
1347 		val = ucontrol->value.integer.value[0] ? A_IOCFG_GPOUT0 : 0;
1348 		change = (reg & A_IOCFG_GPOUT0) != val;
1349 		if (change) {
1350 			reg &= ~A_IOCFG_GPOUT0;
1351 			reg |= val;
1352 			outl(reg | val, emu->port + A_IOCFG);
1353 		}
1354 	}
1355 	reg = inl(emu->port + HCFG);
1356 	val = ucontrol->value.integer.value[0] ? HCFG_GPOUT0 : 0;
1357 	change |= (reg & HCFG_GPOUT0) != val;
1358 	if (change) {
1359 		reg &= ~HCFG_GPOUT0;
1360 		reg |= val;
1361 		outl(reg | val, emu->port + HCFG);
1362 	}
1363 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1364 	return change;
1365 }
1366 
1367 static struct snd_kcontrol_new snd_emu10k1_shared_spdif __devinitdata =
1368 {
1369 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
1370 	.name =		"SB Live Analog/Digital Output Jack",
1371 	.info =		snd_emu10k1_shared_spdif_info,
1372 	.get =		snd_emu10k1_shared_spdif_get,
1373 	.put =		snd_emu10k1_shared_spdif_put
1374 };
1375 
1376 static struct snd_kcontrol_new snd_audigy_shared_spdif __devinitdata =
1377 {
1378 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
1379 	.name =		"Audigy Analog/Digital Output Jack",
1380 	.info =		snd_emu10k1_shared_spdif_info,
1381 	.get =		snd_emu10k1_shared_spdif_get,
1382 	.put =		snd_emu10k1_shared_spdif_put
1383 };
1384 
1385 /*
1386  */
1387 static void snd_emu10k1_mixer_free_ac97(struct snd_ac97 *ac97)
1388 {
1389 	struct snd_emu10k1 *emu = ac97->private_data;
1390 	emu->ac97 = NULL;
1391 }
1392 
1393 /*
1394  */
1395 static int remove_ctl(struct snd_card *card, const char *name)
1396 {
1397 	struct snd_ctl_elem_id id;
1398 	memset(&id, 0, sizeof(id));
1399 	strcpy(id.name, name);
1400 	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1401 	return snd_ctl_remove_id(card, &id);
1402 }
1403 
1404 static struct snd_kcontrol *ctl_find(struct snd_card *card, const char *name)
1405 {
1406 	struct snd_ctl_elem_id sid;
1407 	memset(&sid, 0, sizeof(sid));
1408 	strcpy(sid.name, name);
1409 	sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1410 	return snd_ctl_find_id(card, &sid);
1411 }
1412 
1413 static int rename_ctl(struct snd_card *card, const char *src, const char *dst)
1414 {
1415 	struct snd_kcontrol *kctl = ctl_find(card, src);
1416 	if (kctl) {
1417 		strcpy(kctl->id.name, dst);
1418 		return 0;
1419 	}
1420 	return -ENOENT;
1421 }
1422 
1423 int __devinit snd_emu10k1_mixer(struct snd_emu10k1 *emu,
1424 				int pcm_device, int multi_device)
1425 {
1426 	int err, pcm;
1427 	struct snd_kcontrol *kctl;
1428 	struct snd_card *card = emu->card;
1429 	char **c;
1430 	static char *emu10k1_remove_ctls[] = {
1431 		/* no AC97 mono, surround, center/lfe */
1432 		"Master Mono Playback Switch",
1433 		"Master Mono Playback Volume",
1434 		"PCM Out Path & Mute",
1435 		"Mono Output Select",
1436 		"Front Playback Switch",
1437 		"Front Playback Volume",
1438 		"Surround Playback Switch",
1439 		"Surround Playback Volume",
1440 		"Center Playback Switch",
1441 		"Center Playback Volume",
1442 		"LFE Playback Switch",
1443 		"LFE Playback Volume",
1444 		NULL
1445 	};
1446 	static char *emu10k1_rename_ctls[] = {
1447 		"Surround Digital Playback Volume", "Surround Playback Volume",
1448 		"Center Digital Playback Volume", "Center Playback Volume",
1449 		"LFE Digital Playback Volume", "LFE Playback Volume",
1450 		NULL
1451 	};
1452 	static char *audigy_remove_ctls[] = {
1453 		/* Master/PCM controls on ac97 of Audigy has no effect */
1454 		/* On the Audigy2 the AC97 playback is piped into
1455 		 * the Philips ADC for 24bit capture */
1456 		"PCM Playback Switch",
1457 		"PCM Playback Volume",
1458 		"Master Mono Playback Switch",
1459 		"Master Mono Playback Volume",
1460 		"Master Playback Switch",
1461 		"Master Playback Volume",
1462 		"PCM Out Path & Mute",
1463 		"Mono Output Select",
1464 		/* remove unused AC97 capture controls */
1465 		"Capture Source",
1466 		"Capture Switch",
1467 		"Capture Volume",
1468 		"Mic Select",
1469 		"Video Playback Switch",
1470 		"Video Playback Volume",
1471 		"Mic Playback Switch",
1472 		"Mic Playback Volume",
1473 		NULL
1474 	};
1475 	static char *audigy_rename_ctls[] = {
1476 		/* use conventional names */
1477 		"Wave Playback Volume", "PCM Playback Volume",
1478 		/* "Wave Capture Volume", "PCM Capture Volume", */
1479 		"Wave Master Playback Volume", "Master Playback Volume",
1480 		"AMic Playback Volume", "Mic Playback Volume",
1481 		NULL
1482 	};
1483 	static char *audigy_rename_ctls_i2c_adc[] = {
1484 		//"Analog Mix Capture Volume","OLD Analog Mix Capture Volume",
1485 		"Line Capture Volume", "Analog Mix Capture Volume",
1486 		"Wave Playback Volume", "OLD PCM Playback Volume",
1487 		"Wave Master Playback Volume", "Master Playback Volume",
1488 		"AMic Playback Volume", "Old Mic Playback Volume",
1489 		"CD Capture Volume", "IEC958 Optical Capture Volume",
1490 		NULL
1491 	};
1492 	static char *audigy_remove_ctls_i2c_adc[] = {
1493 		/* On the Audigy2 ZS Notebook
1494 		 * Capture via WM8775  */
1495 		"Mic Capture Volume",
1496 		"Analog Mix Capture Volume",
1497 		"Aux Capture Volume",
1498 		"IEC958 Optical Capture Volume",
1499 		NULL
1500 	};
1501 	static char *audigy_remove_ctls_1361t_adc[] = {
1502 		/* On the Audigy2 the AC97 playback is piped into
1503 		 * the Philips ADC for 24bit capture */
1504 		"PCM Playback Switch",
1505 		"PCM Playback Volume",
1506 		"Master Mono Playback Switch",
1507 		"Master Mono Playback Volume",
1508 		"Capture Source",
1509 		"Capture Switch",
1510 		"Capture Volume",
1511 		"Mic Capture Volume",
1512 		"Headphone Playback Switch",
1513 		"Headphone Playback Volume",
1514 		"3D Control - Center",
1515 		"3D Control - Depth",
1516 		"3D Control - Switch",
1517 		"Line2 Playback Volume",
1518 		"Line2 Capture Volume",
1519 		NULL
1520 	};
1521 	static char *audigy_rename_ctls_1361t_adc[] = {
1522 		"Master Playback Switch", "Master Capture Switch",
1523 		"Master Playback Volume", "Master Capture Volume",
1524 		"Wave Master Playback Volume", "Master Playback Volume",
1525 		"PC Speaker Playback Switch", "PC Speaker Capture Switch",
1526 		"PC Speaker Playback Volume", "PC Speaker Capture Volume",
1527 		"Phone Playback Switch", "Phone Capture Switch",
1528 		"Phone Playback Volume", "Phone Capture Volume",
1529 		"Mic Playback Switch", "Mic Capture Switch",
1530 		"Mic Playback Volume", "Mic Capture Volume",
1531 		"Line Playback Switch", "Line Capture Switch",
1532 		"Line Playback Volume", "Line Capture Volume",
1533 		"CD Playback Switch", "CD Capture Switch",
1534 		"CD Playback Volume", "CD Capture Volume",
1535 		"Aux Playback Switch", "Aux Capture Switch",
1536 		"Aux Playback Volume", "Aux Capture Volume",
1537 		"Video Playback Switch", "Video Capture Switch",
1538 		"Video Playback Volume", "Video Capture Volume",
1539 
1540 		NULL
1541 	};
1542 
1543 	if (emu->card_capabilities->ac97_chip) {
1544 		struct snd_ac97_bus *pbus;
1545 		struct snd_ac97_template ac97;
1546 		static struct snd_ac97_bus_ops ops = {
1547 			.write = snd_emu10k1_ac97_write,
1548 			.read = snd_emu10k1_ac97_read,
1549 		};
1550 
1551 		if ((err = snd_ac97_bus(emu->card, 0, &ops, NULL, &pbus)) < 0)
1552 			return err;
1553 		pbus->no_vra = 1; /* we don't need VRA */
1554 
1555 		memset(&ac97, 0, sizeof(ac97));
1556 		ac97.private_data = emu;
1557 		ac97.private_free = snd_emu10k1_mixer_free_ac97;
1558 		ac97.scaps = AC97_SCAP_NO_SPDIF;
1559 		if ((err = snd_ac97_mixer(pbus, &ac97, &emu->ac97)) < 0) {
1560 			if (emu->card_capabilities->ac97_chip == 1)
1561 				return err;
1562 			snd_printd(KERN_INFO "emu10k1: AC97 is optional on this board\n");
1563 			snd_printd(KERN_INFO"          Proceeding without ac97 mixers...\n");
1564 			snd_device_free(emu->card, pbus);
1565 			goto no_ac97; /* FIXME: get rid of ugly gotos.. */
1566 		}
1567 		if (emu->audigy) {
1568 			/* set master volume to 0 dB */
1569 			snd_ac97_write_cache(emu->ac97, AC97_MASTER, 0x0000);
1570 			/* set capture source to mic */
1571 			snd_ac97_write_cache(emu->ac97, AC97_REC_SEL, 0x0000);
1572 			if (emu->card_capabilities->adc_1361t)
1573 				c = audigy_remove_ctls_1361t_adc;
1574 			else
1575 				c = audigy_remove_ctls;
1576 		} else {
1577 			/*
1578 			 * Credits for cards based on STAC9758:
1579 			 *   James Courtier-Dutton <James@superbug.demon.co.uk>
1580 			 *   Voluspa <voluspa@comhem.se>
1581 			 */
1582 			if (emu->ac97->id == AC97_ID_STAC9758) {
1583 				emu->rear_ac97 = 1;
1584 				snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE|AC97SLOT_REAR_LEFT|AC97SLOT_REAR_RIGHT);
1585 				snd_ac97_write_cache(emu->ac97, AC97_HEADPHONE, 0x0202);
1586 			}
1587 			/* remove unused AC97 controls */
1588 			snd_ac97_write_cache(emu->ac97, AC97_SURROUND_MASTER, 0x0202);
1589 			snd_ac97_write_cache(emu->ac97, AC97_CENTER_LFE_MASTER, 0x0202);
1590 			c = emu10k1_remove_ctls;
1591 		}
1592 		for (; *c; c++)
1593 			remove_ctl(card, *c);
1594 	} else if (emu->card_capabilities->i2c_adc) {
1595 		c = audigy_remove_ctls_i2c_adc;
1596 		for (; *c; c++)
1597 			remove_ctl(card, *c);
1598 	} else {
1599 	no_ac97:
1600 		if (emu->card_capabilities->ecard)
1601 			strcpy(emu->card->mixername, "EMU APS");
1602 		else if (emu->audigy)
1603 			strcpy(emu->card->mixername, "SB Audigy");
1604 		else
1605 			strcpy(emu->card->mixername, "Emu10k1");
1606 	}
1607 
1608 	if (emu->audigy)
1609 		if (emu->card_capabilities->adc_1361t)
1610 			c = audigy_rename_ctls_1361t_adc;
1611 		else if (emu->card_capabilities->i2c_adc)
1612 			c = audigy_rename_ctls_i2c_adc;
1613 		else
1614 			c = audigy_rename_ctls;
1615 	else
1616 		c = emu10k1_rename_ctls;
1617 	for (; *c; c += 2)
1618 		rename_ctl(card, c[0], c[1]);
1619 
1620 	if (emu->card_capabilities->subsystem == 0x20071102) {  /* Audigy 4 Pro */
1621 		rename_ctl(card, "Line2 Capture Volume", "Line1/Mic Capture Volume");
1622 		rename_ctl(card, "Analog Mix Capture Volume", "Line2 Capture Volume");
1623 		rename_ctl(card, "Aux2 Capture Volume", "Line3 Capture Volume");
1624 		rename_ctl(card, "Mic Capture Volume", "Unknown1 Capture Volume");
1625 		remove_ctl(card, "Headphone Playback Switch");
1626 		remove_ctl(card, "Headphone Playback Volume");
1627 		remove_ctl(card, "3D Control - Center");
1628 		remove_ctl(card, "3D Control - Depth");
1629 		remove_ctl(card, "3D Control - Switch");
1630 	}
1631 	if ((kctl = emu->ctl_send_routing = snd_ctl_new1(&snd_emu10k1_send_routing_control, emu)) == NULL)
1632 		return -ENOMEM;
1633 	kctl->id.device = pcm_device;
1634 	if ((err = snd_ctl_add(card, kctl)))
1635 		return err;
1636 	if ((kctl = emu->ctl_send_volume = snd_ctl_new1(&snd_emu10k1_send_volume_control, emu)) == NULL)
1637 		return -ENOMEM;
1638 	kctl->id.device = pcm_device;
1639 	if ((err = snd_ctl_add(card, kctl)))
1640 		return err;
1641 	if ((kctl = emu->ctl_attn = snd_ctl_new1(&snd_emu10k1_attn_control, emu)) == NULL)
1642 		return -ENOMEM;
1643 	kctl->id.device = pcm_device;
1644 	if ((err = snd_ctl_add(card, kctl)))
1645 		return err;
1646 
1647 	if ((kctl = emu->ctl_efx_send_routing = snd_ctl_new1(&snd_emu10k1_efx_send_routing_control, emu)) == NULL)
1648 		return -ENOMEM;
1649 	kctl->id.device = multi_device;
1650 	if ((err = snd_ctl_add(card, kctl)))
1651 		return err;
1652 
1653 	if ((kctl = emu->ctl_efx_send_volume = snd_ctl_new1(&snd_emu10k1_efx_send_volume_control, emu)) == NULL)
1654 		return -ENOMEM;
1655 	kctl->id.device = multi_device;
1656 	if ((err = snd_ctl_add(card, kctl)))
1657 		return err;
1658 
1659 	if ((kctl = emu->ctl_efx_attn = snd_ctl_new1(&snd_emu10k1_efx_attn_control, emu)) == NULL)
1660 		return -ENOMEM;
1661 	kctl->id.device = multi_device;
1662 	if ((err = snd_ctl_add(card, kctl)))
1663 		return err;
1664 
1665 	/* initialize the routing and volume table for each pcm playback stream */
1666 	for (pcm = 0; pcm < 32; pcm++) {
1667 		struct snd_emu10k1_pcm_mixer *mix;
1668 		int v;
1669 
1670 		mix = &emu->pcm_mixer[pcm];
1671 		mix->epcm = NULL;
1672 
1673 		for (v = 0; v < 4; v++)
1674 			mix->send_routing[0][v] =
1675 				mix->send_routing[1][v] =
1676 				mix->send_routing[2][v] = v;
1677 
1678 		memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1679 		mix->send_volume[0][0] = mix->send_volume[0][1] =
1680 		mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1681 
1682 		mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1683 	}
1684 
1685 	/* initialize the routing and volume table for the multichannel playback stream */
1686 	for (pcm = 0; pcm < NUM_EFX_PLAYBACK; pcm++) {
1687 		struct snd_emu10k1_pcm_mixer *mix;
1688 		int v;
1689 
1690 		mix = &emu->efx_pcm_mixer[pcm];
1691 		mix->epcm = NULL;
1692 
1693 		mix->send_routing[0][0] = pcm;
1694 		mix->send_routing[0][1] = (pcm == 0) ? 1 : 0;
1695 		for (v = 0; v < 2; v++)
1696 			mix->send_routing[0][2+v] = 13+v;
1697 		if (emu->audigy)
1698 			for (v = 0; v < 4; v++)
1699 				mix->send_routing[0][4+v] = 60+v;
1700 
1701 		memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1702 		mix->send_volume[0][0]  = 255;
1703 
1704 		mix->attn[0] = 0xffff;
1705 	}
1706 
1707 	if (! emu->card_capabilities->ecard) { /* FIXME: APS has these controls? */
1708 		/* sb live! and audigy */
1709 		if ((kctl = snd_ctl_new1(&snd_emu10k1_spdif_mask_control, emu)) == NULL)
1710 			return -ENOMEM;
1711 		if (!emu->audigy)
1712 			kctl->id.device = emu->pcm_efx->device;
1713 		if ((err = snd_ctl_add(card, kctl)))
1714 			return err;
1715 		if ((kctl = snd_ctl_new1(&snd_emu10k1_spdif_control, emu)) == NULL)
1716 			return -ENOMEM;
1717 		if (!emu->audigy)
1718 			kctl->id.device = emu->pcm_efx->device;
1719 		if ((err = snd_ctl_add(card, kctl)))
1720 			return err;
1721 	}
1722 
1723 	if ( emu->card_capabilities->emu1010) {
1724 		;  /* Disable the snd_audigy_spdif_shared_spdif */
1725 	} else if (emu->audigy) {
1726 		if ((kctl = snd_ctl_new1(&snd_audigy_shared_spdif, emu)) == NULL)
1727 			return -ENOMEM;
1728 		if ((err = snd_ctl_add(card, kctl)))
1729 			return err;
1730 #if 0
1731 		if ((kctl = snd_ctl_new1(&snd_audigy_spdif_output_rate, emu)) == NULL)
1732 			return -ENOMEM;
1733 		if ((err = snd_ctl_add(card, kctl)))
1734 			return err;
1735 #endif
1736 	} else if (! emu->card_capabilities->ecard) {
1737 		/* sb live! */
1738 		if ((kctl = snd_ctl_new1(&snd_emu10k1_shared_spdif, emu)) == NULL)
1739 			return -ENOMEM;
1740 		if ((err = snd_ctl_add(card, kctl)))
1741 			return err;
1742 	}
1743 	if (emu->card_capabilities->ca0151_chip) { /* P16V */
1744 		if ((err = snd_p16v_mixer(emu)))
1745 			return err;
1746 	}
1747 
1748 	if ( emu->card_capabilities->emu1010) {
1749 		int i;
1750 
1751 		for (i = 0; i < ARRAY_SIZE(snd_emu1010_output_enum_ctls); i++) {
1752 			err = snd_ctl_add(card, snd_ctl_new1(&snd_emu1010_output_enum_ctls[i], emu));
1753 			if (err < 0)
1754 				return err;
1755 		}
1756 		for (i = 0; i < ARRAY_SIZE(snd_emu1010_input_enum_ctls); i++) {
1757 			err = snd_ctl_add(card, snd_ctl_new1(&snd_emu1010_input_enum_ctls[i], emu));
1758 			if (err < 0)
1759 				return err;
1760 		}
1761 		for (i = 0; i < ARRAY_SIZE(snd_emu1010_adc_pads); i++) {
1762 			err = snd_ctl_add(card, snd_ctl_new1(&snd_emu1010_adc_pads[i], emu));
1763 			if (err < 0)
1764 				return err;
1765 		}
1766 		for (i = 0; i < ARRAY_SIZE(snd_emu1010_dac_pads); i++) {
1767 			err = snd_ctl_add(card, snd_ctl_new1(&snd_emu1010_dac_pads[i], emu));
1768 			if (err < 0)
1769 				return err;
1770 		}
1771 		err = snd_ctl_add(card, snd_ctl_new1(&snd_emu1010_internal_clock, emu));
1772 		if (err < 0)
1773 			return err;
1774 	}
1775 
1776 	if ( emu->card_capabilities->i2c_adc) {
1777 		int i;
1778 
1779 		err = snd_ctl_add(card, snd_ctl_new1(&snd_audigy_i2c_capture_source, emu));
1780 		if (err < 0)
1781 			return err;
1782 
1783 		for (i = 0; i < ARRAY_SIZE(snd_audigy_i2c_volume_ctls); i++) {
1784 			err = snd_ctl_add(card, snd_ctl_new1(&snd_audigy_i2c_volume_ctls[i], emu));
1785 			if (err < 0)
1786 				return err;
1787 		}
1788 	}
1789 
1790 	return 0;
1791 }
1792