thermal_helpers.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <[email protected]>
  6. *
  7. * Highly based on original thermal_core.c
  8. * Copyright (C) 2008 Intel Corp
  9. * Copyright (C) 2008 Zhang Rui <[email protected]>
  10. * Copyright (C) 2008 Sujith Thomas <[email protected]>
  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 <trace/events/thermal.h>
  20. #include "thermal_core.h"
  21. int get_tz_trend(struct thermal_zone_device *tz, int 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. struct thermal_instance *
  36. get_thermal_instance(struct thermal_zone_device *tz,
  37. struct thermal_cooling_device *cdev, int trip)
  38. {
  39. struct thermal_instance *pos = NULL;
  40. struct thermal_instance *target_instance = NULL;
  41. mutex_lock(&tz->lock);
  42. mutex_lock(&cdev->lock);
  43. list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
  44. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  45. target_instance = pos;
  46. break;
  47. }
  48. }
  49. mutex_unlock(&cdev->lock);
  50. mutex_unlock(&tz->lock);
  51. return target_instance;
  52. }
  53. EXPORT_SYMBOL(get_thermal_instance);
  54. int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
  55. {
  56. int ret = -EINVAL;
  57. int count;
  58. int crit_temp = INT_MAX;
  59. enum thermal_trip_type type;
  60. lockdep_assert_held(&tz->lock);
  61. if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
  62. return -EINVAL;
  63. ret = tz->ops->get_temp(tz, temp);
  64. if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
  65. for (count = 0; count < tz->num_trips; count++) {
  66. ret = tz->ops->get_trip_type(tz, count, &type);
  67. if (!ret && type == THERMAL_TRIP_CRITICAL) {
  68. ret = tz->ops->get_trip_temp(tz, count,
  69. &crit_temp);
  70. break;
  71. }
  72. }
  73. /*
  74. * Only allow emulating a temperature when the real temperature
  75. * is below the critical temperature so that the emulation code
  76. * cannot hide critical conditions.
  77. */
  78. if (!ret && *temp < crit_temp)
  79. *temp = tz->emul_temperature;
  80. }
  81. return ret;
  82. }
  83. /**
  84. * thermal_zone_get_temp() - returns the temperature of a thermal zone
  85. * @tz: a valid pointer to a struct thermal_zone_device
  86. * @temp: a valid pointer to where to store the resulting temperature.
  87. *
  88. * When a valid thermal zone reference is passed, it will fetch its
  89. * temperature and fill @temp.
  90. *
  91. * Return: On success returns 0, an error code otherwise
  92. */
  93. int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
  94. {
  95. int ret;
  96. mutex_lock(&tz->lock);
  97. if (device_is_registered(&tz->device))
  98. ret = __thermal_zone_get_temp(tz, temp);
  99. else
  100. ret = -ENODEV;
  101. mutex_unlock(&tz->lock);
  102. return ret;
  103. }
  104. EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
  105. void __thermal_zone_set_trips(struct thermal_zone_device *tz)
  106. {
  107. int low = -INT_MAX;
  108. int high = INT_MAX;
  109. int trip_temp, hysteresis;
  110. int i, ret;
  111. lockdep_assert_held(&tz->lock);
  112. if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
  113. return;
  114. for (i = 0; i < tz->num_trips; i++) {
  115. int trip_low;
  116. tz->ops->get_trip_temp(tz, i, &trip_temp);
  117. tz->ops->get_trip_hyst(tz, i, &hysteresis);
  118. trip_low = trip_temp - hysteresis;
  119. if (trip_low < tz->temperature && trip_low > low)
  120. low = trip_low;
  121. if (trip_temp > tz->temperature && trip_temp < high)
  122. high = trip_temp;
  123. }
  124. /* No need to change trip points */
  125. if (tz->prev_low_trip == low && tz->prev_high_trip == high)
  126. return;
  127. tz->prev_low_trip = low;
  128. tz->prev_high_trip = high;
  129. dev_dbg(&tz->device,
  130. "new temperature boundaries: %d < x < %d\n", low, high);
  131. /*
  132. * Set a temperature window. When this window is left the driver
  133. * must inform the thermal core via thermal_zone_device_update.
  134. */
  135. ret = tz->ops->set_trips(tz, low, high);
  136. if (ret)
  137. dev_err(&tz->device, "Failed to set trips: %d\n", ret);
  138. }
  139. /**
  140. * thermal_zone_set_trips - Computes the next trip points for the driver
  141. * @tz: a pointer to a thermal zone device structure
  142. *
  143. * The function computes the next temperature boundaries by browsing
  144. * the trip points. The result is the closer low and high trip points
  145. * to the current temperature. These values are passed to the backend
  146. * driver to let it set its own notification mechanism (usually an
  147. * interrupt).
  148. *
  149. * It does not return a value
  150. */
  151. void thermal_zone_set_trips(struct thermal_zone_device *tz)
  152. {
  153. mutex_lock(&tz->lock);
  154. __thermal_zone_set_trips(tz);
  155. mutex_unlock(&tz->lock);
  156. }
  157. static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
  158. int target)
  159. {
  160. if (cdev->ops->set_cur_state(cdev, target))
  161. return;
  162. thermal_notify_cdev_state_update(cdev->id, target);
  163. thermal_cooling_device_stats_update(cdev, target);
  164. }
  165. void __thermal_cdev_update(struct thermal_cooling_device *cdev)
  166. {
  167. struct thermal_instance *instance;
  168. unsigned long target = 0;
  169. /* Make sure cdev enters the deepest cooling state */
  170. list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
  171. dev_dbg(&cdev->device, "zone%d->target=%lu\n",
  172. instance->tz->id, instance->target);
  173. if (instance->target == THERMAL_NO_TARGET)
  174. continue;
  175. if (instance->target > target)
  176. target = instance->target;
  177. }
  178. thermal_cdev_set_cur_state(cdev, target);
  179. trace_cdev_update(cdev, target);
  180. dev_dbg(&cdev->device, "set to state %lu\n", target);
  181. }
  182. /**
  183. * thermal_cdev_update - update cooling device state if needed
  184. * @cdev: pointer to struct thermal_cooling_device
  185. *
  186. * Update the cooling device state if there is a need.
  187. */
  188. void thermal_cdev_update(struct thermal_cooling_device *cdev)
  189. {
  190. mutex_lock(&cdev->lock);
  191. if (!cdev->updated) {
  192. __thermal_cdev_update(cdev);
  193. cdev->updated = true;
  194. }
  195. mutex_unlock(&cdev->lock);
  196. }
  197. EXPORT_SYMBOL_GPL(thermal_cdev_update);
  198. /**
  199. * thermal_zone_get_slope - return the slope attribute of the thermal zone
  200. * @tz: thermal zone device with the slope attribute
  201. *
  202. * Return: If the thermal zone device has a slope attribute, return it, else
  203. * return 1.
  204. */
  205. int thermal_zone_get_slope(struct thermal_zone_device *tz)
  206. {
  207. if (tz && tz->tzp)
  208. return tz->tzp->slope;
  209. return 1;
  210. }
  211. EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
  212. /**
  213. * thermal_zone_get_offset - return the offset attribute of the thermal zone
  214. * @tz: thermal zone device with the offset attribute
  215. *
  216. * Return: If the thermal zone device has a offset attribute, return it, else
  217. * return 0.
  218. */
  219. int thermal_zone_get_offset(struct thermal_zone_device *tz)
  220. {
  221. if (tz && tz->tzp)
  222. return tz->tzp->offset;
  223. return 0;
  224. }
  225. EXPORT_SYMBOL_GPL(thermal_zone_get_offset);