From 23239c19b6e56ecba205854c2639d68d3007284f Mon Sep 17 00:00:00 2001 From: Howard Wu Date: Fri, 1 Dec 2023 17:27:14 +0800 Subject: [PATCH] rockchip: fix fan control for r2c plus --- .../usr/bin/fa-fancontrol-direct.sh | 71 +++++++++---------- .../armv8/base-files/usr/bin/fa-fancontrol.sh | 29 ++++---- 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/target/linux/rockchip/armv8/base-files/usr/bin/fa-fancontrol-direct.sh b/target/linux/rockchip/armv8/base-files/usr/bin/fa-fancontrol-direct.sh index b16c994d669559..4ac09a173e6438 100755 --- a/target/linux/rockchip/armv8/base-files/usr/bin/fa-fancontrol-direct.sh +++ b/target/linux/rockchip/armv8/base-files/usr/bin/fa-fancontrol-direct.sh @@ -5,30 +5,30 @@ if [ -d /sys/devices/platform/pwm-fan ]; then echo "pls use /usr/bin/fa-fancontrol.sh." exit 1 fi - -if [ ! -d /sys/class/pwm/pwmchip1 ]; then +PWMCHIP="/sys/class/pwm/pwmchip1" +[ "$1" ] && PWMCHIP="$1" +if [ ! -d "$PWMCHIP" ]; then echo "this model does not support pwm." exit 1 fi -if [ ! -d /sys/class/pwm/pwmchip1/pwm0 ]; then - echo 0 > /sys/class/pwm/pwmchip1/export +if [ ! -d "$PWMCHIP/pwm0" ]; then + echo 0 >"$PWMCHIP/export" fi sleep 1 -while [ ! -d /sys/class/pwm/pwmchip1/pwm0 ]; -do +while [ ! -d "$PWMCHIP/pwm0" ]; do sleep 1 done -ISENABLE=`cat /sys/class/pwm/pwmchip1/pwm0/enable` -if [ $ISENABLE -eq 1 ]; then - echo 0 > /sys/class/pwm/pwmchip1/pwm0/enable +ISENABLE=$(cat "$PWMCHIP/pwm0/enable") +if [ "$ISENABLE" -eq 1 ]; then + echo 0 >"$PWMCHIP/pwm0/enable" fi -echo 50000 > /sys/class/pwm/pwmchip1/pwm0/period -echo 1 > /sys/class/pwm/pwmchip1/pwm0/enable +echo 50000 >"$PWMCHIP/pwm0/period" +echo 1 >"$PWMCHIP/pwm0/enable" # max speed run 5s -echo 46990 > /sys/class/pwm/pwmchip1/pwm0/duty_cycle +echo 46990 >"$PWMCHIP/pwm0/duty_cycle" sleep 5 -echo 25000 > /sys/class/pwm/pwmchip1/pwm0/duty_cycle +echo 25000 >"$PWMCHIP/pwm0/duty_cycle" # declare -a CpuTemps=(55000 43000 38000 32000) # declare -a PwmDutyCycles=(1000 20000 30000 45000) @@ -40,30 +40,29 @@ declare -a Percents=(100 75 50 25) DefaultDuty=49990 DefaultPercents=0 -while true -do - temp=$(cat /sys/class/thermal/thermal_zone0/temp) - INDEX=0 - FOUNDTEMP=0 - DUTY=$DefaultDuty - PERCENT=$DefaultPercents - - for i in 0 1 2 3; do - if [ $temp -gt ${CpuTemps[$i]} ]; then - INDEX=$i - FOUNDTEMP=1 - break - fi - done - if [ ${FOUNDTEMP} == 1 ]; then - DUTY=${PwmDutyCycles[$i]} - PERCENT=${Percents[$i]} - fi +while true; do + temp=$(cat /sys/class/thermal/thermal_zone0/temp) + INDEX=0 + FOUNDTEMP=0 + DUTY=$DefaultDuty + PERCENT=$DefaultPercents + + for i in 0 1 2 3; do + if [ "$temp" -gt "${CpuTemps[$i]}" ]; then + INDEX=$i + FOUNDTEMP=1 + break + fi + done + if [ ${FOUNDTEMP} == 1 ]; then + DUTY=${PwmDutyCycles[$i]} + PERCENT=${Percents[$INDEX]} + fi - echo $DUTY > /sys/class/pwm/pwmchip1/pwm0/duty_cycle; + echo "$DUTY" >"$PWMCHIP/pwm0/duty_cycle" - # echo "temp: $temp, duty: $DUTY, ${PERCENT}%" - # cat /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_cur_freq + # echo "temp: $temp, duty: $DUTY, ${PERCENT}%" + # cat /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_cur_freq - sleep 2s; + sleep 2s done diff --git a/target/linux/rockchip/armv8/base-files/usr/bin/fa-fancontrol.sh b/target/linux/rockchip/armv8/base-files/usr/bin/fa-fancontrol.sh index e7c14054c80e92..4579bda46771fa 100755 --- a/target/linux/rockchip/armv8/base-files/usr/bin/fa-fancontrol.sh +++ b/target/linux/rockchip/armv8/base-files/usr/bin/fa-fancontrol.sh @@ -2,35 +2,40 @@ # determine fan controller if [ -d /sys/devices/platform/pwm-fan ]; then - (cd /sys/devices/virtual/thermal/thermal_zone0 && { + (cd /sys/devices/virtual/thermal/thermal_zone0 && { logger -p user.info -t "pwmfan" "set the conditions for fan" [ -f trip_point_3_temp ] && { # select fan level 1 - echo 50000 > trip_point_3_temp + echo 50000 >trip_point_3_temp } [ -f trip_point_4_temp ] && { # select fan level 2-4 - echo 55000 > trip_point_4_temp + echo 55000 >trip_point_4_temp } }) (cd /sys/devices/virtual/thermal/cooling_device0 && { - TYPE=`cat type` - if [ $TYPE = 'pwm-fan' ]; then + TYPE=$(cat type) + if [ "$TYPE" = 'pwm-fan' ]; then # run 5s - for i in `seq 1 5`; do + for i in $(seq 1 5); do logger -p user.info -t "pwmfan" "start to spin ${i}/5" - echo 3 > cur_state + echo 3 >cur_state sleep 1 done logger -p user.info -t "pwmfan" "set to auto" - echo 0 > cur_state + echo 0 >cur_state fi }) else logger -p user.info -t "pwmfan" "not found cooling device" - if [ -d /sys/class/pwm/pwmchip1 ]; then - nohup /usr/bin/fa-fancontrol-direct.sh& - fi + i=0 + while [ $i -lt 9 ]; do + PWMCHIP="/sys/class/pwm/pwmchip$i" + if [ -d "$PWMCHIP" ]; then + nohup /usr/bin/fa-fancontrol-direct.sh "$PWMCHIP" & + break + fi + i=$((i + 1)) + done fi -