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