diff mbox series

[1/2] thermal/core: Use clamp() helper in the stepwise governor

Message ID 20220629151012.3115773-1-daniel.lezcano@linaro.org
State Accepted
Commit 25bff3ed9a8aaee45c3b554cca58673f1bea1bdc
Headers show
Series [1/2] thermal/core: Use clamp() helper in the stepwise governor | expand

Commit Message

Daniel Lezcano June 29, 2022, 3:10 p.m. UTC
The code is actually clampling the next cooling device state using the
lowest and highest states of the thermal instance.

That code can be replaced by the clamp() macro which does exactly the
same. It results in a simpler routine to read.

Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
---
 drivers/thermal/gov_step_wise.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)

Comments

Daniel Lezcano July 6, 2022, 12:07 p.m. UTC | #1
On 29/06/2022 17:10, Daniel Lezcano wrote:
> The code is actually clampling the next cooling device state using the
> lowest and highest states of the thermal instance.
> 
> That code can be replaced by the clamp() macro which does exactly the
> same. It results in a simpler routine to read.
> 
> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
> ---

I'll apply this series if nobody has comments


>   drivers/thermal/gov_step_wise.c | 15 ++++-----------
>   1 file changed, 4 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/thermal/gov_step_wise.c b/drivers/thermal/gov_step_wise.c
> index 12acb12aac50..6efbfaf014da 100644
> --- a/drivers/thermal/gov_step_wise.c
> +++ b/drivers/thermal/gov_step_wise.c
> @@ -11,6 +11,7 @@
>    */
>   
>   #include <linux/thermal.h>
> +#include <linux/minmax.h>
>   #include <trace/events/thermal.h>
>   
>   #include "thermal_core.h"
> @@ -52,10 +53,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
>   
>   	if (!instance->initialized) {
>   		if (throttle) {
> -			next_target = (cur_state + 1) >= instance->upper ?
> -					instance->upper :
> -					((cur_state + 1) < instance->lower ?
> -					instance->lower : (cur_state + 1));
> +			next_target = clamp((cur_state + 1), instance->lower, instance->upper);
>   		} else {
>   			next_target = THERMAL_NO_TARGET;
>   		}
> @@ -66,10 +64,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
>   	switch (trend) {
>   	case THERMAL_TREND_RAISING:
>   		if (throttle) {
> -			next_target = cur_state < instance->upper ?
> -				    (cur_state + 1) : instance->upper;
> -			if (next_target < instance->lower)
> -				next_target = instance->lower;
> +			next_target = clamp((cur_state + 1), instance->lower, instance->upper);
>   		}
>   		break;
>   	case THERMAL_TREND_RAISE_FULL:
> @@ -82,9 +77,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
>   				next_target = THERMAL_NO_TARGET;
>   		} else {
>   			if (!throttle) {
> -				next_target = cur_state - 1;
> -				if (next_target > instance->upper)
> -					next_target = instance->upper;
> +				next_target = clamp((cur_state - 1), instance->lower, instance->upper);
>   			}
>   		}
>   		break;
Rafael J. Wysocki July 6, 2022, 12:26 p.m. UTC | #2
On Wed, Jul 6, 2022 at 2:07 PM Daniel Lezcano <daniel.lezcano@linaro.org> wrote:
>
> On 29/06/2022 17:10, Daniel Lezcano wrote:
> > The code is actually clampling the next cooling device state using the
> > lowest and highest states of the thermal instance.
> >
> > That code can be replaced by the clamp() macro which does exactly the
> > same. It results in a simpler routine to read.
> >
> > Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
> > ---
>
> I'll apply this series if nobody has comments

Sounds good to me.

Please feel free to add ACKs from me to the patches.

>
> >   drivers/thermal/gov_step_wise.c | 15 ++++-----------
> >   1 file changed, 4 insertions(+), 11 deletions(-)
> >
> > diff --git a/drivers/thermal/gov_step_wise.c b/drivers/thermal/gov_step_wise.c
> > index 12acb12aac50..6efbfaf014da 100644
> > --- a/drivers/thermal/gov_step_wise.c
> > +++ b/drivers/thermal/gov_step_wise.c
> > @@ -11,6 +11,7 @@
> >    */
> >
> >   #include <linux/thermal.h>
> > +#include <linux/minmax.h>
> >   #include <trace/events/thermal.h>
> >
> >   #include "thermal_core.h"
> > @@ -52,10 +53,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
> >
> >       if (!instance->initialized) {
> >               if (throttle) {
> > -                     next_target = (cur_state + 1) >= instance->upper ?
> > -                                     instance->upper :
> > -                                     ((cur_state + 1) < instance->lower ?
> > -                                     instance->lower : (cur_state + 1));
> > +                     next_target = clamp((cur_state + 1), instance->lower, instance->upper);
> >               } else {
> >                       next_target = THERMAL_NO_TARGET;
> >               }
> > @@ -66,10 +64,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
> >       switch (trend) {
> >       case THERMAL_TREND_RAISING:
> >               if (throttle) {
> > -                     next_target = cur_state < instance->upper ?
> > -                                 (cur_state + 1) : instance->upper;
> > -                     if (next_target < instance->lower)
> > -                             next_target = instance->lower;
> > +                     next_target = clamp((cur_state + 1), instance->lower, instance->upper);
> >               }
> >               break;
> >       case THERMAL_TREND_RAISE_FULL:
> > @@ -82,9 +77,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
> >                               next_target = THERMAL_NO_TARGET;
> >               } else {
> >                       if (!throttle) {
> > -                             next_target = cur_state - 1;
> > -                             if (next_target > instance->upper)
> > -                                     next_target = instance->upper;
> > +                             next_target = clamp((cur_state - 1), instance->lower, instance->upper);
> >                       }
> >               }
> >               break;
>
>
> --
> <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs
>
> Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
> <http://twitter.com/#!/linaroorg> Twitter |
> <http://www.linaro.org/linaro-blog/> Blog
Daniel Lezcano July 6, 2022, 2:44 p.m. UTC | #3
On 06/07/2022 14:26, Rafael J. Wysocki wrote:
> On Wed, Jul 6, 2022 at 2:07 PM Daniel Lezcano <daniel.lezcano@linaro.org> wrote:
>>
>> On 29/06/2022 17:10, Daniel Lezcano wrote:
>>> The code is actually clampling the next cooling device state using the
>>> lowest and highest states of the thermal instance.
>>>
>>> That code can be replaced by the clamp() macro which does exactly the
>>> same. It results in a simpler routine to read.
>>>
>>> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
>>> ---
>>
>> I'll apply this series if nobody has comments
> 
> Sounds good to me.
> 
> Please feel free to add ACKs from me to the patches.
> 

Sure, thanks!
diff mbox series

Patch

diff --git a/drivers/thermal/gov_step_wise.c b/drivers/thermal/gov_step_wise.c
index 12acb12aac50..6efbfaf014da 100644
--- a/drivers/thermal/gov_step_wise.c
+++ b/drivers/thermal/gov_step_wise.c
@@ -11,6 +11,7 @@ 
  */
 
 #include <linux/thermal.h>
+#include <linux/minmax.h>
 #include <trace/events/thermal.h>
 
 #include "thermal_core.h"
@@ -52,10 +53,7 @@  static unsigned long get_target_state(struct thermal_instance *instance,
 
 	if (!instance->initialized) {
 		if (throttle) {
-			next_target = (cur_state + 1) >= instance->upper ?
-					instance->upper :
-					((cur_state + 1) < instance->lower ?
-					instance->lower : (cur_state + 1));
+			next_target = clamp((cur_state + 1), instance->lower, instance->upper);
 		} else {
 			next_target = THERMAL_NO_TARGET;
 		}
@@ -66,10 +64,7 @@  static unsigned long get_target_state(struct thermal_instance *instance,
 	switch (trend) {
 	case THERMAL_TREND_RAISING:
 		if (throttle) {
-			next_target = cur_state < instance->upper ?
-				    (cur_state + 1) : instance->upper;
-			if (next_target < instance->lower)
-				next_target = instance->lower;
+			next_target = clamp((cur_state + 1), instance->lower, instance->upper);
 		}
 		break;
 	case THERMAL_TREND_RAISE_FULL:
@@ -82,9 +77,7 @@  static unsigned long get_target_state(struct thermal_instance *instance,
 				next_target = THERMAL_NO_TARGET;
 		} else {
 			if (!throttle) {
-				next_target = cur_state - 1;
-				if (next_target > instance->upper)
-					next_target = instance->upper;
+				next_target = clamp((cur_state - 1), instance->lower, instance->upper);
 			}
 		}
 		break;