rtctimer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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(struct snd_timer *t);
  39. static int rtctimer_close(struct snd_timer *t);
  40. static int rtctimer_start(struct snd_timer *t);
  41. static int rtctimer_stop(struct snd_timer *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 struct snd_timer *rtctimer;
  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. t->private_data = NULL;
  73. }
  74. return 0;
  75. }
  76. static int
  77. rtctimer_start(struct snd_timer *timer)
  78. {
  79. rtc_task_t *rtc = timer->private_data;
  80. snd_assert(rtc != NULL, return -EINVAL);
  81. rtc_control(rtc, RTC_IRQP_SET, rtctimer_freq);
  82. rtc_control(rtc, RTC_PIE_ON, 0);
  83. return 0;
  84. }
  85. static int
  86. rtctimer_stop(struct snd_timer *timer)
  87. {
  88. rtc_task_t *rtc = timer->private_data;
  89. snd_assert(rtc != NULL, return -EINVAL);
  90. rtc_control(rtc, RTC_PIE_OFF, 0);
  91. return 0;
  92. }
  93. /*
  94. * interrupt
  95. */
  96. static void rtctimer_interrupt(void *private_data)
  97. {
  98. snd_timer_interrupt(private_data, 1);
  99. }
  100. /*
  101. * ENTRY functions
  102. */
  103. static int __init rtctimer_init(void)
  104. {
  105. int err;
  106. struct snd_timer *timer;
  107. if (rtctimer_freq < 2 || rtctimer_freq > 8192 ||
  108. (rtctimer_freq & (rtctimer_freq - 1)) != 0) {
  109. snd_printk(KERN_ERR "rtctimer: invalid frequency %d\n",
  110. rtctimer_freq);
  111. return -EINVAL;
  112. }
  113. /* Create a new timer and set up the fields */
  114. err = snd_timer_global_new("rtc", SNDRV_TIMER_GLOBAL_RTC, &timer);
  115. if (err < 0)
  116. return err;
  117. timer->module = THIS_MODULE;
  118. strcpy(timer->name, "RTC timer");
  119. timer->hw = rtc_hw;
  120. timer->hw.resolution = NANO_SEC / rtctimer_freq;
  121. /* set up RTC callback */
  122. rtc_task.func = rtctimer_interrupt;
  123. rtc_task.private_data = timer;
  124. err = snd_timer_global_register(timer);
  125. if (err < 0) {
  126. snd_timer_global_free(timer);
  127. return err;
  128. }
  129. rtctimer = timer; /* remember this */
  130. return 0;
  131. }
  132. static void __exit rtctimer_exit(void)
  133. {
  134. if (rtctimer) {
  135. snd_timer_global_unregister(rtctimer);
  136. rtctimer = NULL;
  137. }
  138. }
  139. /*
  140. * exported stuff
  141. */
  142. module_init(rtctimer_init)
  143. module_exit(rtctimer_exit)
  144. module_param(rtctimer_freq, int, 0444);
  145. MODULE_PARM_DESC(rtctimer_freq, "timer frequency in Hz");
  146. MODULE_LICENSE("GPL");
  147. MODULE_ALIAS("snd-timer-" __stringify(SNDRV_TIMER_GLOBAL_RTC));
  148. #endif /* CONFIG_RTC || CONFIG_RTC_MODULE */