// SPDX-License-Identifier: (GPL-2.0 OR MIT)
/*
 * Copyright (C) 2023 SmartRG Inc.
 * Author: Chad Monroe <chad.monroe@smartrg.com>
 */

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

/ {
	aliases {
		serial0 = &uart0;
		label-mac-device = &gmac1;
		led-boot = &led_sys_green;
		led-failsafe = &led_sys_blue;
		led-running = &led_sys_white;
		led-upgrade = &led_sys_red;
	};

	chosen {
		stdout-path = &uart0;
		bootargs = "console=ttyS0,115200n1 loglevel=8 pci=pcie_bus_perf root=PARTLABEL=rootfs";
	};

	memory {
		reg = <0x0 0x40000000 0x0 0x40000000>;
	};

	reserved-memory {
		#address-cells = <2>;
		#size-cells = <2>;
		ranges;

		/delete-node/ramoops@42ff0000;

		bootdata@45000000 {
			no-map;
			reg = <0x0 0x45000000 0x0 0x00001000>;
		};

		ramoops_reserved: ramoops@45001000 {
			no-map;
			compatible = "ramoops";
			reg = <0x0 0x45001000 0x0 0x00140000>;
			ftrace-size = <0x20000>;
			record-size = <0x20000>;
			console-size = <0x20000>;
			pmsg-size = <0x80000>;
		};
	};

	reg_1p8v: regulator-1p8v {
		compatible = "regulator-fixed";
		regulator-name = "fixed-1.8V";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		regulator-boot-on;
		regulator-always-on;
	};

	reg_3p3v: regulator-3p3v {
		compatible = "regulator-fixed";
		regulator-name = "fixed-3.3V";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-boot-on;
		regulator-always-on;
	};

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

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

		wps {
			label = "sync";
			linux,code = <KEY_WPS_BUTTON>;
			gpios = <&pio 14 GPIO_ACTIVE_LOW>;
		};
	};

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

		bluetooth_reset {
			gpio-export,name = "bt_reset";
			gpio-export,direction_may_change;
			gpios = <&pio 20 GPIO_ACTIVE_HIGH>;
		};

		bluetooth_txrx_ctl {
			gpio-export,name = "bt_txrx_ctl";
			gpio-export,direction_may_change;
			gpios = <&pio 74 GPIO_ACTIVE_HIGH>;
		};

		gps_enable {
			gpio-export,name = "gps_enable";
			gpio-export,output = <1>;
			gpios = <&pio 9 GPIO_ACTIVE_LOW>;
		};

		slic_interrupt {
			gpio-export,name = "slic_interrupt";
			gpio-export,direction_may_change;
			gpios = <&pio 54 GPIO_ACTIVE_HIGH>;
		};

		slic_reset {
			gpio-export,name = "slic_reset";
			gpio-export,output = <0>;
			gpios = <&pio 72 GPIO_ACTIVE_HIGH>;
		};

		usb_enable {
			gpio-export,name = "usb_enable";
			gpio-export,output = <1>;
			gpios = <&pio 63 GPIO_ACTIVE_HIGH>;
		};
	};
};

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

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

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

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

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

&cpu_thermal {
	/delete-node/cooling-maps;
	/delete-node/trips;

	trips {
		cpu_trip_crit: crit {
			temperature = <125000>;
			hysteresis = <2000>;
			type = "critical";
		};

		cpu_trip_hot: hot {
			temperature = <120000>;
			hysteresis = <2000>;
			type = "hot";
		};

		cpu_trip_active_high: active-high {
			temperature = <110000>;
			hysteresis = <2000>;
			type = "active";
		};

		cpu_trip_active_med: active-med {
			temperature = <80000>;
			hysteresis = <2000>;
			type = "active";
		};

		cpu_trip_active_low: active-low {
			temperature = <60000>;
			hysteresis = <2000>;
			type = "active";
		};

		cpu_trip_active_silent: active-silent {
			temperature = <40000>;
			hysteresis = <2000>;
			type = "active";
		};
	};

	cooling-maps {
		cpu-active-high {
			/* active: set fan to cooling level 3 */
			cooling-device = <&fan 3 3>;
			trip = <&cpu_trip_active_high>;
		};

		cpu-active-med {
			/* active: set fan to cooling level 2 */
			cooling-device = <&fan 2 2>;
			trip = <&cpu_trip_active_med>;
		};

		cpu-active-low {
			/* active: set fan to cooling level 1 */
			cooling-device = <&fan 1 1>;
			trip = <&cpu_trip_active_low>;
		};

		cpu-active-silent {
			/* active: set fan to cooling level 0 */
			cooling-device = <&fan 0 0>;
			trip = <&cpu_trip_active_silent>;
		};
	};
};

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

