# Reference: <https://postmarketos.org/vendorkernel>
# Mainline kernel + the (not-yet-upstream) Motorola rtwo device tree.
# Config base: linux-postmarketos-mainline's config-mainline.aarch64.
maintainer="snlavaise <snlavaise@gmail.com>"
_flavor="postmarketos-qcom-sm8550"
pkgname=linux-$_flavor
pkgver=7.2
pkgrel=49
pkgdesc="Mainline kernel for Qualcomm SM8550 (Snapdragon 8 Gen 2) + Motorola rtwo"
arch="aarch64"
_carch="arm64"
url="https://kernel.org"
license="GPL-2.0-only"
options="
	!check
	!strip
	!tracedeps
	pmb:cross-native
	pmb:kconfigcheck-community
"
makedepends="
	bash
	bison
	clang
	diffutils
	elfutils-dev
	findutils
	flex
	gmp-dev
	kmod
	linux-headers
	lld
	llvm
	mpc1-dev
	mpfr-dev
	openssl-dev
	pahole
	perl
	postmarketos-installkernel
	python3
	xz
	zstd
"

source="
	$pkgname-$pkgver.tar.gz::https://git.kernel.org/torvalds/t/linux-${pkgver/_/-}.tar.gz
	config-motorola-rtwo.aarch64
	rtwo.config
	sm8550-motorola-rtwo.dts
	ath12k-fix-regd-update-completion-hang.patch
	ath12k-fix-regd-range-wideband.patch
	icc-sm8550-keepalive-mm0-mdp-fetch.patch
	ath12k-htc-log-every-rx-header.patch
	ath12k-mhi-keep-awake.patch
	msm-dsi-keep-panel-dsc-config.patch
	msm-dp-ignore-invalid-lttpr-caps.patch
	msm-dp-no-push-idle-when-link-down.patch
	panel-novatek-nt37705.c
	typec-mux-ps5169.c
"
builddir="$srcdir/linux-${pkgver/_/-}"

prepare() {
	default_prepare

	# The Motorola rtwo (Moto Edge 40 Pro) device tree is not upstream yet.
	local _dtsdir="arch/$_carch/boot/dts/qcom"
	cp "$srcdir/sm8550-motorola-rtwo.dts" "$_dtsdir/sm8550-motorola-rtwo.dts"
	if ! grep -q "sm8550-motorola-rtwo.dtb" "$_dtsdir/Makefile"; then
		sed -i '/sm8550-mtp\.dtb/a dtb-$(CONFIG_ARCH_QCOM) += sm8550-motorola-rtwo.dtb' \
			"$_dtsdir/Makefile"
	fi
	# Not-yet-upstream panel driver (DSI command mode + DSC, Motorola rtwo).
	cp "$srcdir/panel-novatek-nt37705.c" \
		drivers/gpu/drm/panel/panel-novatek-nt37705.c
	if ! grep -q "CONFIG_DRM_PANEL_NOVATEK_NT37705" drivers/gpu/drm/panel/Kconfig; then
		cat >> drivers/gpu/drm/panel/Kconfig <<'EOK'

config DRM_PANEL_NOVATEK_NT37705
	tristate "Novatek NT37705 DSI panel"
	depends on OF
	depends on DRM_MIPI_DSI
	depends on BACKLIGHT_CLASS_DEVICE
	select DRM_DISPLAY_DSC_HELPER
	select DRM_DISPLAY_HELPER
	help
	  Say Y here if you want to enable support for Novatek NT37705 DSI
	  panel module. 1080x2400 command mode DSC panel from CSOT, used on
	  the Motorola rtwo.
EOK
	fi
	if ! grep -q "panel-novatek-nt37705" drivers/gpu/drm/panel/Makefile; then
		echo 'obj-$(CONFIG_DRM_PANEL_NOVATEK_NT37705) += panel-novatek-nt37705.o' \
			>> drivers/gpu/drm/panel/Makefile
	fi
	# Not-yet-upstream Parade PS5169 USB-C redriver (DisplayPort alt mode).
	cp "$srcdir/typec-mux-ps5169.c" drivers/usb/typec/mux/ps5169.c
	if ! grep -q "CONFIG_TYPEC_MUX_PS5169" drivers/usb/typec/mux/Kconfig; then
		cat >> drivers/usb/typec/mux/Kconfig <<'EOK'

config TYPEC_MUX_PS5169
	tristate "Parade PS5169 Type-C redriver driver"
	depends on I2C
	depends on DRM || DRM=n
	select DRM_AUX_BRIDGE if DRM_BRIDGE && OF
	select REGMAP_I2C
	help
	  Say Y or M if your system has a Parade PS5169 USB/DP Type-C
	  redriver, as found on the Motorola rtwo.
EOK
	fi
	if ! grep -q "ps5169" drivers/usb/typec/mux/Makefile; then
		echo 'obj-$(CONFIG_TYPEC_MUX_PS5169) += ps5169.o' \
			>> drivers/usb/typec/mux/Makefile
	fi
	# Build this DTB with -@ so it gets a /__symbols__ node. The Motorola
	# SM8550 ABL runs its dtbo-apply path (libufdt) against the base DTB
	# and drops to the bootloader menu if the symbols are missing.
	if ! grep -q "DTC_FLAGS_sm8550-motorola-rtwo" "$_dtsdir/Makefile"; then
		echo 'DTC_FLAGS_sm8550-motorola-rtwo := -@' >> "$_dtsdir/Makefile"
	fi

	# Base config (from linux-postmarketos-mainline) + rtwo bring-up fragment.
	cp "$srcdir/config-motorola-rtwo.aarch64" .config
	cat "$srcdir/rtwo.config" >> .config
	make ARCH="$_carch" LLVM=1 olddefconfig
}

