// SPDX-License-Identifier: (GPL-2.0-only OR MIT)
/*
 * Copyright (C) 2025 Chen-Yu Tsai <wens@csie.org>
 */

/dts-v1/;

#include "sun55i-a523.dtsi"

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/leds/common.h>

/ {
	model = "OrangePi 4A";
	compatible = "xunlong,orangepi-4a", "allwinner,sun55i-t527";

	aliases {
		ethernet0 = &gmac1;
		serial0 = &uart0;
	};

	chosen {
		stdout-path = "serial0:115200n8";
	};

	ext_osc32k: ext-osc32k-clk {
		#clock-cells = <0>;
		compatible = "fixed-clock";
		clock-frequency = <32768>;
		clock-output-names = "ext_osc32k";
	};

	leds {
		compatible = "gpio-leds";

		/* PWM capable pin, but PWM isn't supported yet. */
		led {
			function = LED_FUNCTION_STATUS;
			color = <LED_COLOR_ID_GREEN>;
			gpios = <&pio 3 20 GPIO_ACTIVE_HIGH>; /* PD20 */
		};
	};

	iio-hwmon {
		compatible = "iio-hwmon";
		io-channels = <&axp717_adc 3>, /* vsys_v */
			      <&axp717_adc 4>, /* pmic_temp */
			      <&axp717_adc 7>; /* bkup_batt_v */
	};

	wifi_pwrseq: pwrseq {
		compatible = "mmc-pwrseq-simple";
		reset-gpios = <&r_pio 1 1 GPIO_ACTIVE_LOW>; /* PM1 */
		clocks = <&rtc CLK_OSC32K_FANOUT>;
		clock-names = "ext_clock";
	};

	reg_otg_vbus: regulator-otg-vbus {
		compatible = "regulator-fixed";
		regulator-name = "otg-vbus";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&reg_vcc5v>;
		gpio = <&r_pio 0 4 GPIO_ACTIVE_HIGH>;	/* PL4 */
		enable-active-high;
	};

	reg_pcie_vcc3v3: regulator-pcie-vcc3v3 {
		compatible = "regulator-fixed";
		regulator-name = "vcc-pcie-3v3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&reg_vcc5v>;
		gpio = <&r_pio 0 8 GPIO_ACTIVE_HIGH>;	/* PL8 */
		enable-active-high;
	};

	reg_usb_vbus: regulator-usb-vbus {
		compatible = "regulator-fixed";
		regulator-name = "usb-vbus";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&reg_vcc5v>;
		gpio = <&r_pio 0 12 GPIO_ACTIVE_HIGH>;	/* PL12 */
		enable-active-high;
	};

	reg_vcc5v: regulator-vcc5v {
		/* board wide 5V supply from USB type-C port */
		compatible = "regulator-fixed";
		regulator-name = "vcc-5v";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		regulator-always-on;
	};
};

&ehci0 {
	status = "okay";
};

&ehci1 {
	status = "okay";
};

&gmac1 {
	phy-mode = "rgmii-id";
	phy-handle = <&ext_rgmii_phy>;
	phy-supply = <&reg_cldo4>;

	tx-internal-delay-ps = <0>;
	rx-internal-delay-ps = <300>;

	status = "okay";
};

&gpu {
	mali-supply = <&reg_dcdc2>;
	status = "okay";
};

&mdio1 {
	ext_rgmii_phy: ethernet-phy@1 {
		compatible = "ethernet-phy-ieee802.3-c22";
		reg = <1>;
		interrupts-extended = <&pio 8 16 IRQ_TYPE_LEVEL_LOW>; /* PI16 */
		reset-gpios = <&pio 8 15 GPIO_ACTIVE_LOW>; /* PI15 */
		reset-assert-us = <10000>;
		reset-deassert-us = <150000>;
	};
};

&mmc0 {
	vmmc-supply = <&reg_cldo3>;
	cd-gpios = <&pio 5 6 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>; /* PF6 */
	bus-width = <4>;
	status = "okay";
};

&mmc1 {
	bus-width = <4>;
	mmc-pwrseq = <&wifi_pwrseq>;
	non-removable;
	vmmc-supply = <&reg_dldo1_323>;
	vqmmc-supply = <&reg_bldo1>;
	status = "okay";

	brcmf: wifi@1 {
		compatible = "brcm,bcm4329-fmac";
		reg = <1>;
		interrupt-parent = <&r_pio>;
		interrupts = <1 0 IRQ_TYPE_LEVEL_LOW>; /* PM0 */
		interrupt-names = "host-wake";
	};
};

