1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5 * Routines for control of EMU10K1 chips / PCM routines
6 * Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
15 #include <linux/pci.h>
16 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/time.h>
19 #include <linux/init.h>
20 #include <sound/core.h>
21 #include <sound/emu10k1.h>
23 static void snd_emu10k1_pcm_interrupt(struct snd_emu10k1 *emu,
24 struct snd_emu10k1_voice *voice)
26 struct snd_emu10k1_pcm *epcm;
31 if (epcm->substream == NULL)
34 dev_dbg(emu->card->dev,
35 "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
36 epcm->substream->runtime->hw->pointer(emu, epcm->substream),
37 snd_pcm_lib_period_bytes(epcm->substream),
38 snd_pcm_lib_buffer_bytes(epcm->substream));
40 snd_pcm_period_elapsed(epcm->substream);
43 static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
47 if (status & IPR_ADCBUFHALFFULL) {
48 if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
52 snd_pcm_period_elapsed(emu->pcm_capture_substream);
55 static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
59 if (status & IPR_MICBUFHALFFULL) {
60 if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
64 snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
67 static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
71 if (status & IPR_EFXBUFHALFFULL) {
72 if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
76 snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
79 static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream)
81 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
82 struct snd_pcm_runtime *runtime = substream->runtime;
83 struct snd_emu10k1_pcm *epcm = runtime->private_data;
88 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
89 ptr += runtime->buffer_size;
90 ptr -= epcm->ccca_start_addr;
91 ptr %= runtime->buffer_size;
96 static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
100 if (epcm->voices[1] != NULL && voices < 2) {
101 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
102 epcm->voices[1] = NULL;
104 for (i = 0; i < voices; i++) {
105 if (epcm->voices[i] == NULL)
109 return 0; /* already allocated */
111 for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
112 if (epcm->voices[i]) {
113 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
114 epcm->voices[i] = NULL;
117 err = snd_emu10k1_voice_alloc(epcm->emu,
118 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
124 epcm->voices[0]->epcm = epcm;
126 for (i = 1; i < voices; i++) {
127 epcm->voices[i] = &epcm->emu->voices[(epcm->voices[0]->number + i) % NUM_G];
128 epcm->voices[i]->epcm = epcm;
131 if (epcm->extra == NULL) {
132 err = snd_emu10k1_voice_alloc(epcm->emu,
133 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
138 dev_dbg(emu->card->dev, "pcm_channel_alloc: "
139 "failed extra: voices=%d, frame=%d\n",
142 for (i = 0; i < voices; i++) {
143 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
144 epcm->voices[i] = NULL;
148 epcm->extra->epcm = epcm;
149 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
154 static const unsigned int capture_period_sizes[31] = {
156 384*2, 448*2, 512*2, 640*2,
157 384*4, 448*4, 512*4, 640*4,
158 384*8, 448*8, 512*8, 640*8,
159 384*16, 448*16, 512*16, 640*16,
160 384*32, 448*32, 512*32, 640*32,
161 384*64, 448*64, 512*64, 640*64,
162 384*128,448*128,512*128
165 static const struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
167 .list = capture_period_sizes,
171 static const unsigned int capture_rates[8] = {
172 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
175 static const struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
177 .list = capture_rates,
181 static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
184 case 8000: return ADCCR_SAMPLERATE_8;
185 case 11025: return ADCCR_SAMPLERATE_11;
186 case 16000: return ADCCR_SAMPLERATE_16;
187 case 22050: return ADCCR_SAMPLERATE_22;
188 case 24000: return ADCCR_SAMPLERATE_24;
189 case 32000: return ADCCR_SAMPLERATE_32;
190 case 44100: return ADCCR_SAMPLERATE_44;
191 case 48000: return ADCCR_SAMPLERATE_48;
194 return ADCCR_SAMPLERATE_8;
198 static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
201 case 8000: return A_ADCCR_SAMPLERATE_8;
202 case 11025: return A_ADCCR_SAMPLERATE_11;
203 case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */
204 case 16000: return ADCCR_SAMPLERATE_16;
205 case 22050: return ADCCR_SAMPLERATE_22;
206 case 24000: return ADCCR_SAMPLERATE_24;
207 case 32000: return ADCCR_SAMPLERATE_32;
208 case 44100: return ADCCR_SAMPLERATE_44;
209 case 48000: return ADCCR_SAMPLERATE_48;
212 return A_ADCCR_SAMPLERATE_8;
216 static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
218 unsigned int pitch_target;
220 pitch_target = (rate << 8) / 375;
221 pitch_target = (pitch_target >> 1) + (pitch_target & 1);
225 #define PITCH_48000 0x00004000
226 #define PITCH_96000 0x00008000
227 #define PITCH_85000 0x00007155
228 #define PITCH_80726 0x00006ba2
229 #define PITCH_67882 0x00005a82
230 #define PITCH_57081 0x00004c1c
232 static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
234 if (pitch_target == PITCH_48000)
235 return CCCA_INTERPROM_0;
236 else if (pitch_target < PITCH_48000)
237 return CCCA_INTERPROM_1;
238 else if (pitch_target >= PITCH_96000)
239 return CCCA_INTERPROM_0;
240 else if (pitch_target >= PITCH_85000)
241 return CCCA_INTERPROM_6;
242 else if (pitch_target >= PITCH_80726)
243 return CCCA_INTERPROM_5;
244 else if (pitch_target >= PITCH_67882)
245 return CCCA_INTERPROM_4;
246 else if (pitch_target >= PITCH_57081)
247 return CCCA_INTERPROM_3;
249 return CCCA_INTERPROM_2;
253 * calculate cache invalidate size
255 * stereo: channel is stereo
256 * w_16: using 16bit samples
258 * returns: cache invalidate size in samples
260 static inline int emu10k1_ccis(int stereo, int w_16)
263 return stereo ? 24 : 26;
265 return stereo ? 24*2 : 26*2;
269 static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
270 int master, int extra,
271 struct snd_emu10k1_voice *evoice,
272 unsigned int start_addr,
273 unsigned int end_addr,
274 struct snd_emu10k1_pcm_mixer *mix)
276 struct snd_pcm_substream *substream = evoice->epcm->substream;
277 struct snd_pcm_runtime *runtime = substream->runtime;
278 unsigned int silent_page, tmp;
279 int voice, stereo, w_16;
280 unsigned char send_amount[8];
281 unsigned char send_routing[8];
283 unsigned int pitch_target;
286 voice = evoice->number;
287 stereo = runtime->channels == 2;
288 w_16 = snd_pcm_format_width(runtime->format) == 16;
290 if (!extra && stereo) {
299 spin_lock_irqsave(&emu->reg_lock, flags);
301 /* volume parameters */
303 memset(send_routing, 0, sizeof(send_routing));
308 memset(send_amount, 0, sizeof(send_amount));
310 /* mono, left, right (master voice = left) */
311 tmp = stereo ? (master ? 1 : 2) : 0;
312 memcpy(send_routing, &mix->send_routing[tmp][0], 8);
313 memcpy(send_amount, &mix->send_volume[tmp][0], 8);
316 ccis = emu10k1_ccis(stereo, w_16);
319 evoice->epcm->ccca_start_addr = start_addr + ccis;
322 end_addr += ccis + emu->delay_pcm_irq;
324 if (stereo && !extra) {
325 snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
326 snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
328 snd_emu10k1_ptr_write(emu, CPF, voice, 0);
334 snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
335 snd_emu10k1_compose_audigy_fxrt1(send_routing));
336 snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
337 snd_emu10k1_compose_audigy_fxrt2(send_routing));
338 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
339 ((unsigned int)send_amount[4] << 24) |
340 ((unsigned int)send_amount[5] << 16) |
341 ((unsigned int)send_amount[6] << 8) |
342 (unsigned int)send_amount[7]);
344 snd_emu10k1_ptr_write(emu, FXRT, voice,
345 snd_emu10k1_compose_send_routing(send_routing));
347 /* Assumption that PT is already 0 so no harm overwriting */
348 snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
349 snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
350 snd_emu10k1_ptr_write(emu, PSST, voice,
351 (start_addr + (extra ? emu->delay_pcm_irq : 0)) |
352 (send_amount[2] << 24));
353 if (emu->card_capabilities->emu_model)
354 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
356 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
358 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
359 emu10k1_select_interprom(pitch_target) |
360 (w_16 ? 0 : CCCA_8BITSELECT));
362 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
363 emu10k1_select_interprom(pitch_target) |
364 (w_16 ? 0 : CCCA_8BITSELECT));
365 /* Clear filter delay memory */
366 snd_emu10k1_ptr_write(emu, Z1, voice, 0);
367 snd_emu10k1_ptr_write(emu, Z2, voice, 0);
368 /* invalidate maps */
369 silent_page = ((unsigned int)emu->silent_page.addr << emu->address_mode) | (emu->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
370 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
371 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
372 /* modulation envelope */
373 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
374 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
375 snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
376 snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
377 snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
378 snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
379 snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
380 snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
381 snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
382 snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
383 /* volume envelope */
384 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
385 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
386 /* filter envelope */
387 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
389 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
391 spin_unlock_irqrestore(&emu->reg_lock, flags);
394 static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
395 struct snd_pcm_hw_params *hw_params)
397 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
398 struct snd_pcm_runtime *runtime = substream->runtime;
399 struct snd_emu10k1_pcm *epcm = runtime->private_data;
403 err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params));
407 alloc_size = params_buffer_bytes(hw_params);
408 if (emu->iommu_workaround)
409 alloc_size += EMUPAGESIZE;
410 err = snd_pcm_lib_malloc_pages(substream, alloc_size);
413 if (emu->iommu_workaround && runtime->dma_bytes >= EMUPAGESIZE)
414 runtime->dma_bytes -= EMUPAGESIZE;
415 if (err > 0) { /* change */
417 if (epcm->memblk != NULL)
418 snd_emu10k1_free_pages(emu, epcm->memblk);
419 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
420 epcm->start_addr = 0;
423 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
426 epcm->start_addr = mapped << PAGE_SHIFT;
431 static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
433 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
434 struct snd_pcm_runtime *runtime = substream->runtime;
435 struct snd_emu10k1_pcm *epcm;
437 if (runtime->private_data == NULL)
439 epcm = runtime->private_data;
441 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
444 if (epcm->voices[1]) {
445 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
446 epcm->voices[1] = NULL;
448 if (epcm->voices[0]) {
449 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
450 epcm->voices[0] = NULL;
453 snd_emu10k1_free_pages(emu, epcm->memblk);
455 epcm->start_addr = 0;
457 snd_pcm_lib_free_pages(substream);
461 static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
463 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
464 struct snd_pcm_runtime *runtime = substream->runtime;
465 struct snd_emu10k1_pcm *epcm;
468 if (runtime->private_data == NULL)
470 epcm = runtime->private_data;
472 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
475 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
476 if (epcm->voices[i]) {
477 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
478 epcm->voices[i] = NULL;
482 snd_emu10k1_free_pages(emu, epcm->memblk);
484 epcm->start_addr = 0;
486 snd_pcm_lib_free_pages(substream);
490 static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
492 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
493 struct snd_pcm_runtime *runtime = substream->runtime;
494 struct snd_emu10k1_pcm *epcm = runtime->private_data;
495 unsigned int start_addr, end_addr;
497 start_addr = epcm->start_addr;
498 end_addr = snd_pcm_lib_period_bytes(substream);
499 if (runtime->channels == 2) {
503 end_addr += start_addr;
504 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
505 start_addr, end_addr, NULL);
506 start_addr = epcm->start_addr;
507 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
508 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
509 start_addr, end_addr,
510 &emu->pcm_mixer[substream->number]);
512 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
513 start_addr, end_addr,
514 &emu->pcm_mixer[substream->number]);
518 static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
520 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
521 struct snd_pcm_runtime *runtime = substream->runtime;
522 struct snd_emu10k1_pcm *epcm = runtime->private_data;
523 unsigned int start_addr, end_addr;
524 unsigned int channel_size;
527 start_addr = epcm->start_addr;
528 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
531 * the kX driver leaves some space between voices
533 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
535 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
536 start_addr, start_addr + (channel_size / 2), NULL);
538 /* only difference with the master voice is we use it for the pointer */
539 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
540 start_addr, start_addr + channel_size,
541 &emu->efx_pcm_mixer[0]);
543 start_addr += channel_size;
544 for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
545 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
546 start_addr, start_addr + channel_size,
547 &emu->efx_pcm_mixer[i]);
548 start_addr += channel_size;
554 static const struct snd_pcm_hardware snd_emu10k1_efx_playback =
556 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
557 SNDRV_PCM_INFO_BLOCK_TRANSFER |
558 SNDRV_PCM_INFO_RESUME |
559 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
560 .formats = SNDRV_PCM_FMTBIT_S16_LE,
561 .rates = SNDRV_PCM_RATE_48000,
564 .channels_min = NUM_EFX_PLAYBACK,
565 .channels_max = NUM_EFX_PLAYBACK,
566 .buffer_bytes_max = (64*1024),
567 .period_bytes_min = 64,
568 .period_bytes_max = (64*1024),
574 static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
576 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
577 struct snd_pcm_runtime *runtime = substream->runtime;
578 struct snd_emu10k1_pcm *epcm = runtime->private_data;
581 /* zeroing the buffer size will stop capture */
582 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
583 switch (epcm->type) {
584 case CAPTURE_AC97ADC:
585 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
589 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
590 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
592 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
597 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
598 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
599 epcm->capture_bs_val = 0;
600 for (idx = 0; idx < 31; idx++) {
601 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
602 epcm->capture_bs_val = idx + 1;
606 if (epcm->capture_bs_val == 0) {
608 epcm->capture_bs_val++;
610 if (epcm->type == CAPTURE_AC97ADC) {
611 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
612 if (runtime->channels > 1)
613 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
614 epcm->capture_cr_val |= emu->audigy ?
615 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
616 snd_emu10k1_capture_rate_reg(runtime->rate);
621 static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
623 struct snd_pcm_runtime *runtime;
624 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
628 runtime = evoice->epcm->substream->runtime;
629 voice = evoice->number;
630 stereo = (!extra && runtime->channels == 2);
631 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
632 ccis = emu10k1_ccis(stereo, sample == 0);
633 /* set cs to 2 * number of cache registers beside the invalidated */
634 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
635 if (cs > 16) cs = 16;
636 for (i = 0; i < cs; i++) {
637 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
639 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
643 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
644 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
646 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
647 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
650 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
652 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
656 static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
657 int master, int extra,
658 struct snd_emu10k1_pcm_mixer *mix)
660 struct snd_pcm_substream *substream;
661 struct snd_pcm_runtime *runtime;
662 unsigned int attn, vattn;
663 unsigned int voice, tmp;
665 if (evoice == NULL) /* skip second voice for mono */
667 substream = evoice->epcm->substream;
668 runtime = substream->runtime;
669 voice = evoice->number;
671 attn = extra ? 0 : 0x00ff;
672 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
673 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
674 snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
675 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
676 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
677 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
678 snd_emu10k1_voice_clear_loop_stop(emu, voice);
681 static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
683 struct snd_pcm_substream *substream;
684 struct snd_pcm_runtime *runtime;
685 unsigned int voice, pitch, pitch_target;
687 if (evoice == NULL) /* skip second voice for mono */
689 substream = evoice->epcm->substream;
690 runtime = substream->runtime;
691 voice = evoice->number;
693 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
694 if (emu->card_capabilities->emu_model)
695 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
697 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
698 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
699 if (master || evoice->epcm->type == PLAYBACK_EFX)
700 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
701 snd_emu10k1_ptr_write(emu, IP, voice, pitch);
703 snd_emu10k1_voice_intr_enable(emu, voice);
706 static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
712 voice = evoice->number;
713 snd_emu10k1_voice_intr_disable(emu, voice);
714 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
715 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
716 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
717 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
718 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
719 snd_emu10k1_ptr_write(emu, IP, voice, 0);
722 static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu,
723 struct snd_emu10k1_pcm *epcm,
724 struct snd_pcm_substream *substream,
725 struct snd_pcm_runtime *runtime)
727 unsigned int ptr, period_pos;
729 /* try to sychronize the current position for the interrupt
731 period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt;
732 period_pos %= runtime->period_size;
733 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number);
735 ptr |= epcm->ccca_start_addr + period_pos;
736 snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr);
739 static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
742 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
743 struct snd_pcm_runtime *runtime = substream->runtime;
744 struct snd_emu10k1_pcm *epcm = runtime->private_data;
745 struct snd_emu10k1_pcm_mixer *mix;
749 dev_dbg(emu->card->dev,
750 "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
751 (int)emu, cmd, substream->ops->pointer(substream))
753 spin_lock(&emu->reg_lock);
755 case SNDRV_PCM_TRIGGER_START:
756 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
757 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
759 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
760 case SNDRV_PCM_TRIGGER_RESUME:
761 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
762 snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime);
763 mix = &emu->pcm_mixer[substream->number];
764 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
765 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
766 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
767 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
768 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
769 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
772 case SNDRV_PCM_TRIGGER_STOP:
773 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
774 case SNDRV_PCM_TRIGGER_SUSPEND:
776 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
777 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
778 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
784 spin_unlock(&emu->reg_lock);
788 static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
791 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
792 struct snd_pcm_runtime *runtime = substream->runtime;
793 struct snd_emu10k1_pcm *epcm = runtime->private_data;
796 spin_lock(&emu->reg_lock);
798 case SNDRV_PCM_TRIGGER_START:
799 case SNDRV_PCM_TRIGGER_RESUME:
800 /* hmm this should cause full and half full interrupt to be raised? */
801 outl(epcm->capture_ipr, emu->port + IPR);
802 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
804 dev_dbg(emu->card->dev, "adccr = 0x%x, adcbs = 0x%x\n",
805 epcm->adccr, epcm->adcbs);
807 switch (epcm->type) {
808 case CAPTURE_AC97ADC:
809 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
813 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
814 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
815 dev_dbg(emu->card->dev,
816 "cr_val=0x%x, cr_val2=0x%x\n",
817 epcm->capture_cr_val,
818 epcm->capture_cr_val2);
820 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
825 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
829 case SNDRV_PCM_TRIGGER_STOP:
830 case SNDRV_PCM_TRIGGER_SUSPEND:
832 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
833 outl(epcm->capture_ipr, emu->port + IPR);
834 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
835 switch (epcm->type) {
836 case CAPTURE_AC97ADC:
837 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
841 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
842 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
844 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
853 spin_unlock(&emu->reg_lock);
857 static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
859 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
860 struct snd_pcm_runtime *runtime = substream->runtime;
861 struct snd_emu10k1_pcm *epcm = runtime->private_data;
866 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
867 #if 0 /* Perex's code */
868 ptr += runtime->buffer_size;
869 ptr -= epcm->ccca_start_addr;
870 ptr %= runtime->buffer_size;
871 #else /* EMU10K1 Open Source code from Creative */
872 if (ptr < epcm->ccca_start_addr)
873 ptr += runtime->buffer_size - epcm->ccca_start_addr;
875 ptr -= epcm->ccca_start_addr;
876 if (ptr >= runtime->buffer_size)
877 ptr -= runtime->buffer_size;
881 dev_dbg(emu->card->dev,
882 "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
883 (long)ptr, (long)runtime->buffer_size,
884 (long)runtime->period_size);
890 static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
893 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
894 struct snd_pcm_runtime *runtime = substream->runtime;
895 struct snd_emu10k1_pcm *epcm = runtime->private_data;
899 spin_lock(&emu->reg_lock);
901 case SNDRV_PCM_TRIGGER_START:
903 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
904 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
906 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
908 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
909 case SNDRV_PCM_TRIGGER_RESUME:
910 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
911 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
912 &emu->efx_pcm_mixer[0]);
913 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
914 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
915 &emu->efx_pcm_mixer[i]);
916 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
917 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
918 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
919 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
922 case SNDRV_PCM_TRIGGER_SUSPEND:
923 case SNDRV_PCM_TRIGGER_STOP:
924 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
926 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
927 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
929 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
935 spin_unlock(&emu->reg_lock);
940 static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
942 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
943 struct snd_pcm_runtime *runtime = substream->runtime;
944 struct snd_emu10k1_pcm *epcm = runtime->private_data;
949 if (epcm->first_ptr) {
950 udelay(50); /* hack, it takes awhile until capture is started */
953 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
954 return bytes_to_frames(runtime, ptr);
958 * Playback support device description
961 static const struct snd_pcm_hardware snd_emu10k1_playback =
963 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
964 SNDRV_PCM_INFO_BLOCK_TRANSFER |
965 SNDRV_PCM_INFO_RESUME |
966 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
967 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
968 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
973 .buffer_bytes_max = (128*1024),
974 .period_bytes_min = 64,
975 .period_bytes_max = (128*1024),
982 * Capture support device description
985 static const struct snd_pcm_hardware snd_emu10k1_capture =
987 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
988 SNDRV_PCM_INFO_BLOCK_TRANSFER |
989 SNDRV_PCM_INFO_RESUME |
990 SNDRV_PCM_INFO_MMAP_VALID),
991 .formats = SNDRV_PCM_FMTBIT_S16_LE,
992 .rates = SNDRV_PCM_RATE_8000_48000,
997 .buffer_bytes_max = (64*1024),
998 .period_bytes_min = 384,
999 .period_bytes_max = (64*1024),
1005 static const struct snd_pcm_hardware snd_emu10k1_capture_efx =
1007 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1008 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1009 SNDRV_PCM_INFO_RESUME |
1010 SNDRV_PCM_INFO_MMAP_VALID),
1011 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1012 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1013 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1014 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
1019 .buffer_bytes_max = (64*1024),
1020 .period_bytes_min = 384,
1021 .period_bytes_max = (64*1024),
1031 static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
1033 struct snd_ctl_elem_id id;
1038 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1040 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1041 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1042 SNDRV_CTL_EVENT_MASK_INFO,
1043 snd_ctl_build_ioff(&id, kctl, idx));
1046 static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1048 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1049 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1050 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1053 static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1055 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1056 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1057 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1060 static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
1062 kfree(runtime->private_data);
1065 static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
1067 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1068 struct snd_emu10k1_pcm_mixer *mix;
1071 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1072 mix = &emu->efx_pcm_mixer[i];
1074 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1079 static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
1081 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1082 struct snd_emu10k1_pcm *epcm;
1083 struct snd_emu10k1_pcm_mixer *mix;
1084 struct snd_pcm_runtime *runtime = substream->runtime;
1087 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1091 epcm->type = PLAYBACK_EFX;
1092 epcm->substream = substream;
1094 emu->pcm_playback_efx_substream = substream;
1096 runtime->private_data = epcm;
1097 runtime->private_free = snd_emu10k1_pcm_free_substream;
1098 runtime->hw = snd_emu10k1_efx_playback;
1100 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1101 mix = &emu->efx_pcm_mixer[i];
1102 mix->send_routing[0][0] = i;
1103 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1104 mix->send_volume[0][0] = 255;
1105 mix->attn[0] = 0xffff;
1107 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1112 static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
1114 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1115 struct snd_emu10k1_pcm *epcm;
1116 struct snd_emu10k1_pcm_mixer *mix;
1117 struct snd_pcm_runtime *runtime = substream->runtime;
1118 int i, err, sample_rate;
1120 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1124 epcm->type = PLAYBACK_EMUVOICE;
1125 epcm->substream = substream;
1126 runtime->private_data = epcm;
1127 runtime->private_free = snd_emu10k1_pcm_free_substream;
1128 runtime->hw = snd_emu10k1_playback;
1129 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1134 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
1139 if (emu->card_capabilities->emu_model && emu->emu1010.internal_clock == 0)
1140 sample_rate = 44100;
1142 sample_rate = 48000;
1143 err = snd_pcm_hw_rule_noresample(runtime, sample_rate);
1148 mix = &emu->pcm_mixer[substream->number];
1149 for (i = 0; i < 4; i++)
1150 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1151 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1152 mix->send_volume[0][0] = mix->send_volume[0][1] =
1153 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1154 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1156 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1160 static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1162 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1163 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1166 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1170 static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1172 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1173 struct snd_pcm_runtime *runtime = substream->runtime;
1174 struct snd_emu10k1_pcm *epcm;
1176 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1180 epcm->type = CAPTURE_AC97ADC;
1181 epcm->substream = substream;
1182 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1183 epcm->capture_inte = INTE_ADCBUFENABLE;
1184 epcm->capture_ba_reg = ADCBA;
1185 epcm->capture_bs_reg = ADCBS;
1186 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1187 runtime->private_data = epcm;
1188 runtime->private_free = snd_emu10k1_pcm_free_substream;
1189 runtime->hw = snd_emu10k1_capture;
1190 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1191 emu->pcm_capture_substream = substream;
1192 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1193 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1197 static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1199 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1201 emu->capture_interrupt = NULL;
1202 emu->pcm_capture_substream = NULL;
1206 static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1208 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1209 struct snd_emu10k1_pcm *epcm;
1210 struct snd_pcm_runtime *runtime = substream->runtime;
1212 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1216 epcm->type = CAPTURE_AC97MIC;
1217 epcm->substream = substream;
1218 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1219 epcm->capture_inte = INTE_MICBUFENABLE;
1220 epcm->capture_ba_reg = MICBA;
1221 epcm->capture_bs_reg = MICBS;
1222 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1223 substream->runtime->private_data = epcm;
1224 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1225 runtime->hw = snd_emu10k1_capture;
1226 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1227 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1228 runtime->hw.channels_min = 1;
1229 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1230 emu->pcm_capture_mic_substream = substream;
1231 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1235 static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1237 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1239 emu->capture_mic_interrupt = NULL;
1240 emu->pcm_capture_mic_substream = NULL;
1244 static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1246 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1247 struct snd_emu10k1_pcm *epcm;
1248 struct snd_pcm_runtime *runtime = substream->runtime;
1249 int nefx = emu->audigy ? 64 : 32;
1252 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1256 epcm->type = CAPTURE_EFX;
1257 epcm->substream = substream;
1258 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1259 epcm->capture_inte = INTE_EFXBUFENABLE;
1260 epcm->capture_ba_reg = FXBA;
1261 epcm->capture_bs_reg = FXBS;
1262 epcm->capture_idx_reg = FXIDX;
1263 substream->runtime->private_data = epcm;
1264 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1265 runtime->hw = snd_emu10k1_capture_efx;
1266 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1267 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1268 spin_lock_irq(&emu->reg_lock);
1269 if (emu->card_capabilities->emu_model) {
1270 /* Nb. of channels has been increased to 16 */
1272 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1273 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1274 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1275 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1277 * rate_max = 192000,
1278 * channels_min = 16,
1279 * channels_max = 16,
1280 * Need to add mixer control to fix sample rate
1282 * There are 32 mono channels of 16bits each.
1283 * 24bit Audio uses 2x channels over 16bit
1284 * 96kHz uses 2x channels over 48kHz
1285 * 192kHz uses 4x channels over 48kHz
1286 * So, for 48kHz 24bit, one has 16 channels
1287 * for 96kHz 24bit, one has 8 channels
1288 * for 192kHz 24bit, one has 4 channels
1292 switch (emu->emu1010.internal_clock) {
1295 runtime->hw.rates = SNDRV_PCM_RATE_44100;
1296 runtime->hw.rate_min = runtime->hw.rate_max = 44100;
1297 runtime->hw.channels_min =
1298 runtime->hw.channels_max = 16;
1302 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1303 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1304 runtime->hw.channels_min =
1305 runtime->hw.channels_max = 16;
1311 runtime->hw.rates = SNDRV_PCM_RATE_96000;
1312 runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1313 runtime->hw.channels_min = runtime->hw.channels_max = 4;
1317 runtime->hw.rates = SNDRV_PCM_RATE_192000;
1318 runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1319 runtime->hw.channels_min = runtime->hw.channels_max = 2;
1321 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1322 /* efx_voices_mask[0] is expected to be zero
1323 * efx_voices_mask[1] is expected to have 32bits set
1326 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1327 for (idx = 0; idx < nefx; idx++) {
1328 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1329 runtime->hw.channels_min++;
1330 runtime->hw.channels_max++;
1334 epcm->capture_cr_val = emu->efx_voices_mask[0];
1335 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1336 spin_unlock_irq(&emu->reg_lock);
1337 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1338 emu->pcm_capture_efx_substream = substream;
1339 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1343 static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1345 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1347 emu->capture_efx_interrupt = NULL;
1348 emu->pcm_capture_efx_substream = NULL;
1352 static const struct snd_pcm_ops snd_emu10k1_playback_ops = {
1353 .open = snd_emu10k1_playback_open,
1354 .close = snd_emu10k1_playback_close,
1355 .hw_params = snd_emu10k1_playback_hw_params,
1356 .hw_free = snd_emu10k1_playback_hw_free,
1357 .prepare = snd_emu10k1_playback_prepare,
1358 .trigger = snd_emu10k1_playback_trigger,
1359 .pointer = snd_emu10k1_playback_pointer,
1362 static const struct snd_pcm_ops snd_emu10k1_capture_ops = {
1363 .open = snd_emu10k1_capture_open,
1364 .close = snd_emu10k1_capture_close,
1365 .prepare = snd_emu10k1_capture_prepare,
1366 .trigger = snd_emu10k1_capture_trigger,
1367 .pointer = snd_emu10k1_capture_pointer,
1371 static const struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1372 .open = snd_emu10k1_efx_playback_open,
1373 .close = snd_emu10k1_efx_playback_close,
1374 .hw_params = snd_emu10k1_playback_hw_params,
1375 .hw_free = snd_emu10k1_efx_playback_hw_free,
1376 .prepare = snd_emu10k1_efx_playback_prepare,
1377 .trigger = snd_emu10k1_efx_playback_trigger,
1378 .pointer = snd_emu10k1_efx_playback_pointer,
1381 int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device)
1383 struct snd_pcm *pcm;
1384 struct snd_pcm_substream *substream;
1387 err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm);
1391 pcm->private_data = emu;
1393 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1394 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1396 pcm->info_flags = 0;
1397 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1398 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1401 /* playback substream can't use managed buffers due to alignment */
1402 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1403 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
1407 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1408 snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV,
1409 &emu->pci->dev, 64*1024, 64*1024);
1414 int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device)
1416 struct snd_pcm *pcm;
1417 struct snd_pcm_substream *substream;
1420 err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm);
1424 pcm->private_data = emu;
1426 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1428 pcm->info_flags = 0;
1429 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1430 strcpy(pcm->name, "Multichannel Playback");
1431 emu->pcm_multi = pcm;
1433 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1434 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
1442 static const struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1443 .open = snd_emu10k1_capture_mic_open,
1444 .close = snd_emu10k1_capture_mic_close,
1445 .prepare = snd_emu10k1_capture_prepare,
1446 .trigger = snd_emu10k1_capture_trigger,
1447 .pointer = snd_emu10k1_capture_pointer,
1450 int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device)
1452 struct snd_pcm *pcm;
1455 err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm);
1459 pcm->private_data = emu;
1461 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1463 pcm->info_flags = 0;
1464 strcpy(pcm->name, "Mic Capture");
1467 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,
1473 static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1475 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1476 int nefx = emu->audigy ? 64 : 32;
1477 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1478 uinfo->count = nefx;
1479 uinfo->value.integer.min = 0;
1480 uinfo->value.integer.max = 1;
1484 static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1486 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1487 int nefx = emu->audigy ? 64 : 32;
1490 spin_lock_irq(&emu->reg_lock);
1491 for (idx = 0; idx < nefx; idx++)
1492 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1493 spin_unlock_irq(&emu->reg_lock);
1497 static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1499 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1500 unsigned int nval[2], bits;
1501 int nefx = emu->audigy ? 64 : 32;
1502 int nefxb = emu->audigy ? 7 : 6;
1505 nval[0] = nval[1] = 0;
1506 for (idx = 0, bits = 0; idx < nefx; idx++)
1507 if (ucontrol->value.integer.value[idx]) {
1508 nval[idx / 32] |= 1 << (idx % 32);
1512 for (idx = 0; idx < nefxb; idx++)
1513 if (1 << idx == bits)
1519 spin_lock_irq(&emu->reg_lock);
1520 change = (nval[0] != emu->efx_voices_mask[0]) ||
1521 (nval[1] != emu->efx_voices_mask[1]);
1522 emu->efx_voices_mask[0] = nval[0];
1523 emu->efx_voices_mask[1] = nval[1];
1524 spin_unlock_irq(&emu->reg_lock);
1528 static const struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1529 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1530 .name = "Captured FX8010 Outputs",
1531 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1532 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1533 .put = snd_emu10k1_pcm_efx_voices_mask_put
1536 static const struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1537 .open = snd_emu10k1_capture_efx_open,
1538 .close = snd_emu10k1_capture_efx_close,
1539 .prepare = snd_emu10k1_capture_prepare,
1540 .trigger = snd_emu10k1_capture_trigger,
1541 .pointer = snd_emu10k1_capture_pointer,
1547 #define INITIAL_TRAM_SHIFT 14
1548 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1550 static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1552 struct snd_pcm_substream *substream = private_data;
1553 snd_pcm_period_elapsed(substream);
1556 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1557 unsigned short *dst_right,
1558 unsigned short *src,
1560 unsigned int tram_shift)
1563 dev_dbg(emu->card->dev,
1564 "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
1565 "src = 0x%p, count = 0x%x\n",
1566 dst_left, dst_right, src, count);
1568 if ((tram_shift & 1) == 0) {
1570 *dst_left-- = *src++;
1571 *dst_right-- = *src++;
1575 *dst_right-- = *src++;
1576 *dst_left-- = *src++;
1581 static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1582 struct snd_pcm_indirect *rec, size_t bytes)
1584 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1585 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1586 unsigned int tram_size = pcm->buffer_size;
1587 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1588 unsigned int frames = bytes >> 2, count;
1589 unsigned int tram_pos = pcm->tram_pos;
1590 unsigned int tram_shift = pcm->tram_shift;
1592 while (frames > tram_pos) {
1593 count = tram_pos + 1;
1594 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1595 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1596 src, count, tram_shift);
1599 tram_pos = (tram_size / 2) - 1;
1602 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1603 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1604 src, frames, tram_shift);
1606 pcm->tram_pos = tram_pos;
1607 pcm->tram_shift = tram_shift;
1610 static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1612 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1613 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1615 return snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec,
1616 fx8010_pb_trans_copy);
1619 static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1621 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1622 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1625 for (i = 0; i < pcm->channels; i++)
1626 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1630 static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1632 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1633 struct snd_pcm_runtime *runtime = substream->runtime;
1634 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1638 dev_dbg(emu->card->dev, "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
1639 "buffer_size = 0x%x (0x%x)\n",
1640 emu->fx8010.etram_pages, runtime->dma_area,
1641 runtime->buffer_size, runtime->buffer_size << 2);
1643 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1644 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1645 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1646 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1647 pcm->tram_shift = 0;
1648 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
1649 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
1650 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1651 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
1652 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1653 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1654 for (i = 0; i < pcm->channels; i++)
1655 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1659 static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1661 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1662 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1665 spin_lock(&emu->reg_lock);
1667 case SNDRV_PCM_TRIGGER_START:
1669 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1670 case SNDRV_PCM_TRIGGER_RESUME:
1671 #ifdef EMU10K1_SET_AC3_IEC958
1674 for (i = 0; i < 3; i++) {
1676 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1677 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1678 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1679 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1683 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1686 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1687 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1689 case SNDRV_PCM_TRIGGER_STOP:
1690 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1691 case SNDRV_PCM_TRIGGER_SUSPEND:
1692 snd_emu10k1_fx8010_unregister_irq_handler(emu, &pcm->irq);
1693 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1694 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1695 pcm->tram_shift = 0;
1702 spin_unlock(&emu->reg_lock);
1706 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1708 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1709 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1710 size_t ptr; /* byte pointer */
1712 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1714 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1715 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1718 static const struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1720 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1721 SNDRV_PCM_INFO_RESUME |
1722 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE |
1723 SNDRV_PCM_INFO_SYNC_APPLPTR),
1724 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1725 .rates = SNDRV_PCM_RATE_48000,
1730 .buffer_bytes_max = (128*1024),
1731 .period_bytes_min = 1024,
1732 .period_bytes_max = (128*1024),
1734 .periods_max = 1024,
1738 static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1740 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1741 struct snd_pcm_runtime *runtime = substream->runtime;
1742 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1744 runtime->hw = snd_emu10k1_fx8010_playback;
1745 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1746 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1747 spin_lock_irq(&emu->reg_lock);
1748 if (pcm->valid == 0) {
1749 spin_unlock_irq(&emu->reg_lock);
1753 spin_unlock_irq(&emu->reg_lock);
1757 static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1759 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1760 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1762 spin_lock_irq(&emu->reg_lock);
1764 spin_unlock_irq(&emu->reg_lock);
1768 static const struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1769 .open = snd_emu10k1_fx8010_playback_open,
1770 .close = snd_emu10k1_fx8010_playback_close,
1771 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1772 .prepare = snd_emu10k1_fx8010_playback_prepare,
1773 .trigger = snd_emu10k1_fx8010_playback_trigger,
1774 .pointer = snd_emu10k1_fx8010_playback_pointer,
1775 .ack = snd_emu10k1_fx8010_playback_transfer,
1778 int snd_emu10k1_pcm_efx(struct snd_emu10k1 *emu, int device)
1780 struct snd_pcm *pcm;
1781 struct snd_kcontrol *kctl;
1784 err = snd_pcm_new(emu->card, "emu10k1 efx", device, emu->audigy ? 0 : 8, 1, &pcm);
1788 pcm->private_data = emu;
1791 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1792 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1794 pcm->info_flags = 0;
1796 strcpy(pcm->name, "Multichannel Capture");
1798 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1801 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1805 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1807 emu->efx_voices_mask[0] = 0;
1808 if (emu->card_capabilities->emu_model)
1809 /* Pavel Hofman - 32 voices will be used for
1810 * capture (write mode) -
1811 * each bit = corresponding voice
1813 emu->efx_voices_mask[1] = 0xffffffff;
1815 emu->efx_voices_mask[1] = 0xffff;
1817 emu->efx_voices_mask[0] = 0xffff0000;
1818 emu->efx_voices_mask[1] = 0;
1820 /* For emu1010, the control has to set 32 upper bits (voices)
1821 * out of the 64 bits (voices) to true for the 16-channels capture
1822 * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1823 * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1824 * control can override this register's value.
1826 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1829 kctl->id.device = device;
1830 err = snd_ctl_add(emu->card, kctl);
1834 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,