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