gov_bang_bang.c 3.6 KB

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