blob: 718363f5be400aac71ce5f119ebd29834ac1c774 [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
Javi Merinof5cbb182015-09-14 14:23:53 +010061 * @allocated_tzp: whether we have allocated tzp for this thermal zone and
62 * it needs to be freed on unbind
Javi Merino6b775e82015-03-02 17:17:19 +000063 * @err_integral: accumulated error in the PID controller.
64 * @prev_err: error in the previous iteration of the PID controller.
65 * Used to calculate the derivative term.
66 * @trip_switch_on: first passive trip point of the thermal zone. The
67 * governor switches on when this trip point is crossed.
Javi Merino8b7b3902015-09-14 14:23:52 +010068 * If the thermal zone only has one passive trip point,
69 * @trip_switch_on should be INVALID_TRIP.
Javi Merino6b775e82015-03-02 17:17:19 +000070 * @trip_max_desired_temperature: last passive trip point of the thermal
71 * zone. The temperature we are
72 * controlling for.
73 */
74struct power_allocator_params {
Javi Merinof5cbb182015-09-14 14:23:53 +010075 bool allocated_tzp;
Javi Merino6b775e82015-03-02 17:17:19 +000076 s64 err_integral;
77 s32 prev_err;
78 int trip_switch_on;
79 int trip_max_desired_temperature;
80};
81
82/**
Javi Merinoe055bb02015-09-14 14:23:51 +010083 * estimate_sustainable_power() - Estimate the sustainable power of a thermal zone
84 * @tz: thermal zone we are operating in
85 *
86 * For thermal zones that don't provide a sustainable_power in their
87 * thermal_zone_params, estimate one. Calculate it using the minimum
88 * power of all the cooling devices as that gives a valid value that
89 * can give some degree of functionality. For optimal performance of
90 * this governor, provide a sustainable_power in the thermal zone's
91 * thermal_zone_params.
92 */
93static u32 estimate_sustainable_power(struct thermal_zone_device *tz)
94{
95 u32 sustainable_power = 0;
96 struct thermal_instance *instance;
97 struct power_allocator_params *params = tz->governor_data;
98
99 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
100 struct thermal_cooling_device *cdev = instance->cdev;
101 u32 min_power;
102
103 if (instance->trip != params->trip_max_desired_temperature)
104 continue;
105
106 if (power_actor_get_min_power(cdev, tz, &min_power))
107 continue;
108
109 sustainable_power += min_power;
110 }
111
112 return sustainable_power;
113}
114
115/**
116 * estimate_pid_constants() - Estimate the constants for the PID controller
117 * @tz: thermal zone for which to estimate the constants
118 * @sustainable_power: sustainable power for the thermal zone
119 * @trip_switch_on: trip point number for the switch on temperature
120 * @control_temp: target temperature for the power allocator governor
121 * @force: whether to force the update of the constants
122 *
123 * This function is used to update the estimation of the PID
124 * controller constants in struct thermal_zone_parameters.
125 * Sustainable power is provided in case it was estimated. The
126 * estimated sustainable_power should not be stored in the
127 * thermal_zone_parameters so it has to be passed explicitly to this
128 * function.
129 *
130 * If @force is not set, the values in the thermal zone's parameters
131 * are preserved if they are not zero. If @force is set, the values
132 * in thermal zone's parameters are overwritten.
133 */
134static void estimate_pid_constants(struct thermal_zone_device *tz,
135 u32 sustainable_power, int trip_switch_on,
136 int control_temp, bool force)
137{
138 int ret;
139 int switch_on_temp;
140 u32 temperature_threshold;
141
142 ret = tz->ops->get_trip_temp(tz, trip_switch_on, &switch_on_temp);
143 if (ret)
144 switch_on_temp = 0;
145
146 temperature_threshold = control_temp - switch_on_temp;
147
148 if (!tz->tzp->k_po || force)
149 tz->tzp->k_po = int_to_frac(sustainable_power) /
150 temperature_threshold;
151
152 if (!tz->tzp->k_pu || force)
153 tz->tzp->k_pu = int_to_frac(2 * sustainable_power) /
154 temperature_threshold;
155
156 if (!tz->tzp->k_i || force)
157 tz->tzp->k_i = int_to_frac(10) / 1000;
158 /*
159 * The default for k_d and integral_cutoff is 0, so we can
160 * leave them as they are.
161 */
162}
163
164/**
Javi Merino6b775e82015-03-02 17:17:19 +0000165 * pid_controller() - PID controller
166 * @tz: thermal zone we are operating in
167 * @current_temp: the current temperature in millicelsius
168 * @control_temp: the target temperature in millicelsius
169 * @max_allocatable_power: maximum allocatable power for this thermal zone
170 *
171 * This PID controller increases the available power budget so that the
172 * temperature of the thermal zone gets as close as possible to
173 * @control_temp and limits the power if it exceeds it. k_po is the
174 * proportional term when we are overshooting, k_pu is the
175 * proportional term when we are undershooting. integral_cutoff is a
176 * threshold below which we stop accumulating the error. The
177 * accumulated error is only valid if the requested power will make
178 * the system warmer. If the system is mostly idle, there's no point
179 * in accumulating positive error.
180 *
181 * Return: The power budget for the next period.
182 */
183static u32 pid_controller(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200184 int current_temp,
185 int control_temp,
Javi Merino6b775e82015-03-02 17:17:19 +0000186 u32 max_allocatable_power)
187{
188 s64 p, i, d, power_range;
189 s32 err, max_power_frac;
Javi Merinoe055bb02015-09-14 14:23:51 +0100190 u32 sustainable_power;
Javi Merino6b775e82015-03-02 17:17:19 +0000191 struct power_allocator_params *params = tz->governor_data;
192
193 max_power_frac = int_to_frac(max_allocatable_power);
194
Javi Merinoe055bb02015-09-14 14:23:51 +0100195 if (tz->tzp->sustainable_power) {
196 sustainable_power = tz->tzp->sustainable_power;
197 } else {
198 sustainable_power = estimate_sustainable_power(tz);
199 estimate_pid_constants(tz, sustainable_power,
200 params->trip_switch_on, control_temp,
201 true);
202 }
203
Sascha Hauer17e83512015-07-24 08:12:54 +0200204 err = control_temp - current_temp;
Javi Merino6b775e82015-03-02 17:17:19 +0000205 err = int_to_frac(err);
206
207 /* Calculate the proportional term */
208 p = mul_frac(err < 0 ? tz->tzp->k_po : tz->tzp->k_pu, err);
209
210 /*
211 * Calculate the integral term
212 *
213 * if the error is less than cut off allow integration (but
214 * the integral is limited to max power)
215 */
216 i = mul_frac(tz->tzp->k_i, params->err_integral);
217
218 if (err < int_to_frac(tz->tzp->integral_cutoff)) {
219 s64 i_next = i + mul_frac(tz->tzp->k_i, err);
220
221 if (abs64(i_next) < max_power_frac) {
222 i = i_next;
223 params->err_integral += err;
224 }
225 }
226
227 /*
228 * Calculate the derivative term
229 *
230 * We do err - prev_err, so with a positive k_d, a decreasing
231 * error (i.e. driving closer to the line) results in less
232 * power being applied, slowing down the controller)
233 */
234 d = mul_frac(tz->tzp->k_d, err - params->prev_err);
235 d = div_frac(d, tz->passive_delay);
236 params->prev_err = err;
237
238 power_range = p + i + d;
239
240 /* feed-forward the known sustainable dissipatable power */
Javi Merinoe055bb02015-09-14 14:23:51 +0100241 power_range = sustainable_power + frac_to_int(power_range);
Javi Merino6b775e82015-03-02 17:17:19 +0000242
Javi Merino6828a472015-03-02 17:17:20 +0000243 power_range = clamp(power_range, (s64)0, (s64)max_allocatable_power);
244
245 trace_thermal_power_allocator_pid(tz, frac_to_int(err),
246 frac_to_int(params->err_integral),
247 frac_to_int(p), frac_to_int(i),
248 frac_to_int(d), power_range);
249
250 return power_range;
Javi Merino6b775e82015-03-02 17:17:19 +0000251}
252
253/**
254 * divvy_up_power() - divvy the allocated power between the actors
255 * @req_power: each actor's requested power
256 * @max_power: each actor's maximum available power
257 * @num_actors: size of the @req_power, @max_power and @granted_power's array
258 * @total_req_power: sum of @req_power
259 * @power_range: total allocated power
260 * @granted_power: output array: each actor's granted power
261 * @extra_actor_power: an appropriately sized array to be used in the
262 * function as temporary storage of the extra power given
263 * to the actors
264 *
265 * This function divides the total allocated power (@power_range)
266 * fairly between the actors. It first tries to give each actor a
267 * share of the @power_range according to how much power it requested
268 * compared to the rest of the actors. For example, if only one actor
269 * requests power, then it receives all the @power_range. If
270 * three actors each requests 1mW, each receives a third of the
271 * @power_range.
272 *
273 * If any actor received more than their maximum power, then that
274 * surplus is re-divvied among the actors based on how far they are
275 * from their respective maximums.
276 *
277 * Granted power for each actor is written to @granted_power, which
278 * should've been allocated by the calling function.
279 */
280static void divvy_up_power(u32 *req_power, u32 *max_power, int num_actors,
281 u32 total_req_power, u32 power_range,
282 u32 *granted_power, u32 *extra_actor_power)
283{
284 u32 extra_power, capped_extra_power;
285 int i;
286
287 /*
288 * Prevent division by 0 if none of the actors request power.
289 */
290 if (!total_req_power)
291 total_req_power = 1;
292
293 capped_extra_power = 0;
294 extra_power = 0;
295 for (i = 0; i < num_actors; i++) {
296 u64 req_range = req_power[i] * power_range;
297
Javi Merinoea54cac2015-05-02 10:15:24 +0100298 granted_power[i] = DIV_ROUND_CLOSEST_ULL(req_range,
299 total_req_power);
Javi Merino6b775e82015-03-02 17:17:19 +0000300
301 if (granted_power[i] > max_power[i]) {
302 extra_power += granted_power[i] - max_power[i];
303 granted_power[i] = max_power[i];
304 }
305
306 extra_actor_power[i] = max_power[i] - granted_power[i];
307 capped_extra_power += extra_actor_power[i];
308 }
309
310 if (!extra_power)
311 return;
312
313 /*
314 * Re-divvy the reclaimed extra among actors based on
315 * how far they are from the max
316 */
317 extra_power = min(extra_power, capped_extra_power);
318 if (capped_extra_power > 0)
319 for (i = 0; i < num_actors; i++)
320 granted_power[i] += (extra_actor_power[i] *
321 extra_power) / capped_extra_power;
322}
323
324static int allocate_power(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200325 int current_temp,
326 int control_temp)
Javi Merino6b775e82015-03-02 17:17:19 +0000327{
328 struct thermal_instance *instance;
329 struct power_allocator_params *params = tz->governor_data;
330 u32 *req_power, *max_power, *granted_power, *extra_actor_power;
Javi Merinod5f83102015-07-03 10:24:33 +0100331 u32 *weighted_req_power;
332 u32 total_req_power, max_allocatable_power, total_weighted_req_power;
Javi Merino6828a472015-03-02 17:17:20 +0000333 u32 total_granted_power, power_range;
Javi Merino6b775e82015-03-02 17:17:19 +0000334 int i, num_actors, total_weight, ret = 0;
335 int trip_max_desired_temperature = params->trip_max_desired_temperature;
336
337 mutex_lock(&tz->lock);
338
339 num_actors = 0;
340 total_weight = 0;
341 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
342 if ((instance->trip == trip_max_desired_temperature) &&
343 cdev_is_power_actor(instance->cdev)) {
344 num_actors++;
345 total_weight += instance->weight;
346 }
347 }
348
349 /*
Javi Merinod5f83102015-07-03 10:24:33 +0100350 * We need to allocate five arrays of the same size:
351 * req_power, max_power, granted_power, extra_actor_power and
352 * weighted_req_power. They are going to be needed until this
353 * function returns. Allocate them all in one go to simplify
354 * the allocation and deallocation logic.
Javi Merino6b775e82015-03-02 17:17:19 +0000355 */
356 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*max_power));
357 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*granted_power));
358 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power));
Javi Merinod5f83102015-07-03 10:24:33 +0100359 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*weighted_req_power));
Javi Merino9751a9e42015-08-25 19:22:35 +0100360 req_power = kcalloc(num_actors * 5, sizeof(*req_power), GFP_KERNEL);
Javi Merino6b775e82015-03-02 17:17:19 +0000361 if (!req_power) {
362 ret = -ENOMEM;
363 goto unlock;
364 }
365
366 max_power = &req_power[num_actors];
367 granted_power = &req_power[2 * num_actors];
368 extra_actor_power = &req_power[3 * num_actors];
Javi Merinod5f83102015-07-03 10:24:33 +0100369 weighted_req_power = &req_power[4 * num_actors];
Javi Merino6b775e82015-03-02 17:17:19 +0000370
371 i = 0;
Javi Merinod5f83102015-07-03 10:24:33 +0100372 total_weighted_req_power = 0;
Javi Merino6b775e82015-03-02 17:17:19 +0000373 total_req_power = 0;
374 max_allocatable_power = 0;
375
376 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
377 int weight;
378 struct thermal_cooling_device *cdev = instance->cdev;
379
380 if (instance->trip != trip_max_desired_temperature)
381 continue;
382
383 if (!cdev_is_power_actor(cdev))
384 continue;
385
386 if (cdev->ops->get_requested_power(cdev, tz, &req_power[i]))
387 continue;
388
389 if (!total_weight)
390 weight = 1 << FRAC_BITS;
391 else
392 weight = instance->weight;
393
Javi Merinod5f83102015-07-03 10:24:33 +0100394 weighted_req_power[i] = frac_to_int(weight * req_power[i]);
Javi Merino6b775e82015-03-02 17:17:19 +0000395
396 if (power_actor_get_max_power(cdev, tz, &max_power[i]))
397 continue;
398
399 total_req_power += req_power[i];
400 max_allocatable_power += max_power[i];
Javi Merinod5f83102015-07-03 10:24:33 +0100401 total_weighted_req_power += weighted_req_power[i];
Javi Merino6b775e82015-03-02 17:17:19 +0000402
403 i++;
404 }
405
406 power_range = pid_controller(tz, current_temp, control_temp,
407 max_allocatable_power);
408
Javi Merinod5f83102015-07-03 10:24:33 +0100409 divvy_up_power(weighted_req_power, max_power, num_actors,
410 total_weighted_req_power, power_range, granted_power,
411 extra_actor_power);
Javi Merino6b775e82015-03-02 17:17:19 +0000412
Javi Merino6828a472015-03-02 17:17:20 +0000413 total_granted_power = 0;
Javi Merino6b775e82015-03-02 17:17:19 +0000414 i = 0;
415 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
416 if (instance->trip != trip_max_desired_temperature)
417 continue;
418
419 if (!cdev_is_power_actor(instance->cdev))
420 continue;
421
422 power_actor_set_power(instance->cdev, instance,
423 granted_power[i]);
Javi Merino6828a472015-03-02 17:17:20 +0000424 total_granted_power += granted_power[i];
Javi Merino6b775e82015-03-02 17:17:19 +0000425
426 i++;
427 }
428
Javi Merino6828a472015-03-02 17:17:20 +0000429 trace_thermal_power_allocator(tz, req_power, total_req_power,
430 granted_power, total_granted_power,
431 num_actors, power_range,
432 max_allocatable_power, current_temp,
Sascha Hauer17e83512015-07-24 08:12:54 +0200433 control_temp - current_temp);
Javi Merino6828a472015-03-02 17:17:20 +0000434
Dmitry Torokhovcf736ea2015-08-04 09:33:40 -0700435 kfree(req_power);
Javi Merino6b775e82015-03-02 17:17:19 +0000436unlock:
437 mutex_unlock(&tz->lock);
438
439 return ret;
440}
441
Javi Merino8b7b3902015-09-14 14:23:52 +0100442/**
443 * get_governor_trips() - get the number of the two trip points that are key for this governor
444 * @tz: thermal zone to operate on
445 * @params: pointer to private data for this governor
446 *
447 * The power allocator governor works optimally with two trips points:
448 * a "switch on" trip point and a "maximum desired temperature". These
449 * are defined as the first and last passive trip points.
450 *
451 * If there is only one trip point, then that's considered to be the
452 * "maximum desired temperature" trip point and the governor is always
453 * on. If there are no passive or active trip points, then the
454 * governor won't do anything. In fact, its throttle function
455 * won't be called at all.
456 */
457static void get_governor_trips(struct thermal_zone_device *tz,
458 struct power_allocator_params *params)
Javi Merino6b775e82015-03-02 17:17:19 +0000459{
Javi Merino8b7b3902015-09-14 14:23:52 +0100460 int i, last_active, last_passive;
Javi Merino6b775e82015-03-02 17:17:19 +0000461 bool found_first_passive;
462
463 found_first_passive = false;
Javi Merino8b7b3902015-09-14 14:23:52 +0100464 last_active = INVALID_TRIP;
465 last_passive = INVALID_TRIP;
Javi Merino6b775e82015-03-02 17:17:19 +0000466
467 for (i = 0; i < tz->trips; i++) {
468 enum thermal_trip_type type;
Javi Merino8b7b3902015-09-14 14:23:52 +0100469 int ret;
Javi Merino6b775e82015-03-02 17:17:19 +0000470
471 ret = tz->ops->get_trip_type(tz, i, &type);
Javi Merino8b7b3902015-09-14 14:23:52 +0100472 if (ret) {
473 dev_warn(&tz->device,
474 "Failed to get trip point %d type: %d\n", i,
475 ret);
476 continue;
477 }
Javi Merino6b775e82015-03-02 17:17:19 +0000478
Javi Merino8b7b3902015-09-14 14:23:52 +0100479 if (type == THERMAL_TRIP_PASSIVE) {
480 if (!found_first_passive) {
Javi Merino6b775e82015-03-02 17:17:19 +0000481 params->trip_switch_on = i;
482 found_first_passive = true;
Javi Merino8b7b3902015-09-14 14:23:52 +0100483 } else {
484 last_passive = i;
Javi Merino6b775e82015-03-02 17:17:19 +0000485 }
Javi Merino8b7b3902015-09-14 14:23:52 +0100486 } else if (type == THERMAL_TRIP_ACTIVE) {
487 last_active = i;
Javi Merino6b775e82015-03-02 17:17:19 +0000488 } else {
489 break;
490 }
491 }
492
Javi Merino8b7b3902015-09-14 14:23:52 +0100493 if (last_passive != INVALID_TRIP) {
Javi Merino6b775e82015-03-02 17:17:19 +0000494 params->trip_max_desired_temperature = last_passive;
Javi Merino8b7b3902015-09-14 14:23:52 +0100495 } else if (found_first_passive) {
496 params->trip_max_desired_temperature = params->trip_switch_on;
497 params->trip_switch_on = INVALID_TRIP;
Javi Merino6b775e82015-03-02 17:17:19 +0000498 } else {
Javi Merino8b7b3902015-09-14 14:23:52 +0100499 params->trip_switch_on = INVALID_TRIP;
500 params->trip_max_desired_temperature = last_active;
Javi Merino6b775e82015-03-02 17:17:19 +0000501 }
Javi Merino6b775e82015-03-02 17:17:19 +0000502}
503
504static void reset_pid_controller(struct power_allocator_params *params)
505{
506 params->err_integral = 0;
507 params->prev_err = 0;
508}
509
510static void allow_maximum_power(struct thermal_zone_device *tz)
511{
512 struct thermal_instance *instance;
513 struct power_allocator_params *params = tz->governor_data;
514
515 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
516 if ((instance->trip != params->trip_max_desired_temperature) ||
517 (!cdev_is_power_actor(instance->cdev)))
518 continue;
519
520 instance->target = 0;
521 instance->cdev->updated = false;
522 thermal_cdev_update(instance->cdev);
523 }
524}
525
526/**
527 * power_allocator_bind() - bind the power_allocator governor to a thermal zone
528 * @tz: thermal zone to bind it to
529 *
Javi Merino8b7b3902015-09-14 14:23:52 +0100530 * Initialize the PID controller parameters and bind it to the thermal
531 * zone.
Javi Merino6b775e82015-03-02 17:17:19 +0000532 *
Javi Merinof5cbb182015-09-14 14:23:53 +0100533 * Return: 0 on success, or -ENOMEM if we ran out of memory.
Javi Merino6b775e82015-03-02 17:17:19 +0000534 */
535static int power_allocator_bind(struct thermal_zone_device *tz)
536{
537 int ret;
538 struct power_allocator_params *params;
Javi Merinoe055bb02015-09-14 14:23:51 +0100539 int control_temp;
Javi Merino6b775e82015-03-02 17:17:19 +0000540
Dmitry Torokhovcf736ea2015-08-04 09:33:40 -0700541 params = kzalloc(sizeof(*params), GFP_KERNEL);
Javi Merino6b775e82015-03-02 17:17:19 +0000542 if (!params)
543 return -ENOMEM;
544
Javi Merinof5cbb182015-09-14 14:23:53 +0100545 if (!tz->tzp) {
546 tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL);
547 if (!tz->tzp) {
548 ret = -ENOMEM;
549 goto free_params;
550 }
551
552 params->allocated_tzp = true;
553 }
554
Javi Merinoe055bb02015-09-14 14:23:51 +0100555 if (!tz->tzp->sustainable_power)
556 dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n");
557
Javi Merino8b7b3902015-09-14 14:23:52 +0100558 get_governor_trips(tz, params);
559
560 if (tz->trips > 0) {
561 ret = tz->ops->get_trip_temp(tz,
562 params->trip_max_desired_temperature,
563 &control_temp);
564 if (!ret)
565 estimate_pid_constants(tz, tz->tzp->sustainable_power,
566 params->trip_switch_on,
567 control_temp, false);
Javi Merino6b775e82015-03-02 17:17:19 +0000568 }
569
Javi Merino6b775e82015-03-02 17:17:19 +0000570 reset_pid_controller(params);
571
572 tz->governor_data = params;
573
574 return 0;
Javi Merinof5cbb182015-09-14 14:23:53 +0100575
576free_params:
577 kfree(params);
578
579 return ret;
Javi Merino6b775e82015-03-02 17:17:19 +0000580}
581
582static void power_allocator_unbind(struct thermal_zone_device *tz)
583{
Javi Merinof5cbb182015-09-14 14:23:53 +0100584 struct power_allocator_params *params = tz->governor_data;
585
Javi Merino6b775e82015-03-02 17:17:19 +0000586 dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
Javi Merinof5cbb182015-09-14 14:23:53 +0100587
588 if (params->allocated_tzp) {
589 kfree(tz->tzp);
590 tz->tzp = NULL;
591 }
592
Dmitry Torokhovcf736ea2015-08-04 09:33:40 -0700593 kfree(tz->governor_data);
Javi Merino6b775e82015-03-02 17:17:19 +0000594 tz->governor_data = NULL;
595}
596
597static int power_allocator_throttle(struct thermal_zone_device *tz, int trip)
598{
599 int ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200600 int switch_on_temp, control_temp, current_temp;
Javi Merino6b775e82015-03-02 17:17:19 +0000601 struct power_allocator_params *params = tz->governor_data;
602
603 /*
604 * We get called for every trip point but we only need to do
605 * our calculations once
606 */
607 if (trip != params->trip_max_desired_temperature)
608 return 0;
609
610 ret = thermal_zone_get_temp(tz, &current_temp);
611 if (ret) {
612 dev_warn(&tz->device, "Failed to get temperature: %d\n", ret);
613 return ret;
614 }
615
616 ret = tz->ops->get_trip_temp(tz, params->trip_switch_on,
617 &switch_on_temp);
Javi Merino8b7b3902015-09-14 14:23:52 +0100618 if (!ret && (current_temp < switch_on_temp)) {
Javi Merino6b775e82015-03-02 17:17:19 +0000619 tz->passive = 0;
620 reset_pid_controller(params);
621 allow_maximum_power(tz);
622 return 0;
623 }
624
625 tz->passive = 1;
626
627 ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature,
628 &control_temp);
629 if (ret) {
630 dev_warn(&tz->device,
631 "Failed to get the maximum desired temperature: %d\n",
632 ret);
633 return ret;
634 }
635
636 return allocate_power(tz, current_temp, control_temp);
637}
638
639static struct thermal_governor thermal_gov_power_allocator = {
640 .name = "power_allocator",
641 .bind_to_tz = power_allocator_bind,
642 .unbind_from_tz = power_allocator_unbind,
643 .throttle = power_allocator_throttle,
644};
645
646int thermal_gov_power_allocator_register(void)
647{
648 return thermal_register_governor(&thermal_gov_power_allocator);
649}
650
651void thermal_gov_power_allocator_unregister(void)
652{
653 thermal_unregister_governor(&thermal_gov_power_allocator);
654}