build() {
	unset LDFLAGS
	make ARCH="$_carch" LLVM=1 \
		KBUILD_BUILD_VERSION="$((pkgrel + 1))-$_flavor"
}

package() {
	# Raw uncompressed Image for the Android boot.img: the stock rtwo
	# LineageOS boot.img carries a raw 'MZ' Image (BOARD_KERNEL_IMAGE_NAME
	# := Image), and CONFIG_EFI_ZBOOT is off in rtwo.config.
	install -Dm644 "$builddir/arch/$_carch/boot/Image" \
		"$pkgdir/boot/vmlinuz"

	make modules_install dtbs_install \
		ARCH="$_carch" \
		LLVM=1 \
		INSTALL_MOD_PATH="$pkgdir"/usr \
		INSTALL_MOD_STRIP=1 \
		INSTALL_DTBS_PATH="$pkgdir"/boot/dtbs

	# All-zero dtbo image (full partition size), same trick as
	# device-motorola-dubai: the bootloader finds no valid DT-overlay
	# table magic and applies nothing, so the stock kalama overlay does
	# not get merged onto our mainline base DTB.
	mkdir -p "$pkgdir"/boot
	dd if=/dev/zero of="$pkgdir/boot/dtbo.img" bs=1M count=24 status=none

	rm -f "$pkgdir"/usr/lib/modules/*/build "$pkgdir"/usr/lib/modules/*/source

	install -Dm644 "$builddir"/include/config/kernel.release \
		"$pkgdir"/usr/share/kernel/"$_flavor"/kernel.release
}

# 'pmbootstrap checksum linux-postmarketos-qcom-sm8550' fills in the tarball hash
# (same tarball as linux-postmarketos-mainline, ~150 MB download).
sha512sums="
52d5aa7f94f6aa6141debf4ceb4fc7a2e75505a7cd38b8854e501be83c2b57863020f463f2fdb2fc93667e6cfe13274d077e5207a611e4797bd9404ee0e64c58  linux-postmarketos-qcom-sm8550-7.2.tar.gz
0ab844d874289be5529c7f83a1bdd16d867ea6d5f3f873556779cbd27908316fd5076c201d6b6fee40991da648eadc982422cc0136fe2c9034b7b4846e08a028  config-motorola-rtwo.aarch64
a03843765d996a4555f991b277dd9cc35f1c96da1b740330a3497202d355ec95406f380a0ec6a778d638bf9acfc1f28dbc441e156fb75b1cf00720af136f69b8  rtwo.config
ff2da1726b87210497d3d3961af5d95b9dfb160d48af90bb604dbb321b89c50689a06722c50db265aaf5c95b0078da278939efda8e1a8f30232827d27da1c8ca  sm8550-motorola-rtwo.dts
a068e35a031858161e7a5ac00d7d2db9fd0d842fc19d3ebf08b03275ef470eff5462921e36390f535e2556619b6911192b6e2ccf7c313edafaf22d6da44c1d8e  ath12k-fix-regd-update-completion-hang.patch
891b9b15e9113488667f2dcf9f1502aab24291228cc4096f358acbf4b67d299fb90b2d00b4e103139cd2160339a10833cddb781838d1f03721fef07ea45b3213  ath12k-fix-regd-range-wideband.patch
acbe599c08877065ea4419363da7ab75a16a557aad4b13161f22270f03b27efac1be487884896900c3e80d6df8e3545ca6b1df940d24badef1825fe0d27147ac  icc-sm8550-keepalive-mm0-mdp-fetch.patch
cf4889c4dca8d3bac7544d71284e6b592d6014d9ad94badfc6d2cbe56a8402c61e4c0811c2f48d3352b446de9fbcf36b58226431c9276956d7ff3fe08c9eb589  ath12k-htc-log-every-rx-header.patch
6e01370d013278faba203e57ba20700d500639136672c040c0924a600fc3dcd8185a9f558ee7bf8a9ad5412113f6b6ab1901423006bb839e808dbcb151a16383  ath12k-mhi-keep-awake.patch
7673bec5661e692721bce41e72a330180e653d2b1b0a5cb3d36bdc869ad4a32ce23a6048d01c9cf2cf5c78c2a7e33899a0a149c65e9754d9d1c2cd71b542b84c  msm-dsi-keep-panel-dsc-config.patch
3268835bcb539a77d16c888d933c856df6bb591861d30c7c61c6e0ab70751bac6248961ec30fedf9a62bebdcd63113cf1dd14e58a1b53262493d5e0e90dbbe3f  msm-dp-ignore-invalid-lttpr-caps.patch
4f5ea9bfe01a6df166fdad601f6bd74d4b39049be924e8b4df58a8acbba7e9ebfc568bd81df70354209642f68e113804b9d1b62cbd3c83c319fae5fefcf07595  msm-dp-no-push-idle-when-link-down.patch
f2ec70d834577378c564e8cab51879f14815693a04af5382b3a778a598a1b3c86585ff9dadf004fa526920778d6540cc44db769a4f75c2af883dcf52c2b70af8  panel-novatek-nt37705.c
dd95e80f62879d5167e7bfc7d74b4a64669a43dbf5aacb6a13e62d1ed4a88677ef1042f8cecc46e05e3d12ba75f070a5927df308c90fe846af93b78c6b310e80  typec-mux-ps5169.c
"
