// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Device tree definitions shared by the Orange Pi 5 and Orange Pi 5B
 * but not the Orange Pi 5 Pro.
 */

#include <dt-bindings/usb/pd.h>
#include "rk3588s-orangepi-5.dtsi"

/ {
	aliases {
		ethernet0 = &gmac1;
	};

	analog-sound {
		compatible = "simple-audio-card";
		pinctrl-names = "default";
		pinctrl-0 = <&hp_detect>;
		simple-audio-card,name = "rockchip,es8388";
		simple-audio-card,bitclock-master = <&masterdai>;
		simple-audio-card,format = "i2s";
		simple-audio-card,frame-master = <&masterdai>;
		simple-audio-card,hp-det-gpios = <&gpio1 RK_PD5 GPIO_ACTIVE_HIGH>;
		simple-audio-card,mclk-fs = <256>;
		simple-audio-card,routing =
			"Headphones", "LOUT1",
			"Headphones", "ROUT1",
			"LINPUT1", "Microphone Jack",
			"RINPUT1", "Microphone Jack",
			"LINPUT2", "Onboard Microphone",
			"RINPUT2", "Onboard Microphone";
		simple-audio-card,widgets =
			"Microphone", "Microphone Jack",
			"Microphone", "Onboard Microphone",
			"Headphone", "Headphones";

		simple-audio-card,cpu {
			sound-dai = <&i2s1_8ch>;
		};

		masterdai: simple-audio-card,codec {
			sound-dai = <&es8388>;
			system-clock-frequency = <12288000>;
		};
	};

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

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

	vbus_typec: regulator-vbus-typec {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio3 RK_PC0 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&typec5v_pwren>;
		regulator-name = "vbus_typec";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc5v0_sys>;
	};
};

&gmac1 {
	clock_in_out = "output";
	phy-handle = <&rgmii_phy1>;
	phy-mode = "rgmii-rxid";
	pinctrl-0 = <&gmac1_miim
			&gmac1_tx_bus2
			&gmac1_rx_bus2
			&gmac1_rgmii_clk
			&gmac1_rgmii_bus>;
	pinctrl-names = "default";
	tx_delay = <0x42>;
	status = "okay";
};

&i2c6 {
	es8388: audio-codec@10 {
		compatible = "everest,es8388", "everest,es8328";
		reg = <0x10>;
		clocks = <&cru I2S1_8CH_MCLKOUT>;
		AVDD-supply = <&vcca_3v3_s0>;
		DVDD-supply = <&vcca_1v8_s0>;
		HPVDD-supply = <&vcca_3v3_s0>;
		PVDD-supply = <&vcca_3v3_s0>;
		assigned-clocks = <&cru I2S1_8CH_MCLKOUT>;
		assigned-clock-rates = <12288000>;
		#sound-dai-cells = <0>;
	};

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

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

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

				port@0 {
					reg = <0>;
					usbc0_hs: endpoint {
						remote-endpoint = <&usb_host0_xhci_drd_sw>;
					};
				};

				port@1 {
					reg = <1>;
					usbc0_ss: endpoint {
						remote-endpoint = <&usbdp_phy0_typec_ss>;
					};
				};

				port@2 {
					reg = <2>;
					usbc0_sbu: endpoint {
						remote-endpoint = <&usbdp_phy0_typec_sbu>;
					};
				};
			};
		};
	};
};

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

&pinctrl {
	usb-typec {
		usbc0_int: usbc0-int {
			rockchip,pins = <0 RK_PD3 RK_FUNC_GPIO &pcfg_pull_up>;
		};

		typec5v_pwren: typec5v-pwren {
			rockchip,pins = <3 RK_PC0 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};
};

&i2s1_8ch {
	rockchip,i2s-tx-route = <3 2 1 0>;
	rockchip,i2s-rx-route = <1 3 2 0>;
	pinctrl-names = "default";
	pinctrl-0 = <&i2s1m0_sclk
			   &i2s1m0_mclk
			   &i2s1m0_lrck
			   &i2s1m0_sdi1
			   &i2s1m0_sdo3>;
	status = "okay";
};

&pwm0 {
	pinctrl-0 = <&pwm0m2_pins>;
	pinctrl-names = "default";
	status = "okay";
};

&rk806 {
	regulators {
		vcc_1v8_s0: pldo-reg1 {
			regulator-name = "vcc_1v8_s0";
			regulator-always-on;
			regulator-boot-on;
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;

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

		vcca_1v8_s0: pldo-reg2 {
			regulator-name = "vcca_1v8_s0";
			regulator-always-on;
			regulator-boot-on;
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;

			regulator-state-mem {
				regulator-off-in-suspend;
				regulator-suspend-microvolt = <1800000>;
			};
		};
	};
};


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

	port {
		usb_host0_xhci_drd_sw: endpoint {
			remote-endpoint = <&usbc0_hs>;
		};
	};
};

&usb_host2_xhci {
	status = "okay";
};

&usbdp_phy0 {
	mode-switch;
	orientation-switch;
	sbu1-dc-gpios = <&gpio4 RK_PA5 GPIO_ACTIVE_HIGH>;
	sbu2-dc-gpios = <&gpio4 RK_PA7 GPIO_ACTIVE_HIGH>;

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

		usbdp_phy0_typec_ss: endpoint@0 {
			reg = <0>;
			remote-endpoint = <&usbc0_ss>;
		};

		usbdp_phy0_typec_sbu: endpoint@1 {
			reg = <1>;
			remote-endpoint = <&usbc0_sbu>;
		};
	};
};