rtctimer.c 4.1 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 <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/module.h>
  25. #include <linux/log2.h>
  26. #include <sound/core.h>
  27. #include <sound/timer.h>
  28. #if IS_ENABLED(CONFIG_RTC)
  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. if (snd_BUG_ON(!rtc))
  82. 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. if (snd_BUG_ON(!rtc))
  92. return -EINVAL;
  93. rtc_control(rtc, RTC_PIE_OFF, 0);
  94. return 0;
  95. }
  96. static void rtctimer_tasklet(unsigned long data)
  97. {
  98. snd_timer_interrupt((struct snd_timer *)data, 1);
  99. }
  100. /*
  101. * interrupt
  102. */
  103. static void rtctimer_interrupt(void *private_data)
  104. {
  105. tasklet_schedule(private_data);
  106. }
  107. /*
  108. * ENTRY functions
  109. */
  110. static int __init rtctimer_init(void)
  111. {
  112. int err;
  113. struct snd_timer *timer;
  114. if (rtctimer_freq < 2 || rtctimer_freq > 8192 ||
  115. !is_power_of_2(rtctimer_freq)) {
  116. pr_err("ALSA: rtctimer: invalid frequency %d\n", 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 /* IS_ENABLED(CONFIG_RTC) */