gov_bang_bang.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <peter@piie.net>
  6. *
  7. * Based on step_wise.c with following Copyrights:
  8. * Copyright (C) 2012 Intel Corp
  9. * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
  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. mutex_lock(&tz->lock);
  28. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  29. if (instance->trip != trip)
  30. continue;
  31. /* in case fan is in initial state, switch the fan off */
  32. if (instance->target == THERMAL_NO_TARGET)
  33. instance->target = 0;
  34. /* in case fan is neither on nor off set the fan to active */
  35. if (instance->target != 0 && instance->target != 1) {
  36. pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n",
  37. instance->name, instance->target);
  38. instance->target = 1;
  39. }
  40. /*
  41. * enable fan when temperature exceeds trip_temp and disable
  42. * the fan in case it falls below trip_temp minus hysteresis
  43. */
  44. if (instance->target == 0 && tz->temperature >= trip_temp)
  45. instance->target = 1;
  46. else if (instance->target == 1 &&
  47. tz->temperature <= trip_temp - trip_hyst)
  48. instance->target = 0;
  49. dev_dbg(&instance->cdev->device, "target=%d\n",
  50. (int)instance->target);
  51. mutex_lock(&instance->cdev->lock);
  52. instance->cdev->updated = false; /* cdev needs update */
  53. mutex_unlock(&instance->cdev->lock);
  54. }
  55. mutex_unlock(&tz->lock);
  56. }
  57. /**
  58. * bang_bang_control - controls devices associated with the given zone
  59. * @tz: thermal_zone_device
  60. * @trip: the trip point
  61. *
  62. * Regulation Logic: a two point regulation, deliver cooling state depending
  63. * on the previous state shown in this diagram:
  64. *
  65. * Fan: OFF ON
  66. *
  67. * |
  68. * |
  69. * trip_temp: +---->+
  70. * | | ^
  71. * | | |
  72. * | | Temperature
  73. * (trip_temp - hyst): +<----+
  74. * |
  75. * |
  76. * |
  77. *
  78. * * If the fan is not running and temperature exceeds trip_temp, the fan
  79. * gets turned on.
  80. * * In case the fan is running, temperature must fall below
  81. * (trip_temp - hyst) so that the fan gets turned off again.
  82. *
  83. */
  84. static int bang_bang_control(struct thermal_zone_device *tz, int trip)
  85. {
  86. struct thermal_instance *instance;
  87. thermal_zone_trip_update(tz, trip);
  88. mutex_lock(&tz->lock);
  89. list_for_each_entry(instance, &tz->thermal_instances, tz_node)
  90. thermal_cdev_update(instance->cdev);
  91. mutex_unlock(&tz->lock);
  92. return 0;
  93. }
  94. static struct thermal_governor thermal_gov_bang_bang = {
  95. .name = "bang_bang",
  96. .throttle = bang_bang_control,
  97. };
  98. THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);