// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2020 Aditya Prayoga <aditya@kobol.io>
 */

/*
 * The Kobol Helios64 is a board designed to operate as a NAS and optionally
 * ships with an enclosing that can host five 2.5" hard disks.
 *
 * See https://wiki.kobol.io/helios64/intro/ for further details.
 */

/dts-v1/;
#include "rk3399.dtsi"
#include "rk3399-opp.dtsi"

/ {
	model = "Kobol Helios64";
	compatible = "kobol,helios64", "rockchip,rk3399";

	aliases {
		mmc0 = &sdmmc;
		mmc1 = &sdhci;
		spi1 = &spi1;
		spi2 = &spi2;
		spi5 = &spi5;
	};

	avdd_0v9_s0: avdd-0v9-s0 {
		compatible = "regulator-fixed";
		regulator-name = "avdd_0v9_s0";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <900000>;
		regulator-max-microvolt = <900000>;
		vin-supply = <&vcc1v8_sys_s3>;
	};

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

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

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

	fan1 {
		/* fan connected to P7 */
		compatible = "pwm-fan";
		pwms = <&pwm0 0 40000 0>;
		cooling-levels = <0 80 170 255>;
	};

	fan2 {
		/* fan connected to P6 */
		compatible = "pwm-fan";
		pwms = <&pwm1 0 40000 0>;
		cooling-levels = <0 80 170 255>;
	};

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

		led-0 {
			label = "helios64:green:status";
			gpios = <&gpio0 RK_PB4 GPIO_ACTIVE_HIGH>;
			default-state = "on";
		};

		led-1 {
			label = "helios64:red:fault";
			gpios = <&gpio0 RK_PB5 GPIO_ACTIVE_HIGH>;
			default-state = "keep";
		};
	};

	hdd_a_power: hdd-a-power {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio1 RK_PA0 GPIO_ACTIVE_HIGH>;
		pinctrl-0 = <&hdd_a_power_en>;
		pinctrl-names = "default";
		regulator-always-on;
		regulator-boot-on;
		regulator-name = "hdd_a_power";
		startup-delay-us = <2000000>;
	};

	hdd_b_power: hdd-b-power {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio1 RK_PA1 GPIO_ACTIVE_HIGH>;
		pinctrl-0 = <&hdd_b_power_en>;
		pinctrl-names = "default";
		regulator-always-on;
		regulator-boot-on;
		regulator-name = "hdd_b_power";
		startup-delay-us = <2000000>;
	};

	pcie_power: pcie-power {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio1 RK_PD0 GPIO_ACTIVE_HIGH>;
		pinctrl-0 = <&pcie_pwr>;
		pinctrl-names = "default";
		regulator-boot-on;
		regulator-name = "pcie_power";
		startup-delay-us = <10000>;
		vin-supply = <&vcc5v0_perdev>;
	};

	usblan_power: usblan-power {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio1 RK_PC7 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&usb_lan_en>;
		regulator-name = "usblan_power";
		regulator-always-on;
		regulator-boot-on;
		vin-supply = <&vcc5v0_usb>;
	};

	vcc1v8_sys_s0: vcc1v8-sys-s0 {
		compatible = "regulator-fixed";
		regulator-name = "vcc1v8_sys_s0";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		vin-supply = <&vcc1v8_sys_s3>;
	};

	vcc3v0_sd: vcc3v0-sd {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio0 RK_PA1 GPIO_ACTIVE_HIGH>;
		regulator-name = "vcc3v0_sd";
		regulator-boot-on;
		regulator-min-microvolt = <3000000>;
		regulator-max-microvolt = <3000000>;
		pinctrl-names = "default";
		pinctrl-0 = <&sdmmc0_pwr_h>;
		vin-supply = <&vcc3v3_sys_s3>;
	};

	vcc3v3_sys_s3: vcc_lan: vcc3v3-sys-s3 {
		compatible = "regulator-fixed";
		regulator-name = "vcc3v3_sys_s3";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&vcc5v0_sys>;

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

	vcc5v0_perdev: vcc5v0-perdev {
		compatible = "regulator-fixed";
		regulator-name = "vcc5v0_perdev";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc12v_dcin_bkup>;
	};

	vcc5v0_sys: vcc5v0-sys {
		compatible = "regulator-fixed";
		regulator-name = "vcc5v0_sys";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc12v_dcin_bkup>;

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

	vcc5v0_usb: vcc5v0-usb {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio1 RK_PC6 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&vcc5v0_usb_en>;
		regulator-name = "vcc5v0_usb";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc5v0_perdev>;
	};

	vcc12v_dcin: vcc12v-dcin {
		compatible = "regulator-fixed";
		regulator-name = "vcc12v_dcin";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <12000000>;
		regulator-max-microvolt = <12000000>;
	};

	vcc12v_dcin_bkup: vcc12v-dcin-bkup {
		compatible = "regulator-fixed";
		regulator-name = "vcc12v_dcin_bkup";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <12000000>;
		regulator-max-microvolt = <12000000>;
		vin-supply = <&vcc12v_dcin>;
	};
};

/*
 * The system doesn't run stable with cpu freq enabled, so disallow the lower
 * frequencies until this problem is properly understood and resolved.
 */
&cluster0_opp {
	/delete-node/ opp00;
	/delete-node/ opp01;
	/delete-node/ opp02;
	/delete-node/ opp03;
	/delete-node/ opp04;
};

&cluster1_opp {
	/delete-node/ opp00;
	/delete-node/ opp01;
	/delete-node/ opp02;
	/delete-node/ opp03;
	/delete-node/ opp04;
	/delete-node/ opp05;
	/delete-node/ opp06;
};

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

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

&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-clock-parents = <&clkin_gmac>;
	assigned-clocks = <&cru SCLK_RMII_SRC>;
	clock_in_out = "input";
	phy-mode = "rgmii";
	phy-supply = <&vcc_lan>;
	pinctrl-names = "default";
	pinctrl-0 = <&rgmii_pins &gphy_reset>;
	rx_delay = <0x20>;
	tx_delay = <0x28>;
	snps,reset-active-low;
	snps,reset-delays-us = <0 10000 50000>;
	snps,reset-gpio = <&gpio3 RK_PB7 GPIO_ACTIVE_LOW>;
	status = "okay";
};

&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 = <&gpio0>;
		interrupts = <10 IRQ_TYPE_LEVEL_LOW>;
		clock-output-names = "xin32k", "rk808-clkout2";
		pinctrl-names = "default";
		pinctrl-0 = <&pmic_int_l>;
		rockchip,system-power-controller;
		vcc1-supply = <&vcc5v0_sys>;
		vcc2-supply = <&vcc5v0_sys>;
		vcc3-supply = <&vcc5v0_sys>;
		vcc4-supply = <&vcc5v0_sys>;
		vcc6-supply = <&vcc5v0_sys>;
		vcc7-supply = <&vcc5v0_sys>;
		vcc8-supply = <&vcc3v3_sys_s3>;
		vcc9-supply = <&vcc5v0_sys>;
		vcc10-supply = <&vcc5v0_sys>;
		vcc11-supply = <&vcc5v0_sys>;
		vcc12-supply = <&vcc3v3_sys_s3>;
		vddio-supply = <&vcc3v0_s3>;
		wakeup-source;
		#clock-cells = <1>;

		regulators {
			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;
				};
			};

			vcc1v8_sys_s3: DCDC_REG4 {
				regulator-name = "vcc1v8_sys_s3";
				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_sdio_s0: LDO_REG4 {
				regulator-name = "vcc_sdio_s0";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <3000000>;

				regulator-state-mem {
					regulator-on-in-suspend;
					regulator-suspend-microvolt = <3000000>;
				};
			};

			vcc3v0_s3: LDO_REG8 {
				regulator-name = "vcc3v0_s3";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <3000000>;
				regulator-max-microvolt = <3000000>;

				regulator-state-mem {
					regulator-on-in-suspend;
					regulator-suspend-microvolt = <3000000>;
				};
			};
		};
	};

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

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

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

	temp@4c {
		compatible = "national,lm75";
		reg = <0x4c>;
	};
};

&io_domains {
	audio-supply = <&vcc1v8_sys_s0>;
	bt656-supply = <&vcc1v8_sys_s0>;
	gpio1830-supply = <&vcc3v0_s3>;
	sdmmc-supply = <&vcc_sdio_s0>;
	status = "okay";
};

&pcie_phy {
	status = "okay";
};

&pcie0 {
	ep-gpios = <&gpio2 RK_PD4 GPIO_ACTIVE_HIGH>;
	max-link-speed = <2>;
	num-lanes = <2>;
	pinctrl-names = "default";
	status = "okay";

	vpcie12v-supply = <&vcc12v_dcin>;
	vpcie3v3-supply = <&pcie_power>;
	vpcie1v8-supply = <&avdd_1v8_s0>;
	vpcie0v9-supply = <&avdd_0v9_s0>;
};

&pinctrl {
	gmac {
		gphy_reset: gphy-reset {
			rockchip,pins = <3 RK_PB7 RK_FUNC_GPIO &pcfg_output_low>;
		};
	};

	leds {
		sys_grn_led_on: sys-grn-led-on {
			rockchip,pins = <0 RK_PB4 RK_FUNC_GPIO &pcfg_pull_down>;
		};

		sys_red_led_on: sys-red-led-on {
			rockchip,pins = <0 RK_PB5 RK_FUNC_GPIO &pcfg_pull_down>;
		};
	};

	pcie {
		pcie_pwr: pcie-pwr {
			rockchip,pins =
				<1 RK_PD0 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	pmic {
		pmic_int_l: pmic-int-l {
			rockchip,pins = <0 RK_PB2 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	power {
		hdd_a_power_en: hdd-a-power-en {
			rockchip,pins = <1 RK_PA0 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		hdd_b_power_en: hdd-b-power-en {
			rockchip,pins = <1 RK_PA1 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		vcc5v0_usb_en: vcc5v0-usb-en {
			rockchip,pins = <1 RK_PC6 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		usb_lan_en: usb-lan-en {
			rockchip,pins = <1 RK_PC7 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	vcc3v0-sd {
		sdmmc0_pwr_h: sdmmc0-pwr-h {
			rockchip,pins = <0 RK_PA1 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};
};

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

&pwm0 {
	/* pwm-fan on P7 */
	status = "okay";
};

&pwm1 {
	/* pwm-fan on P6 */
	status = "okay";
};

&sdhci {
	bus-width = <8>;
	mmc-hs200-1_8v;
	non-removable;
	vqmmc-supply = <&vcc1v8_sys_s0>;
	status = "okay";
};

&sdmmc {
	bus-width = <4>;
	cap-sd-highspeed;
	cd-gpios = <&gpio0 RK_PA7 GPIO_ACTIVE_LOW>;
	disable-wp;
	pinctrl-names = "default";
	pinctrl-0 = <&sdmmc_clk &sdmmc_cmd &sdmmc_cd &sdmmc_bus4>;
	vmmc-supply = <&vcc3v0_sd>;
	vqmmc-supply = <&vcc_sdio_s0>;
	status = "okay";
};

&spi1 {
	status = "okay";

	spiflash: flash@0 {
		compatible = "jedec,spi-nor";
		reg = <0x0>;
		spi-max-frequency = <25000000>;
		status = "okay";
		m25p,fast-read;
	};
};

/* UEXT connector */
&spi2 {
	status = "okay";
};

&spi5 {
	status = "okay";
};

&tcphy1 {
	/* phy for &usbdrd_dwc3_1 */
	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";
};

&u2phy1 {
	status = "okay";

	otg-port {
		/* phy for &usbdrd_dwc3_1 */
		phy-supply = <&vcc5v0_usb>;
		status = "okay";
	};
};

&uart2 {
	status = "okay";
};

&usbdrd3_1 {
	status = "okay";

	usb@fe900000 {
		dr_mode = "host";
		status = "okay";
		#address-cells = <1>;
		#size-cells = <0>;

		hub@1 {
			compatible = "usb2109,0815";
			reg = <1>;
			#address-cells = <1>;
			#size-cells = <0>;

			port@1 {
				reg = <1>;
				#trigger-source-cells = <0>;
			};

			port@2 {
				reg = <2>;
				#trigger-source-cells = <0>;
			};

			port@3 {
				reg = <3>;
				#trigger-source-cells = <0>;
			};

			device@4 {
				compatible = "usbbda,8156";
				reg = <4>;
				#address-cells = <2>;
				#size-cells = <0>;

				interface@0 {	/* interface 0 of configuration 1 */
					compatible = "usbbda,8156.config1.0";
					reg = <0 1>;
				};
			};
		};
	};
};