time.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. #ifndef _TIME_H
  3. #define _TIME_H
  4. #include <linux/typecheck.h>
  5. #include <linux/types.h>
  6. unsigned long get_timer(unsigned long base);
  7. /*
  8. * Return the current value of a monotonically increasing microsecond timer.
  9. * Granularity may be larger than 1us if hardware does not support this.
  10. */
  11. unsigned long timer_get_us(void);
  12. uint64_t get_timer_us(uint64_t base);
  13. /*
  14. * timer_test_add_offset()
  15. *
  16. * Allow tests to add to the time reported through lib/time.c functions
  17. * offset: number of milliseconds to advance the system time
  18. */
  19. void timer_test_add_offset(unsigned long offset);
  20. /**
  21. * usec_to_tick() - convert microseconds to clock ticks
  22. *
  23. * @usec: duration in microseconds
  24. * Return: duration in clock ticks
  25. */
  26. uint64_t usec_to_tick(unsigned long usec);
  27. /*
  28. * These inlines deal with timer wrapping correctly. You are
  29. * strongly encouraged to use them
  30. * 1. Because people otherwise forget
  31. * 2. Because if the timer wrap changes in future you won't have to
  32. * alter your driver code.
  33. *
  34. * time_after(a,b) returns true if the time a is after time b.
  35. *
  36. * Do this with "<0" and ">=0" to only test the sign of the result. A
  37. * good compiler would generate better code (and a really good compiler
  38. * wouldn't care). Gcc is currently neither.
  39. */
  40. #define time_after(a,b) \
  41. (typecheck(unsigned long, a) && \
  42. typecheck(unsigned long, b) && \
  43. ((long)((b) - (a)) < 0))
  44. #define time_before(a,b) time_after(b,a)
  45. #define time_after_eq(a,b) \
  46. (typecheck(unsigned long, a) && \
  47. typecheck(unsigned long, b) && \
  48. ((long)((a) - (b)) >= 0))
  49. #define time_before_eq(a,b) time_after_eq(b,a)
  50. /*
  51. * Calculate whether a is in the range of [b, c].
  52. */
  53. #define time_in_range(a,b,c) \
  54. (time_after_eq(a,b) && \
  55. time_before_eq(a,c))
  56. /*
  57. * Calculate whether a is in the range of [b, c).
  58. */
  59. #define time_in_range_open(a,b,c) \
  60. (time_after_eq(a,b) && \
  61. time_before(a,c))
  62. /**
  63. * usec2ticks() - Convert microseconds to internal ticks
  64. *
  65. * @usec: Value of microseconds to convert
  66. * @return Corresponding internal ticks value, calculated using get_tbclk()
  67. */
  68. ulong usec2ticks(unsigned long usec);
  69. /**
  70. * ticks2usec() - Convert internal ticks to microseconds
  71. *
  72. * @ticks: Value of ticks to convert
  73. * @return Corresponding microseconds value, calculated using get_tbclk()
  74. */
  75. ulong ticks2usec(unsigned long ticks);
  76. /**
  77. * wait_ticks() - waits a given number of ticks
  78. *
  79. * This is an internal function typically used to implement udelay() and
  80. * similar. Normally you should use udelay() or mdelay() instead.
  81. *
  82. * @ticks: Number of ticks to wait
  83. */
  84. void wait_ticks(unsigned long ticks);
  85. /**
  86. * timer_get_us() - Get monotonic microsecond timer
  87. *
  88. * @return value of monotonic microsecond timer
  89. */
  90. unsigned long timer_get_us(void);
  91. /**
  92. * get_ticks() - Get the current tick value
  93. *
  94. * This is an internal value used by the timer on the system. Ticks increase
  95. * monotonically at the rate given by get_tbclk().
  96. *
  97. * @return current tick value
  98. */
  99. uint64_t get_ticks(void);
  100. #endif /* _TIME_H */