gov_bang_bang.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * gov_bang_bang.c - A simple thermal throttling governor using hysteresis
  3. *
  4. * Copyright (C) 2014 Peter Feuerer <peter@piie.net>
  5. *
  6. * Based on step_wise.c with following Copyrights:
  7. * Copyright (C) 2012 Intel Corp
  8. * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
  9. *
  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.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  18. * the GNU General Public License for more details.
  19. *
  20. */
  21. #include <linux/thermal.h>
  22. #include "thermal_core.h"
  23. static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
  24. {
  25. int trip_temp, trip_hyst;
  26. struct thermal_instance *instance;
  27. tz->ops->get_trip_temp(tz, trip, &trip_temp);
  28. if (!tz->ops->get_trip_hyst) {
  29. pr_warn_once("Undefined get_trip_hyst for thermal zone %s - "
  30. "running with default hysteresis zero\n", tz->type);
  31. trip_hyst = 0;
  32. } else
  33. tz->ops->get_trip_hyst(tz, trip, &trip_hyst);
  34. dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
  35. trip, trip_temp, tz->temperature,
  36. trip_hyst);
  37. mutex_lock(&tz->lock);
  38. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  39. if (instance->trip != trip)
  40. continue;
  41. /* in case fan is in initial state, switch the fan off */
  42. if (instance->target == THERMAL_NO_TARGET)
  43. instance->target = 0;
  44. /* in case fan is neither on nor off set the fan to active */
  45. if (instance->target != 0 && instance->target != 1) {
  46. pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n",
  47. instance->name, instance->target);
  48. instance->target = 1;
  49. }
  50. /*
  51. * enable fan when temperature exceeds trip_temp and disable
  52. * the fan in case it falls below trip_temp minus hysteresis
  53. */
  54. if (instance->target == 0 && tz->temperature >= trip_temp)
  55. instance->target = 1;
  56. else if (instance->target == 1 &&
  57. tz->temperature <= trip_temp - trip_hyst)
  58. instance->target = 0;
  59. dev_dbg(&instance->cdev->device, "target=%d\n",
  60. (int)instance->target);
  61. mutex_lock(&instance->cdev->lock);
  62. instance->cdev->updated = false; /* cdev needs update */
  63. mutex_unlock(&instance->cdev->lock);
  64. }
  65. mutex_unlock(&tz->lock);
  66. }
  67. /**
  68. * bang_bang_control - controls devices associated with the given zone
  69. * @tz - thermal_zone_device
  70. * @trip - the trip point
  71. *
  72. * Regulation Logic: a two point regulation, deliver cooling state depending
  73. * on the previous state shown in this diagram:
  74. *
  75. * Fan: OFF ON
  76. *
  77. * |
  78. * |
  79. * trip_temp: +---->+
  80. * | | ^
  81. * | | |
  82. * | | Temperature
  83. * (trip_temp - hyst): +<----+
  84. * |
  85. * |
  86. * |
  87. *
  88. * * If the fan is not running and temperature exceeds trip_temp, the fan
  89. * gets turned on.
  90. * * In case the fan is running, temperature must fall below
  91. * (trip_temp - hyst) so that the fan gets turned off again.
  92. *
  93. */
  94. static int bang_bang_control(struct thermal_zone_device *tz, int trip)
  95. {
  96. struct thermal_instance *instance;
  97. thermal_zone_trip_update(tz, trip);
  98. mutex_lock(&tz->lock);
  99. list_for_each_entry(instance, &tz->thermal_instances, tz_node)
  100. thermal_cdev_update(instance->cdev);
  101. mutex_unlock(&tz->lock);
  102. return 0;
  103. }
  104. static struct thermal_governor thermal_gov_bang_bang = {
  105. .name = "bang_bang",
  106. .throttle = bang_bang_control,
  107. };
  108. int thermal_gov_bang_bang_register(void)
  109. {
  110. return thermal_register_governor(&thermal_gov_bang_bang);
  111. }
  112. void thermal_gov_bang_bang_unregister(void)
  113. {
  114. thermal_unregister_governor(&thermal_gov_bang_bang);
  115. }