// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/*
 * Copyright 2025 Josua Mayer <josua@solid-run.com>
 */

#include <dt-bindings/input/input.h>
#include <dt-bindings/leds/common.h>
/ {
	/* power for M.2 B-Key connector (J6) */
	regulator-m2-b {
		compatible = "regulator-fixed";
		regulator-name = "m2-b";
		gpios = <&tca6416_u20 5 GPIO_ACTIVE_HIGH>;
		regulator-always-on;
		regulator-max-microvolt = <3300000>;
		regulator-min-microvolt = <3300000>;
		enable-active-high;
	};

	/* power for M.2 M-Key connector (J4) */
	regulator-m2-m {
		compatible = "regulator-fixed";
		regulator-name = "m2-m";
		gpios = <&tca6416_u20 6 GPIO_ACTIVE_HIGH>;
		regulator-always-on;
		regulator-max-microvolt = <3300000>;
		regulator-min-microvolt = <3300000>;
		enable-active-high;
	};

	/* power for USB-A J27 behind USB Hub Port 3 */
	regulator-vbus-2 {
		compatible = "regulator-fixed";
		regulator-name = "vbus2";
		regulator-always-on;
		regulator-max-microvolt = <5000000>;
		regulator-min-microvolt = <5000000>;
		gpios = <&tca6416_u20 12 GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};

	/* power for USB-A J27 behind USB Hub Port 4 */
	regulator-vbus-3 {
		compatible = "regulator-fixed";
		regulator-name = "vbus3";
		regulator-always-on;
		regulator-max-microvolt = <5000000>;
		regulator-min-microvolt = <5000000>;
		gpios = <&tca6416_u20 13 GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};

	aliases {
		i2c3 = &i2c_exp;
		i2c4 = &i2c_csi;
		i2c5 = &i2c_dsi;
		i2c6 = &i2c_lvds;
		rtc0 = &carrier_rtc;
		rtc1 = &pmic;
		serial3 = &scif3;
	};

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

		wakeup-event {
			interrupts-extended = <&tca6416_u21 11 IRQ_TYPE_EDGE_FALLING>;
			label = "m2-m-wakeup";
			wakeup-source;
			linux,code = <KEY_WAKEUP>;
		};
	};

	can_mux: mux-controller-1 {
		compatible = "gpio-mux";
		/* default J9-55/57/59/61 to on-board transceivers */
		idle-state = <0>;
		#mux-control-cells = <0>;
		/*
		 * Mux routes CAN bus signals between SoM connector pins,
		 * expansion connector (J22) and on-board transceivers using
		 * two GPIO:
		 * - IO3: 0 = on-board transceivers, 1 = expansion connector
		 * - IO4: 0 = J9-55/57/59/61, 1 = J7-12/16 & J9-54/56
		 */
		mux-gpios = <&tca6416_u20 3 GPIO_ACTIVE_HIGH>,
			    <&tca6416_u20 4 GPIO_ACTIVE_HIGH>;
	};

	spi_mux: mux-controller-2 {
		compatible = "gpio-mux";
		/* default on-board */
		idle-state = <0>;
		/*
		 * Mux switches spi bus between on-board tpm
		 * and expansion connector (J22).
		 */
		mux-gpios = <&tca6416_u21 0 GPIO_ACTIVE_HIGH>;
		#mux-control-cells = <0>;
	};

	scif1_scif3_b2b_mux: mux-controller-3 {
		compatible = "gpio-mux";
		/* default on-board */
		idle-state = <0>;
		#mux-control-cells = <0>;
		/*
		 * Mux switches both scif1 and scif3 tx/rx between expansion
		 * connector (J22) and on-board rs232/rs485 transceivers
		 * using one GPIO: 0 = on-board, 1 = connector.
		 */
		mux-gpios = <&tca6416_u20 0 GPIO_ACTIVE_HIGH>;
	};

	scif1_rs_232_485_mux: mux-controller-4 {
		compatible = "gpio-mux";
		/* default rs232 */
		idle-state = <0>;
		#mux-control-cells = <0>;
		/*
		 * Mux switches scif1 tx/rx between rs232 and rs485
		 * transceivers. using one GPIO: 0 = rs232, 1 = rs485.
		 */
		mux-gpios = <&tca6416_u20 1 GPIO_ACTIVE_HIGH>;
	};

	scif3_rs_232_485_mux: mux-controller-5 {
		compatible = "gpio-mux";
		/* default rs232 */
		idle-state = <0>;
		#mux-control-cells = <0>;
		/*
		 * Mux switches scif3 tx/rx between rs232 and rs485
		 * transceivers. using one GPIO: 0 = rs232, 1 = rs485.
		 */
		mux-gpios = <&tca6416_u20 2 GPIO_ACTIVE_HIGH>;
	};

	v_1_2: regulator-1v2 {
		compatible = "regulator-fixed";
		regulator-name = "1v2";
		regulator-max-microvolt = <1200000>;
		regulator-min-microvolt = <1200000>;
	};

	v_3_3: regulator-3v3 {
		compatible = "regulator-fixed";
		regulator-name = "3v3";
		regulator-max-microvolt = <3300000>;
		regulator-min-microvolt = <3300000>;
	};

	reg_dsi_panel: regulator-dsi-panel {
		compatible = "regulator-fixed";
		regulator-name = "dsi-panel";
		gpios = <&tca6416_u20 15 GPIO_ACTIVE_HIGH>;
		regulator-max-microvolt = <11200000>;
		regulator-min-microvolt = <11200000>;
		enable-active-high;
	};

	vmmc: regulator-mmc {
		compatible = "regulator-fixed";
		regulator-name = "vmmc";
		regulator-max-microvolt = <3300000>;
		regulator-min-microvolt = <3300000>;
		startup-delay-us = <250>;
		vin-supply = <&v_3_3>;
		gpios = <&pinctrl RZG2L_GPIO(4, 1) GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};

	/* power for USB-A J5003 */
	vbus1: regulator-vbus-1 {
		compatible = "regulator-fixed";
		regulator-name = "vbus1";
		regulator-max-microvolt = <5000000>;
		regulator-min-microvolt = <5000000>;
		gpios = <&tca6416_u20 14 GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};

	rfkill-m2-b-gnss {
		compatible = "rfkill-gpio";
		/* rfkill-gpio inverts internally */
		shutdown-gpios = <&tca6416_u20 10 GPIO_ACTIVE_LOW>;
		label = "m2-b gnss";
		radio-type = "gps";
	};

	rfkill-m2-b-wwan {
		compatible = "rfkill-gpio";
		/* rfkill-gpio inverts internally */
		shutdown-gpios = <&tca6416_u20 9 GPIO_ACTIVE_HIGH>;
		label = "m2-b radio";
		radio-type = "wwan";
	};
};

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

	hub_2_0: hub@1 {
		compatible = "usb4b4,6502", "usb4b4,6506";
		reg = <1>;
		reset-gpios = <&tca6416_u20 11 GPIO_ACTIVE_LOW>;
		vdd2-supply = <&v_3_3>;
		vdd-supply = <&v_1_2>;
	};
};

