// SPDX-License-Identifier: GPL-2.0-only OR MIT
/*
 * Copyright (C) 2026 PHYTEC America LLC
 * Author: Nathan Morrisson <nmorrisson@phytec.com>
 */

/dts-v1/;

#include <dt-bindings/input/input.h>
#include <dt-bindings/phy/phy.h>
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/interrupt-controller/irq.h>
#include "k3-serdes.h"
#include "k3-j722s.dtsi"
#include "k3-am67-phycore-som.dtsi"

/ {
	compatible = "phytec,am6754-phyboard-rigel",
		     "phytec,am67-phycore-som", "ti,j722s";
	model = "PHYTEC phyBOARD-Rigel AM67";

	aliases {
		gpio1 = &main_gpio1;
		mmc1 = &sdhci1;
		serial2 = &main_uart0;
		usb0 = &usb0;
		usb1 = &usb1;
	};

	can_tc0: can-phy0 {
		compatible = "ti,tcan1042";
		#phy-cells = <0>;
		max-bitrate = <8000000>;
		standby-gpios = <&gpio_exp1 1 GPIO_ACTIVE_HIGH>;
	};

	usb0_connector: connector {
		compatible = "gpio-usb-b-connector", "usb-b-connector";
		label = "USB-C";
		data-role = "dual";

		pinctrl-names = "default";
		pinctrl-0 = <&main_usbc_power_pins_default>;

		id-gpios = <&main_gpio1 15 GPIO_ACTIVE_HIGH>;

		port {
			usb0_con: endpoint {
				remote-endpoint = <&usb0_ep>;
			};
		};
	};

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

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

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

	vcc_1v8: regulator-vcc-1v8 {
		compatible = "regulator-fixed";
		regulator-name = "VCC_1V8";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		regulator-always-on;
		regulator-boot-on;
	};

	vcc_3v3_aud: regulator-vcc-3v3-aud {
		compatible = "regulator-fixed";
		regulator-name = "VCC_3V3_AUD";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-always-on;
		regulator-boot-on;
	};

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

	vcc_3v3_sw: regulator-vcc-3v3-sw {
		compatible = "regulator-fixed";
		regulator-name = "VCC_3V3_SW";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-always-on;
		regulator-boot-on;
	};

	vcc_speaker: regulator-vcc-speaker {
		compatible = "regulator-fixed";
		regulator-name = "VCC_SPEAKER";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		regulator-always-on;
		regulator-boot-on;
	};

	sound {
		compatible = "simple-audio-card";
		simple-audio-card,widgets =
			"Microphone", "Mic Jack",
			"Headphone", "Headphone Jack",
			"Line", "Stereo Jack",
			"Speaker", "L SPKR",
			"Speaker", "R SPKR";
		simple-audio-card,routing =
			"MIC1RP", "Mic Jack",
			"Mic Jack", "MICBIAS",
			"Headphone Jack", "HPL",
			"Headphone Jack", "HPR",
			"MIC1LM", "Stereo Jack",
			"MIC1LP", "Stereo Jack",
			"L SPKR", "SPL",
			"R SPKR", "SPR";
		simple-audio-card,name = "phyBOARD-Rigel";
		simple-audio-card,format = "dsp_b";
		simple-audio-card,bitclock-master = <&sound_master>;
		simple-audio-card,frame-master = <&sound_master>;
		simple-audio-card,bitclock-inversion;

		simple-audio-card,cpu {
			sound-dai = <&mcasp0>;
		};

		sound_master: simple-audio-card,codec {
			sound-dai = <&audio_codec>;
			clocks = <&audio_refclk1>;
		};
	};
};

