thermal_helpers.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * thermal_helpers.c - helper functions to handle thermal devices
  3. *
  4. * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
  5. *
  6. * Highly based on original thermal_core.c
  7. * Copyright (C) 2008 Intel Corp
  8. * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  9. * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/sysfs.h>
  17. #include <linux/device.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/string.h>
  21. #include <trace/events/thermal.h>
  22. #include "thermal_core.h"
  23. int get_tz_trend(struct thermal_zone_device *tz, int trip)
  24. {
  25. enum thermal_trend trend;
  26. if (tz->emul_temperature || !tz->ops->get_trend ||
  27. tz->ops->get_trend(tz, trip, &trend)) {
  28. if (tz->temperature > tz->last_temperature)
  29. trend = THERMAL_TREND_RAISING;
  30. else if (tz->temperature < tz->last_temperature)
  31. trend = THERMAL_TREND_DROPPING;
  32. else
  33. trend = THERMAL_TREND_STABLE;
  34. }
  35. return trend;
  36. }
  37. EXPORT_SYMBOL(get_tz_trend);
  38. struct thermal_instance *
  39. get_thermal_instance(struct thermal_zone_device *tz,
  40. struct thermal_cooling_device *cdev, int trip)
  41. {
  42. struct thermal_instance *pos = NULL;
  43. struct thermal_instance *target_instance = NULL;
  44. mutex_lock(&tz->lock);
  45. mutex_lock(&cdev->lock);
  46. list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
  47. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  48. target_instance = pos;
  49. break;
  50. }
  51. }
  52. mutex_unlock(&cdev->lock);
  53. mutex_unlock(&tz->lock);
  54. return target_instance;
  55. }
  56. EXPORT_SYMBOL(get_thermal_instance);
  57. /**
  58. * thermal_zone_get_temp() - returns the temperature of a thermal zone
  59. * @tz: a valid pointer to a struct thermal_zone_device
  60. * @temp: a valid pointer to where to store the resulting temperature.
  61. *
  62. * When a valid thermal zone reference is passed, it will fetch its
  63. * temperature and fill @temp.
  64. *
  65. * Return: On success returns 0, an error code otherwise
  66. */
  67. int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
  68. {
  69. int ret = -EINVAL;
  70. int count;
  71. int crit_temp = INT_MAX;
  72. enum thermal_trip_type type;
  73. if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
  74. goto exit;
  75. mutex_lock(&tz->lock);
  76. ret = tz->ops->get_temp(tz, temp);
  77. if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
  78. for (count = 0; count < tz->trips; count++) {
  79. ret = tz->ops->get_trip_type(tz, count, &type);
  80. if (!ret && type == THERMAL_TRIP_CRITICAL) {
  81. ret = tz->ops->get_trip_temp(tz, count,
  82. &crit_temp);
  83. break;
  84. }
  85. }
  86. /*
  87. * Only allow emulating a temperature when the real temperature
  88. * is below the critical temperature so that the emulation code
  89. * cannot hide critical conditions.
  90. */
  91. if (!ret && *temp < crit_temp)
  92. *temp = tz->emul_temperature;
  93. }
  94. mutex_unlock(&tz->lock);
  95. exit:
  96. return ret;
  97. }
  98. EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
  99. void thermal_zone_set_trips(struct thermal_zone_device *tz)
  100. {
  101. int low = -INT_MAX;
  102. int high = INT_MAX;
  103. int trip_temp, hysteresis;
  104. int i, ret;
  105. mutex_lock(&tz->lock);
  106. if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
  107. goto exit;
  108. for (i = 0; i < tz->trips; i++) {
  109. int trip_low;
  110. tz->ops->get_trip_temp(tz, i, &trip_temp);
  111. tz->ops->get_trip_hyst(tz, i, &hysteresis);
  112. trip_low = trip_temp - hysteresis;
  113. if (trip_low < tz->temperature && trip_low > low)
  114. low = trip_low;
  115. if (trip_temp > tz->temperature && trip_temp < high)
  116. high = trip_temp;
  117. }
  118. /* No need to change trip points */
  119. if (tz->prev_low_trip == low && tz->prev_high_trip == high)
  120. goto exit;
  121. tz->prev_low_trip = low;
  122. tz->prev_high_trip = high;
  123. dev_dbg(&tz->device,
  124. "new temperature boundaries: %d < x < %d\n", low, high);
  125. /*
  126. * Set a temperature window. When this window is left the driver
  127. * must inform the thermal core via thermal_zone_device_update.
  128. */
  129. ret = tz->ops->set_trips(tz, low, high);
  130. if (ret)
  131. dev_err(&tz->device, "Failed to set trips: %d\n", ret);
  132. exit:
  133. mutex_unlock(&tz->lock);
  134. }
  135. EXPORT_SYMBOL_GPL(thermal_zone_set_trips);
  136. void thermal_cdev_update(struct thermal_cooling_device *cdev)
  137. {
  138. struct thermal_instance *instance;
  139. unsigned long target = 0;
  140. mutex_lock(&cdev->lock);
  141. /* cooling device is updated*/
  142. if (cdev->updated) {
  143. mutex_unlock(&cdev->lock);
  144. return;
  145. }
  146. /* Make sure cdev enters the deepest cooling state */
  147. list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
  148. dev_dbg(&cdev->device, "zone%d->target=%lu\n",
  149. instance->tz->id, instance->target);
  150. if (instance->target == THERMAL_NO_TARGET)
  151. continue;
  152. if (instance->target > target)
  153. target = instance->target;
  154. }
  155. cdev->ops->set_cur_state(cdev, target);
  156. cdev->updated = true;
  157. mutex_unlock(&cdev->lock);
  158. trace_cdev_update(cdev, target);
  159. dev_dbg(&cdev->device, "set to state %lu\n", target);
  160. }
  161. EXPORT_SYMBOL(thermal_cdev_update);
  162. /**
  163. * thermal_zone_get_slope - return the slope attribute of the thermal zone
  164. * @tz: thermal zone device with the slope attribute
  165. *
  166. * Return: If the thermal zone device has a slope attribute, return it, else
  167. * return 1.
  168. */
  169. int thermal_zone_get_slope(struct thermal_zone_device *tz)
  170. {
  171. if (tz && tz->tzp)
  172. return tz->tzp->slope;
  173. return 1;
  174. }
  175. EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
  176. /**
  177. * thermal_zone_get_offset - return the offset attribute of the thermal zone
  178. * @tz: thermal zone device with the offset attribute
  179. *
  180. * Return: If the thermal zone device has a offset attribute, return it, else
  181. * return 0.
  182. */
  183. int thermal_zone_get_offset(struct thermal_zone_device *tz)
  184. {
  185. if (tz && tz->tzp)
  186. return tz->tzp->offset;
  187. return 0;
  188. }
  189. EXPORT_SYMBOL_GPL(thermal_zone_get_offset);