// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2019 Hardkernel Co., Ltd
 * Copyright (c) 2020 Theobroma Systems Design und Consulting GmbH
 * Copyright (c) 2022 Maya Matuszczyk <maccraft123mc@gmail.com>
 */

/dts-v1/;
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/input/input.h>
#include <dt-bindings/leds/common.h>
#include <dt-bindings/pinctrl/rockchip.h>
#include "rk3326.dtsi"

/ {
	aliases {
		mmc0 = &sdmmc;
	};

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

	backlight: backlight {
		compatible = "pwm-backlight";
		power-supply = <&vcc_bl>;
		pwms = <&pwm1 0 25000 0>;
	};

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

		button-sw1 {
			gpios = <&gpio1 RK_PB4 GPIO_ACTIVE_LOW>;
			label = "DPAD-UP";
			linux,code = <BTN_DPAD_UP>;
		};
		button-sw2 {
			gpios = <&gpio1 RK_PB5 GPIO_ACTIVE_LOW>;
			label = "DPAD-DOWN";
			linux,code = <BTN_DPAD_DOWN>;
		};
		button-sw3 {
			gpios = <&gpio1 RK_PB6 GPIO_ACTIVE_LOW>;
			label = "DPAD-LEFT";
			linux,code = <BTN_DPAD_LEFT>;
		};
		button-sw4 {
			gpios = <&gpio1 RK_PB7 GPIO_ACTIVE_LOW>;
			label = "DPAD-RIGHT";
			linux,code = <BTN_DPAD_RIGHT>;
		};
		button-sw5 {
			gpios = <&gpio1 RK_PA2 GPIO_ACTIVE_LOW>;
			label = "BTN-A";
			linux,code = <BTN_EAST>;
		};
		button-sw6 {
			gpios = <&gpio1 RK_PA5 GPIO_ACTIVE_LOW>;
			label = "BTN-B";
			linux,code = <BTN_SOUTH>;
		};
		button-sw7 {
			gpios = <&gpio1 RK_PA6 GPIO_ACTIVE_LOW>;
			label = "BTN-Y";
			linux,code = <BTN_WEST>;
		};
		button-sw8 {
			gpios = <&gpio1 RK_PA7 GPIO_ACTIVE_LOW>;
			label = "BTN-X";
			linux,code = <BTN_NORTH>;
		};
		btn_f1: button-sw9 {
			gpios = <&gpio2 RK_PA0 GPIO_ACTIVE_LOW>;
			label = "F1";
			linux,code = <BTN_TRIGGER_HAPPY1>;
		};
		btn_f2: button-sw10 {
			gpios = <&gpio2 RK_PA1 GPIO_ACTIVE_LOW>;
			label = "F2";
			linux,code = <BTN_TRIGGER_HAPPY2>;
		};
		btn_f3: button-sw11 {
			gpios = <&gpio2 RK_PA2 GPIO_ACTIVE_LOW>;
			label = "F3";
			linux,code = <BTN_TRIGGER_HAPPY3>;
		};
		btn_f4: button-sw12 {
			gpios = <&gpio2 RK_PA3 GPIO_ACTIVE_LOW>;
			label = "F4";
			linux,code = <BTN_TRIGGER_HAPPY4>;
		};
		btn_f5: button-sw13 {
			gpios = <&gpio2 RK_PA4 GPIO_ACTIVE_LOW>;
			label = "F5";
			linux,code = <BTN_TRIGGER_HAPPY5>;
		};
		btn_f6: button-sw14 {
			gpios = <&gpio2 RK_PA5 GPIO_ACTIVE_LOW>;
			label = "F6";
			linux,code = <BTN_TRIGGER_HAPPY6>;
		};
		button-sw15 {
			gpios = <&gpio2 RK_PA6 GPIO_ACTIVE_LOW>;
			label = "TOP-LEFT";
			linux,code = <BTN_TL>;
		};
		button-sw16 {
			gpios = <&gpio2 RK_PA7 GPIO_ACTIVE_LOW>;
			label = "TOP-RIGHT";
			linux,code = <BTN_TR>;
		};
	};

	/* led-1 is wired directly to output of always-on regulator */

	gpio_led: gpio-leds {
		compatible = "gpio-leds";
		pinctrl-names = "default";
		pinctrl-0 = <&red_led_pin>;

		red_led: led-3 {
			color = <LED_COLOR_ID_RED>;
			gpios = <&gpio0 RK_PB5 GPIO_ACTIVE_HIGH>;
			function = LED_FUNCTION_CHARGING;
		};
	};

	pwm_led: led-controller {
		compatible = "pwm-leds";

		blue_led: led-2 {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_STATUS;
			linux,default-trigger = "heartbeat";
			max-brightness = <255>;
			pwms = <&pwm3 0 25000 0>;
		};
	};

	rk817-sound {
		compatible = "simple-audio-card";
		simple-audio-card,name = "rk817_int";
		simple-audio-card,format = "i2s";
		simple-audio-card,hp-det-gpio = <&gpio2 RK_PC6 GPIO_ACTIVE_HIGH>;
		simple-audio-card,mclk-fs = <256>;
		simple-audio-card,widgets =
			"Microphone", "Mic Jack",
			"Headphone", "Headphones",
			"Speaker", "Speaker";
		simple-audio-card,routing =
			"MICL", "Mic Jack",
			"Headphones", "HPOL",
			"Headphones", "HPOR",
			"Speaker", "SPKO";

		simple-audio-card,codec {
			sound-dai = <&rk817>;
		};

		simple-audio-card,cpu {
			sound-dai = <&i2s1_2ch>;
		};
	};

	vccsys: vccsys {
		compatible = "regulator-fixed";
		regulator-name = "vcc3v8_sys";
		regulator-always-on;
		regulator-min-microvolt = <3800000>;
		regulator-max-microvolt = <3800000>;
	};

	vcc_host: vcc_host {
		compatible = "regulator-fixed";
		regulator-name = "vcc_host";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;

		gpio = <&gpio0 RK_PB7 GPIO_ACTIVE_HIGH>;
		enable-active-high;
		regulator-always-on;
		regulator-boot-on;
		vin-supply = <&usb_midu>;
	};
};

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

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

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

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

