thermal_helpers.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/sysfs.h>
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <trace/events/thermal.h>
  19. #include "thermal_core.h"
  20. int get_tz_trend(struct thermal_zone_device *tz, int trip)
  21. {
  22. enum thermal_trend trend;
  23. if (tz->emul_temperature || !tz->ops->get_trend ||
  24. tz->ops->get_trend(tz, trip, &trend)) {
  25. if (tz->temperature > tz->last_temperature)
  26. trend = THERMAL_TREND_RAISING;
  27. else if (tz->temperature < tz->last_temperature)
  28. trend = THERMAL_TREND_DROPPING;
  29. else
  30. trend = THERMAL_TREND_STABLE;
  31. }
  32. return trend;
  33. }
  34. EXPORT_SYMBOL(get_tz_trend);
  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. /**
  55. * thermal_zone_get_temp() - returns the temperature of a thermal zone
  56. * @tz: a valid pointer to a struct thermal_zone_device
  57. * @temp: a valid pointer to where to store the resulting temperature.
  58. *
  59. * When a valid thermal zone reference is passed, it will fetch its
  60. * temperature and fill @temp.
  61. *
  62. * Return: On success returns 0, an error code otherwise
  63. */
  64. int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
  65. {
  66. int ret = -EINVAL;
  67. int count;
  68. int crit_temp = INT_MAX;
  69. enum thermal_trip_type type;
  70. if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
  71. goto exit;
  72. mutex_lock(&tz->lock);
  73. ret = tz->ops->get_temp(tz, temp);
  74. if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
  75. for (count = 0; count < tz->trips; count++) {
  76. ret = tz->ops->get_trip_type(tz, count, &type);
  77. if (!ret && type == THERMAL_TRIP_CRITICAL) {
  78. ret = tz->ops->get_trip_temp(tz, count,
  79. &crit_temp);
  80. break;
  81. }
  82. }
  83. /*
  84. * Only allow emulating a temperature when the real temperature
  85. * is below the critical temperature so that the emulation code
  86. * cannot hide critical conditions.
  87. */
  88. if (!ret && *temp < crit_temp)
  89. *temp = tz->emul_temperature;
  90. }
  91. mutex_unlock(&tz->lock);
  92. exit:
  93. return ret;
  94. }
  95. EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
  96. void thermal_zone_set_trips(struct thermal_zone_device *tz)
  97. {
  98. int low = -INT_MAX;
  99. int high = INT_MAX;
  100. int trip_temp, hysteresis;
  101. int i, ret;
  102. mutex_lock(&tz->lock);
  103. if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
  104. goto exit;
  105. for (i = 0; i < tz->trips; i++) {
  106. int trip_low;
  107. tz->ops->get_trip_temp(tz, i, &trip_temp);
  108. tz->ops->get_trip_hyst(tz, i, &hysteresis);
  109. trip_low = trip_temp - hysteresis;
  110. if (trip_low < tz->temperature && trip_low > low)
  111. low = trip_low;
  112. if (trip_temp > tz->temperature && trip_temp < high)
  113. high = trip_temp;
  114. }
  115. /* No need to change trip points */
  116. if (tz->prev_low_trip == low && tz->prev_high_trip == high)
  117. goto exit;
  118. tz->prev_low_trip = low;
  119. tz->prev_high_trip = high;
  120. dev_dbg(&tz->device,
  121. "new temperature boundaries: %d < x < %d\n", low, high);
  122. /*
  123. * Set a temperature window. When this window is left the driver
  124. * must inform the thermal core via thermal_zone_device_update.
  125. */
  126. ret = tz->ops->set_trips(tz, low, high);
  127. if (ret)
  128. dev_err(&tz->device, "Failed to set trips: %d\n", ret);
  129. exit:
  130. mutex_unlock(&tz->lock);
  131. }
  132. EXPORT_SYMBOL_GPL(thermal_zone_set_trips);
  133. void thermal_cdev_update(struct thermal_cooling_device *cdev)
  134. {
  135. struct thermal_instance *instance;
  136. unsigned long target = 0;
  137. mutex_lock(&cdev->lock);
  138. /* cooling device is updated*/
  139. if (cdev->updated) {
  140. mutex_unlock(&cdev->lock);
  141. return;
  142. }
  143. /* Make sure cdev enters the deepest cooling state */
  144. list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
  145. dev_dbg(&cdev->device, "zone%d->target=%lu\n",
  146. instance->tz->id, instance->target);
  147. if (instance->target == THERMAL_NO_TARGET)
  148. continue;
  149. if (instance->target > target)
  150. target = instance->target;
  151. }
  152. if (!cdev->ops->set_cur_state(cdev, target))
  153. thermal_cooling_device_stats_update(cdev, target);
  154. cdev->updated = true;
  155. mutex_unlock(&cdev->lock);
  156. trace_cdev_update(cdev, target);
  157. dev_dbg(&cdev->device, "set to state %lu\n", target);
  158. }
  159. EXPORT_SYMBOL(thermal_cdev_update);
  160. /**
  161. * thermal_zone_get_slope - return the slope attribute of the thermal zone
  162. * @tz: thermal zone device with the slope attribute
  163. *
  164. * Return: If the thermal zone device has a slope attribute, return it, else
  165. * return 1.
  166. */
  167. int thermal_zone_get_slope(struct thermal_zone_device *tz)
  168. {
  169. if (tz && tz->tzp)
  170. return tz->tzp->slope;
  171. return 1;
  172. }
  173. EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
  174. /**
  175. * thermal_zone_get_offset - return the offset attribute of the thermal zone
  176. * @tz: thermal zone device with the offset attribute
  177. *
  178. * Return: If the thermal zone device has a offset attribute, return it, else
  179. * return 0.
  180. */
  181. int thermal_zone_get_offset(struct thermal_zone_device *tz)
  182. {
  183. if (tz && tz->tzp)
  184. return tz->tzp->offset;
  185. return 0;
  186. }
  187. EXPORT_SYMBOL_GPL(thermal_zone_get_offset);