3 * linux/drivers/sound/dmasound/dmasound_paula.c
5 * Amiga `Paula' DMA Sound Driver
7 * See linux/drivers/sound/dmasound/dmasound_core.c for copyright and credits
11 #include <linux/module.h>
12 #include <linux/config.h>
14 #include <linux/init.h>
15 #include <linux/ioport.h>
16 #include <linux/soundcard.h>
18 #include <asm/uaccess.h>
19 #include <asm/setup.h>
20 #include <asm/amigahw.h>
21 #include <asm/amigaints.h>
22 #include <asm/machdep.h>
28 * The minimum period for audio depends on htotal (for OCS/ECS/AGA)
29 * (Imported from arch/m68k/amiga/amisound.c)
32 extern volatile u_short amiga_audio_min_period
;
36 * amiga_mksound() should be able to restore the period after beeping
37 * (Imported from arch/m68k/amiga/amisound.c)
40 extern u_short amiga_audio_period
;
47 #define AMI_AUDIO_OFF (DMAF_AUD0 | DMAF_AUD1 | DMAF_AUD2 | DMAF_AUD3)
48 #define AMI_AUDIO_8 (DMAF_SETCLR | DMAF_MASTER | DMAF_AUD0 | DMAF_AUD1)
49 #define AMI_AUDIO_14 (AMI_AUDIO_8 | DMAF_AUD2 | DMAF_AUD3)
53 * Helper pointers for 16(14)-bit sound
56 static int write_sq_block_size_half
, write_sq_block_size_quarter
;
59 /*** Low level stuff *********************************************************/
62 static void AmiOpen(void);
63 static void AmiRelease(void);
64 static void *AmiAlloc(unsigned int size
, int flags
);
65 static void AmiFree(void *obj
, unsigned int size
);
66 static int AmiIrqInit(void);
68 static void AmiIrqCleanUp(void);
70 static void AmiSilence(void);
71 static void AmiInit(void);
72 static int AmiSetFormat(int format
);
73 static int AmiSetVolume(int volume
);
74 static int AmiSetTreble(int treble
);
75 static void AmiPlayNextFrame(int index
);
76 static void AmiPlay(void);
77 static void AmiInterrupt(int irq
, void *dummy
, struct pt_regs
*fp
);
79 #ifdef CONFIG_HEARTBEAT
82 * Heartbeat interferes with sound since the 7 kHz low-pass filter and the
83 * power LED are controlled by the same line.
87 #define mach_heartbeat ppc_md.heartbeat
90 static void (*saved_heartbeat
)(int) = NULL
;
92 static inline void disable_heartbeat(void)
95 saved_heartbeat
= mach_heartbeat
;
96 mach_heartbeat
= NULL
;
98 AmiSetTreble(dmasound
.treble
);
101 static inline void enable_heartbeat(void)
104 mach_heartbeat
= saved_heartbeat
;
106 #else /* !CONFIG_HEARTBEAT */
107 #define disable_heartbeat() do { } while (0)
108 #define enable_heartbeat() do { } while (0)
109 #endif /* !CONFIG_HEARTBEAT */
112 /*** Mid level stuff *********************************************************/
114 static void AmiMixerInit(void);
115 static int AmiMixerIoctl(u_int cmd
, u_long arg
);
116 static void AmiWriteSqSetup(void);
117 static int AmiStateInfo(char *buffer
);
120 /*** Translations ************************************************************/
122 /* ++TeSche: radically changed for new expanding purposes...
124 * These two routines now deal with copying/expanding/translating the samples
125 * from user space into our buffer at the right frequency. They take care about
126 * how much data there's actually to read, how much buffer space there is and
127 * to convert samples into the right frequency/encoding. They will only work on
128 * complete samples so it may happen they leave some bytes in the input stream
129 * if the user didn't write a multiple of the current sample size. They both
130 * return the number of bytes they've used from both streams so you may detect
131 * such a situation. Luckily all programs should be able to cope with that.
133 * I think I've optimized anything as far as one can do in plain C, all
134 * variables should fit in registers and the loops are really short. There's
135 * one loop for every possible situation. Writing a more generalized and thus
136 * parameterized loop would only produce slower code. Feel free to optimize
137 * this in assembler if you like. :)
139 * I think these routines belong here because they're not yet really hardware
140 * independent, especially the fact that the Falcon can play 16bit samples
141 * only in stereo is hardcoded in both of them!
143 * ++geert: split in even more functions (one per format)
151 static ssize_t
ami_ct_s8(const u_char
*userPtr
, size_t userCount
,
152 u_char frame
[], ssize_t
*frameUsed
, ssize_t frameLeft
)
156 if (!dmasound
.soft
.stereo
) {
157 void *p
= &frame
[*frameUsed
];
158 count
= min(userCount
, frameLeft
) & ~1;
160 if (copy_from_user(p
, userPtr
, count
))
163 u_char
*left
= &frame
[*frameUsed
>>1];
164 u_char
*right
= left
+write_sq_block_size_half
;
165 count
= min(userCount
, frameLeft
)>>1 & ~1;
168 if (get_user(*left
++, userPtr
++)
169 || get_user(*right
++, userPtr
++))
180 * Copy and convert 8 bit data
183 #define GENERATE_AMI_CT8(funcname, convsample) \
184 static ssize_t funcname(const u_char *userPtr, size_t userCount, \
185 u_char frame[], ssize_t *frameUsed, \
188 ssize_t count, used; \
190 if (!dmasound.soft.stereo) { \
191 u_char *p = &frame[*frameUsed]; \
192 count = min(userCount, frameLeft) & ~1; \
194 while (count > 0) { \
196 if (get_user(data, userPtr++)) \
198 *p++ = convsample(data); \
202 u_char *left = &frame[*frameUsed>>1]; \
203 u_char *right = left+write_sq_block_size_half; \
204 count = min(userCount, frameLeft)>>1 & ~1; \
206 while (count > 0) { \
208 if (get_user(data, userPtr++)) \
210 *left++ = convsample(data); \
211 if (get_user(data, userPtr++)) \
213 *right++ = convsample(data); \
217 *frameUsed += used; \
221 #define AMI_CT_ULAW(x) (dmasound_ulaw2dma8[(x)])
222 #define AMI_CT_ALAW(x) (dmasound_alaw2dma8[(x)])
223 #define AMI_CT_U8(x) ((x) ^ 0x80)
225 GENERATE_AMI_CT8(ami_ct_ulaw
, AMI_CT_ULAW
)
226 GENERATE_AMI_CT8(ami_ct_alaw
, AMI_CT_ALAW
)
227 GENERATE_AMI_CT8(ami_ct_u8
, AMI_CT_U8
)
231 * Copy and convert 16 bit data
234 #define GENERATE_AMI_CT_16(funcname, convsample) \
235 static ssize_t funcname(const u_char *userPtr, size_t userCount, \
236 u_char frame[], ssize_t *frameUsed, \
239 ssize_t count, used; \
242 if (!dmasound.soft.stereo) { \
243 u_char *high = &frame[*frameUsed>>1]; \
244 u_char *low = high+write_sq_block_size_half; \
245 count = min(userCount, frameLeft)>>1 & ~1; \
247 while (count > 0) { \
248 if (get_user(data, ((u_short *)userPtr)++)) \
250 data = convsample(data); \
252 *low++ = (data>>2) & 0x3f; \
256 u_char *lefth = &frame[*frameUsed>>2]; \
257 u_char *leftl = lefth+write_sq_block_size_quarter; \
258 u_char *righth = lefth+write_sq_block_size_half; \
259 u_char *rightl = righth+write_sq_block_size_quarter; \
260 count = min(userCount, frameLeft)>>2 & ~1; \
262 while (count > 0) { \
263 if (get_user(data, ((u_short *)userPtr)++)) \
265 data = convsample(data); \
266 *lefth++ = data>>8; \
267 *leftl++ = (data>>2) & 0x3f; \
268 if (get_user(data, ((u_short *)userPtr)++)) \
270 data = convsample(data); \
271 *righth++ = data>>8; \
272 *rightl++ = (data>>2) & 0x3f; \
276 *frameUsed += used; \
280 #define AMI_CT_S16BE(x) (x)
281 #define AMI_CT_U16BE(x) ((x) ^ 0x8000)
282 #define AMI_CT_S16LE(x) (le2be16((x)))
283 #define AMI_CT_U16LE(x) (le2be16((x)) ^ 0x8000)
285 GENERATE_AMI_CT_16(ami_ct_s16be
, AMI_CT_S16BE
)
286 GENERATE_AMI_CT_16(ami_ct_u16be
, AMI_CT_U16BE
)
287 GENERATE_AMI_CT_16(ami_ct_s16le
, AMI_CT_S16LE
)
288 GENERATE_AMI_CT_16(ami_ct_u16le
, AMI_CT_U16LE
)
291 static TRANS transAmiga
= {
292 ct_ulaw
: ami_ct_ulaw
,
293 ct_alaw
: ami_ct_alaw
,
296 ct_s16be
: ami_ct_s16be
,
297 ct_u16be
: ami_ct_u16be
,
298 ct_s16le
: ami_ct_s16le
,
299 ct_u16le
: ami_ct_u16le
,
302 /*** Low level stuff *********************************************************/
305 static void AmiOpen(void)
310 static void AmiRelease(void)
315 static inline void StopDMA(void)
317 custom
.aud
[0].audvol
= custom
.aud
[1].audvol
= 0;
318 custom
.aud
[2].audvol
= custom
.aud
[3].audvol
= 0;
319 custom
.dmacon
= AMI_AUDIO_OFF
;
323 static void *AmiAlloc(unsigned int size
, int flags
)
325 return amiga_chip_alloc((long)size
, "dmasound [Paula]");
328 static void AmiFree(void *obj
, unsigned int size
)
330 amiga_chip_free (obj
);
333 static int __init
AmiIrqInit(void)
335 /* turn off DMA for audio channels */
338 /* Register interrupt handler. */
339 if (request_irq(IRQ_AMIGA_AUD0
, AmiInterrupt
, 0, "DMA sound",
346 static void AmiIrqCleanUp(void)
348 /* turn off DMA for audio channels */
350 /* release the interrupt */
351 free_irq(IRQ_AMIGA_AUD0
, AmiInterrupt
);
355 static void AmiSilence(void)
357 /* turn off DMA for audio channels */
362 static void AmiInit(void)
368 if (dmasound
.soft
.speed
)
369 period
= amiga_colorclock
/dmasound
.soft
.speed
-1;
371 period
= amiga_audio_min_period
;
372 dmasound
.hard
= dmasound
.soft
;
373 dmasound
.trans_write
= &transAmiga
;
375 if (period
< amiga_audio_min_period
) {
376 /* we would need to squeeze the sound, but we won't do that */
377 period
= amiga_audio_min_period
;
378 } else if (period
> 65535) {
381 dmasound
.hard
.speed
= amiga_colorclock
/(period
+1);
383 for (i
= 0; i
< 4; i
++)
384 custom
.aud
[i
].audper
= period
;
385 amiga_audio_period
= period
;
389 static int AmiSetFormat(int format
)
393 /* Amiga sound DMA supports 8bit and 16bit (pseudo 14 bit) modes */
397 return dmasound
.soft
.format
;
415 dmasound
.soft
.format
= format
;
416 dmasound
.soft
.size
= size
;
417 if (dmasound
.minDev
== SND_DEV_DSP
) {
418 dmasound
.dsp
.format
= format
;
419 dmasound
.dsp
.size
= dmasound
.soft
.size
;
427 #define VOLUME_VOXWARE_TO_AMI(v) \
428 (((v) < 0) ? 0 : ((v) > 100) ? 64 : ((v) * 64)/100)
429 #define VOLUME_AMI_TO_VOXWARE(v) ((v)*100/64)
431 static int AmiSetVolume(int volume
)
433 dmasound
.volume_left
= VOLUME_VOXWARE_TO_AMI(volume
& 0xff);
434 custom
.aud
[0].audvol
= dmasound
.volume_left
;
435 dmasound
.volume_right
= VOLUME_VOXWARE_TO_AMI((volume
& 0xff00) >> 8);
436 custom
.aud
[1].audvol
= dmasound
.volume_right
;
437 if (dmasound
.hard
.size
== 16) {
438 if (dmasound
.volume_left
== 64 && dmasound
.volume_right
== 64) {
439 custom
.aud
[2].audvol
= 1;
440 custom
.aud
[3].audvol
= 1;
442 custom
.aud
[2].audvol
= 0;
443 custom
.aud
[3].audvol
= 0;
446 return VOLUME_AMI_TO_VOXWARE(dmasound
.volume_left
) |
447 (VOLUME_AMI_TO_VOXWARE(dmasound
.volume_right
) << 8);
450 static int AmiSetTreble(int treble
)
452 dmasound
.treble
= treble
;
461 #define AMI_PLAY_LOADED 1
462 #define AMI_PLAY_PLAYING 2
463 #define AMI_PLAY_MASK 3
466 static void AmiPlayNextFrame(int index
)
468 u_char
*start
, *ch0
, *ch1
, *ch2
, *ch3
;
471 /* used by AmiPlay() if all doubts whether there really is something
472 * to be played are already wiped out.
474 start
= write_sq
.buffers
[write_sq
.front
];
475 size
= (write_sq
.count
== index
? write_sq
.rear_size
476 : write_sq
.block_size
)>>1;
478 if (dmasound
.hard
.stereo
) {
480 ch1
= start
+write_sq_block_size_half
;
488 custom
.aud
[0].audvol
= dmasound
.volume_left
;
489 custom
.aud
[1].audvol
= dmasound
.volume_right
;
490 if (dmasound
.hard
.size
== 8) {
491 custom
.aud
[0].audlc
= (u_short
*)ZTWO_PADDR(ch0
);
492 custom
.aud
[0].audlen
= size
;
493 custom
.aud
[1].audlc
= (u_short
*)ZTWO_PADDR(ch1
);
494 custom
.aud
[1].audlen
= size
;
495 custom
.dmacon
= AMI_AUDIO_8
;
498 custom
.aud
[0].audlc
= (u_short
*)ZTWO_PADDR(ch0
);
499 custom
.aud
[0].audlen
= size
;
500 custom
.aud
[1].audlc
= (u_short
*)ZTWO_PADDR(ch1
);
501 custom
.aud
[1].audlen
= size
;
502 if (dmasound
.volume_left
== 64 && dmasound
.volume_right
== 64) {
503 /* We can play pseudo 14-bit only with the maximum volume */
504 ch3
= ch0
+write_sq_block_size_quarter
;
505 ch2
= ch1
+write_sq_block_size_quarter
;
506 custom
.aud
[2].audvol
= 1; /* we are being affected by the beeps */
507 custom
.aud
[3].audvol
= 1; /* restoring volume here helps a bit */
508 custom
.aud
[2].audlc
= (u_short
*)ZTWO_PADDR(ch2
);
509 custom
.aud
[2].audlen
= size
;
510 custom
.aud
[3].audlc
= (u_short
*)ZTWO_PADDR(ch3
);
511 custom
.aud
[3].audlen
= size
;
512 custom
.dmacon
= AMI_AUDIO_14
;
514 custom
.aud
[2].audvol
= 0;
515 custom
.aud
[3].audvol
= 0;
516 custom
.dmacon
= AMI_AUDIO_8
;
519 write_sq
.front
= (write_sq
.front
+1) % write_sq
.max_count
;
520 write_sq
.active
|= AMI_PLAY_LOADED
;
524 static void AmiPlay(void)
528 custom
.intena
= IF_AUD0
;
530 if (write_sq
.active
& AMI_PLAY_LOADED
) {
531 /* There's already a frame loaded */
532 custom
.intena
= IF_SETCLR
| IF_AUD0
;
536 if (write_sq
.active
& AMI_PLAY_PLAYING
)
537 /* Increase threshold: frame 1 is already being played */
540 if (write_sq
.count
< minframes
) {
542 custom
.intena
= IF_SETCLR
| IF_AUD0
;
546 if (write_sq
.count
<= minframes
&&
547 write_sq
.rear_size
< write_sq
.block_size
&& !write_sq
.syncing
) {
548 /* hmmm, the only existing frame is not
549 * yet filled and we're not syncing?
551 custom
.intena
= IF_SETCLR
| IF_AUD0
;
555 AmiPlayNextFrame(minframes
);
557 custom
.intena
= IF_SETCLR
| IF_AUD0
;
561 static void AmiInterrupt(int irq
, void *dummy
, struct pt_regs
*fp
)
565 custom
.intena
= IF_AUD0
;
567 if (!write_sq
.active
) {
568 /* Playing was interrupted and sq_reset() has already cleared
569 * the sq variables, so better don't do anything here.
571 WAKE_UP(write_sq
.sync_queue
);
575 if (write_sq
.active
& AMI_PLAY_PLAYING
) {
576 /* We've just finished a frame */
578 WAKE_UP(write_sq
.action_queue
);
581 if (write_sq
.active
& AMI_PLAY_LOADED
)
582 /* Increase threshold: frame 1 is already being played */
585 /* Shift the flags */
586 write_sq
.active
= (write_sq
.active
<<1) & AMI_PLAY_MASK
;
588 if (!write_sq
.active
)
589 /* No frame is playing, disable audio DMA */
592 custom
.intena
= IF_SETCLR
| IF_AUD0
;
594 if (write_sq
.count
>= minframes
)
595 /* Try to play the next frame */
598 if (!write_sq
.active
)
599 /* Nothing to play anymore.
600 Wake up a process waiting for audio output to drain. */
601 WAKE_UP(write_sq
.sync_queue
);
604 /*** Mid level stuff *********************************************************/
608 * /dev/mixer abstraction
611 static void __init
AmiMixerInit(void)
613 dmasound
.volume_left
= 64;
614 dmasound
.volume_right
= 64;
615 custom
.aud
[0].audvol
= dmasound
.volume_left
;
616 custom
.aud
[3].audvol
= 1; /* For pseudo 14bit */
617 custom
.aud
[1].audvol
= dmasound
.volume_right
;
618 custom
.aud
[2].audvol
= 1; /* For pseudo 14bit */
619 dmasound
.treble
= 50;
622 static int AmiMixerIoctl(u_int cmd
, u_long arg
)
626 case SOUND_MIXER_READ_DEVMASK
:
627 return IOCTL_OUT(arg
, SOUND_MASK_VOLUME
| SOUND_MASK_TREBLE
);
628 case SOUND_MIXER_READ_RECMASK
:
629 return IOCTL_OUT(arg
, 0);
630 case SOUND_MIXER_READ_STEREODEVS
:
631 return IOCTL_OUT(arg
, SOUND_MASK_VOLUME
);
632 case SOUND_MIXER_READ_VOLUME
:
633 return IOCTL_OUT(arg
,
634 VOLUME_AMI_TO_VOXWARE(dmasound
.volume_left
) |
635 VOLUME_AMI_TO_VOXWARE(dmasound
.volume_right
) << 8);
636 case SOUND_MIXER_WRITE_VOLUME
:
638 return IOCTL_OUT(arg
, dmasound_set_volume(data
));
639 case SOUND_MIXER_READ_TREBLE
:
640 return IOCTL_OUT(arg
, dmasound
.treble
);
641 case SOUND_MIXER_WRITE_TREBLE
:
643 return IOCTL_OUT(arg
, dmasound_set_treble(data
));
649 static void AmiWriteSqSetup(void)
651 write_sq_block_size_half
= write_sq
.block_size
>>1;
652 write_sq_block_size_quarter
= write_sq_block_size_half
>>1;
656 static int AmiStateInfo(char *buffer
)
659 len
+= sprintf(buffer
+len
, "\tsound.volume_left = %d [0...64]\n",
660 dmasound
.volume_left
);
661 len
+= sprintf(buffer
+len
, "\tsound.volume_right = %d [0...64]\n",
662 dmasound
.volume_right
);
667 /*** Machine definitions *****************************************************/
670 static MACHINE machAmiga
= {
679 irqcleanup
: AmiIrqCleanUp
,
683 setFormat
: AmiSetFormat
,
684 setVolume
: AmiSetVolume
,
685 setTreble
: AmiSetTreble
,
687 mixer_init
: AmiMixerInit
,
688 mixer_ioctl
: AmiMixerIoctl
,
689 write_sq_setup
: AmiWriteSqSetup
,
690 state_info
: AmiStateInfo
,
695 /*** Config & Setup **********************************************************/
698 int __init
dmasound_paula_init(void)
702 if (MACH_IS_AMIGA
&& AMIGAHW_PRESENT(AMI_AUDIO
)) {
703 if (!request_mem_region(CUSTOM_PHYSADDR
+0xa0, 0x40,
706 dmasound
.mach
= machAmiga
;
707 err
= dmasound_init();
709 release_mem_region(CUSTOM_PHYSADDR
+0xa0, 0x40);
715 static void __exit
dmasound_paula_cleanup(void)
718 release_mem_region(CUSTOM_PHYSADDR
+0xa0, 0x40);
721 module_init(dmasound_paula_init
);
722 module_exit(dmasound_paula_cleanup
);