gov_bang_bang.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 bang_bang_set_instance_target(struct thermal_instance *instance,
  14. unsigned int target)
  15. {
  16. if (instance->target != 0 && instance->target != 1 &&
  17. instance->target != THERMAL_NO_TARGET)
  18. pr_debug("Unexpected state %ld of thermal instance %s in bang-bang\n",
  19. instance->target, instance->name);
  20. /*
  21. * Enable the fan when the trip is crossed on the way up and disable it
  22. * when the trip is crossed on the way down.
  23. */
  24. instance->target = target;
  25. instance->initialized = true;
  26. dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
  27. mutex_lock(&instance->cdev->lock);
  28. __thermal_cdev_update(instance->cdev);
  29. mutex_unlock(&instance->cdev->lock);
  30. }
  31. /**
  32. * bang_bang_control - controls devices associated with the given zone
  33. * @tz: thermal_zone_device
  34. * @trip: the trip point
  35. * @crossed_up: whether or not the trip has been crossed on the way up
  36. *
  37. * Regulation Logic: a two point regulation, deliver cooling state depending
  38. * on the previous state shown in this diagram:
  39. *
  40. * Fan: OFF ON
  41. *
  42. * |
  43. * |
  44. * trip_temp: +---->+
  45. * | | ^
  46. * | | |
  47. * | | Temperature
  48. * (trip_temp - hyst): +<----+
  49. * |
  50. * |
  51. * |
  52. *
  53. * * If the fan is not running and temperature exceeds trip_temp, the fan
  54. * gets turned on.
  55. * * In case the fan is running, temperature must fall below
  56. * (trip_temp - hyst) so that the fan gets turned off again.
  57. *
  58. */
  59. static void bang_bang_control(struct thermal_zone_device *tz,
  60. const struct thermal_trip *trip,
  61. bool crossed_up)
  62. {
  63. const struct thermal_trip_desc *td = trip_to_trip_desc(trip);
  64. struct thermal_instance *instance;
  65. lockdep_assert_held(&tz->lock);
  66. dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
  67. thermal_zone_trip_id(tz, trip), trip->temperature,
  68. tz->temperature, trip->hysteresis);
  69. list_for_each_entry(instance, &td->thermal_instances, trip_node)
  70. bang_bang_set_instance_target(instance, crossed_up);
  71. }
  72. static void bang_bang_manage(struct thermal_zone_device *tz)
  73. {
  74. const struct thermal_trip_desc *td;
  75. struct thermal_instance *instance;
  76. /* If the code below has run already, nothing needs to be done. */
  77. if (tz->governor_data)
  78. return;
  79. for_each_trip_desc(tz, td) {
  80. const struct thermal_trip *trip = &td->trip;
  81. bool turn_on;
  82. if (trip->temperature == THERMAL_TEMP_INVALID ||
  83. trip->type == THERMAL_TRIP_CRITICAL ||
  84. trip->type == THERMAL_TRIP_HOT)
  85. continue;
  86. /*
  87. * Adjust the target states for uninitialized thermal instances
  88. * to the thermal zone temperature and the trip point threshold.
  89. */
  90. turn_on = tz->temperature >= td->threshold;
  91. list_for_each_entry(instance, &td->thermal_instances, trip_node) {
  92. if (!instance->initialized)
  93. bang_bang_set_instance_target(instance, turn_on);
  94. }
  95. }
  96. tz->governor_data = (void *)true;
  97. }
  98. static void bang_bang_update_tz(struct thermal_zone_device *tz,
  99. enum thermal_notify_event reason)
  100. {
  101. /*
  102. * Let bang_bang_manage() know that it needs to walk trips after binding
  103. * a new cdev and after system resume.
  104. */
  105. if (reason == THERMAL_TZ_BIND_CDEV || reason == THERMAL_TZ_RESUME)
  106. tz->governor_data = NULL;
  107. }
  108. static struct thermal_governor thermal_gov_bang_bang = {
  109. .name = "bang_bang",
  110. .trip_crossed = bang_bang_control,
  111. .manage = bang_bang_manage,
  112. .update_tz = bang_bang_update_tz,
  113. };
  114. THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);