// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
/*
 * Copyright (c) 2023, Linaro Ltd
 */

/dts-v1/;

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/leds/common.h>
#include "agatti.dtsi"
#include "pm4125.dtsi"

/ {
	model = "Qualcomm Technologies, Inc. Robotics RB1";
	compatible = "qcom,qrb2210-rb1", "qcom,qrb2210", "qcom,qcm2290";

	aliases {
		serial0 = &uart4;
		serial1 = &uart3;
		sdhc1 = &sdhc_1;
		sdhc2 = &sdhc_2;
	};

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

	clocks {
		clk40m: can-clk {
			compatible = "fixed-clock";
			clock-frequency = <40000000>;
			#clock-cells = <0>;
		};
	};

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

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

		key-volume-up {
			label = "Volume Up";
			linux,code = <KEY_VOLUMEUP>;
			gpios = <&tlmm 96 GPIO_ACTIVE_LOW>;
			debounce-interval = <15>;
			linux,can-disable;
			wakeup-source;
		};
	};

	hdmi-connector {
		compatible = "hdmi-connector";
		type = "a";

		port {
			hdmi_con: endpoint {
				remote-endpoint = <&lt9611_out>;
			};
		};
	};

	i2c2_gpio: i2c {
		compatible = "i2c-gpio";

		sda-gpios = <&tlmm 6 (GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN)>;
		scl-gpios = <&tlmm 7 (GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN)>;
		#address-cells = <1>;
		#size-cells = <0>;

		status = "disabled";
	};

	leds {
		compatible = "gpio-leds";

		led-bt {
			label = "blue:bt";
			function = LED_FUNCTION_BLUETOOTH;
			color = <LED_COLOR_ID_BLUE>;
			gpios = <&tlmm 45 GPIO_ACTIVE_HIGH>;
			linux,default-trigger = "bluetooth-power";
			default-state = "off";
		};

		led-user0 {
			label = "green:user0";
			function = LED_FUNCTION_INDICATOR;
			color = <LED_COLOR_ID_GREEN>;
			gpios = <&tlmm 52 GPIO_ACTIVE_HIGH>;
			linux,default-trigger = "none";
			default-state = "off";
			panic-indicator;
		};

		led-wlan {
			label = "yellow:wlan";
			function = LED_FUNCTION_WLAN;
			color = <LED_COLOR_ID_YELLOW>;
			gpios = <&tlmm 47 GPIO_ACTIVE_HIGH>;
			linux,default-trigger = "phy0tx";
			default-state = "off";
		};
	};

	vreg_hdmi_out_1p2: regulator-hdmi-out-1p2 {
		compatible = "regulator-fixed";
		regulator-name = "VREG_HDMI_OUT_1P2";
		regulator-min-microvolt = <1200000>;
		regulator-max-microvolt = <1200000>;
		regulator-always-on;
		regulator-boot-on;
	};

	lt9611_3v3: regulator-lt9611-3v3 {
		compatible = "regulator-fixed";
		regulator-name = "LT9611_3V3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-always-on;
		regulator-boot-on;
	};

	/* 5V supply stepped down from the barrel jack input */
	vdc_5v: regulator-vdc-5v {
		compatible = "regulator-fixed";
		regulator-name = "VDC_5V";

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

	/* PM2250 charger out, supplied by VBAT */
	vph_pwr: regulator-vph-pwr {
		compatible = "regulator-fixed";
		regulator-name = "vph_pwr";
		regulator-min-microvolt = <3700000>;
		regulator-max-microvolt = <3700000>;

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

	sound {
		compatible = "qcom,qrb2210-sndcard";
		pinctrl-0 = <&lpi_i2s2_active>;
		pinctrl-names = "default";
		model = "Qualcomm-RB1-WSA8815-Speaker-DMIC0";

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

		hdmi-i2s-dai-link {
			link-name = "HDMI/I2S Playback";

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

			cpu {
				sound-dai = <&q6afedai SECONDARY_MI2S_RX>;
			};

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

	wcn3950-pmu {
		compatible = "qcom,wcn3950-pmu";

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

		vddio-supply = <&pm4125_l15>;
		vddxo-supply = <&pm4125_l13>;
		vddrf-supply = <&pm4125_l10>;
		vddch0-supply = <&pm4125_l22>;

		swctrl-gpios = <&tlmm 87 GPIO_ACTIVE_HIGH>;

		regulators {
			vreg_pmu_io: ldo0 {
				regulator-name = "vreg_pmu_io";
			};

			vreg_pmu_xo: ldo1 {
				regulator-name = "vreg_pmu_xo";
			};

			vreg_pmu_rf: ldo2 {
				regulator-name = "vreg_pmu_rf";
			};

			vreg_pmu_ch0: ldo3 {
				regulator-name = "vreg_pmu_ch0";
			};

			vreg_pmu_ch1: ldo4 {
				regulator-name = "vreg_pmu_ch1";
			};
		};
	};
};

&cpu_pd0 {
	/delete-property/ power-domains;
};

&cpu_pd1 {
	/delete-property/ power-domains;
};

&cpu_pd2 {
	/delete-property/ power-domains;
};

&cpu_pd3 {
	/delete-property/ power-domains;
};

/delete-node/ &cluster_pd;

&gpi_dma0 {
	status = "okay";
};

&gpu {
	status = "okay";
};

&gpu_zap_shader {
	firmware-name = "qcom/qcm2290/a702_zap.mbn";
};

&i2c1 {
	clock-frequency = <400000>;

	status = "okay";

	pm8008: pmic@8 {
		compatible = "qcom,pm8008";
		reg = <0x8>;

		interrupts-extended = <&tlmm 25 IRQ_TYPE_EDGE_RISING>;
		reset-gpios = <&tlmm 26 GPIO_ACTIVE_LOW>;

		vdd-l1-l2-supply = <&pm4125_s3>;
		vdd-l3-l4-supply = <&vph_pwr>;
		vdd-l5-supply = <&vph_pwr>;
		vdd-l6-supply = <&vph_pwr>;
		vdd-l7-supply = <&vph_pwr>;

		gpio-controller;
		#gpio-cells = <2>;
		gpio-ranges = <&pm8008 0 0 2>;

		interrupt-controller;
		#interrupt-cells = <2>;

		#thermal-sensor-cells = <0>;

		status = "disabled";

		regulators {
			vreg_l1p: ldo1 {
				regulator-name = "vreg_l1p";
				regulator-min-microvolt = <528000>;
				regulator-max-microvolt = <1200000>;
			};

			vreg_l2p: ldo2 {
				regulator-name = "vreg_l2p";
				regulator-min-microvolt = <528000>;
				regulator-max-microvolt = <1200000>;
			};

			vreg_l3p: ldo3 {
				regulator-name = "vreg_l3p";
				regulator-min-microvolt = <1500000>;
				regulator-max-microvolt = <3400000>;
			};

			vreg_l4p: ldo4 {
				regulator-name = "vreg_l4p";
				regulator-min-microvolt = <1500000>;
				regulator-max-microvolt = <3404000>;
			};

			vreg_l5p: ldo5 {
				regulator-name = "vreg_l5p";
				regulator-min-microvolt = <1500000>;
				regulator-max-microvolt = <3400000>;
			};

			vreg_l6p: ldo6 {
				regulator-name = "vreg_l6p";
				regulator-min-microvolt = <1500000>;
				regulator-max-microvolt = <3400000>;
			};

			vreg_l7p: ldo7 {
				regulator-name = "vreg_l7p";
				regulator-min-microvolt = <1500000>;
				regulator-max-microvolt = <3400000>;
			};
		};
	};
};

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

	lt9611_codec: hdmi-bridge@2b {
		compatible = "lontium,lt9611uxc";
		reg = <0x2b>;
		interrupts-extended = <&tlmm 46 IRQ_TYPE_EDGE_FALLING>;
		reset-gpios = <&tlmm 41 GPIO_ACTIVE_HIGH>;

		vdd-supply = <&vreg_hdmi_out_1p2>;
		vcc-supply = <&lt9611_3v3>;

		pinctrl-0 = <&lt9611_irq_pin &lt9611_rst_pin>;
		pinctrl-names = "default";
		#sound-dai-cells = <1>;

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

			port@0 {
				reg = <0>;

				lt9611_a: endpoint {
					remote-endpoint = <&mdss_dsi0_out>;
				};
			};

			port@2 {
				reg = <2>;

				lt9611_out: endpoint {
					remote-endpoint = <&hdmi_con>;
				};
			};
		};
	};
};

&mdss {
	status = "okay";
};

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

&mdss_dsi0_out {
	remote-endpoint = <&lt9611_a>;
	data-lanes = <0 1 2 3>;
};

&mdss_dsi0_phy {
	status = "okay";
};

&pm4125_resin {
	linux,code = <KEY_VOLUMEDOWN>;
	status = "okay";
};

&pm4125_typec {
	status = "okay";

	connector {
		compatible = "usb-c-connector";

		power-role = "dual";
		data-role = "dual";
		self-powered;

		typec-power-opmode = "default";
		pd-disable;

		vbus-supply = <&pm4125_vbus>;

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

			port@0 {
				reg = <0>;
				pm4125_hs_in: endpoint {
					remote-endpoint = <&usb_dwc3_hs>;
				};
			};

			port@1 {
				reg = <1>;
				pm4125_ss_in: endpoint {
					remote-endpoint = <&usb_qmpphy_out>;
				};
			};
		};
	};
};

