// SPDX-License-Identifier: BSD-3-Clause
/*
 * Copyright (c) 2021, Konrad Dybcio <konrad.dybcio@somainline.org>
 */

#include <dt-bindings/input/input.h>
#include <dt-bindings/pinctrl/qcom,pmic-gpio.h>
#include <dt-bindings/regulator/qcom,rpmh-regulator.h>
#include "sdm845.dtsi"
#include "pm8005.dtsi"
#include "pm8998.dtsi"
#include "pmi8998.dtsi"

/ {
	qcom,msm-id = <321 0x20001>; /* SDM845 v2.1 */
	qcom,board-id = <8 0>;

	aliases {
		serial0 = &uart6;
		serial1 = &uart9;
	};

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

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

		pinctrl-0 = <&focus_n &snapshot_n &vol_down_n &vol_up_n>;
		pinctrl-names = "default";

		key-camera-focus {
			label = "Camera Focus";
			gpios = <&pm8998_gpios 2 GPIO_ACTIVE_LOW>;
			linux,code = <KEY_CAMERA_FOCUS>;
			debounce-interval = <15>;
			linux,can-disable;
			wakeup-source;
		};

		key-camera-snapshot {
			label = "Camera Snapshot";
			gpios = <&pm8998_gpios 7 GPIO_ACTIVE_LOW>;
			linux,code = <KEY_CAMERA>;
			debounce-interval = <15>;
			linux,can-disable;
			wakeup-source;
		};

		key-volume-down {
			label = "Volume Down";
			gpios = <&pm8998_gpios 5 GPIO_ACTIVE_LOW>;
			linux,code = <KEY_VOLUMEDOWN>;
			debounce-interval = <15>;
			linux,can-disable;
			wakeup-source;
		};

		key-volume-up {
			label = "Volume Up";
			gpios = <&pm8998_gpios 6 GPIO_ACTIVE_LOW>;
			linux,code = <KEY_VOLUMEUP>;
			debounce-interval = <15>;
			linux,can-disable;
			wakeup-source;
		};
	};

	vph_pwr: vph-pwr-regulator {
		compatible = "regulator-fixed";
		regulator-name = "vph_pwr";
		regulator-min-microvolt = <3700000>;
		regulator-max-microvolt = <3700000>;
	};

	vreg_s4a_1p8: pm8998-smps4 {
		compatible = "regulator-fixed";
		regulator-name = "vreg_s4a_1p8";

		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;

		regulator-always-on;
		regulator-boot-on;

		vin-supply = <&vph_pwr>;
	};

	reserved-memory {
		/* SONY was cool and didn't diverge from MTP this time, yay! */
		cont_splash_mem: memory@9d400000 {
			reg = <0x0 0x9d400000 0x0 0x2400000>;
			no-map;
		};

		ramoops@ffc00000 {
			compatible = "ramoops";
			reg = <0x0 0xffc00000 0x0 0x100000>;
			record-size = <0x10000>;
			console-size = <0x60000>;
			ftrace-size = <0x10000>;
			pmsg-size = <0x20000>;
			ecc-size = <16>;
			no-map;
		};
	};
};

&adsp_pas {
	firmware-name = "qcom/sdm845/Sony/tama/adsp.mbn";
	status = "okay";
};

