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
)
29 unsigned long trip_hyst
;
30 struct thermal_instance
*instance
;
32 tz
->ops
->get_trip_temp(tz
, trip
, &trip_temp
);
33 tz
->ops
->get_trip_hyst(tz
, trip
, &trip_hyst
);
35 dev_dbg(&tz
->device
, "Trip%d[temp=%ld]:temp=%d:hyst=%ld\n",
36 trip
, trip_temp
, tz
->temperature
,
39 mutex_lock(&tz
->lock
);
41 list_for_each_entry(instance
, &tz
->thermal_instances
, tz_node
) {
42 if (instance
->trip
!= trip
)
45 /* in case fan is in initial state, switch the fan off */
46 if (instance
->target
== THERMAL_NO_TARGET
)
49 /* in case fan is neither on nor off set the fan to active */
50 if (instance
->target
!= 0 && instance
->target
!= 1) {
51 pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n",
52 instance
->name
, instance
->target
);
57 * enable fan when temperature exceeds trip_temp and disable
58 * the fan in case it falls below trip_temp minus hysteresis
60 if (instance
->target
== 0 && tz
->temperature
>= trip_temp
)
62 else if (instance
->target
== 1 &&
63 tz
->temperature
< trip_temp
- trip_hyst
)
66 dev_dbg(&instance
->cdev
->device
, "target=%d\n",
67 (int)instance
->target
);
69 instance
->cdev
->updated
= false; /* cdev needs update */
72 mutex_unlock(&tz
->lock
);
76 * bang_bang_control - controls devices associated with the given zone
77 * @tz - thermal_zone_device
78 * @trip - the trip point
80 * Regulation Logic: a two point regulation, deliver cooling state depending
81 * on the previous state shown in this diagram:
91 * (trip_temp - hyst): +<----+
96 * * If the fan is not running and temperature exceeds trip_temp, the fan
98 * * In case the fan is running, temperature must fall below
99 * (trip_temp - hyst) so that the fan gets turned off again.
102 static int bang_bang_control(struct thermal_zone_device
*tz
, int trip
)
104 struct thermal_instance
*instance
;
106 thermal_zone_trip_update(tz
, trip
);
108 mutex_lock(&tz
->lock
);
110 list_for_each_entry(instance
, &tz
->thermal_instances
, tz_node
)
111 thermal_cdev_update(instance
->cdev
);
113 mutex_unlock(&tz
->lock
);
118 static struct thermal_governor thermal_gov_bang_bang
= {
120 .throttle
= bang_bang_control
,
123 int thermal_gov_bang_bang_register(void)
125 return thermal_register_governor(&thermal_gov_bang_bang
);
128 void thermal_gov_bang_bang_unregister(void)
130 thermal_unregister_governor(&thermal_gov_bang_bang
);