// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * ArmSoM CM5 - Rockchip RK3576 compute module (Raspberry Pi CM4 form factor).
 *
 * Common to all CM5 carriers. The carrier .dts includes this file, sets the
 * board model/compatible, and adds carrier-specific peripherals.
 *
 * On-module devices:
 *   - RK806 PMIC on I2C1
 *   - HYM8563 RTC on I2C2
 *   - eMMC (SDHCI)
 *   - GMAC0 + MotorComm YT8531 Gigabit Ethernet PHY (RGMII)
 *   - SYN43752 WiFi 6 + BT 5.x (SDIO + UART4)
 *
 * Copyright (c) 2024 ArmSoM
 * Copyright (c) 2026 Jiaxing Hu <gahing@gahingwoo.com>
 */

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/rockchip.h>
#include "rk3576.dtsi"

/ {
	compatible = "armsom,cm5", "rockchip,rk3576";

	vcc_1v1_nldo_s3: regulator-vcc-1v1-nldo-s3 {
		compatible = "regulator-fixed";
		regulator-name = "vcc_1v1_nldo_s3";
		regulator-boot-on;
		regulator-always-on;
		regulator-min-microvolt = <1100000>;
		regulator-max-microvolt = <1100000>;
		vin-supply = <&vcc_5v0_sys>;
	};

	vcc_1v2_ufs_vccq_s0: regulator-vcc-1v2-ufs-vccq-s0 {
		compatible = "regulator-fixed";
		regulator-name = "vcc_1v2_ufs_vccq_s0";
		regulator-boot-on;
		regulator-always-on;
		regulator-min-microvolt = <1200000>;
		regulator-max-microvolt = <1200000>;
		vin-supply = <&vcc_5v0_sys>;
	};

	vcc_1v8_s0: regulator-vcc-1v8-s0 {
		compatible = "regulator-fixed";
		regulator-name = "vcc_1v8_s0";
		regulator-boot-on;
		regulator-always-on;
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		vin-supply = <&vcc_1v8_s3>;
	};

	vcc_1v8_ufs_vccq2_s0: regulator-vcc-1v8-ufs-vccq2-s0 {
		compatible = "regulator-fixed";
		regulator-name = "vcc_1v8_ufs_vccq2_s0";
		regulator-boot-on;
		regulator-always-on;
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		vin-supply = <&vcc_1v8_s3>;
	};

	vcc_2v0_pldo_s3: regulator-vcc-2v0-pldo-s3 {
		compatible = "regulator-fixed";
		regulator-name = "vcc_2v0_pldo_s3";
		regulator-boot-on;
		regulator-always-on;
		regulator-min-microvolt = <2000000>;
		regulator-max-microvolt = <2000000>;
		vin-supply = <&vcc_5v0_sys>;
	};

	vcc_3v3_s0: regulator-vcc-3v3-s0 {
		compatible = "regulator-fixed";
		regulator-name = "vcc_3v3_s0";
		regulator-boot-on;
		regulator-always-on;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&vcc_3v3_s3>;
	};

	vcc_3v3_ufs_s0: regulator-vcc-ufs-s0 {
		compatible = "regulator-fixed";
		regulator-name = "vcc_3v3_ufs_s0";
		regulator-boot-on;
		regulator-always-on;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&vcc_5v0_sys>;
	};

	/*
	 * WiFi power sequencer for SYN43752. Reset GPIO1 PC6 active-low,
	 * clock from the HYM8563 RTC oscillator.
	 */
	sdio_pwrseq: sdio-pwrseq {
		compatible = "mmc-pwrseq-simple";
		clocks = <&hym8563>;
		clock-names = "ext_clock";
		pinctrl-names = "default";
		pinctrl-0 = <&wifi_reg_on>;
		reset-gpios = <&gpio1 RK_PC6 GPIO_ACTIVE_LOW>;
	};
};

&cpu_b0 {
	cpu-supply = <&vdd_cpu_big_s0>;
};

&cpu_b1 {
	cpu-supply = <&vdd_cpu_big_s0>;
};

&cpu_b2 {
	cpu-supply = <&vdd_cpu_big_s0>;
};

&cpu_b3 {
	cpu-supply = <&vdd_cpu_big_s0>;
};

&cpu_l0 {
	cpu-supply = <&vdd_cpu_lit_s0>;
};

&cpu_l1 {
	cpu-supply = <&vdd_cpu_lit_s0>;
};

&cpu_l2 {
	cpu-supply = <&vdd_cpu_lit_s0>;
};

