#!/bin/sh
# LCKFB TaishanPi 3M: detect which official MIPI DSI screen is attached
# and enable the matching device tree overlay.
#
# All official screens put their touch controller on I2C0 (enabled in the
# base device tree for this): the 10.1in panels a Goodix GT9271/GT911 at
# 0x5d, the 3.1in panel a Hynitron CST128 at 0x38. The two 10.1in panels
# are told apart by the resolution stored in the Goodix configuration.
#
# Only the dsi-* entry of "overlays=" in armbianEnv.txt is changed. If the
# result differs from the current setting, the file is updated and the
# board reboots once. Set SCREEN_AUTODETECT=no in
# /etc/default/lckfb-tspi-3m-screen to choose the overlay by hand.

ENV=/boot/armbianEnv.txt
STATE=/var/lib/lckfb-tspi-3m-screen
BUS=0
SCREEN_AUTODETECT=yes
[ -r /etc/default/lckfb-tspi-3m-screen ] && . /etc/default/lckfb-tspi-3m-screen
[ "$SCREEN_AUTODETECT" = yes ] || exit 0
[ -w "$ENV" ] || exit 0
command -v i2ctransfer > /dev/null || exit 0

goodix_res() {
	# product id at 0x8140 ("9271", "911"...), resolution at 0x8048..0x804b
	i2ctransfer -f -y $BUS w2@0x5d 0x81 0x40 r4 > /dev/null 2>&1 || return 1
	set -- $(i2ctransfer -f -y $BUS w2@0x5d 0x80 0x48 r4 2>/dev/null) || return 1
	[ $# -eq 4 ] || return 1
	echo $(( $1 | ($2 << 8) ))x$(( $3 | ($4 << 8) ))
}

detect() {
	want=""
	if res=$(goodix_res); then
		case "$res" in
			1200x1920) want=dsi-1200x1920-10-1inch ;;
			*)         want=dsi-c41409463 ;;
		esac
	elif i2ctransfer -f -y $BUS r1@0x38 > /dev/null 2>&1; then
		want=dsi-c42388916
	fi
}

cur=$(sed -n 's/^overlays=//p' "$ENV" | tail -n1)
others=""
have=""
for o in $cur; do
	case "$o" in
		dsi-*) have="$o" ;;
		*) others="$others $o" ;;
	esac
done

# Finding nothing removes a configured overlay, and with it a working
# display. One failed I2C transfer early in boot must not be enough for
# that, so ask again before believing it.
detect
if [ -z "$want" ] && [ -n "$have" ]; then
	for try in 1 2 3; do
		sleep 1
		detect
		[ -n "$want" ] && break
	done
fi
echo "lckfb-tspi-3m-screen: detected '${want:-none}', configured '${have:-none}'"
[ "$want" = "$have" ] && { rm -f "$STATE"; exit 0; }

# Reboot at most once for the same decision, so a screen the detection
# gets wrong cannot cause a reboot loop.
if [ -f "$STATE" ] && [ "$(cat "$STATE")" = "$want" ]; then
	echo "lckfb-tspi-3m-screen: already tried '${want:-none}', leaving it alone"
	exit 0
fi

new=$(echo "$others ${want}" | xargs)
# Record the decision only once the file really says so. Otherwise a failed
# write would be remembered as "already tried" and never attempted again.
if grep -q '^overlays=' "$ENV"; then
	sed -i "s/^overlays=.*/overlays=$new/" "$ENV"
else
	echo "overlays=$new" >> "$ENV"
fi
if [ "$(sed -n 's/^overlays=//p' "$ENV" | tail -n1)" != "$new" ]; then
	echo "lckfb-tspi-3m-screen: could not update $ENV, not rebooting"
	exit 1
fi
mkdir -p "$(dirname "$STATE")"
echo "$want" > "$STATE"
sync
echo "lckfb-tspi-3m-screen: overlays set to '$new', rebooting"
systemctl --no-block reboot
