// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright 2024 Variscite Ltd.
 */

#include "imx8mp-var-som.dtsi"

/ {
	model = "Variscite VAR-SOM-MX8M-PLUS on Symphony-Board";
	compatible = "variscite,var-som-mx8mp-symphony", "variscite,var-som-mx8mp", "fsl,imx8mp";

	chosen {
		stdout-path = &uart2;
	};

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

		button-back {
			label = "Back";
			linux,code = <KEY_BACK>;
			gpios = <&pca9534 1 GPIO_ACTIVE_LOW>;
			wakeup-source;
		};

		button-home {
			label = "Home";
			linux,code = <KEY_HOME>;
			gpios = <&pca9534 2 GPIO_ACTIVE_LOW>;
			wakeup-source;
		};

		button-menu {
			label = "Menu";
			linux,code = <KEY_MENU>;
			gpios = <&pca9534 3 GPIO_ACTIVE_LOW>;
			wakeup-source;
		};
	};

	gpio-leds {
		compatible = "gpio-leds";

		led-0 {
			function = LED_FUNCTION_POWER;
			gpios = <&pca9534 0 GPIO_ACTIVE_HIGH>;
			linux,default-trigger = "heartbeat";
		};
	};

	native-hdmi-connector {
		compatible = "hdmi-connector";
		label = "HDMI OUT";
		type = "a";

		port {
			hdmi_in: endpoint {
				remote-endpoint = <&hdmi_tx_out>;
			};
		};
	};

	pcie0_refclk: pcie0-refclk {
		compatible = "fixed-clock";
		#clock-cells = <0>;
		clock-frequency = <100000000>;
	};

	reg_fec_phy: regulator-fec-phy {
		compatible = "regulator-fixed";
		regulator-name = "fec-phy";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		regulator-enable-ramp-delay = <20000>;
		gpio = <&pca9534 7 GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};

	reg_usdhc2_vmmc: regulator-usdhc2-vmmc {
		compatible = "regulator-fixed";
		regulator-name = "VSD_3V3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		gpios = <&gpio4 22 GPIO_ACTIVE_HIGH>;
		enable-active-high;
		startup-delay-us = <100>;
		off-on-delay-us = <12000>;
	};

	reg_usdhc2_vqmmc: regulator-usdhc2-vqmmc {
		compatible = "regulator-gpio";
		regulator-name = "VSD_VSEL";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <3300000>;
		gpios = <&gpio2 12 GPIO_ACTIVE_HIGH>;
		states = <3300000 0x0 1800000 0x1>;
		vin-supply = <&ldo5>;
	};

	sound-hdmi {
		compatible = "fsl,imx-audio-hdmi";
		model = "audio-hdmi";
		audio-cpu = <&aud2htx>;
		hdmi-out;
	};
};

&aud2htx {
	status = "okay";
};

&ecspi2 {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_ecspi2>;
	cs-gpios = <&gpio5 13 GPIO_ACTIVE_LOW>;
	status = "okay";
};

&eqos {
	mdio {
		ethphy1: ethernet-phy@5 {
			compatible = "ethernet-phy-ieee802.3-c22";
			reg = <5>;
			reset-gpios = <&pca9534 5 GPIO_ACTIVE_LOW>;
			reset-assert-us = <10000>;
			reset-deassert-us = <20000>;
			vddio-supply = <&reg_phy_vddio>;
		};
	};
};

&fec {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_fec>;
	/*
	 * The required RGMII TX and RX 2ns delays are implemented directly
	 * in hardware via passive delay elements on the SOM PCB.
	 * No delay configuration is needed in software via PHY driver.
	 */
	phy-mode = "rgmii";
	phy-handle = <&ethphy1>;
	phy-supply = <&reg_fec_phy>;
	status = "okay";
};

&flexcan2 {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_flexcan2>;
	status = "okay";
};

&hdmi_pai {
	status = "okay";
};

&hdmi_pvi {
	status = "okay";
};

&hdmi_tx {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_hdmi>;
	status = "okay";

	ports {
		port@1 {
			hdmi_tx_out: endpoint {
				remote-endpoint = <&hdmi_in>;
			};
		};
	};
};

&hdmi_tx_phy {
	status = "okay";
};

