// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2021 PHYTEC America, LLC - https://www.phytec.com
 * Author: Matt McKee <mmckee@phytec.com>
 *
 * Copyright (C) 2022 PHYTEC Messtechnik GmbH
 * Author: Wadim Egorov <w.egorov@phytec.de>
 *
 * Product homepage:
 * https://www.phytec.com/product/phyboard-am64x
 */

/dts-v1/;

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/input/input.h>
#include <dt-bindings/leds/common.h>
#include <dt-bindings/leds/leds-pca9532.h>
#include <dt-bindings/phy/phy.h>
#include "k3-am642.dtsi"
#include "k3-am64-phycore-som.dtsi"

#include "k3-serdes.h"

/ {
	compatible = "phytec,am642-phyboard-electra-rdk",
		     "phytec,am64-phycore-som", "ti,am642";
	model = "PHYTEC phyBOARD-Electra-AM64x RDK";

	aliases {
		mmc1 = &sdhci1;
		serial2 = &main_uart0;
		serial3 = &main_uart1;
	};

	chosen {
		stdout-path = &main_uart0;
	};

	can_tc1: can-phy0 {
		compatible = "ti,tcan1042";
		pinctrl-names = "default";
		pinctrl-0 = <&can_tc1_pins_default>;
		#phy-cells = <0>;
		max-bitrate = <5000000>;
		standby-gpios = <&main_gpio0 32 GPIO_ACTIVE_HIGH>;
	};

	can_tc2: can-phy1 {
		compatible = "ti,tcan1042";
		pinctrl-names = "default";
		pinctrl-0 = <&can_tc2_pins_default>;
		#phy-cells = <0>;
		max-bitrate = <5000000>;
		standby-gpios = <&main_gpio0 35 GPIO_ACTIVE_HIGH>;
	};

	keys {
		compatible = "gpio-keys";
		autorepeat;
		pinctrl-names = "default";
		pinctrl-0 = <&gpio_keys_pins_default>;

		key-home {
			label = "home";
			linux,code = <KEY_HOME>;
			gpios = <&main_gpio0 17 GPIO_ACTIVE_HIGH>;
		};

		key-menu {
			label = "menu";
			linux,code = <KEY_MENU>;
			gpios = <&main_gpio0 21 GPIO_ACTIVE_HIGH>;
		};
	};

	leds {
		compatible = "gpio-leds";
		pinctrl-names = "default";
		pinctrl-0 = <&leds_pins_default>, <&user_leds_pins_default>;

		led-1 {
			color = <LED_COLOR_ID_RED>;
			gpios = <&main_gpio0 15 GPIO_ACTIVE_HIGH>;
			linux,default-trigger = "mmc0";
			function = LED_FUNCTION_DISK;
		};

		led-2 {
			color = <LED_COLOR_ID_GREEN>;
			gpios = <&main_gpio0 16 GPIO_ACTIVE_HIGH>;
			linux,default-trigger = "mmc1";
			function = LED_FUNCTION_DISK;
		};
	};

	vcc_3v3_mmc: regulator-sd {
		/* TPS22963C */
		compatible = "regulator-fixed";
		regulator-name = "VCC_3V3_MMC";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-boot-on;
		regulator-always-on;
	};
};

