rtctimer.c 4.2 KB

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