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

/dts-v1/;

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/input/linux-event-codes.h>
#include <dt-bindings/leds/common.h>
#include <dt-bindings/pinctrl/rockchip.h>
#include <dt-bindings/soc/rockchip,vop2.h>
#include "rk3566.dtsi"

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

	adc-joystick {
		compatible = "adc-joystick";
		io-channels = <&adc_mux 0>,
			      <&adc_mux 1>,
			      <&adc_mux 2>,
			      <&adc_mux 3>;
		pinctrl-0 = <&joy_mux_en>;
		pinctrl-names = "default";
		poll-interval = <60>;
		#address-cells = <1>;
		#size-cells = <0>;

		axis@0 {
			reg = <0>;
			abs-flat = <32>;
			abs-fuzz = <32>;
			abs-range = <1023 15>;
			linux,code = <ABS_X>;
		};

		axis@1 {
			reg = <1>;
			abs-flat = <32>;
			abs-fuzz = <32>;
			abs-range = <15 1023>;
			linux,code = <ABS_RX>;
		};

		axis@2 {
			reg = <2>;
			abs-flat = <32>;
			abs-fuzz = <32>;
			abs-range = <15 1023>;
			linux,code = <ABS_Y>;
		};

		axis@3 {
			reg = <3>;
			abs-flat = <32>;
			abs-fuzz = <32>;
			abs-range = <1023 15>;
			linux,code = <ABS_RY>;
		};
	};

	adc_keys: adc-keys {
		compatible = "adc-keys";
		io-channels = <&saradc 0>;
		io-channel-names = "buttons";
		keyup-threshold-microvolt = <1800000>;
		poll-interval = <60>;

		/*
		 * Button is mapped to F key in BSP kernel, but
		 * according to input guidelines it should be mode.
		 */
		button-mode {
			label = "MODE";
			linux,code = <BTN_MODE>;
			press-threshold-microvolt = <1750>;
		};
	};

	adc_mux: adc-mux {
		compatible = "io-channel-mux";
		channels = "left_x", "right_x", "left_y", "right_y";
		#io-channel-cells = <1>;
		io-channels = <&saradc 3>;
		io-channel-names = "parent";
		mux-controls = <&gpio_mux>;
		settle-time-us = <100>;
	};

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

		button-b {
			gpios = <&gpio3 RK_PC3 GPIO_ACTIVE_LOW>;
			label = "SOUTH";
			linux,code = <BTN_SOUTH>;
		};

		button-down {
			gpios = <&gpio3 RK_PA4 GPIO_ACTIVE_LOW>;
			label = "DPAD-DOWN";
			linux,code = <BTN_DPAD_DOWN>;
		};

		button-l1 {
			gpios = <&gpio3 RK_PB1 GPIO_ACTIVE_LOW>;
			label = "TL";
			linux,code = <BTN_TL>;
		};

		button-l2 {
			gpios = <&gpio3 RK_PB2 GPIO_ACTIVE_LOW>;
			label = "TL2";
			linux,code = <BTN_TL2>;
		};

		button-select {
			gpios = <&gpio3 RK_PB6 GPIO_ACTIVE_LOW>;
			label = "SELECT";
			linux,code = <BTN_SELECT>;
		};

		button-start {
			gpios = <&gpio3 RK_PB5 GPIO_ACTIVE_LOW>;
			label = "START";
			linux,code = <BTN_START>;
		};

		button-thumbl {
			gpios = <&gpio3 RK_PA1 GPIO_ACTIVE_LOW>;
			label = "THUMBL";
			linux,code = <BTN_THUMBL>;
		};

		button-thumbr {
			gpios = <&gpio3 RK_PA2 GPIO_ACTIVE_LOW>;
			label = "THUMBR";
			linux,code = <BTN_THUMBR>;
		};

		button-up {
			gpios = <&gpio3 RK_PA3 GPIO_ACTIVE_LOW>;
			label = "DPAD-UP";
			linux,code = <BTN_DPAD_UP>;
		};

		button-x {
			gpios = <&gpio3 RK_PC0 GPIO_ACTIVE_LOW>;
			label = "NORTH";
			linux,code = <BTN_NORTH>;
		};
	};

	gpio_keys_vol: gpio-keys-vol {
		compatible = "gpio-keys";
		autorepeat;
		pinctrl-0 = <&btn_pins_vol>;
		pinctrl-names = "default";

		button-vol-down {
			gpios = <&gpio3 RK_PB0 GPIO_ACTIVE_LOW>;
			label = "VOLUMEDOWN";
			linux,code = <KEY_VOLUMEDOWN>;
		};

		button-vol-up {
			gpios = <&gpio3 RK_PA7 GPIO_ACTIVE_LOW>;
			label = "VOLUMEUP";
			linux,code = <KEY_VOLUMEUP>;
		};
	};

	gpio_mux: mux-controller {
		compatible = "gpio-mux";
		mux-gpios = <&gpio0 RK_PB6 GPIO_ACTIVE_LOW>,
			    <&gpio0 RK_PB7 GPIO_ACTIVE_LOW>;
		#mux-control-cells = <0>;
	};

	hdmi-con {
		compatible = "hdmi-connector";
		ddc-i2c-bus = <&i2c5>;
		type = "c";

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

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

		green_led: led-0 {
			color = <LED_COLOR_ID_GREEN>;
			default-state = "on";
			function = LED_FUNCTION_POWER;
			max-brightness = <255>;
			pwms = <&pwm6 0 25000 0>;
		};

		amber_led: led-1 {
			color = <LED_COLOR_ID_AMBER>;
			function = LED_FUNCTION_CHARGING;
			max-brightness = <255>;
			pwms = <&pwm7 0 25000 0>;
		};

		red_led: led-2 {
			color = <LED_COLOR_ID_RED>;
			default-state = "off";
			function = LED_FUNCTION_STATUS;
			max-brightness = <255>;
			pwms = <&pwm0 0 25000 0>;
		};
	};

	sdio_pwrseq: sdio-pwrseq {
		compatible = "mmc-pwrseq-simple";
		clocks = <&rk817 1>;
		clock-names = "ext_clock";
		pinctrl-0 = <&wifi_enable_h>;
		pinctrl-names = "default";
		post-power-on-delay-ms = <200>;
		reset-gpios = <&gpio4 RK_PA2 GPIO_ACTIVE_LOW>;
	};

	vcc3v3_lcd0_n: regulator-vcc3v3-lcd0 {
		compatible = "regulator-fixed";
		gpio = <&gpio0 RK_PC2 GPIO_ACTIVE_HIGH>;
		enable-active-high;
		pinctrl-0 = <&vcc_lcd_h>;
		pinctrl-names = "default";
		regulator-boot-on;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-name = "vcc3v3_lcd0_n";
		vin-supply = <&vcc_3v3>;
		regulator-state-mem {
			regulator-off-in-suspend;
		};
	};

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

	vcc_wifi: regulator-vcc-wifi {
		compatible = "regulator-fixed";
		enable-active-high;
		gpio = <&gpio0 RK_PA0 GPIO_ACTIVE_HIGH>;
		pinctrl-0 = <&vcc_wifi_h>;
		pinctrl-names = "default";
		regulator-always-on;
		regulator-boot-on;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-name = "vcc_wifi";
	};

	vibrator: pwm-vibrator {
		compatible = "pwm-vibrator";
		pwm-names = "enable";
		pwms = <&pwm5 0 1000000000 0>;
	};
};