&mmc2 {
	bus-width = <8>;
	cap-mmc-hw-reset;
	mmc-ddr-1_8v;
	mmc-hs200-1_8v;
	non-removable;
	vmmc-supply = <&reg_cldo3>;
	vqmmc-supply = <&reg_cldo1>;
	status = "okay";
};

&ohci0 {
	status = "okay";
};

&ohci1 {
	status = "okay";
};

&pio {
	vcc-pb-supply = <&reg_cldo3>;	/* via VCC-IO */
	vcc-pc-supply = <&reg_cldo1>;
	vcc-pd-supply = <&reg_cldo3>;
	vcc-pe-supply = <&reg_aldo2>;
	vcc-pf-supply = <&reg_cldo3>;	/* VCC-IO for 3.3v; VCC-MCSI for 1.8v */
	vcc-pg-supply = <&reg_bldo1>;
	vcc-ph-supply = <&reg_cldo3>;	/* via VCC-IO */
	vcc-pi-supply = <&reg_cldo3>;
	vcc-pj-supply = <&reg_cldo1>;
	vcc-pk-supply = <&reg_cldo1>;
};

&r_i2c0 {
	status = "okay";

	axp717: pmic@35 {
		compatible = "x-powers,axp717";
		reg = <0x35>;
		interrupt-controller;
		#interrupt-cells = <1>;
		interrupts-extended = <&nmi_intc 0 IRQ_TYPE_LEVEL_LOW>;

		vin1-supply = <&reg_vcc5v>;
		vin2-supply = <&reg_vcc5v>;
		vin3-supply = <&reg_vcc5v>;
		vin4-supply = <&reg_vcc5v>;
		aldoin-supply = <&reg_vcc5v>;
		bldoin-supply = <&reg_vcc5v>;
		cldoin-supply = <&reg_vcc5v>;

		axp717_adc: adc {
			compatible = "x-powers,axp717-adc";
			#io-channel-cells = <1>;
		};

		battery-power {
			compatible = "x-powers,axp717-battery-power-supply";
			/* no battery; output used for dcdc4 instead */
			status = "disabled";
		};

		regulators {
			/* Supplies the "little" cluster (1.4 GHz cores) */
			reg_dcdc1: dcdc1 {
				regulator-always-on;
				regulator-min-microvolt = <900000>;
				regulator-max-microvolt = <1160000>;
				regulator-name = "vdd-cpul";
			};

			reg_dcdc2: dcdc2 {
				regulator-always-on;
				regulator-min-microvolt = <920000>;
				regulator-max-microvolt = <920000>;
				regulator-name = "vdd-gpu-sys";
			};

			reg_dcdc3: dcdc3 {
				regulator-always-on;
				regulator-min-microvolt = <1160000>;
				regulator-max-microvolt = <1160000>;
				regulator-name = "vcc-dram";
			};

			reg_dcdc4: dcdc4 {
				/* feeds 3.3V pin on GPIO header */
				regulator-always-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vdd-io";
			};

			aldo1 {
				/* not actually connected */
				regulator-name = "avdd-csi";
			};

			reg_aldo2: aldo2 {
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-name = "vcc-pe";
			};

			reg_aldo3: aldo3 {
				/* supplies the I2C pins for this PMIC */
				regulator-always-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc-pl-usb";
			};

			reg_aldo4: aldo4 {
				regulator-always-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-name = "vcc-pll-dxco-avcc";
			};

			reg_bldo1: bldo1 {
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-name = "vcc-pg-wifi";
			};

			reg_bldo2: bldo2 {
				regulator-always-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-name = "vcc-pm-lpddr";
			};

			bldo3 {
				/* not actually connected */
				regulator-name = "afvcc-csi";
			};

			bldo4 {
				/* not actually connected */
				regulator-name = "dvdd-csi";
			};

			reg_cldo1: cldo1 {
				regulator-always-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-name = "vcc-cvp-pc-lvds-mcsi-pk-efuse-pcie-edp-1v8";
			};

			reg_cldo2: cldo2 {
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc3v3-csi";
			};

			reg_cldo3: cldo3 {
				regulator-always-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc-io-mmc-nand-pd-pi-usb";
			};

			reg_cldo4: cldo4 {
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc-3v3-phy1-lcd";
			};

			reg_cpusldo: cpusldo {
				/* supplies the management core */
				regulator-always-on;
				regulator-min-microvolt = <900000>;
				regulator-max-microvolt = <900000>;
				regulator-name = "vdd-cpus-usb-0v9";
			};
		};

		usb-power {
			compatible = "x-powers,axp717-usb-power-supply";
			input-current-limit-microamp = <3000000>;
		};
	};

	axp323: pmic@36 {
		compatible = "x-powers,axp323";
		reg = <0x36>;
		#interrupt-cells = <1>;
		interrupt-controller;
		status = "okay";

		vin1-supply = <&reg_vcc5v>;
		vin2-supply = <&reg_vcc5v>;
		vin3-supply = <&reg_vcc5v>;

		regulators {
			reg_aldo1_323: aldo1 {
				/* less capable and shares load with dldo1 */
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc-wifi";
			};

			reg_dldo1_323: dldo1 {
				/* more capable and shares load with aldo1 */
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc-wifi2";
			};

			/* Supplies the "big" cluster (1.8 GHz cores) */
			reg_dcdc1_323: dcdc1 {
				regulator-always-on;
				regulator-min-microvolt = <900000>;
				regulator-max-microvolt = <1150000>;
				regulator-name = "vdd-cpub";
			};

			/* DCDC2 is polyphased with DCDC1 */

			/* Some RISC-V management core related voltage */
			reg_dcdc3_323: dcdc3 {
				regulator-always-on;
				regulator-min-microvolt = <900000>;
				regulator-max-microvolt = <900000>;
				regulator-name = "vdd-dnr";
			};
		};
	};
};

