// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/*
 * Device Tree Source for the RZ/T2H EVK board
 *
 * Copyright (C) 2025 Renesas Electronics Corp.
 */

/dts-v1/;

#include <dt-bindings/input/input.h>

#include "r9a09g077m44.dtsi"

/*
 * SD0 can be connected to either eMMC (IC49) or SD card slot CN31
 * Lets by default enable the eMMC, note we need the below SW settings
 * for eMMC. Also ensure that CN78 pins 5 and 6 are connected with a jumper
 * to provide the SD0 power supply when using eMMC.
 * SW2[1] = ON; SW2[2] = ON
 *
 * To enable SD card and disable eMMC on SDHI0 disable the below macro
 * and set the switch as follows. Also ensure that CN78 pins 3 and 4 are connected
 * with a jumper to provide the SD0 power supply when using an SD card.
 *
 * SW2[1] = OFF; SW2[2] = ON.
 */
#define SD0_EMMC	1
#define SD0_SD		(!SD0_EMMC)

/*
 * P17_4 = SD1_CD; SW2[3] = ON
 * P08_5 = SD1_PWEN; SW2[3] = ON
 * P08_6 = SD1_IOVS; SW2[3] = ON; SW5[3] = OFF; SW5[4] = ON
 * To enable proper operation in 1.8V modes, CN77 must have pins 2 and 3
 * connected by the jumper. This connects SD1 power-supply control IC output
 * back to VCC1833_7.
 */
#define SD1_MICRO_SD	1

/*
 * USB Pin Configuration:
 *
 * This board is equipped with three USB connectors: Type-A (CN80), Mini-B
 * (CN79), and Micro-AB (CN33). The RZ/T2H SoC has a single USB channel, so
 * either the USB host interface or the USB function interface can be used,
 * but not both simultaneously when using the CN79 and CN80 connectors.
 *
 * By default, the Type-A (CN80) and Mini-B (CN79) connectors are enabled.
 * Configure the switches as follows:
 *   - P00_0 - P00_2 (control signals for USB power supply): SW1[5] = ON
 *   - USB_VBUSIN (used for USB function): SW7[7] = OFF; SW7[8] = ON
 *   - USB_VBUSEN (used for USB_HF_VBUSEN): SW7[9] = OFF; SW7[10] = ON
 *
 * To enable the Micro-AB (CN33) USB OTG connector, set the following macro
 * to 1 and configure the switches as follows:
 *   - P00_0 - P00_2 (control signals for USB power supply): SW1[5] = ON
 *   - USB_VBUSIN (used for USB OTG): SW7[7] = ON; SW7[8] = OFF
 *   - USB_VBUSEN (used for USB_OTG_VBUSEN): SW7[9] = ON; SW7[10] = OFF
 */
#define USB_OTG		0

#include "rzt2h-n2h-evk-common.dtsi"

/ {
	model = "Renesas RZ/T2H EVK Board based on r9a09g077m44";
	compatible = "renesas,rzt2h-evk", "renesas,r9a09g077m44", "renesas,r9a09g077";

	keys {
		compatible = "gpio-keys";

#if (!SD1_MICRO_SD)
		/* SW2[3]: OFF */
		key-1 {
			interrupts-extended = <&pinctrl RZT2H_GPIO(8, 6) IRQ_TYPE_EDGE_FALLING>;
			linux,code = <KEY_1>;
			label = "SW9";
			wakeup-source;
			debounce-interval = <20>;
		};
#endif

		key-2 {
			interrupts-extended = <&pinctrl RZT2H_GPIO(0, 3) IRQ_TYPE_EDGE_FALLING>;
			linux,code = <KEY_2>;
			label = "SW10";
			wakeup-source;
			debounce-interval = <20>;
		};

		key-3 {
			interrupts-extended = <&pinctrl RZT2H_GPIO(8, 7) IRQ_TYPE_EDGE_FALLING>;
			linux,code = <KEY_3>;
			label = "SW11";
			wakeup-source;
			debounce-interval = <20>;
		};
	};

	leds {
		compatible = "gpio-leds";

		led-0 {
			/* SW8[9]: ON, SW8[10]: OFF */
			gpios = <&pinctrl RZT2H_GPIO(23, 1) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <0>;
		};

		led-1 {
			/* SW5[1]: OFF, SW5[2]: ON */
			gpios = <&pinctrl RZT2H_GPIO(32, 2) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <1>;
		};

		led-2 {
			gpios = <&pinctrl RZT2H_GPIO(6, 7) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_YELLOW>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <2>;
		};

#if (!SD1_MICRO_SD)
		led-3 {
			/* SW2[3]: OFF */
			gpios = <&pinctrl RZT2H_GPIO(8, 5) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_RED>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <3>;
		};
#endif

		led-4 {
			/* SW8[3]: ON, SW8[4]: OFF */
			gpios = <&pinctrl RZT2H_GPIO(18, 0) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <4>;
		};

		led-5 {
			/* SW8[1]: ON, SW8[2]: OFF */
			gpios = <&pinctrl RZT2H_GPIO(18, 1) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_RED>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <5>;
		};

		led-6 {
			/* SW5[9]: OFF, SW5[10]: ON */
			gpios = <&pinctrl RZT2H_GPIO(22, 7) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <6>;
		};

		led-7 {
			/* SW5[7]: OFF, SW5[8]: ON */
			gpios = <&pinctrl RZT2H_GPIO(23, 0) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <7>;
		};

		led-8 {
			/* SW7[5]: OFF, SW7[6]: ON */
			gpios = <&pinctrl RZT2H_GPIO(23, 5) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <8>;
		};
	};
};

