// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
 * Copyright (c) 2019 Vamrs Limited
 * Copyright (c) 2019 Amarula Solutions(India)
 */

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/rockchip.h>
#include <dt-bindings/pwm/pwm.h>

/ {
	compatible = "vamrs,rk3399pro-vmarc-som", "rockchip,rk3399pro";

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

	vcc3v3_pcie: vcc-pcie-regulator {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio4 RK_PD4 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&pcie_pwr>;
		regulator-name = "vcc3v3_pcie";
		regulator-always-on;
		regulator-boot-on;
		vin-supply = <&vcc5v0_sys>;
	};
};

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

&emmc_phy {
	status = "okay";
};

&gmac {
	assigned-clocks = <&cru SCLK_RMII_SRC>;
	phy-supply = <&vcc_lan>;
	snps,reset-gpio = <&gpio3 RK_PB7 GPIO_ACTIVE_LOW>;
};

&hdmi {
	ddc-i2c-bus = <&i2c3>;
	pinctrl-names = "default";
	pinctrl-0 = <&hdmi_cec>;
};

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

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

		vcc1-supply = <&vcc5v0_sys>;
		vcc2-supply = <&vcc5v0_sys>;
		vcc3-supply = <&vcc5v0_sys>;
		vcc4-supply = <&vcc5v0_sys>;
		vcc5-supply = <&vcc_buck5>;
		vcc6-supply = <&vcc_buck5>;
		vcc7-supply = <&vcc5v0_sys>;
		vcc8-supply = <&vcc3v3_sys>;
		vcc9-supply = <&vcc5v0_sys>;

		regulators {
			vdd_log: DCDC_REG1 {
				regulator-name = "vdd_log";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <750000>;
				regulator-max-microvolt = <1350000>;
				regulator-initial-mode = <0x2>;
				regulator-state-mem {
					regulator-off-in-suspend;
					regulator-suspend-microvolt = <900000>;
				};
			};

			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-initial-mode = <0x2>;
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcc_ddr: DCDC_REG3 {
				regulator-name = "vcc_ddr";
				regulator-always-on;
				regulator-boot-on;
				regulator-initial-mode = <0x2>;
				regulator-state-mem {
					regulator-on-in-suspend;
				};
			};

			vcc3v3_sys: DCDC_REG4 {
				regulator-name = "vcc3v3_sys";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-initial-mode = <0x2>;
				regulator-state-mem {
					regulator-on-in-suspend;
					regulator-suspend-microvolt = <3300000>;
				};
			};

			vcc_buck5: DCDC_REG5 {
				regulator-name = "vcc_buck5";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <2200000>;
				regulator-max-microvolt = <2200000>;
				regulator-state-mem {
					regulator-on-in-suspend;
					regulator-suspend-microvolt = <2200000>;
				};
			};

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

			vcc_1v8: LDO_REG2 {
				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>;
				};
			};

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

			vcca_1v8: LDO_REG4 {
				regulator-name = "vcca_1v8";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1850000>;
				regulator-max-microvolt = <1850000>;
				regulator-state-mem {
					regulator-on-in-suspend;
					regulator-suspend-microvolt = <1850000>;
				};
			};

			/*
			 * As per BSP, but schematic not showing any regulator
			 * pin for LD05.
			 */
			vdd1v5_dvp: LDO_REG5 {
				regulator-name = "vdd1v5_dvp";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1500000>;
				regulator-max-microvolt = <1500000>;
				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;
				};
			};

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

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

			/*
			 * As per BSP, but schematic not showing any regulator
			 * pin for LD09.
			 */
			vcc_sd: LDO_REG9 {
				regulator-name = "vcc_sd";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcc5v0_usb2: SWITCH_REG1 {
				regulator-name = "vcc5v0_usb2";
				regulator-min-microvolt = <5000000>;
				regulator-max-microvolt = <5000000>;
				regulator-state-mem {
					regulator-on-in-suspend;
					regulator-suspend-microvolt = <5000000>;
				};
			};

			vccio_3v3: vcc_lan: SWITCH_REG2 {
				regulator-name = "vccio_3v3";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};
		};
	};
};

&i2c1 {
	i2c-scl-falling-time-ns = <30>;
	i2c-scl-rising-time-ns = <140>;
	status = "okay";
};

&i2c2 {
	clock-frequency = <400000>;
	status = "okay";

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

&i2c3 {
	i2c-scl-rising-time-ns = <450>;
	i2c-scl-falling-time-ns = <15>;
	status = "okay";
};

&io_domains {
	status = "okay";
	bt656-supply = <&vcca_1v8>;
	gpio1830-supply = <&vccio_3v0>;
	sdmmc-supply = <&vccio_sd>;
};

&pcie_phy {
	status = "okay";
};

&pcie0 {
	ep-gpios = <&gpio0 RK_PB4 GPIO_ACTIVE_HIGH>;
	num-lanes = <4>;
	pinctrl-0 = <&pcie_clkreqnb_cpm>;
	pinctrl-names = "default";
	vpcie0v9-supply = <&vcca_0v9>;	/* VCC_0V9_S0 */
	vpcie1v8-supply = <&vcca_1v8>;	/* VCC_1V8_S0 */
	vpcie3v3-supply = <&vcc3v3_pcie>;
	status = "okay";
};

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

	pcie {
		pcie_pwr: pcie-pwr {
			rockchip,pins = <4 RK_PD4 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

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

	sdio-pwrseq {
		wifi_enable_h: wifi-enable-h {
			rockchip,pins = <2 RK_PD3 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	vbus_host {
		usb1_en_oc: usb1-en-oc {
			rockchip,pins = <4 RK_PD1 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

	vbus_typec {
		usb0_en_oc: usb0-en-oc {
			rockchip,pins = <4 RK_PD2 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};
};

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

&sdio_pwrseq {
	/*
	 * On the module itself this is one of these (depending
	 * on the actual card populated):
	 * - SDIO_RESET_L_WL_REG_ON
	 * - PDN (power down when low)
	 */
	reset-gpios = <&gpio2 RK_PD3 GPIO_ACTIVE_LOW>;
};

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

&sdmmc {
	cd-gpios = <&gpio0 RK_PA7 GPIO_ACTIVE_LOW>;
	max-frequency = <150000000>;
};

&tcphy0 {
	status = "okay";
};

&tsadc {
	rockchip,hw-tshut-mode = <1>;
	rockchip,hw-tshut-polarity = <1>;
	status = "okay";
};

&u2phy0 {
	status = "okay";

	u2phy0_otg: otg-port {
		phy-supply = <&vbus_typec>;
		status = "okay";
	};

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


&u2phy1 {
	status = "okay";

	u2phy1_host: host-port {
		phy-supply = <&vbus_host>;
		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 {
	status = "okay";
};

&vbus_host {
	enable-active-high;
	gpio = <&gpio4 RK_PD1 GPIO_ACTIVE_HIGH>; /* USB1_EN_OC# */
	pinctrl-names = "default";
	pinctrl-0 = <&usb1_en_oc>;
};

&vbus_typec {
	enable-active-high;
	gpio = <&gpio4 RK_PD2 GPIO_ACTIVE_HIGH>; /* USB0_EN_OC# */
	pinctrl-names = "default";
	pinctrl-0 = <&usb0_en_oc>;
};