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