// SPDX-License-Identifier: GPL-2.0-or-later OR MIT

#include "mt7621.dtsi"

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/input/input.h>
#include <dt-bindings/leds/common.h>

/ {
	compatible = "tplink,ec330-g5u-v1", "mediatek,mt7621-soc";
	model = "TP-Link EC330-G5u v1";

	aliases {
		label-mac-device = &gmac0;

		led-boot = &led_power;
		led-failsafe = &led_power;
		led-running = &led_power;
		led-upgrade = &led_power;
	};

	chosen {
		bootargs = "console=ttyS0,115200";
	};

	leds {
		compatible = "gpio-leds";

		led-0 {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_USB;
			gpios = <&gpio 0 GPIO_ACTIVE_LOW>;
			trigger-sources = <&xhci_ehci_port1>;
			linux,default-trigger = "usbport";
		};

		led-1 {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_WPS;
			gpios = <&gpio 5 GPIO_ACTIVE_LOW>;
		};

		led-2 {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_LAN;
			gpios = <&gpio 9 GPIO_ACTIVE_LOW>;
		};

		led-3 {
			color = <LED_COLOR_ID_AMBER>;
			function = LED_FUNCTION_WAN;
			gpios = <&gpio 13 GPIO_ACTIVE_LOW>;
		};

		led-4 {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_WAN;
			gpios = <&gpio 14 GPIO_ACTIVE_LOW>;
		};

		led-5 {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_WLAN;
			function-enumerator = <50>;
			gpios = <&gpio 15 GPIO_ACTIVE_LOW>;
			linux,default-trigger = "phy1tpt";
		};

		led-6 {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_WLAN;
			function-enumerator = <24>;
			gpios = <&gpio 16 GPIO_ACTIVE_LOW>;
			linux,default-trigger = "phy0tpt";
		};

		led_power: led-7 {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_POWER;
			gpios = <&gpio 18 GPIO_ACTIVE_LOW>;
		};
	};

	keys {
		compatible = "gpio-keys";

		button-0 {
			label = "led";
			gpios = <&gpio 4 GPIO_ACTIVE_LOW>;
			debounce-interval = <60>;
			linux,code = <KEY_LIGHTS_TOGGLE>;
		};

		button-1 {
			label = "wifi on/off";
			gpios = <&gpio 7 GPIO_ACTIVE_LOW>;
			debounce-interval = <60>;
			linux,code = <KEY_RFKILL>;
		};

		button-2 {
			label = "reset";
			gpios = <&gpio 8 GPIO_ACTIVE_LOW>;
			debounce-interval = <60>;
			linux,code = <KEY_RESTART>;
		};

		button-3 {
			label = "wps";
			gpios = <&gpio 10 GPIO_ACTIVE_LOW>;
			debounce-interval = <60>;
			linux,code = <KEY_WPS_BUTTON>;
		};
	};

	gpio-export {
		compatible = "gpio-export";

		led-light {
			gpio-export,name = "led-light";
			gpio-export,output = <0>;
			gpios = <&gpio 3 GPIO_ACTIVE_LOW>;
		};
	};

	ubi-concat {
		compatible = "mtd-concat";
		devices = <&ubiconcat0 &ubiconcat1>;

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

			partition@0 {
				label = "ubi";
				reg = <0x0 0x3c00000>;
			};
		};
	};
};

&nand {
	status = "okay";

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

		partition@0 {
			label = "u-boot";
			reg = <0x0 0x400000>;
			read-only;

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

			partition@0 {
				label = "u-boot-first";
				reg = <0x0 0x20000>;
				read-only;
			};

			partition@20000 {
				label = "u-boot-main";
				reg = <0x20000 0x40000>;
				read-only;
			};

			partition@60000 {
				label = "u-boot-main-reserve";
				reg = <0x60000 0x40000>;
				read-only;
			};
		};

		partition@400000 {
			label = "os0";
			reg = <0x400000 0x3000000>;

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

			partition@0 {
				label = "kernel";
				reg = <0x0 0x400000>;
			};

			ubiconcat0: partition@400000 {
				label = "ubi-os0";
				reg = <0x400000 0x2c00000>;
			};
		};

		partition@3400000 {
			label = "os1";
			reg = <0x3400000 0x3000000>;
			read-only;
		};

		ubiconcat1: partition@6400000 {
			label = "userfs";
			reg = <0x6400000 0x1000000>;
		};

		partition@7400000 {
			label = "u-boot-env";
			reg = <0x7400000 0x400000>;
		};

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

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

				eeprom_factory_8000: eeprom@8000 {
					reg = <0x8000 0x4da8>;
				};

				eeprom_factory_14000: eeprom@14000 {
					reg = <0x14000 0x4da8>;
				};

				macaddr_factory_165: macaddr@165 {
					compatible = "mac-base";
					reg = <0x165 0x11>;
					#nvmem-cell-cells = <1>;
				};
			};
		};

		partition@0_wholeflash {
			label = "wholeflash";
			reg = <0x0 0x7f80000>;
			read-only;
		};
	};
};

&pcie {
	status = "okay";
};

&pcie0 {
	wifi@0,0 {
		compatible = "mediatek,mt76";
		reg = <0x0000 0 0 0 0>;
		ieee80211-freq-limit = <2400000 2500000>;
		nvmem-cells = <&eeprom_factory_8000>, <&macaddr_factory_165 0>;
		nvmem-cell-names = "eeprom", "mac-address";
	};
};

&pcie1 {
	wifi@0,0 {
		compatible = "mediatek,mt76";
		reg = <0x0000 0 0 0 0>;
		ieee80211-freq-limit = <5000000 6000000>;
		nvmem-cells = <&eeprom_factory_14000>, <&macaddr_factory_165 2>;
		nvmem-cell-names = "eeprom", "mac-address";
	};
};

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

&gmac1 {
	status = "okay";
	label = "wan";
	phy-handle = <&ethphy0>;

	nvmem-cells = <&macaddr_factory_165 1>;
	nvmem-cell-names = "mac-address";
};

&ethphy0 {
	/delete-property/ interrupts;
};

&switch0 {
	ports {
		port@1 {
			status = "okay";
			label = "lan1";
		};

		port@2 {
			status = "okay";
			label = "lan2";
		};

		port@3 {
			status = "okay";
			label = "lan3";
		};

		port@4 {
			status = "okay";
			label = "lan4";
		};
	};
};

&state_default {
	gpio {
		groups = "i2c", "jtag", "uart2", "uart3", "wdt";
		function = "gpio";
	};
};