&i2c0 {
	/* highest i2c clock supported by all peripherals is 400kHz */

	tca6416_u20: gpio@20 {
		compatible = "ti,tcal6416";
		reg = <0x20>;
		#gpio-cells = <2>;
		gpio-controller;
		gpio-line-names = "TCA_INT/EXT_UART", "TCA_UARTA_232/485",
				  "TCA_UARTB_232/485", "TCA_INT/EXT_CAN",
				  "TCA_NXP/REN", "TCA_M.2B_3V3_EN",
				  "TCA_M.2M_3V3_EN", "TCA_M.2M_RESET#",
				  "TCA_M.2B_RESET#", "TCA_M.2B_W_DIS#",
				  "TCA_M.2B_GPS_EN#", "TCA_USB-HUB_RST#",
				  "TCA_USB_HUB3_PWR_EN", "TCA_USB_HUB4_PWR_EN",
				  "TCA_USB1_PWR_EN", "TCA_VIDEO_PWR_EN";

		m2-b-reset-hog {
			gpios = <8 GPIO_ACTIVE_LOW>;
			gpio-hog;
			line-name = "m2-b-reset";
			output-low;
		};

		m2-m-reset-hog {
			gpios = <7 GPIO_ACTIVE_LOW>;
			gpio-hog;
			line-name = "m2-m-reset";
			/*
			 * M.2 Key-M connector only supports PCI,
			 * but RZ/G2L(C) has no pci controller.
			 * Keep any card in reset.
			 */
			output-high;
		};
	};

	tca6416_u21: gpio@21 {
		compatible = "ti,tcal6416";
		reg = <0x21>;
		#interrupt-cells = <2>;
		interrupt-controller;
		#gpio-cells = <2>;
		gpio-controller;
		gpio-line-names = "TCA_SPI_TPM/EXT", "TCA_TPM_RST#",
				  "TCA_I2C_RST", "TCA_RS232_SHTD#",
				  "TCA_LCD_I2C_RST", "TCA_DIG_OUT1",
				  "TCA_bDIG_IN1", "TCA_SENS_INT",
				  "TCA_ALERT#", "TCA_TPM_PIRQ#",
				  "TCA_RTC_INT", "TCA_M.2M_WAKW_ON_LAN",
				  "TCA_M.2M_CLKREQ#", "TCA_LVDS_INT#",
				  "", "TCA_POE_AT";
		/*
		 * Level interrupts are typical for gpio expander but not supported on rz/g2l
		 * gpios. However since TCAL6416 latches interrupts and clears only on reading
		 * port register, falling edge trigger doesn't lose events.
		 */
		interrupts-extended = <&pinctrl RZG2L_GPIO(4, 0) IRQ_TYPE_EDGE_FALLING>;

		lcd-i2c-reset-hog {
			gpios = <4 (GPIO_ACTIVE_LOW|GPIO_PULL_UP|GPIO_OPEN_DRAIN)>;
			line-name = "lcd-i2c-reset";
			output-low;
			/*
			 * reset shared between U37 and U48, to be
			 * supported once gpio-pca953x switches to
			 * reset framework.
			 */
			gpio-hog;
		};

		lvds-irq-hog {
			gpios = <13 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
			gpio-hog;
			input;
			line-name = "lvds-irq";
		};

		m2-m-clkreq-hog {
			gpios = <12 GPIO_ACTIVE_LOW>;
			gpio-hog;
			input;
			line-name = "m2-m-clkreq";
		};

		rs232_shutdown: rs232-shutdown-hog {
			gpios = <3 GPIO_ACTIVE_LOW>;
			gpio-hog;
			line-name = "rs232-shutdown";
			output-low;
		};

		sensor-irq-hog {
			gpios = <7 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
			gpio-hog;
			input;
			line-name = "sensor-irq";
		};

		tpm-irq-hog {
			gpios = <9 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
			gpio-hog;
			input;
			line-name = "tpm-irq";
		};
	};

	led-controller@30 {
		compatible = "ti,lp5562";
		reg = <0x30>;
		#address-cells = <1>;
		#size-cells = <0>;
		/* use internal clock, could use external generated by rtc */
		clock-mode = /bits/ 8 <1>;

		multi-led@0 {
			reg = <0x0>;
			#address-cells = <1>;
			#size-cells = <0>;
			color = <LED_COLOR_ID_RGB>;
			label = "D7";

			led@0 {
				reg = <0x0>;
				color = <LED_COLOR_ID_RED>;
				led-cur = /bits/ 8 <0x32>;
				max-cur = /bits/ 8 <0x64>;
			};

			led@1 {
				reg = <0x1>;
				color = <LED_COLOR_ID_GREEN>;
				led-cur = /bits/ 8 <0x19>;
				max-cur = /bits/ 8 <0x32>;
			};

			led@2 {
				reg = <0x2>;
				color = <LED_COLOR_ID_BLUE>;
				led-cur = /bits/ 8 <0x19>;
				max-cur = /bits/ 8 <0x32>;
			};
		};

		led@3 {
			reg = <0x3>;
			chan-name = "D8";
			color = <LED_COLOR_ID_GREEN>;
			label = "D8";
			led-cur = /bits/ 8 <0x19>;
			max-cur = /bits/ 8 <0x64>;
		};
	};

	light-sensor@44 {
		compatible = "isil,isl29023";
		reg = <0x44>;
		/* IRQ shared between accelerometer, light-sensor and Tamper input (J5007) */
		interrupts-extended = <&tca6416_u21 7 IRQ_TYPE_LEVEL_LOW>;
	};

	accelerometer@53 {
		compatible = "adi,adxl345";
		reg = <0x53>;
		interrupts-extended = <&tca6416_u21 7 IRQ_TYPE_LEVEL_LOW>;
		/* IRQ shared between accelerometer, light-sensor and Tamper input (J5007) */
		interrupt-names = "INT1";
	};

	carrier_eeprom: eeprom@57 {
		compatible = "atmel,24c02";
		reg = <0x57>;
		pagesize = <8>;
	};

	carrier_rtc: rtc@69 {
		compatible = "abracon,ab1805";
		reg = <0x69>;
		/*
		 * AM1805 RTC used on this board has only nTIRQ pins wired,
		 * which is for countdown timer irqs only.
		 * Driver does not support this, disable for now.
		 *
		 * interrupts-extended = <&tca6416_u21 10 IRQ_TYPE_LEVEL_LOW>;
		 */
		abracon,tc-diode = "schottky";
		abracon,tc-resistor = <3>;
	};
};

