// SPDX-License-Identifier: BSD-3-Clause
/*
 * Copyright (c) 2025, André Apitzsch <git@apitzsch.eu>
 */

/dts-v1/;

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

#include "msm8976.dtsi"
#include "pm8004.dtsi"
#include "pm8950.dtsi"

/ {
	model = "BQ Aquaris X5 Plus (Longcheer L9360)";
	compatible = "longcheer,l9360", "qcom,msm8976";
	chassis-type = "handset";

	aliases {
		mmc0 = &sdhc_1; /* SDC1 eMMC slot */
		mmc1 = &sdhc_2; /* SDC2 SD card slot */
	};

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

		framebuffer0: framebuffer@83200000 {
			compatible = "simple-framebuffer";
			reg = <0x0 0x83200000 0x0 (1080 * 1920 * 3)>;
			width = <1080>;
			height = <1920>;
			stride = <(1080 * 3)>;
			format = "r8g8b8";

			power-domains = <&gcc MDSS_GDSC>;

			clocks = <&gcc GCC_MDSS_AHB_CLK>,
				 <&gcc GCC_MDSS_AXI_CLK>,
				 <&gcc GCC_MDSS_VSYNC_CLK>,
				 <&gcc GCC_MDSS_MDP_CLK>,
				 <&gcc GCC_MDSS_BYTE0_CLK>,
				 <&gcc GCC_MDSS_PCLK0_CLK>,
				 <&gcc GCC_MDSS_ESC0_CLK>;
		};
	};

	gpio-keys {
		compatible = "gpio-keys";

		pinctrl-0 = <&hall_sensor_default>, <&volume_up_default>;
		pinctrl-names = "default";

		event-hall-sensor {
			label = "Hall Effect Sensor";
			gpios = <&tlmm 107 GPIO_ACTIVE_HIGH>;
			linux,input-type = <EV_SW>;
			linux,code = <SW_LID>;
			linux,can-disable;
			wakeup-source;
		};

		key-volume-up {
			label = "Volume Up";
			gpios = <&tlmm 113 GPIO_ACTIVE_LOW>;
			linux,code = <KEY_VOLUMEUP>;
		};
	};

	leds {
		compatible = "gpio-leds";

		led-0 {
			gpios = <&tlmm 101 GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_WHITE>;
			default-state = "off";
			function = LED_FUNCTION_KBD_BACKLIGHT;

			pinctrl-0 = <&button_backlight_default>;
			pinctrl-names = "default";
		};
	};

	reg_ts_vdd: regulator-vdd-ts {
		compatible = "regulator-fixed";
		regulator-name = "regulator-vdd-ts";

		gpio = <&tlmm 33 GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};

	reserved-memory {
		framebuffer@83000000 {
			reg = <0x0 0x83000000 0x0 0x2800000>;
			no-map;
		};
	};

	vph_pwr: regulator-vph-pwr {
		compatible = "regulator-fixed";
		regulator-name = "vph-pwr";
		regulator-always-on;
		regulator-boot-on;
	};
};

&blsp1_i2c2 {
	status = "okay";

	led-controller@30 {
		compatible = "kinetic,ktd2026";
		reg = <0x30>;
		#address-cells = <1>;
		#size-cells = <0>;

		multi-led {
			color = <LED_COLOR_ID_RGB>;
			function = LED_FUNCTION_STATUS;

			#address-cells = <1>;
			#size-cells = <0>;

			led@0 {
				reg = <0>;
				color = <LED_COLOR_ID_RED>;
			};

			led@1 {
				reg = <1>;
				color = <LED_COLOR_ID_GREEN>;
			};

			led@2 {
				reg = <2>;
				color = <LED_COLOR_ID_BLUE>;
			};
		};
	};
};

&blsp1_i2c4 {
	status = "okay";

	nfc@28 {
		compatible = "nxp,pn547", "nxp,nxp-nci-i2c";
		reg = <0x28>;

		interrupts-extended = <&tlmm 140 IRQ_TYPE_EDGE_RISING>;

		enable-gpios = <&tlmm 122 GPIO_ACTIVE_HIGH>;
		firmware-gpios = <&tlmm 109 GPIO_ACTIVE_HIGH>;

		pinctrl-0 = <&nfc_default>;
		pinctrl-1 = <&nfc_sleep>;
		pinctrl-names = "default", "sleep";
	};
};