&r_pio {
/*
 * Specifying the supply would create a circular dependency.
 *
 *	vcc-pl-supply = <&reg_aldo3>;
 */
	vcc-pm-supply = <&reg_bldo2>;
};

&rtc {
	clocks = <&r_ccu CLK_BUS_R_RTC>, <&osc24M>,
		 <&r_ccu CLK_R_AHB>, <&ext_osc32k>;
	clock-names = "bus", "hosc", "ahb", "ext-osc32k";
	assigned-clocks = <&rtc CLK_OSC32K>;
	assigned-clock-rates = <32768>;
};

&spi0 {
	pinctrl-names = "default";
	pinctrl-0 = <&spi0_pc_pins>, <&spi0_cs0_pc_pin>;
	status = "okay";

	flash@0 {
		#address-cells = <1>;
		#size-cells = <1>;
		compatible = "jedec,spi-nor";
		reg = <0>;
		spi-max-frequency = <20000000>;
		vcc-supply = <&reg_cldo1>;
	};
};

&uart0 {
	pinctrl-names = "default";
	pinctrl-0 = <&uart0_pb_pins>;
	status = "okay";
};

&uart1 {
	pinctrl-names = "default";
	pinctrl-0 = <&uart1_pins>, <&uart1_rts_cts_pins>;
	uart-has-rtscts;
	status = "okay";

	bluetooth {
		compatible = "brcm,bcm4345c5";
		clocks = <&rtc CLK_OSC32K_FANOUT>;
		clock-names = "lpo";
		vbat-supply = <&reg_aldo1_323>;
		vddio-supply = <&reg_bldo1>;
		device-wakeup-gpios = <&r_pio 1 3 GPIO_ACTIVE_HIGH>; /* PM3 */
		host-wakeup-gpios = <&r_pio 1 4 GPIO_ACTIVE_HIGH>; /* PM4 */
		shutdown-gpios = <&r_pio 1 2 GPIO_ACTIVE_HIGH>; /* PM2 */
	};
};

&usb_otg {
	/*
	 * The OTG controller is connected to one of the type-A ports.
	 * There is a regulator, controlled by a GPIO, to provide VBUS power
	 * to the port, and a VBUSDET GPIO, to detect externally provided
	 * power. But without ID or CC pins there is no real way to do a
	 * runtime role detection.
	 */
	dr_mode = "host";
	status = "okay";
};

&usbphy {
	usb0_vbus-supply = <&reg_otg_vbus>;
	usb0_vbus_det-gpios = <&r_pio 0 7 GPIO_ACTIVE_HIGH>; /* PL7 */
	usb1_vbus-supply = <&reg_usb_vbus>;
	status = "okay";
};