&apps_rsc {
	regulators-0 {
		compatible = "qcom,pm8998-rpmh-regulators";
		qcom,pmic-id = "a";

		vdd-s1-supply = <&vph_pwr>;
		vdd-s2-supply = <&vph_pwr>;
		vdd-s3-supply = <&vph_pwr>;
		vdd-s4-supply = <&vph_pwr>;
		vdd-s5-supply = <&vph_pwr>;
		vdd-s6-supply = <&vph_pwr>;
		vdd-s7-supply = <&vph_pwr>;
		vdd-s8-supply = <&vph_pwr>;
		vdd-s9-supply = <&vph_pwr>;
		vdd-s10-supply = <&vph_pwr>;
		vdd-s11-supply = <&vph_pwr>;
		vdd-s12-supply = <&vph_pwr>;
		vdd-s13-supply = <&vph_pwr>;
		vdd-l1-l27-supply = <&vreg_s7a_0p9>;
		vdd-l2-l8-l17-supply = <&vreg_s3a_1p3>;
		vdd-l3-l11-supply = <&vreg_s7a_0p9>;
		vdd-l4-l5-supply = <&vreg_s7a_0p9>;
		vdd-l6-supply = <&vph_pwr>;
		vdd-l7-l12-l14-l15-supply = <&vreg_s5a_1p9>;
		vdd-l9-supply = <&vreg_s5a_1p9>;
		vdd-l10-l23-l25-supply = <&src_vreg_bob>;
		vdd-l13-l19-l21-supply = <&src_vreg_bob>;
		vdd-l16-l28-supply = <&src_vreg_bob>;
		vdd-l18-l22-supply = <&src_vreg_bob>;
		vdd-l20-l24-supply = <&src_vreg_bob>;
		vdd-l26-supply = <&vreg_s3a_1p3>;
		vin-lvs-1-2-supply = <&vreg_s4a_1p8>;

		vreg_s2a_1p1: smps2 {
			regulator-min-microvolt = <1100000>;
			regulator-max-microvolt = <1100000>;
		};

		vreg_s3a_1p3: smps3 {
			regulator-min-microvolt = <1352000>;
			regulator-max-microvolt = <1352000>;
		};

		vreg_s5a_1p9: smps5 {
			regulator-min-microvolt = <1904000>;
			regulator-max-microvolt = <2040000>;
		};

		vreg_s7a_0p9: smps7 {
			regulator-min-microvolt = <900000>;
			regulator-max-microvolt = <1028000>;
		};

		vreg_l1a_0p9: ldo1 {
			regulator-min-microvolt = <880000>;
			regulator-max-microvolt = <880000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l2a_1p2: ldo2 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
			regulator-always-on;
		};

		vreg_l3a_1p0: ldo3 {
			regulator-min-microvolt = <1000000>;
			regulator-max-microvolt = <1000000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l5a_0p8: ldo5 {
			regulator-min-microvolt = <800000>;
			regulator-max-microvolt = <800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l6a_1p8: ldo6 {
			regulator-min-microvolt = <1856000>;
			regulator-max-microvolt = <1856000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l7a_1p8: ldo7 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l8a_1p2: ldo8 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1248000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l9a_1p7: ldo9 {
			regulator-min-microvolt = <1704000>;
			regulator-max-microvolt = <2928000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l10a_1p7: ldo10 {
			regulator-min-microvolt = <1704000>;
			regulator-max-microvolt = <2928000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l11a_1p0: ldo11 {
			regulator-min-microvolt = <1000000>;
			regulator-max-microvolt = <1048000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l12a_1p8: ldo12 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l13a_1p8: ldo13 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <2960000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l14a_1p8: ldo14 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
			regulator-system-load = <62000>;
		};

		vreg_l15a_1p8: ldo15 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l16a_2p7: ldo16 {
			regulator-min-microvolt = <2704000>;
			regulator-max-microvolt = <2704000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l17a_1p3: ldo17 {
			regulator-min-microvolt = <1304000>;
			regulator-max-microvolt = <1304000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l18a_2p7: ldo18 {
			regulator-min-microvolt = <2704000>;
			regulator-max-microvolt = <2960000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l19a_2p7: ldo19 {
			regulator-min-microvolt = <2700000>;
			regulator-max-microvolt = <2700000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;

			/*
			 * The driver *really* doesn't want this regualtor to exist,
			 * saying that it could not get the current voltage (-ENOTRECOVERABLE)
			 * even though it surely is used on these devices (as a voltage
			 * source for camera autofocus)
			 */
			status = "disabled";
		};

		vreg_l20a_2p7: ldo20 {
			regulator-min-microvolt = <2704000>;
			regulator-max-microvolt = <2960000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l21a_2p7: ldo21 {
			regulator-min-microvolt = <2704000>;
			regulator-max-microvolt = <2960000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l22a_2p8: ldo22 {
			regulator-min-microvolt = <2800000>;
			regulator-max-microvolt = <2800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l23a_3p0: ldo23 {
			regulator-min-microvolt = <3000000>;
			regulator-max-microvolt = <3312000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l24a_3p1: ldo24 {
			regulator-min-microvolt = <3088000>;
			regulator-max-microvolt = <3088000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l25a_3p0: ldo25 {
			regulator-min-microvolt = <3000000>;
			regulator-max-microvolt = <3312000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l26a_1p2: ldo26 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l28a_2p8: ldo28 {
			regulator-min-microvolt = <2856000>;
			regulator-max-microvolt = <3008000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
			regulator-system-load = <100000>;
		};

		vreg_lvs1a_1p8: lvs1 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_lvs2a_1p8: lvs2 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};
	};

	regulators-1 {
		compatible = "qcom,pmi8998-rpmh-regulators";
		qcom,pmic-id = "b";

		src_vreg_bob: bob {
			regulator-min-microvolt = <3312000>;
			regulator-max-microvolt = <3600000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};
	};

	regulators-2 {
		compatible = "qcom,pm8005-rpmh-regulators";
		qcom,pmic-id = "c";

		vdd-s1-supply = <&vph_pwr>;
		vdd-s2-supply = <&vph_pwr>;
		vdd-s3-supply = <&vph_pwr>;
		vdd-s4-supply = <&vph_pwr>;

		vreg_s3c_0p6: smps3 {
			regulator-min-microvolt = <600000>;
			regulator-max-microvolt = <600000>;
		};
	};
};

&cdsp_pas {
	firmware-name = "qcom/sdm845/Sony/tama/cdsp.mbn";
	status = "okay";
};

&gcc {
	protected-clocks = <GCC_QSPI_CORE_CLK>,
			<GCC_QSPI_CORE_CLK_SRC>,
			<GCC_QSPI_CNOC_PERIPH_AHB_CLK>,
			<GCC_LPASS_Q6_AXI_CLK>,
			<GCC_LPASS_SWAY_CLK>;
};

&gmu {
	status = "okay";
};

&gpi_dma0 {
	status = "okay";
};

&gpi_dma1 {
	status = "okay";
};

&gpu {
	status = "okay";

	zap-shader {
		memory-region = <&gpu_mem>;
		firmware-name = "qcom/sdm845/Sony/tama/a630_zap.mbn";
	};
};

&i2c5 {
	clock-frequency = <400000>;
	status = "okay";

	touchscreen: touchscreen@2c {
		compatible = "syna,rmi4-i2c";
		reg = <0x2c>;

		interrupts-extended = <&tlmm 125 IRQ_TYPE_EDGE_FALLING>;
		vdd-supply = <&vreg_l14a_1p8>;
		/*
		 * This is a blatant abuse of OF, but the panel driver *needs*
		 * to probe first, as the power/gpio switching needs to be precisely
		 * timed in order for both the display and touch panel to function properly.
		 */
		incell-supply = <&panel>;

		syna,reset-delay-ms = <220>;
		syna,startup-delay-ms = <1000>;

		pinctrl-0 = <&ts_default>;
		pinctrl-1 = <&ts_sleep>;
		pinctrl-names = "default", "sleep";

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

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

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

&i2c10 {
	status = "okay";
	clock-frequency = <400000>;

	/* Qcom SMB1355 @ 8, c */
	/* NXP PN547 NFC @ 28 */
	/* Renesas IDTP9221 Qi charger @ 61 */
};

&i2c14 {
	status = "okay";
	clock-frequency = <400000>;

	/* SONY ToF sensor @ 52 */
	/* AMS TCS3490 RGB+IR color sensor @ 72 */
};

&ibb {
	qcom,discharge-resistor-kohms = <300>;
	regulator-min-microvolt = <5500000>;
	regulator-max-microvolt = <5700000>;
	regulator-min-microamp = <0>;
	regulator-max-microamp = <800000>;
	regulator-over-current-protection;
	regulator-soft-start;
	regulator-pull-down;
};

&lab {
	regulator-min-microvolt = <5500000>;
	regulator-max-microvolt = <5700000>;
	regulator-min-microamp = <200000>;
	regulator-max-microamp = <200000>;
	regulator-over-current-protection;
	regulator-soft-start;
	regulator-pull-down;
};

&mdss {
	status = "okay";
};

&mdss_dsi0 {
	vdda-supply = <&vreg_l26a_1p2>;
	status = "okay";

	panel: panel@0 {
		/* The compatible is assigned in device DTs. */
		reg = <0>;

		backlight = <&pmi8998_wled>;
		vddio-supply = <&vreg_l14a_1p8>;
		vsp-supply = <&lab>;
		vsn-supply = <&ibb>;
		panel-reset-gpios = <&tlmm 6 GPIO_ACTIVE_HIGH>;
		touch-reset-gpios = <&tlmm 99 GPIO_ACTIVE_HIGH>;

		pinctrl-0 = <&sde_dsi_active &sde_te_active_sleep>;
		pinctrl-1 = <&sde_dsi_sleep &sde_te_active_sleep>;
		pinctrl-names = "default", "sleep";

		port {
			panel_in: endpoint {
				remote-endpoint = <&mdss_dsi0_out>;
			};
		};
	};
};

&mdss_dsi0_out {
	remote-endpoint = <&panel_in>;
	data-lanes = <0 1 2 3>;
};

&mdss_dsi0_phy {
	vdds-supply = <&vreg_l1a_0p9>;
	status = "okay";
};

&pm8998_gpios {
	focus_n: focus-n-state {
		pins = "gpio2";
		function = PMIC_GPIO_FUNC_NORMAL;
		power-source = <0>;
		bias-pull-up;
		input-enable;
	};

	vol_down_n: vol-down-n-state {
		pins = "gpio5";
		function = PMIC_GPIO_FUNC_NORMAL;
		power-source = <0>;
		bias-pull-up;
		input-enable;
	};

	vol_up_n: vol-up-n-state {
		pins = "gpio6";
		function = PMIC_GPIO_FUNC_NORMAL;
		power-source = <0>;
		bias-pull-up;
		input-enable;
	};

	snapshot_n: snapshot-n-state {
		pins = "gpio7";
		function = PMIC_GPIO_FUNC_NORMAL;
		power-source = <0>;
		bias-pull-up;
		input-enable;
	};
};

&pmi8998_wled {
	default-brightness = <800>;
	qcom,switching-freq = <800>;
	qcom,ovp-millivolt = <29600>;
	qcom,current-boost-limit = <970>;
	qcom,current-limit-microamp = <20000>;
	qcom,enabled-strings = <0 1 2 3>;
	status = "okay";
};

&qupv3_id_0 {
	status = "okay";
};

&qupv3_id_1 {
	status = "okay";
};

&sdhc_2 {
	status = "okay";

	vmmc-supply = <&vreg_l21a_2p7>;
	vqmmc-supply = <&vreg_l13a_1p8>;
	cd-gpios = <&tlmm 126 GPIO_ACTIVE_HIGH>;
	pinctrl-0 = <&sdc2_default_state>;
	pinctrl-names = "default";
	bus-width = <4>;
	no-sdio;
	no-mmc;
};

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

	sdc2_default_state: sdc2-default-state {
		clk-pins {
			pins = "sdc2_clk";
			drive-strength = <16>;
			bias-disable;
		};

		cmd-pins {
			pins = "sdc2_cmd";
			drive-strength = <10>;
			bias-pull-up;
		};

		data-pins {
			pins = "sdc2_data";
			drive-strength = <10>;
			bias-pull-up;
		};
	};

	sde_dsi_active: sde-dsi-active-state {
		pins = "gpio6";
		function = "gpio";
		drive-strength = <8>;
		bias-disable;
	};

	sde_dsi_sleep: sde-dsi-sleep-state {
		pins = "gpio6";
		function = "gpio";
		drive-strength = <2>;
		bias-pull-down;
	};

	sde_te_active_sleep: sde-te-active-sleep-state {
		pins = "gpio10";
		function = "mdp_vsync";
		drive-strength = <2>;
		bias-pull-down;
	};

	ts_default: ts-default-state {
		reset-pins {
			pins = "gpio99";
			function = "gpio";
			drive-strength = <2>;
			bias-pull-up;
		};

		int-pins {
			pins = "gpio125";
			function = "gpio";
			drive-strength = <2>;
			bias-pull-up;
		};
	};

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

		int-pins {
			pins = "gpio125";
			function = "gpio";
			drive-strength = <2>;
			bias-pull-down;
		};
	};
};

&uart6 {
	status = "okay";
};

&uart9 {
	status = "okay";
};

/* BIG WARNING! DO NOT TOUCH UFS, YOUR DEVICE WILL DIE! */
&ufs_mem_hc { status = "disabled"; };
&ufs_mem_phy { status = "disabled"; };

&usb_1 {
	status = "okay";

	qcom,select-utmi-as-pipe-clk;
};

&usb_1_dwc3 {
	dr_mode = "peripheral";

	maximum-speed = "high-speed";
	phys = <&usb_1_hsphy>;
	phy-names = "usb2-phy";
};

&usb_1_hsphy {
	status = "okay";

	vdd-supply = <&vreg_l1a_0p9>;
	vdda-pll-supply = <&vreg_l12a_1p8>;
	vdda-phy-dpdm-supply = <&vreg_l24a_3p1>;
};

&venus {
	firmware-name = "qcom/sdm845/Sony/tama/venus.mbn";
	status = "okay";
};