Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / sound / core / rtctimer.c
1 /*
2  *  RTC based high-frequency timer
3  *
4  *  Copyright (C) 2000 Takashi Iwai
5  *      based on rtctimer.c by Steve Ratcliffe
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
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>
32
33 #if defined(CONFIG_RTC) || defined(CONFIG_RTC_MODULE)
34
35 #include <linux/mc146818rtc.h>
36
37 #define RTC_FREQ        1024            /* default frequency */
38 #define NANO_SEC        1000000000L     /* 10^9 in sec */
39
40 /*
41  * prototypes
42  */
43 static int rtctimer_open(struct snd_timer *t);
44 static int rtctimer_close(struct snd_timer *t);
45 static int rtctimer_start(struct snd_timer *t);
46 static int rtctimer_stop(struct snd_timer *t);
47
48
49 /*
50  * The hardware dependent description for this timer.
51  */
52 static struct snd_timer_hardware rtc_hw = {
53         .flags =        SNDRV_TIMER_HW_AUTO |
54                         SNDRV_TIMER_HW_FIRST |
55                         SNDRV_TIMER_HW_TASKLET,
56         .ticks =        100000000L,             /* FIXME: XXX */
57         .open =         rtctimer_open,
58         .close =        rtctimer_close,
59         .start =        rtctimer_start,
60         .stop =         rtctimer_stop,
61 };
62
63 static int rtctimer_freq = RTC_FREQ;            /* frequency */
64 static struct snd_timer *rtctimer;
65 static struct tasklet_struct rtc_tasklet;
66 static rtc_task_t rtc_task;
67
68
69 static int
70 rtctimer_open(struct snd_timer *t)
71 {
72         int err;
73
74         err = rtc_register(&rtc_task);
75         if (err < 0)
76                 return err;
77         t->private_data = &rtc_task;
78         return 0;
79 }
80
81 static int
82 rtctimer_close(struct snd_timer *t)
83 {
84         rtc_task_t *rtc = t->private_data;
85         if (rtc) {
86                 rtc_unregister(rtc);
87                 tasklet_kill(&rtc_tasklet);
88                 t->private_data = NULL;
89         }
90         return 0;
91 }
92
93 static int
94 rtctimer_start(struct snd_timer *timer)
95 {
96         rtc_task_t *rtc = timer->private_data;
97         snd_assert(rtc != NULL, return -EINVAL);
98         rtc_control(rtc, RTC_IRQP_SET, rtctimer_freq);
99         rtc_control(rtc, RTC_PIE_ON, 0);
100         return 0;
101 }
102
103 static int
104 rtctimer_stop(struct snd_timer *timer)
105 {
106         rtc_task_t *rtc = timer->private_data;
107         snd_assert(rtc != NULL, return -EINVAL);
108         rtc_control(rtc, RTC_PIE_OFF, 0);
109         return 0;
110 }
111
112 static void rtctimer_tasklet(unsigned long data)
113 {
114         snd_timer_interrupt((struct snd_timer *)data, 1);
115 }
116
117 /*
118  * interrupt
119  */
120 static void rtctimer_interrupt(void *private_data)
121 {
122         tasklet_hi_schedule(private_data);
123 }
124
125
126 /*
127  *  ENTRY functions
128  */
129 static int __init rtctimer_init(void)
130 {
131         int err;
132         struct snd_timer *timer;
133
134         if (rtctimer_freq < 2 || rtctimer_freq > 8192 ||
135             (rtctimer_freq & (rtctimer_freq - 1)) != 0) {
136                 snd_printk(KERN_ERR "rtctimer: invalid frequency %d\n",
137                            rtctimer_freq);
138                 return -EINVAL;
139         }
140
141         /* Create a new timer and set up the fields */
142         err = snd_timer_global_new("rtc", SNDRV_TIMER_GLOBAL_RTC, &timer);
143         if (err < 0)
144                 return err;
145
146         timer->module = THIS_MODULE;
147         strcpy(timer->name, "RTC timer");
148         timer->hw = rtc_hw;
149         timer->hw.resolution = NANO_SEC / rtctimer_freq;
150
151         tasklet_init(&rtc_tasklet, rtctimer_tasklet, (unsigned long)timer);
152
153         /* set up RTC callback */
154         rtc_task.func = rtctimer_interrupt;
155         rtc_task.private_data = &rtc_tasklet;
156
157         err = snd_timer_global_register(timer);
158         if (err < 0) {
159                 snd_timer_global_free(timer);
160                 return err;
161         }
162         rtctimer = timer; /* remember this */
163
164         return 0;
165 }
166
167 static void __exit rtctimer_exit(void)
168 {
169         if (rtctimer) {
170                 snd_timer_global_unregister(rtctimer);
171                 rtctimer = NULL;
172         }
173 }
174
175
176 /*
177  * exported stuff
178  */
179 module_init(rtctimer_init)
180 module_exit(rtctimer_exit)
181
182 module_param(rtctimer_freq, int, 0444);
183 MODULE_PARM_DESC(rtctimer_freq, "timer frequency in Hz");
184
185 MODULE_LICENSE("GPL");
186
187 MODULE_ALIAS("snd-timer-" __stringify(SNDRV_TIMER_GLOBAL_RTC));
188
189 #endif /* CONFIG_RTC || CONFIG_RTC_MODULE */