// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2017 Fuzhou Rockchip Electronics Co., Ltd.
 * Copyright (c) 2025 retro98boy <retro98boy@qq.com>
 */

/dts-v1/;

#include <dt-bindings/input/linux-event-codes.h>
#include <dt-bindings/pwm/pwm.h>
#include "rk3399.dtsi"

/ {
	model = "SMART AM40";
	compatible = "smart,am40", "rockchip,rk3399";

	aliases {
		ethernet0 = &gmac;
		mmc0 = &sdhci;
		mmc1 = &sdmmc;
		rtc0 = &pt7c4563;
		/*
		 * The rk808 circuit design on this board does not have the ability to maintain real-time time after a power outage.
		 * Registering rk808 as rtc99 (most kernel configurations read time from rtc0) can prevent the kernel from reading the time (2013) from rk808 during startup.
		 */
		rtc99 = &rk808;
	};

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

	clkin_gmac: external-gmac-clock {
		compatible = "fixed-clock";
		clock-frequency = <125000000>;
		clock-output-names = "clkin_gmac";
		#clock-cells = <0>;
	};

	gpio-leds {
		compatible = "gpio-leds";
		pinctrl-names = "default";
		pinctrl-0 = <&pwr_led>;

		pwr-led {
			label = "pwr-led";
			gpios = <&gpio4 RK_PC2 GPIO_ACTIVE_HIGH>;
			default-state = "on";
		};
	};

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

		pwr-btn {
			debounce-interval = <100>;
			gpios = <&gpio0 RK_PA5 GPIO_ACTIVE_LOW>;
			label = "pwr-btn";
			linux,code = <KEY_POWER>;
			wakeup-source;
		};
	};

	adc-keys {
		compatible = "adc-keys";
		io-channels = <&saradc 1>;
		io-channel-names = "buttons";
		keyup-threshold-microvolt = <1800000>;
		poll-interval = <100>;

		button-recovery {
			label = "Recovery";
			linux,code = <KEY_VENDOR>;
			press-threshold-microvolt = <18000>;
		};
	};

	vcc1v8_s0: regulator-vcc1v8-s0 {
		compatible = "regulator-fixed";
		regulator-name = "vcc1v8_s0";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
	};

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

	vcc_phy: regulator-vcc-phy {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio1 RK_PB5 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&vcc_phy_en>;
		regulator-name = "vcc_phy";
		regulator-always-on;
		regulator-boot-on;
	};

	vcc3v3_sys: regulator-vcc3v3-sys {
		compatible = "regulator-fixed";
		regulator-name = "vcc3v3_sys";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&vcc_sys>;
	};

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

	vcc3v3_pcie: regulator-vcc3v3-pcie {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio1 RK_PA4 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&vcc3v3_pcie_en>;
		regulator-name = "vcc3v3_pcie";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
	};

	front_hdmi_5v: regulator-front-hdmi-5v {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio4 RK_PD4 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&front_hdmi_5v_en>;
		regulator-name = "front_hdmi_5v";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
	};

	otg_vbus: regulator-otg-vbus {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio1 RK_PC6 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&otg_vbus_en>;
		regulator-name = "otg_vbus";
		regulator-always-on;
		regulator-boot-on;
	};

	vdd_log: regulator-vdd-log {
		compatible = "pwm-regulator";
		pwms = <&pwm2 0 25000 1>;
		pwm-supply = <&vcc_sys>;
		regulator-name = "vdd_log";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <800000>;
		regulator-max-microvolt = <1400000>;
	};

	virtual_pd: virtual-pd {
		compatible = "linux,extcon-usbc-virtual-pd";
		pinctrl-names = "default";
		pinctrl-0 = <&dp_hpd>;
		det-gpios = <&gpio4 RK_PD1 GPIO_ACTIVE_LOW>;
		vpd-data-role = "display-port";
		vpd-super-speed;
	};

	dp-sound {
		compatible = "simple-audio-card";
		simple-audio-card,format = "i2s";
		simple-audio-card,mclk-fs = <256>;
		simple-audio-card,name = "dp-sound";

		simple-audio-card,cpu {
			// sound-dai = <&i2s2>;
			sound-dai = <&spdif>;
		};
		simple-audio-card,codec {
			// sound-dai = <&cdn_dp 0>;
			sound-dai = <&cdn_dp 1>;
		};
	};
};

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

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

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

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

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

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

