1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/sound/oss/dmasound/dmasound_paula.c
5 * Amiga `Paula' DMA Sound Driver
7 * See linux/sound/oss/dmasound/dmasound_core.c for copyright and credits
10 * 28/01/2001 [0.1] Iain Sandoe
12 * - put in and populated the hardware_afmts field.
13 * [0.2] - put in SNDCTL_DSP_GETCAPS value.
14 * [0.3] - put in constraint on state buffer usage.
15 * [0.4] - put in default hard/soft settings
19 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/ioport.h>
23 #include <linux/soundcard.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
27 #include <linux/uaccess.h>
28 #include <asm/setup.h>
29 #include <asm/amigahw.h>
30 #include <asm/amigaints.h>
31 #include <asm/machdep.h>
35 #define DMASOUND_PAULA_REVISION 0
36 #define DMASOUND_PAULA_EDITION 4
38 #define custom amiga_custom
40 * The minimum period for audio depends on htotal (for OCS/ECS/AGA)
41 * (Imported from arch/m68k/amiga/amisound.c)
44 extern volatile u_short amiga_audio_min_period;
48 * amiga_mksound() should be able to restore the period after beeping
49 * (Imported from arch/m68k/amiga/amisound.c)
52 extern u_short amiga_audio_period;
59 #define AMI_AUDIO_OFF (DMAF_AUD0 | DMAF_AUD1 | DMAF_AUD2 | DMAF_AUD3)
60 #define AMI_AUDIO_8 (DMAF_SETCLR | DMAF_MASTER | DMAF_AUD0 | DMAF_AUD1)
61 #define AMI_AUDIO_14 (AMI_AUDIO_8 | DMAF_AUD2 | DMAF_AUD3)
65 * Helper pointers for 16(14)-bit sound
68 static int write_sq_block_size_half, write_sq_block_size_quarter;
71 /*** Low level stuff *********************************************************/
74 static void *AmiAlloc(unsigned int size, gfp_t flags);
75 static void AmiFree(void *obj, unsigned int size);
76 static int AmiIrqInit(void);
78 static void AmiIrqCleanUp(void);
80 static void AmiSilence(void);
81 static void AmiInit(void);
82 static int AmiSetFormat(int format);
83 static int AmiSetVolume(int volume);
84 static int AmiSetTreble(int treble);
85 static void AmiPlayNextFrame(int index);
86 static void AmiPlay(void);
87 static irqreturn_t AmiInterrupt(int irq, void *dummy);
89 #ifdef CONFIG_HEARTBEAT
92 * Heartbeat interferes with sound since the 7 kHz low-pass filter and the
93 * power LED are controlled by the same line.
96 static void (*saved_heartbeat)(int) = NULL;
98 static inline void disable_heartbeat(void)
100 if (mach_heartbeat) {
101 saved_heartbeat = mach_heartbeat;
102 mach_heartbeat = NULL;
104 AmiSetTreble(dmasound.treble);
107 static inline void enable_heartbeat(void)
110 mach_heartbeat = saved_heartbeat;
112 #else /* !CONFIG_HEARTBEAT */
113 #define disable_heartbeat() do { } while (0)
114 #define enable_heartbeat() do { } while (0)
115 #endif /* !CONFIG_HEARTBEAT */
118 /*** Mid level stuff *********************************************************/
120 static void AmiMixerInit(void);
121 static int AmiMixerIoctl(u_int cmd, u_long arg);
122 static int AmiWriteSqSetup(void);
123 static int AmiStateInfo(char *buffer, size_t space);
126 /*** Translations ************************************************************/
128 /* ++TeSche: radically changed for new expanding purposes...
130 * These two routines now deal with copying/expanding/translating the samples
131 * from user space into our buffer at the right frequency. They take care about
132 * how much data there's actually to read, how much buffer space there is and
133 * to convert samples into the right frequency/encoding. They will only work on
134 * complete samples so it may happen they leave some bytes in the input stream
135 * if the user didn't write a multiple of the current sample size. They both
136 * return the number of bytes they've used from both streams so you may detect
137 * such a situation. Luckily all programs should be able to cope with that.
139 * I think I've optimized anything as far as one can do in plain C, all
140 * variables should fit in registers and the loops are really short. There's
141 * one loop for every possible situation. Writing a more generalized and thus
142 * parameterized loop would only produce slower code. Feel free to optimize
143 * this in assembler if you like. :)
145 * I think these routines belong here because they're not yet really hardware
146 * independent, especially the fact that the Falcon can play 16bit samples
147 * only in stereo is hardcoded in both of them!
149 * ++geert: split in even more functions (one per format)
157 static ssize_t ami_ct_s8(const u_char __user *userPtr, size_t userCount,
158 u_char frame[], ssize_t *frameUsed, ssize_t frameLeft)
162 if (!dmasound.soft.stereo) {
163 void *p = &frame[*frameUsed];
164 count = min_t(unsigned long, userCount, frameLeft) & ~1;
166 if (copy_from_user(p, userPtr, count))
169 u_char *left = &frame[*frameUsed>>1];
170 u_char *right = left+write_sq_block_size_half;
171 count = min_t(unsigned long, userCount, frameLeft)>>1 & ~1;
174 if (get_user(*left++, userPtr++)
175 || get_user(*right++, userPtr++))
186 * Copy and convert 8 bit data
189 #define GENERATE_AMI_CT8(funcname, convsample) \
190 static ssize_t funcname(const u_char __user *userPtr, size_t userCount, \
191 u_char frame[], ssize_t *frameUsed, \
194 ssize_t count, used; \
196 if (!dmasound.soft.stereo) { \
197 u_char *p = &frame[*frameUsed]; \
198 count = min_t(size_t, userCount, frameLeft) & ~1; \
200 while (count > 0) { \
202 if (get_user(data, userPtr++)) \
204 *p++ = convsample(data); \
208 u_char *left = &frame[*frameUsed>>1]; \
209 u_char *right = left+write_sq_block_size_half; \
210 count = min_t(size_t, userCount, frameLeft)>>1 & ~1; \
212 while (count > 0) { \
214 if (get_user(data, userPtr++)) \
216 *left++ = convsample(data); \
217 if (get_user(data, userPtr++)) \
219 *right++ = convsample(data); \
223 *frameUsed += used; \
227 #define AMI_CT_ULAW(x) (dmasound_ulaw2dma8[(x)])
228 #define AMI_CT_ALAW(x) (dmasound_alaw2dma8[(x)])
229 #define AMI_CT_U8(x) ((x) ^ 0x80)
231 GENERATE_AMI_CT8(ami_ct_ulaw, AMI_CT_ULAW)
232 GENERATE_AMI_CT8(ami_ct_alaw, AMI_CT_ALAW)
233 GENERATE_AMI_CT8(ami_ct_u8, AMI_CT_U8)
237 * Copy and convert 16 bit data
240 #define GENERATE_AMI_CT_16(funcname, convsample) \
241 static ssize_t funcname(const u_char __user *userPtr, size_t userCount, \
242 u_char frame[], ssize_t *frameUsed, \
245 const u_short __user *ptr = (const u_short __user *)userPtr; \
246 ssize_t count, used; \
249 if (!dmasound.soft.stereo) { \
250 u_char *high = &frame[*frameUsed>>1]; \
251 u_char *low = high+write_sq_block_size_half; \
252 count = min_t(size_t, userCount, frameLeft)>>1 & ~1; \
254 while (count > 0) { \
255 if (get_user(data, ptr++)) \
257 data = convsample(data); \
259 *low++ = (data>>2) & 0x3f; \
263 u_char *lefth = &frame[*frameUsed>>2]; \
264 u_char *leftl = lefth+write_sq_block_size_quarter; \
265 u_char *righth = lefth+write_sq_block_size_half; \
266 u_char *rightl = righth+write_sq_block_size_quarter; \
267 count = min_t(size_t, userCount, frameLeft)>>2 & ~1; \
269 while (count > 0) { \
270 if (get_user(data, ptr++)) \
272 data = convsample(data); \
273 *lefth++ = data>>8; \
274 *leftl++ = (data>>2) & 0x3f; \
275 if (get_user(data, ptr++)) \
277 data = convsample(data); \
278 *righth++ = data>>8; \
279 *rightl++ = (data>>2) & 0x3f; \
283 *frameUsed += used; \
287 #define AMI_CT_S16BE(x) (x)
288 #define AMI_CT_U16BE(x) ((x) ^ 0x8000)
289 #define AMI_CT_S16LE(x) (le2be16((x)))
290 #define AMI_CT_U16LE(x) (le2be16((x)) ^ 0x8000)
292 GENERATE_AMI_CT_16(ami_ct_s16be, AMI_CT_S16BE)
293 GENERATE_AMI_CT_16(ami_ct_u16be, AMI_CT_U16BE)
294 GENERATE_AMI_CT_16(ami_ct_s16le, AMI_CT_S16LE)
295 GENERATE_AMI_CT_16(ami_ct_u16le, AMI_CT_U16LE)
298 static TRANS transAmiga = {
299 .ct_ulaw = ami_ct_ulaw,
300 .ct_alaw = ami_ct_alaw,
303 .ct_s16be = ami_ct_s16be,
304 .ct_u16be = ami_ct_u16be,
305 .ct_s16le = ami_ct_s16le,
306 .ct_u16le = ami_ct_u16le,
309 /*** Low level stuff *********************************************************/
311 static inline void StopDMA(void)
313 custom.aud[0].audvol = custom.aud[1].audvol = 0;
314 custom.aud[2].audvol = custom.aud[3].audvol = 0;
315 custom.dmacon = AMI_AUDIO_OFF;
319 static void *AmiAlloc(unsigned int size, gfp_t flags)
321 return amiga_chip_alloc((long)size, "dmasound [Paula]");
324 static void AmiFree(void *obj, unsigned int size)
326 amiga_chip_free (obj);
329 static int __init AmiIrqInit(void)
331 /* turn off DMA for audio channels */
334 /* Register interrupt handler. */
335 if (request_irq(IRQ_AMIGA_AUD0, AmiInterrupt, 0, "DMA sound",
342 static void AmiIrqCleanUp(void)
344 /* turn off DMA for audio channels */
346 /* release the interrupt */
347 free_irq(IRQ_AMIGA_AUD0, AmiInterrupt);
351 static void AmiSilence(void)
353 /* turn off DMA for audio channels */
358 static void AmiInit(void)
364 if (dmasound.soft.speed)
365 period = amiga_colorclock/dmasound.soft.speed-1;
367 period = amiga_audio_min_period;
368 dmasound.hard = dmasound.soft;
369 dmasound.trans_write = &transAmiga;
371 if (period < amiga_audio_min_period) {
372 /* we would need to squeeze the sound, but we won't do that */
373 period = amiga_audio_min_period;
374 } else if (period > 65535) {
377 dmasound.hard.speed = amiga_colorclock/(period+1);
379 for (i = 0; i < 4; i++)
380 custom.aud[i].audper = period;
381 amiga_audio_period = period;
385 static int AmiSetFormat(int format)
389 /* Amiga sound DMA supports 8bit and 16bit (pseudo 14 bit) modes */
393 return dmasound.soft.format;
411 dmasound.soft.format = format;
412 dmasound.soft.size = size;
413 if (dmasound.minDev == SND_DEV_DSP) {
414 dmasound.dsp.format = format;
415 dmasound.dsp.size = dmasound.soft.size;
423 #define VOLUME_VOXWARE_TO_AMI(v) \
424 (((v) < 0) ? 0 : ((v) > 100) ? 64 : ((v) * 64)/100)
425 #define VOLUME_AMI_TO_VOXWARE(v) ((v)*100/64)
427 static int AmiSetVolume(int volume)
429 dmasound.volume_left = VOLUME_VOXWARE_TO_AMI(volume & 0xff);
430 custom.aud[0].audvol = dmasound.volume_left;
431 dmasound.volume_right = VOLUME_VOXWARE_TO_AMI((volume & 0xff00) >> 8);
432 custom.aud[1].audvol = dmasound.volume_right;
433 if (dmasound.hard.size == 16) {
434 if (dmasound.volume_left == 64 && dmasound.volume_right == 64) {
435 custom.aud[2].audvol = 1;
436 custom.aud[3].audvol = 1;
438 custom.aud[2].audvol = 0;
439 custom.aud[3].audvol = 0;
442 return VOLUME_AMI_TO_VOXWARE(dmasound.volume_left) |
443 (VOLUME_AMI_TO_VOXWARE(dmasound.volume_right) << 8);
446 static int AmiSetTreble(int treble)
448 dmasound.treble = treble;
457 #define AMI_PLAY_LOADED 1
458 #define AMI_PLAY_PLAYING 2
459 #define AMI_PLAY_MASK 3
462 static void AmiPlayNextFrame(int index)
464 u_char *start, *ch0, *ch1, *ch2, *ch3;
467 /* used by AmiPlay() if all doubts whether there really is something
468 * to be played are already wiped out.
470 start = write_sq.buffers[write_sq.front];
471 size = (write_sq.count == index ? write_sq.rear_size
472 : write_sq.block_size)>>1;
474 if (dmasound.hard.stereo) {
476 ch1 = start+write_sq_block_size_half;
484 custom.aud[0].audvol = dmasound.volume_left;
485 custom.aud[1].audvol = dmasound.volume_right;
486 if (dmasound.hard.size == 8) {
487 custom.aud[0].audlc = (u_short *)ZTWO_PADDR(ch0);
488 custom.aud[0].audlen = size;
489 custom.aud[1].audlc = (u_short *)ZTWO_PADDR(ch1);
490 custom.aud[1].audlen = size;
491 custom.dmacon = AMI_AUDIO_8;
494 custom.aud[0].audlc = (u_short *)ZTWO_PADDR(ch0);
495 custom.aud[0].audlen = size;
496 custom.aud[1].audlc = (u_short *)ZTWO_PADDR(ch1);
497 custom.aud[1].audlen = size;
498 if (dmasound.volume_left == 64 && dmasound.volume_right == 64) {
499 /* We can play pseudo 14-bit only with the maximum volume */
500 ch3 = ch0+write_sq_block_size_quarter;
501 ch2 = ch1+write_sq_block_size_quarter;
502 custom.aud[2].audvol = 1; /* we are being affected by the beeps */
503 custom.aud[3].audvol = 1; /* restoring volume here helps a bit */
504 custom.aud[2].audlc = (u_short *)ZTWO_PADDR(ch2);
505 custom.aud[2].audlen = size;
506 custom.aud[3].audlc = (u_short *)ZTWO_PADDR(ch3);
507 custom.aud[3].audlen = size;
508 custom.dmacon = AMI_AUDIO_14;
510 custom.aud[2].audvol = 0;
511 custom.aud[3].audvol = 0;
512 custom.dmacon = AMI_AUDIO_8;
515 write_sq.front = (write_sq.front+1) % write_sq.max_count;
516 write_sq.active |= AMI_PLAY_LOADED;
520 static void AmiPlay(void)
524 custom.intena = IF_AUD0;
526 if (write_sq.active & AMI_PLAY_LOADED) {
527 /* There's already a frame loaded */
528 custom.intena = IF_SETCLR | IF_AUD0;
532 if (write_sq.active & AMI_PLAY_PLAYING)
533 /* Increase threshold: frame 1 is already being played */
536 if (write_sq.count < minframes) {
538 custom.intena = IF_SETCLR | IF_AUD0;
542 if (write_sq.count <= minframes &&
543 write_sq.rear_size < write_sq.block_size && !write_sq.syncing) {
544 /* hmmm, the only existing frame is not
545 * yet filled and we're not syncing?
547 custom.intena = IF_SETCLR | IF_AUD0;
551 AmiPlayNextFrame(minframes);
553 custom.intena = IF_SETCLR | IF_AUD0;
557 static irqreturn_t AmiInterrupt(int irq, void *dummy)
561 custom.intena = IF_AUD0;
563 if (!write_sq.active) {
564 /* Playing was interrupted and sq_reset() has already cleared
565 * the sq variables, so better don't do anything here.
567 WAKE_UP(write_sq.sync_queue);
571 if (write_sq.active & AMI_PLAY_PLAYING) {
572 /* We've just finished a frame */
574 WAKE_UP(write_sq.action_queue);
577 if (write_sq.active & AMI_PLAY_LOADED)
578 /* Increase threshold: frame 1 is already being played */
581 /* Shift the flags */
582 write_sq.active = (write_sq.active<<1) & AMI_PLAY_MASK;
584 if (!write_sq.active)
585 /* No frame is playing, disable audio DMA */
588 custom.intena = IF_SETCLR | IF_AUD0;
590 if (write_sq.count >= minframes)
591 /* Try to play the next frame */
594 if (!write_sq.active)
595 /* Nothing to play anymore.
596 Wake up a process waiting for audio output to drain. */
597 WAKE_UP(write_sq.sync_queue);
601 /*** Mid level stuff *********************************************************/
605 * /dev/mixer abstraction
608 static void __init AmiMixerInit(void)
610 dmasound.volume_left = 64;
611 dmasound.volume_right = 64;
612 custom.aud[0].audvol = dmasound.volume_left;
613 custom.aud[3].audvol = 1; /* For pseudo 14bit */
614 custom.aud[1].audvol = dmasound.volume_right;
615 custom.aud[2].audvol = 1; /* For pseudo 14bit */
616 dmasound.treble = 50;
619 static int AmiMixerIoctl(u_int cmd, u_long arg)
623 case SOUND_MIXER_READ_DEVMASK:
624 return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_TREBLE);
625 case SOUND_MIXER_READ_RECMASK:
626 return IOCTL_OUT(arg, 0);
627 case SOUND_MIXER_READ_STEREODEVS:
628 return IOCTL_OUT(arg, SOUND_MASK_VOLUME);
629 case SOUND_MIXER_READ_VOLUME:
630 return IOCTL_OUT(arg,
631 VOLUME_AMI_TO_VOXWARE(dmasound.volume_left) |
632 VOLUME_AMI_TO_VOXWARE(dmasound.volume_right) << 8);
633 case SOUND_MIXER_WRITE_VOLUME:
635 return IOCTL_OUT(arg, dmasound_set_volume(data));
636 case SOUND_MIXER_READ_TREBLE:
637 return IOCTL_OUT(arg, dmasound.treble);
638 case SOUND_MIXER_WRITE_TREBLE:
640 return IOCTL_OUT(arg, dmasound_set_treble(data));
646 static int AmiWriteSqSetup(void)
648 write_sq_block_size_half = write_sq.block_size>>1;
649 write_sq_block_size_quarter = write_sq_block_size_half>>1;
654 static int AmiStateInfo(char *buffer, size_t space)
657 len += sprintf(buffer+len, "\tsound.volume_left = %d [0...64]\n",
658 dmasound.volume_left);
659 len += sprintf(buffer+len, "\tsound.volume_right = %d [0...64]\n",
660 dmasound.volume_right);
662 printk(KERN_ERR "dmasound_paula: overflowed state buffer alloc.\n") ;
669 /*** Machine definitions *****************************************************/
671 static SETTINGS def_hard = {
678 static SETTINGS def_soft = {
685 static MACHINE machAmiga = {
688 .owner = THIS_MODULE,
689 .dma_alloc = AmiAlloc,
691 .irqinit = AmiIrqInit,
693 .irqcleanup = AmiIrqCleanUp,
696 .silence = AmiSilence,
697 .setFormat = AmiSetFormat,
698 .setVolume = AmiSetVolume,
699 .setTreble = AmiSetTreble,
701 .mixer_init = AmiMixerInit,
702 .mixer_ioctl = AmiMixerIoctl,
703 .write_sq_setup = AmiWriteSqSetup,
704 .state_info = AmiStateInfo,
705 .min_dsp_speed = 8000,
706 .version = ((DMASOUND_PAULA_REVISION<<8) | DMASOUND_PAULA_EDITION),
707 .hardware_afmts = (AFMT_S8 | AFMT_S16_BE), /* h'ware-supported formats *only* here */
708 .capabilities = DSP_CAP_BATCH /* As per SNDCTL_DSP_GETCAPS */
712 /*** Config & Setup **********************************************************/
715 static int __init amiga_audio_probe(struct platform_device *pdev)
717 dmasound.mach = machAmiga;
718 dmasound.mach.default_hard = def_hard ;
719 dmasound.mach.default_soft = def_soft ;
720 return dmasound_init();
723 static int __exit amiga_audio_remove(struct platform_device *pdev)
729 static struct platform_driver amiga_audio_driver = {
730 .remove = __exit_p(amiga_audio_remove),
732 .name = "amiga-audio",
736 module_platform_driver_probe(amiga_audio_driver, amiga_audio_probe);
738 MODULE_LICENSE("GPL");
739 MODULE_ALIAS("platform:amiga-audio");