&main_pmx0 {
	can_tc1_pins_default: can-tc1-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x0084, PIN_OUTPUT, 7)	/* (P16) GPMC0_ADVn_ALE.GPIO0_32 */
		>;
	};

	can_tc2_pins_default: can-tc2-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x0090, PIN_OUTPUT, 7)	/* (P17) GPMC0_BE0n_CLE.GPIO0_35 */
		>;
	};

	gpio_keys_pins_default: gpio-keys-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x0044, PIN_INPUT, 7)	/* (T18) GPMC0_AD2.GPIO0_17 */
			AM64X_IOPAD(0x0054, PIN_INPUT, 7)	/* (V20) GPMC0_AD6.GPIO0_21 */
		>;
	};

	main_i2c1_pins_default: main-i2c1-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x0268, PIN_INPUT, 0)	/* (C18) I2C1_SCL */
			AM64X_IOPAD(0x026c, PIN_INPUT, 0)	/* (B19) I2C1_SDA */
		>;
	};

	main_mcan0_pins_default: main-mcan0-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x0250, PIN_OUTPUT, 0)	/* (A17) MCAN0_TX */
			AM64X_IOPAD(0x0254, PIN_INPUT, 0)	/* (B17) MCAN0_RX */
		>;
	};

	main_mcan1_pins_default: main-mcan1-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x0258, PIN_OUTPUT, 0)	/* (C17) MCAN1_TX */
			AM64X_IOPAD(0x025c, PIN_INPUT, 0)	/* (D17) MCAN1_RX */
		>;
	};

	main_mmc1_pins_default: main-mmc1-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x027c, PIN_INPUT_PULLUP, 0)	/* (K18) MMC1_DAT3 */
			AM64X_IOPAD(0x0280, PIN_INPUT_PULLUP, 0)	/* (K19) MMC1_DAT2 */
			AM64X_IOPAD(0x0284, PIN_INPUT_PULLUP, 0)	/* (L21) MMC1_DAT1 */
			AM64X_IOPAD(0x0288, PIN_INPUT_PULLUP, 0)	/* (K21) MMC1_DAT0 */
			AM64X_IOPAD(0x028c, PIN_INPUT_PULLDOWN, 0)	/* (L20) MMC1_CLK */
			AM64X_IOPAD(0x0290, PIN_INPUT, 0)		/* MMC1_CLKLB */
			AM64X_IOPAD(0x0294, PIN_INPUT_PULLUP, 0)	/* (J19) MMC1_CMD */
			AM64X_IOPAD(0x0298, PIN_INPUT_PULLUP, 0)	/* (D19) MMC1_SDCD */
		>;
	};

	main_uart0_pins_default: main-uart0-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x0230, PIN_INPUT, 0)	/* (D15) UART0_RXD */
			AM64X_IOPAD(0x0234, PIN_OUTPUT, 0)	/* (C16) UART0_TXD */
		>;
	};

	main_uart1_pins_default: main-uart1-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x0248, PIN_INPUT, 0)	/* (D16) UART1_CTSn */
			AM64X_IOPAD(0x024C, PIN_OUTPUT, 0)	/* (E16) UART1_RTSn */
			AM64X_IOPAD(0x0240, PIN_INPUT, 0)	/* (E15) UART1_RXD */
			AM64X_IOPAD(0x0244, PIN_OUTPUT, 0)	/* (E14) UART1_TXD */
		>;
	};

	main_usb0_pins_default: main-usb0-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x02a8, PIN_OUTPUT, 0)	/* (E19) USB0_DRVVBUS */
		>;
	};

	pcie_usb_sel_pins_default: pcie-usb-sel-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x017c, PIN_OUTPUT, 7)	/* (T1) PRG0_PRU0_GPO7.GPIO1_7 */
		>;
	};

	pcie0_pins_default: pcie0-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x0098, PIN_OUTPUT, 7)	/* (W19) GPMC0_WAIT0.GPIO0_37 */
		>;
	};

	user_leds_pins_default: user-leds-default-pins {
		pinctrl-single,pins = <
			AM64X_IOPAD(0x003c, PIN_OUTPUT, 7)	/* (T20) GPMC0_AD0.GPIO0_15 */
			AM64X_IOPAD(0x0040, PIN_OUTPUT, 7)	/* (U21) GPMC0_AD1.GPIO0_16 */
		>;
	};
};

&main_i2c1 {
	status = "okay";
	pinctrl-names = "default";
	pinctrl-0 = <&main_i2c1_pins_default>;
	clock-frequency = <400000>;

	eeprom@51 {
		compatible = "atmel,24c02";
		pagesize = <16>;
		reg = <0x51>;
	};

	led-controller@62 {
		compatible = "nxp,pca9533";
		reg = <0x62>;

		led-3 {
			label = "red:user";
			type = <PCA9532_TYPE_LED>;
		};

		led-4 {
			label = "green:user";
			type = <PCA9532_TYPE_LED>;
		};

		led-5 {
			label = "blue:user";
			type = <PCA9532_TYPE_LED>;
		};
	};
};

&main_mcan0 {
	status = "okay";
	pinctrl-names = "default";
	pinctrl-0 = <&main_mcan0_pins_default>;
	phys = <&can_tc1>;
};

&main_mcan1 {
	status = "okay";
	pinctrl-names = "default";
	pinctrl-0 = <&main_mcan1_pins_default>;
	phys = <&can_tc2>;
};

&main_uart0 {
	status = "okay";
	pinctrl-names = "default";
	pinctrl-0 = <&main_uart0_pins_default>;
	current-speed = <115200>;
};

&main_uart1 {
	status = "okay";
	pinctrl-names = "default";
	pinctrl-0 = <&main_uart1_pins_default>;
	uart-has-rtscts;
	current-speed = <115200>;
};

&sdhci1 {
	vmmc-supply = <&vcc_3v3_mmc>;
	pinctrl-names = "default";
	pinctrl-0 = <&main_mmc1_pins_default>;
	bus-width = <4>;
	ti,driver-strength-ohm = <50>;
	disable-wp;
	no-1-8-v;
};

&serdes0 {
	serdes0_pcie_usb_link: phy@0 {
		reg = <0>;
		cdns,num-lanes = <1>;
		#phy-cells = <0>;
		cdns,phy-type = <PHY_TYPE_USB3>;
		resets = <&serdes_wiz0 1>;
	};
};

&serdes_ln_ctrl {
	idle-states = <AM64_SERDES0_LANE0_USB>;
};

&usbss0 {
	ti,vbus-divider;
};

&usb0 {
	pinctrl-names = "default";
	pinctrl-0 = <&main_usb0_pins_default>;
	dr_mode = "host";
	maximum-speed = "super-speed";
	phys = <&serdes0_pcie_usb_link>;
	phy-names = "cdns3,usb3-phy";
};