thermal_helpers.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. guard(thermal_zone)(tz);
  52. guard(cooling_dev)(cdev);
  53. return thermal_instance_present(tz, cdev, trip);
  54. }
  55. EXPORT_SYMBOL_GPL(thermal_trip_is_bound_to_cdev);
  56. /**
  57. * __thermal_zone_get_temp() - returns the temperature of a thermal zone
  58. * @tz: a valid pointer to a struct thermal_zone_device
  59. * @temp: a valid pointer to where to store the resulting temperature.
  60. *
  61. * When a valid thermal zone reference is passed, it will fetch its
  62. * temperature and fill @temp.
  63. *
  64. * Both tz and tz->ops must be valid pointers when calling this function,
  65. * and the tz->ops.get_temp callback must be provided.
  66. * The function must be called under tz->lock.
  67. *
  68. * Return: On success returns 0, an error code otherwise
  69. */
  70. int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
  71. {
  72. const struct thermal_trip_desc *td;
  73. int crit_temp = INT_MAX;
  74. int ret = -EINVAL;
  75. lockdep_assert_held(&tz->lock);
  76. ret = tz->ops.get_temp(tz, temp);
  77. if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
  78. for_each_trip_desc(tz, td) {
  79. const struct thermal_trip *trip = &td->trip;
  80. if (trip->type == THERMAL_TRIP_CRITICAL) {
  81. crit_temp = trip->temperature;
  82. break;
  83. }
  84. }
  85. /*
  86. * Only allow emulating a temperature when the real temperature
  87. * is below the critical temperature so that the emulation code
  88. * cannot hide critical conditions.
  89. */
  90. if (!ret && *temp < crit_temp)
  91. *temp = tz->emul_temperature;
  92. }
  93. if (ret)
  94. dev_dbg(&tz->device, "Failed to get temperature: %d\n", ret);
  95. return ret;
  96. }
  97. /**
  98. * thermal_zone_get_temp() - returns the temperature of a thermal zone
  99. * @tz: a valid pointer to a struct thermal_zone_device
  100. * @temp: a valid pointer to where to store the resulting temperature.
  101. *
  102. * When a valid thermal zone reference is passed, it will fetch its
  103. * temperature and fill @temp.
  104. *
  105. * Return: On success returns 0, an error code otherwise
  106. */
  107. int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
  108. {
  109. int ret;
  110. if (IS_ERR_OR_NULL(tz))
  111. return -EINVAL;
  112. guard(thermal_zone)(tz);
  113. if (!tz->ops.get_temp)
  114. return -EINVAL;
  115. ret = __thermal_zone_get_temp(tz, temp);
  116. if (!ret && *temp <= THERMAL_TEMP_INVALID)
  117. return -ENODATA;
  118. return ret;
  119. }
  120. EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
  121. static int thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int state)
  122. {
  123. int ret;
  124. /*
  125. * No check is needed for the ops->set_cur_state as the
  126. * registering function checked the ops are correctly set
  127. */
  128. ret = cdev->ops->set_cur_state(cdev, state);
  129. if (ret)
  130. return ret;
  131. thermal_notify_cdev_state_update(cdev, state);
  132. thermal_cooling_device_stats_update(cdev, state);
  133. thermal_debug_cdev_state_update(cdev, state);
  134. return 0;
  135. }
  136. void __thermal_cdev_update(struct thermal_cooling_device *cdev)
  137. {
  138. struct thermal_instance *instance;
  139. unsigned long target = 0;
  140. /* Make sure cdev enters the deepest cooling state */
  141. list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
  142. if (instance->target == THERMAL_NO_TARGET)
  143. continue;
  144. if (instance->target > target)
  145. target = instance->target;
  146. }
  147. thermal_cdev_set_cur_state(cdev, target);
  148. trace_cdev_update(cdev, target);
  149. dev_dbg(&cdev->device, "set to state %lu\n", target);
  150. }
  151. /**
  152. * thermal_cdev_update - update cooling device state if needed
  153. * @cdev: pointer to struct thermal_cooling_device
  154. *
  155. * Update the cooling device state if there is a need.
  156. */
  157. void thermal_cdev_update(struct thermal_cooling_device *cdev)
  158. {
  159. guard(cooling_dev)(cdev);
  160. if (!cdev->updated) {
  161. __thermal_cdev_update(cdev);
  162. cdev->updated = true;
  163. }
  164. }
  165. /**
  166. * thermal_cdev_update_nocheck() - Unconditionally update cooling device state
  167. * @cdev: Target cooling device.
  168. */
  169. void thermal_cdev_update_nocheck(struct thermal_cooling_device *cdev)
  170. {
  171. guard(cooling_dev)(cdev);
  172. __thermal_cdev_update(cdev);
  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);