<div dir="ltr">Hi all, <div>I am trying to modify bang_bang thermal governor</div><div>The source code I have taken from here  <a href="https://android.googlesource.com/kernel/common/+/15753588bcd4/drivers/thermal/gov_bang_bang.c">https://android.googlesource.com/kernel/common/+/15753588bcd4/drivers/thermal/gov_bang_bang.c</a></div><div><br></div><div>bang bang governor will switch fan on when temperature reaches "trip" temperature and turn the fan off when temperature falls below "trip - hysteresis temperature". These Properties are set in the device file.</div><div><br></div><div>I am modifying it to operate in three modes, ON, 50% and 100% fan speed depending on the temperature. At 100% fan is noisy, so 50% offers a good compromise for a slightly hotter system but when the system is too hot a 100% fan should be acceptable.</div><div><br></div><div>Here are the device entries</div><div>"trips" are set for 70-65 and 65-70 degrees celsius. and cooling maps use fan with state 0, 1 and 2 which are OFF, 50% and 100%</div><div><br></div><div>thermal-zones {                              <br>        cpu-thermal {                           <br>            trips {                          <br>                cpu_trip_low: active-low {            <br>                    temperature = <65000>;          <br>                    hysteresis = <5000>;           <br>                    type = "active";             <br>                };                        <br>                                         <br>                cpu_trip_high: active-high {           <br>                    temperature = <70000>;          <br>                    hysteresis = <5000>;           <br>                    type = "active";             <br>                };                        <br>                               <br>            };                            <br>                                         <br>            cooling-maps {                      <br>                map1 {                      <br>                    cooling-device = <&fan 0 1>;       <br>                    trip = <&cpu_trip_low>;          <br>                };                        <br>                                         <br>                map2 {                      <br>                    cooling-device = <&fan 1 2>;       <br>                    trip = <&cpu_trip_high>;         <br>                };                        <br>                                         <br>            };                            <br>        };                                <br>    };        <br></div><div><br></div><div><br></div><div><br></div><div>The modified algorithm is</div><div><br></div><div>--- drivers/thermal/gov_bang_bang.c 2024-11-02 05:54:17.385093959 +0000<br>+++ drivers/thermal/gov_bang_bang.c.orig 2024-11-02 06:09:31.108870820 +0000<br>@@ -13,6 +13,10 @@<br> <br> #include "thermal_core.h"<br> <br>+#define FAN_OFF 0<br>+#define FAN_ON_50 1<br>+#define FAN_ON_100 2<br>+<br> static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)<br> {<br> int trip_temp, trip_hyst;<br>@@ -32,29 +36,59 @@<br> trip_hyst);<br> <br> list_for_each_entry(instance, &tz->thermal_instances, tz_node) {<br>+<br>+ unsigned long upper;<br>+ unsigned long lower;<br>+<br> if (instance->trip != trip)<br> continue;<br> <br>+ upper = instance->upper;<br>+ lower = instance->lower;<br>+<br>+ dev_dbg(&instance->cdev->device,"instance state = %ld trip = %d lower = %ld upper= %ld\n",<br>+ instance->target, trip,lower, upper);<br>+<br> /* in case fan is in initial state, switch the fan off */<br> if (instance->target == THERMAL_NO_TARGET)<br>- instance->target = 0;<br>+ instance->target = FAN_OFF;<br> <br>- /* in case fan is neither on nor off set the fan to active */<br>- if (instance->target != 0 && instance->target != 1) {<br>- pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n",<br>- instance->name, instance->target);<br>- instance->target = 1;<br>+ /*<br>+ * In case fan is not off or running at 50/100% settings, set the fan to<br>+ * upper or 50%.<br>+ */<br>+ if (instance->target != FAN_OFF &&<br>+   instance->target !=  FAN_ON_50 &&<br>+   instance->target !=  FAN_ON_100) {<br>+ pr_warn("Thermal instance %s controlled by bang-bang has"<br>+ " unexpected state: %ld\n lower=%ld upper=%ld",<br>+ instance->name, instance->target,<br>+ lower, upper);<br>+<br>+ if(upper > FAN_ON_100 || lower > FAN_ON_100)<br>+ instance->target = FAN_ON_50;<br>+ else<br>+ instance->target = upper;<br> }<br> <br> /*<br>- * enable fan when temperature exceeds trip_temp and disable<br>- * the fan in case it falls below trip_temp minus hysteresis<br>+ * set fan to upper when temperature exceeds trip_temp and set<br>+ * fan to lower in case it falls below trip_temp minus hysteresis<br> */<br>- if (instance->target == 0 && tz->temperature >= trip_temp)<br>- instance->target = 1;<br>- else if (instance->target == 1 &&<br>- tz->temperature <= trip_temp - trip_hyst)<br>- instance->target = 0;<br>+ if (instance->target < upper && tz->temperature >= trip_temp) {<br>+ dev_dbg(&instance->cdev->device, "increase cooling: target"<br>+ " = %ld, upper = %ld, temp = %ld\n, trip_temp = %ld\n",<br>+ instance->target, upper tz->temperature trip_temp);<br>+ instance->target = upper;<br>+<br>+ } else if (instance->target > lower &&<br>+ tz->temperature <= trip_temp - trip_hyst) {<br>+ dev_dbg(&instance->cdev->device, "increase cooling: target"<br>+ " = %ld, lower = %ld, temp = %ld\n, trip_temp - hyst = %ld\n",<br>+ instance->target, lower, tz->temperature,<br>+ trip_temp - trip_hyst);<br>+ instance->target = lower;<br>+ }<br> <br> dev_dbg(&instance->cdev->device, "target=%d\n",<br> (int)instance->target);<br clear="all"><div><br></div><div>What I have observed </div><div>When temperature goes up from 60-65, the fan is switched on at 50%, and when temperature goes on from 65-70, fan is switched on at 100%.</div><div>When the temperature goes down from 70-65, the fan goes from 100% to 50%, but it doesn't switch off when the temperature goes from 65-60. Fan stays running at 50% when temperature has fallen below the "trip - hysteresis" point. </div><div><br></div><div>I put some debugs around the code to see what is happening. </div><div>I observed that "instance->target" is showing two values 0 and 1, which are OFF and 50%, when I expect it to be OFF. My guess is these values are for different trips, since "trip2" has a "lower" set to 1. </div><div>I am not quite sure how to make sure that value goes back 0 here. Any insight on what I have done wrong and what can I do to make it work?</div><div><br></div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">Thank you <br>Warm Regards<br>Anuz<br></div></div></div>