&cpu_l3 {
	cpu-supply = <&vdd_cpu_lit_s0>;
};

/*
 * GMAC0 drives an on-module MotorComm YT8531. The PHY has no local crystal and
 * takes the SoC 25 MHz reference (REFCLKO25M_GMAC0_OUT); ethm0_clk0_25m_out
 * routes that clock to the pad, and the PHY node consumes and enables it.
 */
&gmac0 {
	phy-mode = "rgmii-id";
	clock_in_out = "output";
	phy-handle = <&rgmii_phy0>;
	phy-supply = <&vcc_3v3_s3>;
	pinctrl-names = "default";
	pinctrl-0 = <&eth0m0_miim
		     &eth0m0_tx_bus2
		     &eth0m0_rx_bus2
		     &eth0m0_rgmii_clk
		     &eth0m0_rgmii_bus
		     &ethm0_clk0_25m_out>;
	status = "okay";
};

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

&i2c1 {
	status = "okay";

	pmic@23 {
		compatible = "rockchip,rk806";
		reg = <0x23>;
		gpio-controller;
		#gpio-cells = <2>;
		interrupt-parent = <&gpio0>;
		interrupts = <RK_PA6 IRQ_TYPE_LEVEL_LOW>;
		pinctrl-names = "default";
		pinctrl-0 = <&pmic_pins>, <&rk806_dvs1_null>,
			    <&rk806_dvs2_null>, <&rk806_dvs3_null>;
		system-power-controller;
		vcc1-supply = <&vcc_5v0_sys>;
		vcc2-supply = <&vcc_5v0_sys>;
		vcc3-supply = <&vcc_5v0_sys>;
		vcc4-supply = <&vcc_5v0_sys>;
		vcc5-supply = <&vcc_5v0_sys>;
		vcc6-supply = <&vcc_5v0_sys>;
		vcc7-supply = <&vcc_5v0_sys>;
		vcc8-supply = <&vcc_5v0_sys>;
		vcc9-supply = <&vcc_5v0_sys>;
		vcc10-supply = <&vcc_5v0_sys>;
		vcc11-supply = <&vcc_2v0_pldo_s3>;
		vcc12-supply = <&vcc_5v0_sys>;
		vcc13-supply = <&vcc_1v1_nldo_s3>;
		vcc14-supply = <&vcc_1v1_nldo_s3>;
		vcca-supply = <&vcc_5v0_sys>;

		rk806_dvs1_null: dvs1-null-pins {
			pins = "gpio_pwrctrl1";
			function = "pin_fun0";
		};

		rk806_dvs2_null: dvs2-null-pins {
			pins = "gpio_pwrctrl2";
			function = "pin_fun0";
		};

		rk806_dvs3_null: dvs3-null-pins {
			pins = "gpio_pwrctrl3";
			function = "pin_fun0";
		};

		regulators {
			vdd_cpu_big_s0: dcdc-reg1 {
				regulator-always-on;
				regulator-boot-on;
				regulator-enable-ramp-delay = <400>;
				regulator-min-microvolt = <550000>;
				regulator-max-microvolt = <950000>;
				regulator-name = "vdd_cpu_big_s0";
				regulator-ramp-delay = <12500>;

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

			vdd_npu_s0: dcdc-reg2 {
				regulator-boot-on;
				regulator-enable-ramp-delay = <400>;
				regulator-min-microvolt = <550000>;
				regulator-max-microvolt = <950000>;
				regulator-name = "vdd_npu_s0";
				regulator-ramp-delay = <12500>;

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

			vdd_cpu_lit_s0: dcdc-reg3 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <550000>;
				regulator-max-microvolt = <950000>;
				regulator-name = "vdd_cpu_lit_s0";
				regulator-ramp-delay = <12500>;

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

			vcc_3v3_s3: dcdc-reg4 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc_3v3_s3";

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

			vdd_gpu_s0: dcdc-reg5 {
				regulator-boot-on;
				regulator-enable-ramp-delay = <400>;
				regulator-min-microvolt = <550000>;
				regulator-max-microvolt = <900000>;
				regulator-ramp-delay = <12500>;
				regulator-name = "vdd_gpu_s0";

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

			vddq_ddr_s0: dcdc-reg6 {
				regulator-always-on;
				regulator-boot-on;
				regulator-name = "vddq_ddr_s0";

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

			vdd_logic_s0: dcdc-reg7 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <550000>;
				regulator-max-microvolt = <800000>;
				regulator-name = "vdd_logic_s0";

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

			vcc_1v8_s3: dcdc-reg8 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-name = "vcc_1v8_s3";

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

			vdd2_ddr_s3: dcdc-reg9 {
				regulator-always-on;
				regulator-boot-on;
				regulator-name = "vdd2_ddr_s3";

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

			vdd_ddr_s0: dcdc-reg10 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <550000>;
				regulator-max-microvolt = <1200000>;
				regulator-name = "vdd_ddr_s0";

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

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

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

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

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

			vdda_1v2_s0: pldo-reg3 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1200000>;
				regulator-max-microvolt = <1200000>;
				regulator-name = "vdda_1v2_s0";

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

			vcca_3v3_s0: pldo-reg4 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcca_3v3_s0";

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

			vccio_sd_s0: pldo-reg5 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vccio_sd_s0";

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

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

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

			vdd_0v75_s3: nldo-reg1 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <750000>;
				regulator-max-microvolt = <750000>;
				regulator-name = "vdd_0v75_s3";

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

			vdda_ddr_pll_s0: nldo-reg2 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <850000>;
				regulator-max-microvolt = <850000>;
				regulator-name = "vdda_ddr_pll_s0";

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

			vdda0v75_hdmi_s0: nldo-reg3 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <837500>;
				regulator-max-microvolt = <837500>;
				regulator-name = "vdda0v75_hdmi_s0";

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

			vdda_0v85_s0: nldo-reg4 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <850000>;
				regulator-max-microvolt = <850000>;
				regulator-name = "vdda_0v85_s0";

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

			vdda_0v75_s0: nldo-reg5 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <750000>;
				regulator-max-microvolt = <750000>;
				regulator-name = "vdda_0v75_s0";

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

/* I2C2: HYM8563 RTC @ 0x51 (interrupt on GPIO0 PA0). */
&i2c2 {
	status = "okay";

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

/*
 * The PHY reset has to be described at the MDIO bus level: mdiobus releases a
 * bus level reset before it scans for PHY IDs, whereas the per-PHY reset-gpios
 * is only applied by phylib once the PHY has already been found, which never
 * happens while the YT8531 is still held in reset.
 *
 * The compatible names the YT8531 explicitly so phylib instantiates the PHY
 * from the device tree without an MDIO ID probe; the crystal-less PHY does not
 * answer on MDIO until its driver has enabled the reference clock below.
 */
&mdio0 {
	reset-gpios = <&gpio2 RK_PB3 GPIO_ACTIVE_LOW>;
	reset-delay-us = <20000>;
	reset-post-delay-us = <100000>;

	rgmii_phy0: ethernet-phy@1 {
		compatible = "ethernet-phy-id4f51.e91b";
		reg = <0x1>;
		clocks = <&cru REFCLKO25M_GMAC0_OUT>;
	};
};

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

/* eMMC: on-module, HS400 200 MHz with enhanced strobe. */
&sdhci {
	bus-width = <8>;
	full-pwr-cycle-in-suspend;
	max-frequency = <200000000>;
	mmc-hs400-1_8v;
	mmc-hs400-enhanced-strobe;
	no-sdio;
	no-sd;
	non-removable;
	/delete-property/ supports-cqe;
	status = "okay";
};

/* SDIO: SYN43752 WiFi, power-sequenced by sdio_pwrseq, clock from HYM8563. */
&sdio {
	bus-width = <4>;
	cap-sdio-irq;
	disable-wp;
	keep-power-in-suspend;
	mmc-pwrseq = <&sdio_pwrseq>;
	no-sd;
	no-mmc;
	non-removable;
	sd-uhs-sdr50;
	sd-uhs-sdr104;
	vmmc-supply = <&vcc_3v3_s3>;
	vqmmc-supply = <&vcc_1v8_s3>;
	wakeup-source;
	status = "okay";
};

/* UART4: Bluetooth (SYN43752). */
&uart4 {
	pinctrl-0 = <&uart4m1_xfer &uart4m1_ctsn &uart4m1_rtsn>;
	pinctrl-names = "default";
	uart-has-rtscts;
	status = "okay";
};

&pinctrl {
	hym8563 {
		hym8563_int: hym8563-int {
			rockchip,pins = <0 RK_PA0 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

	wireless-wlan {
		/* GPIO1 PC6 - WiFi power enable / reset (sdio_pwrseq) */
		wifi_reg_on: wifi-reg-on {
			rockchip,pins = <1 RK_PC6 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};
};