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

/dts-v1/;
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/input/input.h>
#include <dt-bindings/leds/common.h>
#include <dt-bindings/pinctrl/rockchip.h>
#include <dt-bindings/soc/rockchip,vop2.h>
#include "rk3568.dtsi"

/ {
	model = "NLnet XiGuaPi V3";
	compatible = "nlnet,xiguapi-v3", "rockchip,rk3568";

	aliases {
		ethernet0 = &gmac0;
		ethernet1 = &gmac1;
		mmc0 = &sdhci;
		mmc1 = &sdmmc0;

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

	chosen {
		stdout-path = "serial2:1500000n8";
	};

	adc-keys {
		compatible = "adc-keys";
		io-channels = <&saradc 0>;
		io-channel-names = "buttons";
		keyup-threshold-microvolt = <1750000>;

		button-reset {
			label = "reset";
			linux,code = <KEY_RESTART>;
			press-threshold-microvolt = <0>;
		};
	};

	hdmi-con {
		compatible = "hdmi-connector";
		type = "a";

		port {
			hdmi_con_in: endpoint {
				remote-endpoint = <&hdmi_out_con>;
			};
		};
	};

	gpio-leds {
		compatible = "gpio-leds";
		pinctrl-0 = <&sys_led>, <&net_led>;
		pinctrl-names = "default";

		net_led: net-led {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_WAN;
			gpios = <&gpio3 RK_PA4 GPIO_ACTIVE_HIGH>;
		};

		sys_led: sys-led {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_STATUS;
			gpios = <&gpio0 RK_PA0 GPIO_ACTIVE_HIGH>;
		};
	};

	led-controller {
		compatible = "pwm-leds";

		backlight: backlight-led {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_BACKLIGHT;
			pwms = <&pwm14 0 1000000 0>;
			max-brightness = <255>;
			linux,default-trigger = "default-on";
		};
	};

	fan: pwm-fan {
		compatible = "pwm-fan";
		#cooling-cells = <2>;
		cooling-levels = <30 75 135 195 255>;
		pwms = <&pwm7 0 40000 0>;
	};

	rfkill-modem {
		compatible = "rfkill-gpio";
		label = "M.2 USB Modem";
		radio-type = "wwan";
		reset-gpios = <&gpio2 RK_PD7 GPIO_ACTIVE_LOW>;
	};

	vcc0v6_ddr: regulator-vcc0v6-ddr {
		compatible = "regulator-fixed";
		regulator-name = "vcc0v6_ddr";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <600000>;
		regulator-max-microvolt = <600000>;
		vin-supply = <&vcc_sysin_b>;
	};

	vcc_3v3: regulator-vcc-3v3 {
		compatible = "regulator-fixed";
		regulator-name = "vcc_3v3";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
	};

	/* pi6c pcie clock generator */
	vcc3v3_pi6c: regulator-vcc3v3-pi6c {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio0 RK_PD4 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&pcie_en>;
		regulator-name = "vcc3v3_pi6c";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&vcc5v>;
	};

	vcc3v3_sd: regulator-vcc3v3-sd {
		compatible = "regulator-fixed";
		gpio = <&gpio0 RK_PA5 GPIO_ACTIVE_LOW>;
		regulator-name = "vcc3v3_sd";
		regulator-always-on;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&vcc_3v3>;
	};

	/* actually fed by vcc_3v3 */
	vcc_3v3_wifi: regulator-vcc-3v3-wifi {
		compatible = "regulator-fixed";
		regulator-name = "vcc_3v3_wifi";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&vcc3v3_pi6c>;
	};

	/* actually fed by bat+ */
	vcc_4v0_5g: regulator-vcc-4v0-5g {
		compatible = "regulator-fixed";
		regulator-name = "vcc_4v0_5g";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <4000000>;
		regulator-max-microvolt = <4000000>;
		vin-supply = <&vcc3v3_pi6c>;
	};

	vcc5v: regulator-vcc5v {
		compatible = "regulator-fixed";
		regulator-name = "vcc5v";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
	};

	vcc5v0_usb_otg0: regulator-vcc5v0-usb-otg0 {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio0 RK_PA6 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&vcc5v0_usb_otg_en>;
		regulator-name = "vcc5v0_usb_otg0";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc5v>;
	};

	/* sourced from vcc5v and bat+ via rk817 sw5 */
	vcc_sysin_b: regulator-vcc-sysin-b {
		compatible = "regulator-fixed";
		regulator-name = "vcc_sysin_b";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <4000000>;
		regulator-max-microvolt = <4000000>;
	};
};

&combphy0 {
	status = "okay";
};

&combphy1 {
	status = "okay";
};

&combphy2 {
	status = "okay";
};

&cpu0 {
	cpu-supply = <&vdd_cpu>;
};

&cpu1 {
	cpu-supply = <&vdd_cpu>;
};

&cpu2 {
	cpu-supply = <&vdd_cpu>;
};

&cpu3 {
	cpu-supply = <&vdd_cpu>;
};

&cpu_thermal {
	trips {
		cpu_low: cpu-low {
			temperature = <40000>;
			hysteresis = <5000>;
			type = "active";
		};
		cpu_warm: cpu-warm {
			temperature = <50000>;
			hysteresis = <5000>;
			type = "active";
		};
		cpu_hot: cpu-hot {
			temperature = <60000>;
			hysteresis = <3000>;
			type = "active";
		};
		cpu_hall: cpu-hall {
			temperature = <70000>;
			hysteresis = <2000>;
			type = "active";
		};
	};

	cooling-maps {
		map0 {
			trip = <&cpu_low>;
			cooling-device = <&fan 0 1>;
		};
		map1 {
			trip = <&cpu_warm>;
			cooling-device = <&fan 1 2>;
		};
		map2 {
			trip = <&cpu_hot>;
			cooling-device = <&fan 2 3>;
		};
		map3 {
			trip = <&cpu_hall>;
			cooling-device = <&fan 3 4>;
		};
	};
};

&gmac0 {
	assigned-clocks = <&cru SCLK_GMAC0_RX_TX>, <&cru SCLK_GMAC0>;
	assigned-clock-parents = <&cru SCLK_GMAC0_RGMII_SPEED>;
	assigned-clock-rates = <0>, <125000000>;
	clock_in_out = "output";
	phy-handle = <&rgmii_phy0>;
	phy-mode = "rgmii-id";
	pinctrl-names = "default";
	pinctrl-0 = <&gmac0_miim
		     &gmac0_tx_bus2
		     &gmac0_rx_bus2
		     &gmac0_rgmii_clk
		     &gmac0_rgmii_bus>;
	status = "okay";
};

&gmac1 {
	assigned-clocks = <&cru SCLK_GMAC1_RX_TX>, <&cru SCLK_GMAC1>;
	assigned-clock-parents = <&cru SCLK_GMAC1_RGMII_SPEED>;
	assigned-clock-rates = <0>, <125000000>;
	clock_in_out = "output";
	phy-handle = <&rgmii_phy1>;
	phy-mode = "rgmii-id";
	pinctrl-names = "default";
	pinctrl-0 = <&gmac1m1_miim
		     &gmac1m1_tx_bus2
		     &gmac1m1_rx_bus2
		     &gmac1m1_rgmii_clk
		     &gmac1m1_rgmii_bus>;
	status = "okay";
};

&gpu {
	mali-supply = <&vdd_gpu>;
	status = "okay";
};

&hdmi {
	avdd-0v9-supply = <&vdda0v9_image>;
	avdd-1v8-supply = <&vcca1v8_image>;
	status = "okay";
};

&hdmi_in {
	hdmi_in_vp0: endpoint {
		remote-endpoint = <&vp0_out_hdmi>;
	};
};

&hdmi_out {
	hdmi_out_con: endpoint {
		remote-endpoint = <&hdmi_con_in>;
	};
};

&hdmi_sound {
	status = "okay";
};

&i2c0 {
	status = "okay";

	vdd_cpu: regulator@1c {
		compatible = "tcs,tcs4525";
		reg = <0x1c>;
		fcs,suspend-voltage-selector = <1>;
		regulator-name = "vdd_cpu";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <800000>;
		regulator-max-microvolt = <1150000>;
		regulator-ramp-delay = <2300>;
		vin-supply = <&vcc_sysin_b>;

		regulator-state-mem {
			regulator-off-in-suspend;
		};
	};

	rk817: pmic@20 {
		compatible = "rockchip,rk817";
		reg = <0x20>;
		interrupt-parent = <&gpio0>;
		interrupts = <RK_PA3 IRQ_TYPE_LEVEL_LOW>;
		pinctrl-names = "default";
		pinctrl-0 = <&pmic_int>;
		system-power-controller;
		wakeup-source;
		#clock-cells = <1>;

		vcc1-supply = <&vcc_sysin_b>;
		vcc2-supply = <&vcc_sysin_b>;
		vcc3-supply = <&vcc_sysin_b>;
		vcc4-supply = <&vcc_sysin_b>;
		vcc5-supply = <&vcc_sysin_b>;
		vcc6-supply = <&vcc_sysin_b>;
		vcc7-supply = <&vcc_sysin_b>;
		vcc8-supply = <&vcc_sysin_b>;
		vcc9-supply = <&dcdc_boost>;

		regulators {
			vdd_logic: DCDC_REG1 {
				regulator-always-on;
				regulator-boot-on;
				regulator-initial-mode = <0x2>;
				regulator-min-microvolt = <500000>;
				regulator-max-microvolt = <1350000>;
				regulator-name = "vdd_logic";
				regulator-ramp-delay = <6001>;

				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vdd_gpu: DCDC_REG2 {
				regulator-always-on;
				regulator-initial-mode = <0x2>;
				regulator-min-microvolt = <500000>;
				regulator-max-microvolt = <1350000>;
				regulator-name = "vdd_gpu";
				regulator-ramp-delay = <6001>;

				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcc_ddr: DCDC_REG3 {
				regulator-always-on;
				regulator-boot-on;
				regulator-initial-mode = <0x2>;
				regulator-name = "vcc_ddr";

				regulator-state-mem {
					regulator-on-in-suspend;
				};
			};

			vcc3v3_sys: DCDC_REG4 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-initial-mode = <0x2>;
				regulator-name = "vcc3v3_sys";

				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcca1v8_pmu: LDO_REG1 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-name = "vcca1v8_pmu";

				regulator-state-mem {
					regulator-on-in-suspend;
				};
			};

			vdda_0v9: LDO_REG2 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <900000>;
				regulator-max-microvolt = <900000>;
				regulator-name = "vdda_0v9";

				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vdda0v9_pmu: LDO_REG3 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <900000>;
				regulator-max-microvolt = <900000>;
				regulator-name = "vdda0v9_pmu";

				regulator-state-mem {
					regulator-on-in-suspend;
				};
			};

			vccio_acodec: LDO_REG4 {
				regulator-always-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vccio_acodec";

				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vccio_sd: LDO_REG5 {
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vccio_sd";

				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcc3v3_pmu: LDO_REG6 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc3v3_pmu";

				regulator-state-mem {
					regulator-on-in-suspend;
				};
			};

			vcca_1v8: LDO_REG7 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-name = "vcca_1v8";

				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vdda0v9_image: LDO_REG8 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <900000>;
				regulator-max-microvolt = <900000>;
				regulator-name = "vdda0v9_image";

				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcca1v8_image: LDO_REG9 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-name = "vcca1v8_image";

				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			dcdc_boost: BOOST {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <4700000>;
				regulator-max-microvolt = <5400000>;
				regulator-name = "dcdc_boost";

				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			otg_switch: OTG_SWITCH {
				regulator-name = "otg_switch";

				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};
		};
	};
};

&i2c5 {
	status = "okay";
};

&mdio0 {
	rgmii_phy0: ethernet-phy@1 {
		compatible = "ethernet-phy-ieee802.3-c22";
		reg = <0x1>;
		pinctrl-names = "default";
		pinctrl-0 = <&eth_phy0_reset_pin>;
		reset-assert-us = <20000>;
		reset-deassert-us = <100000>;
		reset-gpios = <&gpio3 RK_PB7 GPIO_ACTIVE_LOW>;
	};
};

&mdio1 {
	rgmii_phy1: ethernet-phy@1 {
		compatible = "ethernet-phy-ieee802.3-c22";
		reg = <0x1>;
		pinctrl-names = "default";
		pinctrl-0 = <&eth_phy1_reset_pin>;
		reset-assert-us = <20000>;
		reset-deassert-us = <100000>;
		reset-gpios = <&gpio3 RK_PB0 GPIO_ACTIVE_LOW>;
	};
};

&pcie2x1 {
	reset-gpios = <&gpio3 RK_PC1 GPIO_ACTIVE_HIGH>;
	vpcie3v3-supply = <&vcc_3v3_wifi>;
	status = "okay";
};

&pcie30phy {
	phy-supply = <&vcc3v3_pi6c>;
	data-lanes = <1 2>;
	status = "okay";
};

&pcie3x1 {
	num-lanes = <1>;
	reset-gpios = <&gpio3 RK_PA1 GPIO_ACTIVE_HIGH>;
	vpcie3v3-supply = <&vcc_3v3_wifi>;
	status = "okay";
};

&pcie3x2 {
	num-lanes = <1>;
	reset-gpios = <&gpio2 RK_PD6 GPIO_ACTIVE_HIGH>;
	vpcie3v3-supply = <&vcc_3v3_wifi>;
	status = "okay";
};

&pinctrl {
	backlight {
		backlight_led_pin: backlight-led-pin {
			rockchip,pins = <3 RK_PA2 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	display {
		spi3_display_dc_pin: spi3-display-dc-pin {
			rockchip,pins = <4 RK_PC5 RK_FUNC_GPIO &pcfg_pull_none>;
		};
		display_reset_pin: display-reset-pin {
			rockchip,pins = <4 RK_PC4 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	leds {
		net_led_pin: net-led-pin {
			rockchip,pins = <3 RK_PA4 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		sys_led_pin: sys-led-pin {
			rockchip,pins = <0 RK_PA0 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	pcie {
		pcie_en: pcie-en {
			rockchip,pins = <0 RK_PD4 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	pmic {
		pmic_int: pmic-int {
			rockchip,pins = <0 RK_PA3 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

	spi3 {
		spi3_sck: spi3-sck {
			rockchip,pins = <4 RK_PC2 2 &pcfg_pull_none>;
		};
		spi3_mosi: spi3-mosi {
			rockchip,pins = <4 RK_PC3 2 &pcfg_pull_none>;
		};
	};

	usb {
		vcc5v0_usb_otg_en: vcc5v0-usb-otg-en {
			rockchip,pins = <0 RK_PA6 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	gmac0 {
		eth_phy0_reset_pin: eth-phy0-reset-pin {
			rockchip,pins = <3 RK_PB7 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

	gmac1 {
		eth_phy1_reset_pin: eth-phy1-reset-pin {
			rockchip,pins = <3 RK_PB0 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};
};

&pmu_io_domains {
	pmuio1-supply = <&vcc3v3_pmu>;
	pmuio2-supply = <&vcc3v3_pmu>;
	vccio1-supply = <&vccio_acodec>;
	vccio3-supply = <&vccio_sd>;
	vccio4-supply = <&vcca1v8_pmu>;
	vccio5-supply = <&vcc3v3_sys>;
	vccio6-supply = <&vcca1v8_pmu>;
	vccio7-supply = <&vcc3v3_sys>;
	status = "okay";
};

&pwm7 {
	pinctrl-0 = <&pwm7_pins>;
	pinctrl-names = "default";
	status = "okay";
};

&pwm14 {
	pinctrl-0 = <&pwm14m0_pins>;
	pinctrl-names = "default";
	status = "okay";
};

&saradc {
	vref-supply = <&vcca_1v8>;
	status = "okay";
};

&sdhci {
	bus-width = <8>;
	max-frequency = <200000000>;
	mmc-hs200-1_8v;
	non-removable;
	pinctrl-names = "default";
	pinctrl-0 = <&emmc_bus8 &emmc_clk &emmc_cmd &emmc_datastrobe>;
	status = "okay";
};

&sdmmc0 {
	bus-width = <4>;
	cap-sd-highspeed;
	cd-gpios = <&gpio0 RK_PA4 GPIO_ACTIVE_LOW>;
	disable-wp;
	pinctrl-names = "default";
	pinctrl-0 = <&sdmmc0_bus4 &sdmmc0_clk &sdmmc0_cmd &sdmmc0_det>;
	post-power-on-delay-ms = <800>;
	sd-uhs-sdr50;
	vmmc-supply = <&vcc3v3_sd>;
	vqmmc-supply = <&vccio_sd>;
	status = "okay";
};

&spi3 {
	pinctrl-names = "default";
	pinctrl-0 = <&spi3m1_cs0 &spi3_sck &spi3_mosi>;
	status = "okay";

	display@0 {
		compatible = "galaxycore,gc9307";
		reg = <0>;
		pinctrl-names = "default";
		pinctrl-0 = <&spi3_display_dc_pin &display_reset_pin>;
		dc-gpios = <&gpio4 RK_PC5 GPIO_ACTIVE_HIGH>;
		reset-gpios = <&gpio4 RK_PC4 GPIO_ACTIVE_LOW>;
		backlight = <&backlight>;
		buswidth = <8>;
		rotate = <270>;
	};
};

&tsadc {
	rockchip,hw-tshut-mode = <1>;
	rockchip,hw-tshut-polarity = <0>;
	status = "okay";
};

&uart2 {
	status = "okay";
};

&usb2phy0 {
	status = "okay";
};

&usb2phy0_host {
	phy-supply = <&vcc_4v0_5g>;
	status = "okay";
};

&usb2phy0_otg {
	phy-supply = <&vcc5v0_usb_otg0>;
	status = "okay";
};

&usb2phy1 {
	status = "okay";
};

&usb2phy1_host {
	phy-supply = <&vcc5v0_usb_otg0>;
	status = "okay";
};

&usb2phy1_otg {
	phy-supply = <&vcc5v0_usb_otg0>;
	status = "okay";
};

&usb_host0_ehci {
	status = "okay";
};

&usb_host0_ohci {
	status = "okay";
};

&usb_host0_xhci {
	dr_mode = "host";
	status = "okay";
};

&usb_host1_ehci {
	status = "okay";
};

&usb_host1_ohci {
	status = "okay";
};

&usb_host1_xhci {
	status = "okay";
};

&vop {
	assigned-clocks = <&cru DCLK_VOP0>, <&cru DCLK_VOP1>;
	assigned-clock-parents = <&pmucru PLL_HPLL>, <&cru PLL_VPLL>;
	status = "okay";
};

&vop_mmu {
	status = "okay";
};

&vp0 {
	vp0_out_hdmi: endpoint@ROCKCHIP_VOP2_EP_HDMI0 {
		reg = <ROCKCHIP_VOP2_EP_HDMI0>;
		remote-endpoint = <&hdmi_in_vp0>;
	};
};
