// SPDX-License-Identifier: BSD-3-Clause
/*
 * Lenovo Yoga C630
 *
 * Copyright (c) 2019, Linaro Ltd.
 */

/dts-v1/;

#include <dt-bindings/input/gpio-keys.h>
#include <dt-bindings/input/input.h>
#include <dt-bindings/regulator/qcom,rpmh-regulator.h>
#include <dt-bindings/sound/qcom,q6afe.h>
#include <dt-bindings/sound/qcom,q6asm.h>
#include "sdm850.dtsi"
#include "sdm845-wcd9340.dtsi"
#include "pm8998.dtsi"

/*
 * Update following upstream (sdm845.dtsi) reserved
 * memory mappings for firmware loading to succeed
 * and enable the IPA device.
 */
/delete-node/ &ipa_fw_mem;
/delete-node/ &ipa_gsi_mem;
/delete-node/ &gpu_mem;
/delete-node/ &adsp_mem;
/delete-node/ &wlan_msa_mem;

/ {
	model = "Lenovo Yoga C630";
	compatible = "lenovo,yoga-c630", "qcom,sdm845";
	chassis-type = "convertible";

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

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

		pinctrl-names = "default";
		pinctrl-0 = <&lid_pin_active>, <&mode_pin_active>;

		switch-lid {
			gpios = <&tlmm 124 GPIO_ACTIVE_HIGH>;
			linux,input-type = <EV_SW>;
			linux,code = <SW_LID>;
			wakeup-source;
			wakeup-event-action = <EV_ACT_DEASSERTED>;
		};

		switch-mode {
			gpios = <&tlmm 95 GPIO_ACTIVE_HIGH>;
			linux,input-type = <EV_SW>;
			linux,code = <SW_TABLET_MODE>;
		};
	};

	/* Reserved memory changes for IPA */
	reserved-memory {
		wlan_msa_mem: memory@8c400000 {
			reg = <0 0x8c400000 0 0x100000>;
			no-map;
		};

		gpu_mem: memory@8c515000 {
			reg = <0 0x8c515000 0 0x2000>;
			no-map;
		};

		ipa_fw_mem: memory@8c517000 {
			reg = <0 0x8c517000 0 0x5a000>;
			no-map;
		};

		adsp_mem: memory@8c600000 {
			reg = <0 0x8c600000 0 0x1a00000>;
			no-map;
		};
	};

	sw_edp_1p2: edp-1p2-regulator {
		compatible = "regulator-fixed";
		regulator-name = "sw_edp_1p2";

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

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

		gpio = <&pm8998_gpios 9 GPIO_ACTIVE_HIGH>;
		enable-active-high;

		vin-supply = <&vreg_l2a_1p2>;
	};

	sn65dsi86_refclk: sn65dsi86-refclk {
		compatible = "fixed-clock";
		#clock-cells = <0>;

		clock-frequency = <19200000>;
	};

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

	vlcm_3v3: regulator-vlcm-3v3 {
		compatible = "regulator-fixed";
		regulator-name = "vlcm_3v3";

		vin-supply = <&vph_pwr>;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;

		gpio = <&tlmm 88 GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};

	backlight: backlight {
		compatible = "pwm-backlight";
		pwms = <&sn65dsi86 1000000>;
		enable-gpios = <&tlmm 11 GPIO_ACTIVE_HIGH>;
	};
};

