xref: /linux/sound/isa/gus/gus_timer.c (revision 5ff16a3d)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Routines for Gravis UltraSound soundcards - Timers
4c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  GUS have similar timers as AdLib (OPL2/OPL3 chips).
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/time.h>
101da177e4SLinus Torvalds #include <sound/core.h>
111da177e4SLinus Torvalds #include <sound/gus.h>
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds /*
141da177e4SLinus Torvalds  *  Timer 1 - 80us
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
snd_gf1_timer1_start(struct snd_timer * timer)175e2da206STakashi Iwai static int snd_gf1_timer1_start(struct snd_timer * timer)
181da177e4SLinus Torvalds {
191da177e4SLinus Torvalds 	unsigned long flags;
201da177e4SLinus Torvalds 	unsigned char tmp;
211da177e4SLinus Torvalds 	unsigned int ticks;
225e2da206STakashi Iwai 	struct snd_gus_card *gus;
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds 	gus = snd_timer_chip(timer);
251da177e4SLinus Torvalds 	spin_lock_irqsave(&gus->reg_lock, flags);
261da177e4SLinus Torvalds 	ticks = timer->sticks;
271da177e4SLinus Torvalds 	tmp = (gus->gf1.timer_enabled |= 4);
281da177e4SLinus Torvalds 	snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_1, 256 - ticks);	/* timer 1 count */
291da177e4SLinus Torvalds 	snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp);	/* enable timer 1 IRQ */
301da177e4SLinus Torvalds 	snd_gf1_adlib_write(gus, 0x04, tmp >> 2);	/* timer 2 start */
311da177e4SLinus Torvalds 	spin_unlock_irqrestore(&gus->reg_lock, flags);
321da177e4SLinus Torvalds 	return 0;
331da177e4SLinus Torvalds }
341da177e4SLinus Torvalds 
snd_gf1_timer1_stop(struct snd_timer * timer)355e2da206STakashi Iwai static int snd_gf1_timer1_stop(struct snd_timer * timer)
361da177e4SLinus Torvalds {
371da177e4SLinus Torvalds 	unsigned long flags;
381da177e4SLinus Torvalds 	unsigned char tmp;
395e2da206STakashi Iwai 	struct snd_gus_card *gus;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 	gus = snd_timer_chip(timer);
421da177e4SLinus Torvalds 	spin_lock_irqsave(&gus->reg_lock, flags);
431da177e4SLinus Torvalds 	tmp = (gus->gf1.timer_enabled &= ~4);
441da177e4SLinus Torvalds 	snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp);	/* disable timer #1 */
451da177e4SLinus Torvalds 	spin_unlock_irqrestore(&gus->reg_lock, flags);
461da177e4SLinus Torvalds 	return 0;
471da177e4SLinus Torvalds }
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /*
501da177e4SLinus Torvalds  *  Timer 2 - 320us
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds 
snd_gf1_timer2_start(struct snd_timer * timer)535e2da206STakashi Iwai static int snd_gf1_timer2_start(struct snd_timer * timer)
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds 	unsigned long flags;
561da177e4SLinus Torvalds 	unsigned char tmp;
571da177e4SLinus Torvalds 	unsigned int ticks;
585e2da206STakashi Iwai 	struct snd_gus_card *gus;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	gus = snd_timer_chip(timer);
611da177e4SLinus Torvalds 	spin_lock_irqsave(&gus->reg_lock, flags);
621da177e4SLinus Torvalds 	ticks = timer->sticks;
631da177e4SLinus Torvalds 	tmp = (gus->gf1.timer_enabled |= 8);
641da177e4SLinus Torvalds 	snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_2, 256 - ticks);	/* timer 2 count */
651da177e4SLinus Torvalds 	snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp);	/* enable timer 2 IRQ */
661da177e4SLinus Torvalds 	snd_gf1_adlib_write(gus, 0x04, tmp >> 2);	/* timer 2 start */
671da177e4SLinus Torvalds 	spin_unlock_irqrestore(&gus->reg_lock, flags);
681da177e4SLinus Torvalds 	return 0;
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
snd_gf1_timer2_stop(struct snd_timer * timer)715e2da206STakashi Iwai static int snd_gf1_timer2_stop(struct snd_timer * timer)
721da177e4SLinus Torvalds {
731da177e4SLinus Torvalds 	unsigned long flags;
741da177e4SLinus Torvalds 	unsigned char tmp;
755e2da206STakashi Iwai 	struct snd_gus_card *gus;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	gus = snd_timer_chip(timer);
781da177e4SLinus Torvalds 	spin_lock_irqsave(&gus->reg_lock, flags);
791da177e4SLinus Torvalds 	tmp = (gus->gf1.timer_enabled &= ~8);
801da177e4SLinus Torvalds 	snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp);	/* disable timer #1 */
811da177e4SLinus Torvalds 	spin_unlock_irqrestore(&gus->reg_lock, flags);
821da177e4SLinus Torvalds 	return 0;
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /*
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds  */
881da177e4SLinus Torvalds 
snd_gf1_interrupt_timer1(struct snd_gus_card * gus)895e2da206STakashi Iwai static void snd_gf1_interrupt_timer1(struct snd_gus_card * gus)
901da177e4SLinus Torvalds {
915e2da206STakashi Iwai 	struct snd_timer *timer = gus->gf1.timer1;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	if (timer == NULL)
941da177e4SLinus Torvalds 		return;
951da177e4SLinus Torvalds 	snd_timer_interrupt(timer, timer->sticks);
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
snd_gf1_interrupt_timer2(struct snd_gus_card * gus)985e2da206STakashi Iwai static void snd_gf1_interrupt_timer2(struct snd_gus_card * gus)
991da177e4SLinus Torvalds {
1005e2da206STakashi Iwai 	struct snd_timer *timer = gus->gf1.timer2;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	if (timer == NULL)
1031da177e4SLinus Torvalds 		return;
1041da177e4SLinus Torvalds 	snd_timer_interrupt(timer, timer->sticks);
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds 
111*5ff16a3dSTakashi Iwai static const struct snd_timer_hardware snd_gf1_timer1 =
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	.flags =	SNDRV_TIMER_HW_STOP,
1141da177e4SLinus Torvalds 	.resolution =	80000,
1151da177e4SLinus Torvalds 	.ticks =	256,
1161da177e4SLinus Torvalds 	.start =	snd_gf1_timer1_start,
1171da177e4SLinus Torvalds 	.stop =		snd_gf1_timer1_stop,
1181da177e4SLinus Torvalds };
1191da177e4SLinus Torvalds 
120*5ff16a3dSTakashi Iwai static const struct snd_timer_hardware snd_gf1_timer2 =
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	.flags =	SNDRV_TIMER_HW_STOP,
1231da177e4SLinus Torvalds 	.resolution =	320000,
1241da177e4SLinus Torvalds 	.ticks =	256,
1251da177e4SLinus Torvalds 	.start =	snd_gf1_timer2_start,
1261da177e4SLinus Torvalds 	.stop =		snd_gf1_timer2_stop,
1271da177e4SLinus Torvalds };
1281da177e4SLinus Torvalds 
snd_gf1_timer1_free(struct snd_timer * timer)1295e2da206STakashi Iwai static void snd_gf1_timer1_free(struct snd_timer *timer)
1301da177e4SLinus Torvalds {
1315e2da206STakashi Iwai 	struct snd_gus_card *gus = timer->private_data;
1321da177e4SLinus Torvalds 	gus->gf1.timer1 = NULL;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
snd_gf1_timer2_free(struct snd_timer * timer)1355e2da206STakashi Iwai static void snd_gf1_timer2_free(struct snd_timer *timer)
1361da177e4SLinus Torvalds {
1375e2da206STakashi Iwai 	struct snd_gus_card *gus = timer->private_data;
1381da177e4SLinus Torvalds 	gus->gf1.timer2 = NULL;
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
snd_gf1_timers_init(struct snd_gus_card * gus)1415e2da206STakashi Iwai void snd_gf1_timers_init(struct snd_gus_card * gus)
1421da177e4SLinus Torvalds {
1435e2da206STakashi Iwai 	struct snd_timer *timer;
1445e2da206STakashi Iwai 	struct snd_timer_id tid;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	if (gus->gf1.timer1 != NULL || gus->gf1.timer2 != NULL)
1471da177e4SLinus Torvalds 		return;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	gus->gf1.interrupt_handler_timer1 = snd_gf1_interrupt_timer1;
1501da177e4SLinus Torvalds 	gus->gf1.interrupt_handler_timer2 = snd_gf1_interrupt_timer2;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1531da177e4SLinus Torvalds 	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1541da177e4SLinus Torvalds 	tid.card = gus->card->number;
1551da177e4SLinus Torvalds 	tid.device = gus->timer_dev;
1561da177e4SLinus Torvalds 	tid.subdevice = 0;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
1591da177e4SLinus Torvalds 		strcpy(timer->name, "GF1 timer #1");
1601da177e4SLinus Torvalds 		timer->private_data = gus;
1611da177e4SLinus Torvalds 		timer->private_free = snd_gf1_timer1_free;
1621da177e4SLinus Torvalds 		timer->hw = snd_gf1_timer1;
1631da177e4SLinus Torvalds 	}
1641da177e4SLinus Torvalds 	gus->gf1.timer1 = timer;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	tid.device++;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
1691da177e4SLinus Torvalds 		strcpy(timer->name, "GF1 timer #2");
1701da177e4SLinus Torvalds 		timer->private_data = gus;
1711da177e4SLinus Torvalds 		timer->private_free = snd_gf1_timer2_free;
1721da177e4SLinus Torvalds 		timer->hw = snd_gf1_timer2;
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 	gus->gf1.timer2 = timer;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
snd_gf1_timers_done(struct snd_gus_card * gus)1775e2da206STakashi Iwai void snd_gf1_timers_done(struct snd_gus_card * gus)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_TIMER1 | SNDRV_GF1_HANDLER_TIMER2);
1801da177e4SLinus Torvalds 	if (gus->gf1.timer1) {
1811da177e4SLinus Torvalds 		snd_device_free(gus->card, gus->gf1.timer1);
1821da177e4SLinus Torvalds 		gus->gf1.timer1 = NULL;
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 	if (gus->gf1.timer2) {
1851da177e4SLinus Torvalds 		snd_device_free(gus->card, gus->gf1.timer2);
1861da177e4SLinus Torvalds 		gus->gf1.timer2 = NULL;
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds }
189