2 * gov_bang_bang.c - A simple thermal throttling governor using hysteresis
4 * Copyright (C) 2014 Peter Feuerer <peter@piie.net>
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>
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.
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.
22 #include <linux/thermal.h>
24 #include "thermal_core.h"
26 static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
28 int trip_temp, trip_hyst;
29 struct thermal_instance *instance;
31 tz->ops->get_trip_temp(tz, trip, &trip_temp);
33 if (!tz->ops->get_trip_hyst) {
34 pr_warn_once("Undefined get_trip_hyst for thermal zone %s - "
35 "running with default hysteresis zero\n", tz->type);
38 tz->ops->get_trip_hyst(tz, trip, &trip_hyst);
40 dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
41 trip, trip_temp, tz->temperature,
44 mutex_lock(&tz->lock);
46 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
47 if (instance->trip != trip)
50 /* in case fan is in initial state, switch the fan off */
51 if (instance->target == THERMAL_NO_TARGET)
54 /* in case fan is neither on nor off set the fan to active */
55 if (instance->target != 0 && instance->target != 1) {
56 pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n",
57 instance->name, instance->target);
62 * enable fan when temperature exceeds trip_temp and disable
63 * the fan in case it falls below trip_temp minus hysteresis
65 if (instance->target == 0 && tz->temperature >= trip_temp)
67 else if (instance->target == 1 &&
68 tz->temperature < trip_temp - trip_hyst)
71 dev_dbg(&instance->cdev->device, "target=%d\n",
72 (int)instance->target);
74 mutex_lock(&instance->cdev->lock);
75 instance->cdev->updated = false; /* cdev needs update */
76 mutex_unlock(&instance->cdev->lock);
79 mutex_unlock(&tz->lock);
83 * bang_bang_control - controls devices associated with the given zone
84 * @tz - thermal_zone_device
85 * @trip - the trip point
87 * Regulation Logic: a two point regulation, deliver cooling state depending
88 * on the previous state shown in this diagram:
98 * (trip_temp - hyst): +<----+
103 * * If the fan is not running and temperature exceeds trip_temp, the fan
105 * * In case the fan is running, temperature must fall below
106 * (trip_temp - hyst) so that the fan gets turned off again.
109 static int bang_bang_control(struct thermal_zone_device *tz, int trip)
111 struct thermal_instance *instance;
113 thermal_zone_trip_update(tz, trip);
115 mutex_lock(&tz->lock);
117 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
118 thermal_cdev_update(instance->cdev);
120 mutex_unlock(&tz->lock);
125 static struct thermal_governor thermal_gov_bang_bang = {
127 .throttle = bang_bang_control,
130 int thermal_gov_bang_bang_register(void)
132 return thermal_register_governor(&thermal_gov_bang_bang);
135 void thermal_gov_bang_bang_unregister(void)
137 thermal_unregister_governor(&thermal_gov_bang_bang);