&adsp_pas {
	firmware-name = "qcom/sdm850/LENOVO/81JL/qcadsp850.mbn";
	status = "okay";
};

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

		vdd-l2-l8-l17-supply = <&vreg_s3a_1p35>;
		vdd-l7-l12-l14-l15-supply = <&vreg_s5a_2p04>;

		vreg_s2a_1p125: smps2 {
		};

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

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

		vreg_s5a_2p04: smps5 {
			regulator-min-microvolt = <2040000>;
			regulator-max-microvolt = <2040000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_s7a_1p025: smps7 {
		};

		vdd_qusb_hs0:
		vdda_hp_pcie_core:
		vdda_mipi_csi0_0p9:
		vdda_mipi_csi1_0p9:
		vdda_mipi_csi2_0p9:
		vdda_mipi_dsi0_pll:
		vdda_mipi_dsi1_pll:
		vdda_qlink_lv:
		vdda_qlink_lv_ck:
		vdda_qrefs_0p875:
		vdda_pcie_core:
		vdda_pll_cc_ebi01:
		vdda_pll_cc_ebi23:
		vdda_sp_sensor:
		vdda_ufs1_core:
		vdda_ufs2_core:
		vdda_usb1_ss_core:
		vdda_usb2_ss_core:
		vreg_l1a_0p875: ldo1 {
			regulator-min-microvolt = <880000>;
			regulator-max-microvolt = <880000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vddpx_10:
		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 {
		};

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

		vddpx_13:
		vreg_l6a_1p8: ldo6 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			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 {
		};

		vreg_l9a_1p8: ldo9 {
		};

		vreg_l10a_1p8: ldo10 {
		};

		vreg_l11a_1p0: ldo11 {
		};

		vdd_qfprom:
		vdd_qfprom_sp:
		vdda_apc1_cs_1p8:
		vdda_gfx_cs_1p8:
		vdda_qrefs_1p8:
		vdda_qusb_hs0_1p8:
		vddpx_11:
		vreg_l12a_1p8: ldo12 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vddpx_2:
		vreg_l13a_2p95: ldo13 {
		};

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

		vreg_l15a_1p8: ldo15 {
		};

		vreg_l16a_2p7: ldo16 {
		};

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

		vreg_l18a_2p7: ldo18 {
		};

		vreg_l19a_3p0: ldo19 {
			regulator-min-microvolt = <3100000>;
			regulator-max-microvolt = <3108000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

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

		vreg_l21a_2p95: ldo21 {
		};

		vreg_l22a_2p85: ldo22 {
		};

		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 = <3075000>;
			regulator-max-microvolt = <3083000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

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

		vdda_hp_pcie_1p2:
		vdda_hv_ebi0:
		vdda_hv_ebi1:
		vdda_hv_ebi2:
		vdda_hv_ebi3:
		vdda_mipi_csi_1p25:
		vdda_mipi_dsi0_1p2:
		vdda_mipi_dsi1_1p2:
		vdda_pcie_1p2:
		vdda_ufs1_1p2:
		vdda_ufs2_1p2:
		vdda_usb1_ss_1p2:
		vdda_usb2_ss_1p2:
		vreg_l26a_1p2: ldo26 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1208000>;
			regulator-initial-mode = <RPMH_REGULATOR_MODE_HPM>;
		};

		vreg_l28a_3p0: ldo28 {
		};

		vreg_lvs1a_1p8: lvs1 {
		};

		vreg_lvs2a_1p8: lvs2 {
		};
	};
};

&cdsp_pas {
	firmware-name = "qcom/sdm850/LENOVO/81JL/qccdsp850.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";
};

&gpu {
	status = "okay";
	zap-shader {
		memory-region = <&gpu_mem>;
		firmware-name = "qcom/sdm850/LENOVO/81JL/qcdxkmsuc850.mbn";
	};
};

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

&i2c3 {
	status = "okay";
	clock-frequency = <400000>;
	/* Overwrite pinctrl-0 from sdm845.dtsi */
	pinctrl-0 = <&qup_i2c3_default &i2c3_hid_active>;

	tsel: hid@15 {
		compatible = "hid-over-i2c";
		reg = <0x15>;
		hid-descr-addr = <0x1>;

		interrupts-extended = <&tlmm 37 IRQ_TYPE_LEVEL_HIGH>;
	};

	tsc2: hid@2c {
		compatible = "hid-over-i2c";
		reg = <0x2c>;
		hid-descr-addr = <0x20>;

		interrupts-extended = <&tlmm 37 IRQ_TYPE_LEVEL_HIGH>;
	};
};

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

	tsc1: hid@10 {
		compatible = "hid-over-i2c";
		reg = <0x10>;
		hid-descr-addr = <0x1>;

		interrupts-extended = <&tlmm 125 IRQ_TYPE_LEVEL_LOW>;

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

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

	sn65dsi86: bridge@2c {
		compatible = "ti,sn65dsi86";
		reg = <0x2c>;
		pinctrl-names = "default";
		pinctrl-0 = <&sn65dsi86_pin_active>;

		enable-gpios = <&tlmm 96 GPIO_ACTIVE_HIGH>;

		vcca-supply = <&sw_edp_1p2>;
		vcc-supply = <&sw_edp_1p2>;
		vpll-supply = <&vreg_l14a_1p88>;
		vccio-supply = <&vreg_l14a_1p88>;

		clocks = <&sn65dsi86_refclk>;
		clock-names = "refclk";

		no-hpd;
		#pwm-cells = <1>;

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

			port@0 {
				reg = <0>;
				sn65dsi86_in_a: endpoint {
					remote-endpoint = <&mdss_dsi0_out>;
				};
			};

			port@1 {
				reg = <1>;
				sn65dsi86_out: endpoint {
					remote-endpoint = <&panel_in_edp>;
				};
			};
		};

		aux-bus {
			panel: panel {
				compatible = "boe,nv133fhm-n61";
				backlight = <&backlight>;
				power-supply = <&vlcm_3v3>;

				port {
					panel_in_edp: endpoint {
						remote-endpoint = <&sn65dsi86_out>;
					};
				};
			};
		};
	};
};

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

	ecsh: hid@5c {
		compatible = "hid-over-i2c";
		reg = <0x5c>;
		hid-descr-addr = <0x1>;

		interrupts-extended = <&tlmm 92 IRQ_TYPE_LEVEL_LOW>;

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

&ipa {
	qcom,gsi-loader = "self";
	memory-region = <&ipa_fw_mem>;
	status = "okay";
};

&mdss {
	status = "okay";
};

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

	ports {
		port@1 {
			endpoint {
				remote-endpoint = <&sn65dsi86_in_a>;
				data-lanes = <0 1 2 3>;
			};
		};
	};
};

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

