// SPDX-License-Identifier: (GPL-2.0+ OR MIT)

/dts-v1/;

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/input/input.h>
#include <dt-bindings/interrupt-controller/irq.h>
#include <dt-bindings/leds/common.h>
#include <dt-bindings/pinctrl/rockchip.h>
#include <dt-bindings/pwm/pwm.h>
#include <dt-bindings/soc/rockchip,vop2.h>

#include "rk3588s-orangepi-cm5.dtsi"

/ {
	model = "Xunlong Orange Pi CM5 Base";
	compatible = "xunlong,orangepi-cm5-base", "xunlong,orangepi-cm5", "rockchip,rk3588s";

	aliases {
		ethernet0 = &gmac1;
		mmc1 = &sdmmc;
	};

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

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

		button {
			debounce-interval = <50>;
			gpios = <&gpio3 RK_PC6 GPIO_ACTIVE_LOW>;
			label = "USERKEY";
			linux,code = <BTN_MISC>;
			wakeup-source;
		};
	};

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

		port {
			hdmi0_con_in: endpoint {
				remote-endpoint = <&hdmi0_out_con>;
			};
		};
	};

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

		led-1 {
			color = <LED_COLOR_ID_RED>;
			function = LED_FUNCTION_STATUS;
			linux,default-trigger = "heartbeat";
			max-brightness = <255>;
			pwms = <&pwm2 0 25000 0>;
		};

		led-2 {
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_WAN;
			max-brightness = <255>;
			pwms = <&pwm4 0 25000 PWM_POLARITY_INVERTED>;
		};

		led-3 {
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_LAN;
			function-enumerator = <0>;
			max-brightness = <255>;
			pwms = <&pwm5 0 25000 PWM_POLARITY_INVERTED>;
		};

		led-4 {
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_LAN;
			function-enumerator = <1>;
			max-brightness = <255>;
			pwms = <&pwm6 0 25000 0>;
		};
	};

	vbus_5v0: regulator-vbus-5v0 {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio0 RK_PD3 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&vbus_5v0_en_pin>;
		regulator-name = "vbus_5v0";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc5v0_sys>;
	};

	vcc_3v3: regulator-vcc-3v3 {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio4 RK_PA3 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&vcc_3v3_en_pin>;
		regulator-name = "vcc_3v3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		startup-delay-us = <50000>;
		vin-supply = <&vcc5v0_sys>;
	};

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

&combphy0_ps {
	status = "okay";
};

&combphy2_psu {
	status = "okay";
};

&gmac1 {
	clock_in_out = "output";
	phy-handle = <&rgmii_phy>;
	phy-mode = "rgmii-id";
	phy-supply = <&vcc_3v3>;
	pinctrl-names = "default";
	pinctrl-0 = <&gmac1_miim
		     &gmac1_rx_bus2
		     &gmac1_tx_bus2
		     &gmac1_rgmii_clk
		     &gmac1_rgmii_bus>;
	status = "okay";
};

&hdmi0 {
	frl-enable-gpios = <&gpio4 RK_PB5 GPIO_ACTIVE_LOW>;
	pinctrl-0 = <&hdmim0_tx0_cec &hdmim0_tx0_hpd
		     &hdmim0_tx0_scl &hdmim0_tx0_sda
		     &hdmi0_tx_on_h>;
	pinctrl-names = "default";
	status = "okay";
};

&hdmi0_in {
	hdmi0_in_vp0: endpoint {
		remote-endpoint = <&vp0_out_hdmi0>;
	};
};

&hdmi0_out {
	hdmi0_out_con: endpoint {
		remote-endpoint = <&hdmi0_con_in>;
	};
};

&hdmi0_sound {
	status = "okay";
};

&hdptxphy0 {
	status = "okay";
};

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

	rtc@51 {
		compatible = "haoyu,hym8563";
		reg = <0x51>;
		#clock-cells = <0>;
		clock-output-names = "hym8563";
		interrupt-parent = <&gpio0>;
		interrupts = <RK_PB0 IRQ_TYPE_LEVEL_LOW>;
		pinctrl-names = "default";
		pinctrl-0 = <&rtc_int_pin>;
		wakeup-source;
	};
};

