rtctimer.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/log2.h>
  26. #include <sound/core.h>
  27. #include <sound/timer.h>
  28. #if defined(CONFIG_RTC) || defined(CONFIG_RTC_MODULE)
  29. #include <linux/mc146818rtc.h>
  30. #define RTC_FREQ 1024 /* default frequency */
  31. #define NANO_SEC 1000000000L /* 10^9 in sec */
  32. /*
  33. * prototypes
  34. */
  35. static int rtctimer_open(struct snd_timer *t);
  36. static int rtctimer_close(struct snd_timer *t);
  37. static int rtctimer_start(struct snd_timer *t);
  38. static int rtctimer_stop(struct snd_timer *t);
  39. /*
  40. * The hardware dependent description for this timer.
  41. */
  42. static struct snd_timer_hardware rtc_hw = {
  43. .flags = SNDRV_TIMER_HW_AUTO |
  44. SNDRV_TIMER_HW_FIRST |
  45. SNDRV_TIMER_HW_TASKLET,
  46. .ticks = 100000000L, /* FIXME: XXX */
  47. .open = rtctimer_open,
  48. .close = rtctimer_close,
  49. .start = rtctimer_start,
  50. .stop = rtctimer_stop,
  51. };
  52. static int rtctimer_freq = RTC_FREQ; /* frequency */
  53. static struct snd_timer *rtctimer;
  54. static struct tasklet_struct rtc_tasklet;
  55. static rtc_task_t rtc_task;
  56. static int
  57. rtctimer_open(struct snd_timer *t)
  58. {
  59. int err;
  60. err = rtc_register(&rtc_task);
  61. if (err < 0)
  62. return err;
  63. t->private_data = &rtc_task;
  64. return 0;
  65. }
  66. static int
  67. rtctimer_close(struct snd_timer *t)
  68. {
  69. rtc_task_t *rtc = t->private_data;
  70. if (rtc) {
  71. rtc_unregister(rtc);
  72. tasklet_kill(&rtc_tasklet);
  73. t->private_data = NULL;
  74. }
  75. return 0;
  76. }
  77. static int
  78. rtctimer_start(struct snd_timer *timer)
  79. {
  80. rtc_task_t *rtc = timer->private_data;
  81. snd_assert(rtc != NULL, return -EINVAL);
  82. rtc_control(rtc, RTC_IRQP_SET, rtctimer_freq);
  83. rtc_control(rtc, RTC_PIE_ON, 0);
  84. return 0;
  85. }
  86. static int
  87. rtctimer_stop(struct snd_timer *timer)
  88. {
  89. rtc_task_t *rtc = timer->private_data;
  90. snd_assert(rtc != NULL, return -EINVAL);
  91. rtc_control(rtc, RTC_PIE_OFF, 0);
  92. return 0;
  93. }
  94. static void rtctimer_tasklet(unsigned long data)
  95. {
  96. snd_timer_interrupt((struct snd_timer *)data, 1);
  97. }
  98. /*
  99. * interrupt
  100. */
  101. static void rtctimer_interrupt(void *private_data)
  102. {
  103. tasklet_hi_schedule(private_data);
  104. }
  105. /*
  106. * ENTRY functions
  107. */
  108. static int __init rtctimer_init(void)
  109. {
  110. int err;
  111. struct snd_timer *timer;
  112. if (rtctimer_freq < 2 || rtctimer_freq > 8192 ||
  113. !is_power_of_2(rtctimer_freq)) {
  114. snd_printk(KERN_ERR "rtctimer: invalid frequency %d\n",
  115. rtctimer_freq);
  116. return -EINVAL;
  117. }
  118. /* Create a new timer and set up the fields */
  119. err = snd_timer_global_new("rtc", SNDRV_TIMER_GLOBAL_RTC, &timer);
  120. if (err < 0)
  121. return err;
  122. timer->module = THIS_MODULE;
  123. strcpy(timer->name, "RTC timer");
  124. timer->hw = rtc_hw;
  125. timer->hw.resolution = NANO_SEC / rtctimer_freq;
  126. tasklet_init(&rtc_tasklet, rtctimer_tasklet, (unsigned long)timer);
  127. /* set up RTC callback */
  128. rtc_task.func = rtctimer_interrupt;
  129. rtc_task.private_data = &rtc_tasklet;
  130. err = snd_timer_global_register(timer);
  131. if (err < 0) {
  132. snd_timer_global_free(timer);
  133. return err;
  134. }
  135. rtctimer = timer; /* remember this */
  136. return 0;
  137. }
  138. static void __exit rtctimer_exit(void)
  139. {
  140. if (rtctimer) {
  141. snd_timer_global_free(rtctimer);
  142. rtctimer = NULL;
  143. }
  144. }
  145. /*
  146. * exported stuff
  147. */
  148. module_init(rtctimer_init)
  149. module_exit(rtctimer_exit)
  150. module_param(rtctimer_freq, int, 0444);
  151. MODULE_PARM_DESC(rtctimer_freq, "timer frequency in Hz");
  152. MODULE_LICENSE("GPL");
  153. MODULE_ALIAS("snd-timer-" __stringify(SNDRV_TIMER_GLOBAL_RTC));
  154. #endif /* CONFIG_RTC || CONFIG_RTC_MODULE */