&pm4125_vbus {
	regulator-min-microvolt = <5000000>;
	regulator-max-microvolt = <5000000>;
	status = "okay";
};

/* SECONDARY I2S uses 1 I2S SD Line for audio on LT9611UXC HDMI Bridge */
&q6afedai {
	dai@18 {
		reg = <SECONDARY_MI2S_RX>;
		qcom,sd-lines = <0>;
	};
};

&qupv3_id_0 {
	status = "okay";
};

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

&remoteproc_mpss {
	firmware-name = "qcom/qcm2290/modem.mbn";
	status = "okay";
};

&rpm_requests {
	regulators {
		compatible = "qcom,rpm-pm2250-regulators";
		vdd_s3-supply = <&vph_pwr>;
		vdd_s4-supply = <&vph_pwr>;
		vdd_l1_l2_l3_l5_l6_l7_l8_l9_l10_l11_l12-supply = <&pm4125_s3>;
		vdd_l4_l17_l18_l19_l20_l21_l22-supply = <&vph_pwr>;
		vdd_l13_l14_l15_l16-supply = <&pm4125_s4>;

		/*
		 * S1 - VDD_APC
		 * S2 - VDD_CX
		 */

		pm4125_s3: s3 {
			/* 0.4V-1.6625V -> 1.3V (Power tree requirements) */
			regulator-min-microvolt = <1352000>;
			regulator-max-microvolt = <1352000>;
			regulator-boot-on;
		};

		pm4125_s4: s4 {
			/* 1.2V-2.35V -> 2.05V (Power tree requirements) */
			regulator-min-microvolt = <2072000>;
			regulator-max-microvolt = <2072000>;
			regulator-boot-on;
		};

		/* L1 - VDD_MX */

		pm4125_l2: l2 {
			/* LPDDR4X VDD2 */
			regulator-min-microvolt = <1136000>;
			regulator-max-microvolt = <1136000>;
			regulator-always-on;
			regulator-boot-on;
		};

		pm4125_l3: l3 {
			/* LPDDR4X VDDQ */
			regulator-min-microvolt = <616000>;
			regulator-max-microvolt = <616000>;
			regulator-always-on;
			regulator-boot-on;
		};

		pm4125_l4: l4 {
			/* max = 3.05V -> max = 2.7 to disable 3V signaling (SDHCI2) */
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <2700000>;
			regulator-allow-set-load;
		};

		pm4125_l5: l5 {
			/* CSI/DSI */
			regulator-min-microvolt = <1232000>;
			regulator-max-microvolt = <1232000>;
			regulator-allow-set-load;
			regulator-boot-on;
		};

		pm4125_l6: l6 {
			/* DRAM PLL */
			regulator-min-microvolt = <928000>;
			regulator-max-microvolt = <928000>;
			regulator-always-on;
			regulator-boot-on;
		};

		pm4125_l7: l7 {
			/* Wi-Fi CX/MX */
			regulator-min-microvolt = <664000>;
			regulator-max-microvolt = <664000>;
		};

		/*
		 * L8 - VDD_LPI_CX
		 * L9 - VDD_LPI_MX
		 */

		pm4125_l10: l10 {
			/* Wi-Fi RFA */
			regulator-min-microvolt = <1304000>;
			regulator-max-microvolt = <1304000>;
		};

		pm4125_l11: l11 {
			/* GPS RF1 */
			regulator-min-microvolt = <1000000>;
			regulator-max-microvolt = <1000000>;
			regulator-boot-on;
		};

		pm4125_l12: l12 {
			/* USB PHYs */
			regulator-min-microvolt = <928000>;
			regulator-max-microvolt = <928000>;
			regulator-allow-set-load;
			regulator-boot-on;
		};

		pm4125_l13: l13 {
			/* USB/QFPROM/PLLs */
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-allow-set-load;
			regulator-boot-on;
		};

		pm4125_l14: l14 {
			/* SDHCI1 VQMMC */
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-allow-set-load;
			/* Broken hardware, never turn it off! */
			regulator-always-on;
		};

		pm4125_l15: l15 {
			/* WCD/DSI/BT VDDIO */
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-allow-set-load;
			regulator-always-on;
			regulator-boot-on;
		};

		pm4125_l16: l16 {
			/* GPS RF2 */
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			regulator-boot-on;
		};

		pm4125_l17: l17 {
			regulator-min-microvolt = <3000000>;
			regulator-max-microvolt = <3000000>;
		};

		pm4125_l18: l18 {
			/* VDD_PXn */
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
		};

		pm4125_l19: l19 {
			/* VDD_PXn */
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
		};

		pm4125_l20: l20 {
			/* SDHCI1 VMMC */
			regulator-min-microvolt = <2400000>;
			regulator-max-microvolt = <3600000>;
			regulator-allow-set-load;
		};

		pm4125_l21: l21 {
			/* SDHCI2 VMMC */
			regulator-min-microvolt = <2960000>;
			regulator-max-microvolt = <3300000>;
			regulator-allow-set-load;
			regulator-boot-on;
		};

		pm4125_l22: l22 {
			/* Wi-Fi */
			regulator-min-microvolt = <3312000>;
			regulator-max-microvolt = <3312000>;
		};
	};
};

