Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | |
| 49 | /* |
| 50 | * The hardware dependent description for this timer. |
| 51 | */ |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 52 | static struct snd_timer_hardware rtc_hw = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
| 59 | }; |
| 60 | |
| 61 | static int rtctimer_freq = RTC_FREQ; /* frequency */ |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 62 | static struct snd_timer *rtctimer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | static rtc_task_t rtc_task; |
| 64 | |
| 65 | |
| 66 | static int |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 67 | rtctimer_open(struct snd_timer *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 79 | rtctimer_close(struct snd_timer *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 90 | rtctimer_start(struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 100 | rtctimer_stop(struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
| 102 | rtc_task_t *rtc = timer->private_data; |
| 103 | snd_assert(rtc != NULL, return -EINVAL); |
| 104 | rtc_control(rtc, RTC_PIE_OFF, 0); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * interrupt |
| 110 | */ |
| 111 | static void rtctimer_interrupt(void *private_data) |
| 112 | { |
Clemens Ladisch | adf25df | 2005-09-27 15:56:28 +0200 | [diff] [blame] | 113 | snd_timer_interrupt(private_data, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
| 117 | /* |
| 118 | * ENTRY functions |
| 119 | */ |
| 120 | static int __init rtctimer_init(void) |
| 121 | { |
Clemens Ladisch | d9ad1bd | 2005-09-27 15:57:24 +0200 | [diff] [blame] | 122 | int err; |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 123 | struct snd_timer *timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Clemens Ladisch | d9ad1bd | 2005-09-27 15:57:24 +0200 | [diff] [blame] | 125 | if (rtctimer_freq < 2 || rtctimer_freq > 8192 || |
| 126 | (rtctimer_freq & (rtctimer_freq - 1)) != 0) { |
Clemens Ladisch | de24214 | 2005-10-12 17:12:31 +0200 | [diff] [blame] | 127 | snd_printk(KERN_ERR "rtctimer: invalid frequency %d\n", |
| 128 | rtctimer_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | return -EINVAL; |
| 130 | } |
| 131 | |
| 132 | /* Create a new timer and set up the fields */ |
| 133 | err = snd_timer_global_new("rtc", SNDRV_TIMER_GLOBAL_RTC, &timer); |
| 134 | if (err < 0) |
| 135 | return err; |
| 136 | |
Clemens Ladisch | de24214 | 2005-10-12 17:12:31 +0200 | [diff] [blame] | 137 | timer->module = THIS_MODULE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | strcpy(timer->name, "RTC timer"); |
| 139 | timer->hw = rtc_hw; |
| 140 | timer->hw.resolution = NANO_SEC / rtctimer_freq; |
| 141 | |
| 142 | /* set up RTC callback */ |
| 143 | rtc_task.func = rtctimer_interrupt; |
| 144 | rtc_task.private_data = timer; |
| 145 | |
| 146 | err = snd_timer_global_register(timer); |
| 147 | if (err < 0) { |
| 148 | snd_timer_global_free(timer); |
| 149 | return err; |
| 150 | } |
| 151 | rtctimer = timer; /* remember this */ |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static void __exit rtctimer_exit(void) |
| 157 | { |
| 158 | if (rtctimer) { |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 159 | snd_timer_global_free(rtctimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | rtctimer = NULL; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /* |
| 166 | * exported stuff |
| 167 | */ |
| 168 | module_init(rtctimer_init) |
| 169 | module_exit(rtctimer_exit) |
| 170 | |
| 171 | module_param(rtctimer_freq, int, 0444); |
| 172 | MODULE_PARM_DESC(rtctimer_freq, "timer frequency in Hz"); |
| 173 | |
| 174 | MODULE_LICENSE("GPL"); |
| 175 | |
| 176 | MODULE_ALIAS("snd-timer-" __stringify(SNDRV_TIMER_GLOBAL_RTC)); |
| 177 | |
| 178 | #endif /* CONFIG_RTC || CONFIG_RTC_MODULE */ |