1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/thermal.h>
14#include <trace/events/thermal.h>
15
16#include "thermal_core.h"
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37static unsigned long get_target_state(struct thermal_instance *instance,
38 enum thermal_trend trend, bool throttle)
39{
40 struct thermal_cooling_device *cdev = instance->cdev;
41 unsigned long cur_state;
42 unsigned long next_target;
43
44
45
46
47
48
49 cdev->ops->get_cur_state(cdev, &cur_state);
50 next_target = instance->target;
51 dev_dbg(&cdev->device, "cur_state=%ld\n", cur_state);
52
53 if (!instance->initialized) {
54 if (throttle) {
55 next_target = (cur_state + 1) >= instance->upper ?
56 instance->upper :
57 ((cur_state + 1) < instance->lower ?
58 instance->lower : (cur_state + 1));
59 } else {
60 next_target = THERMAL_NO_TARGET;
61 }
62
63 return next_target;
64 }
65
66 switch (trend) {
67 case THERMAL_TREND_RAISING:
68 if (throttle) {
69 next_target = cur_state < instance->upper ?
70 (cur_state + 1) : instance->upper;
71 if (next_target < instance->lower)
72 next_target = instance->lower;
73 }
74 break;
75 case THERMAL_TREND_RAISE_FULL:
76 if (throttle)
77 next_target = instance->upper;
78 break;
79 case THERMAL_TREND_DROPPING:
80 if (cur_state <= instance->lower) {
81 if (!throttle)
82 next_target = THERMAL_NO_TARGET;
83 } else {
84 if (!throttle) {
85 next_target = cur_state - 1;
86 if (next_target > instance->upper)
87 next_target = instance->upper;
88 }
89 }
90 break;
91 case THERMAL_TREND_DROP_FULL:
92 if (cur_state == instance->lower) {
93 if (!throttle)
94 next_target = THERMAL_NO_TARGET;
95 } else
96 next_target = instance->lower;
97 break;
98 default:
99 break;
100 }
101
102 return next_target;
103}
104
105static void update_passive_instance(struct thermal_zone_device *tz,
106 enum thermal_trip_type type, int value)
107{
108
109
110
111
112 if (type == THERMAL_TRIP_PASSIVE || type == THERMAL_TRIPS_NONE)
113 tz->passive += value;
114}
115
116static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
117{
118 int trip_temp;
119 enum thermal_trip_type trip_type;
120 enum thermal_trend trend;
121 struct thermal_instance *instance;
122 bool throttle = false;
123 int old_target;
124
125 if (trip == THERMAL_TRIPS_NONE) {
126 trip_temp = tz->forced_passive;
127 trip_type = THERMAL_TRIPS_NONE;
128 } else {
129 tz->ops->get_trip_temp(tz, trip, &trip_temp);
130 tz->ops->get_trip_type(tz, trip, &trip_type);
131 }
132
133 trend = get_tz_trend(tz, trip);
134
135 if (tz->temperature >= trip_temp) {
136 throttle = true;
137 trace_thermal_zone_trip(tz, trip, trip_type);
138 }
139
140 dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n",
141 trip, trip_type, trip_temp, trend, throttle);
142
143 mutex_lock(&tz->lock);
144
145 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
146 if (instance->trip != trip)
147 continue;
148
149 old_target = instance->target;
150 instance->target = get_target_state(instance, trend, throttle);
151 dev_dbg(&instance->cdev->device, "old_target=%d, target=%d\n",
152 old_target, (int)instance->target);
153
154 if (instance->initialized && old_target == instance->target)
155 continue;
156
157
158 if (old_target == THERMAL_NO_TARGET &&
159 instance->target != THERMAL_NO_TARGET)
160 update_passive_instance(tz, trip_type, 1);
161
162 else if (old_target != THERMAL_NO_TARGET &&
163 instance->target == THERMAL_NO_TARGET)
164 update_passive_instance(tz, trip_type, -1);
165
166 instance->initialized = true;
167 mutex_lock(&instance->cdev->lock);
168 instance->cdev->updated = false;
169 mutex_unlock(&instance->cdev->lock);
170 }
171
172 mutex_unlock(&tz->lock);
173}
174
175
176
177
178
179
180
181
182
183
184
185
186static int step_wise_throttle(struct thermal_zone_device *tz, int trip)
187{
188 struct thermal_instance *instance;
189
190 thermal_zone_trip_update(tz, trip);
191
192 if (tz->forced_passive)
193 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE);
194
195 mutex_lock(&tz->lock);
196
197 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
198 thermal_cdev_update(instance->cdev);
199
200 mutex_unlock(&tz->lock);
201
202 return 0;
203}
204
205static struct thermal_governor thermal_gov_step_wise = {
206 .name = "step_wise",
207 .throttle = step_wise_throttle,
208};
209THERMAL_GOVERNOR_DECLARE(thermal_gov_step_wise);
210