// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2020, Alexey Minnekhanov <alexey.min@gmail.com>
 * Copyright (c) 2021, Dang Huynh <danct12@riseup.net>
 */

/dts-v1/;

#include "sdm660.dtsi"
#include "pm660.dtsi"
#include "pm660l.dtsi"
#include <dt-bindings/input/input.h>
#include <dt-bindings/input/gpio-keys.h>

/ {
	model = "Xiaomi Redmi Note 7";
	compatible = "xiaomi,lavender", "qcom,sdm660";
	chassis-type = "handset";

	aliases {
		serial0 = &blsp1_uart2;
	};

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

		stdout-path = "serial0:115200n8";

		framebuffer0: framebuffer@9d400000 {
			compatible = "simple-framebuffer";
			reg = <0 0x9d400000 0 (1080 * 2340 * 4)>;
			width = <1080>;
			height = <2340>;
			stride = <(1080 * 4)>;
			format = "a8r8g8b8";
		};
	};

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

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

		key-volup {
			label = "Volume Up";
			gpios = <&pm660l_gpios 7 GPIO_ACTIVE_LOW>;
			linux,code = <KEY_VOLUMEUP>;
			debounce-interval = <15>;
		};
	};

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

		ramoops@a0000000 {
			compatible = "ramoops";
			reg = <0x0 0xa0000000 0x0 0x400000>;
			console-size = <0x20000>;
			record-size = <0x20000>;
			ftrace-size = <0x0>;
			pmsg-size = <0x20000>;
		};

		framebuffer_mem: memory@9d400000 {
			reg = <0x0 0x9d400000 0x0 0x23ff000>;
			no-map;
		};
	};

	/*
	 * Until we hook up type-c detection, we
	 * have to stick with this. But it works.
	 */
	extcon_usb: extcon-usb {
		compatible = "linux,extcon-usb-gpio";
		id-gpios = <&tlmm 58 GPIO_ACTIVE_HIGH>;
	};
};

&blsp1_uart2 {
	status = "okay";
};

&pon_pwrkey {
	status = "okay";
};

&pon_resin {
	status = "okay";

	linux,code = <KEY_VOLUMEDOWN>;
};

&qusb2phy0 {
	status = "okay";

	vdd-supply = <&vreg_l1b_0p925>;
	vdda-phy-dpdm-supply = <&vreg_l7b_3p125>;
};

