blob: fc52016d4e85be590e15f4e1976b8378d2c456d2 [file] [log] [blame]
Peter Feuerere4dbf982014-07-22 17:37:13 +02001/*
2 * gov_bang_bang.c - A simple thermal throttling governor using hysteresis
3 *
4 * Copyright (C) 2014 Peter Feuerer <peter@piie.net>
5 *
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>
9 *
10 *
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.
14 *
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.
19 *
20 */
21
22#include <linux/thermal.h>
23
24#include "thermal_core.h"
25
26static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
27{
Sascha Hauer17e83512015-07-24 08:12:54 +020028 int trip_temp, trip_hyst;
Peter Feuerere4dbf982014-07-22 17:37:13 +020029 struct thermal_instance *instance;
30
31 tz->ops->get_trip_temp(tz, trip, &trip_temp);
Michele Di Giorgio191b0752016-05-11 10:49:07 +010032
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);
36 trip_hyst = 0;
37 } else
38 tz->ops->get_trip_hyst(tz, trip, &trip_hyst);
Peter Feuerere4dbf982014-07-22 17:37:13 +020039
Sascha Hauer17e83512015-07-24 08:12:54 +020040 dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
Peter Feuerere4dbf982014-07-22 17:37:13 +020041 trip, trip_temp, tz->temperature,
42 trip_hyst);
43
44 mutex_lock(&tz->lock);
45
46 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
47 if (instance->trip != trip)
48 continue;
49
50 /* in case fan is in initial state, switch the fan off */
51 if (instance->target == THERMAL_NO_TARGET)
52 instance->target = 0;
53
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);
58 instance->target = 1;
59 }
60
61 /*
62 * enable fan when temperature exceeds trip_temp and disable
63 * the fan in case it falls below trip_temp minus hysteresis
64 */
65 if (instance->target == 0 && tz->temperature >= trip_temp)
66 instance->target = 1;
67 else if (instance->target == 1 &&
68 tz->temperature < trip_temp - trip_hyst)
69 instance->target = 0;
70
71 dev_dbg(&instance->cdev->device, "target=%d\n",
72 (int)instance->target);
73
74 instance->cdev->updated = false; /* cdev needs update */
75 }
76
77 mutex_unlock(&tz->lock);
78}
79
80/**
81 * bang_bang_control - controls devices associated with the given zone
82 * @tz - thermal_zone_device
83 * @trip - the trip point
84 *
85 * Regulation Logic: a two point regulation, deliver cooling state depending
86 * on the previous state shown in this diagram:
87 *
88 * Fan: OFF ON
89 *
90 * |
91 * |
92 * trip_temp: +---->+
93 * | | ^
94 * | | |
95 * | | Temperature
96 * (trip_temp - hyst): +<----+
97 * |
98 * |
99 * |
100 *
101 * * If the fan is not running and temperature exceeds trip_temp, the fan
102 * gets turned on.
103 * * In case the fan is running, temperature must fall below
104 * (trip_temp - hyst) so that the fan gets turned off again.
105 *
106 */
107static int bang_bang_control(struct thermal_zone_device *tz, int trip)
108{
109 struct thermal_instance *instance;
110
111 thermal_zone_trip_update(tz, trip);
112
113 mutex_lock(&tz->lock);
114
115 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
116 thermal_cdev_update(instance->cdev);
117
118 mutex_unlock(&tz->lock);
119
120 return 0;
121}
122
123static struct thermal_governor thermal_gov_bang_bang = {
124 .name = "bang_bang",
125 .throttle = bang_bang_control,
126};
127
128int thermal_gov_bang_bang_register(void)
129{
130 return thermal_register_governor(&thermal_gov_bang_bang);
131}
132
133void thermal_gov_bang_bang_unregister(void)
134{
135 thermal_unregister_governor(&thermal_gov_bang_bang);
136}