2 * Routines for Gravis UltraSound soundcards - Timers
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
5 * GUS have similar timers as AdLib (OPL2/OPL3 chips).
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sound/driver.h>
25 #include <linux/time.h>
26 #include <sound/core.h>
27 #include <sound/gus.h>
29 #define chip_t snd_gus_card_t
35 static int snd_gf1_timer1_start(snd_timer_t * timer)
42 gus = snd_timer_chip(timer);
43 spin_lock_irqsave(&gus->reg_lock, flags);
44 ticks = timer->sticks;
45 tmp = (gus->gf1.timer_enabled |= 4);
46 snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_1, 256 - ticks); /* timer 1 count */
47 snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* enable timer 1 IRQ */
48 snd_gf1_adlib_write(gus, 0x04, tmp >> 2); /* timer 2 start */
49 spin_unlock_irqrestore(&gus->reg_lock, flags);
53 static int snd_gf1_timer1_stop(snd_timer_t * timer)
59 gus = snd_timer_chip(timer);
60 spin_lock_irqsave(&gus->reg_lock, flags);
61 tmp = (gus->gf1.timer_enabled &= ~4);
62 snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* disable timer #1 */
63 spin_unlock_irqrestore(&gus->reg_lock, flags);
71 static int snd_gf1_timer2_start(snd_timer_t * timer)
78 gus = snd_timer_chip(timer);
79 spin_lock_irqsave(&gus->reg_lock, flags);
80 ticks = timer->sticks;
81 tmp = (gus->gf1.timer_enabled |= 8);
82 snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_2, 256 - ticks); /* timer 2 count */
83 snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* enable timer 2 IRQ */
84 snd_gf1_adlib_write(gus, 0x04, tmp >> 2); /* timer 2 start */
85 spin_unlock_irqrestore(&gus->reg_lock, flags);
89 static int snd_gf1_timer2_stop(snd_timer_t * timer)
95 gus = snd_timer_chip(timer);
96 spin_lock_irqsave(&gus->reg_lock, flags);
97 tmp = (gus->gf1.timer_enabled &= ~8);
98 snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* disable timer #1 */
99 spin_unlock_irqrestore(&gus->reg_lock, flags);
107 static void snd_gf1_interrupt_timer1(snd_gus_card_t * gus)
109 snd_timer_t *timer = gus->gf1.timer1;
113 snd_timer_interrupt(timer, timer->sticks);
116 static void snd_gf1_interrupt_timer2(snd_gus_card_t * gus)
118 snd_timer_t *timer = gus->gf1.timer2;
122 snd_timer_interrupt(timer, timer->sticks);
129 static struct _snd_timer_hardware snd_gf1_timer1 =
131 .flags = SNDRV_TIMER_HW_STOP,
134 .start = snd_gf1_timer1_start,
135 .stop = snd_gf1_timer1_stop,
138 static struct _snd_timer_hardware snd_gf1_timer2 =
140 .flags = SNDRV_TIMER_HW_STOP,
141 .resolution = 320000,
143 .start = snd_gf1_timer2_start,
144 .stop = snd_gf1_timer2_stop,
147 static void snd_gf1_timer1_free(snd_timer_t *timer)
149 snd_gus_card_t *gus = snd_magic_cast(snd_gus_card_t, timer->private_data, return);
150 gus->gf1.timer1 = NULL;
153 static void snd_gf1_timer2_free(snd_timer_t *timer)
155 snd_gus_card_t *gus = snd_magic_cast(snd_gus_card_t, timer->private_data, return);
156 gus->gf1.timer2 = NULL;
159 void snd_gf1_timers_init(snd_gus_card_t * gus)
164 if (gus->gf1.timer1 != NULL || gus->gf1.timer2 != NULL)
167 gus->gf1.interrupt_handler_timer1 = snd_gf1_interrupt_timer1;
168 gus->gf1.interrupt_handler_timer2 = snd_gf1_interrupt_timer2;
170 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
171 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
172 tid.card = gus->card->number;
173 tid.device = gus->timer_dev;
176 if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
177 strcpy(timer->name, "GF1 timer #1");
178 timer->private_data = gus;
179 timer->private_free = snd_gf1_timer1_free;
180 timer->hw = snd_gf1_timer1;
182 gus->gf1.timer1 = timer;
186 if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
187 strcpy(timer->name, "GF1 timer #2");
188 timer->private_data = gus;
189 timer->private_free = snd_gf1_timer2_free;
190 timer->hw = snd_gf1_timer2;
192 gus->gf1.timer2 = timer;
195 void snd_gf1_timers_done(snd_gus_card_t * gus)
197 snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_TIMER1 | SNDRV_GF1_HANDLER_TIMER2);
198 if (gus->gf1.timer1) {
199 snd_device_free(gus->card, gus->gf1.timer1);
200 gus->gf1.timer1 = NULL;
202 if (gus->gf1.timer2) {
203 snd_device_free(gus->card, gus->gf1.timer2);
204 gus->gf1.timer2 = NULL;