&emmc_phy {
	status = "okay";
};

&gmac {
	assigned-clock-parents = <&clkin_gmac>;
	assigned-clocks = <&cru SCLK_RMII_SRC>;
	clock_in_out = "input";
	pinctrl-names = "default";
	pinctrl-0 = <&rgmii_pins>, <&phy_rst_l>;
	phy-handle = <&rtl8211f>;
	phy-mode = "rgmii";
	phy-supply = <&vcc_phy>;
	tx_delay = <0x28>;
	rx_delay = <0x11>;
	status = "okay";

	mdio {
		compatible = "snps,dwmac-mdio";
		#address-cells = <1>;
		#size-cells = <0>;

		rtl8211f: ethernet-phy@0 {
			reg = <0>;
			reset-assert-us = <10000>;
			reset-deassert-us = <30000>;
			reset-gpios = <&gpio3 RK_PB7 GPIO_ACTIVE_LOW>;
		};
	};
};

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

&hdmi {
	avdd-0v9-supply = <&vcca0v9_hdmi>;
	avdd-1v8-supply = <&vcca1v8_hdmi>;
	ddc-i2c-bus = <&i2c3>;
	pinctrl-names = "default";
	pinctrl-0 = <&hdmi_cec>;
	status = "disabled";
};

&hdmi_sound {
	status = "disabled";
};

