#!/bin/sh /etc/rc.common
#
# Copyright (C) 2025 openwrt.org
#

START=99

. /lib/functions.sh

find_nct7802()
{
	for hwmon in /sys/class/hwmon/hwmon*; do
		if [ -f "$hwmon/name" ] && [ "$(cat "$hwmon/name" 2>/dev/null)" = "nct7802" ]; then
			echo "$hwmon"
			return
		fi
	done
	echo "/sys/class/hwmon/hwmon3" # fallback
}

boot()
{
	case $(board_name) in
	gemtek,w1700k-ubi)
		hwmon=$(find_nct7802)

		# Temporarily disable automatic fan control to configure curve
		echo 1 > $hwmon/pwm1_enable

		# Match trigger points from vendor firmware
		echo 40000 > $hwmon/pwm1_auto_point1_temp
		echo 50000 > $hwmon/pwm1_auto_point2_temp
		echo 60000 > $hwmon/pwm1_auto_point3_temp
		echo 70000 > $hwmon/pwm1_auto_point4_temp
		echo 80000 > $hwmon/pwm1_auto_point5_temp

		# Match fan speed from vendor firmware
		echo 54  > $hwmon/pwm1_auto_point1_pwm
		echo 69  > $hwmon/pwm1_auto_point2_pwm
		echo 95  > $hwmon/pwm1_auto_point3_pwm
		echo 199 > $hwmon/pwm1_auto_point4_pwm

		# Re-enable automatic fan control
		echo 2 > $hwmon/pwm1_enable
		;;
	esac
}

