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

#include <dt-bindings/pinctrl/qcom,pmic-gpio.h>
#include <dt-bindings/regulator/qcom,rpmh-regulator.h>
#include "sm8450.dtsi"
#include "pm8350.dtsi"
#include "pm8350b.dtsi"
#include "pm8350c.dtsi"
#include "pm8450.dtsi"
#include "pmk8350.dtsi"
#include "pmr735a.dtsi"

/delete-node/ &adsp_mem;
/delete-node/ &rmtfs_mem;
/delete-node/ &video_mem;

/ {
	chassis-type = "handset";

	aliases {
		serial0 = &uart7;
	};

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

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

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

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

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

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

	reserved-memory {
		adsp_mem: memory@85700000 {
			reg = <0x0 0x85700000 0x0 0x2800000>;
			no-map;
		};

		video_mem: memory@9fd00000 {
			reg = <0x0 0x9fd00000 0x0 0x700000>;
			no-map;
		};

		rmtfs_mem: memory@f3300000 {
			compatible = "qcom,rmtfs-mem";
			reg = <0x0 0xf3300000 0x0 0x280000>;
			no-map;

			qcom,client-id = <1>;
			qcom,vmid = <15>;
		};

		ramoops@ffc00000 {
			compatible = "ramoops";
			reg = <0 0xffc00000 0 0x200000>;
			console-size = <0x40000>;
			record-size = <0x1000>;
			ecc-size = <16>;
			no-map;
		};
	};

	/* Sadly, the voltages for these GPIO regulators are unknown. */
	imx650_vana_regulator: imx650-vana-regulator {
		compatible = "regulator-fixed";
		regulator-name = "imx650_vana_regulator";
		gpio = <&tlmm 23 GPIO_ACTIVE_HIGH>;
		enable-active-high;

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

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

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

&apps_rsc {
	regulators-0 {
		compatible = "qcom,pm8350-rpmh-regulators";
		qcom,pmic-id = "b";

		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-l1-l4-supply = <&pm8350_s11>;
		vdd-l2-l7-supply = <&vreg_bob>;
		vdd-l3-l5-supply = <&vreg_bob>;
		vdd-l6-l9-l10-supply = <&pm8350_s12>;

		/*
		 * ARC regulators:
		 * s5 - gfx.lvl
		 * l8 - lcx.lvl
		 */

		pm8350_s10: smps10 {
			regulator-name = "pm8350_s10";
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
		};

		pm8350_s11: smps11 {
			regulator-name = "pm8350_s11";
			regulator-min-microvolt = <848000>;
			regulator-max-microvolt = <1104000>;
		};

		pm8350_s12: smps12 {
			regulator-name = "pm8350_s12";
			regulator-min-microvolt = <1224000>;
			regulator-max-microvolt = <1400000>;
		};

		pm8350_l1: ldo1 {
			regulator-name = "pm8350_l1";
			regulator-min-microvolt = <912000>;
			regulator-max-microvolt = <920000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350_l2: ldo2 {
			regulator-name = "pm8350_l2";
			regulator-min-microvolt = <3072000>;
			regulator-max-microvolt = <3072000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350_l3: ldo3 {
			regulator-name = "pm8350_l3";
			regulator-min-microvolt = <904000>;
			regulator-max-microvolt = <904000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350_l5: ldo5 {
			regulator-name = "pm8350_l5";
			regulator-min-microvolt = <880000>;
			regulator-max-microvolt = <912000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350_l6: ldo6 {
			regulator-name = "pm8350_l6";
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350_l7: ldo7 {
			regulator-name = "pm8350_l7";
			regulator-min-microvolt = <2504000>;
			regulator-max-microvolt = <2504000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350_l9: ldo9 {
			regulator-name = "pm8350_l9";
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};
	};

	regulators-1 {
		compatible = "qcom,pm8350c-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>;
		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-l1-l12-supply = <&vreg_bob>;
		vdd-l2-l8-supply = <&vreg_bob>;
		vdd-l3-l4-l5-l7-l13-supply = <&vreg_bob>;
		vdd-l6-l9-l11-supply = <&vreg_bob>;
		vdd-l10-supply = <&pm8350_s12>;

		vdd-bob-supply = <&vph_pwr>;

		/*
		 * ARC regulators:
		 * s2 - mxc.lvl
		 * s4 - mss.lvl
		 * s6 - cx.lvl
		 */

		pm8350c_s1: smps1 {
			regulator-name = "pm8350c_s1";
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <2024000>;
		};

		pm8350c_s10: smps10 {
			regulator-name = "pm8350c_s10";
			regulator-min-microvolt = <1000000>;
			regulator-max-microvolt = <1100000>;
		};

		vreg_bob: bob {
			regulator-name = "vreg_bob";
			regulator-min-microvolt = <3400000>;
			regulator-max-microvolt = <3960000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_AUTO>;
		};

		pm8350c_l1: ldo1 {
			regulator-name = "pm8350c_l1";
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350c_l2: ldo2 {
			regulator-name = "pm8350c_l2";
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350c_l3: ldo3 {
			regulator-name = "pm8350c_l3";
			regulator-min-microvolt = <3296000>;
			regulator-max-microvolt = <3304000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350c_l4: ldo4 {
			regulator-name = "pm8350c_l4";
			regulator-min-microvolt = <1704000>;
			regulator-max-microvolt = <3000000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350c_l5: ldo5 {
			regulator-name = "pm8350c_l5";
			regulator-min-microvolt = <1704000>;
			regulator-max-microvolt = <3000000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350c_l6: ldo6 {
			regulator-name = "pm8350c_l6";
			regulator-min-microvolt = <2960000>;
			/* Originally max = 3008000 but SDHCI expects 2960000 */
			regulator-max-microvolt = <2960000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350c_l7: ldo7 {
			regulator-name = "pm8350c_l7";
			regulator-min-microvolt = <3008000>;
			regulator-max-microvolt = <3008000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350c_l8: ldo8 {
			regulator-name = "pm8350c_l8";
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350c_l9: ldo9 {
			regulator-name = "pm8350c_l9";
			regulator-min-microvolt = <2960000>;
			/* Originally max = 3008000 but SDHCI expects 2960000 */
			regulator-max-microvolt = <2960000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350c_l10: ldo10 {
			regulator-name = "pm8350c_l10";
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350c_l12: ldo12 {
			regulator-name = "pm8350c_l12";
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1968000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8350c_l13: ldo13 {
			regulator-name = "pm8350c_l13";
			regulator-min-microvolt = <3000000>;
			regulator-max-microvolt = <3000000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};
	};

	regulators-2 {
		compatible = "qcom,pm8450-rpmh-regulators";
		qcom,pmic-id = "h";

		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-l2-supply = <&vreg_bob>;
		vdd-l3-supply = <&vreg_bob>;
		vdd-l4-supply = <&vreg_bob>;

		/*
		 * ARC regulators:
		 * S2 - ebi.lvl
		 * S4 - mmcx.lvl
		 * S6 - mx.lvl
		 * L1 - lmx.lvl
		 */

		pm8450_s3: smps3 {
			regulator-name = "pm8450_s3";
			regulator-min-microvolt = <500000>;
			regulator-max-microvolt = <600000>;
		};

		pm8450_l2: ldo2 {
			regulator-name = "pm8450_l2";
			regulator-min-microvolt = <880000>;
			regulator-max-microvolt = <912000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		pm8450_l3: ldo3 {
			regulator-name = "pm8450_l3";
			regulator-min-microvolt = <912000>;
			regulator-max-microvolt = <912000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};
	};

	regulators-3 {
		compatible = "qcom,pmr735a-rpmh-regulators";
		qcom,pmic-id = "e";

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

		vdd-l1-l2-supply = <&pmr735a_s2>;
		vdd-l3-supply = <&pmr735a_s1>;
		vdd-l4-supply = <&pm8350c_s1>;
		vdd-l5-l6-supply = <&pm8350c_s1>;
		vdd-l7-bob-supply = <&vreg_bob>;

		pmr735a_s1: smps1 {
			regulator-name = "pmr735a_s1";
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1296000>;
		};

		pmr735a_s2: smps2 {
			regulator-name = "pmr735a_s2";
			regulator-min-microvolt = <500000>;
			regulator-max-microvolt = <1040000>;
		};

		pmr735a_s3: smps3 {
			regulator-name = "pmr735a_s3";
			regulator-min-microvolt = <435000>;
			regulator-max-microvolt = <2352000>;
		};

		pmr735a_l1: ldo1 {
			regulator-name = "pmr735a_l1";
			regulator-min-microvolt = <800000>;
			regulator-max-microvolt = <800000>;
		};

		pmr735a_l2: ldo2 {
			regulator-name = "pmr735a_l2";
			regulator-min-microvolt = <480000>;
			regulator-max-microvolt = <912000>;
		};

		pmr735a_l3: ldo3 {
			regulator-name = "pmr735a_l3";
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
		};

		pmr735a_l4: ldo4 {
			regulator-name = "pmr735a_l4";
			regulator-min-microvolt = <1776000>;
			regulator-max-microvolt = <1776000>;
		};

		pmr735a_l5: ldo5 {
			regulator-name = "pmr735a_l5";
			regulator-min-microvolt = <880000>;
			regulator-max-microvolt = <880000>;
		};

		pmr735a_l6: ldo6 {
			regulator-name = "pmr735a_l6";
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
		};

		pmr735a_l7: ldo7 {
			regulator-name = "pmr735a_l7";
			regulator-min-microvolt = <2800000>;
			regulator-max-microvolt = <2800000>;
		};
	};
};

&gpi_dma0 {
	status = "okay";
};

&gpi_dma1 {
	status = "okay";
};

&gpi_dma2 {
	status = "okay";
};

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

	touchscreen@48 {
		compatible = "samsung,s6sy761";
		reg = <0x48>;
		interrupt-parent = <&tlmm>;
		interrupts = <21 0x2008>;
		vdd-supply = <&pm8350c_l2>;
		avdd-supply = <&pm8350c_l3>;

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

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

	pmic@75 {
		compatible = "dlg,slg51000";
		reg = <0x75>;
		dlg,cs-gpios = <&pm8350b_gpios 1 GPIO_ACTIVE_HIGH>;

		pinctrl-names = "default";
		pinctrl-0 = <&cam_pwr_a_cs>;

		regulators {
			slg51000_a_ldo1: ldo1 {
				regulator-name = "slg51000_a_ldo1";
				regulator-min-microvolt = <2400000>;
				regulator-max-microvolt = <3300000>;
			};

			slg51000_a_ldo2: ldo2 {
				regulator-name = "slg51000_a_ldo2";
				regulator-min-microvolt = <2400000>;
				regulator-max-microvolt = <3300000>;
			};

			slg51000_a_ldo3: ldo3 {
				regulator-name = "slg51000_a_ldo3";
				regulator-min-microvolt = <1200000>;
				regulator-max-microvolt = <3750000>;
			};

			slg51000_a_ldo4: ldo4 {
				regulator-name = "slg51000_a_ldo4";
				regulator-min-microvolt = <1200000>;
				regulator-max-microvolt = <3750000>;
			};

			slg51000_a_ldo5: ldo5 {
				regulator-name = "slg51000_a_ldo5";
				regulator-min-microvolt = <500000>;
				regulator-max-microvolt = <1200000>;
			};

			slg51000_a_ldo6: ldo6 {
				regulator-name = "slg51000_a_ldo6";
				regulator-min-microvolt = <500000>;
				regulator-max-microvolt = <1200000>;
			};

			slg51000_a_ldo7: ldo7 {
				regulator-name = "slg51000_a_ldo7";
				regulator-min-microvolt = <1200000>;
				regulator-max-microvolt = <3750000>;
			};
		};
	};
};

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

	/* NXP SN1X0 NFC @ 28 */
};

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

	cs35l41_l: speaker-amp@40 {
		compatible = "cirrus,cs35l41";
		reg = <0x40>;
		interrupt-parent = <&tlmm>;
		interrupts = <182 IRQ_TYPE_LEVEL_LOW>;
		reset-gpios = <&tlmm 183 GPIO_ACTIVE_HIGH>;
		cirrus,boost-peak-milliamp = <4000>;
		cirrus,boost-ind-nanohenry = <1000>;
		cirrus,boost-cap-microfarad = <15>;
		cirrus,gpio2-src-select = <2>;
		cirrus,gpio2-output-enable;
		cirrus,asp-sdout-hiz = <3>;
		#sound-dai-cells = <1>;
	};

	cs35l41_r: speaker-amp@41 {
		compatible = "cirrus,cs35l41";
		reg = <0x41>;
		interrupt-parent = <&tlmm>;
		interrupts = <182 IRQ_TYPE_LEVEL_LOW>;
		reset-gpios = <&tlmm 183 GPIO_ACTIVE_HIGH>;
		cirrus,boost-peak-milliamp = <4000>;
		cirrus,boost-ind-nanohenry = <1000>;
		cirrus,boost-cap-microfarad = <15>;
		cirrus,gpio2-src-select = <2>;
		cirrus,gpio2-output-enable;
		cirrus,asp-sdout-hiz = <3>;
		#sound-dai-cells = <1>;
	};
};

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

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

&i2c19 {
	clock-frequency = <1000000>;
	status = "okay";

	/* Cirrus Logic CS40L25A boosted haptics driver @ 40 */
};

&pcie0 {
	max-link-speed = <2>;
	status = "okay";
};

&pcie0_phy {
	vdda-phy-supply = <&pm8350_l5>;
	vdda-pll-supply = <&pm8350_l6>;
	status = "okay";
};

&pm8350_gpios {
	gpio-line-names = "ASSIGN1_THERM", /* GPIO_1 */
			  "LCD_ID",
			  "SDR_MMW_THERM",
			  "RF_ID",
			  "NC",
			  "VOL_DOWN_N",
			  "NC",
			  "NC",
			  "NC",
			  "PM8350_OPTION"; /* GPIO_10 */

	vol_down_n: vol-down-n-state {
		pins = "gpio6";
		function = "normal";
		power-source = <1>;
		bias-pull-up;
		input-enable;
	};
};

&pm8350b_gpios {
	cam_pwr_a_cs: cam-pwr-a-cs-state {
		pins = "gpio1";
		function = "normal";
		qcom,drive-strength = <PMIC_GPIO_STRENGTH_LOW>;
		power-source = <1>;
		drive-push-pull;
		output-high;
	};

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

	focus_n: focus-n-state {
		pins = "gpio8";
		function = "normal";
		power-source = <0>;
		bias-pull-up;
		input-enable;
	};
};

&pm8450_gpios {
	gpio-line-names = "FP_LDO_EN", /* GPIO_1 */
			  "",
			  "",
			  "";
};

&pmk8350_gpios {
	gpio-line-names = "NC", /* GPIO_1 */
			  "NC",
			  "DISP_THERM",
			  "PMK8350_OPTION";
};

&pon_pwrkey {
	status = "okay";
};

&pon_resin {
	linux,code = <KEY_VOLUMEUP>;
	status = "okay";
};

&remoteproc_adsp {
	firmware-name = "qcom/sm8450/Sony/nagara/adsp.mbn";
	status = "okay";
};

&remoteproc_cdsp {
	firmware-name = "qcom/sm8450/Sony/nagara/cdsp.mbn";
	status = "okay";
};

&remoteproc_slpi {
	firmware-name = "qcom/sm8450/Sony/nagara/slpi.mbn";
	status = "okay";
};

&qupv3_id_0 {
	status = "okay";
};

&qupv3_id_1 {
	status = "okay";
};

&qupv3_id_2 {
	status = "okay";
};

&sdhc_2 {
	cd-gpios = <&tlmm 92 GPIO_ACTIVE_HIGH>;
	pinctrl-names = "default", "sleep";
	pinctrl-0 = <&sdc2_default_state &sdc2_card_det_n>;
	pinctrl-1 = <&sdc2_sleep_state &sdc2_card_det_n>;
	vmmc-supply = <&pm8350c_l9>;
	vqmmc-supply = <&pm8350c_l6>;
	no-sdio;
	no-mmc;
	status = "okay";
};

&spi10 {
	status = "okay";

	/* NXP SN1X0 NFC Secure Element @ 0 */
};

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

	ts_reset_default: ts-reset-default-state {
		pins = "gpio20";
		function = "gpio";
		drive-strength = <2>;
		bias-disable;
		output-high;
	};

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

	telec_pwr_en: telec-pwr-en-state {
		pins = "gpio23";
		function = "gpio";
		drive-strength = <2>;
		bias-disable;
		output-low;
	};

	sdc2_card_det_n: sd-card-det-n-state {
		pins = "gpio92";
		function = "gpio";
		drive-strength = <2>;
		bias-pull-up;
	};
};

&uart7 {
	status = "okay";
};

&usb_1 {
	status = "okay";
};

&usb_1_dwc3 {
	dr_mode = "peripheral";
};

&usb_1_hsphy {
	vdda-pll-supply = <&pm8350_l5>;
	vdda18-supply = <&pm8350c_l1>;
	vdda33-supply = <&pm8350_l2>;
	status = "okay";
};

&usb_1_qmpphy {
	vdda-phy-supply = <&pm8350_l6>;
	vdda-pll-supply = <&pm8350_l1>;
	status = "okay";
};