// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2026 Khadas Technology Co., Ltd.
 */

/dts-v1/;

#include "meson-s4.dtsi"
#include <dt-bindings/input/input.h>
#include <dt-bindings/leds/common.h>

/ {
	model = "Khadas VIM1S";
	compatible = "khadas,vim1s", "amlogic,s905y4", "amlogic,s4";
	interrupt-parent = <&gic>;
	#address-cells = <2>;
	#size-cells = <2>;

	aliases {
		mmc0 = &emmc; /* eMMC */
		mmc1 = &sd; /* SD card */
		mmc2 = &sdio; /* SDIO */
		rtc0 = &rtc;
		rtc1 = &vrtc;
		serial0 = &uart_b;
	};

	memory@0 {
		device_type = "memory";
		reg = <0x0 0x0 0x0 0x80000000>;
	};

	reserved-memory {
		#address-cells = <2>;
		#size-cells = <2>;
		ranges;

		/* 52 MiB reserved for ARM Trusted Firmware */
		secmon_reserved: secmon@5000000 {
			reg = <0x0 0x05000000 0x0 0x3400000>;
			no-map;
		};
	};

	adc-keys {
		compatible = "adc-keys";
		io-channels = <&saradc 0>;
		io-channel-names = "buttons";
		keyup-threshold-microvolt = <1710000>;
		poll-interval = <100>;

		button-function {
			label = "Function";
			linux,code = <KEY_FN>;
			press-threshold-microvolt = <10000>;
		};
	};

	emmc_pwrseq: emmc-pwrseq {
		compatible = "mmc-pwrseq-emmc";
		reset-gpios = <&gpio GPIOB_9 GPIO_ACTIVE_LOW>;
	};

	gpio-keys-polled {
		compatible = "gpio-keys-polled";
		poll-interval = <100>;

		power-button {
			label = "power";
			linux,code = <KEY_POWER>;
			gpios = <&gpio GPIOD_8 GPIO_ACTIVE_LOW>;
		};
	};

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

		led-1 {
			color = <LED_COLOR_ID_WHITE>;
			function = LED_FUNCTION_STATUS;
			linux,default-trigger = "heartbeat";
			max-brightness = <255>;
			pwms = <&pwm_gh 0 30040 0>;
		};
	};

	sdio_32k: sdio-32k {
		compatible = "pwm-clock";
		#clock-cells = <0>;
		clock-frequency = <32768>;
		pwms = <&pwm_ef 0 30518 0>; /* PWM_E at 32.768KHz */
	};

	sdio_pwrseq: sdio-pwrseq {
		compatible = "mmc-pwrseq-simple";
		reset-gpios = <&gpio GPIOX_6 GPIO_ACTIVE_LOW>;
		clocks = <&sdio_32k>;
		clock-names = "ext_clock";
	};

	main_5v: regulator-main-5v {
		compatible = "regulator-fixed";
		regulator-name = "5V";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		regulator-always-on;
	};

	sd_3v3: regulator-sd-3v3 {
		compatible = "regulator-fixed";
		regulator-name = "SD_3V3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		gpio = <&gpio GPIOD_4 GPIO_ACTIVE_LOW>;
		regulator-always-on;
	};

	vddio_sd: regulator-vddio-sd {
		compatible = "regulator-gpio";
		regulator-name = "VDDIO_SD";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <3300000>;
		gpios = <&gpio GPIOD_9 GPIO_ACTIVE_HIGH>;
		gpios-states = <1>;
		states = <1800000 1
				3300000 0>;
	};

	vddao_3v3: regulator-vddao-3v3 {
		compatible = "regulator-fixed";
		regulator-name = "VDDAO_3V3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&main_5v>;
		regulator-always-on;
	};

	vddio_ao1v8: regulator-vddio-ao1v8 {
		compatible = "regulator-fixed";
		regulator-name = "VDDIO_AO1V8";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		vin-supply = <&vddao_3v3>;
		regulator-always-on;
	};

	/* SY8120B1ABC DC/DC Regulator. */
	vddcpu: regulator-vddcpu {
		compatible = "pwm-regulator";

		regulator-name = "VDDCPU";
		regulator-min-microvolt = <689000>;
		regulator-max-microvolt = <1049000>;

		vin-supply = <&main_5v>;

		pwms = <&pwm_ij 1 1500 0>;
		pwm-dutycycle-range = <100 0>;

		regulator-boot-on;
		regulator-always-on;
		/* Voltage Duty-Cycle */
		voltage-table = <1049000 0>,
				<1039000 3>,
				<1029000 6>,
				<1019000 9>,
				<1009000 12>,
				<999000 14>,
				<989000 17>,
				<979000 20>,
				<969000 23>,
				<959000 26>,
				<949000 29>,
				<939000 31>,
				<929000 34>,
				<919000 37>,
				<909000 40>,
				<899000 43>,
				<889000 45>,
				<879000 48>,
				<869000 51>,
				<859000 54>,
				<849000 56>,
				<839000 59>,
				<829000 62>,
				<819000 65>,
				<809000 68>,
				<799000 70>,
				<789000 73>,
				<779000 76>,
				<769000 79>,
				<759000 81>,
				<749000 84>,
				<739000 87>,
				<729000 89>,
				<719000 92>,
				<709000 95>,
				<699000 98>,
				<689000 100>;
	};
};

