blob: ea57759eb095c5b4f6a75cc6eff463862516936e [file] [log] [blame]
Javi Merino6b775e82015-03-02 17:17:19 +00001/*
2 * A power allocator to manage temperature
3 *
4 * Copyright (C) 2014 ARM Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#define pr_fmt(fmt) "Power allocator: " fmt
17
18#include <linux/rculist.h>
19#include <linux/slab.h>
20#include <linux/thermal.h>
21
Javi Merino6828a472015-03-02 17:17:20 +000022#define CREATE_TRACE_POINTS
23#include <trace/events/thermal_power_allocator.h>
24
Javi Merino6b775e82015-03-02 17:17:19 +000025#include "thermal_core.h"
26
Javi Merino8b7b3902015-09-14 14:23:52 +010027#define INVALID_TRIP -1
28
Javi Merino6b775e82015-03-02 17:17:19 +000029#define FRAC_BITS 10
30#define int_to_frac(x) ((x) << FRAC_BITS)
31#define frac_to_int(x) ((x) >> FRAC_BITS)
32
33/**
34 * mul_frac() - multiply two fixed-point numbers
35 * @x: first multiplicand
36 * @y: second multiplicand
37 *
38 * Return: the result of multiplying two fixed-point numbers. The
39 * result is also a fixed-point number.
40 */
41static inline s64 mul_frac(s64 x, s64 y)
42{
43 return (x * y) >> FRAC_BITS;
44}
45
46/**
47 * div_frac() - divide two fixed-point numbers
48 * @x: the dividend
49 * @y: the divisor
50 *
51 * Return: the result of dividing two fixed-point numbers. The
52 * result is also a fixed-point number.
53 */
54static inline s64 div_frac(s64 x, s64 y)
55{
56 return div_s64(x << FRAC_BITS, y);
57}
58
59/**
60 * struct power_allocator_params - parameters for the power allocator governor
61 * @err_integral: accumulated error in the PID controller.
62 * @prev_err: error in the previous iteration of the PID controller.
63 * Used to calculate the derivative term.
64 * @trip_switch_on: first passive trip point of the thermal zone. The
65 * governor switches on when this trip point is crossed.
Javi Merino8b7b3902015-09-14 14:23:52 +010066 * If the thermal zone only has one passive trip point,
67 * @trip_switch_on should be INVALID_TRIP.
Javi Merino6b775e82015-03-02 17:17:19 +000068 * @trip_max_desired_temperature: last passive trip point of the thermal
69 * zone. The temperature we are
70 * controlling for.
71 */
72struct power_allocator_params {
73 s64 err_integral;
74 s32 prev_err;
75 int trip_switch_on;
76 int trip_max_desired_temperature;
77};
78
79/**
Javi Merinoe055bb02015-09-14 14:23:51 +010080 * estimate_sustainable_power() - Estimate the sustainable power of a thermal zone
81 * @tz: thermal zone we are operating in
82 *
83 * For thermal zones that don't provide a sustainable_power in their
84 * thermal_zone_params, estimate one. Calculate it using the minimum
85 * power of all the cooling devices as that gives a valid value that
86 * can give some degree of functionality. For optimal performance of
87 * this governor, provide a sustainable_power in the thermal zone's
88 * thermal_zone_params.
89 */
90static u32 estimate_sustainable_power(struct thermal_zone_device *tz)
91{
92 u32 sustainable_power = 0;
93 struct thermal_instance *instance;
94 struct power_allocator_params *params = tz->governor_data;
95
96 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
97 struct thermal_cooling_device *cdev = instance->cdev;
98 u32 min_power;
99
100 if (instance->trip != params->trip_max_desired_temperature)
101 continue;
102
103 if (power_actor_get_min_power(cdev, tz, &min_power))
104 continue;
105
106 sustainable_power += min_power;
107 }
108
109 return sustainable_power;
110}
111
112/**
113 * estimate_pid_constants() - Estimate the constants for the PID controller
114 * @tz: thermal zone for which to estimate the constants
115 * @sustainable_power: sustainable power for the thermal zone
116 * @trip_switch_on: trip point number for the switch on temperature
117 * @control_temp: target temperature for the power allocator governor
118 * @force: whether to force the update of the constants
119 *
120 * This function is used to update the estimation of the PID
121 * controller constants in struct thermal_zone_parameters.
122 * Sustainable power is provided in case it was estimated. The
123 * estimated sustainable_power should not be stored in the
124 * thermal_zone_parameters so it has to be passed explicitly to this
125 * function.
126 *
127 * If @force is not set, the values in the thermal zone's parameters
128 * are preserved if they are not zero. If @force is set, the values
129 * in thermal zone's parameters are overwritten.
130 */
131static void estimate_pid_constants(struct thermal_zone_device *tz,
132 u32 sustainable_power, int trip_switch_on,
133 int control_temp, bool force)
134{
135 int ret;
136 int switch_on_temp;
137 u32 temperature_threshold;
138
139 ret = tz->ops->get_trip_temp(tz, trip_switch_on, &switch_on_temp);
140 if (ret)
141 switch_on_temp = 0;
142
143 temperature_threshold = control_temp - switch_on_temp;
144
145 if (!tz->tzp->k_po || force)
146 tz->tzp->k_po = int_to_frac(sustainable_power) /
147 temperature_threshold;
148
149 if (!tz->tzp->k_pu || force)
150 tz->tzp->k_pu = int_to_frac(2 * sustainable_power) /
151 temperature_threshold;
152
153 if (!tz->tzp->k_i || force)
154 tz->tzp->k_i = int_to_frac(10) / 1000;
155 /*
156 * The default for k_d and integral_cutoff is 0, so we can
157 * leave them as they are.
158 */
159}
160
161/**
Javi Merino6b775e82015-03-02 17:17:19 +0000162 * pid_controller() - PID controller
163 * @tz: thermal zone we are operating in
164 * @current_temp: the current temperature in millicelsius
165 * @control_temp: the target temperature in millicelsius
166 * @max_allocatable_power: maximum allocatable power for this thermal zone
167 *
168 * This PID controller increases the available power budget so that the
169 * temperature of the thermal zone gets as close as possible to
170 * @control_temp and limits the power if it exceeds it. k_po is the
171 * proportional term when we are overshooting, k_pu is the
172 * proportional term when we are undershooting. integral_cutoff is a
173 * threshold below which we stop accumulating the error. The
174 * accumulated error is only valid if the requested power will make
175 * the system warmer. If the system is mostly idle, there's no point
176 * in accumulating positive error.
177 *
178 * Return: The power budget for the next period.
179 */
180static u32 pid_controller(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200181 int current_temp,
182 int control_temp,
Javi Merino6b775e82015-03-02 17:17:19 +0000183 u32 max_allocatable_power)
184{
185 s64 p, i, d, power_range;
186 s32 err, max_power_frac;
Javi Merinoe055bb02015-09-14 14:23:51 +0100187 u32 sustainable_power;
Javi Merino6b775e82015-03-02 17:17:19 +0000188 struct power_allocator_params *params = tz->governor_data;
189
190 max_power_frac = int_to_frac(max_allocatable_power);
191
Javi Merinoe055bb02015-09-14 14:23:51 +0100192 if (tz->tzp->sustainable_power) {
193 sustainable_power = tz->tzp->sustainable_power;
194 } else {
195 sustainable_power = estimate_sustainable_power(tz);
196 estimate_pid_constants(tz, sustainable_power,
197 params->trip_switch_on, control_temp,
198 true);
199 }
200
Sascha Hauer17e83512015-07-24 08:12:54 +0200201 err = control_temp - current_temp;
Javi Merino6b775e82015-03-02 17:17:19 +0000202 err = int_to_frac(err);
203
204 /* Calculate the proportional term */
205 p = mul_frac(err < 0 ? tz->tzp->k_po : tz->tzp->k_pu, err);
206
207 /*
208 * Calculate the integral term
209 *
210 * if the error is less than cut off allow integration (but
211 * the integral is limited to max power)
212 */
213 i = mul_frac(tz->tzp->k_i, params->err_integral);
214
215 if (err < int_to_frac(tz->tzp->integral_cutoff)) {
216 s64 i_next = i + mul_frac(tz->tzp->k_i, err);
217
218 if (abs64(i_next) < max_power_frac) {
219 i = i_next;
220 params->err_integral += err;
221 }
222 }
223
224 /*
225 * Calculate the derivative term
226 *
227 * We do err - prev_err, so with a positive k_d, a decreasing
228 * error (i.e. driving closer to the line) results in less
229 * power being applied, slowing down the controller)
230 */
231 d = mul_frac(tz->tzp->k_d, err - params->prev_err);
232 d = div_frac(d, tz->passive_delay);
233 params->prev_err = err;
234
235 power_range = p + i + d;
236
237 /* feed-forward the known sustainable dissipatable power */
Javi Merinoe055bb02015-09-14 14:23:51 +0100238 power_range = sustainable_power + frac_to_int(power_range);
Javi Merino6b775e82015-03-02 17:17:19 +0000239
Javi Merino6828a472015-03-02 17:17:20 +0000240 power_range = clamp(power_range, (s64)0, (s64)max_allocatable_power);
241
242 trace_thermal_power_allocator_pid(tz, frac_to_int(err),
243 frac_to_int(params->err_integral),
244 frac_to_int(p), frac_to_int(i),
245 frac_to_int(d), power_range);
246
247 return power_range;
Javi Merino6b775e82015-03-02 17:17:19 +0000248}
249
250/**
251 * divvy_up_power() - divvy the allocated power between the actors
252 * @req_power: each actor's requested power
253 * @max_power: each actor's maximum available power
254 * @num_actors: size of the @req_power, @max_power and @granted_power's array
255 * @total_req_power: sum of @req_power
256 * @power_range: total allocated power
257 * @granted_power: output array: each actor's granted power
258 * @extra_actor_power: an appropriately sized array to be used in the
259 * function as temporary storage of the extra power given
260 * to the actors
261 *
262 * This function divides the total allocated power (@power_range)
263 * fairly between the actors. It first tries to give each actor a
264 * share of the @power_range according to how much power it requested
265 * compared to the rest of the actors. For example, if only one actor
266 * requests power, then it receives all the @power_range. If
267 * three actors each requests 1mW, each receives a third of the
268 * @power_range.
269 *
270 * If any actor received more than their maximum power, then that
271 * surplus is re-divvied among the actors based on how far they are
272 * from their respective maximums.
273 *
274 * Granted power for each actor is written to @granted_power, which
275 * should've been allocated by the calling function.
276 */
277static void divvy_up_power(u32 *req_power, u32 *max_power, int num_actors,
278 u32 total_req_power, u32 power_range,
279 u32 *granted_power, u32 *extra_actor_power)
280{
281 u32 extra_power, capped_extra_power;
282 int i;
283
284 /*
285 * Prevent division by 0 if none of the actors request power.
286 */
287 if (!total_req_power)
288 total_req_power = 1;
289
290 capped_extra_power = 0;
291 extra_power = 0;
292 for (i = 0; i < num_actors; i++) {
293 u64 req_range = req_power[i] * power_range;
294
Javi Merinoea54cac2015-05-02 10:15:24 +0100295 granted_power[i] = DIV_ROUND_CLOSEST_ULL(req_range,
296 total_req_power);
Javi Merino6b775e82015-03-02 17:17:19 +0000297
298 if (granted_power[i] > max_power[i]) {
299 extra_power += granted_power[i] - max_power[i];
300 granted_power[i] = max_power[i];
301 }
302
303 extra_actor_power[i] = max_power[i] - granted_power[i];
304 capped_extra_power += extra_actor_power[i];
305 }
306
307 if (!extra_power)
308 return;
309
310 /*
311 * Re-divvy the reclaimed extra among actors based on
312 * how far they are from the max
313 */
314 extra_power = min(extra_power, capped_extra_power);
315 if (capped_extra_power > 0)
316 for (i = 0; i < num_actors; i++)
317 granted_power[i] += (extra_actor_power[i] *
318 extra_power) / capped_extra_power;
319}
320
321static int allocate_power(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200322 int current_temp,
323 int control_temp)
Javi Merino6b775e82015-03-02 17:17:19 +0000324{
325 struct thermal_instance *instance;
326 struct power_allocator_params *params = tz->governor_data;
327 u32 *req_power, *max_power, *granted_power, *extra_actor_power;
Javi Merinod5f83102015-07-03 10:24:33 +0100328 u32 *weighted_req_power;
329 u32 total_req_power, max_allocatable_power, total_weighted_req_power;
Javi Merino6828a472015-03-02 17:17:20 +0000330 u32 total_granted_power, power_range;
Javi Merino6b775e82015-03-02 17:17:19 +0000331 int i, num_actors, total_weight, ret = 0;
332 int trip_max_desired_temperature = params->trip_max_desired_temperature;
333
334 mutex_lock(&tz->lock);
335
336 num_actors = 0;
337 total_weight = 0;
338 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
339 if ((instance->trip == trip_max_desired_temperature) &&
340 cdev_is_power_actor(instance->cdev)) {
341 num_actors++;
342 total_weight += instance->weight;
343 }
344 }
345
346 /*
Javi Merinod5f83102015-07-03 10:24:33 +0100347 * We need to allocate five arrays of the same size:
348 * req_power, max_power, granted_power, extra_actor_power and
349 * weighted_req_power. They are going to be needed until this
350 * function returns. Allocate them all in one go to simplify
351 * the allocation and deallocation logic.
Javi Merino6b775e82015-03-02 17:17:19 +0000352 */
353 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*max_power));
354 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*granted_power));
355 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power));
Javi Merinod5f83102015-07-03 10:24:33 +0100356 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*weighted_req_power));
Javi Merino9751a9e42015-08-25 19:22:35 +0100357 req_power = kcalloc(num_actors * 5, sizeof(*req_power), GFP_KERNEL);
Javi Merino6b775e82015-03-02 17:17:19 +0000358 if (!req_power) {
359 ret = -ENOMEM;
360 goto unlock;
361 }
362
363 max_power = &req_power[num_actors];
364 granted_power = &req_power[2 * num_actors];
365 extra_actor_power = &req_power[3 * num_actors];
Javi Merinod5f83102015-07-03 10:24:33 +0100366 weighted_req_power = &req_power[4 * num_actors];
Javi Merino6b775e82015-03-02 17:17:19 +0000367
368 i = 0;
Javi Merinod5f83102015-07-03 10:24:33 +0100369 total_weighted_req_power = 0;
Javi Merino6b775e82015-03-02 17:17:19 +0000370 total_req_power = 0;
371 max_allocatable_power = 0;
372
373 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
374 int weight;
375 struct thermal_cooling_device *cdev = instance->cdev;
376
377 if (instance->trip != trip_max_desired_temperature)
378 continue;
379
380 if (!cdev_is_power_actor(cdev))
381 continue;
382
383 if (cdev->ops->get_requested_power(cdev, tz, &req_power[i]))
384 continue;
385
386 if (!total_weight)
387 weight = 1 << FRAC_BITS;
388 else
389 weight = instance->weight;
390
Javi Merinod5f83102015-07-03 10:24:33 +0100391 weighted_req_power[i] = frac_to_int(weight * req_power[i]);
Javi Merino6b775e82015-03-02 17:17:19 +0000392
393 if (power_actor_get_max_power(cdev, tz, &max_power[i]))
394 continue;
395
396 total_req_power += req_power[i];
397 max_allocatable_power += max_power[i];
Javi Merinod5f83102015-07-03 10:24:33 +0100398 total_weighted_req_power += weighted_req_power[i];
Javi Merino6b775e82015-03-02 17:17:19 +0000399
400 i++;
401 }
402
403 power_range = pid_controller(tz, current_temp, control_temp,
404 max_allocatable_power);
405
Javi Merinod5f83102015-07-03 10:24:33 +0100406 divvy_up_power(weighted_req_power, max_power, num_actors,
407 total_weighted_req_power, power_range, granted_power,
408 extra_actor_power);
Javi Merino6b775e82015-03-02 17:17:19 +0000409
Javi Merino6828a472015-03-02 17:17:20 +0000410 total_granted_power = 0;
Javi Merino6b775e82015-03-02 17:17:19 +0000411 i = 0;
412 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
413 if (instance->trip != trip_max_desired_temperature)
414 continue;
415
416 if (!cdev_is_power_actor(instance->cdev))
417 continue;
418
419 power_actor_set_power(instance->cdev, instance,
420 granted_power[i]);
Javi Merino6828a472015-03-02 17:17:20 +0000421 total_granted_power += granted_power[i];
Javi Merino6b775e82015-03-02 17:17:19 +0000422
423 i++;
424 }
425
Javi Merino6828a472015-03-02 17:17:20 +0000426 trace_thermal_power_allocator(tz, req_power, total_req_power,
427 granted_power, total_granted_power,
428 num_actors, power_range,
429 max_allocatable_power, current_temp,
Sascha Hauer17e83512015-07-24 08:12:54 +0200430 control_temp - current_temp);
Javi Merino6828a472015-03-02 17:17:20 +0000431
Dmitry Torokhovcf736ea2015-08-04 09:33:40 -0700432 kfree(req_power);
Javi Merino6b775e82015-03-02 17:17:19 +0000433unlock:
434 mutex_unlock(&tz->lock);
435
436 return ret;
437}
438
Javi Merino8b7b3902015-09-14 14:23:52 +0100439/**
440 * get_governor_trips() - get the number of the two trip points that are key for this governor
441 * @tz: thermal zone to operate on
442 * @params: pointer to private data for this governor
443 *
444 * The power allocator governor works optimally with two trips points:
445 * a "switch on" trip point and a "maximum desired temperature". These
446 * are defined as the first and last passive trip points.
447 *
448 * If there is only one trip point, then that's considered to be the
449 * "maximum desired temperature" trip point and the governor is always
450 * on. If there are no passive or active trip points, then the
451 * governor won't do anything. In fact, its throttle function
452 * won't be called at all.
453 */
454static void get_governor_trips(struct thermal_zone_device *tz,
455 struct power_allocator_params *params)
Javi Merino6b775e82015-03-02 17:17:19 +0000456{
Javi Merino8b7b3902015-09-14 14:23:52 +0100457 int i, last_active, last_passive;
Javi Merino6b775e82015-03-02 17:17:19 +0000458 bool found_first_passive;
459
460 found_first_passive = false;
Javi Merino8b7b3902015-09-14 14:23:52 +0100461 last_active = INVALID_TRIP;
462 last_passive = INVALID_TRIP;
Javi Merino6b775e82015-03-02 17:17:19 +0000463
464 for (i = 0; i < tz->trips; i++) {
465 enum thermal_trip_type type;
Javi Merino8b7b3902015-09-14 14:23:52 +0100466 int ret;
Javi Merino6b775e82015-03-02 17:17:19 +0000467
468 ret = tz->ops->get_trip_type(tz, i, &type);
Javi Merino8b7b3902015-09-14 14:23:52 +0100469 if (ret) {
470 dev_warn(&tz->device,
471 "Failed to get trip point %d type: %d\n", i,
472 ret);
473 continue;
474 }
Javi Merino6b775e82015-03-02 17:17:19 +0000475
Javi Merino8b7b3902015-09-14 14:23:52 +0100476 if (type == THERMAL_TRIP_PASSIVE) {
477 if (!found_first_passive) {
Javi Merino6b775e82015-03-02 17:17:19 +0000478 params->trip_switch_on = i;
479 found_first_passive = true;
Javi Merino8b7b3902015-09-14 14:23:52 +0100480 } else {
481 last_passive = i;
Javi Merino6b775e82015-03-02 17:17:19 +0000482 }
Javi Merino8b7b3902015-09-14 14:23:52 +0100483 } else if (type == THERMAL_TRIP_ACTIVE) {
484 last_active = i;
Javi Merino6b775e82015-03-02 17:17:19 +0000485 } else {
486 break;
487 }
488 }
489
Javi Merino8b7b3902015-09-14 14:23:52 +0100490 if (last_passive != INVALID_TRIP) {
Javi Merino6b775e82015-03-02 17:17:19 +0000491 params->trip_max_desired_temperature = last_passive;
Javi Merino8b7b3902015-09-14 14:23:52 +0100492 } else if (found_first_passive) {
493 params->trip_max_desired_temperature = params->trip_switch_on;
494 params->trip_switch_on = INVALID_TRIP;
Javi Merino6b775e82015-03-02 17:17:19 +0000495 } else {
Javi Merino8b7b3902015-09-14 14:23:52 +0100496 params->trip_switch_on = INVALID_TRIP;
497 params->trip_max_desired_temperature = last_active;
Javi Merino6b775e82015-03-02 17:17:19 +0000498 }
Javi Merino6b775e82015-03-02 17:17:19 +0000499}
500
501static void reset_pid_controller(struct power_allocator_params *params)
502{
503 params->err_integral = 0;
504 params->prev_err = 0;
505}
506
507static void allow_maximum_power(struct thermal_zone_device *tz)
508{
509 struct thermal_instance *instance;
510 struct power_allocator_params *params = tz->governor_data;
511
512 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
513 if ((instance->trip != params->trip_max_desired_temperature) ||
514 (!cdev_is_power_actor(instance->cdev)))
515 continue;
516
517 instance->target = 0;
518 instance->cdev->updated = false;
519 thermal_cdev_update(instance->cdev);
520 }
521}
522
523/**
524 * power_allocator_bind() - bind the power_allocator governor to a thermal zone
525 * @tz: thermal zone to bind it to
526 *
Javi Merino8b7b3902015-09-14 14:23:52 +0100527 * Initialize the PID controller parameters and bind it to the thermal
528 * zone.
Javi Merino6b775e82015-03-02 17:17:19 +0000529 *
Javi Merino8b7b3902015-09-14 14:23:52 +0100530 * Return: 0 on success, -EINVAL if the thermal zone doesn't have tzp or -ENOMEM
Javi Merino6b775e82015-03-02 17:17:19 +0000531 * if we ran out of memory.
532 */
533static int power_allocator_bind(struct thermal_zone_device *tz)
534{
535 int ret;
536 struct power_allocator_params *params;
Javi Merinoe055bb02015-09-14 14:23:51 +0100537 int control_temp;
Javi Merino6b775e82015-03-02 17:17:19 +0000538
Javi Merinoe055bb02015-09-14 14:23:51 +0100539 if (!tz->tzp)
Javi Merino6b775e82015-03-02 17:17:19 +0000540 return -EINVAL;
Javi Merino6b775e82015-03-02 17:17:19 +0000541
Dmitry Torokhovcf736ea2015-08-04 09:33:40 -0700542 params = kzalloc(sizeof(*params), GFP_KERNEL);
Javi Merino6b775e82015-03-02 17:17:19 +0000543 if (!params)
544 return -ENOMEM;
545
Javi Merinoe055bb02015-09-14 14:23:51 +0100546 if (!tz->tzp->sustainable_power)
547 dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n");
548
Javi Merino8b7b3902015-09-14 14:23:52 +0100549 get_governor_trips(tz, params);
550
551 if (tz->trips > 0) {
552 ret = tz->ops->get_trip_temp(tz,
553 params->trip_max_desired_temperature,
554 &control_temp);
555 if (!ret)
556 estimate_pid_constants(tz, tz->tzp->sustainable_power,
557 params->trip_switch_on,
558 control_temp, false);
Javi Merino6b775e82015-03-02 17:17:19 +0000559 }
560
Javi Merino6b775e82015-03-02 17:17:19 +0000561 reset_pid_controller(params);
562
563 tz->governor_data = params;
564
565 return 0;
Javi Merino6b775e82015-03-02 17:17:19 +0000566}
567
568static void power_allocator_unbind(struct thermal_zone_device *tz)
569{
570 dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
Dmitry Torokhovcf736ea2015-08-04 09:33:40 -0700571 kfree(tz->governor_data);
Javi Merino6b775e82015-03-02 17:17:19 +0000572 tz->governor_data = NULL;
573}
574
575static int power_allocator_throttle(struct thermal_zone_device *tz, int trip)
576{
577 int ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200578 int switch_on_temp, control_temp, current_temp;
Javi Merino6b775e82015-03-02 17:17:19 +0000579 struct power_allocator_params *params = tz->governor_data;
580
581 /*
582 * We get called for every trip point but we only need to do
583 * our calculations once
584 */
585 if (trip != params->trip_max_desired_temperature)
586 return 0;
587
588 ret = thermal_zone_get_temp(tz, &current_temp);
589 if (ret) {
590 dev_warn(&tz->device, "Failed to get temperature: %d\n", ret);
591 return ret;
592 }
593
594 ret = tz->ops->get_trip_temp(tz, params->trip_switch_on,
595 &switch_on_temp);
Javi Merino8b7b3902015-09-14 14:23:52 +0100596 if (!ret && (current_temp < switch_on_temp)) {
Javi Merino6b775e82015-03-02 17:17:19 +0000597 tz->passive = 0;
598 reset_pid_controller(params);
599 allow_maximum_power(tz);
600 return 0;
601 }
602
603 tz->passive = 1;
604
605 ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature,
606 &control_temp);
607 if (ret) {
608 dev_warn(&tz->device,
609 "Failed to get the maximum desired temperature: %d\n",
610 ret);
611 return ret;
612 }
613
614 return allocate_power(tz, current_temp, control_temp);
615}
616
617static struct thermal_governor thermal_gov_power_allocator = {
618 .name = "power_allocator",
619 .bind_to_tz = power_allocator_bind,
620 .unbind_from_tz = power_allocator_unbind,
621 .throttle = power_allocator_throttle,
622};
623
624int thermal_gov_power_allocator_register(void)
625{
626 return thermal_register_governor(&thermal_gov_power_allocator);
627}
628
629void thermal_gov_power_allocator_unregister(void)
630{
631 thermal_unregister_governor(&thermal_gov_power_allocator);
632}