slc90e66 update
[linux-2.6/history.git] / sound / core / rtctimer.c
bloba2265f0227d6bbc95e550dabef6b6ed4985b9702
1 /*
2 * RTC based high-frequency timer
4 * Copyright (C) 2000 Takashi Iwai
5 * based on rtctimer.c by Steve Ratcliffe
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sound/driver.h>
24 #include <linux/init.h>
25 #include <linux/time.h>
26 #include <linux/interrupt.h>
27 #include <sound/core.h>
28 #include <sound/timer.h>
29 #include <sound/info.h>
31 #if defined(CONFIG_RTC) || defined(CONFIG_RTC_MODULE)
33 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 12) /* FIXME: which 2.2.x kernel? */
34 #include <linux/rtc.h>
35 #else
36 #include <linux/mc146818rtc.h>
37 #endif
39 /* use tasklet for interrupt handling */
40 #define USE_TASKLET
42 #define RTC_FREQ 1024 /* default frequency */
43 #define NANO_SEC 1000000000L /* 10^9 in sec */
46 * prototypes
48 static int rtctimer_open(snd_timer_t *t);
49 static int rtctimer_close(snd_timer_t *t);
50 static int rtctimer_start(snd_timer_t *t);
51 static int rtctimer_stop(snd_timer_t *t);
55 * The hardware dependent description for this timer.
57 static struct _snd_timer_hardware rtc_hw = {
58 flags: SNDRV_TIMER_HW_FIRST|SNDRV_TIMER_HW_AUTO,
59 ticks: 100000000L, /* FIXME: XXX */
60 open: rtctimer_open,
61 close: rtctimer_close,
62 start: rtctimer_start,
63 stop: rtctimer_stop,
66 int rtctimer_freq = RTC_FREQ; /* frequency */
67 static snd_timer_t *rtctimer;
68 static atomic_t rtc_inc = ATOMIC_INIT(0);
69 static rtc_task_t rtc_task;
71 /* tasklet */
72 #ifdef USE_TASKLET
73 static struct tasklet_struct rtc_tq;
74 #endif
76 static int
77 rtctimer_open(snd_timer_t *t)
79 int err;
81 err = rtc_register(&rtc_task);
82 if (err < 0)
83 return err;
84 t->private_data = &rtc_task;
85 MOD_INC_USE_COUNT;
86 return 0;
89 static int
90 rtctimer_close(snd_timer_t *t)
92 rtc_task_t *rtc = t->private_data;
93 if (rtc) {
94 rtc_unregister(rtc);
95 t->private_data = NULL;
97 MOD_DEC_USE_COUNT;
98 return 0;
101 static int
102 rtctimer_start(snd_timer_t *timer)
104 rtc_task_t *rtc = timer->private_data;
105 snd_assert(rtc != NULL, return -EINVAL);
106 rtc_control(rtc, RTC_IRQP_SET, rtctimer_freq);
107 rtc_control(rtc, RTC_PIE_ON, 0);
108 atomic_set(&rtc_inc, 0);
109 return 0;
112 static int
113 rtctimer_stop(snd_timer_t *timer)
115 rtc_task_t *rtc = timer->private_data;
116 snd_assert(rtc != NULL, return -EINVAL);
117 rtc_control(rtc, RTC_PIE_OFF, 0);
118 return 0;
122 * interrupt
124 static void rtctimer_interrupt(void *private_data)
126 atomic_inc(&rtc_inc);
127 #ifdef USE_TASKLET
128 tasklet_hi_schedule(&rtc_tq);
129 #else
131 int ticks = atomic_read(&rtc_inc);
132 snd_timer_interrupt((snd_timer_t*)private_data, ticks);
133 atomic_sub(ticks, &rtc_inc);
135 #endif /* USE_TASKLET */
138 #ifdef USE_TASKLET
139 static void rtctimer_interrupt2(unsigned long private_data)
141 snd_timer_t *timer = (snd_timer_t *)private_data;
142 int ticks;
144 snd_assert(timer != NULL, return);
145 do {
146 ticks = atomic_read(&rtc_inc);
147 snd_timer_interrupt(timer, ticks);
148 } while (!atomic_sub_and_test(ticks, &rtc_inc));
150 #endif /* USE_TASKLET */
154 * ENTRY functions
156 static int __init rtctimer_init(void)
158 int order, err;
159 snd_timer_t *timer;
161 if (rtctimer_freq < 2 || rtctimer_freq > 8192) {
162 snd_printk(KERN_ERR "rtctimer: invalid frequency %d\n", rtctimer_freq);
163 return -EINVAL;
165 for (order = 1; rtctimer_freq > order; order <<= 1)
167 if (rtctimer_freq != order) {
168 snd_printk(KERN_ERR "rtctimer: invalid frequency %d\n", rtctimer_freq);
169 return -EINVAL;
172 /* Create a new timer and set up the fields */
173 err = snd_timer_global_new("rtc", SNDRV_TIMER_GLOBAL_RTC, &timer);
174 if (err < 0)
175 return err;
177 #ifdef USE_TASKLET
178 tasklet_init(&rtc_tq, rtctimer_interrupt2, (unsigned long)timer);
179 #endif /* USE_TASKLET */
181 strcpy(timer->name, "RTC timer");
182 timer->hw = rtc_hw;
183 timer->hw.resolution = NANO_SEC / rtctimer_freq;
185 /* set up RTC callback */
186 rtc_task.func = rtctimer_interrupt;
187 rtc_task.private_data = timer;
189 err = snd_timer_global_register(timer);
190 if (err < 0) {
191 snd_timer_global_free(timer);
192 return err;
194 rtctimer = timer; /* remember this */
196 return 0;
199 static void __exit rtctimer_exit(void)
201 if (rtctimer) {
202 snd_timer_global_unregister(rtctimer);
203 rtctimer = NULL;
209 * exported stuff
211 module_init(rtctimer_init)
212 module_exit(rtctimer_exit)
214 MODULE_PARM(rtctimer_freq, "i");
215 MODULE_PARM_DESC(rtctimer_freq, "timer frequency in Hz");
217 MODULE_LICENSE("GPL");
219 #endif /* CONFIG_RTC || CONFIG_RTC_MODULE */