&adc2 {
	status = "okay";

	channel@0 {
		reg = <0x0>;
	};

	channel@1 {
		reg = <0x1>;
	};

	channel@2 {
		reg = <0x2>;
	};

	channel@3 {
		reg = <0x3>;
	};

	channel@4 {
		reg = <0x4>;
	};

	channel@5 {
		reg = <0x5>;
	};
};

&canfd {
	pinctrl-0 = <&can0_pins>;
	pinctrl-names = "default";
	status = "okay";

	channel0 {
		status = "okay";
	};
};

&i2c0 {
	pinctrl-0 = <&i2c0_pins>;
	pinctrl-names = "default";
	clock-frequency = <400000>;
	status = "okay";
};

&i2c1 {
	pinctrl-0 = <&i2c1_pins>;
	pinctrl-names = "default";
	clock-frequency = <400000>;
	status = "okay";
};

&mdio1_phy {
	interrupts-extended = <&icu RZT2H_IRQ3 IRQ_TYPE_EDGE_FALLING>;
	reset-gpios = <&pinctrl RZT2H_GPIO(32, 3) GPIO_ACTIVE_LOW>;
};

&mdio2_phy {
	interrupts-extended = <&icu RZT2H_IRQ13 IRQ_TYPE_EDGE_FALLING>;
	/*
	 * PHY2 Reset Configuration:
	 *
	 * SW6[1] OFF; SW6[2] ON; SW6[3] OFF - use pin P17_5 for GMAC_RESETOUT2#
	 */
	reset-gpios = <&pinctrl RZT2H_GPIO(17, 5) GPIO_ACTIVE_LOW>;
};

