// SPDX-License-Identifier: BSD-3-Clause
/*
 * Copyright (c) 2020, Konrad Dybcio <konrad.dybcio@somainline.org>
 * Copyright (c) 2020, AngeloGioacchino Del Regno
 *                     <angelogioacchino.delregno@somainline.org>
 */

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

/ {
	/* required for bootloader to select correct board */
	qcom,msm-id = <318 0>;
	qcom,board-id = <8 1>;
	qcom,pmic-id = <0x1001b 0x101011a 0x00 0x00 0x1001b 0x201011a 0x00 0x00>;

	/* This part enables graphical output via bootloader-enabled display */
	chosen {
		bootargs = "earlycon=tty0 console=tty0";

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

		stdout-path = "framebuffer0";

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

	board_vbat: vbat-regulator {
		compatible = "regulator-fixed";
		regulator-name = "VBAT";

		regulator-min-microvolt = <4000000>;
		regulator-max-microvolt = <4000000>;
		regulator-always-on;
		regulator-boot-on;
	};

	vph_pwr: vph-pwr-regulator {
		compatible = "regulator-fixed";
		regulator-name = "vph_pwr";

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

	cam_vdig_imx300_219_vreg: cam-vdig-imx300-219-regulator {
		compatible = "regulator-fixed";
		regulator-name = "cam_vdig_imx300_219_vreg";
		startup-delay-us = <0>;
		enable-active-high;
		gpio = <&tlmm 52 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&cam_vdig_default>;
	};

	cam_vana_front_vreg: cam-vana-front-regulator {
		compatible = "regulator-fixed";
		regulator-name = "cam_vana_front_vreg";
		startup-delay-us = <0>;
		enable-active-high;
		gpio = <&tlmm 51 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "default";
		pinctrl-0 = <&imx219_vana_default>;
	};

	cam_vana_rear_vreg: cam-vana-rear-regulator {
		compatible = "regulator-fixed";
		regulator-name = "cam_vana_rear_vreg";
		startup-delay-us = <0>;
		enable-active-high;
		gpio = <&tlmm 50 GPIO_ACTIVE_HIGH>;
		regulator-always-on;
		pinctrl-names = "default";
		pinctrl-0 = <&imx300_vana_default>;
	};

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

		key-camera-focus {
			label = "Camera Focus";
			gpios = <&tlmm 64 GPIO_ACTIVE_LOW>;
			linux,input-type = <1>;
			linux,code = <KEY_CAMERA_FOCUS>;
			debounce-interval = <15>;
		};

		key-camera-snapshot {
			label = "Camera Snapshot";
			gpios = <&tlmm 113 GPIO_ACTIVE_LOW>;
			linux,input-type = <1>;
			linux,code = <KEY_CAMERA>;
			debounce-interval = <15>;
		};

		key-vol-down {
			label = "Volume Down";
			gpios = <&pm660l_gpios 7 GPIO_ACTIVE_LOW>;
			linux,input-type = <1>;
			linux,code = <KEY_VOLUMEDOWN>;
			wakeup-source;
			debounce-interval = <15>;
		};
	};

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

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

		debug@ffb00000 {
			reg = <0x00 0xffb00000 0x00 0x100000>;
			no-map;
		};

		cont_splash_mem: splash@9d400000 {
			reg = <0 0x9d400000 0 (1920 * 1080 * 4)>;
			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>;
	};
};

&adsp_pil {
	firmware-name = "qcom/sdm630/Sony/nile/adsp.mdt";
};

&blsp_i2c1 {
	status = "okay";

	touchscreen: synaptics-rmi4-i2c@70 {
		compatible = "syna,rmi4-i2c";
		reg = <0x70>;
		#address-cells = <1>;
		#size-cells = <0>;
		interrupts-extended = <&tlmm 45 0x2008>;

		pinctrl-names = "default";
		pinctrl-0 = <&ts_int_active &ts_lcd_id_active>;

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

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

		rmi4-f11@11 {
			reg = <0x11>;
			syna,sensor-type = <1>;
		};
	};
};

&blsp_i2c2 {
	status = "okay";

	/* SMB1351 charger */
};

/* I2C3, 4, 5, 7 and 8 are disabled on this board. */

&blsp_i2c6 {
	status = "okay";

	/* NXP NFC */
};

&blsp1_uart2 {
	status = "okay";

	/* MSM serial console */
};

&blsp2_uart1 {
	status = "okay";

	/* HCI Bluetooth */
};

&pm660l_lpg {
	qcom,power-source = <1>;

	status = "okay";

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

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

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

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

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

&pon_pwrkey {
	status = "okay";
};

&pon_resin {
	status = "okay";

	linux,code = <KEY_VOLUMEUP>;
};

&qhee_code {
	reg = <0x00 0x85800000 0x00 0x3700000>;
};

&qusb2phy0 {
	status = "okay";

	vdd-supply = <&vreg_l1b_0p925>;
	vdda-pll-supply = <&vreg_l10a_1p8>;
	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>;
			regulator-ramp-delay = <0>;
		};

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

		/*
		 * At least on Nile's configuration, S3B/S4B (VDD_CX) and
		 * S5B (VDD_MX) are managed only through RPM Power Domains.
		 * Trying to set a voltage on the main supply will create
		 * havoc and freeze the SoC.
		 * In any case, reference voltages for these regulators are:
		 * S3B/S4B: 0.870V
		 * S5B: 0.915V
		 */

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

		vreg_l2b_2p95: l2 {
			/*
			 * This regulator supports 1.648 - 3.104V on this board
			 * but we set a max voltage of anything less than 2.7V
			 * to satisfy a condition in sdhci.c that will disable
			 * 3.3V SDHCI signaling, which happens to be not really
			 * supported on the Xperia Nile/Ganges platform.
			 */
			regulator-min-microvolt = <1648000>;
			regulator-max-microvolt = <2696000>;
			regulator-enable-ramp-delay = <250>;
			regulator-ramp-delay = <0>;
			regulator-allow-set-load;
		};

		vreg_l3b_3p0: l3 {
			regulator-min-microvolt = <2800000>;
			regulator-max-microvolt = <2800000>;
			regulator-enable-ramp-delay = <250>;
			regulator-ramp-delay = <0>;
			regulator-min-microamp = <200>;
			regulator-max-microamp = <600000>;
			regulator-system-load = <100000>;
			regulator-allow-set-load;
		};

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

			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-ramp-delay = <0>;
			regulator-allow-set-load;
			regulator-system-load = <800000>;
		};

		vreg_l6b_3p3: l6 {
			regulator-min-microvolt = <1704000>;
			regulator-max-microvolt = <3312000>;
			regulator-enable-ramp-delay = <250>;
			regulator-ramp-delay = <0>;
		};

		vreg_l7b_3p125: l7 {
			regulator-min-microvolt = <2704000>;
			regulator-max-microvolt = <3128000>;
			regulator-enable-ramp-delay = <250>;
			regulator-ramp-delay = <0>;
		};

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

		/* L9B (870mV) is currently unused */
		/* L10B (915mV) is currently unused */

		vreg_bob: bob {
			regulator-min-microvolt = <3304000>;
			regulator-max-microvolt = <3624000>;
			regulator-enable-ramp-delay = <500>;
			regulator-ramp-delay = <0>;
		};
	};

	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 = <2040000>;
			regulator-max-microvolt = <2040000>;
			regulator-enable-ramp-delay = <200>;
			regulator-ramp-delay = <0>;
			regulator-always-on;
		};

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

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

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

		vreg_l2a_1p0: l2 {
			regulator-min-microvolt = <944000>;
			regulator-max-microvolt = <1008000>;
			regulator-enable-ramp-delay = <250>;
			regulator-ramp-delay = <0>;
		};

		vreg_l3a_1p0: l3 {
			regulator-min-microvolt = <944000>;
			regulator-max-microvolt = <1008000>;
			regulator-enable-ramp-delay = <250>;
			regulator-ramp-delay = <0>;
		};

		vreg_l5a_0p848: l5 {
			regulator-min-microvolt = <800000>;
			regulator-max-microvolt = <952000>;
			regulator-enable-ramp-delay = <250>;
			regulator-ramp-delay = <0>;
		};

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

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

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

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

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

		vreg_l11a_1p8: l11 {
			regulator-min-microvolt = <1784000>;
			regulator-max-microvolt = <1944000>;
			regulator-enable-ramp-delay = <250>;
			regulator-ramp-delay = <0>;
		};

		vreg_l12a_1p8: l12 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1944000>;
			regulator-enable-ramp-delay = <250>;
			regulator-ramp-delay = <0>;
		};

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

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

		vreg_l15a_1p8: l15 {
			regulator-min-microvolt = <1648000>;
			regulator-max-microvolt = <2952000>;
			regulator-enable-ramp-delay = <250>;
			regulator-ramp-delay = <0>;
		};

		/* L16A (2.70V) is unused */

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

		vreg_l18a_1v8: l18 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-enable-ramp-delay = <250>;
			regulator-ramp-delay = <10>;
			regulator-min-microamp = <200>;
			regulator-max-microamp = <50000>;
			regulator-system-load = <10000>;
			regulator-allow-set-load;
		};

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

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

	/* SoMC Nile platform's eMMC doesn't support HS200 mode */
	mmc-ddr-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>;

	ts_int_active: ts-int-active-state {
		pins = "gpio45";
		function = "gpio";
		drive-strength = <8>;
		bias-pull-up;
	};

	ts_lcd_id_active: ts-lcd-id-active-state {
		pins = "gpio56";
		function = "gpio";
		drive-strength = <8>;
		bias-disable;
	};

	imx300_vana_default: imx300-vana-default-state {
		pins = "gpio50";
		function = "gpio";
		bias-disable;
		drive-strength = <2>;
	};

	imx219_vana_default: imx219-vana-default-state {
		pins = "gpio51";
		function = "gpio";
		bias-disable;
		drive-strength = <2>;
	};

	cam_vdig_default: cam-vdig-default-state {
		pins = "gpio52";
		function = "gpio";
		bias-disable;
		drive-strength = <2>;
	};
};

&usb3 {
	status = "okay";
};

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