thermal_helpers.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * thermal_helpers.c - helper functions to handle thermal devices
  4. *
  5. * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
  6. *
  7. * Highly based on original thermal_core.c
  8. * Copyright (C) 2008 Intel Corp
  9. * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  10. * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/export.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/sysfs.h>
  19. #include "thermal_core.h"
  20. #include "thermal_trace.h"
  21. int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip)
  22. {
  23. enum thermal_trend trend;
  24. if (tz->emul_temperature || !tz->ops.get_trend ||
  25. tz->ops.get_trend(tz, trip, &trend)) {
  26. if (tz->temperature > tz->last_temperature)
  27. trend = THERMAL_TREND_RAISING;
  28. else if (tz->temperature < tz->last_temperature)
  29. trend = THERMAL_TREND_DROPPING;
  30. else
  31. trend = THERMAL_TREND_STABLE;
  32. }
  33. return trend;
  34. }
  35. static bool thermal_instance_present(struct thermal_zone_device *tz,
  36. struct thermal_cooling_device *cdev,
  37. const struct thermal_trip *trip)
  38. {
  39. const struct thermal_trip_desc *td = trip_to_trip_desc(trip);
  40. struct thermal_instance *ti;
  41. list_for_each_entry(ti, &td->thermal_instances, trip_node) {
  42. if (ti->cdev == cdev)
  43. return true;
  44. }
  45. return false;
  46. }
  47. bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz,
  48. const struct thermal_trip *trip,
  49. struct thermal_cooling_device *cdev)
  50. {
  51. bool ret;
  52. mutex_lock(&tz->lock);
  53. mutex_lock(&cdev->lock);
  54. ret = thermal_instance_present(tz, cdev, trip);
  55. mutex_unlock(&cdev->lock);
  56. mutex_unlock(&tz->lock);
  57. return ret;
  58. }
  59. EXPORT_SYMBOL_GPL(thermal_trip_is_bound_to_cdev);
  60. /**
  61. * __thermal_zone_get_temp() - returns the temperature of a thermal zone
  62. * @tz: a valid pointer to a struct thermal_zone_device
  63. * @temp: a valid pointer to where to store the resulting temperature.
  64. *
  65. * When a valid thermal zone reference is passed, it will fetch its
  66. * temperature and fill @temp.
  67. *
  68. * Both tz and tz->ops must be valid pointers when calling this function,
  69. * and the tz->ops.get_temp callback must be provided.
  70. * The function must be called under tz->lock.
  71. *
  72. * Return: On success returns 0, an error code otherwise
  73. */
  74. int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
  75. {
  76. const struct thermal_trip_desc *td;
  77. int crit_temp = INT_MAX;
  78. int ret = -EINVAL;
  79. lockdep_assert_held(&tz->lock);
  80. ret = tz->ops.get_temp(tz, temp);
  81. if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
  82. for_each_trip_desc(tz, td) {
  83. const struct thermal_trip *trip = &td->trip;
  84. if (trip->type == THERMAL_TRIP_CRITICAL) {
  85. crit_temp = trip->temperature;
  86. break;
  87. }
  88. }
  89. /*
  90. * Only allow emulating a temperature when the real temperature
  91. * is below the critical temperature so that the emulation code
  92. * cannot hide critical conditions.
  93. */
  94. if (!ret && *temp < crit_temp)
  95. *temp = tz->emul_temperature;
  96. }
  97. if (ret)
  98. dev_dbg(&tz->device, "Failed to get temperature: %d\n", ret);
  99. return ret;
  100. }
  101. /**
  102. * thermal_zone_get_temp() - returns the temperature of a thermal zone
  103. * @tz: a valid pointer to a struct thermal_zone_device
  104. * @temp: a valid pointer to where to store the resulting temperature.
  105. *
  106. * When a valid thermal zone reference is passed, it will fetch its
  107. * temperature and fill @temp.
  108. *
  109. * Return: On success returns 0, an error code otherwise
  110. */
  111. int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
  112. {
  113. int ret;
  114. if (IS_ERR_OR_NULL(tz))
  115. return -EINVAL;
  116. mutex_lock(&tz->lock);
  117. if (!tz->ops.get_temp) {
  118. ret = -EINVAL;
  119. goto unlock;
  120. }
  121. ret = __thermal_zone_get_temp(tz, temp);
  122. if (!ret && *temp <= THERMAL_TEMP_INVALID)
  123. ret = -ENODATA;
  124. unlock:
  125. mutex_unlock(&tz->lock);
  126. return ret;
  127. }
  128. EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
  129. static int thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int state)
  130. {
  131. int ret;
  132. /*
  133. * No check is needed for the ops->set_cur_state as the
  134. * registering function checked the ops are correctly set
  135. */
  136. ret = cdev->ops->set_cur_state(cdev, state);
  137. if (ret)
  138. return ret;
  139. thermal_notify_cdev_state_update(cdev, state);
  140. thermal_cooling_device_stats_update(cdev, state);
  141. thermal_debug_cdev_state_update(cdev, state);
  142. return 0;
  143. }
  144. void __thermal_cdev_update(struct thermal_cooling_device *cdev)
  145. {
  146. struct thermal_instance *instance;
  147. unsigned long target = 0;
  148. /* Make sure cdev enters the deepest cooling state */
  149. list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
  150. if (instance->target == THERMAL_NO_TARGET)
  151. continue;
  152. if (instance->target > target)
  153. target = instance->target;
  154. }
  155. thermal_cdev_set_cur_state(cdev, target);
  156. trace_cdev_update(cdev, target);
  157. dev_dbg(&cdev->device, "set to state %lu\n", target);
  158. }
  159. /**
  160. * thermal_cdev_update - update cooling device state if needed
  161. * @cdev: pointer to struct thermal_cooling_device
  162. *
  163. * Update the cooling device state if there is a need.
  164. */
  165. void thermal_cdev_update(struct thermal_cooling_device *cdev)
  166. {
  167. mutex_lock(&cdev->lock);
  168. if (!cdev->updated) {
  169. __thermal_cdev_update(cdev);
  170. cdev->updated = true;
  171. }
  172. mutex_unlock(&cdev->lock);
  173. }
  174. /**
  175. * thermal_zone_get_slope - return the slope attribute of the thermal zone
  176. * @tz: thermal zone device with the slope attribute
  177. *
  178. * Return: If the thermal zone device has a slope attribute, return it, else
  179. * return 1.
  180. */
  181. int thermal_zone_get_slope(struct thermal_zone_device *tz)
  182. {
  183. if (tz && tz->tzp)
  184. return tz->tzp->slope;
  185. return 1;
  186. }
  187. EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
  188. /**
  189. * thermal_zone_get_offset - return the offset attribute of the thermal zone
  190. * @tz: thermal zone device with the offset attribute
  191. *
  192. * Return: If the thermal zone device has a offset attribute, return it, else
  193. * return 0.
  194. */
  195. int thermal_zone_get_offset(struct thermal_zone_device *tz)
  196. {
  197. if (tz && tz->tzp)
  198. return tz->tzp->offset;
  199. return 0;
  200. }
  201. EXPORT_SYMBOL_GPL(thermal_zone_get_offset);