&combphy1 {
	status = "okay";
};

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

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

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

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

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

&hdmi {
	ddc-i2c-bus = <&i2c5>;
	pinctrl-0 = <&hdmitxm0_cec>;
	pinctrl-names = "default";
	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";

	rk817: pmic@20 {
		compatible = "rockchip,rk817";
		reg = <0x20>;
		interrupt-parent = <&gpio0>;
		interrupts = <RK_PA3 IRQ_TYPE_LEVEL_LOW>;
		clock-output-names = "rk808-clkout1", "rk808-clkout2";
		clock-names = "mclk";
		clocks = <&cru I2S1_MCLKOUT_TX>;
		assigned-clocks = <&cru I2S1_MCLKOUT_TX>;
		assigned-clock-parents = <&cru CLK_I2S1_8CH_TX>;
		#clock-cells = <1>;
		#sound-dai-cells = <0>;
		pinctrl-names = "default";
		pinctrl-0 = <&i2s1m0_mclk>, <&pmic_int_l>;
		wakeup-source;

		vcc1-supply = <&vcc_sys>;
		vcc2-supply = <&vcc_sys>;
		vcc3-supply = <&vcc_sys>;
		vcc4-supply = <&vcc_sys>;
		vcc5-supply = <&vcc_sys>;
		vcc6-supply = <&vcc_sys>;
		vcc7-supply = <&vcc_sys>;
		vcc8-supply = <&vcc_sys>;
		vcc9-supply = <&dcdc_boost>;

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

			vdd_gpu: DCDC_REG2 {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <500000>;
				regulator-max-microvolt = <1350000>;
				regulator-ramp-delay = <6001>;
				regulator-initial-mode = <0x2>;
				regulator-name = "vdd_gpu";
				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;
				};
			};

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

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

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

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

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

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

			dcdc_boost: BOOST {
				regulator-always-on;
				regulator-boot-on;
				regulator-min-microvolt = <4700000>;
				regulator-max-microvolt = <5400000>;
				regulator-name = "boost";
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};

			otg_switch: OTG_SWITCH {
				regulator-name = "otg_switch";
				regulator-state-mem {
					regulator-off-in-suspend;
				};
			};
		};
	};

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

&i2c1 {
	/* Unknown/unused device at 0x3c */
	status = "disabled";
};

&i2c5 {
	pinctrl-0 = <&i2c5m1_xfer>;
	pinctrl-names = "default";
	status = "okay";
};

