rtctimer.c 4.2 KB

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