Skip to content

Instantly share code, notes, and snippets.

@geek-at
Created December 29, 2023 10:51
Show Gist options
  • Save geek-at/502199c193da41147cfab2e593012ccd to your computer and use it in GitHub Desktop.
Save geek-at/502199c193da41147cfab2e593012ccd to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Script to control the fan on the XG-7100.
# The EMC2104 is accessed via the smbus on the ICH.
#
# SteveW 24/01/2019
#
# Variables
smb_dev="/dev/null"
smb_led_dev="/dev/null"
fan_speed=0
start_speed=96
minimum_speed=60
maximum_speed=255
interval=2
hysteresis=1
ramp_rate=2
target_temp=38
verbose=false
coretemp=0
target_low=`expr $target_temp - $hysteresis`
target_high=`expr $target_temp + $hysteresis`
# Load required modules if they are not loaded
for mod in ichsmb ismt smb coretemp; do
kldstat | grep " $mod.ko" > /dev/null
if [ $? -eq 1 ]
then
kldload $mod
$verbose && echo "$mod loaded"
else
$verbose && echo "$mod already loaded"
fi
done
# Test we can "see" the fan controller on the smbus
if [ "$(smbmsg -f /dev/smb0 -s 0x5e -c 0xfd -i 1)" = 0x1d ]
then
smb_dev="/dev/smb0"
smb_led_dev="/dev/smb1"
$verbose && echo "Found EMC2104 on $smb_dev"
else
if [ "$(smbmsg -f /dev/smb1 -s 0x5e -c 0xfd -i 1)" = 0x1d ]
then
smb_dev="/dev/smb1"
smb_led_dev="/dev/smb0"
$verbose && echo "Found EMC2104 on $smb_dev"
else
echo "EMC2104 not found"
exit 1
fi
fi
# Set both fan controller to manual drive mode
smbmsg -f $smb_dev -s 0x5e -c 0x50 -o 1 0x10
smbmsg -f $smb_dev -s 0x5e -c 0x90 -o 1 0x10
$verbose && echo "Fan controllers set to manual"
# Read initial Coretemp Value
core_temp=`sysctl -n dev.cpu.0.temperature | cut -c1-2`
$verbose && echo "CPU temperature is $core_temp"
# Set Fan start speeds
smbmsg -f $smb_dev -s 0x5e -c 0x40 -o 1 $start_speed
smbmsg -f $smb_dev -s 0x5e -c 0x80 -o 1 $start_speed
fan_speed=$start_speed
# Fan control loop
logger Fan control started
while true; do
core_temp=`sysctl -n dev.cpu.0.temperature | cut -c1-2`
echo "Coretemp0 is $core_temp, Fanspeed is $fan_speed"
if [ $core_temp -gt $target_high ]
then
fan_speed=`expr $fan_speed + $ramp_rate`
$verbose && echo "Fanspeed increase"
else
if [ $core_temp -lt $target_low ] && [ $fan_speed -gt $minimum_speed ]
then
fan_speed=`expr $fan_speed - $ramp_rate`
$verbose && echo "Fanspeed decrease"
else
$verbose && echo "Fanspeed is good"
fi
fi
$verbose && echo "New fanspeed is $fan_speed"
smbmsg -f $smb_dev -s 0x5e -c 0x40 -o 1 $fan_speed
smbmsg -f $smb_dev -s 0x5e -c 0x80 -o 1 $fan_speed
smbmsg -f $smb_led_dev -s 0xb6 -c 0x3 -o 1 0x0 # turn off the LED
sleep $interval
smbmsg -f $smb_led_dev -s 0xb6 -c 0x3 -o 1 0x3 # turn on the orange (red & green) LED
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment