// SPDX-License-Identifier: GPL-2.0-or-later OR MIT
/*
 * Copyright (C) 2025 Tianling Shen <cnsztl@immortalwrt.org>
 */

/dts-v1/;
#include <dt-bindings/input/input.h>
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/leds/common.h>
#include <dt-bindings/pinctrl/mt65xx.h>
#include <dt-bindings/regulator/richtek,rt5190a-regulator.h>

#include "mt7988a.dtsi"

/ {
	aliases {
		led-boot = &led_status_red;
		led-failsafe = &led_status_red;
		led-running = &led_status_green;
		led-upgrade = &led_status_green;
		label-mac-device = &gmac0;
		serial0 = &uart0;
	};

	chosen {
		rootdisk = <&ubi_rootdisk>;
		stdout-path = "serial0:115200n8";
	};

	cpus {
		/delete-node/ cpu@3;
	};

	memory@40000000 {
		reg = <0 0x40000000 0 0x20000000>;
	};

	gpio-keys {
		compatible = "gpio-keys";
		pinctrl-names = "default";
		pinctrl-0 = <&button_pins>;

		button-mesh {
			label = "mesh";
			linux,code = <BTN_9>;
			gpios = <&pio 14 GPIO_ACTIVE_LOW>;
		};

		button-reset {
			label = "reset";
			linux,code = <KEY_RESTART>;
			gpios = <&pio 13 GPIO_ACTIVE_LOW>;
		};
	};

	gpio-leds {
		compatible = "gpio-leds";

		led_status_red: led-0 {
			color = <LED_COLOR_ID_RED>;
			function = LED_FUNCTION_STATUS;
			gpios = <&pio 53 GPIO_ACTIVE_HIGH>;
		};

		led_status_green: led-1 {
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_STATUS;
			gpios = <&pio 54 GPIO_ACTIVE_HIGH>;
		};
	};
};

&cpu0 {
	proc-supply = <&rt5190_buck3>;
};

&cpu1 {
	proc-supply = <&rt5190_buck3>;
};

&cpu2 {
	proc-supply = <&rt5190_buck3>;
};

&cci {
	proc-supply = <&rt5190_buck3>;
};

&eth {
	pinctrl-names = "default";
	pinctrl-0 = <&mdio0_pins>;
	status = "okay";
};

&gmac0 {
	nvmem-cells = <&macaddr_factory_1e001c 0>;
	nvmem-cell-names = "mac-address";
	status = "okay";
};

&gmac1 {
	phy-mode = "internal";
	phy-connection-type = "internal";
	phy = <&int_2p5g_phy>;
	status = "okay";
};

&gsw_phy0 {
	pinctrl-names = "gbe-led";
	pinctrl-0 = <&gbe0_led0_pins>;
};

&gsw_phy0_led0 {
	color = <LED_COLOR_ID_GREEN>;
	status = "okay";
};

&gsw_phy1 {
	pinctrl-names = "gbe-led";
	pinctrl-0 = <&gbe1_led0_pins>;
};

&gsw_phy1_led0 {
	color = <LED_COLOR_ID_GREEN>;
	status = "okay";
};

&i2c0 {
	pinctrl-names = "default";
	pinctrl-0 = <&i2c0_pins>;
	status = "okay";

	rt5190a_64: rt5190a@64 {
		compatible = "richtek,rt5190a";
		reg = <0x64>;
		/*interrupts-extended = <&gpio26 0 IRQ_TYPE_LEVEL_LOW>;*/
		vin2-supply = <&rt5190_buck1>;
		vin3-supply = <&rt5190_buck1>;
		vin4-supply = <&rt5190_buck1>;

		regulators {
			rt5190_buck1: buck1 {
				regulator-name = "rt5190a-buck1";
				regulator-min-microvolt = <5090000>;
				regulator-max-microvolt = <5090000>;
				regulator-allowed-modes = <RT5190A_OPMODE_AUTO RT5190A_OPMODE_FPWM>;
				regulator-boot-on;
				regulator-always-on;
			};

			buck2 {
				regulator-name = "vcore";
				regulator-min-microvolt = <600000>;
				regulator-max-microvolt = <1400000>;
				regulator-boot-on;
				regulator-always-on;
			};

			rt5190_buck3: buck3 {
				regulator-name = "vproc";
				regulator-min-microvolt = <600000>;
				regulator-max-microvolt = <1400000>;
				regulator-boot-on;
			};

			buck4 {
				regulator-name = "rt5190a-buck4";
				regulator-min-microvolt = <850000>;
				regulator-max-microvolt = <850000>;
				regulator-allowed-modes = <RT5190A_OPMODE_AUTO RT5190A_OPMODE_FPWM>;
				regulator-boot-on;
				regulator-always-on;
			};

			ldo {
				regulator-name = "rt5190a-ldo";
				regulator-min-microvolt = <1200000>;
				regulator-max-microvolt = <1200000>;
				regulator-boot-on;
				regulator-always-on;
			};
		};
	};
};

&i2p5gbe_led0 {
	color = <LED_COLOR_ID_GREEN>;
	status = "okay";
};

&int_2p5g_phy {
	pinctrl-names = "i2p5gbe-led";
	pinctrl-0 = <&i2p5gbe_led0_pins>;
};

&pcie0 {
	pinctrl-names = "default";
	pinctrl-0 = <&pcie0_1_pins>;
	reset-gpios = <&pio 7 GPIO_ACTIVE_LOW>;
	status = "okay";

	pcie@0,0 {
		reg = <0x0000 0 0 0 0>;
		#address-cells = <3>;
		#size-cells = <2>;

		mt7992@0,0 {
			reg = <0x0000 0 0 0 0>;
			#address-cells = <1>;
			#size-cells = <0>;
			nvmem-cells = <&eeprom_factory_0>;
			nvmem-cell-names = "eeprom";

			band@0 {
				reg = <0>;
				nvmem-cells = <&macaddr_factory_1e001c 0>;
				nvmem-cell-names = "mac-address";
			};

			/* FIXME: this does not work */
			band@1 {
				reg = <1>;
				nvmem-cells = <&macaddr_factory_1e001c 2>;
				nvmem-cell-names = "mac-address";
			};
		};
	};
};

&pio {
	button_pins: button-pins {
		pins = "GPIO_WPS", "GPIO_RESET";
		mediatek,pull-down-adv = <0>;
	};

	pcie0_1_pins: pcie0-pins-g1 {
		mux {
			function = "pcie";
			groups = "pcie_2l_0_pereset", "pcie_clk_req_n0_0";
		};
	};
};

&spi0 {
	pinctrl-names = "default";
	pinctrl-0 = <&spi0_flash_pins>;
	status = "okay";

	spi_nand: flash@0 {
		compatible = "spi-nand";
		reg = <0>;
		#address-cells = <1>;
		#size-cells = <1>;

		spi-max-frequency = <52000000>;
		spi-tx-bus-width = <4>;
		spi-rx-bus-width = <4>;

		partitions {
			compatible = "fixed-partitions";
			#address-cells = <1>;
			#size-cells = <1>;

			partition@0 {
				label = "bl2";
				reg = <0x0 0x100000>;
				read-only;
			};

			partition@100000 {
				label = "u-boot-env";
				reg = <0x100000 0x80000>;
				read-only;
			};

			partition@180000 {
				label = "factory";
				reg = <0x180000 0x400000>;
				read-only;

				nvmem-layout {
					compatible = "fixed-layout";
					#address-cells = <1>;
					#size-cells = <1>;

					eeprom_factory_0: eeprom@0 {
						reg = <0x0 0x1e00>;
					};

					macaddr_factory_1e001c: eeprom@1e001c {
						compatible = "mac-base";
						reg = <0x1e001c 0x6>;
						#nvmem-cell-cells = <1>;
					};
				};
			};

			partition@580000 {
				label = "fip";
				reg = <0x580000 0x200000>;
				read-only;
			};

			partition@780000 {
				compatible = "linux,ubi";
				reg = <0x780000 0x7080000>;
				label = "ubi";

				volumes {
					ubi_rootdisk: ubi-volume-fit {
						volname = "fit";
					};
				};
			};
		};
	};
};

&switch {
	status = "okay";
};

&uart0 {
	status = "okay";
};

&watchdog {
	status = "okay";
};
