11da177e4SLinus Torvalds/*
21da177e4SLinus Torvalds * linux/arch/m68k/amiga/amisound.c
31da177e4SLinus Torvalds *
41da177e4SLinus Torvalds * amiga sound driver for Linux/m68k
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds * This file is subject to the terms and conditions of the GNU General Public
71da177e4SLinus Torvalds * License.  See the file COPYING in the main directory of this archive
81da177e4SLinus Torvalds * for more details.
91da177e4SLinus Torvalds */
101da177e4SLinus Torvalds
111da177e4SLinus Torvalds#include <linux/jiffies.h>
121da177e4SLinus Torvalds#include <linux/timer.h>
131da177e4SLinus Torvalds#include <linux/init.h>
141da177e4SLinus Torvalds#include <linux/string.h>
158b169fa2SAdrian Bunk#include <linux/module.h>
161da177e4SLinus Torvalds
171da177e4SLinus Torvalds#include <asm/amigahw.h>
181da177e4SLinus Torvalds
191da177e4SLinus Torvaldsstatic unsigned short *snd_data;
201da177e4SLinus Torvaldsstatic const signed char sine_data[] = {
211da177e4SLinus Torvalds	0,  39,  75,  103,  121,  127,  121,  103,  75,  39,
221da177e4SLinus Torvalds	0, -39, -75, -103, -121, -127, -121, -103, -75, -39
231da177e4SLinus Torvalds};
24b9e5e119SAlejandro Martinez Ruiz#define DATA_SIZE	ARRAY_SIZE(sine_data)
251da177e4SLinus Torvalds
26b4290a23SAl Viro#define custom amiga_custom
27b4290a23SAl Viro
281da177e4SLinus Torvalds    /*
291da177e4SLinus Torvalds     * The minimum period for audio may be modified by the frame buffer
301da177e4SLinus Torvalds     * device since it depends on htotal (for OCS/ECS/AGA)
311da177e4SLinus Torvalds     */
321da177e4SLinus Torvalds
331da177e4SLinus Torvaldsvolatile unsigned short amiga_audio_min_period = 124; /* Default for pre-OCS */
348b169fa2SAdrian BunkEXPORT_SYMBOL(amiga_audio_min_period);
351da177e4SLinus Torvalds
361da177e4SLinus Torvalds#define MAX_PERIOD	(65535)
371da177e4SLinus Torvalds
381da177e4SLinus Torvalds
391da177e4SLinus Torvalds    /*
401da177e4SLinus Torvalds     *	Current period (set by dmasound.c)
411da177e4SLinus Torvalds     */
421da177e4SLinus Torvalds
431da177e4SLinus Torvaldsunsigned short amiga_audio_period = MAX_PERIOD;
448b169fa2SAdrian BunkEXPORT_SYMBOL(amiga_audio_period);
451da177e4SLinus Torvalds
461da177e4SLinus Torvaldsstatic unsigned long clock_constant;
471da177e4SLinus Torvalds
481da177e4SLinus Torvaldsvoid __init amiga_init_sound(void)
491da177e4SLinus Torvalds{
501da177e4SLinus Torvalds	static struct resource beep_res = { .name = "Beep" };
511da177e4SLinus Torvalds
521da177e4SLinus Torvalds	snd_data = amiga_chip_alloc_res(sizeof(sine_data), &beep_res);
531da177e4SLinus Torvalds	if (!snd_data) {
54f296401bSFabian Frederick		pr_crit("amiga init_sound: failed to allocate chipmem\n");
551da177e4SLinus Torvalds		return;
561da177e4SLinus Torvalds	}
571da177e4SLinus Torvalds	memcpy (snd_data, sine_data, sizeof(sine_data));
581da177e4SLinus Torvalds
591da177e4SLinus Torvalds	/* setup divisor */
601da177e4SLinus Torvalds	clock_constant = (amiga_colorclock+DATA_SIZE/2)/DATA_SIZE;
611da177e4SLinus Torvalds
621da177e4SLinus Torvalds	/* without amifb, turn video off and enable high quality sound */
631da177e4SLinus Torvalds#ifndef CONFIG_FB_AMIGA
641da177e4SLinus Torvalds	amifb_video_off();
651da177e4SLinus Torvalds#endif
661da177e4SLinus Torvalds}
671da177e4SLinus Torvalds
6824ed960aSKees Cookstatic void nosound(struct timer_list *unused);
691d27e3e2SKees Cookstatic DEFINE_TIMER(sound_timer, nosound);
701da177e4SLinus Torvalds
711da177e4SLinus Torvaldsvoid amiga_mksound( unsigned int hz, unsigned int ticks )
721da177e4SLinus Torvalds{
731da177e4SLinus Torvalds	unsigned long flags;
741da177e4SLinus Torvalds
751da177e4SLinus Torvalds	if (!snd_data)
761da177e4SLinus Torvalds		return;
771da177e4SLinus Torvalds
781da177e4SLinus Torvalds	local_irq_save(flags);
791da177e4SLinus Torvalds	del_timer( &sound_timer );
801da177e4SLinus Torvalds
811da177e4SLinus Torvalds	if (hz > 20 && hz < 32767) {
821da177e4SLinus Torvalds		unsigned long period = (clock_constant / hz);
831da177e4SLinus Torvalds
841da177e4SLinus Torvalds		if (period < amiga_audio_min_period)
851da177e4SLinus Torvalds			period = amiga_audio_min_period;
861da177e4SLinus Torvalds		if (period > MAX_PERIOD)
871da177e4SLinus Torvalds			period = MAX_PERIOD;
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds		/* setup pointer to data, period, length and volume */
901da177e4SLinus Torvalds		custom.aud[2].audlc = snd_data;
911da177e4SLinus Torvalds		custom.aud[2].audlen = sizeof(sine_data)/2;
921da177e4SLinus Torvalds		custom.aud[2].audper = (unsigned short)period;
931da177e4SLinus Torvalds		custom.aud[2].audvol = 32; /* 50% of maxvol */
941da177e4SLinus Torvalds
951da177e4SLinus Torvalds		if (ticks) {
961da177e4SLinus Torvalds			sound_timer.expires = jiffies + ticks;
971da177e4SLinus Torvalds			add_timer( &sound_timer );
981da177e4SLinus Torvalds		}
991da177e4SLinus Torvalds
1001da177e4SLinus Torvalds		/* turn on DMA for audio channel 2 */
1011da177e4SLinus Torvalds		custom.dmacon = DMAF_SETCLR | DMAF_AUD2;
1021da177e4SLinus Torvalds
1031da177e4SLinus Torvalds	} else
1041da177e4SLinus Torvalds		nosound( 0 );
1051da177e4SLinus Torvalds
1061da177e4SLinus Torvalds	local_irq_restore(flags);
1071da177e4SLinus Torvalds}
1081da177e4SLinus Torvalds
1091da177e4SLinus Torvalds
11024ed960aSKees Cookstatic void nosound(struct timer_list *unused)
1111da177e4SLinus Torvalds{
1121da177e4SLinus Torvalds	/* turn off DMA for audio channel 2 */
1131da177e4SLinus Torvalds	custom.dmacon = DMAF_AUD2;
1141da177e4SLinus Torvalds	/* restore period to previous value after beeping */
1151da177e4SLinus Torvalds	custom.aud[2].audper = amiga_audio_period;
1161da177e4SLinus Torvalds}