159 lines
3.3 KiB
Bash
Executable file
159 lines
3.3 KiB
Bash
Executable file
#!/bin/sh
|
|
|
|
# config file
|
|
config_path="/etc/zgoc"
|
|
config_file="$config_path/zgoc.conf"
|
|
|
|
# load config
|
|
[ -f "$config_file" ] && . "$config_file"
|
|
|
|
# default conf
|
|
[ -z "$FANC_TIME_INTERVAL" ] && FANC_TIME_INTERVAL=2
|
|
[ -z "$FANC_DEFAULT_PROFILE" ] && FANC_DEFAULT_PROFILE=2
|
|
[ -z "$FANC_PROFILE_PATH" ] && FANC_PROFILE_PATH=profiles/power
|
|
[ $(echo "$FANC_PROFILE_PATH" | cut -c1) != '/' ] && FANC_PROFILE_PATH="$config_path/$FANC_PROFILE_PATH"
|
|
|
|
# load profile
|
|
profile=$FANC_PROFILE_PATH/$FANC_DEFAULT_PROFILE
|
|
[ ! -f "$profile" ] && echo "'$profile' not found" >&2 && exit 1
|
|
|
|
{
|
|
read -r temps
|
|
read -r fans
|
|
} < "$profile"
|
|
|
|
NT=$(echo "$temps" | awk '{print NF}')
|
|
NF=$(echo "$fans" | awk '{print NF}')
|
|
|
|
error () {
|
|
printf "\033[1;31m%s\033[0m\n" "$1" >&2
|
|
}
|
|
|
|
if [ "$NT" -ne "$NF" ]
|
|
then
|
|
error "Amount of temperature and pwm values do not match"
|
|
exit 30
|
|
fi
|
|
|
|
# $1 = array , $2 = rank
|
|
getval()
|
|
{
|
|
echo "$1" | awk "{print \$$(($2+1))}"
|
|
}
|
|
|
|
file_report()
|
|
{
|
|
if [ -f "$(pwd)/$1" ]
|
|
then
|
|
echo "$(pwd)/$1 = $(cat "$(pwd)/$1")"
|
|
elif [ -f "$1" ]
|
|
then
|
|
echo "$1 = $(cat "$1")"
|
|
else
|
|
error "File $1 from $(pwd) unaccessible"
|
|
fi
|
|
}
|
|
|
|
file_write ()
|
|
{
|
|
if ! echo "$1" > "$2"
|
|
then
|
|
error "Error writing to $2 from $(pwd)"
|
|
exit 12
|
|
fi
|
|
file_report "$2"
|
|
}
|
|
|
|
_stop() {
|
|
echo ""
|
|
file_write 2 pwm1_enable
|
|
exit $1
|
|
}
|
|
|
|
if [ $(lspci | grep -c VGA) -gt 1 ]
|
|
then
|
|
if [ -z "$GPU_NAME" ] && [ -z "$GPU_PCI_ID" ] ; then
|
|
error "Several GPUs are present and no GPU is specified"
|
|
exit 20
|
|
fi
|
|
if [ -z "$GPU_PCI_ID" ] ; then
|
|
GPU_PCI_ID=$(lspci | grep VGA | grep "$GPU_NAME" | cut -d ' ' -f1)
|
|
if [ "$(echo "$GPU_PCI_ID" | wc -l)" -gt 1 ] ; then
|
|
error "More than one name match"
|
|
exit 21
|
|
elif [ "$(echo "$GPU_PCI_ID" | wc -l)" -lt 1 ] ; then
|
|
error "No name match"
|
|
exit 22
|
|
fi
|
|
fi
|
|
cd /sys/class/drm || exit
|
|
|
|
if ! cd "$(ls -l card? | grep "$GPU_PCI_ID" | tr -s ' ' | cut -d ' ' -f9)"
|
|
then
|
|
error "Error finding pci device"
|
|
echo 23
|
|
fi
|
|
else
|
|
cd "$(find /sys/class/drm/card? | head -n1)" || exit
|
|
fi
|
|
echo -n "Device at "
|
|
pwd
|
|
cd "device/hwmon/$(ls device/hwmon)" || exit
|
|
|
|
if [ $(id -u) -ne 0 ]
|
|
then
|
|
echo "Root privileges required"
|
|
exit 10
|
|
fi
|
|
|
|
if [ ! -f pwm1 ]
|
|
then
|
|
error "PWM not available on this device"
|
|
exit 11
|
|
fi
|
|
|
|
trap '_stop 1' INT
|
|
|
|
file_write 1 pwm1_enable
|
|
|
|
while true
|
|
do
|
|
sleep "$FANC_TIME_INTERVAL" &
|
|
temp=$(zgpu -g0 -Wnt -i0 | cut -d '.' -f1 | tr -d '\n')
|
|
I=1
|
|
if [ "$temp" -lt "$(getval "$temps" 0)" ]
|
|
then
|
|
fan=$(getval "$fans" 0)
|
|
else
|
|
while [ $I -lt $NT ]
|
|
do
|
|
if [ $temp -lt $(getval "$temps" $I) ]
|
|
then
|
|
LOWERTEMP=$(getval "$temps" $((I-1)))
|
|
HIGHERTEMP=$(getval "$temps" $I)
|
|
LOWERPWM=$(getval "$fans" $((I-1)))
|
|
HIGHERPWM=$(getval "$fans" $I)
|
|
fan=$(echo "( ( $temp - $LOWERTEMP ) * ( $HIGHERPWM - $LOWERPWM ) / ( $HIGHERTEMP - $LOWERTEMP ) ) + $LOWERPWM" | bc)
|
|
I=$NT
|
|
fi
|
|
I=$(($I + 1))
|
|
done
|
|
fi
|
|
if [ -z "$fan" ]
|
|
then
|
|
fan=$(getval "$fans" $((I-1)))
|
|
fi
|
|
# get min/max values
|
|
fan_max=$(cat pwm1_max) && fan_min=$(cat pwm1_min) || _stop 1
|
|
# scale to min/max
|
|
fan=$(echo "( ( $fan * ($fan_max - $fan_min) ) / 100 ) + $fan_min" | bc)
|
|
# smooth with oldval
|
|
[ -z "$oldfan" ] && oldfan=$fan
|
|
[ "$FANC_SMOOTH" = "true" ] && fan=$(echo "($oldfan + $fan) / 2" | bc)
|
|
oldfan=$fan
|
|
# write
|
|
file_write "$fan" pwm1
|
|
wait $(jobs -p)
|
|
done
|
|
|
|
_stop 0
|