2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
3 * Takashi Iwai <tiwai@suse.de>
5 * Routines for control of EMU10K1 chips / mixer routines
6 * Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sound/driver.h>
31 #include <linux/time.h>
32 #include <linux/init.h>
33 #include <sound/core.h>
34 #include <sound/emu10k1.h>
36 #define AC97_ID_STAC9758 0x83847658
38 static int snd_emu10k1_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
40 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
45 static int snd_emu10k1_spdif_get(snd_kcontrol_t * kcontrol,
46 snd_ctl_elem_value_t * ucontrol)
48 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
49 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
52 spin_lock_irqsave(&emu->reg_lock, flags);
53 ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
54 ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
55 ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
56 ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
57 spin_unlock_irqrestore(&emu->reg_lock, flags);
61 static int snd_emu10k1_spdif_get_mask(snd_kcontrol_t * kcontrol,
62 snd_ctl_elem_value_t * ucontrol)
64 ucontrol->value.iec958.status[0] = 0xff;
65 ucontrol->value.iec958.status[1] = 0xff;
66 ucontrol->value.iec958.status[2] = 0xff;
67 ucontrol->value.iec958.status[3] = 0xff;
72 static int snd_audigy_spdif_output_rate_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
74 static char *texts[] = {"44100", "48000", "96000"};
76 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
78 uinfo->value.enumerated.items = 3;
79 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
80 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
81 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
85 static int snd_audigy_spdif_output_rate_get(snd_kcontrol_t * kcontrol,
86 snd_ctl_elem_value_t * ucontrol)
88 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
93 spin_lock_irqsave(&emu->reg_lock, flags);
94 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
95 switch (tmp & A_SPDIF_RATE_MASK) {
97 ucontrol->value.enumerated.item[0] = 0;
100 ucontrol->value.enumerated.item[0] = 1;
103 ucontrol->value.enumerated.item[0] = 2;
106 ucontrol->value.enumerated.item[0] = 1;
108 spin_unlock_irqrestore(&emu->reg_lock, flags);
112 static int snd_audigy_spdif_output_rate_put(snd_kcontrol_t * kcontrol,
113 snd_ctl_elem_value_t * ucontrol)
115 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
117 unsigned int reg, val, tmp;
120 switch(ucontrol->value.enumerated.item[0]) {
136 spin_lock_irqsave(&emu->reg_lock, flags);
137 reg = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
138 tmp = reg & ~A_SPDIF_RATE_MASK;
140 if ((change = (tmp != reg)))
141 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
142 spin_unlock_irqrestore(&emu->reg_lock, flags);
146 static snd_kcontrol_new_t snd_audigy_spdif_output_rate =
148 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
149 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
150 .name = "Audigy SPDIF Output Sample Rate",
152 .info = snd_audigy_spdif_output_rate_info,
153 .get = snd_audigy_spdif_output_rate_get,
154 .put = snd_audigy_spdif_output_rate_put
158 static int snd_emu10k1_spdif_put(snd_kcontrol_t * kcontrol,
159 snd_ctl_elem_value_t * ucontrol)
161 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
162 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
167 val = (ucontrol->value.iec958.status[0] << 0) |
168 (ucontrol->value.iec958.status[1] << 8) |
169 (ucontrol->value.iec958.status[2] << 16) |
170 (ucontrol->value.iec958.status[3] << 24);
171 spin_lock_irqsave(&emu->reg_lock, flags);
172 change = val != emu->spdif_bits[idx];
174 snd_emu10k1_ptr_write(emu, SPCS0 + idx, 0, val);
175 emu->spdif_bits[idx] = val;
177 spin_unlock_irqrestore(&emu->reg_lock, flags);
181 static snd_kcontrol_new_t snd_emu10k1_spdif_mask_control =
183 .access = SNDRV_CTL_ELEM_ACCESS_READ,
184 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
185 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
187 .info = snd_emu10k1_spdif_info,
188 .get = snd_emu10k1_spdif_get_mask
191 static snd_kcontrol_new_t snd_emu10k1_spdif_control =
193 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
194 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
196 .info = snd_emu10k1_spdif_info,
197 .get = snd_emu10k1_spdif_get,
198 .put = snd_emu10k1_spdif_put
202 static void update_emu10k1_fxrt(emu10k1_t *emu, int voice, unsigned char *route)
205 snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
206 snd_emu10k1_compose_audigy_fxrt1(route));
207 snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
208 snd_emu10k1_compose_audigy_fxrt2(route));
210 snd_emu10k1_ptr_write(emu, FXRT, voice,
211 snd_emu10k1_compose_send_routing(route));
215 static void update_emu10k1_send_volume(emu10k1_t *emu, int voice, unsigned char *volume)
217 snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_A, voice, volume[0]);
218 snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_B, voice, volume[1]);
219 snd_emu10k1_ptr_write(emu, PSST_FXSENDAMOUNT_C, voice, volume[2]);
220 snd_emu10k1_ptr_write(emu, DSL_FXSENDAMOUNT_D, voice, volume[3]);
222 unsigned int val = ((unsigned int)volume[4] << 24) |
223 ((unsigned int)volume[5] << 16) |
224 ((unsigned int)volume[6] << 8) |
225 (unsigned int)volume[7];
226 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice, val);
230 /* PCM stream controls */
232 static int snd_emu10k1_send_routing_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
234 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
235 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
236 uinfo->count = emu->audigy ? 3*8 : 3*4;
237 uinfo->value.integer.min = 0;
238 uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
242 static int snd_emu10k1_send_routing_get(snd_kcontrol_t * kcontrol,
243 snd_ctl_elem_value_t * ucontrol)
246 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
247 emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
249 int num_efx = emu->audigy ? 8 : 4;
250 int mask = emu->audigy ? 0x3f : 0x0f;
252 spin_lock_irqsave(&emu->reg_lock, flags);
253 for (voice = 0; voice < 3; voice++)
254 for (idx = 0; idx < num_efx; idx++)
255 ucontrol->value.integer.value[(voice * num_efx) + idx] =
256 mix->send_routing[voice][idx] & mask;
257 spin_unlock_irqrestore(&emu->reg_lock, flags);
261 static int snd_emu10k1_send_routing_put(snd_kcontrol_t * kcontrol,
262 snd_ctl_elem_value_t * ucontrol)
265 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
266 emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
267 int change = 0, voice, idx, val;
268 int num_efx = emu->audigy ? 8 : 4;
269 int mask = emu->audigy ? 0x3f : 0x0f;
271 spin_lock_irqsave(&emu->reg_lock, flags);
272 for (voice = 0; voice < 3; voice++)
273 for (idx = 0; idx < num_efx; idx++) {
274 val = ucontrol->value.integer.value[(voice * num_efx) + idx] & mask;
275 if (mix->send_routing[voice][idx] != val) {
276 mix->send_routing[voice][idx] = val;
280 if (change && mix->epcm) {
281 if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
282 update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
283 &mix->send_routing[1][0]);
284 update_emu10k1_fxrt(emu, mix->epcm->voices[1]->number,
285 &mix->send_routing[2][0]);
286 } else if (mix->epcm->voices[0]) {
287 update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
288 &mix->send_routing[0][0]);
291 spin_unlock_irqrestore(&emu->reg_lock, flags);
295 static snd_kcontrol_new_t snd_emu10k1_send_routing_control =
297 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
298 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
299 .name = "EMU10K1 PCM Send Routing",
301 .info = snd_emu10k1_send_routing_info,
302 .get = snd_emu10k1_send_routing_get,
303 .put = snd_emu10k1_send_routing_put
306 static int snd_emu10k1_send_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
308 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
309 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
310 uinfo->count = emu->audigy ? 3*8 : 3*4;
311 uinfo->value.integer.min = 0;
312 uinfo->value.integer.max = 255;
316 static int snd_emu10k1_send_volume_get(snd_kcontrol_t * kcontrol,
317 snd_ctl_elem_value_t * ucontrol)
320 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
321 emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
323 int num_efx = emu->audigy ? 8 : 4;
325 spin_lock_irqsave(&emu->reg_lock, flags);
326 for (idx = 0; idx < 3*num_efx; idx++)
327 ucontrol->value.integer.value[idx] = mix->send_volume[idx/num_efx][idx%num_efx];
328 spin_unlock_irqrestore(&emu->reg_lock, flags);
332 static int snd_emu10k1_send_volume_put(snd_kcontrol_t * kcontrol,
333 snd_ctl_elem_value_t * ucontrol)
336 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
337 emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
338 int change = 0, idx, val;
339 int num_efx = emu->audigy ? 8 : 4;
341 spin_lock_irqsave(&emu->reg_lock, flags);
342 for (idx = 0; idx < 3*num_efx; idx++) {
343 val = ucontrol->value.integer.value[idx] & 255;
344 if (mix->send_volume[idx/num_efx][idx%num_efx] != val) {
345 mix->send_volume[idx/num_efx][idx%num_efx] = val;
349 if (change && mix->epcm) {
350 if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
351 update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
352 &mix->send_volume[1][0]);
353 update_emu10k1_send_volume(emu, mix->epcm->voices[1]->number,
354 &mix->send_volume[2][0]);
355 } else if (mix->epcm->voices[0]) {
356 update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
357 &mix->send_volume[0][0]);
360 spin_unlock_irqrestore(&emu->reg_lock, flags);
364 static snd_kcontrol_new_t snd_emu10k1_send_volume_control =
366 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
367 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
368 .name = "EMU10K1 PCM Send Volume",
370 .info = snd_emu10k1_send_volume_info,
371 .get = snd_emu10k1_send_volume_get,
372 .put = snd_emu10k1_send_volume_put
375 static int snd_emu10k1_attn_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
377 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
379 uinfo->value.integer.min = 0;
380 uinfo->value.integer.max = 0xffff;
384 static int snd_emu10k1_attn_get(snd_kcontrol_t * kcontrol,
385 snd_ctl_elem_value_t * ucontrol)
387 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
388 emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
392 spin_lock_irqsave(&emu->reg_lock, flags);
393 for (idx = 0; idx < 3; idx++)
394 ucontrol->value.integer.value[idx] = mix->attn[idx];
395 spin_unlock_irqrestore(&emu->reg_lock, flags);
399 static int snd_emu10k1_attn_put(snd_kcontrol_t * kcontrol,
400 snd_ctl_elem_value_t * ucontrol)
403 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
404 emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
405 int change = 0, idx, val;
407 spin_lock_irqsave(&emu->reg_lock, flags);
408 for (idx = 0; idx < 3; idx++) {
409 val = ucontrol->value.integer.value[idx] & 0xffff;
410 if (mix->attn[idx] != val) {
411 mix->attn[idx] = val;
415 if (change && mix->epcm) {
416 if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
417 snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[1]);
418 snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[1]->number, mix->attn[2]);
419 } else if (mix->epcm->voices[0]) {
420 snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[0]);
423 spin_unlock_irqrestore(&emu->reg_lock, flags);
427 static snd_kcontrol_new_t snd_emu10k1_attn_control =
429 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
430 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
431 .name = "EMU10K1 PCM Volume",
433 .info = snd_emu10k1_attn_info,
434 .get = snd_emu10k1_attn_get,
435 .put = snd_emu10k1_attn_put
438 /* Mutichannel PCM stream controls */
440 static int snd_emu10k1_efx_send_routing_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
442 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
443 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
444 uinfo->count = emu->audigy ? 8 : 4;
445 uinfo->value.integer.min = 0;
446 uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
450 static int snd_emu10k1_efx_send_routing_get(snd_kcontrol_t * kcontrol,
451 snd_ctl_elem_value_t * ucontrol)
454 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
455 emu10k1_pcm_mixer_t *mix = &emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
457 int num_efx = emu->audigy ? 8 : 4;
458 int mask = emu->audigy ? 0x3f : 0x0f;
460 spin_lock_irqsave(&emu->reg_lock, flags);
461 for (idx = 0; idx < num_efx; idx++)
462 ucontrol->value.integer.value[idx] =
463 mix->send_routing[0][idx] & mask;
464 spin_unlock_irqrestore(&emu->reg_lock, flags);
468 static int snd_emu10k1_efx_send_routing_put(snd_kcontrol_t * kcontrol,
469 snd_ctl_elem_value_t * ucontrol)
472 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
473 int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
474 emu10k1_pcm_mixer_t *mix = &emu->efx_pcm_mixer[ch];
475 int change = 0, idx, val;
476 int num_efx = emu->audigy ? 8 : 4;
477 int mask = emu->audigy ? 0x3f : 0x0f;
479 spin_lock_irqsave(&emu->reg_lock, flags);
480 for (idx = 0; idx < num_efx; idx++) {
481 val = ucontrol->value.integer.value[idx] & mask;
482 if (mix->send_routing[0][idx] != val) {
483 mix->send_routing[0][idx] = val;
488 if (change && mix->epcm) {
489 if (mix->epcm->voices[ch]) {
490 update_emu10k1_fxrt(emu, mix->epcm->voices[ch]->number,
491 &mix->send_routing[0][0]);
494 spin_unlock_irqrestore(&emu->reg_lock, flags);
498 static snd_kcontrol_new_t snd_emu10k1_efx_send_routing_control =
500 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
501 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
502 .name = "Multichannel PCM Send Routing",
504 .info = snd_emu10k1_efx_send_routing_info,
505 .get = snd_emu10k1_efx_send_routing_get,
506 .put = snd_emu10k1_efx_send_routing_put
509 static int snd_emu10k1_efx_send_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
511 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
512 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
513 uinfo->count = emu->audigy ? 8 : 4;
514 uinfo->value.integer.min = 0;
515 uinfo->value.integer.max = 255;
519 static int snd_emu10k1_efx_send_volume_get(snd_kcontrol_t * kcontrol,
520 snd_ctl_elem_value_t * ucontrol)
523 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
524 emu10k1_pcm_mixer_t *mix = &emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
526 int num_efx = emu->audigy ? 8 : 4;
528 spin_lock_irqsave(&emu->reg_lock, flags);
529 for (idx = 0; idx < num_efx; idx++)
530 ucontrol->value.integer.value[idx] = mix->send_volume[0][idx];
531 spin_unlock_irqrestore(&emu->reg_lock, flags);
535 static int snd_emu10k1_efx_send_volume_put(snd_kcontrol_t * kcontrol,
536 snd_ctl_elem_value_t * ucontrol)
539 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
540 int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
541 emu10k1_pcm_mixer_t *mix = &emu->efx_pcm_mixer[ch];
542 int change = 0, idx, val;
543 int num_efx = emu->audigy ? 8 : 4;
545 spin_lock_irqsave(&emu->reg_lock, flags);
546 for (idx = 0; idx < num_efx; idx++) {
547 val = ucontrol->value.integer.value[idx] & 255;
548 if (mix->send_volume[0][idx] != val) {
549 mix->send_volume[0][idx] = val;
553 if (change && mix->epcm) {
554 if (mix->epcm->voices[ch]) {
555 update_emu10k1_send_volume(emu, mix->epcm->voices[ch]->number,
556 &mix->send_volume[0][0]);
559 spin_unlock_irqrestore(&emu->reg_lock, flags);
564 static snd_kcontrol_new_t snd_emu10k1_efx_send_volume_control =
566 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
567 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
568 .name = "Multichannel PCM Send Volume",
570 .info = snd_emu10k1_efx_send_volume_info,
571 .get = snd_emu10k1_efx_send_volume_get,
572 .put = snd_emu10k1_efx_send_volume_put
575 static int snd_emu10k1_efx_attn_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
577 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
579 uinfo->value.integer.min = 0;
580 uinfo->value.integer.max = 0xffff;
584 static int snd_emu10k1_efx_attn_get(snd_kcontrol_t * kcontrol,
585 snd_ctl_elem_value_t * ucontrol)
587 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
588 emu10k1_pcm_mixer_t *mix = &emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
591 spin_lock_irqsave(&emu->reg_lock, flags);
592 ucontrol->value.integer.value[0] = mix->attn[0];
593 spin_unlock_irqrestore(&emu->reg_lock, flags);
597 static int snd_emu10k1_efx_attn_put(snd_kcontrol_t * kcontrol,
598 snd_ctl_elem_value_t * ucontrol)
601 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
602 int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
603 emu10k1_pcm_mixer_t *mix = &emu->efx_pcm_mixer[ch];
606 spin_lock_irqsave(&emu->reg_lock, flags);
607 val = ucontrol->value.integer.value[0] & 0xffff;
608 if (mix->attn[0] != val) {
612 if (change && mix->epcm) {
613 if (mix->epcm->voices[ch]) {
614 snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[ch]->number, mix->attn[0]);
617 spin_unlock_irqrestore(&emu->reg_lock, flags);
621 static snd_kcontrol_new_t snd_emu10k1_efx_attn_control =
623 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
624 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
625 .name = "Multichannel PCM Volume",
627 .info = snd_emu10k1_efx_attn_info,
628 .get = snd_emu10k1_efx_attn_get,
629 .put = snd_emu10k1_efx_attn_put
632 static int snd_emu10k1_shared_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
634 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
636 uinfo->value.integer.min = 0;
637 uinfo->value.integer.max = 1;
641 static int snd_emu10k1_shared_spdif_get(snd_kcontrol_t * kcontrol,
642 snd_ctl_elem_value_t * ucontrol)
644 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
647 ucontrol->value.integer.value[0] = inl(emu->port + A_IOCFG) & A_IOCFG_GPOUT0 ? 1 : 0;
649 ucontrol->value.integer.value[0] = inl(emu->port + HCFG) & HCFG_GPOUT0 ? 1 : 0;
653 static int snd_emu10k1_shared_spdif_put(snd_kcontrol_t * kcontrol,
654 snd_ctl_elem_value_t * ucontrol)
657 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
658 unsigned int reg, val;
661 spin_lock_irqsave(&emu->reg_lock, flags);
663 reg = inl(emu->port + A_IOCFG);
664 val = ucontrol->value.integer.value[0] ? A_IOCFG_GPOUT0 : 0;
665 change = (reg & A_IOCFG_GPOUT0) != val;
667 reg &= ~A_IOCFG_GPOUT0;
669 outl(reg | val, emu->port + A_IOCFG);
672 reg = inl(emu->port + HCFG);
673 val = ucontrol->value.integer.value[0] ? HCFG_GPOUT0 : 0;
674 change |= (reg & HCFG_GPOUT0) != val;
678 outl(reg | val, emu->port + HCFG);
680 spin_unlock_irqrestore(&emu->reg_lock, flags);
684 static snd_kcontrol_new_t snd_emu10k1_shared_spdif __devinitdata =
686 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
687 .name = "SB Live Analog/Digital Output Jack",
688 .info = snd_emu10k1_shared_spdif_info,
689 .get = snd_emu10k1_shared_spdif_get,
690 .put = snd_emu10k1_shared_spdif_put
693 static snd_kcontrol_new_t snd_audigy_shared_spdif __devinitdata =
695 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
696 .name = "Audigy Analog/Digital Output Jack",
697 .info = snd_emu10k1_shared_spdif_info,
698 .get = snd_emu10k1_shared_spdif_get,
699 .put = snd_emu10k1_shared_spdif_put
704 static void snd_emu10k1_mixer_free_ac97(ac97_t *ac97)
706 emu10k1_t *emu = ac97->private_data;
712 static int remove_ctl(snd_card_t *card, const char *name)
714 snd_ctl_elem_id_t id;
715 memset(&id, 0, sizeof(id));
716 strcpy(id.name, name);
717 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
718 return snd_ctl_remove_id(card, &id);
721 static snd_kcontrol_t *ctl_find(snd_card_t *card, const char *name)
723 snd_ctl_elem_id_t sid;
724 memset(&sid, 0, sizeof(sid));
725 strcpy(sid.name, name);
726 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
727 return snd_ctl_find_id(card, &sid);
730 static int rename_ctl(snd_card_t *card, const char *src, const char *dst)
732 snd_kcontrol_t *kctl = ctl_find(card, src);
734 strcpy(kctl->id.name, dst);
740 int __devinit snd_emu10k1_mixer(emu10k1_t *emu,
741 int pcm_device, int multi_device)
744 snd_kcontrol_t *kctl;
745 snd_card_t *card = emu->card;
747 static char *emu10k1_remove_ctls[] = {
748 /* no AC97 mono, surround, center/lfe */
749 "Master Mono Playback Switch",
750 "Master Mono Playback Volume",
751 "PCM Out Path & Mute",
752 "Mono Output Select",
753 "Surround Playback Switch",
754 "Surround Playback Volume",
755 "Center Playback Switch",
756 "Center Playback Volume",
757 "LFE Playback Switch",
758 "LFE Playback Volume",
761 static char *emu10k1_rename_ctls[] = {
762 "Surround Digital Playback Volume", "Surround Playback Volume",
763 "Center Digital Playback Volume", "Center Playback Volume",
764 "LFE Digital Playback Volume", "LFE Playback Volume",
767 static char *audigy_remove_ctls[] = {
768 /* Master/PCM controls on ac97 of Audigy has no effect */
769 "PCM Playback Switch",
770 "PCM Playback Volume",
771 "Master Mono Playback Switch",
772 "Master Mono Playback Volume",
773 "Master Playback Switch",
774 "Master Playback Volume",
775 "PCM Out Path & Mute",
776 "Mono Output Select",
777 /* remove unused AC97 capture controls */
782 "Video Playback Switch",
783 "Video Playback Volume",
784 "Mic Playback Switch",
785 "Mic Playback Volume",
788 static char *audigy_rename_ctls[] = {
789 /* use conventional names */
790 "Wave Playback Volume", "PCM Playback Volume",
791 /* "Wave Capture Volume", "PCM Capture Volume", */
792 "Wave Master Playback Volume", "Master Playback Volume",
793 "AMic Playback Volume", "Mic Playback Volume",
797 if (emu->card_capabilities->ac97_chip) {
799 ac97_template_t ac97;
800 static ac97_bus_ops_t ops = {
801 .write = snd_emu10k1_ac97_write,
802 .read = snd_emu10k1_ac97_read,
805 if ((err = snd_ac97_bus(emu->card, 0, &ops, NULL, &pbus)) < 0)
807 pbus->no_vra = 1; /* we don't need VRA */
809 memset(&ac97, 0, sizeof(ac97));
810 ac97.private_data = emu;
811 ac97.private_free = snd_emu10k1_mixer_free_ac97;
812 ac97.scaps = AC97_SCAP_NO_SPDIF;
813 if ((err = snd_ac97_mixer(pbus, &ac97, &emu->ac97)) < 0)
816 /* set master volume to 0 dB */
817 snd_ac97_write(emu->ac97, AC97_MASTER, 0x0000);
818 /* set capture source to mic */
819 snd_ac97_write(emu->ac97, AC97_REC_SEL, 0x0000);
820 c = audigy_remove_ctls;
823 * Credits for cards based on STAC9758:
824 * James Courtier-Dutton <James@superbug.demon.co.uk>
825 * Voluspa <voluspa@comhem.se>
827 if (emu->ac97->id == AC97_ID_STAC9758) {
829 snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE|AC97SLOT_REAR_LEFT|AC97SLOT_REAR_RIGHT);
831 /* remove unused AC97 controls */
832 snd_ac97_write(emu->ac97, AC97_SURROUND_MASTER, 0x0202);
833 snd_ac97_write(emu->ac97, AC97_CENTER_LFE_MASTER, 0x0202);
834 c = emu10k1_remove_ctls;
837 remove_ctl(card, *c);
839 if (emu->card_capabilities->ecard)
840 strcpy(emu->card->mixername, "EMU APS");
841 else if (emu->audigy)
842 strcpy(emu->card->mixername, "SB Audigy");
844 strcpy(emu->card->mixername, "Emu10k1");
848 c = audigy_rename_ctls;
850 c = emu10k1_rename_ctls;
852 rename_ctl(card, c[0], c[1]);
854 if ((kctl = emu->ctl_send_routing = snd_ctl_new1(&snd_emu10k1_send_routing_control, emu)) == NULL)
856 kctl->id.device = pcm_device;
857 if ((err = snd_ctl_add(card, kctl)))
859 if ((kctl = emu->ctl_send_volume = snd_ctl_new1(&snd_emu10k1_send_volume_control, emu)) == NULL)
861 kctl->id.device = pcm_device;
862 if ((err = snd_ctl_add(card, kctl)))
864 if ((kctl = emu->ctl_attn = snd_ctl_new1(&snd_emu10k1_attn_control, emu)) == NULL)
866 kctl->id.device = pcm_device;
867 if ((err = snd_ctl_add(card, kctl)))
870 if ((kctl = emu->ctl_efx_send_routing = snd_ctl_new1(&snd_emu10k1_efx_send_routing_control, emu)) == NULL)
872 kctl->id.device = multi_device;
873 if ((err = snd_ctl_add(card, kctl)))
876 if ((kctl = emu->ctl_efx_send_volume = snd_ctl_new1(&snd_emu10k1_efx_send_volume_control, emu)) == NULL)
878 kctl->id.device = multi_device;
879 if ((err = snd_ctl_add(card, kctl)))
882 if ((kctl = emu->ctl_efx_attn = snd_ctl_new1(&snd_emu10k1_efx_attn_control, emu)) == NULL)
884 kctl->id.device = multi_device;
885 if ((err = snd_ctl_add(card, kctl)))
888 /* initialize the routing and volume table for each pcm playback stream */
889 for (pcm = 0; pcm < 32; pcm++) {
890 emu10k1_pcm_mixer_t *mix;
893 mix = &emu->pcm_mixer[pcm];
896 for (v = 0; v < 4; v++)
897 mix->send_routing[0][v] =
898 mix->send_routing[1][v] =
899 mix->send_routing[2][v] = v;
901 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
902 mix->send_volume[0][0] = mix->send_volume[0][1] =
903 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
905 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
908 /* initialize the routing and volume table for the multichannel playback stream */
909 for (pcm = 0; pcm < NUM_EFX_PLAYBACK; pcm++) {
910 emu10k1_pcm_mixer_t *mix;
913 mix = &emu->efx_pcm_mixer[pcm];
916 mix->send_routing[0][0] = pcm;
917 mix->send_routing[0][1] = (pcm == 0) ? 1 : 0;
918 for (v = 0; v < 2; v++)
919 mix->send_routing[0][2+v] = 13+v;
921 for (v = 0; v < 4; v++)
922 mix->send_routing[0][4+v] = 60+v;
924 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
925 mix->send_volume[0][0] = 255;
927 mix->attn[0] = 0xffff;
930 if (! emu->card_capabilities->ecard) { /* FIXME: APS has these controls? */
931 /* sb live! and audigy */
932 if ((kctl = snd_ctl_new1(&snd_emu10k1_spdif_mask_control, emu)) == NULL)
935 kctl->id.device = emu->pcm_efx->device;
936 if ((err = snd_ctl_add(card, kctl)))
938 if ((kctl = snd_ctl_new1(&snd_emu10k1_spdif_control, emu)) == NULL)
941 kctl->id.device = emu->pcm_efx->device;
942 if ((err = snd_ctl_add(card, kctl)))
947 if ((kctl = snd_ctl_new1(&snd_audigy_shared_spdif, emu)) == NULL)
949 if ((err = snd_ctl_add(card, kctl)))
952 if ((kctl = snd_ctl_new1(&snd_audigy_spdif_output_rate, emu)) == NULL)
954 if ((err = snd_ctl_add(card, kctl)))
957 } else if (! emu->card_capabilities->ecard) {
959 if ((kctl = snd_ctl_new1(&snd_emu10k1_shared_spdif, emu)) == NULL)
961 if ((err = snd_ctl_add(card, kctl)))
964 if (emu->card_capabilities->ca0151_chip) { /* P16V */
965 if ((err = snd_p16v_mixer(emu)))