&i2c1 {
	/* highest i2c clock supported by all peripherals is 400kHz */

	i2c-mux@70 {
		compatible = "nxp,pca9546";
		reg = <0x70>;
		#address-cells = <1>;
		#size-cells = <0>;
		/*
		 * This reset is open drain with HW 10k pull-up resistor,
		 * but reset core does not support GPIO_OPEN_DRAIN flag.
		 * The pull-up and GPIO voltages match, push-pull is safe.
		 */
		reset-gpios = <&tca6416_u21 2 GPIO_ACTIVE_LOW>;

		/* channel 0 routed to expansion connector (J22) */
		i2c_exp: i2c@0 {
			reg = <0>;
			#address-cells = <1>;
			#size-cells = <0>;
		};

		/* channel 1 routed to mipi-csi connector (J23) */
		i2c_csi: i2c@1 {
			reg = <1>;
			#address-cells = <1>;
			#size-cells = <0>;
		};

		/* channel 2 routed to mipi-dsi connector (J25) */
		i2c_dsi: i2c@2 {
			reg = <2>;
			#address-cells = <1>;
			#size-cells = <0>;

			tca6408_u48: gpio@21 {
				compatible = "ti,tca6408";
				reg = <0x21>;
				#gpio-cells = <2>;
				gpio-line-names = "CAM_RST#", "DSI_RESET",
						  "DSI_STBYB", "DSI_PWM_BL",
						  "DSI_L/R", "DSI_U/D",
						  "DSI_CTP_/RST", "CAM_TRIG";
				/*
				 * reset shared between U37 and U48, to be
				 * supported once gpio-pca953x switches to
				 * reset framework.
				 *
				 * reset-gpios = <&tca6416_u21 4
				 *                (GPIO_ACTIVE_LOW|GPIO_PULL_UP|GPIO_OPEN_DRAIN)>;
				 */
				gpio-controller;
			};
		};

		/* channel 3 routed to lvds connector (J24) */
		i2c_lvds: i2c@3 {
			reg = <3>;
			#address-cells = <1>;
			#size-cells = <0>;

			tca6408_u37: gpio@20 {
				compatible = "ti,tca6408";
				reg = <0x20>;
				#gpio-cells = <2>;
				gpio-line-names = "SELB", "LVDS_RESET",
						  "LVDS_STBYB", "LVDS_PWM_BL",
						  "LVDS_L/R", "LVDS_U/D",
						  "LVDS_CTP_/RST", "";
				/*
				 * reset shared between U37 and U48, to be
				 * supported once gpio-pca953x switches to
				 * reset framework.
				 *
				 * reset-gpios = <&tca6416_u21 4
				 *                (GPIO_ACTIVE_LOW|GPIO_PULL_UP|GPIO_OPEN_DRAIN)>;
				 */
				gpio-controller;
			};
		};
	};
};

