#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2013-2023 Igor Pecovnik, igor@armbian.com
#
# This file is a part of the Armbian Build Framework
# https://github.com/armbian/build/
#
source "${BASH_SOURCE%/*}/include/sunxi64_common.inc"

[[ -z $CPUMIN ]] && CPUMIN=480000
[[ -z $CPUMAX ]] && CPUMAX=1512000
GOVERNOR=ondemand

case $BRANCH in

	legacy)
		LINUXFAMILY=sun50iw9-btt
		KERNELSOURCE='https://github.com/bigtreetech/linux.git'
		declare -g KERNEL_MAJOR_MINOR="6.1" # Major and minor versions of this kernel.
		KERNELBRANCH="branch:linux-6.1.y-cb1"
		KERNELPATCHDIR="archive/sun50iw9-btt-6.1"
		BOOTSOURCE='https://github.com/bigtreetech/u-boot.git'
		BOOTBRANCH='branch:v2023.07-cb1'
		BOOTPATCHDIR='NEED-NOT'
		BOOTDELAY=-2
		ASOUND_STATE='asound.state.sun50iw9-legacy'

		ATFSOURCE='https://github.com/ARM-software/arm-trusted-firmware'
		ATFBRANCH='tag:lts-v2.10.2'
		ATF_PLAT="sun50i_h616"
		ATF_TARGET_MAP='PLAT=sun50i_h616 DEBUG=1 bl31;;build/sun50i_h616/debug/bl31.bin'
		BOOTSCRIPT='boot-sun50i-next.cmd:boot.cmd'
		KERNEL_DRIVERS_SKIP+=(driver_rtw88) # This is custom 6.1 driver, skip rtw88 to avoid patching failures
		;;

	current | edge)
		ATFSOURCE='https://github.com/ARM-software/arm-trusted-firmware'
		ATFBRANCH='tag:lts-v2.10.2'
		ATF_PLAT="sun50i_h616"
		ATF_TARGET_MAP='PLAT=sun50i_h616 DEBUG=1 bl31;;build/sun50i_h616/debug/bl31.bin'
		BOOTSCRIPT='boot-sun50i-next.cmd:boot.cmd'

		;;
esac

# This build requires busybox (and dos2unix)
function add_host_dependencies__sun50iw9_add_busybox_hostdep() {
	display_alert "Adding busybox dep" "for ${BOARD} bootloader compile" "debug"
	declare -g EXTRA_BUILD_DEPS="${EXTRA_BUILD_DEPS} busybox"
}

function family_tweaks_s() {
	if [[ -f $SDCARD/lib/systemd/system/hdmi-audio.service ]]; then
		chroot $SDCARD /bin/bash -c "systemctl --no-reload enable hdmi-audio.service >/dev/null 2>&1"
	fi
}

function family_tweaks_bsp() {
	install -m 755 $SRC/packages/blobs/sunxi/h616/inithdmiaudio $destination/usr/bin/
	cp $SRC/packages/blobs/sunxi/h616/libtinyalsa.so $destination/usr/lib/
	cp $SRC/packages/bsp/sunxi/hdmi-audio.service $destination/lib/systemd/system/

}