&fan {
	pwms = <&pwm 0 40000 0>;

	/**
	 * set fan speed
	 *
	 * 0 = off
	 * 61 = 24% duty cycle
	 * 77 = 30% duty cycle
	 * 102 = 40% duty cycle
	 * 128 - 50% duty cycle
	 * 255 = 100% duty cycle
	 */
	cooling-levels = <61 77 102 128>;

	interrupt-parent = <&pio>;
	interrupts = <21 IRQ_TYPE_EDGE_FALLING>;
	pulses-per-revolution = <2>;

	status = "okay";
};

&gmac0 {
	nvmem-cells = <&macaddr 1>;
	nvmem-cell-names = "mac-address";

	status = "okay";
};

&gmac1 {
	nvmem-cells = <&macaddr 0>;
	nvmem-cell-names = "mac-address";
	label = "wan";
	phy-mode = "usxgmii";

	status = "okay";
};

&gmac2 {
	nvmem-cells = <&macaddr 1>;
	nvmem-cell-names = "mac-address";
	label = "lan1";
	phy-mode = "usxgmii";
	phy-connection-type = "usxgmii";
	phy = <&phy8>;

	status = "okay";
};

&gsw_phy0 {
	status = "disabled";
};

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

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

&gsw_phy1_led1 {
	status = "okay";
	color = <LED_COLOR_ID_AMBER>;
};

&gsw_phy2 {
	pinctrl-names = "gbe-led";
	pinctrl-0 = <&gbe2_led0_pins>, <&gbe2_led1_pins>;
};

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

&gsw_phy2_led1 {
	status = "okay";
	color = <LED_COLOR_ID_AMBER>;
};

&gsw_phy3 {
	pinctrl-names = "gbe-led";
	pinctrl-0 = <&gbe3_led0_pins>, <&gbe3_led1_pins>;
};

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

&gsw_phy3_led1 {
	status = "okay";
	color = <LED_COLOR_ID_AMBER>;
};

&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;
			};
		};
	};
};

&i2c1 {
	pinctrl-names = "default";
	pinctrl-0 = <&i2c1_pins>;
	status = "okay";

	system-leds {
		compatible = "srg,sysled";
		reg = <0x30>;

		led_sys_red: system_red {
			label = "red";
			reg = <1>;
		};

		led_sys_green: system_green {
			label = "green";
			reg = <2>;
		};

		led_sys_blue: system_blue {
			label = "blue";
			reg = <3>;
		};

		led_sys_white: system_white {
			label = "white";
			reg = <4>;
		};
	};
};

&mdio_bus {
	phy0: ethernet-phy@0 {
		/* AQR113C */
		compatible = "ethernet-phy-ieee802.3-c45";
		reg = <0>;

		reset-gpios = <&pio 62 GPIO_ACTIVE_LOW>;
		reset-assert-us = <100000>;
		reset-deassert-us = <1000000>;

		leds {
			#address-cells = <1>;
			#size-cells = <0>;

			led@0 {
				reg = <0>;
				function = LED_FUNCTION_WAN;
				color = <LED_COLOR_ID_GREEN>;
			};

			led@1 {
				reg = <1>;
				function = LED_FUNCTION_WAN;
				color = <LED_COLOR_ID_ORANGE>;
			};

			led@2 {
				reg = <2>;
				function = LED_FUNCTION_WAN;
				color = <LED_COLOR_ID_WHITE>;
				active-high;
			};
		};
	};

	phy8: ethernet-phy@8 {
		/* AQR113C */
		compatible = "ethernet-phy-ieee802.3-c45";
		reg = <8>;

		reset-gpios = <&pio 71 GPIO_ACTIVE_LOW>;
		reset-assert-us = <100000>;
		reset-deassert-us = <1000000>;

		leds {
			#address-cells = <1>;
			#size-cells = <0>;

			aqr_green_led: led@0 {
				reg = <0>;
				function = LED_FUNCTION_LAN;
				color = <LED_COLOR_ID_GREEN>;
			};

			aqr_orange_led: led@1 {
				reg = <1>;
				function = LED_FUNCTION_LAN;
				color = <LED_COLOR_ID_ORANGE>;
			};

			aqr_white_led: led@2 {
				reg = <2>;
				function = LED_FUNCTION_LAN;
				color = <LED_COLOR_ID_WHITE>;
				active-high;
			};
		};
	};
};

