cam_common_util.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /**
  32. * cam_common_util_get_string_index()
  33. *
  34. * @brief Match the string from list of strings to return
  35. * matching index
  36. *
  37. * @strings: Pointer to list of strings
  38. * @num_strings: Number of strings in 'strings'
  39. * @matching_string: String to match
  40. * @index: Pointer to index to return matching index
  41. *
  42. * @return: 0 for success
  43. * -EINVAL for Fail
  44. */
  45. int cam_common_util_get_string_index(const char **strings,
  46. uint32_t num_strings, const char *matching_string, uint32_t *index);
  47. /**
  48. * cam_common_util_remove_duplicate_arr()
  49. *
  50. * @brief Move all the unique integers to the start of
  51. * the array and return the number of unique integers
  52. *
  53. * @array: Pointer to the first integer of array
  54. * @num: Number of elements in array
  55. *
  56. * @return: Number of unique integers in array
  57. */
  58. uint32_t cam_common_util_remove_duplicate_arr(int32_t *array,
  59. uint32_t num);
  60. /**
  61. * cam_common_wait_for_completion_timeout()
  62. *
  63. * @brief common interface to implement wait for completion
  64. * for slow environment like presil, single debug
  65. * timeout variable can take care
  66. *
  67. * @complete: Pointer to the first integer of array
  68. * @timeout_jiffies: Timeout value in jiffie
  69. *
  70. * @return: Remaining jiffies, non-zero for success, zero
  71. * in case of failure
  72. */
  73. unsigned long cam_common_wait_for_completion_timeout(
  74. struct completion *complete,
  75. unsigned long timeout_jiffies);
  76. /**
  77. * cam_common_read_poll_timeout()
  78. *
  79. * @brief common interface to read poll timeout
  80. *
  81. * @addr: Address of IO register
  82. * @delay: Delay interval of poll
  83. * @timeout: Timeout for poll
  84. * @mask: Mask to be checked
  85. * @check_val: Value to be compared to break poll
  86. * @status: Status of register of IO
  87. *
  88. * @return: 0 if success and negative if fail
  89. * */
  90. int cam_common_read_poll_timeout(
  91. void __iomem *addr,
  92. unsigned long delay,
  93. unsigned long timeout,
  94. uint32_t mask,
  95. uint32_t check_val,
  96. uint32_t *status);
  97. /**
  98. * cam_common_modify_timer()
  99. *
  100. * @brief common interface to modify timer,
  101. *
  102. * @timer: reference to system timer
  103. * @timeout_val: timeout value for timer
  104. *
  105. * @return: 0 if success and negative if fail
  106. */
  107. int cam_common_modify_timer(struct timer_list *timer, int32_t timeout_val);
  108. #endif /* _CAM_COMMON_UTIL_H_ */