&main_pmx0 {
	audio_ext_refclk1_pins_default: audio-ext-refclk1-default-pins {
		pinctrl-single,pins = <
			J722S_IOPAD(0x0a0, PIN_OUTPUT, 1)	/* (N24) GPMC0_WPn.AUDIO_EXT_REFCLK1 */
		>;
	};

	gpio_exp0_int_pins_default: gpio-exp0-int-default-pins {
		pinctrl-single,pins = <
			J722S_IOPAD(0x0054, PIN_INPUT, 7)	/* (T21) GPMC0_AD6.GPIO0_21 */
		>;
	};

	gpio_exp1_int_pins_default: gpio-exp1-int-default-pins {
		pinctrl-single,pins = <
			J722S_IOPAD(0x0244, PIN_INPUT, 7)	/* (A24) MMC1_SDWP.GPIO1_49 */
		>;
	};

	gpio_exp2_int_pins_default: gpio-exp2-int-default-pins {
		pinctrl-single,pins = <
			J722S_IOPAD(0x0050, PIN_INPUT, 7)	/* (T24) GPMC0_AD5.GPIO0_20 */
		>;
	};

	gpio_keys_pins_default: gpio-keys-default-pins {
		pinctrl-single,pins = <
			J722S_IOPAD(0x01d4, PIN_INPUT, 7)	/* (B21) UART0_RTSn.GPIO1_23 */
		>;
	};

	main_i2c0_pins_default: main-i2c0-default-pins {
		pinctrl-single,pins = <
			J722S_IOPAD(0x01e0, PIN_INPUT_PULLUP, 0)	/* (D23) I2C0_SCL */
			J722S_IOPAD(0x01e4, PIN_INPUT_PULLUP, 0)	/* (B22) I2C0_SDA */
		>;
		bootph-all;
	};

	main_i2c1_pins_default: main-i2c1-default-pins {
		pinctrl-single,pins = <
			J722S_IOPAD(0x01e8, PIN_INPUT_PULLUP, 0)	/* (C24) I2C1_SCL */
			J722S_IOPAD(0x01ec, PIN_INPUT_PULLUP, 0)	/* (A22) I2C1_SDA */
		>;
		bootph-all;
	};

	main_mcan0_pins_default: main-mcan0-default-pins {
		pinctrl-single,pins = <
			J722S_IOPAD(0x1dc, PIN_INPUT, 0)	/* (C22) MCAN0_RX */
			J722S_IOPAD(0x1d8, PIN_OUTPUT, 0)	/* (D22) MCAN0_TX */
		>;
	};

	main_mcasp0_pins_default: main-mcasp0-default-pins {
		pinctrl-single,pins = <
			J722S_IOPAD(0x1a8, PIN_INPUT, 0)	/* (C26) MCASP0_AFSX */
			J722S_IOPAD(0x1a4, PIN_INPUT, 0)	/* (D25) MCASP0_ACLKX */
			J722S_IOPAD(0x198, PIN_OUTPUT, 0)	/* (A26) MCASP0_AXR2 */
			J722S_IOPAD(0x194, PIN_INPUT, 0)	/* (A25) MCASP0_AXR3 */
		>;
	};

	main_mmc1_pins_default: main-mmc1-default-pins {
		pinctrl-single,pins = <
			J722S_IOPAD(0x023c, PIN_INPUT, 0)	/* (H22) MMC1_CMD */
			J722S_IOPAD(0x0234, PIN_INPUT, 0)	/* (H24) MMC1_CLK */
			J722S_IOPAD(0x0230, PIN_INPUT, 0)	/* (H23) MMC1_DAT0 */
			J722S_IOPAD(0x022c, PIN_INPUT, 0)	/* (H20) MMC1_DAT1 */
			J722S_IOPAD(0x0228, PIN_INPUT, 0)	/* (J23) MMC1_DAT2 */
			J722S_IOPAD(0x0224, PIN_INPUT, 0)	/* (H25) MMC1_DAT3 */
			J722S_IOPAD(0x0240, PIN_INPUT, 0)	/* (B24) MMC1_SDCD */
		>;
		bootph-all;
	};

	main_uart0_pins_default: main-uart0-default-pins {
		pinctrl-single,pins = <
			J722S_IOPAD(0x01c8, PIN_INPUT, 0)	/* (F19) UART0_RXD */
			J722S_IOPAD(0x01cc, PIN_OUTPUT, 0)	/* (F20) UART0_TXD */
		>;
		bootph-all;
	};

	main_usbc_power_pins_default: main-usbc-power-default-pins {
		pinctrl-single,pins = <
			J722S_IOPAD(0x1b4, PIN_INPUT, 7)	/* (B20) SPI0_CS0.GPIO1_15 */
		>;
	};
};