&rpm_requests {
	regulators-0 {
		compatible = "qcom,rpm-pm660l-regulators";

		vdd_s1-supply = <&vph_pwr>;
		vdd_s2-supply = <&vph_pwr>;
		vdd_s3_s4-supply = <&vph_pwr>;
		vdd_s5-supply = <&vph_pwr>;
		vdd_s6-supply = <&vph_pwr>;

		vdd_l1_l9_l10-supply = <&vreg_s2b_1p05>;
		vdd_l2-supply = <&vreg_bob>;
		vdd_l3_l5_l7_l8-supply = <&vreg_bob>;
		vdd_l4_l6-supply = <&vreg_bob>;
		vdd_bob-supply = <&vph_pwr>;

		vreg_s1b_1p125: s1 {
			regulator-min-microvolt = <1125000>;
			regulator-max-microvolt = <1125000>;
			regulator-enable-ramp-delay = <200>;
		};

		vreg_s2b_1p05: s2 {
			regulator-min-microvolt = <1050000>;
			regulator-max-microvolt = <1050000>;
			regulator-enable-ramp-delay = <200>;
		};

		/* LDOs */
		vreg_l1b_0p925: l1 {
			regulator-min-microvolt = <800000>;
			regulator-max-microvolt = <925000>;
			regulator-enable-ramp-delay = <250>;
			regulator-allow-set-load;
		};

		/* SDHCI 3.3V signal doesn't seem to be supported. */
		vreg_l2b_2p95: l2 {
			regulator-min-microvolt = <1648000>;
			regulator-max-microvolt = <2696000>;
			regulator-enable-ramp-delay = <250>;
			regulator-allow-set-load;
		};

		vreg_l3b_3p3: l3 {
			regulator-min-microvolt = <1700000>;
			regulator-max-microvolt = <3300000>;
			regulator-enable-ramp-delay = <250>;
			regulator-allow-set-load;
		};

		vreg_l4b_2p95: l4 {
			regulator-min-microvolt = <2944000>;
			regulator-max-microvolt = <2952000>;
			regulator-enable-ramp-delay = <250>;

			regulator-min-microamp = <200>;
			regulator-max-microamp = <600000>;
			regulator-system-load = <570000>;
			regulator-allow-set-load;
		};

		/*
		 * Downstream specifies a range of 1721-3600mV,
		 * but the only assigned consumers are SDHCI2 VMMC
		 * and Coresight QPDI that both request pinned 2.95V.
		 * Tighten the range to 1.8-3.328 (closest to 3.3) to
		 * make the mmc driver happy.
		 */
		vreg_l5b_2p95: l5 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <3328000>;
			regulator-enable-ramp-delay = <250>;
			regulator-allow-set-load;
			regulator-system-load = <800000>;
		};

		vreg_l7b_3p125: l7 {
			regulator-min-microvolt = <2700000>;
			regulator-max-microvolt = <3125000>;
			regulator-enable-ramp-delay = <250>;
		};

		vreg_l8b_3p3: l8 {
			regulator-min-microvolt = <3200000>;
			regulator-max-microvolt = <3400000>;
			regulator-enable-ramp-delay = <250>;
		};

		vreg_bob: bob {
			regulator-min-microvolt = <3300000>;
			regulator-max-microvolt = <3600000>;
			regulator-enable-ramp-delay = <500>;
		};
	};

	regulators-1 {
		compatible = "qcom,rpm-pm660-regulators";

		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_l1_l6_l7-supply = <&vreg_s5a_1p35>;
		vdd_l2_l3-supply = <&vreg_s2b_1p05>;
		vdd_l5-supply = <&vreg_s2b_1p05>;
		vdd_l8_l9_l10_l11_l12_l13_l14-supply = <&vreg_s4a_2p04>;
		vdd_l15_l16_l17_l18_l19-supply = <&vreg_bob>;

		/*
		 * S1A (FTAPC0), S2A (FTAPC1), S3A (HFAPC1) are managed
		 * by the Core Power Reduction hardened (CPRh) and the
		 * Operating State Manager (OSM) HW automatically.
		 */

		vreg_s4a_2p04: s4 {
			regulator-min-microvolt = <1805000>;
			regulator-max-microvolt = <2040000>;
			regulator-enable-ramp-delay = <200>;
			regulator-always-on;
		};

		vreg_s5a_1p35: s5 {
			regulator-min-microvolt = <1224000>;
			regulator-max-microvolt = <1350000>;
			regulator-enable-ramp-delay = <200>;
		};

		vreg_s6a_0p87: s6 {
			regulator-min-microvolt = <504000>;
			regulator-max-microvolt = <992000>;
			regulator-enable-ramp-delay = <150>;
		};

		/* LDOs */
		vreg_l1a_1p225: l1 {
			regulator-min-microvolt = <1150000>;
			regulator-max-microvolt = <1250000>;
			regulator-enable-ramp-delay = <250>;
			regulator-allow-set-load;
		};

		vreg_l2a_1p0: l2 {
			regulator-min-microvolt = <950000>;
			regulator-max-microvolt = <1010000>;
			regulator-enable-ramp-delay = <250>;
		};

		vreg_l3a_1p0: l3 {
			regulator-min-microvolt = <950000>;
			regulator-max-microvolt = <1010000>;
			regulator-enable-ramp-delay = <250>;
		};

		vreg_l5a_0p848: l5 {
			regulator-min-microvolt = <525000>;
			regulator-max-microvolt = <950000>;
			regulator-enable-ramp-delay = <250>;
		};

		vreg_l6a_1p3: l6 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1370000>;
			regulator-allow-set-load;
			regulator-enable-ramp-delay = <250>;
		};

		vreg_l7a_1p2: l7 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
			regulator-enable-ramp-delay = <250>;
		};

		vreg_l8a_1p8: l8 {
			regulator-min-microvolt = <1750000>;
			regulator-max-microvolt = <1800000>;
			regulator-enable-ramp-delay = <250>;
			regulator-system-load = <325000>;
			regulator-allow-set-load;
		};

		vreg_l9a_1p8: l9 {
			regulator-min-microvolt = <1750000>;
			regulator-max-microvolt = <1900000>;
			regulator-enable-ramp-delay = <250>;
			regulator-allow-set-load;
		};

		vreg_l10a_1p8: l10 {
			regulator-min-microvolt = <1780000>;
			regulator-max-microvolt = <1950000>;
			regulator-enable-ramp-delay = <250>;
			regulator-allow-set-load;
		};

		vreg_l11a_1p8: l11 {
			regulator-min-microvolt = <1780000>;
			regulator-max-microvolt = <1950000>;
			regulator-enable-ramp-delay = <250>;
		};

		vreg_l12a_1p8: l12 {
			regulator-min-microvolt = <1780000>;
			regulator-max-microvolt = <1950000>;
			regulator-enable-ramp-delay = <250>;
		};

		/* This gives power to the LPDDR4: never turn it off! */
		vreg_l13a_1p8: l13 {
			regulator-min-microvolt = <1780000>;
			regulator-max-microvolt = <1950000>;
			regulator-enable-ramp-delay = <250>;
			regulator-boot-on;
			regulator-always-on;
		};

		vreg_l14a_1p8: l14 {
			regulator-min-microvolt = <1710000>;
			regulator-max-microvolt = <1900000>;
			regulator-enable-ramp-delay = <250>;
		};

		vreg_l15a_1p8: l15 {
			regulator-min-microvolt = <1650000>;
			regulator-max-microvolt = <2950000>;
			regulator-enable-ramp-delay = <250>;
		};

		vreg_l16a_2p7: l16 {
			regulator-min-microvolt = <2800000>;
			regulator-max-microvolt = <2800000>;
			regulator-enable-ramp-delay = <250>;
			regulator-always-on;
		};

		vreg_l17a_1p8: l17 {
			regulator-min-microvolt = <1648000>;
			regulator-max-microvolt = <2952000>;
			regulator-enable-ramp-delay = <250>;
		};

		vreg_l19a_3p3: l19 {
			regulator-min-microvolt = <3312000>;
			regulator-max-microvolt = <3400000>;
			regulator-enable-ramp-delay = <250>;
			regulator-allow-set-load;
		};
	};
};

&pm660l_wled {
	status = "okay";

	qcom,switching-freq = <800>;
	qcom,current-limit-microamp = <20000>;
	qcom,num-strings = <2>;
};

&sdc2_state_on {
	sd-cd-pins {
		pins = "gpio54";
		function = "gpio";
		bias-pull-up;
		drive-strength = <2>;
	};
};

&sdc2_state_off {
	sd-cd-pins {
		pins = "gpio54";
		function = "gpio";
		bias-disable;
		drive-strength = <2>;
	};
};

&sdhc_1 {
	status = "okay";
	supports-cqe;

	mmc-hs200-1_8v;
	mmc-hs400-1_8v;
	mmc-hs400-enhanced-strobe;

	vmmc-supply = <&vreg_l4b_2p95>;
	vqmmc-supply = <&vreg_l8a_1p8>;
};

&sdhc_2 {
	status = "okay";

	vmmc-supply = <&vreg_l5b_2p95>;
	vqmmc-supply = <&vreg_l2b_2p95>;
};

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

&usb3 {
	status = "okay";
};

&usb3_dwc3 {
	dr_mode = "peripheral";
	extcon = <&extcon_usb>;
};