1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
4  *                   Takashi Iwai <tiwai@suse.de>
5  *                   Creative Labs, Inc.
6  *  Routines for control of EMU10K1 chips / mixer routines
7  *  Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
8  *
9  *  Copyright (c) by James Courtier-Dutton <James@superbug.co.uk>
10  *  	Added EMU 1010 support.
11  *
12  *  BUGS:
13  *    --
14  *
15  *  TODO:
16  *    --
17  */
18 
19 #include <linux/time.h>
20 #include <linux/init.h>
21 #include <sound/core.h>
22 #include <sound/emu10k1.h>
23 #include <linux/delay.h>
24 #include <sound/tlv.h>
25 
26 #include "p17v.h"
27 
28 #define AC97_ID_STAC9758	0x83847658
29 
30 static const DECLARE_TLV_DB_SCALE(snd_audigy_db_scale2, -10350, 50, 1); /* WM8775 gain scale */
31 
snd_emu10k1_spdif_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)32 static int snd_emu10k1_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
33 {
34 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
35 	uinfo->count = 1;
36 	return 0;
37 }
38 
snd_emu10k1_spdif_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)39 static int snd_emu10k1_spdif_get(struct snd_kcontrol *kcontrol,
40                                  struct snd_ctl_elem_value *ucontrol)
41 {
42 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
43 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
44 	unsigned long flags;
45 
46 	/* Limit: emu->spdif_bits */
47 	if (idx >= 3)
48 		return -EINVAL;
49 	spin_lock_irqsave(&emu->reg_lock, flags);
50 	ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
51 	ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
52 	ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
53 	ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
54 	spin_unlock_irqrestore(&emu->reg_lock, flags);
55 	return 0;
56 }
57 
snd_emu10k1_spdif_get_mask(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)58 static int snd_emu10k1_spdif_get_mask(struct snd_kcontrol *kcontrol,
59 				      struct snd_ctl_elem_value *ucontrol)
60 {
61 	ucontrol->value.iec958.status[0] = 0xff;
62 	ucontrol->value.iec958.status[1] = 0xff;
63 	ucontrol->value.iec958.status[2] = 0xff;
64 	ucontrol->value.iec958.status[3] = 0xff;
65 	return 0;
66 }
67 
68 /*
69  * Items labels in enum mixer controls assigning source data to
70  * each destination
71  */
72 static const char * const emu1010_src_texts[] = {
73 	"Silence",
74 	"Dock Mic A",
75 	"Dock Mic B",
76 	"Dock ADC1 Left",
77 	"Dock ADC1 Right",
78 	"Dock ADC2 Left",
79 	"Dock ADC2 Right",
80 	"Dock ADC3 Left",
81 	"Dock ADC3 Right",
82 	"0202 ADC Left",
83 	"0202 ADC Right",
84 	"0202 SPDIF Left",
85 	"0202 SPDIF Right",
86 	"ADAT 0",
87 	"ADAT 1",
88 	"ADAT 2",
89 	"ADAT 3",
90 	"ADAT 4",
91 	"ADAT 5",
92 	"ADAT 6",
93 	"ADAT 7",
94 	"DSP 0",
95 	"DSP 1",
96 	"DSP 2",
97 	"DSP 3",
98 	"DSP 4",
99 	"DSP 5",
100 	"DSP 6",
101 	"DSP 7",
102 	"DSP 8",
103 	"DSP 9",
104 	"DSP 10",
105 	"DSP 11",
106 	"DSP 12",
107 	"DSP 13",
108 	"DSP 14",
109 	"DSP 15",
110 	"DSP 16",
111 	"DSP 17",
112 	"DSP 18",
113 	"DSP 19",
114 	"DSP 20",
115 	"DSP 21",
116 	"DSP 22",
117 	"DSP 23",
118 	"DSP 24",
119 	"DSP 25",
120 	"DSP 26",
121 	"DSP 27",
122 	"DSP 28",
123 	"DSP 29",
124 	"DSP 30",
125 	"DSP 31",
126 };
127 
128 /* 1616(m) cardbus */
129 
130 static const char * const emu1616_src_texts[] = {
131 	"Silence",
132 	"Dock Mic A",
133 	"Dock Mic B",
134 	"Dock ADC1 Left",
135 	"Dock ADC1 Right",
136 	"Dock ADC2 Left",
137 	"Dock ADC2 Right",
138 	"Dock SPDIF Left",
139 	"Dock SPDIF Right",
140 	"ADAT 0",
141 	"ADAT 1",
142 	"ADAT 2",
143 	"ADAT 3",
144 	"ADAT 4",
145 	"ADAT 5",
146 	"ADAT 6",
147 	"ADAT 7",
148 	"DSP 0",
149 	"DSP 1",
150 	"DSP 2",
151 	"DSP 3",
152 	"DSP 4",
153 	"DSP 5",
154 	"DSP 6",
155 	"DSP 7",
156 	"DSP 8",
157 	"DSP 9",
158 	"DSP 10",
159 	"DSP 11",
160 	"DSP 12",
161 	"DSP 13",
162 	"DSP 14",
163 	"DSP 15",
164 	"DSP 16",
165 	"DSP 17",
166 	"DSP 18",
167 	"DSP 19",
168 	"DSP 20",
169 	"DSP 21",
170 	"DSP 22",
171 	"DSP 23",
172 	"DSP 24",
173 	"DSP 25",
174 	"DSP 26",
175 	"DSP 27",
176 	"DSP 28",
177 	"DSP 29",
178 	"DSP 30",
179 	"DSP 31",
180 };
181 
182 
183 /*
184  * List of data sources available for each destination
185  */
186 static const unsigned int emu1010_src_regs[] = {
187 	EMU_SRC_SILENCE,/* 0 */
188 	EMU_SRC_DOCK_MIC_A1, /* 1 */
189 	EMU_SRC_DOCK_MIC_B1, /* 2 */
190 	EMU_SRC_DOCK_ADC1_LEFT1, /* 3 */
191 	EMU_SRC_DOCK_ADC1_RIGHT1, /* 4 */
192 	EMU_SRC_DOCK_ADC2_LEFT1, /* 5 */
193 	EMU_SRC_DOCK_ADC2_RIGHT1, /* 6 */
194 	EMU_SRC_DOCK_ADC3_LEFT1, /* 7 */
195 	EMU_SRC_DOCK_ADC3_RIGHT1, /* 8 */
196 	EMU_SRC_HAMOA_ADC_LEFT1, /* 9 */
197 	EMU_SRC_HAMOA_ADC_RIGHT1, /* 10 */
198 	EMU_SRC_HANA_SPDIF_LEFT1, /* 11 */
199 	EMU_SRC_HANA_SPDIF_RIGHT1, /* 12 */
200 	EMU_SRC_HANA_ADAT, /* 13 */
201 	EMU_SRC_HANA_ADAT+1, /* 14 */
202 	EMU_SRC_HANA_ADAT+2, /* 15 */
203 	EMU_SRC_HANA_ADAT+3, /* 16 */
204 	EMU_SRC_HANA_ADAT+4, /* 17 */
205 	EMU_SRC_HANA_ADAT+5, /* 18 */
206 	EMU_SRC_HANA_ADAT+6, /* 19 */
207 	EMU_SRC_HANA_ADAT+7, /* 20 */
208 	EMU_SRC_ALICE_EMU32A, /* 21 */
209 	EMU_SRC_ALICE_EMU32A+1, /* 22 */
210 	EMU_SRC_ALICE_EMU32A+2, /* 23 */
211 	EMU_SRC_ALICE_EMU32A+3, /* 24 */
212 	EMU_SRC_ALICE_EMU32A+4, /* 25 */
213 	EMU_SRC_ALICE_EMU32A+5, /* 26 */
214 	EMU_SRC_ALICE_EMU32A+6, /* 27 */
215 	EMU_SRC_ALICE_EMU32A+7, /* 28 */
216 	EMU_SRC_ALICE_EMU32A+8, /* 29 */
217 	EMU_SRC_ALICE_EMU32A+9, /* 30 */
218 	EMU_SRC_ALICE_EMU32A+0xa, /* 31 */
219 	EMU_SRC_ALICE_EMU32A+0xb, /* 32 */
220 	EMU_SRC_ALICE_EMU32A+0xc, /* 33 */
221 	EMU_SRC_ALICE_EMU32A+0xd, /* 34 */
222 	EMU_SRC_ALICE_EMU32A+0xe, /* 35 */
223 	EMU_SRC_ALICE_EMU32A+0xf, /* 36 */
224 	EMU_SRC_ALICE_EMU32B, /* 37 */
225 	EMU_SRC_ALICE_EMU32B+1, /* 38 */
226 	EMU_SRC_ALICE_EMU32B+2, /* 39 */
227 	EMU_SRC_ALICE_EMU32B+3, /* 40 */
228 	EMU_SRC_ALICE_EMU32B+4, /* 41 */
229 	EMU_SRC_ALICE_EMU32B+5, /* 42 */
230 	EMU_SRC_ALICE_EMU32B+6, /* 43 */
231 	EMU_SRC_ALICE_EMU32B+7, /* 44 */
232 	EMU_SRC_ALICE_EMU32B+8, /* 45 */
233 	EMU_SRC_ALICE_EMU32B+9, /* 46 */
234 	EMU_SRC_ALICE_EMU32B+0xa, /* 47 */
235 	EMU_SRC_ALICE_EMU32B+0xb, /* 48 */
236 	EMU_SRC_ALICE_EMU32B+0xc, /* 49 */
237 	EMU_SRC_ALICE_EMU32B+0xd, /* 50 */
238 	EMU_SRC_ALICE_EMU32B+0xe, /* 51 */
239 	EMU_SRC_ALICE_EMU32B+0xf, /* 52 */
240 };
241 
242 /* 1616(m) cardbus */
243 static const unsigned int emu1616_src_regs[] = {
244 	EMU_SRC_SILENCE,
245 	EMU_SRC_DOCK_MIC_A1,
246 	EMU_SRC_DOCK_MIC_B1,
247 	EMU_SRC_DOCK_ADC1_LEFT1,
248 	EMU_SRC_DOCK_ADC1_RIGHT1,
249 	EMU_SRC_DOCK_ADC2_LEFT1,
250 	EMU_SRC_DOCK_ADC2_RIGHT1,
251 	EMU_SRC_MDOCK_SPDIF_LEFT1,
252 	EMU_SRC_MDOCK_SPDIF_RIGHT1,
253 	EMU_SRC_MDOCK_ADAT,
254 	EMU_SRC_MDOCK_ADAT+1,
255 	EMU_SRC_MDOCK_ADAT+2,
256 	EMU_SRC_MDOCK_ADAT+3,
257 	EMU_SRC_MDOCK_ADAT+4,
258 	EMU_SRC_MDOCK_ADAT+5,
259 	EMU_SRC_MDOCK_ADAT+6,
260 	EMU_SRC_MDOCK_ADAT+7,
261 	EMU_SRC_ALICE_EMU32A,
262 	EMU_SRC_ALICE_EMU32A+1,
263 	EMU_SRC_ALICE_EMU32A+2,
264 	EMU_SRC_ALICE_EMU32A+3,
265 	EMU_SRC_ALICE_EMU32A+4,
266 	EMU_SRC_ALICE_EMU32A+5,
267 	EMU_SRC_ALICE_EMU32A+6,
268 	EMU_SRC_ALICE_EMU32A+7,
269 	EMU_SRC_ALICE_EMU32A+8,
270 	EMU_SRC_ALICE_EMU32A+9,
271 	EMU_SRC_ALICE_EMU32A+0xa,
272 	EMU_SRC_ALICE_EMU32A+0xb,
273 	EMU_SRC_ALICE_EMU32A+0xc,
274 	EMU_SRC_ALICE_EMU32A+0xd,
275 	EMU_SRC_ALICE_EMU32A+0xe,
276 	EMU_SRC_ALICE_EMU32A+0xf,
277 	EMU_SRC_ALICE_EMU32B,
278 	EMU_SRC_ALICE_EMU32B+1,
279 	EMU_SRC_ALICE_EMU32B+2,
280 	EMU_SRC_ALICE_EMU32B+3,
281 	EMU_SRC_ALICE_EMU32B+4,
282 	EMU_SRC_ALICE_EMU32B+5,
283 	EMU_SRC_ALICE_EMU32B+6,
284 	EMU_SRC_ALICE_EMU32B+7,
285 	EMU_SRC_ALICE_EMU32B+8,
286 	EMU_SRC_ALICE_EMU32B+9,
287 	EMU_SRC_ALICE_EMU32B+0xa,
288 	EMU_SRC_ALICE_EMU32B+0xb,
289 	EMU_SRC_ALICE_EMU32B+0xc,
290 	EMU_SRC_ALICE_EMU32B+0xd,
291 	EMU_SRC_ALICE_EMU32B+0xe,
292 	EMU_SRC_ALICE_EMU32B+0xf,
293 };
294 
295 /*
296  * Data destinations - physical EMU outputs.
297  * Each destination has an enum mixer control to choose a data source
298  */
299 static const unsigned int emu1010_output_dst[] = {
300 	EMU_DST_DOCK_DAC1_LEFT1, /* 0 */
301 	EMU_DST_DOCK_DAC1_RIGHT1, /* 1 */
302 	EMU_DST_DOCK_DAC2_LEFT1, /* 2 */
303 	EMU_DST_DOCK_DAC2_RIGHT1, /* 3 */
304 	EMU_DST_DOCK_DAC3_LEFT1, /* 4 */
305 	EMU_DST_DOCK_DAC3_RIGHT1, /* 5 */
306 	EMU_DST_DOCK_DAC4_LEFT1, /* 6 */
307 	EMU_DST_DOCK_DAC4_RIGHT1, /* 7 */
308 	EMU_DST_DOCK_PHONES_LEFT1, /* 8 */
309 	EMU_DST_DOCK_PHONES_RIGHT1, /* 9 */
310 	EMU_DST_DOCK_SPDIF_LEFT1, /* 10 */
311 	EMU_DST_DOCK_SPDIF_RIGHT1, /* 11 */
312 	EMU_DST_HANA_SPDIF_LEFT1, /* 12 */
313 	EMU_DST_HANA_SPDIF_RIGHT1, /* 13 */
314 	EMU_DST_HAMOA_DAC_LEFT1, /* 14 */
315 	EMU_DST_HAMOA_DAC_RIGHT1, /* 15 */
316 	EMU_DST_HANA_ADAT, /* 16 */
317 	EMU_DST_HANA_ADAT+1, /* 17 */
318 	EMU_DST_HANA_ADAT+2, /* 18 */
319 	EMU_DST_HANA_ADAT+3, /* 19 */
320 	EMU_DST_HANA_ADAT+4, /* 20 */
321 	EMU_DST_HANA_ADAT+5, /* 21 */
322 	EMU_DST_HANA_ADAT+6, /* 22 */
323 	EMU_DST_HANA_ADAT+7, /* 23 */
324 };
325 
326 /* 1616(m) cardbus */
327 static const unsigned int emu1616_output_dst[] = {
328 	EMU_DST_DOCK_DAC1_LEFT1,
329 	EMU_DST_DOCK_DAC1_RIGHT1,
330 	EMU_DST_DOCK_DAC2_LEFT1,
331 	EMU_DST_DOCK_DAC2_RIGHT1,
332 	EMU_DST_DOCK_DAC3_LEFT1,
333 	EMU_DST_DOCK_DAC3_RIGHT1,
334 	EMU_DST_MDOCK_SPDIF_LEFT1,
335 	EMU_DST_MDOCK_SPDIF_RIGHT1,
336 	EMU_DST_MDOCK_ADAT,
337 	EMU_DST_MDOCK_ADAT+1,
338 	EMU_DST_MDOCK_ADAT+2,
339 	EMU_DST_MDOCK_ADAT+3,
340 	EMU_DST_MDOCK_ADAT+4,
341 	EMU_DST_MDOCK_ADAT+5,
342 	EMU_DST_MDOCK_ADAT+6,
343 	EMU_DST_MDOCK_ADAT+7,
344 	EMU_DST_MANA_DAC_LEFT,
345 	EMU_DST_MANA_DAC_RIGHT,
346 };
347 
348 /*
349  * Data destinations - HANA outputs going to Alice2 (audigy) for
350  *   capture (EMU32 + I2S links)
351  * Each destination has an enum mixer control to choose a data source
352  */
353 static const unsigned int emu1010_input_dst[] = {
354 	EMU_DST_ALICE2_EMU32_0,
355 	EMU_DST_ALICE2_EMU32_1,
356 	EMU_DST_ALICE2_EMU32_2,
357 	EMU_DST_ALICE2_EMU32_3,
358 	EMU_DST_ALICE2_EMU32_4,
359 	EMU_DST_ALICE2_EMU32_5,
360 	EMU_DST_ALICE2_EMU32_6,
361 	EMU_DST_ALICE2_EMU32_7,
362 	EMU_DST_ALICE2_EMU32_8,
363 	EMU_DST_ALICE2_EMU32_9,
364 	EMU_DST_ALICE2_EMU32_A,
365 	EMU_DST_ALICE2_EMU32_B,
366 	EMU_DST_ALICE2_EMU32_C,
367 	EMU_DST_ALICE2_EMU32_D,
368 	EMU_DST_ALICE2_EMU32_E,
369 	EMU_DST_ALICE2_EMU32_F,
370 	EMU_DST_ALICE_I2S0_LEFT,
371 	EMU_DST_ALICE_I2S0_RIGHT,
372 	EMU_DST_ALICE_I2S1_LEFT,
373 	EMU_DST_ALICE_I2S1_RIGHT,
374 	EMU_DST_ALICE_I2S2_LEFT,
375 	EMU_DST_ALICE_I2S2_RIGHT,
376 };
377 
snd_emu1010_input_output_source_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)378 static int snd_emu1010_input_output_source_info(struct snd_kcontrol *kcontrol,
379 						struct snd_ctl_elem_info *uinfo)
380 {
381 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
382 
383 	if (emu->card_capabilities->emu_model == EMU_MODEL_EMU1616)
384 		return snd_ctl_enum_info(uinfo, 1, 49, emu1616_src_texts);
385 	else
386 		return snd_ctl_enum_info(uinfo, 1, 53, emu1010_src_texts);
387 }
388 
snd_emu1010_output_source_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)389 static int snd_emu1010_output_source_get(struct snd_kcontrol *kcontrol,
390                                  struct snd_ctl_elem_value *ucontrol)
391 {
392 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
393 	unsigned int channel;
394 
395 	channel = (kcontrol->private_value) & 0xff;
396 	/* Limit: emu1010_output_dst, emu->emu1010.output_source */
397 	if (channel >= 24 ||
398 	    (emu->card_capabilities->emu_model == EMU_MODEL_EMU1616 &&
399 	     channel >= 18))
400 		return -EINVAL;
401 	ucontrol->value.enumerated.item[0] = emu->emu1010.output_source[channel];
402 	return 0;
403 }
404 
snd_emu1010_output_source_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)405 static int snd_emu1010_output_source_put(struct snd_kcontrol *kcontrol,
406                                  struct snd_ctl_elem_value *ucontrol)
407 {
408 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
409 	unsigned int val;
410 	unsigned int channel;
411 
412 	val = ucontrol->value.enumerated.item[0];
413 	if (val >= 53 ||
414 	    (emu->card_capabilities->emu_model == EMU_MODEL_EMU1616 &&
415 	     val >= 49))
416 		return -EINVAL;
417 	channel = (kcontrol->private_value) & 0xff;
418 	/* Limit: emu1010_output_dst, emu->emu1010.output_source */
419 	if (channel >= 24 ||
420 	    (emu->card_capabilities->emu_model == EMU_MODEL_EMU1616 &&
421 	     channel >= 18))
422 		return -EINVAL;
423 	if (emu->emu1010.output_source[channel] == val)
424 		return 0;
425 	emu->emu1010.output_source[channel] = val;
426 	if (emu->card_capabilities->emu_model == EMU_MODEL_EMU1616)
427 		snd_emu1010_fpga_link_dst_src_write(emu,
428 			emu1616_output_dst[channel], emu1616_src_regs[val]);
429 	else
430 		snd_emu1010_fpga_link_dst_src_write(emu,
431 			emu1010_output_dst[channel], emu1010_src_regs[val]);
432 	return 1;
433 }
434 
snd_emu1010_input_source_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)435 static int snd_emu1010_input_source_get(struct snd_kcontrol *kcontrol,
436                                  struct snd_ctl_elem_value *ucontrol)
437 {
438 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
439 	unsigned int channel;
440 
441 	channel = (kcontrol->private_value) & 0xff;
442 	/* Limit: emu1010_input_dst, emu->emu1010.input_source */
443 	if (channel >= 22)
444 		return -EINVAL;
445 	ucontrol->value.enumerated.item[0] = emu->emu1010.input_source[channel];
446 	return 0;
447 }
448 
snd_emu1010_input_source_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)449 static int snd_emu1010_input_source_put(struct snd_kcontrol *kcontrol,
450                                  struct snd_ctl_elem_value *ucontrol)
451 {
452 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
453 	unsigned int val;
454 	unsigned int channel;
455 
456 	val = ucontrol->value.enumerated.item[0];
457 	if (val >= 53 ||
458 	    (emu->card_capabilities->emu_model == EMU_MODEL_EMU1616 &&
459 	     val >= 49))
460 		return -EINVAL;
461 	channel = (kcontrol->private_value) & 0xff;
462 	/* Limit: emu1010_input_dst, emu->emu1010.input_source */
463 	if (channel >= 22)
464 		return -EINVAL;
465 	if (emu->emu1010.input_source[channel] == val)
466 		return 0;
467 	emu->emu1010.input_source[channel] = val;
468 	if (emu->card_capabilities->emu_model == EMU_MODEL_EMU1616)
469 		snd_emu1010_fpga_link_dst_src_write(emu,
470 			emu1010_input_dst[channel], emu1616_src_regs[val]);
471 	else
472 		snd_emu1010_fpga_link_dst_src_write(emu,
473 			emu1010_input_dst[channel], emu1010_src_regs[val]);
474 	return 1;
475 }
476 
477 #define EMU1010_SOURCE_OUTPUT(xname,chid) \
478 {								\
479 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
480 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
481 	.info =  snd_emu1010_input_output_source_info,		\
482 	.get =   snd_emu1010_output_source_get,			\
483 	.put =   snd_emu1010_output_source_put,			\
484 	.private_value = chid					\
485 }
486 
487 static const struct snd_kcontrol_new snd_emu1010_output_enum_ctls[] = {
488 	EMU1010_SOURCE_OUTPUT("Dock DAC1 Left Playback Enum", 0),
489 	EMU1010_SOURCE_OUTPUT("Dock DAC1 Right Playback Enum", 1),
490 	EMU1010_SOURCE_OUTPUT("Dock DAC2 Left Playback Enum", 2),
491 	EMU1010_SOURCE_OUTPUT("Dock DAC2 Right Playback Enum", 3),
492 	EMU1010_SOURCE_OUTPUT("Dock DAC3 Left Playback Enum", 4),
493 	EMU1010_SOURCE_OUTPUT("Dock DAC3 Right Playback Enum", 5),
494 	EMU1010_SOURCE_OUTPUT("Dock DAC4 Left Playback Enum", 6),
495 	EMU1010_SOURCE_OUTPUT("Dock DAC4 Right Playback Enum", 7),
496 	EMU1010_SOURCE_OUTPUT("Dock Phones Left Playback Enum", 8),
497 	EMU1010_SOURCE_OUTPUT("Dock Phones Right Playback Enum", 9),
498 	EMU1010_SOURCE_OUTPUT("Dock SPDIF Left Playback Enum", 0xa),
499 	EMU1010_SOURCE_OUTPUT("Dock SPDIF Right Playback Enum", 0xb),
500 	EMU1010_SOURCE_OUTPUT("1010 SPDIF Left Playback Enum", 0xc),
501 	EMU1010_SOURCE_OUTPUT("1010 SPDIF Right Playback Enum", 0xd),
502 	EMU1010_SOURCE_OUTPUT("0202 DAC Left Playback Enum", 0xe),
503 	EMU1010_SOURCE_OUTPUT("0202 DAC Right Playback Enum", 0xf),
504 	EMU1010_SOURCE_OUTPUT("1010 ADAT 0 Playback Enum", 0x10),
505 	EMU1010_SOURCE_OUTPUT("1010 ADAT 1 Playback Enum", 0x11),
506 	EMU1010_SOURCE_OUTPUT("1010 ADAT 2 Playback Enum", 0x12),
507 	EMU1010_SOURCE_OUTPUT("1010 ADAT 3 Playback Enum", 0x13),
508 	EMU1010_SOURCE_OUTPUT("1010 ADAT 4 Playback Enum", 0x14),
509 	EMU1010_SOURCE_OUTPUT("1010 ADAT 5 Playback Enum", 0x15),
510 	EMU1010_SOURCE_OUTPUT("1010 ADAT 6 Playback Enum", 0x16),
511 	EMU1010_SOURCE_OUTPUT("1010 ADAT 7 Playback Enum", 0x17),
512 };
513 
514 
515 /* 1616(m) cardbus */
516 static const struct snd_kcontrol_new snd_emu1616_output_enum_ctls[] = {
517 	EMU1010_SOURCE_OUTPUT("Dock DAC1 Left Playback Enum", 0),
518 	EMU1010_SOURCE_OUTPUT("Dock DAC1 Right Playback Enum", 1),
519 	EMU1010_SOURCE_OUTPUT("Dock DAC2 Left Playback Enum", 2),
520 	EMU1010_SOURCE_OUTPUT("Dock DAC2 Right Playback Enum", 3),
521 	EMU1010_SOURCE_OUTPUT("Dock DAC3 Left Playback Enum", 4),
522 	EMU1010_SOURCE_OUTPUT("Dock DAC3 Right Playback Enum", 5),
523 	EMU1010_SOURCE_OUTPUT("Dock SPDIF Left Playback Enum", 6),
524 	EMU1010_SOURCE_OUTPUT("Dock SPDIF Right Playback Enum", 7),
525 	EMU1010_SOURCE_OUTPUT("Dock ADAT 0 Playback Enum", 8),
526 	EMU1010_SOURCE_OUTPUT("Dock ADAT 1 Playback Enum", 9),
527 	EMU1010_SOURCE_OUTPUT("Dock ADAT 2 Playback Enum", 0xa),
528 	EMU1010_SOURCE_OUTPUT("Dock ADAT 3 Playback Enum", 0xb),
529 	EMU1010_SOURCE_OUTPUT("Dock ADAT 4 Playback Enum", 0xc),
530 	EMU1010_SOURCE_OUTPUT("Dock ADAT 5 Playback Enum", 0xd),
531 	EMU1010_SOURCE_OUTPUT("Dock ADAT 6 Playback Enum", 0xe),
532 	EMU1010_SOURCE_OUTPUT("Dock ADAT 7 Playback Enum", 0xf),
533 	EMU1010_SOURCE_OUTPUT("Mana DAC Left Playback Enum", 0x10),
534 	EMU1010_SOURCE_OUTPUT("Mana DAC Right Playback Enum", 0x11),
535 };
536 
537 
538 #define EMU1010_SOURCE_INPUT(xname,chid) \
539 {								\
540 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
541 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
542 	.info =  snd_emu1010_input_output_source_info,		\
543 	.get =   snd_emu1010_input_source_get,			\
544 	.put =   snd_emu1010_input_source_put,			\
545 	.private_value = chid					\
546 }
547 
548 static const struct snd_kcontrol_new snd_emu1010_input_enum_ctls[] = {
549 	EMU1010_SOURCE_INPUT("DSP 0 Capture Enum", 0),
550 	EMU1010_SOURCE_INPUT("DSP 1 Capture Enum", 1),
551 	EMU1010_SOURCE_INPUT("DSP 2 Capture Enum", 2),
552 	EMU1010_SOURCE_INPUT("DSP 3 Capture Enum", 3),
553 	EMU1010_SOURCE_INPUT("DSP 4 Capture Enum", 4),
554 	EMU1010_SOURCE_INPUT("DSP 5 Capture Enum", 5),
555 	EMU1010_SOURCE_INPUT("DSP 6 Capture Enum", 6),
556 	EMU1010_SOURCE_INPUT("DSP 7 Capture Enum", 7),
557 	EMU1010_SOURCE_INPUT("DSP 8 Capture Enum", 8),
558 	EMU1010_SOURCE_INPUT("DSP 9 Capture Enum", 9),
559 	EMU1010_SOURCE_INPUT("DSP A Capture Enum", 0xa),
560 	EMU1010_SOURCE_INPUT("DSP B Capture Enum", 0xb),
561 	EMU1010_SOURCE_INPUT("DSP C Capture Enum", 0xc),
562 	EMU1010_SOURCE_INPUT("DSP D Capture Enum", 0xd),
563 	EMU1010_SOURCE_INPUT("DSP E Capture Enum", 0xe),
564 	EMU1010_SOURCE_INPUT("DSP F Capture Enum", 0xf),
565 	EMU1010_SOURCE_INPUT("DSP 10 Capture Enum", 0x10),
566 	EMU1010_SOURCE_INPUT("DSP 11 Capture Enum", 0x11),
567 	EMU1010_SOURCE_INPUT("DSP 12 Capture Enum", 0x12),
568 	EMU1010_SOURCE_INPUT("DSP 13 Capture Enum", 0x13),
569 	EMU1010_SOURCE_INPUT("DSP 14 Capture Enum", 0x14),
570 	EMU1010_SOURCE_INPUT("DSP 15 Capture Enum", 0x15),
571 };
572 
573 
574 
575 #define snd_emu1010_adc_pads_info	snd_ctl_boolean_mono_info
576 
snd_emu1010_adc_pads_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)577 static int snd_emu1010_adc_pads_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
578 {
579 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
580 	unsigned int mask = kcontrol->private_value & 0xff;
581 	ucontrol->value.integer.value[0] = (emu->emu1010.adc_pads & mask) ? 1 : 0;
582 	return 0;
583 }
584 
snd_emu1010_adc_pads_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)585 static int snd_emu1010_adc_pads_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
586 {
587 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
588 	unsigned int mask = kcontrol->private_value & 0xff;
589 	unsigned int val, cache;
590 	val = ucontrol->value.integer.value[0];
591 	cache = emu->emu1010.adc_pads;
592 	if (val == 1)
593 		cache = cache | mask;
594 	else
595 		cache = cache & ~mask;
596 	if (cache != emu->emu1010.adc_pads) {
597 		snd_emu1010_fpga_write(emu, EMU_HANA_ADC_PADS, cache );
598 	        emu->emu1010.adc_pads = cache;
599 	}
600 
601 	return 0;
602 }
603 
604 
605 
606 #define EMU1010_ADC_PADS(xname,chid) \
607 {								\
608 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
609 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
610 	.info =  snd_emu1010_adc_pads_info,			\
611 	.get =   snd_emu1010_adc_pads_get,			\
612 	.put =   snd_emu1010_adc_pads_put,			\
613 	.private_value = chid					\
614 }
615 
616 static const struct snd_kcontrol_new snd_emu1010_adc_pads[] = {
617 	EMU1010_ADC_PADS("ADC1 14dB PAD Audio Dock Capture Switch", EMU_HANA_DOCK_ADC_PAD1),
618 	EMU1010_ADC_PADS("ADC2 14dB PAD Audio Dock Capture Switch", EMU_HANA_DOCK_ADC_PAD2),
619 	EMU1010_ADC_PADS("ADC3 14dB PAD Audio Dock Capture Switch", EMU_HANA_DOCK_ADC_PAD3),
620 	EMU1010_ADC_PADS("ADC1 14dB PAD 0202 Capture Switch", EMU_HANA_0202_ADC_PAD1),
621 };
622 
623 #define snd_emu1010_dac_pads_info	snd_ctl_boolean_mono_info
624 
snd_emu1010_dac_pads_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)625 static int snd_emu1010_dac_pads_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
626 {
627 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
628 	unsigned int mask = kcontrol->private_value & 0xff;
629 	ucontrol->value.integer.value[0] = (emu->emu1010.dac_pads & mask) ? 1 : 0;
630 	return 0;
631 }
632 
snd_emu1010_dac_pads_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)633 static int snd_emu1010_dac_pads_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
634 {
635 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
636 	unsigned int mask = kcontrol->private_value & 0xff;
637 	unsigned int val, cache;
638 	val = ucontrol->value.integer.value[0];
639 	cache = emu->emu1010.dac_pads;
640 	if (val == 1)
641 		cache = cache | mask;
642 	else
643 		cache = cache & ~mask;
644 	if (cache != emu->emu1010.dac_pads) {
645 		snd_emu1010_fpga_write(emu, EMU_HANA_DAC_PADS, cache );
646 	        emu->emu1010.dac_pads = cache;
647 	}
648 
649 	return 0;
650 }
651 
652 
653 
654 #define EMU1010_DAC_PADS(xname,chid) \
655 {								\
656 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
657 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
658 	.info =  snd_emu1010_dac_pads_info,			\
659 	.get =   snd_emu1010_dac_pads_get,			\
660 	.put =   snd_emu1010_dac_pads_put,			\
661 	.private_value = chid					\
662 }
663 
664 static const struct snd_kcontrol_new snd_emu1010_dac_pads[] = {
665 	EMU1010_DAC_PADS("DAC1 Audio Dock 14dB PAD Playback Switch", EMU_HANA_DOCK_DAC_PAD1),
666 	EMU1010_DAC_PADS("DAC2 Audio Dock 14dB PAD Playback Switch", EMU_HANA_DOCK_DAC_PAD2),
667 	EMU1010_DAC_PADS("DAC3 Audio Dock 14dB PAD Playback Switch", EMU_HANA_DOCK_DAC_PAD3),
668 	EMU1010_DAC_PADS("DAC4 Audio Dock 14dB PAD Playback Switch", EMU_HANA_DOCK_DAC_PAD4),
669 	EMU1010_DAC_PADS("DAC1 0202 14dB PAD Playback Switch", EMU_HANA_0202_DAC_PAD1),
670 };
671 
672 
snd_emu1010_internal_clock_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)673 static int snd_emu1010_internal_clock_info(struct snd_kcontrol *kcontrol,
674 					  struct snd_ctl_elem_info *uinfo)
675 {
676 	static const char * const texts[4] = {
677 		"44100", "48000", "SPDIF", "ADAT"
678 	};
679 
680 	return snd_ctl_enum_info(uinfo, 1, 4, texts);
681 }
682 
snd_emu1010_internal_clock_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)683 static int snd_emu1010_internal_clock_get(struct snd_kcontrol *kcontrol,
684 					struct snd_ctl_elem_value *ucontrol)
685 {
686 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
687 
688 	ucontrol->value.enumerated.item[0] = emu->emu1010.internal_clock;
689 	return 0;
690 }
691 
snd_emu1010_internal_clock_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)692 static int snd_emu1010_internal_clock_put(struct snd_kcontrol *kcontrol,
693 					struct snd_ctl_elem_value *ucontrol)
694 {
695 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
696 	unsigned int val;
697 	int change = 0;
698 
699 	val = ucontrol->value.enumerated.item[0] ;
700 	/* Limit: uinfo->value.enumerated.items = 4; */
701 	if (val >= 4)
702 		return -EINVAL;
703 	change = (emu->emu1010.internal_clock != val);
704 	if (change) {
705 		emu->emu1010.internal_clock = val;
706 		switch (val) {
707 		case 0:
708 			/* 44100 */
709 			/* Mute all */
710 			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_MUTE );
711 			/* Default fallback clock 48kHz */
712 			snd_emu1010_fpga_write(emu, EMU_HANA_DEFCLOCK, EMU_HANA_DEFCLOCK_44_1K );
713 			/* Word Clock source, Internal 44.1kHz x1 */
714 			snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK,
715 			EMU_HANA_WCLOCK_INT_44_1K | EMU_HANA_WCLOCK_1X );
716 			/* Set LEDs on Audio Dock */
717 			snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_LEDS_2,
718 				EMU_HANA_DOCK_LEDS_2_44K | EMU_HANA_DOCK_LEDS_2_LOCK );
719 			/* Allow DLL to settle */
720 			msleep(10);
721 			/* Unmute all */
722 			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_UNMUTE );
723 			break;
724 		case 1:
725 			/* 48000 */
726 			/* Mute all */
727 			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_MUTE );
728 			/* Default fallback clock 48kHz */
729 			snd_emu1010_fpga_write(emu, EMU_HANA_DEFCLOCK, EMU_HANA_DEFCLOCK_48K );
730 			/* Word Clock source, Internal 48kHz x1 */
731 			snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK,
732 				EMU_HANA_WCLOCK_INT_48K | EMU_HANA_WCLOCK_1X );
733 			/* Set LEDs on Audio Dock */
734 			snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_LEDS_2,
735 				EMU_HANA_DOCK_LEDS_2_48K | EMU_HANA_DOCK_LEDS_2_LOCK );
736 			/* Allow DLL to settle */
737 			msleep(10);
738 			/* Unmute all */
739 			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_UNMUTE );
740 			break;
741 
742 		case 2: /* Take clock from S/PDIF IN */
743 			/* Mute all */
744 			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_MUTE );
745 			/* Default fallback clock 48kHz */
746 			snd_emu1010_fpga_write(emu, EMU_HANA_DEFCLOCK, EMU_HANA_DEFCLOCK_48K );
747 			/* Word Clock source, sync to S/PDIF input */
748 			snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK,
749 				EMU_HANA_WCLOCK_HANA_SPDIF_IN | EMU_HANA_WCLOCK_1X );
750 			/* Set LEDs on Audio Dock */
751 			snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_LEDS_2,
752 				EMU_HANA_DOCK_LEDS_2_EXT | EMU_HANA_DOCK_LEDS_2_LOCK );
753 			/* FIXME: We should set EMU_HANA_DOCK_LEDS_2_LOCK only when clock signal is present and valid */
754 			/* Allow DLL to settle */
755 			msleep(10);
756 			/* Unmute all */
757 			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_UNMUTE );
758 			break;
759 
760 		case 3:
761 			/* Take clock from ADAT IN */
762 			/* Mute all */
763 			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_MUTE );
764 			/* Default fallback clock 48kHz */
765 			snd_emu1010_fpga_write(emu, EMU_HANA_DEFCLOCK, EMU_HANA_DEFCLOCK_48K );
766 			/* Word Clock source, sync to ADAT input */
767 			snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK,
768 				EMU_HANA_WCLOCK_HANA_ADAT_IN | EMU_HANA_WCLOCK_1X );
769 			/* Set LEDs on Audio Dock */
770 			snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_LEDS_2, EMU_HANA_DOCK_LEDS_2_EXT | EMU_HANA_DOCK_LEDS_2_LOCK );
771 			/* FIXME: We should set EMU_HANA_DOCK_LEDS_2_LOCK only when clock signal is present and valid */
772 			/* Allow DLL to settle */
773 			msleep(10);
774 			/*   Unmute all */
775 			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_UNMUTE );
776 
777 
778 			break;
779 		}
780 	}
781         return change;
782 }
783 
784 static const struct snd_kcontrol_new snd_emu1010_internal_clock =
785 {
786 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
787 	.iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
788 	.name =         "Clock Internal Rate",
789 	.count =	1,
790 	.info =         snd_emu1010_internal_clock_info,
791 	.get =          snd_emu1010_internal_clock_get,
792 	.put =          snd_emu1010_internal_clock_put
793 };
794 
snd_emu1010_optical_out_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)795 static int snd_emu1010_optical_out_info(struct snd_kcontrol *kcontrol,
796 					  struct snd_ctl_elem_info *uinfo)
797 {
798 	static const char * const texts[2] = {
799 		"SPDIF", "ADAT"
800 	};
801 
802 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
803 }
804 
snd_emu1010_optical_out_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)805 static int snd_emu1010_optical_out_get(struct snd_kcontrol *kcontrol,
806 					struct snd_ctl_elem_value *ucontrol)
807 {
808 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
809 
810 	ucontrol->value.enumerated.item[0] = emu->emu1010.optical_out;
811 	return 0;
812 }
813 
snd_emu1010_optical_out_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)814 static int snd_emu1010_optical_out_put(struct snd_kcontrol *kcontrol,
815 					struct snd_ctl_elem_value *ucontrol)
816 {
817 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
818 	unsigned int val;
819 	u32 tmp;
820 	int change = 0;
821 
822 	val = ucontrol->value.enumerated.item[0];
823 	/* Limit: uinfo->value.enumerated.items = 2; */
824 	if (val >= 2)
825 		return -EINVAL;
826 	change = (emu->emu1010.optical_out != val);
827 	if (change) {
828 		emu->emu1010.optical_out = val;
829 		tmp = (emu->emu1010.optical_in ? EMU_HANA_OPTICAL_IN_ADAT : 0) |
830 			(emu->emu1010.optical_out ? EMU_HANA_OPTICAL_OUT_ADAT : 0);
831 		snd_emu1010_fpga_write(emu, EMU_HANA_OPTICAL_TYPE, tmp);
832 	}
833 	return change;
834 }
835 
836 static const struct snd_kcontrol_new snd_emu1010_optical_out = {
837 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
838 	.iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
839 	.name =         "Optical Output Mode",
840 	.count =	1,
841 	.info =         snd_emu1010_optical_out_info,
842 	.get =          snd_emu1010_optical_out_get,
843 	.put =          snd_emu1010_optical_out_put
844 };
845 
snd_emu1010_optical_in_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)846 static int snd_emu1010_optical_in_info(struct snd_kcontrol *kcontrol,
847 					  struct snd_ctl_elem_info *uinfo)
848 {
849 	static const char * const texts[2] = {
850 		"SPDIF", "ADAT"
851 	};
852 
853 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
854 }
855 
snd_emu1010_optical_in_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)856 static int snd_emu1010_optical_in_get(struct snd_kcontrol *kcontrol,
857 					struct snd_ctl_elem_value *ucontrol)
858 {
859 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
860 
861 	ucontrol->value.enumerated.item[0] = emu->emu1010.optical_in;
862 	return 0;
863 }
864 
snd_emu1010_optical_in_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)865 static int snd_emu1010_optical_in_put(struct snd_kcontrol *kcontrol,
866 					struct snd_ctl_elem_value *ucontrol)
867 {
868 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
869 	unsigned int val;
870 	u32 tmp;
871 	int change = 0;
872 
873 	val = ucontrol->value.enumerated.item[0];
874 	/* Limit: uinfo->value.enumerated.items = 2; */
875 	if (val >= 2)
876 		return -EINVAL;
877 	change = (emu->emu1010.optical_in != val);
878 	if (change) {
879 		emu->emu1010.optical_in = val;
880 		tmp = (emu->emu1010.optical_in ? EMU_HANA_OPTICAL_IN_ADAT : 0) |
881 			(emu->emu1010.optical_out ? EMU_HANA_OPTICAL_OUT_ADAT : 0);
882 		snd_emu1010_fpga_write(emu, EMU_HANA_OPTICAL_TYPE, tmp);
883 	}
884 	return change;
885 }
886 
887 static const struct snd_kcontrol_new snd_emu1010_optical_in = {
888 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
889 	.iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
890 	.name =         "Optical Input Mode",
891 	.count =	1,
892 	.info =         snd_emu1010_optical_in_info,
893 	.get =          snd_emu1010_optical_in_get,
894 	.put =          snd_emu1010_optical_in_put
895 };
896 
snd_audigy_i2c_capture_source_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)897 static int snd_audigy_i2c_capture_source_info(struct snd_kcontrol *kcontrol,
898 					  struct snd_ctl_elem_info *uinfo)
899 {
900 #if 0
901 	static const char * const texts[4] = {
902 		"Unknown1", "Unknown2", "Mic", "Line"
903 	};
904 #endif
905 	static const char * const texts[2] = {
906 		"Mic", "Line"
907 	};
908 
909 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
910 }
911 
snd_audigy_i2c_capture_source_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)912 static int snd_audigy_i2c_capture_source_get(struct snd_kcontrol *kcontrol,
913 					struct snd_ctl_elem_value *ucontrol)
914 {
915 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
916 
917 	ucontrol->value.enumerated.item[0] = emu->i2c_capture_source;
918 	return 0;
919 }
920 
snd_audigy_i2c_capture_source_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)921 static int snd_audigy_i2c_capture_source_put(struct snd_kcontrol *kcontrol,
922 					struct snd_ctl_elem_value *ucontrol)
923 {
924 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
925 	unsigned int source_id;
926 	unsigned int ngain, ogain;
927 	u32 gpio;
928 	int change = 0;
929 	unsigned long flags;
930 	u32 source;
931 	/* If the capture source has changed,
932 	 * update the capture volume from the cached value
933 	 * for the particular source.
934 	 */
935 	source_id = ucontrol->value.enumerated.item[0];
936 	/* Limit: uinfo->value.enumerated.items = 2; */
937 	/*        emu->i2c_capture_volume */
938 	if (source_id >= 2)
939 		return -EINVAL;
940 	change = (emu->i2c_capture_source != source_id);
941 	if (change) {
942 		snd_emu10k1_i2c_write(emu, ADC_MUX, 0); /* Mute input */
943 		spin_lock_irqsave(&emu->emu_lock, flags);
944 		gpio = inl(emu->port + A_IOCFG);
945 		if (source_id==0)
946 			outl(gpio | 0x4, emu->port + A_IOCFG);
947 		else
948 			outl(gpio & ~0x4, emu->port + A_IOCFG);
949 		spin_unlock_irqrestore(&emu->emu_lock, flags);
950 
951 		ngain = emu->i2c_capture_volume[source_id][0]; /* Left */
952 		ogain = emu->i2c_capture_volume[emu->i2c_capture_source][0]; /* Left */
953 		if (ngain != ogain)
954 			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff));
955 		ngain = emu->i2c_capture_volume[source_id][1]; /* Right */
956 		ogain = emu->i2c_capture_volume[emu->i2c_capture_source][1]; /* Right */
957 		if (ngain != ogain)
958 			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
959 
960 		source = 1 << (source_id + 2);
961 		snd_emu10k1_i2c_write(emu, ADC_MUX, source); /* Set source */
962 		emu->i2c_capture_source = source_id;
963 	}
964         return change;
965 }
966 
967 static const struct snd_kcontrol_new snd_audigy_i2c_capture_source =
968 {
969 		.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
970 		.name =		"Capture Source",
971 		.info =		snd_audigy_i2c_capture_source_info,
972 		.get =		snd_audigy_i2c_capture_source_get,
973 		.put =		snd_audigy_i2c_capture_source_put
974 };
975 
snd_audigy_i2c_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)976 static int snd_audigy_i2c_volume_info(struct snd_kcontrol *kcontrol,
977 				  struct snd_ctl_elem_info *uinfo)
978 {
979 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
980 	uinfo->count = 2;
981 	uinfo->value.integer.min = 0;
982 	uinfo->value.integer.max = 255;
983 	return 0;
984 }
985 
snd_audigy_i2c_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)986 static int snd_audigy_i2c_volume_get(struct snd_kcontrol *kcontrol,
987 				 struct snd_ctl_elem_value *ucontrol)
988 {
989 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
990 	unsigned int source_id;
991 
992 	source_id = kcontrol->private_value;
993 	/* Limit: emu->i2c_capture_volume */
994         /*        capture_source: uinfo->value.enumerated.items = 2 */
995 	if (source_id >= 2)
996 		return -EINVAL;
997 
998 	ucontrol->value.integer.value[0] = emu->i2c_capture_volume[source_id][0];
999 	ucontrol->value.integer.value[1] = emu->i2c_capture_volume[source_id][1];
1000 	return 0;
1001 }
1002 
snd_audigy_i2c_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1003 static int snd_audigy_i2c_volume_put(struct snd_kcontrol *kcontrol,
1004 				 struct snd_ctl_elem_value *ucontrol)
1005 {
1006 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1007 	unsigned int ogain;
1008 	unsigned int ngain;
1009 	unsigned int source_id;
1010 	int change = 0;
1011 
1012 	source_id = kcontrol->private_value;
1013 	/* Limit: emu->i2c_capture_volume */
1014         /*        capture_source: uinfo->value.enumerated.items = 2 */
1015 	if (source_id >= 2)
1016 		return -EINVAL;
1017 	ogain = emu->i2c_capture_volume[source_id][0]; /* Left */
1018 	ngain = ucontrol->value.integer.value[0];
1019 	if (ngain > 0xff)
1020 		return 0;
1021 	if (ogain != ngain) {
1022 		if (emu->i2c_capture_source == source_id)
1023 			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff) );
1024 		emu->i2c_capture_volume[source_id][0] = ngain;
1025 		change = 1;
1026 	}
1027 	ogain = emu->i2c_capture_volume[source_id][1]; /* Right */
1028 	ngain = ucontrol->value.integer.value[1];
1029 	if (ngain > 0xff)
1030 		return 0;
1031 	if (ogain != ngain) {
1032 		if (emu->i2c_capture_source == source_id)
1033 			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
1034 		emu->i2c_capture_volume[source_id][1] = ngain;
1035 		change = 1;
1036 	}
1037 
1038 	return change;
1039 }
1040 
1041 #define I2C_VOLUME(xname,chid) \
1042 {								\
1043 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
1044 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |		\
1045 	          SNDRV_CTL_ELEM_ACCESS_TLV_READ,		\
1046 	.info =  snd_audigy_i2c_volume_info,			\
1047 	.get =   snd_audigy_i2c_volume_get,			\
1048 	.put =   snd_audigy_i2c_volume_put,			\
1049 	.tlv = { .p = snd_audigy_db_scale2 },			\
1050 	.private_value = chid					\
1051 }
1052 
1053 
1054 static const struct snd_kcontrol_new snd_audigy_i2c_volume_ctls[] = {
1055 	I2C_VOLUME("Mic Capture Volume", 0),
1056 	I2C_VOLUME("Line Capture Volume", 0)
1057 };
1058 
1059 #if 0
1060 static int snd_audigy_spdif_output_rate_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1061 {
1062 	static const char * const texts[] = {"44100", "48000", "96000"};
1063 
1064 	return snd_ctl_enum_info(uinfo, 1, 3, texts);
1065 }
1066 
1067 static int snd_audigy_spdif_output_rate_get(struct snd_kcontrol *kcontrol,
1068                                  struct snd_ctl_elem_value *ucontrol)
1069 {
1070 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1071 	unsigned int tmp;
1072 	unsigned long flags;
1073 
1074 
1075 	spin_lock_irqsave(&emu->reg_lock, flags);
1076 	tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
1077 	switch (tmp & A_SPDIF_RATE_MASK) {
1078 	case A_SPDIF_44100:
1079 		ucontrol->value.enumerated.item[0] = 0;
1080 		break;
1081 	case A_SPDIF_48000:
1082 		ucontrol->value.enumerated.item[0] = 1;
1083 		break;
1084 	case A_SPDIF_96000:
1085 		ucontrol->value.enumerated.item[0] = 2;
1086 		break;
1087 	default:
1088 		ucontrol->value.enumerated.item[0] = 1;
1089 	}
1090 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1091 	return 0;
1092 }
1093 
1094 static int snd_audigy_spdif_output_rate_put(struct snd_kcontrol *kcontrol,
1095                                  struct snd_ctl_elem_value *ucontrol)
1096 {
1097 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1098 	int change;
1099 	unsigned int reg, val, tmp;
1100 	unsigned long flags;
1101 
1102 	switch(ucontrol->value.enumerated.item[0]) {
1103 	case 0:
1104 		val = A_SPDIF_44100;
1105 		break;
1106 	case 1:
1107 		val = A_SPDIF_48000;
1108 		break;
1109 	case 2:
1110 		val = A_SPDIF_96000;
1111 		break;
1112 	default:
1113 		val = A_SPDIF_48000;
1114 		break;
1115 	}
1116 
1117 
1118 	spin_lock_irqsave(&emu->reg_lock, flags);
1119 	reg = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
1120 	tmp = reg & ~A_SPDIF_RATE_MASK;
1121 	tmp |= val;
1122 	change = (tmp != reg);
1123 	if (change)
1124 		snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
1125 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1126 	return change;
1127 }
1128 
1129 static const struct snd_kcontrol_new snd_audigy_spdif_output_rate =
1130 {
1131 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
1132 	.iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1133 	.name =         "Audigy SPDIF Output Sample Rate",
1134 	.count =	1,
1135 	.info =         snd_audigy_spdif_output_rate_info,
1136 	.get =          snd_audigy_spdif_output_rate_get,
1137 	.put =          snd_audigy_spdif_output_rate_put
1138 };
1139 #endif
1140 
snd_emu10k1_spdif_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1141 static int snd_emu10k1_spdif_put(struct snd_kcontrol *kcontrol,
1142                                  struct snd_ctl_elem_value *ucontrol)
1143 {
1144 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1145 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1146 	int change;
1147 	unsigned int val;
1148 	unsigned long flags;
1149 
1150 	/* Limit: emu->spdif_bits */
1151 	if (idx >= 3)
1152 		return -EINVAL;
1153 	val = (ucontrol->value.iec958.status[0] << 0) |
1154 	      (ucontrol->value.iec958.status[1] << 8) |
1155 	      (ucontrol->value.iec958.status[2] << 16) |
1156 	      (ucontrol->value.iec958.status[3] << 24);
1157 	spin_lock_irqsave(&emu->reg_lock, flags);
1158 	change = val != emu->spdif_bits[idx];
1159 	if (change) {
1160 		snd_emu10k1_ptr_write(emu, SPCS0 + idx, 0, val);
1161 		emu->spdif_bits[idx] = val;
1162 	}
1163 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1164 	return change;
1165 }
1166 
1167 static const struct snd_kcontrol_new snd_emu10k1_spdif_mask_control =
1168 {
1169 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1170 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1171 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1172 	.count =	3,
1173 	.info =         snd_emu10k1_spdif_info,
1174 	.get =          snd_emu10k1_spdif_get_mask
1175 };
1176 
1177 static const struct snd_kcontrol_new snd_emu10k1_spdif_control =
1178 {
1179 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1180 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1181 	.count =	3,
1182 	.info =         snd_emu10k1_spdif_info,
1183 	.get =          snd_emu10k1_spdif_get,
1184 	.put =          snd_emu10k1_spdif_put
1185 };
1186 
1187 
update_emu10k1_fxrt(struct snd_emu10k1 * emu,int voice,unsigned char * route)1188 static void update_emu10k1_fxrt(struct snd_emu10k1 *emu, int voice, unsigned char *route)
1189 {
1190 	if (emu->audigy) {
1191 		snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
1192 				      snd_emu10k1_compose_audigy_fxrt1(route));
1193 		snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
1194 				      snd_emu10k1_compose_audigy_fxrt2(route));
1195 	} else {
1196 		snd_emu10k1_ptr_write(emu, FXRT, voice,
1197 				      snd_emu10k1_compose_send_routing(route));
1198 	}
1199 }
1200 
update_emu10k1_send_volume(struct snd_emu10k1 * emu,int voice,unsigned char * volume)1201 static void update_emu10k1_send_volume(struct snd_emu10k1 *emu, int voice, unsigned char *volume)
1202 {
1203 	snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_A, voice, volume[0]);
1204 	snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_B, voice, volume[1]);
1205 	snd_emu10k1_ptr_write(emu, PSST_FXSENDAMOUNT_C, voice, volume[2]);
1206 	snd_emu10k1_ptr_write(emu, DSL_FXSENDAMOUNT_D, voice, volume[3]);
1207 	if (emu->audigy) {
1208 		unsigned int val = ((unsigned int)volume[4] << 24) |
1209 			((unsigned int)volume[5] << 16) |
1210 			((unsigned int)volume[6] << 8) |
1211 			(unsigned int)volume[7];
1212 		snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice, val);
1213 	}
1214 }
1215 
1216 /* PCM stream controls */
1217 
snd_emu10k1_send_routing_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1218 static int snd_emu10k1_send_routing_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1219 {
1220 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1221 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1222 	uinfo->count = emu->audigy ? 3*8 : 3*4;
1223 	uinfo->value.integer.min = 0;
1224 	uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
1225 	return 0;
1226 }
1227 
snd_emu10k1_send_routing_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1228 static int snd_emu10k1_send_routing_get(struct snd_kcontrol *kcontrol,
1229                                         struct snd_ctl_elem_value *ucontrol)
1230 {
1231 	unsigned long flags;
1232 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1233 	struct snd_emu10k1_pcm_mixer *mix =
1234 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1235 	int voice, idx;
1236 	int num_efx = emu->audigy ? 8 : 4;
1237 	int mask = emu->audigy ? 0x3f : 0x0f;
1238 
1239 	spin_lock_irqsave(&emu->reg_lock, flags);
1240 	for (voice = 0; voice < 3; voice++)
1241 		for (idx = 0; idx < num_efx; idx++)
1242 			ucontrol->value.integer.value[(voice * num_efx) + idx] =
1243 				mix->send_routing[voice][idx] & mask;
1244 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1245 	return 0;
1246 }
1247 
snd_emu10k1_send_routing_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1248 static int snd_emu10k1_send_routing_put(struct snd_kcontrol *kcontrol,
1249                                         struct snd_ctl_elem_value *ucontrol)
1250 {
1251 	unsigned long flags;
1252 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1253 	struct snd_emu10k1_pcm_mixer *mix =
1254 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1255 	int change = 0, voice, idx, val;
1256 	int num_efx = emu->audigy ? 8 : 4;
1257 	int mask = emu->audigy ? 0x3f : 0x0f;
1258 
1259 	spin_lock_irqsave(&emu->reg_lock, flags);
1260 	for (voice = 0; voice < 3; voice++)
1261 		for (idx = 0; idx < num_efx; idx++) {
1262 			val = ucontrol->value.integer.value[(voice * num_efx) + idx] & mask;
1263 			if (mix->send_routing[voice][idx] != val) {
1264 				mix->send_routing[voice][idx] = val;
1265 				change = 1;
1266 			}
1267 		}
1268 	if (change && mix->epcm) {
1269 		if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
1270 			update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
1271 					    &mix->send_routing[1][0]);
1272 			update_emu10k1_fxrt(emu, mix->epcm->voices[1]->number,
1273 					    &mix->send_routing[2][0]);
1274 		} else if (mix->epcm->voices[0]) {
1275 			update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
1276 					    &mix->send_routing[0][0]);
1277 		}
1278 	}
1279 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1280 	return change;
1281 }
1282 
1283 static const struct snd_kcontrol_new snd_emu10k1_send_routing_control =
1284 {
1285 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1286 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1287 	.name =         "EMU10K1 PCM Send Routing",
1288 	.count =	32,
1289 	.info =         snd_emu10k1_send_routing_info,
1290 	.get =          snd_emu10k1_send_routing_get,
1291 	.put =          snd_emu10k1_send_routing_put
1292 };
1293 
snd_emu10k1_send_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1294 static int snd_emu10k1_send_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1295 {
1296 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1297 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1298 	uinfo->count = emu->audigy ? 3*8 : 3*4;
1299 	uinfo->value.integer.min = 0;
1300 	uinfo->value.integer.max = 255;
1301 	return 0;
1302 }
1303 
snd_emu10k1_send_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1304 static int snd_emu10k1_send_volume_get(struct snd_kcontrol *kcontrol,
1305                                        struct snd_ctl_elem_value *ucontrol)
1306 {
1307 	unsigned long flags;
1308 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1309 	struct snd_emu10k1_pcm_mixer *mix =
1310 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1311 	int idx;
1312 	int num_efx = emu->audigy ? 8 : 4;
1313 
1314 	spin_lock_irqsave(&emu->reg_lock, flags);
1315 	for (idx = 0; idx < 3*num_efx; idx++)
1316 		ucontrol->value.integer.value[idx] = mix->send_volume[idx/num_efx][idx%num_efx];
1317 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1318 	return 0;
1319 }
1320 
snd_emu10k1_send_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1321 static int snd_emu10k1_send_volume_put(struct snd_kcontrol *kcontrol,
1322                                        struct snd_ctl_elem_value *ucontrol)
1323 {
1324 	unsigned long flags;
1325 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1326 	struct snd_emu10k1_pcm_mixer *mix =
1327 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1328 	int change = 0, idx, val;
1329 	int num_efx = emu->audigy ? 8 : 4;
1330 
1331 	spin_lock_irqsave(&emu->reg_lock, flags);
1332 	for (idx = 0; idx < 3*num_efx; idx++) {
1333 		val = ucontrol->value.integer.value[idx] & 255;
1334 		if (mix->send_volume[idx/num_efx][idx%num_efx] != val) {
1335 			mix->send_volume[idx/num_efx][idx%num_efx] = val;
1336 			change = 1;
1337 		}
1338 	}
1339 	if (change && mix->epcm) {
1340 		if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
1341 			update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
1342 						   &mix->send_volume[1][0]);
1343 			update_emu10k1_send_volume(emu, mix->epcm->voices[1]->number,
1344 						   &mix->send_volume[2][0]);
1345 		} else if (mix->epcm->voices[0]) {
1346 			update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
1347 						   &mix->send_volume[0][0]);
1348 		}
1349 	}
1350 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1351 	return change;
1352 }
1353 
1354 static const struct snd_kcontrol_new snd_emu10k1_send_volume_control =
1355 {
1356 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1357 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1358 	.name =         "EMU10K1 PCM Send Volume",
1359 	.count =	32,
1360 	.info =         snd_emu10k1_send_volume_info,
1361 	.get =          snd_emu10k1_send_volume_get,
1362 	.put =          snd_emu10k1_send_volume_put
1363 };
1364 
snd_emu10k1_attn_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1365 static int snd_emu10k1_attn_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1366 {
1367 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1368 	uinfo->count = 3;
1369 	uinfo->value.integer.min = 0;
1370 	uinfo->value.integer.max = 0xffff;
1371 	return 0;
1372 }
1373 
snd_emu10k1_attn_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1374 static int snd_emu10k1_attn_get(struct snd_kcontrol *kcontrol,
1375                                 struct snd_ctl_elem_value *ucontrol)
1376 {
1377 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1378 	struct snd_emu10k1_pcm_mixer *mix =
1379 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1380 	unsigned long flags;
1381 	int idx;
1382 
1383 	spin_lock_irqsave(&emu->reg_lock, flags);
1384 	for (idx = 0; idx < 3; idx++)
1385 		ucontrol->value.integer.value[idx] = mix->attn[idx];
1386 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1387 	return 0;
1388 }
1389 
snd_emu10k1_attn_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1390 static int snd_emu10k1_attn_put(struct snd_kcontrol *kcontrol,
1391 				struct snd_ctl_elem_value *ucontrol)
1392 {
1393 	unsigned long flags;
1394 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1395 	struct snd_emu10k1_pcm_mixer *mix =
1396 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1397 	int change = 0, idx, val;
1398 
1399 	spin_lock_irqsave(&emu->reg_lock, flags);
1400 	for (idx = 0; idx < 3; idx++) {
1401 		val = ucontrol->value.integer.value[idx] & 0xffff;
1402 		if (mix->attn[idx] != val) {
1403 			mix->attn[idx] = val;
1404 			change = 1;
1405 		}
1406 	}
1407 	if (change && mix->epcm) {
1408 		if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
1409 			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[1]);
1410 			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[1]->number, mix->attn[2]);
1411 		} else if (mix->epcm->voices[0]) {
1412 			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[0]);
1413 		}
1414 	}
1415 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1416 	return change;
1417 }
1418 
1419 static const struct snd_kcontrol_new snd_emu10k1_attn_control =
1420 {
1421 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1422 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1423 	.name =         "EMU10K1 PCM Volume",
1424 	.count =	32,
1425 	.info =         snd_emu10k1_attn_info,
1426 	.get =          snd_emu10k1_attn_get,
1427 	.put =          snd_emu10k1_attn_put
1428 };
1429 
1430 /* Mutichannel PCM stream controls */
1431 
snd_emu10k1_efx_send_routing_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1432 static int snd_emu10k1_efx_send_routing_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1433 {
1434 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1435 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1436 	uinfo->count = emu->audigy ? 8 : 4;
1437 	uinfo->value.integer.min = 0;
1438 	uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
1439 	return 0;
1440 }
1441 
snd_emu10k1_efx_send_routing_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1442 static int snd_emu10k1_efx_send_routing_get(struct snd_kcontrol *kcontrol,
1443                                         struct snd_ctl_elem_value *ucontrol)
1444 {
1445 	unsigned long flags;
1446 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1447 	struct snd_emu10k1_pcm_mixer *mix =
1448 		&emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1449 	int idx;
1450 	int num_efx = emu->audigy ? 8 : 4;
1451 	int mask = emu->audigy ? 0x3f : 0x0f;
1452 
1453 	spin_lock_irqsave(&emu->reg_lock, flags);
1454 	for (idx = 0; idx < num_efx; idx++)
1455 		ucontrol->value.integer.value[idx] =
1456 			mix->send_routing[0][idx] & mask;
1457 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1458 	return 0;
1459 }
1460 
snd_emu10k1_efx_send_routing_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1461 static int snd_emu10k1_efx_send_routing_put(struct snd_kcontrol *kcontrol,
1462                                         struct snd_ctl_elem_value *ucontrol)
1463 {
1464 	unsigned long flags;
1465 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1466 	int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1467 	struct snd_emu10k1_pcm_mixer *mix = &emu->efx_pcm_mixer[ch];
1468 	int change = 0, idx, val;
1469 	int num_efx = emu->audigy ? 8 : 4;
1470 	int mask = emu->audigy ? 0x3f : 0x0f;
1471 
1472 	spin_lock_irqsave(&emu->reg_lock, flags);
1473 	for (idx = 0; idx < num_efx; idx++) {
1474 		val = ucontrol->value.integer.value[idx] & mask;
1475 		if (mix->send_routing[0][idx] != val) {
1476 			mix->send_routing[0][idx] = val;
1477 			change = 1;
1478 		}
1479 	}
1480 
1481 	if (change && mix->epcm) {
1482 		if (mix->epcm->voices[ch]) {
1483 			update_emu10k1_fxrt(emu, mix->epcm->voices[ch]->number,
1484 					&mix->send_routing[0][0]);
1485 		}
1486 	}
1487 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1488 	return change;
1489 }
1490 
1491 static const struct snd_kcontrol_new snd_emu10k1_efx_send_routing_control =
1492 {
1493 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1494 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1495 	.name =         "Multichannel PCM Send Routing",
1496 	.count =	16,
1497 	.info =         snd_emu10k1_efx_send_routing_info,
1498 	.get =          snd_emu10k1_efx_send_routing_get,
1499 	.put =          snd_emu10k1_efx_send_routing_put
1500 };
1501 
snd_emu10k1_efx_send_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1502 static int snd_emu10k1_efx_send_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1503 {
1504 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1505 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1506 	uinfo->count = emu->audigy ? 8 : 4;
1507 	uinfo->value.integer.min = 0;
1508 	uinfo->value.integer.max = 255;
1509 	return 0;
1510 }
1511 
snd_emu10k1_efx_send_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1512 static int snd_emu10k1_efx_send_volume_get(struct snd_kcontrol *kcontrol,
1513                                        struct snd_ctl_elem_value *ucontrol)
1514 {
1515 	unsigned long flags;
1516 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1517 	struct snd_emu10k1_pcm_mixer *mix =
1518 		&emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1519 	int idx;
1520 	int num_efx = emu->audigy ? 8 : 4;
1521 
1522 	spin_lock_irqsave(&emu->reg_lock, flags);
1523 	for (idx = 0; idx < num_efx; idx++)
1524 		ucontrol->value.integer.value[idx] = mix->send_volume[0][idx];
1525 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1526 	return 0;
1527 }
1528 
snd_emu10k1_efx_send_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1529 static int snd_emu10k1_efx_send_volume_put(struct snd_kcontrol *kcontrol,
1530                                        struct snd_ctl_elem_value *ucontrol)
1531 {
1532 	unsigned long flags;
1533 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1534 	int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1535 	struct snd_emu10k1_pcm_mixer *mix = &emu->efx_pcm_mixer[ch];
1536 	int change = 0, idx, val;
1537 	int num_efx = emu->audigy ? 8 : 4;
1538 
1539 	spin_lock_irqsave(&emu->reg_lock, flags);
1540 	for (idx = 0; idx < num_efx; idx++) {
1541 		val = ucontrol->value.integer.value[idx] & 255;
1542 		if (mix->send_volume[0][idx] != val) {
1543 			mix->send_volume[0][idx] = val;
1544 			change = 1;
1545 		}
1546 	}
1547 	if (change && mix->epcm) {
1548 		if (mix->epcm->voices[ch]) {
1549 			update_emu10k1_send_volume(emu, mix->epcm->voices[ch]->number,
1550 						   &mix->send_volume[0][0]);
1551 		}
1552 	}
1553 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1554 	return change;
1555 }
1556 
1557 
1558 static const struct snd_kcontrol_new snd_emu10k1_efx_send_volume_control =
1559 {
1560 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1561 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1562 	.name =         "Multichannel PCM Send Volume",
1563 	.count =	16,
1564 	.info =         snd_emu10k1_efx_send_volume_info,
1565 	.get =          snd_emu10k1_efx_send_volume_get,
1566 	.put =          snd_emu10k1_efx_send_volume_put
1567 };
1568 
snd_emu10k1_efx_attn_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1569 static int snd_emu10k1_efx_attn_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1570 {
1571 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1572 	uinfo->count = 1;
1573 	uinfo->value.integer.min = 0;
1574 	uinfo->value.integer.max = 0xffff;
1575 	return 0;
1576 }
1577 
snd_emu10k1_efx_attn_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1578 static int snd_emu10k1_efx_attn_get(struct snd_kcontrol *kcontrol,
1579                                 struct snd_ctl_elem_value *ucontrol)
1580 {
1581 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1582 	struct snd_emu10k1_pcm_mixer *mix =
1583 		&emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1584 	unsigned long flags;
1585 
1586 	spin_lock_irqsave(&emu->reg_lock, flags);
1587 	ucontrol->value.integer.value[0] = mix->attn[0];
1588 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1589 	return 0;
1590 }
1591 
snd_emu10k1_efx_attn_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1592 static int snd_emu10k1_efx_attn_put(struct snd_kcontrol *kcontrol,
1593 				struct snd_ctl_elem_value *ucontrol)
1594 {
1595 	unsigned long flags;
1596 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1597 	int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1598 	struct snd_emu10k1_pcm_mixer *mix = &emu->efx_pcm_mixer[ch];
1599 	int change = 0, val;
1600 
1601 	spin_lock_irqsave(&emu->reg_lock, flags);
1602 	val = ucontrol->value.integer.value[0] & 0xffff;
1603 	if (mix->attn[0] != val) {
1604 		mix->attn[0] = val;
1605 		change = 1;
1606 	}
1607 	if (change && mix->epcm) {
1608 		if (mix->epcm->voices[ch]) {
1609 			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[ch]->number, mix->attn[0]);
1610 		}
1611 	}
1612 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1613 	return change;
1614 }
1615 
1616 static const struct snd_kcontrol_new snd_emu10k1_efx_attn_control =
1617 {
1618 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1619 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1620 	.name =         "Multichannel PCM Volume",
1621 	.count =	16,
1622 	.info =         snd_emu10k1_efx_attn_info,
1623 	.get =          snd_emu10k1_efx_attn_get,
1624 	.put =          snd_emu10k1_efx_attn_put
1625 };
1626 
1627 #define snd_emu10k1_shared_spdif_info	snd_ctl_boolean_mono_info
1628 
snd_emu10k1_shared_spdif_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1629 static int snd_emu10k1_shared_spdif_get(struct snd_kcontrol *kcontrol,
1630 					struct snd_ctl_elem_value *ucontrol)
1631 {
1632 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1633 
1634 	if (emu->audigy)
1635 		ucontrol->value.integer.value[0] = inl(emu->port + A_IOCFG) & A_IOCFG_GPOUT0 ? 1 : 0;
1636 	else
1637 		ucontrol->value.integer.value[0] = inl(emu->port + HCFG) & HCFG_GPOUT0 ? 1 : 0;
1638 	if (emu->card_capabilities->invert_shared_spdif)
1639 		ucontrol->value.integer.value[0] =
1640 			!ucontrol->value.integer.value[0];
1641 
1642 	return 0;
1643 }
1644 
snd_emu10k1_shared_spdif_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1645 static int snd_emu10k1_shared_spdif_put(struct snd_kcontrol *kcontrol,
1646 					struct snd_ctl_elem_value *ucontrol)
1647 {
1648 	unsigned long flags;
1649 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1650 	unsigned int reg, val, sw;
1651 	int change = 0;
1652 
1653 	sw = ucontrol->value.integer.value[0];
1654 	if (emu->card_capabilities->invert_shared_spdif)
1655 		sw = !sw;
1656 	spin_lock_irqsave(&emu->reg_lock, flags);
1657 	if ( emu->card_capabilities->i2c_adc) {
1658 		/* Do nothing for Audigy 2 ZS Notebook */
1659 	} else if (emu->audigy) {
1660 		reg = inl(emu->port + A_IOCFG);
1661 		val = sw ? A_IOCFG_GPOUT0 : 0;
1662 		change = (reg & A_IOCFG_GPOUT0) != val;
1663 		if (change) {
1664 			reg &= ~A_IOCFG_GPOUT0;
1665 			reg |= val;
1666 			outl(reg | val, emu->port + A_IOCFG);
1667 		}
1668 	}
1669 	reg = inl(emu->port + HCFG);
1670 	val = sw ? HCFG_GPOUT0 : 0;
1671 	change |= (reg & HCFG_GPOUT0) != val;
1672 	if (change) {
1673 		reg &= ~HCFG_GPOUT0;
1674 		reg |= val;
1675 		outl(reg | val, emu->port + HCFG);
1676 	}
1677 	spin_unlock_irqrestore(&emu->reg_lock, flags);
1678 	return change;
1679 }
1680 
1681 static const struct snd_kcontrol_new snd_emu10k1_shared_spdif =
1682 {
1683 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
1684 	.name =		"SB Live Analog/Digital Output Jack",
1685 	.info =		snd_emu10k1_shared_spdif_info,
1686 	.get =		snd_emu10k1_shared_spdif_get,
1687 	.put =		snd_emu10k1_shared_spdif_put
1688 };
1689 
1690 static const struct snd_kcontrol_new snd_audigy_shared_spdif =
1691 {
1692 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
1693 	.name =		"Audigy Analog/Digital Output Jack",
1694 	.info =		snd_emu10k1_shared_spdif_info,
1695 	.get =		snd_emu10k1_shared_spdif_get,
1696 	.put =		snd_emu10k1_shared_spdif_put
1697 };
1698 
1699 /* workaround for too low volume on Audigy due to 16bit/24bit conversion */
1700 
1701 #define snd_audigy_capture_boost_info	snd_ctl_boolean_mono_info
1702 
snd_audigy_capture_boost_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1703 static int snd_audigy_capture_boost_get(struct snd_kcontrol *kcontrol,
1704 					struct snd_ctl_elem_value *ucontrol)
1705 {
1706 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1707 	unsigned int val;
1708 
1709 	/* FIXME: better to use a cached version */
1710 	val = snd_ac97_read(emu->ac97, AC97_REC_GAIN);
1711 	ucontrol->value.integer.value[0] = !!val;
1712 	return 0;
1713 }
1714 
snd_audigy_capture_boost_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1715 static int snd_audigy_capture_boost_put(struct snd_kcontrol *kcontrol,
1716 					struct snd_ctl_elem_value *ucontrol)
1717 {
1718 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1719 	unsigned int val;
1720 
1721 	if (ucontrol->value.integer.value[0])
1722 		val = 0x0f0f;
1723 	else
1724 		val = 0;
1725 	return snd_ac97_update(emu->ac97, AC97_REC_GAIN, val);
1726 }
1727 
1728 static const struct snd_kcontrol_new snd_audigy_capture_boost =
1729 {
1730 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
1731 	.name =		"Mic Extra Boost",
1732 	.info =		snd_audigy_capture_boost_info,
1733 	.get =		snd_audigy_capture_boost_get,
1734 	.put =		snd_audigy_capture_boost_put
1735 };
1736 
1737 
1738 /*
1739  */
snd_emu10k1_mixer_free_ac97(struct snd_ac97 * ac97)1740 static void snd_emu10k1_mixer_free_ac97(struct snd_ac97 *ac97)
1741 {
1742 	struct snd_emu10k1 *emu = ac97->private_data;
1743 	emu->ac97 = NULL;
1744 }
1745 
1746 /*
1747  */
remove_ctl(struct snd_card * card,const char * name)1748 static int remove_ctl(struct snd_card *card, const char *name)
1749 {
1750 	struct snd_ctl_elem_id id;
1751 	memset(&id, 0, sizeof(id));
1752 	strcpy(id.name, name);
1753 	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1754 	return snd_ctl_remove_id(card, &id);
1755 }
1756 
ctl_find(struct snd_card * card,const char * name)1757 static struct snd_kcontrol *ctl_find(struct snd_card *card, const char *name)
1758 {
1759 	struct snd_ctl_elem_id sid;
1760 	memset(&sid, 0, sizeof(sid));
1761 	strcpy(sid.name, name);
1762 	sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1763 	return snd_ctl_find_id(card, &sid);
1764 }
1765 
rename_ctl(struct snd_card * card,const char * src,const char * dst)1766 static int rename_ctl(struct snd_card *card, const char *src, const char *dst)
1767 {
1768 	struct snd_kcontrol *kctl = ctl_find(card, src);
1769 	if (kctl) {
1770 		snd_ctl_rename(card, kctl, dst);
1771 		return 0;
1772 	}
1773 	return -ENOENT;
1774 }
1775 
snd_emu10k1_mixer(struct snd_emu10k1 * emu,int pcm_device,int multi_device)1776 int snd_emu10k1_mixer(struct snd_emu10k1 *emu,
1777 		      int pcm_device, int multi_device)
1778 {
1779 	int err, pcm;
1780 	struct snd_kcontrol *kctl;
1781 	struct snd_card *card = emu->card;
1782 	const char * const *c;
1783 	static const char * const emu10k1_remove_ctls[] = {
1784 		/* no AC97 mono, surround, center/lfe */
1785 		"Master Mono Playback Switch",
1786 		"Master Mono Playback Volume",
1787 		"PCM Out Path & Mute",
1788 		"Mono Output Select",
1789 		"Surround Playback Switch",
1790 		"Surround Playback Volume",
1791 		"Center Playback Switch",
1792 		"Center Playback Volume",
1793 		"LFE Playback Switch",
1794 		"LFE Playback Volume",
1795 		NULL
1796 	};
1797 	static const char * const emu10k1_rename_ctls[] = {
1798 		"Surround Digital Playback Volume", "Surround Playback Volume",
1799 		"Center Digital Playback Volume", "Center Playback Volume",
1800 		"LFE Digital Playback Volume", "LFE Playback Volume",
1801 		NULL
1802 	};
1803 	static const char * const audigy_remove_ctls[] = {
1804 		/* Master/PCM controls on ac97 of Audigy has no effect */
1805 		/* On the Audigy2 the AC97 playback is piped into
1806 		 * the Philips ADC for 24bit capture */
1807 		"PCM Playback Switch",
1808 		"PCM Playback Volume",
1809 		"Master Playback Switch",
1810 		"Master Playback Volume",
1811 		"PCM Out Path & Mute",
1812 		"Mono Output Select",
1813 		/* remove unused AC97 capture controls */
1814 		"Capture Source",
1815 		"Capture Switch",
1816 		"Capture Volume",
1817 		"Mic Select",
1818 		"Headphone Playback Switch",
1819 		"Headphone Playback Volume",
1820 		"3D Control - Center",
1821 		"3D Control - Depth",
1822 		"3D Control - Switch",
1823 		"Video Playback Switch",
1824 		"Video Playback Volume",
1825 		"Mic Playback Switch",
1826 		"Mic Playback Volume",
1827 		"External Amplifier",
1828 		NULL
1829 	};
1830 	static const char * const audigy_rename_ctls[] = {
1831 		/* use conventional names */
1832 		"Wave Playback Volume", "PCM Playback Volume",
1833 		/* "Wave Capture Volume", "PCM Capture Volume", */
1834 		"Wave Master Playback Volume", "Master Playback Volume",
1835 		"AMic Playback Volume", "Mic Playback Volume",
1836 		"Master Mono Playback Switch", "Phone Output Playback Switch",
1837 		"Master Mono Playback Volume", "Phone Output Playback Volume",
1838 		NULL
1839 	};
1840 	static const char * const audigy_rename_ctls_i2c_adc[] = {
1841 		//"Analog Mix Capture Volume","OLD Analog Mix Capture Volume",
1842 		"Line Capture Volume", "Analog Mix Capture Volume",
1843 		"Wave Playback Volume", "OLD PCM Playback Volume",
1844 		"Wave Master Playback Volume", "Master Playback Volume",
1845 		"AMic Playback Volume", "Old Mic Playback Volume",
1846 		"CD Capture Volume", "IEC958 Optical Capture Volume",
1847 		NULL
1848 	};
1849 	static const char * const audigy_remove_ctls_i2c_adc[] = {
1850 		/* On the Audigy2 ZS Notebook
1851 		 * Capture via WM8775  */
1852 		"Mic Capture Volume",
1853 		"Analog Mix Capture Volume",
1854 		"Aux Capture Volume",
1855 		"IEC958 Optical Capture Volume",
1856 		NULL
1857 	};
1858 	static const char * const audigy_remove_ctls_1361t_adc[] = {
1859 		/* On the Audigy2 the AC97 playback is piped into
1860 		 * the Philips ADC for 24bit capture */
1861 		"PCM Playback Switch",
1862 		"PCM Playback Volume",
1863 		"Capture Source",
1864 		"Capture Switch",
1865 		"Capture Volume",
1866 		"Mic Capture Volume",
1867 		"Headphone Playback Switch",
1868 		"Headphone Playback Volume",
1869 		"3D Control - Center",
1870 		"3D Control - Depth",
1871 		"3D Control - Switch",
1872 		"Line2 Playback Volume",
1873 		"Line2 Capture Volume",
1874 		NULL
1875 	};
1876 	static const char * const audigy_rename_ctls_1361t_adc[] = {
1877 		"Master Playback Switch", "Master Capture Switch",
1878 		"Master Playback Volume", "Master Capture Volume",
1879 		"Wave Master Playback Volume", "Master Playback Volume",
1880 		"Beep Playback Switch", "Beep Capture Switch",
1881 		"Beep Playback Volume", "Beep Capture Volume",
1882 		"Phone Playback Switch", "Phone Capture Switch",
1883 		"Phone Playback Volume", "Phone Capture Volume",
1884 		"Mic Playback Switch", "Mic Capture Switch",
1885 		"Mic Playback Volume", "Mic Capture Volume",
1886 		"Line Playback Switch", "Line Capture Switch",
1887 		"Line Playback Volume", "Line Capture Volume",
1888 		"CD Playback Switch", "CD Capture Switch",
1889 		"CD Playback Volume", "CD Capture Volume",
1890 		"Aux Playback Switch", "Aux Capture Switch",
1891 		"Aux Playback Volume", "Aux Capture Volume",
1892 		"Video Playback Switch", "Video Capture Switch",
1893 		"Video Playback Volume", "Video Capture Volume",
1894 		"Master Mono Playback Switch", "Phone Output Playback Switch",
1895 		"Master Mono Playback Volume", "Phone Output Playback Volume",
1896 		NULL
1897 	};
1898 
1899 	if (emu->card_capabilities->ac97_chip) {
1900 		struct snd_ac97_bus *pbus;
1901 		struct snd_ac97_template ac97;
1902 		static const struct snd_ac97_bus_ops ops = {
1903 			.write = snd_emu10k1_ac97_write,
1904 			.read = snd_emu10k1_ac97_read,
1905 		};
1906 
1907 		err = snd_ac97_bus(emu->card, 0, &ops, NULL, &pbus);
1908 		if (err < 0)
1909 			return err;
1910 		pbus->no_vra = 1; /* we don't need VRA */
1911 
1912 		memset(&ac97, 0, sizeof(ac97));
1913 		ac97.private_data = emu;
1914 		ac97.private_free = snd_emu10k1_mixer_free_ac97;
1915 		ac97.scaps = AC97_SCAP_NO_SPDIF;
1916 		err = snd_ac97_mixer(pbus, &ac97, &emu->ac97);
1917 		if (err < 0) {
1918 			if (emu->card_capabilities->ac97_chip == 1)
1919 				return err;
1920 			dev_info(emu->card->dev,
1921 				 "AC97 is optional on this board\n");
1922 			dev_info(emu->card->dev,
1923 				 "Proceeding without ac97 mixers...\n");
1924 			snd_device_free(emu->card, pbus);
1925 			goto no_ac97; /* FIXME: get rid of ugly gotos.. */
1926 		}
1927 		if (emu->audigy) {
1928 			/* set master volume to 0 dB */
1929 			snd_ac97_write_cache(emu->ac97, AC97_MASTER, 0x0000);
1930 			/* set capture source to mic */
1931 			snd_ac97_write_cache(emu->ac97, AC97_REC_SEL, 0x0000);
1932 			/* set mono output (TAD) to mic */
1933 			snd_ac97_update_bits(emu->ac97, AC97_GENERAL_PURPOSE,
1934 				0x0200, 0x0200);
1935 			if (emu->card_capabilities->adc_1361t)
1936 				c = audigy_remove_ctls_1361t_adc;
1937 			else
1938 				c = audigy_remove_ctls;
1939 		} else {
1940 			/*
1941 			 * Credits for cards based on STAC9758:
1942 			 *   James Courtier-Dutton <James@superbug.demon.co.uk>
1943 			 *   Voluspa <voluspa@comhem.se>
1944 			 */
1945 			if (emu->ac97->id == AC97_ID_STAC9758) {
1946 				emu->rear_ac97 = 1;
1947 				snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE|AC97SLOT_REAR_LEFT|AC97SLOT_REAR_RIGHT);
1948 				snd_ac97_write_cache(emu->ac97, AC97_HEADPHONE, 0x0202);
1949 				remove_ctl(card,"Front Playback Volume");
1950 				remove_ctl(card,"Front Playback Switch");
1951 			}
1952 			/* remove unused AC97 controls */
1953 			snd_ac97_write_cache(emu->ac97, AC97_SURROUND_MASTER, 0x0202);
1954 			snd_ac97_write_cache(emu->ac97, AC97_CENTER_LFE_MASTER, 0x0202);
1955 			c = emu10k1_remove_ctls;
1956 		}
1957 		for (; *c; c++)
1958 			remove_ctl(card, *c);
1959 	} else if (emu->card_capabilities->i2c_adc) {
1960 		c = audigy_remove_ctls_i2c_adc;
1961 		for (; *c; c++)
1962 			remove_ctl(card, *c);
1963 	} else {
1964 	no_ac97:
1965 		if (emu->card_capabilities->ecard)
1966 			strcpy(emu->card->mixername, "EMU APS");
1967 		else if (emu->audigy)
1968 			strcpy(emu->card->mixername, "SB Audigy");
1969 		else
1970 			strcpy(emu->card->mixername, "Emu10k1");
1971 	}
1972 
1973 	if (emu->audigy)
1974 		if (emu->card_capabilities->adc_1361t)
1975 			c = audigy_rename_ctls_1361t_adc;
1976 		else if (emu->card_capabilities->i2c_adc)
1977 			c = audigy_rename_ctls_i2c_adc;
1978 		else
1979 			c = audigy_rename_ctls;
1980 	else
1981 		c = emu10k1_rename_ctls;
1982 	for (; *c; c += 2)
1983 		rename_ctl(card, c[0], c[1]);
1984 
1985 	if (emu->card_capabilities->subsystem == 0x80401102) { /* SB Live! Platinum CT4760P */
1986 		remove_ctl(card, "Center Playback Volume");
1987 		remove_ctl(card, "LFE Playback Volume");
1988 		remove_ctl(card, "Wave Center Playback Volume");
1989 		remove_ctl(card, "Wave LFE Playback Volume");
1990 	}
1991 	if (emu->card_capabilities->subsystem == 0x20071102) {  /* Audigy 4 Pro */
1992 		rename_ctl(card, "Line2 Capture Volume", "Line1/Mic Capture Volume");
1993 		rename_ctl(card, "Analog Mix Capture Volume", "Line2 Capture Volume");
1994 		rename_ctl(card, "Aux2 Capture Volume", "Line3 Capture Volume");
1995 		rename_ctl(card, "Mic Capture Volume", "Unknown1 Capture Volume");
1996 	}
1997 	kctl = emu->ctl_send_routing = snd_ctl_new1(&snd_emu10k1_send_routing_control, emu);
1998 	if (!kctl)
1999 		return -ENOMEM;
2000 	kctl->id.device = pcm_device;
2001 	err = snd_ctl_add(card, kctl);
2002 	if (err)
2003 		return err;
2004 	kctl = emu->ctl_send_volume = snd_ctl_new1(&snd_emu10k1_send_volume_control, emu);
2005 	if (!kctl)
2006 		return -ENOMEM;
2007 	kctl->id.device = pcm_device;
2008 	err = snd_ctl_add(card, kctl);
2009 	if (err)
2010 		return err;
2011 	kctl = emu->ctl_attn = snd_ctl_new1(&snd_emu10k1_attn_control, emu);
2012 	if (!kctl)
2013 		return -ENOMEM;
2014 	kctl->id.device = pcm_device;
2015 	err = snd_ctl_add(card, kctl);
2016 	if (err)
2017 		return err;
2018 
2019 	kctl = emu->ctl_efx_send_routing = snd_ctl_new1(&snd_emu10k1_efx_send_routing_control, emu);
2020 	if (!kctl)
2021 		return -ENOMEM;
2022 	kctl->id.device = multi_device;
2023 	err = snd_ctl_add(card, kctl);
2024 	if (err)
2025 		return err;
2026 
2027 	kctl = emu->ctl_efx_send_volume = snd_ctl_new1(&snd_emu10k1_efx_send_volume_control, emu);
2028 	if (!kctl)
2029 		return -ENOMEM;
2030 	kctl->id.device = multi_device;
2031 	err = snd_ctl_add(card, kctl);
2032 	if (err)
2033 		return err;
2034 
2035 	kctl = emu->ctl_efx_attn = snd_ctl_new1(&snd_emu10k1_efx_attn_control, emu);
2036 	if (!kctl)
2037 		return -ENOMEM;
2038 	kctl->id.device = multi_device;
2039 	err = snd_ctl_add(card, kctl);
2040 	if (err)
2041 		return err;
2042 
2043 	/* initialize the routing and volume table for each pcm playback stream */
2044 	for (pcm = 0; pcm < 32; pcm++) {
2045 		struct snd_emu10k1_pcm_mixer *mix;
2046 		int v;
2047 
2048 		mix = &emu->pcm_mixer[pcm];
2049 		mix->epcm = NULL;
2050 
2051 		for (v = 0; v < 4; v++)
2052 			mix->send_routing[0][v] =
2053 				mix->send_routing[1][v] =
2054 				mix->send_routing[2][v] = v;
2055 
2056 		memset(&mix->send_volume, 0, sizeof(mix->send_volume));
2057 		mix->send_volume[0][0] = mix->send_volume[0][1] =
2058 		mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
2059 
2060 		mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
2061 	}
2062 
2063 	/* initialize the routing and volume table for the multichannel playback stream */
2064 	for (pcm = 0; pcm < NUM_EFX_PLAYBACK; pcm++) {
2065 		struct snd_emu10k1_pcm_mixer *mix;
2066 		int v;
2067 
2068 		mix = &emu->efx_pcm_mixer[pcm];
2069 		mix->epcm = NULL;
2070 
2071 		mix->send_routing[0][0] = pcm;
2072 		mix->send_routing[0][1] = (pcm == 0) ? 1 : 0;
2073 		for (v = 0; v < 2; v++)
2074 			mix->send_routing[0][2+v] = 13+v;
2075 		if (emu->audigy)
2076 			for (v = 0; v < 4; v++)
2077 				mix->send_routing[0][4+v] = 60+v;
2078 
2079 		memset(&mix->send_volume, 0, sizeof(mix->send_volume));
2080 		mix->send_volume[0][0]  = 255;
2081 
2082 		mix->attn[0] = 0xffff;
2083 	}
2084 
2085 	if (! emu->card_capabilities->ecard) { /* FIXME: APS has these controls? */
2086 		/* sb live! and audigy */
2087 		kctl = snd_ctl_new1(&snd_emu10k1_spdif_mask_control, emu);
2088 		if (!kctl)
2089 			return -ENOMEM;
2090 		if (!emu->audigy)
2091 			kctl->id.device = emu->pcm_efx->device;
2092 		err = snd_ctl_add(card, kctl);
2093 		if (err)
2094 			return err;
2095 		kctl = snd_ctl_new1(&snd_emu10k1_spdif_control, emu);
2096 		if (!kctl)
2097 			return -ENOMEM;
2098 		if (!emu->audigy)
2099 			kctl->id.device = emu->pcm_efx->device;
2100 		err = snd_ctl_add(card, kctl);
2101 		if (err)
2102 			return err;
2103 	}
2104 
2105 	if (emu->card_capabilities->emu_model) {
2106 		;  /* Disable the snd_audigy_spdif_shared_spdif */
2107 	} else if (emu->audigy) {
2108 		kctl = snd_ctl_new1(&snd_audigy_shared_spdif, emu);
2109 		if (!kctl)
2110 			return -ENOMEM;
2111 		err = snd_ctl_add(card, kctl);
2112 		if (err)
2113 			return err;
2114 #if 0
2115 		kctl = snd_ctl_new1(&snd_audigy_spdif_output_rate, emu);
2116 		if (!kctl)
2117 			return -ENOMEM;
2118 		err = snd_ctl_add(card, kctl);
2119 		if (err)
2120 			return err;
2121 #endif
2122 	} else if (! emu->card_capabilities->ecard) {
2123 		/* sb live! */
2124 		kctl = snd_ctl_new1(&snd_emu10k1_shared_spdif, emu);
2125 		if (!kctl)
2126 			return -ENOMEM;
2127 		err = snd_ctl_add(card, kctl);
2128 		if (err)
2129 			return err;
2130 	}
2131 	if (emu->card_capabilities->ca0151_chip) { /* P16V */
2132 		err = snd_p16v_mixer(emu);
2133 		if (err)
2134 			return err;
2135 	}
2136 
2137 	if (emu->card_capabilities->emu_model == EMU_MODEL_EMU1616) {
2138 		/* 1616(m) cardbus */
2139 		int i;
2140 
2141 		for (i = 0; i < ARRAY_SIZE(snd_emu1616_output_enum_ctls); i++) {
2142 			err = snd_ctl_add(card,
2143 				snd_ctl_new1(&snd_emu1616_output_enum_ctls[i],
2144 					     emu));
2145 			if (err < 0)
2146 				return err;
2147 		}
2148 		for (i = 0; i < ARRAY_SIZE(snd_emu1010_input_enum_ctls); i++) {
2149 			err = snd_ctl_add(card,
2150 				snd_ctl_new1(&snd_emu1010_input_enum_ctls[i],
2151 					     emu));
2152 			if (err < 0)
2153 				return err;
2154 		}
2155 		for (i = 0; i < ARRAY_SIZE(snd_emu1010_adc_pads) - 2; i++) {
2156 			err = snd_ctl_add(card,
2157 				snd_ctl_new1(&snd_emu1010_adc_pads[i], emu));
2158 			if (err < 0)
2159 				return err;
2160 		}
2161 		for (i = 0; i < ARRAY_SIZE(snd_emu1010_dac_pads) - 2; i++) {
2162 			err = snd_ctl_add(card,
2163 				snd_ctl_new1(&snd_emu1010_dac_pads[i], emu));
2164 			if (err < 0)
2165 				return err;
2166 		}
2167 		err = snd_ctl_add(card,
2168 			snd_ctl_new1(&snd_emu1010_internal_clock, emu));
2169 		if (err < 0)
2170 			return err;
2171 		err = snd_ctl_add(card,
2172 			snd_ctl_new1(&snd_emu1010_optical_out, emu));
2173 		if (err < 0)
2174 			return err;
2175 		err = snd_ctl_add(card,
2176 			snd_ctl_new1(&snd_emu1010_optical_in, emu));
2177 		if (err < 0)
2178 			return err;
2179 
2180 	} else if (emu->card_capabilities->emu_model) {
2181 		/* all other e-mu cards for now */
2182 		int i;
2183 
2184 		for (i = 0; i < ARRAY_SIZE(snd_emu1010_output_enum_ctls); i++) {
2185 			err = snd_ctl_add(card,
2186 				snd_ctl_new1(&snd_emu1010_output_enum_ctls[i],
2187 					     emu));
2188 			if (err < 0)
2189 				return err;
2190 		}
2191 		for (i = 0; i < ARRAY_SIZE(snd_emu1010_input_enum_ctls); i++) {
2192 			err = snd_ctl_add(card,
2193 				snd_ctl_new1(&snd_emu1010_input_enum_ctls[i],
2194 					     emu));
2195 			if (err < 0)
2196 				return err;
2197 		}
2198 		for (i = 0; i < ARRAY_SIZE(snd_emu1010_adc_pads); i++) {
2199 			err = snd_ctl_add(card,
2200 				snd_ctl_new1(&snd_emu1010_adc_pads[i], emu));
2201 			if (err < 0)
2202 				return err;
2203 		}
2204 		for (i = 0; i < ARRAY_SIZE(snd_emu1010_dac_pads); i++) {
2205 			err = snd_ctl_add(card,
2206 				snd_ctl_new1(&snd_emu1010_dac_pads[i], emu));
2207 			if (err < 0)
2208 				return err;
2209 		}
2210 		err = snd_ctl_add(card,
2211 			snd_ctl_new1(&snd_emu1010_internal_clock, emu));
2212 		if (err < 0)
2213 			return err;
2214 		err = snd_ctl_add(card,
2215 			snd_ctl_new1(&snd_emu1010_optical_out, emu));
2216 		if (err < 0)
2217 			return err;
2218 		err = snd_ctl_add(card,
2219 			snd_ctl_new1(&snd_emu1010_optical_in, emu));
2220 		if (err < 0)
2221 			return err;
2222 	}
2223 
2224 	if ( emu->card_capabilities->i2c_adc) {
2225 		int i;
2226 
2227 		err = snd_ctl_add(card, snd_ctl_new1(&snd_audigy_i2c_capture_source, emu));
2228 		if (err < 0)
2229 			return err;
2230 
2231 		for (i = 0; i < ARRAY_SIZE(snd_audigy_i2c_volume_ctls); i++) {
2232 			err = snd_ctl_add(card, snd_ctl_new1(&snd_audigy_i2c_volume_ctls[i], emu));
2233 			if (err < 0)
2234 				return err;
2235 		}
2236 	}
2237 
2238 	if (emu->card_capabilities->ac97_chip && emu->audigy) {
2239 		err = snd_ctl_add(card, snd_ctl_new1(&snd_audigy_capture_boost,
2240 						     emu));
2241 		if (err < 0)
2242 			return err;
2243 	}
2244 
2245 	return 0;
2246 }
2247