cam_common_util.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 PTR_TO_U64(ptr) ((uint64_t)(uintptr_t)ptr)
  11. #define U64_TO_PTR(ptr) ((void *)(uintptr_t)ptr)
  12. #define CAM_GET_TIMESTAMP(timestamp) ktime_get_real_ts64(&(timestamp))
  13. #define CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts_start, ts_end, diff_microsec) \
  14. ({ \
  15. diff_microsec = 0; \
  16. if (ts_end.tv_nsec >= ts_start.tv_nsec) { \
  17. diff_microsec = \
  18. (ts_end.tv_nsec - ts_start.tv_nsec) / 1000; \
  19. diff_microsec += \
  20. (ts_end.tv_sec - ts_start.tv_sec) * 1000 * 1000; \
  21. } else { \
  22. diff_microsec = \
  23. (ts_end.tv_nsec + \
  24. (1000*1000*1000 - ts_start.tv_nsec)) / 1000; \
  25. diff_microsec += \
  26. (ts_end.tv_sec - ts_start.tv_sec - 1) * 1000 * 1000; \
  27. } \
  28. })
  29. /**
  30. * cam_common_util_get_string_index()
  31. *
  32. * @brief Match the string from list of strings to return
  33. * matching index
  34. *
  35. * @strings: Pointer to list of strings
  36. * @num_strings: Number of strings in 'strings'
  37. * @matching_string: String to match
  38. * @index: Pointer to index to return matching index
  39. *
  40. * @return: 0 for success
  41. * -EINVAL for Fail
  42. */
  43. int cam_common_util_get_string_index(const char **strings,
  44. uint32_t num_strings, const char *matching_string, uint32_t *index);
  45. /**
  46. * cam_common_util_remove_duplicate_arr()
  47. *
  48. * @brief Move all the unique integers to the start of
  49. * the array and return the number of unique integers
  50. *
  51. * @array: Pointer to the first integer of array
  52. * @num: Number of elements in array
  53. *
  54. * @return: Number of unique integers in array
  55. */
  56. uint32_t cam_common_util_remove_duplicate_arr(int32_t *array,
  57. uint32_t num);
  58. /**
  59. * cam_common_wait_for_completion_timeout()
  60. *
  61. * @brief common interface to implement wait for completion
  62. * for slow environment like presil, single debug
  63. * timeout variable can take care
  64. *
  65. * @complete: Pointer to the first integer of array
  66. * @timeout_jiffies: Timeout value in jiffie
  67. *
  68. * @return: Remaining jiffies, non-zero for success, zero
  69. * in case of failure
  70. */
  71. unsigned long cam_common_wait_for_completion_timeout(
  72. struct completion *complete,
  73. unsigned long timeout_jiffies);
  74. /**
  75. * cam_common_read_poll_timeout()
  76. *
  77. * @brief common interface to read poll timeout
  78. *
  79. * @addr: Address of IO register
  80. * @delay: Delay interval of poll
  81. * @timeout: Timeout for poll
  82. * @mask: Mask to be checked
  83. * @check_val: Value to be compared to break poll
  84. * @status: Status of register of IO
  85. *
  86. * @return: 0 if success and negative if fail
  87. * */
  88. int cam_common_read_poll_timeout(
  89. void __iomem *addr,
  90. unsigned long delay,
  91. unsigned long timeout,
  92. uint32_t mask,
  93. uint32_t check_val,
  94. uint32_t *status);
  95. #endif /* _CAM_COMMON_UTIL_H_ */