time.h 3.0 KB

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