[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / sound / core / rtctimer.c
blobbd5d584d284d58f13035aa2a71f728095ec2f03f
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/threads.h>
27 #include <linux/interrupt.h>
28 #include <linux/moduleparam.h>
29 #include <sound/core.h>
30 #include <sound/timer.h>
31 #include <sound/info.h>
33 #if defined(CONFIG_RTC) || defined(CONFIG_RTC_MODULE)
35 #include <linux/mc146818rtc.h>
37 #define RTC_FREQ 1024 /* default frequency */
38 #define NANO_SEC 1000000000L /* 10^9 in sec */
41 * prototypes
43 static int rtctimer_open(snd_timer_t *t);
44 static int rtctimer_close(snd_timer_t *t);
45 static int rtctimer_start(snd_timer_t *t);
46 static int rtctimer_stop(snd_timer_t *t);
50 * The hardware dependent description for this timer.
52 static struct _snd_timer_hardware rtc_hw = {
53 .flags = SNDRV_TIMER_HW_FIRST|SNDRV_TIMER_HW_AUTO,
54 .ticks = 100000000L, /* FIXME: XXX */
55 .open = rtctimer_open,
56 .close = rtctimer_close,
57 .start = rtctimer_start,
58 .stop = rtctimer_stop,
61 static int rtctimer_freq = RTC_FREQ; /* frequency */
62 static snd_timer_t *rtctimer;
63 static atomic_t rtc_inc = ATOMIC_INIT(0);
64 static rtc_task_t rtc_task;
67 static int
68 rtctimer_open(snd_timer_t *t)
70 int err;
72 err = rtc_register(&rtc_task);
73 if (err < 0)
74 return err;
75 t->private_data = &rtc_task;
76 return 0;
79 static int
80 rtctimer_close(snd_timer_t *t)
82 rtc_task_t *rtc = t->private_data;
83 if (rtc) {
84 rtc_unregister(rtc);
85 t->private_data = NULL;
87 return 0;
90 static int
91 rtctimer_start(snd_timer_t *timer)
93 rtc_task_t *rtc = timer->private_data;
94 snd_assert(rtc != NULL, return -EINVAL);
95 rtc_control(rtc, RTC_IRQP_SET, rtctimer_freq);
96 rtc_control(rtc, RTC_PIE_ON, 0);
97 atomic_set(&rtc_inc, 0);
98 return 0;
101 static int
102 rtctimer_stop(snd_timer_t *timer)
104 rtc_task_t *rtc = timer->private_data;
105 snd_assert(rtc != NULL, return -EINVAL);
106 rtc_control(rtc, RTC_PIE_OFF, 0);
107 return 0;
111 * interrupt
113 static void rtctimer_interrupt(void *private_data)
115 int ticks;
117 atomic_inc(&rtc_inc);
118 ticks = atomic_read(&rtc_inc);
119 snd_timer_interrupt((snd_timer_t*)private_data, ticks);
120 atomic_sub(ticks, &rtc_inc);
125 * ENTRY functions
127 static int __init rtctimer_init(void)
129 int order, err;
130 snd_timer_t *timer;
132 if (rtctimer_freq < 2 || rtctimer_freq > 8192) {
133 snd_printk(KERN_ERR "rtctimer: invalid frequency %d\n", rtctimer_freq);
134 return -EINVAL;
136 for (order = 1; rtctimer_freq > order; order <<= 1)
138 if (rtctimer_freq != order) {
139 snd_printk(KERN_ERR "rtctimer: invalid frequency %d\n", rtctimer_freq);
140 return -EINVAL;
143 /* Create a new timer and set up the fields */
144 err = snd_timer_global_new("rtc", SNDRV_TIMER_GLOBAL_RTC, &timer);
145 if (err < 0)
146 return err;
148 strcpy(timer->name, "RTC timer");
149 timer->hw = rtc_hw;
150 timer->hw.resolution = NANO_SEC / rtctimer_freq;
152 /* set up RTC callback */
153 rtc_task.func = rtctimer_interrupt;
154 rtc_task.private_data = timer;
156 err = snd_timer_global_register(timer);
157 if (err < 0) {
158 snd_timer_global_free(timer);
159 return err;
161 rtctimer = timer; /* remember this */
163 return 0;
166 static void __exit rtctimer_exit(void)
168 if (rtctimer) {
169 snd_timer_global_unregister(rtctimer);
170 rtctimer = NULL;
176 * exported stuff
178 module_init(rtctimer_init)
179 module_exit(rtctimer_exit)
181 module_param(rtctimer_freq, int, 0444);
182 MODULE_PARM_DESC(rtctimer_freq, "timer frequency in Hz");
184 MODULE_LICENSE("GPL");
186 MODULE_ALIAS("snd-timer-" __stringify(SNDRV_TIMER_GLOBAL_RTC));
188 #endif /* CONFIG_RTC || CONFIG_RTC_MODULE */