ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 <sound/core.h>
29 #include <sound/timer.h>
30 #include <sound/info.h>
31
32 #if defined(CONFIG_RTC) || defined(CONFIG_RTC_MODULE)
33
34 #include <linux/mc146818rtc.h>
35
36 #define RTC_FREQ        1024            /* default frequency */
37 #define NANO_SEC        1000000000L     /* 10^9 in sec */
38
39 /*
40  * prototypes
41  */
42 static int rtctimer_open(snd_timer_t *t);
43 static int rtctimer_close(snd_timer_t *t);
44 static int rtctimer_start(snd_timer_t *t);
45 static int rtctimer_stop(snd_timer_t *t);
46
47
48 /*
49  * The hardware dependent description for this timer.
50  */
51 static struct _snd_timer_hardware rtc_hw = {
52         .flags =        SNDRV_TIMER_HW_FIRST|SNDRV_TIMER_HW_AUTO,
53         .ticks =        100000000L,             /* FIXME: XXX */
54         .open =         rtctimer_open,
55         .close =        rtctimer_close,
56         .start =        rtctimer_start,
57         .stop =         rtctimer_stop,
58 };
59
60 static int rtctimer_freq = RTC_FREQ;            /* frequency */
61 static snd_timer_t *rtctimer;
62 static atomic_t rtc_inc = ATOMIC_INIT(0);
63 static rtc_task_t rtc_task;
64
65
66 static int
67 rtctimer_open(snd_timer_t *t)
68 {
69         int err;
70
71         err = rtc_register(&rtc_task);
72         if (err < 0)
73                 return err;
74         t->private_data = &rtc_task;
75         return 0;
76 }
77
78 static int
79 rtctimer_close(snd_timer_t *t)
80 {
81         rtc_task_t *rtc = t->private_data;
82         if (rtc) {
83                 rtc_unregister(rtc);
84                 t->private_data = NULL;
85         }
86         return 0;
87 }
88
89 static int
90 rtctimer_start(snd_timer_t *timer)
91 {
92         rtc_task_t *rtc = timer->private_data;
93         snd_assert(rtc != NULL, return -EINVAL);
94         rtc_control(rtc, RTC_IRQP_SET, rtctimer_freq);
95         rtc_control(rtc, RTC_PIE_ON, 0);
96         atomic_set(&rtc_inc, 0);
97         return 0;
98 }
99
100 static int
101 rtctimer_stop(snd_timer_t *timer)
102 {
103         rtc_task_t *rtc = timer->private_data;
104         snd_assert(rtc != NULL, return -EINVAL);
105         rtc_control(rtc, RTC_PIE_OFF, 0);
106         return 0;
107 }
108
109 /*
110  * interrupt
111  */
112 static void rtctimer_interrupt(void *private_data)
113 {
114         int ticks;
115
116         atomic_inc(&rtc_inc);
117         ticks = atomic_read(&rtc_inc);
118         snd_timer_interrupt((snd_timer_t*)private_data, ticks);
119         atomic_sub(ticks, &rtc_inc);
120 }
121
122
123 /*
124  *  ENTRY functions
125  */
126 static int __init rtctimer_init(void)
127 {
128         int order, err;
129         snd_timer_t *timer;
130
131         if (rtctimer_freq < 2 || rtctimer_freq > 8192) {
132                 snd_printk(KERN_ERR "rtctimer: invalid frequency %d\n", rtctimer_freq);
133                 return -EINVAL;
134         }
135         for (order = 1; rtctimer_freq > order; order <<= 1)
136                 ;
137         if (rtctimer_freq != order) {
138                 snd_printk(KERN_ERR "rtctimer: invalid frequency %d\n", rtctimer_freq);
139                 return -EINVAL;
140         }
141
142         /* Create a new timer and set up the fields */
143         err = snd_timer_global_new("rtc", SNDRV_TIMER_GLOBAL_RTC, &timer);
144         if (err < 0)
145                 return err;
146
147         strcpy(timer->name, "RTC timer");
148         timer->hw = rtc_hw;
149         timer->hw.resolution = NANO_SEC / rtctimer_freq;
150
151         /* set up RTC callback */
152         rtc_task.func = rtctimer_interrupt;
153         rtc_task.private_data = timer;
154
155         err = snd_timer_global_register(timer);
156         if (err < 0) {
157                 snd_timer_global_free(timer);
158                 return err;
159         }
160         rtctimer = timer; /* remember this */
161
162         return 0;
163 }
164
165 static void __exit rtctimer_exit(void)
166 {
167         if (rtctimer) {
168                 snd_timer_global_unregister(rtctimer);
169                 rtctimer = NULL;
170         }
171 }
172
173
174 /*
175  * exported stuff
176  */
177 module_init(rtctimer_init)
178 module_exit(rtctimer_exit)
179
180 MODULE_PARM(rtctimer_freq, "i");
181 MODULE_PARM_DESC(rtctimer_freq, "timer frequency in Hz");
182
183 MODULE_LICENSE("GPL");
184
185 #ifndef MODULE
186 /* format is: snd-rtctimer=freq */
187
188 static int __init rtctimer_setup(char *str)
189 {
190         (void)(get_option(&str,&rtctimer_freq) == 2);
191         return 1;
192 }
193
194 __setup("snd-rtctimer=", rtctimer_setup);
195 #endif /* ifndef MODULE */
196
197 #endif /* CONFIG_RTC || CONFIG_RTC_MODULE */