blob: 825991e2223a0e5f8a782d97c4f9454e0048e40a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Masahiro Yamadaa4f0b8e2016-12-28 00:36:00 +09002
3#ifndef _TIME_H
4#define _TIME_H
5
Masahiro Yamada267cab62016-12-28 00:36:02 +09006#include <linux/typecheck.h>
7
Masahiro Yamadaa4f0b8e2016-12-28 00:36:00 +09008unsigned long get_timer(unsigned long base);
9
10/*
11 * Return the current value of a monotonically increasing microsecond timer.
12 * Granularity may be larger than 1us if hardware does not support this.
13 */
14unsigned long timer_get_us(void);
15
Masahiro Yamada267cab62016-12-28 00:36:02 +090016/*
17 * These inlines deal with timer wrapping correctly. You are
18 * strongly encouraged to use them
19 * 1. Because people otherwise forget
20 * 2. Because if the timer wrap changes in future you won't have to
21 * alter your driver code.
22 *
23 * time_after(a,b) returns true if the time a is after time b.
24 *
25 * Do this with "<0" and ">=0" to only test the sign of the result. A
26 * good compiler would generate better code (and a really good compiler
27 * wouldn't care). Gcc is currently neither.
28 */
29#define time_after(a,b) \
30 (typecheck(unsigned long, a) && \
31 typecheck(unsigned long, b) && \
32 ((long)((b) - (a)) < 0))
33#define time_before(a,b) time_after(b,a)
34
35#define time_after_eq(a,b) \
36 (typecheck(unsigned long, a) && \
37 typecheck(unsigned long, b) && \
38 ((long)((a) - (b)) >= 0))
39#define time_before_eq(a,b) time_after_eq(b,a)
40
41/*
42 * Calculate whether a is in the range of [b, c].
43 */
44#define time_in_range(a,b,c) \
45 (time_after_eq(a,b) && \
46 time_before_eq(a,c))
47
48/*
49 * Calculate whether a is in the range of [b, c).
50 */
51#define time_in_range_open(a,b,c) \
52 (time_after_eq(a,b) && \
53 time_before(a,c))
54
Masahiro Yamadaa4f0b8e2016-12-28 00:36:00 +090055#endif /* _TIME_H */