&i2s0_8ch {
	status = "okay";
};

&i2s1_8ch {
	pinctrl-0 = <&i2s1m0_sclktx
		     &i2s1m0_lrcktx
		     &i2s1m0_sdi0
		     &i2s1m0_sdo0>;
	pinctrl-names = "default";
	rockchip,trcm-sync-tx-only;
	status = "okay";
};

&pinctrl {
	gpio-btns {
		btn_pins_ctrl: btn-pins-ctrl {
			rockchip,pins =
				<3 RK_PA1 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PA2 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PA3 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PA4 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PA5 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PA6 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PB1 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PB2 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PB3 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PB4 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PB5 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PB6 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PC0 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PC1 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PC2 RK_FUNC_GPIO &pcfg_pull_up>,
				<3 RK_PC3 RK_FUNC_GPIO &pcfg_pull_up>;
		};

		btn_pins_vol: btn-pins-vol {
			rockchip,pins =
			<3 RK_PA7 RK_FUNC_GPIO &pcfg_pull_up>,
			<3 RK_PB0 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

	joy-mux {
		joy_mux_en: joy-mux-en {
			rockchip,pins =
				<0 RK_PB5 RK_FUNC_GPIO &pcfg_output_low>;
		};
	};

	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 =
				<4 RK_PA2 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	vcc3v3-lcd {
		vcc_lcd_h: vcc-lcd-h {
			rockchip,pins =
				<0 RK_PC2 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	vcc-wifi {
		vcc_wifi_h: vcc-wifi-h {
			rockchip,pins =
				<0 RK_PA0 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};
};

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

&pwm0 {
	pinctrl-0 = <&pwm0m1_pins>;
	pinctrl-names = "default";
	status = "okay";
};

&pwm5 {
	status = "okay";
};

&pwm6 {
	status = "okay";
};

&pwm7 {
	status = "okay";
};

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

&sdmmc0 {
	bus-width = <4>;
	cap-sd-highspeed;
	cd-gpios = <&gpio0 RK_PA4 GPIO_ACTIVE_LOW>;
	disable-wp;
	pinctrl-0 = <&sdmmc0_bus4 &sdmmc0_clk &sdmmc0_cmd &sdmmc0_det>;
	pinctrl-names = "default";
	sd-uhs-sdr104;
	vmmc-supply = <&vcc_3v3>;
	vqmmc-supply = <&vccio_sd>;
	status = "okay";
};

&sdmmc1 {
	bus-width = <4>;
	cap-sd-highspeed;
	cd-gpios = <&gpio2 RK_PB2 GPIO_ACTIVE_LOW>;
	disable-wp;
	pinctrl-0 = <&sdmmc1_bus4 &sdmmc1_cmd &sdmmc1_clk &sdmmc1_det>;
	pinctrl-names = "default";
	sd-uhs-sdr104;
	vmmc-supply = <&vcc_3v3>;
	vqmmc-supply = <&vcc1v8_dvp>;
	status = "okay";
};

&sdmmc2 {
	bus-width = <4>;
	cap-sd-highspeed;
	cap-sdio-irq;
	keep-power-in-suspend;
	mmc-pwrseq = <&sdio_pwrseq>;
	non-removable;
	pinctrl-0 = <&sdmmc2m0_bus4 &sdmmc2m0_cmd &sdmmc2m0_clk>;
	pinctrl-names = "default";
	vmmc-supply = <&vcc_wifi>;
	vqmmc-supply = <&vcca1v8_pmu>;
	status = "okay";
};

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

&uart1 {
	pinctrl-0 = <&uart1m1_xfer &uart1m1_ctsn &uart1m1_rtsn>;
	pinctrl-names = "default";
	uart-has-rtscts;
	status = "okay";

	bluetooth {
		compatible = "realtek,rtl8821cs-bt", "realtek,rtl8723bs-bt";
		device-wake-gpios = <&gpio4 4 GPIO_ACTIVE_HIGH>;
		enable-gpios = <&gpio4 3 GPIO_ACTIVE_HIGH>;
		host-wake-gpios = <&gpio4 5 GPIO_ACTIVE_HIGH>;
	};
};

&uart2 {
	status = "okay";
};

/*
 * Lack the schematics to verify, but port works as a peripheral
 * (and not a host or OTG port).
 */
&usb_host0_xhci {
	dr_mode = "peripheral";
	phys = <&usb2phy0_otg>;
	phy-names = "usb2-phy";
	status = "okay";
};

&usb_host1_ehci {
	status = "okay";
};

&usb_host1_ohci {
	status = "okay";
};

&usb_host1_xhci {
	phy-names = "usb2-phy", "usb3-phy";
	phys = <&usb2phy1_host>, <&combphy1 PHY_TYPE_USB3>;
	status = "okay";
};

&usb2phy0 {
	status = "okay";
};

&usb2phy0_otg {
	status = "okay";
};

&usb2phy1 {
	status = "okay";
};

&usb2phy1_host {
	status = "okay";
};

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