// SPDX-License-Identifier: (GPL-2.0+ OR MIT)

/dts-v1/;

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/rockchip.h>
#include <dt-bindings/soc/rockchip,vop2.h>
#include "rk3566.dtsi"

/ {
	model = "Pine64 RK3566 SoQuartz SOM";
	compatible = "pine64,soquartz", "rockchip,rk3566";

	aliases {
		ethernet0 = &gmac1;
		mmc0 = &sdmmc0;
		mmc1 = &sdhci;
		mmc2 = &sdmmc1;
	};

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

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

	hdmi-con {
		compatible = "hdmi-connector";
		type = "a";

		port {
			hdmi_con_in: endpoint {
				remote-endpoint = <&hdmi_out_con>;
			};
		};
	};

	leds {
		compatible = "gpio-leds";

		led_diy: led-diy {
			label = "diy-led";
			default-state = "on";
			gpios = <&gpio0 RK_PC1 GPIO_ACTIVE_LOW>;
			linux,default-trigger = "heartbeat";
			pinctrl-names = "default";
			pinctrl-0 = <&diy_led_enable_h>;
			retain-state-suspended;
			status = "disabled";
		};

		led_work: led-work {
			label = "work-led";
			default-state = "off";
			gpios = <&gpio0 RK_PC0 GPIO_ACTIVE_LOW>;
			pinctrl-names = "default";
			pinctrl-0 = <&work_led_enable_h>;
			retain-state-suspended;
			status = "disabled";
		};
	};

	sdio_pwrseq: sdio-pwrseq {
		status = "okay";
		compatible = "mmc-pwrseq-simple";
		clocks = <&rk809 1>;
		clock-names = "ext_clock";
		pinctrl-names = "default";
		pinctrl-0 = <&wifi_enable_h>;
		reset-gpios = <&gpio2 RK_PC2 GPIO_ACTIVE_LOW>;
	};

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

	/* sourced from vbus, vbus is provided by the carrier board */
	vcc5v0_sys: vcc5v0-sys-regulator {
		compatible = "regulator-fixed";
		regulator-name = "vcc5v0_sys";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vbus>;
	};

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

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

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

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

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

&gmac1 {
	assigned-clocks = <&cru SCLK_GMAC1_RX_TX>, <&cru SCLK_GMAC1_RGMII_SPEED>, <&cru SCLK_GMAC1>;
	assigned-clock-parents = <&cru SCLK_GMAC1_RGMII_SPEED>, <&cru SCLK_GMAC1>, <&gmac1_clkin>;
	clock_in_out = "input";
	phy-supply = <&vcc_3v3>;
	phy-mode = "rgmii";
	pinctrl-names = "default";
	pinctrl-0 = <&gmac1m0_miim
		     &gmac1m0_tx_bus2
		     &gmac1m0_rx_bus2
		     &gmac1m0_rgmii_clk
		     &gmac1m0_clkinout
		     &gmac1m0_rgmii_bus>;
	snps,reset-gpio = <&gpio0 RK_PC3 GPIO_ACTIVE_LOW>;
	snps,reset-active-low;
	/* Reset time is 20ms, 100ms for rtl8211f, also works well here */
	snps,reset-delays-us = <0 20000 100000>;
	tx_delay = <0x30>;
	rx_delay = <0x10>;
	phy-handle = <&rgmii_phy1>;
	status = "disabled";
};

&gpio0 {
	nextrst-hog {
		gpio-hog;
		/*
		 * GPIO_ACTIVE_LOW + output-low here means that the pin is set
		 * to high, because output-low decides the value pre-inversion.
		 */
		gpios = <RK_PA5 GPIO_ACTIVE_LOW>;
		line-name = "nEXTRST";
		output-low;
	};
};

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

&hdmi {
	avdd-0v9-supply = <&vdda0v9_image>;
	avdd-1v8-supply = <&vcca1v8_image>;
	status = "okay";
};

&hdmi_in {
	hdmi_in_vp0: endpoint {
		remote-endpoint = <&vp0_out_hdmi>;
	};
};

&hdmi_out {
	hdmi_out_con: endpoint {
		remote-endpoint = <&hdmi_con_in>;
	};
};

&hdmi_sound {
	status = "okay";
};

&i2c0 {
	status = "okay";

	vdd_cpu: regulator@1c {
		compatible = "tcs,tcs4525";
		reg = <0x1c>;
		fcs,suspend-voltage-selector = <1>;
		regulator-name = "vdd_cpu";
		regulator-min-microvolt = <800000>;
		regulator-max-microvolt = <1150000>;
		regulator-ramp-delay = <2300>;
		regulator-always-on;
		regulator-boot-on;
		vin-supply = <&vcc5v0_sys>;

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

	rk809: pmic@20 {
		compatible = "rockchip,rk809";
		reg = <0x20>;
		interrupt-parent = <&gpio0>;
		interrupts = <RK_PA3 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 = <&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>;

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

			vdd_gpu: DCDC_REG2 {
				regulator-name = "vdd_gpu";
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <500000>;
				regulator-max-microvolt = <1350000>;
				regulator-ramp-delay = <6001>;
				regulator-initial-mode = <0x2>;
					regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

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

			vdd_npu: DCDC_REG4 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <500000>;
				regulator-max-microvolt = <1350000>;
				regulator-initial-mode = <0x2>;
				regulator-name = "vdd_npu";
				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-on-in-suspend;
					regulator-suspend-microvolt = <1800000>;
				};
			};

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

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

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

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

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

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

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

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

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

			vcc_3v3: SWITCH_REG1 {
				regulator-name = "vcc_3v3";
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			vcc3v3_sd: SWITCH_REG2 {
				regulator-name = "vcc3v3_sd";
				status = "disabled";
				regulator-state-mem {
					regulator-on-in-suspend;
				};
			};

		};
	};
};

/*
 * i2c1 is exposed on CM1 / Module1A
 * pin 80 - i2c1_scl_m0, pullup to vcc3v3_pmu
 * pin 82 - i2c1_sda_m0, pullup to vcc3v3_pmu
 */
&i2c1 {
	status = "disabled";
};

/*
 * i2c2 is exposed on CM1 / Module1A
 * pin 56 - i2c2_scl_m1, pullup to vcc_3v3, shared with i2s1_8ch
 * pin 58 - i2c2_sda_m1, pullup to vcc_3v3
 */
&i2c2 {
	pinctrl-names = "default";
	pinctrl-0 = <&i2c2m1_xfer>;
	status = "disabled";
};

/*
 * i2c3 is exposed on CM1 / Module1A
 * pin 35 - i2c3_scl_m0, pullup to vcc_3v3
 * pin 36 - i2c3_sda_m0, pullup to vcc_3v3
 */
&i2c3 {
	status = "disabled";
};

/*
 * i2c4 is exposed on CM2 / Module1B
 * pin 45 - i2c4_scl_m1
 * pin 47 - i2c4_sda_m1
 */
&i2c4 {
	pinctrl-names = "default";
	pinctrl-0 = <&i2c4m1_xfer>;
	status = "disabled";
};

&i2s0_8ch {
	status = "okay";
};

/*
 * i2s1_8ch is exposed on CM1 / Module1A
 * pin 24 - i2s1_sdi1_m1
 * pin 25 - i2s1_sdo0_m1
 * pin 26 - i2s1_lrck_tx_m1
 * pin 27 - i2s1_sdi0_m1
 * pin 29 - i2s1_sdi3_m1
 * pin 30 - i2s1_sdi2_m1
 * pin 40 - i2s1_sdo1_m1, shared with spi3
 * pin 41 - i2s1_sdo2_m1
 * pin 49 - i2s1_sclk_tx_m1
 * pin 50 - i2s1_mclk_m1
 * pin 56 - i2s1_sdo3_m1, shared with i2c2
 */
&i2s1_8ch {
	pinctrl-names = "default";
	pinctrl-0 = <&i2s1m1_sclktx &i2s1m1_sclkrx
		     &i2s1m1_lrcktx &i2s1m1_lrckrx
		     &i2s1m1_sdi0   &i2s1m1_sdi1
		     &i2s1m1_sdi2   &i2s1m1_sdi3
		     &i2s1m1_sdo0   &i2s1m1_sdo1
		     &i2s1m1_sdo2   &i2s1m1_sdo3>;
	status = "disabled";
};

&mdio1 {
	rgmii_phy1: ethernet-phy@0 {
		compatible = "ethernet-phy-ieee802.3-c22";
		reg = <0>;
		status = "disabled";
	};
};

&pcie2x1 {
	pinctrl-names = "default";
	pinctrl-0 = <&pcie_reset_h>;
	reset-gpios = <&gpio1 RK_PB2 GPIO_ACTIVE_HIGH>;
};

&pinctrl {
	bt {
		bt_enable_h: bt-enable-h {
			rockchip,pins = <2 RK_PB7 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		bt_host_wake_l: bt-host-wake-l {
			rockchip,pins = <2 RK_PC0 RK_FUNC_GPIO &pcfg_pull_down>;
		};

		bt_wake_l: bt-wake-l {
			rockchip,pins = <2 RK_PC1 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	leds {
		work_led_enable_h: work-led-enable-h {
			rockchip,pins = <0 RK_PC0 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		diy_led_enable_h: diy-led-enable-h {
			rockchip,pins = <0 RK_PC1 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	pcie {
		pcie_clkreq_h: pcie-clkreq-h {
			rockchip,pins = <1 RK_PB0 RK_FUNC_GPIO &pcfg_pull_none>;
		};
		pcie_reset_h: pcie-reset-h {
			rockchip,pins = <1 RK_PB2 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

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

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

&pmu_io_domains {
	pmuio1-supply = <&vcc3v3_pmu>;
	pmuio2-supply = <&vcc3v3_pmu>;
	vccio1-supply = <&vcc_3v3>;
	vccio2-supply = <&vcc_1v8>;
	vccio3-supply = <&vccio_sd>;
	vccio4-supply = <&vcc_1v8>;
	vccio5-supply = <&vcc_3v3>;
	vccio6-supply = <&vcc_3v3>;
	vccio7-supply = <&vcc_3v3>;
	status = "okay";
};

/*
 * saradc is exposed on CM1 / Module1A
 * pin 94 - saradc_vin3
 * pin 96 - saradc_vin2
 */
&saradc {
	vref-supply = <&vcca_1v8>;
	status = "disabled";
};

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

&sdmmc0 {
	broken-cd;
	bus-width = <4>;
	cap-sd-highspeed;
	disable-wp;
	pinctrl-names = "default";
	pinctrl-0 = <&sdmmc0_bus4 &sdmmc0_clk &sdmmc0_cmd &sdmmc0_det>;
	vqmmc-supply = <&vccio_sd>;
	status = "disabled";
};

&sdmmc1 {
	bus-width = <4>;
	cap-sd-highspeed;
	cap-sdio-irq;
	keep-power-in-suspend;
	mmc-pwrseq = <&sdio_pwrseq>;
	non-removable;
	pinctrl-names = "default";
	pinctrl-0 = <&sdmmc1_bus4 &sdmmc1_cmd &sdmmc1_clk>;
	sd-uhs-sdr50;
	vmmc-supply = <&vcc3v3_sys>;
	vqmmc-supply = <&vcc_1v8>;
	status = "okay";
};

/*
 * spi3 is exposed on CM1 / Module1A
 * pin 37 - spi3_cs1_m0
 * pin 38 - spi3_clk_m0
 * pin 39 - spi3_cs0_m0
 * pin 40 - spi3_miso_m0, shared with i2s1_8ch
 * pin 44 - spi3_mosi_m0
 */
&spi3 {
	status = "disabled";
};

&tsadc {
	status = "okay";
};

&uart1 {
	pinctrl-names = "default";
	pinctrl-0 = <&uart1m0_xfer &uart1m0_ctsn &uart1m0_rtsn>;
	uart-has-rtscts;
	status = "okay";

	bluetooth {
		compatible = "brcm,bcm43438-bt";
		clocks = <&rk809 1>;
		clock-names = "lpo";
		device-wakeup-gpios = <&gpio2 RK_PC1 GPIO_ACTIVE_HIGH>;
		host-wakeup-gpios = <&gpio2 RK_PC0 GPIO_ACTIVE_HIGH>;
		shutdown-gpios = <&gpio2 RK_PB7 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&bt_host_wake_l &bt_wake_l &bt_enable_h>;
		vbat-supply = <&vcc3v3_sys>;
		vddio-supply = <&vcca1v8_pmu>;
	};
};

/*
 * uart2 is exposed on CM1 / Module1A
 * pin 51 - uart2_rx_m0
 * pin 55 - uart2_tx_m0
 */
&uart2 {
	status = "disabled";
};

/*
 * uart7 is exposed on CM1 / Module1A
 * pin 46 - uart7_tx_m2
 * pin 47 - uart7_rx_m2
 */
&uart7 {
	pinctrl-names = "default";
	pinctrl-0 = <&uart7m2_xfer>;
	status = "disabled";
};

/* dwc3_otg is the only usb port available */
&usb2phy0 {
	status = "disabled";
};

&usb2phy0_otg {
	status = "disabled";
};

&usb_host0_xhci {
	status = "disabled";
};

&vop {
	assigned-clocks = <&cru DCLK_VOP0>, <&cru DCLK_VOP1>;
	assigned-clock-parents = <&pmucru PLL_HPLL>, <&cru PLL_VPLL>;
	status = "okay";
};

&vop_mmu {
	status = "okay";
};

&vp0 {
	vp0_out_hdmi: endpoint@ROCKCHIP_VOP2_EP_HDMI0 {
		reg = <ROCKCHIP_VOP2_EP_HDMI0>;
		remote-endpoint = <&hdmi_in_vp0>;
	};
};