&blsp2_i2c2 {
	status = "okay";

	touchscreen@20 {
		reg = <0x20>;
		compatible = "syna,rmi4-i2c";

		interrupts-extended = <&tlmm 65 IRQ_TYPE_EDGE_FALLING>;

		pinctrl-0 = <&ts_int_default>, <&ts_reset_default>;
		pinctrl-1 = <&ts_int_sleep>, <&ts_reset_sleep>;
		pinctrl-names = "default", "sleep";

		vdd-supply = <&pm8950_l6>;
		vio-supply = <&reg_ts_vdd>;

		reset-gpios = <&tlmm 64 GPIO_ACTIVE_LOW>;

		syna,reset-delay-ms = <200>;
		syna,startup-delay-ms = <200>;

		#address-cells = <1>;
		#size-cells = <0>;

		rmi4-f01@1 {
			reg = <0x1>;
			syna,nosleep-mode = <1>;
		};

		rmi4-f12@12 {
			reg = <0x12>;
			syna,sensor-type = <1>;
		};

		rmi4-f1a@1a {
			reg = <0x1a>;
			/* Keys listed from right to left */
			linux,keycodes = <KEY_APPSELECT KEY_HOMEPAGE KEY_BACK>;
		};
	};
};

&blsp2_uart2 {
	status = "okay";
};

&gcc {
	vdd_gfx-supply = <&pm8004_s5>;
};

&pm8004_spmi_regulators {
	vdd_s2-supply = <&vph_pwr>;
	vdd_s5-supply = <&vph_pwr>;

	/* Cluster 1 supply */
	pm8004_s2: s2 {
		/* regulator-min-microvolt = <500000>; */
		/* Set .95V to prevent unstabilities until CPR for this SoC is done */
		regulator-min-microvolt = <950000>;
		regulator-max-microvolt = <1165000>;
		regulator-name = "vdd_apc1";
		/* Set always on until the CPU PLL is done */
		regulator-always-on;
		regulator-boot-on;
	};

	pm8004_s5: s5 {
		regulator-min-microvolt = <950000>;
		regulator-max-microvolt = <1165000>;
		regulator-enable-ramp-delay = <500>;
		regulator-name = "vdd_gfx";
		/* Hack this on until the gpu driver is ready for it */
		regulator-always-on;
	};
};

&pm8950_resin {
	linux,code = <KEY_VOLUMEDOWN>;
	status = "okay";
};

&pm8950_spmi_regulators {
	vdd_s5-supply = <&vph_pwr>;

	/* Cluster 0 supply */
	pm8950_spmi_s5: s5 {
		/* Set .95V to prevent unstabilities until CPR for this SoC is done */
		/* regulator-min-microvolt = <500000>; */
		regulator-min-microvolt = <950000>;
		regulator-max-microvolt = <1165000>;
		regulator-name = "vdd_apc0";
		/* Set always on until the CPU PLL is done */
		regulator-always-on;
		regulator-boot-on;
	};
};

