2 * step_wise.c - A step-by-step Thermal throttling governor
4 * Copyright (C) 2012 Intel Corp
5 * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include <linux/thermal.h>
27 #include "thermal_core.h"
30 * If the temperature is higher than a trip point,
31 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
32 * state for this trip point
33 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
34 * state for this trip point
35 * c. if the trend is THERMAL_TREND_RAISE_FULL, use upper limit
37 * d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit
39 * If the temperature is lower than a trip point,
40 * a. if the trend is THERMAL_TREND_RAISING, do nothing
41 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
42 * state for this trip point, if the cooling state already
43 * equals lower limit, deactivate the thermal instance
44 * c. if the trend is THERMAL_TREND_RAISE_FULL, do nothing
45 * d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit,
46 * if the cooling state already equals lower limit,
47 * deactive the thermal instance
50 static unsigned long get_target_state(struct thermal_instance *instance,
51 enum thermal_trend trend, bool throttle)
53 struct thermal_cooling_device *cdev = instance->cdev;
54 unsigned long cur_state;
56 cdev->ops->get_cur_state(cdev, &cur_state);
57 dev_dbg(&cdev->device, "cur_state=%ld\n", cur_state);
60 case THERMAL_TREND_RAISING:
62 cur_state = cur_state < instance->upper ?
63 (cur_state + 1) : instance->upper;
64 if (cur_state < instance->lower)
65 cur_state = instance->lower;
68 case THERMAL_TREND_RAISE_FULL:
70 cur_state = instance->upper;
72 case THERMAL_TREND_DROPPING:
73 if (cur_state == instance->lower) {
78 if (cur_state > instance->upper)
79 cur_state = instance->upper;
82 case THERMAL_TREND_DROP_FULL:
83 if (cur_state == instance->lower) {
87 cur_state = instance->lower;
96 static void update_passive_instance(struct thermal_zone_device *tz,
97 enum thermal_trip_type type, int value)
100 * If value is +1, activate a passive instance.
101 * If value is -1, deactivate a passive instance.
103 if (type == THERMAL_TRIP_PASSIVE || type == THERMAL_TRIPS_NONE)
104 tz->passive += value;
107 static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
110 enum thermal_trip_type trip_type;
111 enum thermal_trend trend;
112 struct thermal_instance *instance;
113 bool throttle = false;
116 if (trip == THERMAL_TRIPS_NONE) {
117 trip_temp = tz->forced_passive;
118 trip_type = THERMAL_TRIPS_NONE;
120 tz->ops->get_trip_temp(tz, trip, &trip_temp);
121 tz->ops->get_trip_type(tz, trip, &trip_type);
124 trend = get_tz_trend(tz, trip);
126 if (tz->temperature >= trip_temp)
129 dev_dbg(&tz->device, "Trip%d[type=%d,temp=%ld]:trend=%d,throttle=%d\n",
130 trip, trip_type, trip_temp, trend, throttle);
132 mutex_lock(&tz->lock);
134 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
135 if (instance->trip != trip)
138 old_target = instance->target;
139 instance->target = get_target_state(instance, trend, throttle);
140 dev_dbg(&instance->cdev->device, "old_target=%d, target=%d\n",
141 old_target, (int)instance->target);
143 /* Activate a passive thermal instance */
144 if (old_target == THERMAL_NO_TARGET &&
145 instance->target != THERMAL_NO_TARGET)
146 update_passive_instance(tz, trip_type, 1);
147 /* Deactivate a passive thermal instance */
148 else if (old_target != THERMAL_NO_TARGET &&
149 instance->target == THERMAL_NO_TARGET)
150 update_passive_instance(tz, trip_type, -1);
153 instance->cdev->updated = false; /* cdev needs update */
156 mutex_unlock(&tz->lock);
160 static int get_hyst_target(struct thermal_zone_device *tz)
163 unsigned long trip_temp, trip_hyst, hyst_temp;
165 if (tz->trips == 0 || !tz->ops->get_trip_hyst){
168 for (count = 0; count < tz->trips; count++) {
169 tz->ops->get_trip_temp(tz, count, &trip_temp);
170 tz->ops->get_trip_hyst(tz, count, &trip_hyst);
171 hyst_temp = trip_temp - trip_hyst;
172 printk("trip:%d, temp:%d, hyst_temp:%d\n", count, tz->temperature, hyst_temp);
173 if (tz->temperature < (long)hyst_temp + TRIP_TEMP_OFFSET)
179 static int get_trip_target(struct thermal_zone_device *tz)
182 unsigned long trip_temp;
184 if (tz->trips == 0 || !tz->ops->get_trip_temp)
187 for (count = 0; count < tz->trips; count++) {
188 tz->ops->get_trip_temp(tz, count, &trip_temp);
189 printk("trip:%d, temp:%d, trip_temp:%d\n", count, tz->temperature, trip_temp);
190 if (tz->temperature < (long)trip_temp - TRIP_TEMP_OFFSET)
196 static int get_step_wise_target(struct thermal_instance *instance,
197 struct thermal_zone_device *tz, int trip)
199 struct thermal_cooling_device *cdev = instance->cdev;
200 enum thermal_trend trend;
201 unsigned long cur_target;
202 unsigned long new_target;
204 cur_target = instance->target;
205 printk("%s: %s trip:%d cur_target:%d\n", cdev->type, __func__, trip, cur_target);
206 if (cur_target == THERMAL_NO_TARGET){
207 cur_target = instance->lower;
209 if (tz->ops->get_trend){
210 tz->ops->get_trend(tz, trip, &trend);
212 new_target = get_trip_target(tz);
213 printk("%s: %s trip:%d new_target:%d\n", cdev->type, __func__, trip, new_target);
214 if (new_target < cur_target){
215 trend = THERMAL_TREND_DROPPING;
216 if (!tz->ops->get_trip_hyst){
219 }else if(new_target >= cur_target){
224 case THERMAL_TREND_RAISING:
225 new_target = get_trip_target(tz);
226 printk("%s: %s trend raising, target:%d\n", cdev->type, __func__, new_target);
228 case THERMAL_TREND_DROPPING:
229 if (tz->ops->get_trip_hyst){
230 new_target = get_hyst_target(tz);
232 new_target = get_trip_target(tz);
234 printk("%s: %s trend dropping, target:%d\n", cdev->type, __func__, new_target);
236 case THERMAL_TREND_STABLE:
238 printk("%s: %s trend stable\n", cdev->type, __func__);
239 return instance->lower;
242 if (new_target > cur_target){
247 if (new_target < cur_target){
254 static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
256 struct thermal_instance *instance;
257 unsigned long target;
259 mutex_lock(&tz->lock);
260 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
261 if (instance->trip != trip)
263 target = get_step_wise_target(instance, tz, trip);
264 printk("%s: get target_state: %d\n", instance->cdev->type, target);
265 if (target == THERMAL_NO_TARGET){
268 if (target < instance->lower){
269 target = instance->lower;
271 if (target > instance->upper){
272 target = instance->upper;
274 instance->target = target;
275 instance->cdev->updated = false; /* cdev needs update */
277 mutex_unlock(&tz->lock);
284 * step_wise_throttle - throttles devices asscciated with the given zone
285 * @tz - thermal_zone_device
286 * @trip - the trip point
287 * @trip_type - type of the trip point
289 * Throttling Logic: This uses the trend of the thermal zone to throttle.
290 * If the thermal zone is 'heating up' this throttles all the cooling
291 * devices associated with the zone and its particular trip point, by one
292 * step. If the zone is 'cooling down' it brings back the performance of
293 * the devices by one step.
295 static int step_wise_throttle(struct thermal_zone_device *tz, int trip)
297 struct thermal_instance *instance;
299 thermal_zone_trip_update(tz, trip);
301 if (tz->forced_passive)
302 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE);
304 mutex_lock(&tz->lock);
306 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
307 thermal_cdev_update(instance->cdev);
309 mutex_unlock(&tz->lock);
314 static struct thermal_governor thermal_gov_step_wise = {
316 .throttle = step_wise_throttle,
319 int thermal_gov_step_wise_register(void)
321 return thermal_register_governor(&thermal_gov_step_wise);
324 void thermal_gov_step_wise_unregister(void)
326 thermal_unregister_governor(&thermal_gov_step_wise);