thermal_zone_internal.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2023-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #ifndef __QTI_THERMAL_ZONE_INTERNAL_H
  6. #define __QTI_THERMAL_ZONE_INTERNAL_H
  7. #include <linux/thermal.h>
  8. #include <trace/hooks/thermal.h>
  9. #include "../thermal_core.h"
  10. /* Generic helpers for thermal zone -> change_mode ops */
  11. static inline __maybe_unused int qti_tz_change_mode(struct thermal_zone_device *tz,
  12. enum thermal_device_mode mode)
  13. {
  14. struct thermal_instance *instance;
  15. if (!tz)
  16. return 0;
  17. tz->passive = 0;
  18. tz->temperature = THERMAL_TEMP_INVALID;
  19. tz->prev_low_trip = -INT_MAX;
  20. tz->prev_high_trip = INT_MAX;
  21. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  22. instance->initialized = false;
  23. if (mode == THERMAL_DEVICE_DISABLED) {
  24. instance->target = THERMAL_NO_TARGET;
  25. instance->cdev->updated = false;
  26. thermal_cdev_update(instance->cdev);
  27. }
  28. }
  29. return 0;
  30. }
  31. static inline __maybe_unused int qti_update_tz_ops(struct thermal_zone_device *tz, bool enable)
  32. {
  33. if (!tz || !tz->ops)
  34. return -EINVAL;
  35. mutex_lock(&tz->lock);
  36. if (enable) {
  37. if (!tz->ops->change_mode)
  38. tz->ops->change_mode = qti_tz_change_mode;
  39. } else {
  40. tz->ops->change_mode = NULL;
  41. }
  42. mutex_unlock(&tz->lock);
  43. return 0;
  44. }
  45. static void disable_cdev_stats(void *unused,
  46. struct thermal_cooling_device *cdev, bool *disable)
  47. {
  48. *disable = true;
  49. }
  50. /* Generic thermal vendor hooks initialization API */
  51. static inline __maybe_unused void thermal_vendor_hooks_init(void)
  52. {
  53. int ret;
  54. ret = register_trace_android_vh_disable_thermal_cooling_stats(
  55. disable_cdev_stats, NULL);
  56. if (ret) {
  57. pr_err("Failed to register disable thermal cdev stats hooks\n");
  58. return;
  59. }
  60. }
  61. static inline __maybe_unused void thermal_vendor_hooks_exit(void)
  62. {
  63. unregister_trace_android_vh_disable_thermal_cooling_stats(
  64. disable_cdev_stats, NULL);
  65. }
  66. /*Generic helpers for thermal zone -> get_trend ops */
  67. static __maybe_unused inline int qti_tz_get_trend(
  68. struct thermal_zone_device *tz, int trip,
  69. enum thermal_trend *trend)
  70. {
  71. int trip_temp = 0, trip_hyst = 0, temp, ret;
  72. struct thermal_instance *instance;
  73. bool monitor_trip_only = false;
  74. if (!tz)
  75. return -EINVAL;
  76. ret = tz->ops->get_trip_temp(tz, trip, &trip_temp);
  77. if (ret)
  78. return ret;
  79. if (tz->ops->get_trip_hyst) {
  80. ret = tz->ops->get_trip_hyst(tz, trip, &trip_hyst);
  81. if (ret)
  82. return ret;
  83. }
  84. if (!trip_hyst)
  85. return -EINVAL;
  86. temp = READ_ONCE(tz->temperature);
  87. /*
  88. * Handle only monitor trip clear condition, fallback to default
  89. * trend estimation for all other cases.
  90. * If all the instances of a given trip are monitor type(upper == lower),
  91. * then only treat this trip as monitor trip and consider hysterisis for
  92. * clear condition
  93. */
  94. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  95. if (trip != instance->trip)
  96. continue;
  97. if (instance->lower != instance->upper)
  98. return -EINVAL;
  99. monitor_trip_only = true;
  100. }
  101. if (monitor_trip_only && temp < trip_temp &&
  102. (temp > (trip_temp - trip_hyst))) {
  103. *trend = THERMAL_TREND_STABLE;
  104. return 0;
  105. }
  106. return -EINVAL;
  107. }
  108. #endif // __QTI_THERMAL_ZONE_INTERNAL_H