&i2c3 {
	clock-frequency = <400000>;
	pinctrl-names = "default", "gpio";
	pinctrl-0 = <&pinctrl_i2c3>;
	pinctrl-1 = <&pinctrl_i2c3_gpio>;
	scl-gpios = <&gpio5 18 (GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN)>;
	sda-gpios = <&gpio5 19 (GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN)>;
	status = "okay";

	/* GPIO expander */
	pca9534: gpio@20 {
		compatible = "nxp,pca9534";
		reg = <0x20>;
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_pca9534>;
		gpio-controller;
		#gpio-cells = <2>;
		interrupt-parent = <&gpio1>;
		interrupts = <15 IRQ_TYPE_EDGE_FALLING>;
		wakeup-source;

		usb3-sata-sel-hog {
			gpio-hog;
			gpios = <4 0>;
			output-low;
			line-name = "usb3_sata_sel";
		};
	};

	/* USB Type-C Controller */
	typec@3d {
		compatible = "nxp,ptn5150";
		reg = <0x3d>;
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_ptn5150>;
		interrupt-parent = <&gpio1>;
		interrupts = <5 IRQ_TYPE_EDGE_FALLING>;

		port {
			typec_dr_sw: endpoint {
				remote-endpoint = <&usb3_drd_sw>;
			};
		};
	};
};

&i2c4 {
	clock-frequency = <400000>;
	pinctrl-names = "default", "gpio", "sleep";
	pinctrl-0 = <&pinctrl_i2c4>;
	pinctrl-1 = <&pinctrl_i2c4_gpio>;
	pinctrl-2 = <&pinctrl_i2c4_gpio>;
	scl-gpios = <&gpio5 20 (GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN)>;
	sda-gpios = <&gpio5 21 (GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN)>;
	status = "okay";

	pcal6408: gpio@21 {
		compatible = "nxp,pcal6408";
		reg = <0x21>;
		#gpio-cells = <2>;
		gpio-controller;

		/* RGB_SEL */
		lvds-brg-enable-hog {
			gpio-hog;
			gpios = <7 GPIO_ACTIVE_HIGH>;
			output-low;
			line-name = "rgb_sel";
		};
	};

	st33ktpm2xi2c: tpm@2e {
		compatible = "st,st33ktpm2xi2c", "tcg,tpm-tis-i2c";
		reg = <0x2e>;
		label = "tpm";
		reset-gpios = <&pcal6408 4 GPIO_ACTIVE_LOW>;
	};

	/* Capacitive touch controller */
	ft5x06_ts: touchscreen@38 {
		compatible = "edt,edt-ft5206";
		reg = <0x38>;
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_captouch>;
		interrupt-parent = <&gpio5>;
		interrupts = <3 IRQ_TYPE_EDGE_FALLING>;
		touchscreen-size-x = <800>;
		touchscreen-size-y = <480>;
		touchscreen-inverted-x;
		touchscreen-inverted-y;
		wakeup-source;
	};

	rtc@68 {
		compatible = "dallas,ds1337";
		reg = <0x68>;
	};
};

&lcdif3 {
	status = "okay";
};

&pcie {
	reset-gpios = <&pcal6408 1 GPIO_ACTIVE_LOW>;
	status = "okay";
};

&pcie_phy {
	clocks = <&pcie0_refclk>;
	clock-names = "ref";
	fsl,refclk-pad-mode = <IMX8_PCIE_REFCLK_PAD_INPUT>;
	status = "okay";
};

&pwm1 {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_pwm1>;
	status = "okay";
};

&snvs_pwrkey {
	status = "okay";
};

&snvs_rtc {
	status = "disabled";
};

/* Header UART */
&uart1 {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_uart1>;
	status = "okay";
};

/* Console */
&uart2 {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_uart2>;
	status = "okay";
};

/* Header UART */
&uart4 {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_uart4>;
	status = "okay";
};

&usb3_0 {
	status = "okay";
};

&usb3_1 {
	status = "okay";
};

&usb_dwc3_0 {
	adp-disable;
	dr_mode = "otg";
	hnp-disable;
	snps,dis-u1-entry-quirk;
	snps,dis-u2-entry-quirk;
	srp-disable;
	usb-role-switch;
	status = "okay";

	port {
		usb3_drd_sw: endpoint {
			remote-endpoint = <&typec_dr_sw>;
		};
	};
};

&usb_dwc3_1 {
	dr_mode = "host";
	status = "okay";
};

&usb3_phy0 {
	fsl,phy-comp-dis-tune-percent = <115>;
	fsl,phy-pcs-tx-deemph-3p5db-attenuation-db = <33>;
	fsl,phy-pcs-tx-swing-full-percent = <100>;
	fsl,phy-tx-preemp-amp-tune-microamp = <1800>;
	fsl,phy-tx-vboost-level-microvolt = <1156>;
	fsl,phy-tx-vref-tune-percent = <122>;
	status = "okay";
};

