1616
1717#include "thermal_core.h"
1818
19- #define INVALID_TRIP -1
20-
2119#define FRAC_BITS 10
2220#define int_to_frac (x ) ((x) << FRAC_BITS)
2321#define frac_to_int (x ) ((x) >> FRAC_BITS)
@@ -55,23 +53,23 @@ static inline s64 div_frac(s64 x, s64 y)
5553 * @err_integral: accumulated error in the PID controller.
5654 * @prev_err: error in the previous iteration of the PID controller.
5755 * Used to calculate the derivative term.
56+ * @sustainable_power: Sustainable power (heat) that this thermal zone can
57+ * dissipate
5858 * @trip_switch_on: first passive trip point of the thermal zone. The
5959 * governor switches on when this trip point is crossed.
6060 * If the thermal zone only has one passive trip point,
61- * @trip_switch_on should be INVALID_TRIP .
61+ * @trip_switch_on should be NULL .
6262 * @trip_max_desired_temperature: last passive trip point of the thermal
6363 * zone. The temperature we are
6464 * controlling for.
65- * @sustainable_power: Sustainable power (heat) that this thermal zone can
66- * dissipate
6765 */
6866struct power_allocator_params {
6967 bool allocated_tzp ;
7068 s64 err_integral ;
7169 s32 prev_err ;
72- int trip_switch_on ;
73- int trip_max_desired_temperature ;
7470 u32 sustainable_power ;
71+ const struct thermal_trip * trip_switch_on ;
72+ const struct thermal_trip * trip_max_desired_temperature ;
7573};
7674
7775/**
@@ -90,14 +88,12 @@ static u32 estimate_sustainable_power(struct thermal_zone_device *tz)
9088 u32 sustainable_power = 0 ;
9189 struct thermal_instance * instance ;
9290 struct power_allocator_params * params = tz -> governor_data ;
93- const struct thermal_trip * trip_max_desired_temperature =
94- & tz -> trips [params -> trip_max_desired_temperature ];
9591
9692 list_for_each_entry (instance , & tz -> thermal_instances , tz_node ) {
9793 struct thermal_cooling_device * cdev = instance -> cdev ;
9894 u32 min_power ;
9995
100- if (instance -> trip != trip_max_desired_temperature )
96+ if (instance -> trip != params -> trip_max_desired_temperature )
10197 continue ;
10298
10399 if (!cdev_is_power_actor (cdev ))
@@ -116,24 +112,22 @@ static u32 estimate_sustainable_power(struct thermal_zone_device *tz)
116112 * estimate_pid_constants() - Estimate the constants for the PID controller
117113 * @tz: thermal zone for which to estimate the constants
118114 * @sustainable_power: sustainable power for the thermal zone
119- * @trip_switch_on: trip point number for the switch on temperature
115+ * @trip_switch_on: trip point for the switch on temperature
120116 * @control_temp: target temperature for the power allocator governor
121117 *
122118 * This function is used to update the estimation of the PID
123119 * controller constants in struct thermal_zone_parameters.
124120 */
125121static void estimate_pid_constants (struct thermal_zone_device * tz ,
126- u32 sustainable_power , int trip_switch_on ,
122+ u32 sustainable_power ,
123+ const struct thermal_trip * trip_switch_on ,
127124 int control_temp )
128125{
129- struct thermal_trip trip ;
130126 u32 temperature_threshold = control_temp ;
131- int ret ;
132127 s32 k_i ;
133128
134- ret = __thermal_zone_get_trip (tz , trip_switch_on , & trip );
135- if (!ret )
136- temperature_threshold -= trip .temperature ;
129+ if (trip_switch_on )
130+ temperature_threshold -= trip_switch_on -> temperature ;
137131
138132 /*
139133 * estimate_pid_constants() tries to find appropriate default
@@ -386,7 +380,7 @@ static int allocate_power(struct thermal_zone_device *tz,
386380 struct thermal_instance * instance ;
387381 struct power_allocator_params * params = tz -> governor_data ;
388382 const struct thermal_trip * trip_max_desired_temperature =
389- & tz -> trips [ params -> trip_max_desired_temperature ] ;
383+ params -> trip_max_desired_temperature ;
390384 u32 * req_power , * max_power , * granted_power , * extra_actor_power ;
391385 u32 * weighted_req_power ;
392386 u32 total_req_power , max_allocatable_power , total_weighted_req_power ;
@@ -496,7 +490,7 @@ static int allocate_power(struct thermal_zone_device *tz,
496490}
497491
498492/**
499- * get_governor_trips() - get the number of the two trip points that are key for this governor
493+ * get_governor_trips() - get the two trip points that are key for this governor
500494 * @tz: thermal zone to operate on
501495 * @params: pointer to private data for this governor
502496 *
@@ -513,46 +507,36 @@ static int allocate_power(struct thermal_zone_device *tz,
513507static void get_governor_trips (struct thermal_zone_device * tz ,
514508 struct power_allocator_params * params )
515509{
516- int i , last_active , last_passive ;
517- bool found_first_passive ;
518-
519- found_first_passive = false;
520- last_active = INVALID_TRIP ;
521- last_passive = INVALID_TRIP ;
522-
523- for (i = 0 ; i < tz -> num_trips ; i ++ ) {
524- struct thermal_trip trip ;
525- int ret ;
526-
527- ret = __thermal_zone_get_trip (tz , i , & trip );
528- if (ret ) {
529- dev_warn (& tz -> device ,
530- "Failed to get trip point %d type: %d\n" , i ,
531- ret );
532- continue ;
533- }
534-
535- if (trip .type == THERMAL_TRIP_PASSIVE ) {
536- if (!found_first_passive ) {
537- params -> trip_switch_on = i ;
538- found_first_passive = true;
539- } else {
540- last_passive = i ;
510+ const struct thermal_trip * first_passive = NULL ;
511+ const struct thermal_trip * last_passive = NULL ;
512+ const struct thermal_trip * last_active = NULL ;
513+ const struct thermal_trip * trip ;
514+
515+ for_each_trip (tz , trip ) {
516+ switch (trip -> type ) {
517+ case THERMAL_TRIP_PASSIVE :
518+ if (!first_passive ) {
519+ first_passive = trip ;
520+ break ;
541521 }
542- } else if (trip .type == THERMAL_TRIP_ACTIVE ) {
543- last_active = i ;
544- } else {
522+ last_passive = trip ;
523+ break ;
524+ case THERMAL_TRIP_ACTIVE :
525+ last_active = trip ;
526+ break ;
527+ default :
545528 break ;
546529 }
547530 }
548531
549- if (last_passive != INVALID_TRIP ) {
532+ if (last_passive ) {
533+ params -> trip_switch_on = first_passive ;
550534 params -> trip_max_desired_temperature = last_passive ;
551- } else if (found_first_passive ) {
552- params -> trip_max_desired_temperature = params -> trip_switch_on ;
553- params -> trip_switch_on = INVALID_TRIP ;
535+ } else if (first_passive ) {
536+ params -> trip_switch_on = NULL ;
537+ params -> trip_max_desired_temperature = first_passive ;
554538 } else {
555- params -> trip_switch_on = INVALID_TRIP ;
539+ params -> trip_switch_on = NULL ;
556540 params -> trip_max_desired_temperature = last_active ;
557541 }
558542}
@@ -567,14 +551,12 @@ static void allow_maximum_power(struct thermal_zone_device *tz, bool update)
567551{
568552 struct thermal_instance * instance ;
569553 struct power_allocator_params * params = tz -> governor_data ;
570- const struct thermal_trip * trip_max_desired_temperature =
571- & tz -> trips [params -> trip_max_desired_temperature ];
572554 u32 req_power ;
573555
574556 list_for_each_entry (instance , & tz -> thermal_instances , tz_node ) {
575557 struct thermal_cooling_device * cdev = instance -> cdev ;
576558
577- if (( instance -> trip != trip_max_desired_temperature ) ||
559+ if (instance -> trip != params -> trip_max_desired_temperature ||
578560 (!cdev_is_power_actor (instance -> cdev )))
579561 continue ;
580562
@@ -636,7 +618,6 @@ static int power_allocator_bind(struct thermal_zone_device *tz)
636618{
637619 int ret ;
638620 struct power_allocator_params * params ;
639- struct thermal_trip trip ;
640621
641622 ret = check_power_actors (tz );
642623 if (ret )
@@ -661,13 +642,11 @@ static int power_allocator_bind(struct thermal_zone_device *tz)
661642
662643 get_governor_trips (tz , params );
663644
664- if (tz -> num_trips > 0 ) {
665- ret = __thermal_zone_get_trip (tz , params -> trip_max_desired_temperature ,
666- & trip );
667- if (!ret )
668- estimate_pid_constants (tz , tz -> tzp -> sustainable_power ,
669- params -> trip_switch_on ,
670- trip .temperature );
645+ if (params -> trip_max_desired_temperature ) {
646+ int temp = params -> trip_max_desired_temperature -> temperature ;
647+
648+ estimate_pid_constants (tz , tz -> tzp -> sustainable_power ,
649+ params -> trip_switch_on , temp );
671650 }
672651
673652 reset_pid_controller (params );
@@ -697,11 +676,10 @@ static void power_allocator_unbind(struct thermal_zone_device *tz)
697676 tz -> governor_data = NULL ;
698677}
699678
700- static int power_allocator_throttle (struct thermal_zone_device * tz , int trip_id )
679+ static int power_allocator_throttle (struct thermal_zone_device * tz ,
680+ const struct thermal_trip * trip )
701681{
702682 struct power_allocator_params * params = tz -> governor_data ;
703- struct thermal_trip trip ;
704- int ret ;
705683 bool update ;
706684
707685 lockdep_assert_held (& tz -> lock );
@@ -710,12 +688,12 @@ static int power_allocator_throttle(struct thermal_zone_device *tz, int trip_id)
710688 * We get called for every trip point but we only need to do
711689 * our calculations once
712690 */
713- if (trip_id != params -> trip_max_desired_temperature )
691+ if (trip != params -> trip_max_desired_temperature )
714692 return 0 ;
715693
716- ret = __thermal_zone_get_trip ( tz , params -> trip_switch_on , & trip ) ;
717- if (! ret && ( tz -> temperature < trip . temperature ) ) {
718- update = ( tz -> last_temperature >= trip . temperature ) ;
694+ trip = params -> trip_switch_on ;
695+ if (trip && tz -> temperature < trip -> temperature ) {
696+ update = tz -> last_temperature >= trip -> temperature ;
719697 tz -> passive = 0 ;
720698 reset_pid_controller (params );
721699 allow_maximum_power (tz , update );
@@ -724,14 +702,7 @@ static int power_allocator_throttle(struct thermal_zone_device *tz, int trip_id)
724702
725703 tz -> passive = 1 ;
726704
727- ret = __thermal_zone_get_trip (tz , params -> trip_max_desired_temperature , & trip );
728- if (ret ) {
729- dev_warn (& tz -> device , "Failed to get the maximum desired temperature: %d\n" ,
730- ret );
731- return ret ;
732- }
733-
734- return allocate_power (tz , trip .temperature );
705+ return allocate_power (tz , params -> trip_max_desired_temperature -> temperature );
735706}
736707
737708static struct thermal_governor thermal_gov_power_allocator = {
0 commit comments