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

/dts-v1/;

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

#include "r9a09g087m44.dtsi"

/*
 * SD0 can be connected to either eMMC (U33) or SD card slot CN21
 * Lets by default enable the eMMC, note we need the below SW settings
 * for eMMC. Also ensure that JP23 pins 5 and 6 are connected with a jumper
 * to provide the SD0 power supply when using eMMC.
 * DSW5[1] = ON; DSW5[2] = ON
 * DSW17[5] = OFF; DSW17[6] = ON
 *
 * To enable SD card and disable eMMC on SDHI0 disable the below macro
 * and set the below switch settings. Also ensure that JP23 pins 3 and 4 are
 * connected with a jumper to provide the SD0 power supply when using an SD card.
 * DSW5[1] = OFF; DSW5[2] = ON
 * P22_6 = SD0_WP; DSW15[1] = OFF; DSW15[2] = ON
 * P22_5 = SD0_CD; DSW15[3] = OFF; DSW15[4] = ON
 * P02_6 = SD0_IOVS; DSW17[5] = OFF; DSW17[6] = ON
 * P02_5 = SD0_PWEN; DSW17[7] = OFF; DSW17[8] = ON
 */
#define SD0_EMMC       1
#define SD0_SD		(!SD0_EMMC)

/*
 * To enable CANFD interface disable both eMMC and SD card on SDHI0 by
 * setting SD0_EMMC and SD0_SD macros to 0 as pins P12_0 and P12_1
 * will be used for CANFD interface.
 */
#define CANFD_ENABLE	(!SD0_EMMC && !SD0_SD)

/*
 * P17_4 = SD1_CD; DSW5[3] = ON; DSW19[1] = OFF; DSW19[2] = ON
 * P08_6 = SD1_IOVS; DSW5[3] = ON
 * To enable proper operation in 1.8V modes, JP21 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 (CN7), Mini-B
 * (CN8), and Micro-AB (CN9). The RZ/N2H 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 CN7 and CN8 connectors.
 *
 * By default, the Type-A (CN7) and Mini-B (CN8) connectors are enabled.
 * Configure the switches as follows:
 *   - P02_2 - P02_3 (control signals for USB power supply): DSW2[6] = OFF;
 *     - P02_2 (used for VBUSEN): DSW14[5] = OFF; DSW14[6] = ON
 *     - P02_3 (used for USB_OVRCUR): DSW14[1] = OFF; DSW14[2] = ON
 *   - USB_VBUSIN (used for VBUS of CN8): DSW16[1] = OFF; DSW16[2] = ON
 *   - USB_VBUSEN (used for USB_HF_VBUSEN): DSW16[3] = OFF; DSW16[4] = ON
 *
 * To enable the Micro-AB (CN9) USB OTG connector, set the following macro
 * to 1 and configure the switches as follows:
 *   - P02_2 - P02_3 (control signals for USB power supply): DSW2[6] = OFF;
 *     - P02_2 (used for VBUSEN): DSW14[5] = OFF; DSW14[6] = ON
 *     - P02_3 (used for USB_OVRCUR): DSW14[1] = OFF; DSW14[2] = ON
 *   - USB_VBUSIN (used for VBUS for OTG): DSW16[1] = ON; DSW16[2] = OFF
 *   - USB_VBUSEN (used for USB_OTG_VBUSEN): DSW16[3] = ON; DSW16[4] = OFF
 *   - USB_EXICEN (used for USB OTG EXICEN): DSW14[3] = OFF; DSW14[4] = ON
 */
#define USB_OTG		0

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

/*
 * I2C0 and LED8/9 share the same pins use the below
 * macro to choose (and set approopriate DIP switches).
 */
#define I2C0	1
#define LED8	(!I2C0)
#define LED9	(!I2C0)

