blob: 7cd5e8f5d4cebc2375f7303ce00d48e09046fd96 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/interrupt.h>
26#include <linux/moduleparam.h>
vignesh babu62e96a12007-02-22 13:23:01 +010027#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include <sound/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#if defined(CONFIG_RTC) || defined(CONFIG_RTC_MODULE)
32
33#include <linux/mc146818rtc.h>
34
35#define RTC_FREQ 1024 /* default frequency */
36#define NANO_SEC 1000000000L /* 10^9 in sec */
37
38/*
39 * prototypes
40 */
Takashi Iwai53d2f742005-11-17 13:56:05 +010041static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46
47/*
48 * The hardware dependent description for this timer.
49 */
Takashi Iwai53d2f742005-11-17 13:56:05 +010050static struct snd_timer_hardware rtc_hw = {
Clemens Ladischac5d1a72006-10-27 10:45:00 +020051 .flags = SNDRV_TIMER_HW_AUTO |
52 SNDRV_TIMER_HW_FIRST |
53 SNDRV_TIMER_HW_TASKLET,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .ticks = 100000000L, /* FIXME: XXX */
55 .open = rtctimer_open,
56 .close = rtctimer_close,
57 .start = rtctimer_start,
58 .stop = rtctimer_stop,
59};
60
61static int rtctimer_freq = RTC_FREQ; /* frequency */
Takashi Iwai53d2f742005-11-17 13:56:05 +010062static struct snd_timer *rtctimer;
Clemens Ladischac5d1a72006-10-27 10:45:00 +020063static struct tasklet_struct rtc_tasklet;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static rtc_task_t rtc_task;
65
66
67static int
Takashi Iwai53d2f742005-11-17 13:56:05 +010068rtctimer_open(struct snd_timer *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
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
Takashi Iwai53d2f742005-11-17 13:56:05 +010080rtctimer_close(struct snd_timer *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 rtc_task_t *rtc = t->private_data;
83 if (rtc) {
84 rtc_unregister(rtc);
Clemens Ladischac5d1a72006-10-27 10:45:00 +020085 tasklet_kill(&rtc_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 t->private_data = NULL;
87 }
88 return 0;
89}
90
91static int
Takashi Iwai53d2f742005-11-17 13:56:05 +010092rtctimer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return 0;
99}
100
101static int
Takashi Iwai53d2f742005-11-17 13:56:05 +0100102rtctimer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
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
Clemens Ladischac5d1a72006-10-27 10:45:00 +0200110static void rtctimer_tasklet(unsigned long data)
111{
112 snd_timer_interrupt((struct snd_timer *)data, 1);
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/*
116 * interrupt
117 */
118static void rtctimer_interrupt(void *private_data)
119{
Clemens Ladischac5d1a72006-10-27 10:45:00 +0200120 tasklet_hi_schedule(private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123
124/*
125 * ENTRY functions
126 */
127static int __init rtctimer_init(void)
128{
Clemens Ladischd9ad1bd2005-09-27 15:57:24 +0200129 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100130 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Clemens Ladischd9ad1bd2005-09-27 15:57:24 +0200132 if (rtctimer_freq < 2 || rtctimer_freq > 8192 ||
vignesh babu62e96a12007-02-22 13:23:01 +0100133 !is_power_of_2(rtctimer_freq)) {
Clemens Ladischde242142005-10-12 17:12:31 +0200134 snd_printk(KERN_ERR "rtctimer: invalid frequency %d\n",
135 rtctimer_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return -EINVAL;
137 }
138
139 /* Create a new timer and set up the fields */
140 err = snd_timer_global_new("rtc", SNDRV_TIMER_GLOBAL_RTC, &timer);
141 if (err < 0)
142 return err;
143
Clemens Ladischde242142005-10-12 17:12:31 +0200144 timer->module = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 strcpy(timer->name, "RTC timer");
146 timer->hw = rtc_hw;
147 timer->hw.resolution = NANO_SEC / rtctimer_freq;
148
Clemens Ladischac5d1a72006-10-27 10:45:00 +0200149 tasklet_init(&rtc_tasklet, rtctimer_tasklet, (unsigned long)timer);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* set up RTC callback */
152 rtc_task.func = rtctimer_interrupt;
Clemens Ladischac5d1a72006-10-27 10:45:00 +0200153 rtc_task.private_data = &rtc_tasklet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
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
165static void __exit rtctimer_exit(void)
166{
167 if (rtctimer) {
Takashi Iwaic4614822006-06-23 14:38:23 +0200168 snd_timer_global_free(rtctimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 rtctimer = NULL;
170 }
171}
172
173
174/*
175 * exported stuff
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 /* CONFIG_RTC || CONFIG_RTC_MODULE */