&i2s5_8ch {
	status = "okay";
};

&mdio1 {
	rgmii_phy: ethernet-phy@1 {
		/* YT8531C */
		compatible = "ethernet-phy-ieee802.3-c22";
		reg = <0x1>;
		pinctrl-names = "default";
		pinctrl-0 = <&rgmii_phy_pin>;
		reset-assert-us = <20000>;
		reset-deassert-us = <100000>;
		reset-gpios = <&gpio1 RK_PC1 GPIO_ACTIVE_LOW>;
	};
};

&pcie2x1l1 {
	reset-gpios = <&gpio4 RK_PA2 GPIO_ACTIVE_HIGH>;
	vpcie3v3-supply = <&vcc_3v3>;
	status = "okay";
};

&pcie2x1l2 {
	reset-gpios = <&gpio3 RK_PD1 GPIO_ACTIVE_HIGH>;
	vpcie3v3-supply = <&vcc_3v3>;
	status = "okay";
};

&pinctrl {
	camera {
		cam1_reset_pin: cam1-reset-pin {
			rockchip,pins = <1 RK_PD0 RK_FUNC_GPIO &pcfg_pull_none>;
		};
		cam2_reset_pin: cam2-reset-pin {
			rockchip,pins = <1 RK_PD1 RK_FUNC_GPIO &pcfg_pull_none>;
		};
		cam3_reset_pin: cam3-reset-pin {
			rockchip,pins = <1 RK_PD2 RK_FUNC_GPIO &pcfg_pull_none>;
		};
		cam4_reset_pin: cam4-reset-pin {
			rockchip,pins = <1 RK_PD3 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	ethernet {
		rgmii_phy_pin: rgmii-phy-pin {
			rockchip,pins = <1 RK_PC1 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	gpio-key {
		key1_pin: key1-pin {
			rockchip,pins = <3 RK_PC6 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

	hdmi {
		hdmi0_tx_on_h: hdmi0-tx-on-h {
			rockchip,pins = <4 RK_PB5 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	power {
		vcc_3v3_en_pin: vcc-3v3-en-pin {
			rockchip,pins = <4 RK_PA3 RK_FUNC_GPIO &pcfg_pull_down>;
		};
	};

	rtc {
		rtc_int_pin: rtc-int-pin {
			rockchip,pins = <0 RK_PB0 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	usb {
		vbus_5v0_en_pin: vbus-5v0-en-pin {
			rockchip,pins = <0 RK_PD3 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};
};

&pwm2 {
	status = "okay";
};

&pwm4 {
	status = "okay";
};

&pwm5 {
	pinctrl-0 = <&pwm5m1_pins>;
	pinctrl-names = "default";
	status = "okay";
};

&pwm6 {
	status = "okay";
};

&sdmmc {
	bus-width = <4>;
	cap-sd-highspeed;
	disable-wp;
	max-frequency = <150000000>;
	no-mmc;
	no-sdio;
	sd-uhs-sdr104;
	vmmc-supply = <&vcc_3v3_s3>;
	vqmmc-supply = <&vccio_sd_s0>;
	status = "okay";
};

&u2phy0 {
	status = "okay";
};

&u2phy0_otg {
	phy-supply = <&vbus_5v0>;
	status = "okay";
};

&u2phy2 {
	status = "okay";
};

&u2phy2_host {
	phy-supply = <&vbus_5v0>;
	status = "okay";
};

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

&usb_host0_ehci {
	status = "okay";
};

&usb_host0_ohci {
	status = "okay";
};

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

&usbdp_phy0 {
	status = "okay";
};

&vop {
	status = "okay";
};

&vop_mmu {
	status = "okay";
};

&vp0 {
	vp0_out_hdmi0: endpoint@ROCKCHIP_VOP2_EP_HDMI0 {
		reg = <ROCKCHIP_VOP2_EP_HDMI0>;
		remote-endpoint = <&hdmi0_in_vp0>;
	};
};