&cru {
	assigned-clocks = <&cru PLL_NPLL>,
		<&cru ACLK_BUS_PRE>, <&cru ACLK_PERI_PRE>,
		<&cru HCLK_BUS_PRE>, <&cru HCLK_PERI_PRE>,
		<&cru PCLK_BUS_PRE>, <&cru SCLK_GPU>;

	assigned-clock-rates = <1188000000>,
		<200000000>, <200000000>,
		<150000000>, <150000000>,
		<100000000>, <200000000>;
};

&display_subsystem {
	status = "okay";
};

&dsi {
	status = "okay";

	ports {
		mipi_out: port@1 {
			reg = <1>;

			mipi_out_panel: endpoint {
				remote-endpoint = <&mipi_in_panel>;
			};
		};
	};

	internal_display: panel@0 {
		reg = <0>;
		backlight = <&backlight>;
		reset-gpios = <&gpio3 RK_PC0 GPIO_ACTIVE_LOW>;
		rotation = <270>;

		port {
			mipi_in_panel: endpoint {
				remote-endpoint = <&mipi_out_panel>;
			};
		};
	};
};

&dsi_dphy {
	status = "okay";
};

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

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

	rk817: pmic@20 {
		compatible = "rockchip,rk817";
		reg = <0x20>;
		interrupt-parent = <&gpio0>;
		interrupts = <RK_PB2 IRQ_TYPE_LEVEL_LOW>;
		clock-output-names = "rk808-clkout1", "xin32k";
		clock-names = "mclk";
		clocks = <&cru SCLK_I2S1_OUT>;
		pinctrl-names = "default";
		pinctrl-0 = <&pmic_int>, <&i2s1_2ch_mclk>;
		wakeup-source;
		#clock-cells = <1>;
		#sound-dai-cells = <0>;

		vcc1-supply = <&vccsys>;
		vcc2-supply = <&vccsys>;
		vcc3-supply = <&vccsys>;
		vcc4-supply = <&vccsys>;
		vcc5-supply = <&vccsys>;
		vcc6-supply = <&vccsys>;
		vcc7-supply = <&vccsys>;
		vcc8-supply = <&vccsys>;

		regulators {
			vdd_logic: DCDC_REG1 {
				regulator-name = "vdd_logic";
				regulator-min-microvolt = <950000>;
				regulator-max-microvolt = <1150000>;
				regulator-ramp-delay = <6001>;
				regulator-always-on;
				regulator-boot-on;

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

			vdd_arm: DCDC_REG2 {
				regulator-name = "vdd_arm";
				regulator-min-microvolt = <950000>;
				regulator-max-microvolt = <1350000>;
				regulator-ramp-delay = <6001>;
				regulator-always-on;
				regulator-boot-on;

				regulator-state-mem {
					regulator-off-in-suspend;
					regulator-suspend-microvolt = <950000>;
				};
			};

			vcc_ddr: DCDC_REG3 {
				regulator-name = "vcc_ddr";
				regulator-always-on;
				regulator-boot-on;

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

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

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

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

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

			vdd_1v0: LDO_REG3 {
				regulator-name = "vdd_1v0";
				regulator-min-microvolt = <1000000>;
				regulator-max-microvolt = <1000000>;
				regulator-always-on;
				regulator-boot-on;

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

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

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

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

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

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

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

			vcc_bl: LDO_REG7 {
				regulator-name = "vcc_bl";
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;

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

			vcc_lcd: LDO_REG8 {
				regulator-name = "vcc_lcd";
				regulator-min-microvolt = <2800000>;
				regulator-max-microvolt = <2800000>;

				regulator-state-mem {
					regulator-off-in-suspend;
					regulator-suspend-microvolt = <2800000>;
				};
			};

			LDO_REG9 {
				/* unused */
			};

			usb_midu: BOOST {
				regulator-name = "usb_midu";
				regulator-min-microvolt = <5000000>;
				regulator-max-microvolt = <5400000>;
				regulator-always-on;
				regulator-boot-on;
			};
		};

		rk817_charger: charger {
			rockchip,resistor-sense-micro-ohms = <10000>;
			rockchip,sleep-enter-current-microamp = <300000>;
			rockchip,sleep-filter-current-microamp = <100000>;
		};

		rk817_codec: codec {
			rockchip,mic-in-differential;
		};
	};
};

/* EXT Header(P2): 7(SCL:GPIO0.C2), 8(SDA:GPIO0.C3) */
&i2c1 {
	clock-frequency = <400000>;
	status = "okay";
};

/* I2S 1 Channel Used */
&i2s1_2ch {
	status = "okay";
};

&io_domains {
	vccio1-supply = <&vcc_3v3>;
	vccio2-supply = <&vccio_sd>;
	vccio3-supply = <&vcc_3v3>;
	vccio4-supply = <&vcc_3v3>;
	vccio5-supply = <&vcc_3v3>;
	vccio6-supply = <&vcc_3v3>;
	status = "okay";
};

&pmu_io_domains {
	pmuio1-supply = <&vcc3v3_pmu>;
	pmuio2-supply = <&vcc3v3_pmu>;
	status = "okay";
};

&pwm1 {
	status = "okay";
};

&pwm3 {
	status = "okay";
};

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

&sdmmc {
	cap-sd-highspeed;
	card-detect-delay = <200>;
	cd-gpios = <&gpio0 RK_PA3 GPIO_ACTIVE_LOW>; /*[> CD GPIO <]*/
	sd-uhs-sdr12;
	sd-uhs-sdr25;
	sd-uhs-sdr50;
	sd-uhs-sdr104;
	vmmc-supply = <&vcc_sd>;
	vqmmc-supply = <&vccio_sd>;
	status = "okay";
};

&sfc {
	pinctrl-0 = <&sfc_clk &sfc_cs0 &sfc_bus2>;
	pinctrl-names = "default";
	#address-cells = <1>;
	#size-cells = <0>;
	status = "okay";

	flash@0 {
		compatible = "jedec,spi-nor";
		reg = <0>;
		spi-max-frequency = <108000000>;
		spi-rx-bus-width = <2>;
		spi-tx-bus-width = <1>;
	};
};

&tsadc {
	status = "okay";
};

&u2phy {
	status = "okay";

	u2phy_host: host-port {
		status = "okay";
	};

	u2phy_otg: otg-port {
		status = "disabled";
	};
};

&usb20_otg {
	status = "okay";
};

/* EXT Header(P2): 2(RXD:GPIO1.C0),3(TXD:.C1),4(CTS:.C2),5(RTS:.C3) */
&uart1 {
	pinctrl-names = "default";
	pinctrl-0 = <&uart1_xfer &uart1_cts>;
	status = "okay";
};

&uart2 {
	pinctrl-names = "default";
	pinctrl-0 = <&uart2m1_xfer>;
	status = "okay";
};

&vopb {
	status = "okay";
};

&vopb_mmu {
	status = "okay";
};

&pinctrl {
	btns {
		btn_pins: btn-pins {
			rockchip,pins = <1 RK_PA2 RK_FUNC_GPIO &pcfg_pull_up>,
					<1 RK_PA5 RK_FUNC_GPIO &pcfg_pull_up>,
					<1 RK_PA6 RK_FUNC_GPIO &pcfg_pull_up>,
					<1 RK_PA7 RK_FUNC_GPIO &pcfg_pull_up>,
					<1 RK_PB4 RK_FUNC_GPIO &pcfg_pull_up>,
					<1 RK_PB5 RK_FUNC_GPIO &pcfg_pull_up>,
					<1 RK_PB6 RK_FUNC_GPIO &pcfg_pull_up>,
					<1 RK_PB7 RK_FUNC_GPIO &pcfg_pull_up>,
					<2 RK_PA0 RK_FUNC_GPIO &pcfg_pull_up>,
					<2 RK_PA1 RK_FUNC_GPIO &pcfg_pull_up>,
					<2 RK_PA2 RK_FUNC_GPIO &pcfg_pull_up>,
					<2 RK_PA3 RK_FUNC_GPIO &pcfg_pull_up>,
					<2 RK_PA4 RK_FUNC_GPIO &pcfg_pull_up>,
					<2 RK_PA5 RK_FUNC_GPIO &pcfg_pull_up>,
					<2 RK_PA6 RK_FUNC_GPIO &pcfg_pull_up>,
					<2 RK_PA7 RK_FUNC_GPIO &pcfg_pull_up>;
		};
	};

	headphone {
		hp_det: hp-det {
			rockchip,pins = <2 RK_PC6 RK_FUNC_GPIO &pcfg_pull_down>;
		};
	};

	leds {
		red_led_pin: red-led-pin {
			rockchip,pins = <0 RK_PB5 RK_FUNC_GPIO &pcfg_pull_none>;
		};
	};

	pmic {
		dc_det: dc-det {
			rockchip,pins = <0 RK_PB3 RK_FUNC_GPIO &pcfg_pull_none>;
		};

		pmic_int: pmic-int {
			rockchip,pins = <0 RK_PB2 RK_FUNC_GPIO &pcfg_pull_up>;
		};

		soc_slppin_gpio: soc_slppin_gpio {
			rockchip,pins = <0 RK_PA4 RK_FUNC_GPIO &pcfg_output_low>;
		};

		soc_slppin_rst: soc_slppin_rst {
			rockchip,pins = <0 RK_PA4 2 &pcfg_pull_none>;
		};

		soc_slppin_slp: soc_slppin_slp {
			rockchip,pins = <0 RK_PA4 1 &pcfg_pull_none>;
		};
	};
};