&sdhc_1 {
	vmmc-supply = <&pm4125_l20>;
	vqmmc-supply = <&pm4125_l14>;
	pinctrl-0 = <&sdc1_state_on>;
	pinctrl-1 = <&sdc1_state_off>;
	pinctrl-names = "default", "sleep";
	non-removable;
	supports-cqe;
	no-sdio;
	no-sd;
	status = "okay";
};

&sdhc_2 {
	vmmc-supply = <&pm4125_l21>;
	vqmmc-supply = <&pm4125_l4>;
	cd-gpios = <&tlmm 88 GPIO_ACTIVE_LOW>;
	pinctrl-0 = <&sdc2_state_on &sd_det_in_on>;
	pinctrl-1 = <&sdc2_state_off &sd_det_in_off>;
	pinctrl-names = "default", "sleep";
	no-sdio;
	no-mmc;
	status = "okay";
};

&spi5 {
	status = "okay";

	can@0 {
		compatible = "microchip,mcp2518fd";
		reg = <0>;
		interrupts-extended = <&tlmm 39 IRQ_TYPE_LEVEL_LOW>;
		clocks = <&clk40m>;
		spi-max-frequency = <10000000>;
		vdd-supply = <&vdc_5v>;
		xceiver-supply = <&vdc_5v>;
	};
};

