1 // SPDX-License-Identifier: GPL-2.0-only
3 * gov_bang_bang.c - A simple thermal throttling governor using hysteresis
7 * Based on step_wise.c with following Copyrights:
8 * Copyright (C) 2012 Intel Corp
12 #include <linux/thermal.h>
14 #include "thermal_core.h"
16 static int thermal_zone_trip_update(struct thermal_zone_device *tz,
17 const struct thermal_trip *trip)
19 int trip_index = thermal_zone_trip_id(tz, trip);
20 struct thermal_instance *instance;
22 if (!trip->hysteresis)
23 dev_info_once(&tz->device,
24 "Zero hysteresis value for thermal zone %s\n", tz->type);
26 dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
27 trip_index, trip->temperature, tz->temperature,
30 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
31 if (instance->trip != trip)
34 /* in case fan is in initial state, switch the fan off */
35 if (instance->target == THERMAL_NO_TARGET)
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);
46 * enable fan when temperature exceeds trip_temp and disable
47 * the fan in case it falls below trip_temp minus hysteresis
49 if (instance->target == 0 && tz->temperature >= trip->temperature)
51 else if (instance->target == 1 &&
52 tz->temperature <= trip->temperature - trip->hysteresis)
55 dev_dbg(&instance->cdev->device, "target=%d\n",
56 (int)instance->target);
58 mutex_lock(&instance->cdev->lock);
59 instance->cdev->updated = false; /* cdev needs update */
60 mutex_unlock(&instance->cdev->lock);
67 * bang_bang_control - controls devices associated with the given zone
68 * @tz: thermal_zone_device
69 * @trip: the trip point
71 * Regulation Logic: a two point regulation, deliver cooling state depending
72 * on the previous state shown in this diagram:
82 * (trip_temp - hyst): +<----+
87 * * If the fan is not running and temperature exceeds trip_temp, the fan
89 * * In case the fan is running, temperature must fall below
90 * (trip_temp - hyst) so that the fan gets turned off again.
93 static int bang_bang_control(struct thermal_zone_device *tz,
94 const struct thermal_trip *trip)
96 struct thermal_instance *instance;
99 lockdep_assert_held(&tz->lock);
101 ret = thermal_zone_trip_update(tz, trip);
105 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
106 thermal_cdev_update(instance->cdev);
111 static struct thermal_governor thermal_gov_bang_bang = {
113 .throttle = bang_bang_control,
115 THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);