&rpm_requests {
	pm8950_regulators: regulators {
		compatible = "qcom,rpm-pm8950-regulators";

		vdd_s1-supply = <&vph_pwr>;
		vdd_s2-supply = <&vph_pwr>;
		vdd_s3-supply = <&vph_pwr>;
		vdd_s4-supply = <&vph_pwr>;
		vdd_s6-supply = <&vph_pwr>;
		vdd_l1_l19-supply = <&pm8950_s3>;
		vdd_l2_l23-supply = <&pm8950_s3>;
		vdd_l3-supply = <&pm8950_s3>;
		vdd_l5_l6_l7_l16-supply = <&pm8950_s4>;
		vdd_l8_l11_l12_l17_l22-supply = <&vph_pwr>;

		pm8950_s1: s1 {
			regulator-min-microvolt = <1000000>;
			regulator-max-microvolt = <1162500>;
		};

		pm8950_s3: s3 {
			regulator-min-microvolt = <1325000>;
			regulator-max-microvolt = <1325000>;
		};

		pm8950_s4: s4 {
			regulator-min-microvolt = <2050000>;
			regulator-max-microvolt = <2050000>;
		};

		pm8950_l1: l1 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
		};

		pm8950_l2: l2 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
		};

		pm8950_l3: l3 {
			regulator-min-microvolt = <1000000>;
			regulator-max-microvolt = <1100000>;
		};

		pm8950_l5: l5 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
		};

		pm8950_l6: l6 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
		};

		pm8950_l7: l7 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
		};

		pm8950_l8: l8 {
			regulator-min-microvolt = <2900000>;
			regulator-max-microvolt = <2900000>;
		};

		pm8950_l9: l9 {
			regulator-min-microvolt = <3000000>;
			regulator-max-microvolt = <3300000>;
		};

		pm8950_l10: l10 {
			regulator-min-microvolt = <2800000>;
			regulator-max-microvolt = <2800000>;
		};

		pm8950_l11: l11 {
			regulator-min-microvolt = <2950000>;
			regulator-max-microvolt = <2950000>;
		};

		pm8950_l12: l12 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <2950000>;
		};

		pm8950_l13: l13 {
			regulator-min-microvolt = <3075000>;
			regulator-max-microvolt = <3075000>;
		};

		pm8950_l14: l14 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <3300000>;
		};

		pm8950_l15: l15 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <3300000>;
		};

		pm8950_l16: l16 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
		};

		pm8950_l17: l17 {
			regulator-min-microvolt = <2850000>;
			regulator-max-microvolt = <2850000>;
		};

		pm8950_l19: l19 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1350000>;
		};

		pm8950_l22: l22 {
			regulator-min-microvolt = <2800000>;
			regulator-max-microvolt = <2800000>;
		};

		pm8950_l23: l23 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
		};
	};
};

&sdhc_1 {
	bus-width = <8>;
	non-removable;
	vmmc-supply = <&pm8950_l8>;
	vqmmc-supply = <&pm8950_l5>;
	status = "okay";
};

&sdhc_2 {
	bus-width = <4>;
	cd-gpios = <&tlmm 100 GPIO_ACTIVE_LOW>;
	vmmc-supply = <&pm8950_l11>;
	vqmmc-supply = <&pm8950_l12>;

	pinctrl-0 = <&sdc2_default>, <&sdc2_cd_default>;
	pinctrl-1 = <&sdc2_sleep>, <&sdc2_cd_sleep>;
	pinctrl-names = "default", "sleep";

	status = "okay";
};

&tlmm {
	gpio-reserved-ranges = <0 4>;

	button_backlight_default: button-backlight-default-state {
		pins = "gpio101";
		function = "gpio";
		drive-strength = <2>;
		bias-disable;
	};

	hall_sensor_default: hall-sensor-default-state {
		pins = "gpio107";
		function = "gpio";
		drive-strength = <6>;
		bias-pull-up;
	};

	nfc_default: nfc-default-state {
		pins = "gpio122", "gpio140";
		function = "gpio";
		drive-strength = <6>;
		bias-pull-up;
	};

	nfc_sleep: nfc-sleep-state {
		int-pins {
			pins = "gpio140";
			function = "gpio";
			drive-strength = <6>;
			bias-pull-up;
		};
		ven-pins {
			pins = "gpio122";
			function = "gpio";
			drive-strength = <6>;
			bias-disable;
		};
	};

	sdc2_cd_default: sdc2-cd-default-state {
		pins = "gpio100";
		function = "gpio";
		drive-strength = <2>;
		bias-pull-up;
	};

	sdc2_cd_sleep: sdc2-cd-sleep-state {
		pins = "gpio100";
		function = "gpio";
		drive-strength = <2>;
		bias-disable;
	};

	ts_int_default: ts-int-default-state {
		pins = "gpio65";
		function = "gpio";
		drive-strength = <2>;
		bias-pull-down;
	};

	ts_int_sleep: ts-int-sleep-state {
		pins = "gpio65";
		function = "gpio";
		drive-strength = <2>;
		bias-pull-up;
	};

	ts_reset_default: ts-reset-state {
		pins = "gpio64";
		function = "gpio";
		drive-strength = <8>;
		bias-pull-up;
	};

	ts_reset_sleep: ts-sleep-state {
		pins = "gpio64";
		function = "gpio";
		drive-strength = <2>;
		bias-pull-down;
	};

	volume_up_default: volume-up-default-state {
		pins = "gpio113";
		function = "gpio";
		drive-strength = <2>;
		bias-pull-up;
	};
};

&xo_board {
	clock-frequency = <19200000>;
};