&pinctrl {
	/*
	 * CAN0 Pin Configuration:
	 *
	 * SW7[1] OFF; SW7[2] ON - Use P24_4 as CANTX0.
	 * SW7[3] OFF; SW7[4] ON - Use P24_3 as CANRX0.
	 */
	can0_pins: can0-pins {
		pinmux = <RZT2H_PORT_PINMUX(24, 3, 0x19)>, /* CANRX0 */
			 <RZT2H_PORT_PINMUX(24, 4, 0x19)>; /* CANTX0 */
	};

	/*
	 * GMAC1 Pin Configuration:
	 *
	 * SW2[8] ON - use pins P33_2-P33_7 and P34_0-P34_5 for Ethernet port 3
	 */
	gmac1_pins: gmac1-group {
		irq-pins {
			pinmux = <RZT2H_PORT_PINMUX(27, 2, 0x0)>; /* IRQ3 */
			drive-strength-microamp = <5000>;
			slew-rate = <0>;
			input-schmitt-disable;
		};

		md-pins {
			pinmux = <RZT2H_PORT_PINMUX(26, 1, 0x10)>, /* GMAC1_MDC */
				 <RZT2H_PORT_PINMUX(26, 2, 0x10)>; /* GMAC1_MDIO */
			drive-strength-microamp = <5000>;
			slew-rate = <0>;
			input-schmitt-disable;
		};

		refclk-pins {
			pinmux = <RZT2H_PORT_PINMUX(34, 6, 0x2)>; /* ETH3_REFCLK */
			drive-strength-microamp = <5000>;
			slew-rate = <1>;
		};

		rx-pins {
			pinmux = <RZT2H_PORT_PINMUX(34, 0, 0xf)>, /* ETH3_RXCLK */
				 <RZT2H_PORT_PINMUX(34, 1, 0xf)>, /* ETH3_RXD0 */
				 <RZT2H_PORT_PINMUX(34, 2, 0xf)>, /* ETH3_RXD1 */
				 <RZT2H_PORT_PINMUX(34, 3, 0xf)>, /* ETH3_RXD2 */
				 <RZT2H_PORT_PINMUX(34, 4, 0xf)>, /* ETH3_RXD3 */
				 <RZT2H_PORT_PINMUX(34, 5, 0xf)>; /* ETH3_RXDV */
			input-schmitt-disable;
		};

		txclk-pins {
			pinmux = <RZT2H_PORT_PINMUX(33, 2, 0xf)>; /* ETH3_TXCLK */
			drive-strength-microamp = <11800>;
			slew-rate = <1>;
			input-schmitt-disable;
		};

		txd-en-pins {
			pinmux = <RZT2H_PORT_PINMUX(33, 3, 0xf)>, /* ETH3_TXD0 */
				 <RZT2H_PORT_PINMUX(33, 4, 0xf)>, /* ETH3_TXD1 */
				 <RZT2H_PORT_PINMUX(33, 5, 0xf)>, /* ETH3_TXD2 */
				 <RZT2H_PORT_PINMUX(33, 6, 0xf)>, /* ETH3_TXD3 */
				 <RZT2H_PORT_PINMUX(33, 7, 0xf)>; /* ETH3_TXEN */
			drive-strength-microamp = <11800>;
			slew-rate = <1>;
		};
	};

	/*
	 * GMAC2 Pin Configuration:
	 *
	 * SW2[6] OFF - connect MDC/MDIO of Ethernet port 2 to GMAC2
	 * SW2[7] ON - use pins P29_1-P29_7 and P30_0-P30_4 for Ethernet port 2
	 */
	gmac2_pins: gmac2-group {
		irq-pins {
			pinmux = <RZT2H_PORT_PINMUX(31, 1, 0x0)>; /* IRQ13 */
			drive-strength-microamp = <5000>;
			slew-rate = <0>;
			input-schmitt-disable;
		};

		md-pins {
			pinmux = <RZT2H_PORT_PINMUX(30, 5, 0x10)>, /* GMAC2_MDC */
				 <RZT2H_PORT_PINMUX(30, 6, 0x10)>; /* GMAC2_MDIO */
			drive-strength-microamp = <5000>;
			slew-rate = <0>;
			input-schmitt-disable;
		};

		refclk-pins {
			pinmux = <RZT2H_PORT_PINMUX(31, 0, 0x2)>; /* ETH2_REFCLK */
			drive-strength-microamp = <5000>;
			slew-rate = <1>;
		};

		rx-pins {
			pinmux = <RZT2H_PORT_PINMUX(29, 7, 0xf)>, /* ETH2_RXCLK */
				 <RZT2H_PORT_PINMUX(30, 0, 0xf)>, /* ETH2_RXD0 */
				 <RZT2H_PORT_PINMUX(30, 1, 0xf)>, /* ETH2_RXD1 */
				 <RZT2H_PORT_PINMUX(30, 2, 0xf)>, /* ETH2_RXD2 */
				 <RZT2H_PORT_PINMUX(30, 3, 0xf)>, /* ETH2_RXD3 */
				 <RZT2H_PORT_PINMUX(30, 4, 0xf)>; /* ETH2_RXDV */
			input-schmitt-disable;
		};

		txclk-pins {
			pinmux = <RZT2H_PORT_PINMUX(29, 1, 0xf)>; /* ETH2_TXCLK */
			drive-strength-microamp = <11800>;
			slew-rate = <1>;
			input-schmitt-disable;
		};

		txd-en-pins {
			pinmux = <RZT2H_PORT_PINMUX(29, 2, 0xf)>, /* ETH2_TXD0 */
				 <RZT2H_PORT_PINMUX(29, 3, 0xf)>, /* ETH2_TXD1 */
				 <RZT2H_PORT_PINMUX(29, 4, 0xf)>, /* ETH2_TXD2 */
				 <RZT2H_PORT_PINMUX(29, 5, 0xf)>, /* ETH2_TXD3 */
				 <RZT2H_PORT_PINMUX(29, 6, 0xf)>; /* ETH2_TXEN */
			drive-strength-microamp = <11800>;
			slew-rate = <1>;
		};
	};

	/*
	 * I2C0 Pin Configuration:
	 * ------------------------
	 * Signal | Pin     | SW6
	 * -------|---------|--------------
	 * SCL    | P23_3   | 7: ON, 8: OFF
	 * SDA    | P23_4   | 9: ON, 10: OFF
	 */
	i2c0_pins: i2c0-pins {
		pinmux = <RZT2H_PORT_PINMUX(23, 3, 0x17)>,
			 <RZT2H_PORT_PINMUX(23, 4, 0x17)>;
	};

	i2c1_pins: i2c1-pins {
		pinmux = <RZT2H_PORT_PINMUX(5, 0, 0x17)>, /* SDA */
			 <RZT2H_PORT_PINMUX(4, 7, 0x17)>; /* SCL */
	};

#if USB_OTG
	usb-exicen-hog {
		gpio-hog;
		gpios = <RZT2H_GPIO(0, 2) GPIO_ACTIVE_HIGH>;
		output-high;
		line-name = "usb_exicen_a";
	};
#endif

	usb_pins: usb-pins {
		pinmux = <RZT2H_PORT_PINMUX(0, 0, 0x13)>, /* VBUSEN */
			 <RZT2H_PORT_PINMUX(0, 1, 0x13)>; /* OVRCUR */
	};
};

/*
 * XSPI0 Pin Configuration:
 * ------------------------
 * Signal     | Pin     | SW5
 * -----------|---------|---------------
 * XSPI0_ECS  | P07_5   | 5: OFF, 6: ON
 */
&xspi0_pins {
	ecs-pins {
		pinmux = <RZT2H_PORT_PINMUX(7, 5, 0x1c)>; /* XSPI0_ECS0 */
		drive-strength-microamp = <2500>;
		input-schmitt-disable;
		slew-rate = <0>;
	};
};