&emmc {
	status = "okay";
	pinctrl-0 = <&emmc_pins>, <&emmc_ds_pins>;
	pinctrl-1 = <&emmc_clk_gate_pins>;
	pinctrl-names = "default", "clk-gate";

	bus-width = <8>;
	cap-mmc-highspeed;
	mmc-ddr-1_8v;
	mmc-hs200-1_8v;
	max-frequency = <200000000>;
	non-removable;
	disable-wp;

	mmc-pwrseq = <&emmc_pwrseq>;
	vmmc-supply = <&vddao_3v3>;
	vqmmc-supply = <&vddio_ao1v8>;
};

&ethmac {
	status = "okay";
	phy-handle = <&internal_ephy>;
	phy-mode = "rmii";
};

&i2c1 {
	status = "okay";
	pinctrl-names = "default";
	pinctrl-0 = <&i2c1_pins2>;
	clock-frequency = <100000>;

	rtc: rtc@51 {
		compatible = "haoyu,hym8563";
		reg = <0x51>;
		#clock-cells = <0>;
	};
};

&ir {
	status = "okay";
	pinctrl-0 = <&remote_pins>;
	pinctrl-names = "default";
	linux,rc-map-name = "rc-khadas";
};

&pwm_ef {
	status = "okay";
	pinctrl-0 = <&pwm_e_pins1>;
	pinctrl-names = "default";
};

&pwm_gh {
	status = "okay";
	pinctrl-0 = <&pwm_g_pins1>;
	pinctrl-names = "default";
};

&pwm_ij {
	status = "okay";
};

&sd {
	status = "okay";
	pinctrl-0 = <&sdcard_pins>;
	pinctrl-1 = <&sdcard_clk_gate_pins>;
	pinctrl-names = "default", "clk-gate";
	bus-width = <4>;
	cap-sd-highspeed;
	sd-uhs-sdr12;
	sd-uhs-sdr25;
	sd-uhs-sdr50;
	sd-uhs-sdr104;
	max-frequency = <200000000>;
	disable-wp;

	cd-gpios = <&gpio GPIOC_6 GPIO_ACTIVE_LOW>;

	vmmc-supply = <&sd_3v3>;
	vqmmc-supply = <&vddio_sd>;
};

&sdio {
	status = "okay";
	pinctrl-0 = <&sdio_pins>;
	pinctrl-1 = <&sdio_clk_gate_pins>;
	pinctrl-names = "default", "clk-gate";
	#address-cells = <1>;
	#size-cells = <0>;
	bus-width = <4>;
	cap-sd-highspeed;
	sd-uhs-sdr50;
	sd-uhs-sdr104;
	max-frequency = <50000000>;
	non-removable;
	disable-wp;

	no-sd;
	no-mmc;
	mmc-pwrseq = <&sdio_pwrseq>;
	vmmc-supply = <&vddao_3v3>;
	vqmmc-supply = <&vddio_ao1v8>;

	brcmf: wifi@1 {
		reg = <1>;
		compatible = "brcm,bcm43456-fmac", "brcm,bcm4329-fmac";
	};
};

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

&spicc0 {
	status = "okay";
	pinctrl-names = "default";
	pinctrl-0 = <&spicc0_pins_x>;
	cs-gpios = <&gpio GPIOX_10 GPIO_ACTIVE_LOW>;
};

&uart_a {
	status = "okay";
	pinctrl-0 = <&uart_a_pins>, <&uart_a_cts_rts_pins>;
	pinctrl-names = "default";
	uart-has-rtscts;

	bluetooth {
		compatible = "brcm,bcm4345c5";
		shutdown-gpios = <&gpio GPIOX_17 GPIO_ACTIVE_HIGH>;
		max-speed = <2000000>;
		clocks = <&sdio_32k>;
		clock-names = "lpo";
	};
};

&uart_b {
	status = "okay";
};