/ {
	model = "Renesas RZ/N2H EVK Board based on r9a09g087m44";
	compatible = "renesas,rzn2h-evk", "renesas,r9a09g087m44", "renesas,r9a09g087";

	keys {
		compatible = "gpio-keys";

		key-1 {
			interrupts-extended = <&pinctrl RZT2H_GPIO(18, 2) IRQ_TYPE_EDGE_FALLING>;
			linux,code = <KEY_1>;
			label = "SW2";
			wakeup-source;
			debounce-interval = <20>;
		};

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

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

	leds {
		compatible = "gpio-leds";

		led-3 {
			/* DSW18[7]: ON, DSW18[8]: OFF */
			gpios = <&pinctrl RZT2H_GPIO(31, 6) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <4>;
		};

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

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

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

		led-7 {
			/*
			 * DSW18[5]: ON, DSW18[6]: OFF
			 * DSW19[3]: OFF, DSW19[4]: ON
			 */
			gpios = <&pinctrl RZT2H_GPIO(14, 3) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <8>;
		};

		led-8 {
#if LED8
			/*
			 * USER_LED0
			 * DSW15[8]: OFF, DSW15[9]: OFF, DSW15[10]: ON
			 */
			gpios = <&pinctrl RZT2H_GPIO(14, 6) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <0>;
#else
			status = "disabled";
#endif
		};

		led-9 {
#if LED9
			/*
			 * USER_LED1
			 * DSW15[5]: OFF, DSW15[6]: ON
			 */
			gpios = <&pinctrl RZT2H_GPIO(14, 7) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <1>;
#else
			status = "disabled";
#endif
		};

		led-10 {
			/*
			 * USER_LED2
			 * DSW17[3]: OFF, DSW17[4]: ON
			 */
			gpios = <&pinctrl RZT2H_GPIO(2, 7) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_YELLOW>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <2>;
		};

		led-11 {
			/*
			 * USER_LED3
			 * DSW17[1]: OFF, DSW17[2]: ON
			 */
			gpios = <&pinctrl RZT2H_GPIO(3, 0) GPIO_ACTIVE_HIGH>;
			color = <LED_COLOR_ID_RED>;
			function = LED_FUNCTION_DEBUG;
			function-enumerator = <3>;
		};
	};
};

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

	channel@6 {
		reg = <0x6>;
	};

	channel@7 {
		reg = <0x7>;
	};

	channel@8 {
		reg = <0x8>;
	};

	channel@9 {
		reg = <0x9>;
	};

	channel@a {
		reg = <0xa>;
	};

	channel@b {
		reg = <0xb>;
	};

	channel@c {
		reg = <0xc>;
	};

	channel@d {
		reg = <0xd>;
	};

	channel@e {
		reg = <0xe>;
	};
};

#if CANFD_ENABLE
&canfd {
	pinctrl-0 = <&can1_pins>;
	pinctrl-names = "default";
	status = "okay";

	channel1 {
		status = "okay";
	};
};
#endif

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

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

&mdio1_phy {
	interrupts-extended = <&icu RZN2H_IRQ15 IRQ_TYPE_EDGE_FALLING>;
	/*
	 * PHY3 Reset Configuration:
	 *
	 * DSW12[5] OFF; DSW12[6] ON - use pin P03_2 for GMAC_RESETOUT3#
	 */
	reset-gpios = <&pinctrl RZT2H_GPIO(3, 2) GPIO_ACTIVE_LOW>;
};

&mdio2_phy {
	interrupts-extended = <&icu RZN2H_IRQ14 IRQ_TYPE_EDGE_FALLING>;
	/*
	 * PHY2 Reset Configuration:
	 *
	 * DSW8[1] ON; DSW8[2] OFF; DSW12[7] OFF; DSW12[8] ON - use pin
	 * P03_1 for GMAC_RESETOUT2#
	 */
	reset-gpios = <&pinctrl RZT2H_GPIO(3, 1) GPIO_ACTIVE_LOW>;
};

&pinctrl {
	/*
	 * CAN1 Pin Configuration:
	 *
	 * DSW5[1] ON; DSW5[2] OFF - Use P12_0 and P12_1 for CAN1 interface.
	 */
	can1_pins: can1-pins {
		pinmux = <RZT2H_PORT_PINMUX(12, 0, 0x19)>, /* CANRX1 */
			 <RZT2H_PORT_PINMUX(12, 1, 0x19)>; /* CANTX1 */
	};

	/*
	 * GMAC1 Pin Configuration:
	 *
	 * DSW5[8] ON - use pins P33_2-P33_7 and P34_0-P34_6 for Ethernet port 3
	 */
	gmac1_pins: gmac1-group {
		irq-pins {
			pinmux = <RZT2H_PORT_PINMUX(17, 3, 0x0)>; /* IRQ15 */
			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:
	 *
	 * DSW5[6] OFF - connect MDC/MDIO of Ethernet port 2 to GMAC2
	 * DSW5[7] ON - use pins P29_1-P29_7 and P30_0-P30_4 for Ethernet port 2
	 * DSW13[7] OFF; DSW13[8] ON - use pin P13_7 for IRQ14
	 */
	gmac2_pins: gmac2-group {
		irq-pins {
			pinmux = <RZT2H_PORT_PINMUX(13, 7, 0x0)>; /* IRQ14 */
			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     | DSW15
	 * -------|---------|--------------
	 * SCL    | P14_6   | 8: OFF, 9: ON, 10: OFF
	 * SDA    | P14_7   | 5: ON, 6: OFF
	 */
	i2c0_pins: i2c0-pins {
		pinmux = <RZT2H_PORT_PINMUX(14, 6, 0x17)>,
			 <RZT2H_PORT_PINMUX(14, 7, 0x17)>;
	};

	/*
	 * I2C1 Pin Configuration:
	 * ------------------------
	 * Signal | Pin     | DSW7
	 * -------|---------|--------------
	 * SCL    | P03_3   | 1: ON, 2: OFF
	 * SDA    | P03_4   | 3: ON, 4: OFF
	 */
	i2c1_pins: i2c1-pins {
		pinmux = <RZT2H_PORT_PINMUX(3, 3, 0x17)>,
			 <RZT2H_PORT_PINMUX(3, 4, 0x17)>;
	};

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

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