^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Routines for Gravis UltraSound soundcards - Timers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * GUS have similar timers as AdLib (OPL2/OPL3 chips).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <sound/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <sound/gus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Timer 1 - 80us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static int snd_gf1_timer1_start(struct snd_timer * timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) unsigned char tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned int ticks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct snd_gus_card *gus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) gus = snd_timer_chip(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) spin_lock_irqsave(&gus->reg_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) ticks = timer->sticks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) tmp = (gus->gf1.timer_enabled |= 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_1, 256 - ticks); /* timer 1 count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* enable timer 1 IRQ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) snd_gf1_adlib_write(gus, 0x04, tmp >> 2); /* timer 2 start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) spin_unlock_irqrestore(&gus->reg_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int snd_gf1_timer1_stop(struct snd_timer * timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned char tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct snd_gus_card *gus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) gus = snd_timer_chip(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) spin_lock_irqsave(&gus->reg_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) tmp = (gus->gf1.timer_enabled &= ~4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* disable timer #1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) spin_unlock_irqrestore(&gus->reg_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * Timer 2 - 320us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static int snd_gf1_timer2_start(struct snd_timer * timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned char tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) unsigned int ticks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct snd_gus_card *gus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) gus = snd_timer_chip(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) spin_lock_irqsave(&gus->reg_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ticks = timer->sticks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) tmp = (gus->gf1.timer_enabled |= 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_2, 256 - ticks); /* timer 2 count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* enable timer 2 IRQ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) snd_gf1_adlib_write(gus, 0x04, tmp >> 2); /* timer 2 start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) spin_unlock_irqrestore(&gus->reg_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int snd_gf1_timer2_stop(struct snd_timer * timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned char tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct snd_gus_card *gus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) gus = snd_timer_chip(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) spin_lock_irqsave(&gus->reg_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) tmp = (gus->gf1.timer_enabled &= ~8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* disable timer #1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) spin_unlock_irqrestore(&gus->reg_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static void snd_gf1_interrupt_timer1(struct snd_gus_card * gus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct snd_timer *timer = gus->gf1.timer1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (timer == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) snd_timer_interrupt(timer, timer->sticks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static void snd_gf1_interrupt_timer2(struct snd_gus_card * gus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct snd_timer *timer = gus->gf1.timer2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (timer == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) snd_timer_interrupt(timer, timer->sticks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static const struct snd_timer_hardware snd_gf1_timer1 =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .flags = SNDRV_TIMER_HW_STOP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .resolution = 80000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .ticks = 256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .start = snd_gf1_timer1_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .stop = snd_gf1_timer1_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static const struct snd_timer_hardware snd_gf1_timer2 =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .flags = SNDRV_TIMER_HW_STOP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .resolution = 320000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .ticks = 256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .start = snd_gf1_timer2_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) .stop = snd_gf1_timer2_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static void snd_gf1_timer1_free(struct snd_timer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct snd_gus_card *gus = timer->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) gus->gf1.timer1 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static void snd_gf1_timer2_free(struct snd_timer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct snd_gus_card *gus = timer->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) gus->gf1.timer2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) void snd_gf1_timers_init(struct snd_gus_card * gus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct snd_timer *timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct snd_timer_id tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (gus->gf1.timer1 != NULL || gus->gf1.timer2 != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) gus->gf1.interrupt_handler_timer1 = snd_gf1_interrupt_timer1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) gus->gf1.interrupt_handler_timer2 = snd_gf1_interrupt_timer2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) tid.dev_class = SNDRV_TIMER_CLASS_CARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) tid.card = gus->card->number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) tid.device = gus->timer_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) tid.subdevice = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) strcpy(timer->name, "GF1 timer #1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) timer->private_data = gus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) timer->private_free = snd_gf1_timer1_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) timer->hw = snd_gf1_timer1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) gus->gf1.timer1 = timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) tid.device++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) strcpy(timer->name, "GF1 timer #2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) timer->private_data = gus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) timer->private_free = snd_gf1_timer2_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) timer->hw = snd_gf1_timer2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) gus->gf1.timer2 = timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) void snd_gf1_timers_done(struct snd_gus_card * gus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_TIMER1 | SNDRV_GF1_HANDLER_TIMER2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (gus->gf1.timer1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) snd_device_free(gus->card, gus->gf1.timer1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) gus->gf1.timer1 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (gus->gf1.timer2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) snd_device_free(gus->card, gus->gf1.timer2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) gus->gf1.timer2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }