#include <dt-bindings/input/input.h>
#include <dt-bindings/leds/common.h>
#include <dt-bindings/gpio/gpio.h>

/ {
	aliases {
		mmc0 = &sdhci;
		rtc0 = &rtc_rv8263;
	};

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

	keys: keys {
		compatible = "gpio-keys";
		pinctrl-0 = <&reset_button_pin>;
		pinctrl-names = "default";

		key-reset {
			label = "reset";
			gpios = <&gpio0 RK_PB5 GPIO_ACTIVE_LOW>;
			linux,code = <KEY_RESTART>;
		};
	};

	leds: leds {
		compatible = "gpio-leds";

		led-0 {
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_DISK;
			gpios = <&gpio1 RK_PD5 GPIO_ACTIVE_LOW>;
			label = "hdd1:green:disk";
			linux,default-trigger = "disk-activity";
			pinctrl-names = "default";
			pinctrl-0 = <&hdd1_led_pin>;
		};
	};

	dc_12v: regulator-dc-12v {
		compatible = "regulator-fixed";
		regulator-name = "dc_12v";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <12000000>;
		regulator-max-microvolt = <12000000>;
	};

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

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

	vcc5v0_otg: regulator-vcc5v0-otg {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio0 RK_PA5 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&vcc5v0_otg_en>;
		regulator-name = "vcc5v0_otg";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc5v0_usb>;
	};

	vcc5v0_sys: regulator-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 = <&dc_12v>;
	};

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

/* Connected USB3 on TS133 / SATA1 on all the others */
&combphy1 {
	status = "okay";
};

/* Connected to SATA2 */
&combphy2 {
	status = "okay";
};

&cpu0 {
	cpu-supply = <&vdd_cpu>;
};

&cpu1 {
	cpu-supply = <&vdd_cpu>;
};

&cpu2 {
	cpu-supply = <&vdd_cpu>;
};

&cpu3 {
	cpu-supply = <&vdd_cpu>;
};

/*
 * The MCU can provide system temperature too, but only by polling and of
 * course also cannot set trip points. So attach to the cpu thermal-zone
 * instead to control the fan.
 */
&cpu_thermal {
	trips {
		case_fan0: case-fan0 {
			hysteresis = <2000>;
			temperature = <35000>;
			type = "active";
		};

		case_fan1: case-fan1 {
			hysteresis = <2000>;
			temperature = <45000>;
			type = "active";
		};

		case_fan2: case-fan2 {
			hysteresis = <2000>;
			temperature = <65000>;
			type = "active";
		};
	};

	cooling-maps {
		/*
		 * Always provide some air movement, due to small case
		 * full of harddrives.
		 */
		map1 {
			cooling-device = <&fan THERMAL_NO_LIMIT 1>;
			trip = <&case_fan0>;
		};

		map2 {
			cooling-device = <&fan 2 3>;
			trip = <&case_fan1>;
		};

		map3 {
			cooling-device = <&fan 4 THERMAL_NO_LIMIT>;
			trip = <&case_fan2>;
		};
	};
};

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

