Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Routines for Gravis UltraSound soundcards - Timers
0004  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
0005  *
0006  *  GUS have similar timers as AdLib (OPL2/OPL3 chips).
0007  */
0008 
0009 #include <linux/time.h>
0010 #include <sound/core.h>
0011 #include <sound/gus.h>
0012 
0013 /*
0014  *  Timer 1 - 80us
0015  */
0016 
0017 static int snd_gf1_timer1_start(struct snd_timer * timer)
0018 {
0019     unsigned long flags;
0020     unsigned char tmp;
0021     unsigned int ticks;
0022     struct snd_gus_card *gus;
0023 
0024     gus = snd_timer_chip(timer);
0025     spin_lock_irqsave(&gus->reg_lock, flags);
0026     ticks = timer->sticks;
0027     tmp = (gus->gf1.timer_enabled |= 4);
0028     snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_1, 256 - ticks);   /* timer 1 count */
0029     snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp);   /* enable timer 1 IRQ */
0030     snd_gf1_adlib_write(gus, 0x04, tmp >> 2);   /* timer 2 start */
0031     spin_unlock_irqrestore(&gus->reg_lock, flags);
0032     return 0;
0033 }
0034 
0035 static int snd_gf1_timer1_stop(struct snd_timer * timer)
0036 {
0037     unsigned long flags;
0038     unsigned char tmp;
0039     struct snd_gus_card *gus;
0040 
0041     gus = snd_timer_chip(timer);
0042     spin_lock_irqsave(&gus->reg_lock, flags);
0043     tmp = (gus->gf1.timer_enabled &= ~4);
0044     snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp);   /* disable timer #1 */
0045     spin_unlock_irqrestore(&gus->reg_lock, flags);
0046     return 0;
0047 }
0048 
0049 /*
0050  *  Timer 2 - 320us
0051  */
0052 
0053 static int snd_gf1_timer2_start(struct snd_timer * timer)
0054 {
0055     unsigned long flags;
0056     unsigned char tmp;
0057     unsigned int ticks;
0058     struct snd_gus_card *gus;
0059 
0060     gus = snd_timer_chip(timer);
0061     spin_lock_irqsave(&gus->reg_lock, flags);
0062     ticks = timer->sticks;
0063     tmp = (gus->gf1.timer_enabled |= 8);
0064     snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_2, 256 - ticks);   /* timer 2 count */
0065     snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp);   /* enable timer 2 IRQ */
0066     snd_gf1_adlib_write(gus, 0x04, tmp >> 2);   /* timer 2 start */
0067     spin_unlock_irqrestore(&gus->reg_lock, flags);
0068     return 0;
0069 }
0070 
0071 static int snd_gf1_timer2_stop(struct snd_timer * timer)
0072 {
0073     unsigned long flags;
0074     unsigned char tmp;
0075     struct snd_gus_card *gus;
0076 
0077     gus = snd_timer_chip(timer);
0078     spin_lock_irqsave(&gus->reg_lock, flags);
0079     tmp = (gus->gf1.timer_enabled &= ~8);
0080     snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp);   /* disable timer #1 */
0081     spin_unlock_irqrestore(&gus->reg_lock, flags);
0082     return 0;
0083 }
0084 
0085 /*
0086 
0087  */
0088 
0089 static void snd_gf1_interrupt_timer1(struct snd_gus_card * gus)
0090 {
0091     struct snd_timer *timer = gus->gf1.timer1;
0092 
0093     if (timer == NULL)
0094         return;
0095     snd_timer_interrupt(timer, timer->sticks);
0096 }
0097 
0098 static void snd_gf1_interrupt_timer2(struct snd_gus_card * gus)
0099 {
0100     struct snd_timer *timer = gus->gf1.timer2;
0101 
0102     if (timer == NULL)
0103         return;
0104     snd_timer_interrupt(timer, timer->sticks);
0105 }
0106 
0107 /*
0108 
0109  */
0110 
0111 static const struct snd_timer_hardware snd_gf1_timer1 =
0112 {
0113     .flags =    SNDRV_TIMER_HW_STOP,
0114     .resolution =   80000,
0115     .ticks =    256,
0116     .start =    snd_gf1_timer1_start,
0117     .stop =     snd_gf1_timer1_stop,
0118 };
0119 
0120 static const struct snd_timer_hardware snd_gf1_timer2 =
0121 {
0122     .flags =    SNDRV_TIMER_HW_STOP,
0123     .resolution =   320000,
0124     .ticks =    256,
0125     .start =    snd_gf1_timer2_start,
0126     .stop =     snd_gf1_timer2_stop,
0127 };
0128 
0129 static void snd_gf1_timer1_free(struct snd_timer *timer)
0130 {
0131     struct snd_gus_card *gus = timer->private_data;
0132     gus->gf1.timer1 = NULL;
0133 }
0134 
0135 static void snd_gf1_timer2_free(struct snd_timer *timer)
0136 {
0137     struct snd_gus_card *gus = timer->private_data;
0138     gus->gf1.timer2 = NULL;
0139 }
0140 
0141 void snd_gf1_timers_init(struct snd_gus_card * gus)
0142 {
0143     struct snd_timer *timer;
0144     struct snd_timer_id tid;
0145 
0146     if (gus->gf1.timer1 != NULL || gus->gf1.timer2 != NULL)
0147         return;
0148 
0149     gus->gf1.interrupt_handler_timer1 = snd_gf1_interrupt_timer1;
0150     gus->gf1.interrupt_handler_timer2 = snd_gf1_interrupt_timer2;
0151 
0152     tid.dev_class = SNDRV_TIMER_CLASS_CARD;
0153     tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
0154     tid.card = gus->card->number;
0155     tid.device = gus->timer_dev;
0156     tid.subdevice = 0;
0157 
0158     if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
0159         strcpy(timer->name, "GF1 timer #1");
0160         timer->private_data = gus;
0161         timer->private_free = snd_gf1_timer1_free;
0162         timer->hw = snd_gf1_timer1;
0163     }
0164     gus->gf1.timer1 = timer;
0165 
0166     tid.device++;
0167 
0168     if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
0169         strcpy(timer->name, "GF1 timer #2");
0170         timer->private_data = gus;
0171         timer->private_free = snd_gf1_timer2_free;
0172         timer->hw = snd_gf1_timer2;
0173     }
0174     gus->gf1.timer2 = timer;
0175 }
0176 
0177 void snd_gf1_timers_done(struct snd_gus_card * gus)
0178 {
0179     snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_TIMER1 | SNDRV_GF1_HANDLER_TIMER2);
0180     if (gus->gf1.timer1) {
0181         snd_device_free(gus->card, gus->gf1.timer1);
0182         gus->gf1.timer1 = NULL;
0183     }
0184     if (gus->gf1.timer2) {
0185         snd_device_free(gus->card, gus->gf1.timer2);
0186         gus->gf1.timer2 = NULL;
0187     }
0188 }