ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / isa / gus / gus_timer.c
1 /*
2  *  Routines for Gravis UltraSound soundcards - Timers
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *  GUS have similar timers as AdLib (OPL2/OPL3 chips).
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include <sound/driver.h>
25 #include <linux/time.h>
26 #include <sound/core.h>
27 #include <sound/gus.h>
28
29 #define chip_t snd_gus_card_t
30
31 /*
32  *  Timer 1 - 80us
33  */
34
35 static int snd_gf1_timer1_start(snd_timer_t * timer)
36 {
37         unsigned long flags;
38         unsigned char tmp;
39         unsigned int ticks;
40         snd_gus_card_t *gus;
41
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);
50         return 0;
51 }
52
53 static int snd_gf1_timer1_stop(snd_timer_t * timer)
54 {
55         unsigned long flags;
56         unsigned char tmp;
57         snd_gus_card_t *gus;
58
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);
64         return 0;
65 }
66
67 /*
68  *  Timer 2 - 320us
69  */
70
71 static int snd_gf1_timer2_start(snd_timer_t * timer)
72 {
73         unsigned long flags;
74         unsigned char tmp;
75         unsigned int ticks;
76         snd_gus_card_t *gus;
77
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);
86         return 0;
87 }
88
89 static int snd_gf1_timer2_stop(snd_timer_t * timer)
90 {
91         unsigned long flags;
92         unsigned char tmp;
93         snd_gus_card_t *gus;
94
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);
100         return 0;
101 }
102
103 /*
104
105  */
106
107 static void snd_gf1_interrupt_timer1(snd_gus_card_t * gus)
108 {
109         snd_timer_t *timer = gus->gf1.timer1;
110
111         if (timer == NULL)
112                 return;
113         snd_timer_interrupt(timer, timer->sticks);
114 }
115
116 static void snd_gf1_interrupt_timer2(snd_gus_card_t * gus)
117 {
118         snd_timer_t *timer = gus->gf1.timer2;
119
120         if (timer == NULL)
121                 return;
122         snd_timer_interrupt(timer, timer->sticks);
123 }
124
125 /*
126
127  */
128
129 static struct _snd_timer_hardware snd_gf1_timer1 =
130 {
131         .flags =        SNDRV_TIMER_HW_STOP,
132         .resolution =   80000,
133         .ticks =        256,
134         .start =        snd_gf1_timer1_start,
135         .stop =         snd_gf1_timer1_stop,
136 };
137
138 static struct _snd_timer_hardware snd_gf1_timer2 =
139 {
140         .flags =        SNDRV_TIMER_HW_STOP,
141         .resolution =   320000,
142         .ticks =        256,
143         .start =        snd_gf1_timer2_start,
144         .stop =         snd_gf1_timer2_stop,
145 };
146
147 static void snd_gf1_timer1_free(snd_timer_t *timer)
148 {
149         snd_gus_card_t *gus = snd_magic_cast(snd_gus_card_t, timer->private_data, return);
150         gus->gf1.timer1 = NULL;
151 }
152
153 static void snd_gf1_timer2_free(snd_timer_t *timer)
154 {
155         snd_gus_card_t *gus = snd_magic_cast(snd_gus_card_t, timer->private_data, return);
156         gus->gf1.timer2 = NULL;
157 }
158
159 void snd_gf1_timers_init(snd_gus_card_t * gus)
160 {
161         snd_timer_t *timer;
162         snd_timer_id_t tid;
163
164         if (gus->gf1.timer1 != NULL || gus->gf1.timer2 != NULL)
165                 return;
166
167         gus->gf1.interrupt_handler_timer1 = snd_gf1_interrupt_timer1;
168         gus->gf1.interrupt_handler_timer2 = snd_gf1_interrupt_timer2;
169
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;
174         tid.subdevice = 0;
175
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;
181         }
182         gus->gf1.timer1 = timer;
183
184         tid.device++;
185
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;
191         }
192         gus->gf1.timer2 = timer;
193 }
194
195 void snd_gf1_timers_done(snd_gus_card_t * gus)
196 {
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;
201         }
202         if (gus->gf1.timer2) {
203                 snd_device_free(gus->card, gus->gf1.timer2);
204                 gus->gf1.timer2 = NULL;
205         }
206 }