*/
 
 #include <linux/thermal.h>
+#include <linux/minmax.h>
 #include <trace/events/thermal.h>
 
 #include "thermal_core.h"
 
        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;
                }
        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:
                                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;