&i2c0 {
	status = "okay";

	pmic@20 {
		compatible = "rockchip,rk809";
		reg = <0x20>;
		interrupt-parent = <&gpio0>;
		interrupts = <RK_PA3 IRQ_TYPE_LEVEL_LOW>;
		#clock-cells = <1>;
		pinctrl-names = "default";
		pinctrl-0 = <&pmic_int_l>;
		system-power-controller;
		vcc1-supply = <&vcc3v3_sys>;
		vcc2-supply = <&vcc3v3_sys>;
		vcc3-supply = <&vcc3v3_sys>;
		vcc4-supply = <&vcc3v3_sys>;
		vcc5-supply = <&vcc3v3_sys>;
		vcc6-supply = <&vcc3v3_sys>;
		vcc7-supply = <&vcc3v3_sys>;
		vcc8-supply = <&vcc3v3_sys>;
		vcc9-supply = <&vcc3v3_sys>;
		wakeup-source;

		regulators {
			vdd_logic: DCDC_REG1 {
				regulator-name = "vdd_logic";
				regulator-always-on;
				regulator-boot-on;
				regulator-initial-mode = <0x2>;
				regulator-min-microvolt = <500000>;
				regulator-max-microvolt = <1350000>;
				regulator-ramp-delay = <6001>;

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

			vdd_gpu: DCDC_REG2 {
				regulator-name = "vdd_gpu";
				regulator-always-on;
				regulator-initial-mode = <0x2>;
				regulator-min-microvolt = <500000>;
				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-initial-mode = <0x2>;

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

			vdd_npu: DCDC_REG4 {
				regulator-name = "vdd_npu";
				regulator-initial-mode = <0x2>;
				regulator-min-microvolt = <500000>;
				regulator-max-microvolt = <1350000>;
				regulator-ramp-delay = <6001>;

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

			vcc_1v8: DCDC_REG5 {
				regulator-name = "vcc_1v8";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;

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

			vdda0v9_image: LDO_REG1 {
				regulator-name = "vdda0v9_image";
				regulator-always-on;
				regulator-min-microvolt = <900000>;
				regulator-max-microvolt = <900000>;

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

			vdda_0v9: LDO_REG2 {
				regulator-name = "vdda_0v9";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <900000>;
				regulator-max-microvolt = <900000>;

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

			vdda0v9_pmu: LDO_REG3 {
				regulator-name = "vdda0v9_pmu";
				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>;
				};
			};

			vccio_acodec: LDO_REG4 {
				regulator-name = "vccio_acodec";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;

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

			vccio_sd: LDO_REG5 {
				regulator-name = "vccio_sd";
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <3300000>;

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

			vcc3v3_pmu: LDO_REG6 {
				regulator-name = "vcc3v3_pmu";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;

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

			vcca_1v8: LDO_REG7 {
				regulator-name = "vcca_1v8";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;

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

			vcca1v8_pmu: LDO_REG8 {
				regulator-name = "vcca1v8_pmu";
				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>;
				};
			};

			vcca1v8_image: LDO_REG9 {
				regulator-name = "vcca1v8_image";
				regulator-always-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;

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

			vcc_3v3: SWITCH_REG1 {
				regulator-name = "vcc_3v3";
				regulator-always-on;
				regulator-boot-on;

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

			vcc3v3_sd: SWITCH_REG2 {
				regulator-name = "vcc3v3_sd";
				/*
				 * turning this off, breaks access to both
				 * PCIe controllers, refclk generator perhaps
				 */
				regulator-always-on;
				regulator-boot-on;

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

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

&i2c1 {
	status = "okay";

	rtc_rv8263: rtc@51 {
		compatible = "microcrystal,rv8263";
		reg = <0x51>;
		wakeup-source;
	};

	/* eeprom for vital-product-data on the mainboard */
	eeprom@54 {
		compatible = "giantec,gt24c04a", "atmel,24c04";
		reg = <0x54>;
		label = "VPD_MB";
		num-addresses = <2>;
		pagesize = <16>;
		read-only;
	};
};

&pinctrl {
	keys {
		copy_button_pin: copy-button-pin {
			rockchip,pins = <0 RK_PB6 RK_FUNC_GPIO &pcfg_pull_up>;
		};

		reset_button_pin: reset-button-pin {
			rockchip,pins = <0 RK_PB5 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

	leds {
		hdd1_led_pin: hdd1-led-pin {
			rockchip,pins = <1 RK_PD5 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

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

	usb {
		vcc5v0_host_en: vcc5v0-host-en {
			rockchip,pins = <0 RK_PA6 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		vcc5v0_otg_en: vcc5v0-otg-en {
			rockchip,pins = <0 RK_PA5 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};
};

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

&sata2 {
	status = "okay";
};

&sdhci {
	bus-width = <8>;
	max-frequency = <200000000>;
	non-removable;
	pinctrl-names = "default";
	pinctrl-0 = <&emmc_bus8 &emmc_clk &emmc_cmd &emmc_datastrobe>;
	status = "okay";
};

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

/*
 * Connected to an MCU, that provides access to more LEDs,
 * buzzer, fan control and more.
 */
&uart0 {
	status = "okay";

	mcu: mcu {
		fan: fan-0 {
			#cooling-cells = <2>;
			cooling-levels = <0 64 89 128 166 204 221 238>;
		};

		nvmem-layout {
			compatible = "fixed-layout";
			#address-cells = <1>;
			#size-cells = <1>;

			serial-number@0 {
				reg = <0x0 0x13>;
			};

			ext-port@22 {
				reg = <0x22 0x2>;
			};

			mac0: mac@24 {
				compatible = "mac-base";
				reg = <0x24 0x11>;
				#nvmem-cell-cells = <1>;
			};

			mac1: mac@35 {
				compatible = "mac-base";
				reg = <0x35 0x11>;
				#nvmem-cell-cells = <1>;
			};

			mac2: mac@46 {
				compatible = "mac-base";
				reg = <0x46 0x11>;
				#nvmem-cell-cells = <1>;
			};

			mac3: mac@57 {
				compatible = "mac-base";
				reg = <0x57 0x11>;
				#nvmem-cell-cells = <1>;
			};

			mac4: mac@68 {
				compatible = "mac-base";
				reg = <0x68 0x11>;
				#nvmem-cell-cells = <1>;
			};

			mac5: mac@79 {
				compatible = "mac-base";
				reg = <0x79 0x11>;
				#nvmem-cell-cells = <1>;
			};

			mac6: mac@8a {
				compatible = "mac-base";
				reg = <0x8a 0x11>;
				#nvmem-cell-cells = <1>;
			};

			mac7: mac@9b {
				compatible = "mac-base";
				reg = <0x9b 0x11>;
				#nvmem-cell-cells = <1>;
			};
		};
	};
};

/*
 * Pins available on CN3 connector at TTL voltage level (3V3).
 * ,_  _.
 * |1234|  1=TX 2=VCC
 * `----'  3=RX 4=GND
 */
&uart2 {
	status = "okay";
};

&usb2phy0 {
	status = "okay";
};

/* connected to usb_host0_xhci */
&usb2phy0_otg {
	phy-supply = <&vcc5v0_otg>;
	status = "okay";
};

/* front port */
&usb_host0_xhci {
	dr_mode = "host";
	status = "okay";
};
