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;
28 if ((epcm = voice->epcm) == NULL)
30 if (epcm->substream == NULL)
33 dev_dbg(emu->card->dev,
34 "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
35 epcm->substream->runtime->hw->pointer(emu, epcm->substream),
36 snd_pcm_lib_period_bytes(epcm->substream),
37 snd_pcm_lib_buffer_bytes(epcm->substream));
39 snd_pcm_period_elapsed(epcm->substream);
42 static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
46 if (status & IPR_ADCBUFHALFFULL) {
47 if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
51 snd_pcm_period_elapsed(emu->pcm_capture_substream);
54 static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
58 if (status & IPR_MICBUFHALFFULL) {
59 if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
63 snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
66 static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
70 if (status & IPR_EFXBUFHALFFULL) {
71 if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
75 snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
78 static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream)
80 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
81 struct snd_pcm_runtime *runtime = substream->runtime;
82 struct snd_emu10k1_pcm *epcm = runtime->private_data;
87 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
88 ptr += runtime->buffer_size;
89 ptr -= epcm->ccca_start_addr;
90 ptr %= runtime->buffer_size;
95 static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
99 if (epcm->voices[1] != NULL && voices < 2) {
100 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
101 epcm->voices[1] = NULL;
103 for (i = 0; i < voices; i++) {
104 if (epcm->voices[i] == NULL)
108 return 0; /* already allocated */
110 for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
111 if (epcm->voices[i]) {
112 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
113 epcm->voices[i] = NULL;
116 err = snd_emu10k1_voice_alloc(epcm->emu,
117 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
123 epcm->voices[0]->epcm = epcm;
125 for (i = 1; i < voices; i++) {
126 epcm->voices[i] = &epcm->emu->voices[epcm->voices[0]->number + i];
127 epcm->voices[i]->epcm = epcm;
130 if (epcm->extra == NULL) {
131 err = snd_emu10k1_voice_alloc(epcm->emu,
132 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
137 dev_dbg(emu->card->dev, "pcm_channel_alloc: "
138 "failed extra: voices=%d, frame=%d\n",
141 for (i = 0; i < voices; i++) {
142 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
143 epcm->voices[i] = NULL;
147 epcm->extra->epcm = epcm;
148 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
153 static const unsigned int capture_period_sizes[31] = {
155 384*2, 448*2, 512*2, 640*2,
156 384*4, 448*4, 512*4, 640*4,
157 384*8, 448*8, 512*8, 640*8,
158 384*16, 448*16, 512*16, 640*16,
159 384*32, 448*32, 512*32, 640*32,
160 384*64, 448*64, 512*64, 640*64,
161 384*128,448*128,512*128
164 static const struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
166 .list = capture_period_sizes,
170 static const unsigned int capture_rates[8] = {
171 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
174 static const struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
176 .list = capture_rates,
180 static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
183 case 8000: return ADCCR_SAMPLERATE_8;
184 case 11025: return ADCCR_SAMPLERATE_11;
185 case 16000: return ADCCR_SAMPLERATE_16;
186 case 22050: return ADCCR_SAMPLERATE_22;
187 case 24000: return ADCCR_SAMPLERATE_24;
188 case 32000: return ADCCR_SAMPLERATE_32;
189 case 44100: return ADCCR_SAMPLERATE_44;
190 case 48000: return ADCCR_SAMPLERATE_48;
193 return ADCCR_SAMPLERATE_8;
197 static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
200 case 8000: return A_ADCCR_SAMPLERATE_8;
201 case 11025: return A_ADCCR_SAMPLERATE_11;
202 case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */
203 case 16000: return ADCCR_SAMPLERATE_16;
204 case 22050: return ADCCR_SAMPLERATE_22;
205 case 24000: return ADCCR_SAMPLERATE_24;
206 case 32000: return ADCCR_SAMPLERATE_32;
207 case 44100: return ADCCR_SAMPLERATE_44;
208 case 48000: return ADCCR_SAMPLERATE_48;
211 return A_ADCCR_SAMPLERATE_8;
215 static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
217 unsigned int pitch_target;
219 pitch_target = (rate << 8) / 375;
220 pitch_target = (pitch_target >> 1) + (pitch_target & 1);
224 #define PITCH_48000 0x00004000
225 #define PITCH_96000 0x00008000
226 #define PITCH_85000 0x00007155
227 #define PITCH_80726 0x00006ba2
228 #define PITCH_67882 0x00005a82
229 #define PITCH_57081 0x00004c1c
231 static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
233 if (pitch_target == PITCH_48000)
234 return CCCA_INTERPROM_0;
235 else if (pitch_target < PITCH_48000)
236 return CCCA_INTERPROM_1;
237 else if (pitch_target >= PITCH_96000)
238 return CCCA_INTERPROM_0;
239 else if (pitch_target >= PITCH_85000)
240 return CCCA_INTERPROM_6;
241 else if (pitch_target >= PITCH_80726)
242 return CCCA_INTERPROM_5;
243 else if (pitch_target >= PITCH_67882)
244 return CCCA_INTERPROM_4;
245 else if (pitch_target >= PITCH_57081)
246 return CCCA_INTERPROM_3;
248 return CCCA_INTERPROM_2;
252 * calculate cache invalidate size
254 * stereo: channel is stereo
255 * w_16: using 16bit samples
257 * returns: cache invalidate size in samples
259 static inline int emu10k1_ccis(int stereo, int w_16)
262 return stereo ? 24 : 26;
264 return stereo ? 24*2 : 26*2;
268 static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
269 int master, int extra,
270 struct snd_emu10k1_voice *evoice,
271 unsigned int start_addr,
272 unsigned int end_addr,
273 struct snd_emu10k1_pcm_mixer *mix)
275 struct snd_pcm_substream *substream = evoice->epcm->substream;
276 struct snd_pcm_runtime *runtime = substream->runtime;
277 unsigned int silent_page, tmp;
278 int voice, stereo, w_16;
279 unsigned char send_amount[8];
280 unsigned char send_routing[8];
282 unsigned int pitch_target;
285 voice = evoice->number;
286 stereo = runtime->channels == 2;
287 w_16 = snd_pcm_format_width(runtime->format) == 16;
289 if (!extra && stereo) {
298 spin_lock_irqsave(&emu->reg_lock, flags);
300 /* volume parameters */
302 memset(send_routing, 0, sizeof(send_routing));
307 memset(send_amount, 0, sizeof(send_amount));
309 /* mono, left, right (master voice = left) */
310 tmp = stereo ? (master ? 1 : 2) : 0;
311 memcpy(send_routing, &mix->send_routing[tmp][0], 8);
312 memcpy(send_amount, &mix->send_volume[tmp][0], 8);
315 ccis = emu10k1_ccis(stereo, w_16);
318 evoice->epcm->ccca_start_addr = start_addr + ccis;
321 end_addr += ccis + emu->delay_pcm_irq;
323 if (stereo && !extra) {
324 snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
325 snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
327 snd_emu10k1_ptr_write(emu, CPF, voice, 0);
333 snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
334 snd_emu10k1_compose_audigy_fxrt1(send_routing));
335 snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
336 snd_emu10k1_compose_audigy_fxrt2(send_routing));
337 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
338 ((unsigned int)send_amount[4] << 24) |
339 ((unsigned int)send_amount[5] << 16) |
340 ((unsigned int)send_amount[6] << 8) |
341 (unsigned int)send_amount[7]);
343 snd_emu10k1_ptr_write(emu, FXRT, voice,
344 snd_emu10k1_compose_send_routing(send_routing));
346 /* Assumption that PT is already 0 so no harm overwriting */
347 snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
348 snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
349 snd_emu10k1_ptr_write(emu, PSST, voice,
350 (start_addr + (extra ? emu->delay_pcm_irq : 0)) |
351 (send_amount[2] << 24));
352 if (emu->card_capabilities->emu_model)
353 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
355 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
357 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
358 emu10k1_select_interprom(pitch_target) |
359 (w_16 ? 0 : CCCA_8BITSELECT));
361 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
362 emu10k1_select_interprom(pitch_target) |
363 (w_16 ? 0 : CCCA_8BITSELECT));
364 /* Clear filter delay memory */
365 snd_emu10k1_ptr_write(emu, Z1, voice, 0);
366 snd_emu10k1_ptr_write(emu, Z2, voice, 0);
367 /* invalidate maps */
368 silent_page = ((unsigned int)emu->silent_page.addr << emu->address_mode) | (emu->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
369 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
370 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
371 /* modulation envelope */
372 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
373 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
374 snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
375 snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
376 snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
377 snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
378 snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
379 snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
380 snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
381 snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
382 /* volume envelope */
383 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
384 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
385 /* filter envelope */
386 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
388 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
390 spin_unlock_irqrestore(&emu->reg_lock, flags);
393 static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
394 struct snd_pcm_hw_params *hw_params)
396 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
397 struct snd_pcm_runtime *runtime = substream->runtime;
398 struct snd_emu10k1_pcm *epcm = runtime->private_data;
402 if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
405 alloc_size = params_buffer_bytes(hw_params);
406 if (emu->iommu_workaround)
407 alloc_size += EMUPAGESIZE;
408 err = snd_pcm_lib_malloc_pages(substream, alloc_size);
411 if (emu->iommu_workaround && runtime->dma_bytes >= EMUPAGESIZE)
412 runtime->dma_bytes -= EMUPAGESIZE;
413 if (err > 0) { /* change */
415 if (epcm->memblk != NULL)
416 snd_emu10k1_free_pages(emu, epcm->memblk);
417 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
418 epcm->start_addr = 0;
421 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
424 epcm->start_addr = mapped << PAGE_SHIFT;
429 static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
431 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
432 struct snd_pcm_runtime *runtime = substream->runtime;
433 struct snd_emu10k1_pcm *epcm;
435 if (runtime->private_data == NULL)
437 epcm = runtime->private_data;
439 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
442 if (epcm->voices[1]) {
443 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
444 epcm->voices[1] = NULL;
446 if (epcm->voices[0]) {
447 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
448 epcm->voices[0] = NULL;
451 snd_emu10k1_free_pages(emu, epcm->memblk);
453 epcm->start_addr = 0;
455 snd_pcm_lib_free_pages(substream);
459 static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
461 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
462 struct snd_pcm_runtime *runtime = substream->runtime;
463 struct snd_emu10k1_pcm *epcm;
466 if (runtime->private_data == NULL)
468 epcm = runtime->private_data;
470 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
473 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
474 if (epcm->voices[i]) {
475 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
476 epcm->voices[i] = NULL;
480 snd_emu10k1_free_pages(emu, epcm->memblk);
482 epcm->start_addr = 0;
484 snd_pcm_lib_free_pages(substream);
488 static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
490 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
491 struct snd_pcm_runtime *runtime = substream->runtime;
492 struct snd_emu10k1_pcm *epcm = runtime->private_data;
493 unsigned int start_addr, end_addr;
495 start_addr = epcm->start_addr;
496 end_addr = snd_pcm_lib_period_bytes(substream);
497 if (runtime->channels == 2) {
501 end_addr += start_addr;
502 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
503 start_addr, end_addr, NULL);
504 start_addr = epcm->start_addr;
505 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
506 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
507 start_addr, end_addr,
508 &emu->pcm_mixer[substream->number]);
510 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
511 start_addr, end_addr,
512 &emu->pcm_mixer[substream->number]);
516 static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
518 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
519 struct snd_pcm_runtime *runtime = substream->runtime;
520 struct snd_emu10k1_pcm *epcm = runtime->private_data;
521 unsigned int start_addr, end_addr;
522 unsigned int channel_size;
525 start_addr = epcm->start_addr;
526 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
529 * the kX driver leaves some space between voices
531 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
533 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
534 start_addr, start_addr + (channel_size / 2), NULL);
536 /* only difference with the master voice is we use it for the pointer */
537 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
538 start_addr, start_addr + channel_size,
539 &emu->efx_pcm_mixer[0]);
541 start_addr += channel_size;
542 for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
543 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
544 start_addr, start_addr + channel_size,
545 &emu->efx_pcm_mixer[i]);
546 start_addr += channel_size;
552 static const struct snd_pcm_hardware snd_emu10k1_efx_playback =
554 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
555 SNDRV_PCM_INFO_BLOCK_TRANSFER |
556 SNDRV_PCM_INFO_RESUME |
557 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
558 .formats = SNDRV_PCM_FMTBIT_S16_LE,
559 .rates = SNDRV_PCM_RATE_48000,
562 .channels_min = NUM_EFX_PLAYBACK,
563 .channels_max = NUM_EFX_PLAYBACK,
564 .buffer_bytes_max = (64*1024),
565 .period_bytes_min = 64,
566 .period_bytes_max = (64*1024),
572 static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
574 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
575 struct snd_pcm_runtime *runtime = substream->runtime;
576 struct snd_emu10k1_pcm *epcm = runtime->private_data;
579 /* zeroing the buffer size will stop capture */
580 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
581 switch (epcm->type) {
582 case CAPTURE_AC97ADC:
583 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
587 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
588 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
590 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
595 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
596 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
597 epcm->capture_bs_val = 0;
598 for (idx = 0; idx < 31; idx++) {
599 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
600 epcm->capture_bs_val = idx + 1;
604 if (epcm->capture_bs_val == 0) {
606 epcm->capture_bs_val++;
608 if (epcm->type == CAPTURE_AC97ADC) {
609 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
610 if (runtime->channels > 1)
611 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
612 epcm->capture_cr_val |= emu->audigy ?
613 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
614 snd_emu10k1_capture_rate_reg(runtime->rate);
619 static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
621 struct snd_pcm_runtime *runtime;
622 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
626 runtime = evoice->epcm->substream->runtime;
627 voice = evoice->number;
628 stereo = (!extra && runtime->channels == 2);
629 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
630 ccis = emu10k1_ccis(stereo, sample == 0);
631 /* set cs to 2 * number of cache registers beside the invalidated */
632 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
633 if (cs > 16) cs = 16;
634 for (i = 0; i < cs; i++) {
635 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
637 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
641 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
642 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
644 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
645 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
648 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
650 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
654 static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
655 int master, int extra,
656 struct snd_emu10k1_pcm_mixer *mix)
658 struct snd_pcm_substream *substream;
659 struct snd_pcm_runtime *runtime;
660 unsigned int attn, vattn;
661 unsigned int voice, tmp;
663 if (evoice == NULL) /* skip second voice for mono */
665 substream = evoice->epcm->substream;
666 runtime = substream->runtime;
667 voice = evoice->number;
669 attn = extra ? 0 : 0x00ff;
670 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
671 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
672 snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
673 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
674 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
675 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
676 snd_emu10k1_voice_clear_loop_stop(emu, voice);
679 static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
681 struct snd_pcm_substream *substream;
682 struct snd_pcm_runtime *runtime;
683 unsigned int voice, pitch, pitch_target;
685 if (evoice == NULL) /* skip second voice for mono */
687 substream = evoice->epcm->substream;
688 runtime = substream->runtime;
689 voice = evoice->number;
691 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
692 if (emu->card_capabilities->emu_model)
693 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
695 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
696 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
697 if (master || evoice->epcm->type == PLAYBACK_EFX)
698 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
699 snd_emu10k1_ptr_write(emu, IP, voice, pitch);
701 snd_emu10k1_voice_intr_enable(emu, voice);
704 static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
710 voice = evoice->number;
711 snd_emu10k1_voice_intr_disable(emu, voice);
712 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
713 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
714 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
715 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
716 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
717 snd_emu10k1_ptr_write(emu, IP, voice, 0);
720 static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu,
721 struct snd_emu10k1_pcm *epcm,
722 struct snd_pcm_substream *substream,
723 struct snd_pcm_runtime *runtime)
725 unsigned int ptr, period_pos;
727 /* try to sychronize the current position for the interrupt
729 period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt;
730 period_pos %= runtime->period_size;
731 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number);
733 ptr |= epcm->ccca_start_addr + period_pos;
734 snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr);
737 static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
740 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
741 struct snd_pcm_runtime *runtime = substream->runtime;
742 struct snd_emu10k1_pcm *epcm = runtime->private_data;
743 struct snd_emu10k1_pcm_mixer *mix;
747 dev_dbg(emu->card->dev,
748 "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
749 (int)emu, cmd, substream->ops->pointer(substream))
751 spin_lock(&emu->reg_lock);
753 case SNDRV_PCM_TRIGGER_START:
754 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
755 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
757 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
758 case SNDRV_PCM_TRIGGER_RESUME:
759 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
760 snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime);
761 mix = &emu->pcm_mixer[substream->number];
762 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
763 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
764 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
765 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
766 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
767 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
770 case SNDRV_PCM_TRIGGER_STOP:
771 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
772 case SNDRV_PCM_TRIGGER_SUSPEND:
774 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
775 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
776 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
782 spin_unlock(&emu->reg_lock);
786 static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
789 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
790 struct snd_pcm_runtime *runtime = substream->runtime;
791 struct snd_emu10k1_pcm *epcm = runtime->private_data;
794 spin_lock(&emu->reg_lock);
796 case SNDRV_PCM_TRIGGER_START:
797 case SNDRV_PCM_TRIGGER_RESUME:
798 /* hmm this should cause full and half full interrupt to be raised? */
799 outl(epcm->capture_ipr, emu->port + IPR);
800 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
802 dev_dbg(emu->card->dev, "adccr = 0x%x, adcbs = 0x%x\n",
803 epcm->adccr, epcm->adcbs);
805 switch (epcm->type) {
806 case CAPTURE_AC97ADC:
807 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
811 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
812 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
813 dev_dbg(emu->card->dev,
814 "cr_val=0x%x, cr_val2=0x%x\n",
815 epcm->capture_cr_val,
816 epcm->capture_cr_val2);
818 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
823 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
827 case SNDRV_PCM_TRIGGER_STOP:
828 case SNDRV_PCM_TRIGGER_SUSPEND:
830 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
831 outl(epcm->capture_ipr, emu->port + IPR);
832 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
833 switch (epcm->type) {
834 case CAPTURE_AC97ADC:
835 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
839 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
840 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
842 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
851 spin_unlock(&emu->reg_lock);
855 static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
857 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
858 struct snd_pcm_runtime *runtime = substream->runtime;
859 struct snd_emu10k1_pcm *epcm = runtime->private_data;
864 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
865 #if 0 /* Perex's code */
866 ptr += runtime->buffer_size;
867 ptr -= epcm->ccca_start_addr;
868 ptr %= runtime->buffer_size;
869 #else /* EMU10K1 Open Source code from Creative */
870 if (ptr < epcm->ccca_start_addr)
871 ptr += runtime->buffer_size - epcm->ccca_start_addr;
873 ptr -= epcm->ccca_start_addr;
874 if (ptr >= runtime->buffer_size)
875 ptr -= runtime->buffer_size;
879 dev_dbg(emu->card->dev,
880 "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
881 (long)ptr, (long)runtime->buffer_size,
882 (long)runtime->period_size);
888 static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
891 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
892 struct snd_pcm_runtime *runtime = substream->runtime;
893 struct snd_emu10k1_pcm *epcm = runtime->private_data;
897 spin_lock(&emu->reg_lock);
899 case SNDRV_PCM_TRIGGER_START:
901 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
902 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
904 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
906 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
907 case SNDRV_PCM_TRIGGER_RESUME:
908 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
909 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
910 &emu->efx_pcm_mixer[0]);
911 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
912 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
913 &emu->efx_pcm_mixer[i]);
914 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
915 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
916 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
917 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
920 case SNDRV_PCM_TRIGGER_SUSPEND:
921 case SNDRV_PCM_TRIGGER_STOP:
922 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
924 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
925 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
927 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
933 spin_unlock(&emu->reg_lock);
938 static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
940 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
941 struct snd_pcm_runtime *runtime = substream->runtime;
942 struct snd_emu10k1_pcm *epcm = runtime->private_data;
947 if (epcm->first_ptr) {
948 udelay(50); /* hack, it takes awhile until capture is started */
951 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
952 return bytes_to_frames(runtime, ptr);
956 * Playback support device description
959 static const struct snd_pcm_hardware snd_emu10k1_playback =
961 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
962 SNDRV_PCM_INFO_BLOCK_TRANSFER |
963 SNDRV_PCM_INFO_RESUME |
964 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
965 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
966 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
971 .buffer_bytes_max = (128*1024),
972 .period_bytes_min = 64,
973 .period_bytes_max = (128*1024),
980 * Capture support device description
983 static const struct snd_pcm_hardware snd_emu10k1_capture =
985 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
986 SNDRV_PCM_INFO_BLOCK_TRANSFER |
987 SNDRV_PCM_INFO_RESUME |
988 SNDRV_PCM_INFO_MMAP_VALID),
989 .formats = SNDRV_PCM_FMTBIT_S16_LE,
990 .rates = SNDRV_PCM_RATE_8000_48000,
995 .buffer_bytes_max = (64*1024),
996 .period_bytes_min = 384,
997 .period_bytes_max = (64*1024),
1003 static const struct snd_pcm_hardware snd_emu10k1_capture_efx =
1005 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1006 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1007 SNDRV_PCM_INFO_RESUME |
1008 SNDRV_PCM_INFO_MMAP_VALID),
1009 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1010 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1011 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1012 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
1017 .buffer_bytes_max = (64*1024),
1018 .period_bytes_min = 384,
1019 .period_bytes_max = (64*1024),
1029 static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
1031 struct snd_ctl_elem_id id;
1036 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1038 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1039 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1040 SNDRV_CTL_EVENT_MASK_INFO,
1041 snd_ctl_build_ioff(&id, kctl, idx));
1044 static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1046 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1047 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1048 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1051 static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1053 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1054 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1055 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1058 static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
1060 kfree(runtime->private_data);
1063 static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
1065 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1066 struct snd_emu10k1_pcm_mixer *mix;
1069 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1070 mix = &emu->efx_pcm_mixer[i];
1072 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1077 static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
1079 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1080 struct snd_emu10k1_pcm *epcm;
1081 struct snd_emu10k1_pcm_mixer *mix;
1082 struct snd_pcm_runtime *runtime = substream->runtime;
1085 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1089 epcm->type = PLAYBACK_EFX;
1090 epcm->substream = substream;
1092 emu->pcm_playback_efx_substream = substream;
1094 runtime->private_data = epcm;
1095 runtime->private_free = snd_emu10k1_pcm_free_substream;
1096 runtime->hw = snd_emu10k1_efx_playback;
1098 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1099 mix = &emu->efx_pcm_mixer[i];
1100 mix->send_routing[0][0] = i;
1101 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1102 mix->send_volume[0][0] = 255;
1103 mix->attn[0] = 0xffff;
1105 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1110 static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
1112 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1113 struct snd_emu10k1_pcm *epcm;
1114 struct snd_emu10k1_pcm_mixer *mix;
1115 struct snd_pcm_runtime *runtime = substream->runtime;
1116 int i, err, sample_rate;
1118 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1122 epcm->type = PLAYBACK_EMUVOICE;
1123 epcm->substream = substream;
1124 runtime->private_data = epcm;
1125 runtime->private_free = snd_emu10k1_pcm_free_substream;
1126 runtime->hw = snd_emu10k1_playback;
1127 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1131 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1135 if (emu->card_capabilities->emu_model && emu->emu1010.internal_clock == 0)
1136 sample_rate = 44100;
1138 sample_rate = 48000;
1139 err = snd_pcm_hw_rule_noresample(runtime, sample_rate);
1144 mix = &emu->pcm_mixer[substream->number];
1145 for (i = 0; i < 4; i++)
1146 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1147 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1148 mix->send_volume[0][0] = mix->send_volume[0][1] =
1149 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1150 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1152 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1156 static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1158 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1159 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1162 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1166 static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1168 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1169 struct snd_pcm_runtime *runtime = substream->runtime;
1170 struct snd_emu10k1_pcm *epcm;
1172 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1176 epcm->type = CAPTURE_AC97ADC;
1177 epcm->substream = substream;
1178 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1179 epcm->capture_inte = INTE_ADCBUFENABLE;
1180 epcm->capture_ba_reg = ADCBA;
1181 epcm->capture_bs_reg = ADCBS;
1182 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1183 runtime->private_data = epcm;
1184 runtime->private_free = snd_emu10k1_pcm_free_substream;
1185 runtime->hw = snd_emu10k1_capture;
1186 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1187 emu->pcm_capture_substream = substream;
1188 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1189 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1193 static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1195 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1197 emu->capture_interrupt = NULL;
1198 emu->pcm_capture_substream = NULL;
1202 static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1204 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1205 struct snd_emu10k1_pcm *epcm;
1206 struct snd_pcm_runtime *runtime = substream->runtime;
1208 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1212 epcm->type = CAPTURE_AC97MIC;
1213 epcm->substream = substream;
1214 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1215 epcm->capture_inte = INTE_MICBUFENABLE;
1216 epcm->capture_ba_reg = MICBA;
1217 epcm->capture_bs_reg = MICBS;
1218 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1219 substream->runtime->private_data = epcm;
1220 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1221 runtime->hw = snd_emu10k1_capture;
1222 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1223 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1224 runtime->hw.channels_min = 1;
1225 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1226 emu->pcm_capture_mic_substream = substream;
1227 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1231 static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1233 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1235 emu->capture_interrupt = NULL;
1236 emu->pcm_capture_mic_substream = NULL;
1240 static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1242 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1243 struct snd_emu10k1_pcm *epcm;
1244 struct snd_pcm_runtime *runtime = substream->runtime;
1245 int nefx = emu->audigy ? 64 : 32;
1248 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1252 epcm->type = CAPTURE_EFX;
1253 epcm->substream = substream;
1254 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1255 epcm->capture_inte = INTE_EFXBUFENABLE;
1256 epcm->capture_ba_reg = FXBA;
1257 epcm->capture_bs_reg = FXBS;
1258 epcm->capture_idx_reg = FXIDX;
1259 substream->runtime->private_data = epcm;
1260 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1261 runtime->hw = snd_emu10k1_capture_efx;
1262 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1263 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1264 spin_lock_irq(&emu->reg_lock);
1265 if (emu->card_capabilities->emu_model) {
1266 /* Nb. of channels has been increased to 16 */
1268 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1269 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1270 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1271 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1273 * rate_max = 192000,
1274 * channels_min = 16,
1275 * channels_max = 16,
1276 * Need to add mixer control to fix sample rate
1278 * There are 32 mono channels of 16bits each.
1279 * 24bit Audio uses 2x channels over 16bit
1280 * 96kHz uses 2x channels over 48kHz
1281 * 192kHz uses 4x channels over 48kHz
1282 * So, for 48kHz 24bit, one has 16 channels
1283 * for 96kHz 24bit, one has 8 channels
1284 * for 192kHz 24bit, one has 4 channels
1288 switch (emu->emu1010.internal_clock) {
1291 runtime->hw.rates = SNDRV_PCM_RATE_44100;
1292 runtime->hw.rate_min = runtime->hw.rate_max = 44100;
1293 runtime->hw.channels_min =
1294 runtime->hw.channels_max = 16;
1298 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1299 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1300 runtime->hw.channels_min =
1301 runtime->hw.channels_max = 16;
1307 runtime->hw.rates = SNDRV_PCM_RATE_96000;
1308 runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1309 runtime->hw.channels_min = runtime->hw.channels_max = 4;
1313 runtime->hw.rates = SNDRV_PCM_RATE_192000;
1314 runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1315 runtime->hw.channels_min = runtime->hw.channels_max = 2;
1317 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1318 /* efx_voices_mask[0] is expected to be zero
1319 * efx_voices_mask[1] is expected to have 32bits set
1322 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1323 for (idx = 0; idx < nefx; idx++) {
1324 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1325 runtime->hw.channels_min++;
1326 runtime->hw.channels_max++;
1330 epcm->capture_cr_val = emu->efx_voices_mask[0];
1331 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1332 spin_unlock_irq(&emu->reg_lock);
1333 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1334 emu->pcm_capture_efx_substream = substream;
1335 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1339 static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1341 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1343 emu->capture_interrupt = NULL;
1344 emu->pcm_capture_efx_substream = NULL;
1348 static const struct snd_pcm_ops snd_emu10k1_playback_ops = {
1349 .open = snd_emu10k1_playback_open,
1350 .close = snd_emu10k1_playback_close,
1351 .hw_params = snd_emu10k1_playback_hw_params,
1352 .hw_free = snd_emu10k1_playback_hw_free,
1353 .prepare = snd_emu10k1_playback_prepare,
1354 .trigger = snd_emu10k1_playback_trigger,
1355 .pointer = snd_emu10k1_playback_pointer,
1358 static const struct snd_pcm_ops snd_emu10k1_capture_ops = {
1359 .open = snd_emu10k1_capture_open,
1360 .close = snd_emu10k1_capture_close,
1361 .prepare = snd_emu10k1_capture_prepare,
1362 .trigger = snd_emu10k1_capture_trigger,
1363 .pointer = snd_emu10k1_capture_pointer,
1367 static const struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1368 .open = snd_emu10k1_efx_playback_open,
1369 .close = snd_emu10k1_efx_playback_close,
1370 .hw_params = snd_emu10k1_playback_hw_params,
1371 .hw_free = snd_emu10k1_efx_playback_hw_free,
1372 .prepare = snd_emu10k1_efx_playback_prepare,
1373 .trigger = snd_emu10k1_efx_playback_trigger,
1374 .pointer = snd_emu10k1_efx_playback_pointer,
1377 int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device)
1379 struct snd_pcm *pcm;
1380 struct snd_pcm_substream *substream;
1383 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1386 pcm->private_data = emu;
1388 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1389 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1391 pcm->info_flags = 0;
1392 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1393 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1396 /* playback substream can't use managed buffers due to alignment */
1397 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1398 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
1402 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1403 snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV,
1404 &emu->pci->dev, 64*1024, 64*1024);
1409 int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device)
1411 struct snd_pcm *pcm;
1412 struct snd_pcm_substream *substream;
1415 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1418 pcm->private_data = emu;
1420 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1422 pcm->info_flags = 0;
1423 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1424 strcpy(pcm->name, "Multichannel Playback");
1425 emu->pcm_multi = pcm;
1427 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1428 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
1436 static const struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1437 .open = snd_emu10k1_capture_mic_open,
1438 .close = snd_emu10k1_capture_mic_close,
1439 .prepare = snd_emu10k1_capture_prepare,
1440 .trigger = snd_emu10k1_capture_trigger,
1441 .pointer = snd_emu10k1_capture_pointer,
1444 int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device)
1446 struct snd_pcm *pcm;
1449 if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1452 pcm->private_data = emu;
1454 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1456 pcm->info_flags = 0;
1457 strcpy(pcm->name, "Mic Capture");
1460 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,
1466 static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1468 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1469 int nefx = emu->audigy ? 64 : 32;
1470 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1471 uinfo->count = nefx;
1472 uinfo->value.integer.min = 0;
1473 uinfo->value.integer.max = 1;
1477 static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1479 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1480 int nefx = emu->audigy ? 64 : 32;
1483 spin_lock_irq(&emu->reg_lock);
1484 for (idx = 0; idx < nefx; idx++)
1485 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1486 spin_unlock_irq(&emu->reg_lock);
1490 static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1492 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1493 unsigned int nval[2], bits;
1494 int nefx = emu->audigy ? 64 : 32;
1495 int nefxb = emu->audigy ? 7 : 6;
1498 nval[0] = nval[1] = 0;
1499 for (idx = 0, bits = 0; idx < nefx; idx++)
1500 if (ucontrol->value.integer.value[idx]) {
1501 nval[idx / 32] |= 1 << (idx % 32);
1505 for (idx = 0; idx < nefxb; idx++)
1506 if (1 << idx == bits)
1512 spin_lock_irq(&emu->reg_lock);
1513 change = (nval[0] != emu->efx_voices_mask[0]) ||
1514 (nval[1] != emu->efx_voices_mask[1]);
1515 emu->efx_voices_mask[0] = nval[0];
1516 emu->efx_voices_mask[1] = nval[1];
1517 spin_unlock_irq(&emu->reg_lock);
1521 static const struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1522 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1523 .name = "Captured FX8010 Outputs",
1524 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1525 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1526 .put = snd_emu10k1_pcm_efx_voices_mask_put
1529 static const struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1530 .open = snd_emu10k1_capture_efx_open,
1531 .close = snd_emu10k1_capture_efx_close,
1532 .prepare = snd_emu10k1_capture_prepare,
1533 .trigger = snd_emu10k1_capture_trigger,
1534 .pointer = snd_emu10k1_capture_pointer,
1540 #define INITIAL_TRAM_SHIFT 14
1541 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1543 static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1545 struct snd_pcm_substream *substream = private_data;
1546 snd_pcm_period_elapsed(substream);
1549 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1550 unsigned short *dst_right,
1551 unsigned short *src,
1553 unsigned int tram_shift)
1556 dev_dbg(emu->card->dev,
1557 "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
1558 "src = 0x%p, count = 0x%x\n",
1559 dst_left, dst_right, src, count);
1561 if ((tram_shift & 1) == 0) {
1563 *dst_left-- = *src++;
1564 *dst_right-- = *src++;
1568 *dst_right-- = *src++;
1569 *dst_left-- = *src++;
1574 static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1575 struct snd_pcm_indirect *rec, size_t bytes)
1577 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1578 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1579 unsigned int tram_size = pcm->buffer_size;
1580 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1581 unsigned int frames = bytes >> 2, count;
1582 unsigned int tram_pos = pcm->tram_pos;
1583 unsigned int tram_shift = pcm->tram_shift;
1585 while (frames > tram_pos) {
1586 count = tram_pos + 1;
1587 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1588 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1589 src, count, tram_shift);
1592 tram_pos = (tram_size / 2) - 1;
1595 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1596 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1597 src, frames, tram_shift);
1599 pcm->tram_pos = tram_pos;
1600 pcm->tram_shift = tram_shift;
1603 static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1605 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1606 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1608 return snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec,
1609 fx8010_pb_trans_copy);
1612 static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1614 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1615 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1618 for (i = 0; i < pcm->channels; i++)
1619 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1623 static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1625 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1626 struct snd_pcm_runtime *runtime = substream->runtime;
1627 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1631 dev_dbg(emu->card->dev, "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
1632 "buffer_size = 0x%x (0x%x)\n",
1633 emu->fx8010.etram_pages, runtime->dma_area,
1634 runtime->buffer_size, runtime->buffer_size << 2);
1636 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1637 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1638 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1639 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1640 pcm->tram_shift = 0;
1641 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
1642 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
1643 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1644 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
1645 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1646 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1647 for (i = 0; i < pcm->channels; i++)
1648 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1652 static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1654 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1655 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1658 spin_lock(&emu->reg_lock);
1660 case SNDRV_PCM_TRIGGER_START:
1662 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1663 case SNDRV_PCM_TRIGGER_RESUME:
1664 #ifdef EMU10K1_SET_AC3_IEC958
1667 for (i = 0; i < 3; i++) {
1669 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1670 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1671 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1672 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1676 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1679 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1680 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1682 case SNDRV_PCM_TRIGGER_STOP:
1683 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1684 case SNDRV_PCM_TRIGGER_SUSPEND:
1685 snd_emu10k1_fx8010_unregister_irq_handler(emu, &pcm->irq);
1686 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1687 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1688 pcm->tram_shift = 0;
1695 spin_unlock(&emu->reg_lock);
1699 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1701 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1702 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1703 size_t ptr; /* byte pointer */
1705 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1707 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1708 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1711 static const struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1713 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1714 SNDRV_PCM_INFO_RESUME |
1715 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE |
1716 SNDRV_PCM_INFO_SYNC_APPLPTR),
1717 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1718 .rates = SNDRV_PCM_RATE_48000,
1723 .buffer_bytes_max = (128*1024),
1724 .period_bytes_min = 1024,
1725 .period_bytes_max = (128*1024),
1727 .periods_max = 1024,
1731 static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1733 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1734 struct snd_pcm_runtime *runtime = substream->runtime;
1735 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1737 runtime->hw = snd_emu10k1_fx8010_playback;
1738 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1739 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1740 spin_lock_irq(&emu->reg_lock);
1741 if (pcm->valid == 0) {
1742 spin_unlock_irq(&emu->reg_lock);
1746 spin_unlock_irq(&emu->reg_lock);
1750 static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1752 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1753 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1755 spin_lock_irq(&emu->reg_lock);
1757 spin_unlock_irq(&emu->reg_lock);
1761 static const struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1762 .open = snd_emu10k1_fx8010_playback_open,
1763 .close = snd_emu10k1_fx8010_playback_close,
1764 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1765 .prepare = snd_emu10k1_fx8010_playback_prepare,
1766 .trigger = snd_emu10k1_fx8010_playback_trigger,
1767 .pointer = snd_emu10k1_fx8010_playback_pointer,
1768 .ack = snd_emu10k1_fx8010_playback_transfer,
1771 int snd_emu10k1_pcm_efx(struct snd_emu10k1 *emu, int device)
1773 struct snd_pcm *pcm;
1774 struct snd_kcontrol *kctl;
1777 if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1780 pcm->private_data = emu;
1782 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1783 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1785 pcm->info_flags = 0;
1786 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1789 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1793 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1795 emu->efx_voices_mask[0] = 0;
1796 if (emu->card_capabilities->emu_model)
1797 /* Pavel Hofman - 32 voices will be used for
1798 * capture (write mode) -
1799 * each bit = corresponding voice
1801 emu->efx_voices_mask[1] = 0xffffffff;
1803 emu->efx_voices_mask[1] = 0xffff;
1805 emu->efx_voices_mask[0] = 0xffff0000;
1806 emu->efx_voices_mask[1] = 0;
1808 /* For emu1010, the control has to set 32 upper bits (voices)
1809 * out of the 64 bits (voices) to true for the 16-channels capture
1810 * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1811 * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1812 * control can override this register's value.
1814 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1817 kctl->id.device = device;
1818 err = snd_ctl_add(emu->card, kctl);
1822 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,