&tlmm {
	uart3_default: uart3-default-state {
		cts-pins {
			pins = "gpio8";
			function = "qup3";
			drive-strength = <2>;
			bias-bus-hold;
		};

		rts-pins {
			pins = "gpio9";
			function = "qup3";
			drive-strength = <2>;
			bias-disable;
		};

		tx-pins {
			pins = "gpio10";
			function = "qup3";
			drive-strength = <2>;
			bias-disable;
		};

		rx-pins {
			pins = "gpio11";
			function = "qup3";
			drive-strength = <2>;
			bias-pull-up;
		};
	};

	uart3_sleep: uart3-sleep-state {
		cts-pins {
			pins = "gpio8";
			function = "gpio";
			drive-strength = <2>;
			bias-bus-hold;
		};

		rts-pins {
			pins = "gpio9";
			function = "gpio";
			drive-strength = <2>;
			bias-pull-down;
		};

		tx-pins {
			pins = "gpio10";
			function = "gpio";
			drive-strength = <2>;
			bias-pull-up;
		};

		rx-pins {
			pins = "gpio11";
			function = "gpio";
			drive-strength = <2>;
			bias-pull-up;
		};
	};

	lt9611_rst_pin: lt9611-rst-state {
		pins = "gpio41";
		function = "gpio";
		input-disable;
		output-high;
	};

	lt9611_irq_pin: lt9611-irq-state {
		pins = "gpio46";
		function = "gpio";
		bias-disable;
	};

	sw_ctrl_default: sw-ctrl-default-state {
		pins = "gpio87";
		function = "gpio";
		bias-pull-down;
	};

	sd_det_in_on: sd-det-in-on-state {
		pins = "gpio88";
		function = "gpio";
		drive-strength = <2>;
		bias-pull-up;
	};

	sd_det_in_off: sd-det-in-off-state {
		pins = "gpio88";
		function = "gpio";
		drive-strength = <2>;
		bias-disable;
	};

	key_volp_n: key-volp-n-state {
		pins = "gpio96";
		function = "gpio";
		bias-pull-up;
		output-disable;
	};
};

