hrtimer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ALSA timer back-end using hrtimer
  4. * Copyright (C) 2008 Takashi Iwai
  5. */
  6. #include <linux/init.h>
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/hrtimer.h>
  11. #include <sound/core.h>
  12. #include <sound/timer.h>
  13. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  14. MODULE_DESCRIPTION("ALSA hrtimer backend");
  15. MODULE_LICENSE("GPL");
  16. MODULE_ALIAS("snd-timer-" __stringify(SNDRV_TIMER_GLOBAL_HRTIMER));
  17. #define NANO_SEC 1000000000UL /* 10^9 in sec */
  18. static unsigned int resolution;
  19. struct snd_hrtimer {
  20. struct snd_timer *timer;
  21. struct hrtimer hrt;
  22. bool in_callback;
  23. };
  24. static enum hrtimer_restart snd_hrtimer_callback(struct hrtimer *hrt)
  25. {
  26. struct snd_hrtimer *stime = container_of(hrt, struct snd_hrtimer, hrt);
  27. struct snd_timer *t = stime->timer;
  28. ktime_t delta;
  29. unsigned long ticks;
  30. enum hrtimer_restart ret = HRTIMER_NORESTART;
  31. scoped_guard(spinlock, &t->lock) {
  32. if (!t->running)
  33. return HRTIMER_NORESTART; /* fast path */
  34. stime->in_callback = true;
  35. ticks = t->sticks;
  36. }
  37. /* calculate the drift */
  38. delta = ktime_sub(hrt->base->get_time(), hrtimer_get_expires(hrt));
  39. if (delta > 0)
  40. ticks += ktime_divns(delta, ticks * resolution);
  41. snd_timer_interrupt(stime->timer, ticks);
  42. guard(spinlock)(&t->lock);
  43. if (t->running) {
  44. hrtimer_add_expires_ns(hrt, t->sticks * resolution);
  45. ret = HRTIMER_RESTART;
  46. }
  47. stime->in_callback = false;
  48. return ret;
  49. }
  50. static int snd_hrtimer_open(struct snd_timer *t)
  51. {
  52. struct snd_hrtimer *stime;
  53. stime = kzalloc(sizeof(*stime), GFP_KERNEL);
  54. if (!stime)
  55. return -ENOMEM;
  56. hrtimer_init(&stime->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  57. stime->timer = t;
  58. stime->hrt.function = snd_hrtimer_callback;
  59. t->private_data = stime;
  60. return 0;
  61. }
  62. static int snd_hrtimer_close(struct snd_timer *t)
  63. {
  64. struct snd_hrtimer *stime = t->private_data;
  65. if (stime) {
  66. scoped_guard(spinlock_irq, &t->lock) {
  67. t->running = 0; /* just to be sure */
  68. stime->in_callback = 1; /* skip start/stop */
  69. }
  70. hrtimer_cancel(&stime->hrt);
  71. kfree(stime);
  72. t->private_data = NULL;
  73. }
  74. return 0;
  75. }
  76. static int snd_hrtimer_start(struct snd_timer *t)
  77. {
  78. struct snd_hrtimer *stime = t->private_data;
  79. if (stime->in_callback)
  80. return 0;
  81. hrtimer_start(&stime->hrt, ns_to_ktime(t->sticks * resolution),
  82. HRTIMER_MODE_REL);
  83. return 0;
  84. }
  85. static int snd_hrtimer_stop(struct snd_timer *t)
  86. {
  87. struct snd_hrtimer *stime = t->private_data;
  88. if (stime->in_callback)
  89. return 0;
  90. hrtimer_try_to_cancel(&stime->hrt);
  91. return 0;
  92. }
  93. static const struct snd_timer_hardware hrtimer_hw __initconst = {
  94. .flags = SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_WORK,
  95. .open = snd_hrtimer_open,
  96. .close = snd_hrtimer_close,
  97. .start = snd_hrtimer_start,
  98. .stop = snd_hrtimer_stop,
  99. };
  100. /*
  101. * entry functions
  102. */
  103. static struct snd_timer *mytimer;
  104. static int __init snd_hrtimer_init(void)
  105. {
  106. struct snd_timer *timer;
  107. int err;
  108. resolution = hrtimer_resolution;
  109. /* Create a new timer and set up the fields */
  110. err = snd_timer_global_new("hrtimer", SNDRV_TIMER_GLOBAL_HRTIMER,
  111. &timer);
  112. if (err < 0)
  113. return err;
  114. timer->module = THIS_MODULE;
  115. strcpy(timer->name, "HR timer");
  116. timer->hw = hrtimer_hw;
  117. timer->hw.resolution = resolution;
  118. timer->hw.ticks = NANO_SEC / resolution;
  119. timer->max_instances = 100; /* lower the limit */
  120. err = snd_timer_global_register(timer);
  121. if (err < 0) {
  122. snd_timer_global_free(timer);
  123. return err;
  124. }
  125. mytimer = timer; /* remember this */
  126. return 0;
  127. }
  128. static void __exit snd_hrtimer_exit(void)
  129. {
  130. if (mytimer) {
  131. snd_timer_global_free(mytimer);
  132. mytimer = NULL;
  133. }
  134. }
  135. module_init(snd_hrtimer_init);
  136. module_exit(snd_hrtimer_exit);