&i2c0 {
	clock-frequency = <400000>;
	i2c-scl-rising-time-ns = <168>;
	i2c-scl-falling-time-ns = <4>;
	status = "okay";

	rk808: pmic@1b {
		compatible = "rockchip,rk808";
		reg = <0x1b>;
		interrupt-parent = <&gpio1>;
		interrupts = <21 IRQ_TYPE_LEVEL_LOW>;
		#clock-cells = <1>;
		clock-output-names = "xin32k", "rk808-clkout2";
		pinctrl-names = "default";
		pinctrl-0 = <&pmic_int_l>;
		system-power-controller;
		wakeup-source;

		vcc1-supply = <&vcc_sys>;
		vcc2-supply = <&vcc_sys>;
		vcc3-supply = <&vcc_sys>;
		vcc4-supply = <&vcc_sys>;
		vcc6-supply = <&vcc_sys>;
		vcc7-supply = <&vcc_sys>;
		vcc8-supply = <&vcc3v3_sys>;
		vcc9-supply = <&vcc_sys>;
		vcc10-supply = <&vcc_sys>;
		vcc11-supply = <&vcc_sys>;
		vcc12-supply = <&vcc3v3_sys>;
		vddio-supply = <&vcc_1v8>;

		regulators {
			vdd_center: DCDC_REG1 {
				regulator-name = "vdd_center";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <750000>;
				regulator-max-microvolt = <1350000>;
				regulator-ramp-delay = <6001>;
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vdd_cpu_l: DCDC_REG2 {
				regulator-name = "vdd_cpu_l";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <750000>;
				regulator-max-microvolt = <1350000>;
				regulator-ramp-delay = <6001>;
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcc_ddr: DCDC_REG3 {
				regulator-name = "vcc_ddr";
				regulator-always-on;
				regulator-boot-on;
				regulator-state-mem {
					regulator-on-in-suspend;
				};
			};

			vcc_1v8: DCDC_REG4 {
				regulator-name = "vcc_1v8";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-state-mem {
					regulator-on-in-suspend;
					regulator-suspend-microvolt = <1800000>;
				};
			};

			vcc1v8_dvp: LDO_REG1 {
				regulator-name = "vcc1v8_dvp";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcca1v8_hdmi: LDO_REG2 {
				regulator-name = "vcca1v8_hdmi";
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcca1v8: LDO_REG3 {
				regulator-name = "vcca1v8";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-state-mem {
					regulator-on-in-suspend;
					regulator-suspend-microvolt = <1800000>;
				};
			};

			vcc_sd: LDO_REG4 {
				regulator-name = "vcc_sd";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <3000000>;
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcc3v0_sd: LDO_REG5 {
				regulator-name = "vcc3v0_sd";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <3000000>;
				regulator-max-microvolt = <3000000>;
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcc_1v5: LDO_REG6 {
				regulator-name = "vcc_1v5";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1500000>;
				regulator-max-microvolt = <1500000>;
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcca0v9_hdmi: LDO_REG7 {
				regulator-name = "vcca0v9_hdmi";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <900000>;
				regulator-max-microvolt = <900000>;
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcc_3v0: LDO_REG8 {
				regulator-name = "vcc_3v0";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <3000000>;
				regulator-max-microvolt = <3000000>;
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcc3v3_s3: SWITCH_REG1 {
				regulator-name = "vcc3v3_s3";
				regulator-always-on;
				regulator-boot-on;
				regulator-state-mem {
					regulator-on-in-suspend;
				};
			};

			vcc3v3_s0: SWITCH_REG2 {
				regulator-name = "vcc3v3_s0";
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};
		};
	};

	vdd_cpu_b: regulator@40 {
		compatible = "silergy,syr827";
		reg = <0x40>;
		fcs,suspend-voltage-selector = <1>;
		pinctrl-names = "default";
		pinctrl-0 = <&vsel1_pin>;
		regulator-name = "vdd_cpu_b";
		regulator-min-microvolt = <712500>;
		regulator-max-microvolt = <1500000>;
		regulator-ramp-delay = <1000>;
		regulator-always-on;
		regulator-boot-on;
		vin-supply = <&vcc_sys>;

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

	vdd_gpu: regulator@41 {
		compatible = "silergy,syr828";
		reg = <0x41>;
		fcs,suspend-voltage-selector = <1>;
		pinctrl-names = "default";
		pinctrl-0 = <&vsel2_pin>;
		regulator-name = "vdd_gpu";
		regulator-min-microvolt = <712500>;
		regulator-max-microvolt = <1500000>;
		regulator-ramp-delay = <1000>;
		regulator-always-on;
		regulator-boot-on;
		vin-supply = <&vcc_sys>;

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

&i2c4 {
	status = "okay";
	i2c-scl-rising-time-ns = <300>;
	i2c-scl-falling-time-ns = <300>;

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

&i2s2 {
	status = "okay";
};

&io_domains {
	bt656-supply = <&vcc1v8_s0>;
	audio-supply = <&vcc1v8_s0>;
	sdmmc-supply = <&vcc_sd>;
	gpio1830-supply = <&vcc_3v0>;
	status = "okay";
};

&pcie0 {
	ep-gpios = <&gpio0 RK_PB5 GPIO_ACTIVE_HIGH>;
	num-lanes = <4>;
	pinctrl-0 = <&pcie_clkreqn>;
	pinctrl-names = "default";
	vpcie3v3-supply = <&vcc3v3_pcie>;
	status = "okay";
};

&pcie_phy {
	status = "okay";
};

&pinctrl {
	buttons {
		pwr_btn: pwr-btn {
			rockchip,pins = <0 RK_PA5 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

	display {
		dp_hpd: dp-hpd {
			rockchip,pins = <4 RK_PD1 RK_FUNC_GPIO &pcfg_input_pull_up>;
		};

		front_hdmi_5v_en: front-hdmi-5v-en {
			rockchip,pins = <4 RK_PD4 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	gmac {
		phy_rst_l: phy-rst-l {
			rockchip,pins = <3 RK_PB7 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		vcc_phy_en: vcc-phy-en {
			rockchip,pins = <1 RK_PB5 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	leds {
		pwr_led: pwr-led {
			rockchip,pins = <4 RK_PC2 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	pcie {
		pcie_clkreqn: pci-clkreqn {
			rockchip,pins = <2 RK_PD2 2 &pcfg_pull_none>;
		};

		vcc3v3_pcie_en: vcc3v3-pcie-en {
			rockchip,pins = <1 RK_PA4 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	pmic {
		pmic_int_l: pmic-int-l {
			rockchip,pins = <1 RK_PC5 RK_FUNC_GPIO &pcfg_pull_up>;
		};

		vsel1_pin: vsel1-pin {
			rockchip,pins = <1 RK_PC1 RK_FUNC_GPIO &pcfg_pull_down>;
		};

		vsel2_pin: vsel2-pin {
			rockchip,pins = <1 RK_PB6 RK_FUNC_GPIO &pcfg_pull_down>;
		};
	};

	rtc {
		rtc_int: rtc-int {
			rockchip,pins = <0 RK_PA6 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

	usb2 {
		vcc5v0_host_en: vcc5v0-hots-en {
			rockchip,pins = <1 RK_PA3 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		otg_vbus_en: otg-vbus-en {
			rockchip,pins = <1 RK_PC6 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};
};

&pmu_io_domains {
	pmu1830-supply = <&vcc_1v8>;
	status = "okay";
};

&pwm2 {
	status = "okay";
};

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

&sdhci {
	max-frequency = <150000000>;
	bus-width = <8>;
	mmc-hs400-1_8v;
	mmc-hs400-enhanced-strobe;
	non-removable;
	status = "okay";
};

&spdif {
	status = "okay";
};

&sdmmc {
	bus-width = <4>;
	cap-mmc-highspeed;
	cap-sd-highspeed;
	cd-gpios = <&gpio0 RK_PA7 GPIO_ACTIVE_LOW>;
	disable-wp;
	max-frequency = <150000000>;
	pinctrl-names = "default";
	pinctrl-0 = <&sdmmc_clk &sdmmc_cd &sdmmc_cmd &sdmmc_bus4>;
	vqmmc-supply = <&vcc_sd>;
	sd-uhs-sdr104;
	status = "okay";
};

&cdn_dp {
	phys = <&tcphy0_dp>;
	extcon = <&virtual_pd>;
	status = "okay";
};

&tcphy0 {
	extcon = <&virtual_pd>;
	status = "okay";
};

&tcphy1 {
	status = "okay";
};

&tsadc {
	/* tshut mode 0:CRU 1:GPIO */
	rockchip,hw-tshut-mode = <1>;
	/* tshut polarity 0:LOW 1:HIGH */
	rockchip,hw-tshut-polarity = <1>;
	status = "okay";
};

&u2phy0 {
	status = "okay";

	u2phy0_host: host-port {
		phy-supply = <&vcc5v0_host>;
		status = "okay";
	};
};

&u2phy1 {
	status = "okay";

	u2phy1_host: host-port {
		phy-supply = <&vcc5v0_host>;
		status = "okay";
	};
};

&uart0 {
	status = "okay";
};

&uart2 {
	status = "okay";
};

&uart4 {
	status = "okay";
};

&usb_host0_ehci {
	status = "okay";
};

&usb_host0_ohci {
	status = "okay";
};

&usb_host1_ehci {
	status = "okay";
};

&usb_host1_ohci {
	status = "okay";
};

&usbdrd3_0 {
	status = "okay";
};

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

&usbdrd3_1 {
	status = "okay";
};

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

&vopb {
	status = "okay";
};

&vopb_mmu {
	status = "okay";
};

&vopl {
	status = "okay";
};

&vopl_mmu {
	status = "okay";
};

&vopl_out_dp {
	status = "disabled";
};

&dp_in_vopl {
	status = "disabled";
};

&vopb_out_hdmi {
	status = "disabled";
};

&hdmi_in_vopb {
	status = "disabled";
};
