// SPDX-License-Identifier: GPL-2.0
/*
 * SDM845 OnePlus 6(T) (enchilada / fajita) common device tree source
 *
 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
 */

/dts-v1/;

#include <dt-bindings/input/linux-event-codes.h>
#include <dt-bindings/regulator/qcom,rpmh-regulator.h>
#include <dt-bindings/sound/qcom,q6afe.h>
#include <dt-bindings/sound/qcom,q6asm.h>

#include "sdm845.dtsi"
#include "sdm845-wcd9340.dtsi"
#include "pm8998.dtsi"
#include "pmi8998.dtsi"

/delete-node/ &rmtfs_mem;

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

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

	gpio-hall-sensor {
		compatible = "gpio-keys";
		label = "Hall effect sensor";

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

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

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

		pinctrl-names = "default";
		pinctrl-0 = <&volume_down_gpio &volume_up_gpio>;

		key-vol-down {
			label = "Volume down";
			linux,code = <KEY_VOLUMEDOWN>;
			gpios = <&pm8998_gpios 5 GPIO_ACTIVE_LOW>;
			debounce-interval = <15>;
		};

		key-vol-up {
			label = "Volume up";
			linux,code = <KEY_VOLUMEUP>;
			gpios = <&pm8998_gpios 6 GPIO_ACTIVE_LOW>;
			debounce-interval = <15>;
		};
	};

	reserved-memory {
		/*
		 * The rmtfs_mem needs to be guarded due to "XPU limitations"
		 * it is otherwise possible for an allocation adjacent to the
		 * rmtfs_mem region to trigger an XPU violation, causing a crash.
		 */
		rmtfs_lower_guard: rmtfs-lower-guard@f5b00000 {
			no-map;
			reg = <0 0xf5b00000 0 0x1000>;
		};
		/*
		 * The rmtfs memory region in downstream is 'dynamically allocated'
		 * but given the same address every time. Hard code it as this address is
		 * where the modem firmware expects it to be.
		 */
		rmtfs_mem: rmtfs-mem@f5b01000 {
			compatible = "qcom,rmtfs-mem";
			reg = <0 0xf5b01000 0 0x200000>;
			no-map;

			qcom,client-id = <1>;
			qcom,vmid = <15>;
		};
		rmtfs_upper_guard: rmtfs-upper-guard@f5d01000 {
			no-map;
			reg = <0 0xf5d01000 0 0x1000>;
		};

		/*
		 * It seems like reserving the old rmtfs_mem region is also needed to prevent
		 * random crashes which are most likely modem related, more testing needed.
		 */
		removed_region: removed-region@88f00000 {
			no-map;
			reg = <0 0x88f00000 0 0x1c00000>;
		};

		ramoops: ramoops@ac300000 {
			compatible = "ramoops";
			reg = <0 0xac300000 0 0x400000>;
			record-size = <0x40000>;
			console-size = <0x40000>;
			ftrace-size = <0x40000>;
			pmsg-size = <0x200000>;
			ecc-size = <16>;
		};
	};

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

	/*
	 * Apparently RPMh does not provide support for PM8998 S4 because it
	 * is always-on; model it as a fixed regulator.
	 */
	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>;
	};

	/*
	 * The touchscreen regulator seems to be controlled somehow by a gpio.
	 * Model it as a fixed regulator and keep it on. Without schematics we
	 * don't know how this is actually wired up...
	 */
	ts_1p8_supply: ts-1p8-regulator {
		compatible = "regulator-fixed";
		regulator-name = "ts_1p8_supply";

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

		gpio = <&tlmm 88 0>;
		enable-active-high;
		regulator-boot-on;
	};
};

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

