fan.patch 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. From 6bd64bbc79b7a86224dea328dee07fa27f2cc06b Mon Sep 17 00:00:00 2001
  2. From: Lawrence-Tang <[email protected]>
  3. Date: Wed, 20 Oct 2021 15:00:23 +0800
  4. Subject: [PATCH] rockchip: add fan control for r2s/r4s
  5. ---
  6. .../armv8/base-files/etc/init.d/fa-fancontrol | 16 ++++
  7. .../armv8/base-files/usr/bin/cputemp.sh | 14 ++++
  8. .../usr/bin/fa-fancontrol-direct.sh | 69 +++++++++++++++++++
  9. .../armv8/base-files/usr/bin/fa-fancontrol.sh | 36 ++++++++++
  10. 4 files changed, 127 insertions(+)
  11. create mode 100755 target/linux/rockchip_rk3588/armv8/base-files/etc/init.d/fa-fancontrol
  12. create mode 100755 target/linux/rockchip_rk3588/armv8/base-files/usr/bin/cputemp.sh
  13. create mode 100755 target/linux/rockchip_rk3588/armv8/base-files/usr/bin/fa-fancontrol-direct.sh
  14. create mode 100755 target/linux/rockchip_rk3588/armv8/base-files/usr/bin/fa-fancontrol.sh
  15. diff --git a/target/linux/rockchip_rk3588/armv8/base-files/etc/init.d/fa-fancontrol b/target/linux/rockchip_rk3588/armv8/base-files/etc/init.d/fa-fancontrol
  16. new file mode 100755
  17. index 0000000000000..dd848927101b0
  18. --- /dev/null
  19. +++ b/target/linux/rockchip_rk3588/armv8/base-files/etc/init.d/fa-fancontrol
  20. @@ -0,0 +1,16 @@
  21. +#!/bin/sh /etc/rc.common
  22. +
  23. +START=21
  24. +start() {
  25. + /usr/bin/fa-fancontrol.sh &
  26. +}
  27. +
  28. +stop() {
  29. +kill -9 $(ps -w | grep fa-fancontrol | grep -v grep | awk '{print$1}') 2>/dev/null
  30. +}
  31. +
  32. +restart() {
  33. +stop
  34. +start
  35. +}
  36. +
  37. diff --git a/target/linux/rockchip_rk3588/armv8/base-files/usr/bin/cputemp.sh b/target/linux/rockchip_rk3588/armv8/base-files/usr/bin/cputemp.sh
  38. new file mode 100755
  39. index 0000000000000..e497269bccdcd
  40. --- /dev/null
  41. +++ b/target/linux/rockchip_rk3588/armv8/base-files/usr/bin/cputemp.sh
  42. @@ -0,0 +1,14 @@
  43. +#!/bin/bash
  44. +
  45. +while true
  46. +do
  47. +echo "-----------------"
  48. +echo "Curr freq: `cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq`,`cat /sys/devices/system/cpu/cpu4/cpufreq/cpuinfo_cur_freq`"
  49. +echo "Temp: `cat /sys/class/thermal/thermal_zone0/temp`"
  50. +if [ -d /sys/devices/platform/pwm-fan ]; then
  51. + CUR=`cat /sys/devices/virtual/thermal/cooling_device0/cur_state`
  52. + MAX=`cat /sys/devices/virtual/thermal/cooling_device0/max_state`
  53. + echo "Fan Level: ${CUR}/${MAX}"
  54. +fi
  55. +sleep 5
  56. +done
  57. diff --git a/target/linux/rockchip_rk3588/armv8/base-files/usr/bin/fa-fancontrol-direct.sh b/target/linux/rockchip_rk3588/armv8/base-files/usr/bin/fa-fancontrol-direct.sh
  58. new file mode 100755
  59. index 0000000000000..b16c994d66955
  60. --- /dev/null
  61. +++ b/target/linux/rockchip_rk3588/armv8/base-files/usr/bin/fa-fancontrol-direct.sh
  62. @@ -0,0 +1,69 @@
  63. +#!/bin/bash
  64. +
  65. +# determine fan controller
  66. +if [ -d /sys/devices/platform/pwm-fan ]; then
  67. + echo "pls use /usr/bin/fa-fancontrol.sh."
  68. + exit 1
  69. +fi
  70. +
  71. +if [ ! -d /sys/class/pwm/pwmchip1 ]; then
  72. + echo "this model does not support pwm."
  73. + exit 1
  74. +fi
  75. +if [ ! -d /sys/class/pwm/pwmchip1/pwm0 ]; then
  76. + echo 0 > /sys/class/pwm/pwmchip1/export
  77. +fi
  78. +sleep 1
  79. +while [ ! -d /sys/class/pwm/pwmchip1/pwm0 ];
  80. +do
  81. + sleep 1
  82. +done
  83. +ISENABLE=`cat /sys/class/pwm/pwmchip1/pwm0/enable`
  84. +if [ $ISENABLE -eq 1 ]; then
  85. + echo 0 > /sys/class/pwm/pwmchip1/pwm0/enable
  86. +fi
  87. +echo 50000 > /sys/class/pwm/pwmchip1/pwm0/period
  88. +echo 1 > /sys/class/pwm/pwmchip1/pwm0/enable
  89. +
  90. +# max speed run 5s
  91. +echo 46990 > /sys/class/pwm/pwmchip1/pwm0/duty_cycle
  92. +sleep 5
  93. +echo 25000 > /sys/class/pwm/pwmchip1/pwm0/duty_cycle
  94. +
  95. +# declare -a CpuTemps=(55000 43000 38000 32000)
  96. +# declare -a PwmDutyCycles=(1000 20000 30000 45000)
  97. +
  98. +declare -a CpuTemps=(75000 63000 58000 52000)
  99. +declare -a PwmDutyCycles=(25000 35000 45000 46990)
  100. +
  101. +declare -a Percents=(100 75 50 25)
  102. +DefaultDuty=49990
  103. +DefaultPercents=0
  104. +
  105. +while true
  106. +do
  107. + temp=$(cat /sys/class/thermal/thermal_zone0/temp)
  108. + INDEX=0
  109. + FOUNDTEMP=0
  110. + DUTY=$DefaultDuty
  111. + PERCENT=$DefaultPercents
  112. +
  113. + for i in 0 1 2 3; do
  114. + if [ $temp -gt ${CpuTemps[$i]} ]; then
  115. + INDEX=$i
  116. + FOUNDTEMP=1
  117. + break
  118. + fi
  119. + done
  120. + if [ ${FOUNDTEMP} == 1 ]; then
  121. + DUTY=${PwmDutyCycles[$i]}
  122. + PERCENT=${Percents[$i]}
  123. + fi
  124. +
  125. + echo $DUTY > /sys/class/pwm/pwmchip1/pwm0/duty_cycle;
  126. +
  127. + # echo "temp: $temp, duty: $DUTY, ${PERCENT}%"
  128. + # cat /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_cur_freq
  129. +
  130. + sleep 2s;
  131. +done
  132. diff --git a/target/linux/rockchip_rk3588/armv8/base-files/usr/bin/fa-fancontrol.sh b/target/linux/rockchip_rk3588/armv8/base-files/usr/bin/fa-fancontrol.sh
  133. new file mode 100755
  134. index 0000000000000..e7c14054c80e9
  135. --- /dev/null
  136. +++ b/target/linux/rockchip_rk3588/armv8/base-files/usr/bin/fa-fancontrol.sh
  137. @@ -0,0 +1,36 @@
  138. +#!/bin/bash
  139. +
  140. +# determine fan controller
  141. +if [ -d /sys/devices/platform/pwm-fan ]; then
  142. + (cd /sys/devices/virtual/thermal/thermal_zone0 && {
  143. + logger -p user.info -t "pwmfan" "set the conditions for fan"
  144. + [ -f trip_point_3_temp ] && {
  145. + # select fan level 1
  146. + echo 50000 > trip_point_3_temp
  147. + }
  148. + [ -f trip_point_4_temp ] && {
  149. + # select fan level 2-4
  150. + echo 55000 > trip_point_4_temp
  151. + }
  152. + })
  153. +
  154. + (cd /sys/devices/virtual/thermal/cooling_device0 && {
  155. + TYPE=`cat type`
  156. + if [ $TYPE = 'pwm-fan' ]; then
  157. + # run 5s
  158. + for i in `seq 1 5`; do
  159. + logger -p user.info -t "pwmfan" "start to spin ${i}/5"
  160. + echo 3 > cur_state
  161. + sleep 1
  162. + done
  163. + logger -p user.info -t "pwmfan" "set to auto"
  164. + echo 0 > cur_state
  165. + fi
  166. + })
  167. +else
  168. + logger -p user.info -t "pwmfan" "not found cooling device"
  169. + if [ -d /sys/class/pwm ]; then
  170. + nohup /usr/bin/fa-fancontrol-direct.sh&
  171. + fi
  172. +fi
  173. +