&mmc0 {
	pinctrl-names = "default", "state_uhs";
	pinctrl-0 = <&mmc0_pins_emmc_51>;
	pinctrl-1 = <&mmc0_pins_emmc_51>;
	bus-width = <8>;
	max-frequency = <200000000>;
	cap-mmc-highspeed;
	mmc-hs200-1_8v;
	vmmc-supply = <&reg_3p3v>;
	vqmmc-supply = <&reg_1p8v>;
	non-removable;
	no-sd;
	no-sdio;
	status = "okay";
	#address-cells = <1>;
	#size-cells = <0>;

	card@0 {
		#address-cells = <0>;
		#size-cells = <0>;
		compatible = "mmc-card";
		reg = <0>;

		block {
			compatible = "block-device";

			partitions {
				block-partition-factory {
					partname = "factory";

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

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

				block-partition-mfginfo {
					partname = "mfginfo";

					nvmem-layout {
						compatible = "adtran,mfginfo";

						macaddr: mfg-mac {
							#nvmem-cell-cells = <1>;
						};
					};
				};
			};
		};
	};
};

&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>;

		mt7996@0,0 {
			reg = <0x0000 0 0 0 0>;
			nvmem-cells = <&eeprom_factory_0>;
			nvmem-cell-names = "eeprom";
			ieee80211-freq-limit = <2400000 2500000>, <5170000 5835000>, <5945000 7125000>;

			band@0 {
				/* 2.4 GHz */
				reg = <0>;
				nvmem-cells = <&macaddr 4>;
				nvmem-cell-names = "mac-address";
			};

			band@1 {
				/* 5 GHz */
				reg = <1>;
				nvmem-cells = <&macaddr 10>;
				nvmem-cell-names = "mac-address";
			};

			band@2 {
				/* 6 GHz */
				reg = <2>;
				nvmem-cells = <&macaddr 6>;
				nvmem-cell-names = "mac-address";
			};
		};
	};
};

&pcie1 {
	pinctrl-names = "default";
	pinctrl-0 = <&pcie1_pins>;
	status = "okay";
};

&pcie2 {
	status = "disabled";
};

&pcie3 {
	status = "disabled";
};

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

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

	pwm_pins: pwm-pins {
		mux {
			function = "pwm";
			groups = "pwm0", "pwm1";
		};
	};

	sfp_i2c_pins: sfp-i2c-pins {
		conf-scl {
			pins = "LED_A";
			drive-strength =  <8>;
			mediatek,pull-up-adv = <1>;
		};
		conf-sda {
			pins = "LED_E";
			drive-strength =  <8>;
			mediatek,pull-up-adv = <0>;
		};
	};

	uart1_pins: uart1-pins {
		mux {
			function = "uart";
			groups = "uart1_2";
		};
	};

	uart2_pins: uart2-pins {
		mux {
			function = "uart";
			groups = "uart2";
		};
	};
};

&pwm {
	pinctrl-names = "default";
	pinctrl-0 = <&pwm_pins>;
	status = "okay";
};

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

	spi_nand: spi_nand@0 {
		compatible = "spi-nand";
		reg = <0>;
		spi-max-frequency = <52000000>;
		spi-tx-buswidth = <4>;
		spi-rx-buswidth = <4>;
	};

};

&ssusb0 {
	status = "okay";
};

&ssusb1 {
	status = "okay";
};

&switch {
	status = "okay";

	ports {
		port@0 {
			status = "disabled";
		};

		port@1 {
			label = "lan2";
		};

		port@2 {
			label = "lan3";
		};

		port@3 {
			label = "lan4";
		};
	};
};

&tphy {
	status = "okay";
};

&uart0 {
	status = "okay";
};

&uart1 {
	pinctrl-names = "default";
	pinctrl-0 = <&uart1_pins>;
	status = "okay";

	/* Airoha AG3352 GPS */
};

&uart2 {
	pinctrl-names = "default";
	pinctrl-0 = <&uart2_pins>;
	status = "okay";

	/* DA14531MOD Bluetooth */
};

&watchdog {
	status = "okay";
};

&xphy {
	status = "okay";
};