&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_1p025>;
		vdd-l2-l8-l17-supply = <&vreg_s3a_1p35>;
		vdd-l3-l11-supply = <&vreg_s7a_1p025>;
		vdd-l4-l5-supply = <&vreg_s7a_1p025>;
		vdd-l6-supply = <&vph_pwr>;
		vdd-l7-l12-l14-l15-supply = <&vreg_s5a_2p04>;
		vdd-l9-supply = <&vreg_bob>;
		vdd-l10-l23-l25-supply = <&vreg_bob>;
		vdd-l13-l19-l21-supply = <&vreg_bob>;
		vdd-l16-l28-supply = <&vreg_bob>;
		vdd-l18-l22-supply = <&vreg_bob>;
		vdd-l20-l24-supply = <&vreg_bob>;
		vdd-l26-supply = <&vreg_s3a_1p35>;
		vin-lvs-1-2-supply = <&vreg_s4a_1p8>;

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

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

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

		vdda_mipi_dsi0_pll:
		vdda_qlink_lv:
		vdda_ufs1_core:
		vdda_usb1_ss_core:
		vreg_l1a_0p875: 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_l5a_0p8: ldo5 {
			regulator-min-microvolt = <800000>;
			regulator-max-microvolt = <800000>;
			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>;
		};

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

		vreg_l14a_1p88: ldo14 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
			regulator-always-on;
		};

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

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

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

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

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

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

		vreg_l28a_3p0: ldo28 {
			regulator-min-microvolt = <2856000>;
			regulator-max-microvolt = <3008000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};
	};

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

		vdd-bob-supply = <&vph_pwr>;

		vreg_bob: bob {
			regulator-min-microvolt = <3312000>;
			regulator-max-microvolt = <3600000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_AUTO>;
			regulator-allow-bypass;
		};
	};

	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 {
	status = "okay";
	firmware-name = "qcom/sdm845/oneplus6/cdsp.mbn";
};

&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";
};

&gpu {
	status = "okay";

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

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

	bq27441_fg: bq27441-battery@55 {
		compatible = "ti,bq27411";
		status = "okay";
		reg = <0x55>;
	};
};

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

	synaptics-rmi4-i2c@20 {
		compatible = "syna,rmi4-i2c";
		reg = <0x20>;
		#address-cells = <1>;
		#size-cells = <0>;
		interrupts-extended = <&tlmm 125 IRQ_TYPE_EDGE_FALLING>;

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

		vdd-supply = <&vreg_l28a_3p0>;
		vio-supply = <&ts_1p8_supply>;

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

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

		rmi4_f12: rmi4-f12@12 {
			reg = <0x12>;
			touchscreen-x-mm = <68>;
			touchscreen-y-mm = <144>;
			syna,sensor-type = <1>;
			syna,rezero-wait-ms = <200>;
		};
	};
};

&ipa {
	qcom,gsi-loader = "self";
	memory-region = <&ipa_fw_mem>;
	firmware-name = "qcom/sdm845/oneplus6/ipa_fws.mbn";
	status = "okay";
};