&usb3_phy1 {
	fsl,phy-tx-preemp-amp-tune-microamp = <1800>;
	fsl,phy-tx-vref-tune-percent = <116>;
	status = "okay";
};

/* SD-card */
&usdhc2 {
	pinctrl-names = "default", "state_100mhz", "state_200mhz";
	pinctrl-0 = <&pinctrl_usdhc2>, <&pinctrl_usdhc2_gpio>;
	pinctrl-1 = <&pinctrl_usdhc2_100mhz>, <&pinctrl_usdhc2_gpio>;
	pinctrl-2 = <&pinctrl_usdhc2_200mhz>, <&pinctrl_usdhc2_gpio>;
	cd-gpios = <&gpio1 14 GPIO_ACTIVE_LOW>;
	vmmc-supply = <&reg_usdhc2_vmmc>;
	vqmmc-supply = <&reg_usdhc2_vqmmc>;
	bus-width = <4>;
	status = "okay";
};

&iomuxc {
	pinctrl_captouch: captouchgrp {
		fsl,pins = <
			MX8MP_IOMUXC_SPDIF_TX__GPIO5_IO03				0x16
		>;
	};

	pinctrl_ecspi2: ecspi2grp {
		fsl,pins = <
			MX8MP_IOMUXC_ECSPI2_SCLK__ECSPI2_SCLK				0x12
			MX8MP_IOMUXC_ECSPI2_MOSI__ECSPI2_MOSI				0x12
			MX8MP_IOMUXC_ECSPI2_MISO__ECSPI2_MISO				0x12
			MX8MP_IOMUXC_ECSPI2_SS0__GPIO5_IO13				0x12
		>;
	};

	pinctrl_fec: fecgrp {
		fsl,pins = <
			MX8MP_IOMUXC_SAI1_RXD4__ENET1_RGMII_RD0				0x90
			MX8MP_IOMUXC_SAI1_RXD5__ENET1_RGMII_RD1				0x90
			MX8MP_IOMUXC_SAI1_RXD6__ENET1_RGMII_RD2				0x90
			MX8MP_IOMUXC_SAI1_RXD7__ENET1_RGMII_RD3				0x90
			MX8MP_IOMUXC_SAI1_TXC__ENET1_RGMII_RXC				0x90
			MX8MP_IOMUXC_SAI1_TXFS__ENET1_RGMII_RX_CTL			0x90
			MX8MP_IOMUXC_SAI1_TXD0__ENET1_RGMII_TD0				0x00
			MX8MP_IOMUXC_SAI1_TXD1__ENET1_RGMII_TD1				0x00
			MX8MP_IOMUXC_SAI1_TXD2__ENET1_RGMII_TD2				0x00
			MX8MP_IOMUXC_SAI1_TXD3__ENET1_RGMII_TD3				0x00
			MX8MP_IOMUXC_SAI1_TXD4__ENET1_RGMII_TX_CTL			0x00
			MX8MP_IOMUXC_SAI1_TXD5__ENET1_RGMII_TXC				0x00
		>;
	};

	pinctrl_flexcan2: flexcan2grp {
		fsl,pins = <
			MX8MP_IOMUXC_UART3_RXD__CAN2_TX					0x154
			MX8MP_IOMUXC_UART3_TXD__CAN2_RX					0x154
		>;
	};

	pinctrl_hdmi: hdmigrp {
		fsl,pins = <
			MX8MP_IOMUXC_HDMI_DDC_SCL__HDMIMIX_HDMI_SCL			0x1c2
			MX8MP_IOMUXC_HDMI_DDC_SDA__HDMIMIX_HDMI_SDA			0x1c2
			MX8MP_IOMUXC_HDMI_CEC__HDMIMIX_HDMI_CEC				0x10
			MX8MP_IOMUXC_HDMI_HPD__HDMIMIX_HDMI_HPD				0x10
		>;
	};

	pinctrl_i2c3: i2c3grp {
		fsl,pins = <
			MX8MP_IOMUXC_I2C3_SCL__I2C3_SCL                                 0x400001c2
			MX8MP_IOMUXC_I2C3_SDA__I2C3_SDA                                 0x400001c2
		>;
	};

	pinctrl_i2c3_gpio: i2c3gpiogrp {
		fsl,pins = <
			MX8MP_IOMUXC_I2C3_SCL__GPIO5_IO18				0x400001c2
			MX8MP_IOMUXC_I2C3_SDA__GPIO5_IO19				0x400001c2
		>;
	};

	pinctrl_i2c4: i2c4grp {
		fsl,pins = <
			MX8MP_IOMUXC_I2C4_SCL__I2C4_SCL					0x400001c2
			MX8MP_IOMUXC_I2C4_SDA__I2C4_SDA					0x400001c2
		>;
	};

	pinctrl_i2c4_gpio: i2c4gpiogrp {
		fsl,pins = <
			MX8MP_IOMUXC_I2C4_SCL__GPIO5_IO20				0x400001c2
			MX8MP_IOMUXC_I2C4_SDA__GPIO5_IO21				0x400001c2
		>;
	};

	pinctrl_pca9534: pca9534grp {
		fsl,pins = <
			MX8MP_IOMUXC_GPIO1_IO15__GPIO1_IO15                             0xc0
		>;
	};

	pinctrl_ptn5150: ptn5150grp {
		fsl,pins = <
			MX8MP_IOMUXC_GPIO1_IO05__GPIO1_IO05				0x10
		>;
	};

	pinctrl_pwm1: pwm1grp {
		fsl,pins = <
			MX8MP_IOMUXC_SPDIF_EXT_CLK__PWM1_OUT				0x116
		>;
	};

	pinctrl_uart1: uart1grp {
		fsl,pins = <
			MX8MP_IOMUXC_UART1_RXD__UART1_DCE_RX				0x40
			MX8MP_IOMUXC_UART1_TXD__UART1_DCE_TX				0x40
		>;
	};

	pinctrl_uart2: uart2grp {
		fsl,pins = <
			MX8MP_IOMUXC_UART2_RXD__UART2_DCE_RX                            0x40
			MX8MP_IOMUXC_UART2_TXD__UART2_DCE_TX                            0x40
		>;
	};

	pinctrl_uart4: uart4grp {
		fsl,pins = <
			MX8MP_IOMUXC_UART4_RXD__UART4_DCE_RX				0x40
			MX8MP_IOMUXC_UART4_TXD__UART4_DCE_TX				0x40
		>;
	};

	pinctrl_usdhc2_gpio: usdhc2-gpiogrp {
		fsl,pins = <
			MX8MP_IOMUXC_GPIO1_IO14__GPIO1_IO14                             0x1c4
			MX8MP_IOMUXC_SAI2_RXC__GPIO4_IO22                               0x10
			MX8MP_IOMUXC_SD2_CD_B__GPIO2_IO12                               0xc0
		>;
	};

	pinctrl_usdhc2: usdhc2grp {
		fsl,pins = <
			MX8MP_IOMUXC_SD2_CLK__USDHC2_CLK                                0x190
			MX8MP_IOMUXC_SD2_CMD__USDHC2_CMD                                0x1d0
			MX8MP_IOMUXC_SD2_DATA0__USDHC2_DATA0                            0x1d0
			MX8MP_IOMUXC_SD2_DATA1__USDHC2_DATA1                            0x1d0
			MX8MP_IOMUXC_SD2_DATA2__USDHC2_DATA2                            0x1d0
			MX8MP_IOMUXC_SD2_DATA3__USDHC2_DATA3                            0x1d0
		>;
	};

	pinctrl_usdhc2_100mhz: usdhc2-100mhzgrp {
		fsl,pins = <
			MX8MP_IOMUXC_SD2_CLK__USDHC2_CLK                                0x194
			MX8MP_IOMUXC_SD2_CMD__USDHC2_CMD                                0x1d4
			MX8MP_IOMUXC_SD2_DATA0__USDHC2_DATA0                            0x1d4
			MX8MP_IOMUXC_SD2_DATA1__USDHC2_DATA1                            0x1d4
			MX8MP_IOMUXC_SD2_DATA2__USDHC2_DATA2                            0x1d4
			MX8MP_IOMUXC_SD2_DATA3__USDHC2_DATA3                            0x1d4
		>;
	};

	pinctrl_usdhc2_200mhz: usdhc2-200mhzgrp {
		fsl,pins = <
			MX8MP_IOMUXC_SD2_CLK__USDHC2_CLK                                0x196
			MX8MP_IOMUXC_SD2_CMD__USDHC2_CMD                                0x1d6
			MX8MP_IOMUXC_SD2_DATA0__USDHC2_DATA0                            0x1d6
			MX8MP_IOMUXC_SD2_DATA1__USDHC2_DATA1                            0x1d6
			MX8MP_IOMUXC_SD2_DATA2__USDHC2_DATA2                            0x1d6
			MX8MP_IOMUXC_SD2_DATA3__USDHC2_DATA3                            0x1d6
		>;
	};
};