#!/bin/sh /etc/rc.common

START=18

# Print the hwmon directory of the single ADT747x fan controller, on whichever
# I2C bus it sits.
adt747x_hwmon() {
	local dev found

	for dev in /sys/bus/i2c/devices/*; do
		case "$(cat "$dev/name" 2>/dev/null)" in
		adt7473|adt7475|adt7476|adt7490)
			[ -z "$found" ] || return 1
			found="$dev"
			;;
		esac
	done

	for dev in "$found"/hwmon/hwmon*; do
		[ -d "$dev" ] && echo "$dev" && return 0
	done

	return 1
}

# Let an ADT747x run fan <pwm> on its own, from all three temperature sensors.
# For more information, see Documentation/hwmon/sysfs-interface.rst.
#  $1  PWM output (1-3)
#  $2  duty cycle at the lower temperature point (0-255); full duty at the upper
#  $3  lower temperature point (millidegrees C)
#  $4  upper temperature point; the chip rounds it to its own steps
#  $5  critical temperature
#  $6  critical temperature hysteresis
#  $7  1: keep the fan at the lower duty below the lower point, not off
#  $8  PWM frequency in Hz (optional)
adt747x_fancontrol() {
	local pwm="$1"
	local path_to_hwmon
	local i

	path_to_hwmon="$(adt747x_hwmon)" || {
		logger -t hwmon_fancontrol "ADT747x fan controller not found"
		return 1
	}

	echo 7 > "$path_to_hwmon/pwm${pwm}_auto_channels_temp"
	echo "$2" > "$path_to_hwmon/pwm${pwm}_auto_point1_pwm"
	echo 255 > "$path_to_hwmon/pwm${pwm}_auto_point2_pwm"
	[ -n "$8" ] && echo "$8" > "$path_to_hwmon/pwm${pwm}_freq"

	for i in 1 2 3; do
		echo "$5" > "$path_to_hwmon/temp${i}_crit"
	done
	for i in 1 2 3; do
		echo "$6" > "$path_to_hwmon/temp${i}_crit_hyst"
	done
	for i in 1 2 3; do
		echo "$3" > "$path_to_hwmon/temp${i}_auto_point1_temp"
	done
	for i in 1 2 3; do
		echo "$4" > "$path_to_hwmon/temp${i}_auto_point2_temp"
	done

	[ "$7" = 1 ] && echo 1 > "$path_to_hwmon/pwm${pwm}_stall_disable"

	echo 2 > "$path_to_hwmon/pwm${pwm}_enable"
}

dlink_dgs_1210() {
	local path_to_hwmon="$(find -L /sys/bus/i2c/devices/4-004c -type d -maxdepth 2 -mindepth 2 -name 'hwmon*' || true)"

	if [ -z "$path_to_hwmon" ]; then
		logger -t hwmon_fancontrol "LM63 hwmon interface not found on i2c 4-004c (kmod lm63 loaded?)"
		return 1
	fi

	# Set to manual mode temporarily to allow LUT and frequency setup
	echo 1 > "$path_to_hwmon/pwm1_enable"

	# Configure PWM frequency (~7826 Hz / PFR divisor 23) matching OEM defaults
	echo 7826 > "$path_to_hwmon/pwm1_freq"

	# Point 1: If temp > 0 C, set fan speed to PWM 155/255 (OEM default 0x1c)
	echo 0 > "$path_to_hwmon/pwm1_auto_point1_temp"
	echo $((0x1c * 255 / 46)) > "$path_to_hwmon/pwm1_auto_point1_pwm"

	# Point 2: If temp > 51 C, set fan speed to PWM 255/255 (OEM default 0x2e)
	echo 51000 > "$path_to_hwmon/pwm1_auto_point2_temp"
	echo $((0x2e * 255 / 46)) > "$path_to_hwmon/pwm1_auto_point2_pwm"

	# Point 3-8: Set the chip default values for unused points as reboot does not clean them
	for p in 3 4 5 6 7 8; do
		echo 127000 > "$path_to_hwmon/pwm1_auto_point${p}_temp"
		echo 255 > "$path_to_hwmon/pwm1_auto_point${p}_pwm"
	done

	# Hysteresis: Set 3 C (3000 mC) hysteresis for LUT (OEM default 0x03)
	echo 3000 > "$path_to_hwmon/pwm1_auto_point_temp_hyst"

	# Switch to hardware auto mode
	echo 2 > "$path_to_hwmon/pwm1_enable"
}

dlink_dgs_1250() {
	local path_temp_hwmon="$(find /sys/devices/platform/i2c-gpio-4/i2c-4/4-0048/hwmon/ -type d -maxdepth 1 -mindepth 1 -name 'hwmon*')"
	local path_fan_hwmon="$(find /sys/devices/platform/gpio-fan/hwmon/ -type d -maxdepth 1 -mindepth 1 -name 'hwmon*')"

	# Activate fan and let LM75 steer high/low speed via alert pin
	# vendor defaults for DGS-1250-28X are 69C low->high and 57C high->low
	echo 1 > "$path_fan_hwmon/fan1_target"
	echo 69000 > "$path_temp_hwmon/temp1_max"
	echo 57000 > "$path_temp_hwmon/temp1_max_hyst"
}

# Find the LM63 independent of the I2C bus and hwmon device number.
lm63_hwmon() {
	local dev found

	for dev in /sys/class/hwmon/hwmon*; do
		[ "$(cat "$dev/name" 2>/dev/null)" = lm63 ] || continue
		[ -z "$found" ] || {
			logger -t hwmon_fancontrol "Multiple LM63 controllers found"
			return 1
		}
		found="$dev"
	done

	[ -n "$found" ] || {
		logger -t hwmon_fancontrol "LM63 controller not found"
		return 1
	}
	printf '%s\n' "$found"
}

linksys_lgs328mpc_v2() {
	# The U-Boot of this device does not set up the LM63 fan controller for us, so we do it ourselves:
	# Let LM63 steer high/low speed full automatically, vendor defaults are 40C low->high and 31C high->low.
	# For this, reduce PMW frequency as otherwise the fan won't start at low speeds.
	# Temperature point 1 is set to 1C and a hysteresis of 9C gives 1C - 9C = -8C - most likely always on.
	# Temperature point 2 is set to 40C and a hysteresis of 9C gives 40C - 9C = 31C.
	# Other points must be non-decreasing for the kernel to accept auto mode.
	local path_to_hwmon p

	path_to_hwmon="$(lm63_hwmon)" || return 1

	# switch to manual mode, otherwise LUT is not writable
	echo 1 > "$path_to_hwmon/pwm1_enable"
	# Set PWM Frequency to 22Hz as otherwise the fan doesn't spin at low duty cycles.
	echo 22 > "$path_to_hwmon/pwm1_freq"

	echo 1000  > "$path_to_hwmon/pwm1_auto_point1_temp" # 1°C
	echo 128   > "$path_to_hwmon/pwm1_auto_point1_pwm"  # 50% fan speed
	echo 40000 > "$path_to_hwmon/pwm1_auto_point2_temp" # 40°C
	echo 255   > "$path_to_hwmon/pwm1_auto_point2_pwm"  # 100% fan speed
	# Unused points must be non-decreasing.
	for p in 3 4 5 6 7 8; do
		echo 127000 > "$path_to_hwmon/pwm1_auto_point${p}_temp"
		echo 255 > "$path_to_hwmon/pwm1_auto_point${p}_pwm"
	done

	# set temperature hysteresis to 9K
	echo 9000 > "$path_to_hwmon/pwm1_auto_point_temp_hyst"

	# switch to automatic, LUT driven mode
	echo 2 > "$path_to_hwmon/pwm1_enable"
}

# Configure the original Linksys LGS352C LM63 fan curve.
linksys_lgs352c() (
	local path_to_hwmon p

	path_to_hwmon="$(lm63_hwmon)" || return 1

	# If initialization fails, request maximum fan speed.
	trap 'rc=$?; if [ "$rc" -ne 0 ]; then
		echo 1 > "$path_to_hwmon/pwm1_enable" 2>/dev/null || :
		echo 255 > "$path_to_hwmon/pwm1" 2>/dev/null || :
		logger -t hwmon_fancontrol "LGS352C LM63 initialization failed; requesting full fan speed"
	fi' EXIT
	set -e

	# Set full speed while configuring the original Linksys curve.
	echo 1 > "$path_to_hwmon/pwm1_enable"
	echo 255 > "$path_to_hwmon/pwm1"
	echo 23 > "$path_to_hwmon/pwm1_freq"

	echo 20000 > "$path_to_hwmon/pwm1_auto_point1_temp"
	echo 102 > "$path_to_hwmon/pwm1_auto_point1_pwm"
	echo 55000 > "$path_to_hwmon/pwm1_auto_point2_temp"
	echo 255 > "$path_to_hwmon/pwm1_auto_point2_pwm"

	for p in 3 4 5 6 7 8; do
		echo 127000 > "$path_to_hwmon/pwm1_auto_point${p}_temp"
		echo 255 > "$path_to_hwmon/pwm1_auto_point${p}_pwm"
	done

	echo 4000 > "$path_to_hwmon/pwm1_auto_point_temp_hyst"
	echo 2 > "$path_to_hwmon/pwm1_enable"

	[ "$(cat "$path_to_hwmon/pwm1_enable")" = 2 ]
	[ "$(cat "$path_to_hwmon/pwm1_freq")" = 23 ]
)

boot() {
	case $(board_name) in
	d-link,dgs-1210-28mp-f|\
	d-link,dgs-1210-28p-f)
		dlink_dgs_1210
		;;
	d-link,dgs-1250-28x)
		dlink_dgs_1250
		;;
	horaco,zx-swtgw2c48ns)
		# Duty 40 (the stock idle speed) from 45 C, full speed at 71.67 C;
		# the fan is not stable at lower duty.
		adt747x_fancontrol 1 40 45000 71670 85000 80000 1
		;;
	linksys,lgs328mpc-v2)
		linksys_lgs328mpc_v2
		;;
	linksys,lgs352c)
		linksys_lgs352c
		;;
	plasmacloud,esx28|\
	plasmacloud,psx28)
		# Lowest speed at 50 C, full speed at 70 C; critical at 80 C, as
		# per the datasheet.
		adt747x_fancontrol 2 1 50000 70000 80000 75000 0 22500
		;;
	esac
}