&mdss {
	status = "okay";
};

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

	/*
	 * Both devices use different panels but all other properties
	 * are common. Compatible line is declared in device dts.
	 */
	display_panel: panel@0 {
		status = "disabled";

		reg = <0>;

		vddio-supply = <&vreg_l14a_1p88>;

		reset-gpios = <&tlmm 6 GPIO_ACTIVE_LOW>;

		pinctrl-names = "default";
		pinctrl-0 = <&panel_reset_pins &panel_te_pin &panel_esd_pin>;

		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 {
	status = "okay";
	vdds-supply = <&vdda_mipi_dsi0_pll>;
};

/* Modem/wifi */
&mss_pil {
	status = "okay";
	firmware-name = "qcom/sdm845/oneplus6/mba.mbn", "qcom/sdm845/oneplus6/modem.mbn";
};

&pm8998_gpios {
	volume_down_gpio: pm8998-gpio5-state {
		pinconf {
			pins = "gpio5";
			function = "normal";
			input-enable;
			bias-pull-up;
			qcom,drive-strength = <0>;
		};
	};

	volume_up_gpio: pm8998-gpio6-state {
		pinconf {
			pins = "gpio6";
			function = "normal";
			input-enable;
			bias-pull-up;
			qcom,drive-strength = <0>;
		};
	};
};

&pmi8998_charger {
	status = "okay";
};

&q6afedai {
	qi2s@22 {
		reg = <22>;
		qcom,sd-lines = <1>;
	};

	qi2s@23 {
		reg = <23>;
		qcom,sd-lines = <0>;
	};
};

&q6asmdai {
	dai@0 {
		reg = <0>;
	};

	dai@1 {
		reg = <1>;
	};

	dai@2 {
		reg = <2>;
	};

	dai@3 {
		reg = <3>;
	};

	dai@4 {
		reg = <4>;
	};

	dai@5 {
		reg = <5>;
	};
};

&qupv3_id_1 {
	status = "okay";
};

&qupv3_id_0 {
	status = "okay";
};

&qup_i2c10_default {
	drive-strength = <2>;
	bias-disable;
};

&qup_i2c12_default {
	drive-strength = <2>;
	bias-disable;
};

&qup_uart9_rx {
	drive-strength = <2>;
	bias-pull-up;
};

&qup_uart9_tx {
	drive-strength = <2>;
	bias-disable;
};

&slpi_pas {
	firmware-name = "qcom/sdm845/oneplus6/slpi.mbn";
	status = "okay";
};

&sound {
	compatible = "qcom,sdm845-sndcard";
	pinctrl-0 = <&quat_mi2s_active &quat_mi2s_sd0_active &quat_mi2s_sd1_active>;
	pinctrl-names = "default";
	status = "okay";

	mm1-dai-link {
		link-name = "MultiMedia1";
		cpu {
			sound-dai = <&q6asmdai MSM_FRONTEND_DAI_MULTIMEDIA1>;
		};
	};

	mm2-dai-link {
		link-name = "MultiMedia2";
		cpu {
			sound-dai = <&q6asmdai MSM_FRONTEND_DAI_MULTIMEDIA2>;
		};
	};

	mm3-dai-link {
		link-name = "MultiMedia3";
		cpu {
			sound-dai = <&q6asmdai MSM_FRONTEND_DAI_MULTIMEDIA3>;
		};
	};

	mm4-dai-link {
		link-name = "MultiMedia4";
		cpu {
			sound-dai = <&q6asmdai MSM_FRONTEND_DAI_MULTIMEDIA4>;
		};
	};

	mm5-dai-link {
		link-name = "MultiMedia5";
		cpu {
			sound-dai = <&q6asmdai MSM_FRONTEND_DAI_MULTIMEDIA5>;
		};
	};

	mm6-dai-link {
		link-name = "MultiMedia6";
		cpu {
			sound-dai = <&q6asmdai MSM_FRONTEND_DAI_MULTIMEDIA6>;
		};
	};

	speaker_playback_dai: speaker-dai-link {
		link-name = "Speaker Playback";
		cpu {
			sound-dai = <&q6afedai QUATERNARY_MI2S_RX>;
		};

		platform {
			sound-dai = <&q6routing>;
		};
	};

	slim-dai-link {
		link-name = "SLIM Playback 1";
		cpu {
			sound-dai = <&q6afedai SLIMBUS_0_RX>;
		};

		platform {
			sound-dai = <&q6routing>;
		};

		codec {
			sound-dai = <&wcd9340 0>;
		};
	};

	slimcap-dai-link {
		link-name = "SLIM Capture 1";
		cpu {
			sound-dai = <&q6afedai SLIMBUS_0_TX>;
		};

		platform {
			sound-dai = <&q6routing>;
		};

		codec {
			sound-dai = <&wcd9340 1>;
		};
	};

	slim2-dai-link {
		link-name = "SLIM Playback 2";
		cpu {
			sound-dai = <&q6afedai SLIMBUS_1_RX>;
		};

		platform {
			sound-dai = <&q6routing>;
		};

		codec {
			sound-dai = <&wcd9340 2>; /* AIF2_PB */
		};
	};

	slimcap2-dai-link {
		link-name = "SLIM Capture 2";
		cpu {
			sound-dai = <&q6afedai SLIMBUS_1_TX>;
		};

		platform {
			sound-dai = <&q6routing>;
		};

		codec {
			sound-dai = <&wcd9340 3>; /* AIF2_CAP */
		};
	};

	slimcap3-dai-link {
		link-name = "SLIM Capture 3";
		cpu {
			sound-dai = <&q6afedai SLIMBUS_2_TX>;
		};

		platform {
			sound-dai = <&q6routing>;
		};

		codec {
			sound-dai = <&wcd9340 5>; /* AIF3_CAP */
		};
	};
};

&uart6 {
	status = "okay";

	pinctrl-0 = <&qup_uart6_4pin>;

	bluetooth {
		compatible = "qcom,wcn3990-bt";

		/*
		 * This path is relative to the qca/
		 * subdir under lib/firmware.
		 */
		firmware-name = "oneplus6/crnv21.bin";

		vddio-supply = <&vreg_s4a_1p8>;
		vddxo-supply = <&vreg_l7a_1p8>;
		vddrf-supply = <&vreg_l17a_1p3>;
		vddch0-supply = <&vreg_l25a_3p3>;
		max-speed = <3200000>;
	};
};

&uart9 {
	label = "LS-UART1";
	status = "okay";
};

&ufs_mem_hc {
	status = "okay";

	reset-gpios = <&tlmm 150 GPIO_ACTIVE_LOW>;

	vcc-supply = <&vreg_l20a_2p95>;
	vcc-max-microamp = <600000>;
};

&ufs_mem_phy {
	status = "okay";

	vdda-phy-supply = <&vdda_ufs1_core>;
	vdda-pll-supply = <&vdda_ufs1_1p2>;
};

&usb_1 {
	status = "okay";

	/*
	 * disable USB3 clock requirement as the device only supports
	 * USB2.
	 */
	qcom,select-utmi-as-pipe-clk;
};

&usb_1_dwc3 {
	/*
	 * We don't have the capability to switch modes yet.
	 */
	dr_mode = "peripheral";

	/* fastest mode for USB 2 */
	maximum-speed = "high-speed";

	/* Remove USB3 phy as it's unused on this device. */
	phys = <&usb_1_hsphy>;
	phy-names = "usb2-phy";
};

&usb_1_hsphy {
	status = "okay";

	vdd-supply = <&vdda_usb1_ss_core>;
	vdda-pll-supply = <&vdda_qusb_hs0_1p8>;
	vdda-phy-dpdm-supply = <&vdda_qusb_hs0_3p1>;

	qcom,imp-res-offset-value = <8>;
	qcom,hstx-trim-value = <QUSB2_V2_HSTX_TRIM_21_6_MA>;
	qcom,preemphasis-level = <QUSB2_V2_PREEMPHASIS_5_PERCENT>;
	qcom,preemphasis-width = <QUSB2_V2_PREEMPHASIS_WIDTH_HALF_BIT>;
};

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

	hall_sensor_default: hall-sensor-default-state {
		pins = "gpio124";
		function = "gpio";
		drive-strength = <2>;
		bias-disable;
	};

	tri_state_key_default: tri-state-key-default-state {
		pins = "gpio40", "gpio42", "gpio26";
		function = "gpio";
		drive-strength = <2>;
		bias-disable;
	};

	ts_default_pins: ts-int-state {
		pins = "gpio99", "gpio125";
		function = "gpio";
		drive-strength = <16>;
		bias-pull-up;
	};

	panel_reset_pins: panel-reset-state {
		pins = "gpio6", "gpio25", "gpio26";
		function = "gpio";
		drive-strength = <8>;
		bias-disable;
	};

	panel_te_pin: panel-te-state {
		pins = "gpio10";
		function = "mdp_vsync";
		drive-strength = <2>;
		bias-disable;
	};

	panel_esd_pin: panel-esd-state {
		pins = "gpio30";
		function = "gpio";
		drive-strength = <2>;
		bias-pull-down;
	};

	speaker_default: speaker-default-state {
		pins = "gpio69";
		function = "gpio";
		drive-strength = <16>;
		bias-pull-up;
		output-high;
	};
};

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

&wcd9340 {
	pinctrl-0 = <&wcd_intr_default>;
	pinctrl-names = "default";
	reset-gpios = <&tlmm 64 GPIO_ACTIVE_HIGH>;
	vdd-buck-supply = <&vreg_s4a_1p8>;
	vdd-buck-sido-supply = <&vreg_s4a_1p8>;
	vdd-tx-supply = <&vreg_s4a_1p8>;
	vdd-rx-supply = <&vreg_s4a_1p8>;
	vdd-io-supply = <&vreg_s4a_1p8>;
};

&wifi {
	status = "okay";
	vdd-0.8-cx-mx-supply = <&vreg_l5a_0p8>;
	vdd-1.8-xo-supply = <&vreg_l7a_1p8>;
	vdd-1.3-rfa-supply = <&vreg_l17a_1p3>;
	vdd-3.3-ch0-supply = <&vreg_l25a_3p3>;
	vdd-3.3-ch1-supply = <&vreg_l23a_3p3>;

	qcom,snoc-host-cap-8bit-quirk;
};