1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <sound/driver.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/moduleparam.h>
27#include <linux/log2.h>
28#include <sound/core.h>
29#include <sound/timer.h>
30
31#if defined(CONFIG_RTC) || defined(CONFIG_RTC_MODULE)
32
33#include <linux/mc146818rtc.h>
34
35#define RTC_FREQ 1024
36#define NANO_SEC 1000000000L
37
38
39
40
41static int rtctimer_open(struct snd_timer *t);
42static int rtctimer_close(struct snd_timer *t);
43static int rtctimer_start(struct snd_timer *t);
44static int rtctimer_stop(struct snd_timer *t);
45
46
47
48
49
50static struct snd_timer_hardware rtc_hw = {
51 .flags = SNDRV_TIMER_HW_AUTO |
52 SNDRV_TIMER_HW_FIRST |
53 SNDRV_TIMER_HW_TASKLET,
54 .ticks = 100000000L,
55 .open = rtctimer_open,
56 .close = rtctimer_close,
57 .start = rtctimer_start,
58 .stop = rtctimer_stop,
59};
60
61static int rtctimer_freq = RTC_FREQ;
62static struct snd_timer *rtctimer;
63static struct tasklet_struct rtc_tasklet;
64static rtc_task_t rtc_task;
65
66
67static int
68rtctimer_open(struct snd_timer *t)
69{
70 int err;
71
72 err = rtc_register(&rtc_task);
73 if (err < 0)
74 return err;
75 t->private_data = &rtc_task;
76 return 0;
77}
78
79static int
80rtctimer_close(struct snd_timer *t)
81{
82 rtc_task_t *rtc = t->private_data;
83 if (rtc) {
84 rtc_unregister(rtc);
85 tasklet_kill(&rtc_tasklet);
86 t->private_data = NULL;
87 }
88 return 0;
89}
90
91static int
92rtctimer_start(struct snd_timer *timer)
93{
94 rtc_task_t *rtc = timer->private_data;
95 snd_assert(rtc != NULL, return -EINVAL);
96 rtc_control(rtc, RTC_IRQP_SET, rtctimer_freq);
97 rtc_control(rtc, RTC_PIE_ON, 0);
98 return 0;
99}
100
101static int
102rtctimer_stop(struct snd_timer *timer)
103{
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;
108}
109
110static void rtctimer_tasklet(unsigned long data)
111{
112 snd_timer_interrupt((struct snd_timer *)data, 1);
113}
114
115
116
117
118static void rtctimer_interrupt(void *private_data)
119{
120 tasklet_hi_schedule(private_data);
121}
122
123
124
125
126
127static int __init rtctimer_init(void)
128{
129 int err;
130 struct snd_timer *timer;
131
132 if (rtctimer_freq < 2 || rtctimer_freq > 8192 ||
133 !is_power_of_2(rtctimer_freq)) {
134 snd_printk(KERN_ERR "rtctimer: invalid frequency %d\n",
135 rtctimer_freq);
136 return -EINVAL;
137 }
138
139
140 err = snd_timer_global_new("rtc", SNDRV_TIMER_GLOBAL_RTC, &timer);
141 if (err < 0)
142 return err;
143
144 timer->module = THIS_MODULE;
145 strcpy(timer->name, "RTC timer");
146 timer->hw = rtc_hw;
147 timer->hw.resolution = NANO_SEC / rtctimer_freq;
148
149 tasklet_init(&rtc_tasklet, rtctimer_tasklet, (unsigned long)timer);
150
151
152 rtc_task.func = rtctimer_interrupt;
153 rtc_task.private_data = &rtc_tasklet;
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;
161
162 return 0;
163}
164
165static void __exit rtctimer_exit(void)
166{
167 if (rtctimer) {
168 snd_timer_global_free(rtctimer);
169 rtctimer = NULL;
170 }
171}
172
173
174
175
176
177module_init(rtctimer_init)
178module_exit(rtctimer_exit)
179
180module_param(rtctimer_freq, int, 0444);
181MODULE_PARM_DESC(rtctimer_freq, "timer frequency in Hz");
182
183MODULE_LICENSE("GPL");
184
185MODULE_ALIAS("snd-timer-" __stringify(SNDRV_TIMER_GLOBAL_RTC));
186
187#endif
188