Thomas Gleixner | 50acfb2 | 2019-05-29 07:18:00 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 2 | /* |
| 3 | * gov_bang_bang.c - A simple thermal throttling governor using hysteresis |
| 4 | * |
Peter Kaestle | d3f5b73 | 2019-10-19 00:59:36 +0200 | [diff] [blame] | 5 | * Copyright (C) 2014 Peter Kaestle <peter@piie.net> |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 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> |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/thermal.h> |
| 13 | |
| 14 | #include "thermal_core.h" |
| 15 | |
Rafael J. Wysocki | 8c35b1f | 2023-10-12 20:34:50 +0200 | [diff] [blame] | 16 | static int thermal_zone_trip_update(struct thermal_zone_device *tz, |
| 17 | const struct thermal_trip *trip) |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 18 | { |
Rafael J. Wysocki | 8c35b1f | 2023-10-12 20:34:50 +0200 | [diff] [blame] | 19 | int trip_index = thermal_zone_trip_id(tz, trip); |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 20 | struct thermal_instance *instance; |
| 21 | |
Rafael J. Wysocki | 2c7b4bf | 2023-09-21 19:52:44 +0200 | [diff] [blame] | 22 | if (!trip->hysteresis) |
Daniel Lezcano | 7f725a2 | 2022-10-03 11:25:37 +0200 | [diff] [blame] | 23 | dev_info_once(&tz->device, |
| 24 | "Zero hysteresis value for thermal zone %s\n", tz->type); |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 25 | |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 26 | dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n", |
Rafael J. Wysocki | 2c7b4bf | 2023-09-21 19:52:44 +0200 | [diff] [blame] | 27 | trip_index, trip->temperature, tz->temperature, |
| 28 | trip->hysteresis); |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 29 | |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 30 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
Rafael J. Wysocki | 2c7b4bf | 2023-09-21 19:52:44 +0200 | [diff] [blame] | 31 | if (instance->trip != trip) |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 32 | continue; |
| 33 | |
| 34 | /* in case fan is in initial state, switch the fan off */ |
| 35 | if (instance->target == THERMAL_NO_TARGET) |
| 36 | instance->target = 0; |
| 37 | |
| 38 | /* in case fan is neither on nor off set the fan to active */ |
| 39 | if (instance->target != 0 && instance->target != 1) { |
| 40 | pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n", |
| 41 | instance->name, instance->target); |
| 42 | instance->target = 1; |
| 43 | } |
| 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 | */ |
Rafael J. Wysocki | 2c7b4bf | 2023-09-21 19:52:44 +0200 | [diff] [blame] | 49 | if (instance->target == 0 && tz->temperature >= trip->temperature) |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 50 | instance->target = 1; |
| 51 | else if (instance->target == 1 && |
Rafael J. Wysocki | 2c7b4bf | 2023-09-21 19:52:44 +0200 | [diff] [blame] | 52 | tz->temperature <= trip->temperature - trip->hysteresis) |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 53 | instance->target = 0; |
| 54 | |
| 55 | dev_dbg(&instance->cdev->device, "target=%d\n", |
| 56 | (int)instance->target); |
| 57 | |
Michele Di Giorgio | d0b7306 | 2016-06-02 15:25:31 +0100 | [diff] [blame] | 58 | mutex_lock(&instance->cdev->lock); |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 59 | instance->cdev->updated = false; /* cdev needs update */ |
Michele Di Giorgio | d0b7306 | 2016-06-02 15:25:31 +0100 | [diff] [blame] | 60 | mutex_unlock(&instance->cdev->lock); |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 61 | } |
Daniel Lezcano | 7f725a2 | 2022-10-03 11:25:37 +0200 | [diff] [blame] | 62 | |
| 63 | return 0; |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * bang_bang_control - controls devices associated with the given zone |
Amit Kucheria | 53d256e | 2019-11-20 21:15:12 +0530 | [diff] [blame] | 68 | * @tz: thermal_zone_device |
| 69 | * @trip: the trip point |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 70 | * |
| 71 | * Regulation Logic: a two point regulation, deliver cooling state depending |
| 72 | * on the previous state shown in this diagram: |
| 73 | * |
| 74 | * Fan: OFF ON |
| 75 | * |
| 76 | * | |
| 77 | * | |
| 78 | * trip_temp: +---->+ |
| 79 | * | | ^ |
| 80 | * | | | |
| 81 | * | | Temperature |
| 82 | * (trip_temp - hyst): +<----+ |
| 83 | * | |
| 84 | * | |
| 85 | * | |
| 86 | * |
| 87 | * * If the fan is not running and temperature exceeds trip_temp, the fan |
| 88 | * gets turned on. |
| 89 | * * In case the fan is running, temperature must fall below |
| 90 | * (trip_temp - hyst) so that the fan gets turned off again. |
| 91 | * |
| 92 | */ |
Rafael J. Wysocki | 8c35b1f | 2023-10-12 20:34:50 +0200 | [diff] [blame] | 93 | static int bang_bang_control(struct thermal_zone_device *tz, |
| 94 | const struct thermal_trip *trip) |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 95 | { |
| 96 | struct thermal_instance *instance; |
Daniel Lezcano | 7f725a2 | 2022-10-03 11:25:37 +0200 | [diff] [blame] | 97 | int ret; |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 98 | |
Daniel Lezcano | 670a5e3 | 2022-08-05 17:38:33 +0200 | [diff] [blame] | 99 | lockdep_assert_held(&tz->lock); |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 100 | |
Daniel Lezcano | 7f725a2 | 2022-10-03 11:25:37 +0200 | [diff] [blame] | 101 | ret = thermal_zone_trip_update(tz, trip); |
| 102 | if (ret) |
| 103 | return ret; |
Daniel Lezcano | 63561fe | 2022-08-05 17:38:32 +0200 | [diff] [blame] | 104 | |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 105 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) |
| 106 | thermal_cdev_update(instance->cdev); |
| 107 | |
Peter Feuerer | e4dbf98 | 2014-07-22 17:37:13 +0200 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static struct thermal_governor thermal_gov_bang_bang = { |
| 112 | .name = "bang_bang", |
| 113 | .throttle = bang_bang_control, |
| 114 | }; |
Daniel Lezcano | 57c5b2e | 2019-06-12 22:13:25 +0200 | [diff] [blame] | 115 | THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang); |