// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * ArmSoM CM5-IO - official carrier board for the ArmSoM CM5 (RK3576) module.
 *
 * Carrier provides:
 *   - Full-size HDMI 2.1 (HDMI TX via HDPTX PHY)
 *   - Gigabit Ethernet RJ45 (GMAC0 + on-module YT8531)
 *   - 4x USB-A behind a USB3 hub (combphy1 + u2phy1)
 *   - USB-C with PD/altmode (FUSB302 on i2c0, USBDP PHY)
 *   - M.2 PCIe 2.0 x1 slot (combphy0 as PCIe lane)
 *   - microSD card slot
 *   - Green/red status LEDs, 40-pin RPi-compatible header
 *   - 12 V DC barrel jack
 *
 * Copyright (c) 2024 ArmSoM
 * Copyright (c) 2026 Jiaxing Hu <gahing@gahingwoo.com>
 */

/dts-v1/;

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/leds/common.h>
#include <dt-bindings/pinctrl/rockchip.h>
#include <dt-bindings/soc/rockchip,vop2.h>
#include <dt-bindings/usb/pd.h>
#include "rk3576-armsom-cm5.dtsi"

/ {
	model = "ArmSoM CM5-IO";
	compatible = "armsom,cm5-io", "armsom,cm5", "rockchip,rk3576";

	aliases {
		ethernet0 = &gmac0;
		mmc0 = &sdhci;
		mmc1 = &sdmmc;
	};

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

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

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

	leds: leds {
		compatible = "gpio-leds";

		led_green: led-0 {
			color = <LED_COLOR_ID_GREEN>;
			default-state = "on";
			function = LED_FUNCTION_STATUS;
			gpios = <&gpio2 RK_PD0 GPIO_ACTIVE_HIGH>;
			pinctrl-names = "default";
			pinctrl-0 = <&led_green_en>;
		};

		led_red: led-1 {
			color = <LED_COLOR_ID_RED>;
			default-state = "off";
			function = LED_FUNCTION_FAULT;
			gpios = <&gpio2 RK_PD1 GPIO_ACTIVE_HIGH>;
			pinctrl-names = "default";
			pinctrl-0 = <&led_red_en>;
		};
	};

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

	/* PCIe 3.3 V for the M.2 slot */
	vcc_3v3_pcie: regulator-vcc-3v3-pcie {
		compatible = "regulator-fixed";
		pinctrl-names = "default";
		pinctrl-0 = <&pcie_pwr_en>;
		regulator-name = "vcc_3v3_pcie";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		enable-active-high;
		gpios = <&gpio0 RK_PC3 GPIO_ACTIVE_HIGH>;
		startup-delay-us = <5000>;
		vin-supply = <&vcc_5v0_sys>;
	};

	vcc_5v0_device: regulator-vcc-5v0-device {
		compatible = "regulator-fixed";
		regulator-name = "vcc_5v0_device";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc_12v0_dcin>;
	};

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

	vcc_5v0_typec0: regulator-vcc-5v0-typec0 {
		compatible = "regulator-fixed";
		enable-active-high;
		gpios = <&gpio2 RK_PB6 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&usb_otg0_pwren>;
		regulator-name = "vcc_5v0_typec0";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc_5v0_device>;
	};

	/* USB-A host 5 V (through the hub) */
	vcc_5v0_usbhost: regulator-vcc-5v0-usbhost {
		compatible = "regulator-fixed";
		enable-active-high;
		gpios = <&gpio4 RK_PB0 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&usb_host_pwren>;
		regulator-name = "vcc_5v0_usbhost";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc_5v0_device>;
	};
};

/* ComboPHY0 -> PCIe lane (M.2 slot); ComboPHY1 -> USB3 lane (USB-A hub). */
&combphy0_ps {
	status = "okay";
};

&combphy1_psu {
	status = "okay";
};

&hdmi {
	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";
};

&hdptxphy {
	status = "okay";
};

&i2c0 {
	pinctrl-names = "default";
	pinctrl-0 = <&i2c0m1_xfer>;
	status = "okay";

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

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

			altmodes {
				displayport {
					svid = /bits/ 16 <0xff01>;
					vdo = <0xffffffff>;
				};
			};

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

				port@0 {
					reg = <0>;
					usbc0_orien_sw: endpoint {
						remote-endpoint = <&usbdp_phy_orientation_switch>;
					};
				};

				port@1 {
					reg = <1>;
					usbc0_role_sw: endpoint {
						remote-endpoint = <&usb_drd0_role_switch>;
					};
				};

				port@2 {
					reg = <2>;
					dp_altmode_mux: endpoint {
						remote-endpoint = <&usbdp_phy_dp_altmode_mux>;
					};
				};
			};
		};
	};
};

/* PCIe 2.0 x1 M.2 slot + */
&pcie0 {
	pinctrl-names = "default";
	pinctrl-0 = <&pcie_reset>;
	reset-gpios = <&gpio2 RK_PB1 GPIO_ACTIVE_HIGH>;
	vpcie3v3-supply = <&vcc_3v3_pcie>;
	status = "okay";
};

&pinctrl {
	leds {
		led_green_en: led-green-en {
			rockchip,pins = <2 RK_PD0 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		led_red_en: led-red-en {
			rockchip,pins = <2 RK_PD1 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	pcie {
		pcie_pwr_en: pcie-pwr-en {
			rockchip,pins = <0 RK_PC3 RK_FUNC_GPIO &pcfg_pull_down>;
		};

		pcie_reset: pcie-reset {
			rockchip,pins = <2 RK_PB1 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

	usb {
		usb_host_pwren: usb-host-pwren {
			rockchip,pins = <4 RK_PB0 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		usb_otg0_pwren: usb-otg0-pwren {
			rockchip,pins = <2 RK_PB6 RK_FUNC_GPIO &pcfg_pull_none>;
		};

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

/* SAI6 is the I2S CPU DAI feeding the HDMI audio path. */
&sai6 {
	status = "okay";
};

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

&u2phy0 {
	status = "okay";
};

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

&u2phy1 {
	status = "okay";
};

&u2phy1_otg {
	phy-supply = <&vcc_5v0_usbhost>;
	status = "okay";
};

/* Debug UART0 on the 40-pin header. */
&uart0 {
	pinctrl-0 = <&uart0m0_xfer>;
	status = "okay";
};

/* USB DRD0: USB-C (USBDP SuperSpeed + USB2 HS), OTG with FUSB302 */
&usb_drd0_dwc3 {
	dr_mode = "otg";
	usb-role-switch;
	status = "okay";

	port {
		usb_drd0_role_switch: endpoint {
			remote-endpoint = <&usbc0_role_sw>;
		};
	};
};

/* USB DRD1: USB-A ports behind the hub */
&usb_drd1_dwc3 {
	dr_mode = "host";
	status = "okay";
};

&usbdp_phy {
	status = "okay";
	mode-switch;
	orientation-switch;
	sbu1-dc-gpios = <&gpio4 RK_PC4 GPIO_ACTIVE_HIGH>;
	sbu2-dc-gpios = <&gpio4 RK_PC5 GPIO_ACTIVE_HIGH>;

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

		usbdp_phy_orientation_switch: endpoint@0 {
			reg = <0>;
			remote-endpoint = <&usbc0_orien_sw>;
		};

		usbdp_phy_dp_altmode_mux: endpoint@1 {
			reg = <1>;
			remote-endpoint = <&dp_altmode_mux>;
		};
	};
};

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