123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #include <linux/init.h>
- #include <linux/interrupt.h>
- #include <linux/module.h>
- #include <linux/log2.h>
- #include <sound/core.h>
- #include <sound/timer.h>
- #if IS_ENABLED(CONFIG_RTC)
- #include <linux/mc146818rtc.h>
- #define RTC_FREQ 1024
- #define NANO_SEC 1000000000L
- static int rtctimer_open(struct snd_timer *t);
- static int rtctimer_close(struct snd_timer *t);
- static int rtctimer_start(struct snd_timer *t);
- static int rtctimer_stop(struct snd_timer *t);
- static struct snd_timer_hardware rtc_hw = {
- .flags = SNDRV_TIMER_HW_AUTO |
- SNDRV_TIMER_HW_FIRST |
- SNDRV_TIMER_HW_TASKLET,
- .ticks = 100000000L,
- .open = rtctimer_open,
- .close = rtctimer_close,
- .start = rtctimer_start,
- .stop = rtctimer_stop,
- };
- static int rtctimer_freq = RTC_FREQ;
- static struct snd_timer *rtctimer;
- static struct tasklet_struct rtc_tasklet;
- static rtc_task_t rtc_task;
- static int
- rtctimer_open(struct snd_timer *t)
- {
- int err;
- err = rtc_register(&rtc_task);
- if (err < 0)
- return err;
- t->private_data = &rtc_task;
- return 0;
- }
- static int
- rtctimer_close(struct snd_timer *t)
- {
- rtc_task_t *rtc = t->private_data;
- if (rtc) {
- rtc_unregister(rtc);
- tasklet_kill(&rtc_tasklet);
- t->private_data = NULL;
- }
- return 0;
- }
- static int
- rtctimer_start(struct snd_timer *timer)
- {
- rtc_task_t *rtc = timer->private_data;
- if (snd_BUG_ON(!rtc))
- return -EINVAL;
- rtc_control(rtc, RTC_IRQP_SET, rtctimer_freq);
- rtc_control(rtc, RTC_PIE_ON, 0);
- return 0;
- }
- static int
- rtctimer_stop(struct snd_timer *timer)
- {
- rtc_task_t *rtc = timer->private_data;
- if (snd_BUG_ON(!rtc))
- return -EINVAL;
- rtc_control(rtc, RTC_PIE_OFF, 0);
- return 0;
- }
- static void rtctimer_tasklet(unsigned long data)
- {
- snd_timer_interrupt((struct snd_timer *)data, 1);
- }
- static void rtctimer_interrupt(void *private_data)
- {
- tasklet_schedule(private_data);
- }
- static int __init rtctimer_init(void)
- {
- int err;
- struct snd_timer *timer;
- if (rtctimer_freq < 2 || rtctimer_freq > 8192 ||
- !is_power_of_2(rtctimer_freq)) {
- pr_err("ALSA: rtctimer: invalid frequency %d\n", rtctimer_freq);
- return -EINVAL;
- }
-
- err = snd_timer_global_new("rtc", SNDRV_TIMER_GLOBAL_RTC, &timer);
- if (err < 0)
- return err;
- timer->module = THIS_MODULE;
- strcpy(timer->name, "RTC timer");
- timer->hw = rtc_hw;
- timer->hw.resolution = NANO_SEC / rtctimer_freq;
- tasklet_init(&rtc_tasklet, rtctimer_tasklet, (unsigned long)timer);
-
- rtc_task.func = rtctimer_interrupt;
- rtc_task.private_data = &rtc_tasklet;
- err = snd_timer_global_register(timer);
- if (err < 0) {
- snd_timer_global_free(timer);
- return err;
- }
- rtctimer = timer;
- return 0;
- }
- static void __exit rtctimer_exit(void)
- {
- if (rtctimer) {
- snd_timer_global_free(rtctimer);
- rtctimer = NULL;
- }
- }
- module_init(rtctimer_init)
- module_exit(rtctimer_exit)
- module_param(rtctimer_freq, int, 0444);
- MODULE_PARM_DESC(rtctimer_freq, "timer frequency in Hz");
- MODULE_LICENSE("GPL");
- MODULE_ALIAS("snd-timer-" __stringify(SNDRV_TIMER_GLOBAL_RTC));
- #endif
|