&uart3 {
	/delete-property/ interrupts;
	interrupts-extended = <&intc GIC_SPI 330 IRQ_TYPE_LEVEL_HIGH>,
			      <&tlmm 11 IRQ_TYPE_EDGE_FALLING>;
	pinctrl-0 = <&uart3_default>;
	pinctrl-1 = <&uart3_sleep>;
	pinctrl-names = "default", "sleep";

	status = "okay";

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

		vddio-supply = <&vreg_pmu_io>;
		vddxo-supply = <&vreg_pmu_xo>;
		vddrf-supply = <&vreg_pmu_rf>;
		vddch0-supply = <&vreg_pmu_ch0>;
		max-speed = <3200000>;
	};
};

/* UART connected to the Micro-USB port via a FTDI chip */
&uart4 {
	compatible = "qcom,geni-debug-uart";
	status = "okay";
};

&usb {
	status = "okay";
};

&usb_dwc3_hs {
	remote-endpoint = <&pm4125_hs_in>;
};

&usb_hsphy {
	vdd-supply = <&pm4125_l12>;
	vdda-pll-supply = <&pm4125_l13>;
	vdda-phy-dpdm-supply = <&pm4125_l21>;
	status = "okay";
};

&usb_qmpphy {
	vdda-phy-supply = <&pm4125_l12>;
	vdda-pll-supply = <&pm4125_l13>;
	status = "okay";
};

&usb_qmpphy_out {
	remote-endpoint = <&pm4125_ss_in>;
};

&venus {
	status = "okay";
};

&wifi {
	/* SoC */
	vdd-0.8-cx-mx-supply = <&pm4125_l7>;

	/* WiFi / BT PMU */
	vdd-1.8-xo-supply = <&vreg_pmu_xo>;
	vdd-1.3-rfa-supply = <&vreg_pmu_rf>;
	vdd-3.3-ch0-supply = <&vreg_pmu_ch0>;
	qcom,calibration-variant = "Thundercomm_RB1";
	firmware-name = "qcm2290";
	status = "okay";
};

&xo_board {
	clock-frequency = <38400000>;
};