cam_common_util.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #ifndef _CAM_COMMON_UTIL_H_
  6. #define _CAM_COMMON_UTIL_H_
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #define CAM_BITS_MASK_SHIFT(x, mask, shift) (((x) & (mask)) >> shift)
  10. #define CAM_36BIT_INTF_GET_IOVA_BASE(iova) ((iova) >> 8)
  11. #define CAM_36BIT_INTF_GET_IOVA_OFFSET(iova) ((iova) & 0xff)
  12. #define PTR_TO_U64(ptr) ((uint64_t)(uintptr_t)ptr)
  13. #define U64_TO_PTR(ptr) ((void *)(uintptr_t)ptr)
  14. #define CAM_GET_TIMESTAMP(timestamp) ktime_get_real_ts64(&(timestamp))
  15. #define CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts_start, ts_end, diff_microsec) \
  16. ({ \
  17. diff_microsec = 0; \
  18. if (ts_end.tv_nsec >= ts_start.tv_nsec) { \
  19. diff_microsec = \
  20. (ts_end.tv_nsec - ts_start.tv_nsec) / 1000; \
  21. diff_microsec += \
  22. (ts_end.tv_sec - ts_start.tv_sec) * 1000 * 1000; \
  23. } else { \
  24. diff_microsec = \
  25. (ts_end.tv_nsec + \
  26. (1000*1000*1000 - ts_start.tv_nsec)) / 1000; \
  27. diff_microsec += \
  28. (ts_end.tv_sec - ts_start.tv_sec - 1) * 1000 * 1000; \
  29. } \
  30. })
  31. #define CAM_CONVERT_TIMESTAMP_FORMAT(ts, hrs, min, sec, ms) \
  32. ({ \
  33. uint64_t tmp = ((ts).tv_sec); \
  34. (ms) = ((ts).tv_nsec) / 1000000; \
  35. (sec) = do_div(tmp, 60); \
  36. (min) = do_div(tmp, 60); \
  37. (hrs) = do_div(tmp, 24); \
  38. })
  39. /**
  40. * cam_common_util_get_string_index()
  41. *
  42. * @brief Match the string from list of strings to return
  43. * matching index
  44. *
  45. * @strings: Pointer to list of strings
  46. * @num_strings: Number of strings in 'strings'
  47. * @matching_string: String to match
  48. * @index: Pointer to index to return matching index
  49. *
  50. * @return: 0 for success
  51. * -EINVAL for Fail
  52. */
  53. int cam_common_util_get_string_index(const char **strings,
  54. uint32_t num_strings, const char *matching_string, uint32_t *index);
  55. /**
  56. * cam_common_util_remove_duplicate_arr()
  57. *
  58. * @brief Move all the unique integers to the start of
  59. * the array and return the number of unique integers
  60. *
  61. * @array: Pointer to the first integer of array
  62. * @num: Number of elements in array
  63. *
  64. * @return: Number of unique integers in array
  65. */
  66. uint32_t cam_common_util_remove_duplicate_arr(int32_t *array,
  67. uint32_t num);
  68. /**
  69. * cam_common_wait_for_completion_timeout()
  70. *
  71. * @brief common interface to implement wait for completion
  72. * for slow environment like presil, single debug
  73. * timeout variable can take care
  74. *
  75. * @complete: Pointer to the first integer of array
  76. * @timeout_jiffies: Timeout value in jiffie
  77. *
  78. * @return: Remaining jiffies, non-zero for success, zero
  79. * in case of failure
  80. */
  81. unsigned long cam_common_wait_for_completion_timeout(
  82. struct completion *complete,
  83. unsigned long timeout_jiffies);
  84. /**
  85. * cam_common_read_poll_timeout()
  86. *
  87. * @brief common interface to read poll timeout
  88. *
  89. * @addr: Address of IO register
  90. * @delay: Delay interval of poll
  91. * @timeout: Timeout for poll
  92. * @mask: Mask to be checked
  93. * @check_val: Value to be compared to break poll
  94. * @status: Status of register of IO
  95. *
  96. * @return: 0 if success and negative if fail
  97. * */
  98. int cam_common_read_poll_timeout(
  99. void __iomem *addr,
  100. unsigned long delay,
  101. unsigned long timeout,
  102. uint32_t mask,
  103. uint32_t check_val,
  104. uint32_t *status);
  105. /**
  106. * cam_common_modify_timer()
  107. *
  108. * @brief common interface to modify timer,
  109. *
  110. * @timer: reference to system timer
  111. * @timeout_val: timeout value for timer
  112. *
  113. * @return: 0 if success and negative if fail
  114. */
  115. int cam_common_modify_timer(struct timer_list *timer, int32_t timeout_val);
  116. #endif /* _CAM_COMMON_UTIL_H_ */