xref: /linux/arch/m68k/amiga/amisound.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
1 /*
2  * linux/arch/m68k/amiga/amisound.c
3  *
4  * amiga sound driver for Linux/m68k
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file COPYING in the main directory of this archive
8  * for more details.
9  */
10 
11 #include <linux/config.h>
12 #include <linux/jiffies.h>
13 #include <linux/timer.h>
14 #include <linux/init.h>
15 #include <linux/string.h>
16 
17 #include <asm/system.h>
18 #include <asm/amigahw.h>
19 
20 static unsigned short *snd_data;
21 static const signed char sine_data[] = {
22 	0,  39,  75,  103,  121,  127,  121,  103,  75,  39,
23 	0, -39, -75, -103, -121, -127, -121, -103, -75, -39
24 };
25 #define DATA_SIZE	(sizeof(sine_data)/sizeof(sine_data[0]))
26 
27 #define custom amiga_custom
28 
29     /*
30      * The minimum period for audio may be modified by the frame buffer
31      * device since it depends on htotal (for OCS/ECS/AGA)
32      */
33 
34 volatile unsigned short amiga_audio_min_period = 124; /* Default for pre-OCS */
35 
36 #define MAX_PERIOD	(65535)
37 
38 
39     /*
40      *	Current period (set by dmasound.c)
41      */
42 
43 unsigned short amiga_audio_period = MAX_PERIOD;
44 
45 static unsigned long clock_constant;
46 
47 void __init amiga_init_sound(void)
48 {
49 	static struct resource beep_res = { .name = "Beep" };
50 
51 	snd_data = amiga_chip_alloc_res(sizeof(sine_data), &beep_res);
52 	if (!snd_data) {
53 		printk (KERN_CRIT "amiga init_sound: failed to allocate chipmem\n");
54 		return;
55 	}
56 	memcpy (snd_data, sine_data, sizeof(sine_data));
57 
58 	/* setup divisor */
59 	clock_constant = (amiga_colorclock+DATA_SIZE/2)/DATA_SIZE;
60 
61 	/* without amifb, turn video off and enable high quality sound */
62 #ifndef CONFIG_FB_AMIGA
63 	amifb_video_off();
64 #endif
65 }
66 
67 static void nosound( unsigned long ignored );
68 static DEFINE_TIMER(sound_timer, nosound, 0, 0);
69 
70 void amiga_mksound( unsigned int hz, unsigned int ticks )
71 {
72 	unsigned long flags;
73 
74 	if (!snd_data)
75 		return;
76 
77 	local_irq_save(flags);
78 	del_timer( &sound_timer );
79 
80 	if (hz > 20 && hz < 32767) {
81 		unsigned long period = (clock_constant / hz);
82 
83 		if (period < amiga_audio_min_period)
84 			period = amiga_audio_min_period;
85 		if (period > MAX_PERIOD)
86 			period = MAX_PERIOD;
87 
88 		/* setup pointer to data, period, length and volume */
89 		custom.aud[2].audlc = snd_data;
90 		custom.aud[2].audlen = sizeof(sine_data)/2;
91 		custom.aud[2].audper = (unsigned short)period;
92 		custom.aud[2].audvol = 32; /* 50% of maxvol */
93 
94 		if (ticks) {
95 			sound_timer.expires = jiffies + ticks;
96 			add_timer( &sound_timer );
97 		}
98 
99 		/* turn on DMA for audio channel 2 */
100 		custom.dmacon = DMAF_SETCLR | DMAF_AUD2;
101 
102 	} else
103 		nosound( 0 );
104 
105 	local_irq_restore(flags);
106 }
107 
108 
109 static void nosound( unsigned long ignored )
110 {
111 	/* turn off DMA for audio channel 2 */
112 	custom.dmacon = DMAF_AUD2;
113 	/* restore period to previous value after beeping */
114 	custom.aud[2].audper = amiga_audio_period;
115 }
116