// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2025 Joseph Kogut <joseph.kogut@gmail.com>
 */

/*
 * CM5 IO board data sheet
 * https://dl.radxa.com/cm5/v2200/radxa_cm5_io_v2200_schematic.pdf
 */

/dts-v1/;
#include "rk3588s.dtsi"
#include "rk3588s-radxa-cm5.dtsi"

/ {
	model = "Radxa Compute Module 5 (CM5) IO Board";
	compatible = "radxa,cm5-io", "radxa,cm5", "rockchip,rk3588s";

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

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

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

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

	vcc12v_dcin: regulator-12v0-vcc-dcin {
		compatible = "regulator-fixed";
		regulator-name = "vcc12v_dcin";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <12000000>;
		regulator-max-microvolt = <12000000>;
	};

	vcc5v0_host: vcc5v0-host-regulator {
		compatible = "regulator-fixed";
		regulator-name = "vcc5v0_host";
		regulator-boot-on;
		regulator-always-on;
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		enable-active-high;
		gpio = <&gpio1 RK_PA0 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&vcc5v0_host_en>;
		vin-supply = <&vcc5v0_sys>;
	};

	vcc5v0_sys: regulator-5v0-sys {
		compatible = "regulator-fixed";
		regulator-name = "vcc5v0_sys";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc12v_dcin>;
	};

	vbus5v0_typec: vbus5v0-typec {
		compatible = "regulator-fixed";
		regulator-name = "vbus5v0_typec";
		gpio = <&gpio0 RK_PD5 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&vbus5v0_typec_en>;
		enable-active-high;
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc5v0_sys>;
	};

	vcc3v3_pcie: regulator-3v3-vcc-pcie {
		compatible = "regulator-fixed";
		regulator-name = "vcc3v3_pcie2x1l0";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		enable-active-high;
		gpios = <&gpio1 RK_PD3 GPIO_ACTIVE_HIGH>;
		startup-delay-us = <50000>;
		vin-supply = <&vcc5v0_sys>;
	};

	vcc_3v3_s0: pldo-reg4 {
		compatible = "regulator-fixed";
		regulator-name = "vcc_3v3_s0";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-ramp-delay = <12500>;

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

&combphy0_ps {
	status = "okay";
};

&combphy2_psu {
	status = "okay";
};

&gmac1 {
	status = "okay";
};

&hdmi0 {
	frl-enable-gpios = <&gpio4 RK_PB6 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 = <&hdmi_con_in>;
	};
};

&hdmi0_sound {
	status = "okay";
};

&hdptxphy0 {
	status = "okay";
};

&i2c6 {
	pinctrl-names = "default";
	pinctrl-0 = <&i2c6m3_xfer>;
	status = "okay";

	fusb302: usb-typec@22 {
		compatible = "fcs,fusb302";
		reg = <0x22>;
		interrupt-parent = <&gpio0>;
		interrupts = <RK_PC4 IRQ_TYPE_LEVEL_LOW>;
		pinctrl-names = "default";
		pinctrl-0 = <&usbc0_int>;
		vbus-supply = <&vbus5v0_typec>;

		connector {
			compatible = "usb-c-connector";
			data-role = "dual";
			label = "USB-C";
			power-role = "dual";
			try-power-role = "sink";
			source-pdos = <PDO_FIXED(5000, 3000, PDO_FIXED_USB_COMM)>;
			sink-pdos = <PDO_FIXED(5000, 1000, PDO_FIXED_USB_COMM)>;
			op-sink-microwatt = <1000000>;

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

				port@0 {
					reg = <0>;
					usbc0_orientation_switch: endpoint {
						remote-endpoint = <&usbdp_phy0_orientation_switch>;
					};
				};

				port@1 {
					reg = <1>;
					usbc0_role_switch: endpoint {
						remote-endpoint = <&usb_host0_xhci_role_switch>;
					};
				};

				port@2 {
					reg = <2>;
					usbc0_dp_altmode_mux: endpoint {
						remote-endpoint = <&usbdp_phy0_dp_altmode_mux>;
					};
				};
			};
		};
	};

	hym8563: 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_l>;
		wakeup-source;
	};
};

&i2s5_8ch {
	status = "okay";
};

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

&pinctrl {
	fusb302 {
		vbus5v0_typec_en: vbus5v0-typec-en {
			rockchip,pins = <0 RK_PD5 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		usbc0_int: usbc0-int {
			rockchip,pins = <0 RK_PC4 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

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

	hym8563 {
		rtc_int_l: rtc-int-l {
			rockchip,pins = <0 RK_PB0 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

	usb {
		vcc5v0_host_en: vcc5v0-host-en {
			rockchip,pins = <1 RK_PA0 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};
};

&sdmmc {
	bus-width = <4>;
	cap-mmc-highspeed;
	cap-sd-highspeed;
	disable-wp;
	no-sdio;
	sd-uhs-sdr104;
	pinctrl-names = "default";
	pinctrl-0 = <&sdmmc_bus4 &sdmmc_clk &sdmmc_cmd &sdmmc_det>;
	vmmc-supply = <&vcc_3v3_s3>;
	vqmmc-supply = <&vccio_sd_s0>;
	status = "okay";
};

&u2phy0 {
	status = "okay";
};

&u2phy0_otg {
	status = "okay";
};

&u2phy2 {
	status = "okay";
};

&u2phy2_host {
	status = "okay";
};

&u2phy3 {
	status = "okay";
};

&u2phy3_host {
	status = "okay";
};

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

&usb_host0_ehci {
	status = "okay";
};

&usb_host0_ohci {
	status = "okay";
};

&usb_host0_xhci {
	dr_mode = "otg";
	usb-role-switch;
	status = "okay";

	port {
		usb_host0_xhci_role_switch: endpoint {
			remote-endpoint = <&usbc0_role_switch>;
		};
	};
};

&usb_host1_ehci {
	status = "okay";
};

&usb_host1_ohci {
	status = "okay";
};

&usb_host2_xhci {
	status = "okay";
};

&usbdp_phy0 {
	mode-switch;
	orientation-switch;
	sbu1-dc-gpios = <&gpio3 RK_PC4 GPIO_ACTIVE_HIGH>;
	sbu2-dc-gpios = <&gpio3 RK_PD4 GPIO_ACTIVE_HIGH>;
	status = "okay";

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

		usbdp_phy0_orientation_switch: endpoint@0 {
			reg = <0>;
			remote-endpoint = <&usbc0_orientation_switch>;
		};

		usbdp_phy0_dp_altmode_mux: endpoint@1 {
			reg = <1>;
			remote-endpoint = <&usbc0_dp_altmode_mux>;
		};
	};
};

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