gov_bang_bang.c 3.6 KB

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