&audio_refclk1 {
	assigned-clock-rates = <25000000>;
};

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

	veml6030: light-sensor@10 {
		compatible = "vishay,veml6030";
		reg = <0x10>;
		vdd-supply = <&vcc_3v3_sw>;
	};
};

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

	audio_codec: audio-codec@18 {
		compatible = "ti,tlv320aic3110";
		reg = <0x18>;
		pinctrl-names = "default";
		pinctrl-0 = <&audio_ext_refclk1_pins_default>;
		#sound-dai-cells = <0>;
		ai31xx-micbias-vg = <2>;
		reset-gpios = <&gpio_exp1 7 GPIO_ACTIVE_LOW>;

		HPVDD-supply = <&vcc_3v3_aud>;
		SPRVDD-supply = <&vcc_speaker>;
		SPLVDD-supply = <&vcc_speaker>;
		AVDD-supply = <&vcc_3v3_aud>;
		IOVDD-supply = <&vcc_3v3_aud>;
		DVDD-supply = <&vcc_1v8>;
	};

	gpio_exp0: gpio@20 {
		compatible = "nxp,pcf8574";
		reg = <0x20>;
		gpio-controller;
		#gpio-cells = <2>;
		pinctrl-names = "default";
		pinctrl-0 = <&gpio_exp0_int_pins_default>;
		interrupt-parent = <&main_gpio0>;
		interrupts = <21 IRQ_TYPE_EDGE_FALLING>;
		gpio-line-names = "CSI3_STROBE", "CSI3_TRIGGER",
				  "CSI3_SHUTTER", "CSI3_OE",
				  "CSI2_STROBE", "CSI2_TRIGGER",
				  "CSI2_SHUTTER", "CSI2_OE";
	};

	gpio_exp1: gpio@21 {
		compatible = "nxp,pcf8574";
		reg = <0x21>;
		gpio-controller;
		#gpio-cells = <2>;
		pinctrl-names = "default";
		pinctrl-0 = <&gpio_exp1_int_pins_default>;
		interrupt-parent = <&main_gpio1>;
		interrupts = <49 IRQ_TYPE_EDGE_FALLING>;
		gpio-line-names = "GPIO0_HDMI_RST", "GPIO1_CAN_nEN",
				  "GPIO2_LED", "GPIO3_MCU_CAN0_nEN",
				  "GPIO4_BUT2", "GPIO5_MCU_CAN1_nEN",
				  "GPIO6_AUDIO_GPIO", "GPIO7_AUDIO_USER_RESET";
	};

	gpio_exp2: gpio@23 {
		compatible = "nxp,pcf8574";
		reg = <0x23>;
		gpio-controller;
		#gpio-cells = <2>;
		pinctrl-names = "default";
		pinctrl-0 = <&gpio_exp2_int_pins_default>;
		interrupt-parent = <&main_gpio0>;
		interrupts = <20 IRQ_TYPE_EDGE_FALLING>;
		gpio-line-names = "CSI1_STROBE", "CSI1_TRIGGER",
				  "CSI1_SHUTTER", "CSI1_OE",
				  "CSI0_STROBE", "CSI0_TRIGGER",
				  "CSI0_SHUTTER", "CSI0_OE";
	};

	current-sensor@40 {
		compatible = "ti,ina233";
		reg = <0x40>;
		shunt-resistor = <18000>;
	};

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

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

&main_uart0 {
	pinctrl-names = "default";
	pinctrl-0 = <&main_uart0_pins_default>;
	bootph-all;
	status = "okay";
};

&mcasp0 {
	#sound-dai-cells = <0>;
	op-mode = <0>; /* MCASP_IIS_MODE */
	pinctrl-names = "default";
	pinctrl-0 = <&main_mcasp0_pins_default>;
	tdm-slots = <2>;
	serial-dir = < /* 0: INACTIVE, 1: TX, 2: RX */
	       0 0 1 2
	       0 0 0 0
	       0 0 0 0
	       0 0 0 0
	>;
	status = "okay";
};

&sdhci1 {
	/* SD/MMC */
	vmmc-supply = <&vcc_3v3_mmc>;
	vqmmc-supply = <&vddshv_sdio>;
	pinctrl-names = "default";
	pinctrl-0 = <&main_mmc1_pins_default>;
	disable-wp;
	no-1-8-v;
	bootph-all;
	status = "okay";
};

&serdes_ln_ctrl {
	idle-states = <J722S_SERDES0_LANE0_USB>,
		      <J722S_SERDES1_LANE0_PCIE0_LANE0>;
};

&serdes0 {
	status = "okay";

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

&serdes_wiz0 {
	status = "okay";
};

&usbss0 {
	ti,vbus-divider;
	status = "okay";
};

&usb0 {
	dr_mode = "otg";
	usb-role-switch;
	maximum-speed = "high-speed";

	port {
		usb0_ep: endpoint {
			remote-endpoint = <&usb0_con>;
		};
	};
};

&usbss1 {
	ti,vbus-divider;
	status = "okay";
};

&usb1 {
	dr_mode = "host";
	phys = <&serdes0_usb_link>;
	phy-names = "cdns3,usb3-phy";
	maximum-speed = "super-speed";
};