blob: 99eac573c41418542da212d6ec0b1a55464d85e9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines for Gravis UltraSound soundcards - Timers
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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/*
30 * Timer 1 - 80us
31 */
32
Takashi Iwai5e2da202005-11-17 14:36:44 +010033static int snd_gf1_timer1_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 unsigned long flags;
36 unsigned char tmp;
37 unsigned int ticks;
Takashi Iwai5e2da202005-11-17 14:36:44 +010038 struct snd_gus_card *gus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 gus = snd_timer_chip(timer);
41 spin_lock_irqsave(&gus->reg_lock, flags);
42 ticks = timer->sticks;
43 tmp = (gus->gf1.timer_enabled |= 4);
44 snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_1, 256 - ticks); /* timer 1 count */
45 snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* enable timer 1 IRQ */
46 snd_gf1_adlib_write(gus, 0x04, tmp >> 2); /* timer 2 start */
47 spin_unlock_irqrestore(&gus->reg_lock, flags);
48 return 0;
49}
50
Takashi Iwai5e2da202005-11-17 14:36:44 +010051static int snd_gf1_timer1_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 unsigned long flags;
54 unsigned char tmp;
Takashi Iwai5e2da202005-11-17 14:36:44 +010055 struct snd_gus_card *gus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 gus = snd_timer_chip(timer);
58 spin_lock_irqsave(&gus->reg_lock, flags);
59 tmp = (gus->gf1.timer_enabled &= ~4);
60 snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* disable timer #1 */
61 spin_unlock_irqrestore(&gus->reg_lock, flags);
62 return 0;
63}
64
65/*
66 * Timer 2 - 320us
67 */
68
Takashi Iwai5e2da202005-11-17 14:36:44 +010069static int snd_gf1_timer2_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 unsigned long flags;
72 unsigned char tmp;
73 unsigned int ticks;
Takashi Iwai5e2da202005-11-17 14:36:44 +010074 struct snd_gus_card *gus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 gus = snd_timer_chip(timer);
77 spin_lock_irqsave(&gus->reg_lock, flags);
78 ticks = timer->sticks;
79 tmp = (gus->gf1.timer_enabled |= 8);
80 snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_2, 256 - ticks); /* timer 2 count */
81 snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* enable timer 2 IRQ */
82 snd_gf1_adlib_write(gus, 0x04, tmp >> 2); /* timer 2 start */
83 spin_unlock_irqrestore(&gus->reg_lock, flags);
84 return 0;
85}
86
Takashi Iwai5e2da202005-11-17 14:36:44 +010087static int snd_gf1_timer2_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 unsigned long flags;
90 unsigned char tmp;
Takashi Iwai5e2da202005-11-17 14:36:44 +010091 struct snd_gus_card *gus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 gus = snd_timer_chip(timer);
94 spin_lock_irqsave(&gus->reg_lock, flags);
95 tmp = (gus->gf1.timer_enabled &= ~8);
96 snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* disable timer #1 */
97 spin_unlock_irqrestore(&gus->reg_lock, flags);
98 return 0;
99}
100
101/*
102
103 */
104
Takashi Iwai5e2da202005-11-17 14:36:44 +0100105static void snd_gf1_interrupt_timer1(struct snd_gus_card * gus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100107 struct snd_timer *timer = gus->gf1.timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 if (timer == NULL)
110 return;
111 snd_timer_interrupt(timer, timer->sticks);
112}
113
Takashi Iwai5e2da202005-11-17 14:36:44 +0100114static void snd_gf1_interrupt_timer2(struct snd_gus_card * gus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100116 struct snd_timer *timer = gus->gf1.timer2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (timer == NULL)
119 return;
120 snd_timer_interrupt(timer, timer->sticks);
121}
122
123/*
124
125 */
126
Takashi Iwai5e2da202005-11-17 14:36:44 +0100127static struct snd_timer_hardware snd_gf1_timer1 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 .flags = SNDRV_TIMER_HW_STOP,
130 .resolution = 80000,
131 .ticks = 256,
132 .start = snd_gf1_timer1_start,
133 .stop = snd_gf1_timer1_stop,
134};
135
Takashi Iwai5e2da202005-11-17 14:36:44 +0100136static struct snd_timer_hardware snd_gf1_timer2 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 .flags = SNDRV_TIMER_HW_STOP,
139 .resolution = 320000,
140 .ticks = 256,
141 .start = snd_gf1_timer2_start,
142 .stop = snd_gf1_timer2_stop,
143};
144
Takashi Iwai5e2da202005-11-17 14:36:44 +0100145static void snd_gf1_timer1_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100147 struct snd_gus_card *gus = timer->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 gus->gf1.timer1 = NULL;
149}
150
Takashi Iwai5e2da202005-11-17 14:36:44 +0100151static void snd_gf1_timer2_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100153 struct snd_gus_card *gus = timer->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 gus->gf1.timer2 = NULL;
155}
156
Takashi Iwai5e2da202005-11-17 14:36:44 +0100157void snd_gf1_timers_init(struct snd_gus_card * gus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100159 struct snd_timer *timer;
160 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (gus->gf1.timer1 != NULL || gus->gf1.timer2 != NULL)
163 return;
164
165 gus->gf1.interrupt_handler_timer1 = snd_gf1_interrupt_timer1;
166 gus->gf1.interrupt_handler_timer2 = snd_gf1_interrupt_timer2;
167
168 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
169 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
170 tid.card = gus->card->number;
171 tid.device = gus->timer_dev;
172 tid.subdevice = 0;
173
174 if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
175 strcpy(timer->name, "GF1 timer #1");
176 timer->private_data = gus;
177 timer->private_free = snd_gf1_timer1_free;
178 timer->hw = snd_gf1_timer1;
179 }
180 gus->gf1.timer1 = timer;
181
182 tid.device++;
183
184 if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
185 strcpy(timer->name, "GF1 timer #2");
186 timer->private_data = gus;
187 timer->private_free = snd_gf1_timer2_free;
188 timer->hw = snd_gf1_timer2;
189 }
190 gus->gf1.timer2 = timer;
191}
192
Takashi Iwai5e2da202005-11-17 14:36:44 +0100193void snd_gf1_timers_done(struct snd_gus_card * gus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_TIMER1 | SNDRV_GF1_HANDLER_TIMER2);
196 if (gus->gf1.timer1) {
197 snd_device_free(gus->card, gus->gf1.timer1);
198 gus->gf1.timer1 = NULL;
199 }
200 if (gus->gf1.timer2) {
201 snd_device_free(gus->card, gus->gf1.timer2);
202 gus->gf1.timer2 = NULL;
203 }
204}