gov_bang_bang.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * gov_bang_bang.c - A simple thermal throttling governor using hysteresis
  4. *
  5. * Copyright (C) 2014 Peter Kaestle <[email protected]>
  6. *
  7. * Based on step_wise.c with following Copyrights:
  8. * Copyright (C) 2012 Intel Corp
  9. * Copyright (C) 2012 Durgadoss R <[email protected]>
  10. */
  11. #include <linux/thermal.h>
  12. #include "thermal_core.h"
  13. static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
  14. {
  15. int trip_temp, trip_hyst;
  16. struct thermal_instance *instance;
  17. tz->ops->get_trip_temp(tz, trip, &trip_temp);
  18. if (!tz->ops->get_trip_hyst) {
  19. pr_warn_once("Undefined get_trip_hyst for thermal zone %s - "
  20. "running with default hysteresis zero\n", tz->type);
  21. trip_hyst = 0;
  22. } else
  23. tz->ops->get_trip_hyst(tz, trip, &trip_hyst);
  24. dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
  25. trip, trip_temp, tz->temperature,
  26. trip_hyst);
  27. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  28. if (instance->trip != trip)
  29. continue;
  30. /* in case fan is in initial state, switch the fan off */
  31. if (instance->target == THERMAL_NO_TARGET)
  32. instance->target = 0;
  33. /* in case fan is neither on nor off set the fan to active */
  34. if (instance->target != 0 && instance->target != 1) {
  35. pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n",
  36. instance->name, instance->target);
  37. instance->target = 1;
  38. }
  39. /*
  40. * enable fan when temperature exceeds trip_temp and disable
  41. * the fan in case it falls below trip_temp minus hysteresis
  42. */
  43. if (instance->target == 0 && tz->temperature >= trip_temp)
  44. instance->target = 1;
  45. else if (instance->target == 1 &&
  46. tz->temperature <= trip_temp - trip_hyst)
  47. instance->target = 0;
  48. dev_dbg(&instance->cdev->device, "target=%d\n",
  49. (int)instance->target);
  50. mutex_lock(&instance->cdev->lock);
  51. instance->cdev->updated = false; /* cdev needs update */
  52. mutex_unlock(&instance->cdev->lock);
  53. }
  54. }
  55. /**
  56. * bang_bang_control - controls devices associated with the given zone
  57. * @tz: thermal_zone_device
  58. * @trip: the trip point
  59. *
  60. * Regulation Logic: a two point regulation, deliver cooling state depending
  61. * on the previous state shown in this diagram:
  62. *
  63. * Fan: OFF ON
  64. *
  65. * |
  66. * |
  67. * trip_temp: +---->+
  68. * | | ^
  69. * | | |
  70. * | | Temperature
  71. * (trip_temp - hyst): +<----+
  72. * |
  73. * |
  74. * |
  75. *
  76. * * If the fan is not running and temperature exceeds trip_temp, the fan
  77. * gets turned on.
  78. * * In case the fan is running, temperature must fall below
  79. * (trip_temp - hyst) so that the fan gets turned off again.
  80. *
  81. */
  82. static int bang_bang_control(struct thermal_zone_device *tz, int trip)
  83. {
  84. struct thermal_instance *instance;
  85. lockdep_assert_held(&tz->lock);
  86. thermal_zone_trip_update(tz, trip);
  87. list_for_each_entry(instance, &tz->thermal_instances, tz_node)
  88. thermal_cdev_update(instance->cdev);
  89. return 0;
  90. }
  91. static struct thermal_governor thermal_gov_bang_bang = {
  92. .name = "bang_bang",
  93. .throttle = bang_bang_control,
  94. };
  95. THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);