&ohci0 {
	dr_mode = "host";
};

&phy0 {
	leds {
		#address-cells = <1>;
		#size-cells = <0>;

		/* LED_0 pin */
		led@0 {
			reg = <0>;
			color = <LED_COLOR_ID_GREEN>;
			default-state = "keep";
			function = LED_FUNCTION_LAN;
		};
	};
};

&pinctrl {
	/* UARTA */
	scif1_pins: scif1 {
		pinmux = <RZG2L_PORT_PINMUX(40, 1, 1)>, /* SCIF1_RXD */
			 <RZG2L_PORT_PINMUX(40, 0, 1)>; /* SCIF1_TXD */
	};

	/* UARTB */
	scif3_pins: scif3 {
		pinmux = <RZG2L_PORT_PINMUX(0, 1, 5)>, /* SCIF3_RXD */
			 <RZG2L_PORT_PINMUX(0, 0, 5)>; /* SCIF3_TXD */
	};
};

&scif1 {
	pinctrl-0 = <&scif1_pins>;
	pinctrl-names = "default";
	status = "okay";
};

&scif3 {
	pinctrl-0 = <&scif3_pins>;
	pinctrl-names = "default";
	status = "okay";
};

&spi1 {
	/* native cs does not support cs persistence required for tpm */
	cs-gpios = <&pinctrl RZG2L_GPIO(44, 3) GPIO_ACTIVE_LOW>;
	num-cs = <1>;
	pinctrl-0 = <&spi1_pins>;
	pinctrl-names = "default";
	status = "okay";

	spi1_muxed: spi@0 {
		compatible = "spi-mux";
		reg = <0>;
		#address-cells = <1>;
		#size-cells = <0>;
		mux-controls = <&spi_mux>;
		/* mux bandwidth is 2GHz, soc max. spi clock is P0/2 = 50MHz */
		spi-max-frequency = <50000000>;

		tpm@0 {
			compatible = "infineon,slb9670", "tcg,tpm_tis-spi";
			reg = <0>;
			interrupts-extended = <&tca6416_u21 9 IRQ_TYPE_LEVEL_LOW>;
			reset-gpios = <&tca6416_u21 1 (GPIO_ACTIVE_LOW | GPIO_OPEN_DRAIN)>;
			spi-max-frequency = <43000000>;
		};
	};
};

&usb2_phy0 {
	vbus-supply = <&vbus1>;
};