&mss_pil {
	status = "okay";
	firmware-name = "qcom/sdm850/LENOVO/81JL/qcdsp1v2850.mbn", "qcom/sdm850/LENOVO/81JL/qcdsp2850.mbn";
};

&pm8998_gpios {
	/* This pin is pulled down by a fixed resistor */
	sw_edp_1p2_en: pm8998-gpio9-state {
		pins = "gpio9";
		function = "normal";
		bias-disable;
		qcom,drive-strength = <0>;
	};
};

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

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

&qupv3_id_0 {
	status = "okay";
};

&qupv3_id_1 {
	status = "okay";
};

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

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

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

&sound {
	compatible = "lenovo,yoga-c630-sndcard", "qcom,sdm845-sndcard";
	model = "Lenovo-YOGA-C630-13Q50";

	audio-routing =
		"RX_BIAS", "MCLK",
		"AMIC2", "MIC BIAS2",
		"SpkrLeft IN", "SPK1 OUT",
		"SpkrRight IN", "SPK2 OUT",
		"MM_DL1",  "MultiMedia1 Playback",
		"MM_DL3",  "MultiMedia3 Playback",
		"MultiMedia2 Capture", "MM_UL2";

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

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

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

		codec {
			sound-dai = <&left_spkr>, <&right_spkr>, <&swm 0>, <&wcd9340 0>;
		};
	};

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

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

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

	slim-wcd-dai-link {
		link-name = "SLIM WCD Playback";
		cpu {
			sound-dai = <&q6afedai SLIMBUS_1_RX>;
		};

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

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

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

	sn65dsi86_pin_active: sn65dsi86-enable-state {
		pins = "gpio96";
		function = "gpio";
		drive-strength = <2>;
		bias-disable;
	};

	i2c3_hid_active: i2c2-hid-active-state {
		pins = "gpio37";
		function = "gpio";

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

	i2c5_hid_active: i2c5-hid-active-state {
		pins = "gpio125";
		function = "gpio";

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

	i2c11_hid_active: i2c11-hid-active-state {
		pins = "gpio92";
		function = "gpio";

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

	lid_pin_active: lid-pin-state {
		pins = "gpio124";
		function = "gpio";

		bias-disable;
	};

	mode_pin_active: mode-pin-state {
		pins = "gpio95";
		function = "gpio";

		bias-disable;
	};
};

&uart6 {
	pinctrl-names = "default";
	pinctrl-0 = <&qup_uart6_4pin>;
	status = "okay";

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

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

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

&usb_1_dwc3 {
	dr_mode = "host";
};

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

&usb_1_qmpphy {
	status = "okay";

	vdda-phy-supply = <&vdda_usb1_ss_1p2>;
	vdda-pll-supply = <&vdda_usb1_ss_core>;
};

&usb_2 {
	status = "okay";
};

&usb_2_dwc3 {
	dr_mode = "host";
};

&usb_2_hsphy {
	status = "okay";

	vdd-supply = <&vdda_usb2_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_22_8_MA>;
};

&usb_2_qmpphy {
	status = "okay";

	vdda-phy-supply = <&vdda_usb2_ss_1p2>;
	vdda-pll-supply = <&vdda_usb2_ss_core>;
};

&venus {
	firmware-name = "qcom/sdm850/LENOVO/81JL/qcvss850.mbn";
	status = "okay";
};

&wcd9340 {
	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>;
	qcom,mbhc-buttons-vthreshold-microvolt = <75000 150000 237000 500000 500000 500000 500000 500000>;
	qcom,mbhc-headset-vthreshold-microvolt = <1700000>;
	qcom,mbhc-headphone-vthreshold-microvolt = <50000>;

	swm: swm@c85 {
		left_spkr: speaker@0,3 {
			compatible = "sdw10217211000";
			reg = <0 3>;
			powerdown-gpios = <&wcdgpio 1 GPIO_ACTIVE_LOW>;
			#thermal-sensor-cells = <0>;
			sound-name-prefix = "SpkrLeft";
			#sound-dai-cells = <0>;
		};

		right_spkr: speaker@0,4 {
			compatible = "sdw10217211000";
			powerdown-gpios = <&wcdgpio 2 GPIO_ACTIVE_LOW>;
			reg = <0 4>;
			#thermal-sensor-cells = <0>;
			sound-name-prefix = "SpkrRight";
			#sound-dai-cells = <0>;
		};
	};
};

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

&crypto {
	/* FIXME: qce_start triggers an SError */
	status = "disabled";
};