// SPDX-License-Identifier: (GPL-2.0-only OR MIT)
// Copyright (C) 2024 Arm Ltd.

/dts-v1/;

#include "sun55i-a523.dtsi"

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

/ {
	model = "X96Q Pro+";
	compatible = "amediatech,x96q-pro-plus", "allwinner,sun55i-h728";

	aliases {
		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";
	};

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

	reg_vcc3v3: vcc3v3 {
		/* 3.3V dummy supply for the SD card */
		compatible = "regulator-fixed";
		regulator-name = "vcc-3v3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&reg_vcc5v>;
		regulator-always-on;
	};

	sound-spdif {
		compatible = "simple-audio-card";
		simple-audio-card,name = "spdif-out";

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

		simple-audio-card,codec {
			sound-dai = <&spdif_out>;
		};
	};

	spdif_out: spdif-out {
		compatible = "linux,spdif-dit";
		#sound-dai-cells = <0>;
	};
};

&ehci0 {
	status = "okay";
};

&ehci1 {
	status = "okay";
};

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

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

&mmc2 {
	vmmc-supply = <&reg_cldo3>;
	vqmmc-supply = <&reg_cldo1>;
	bus-width = <8>;
	non-removable;
	cap-mmc-hw-reset;
	mmc-ddr-1_8v;
	mmc-hs200-1_8v;
	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_dcdc4>;
	vcc-pe-supply = <&reg_dcdc4>;
	vcc-pf-supply = <&reg_cldo3>;	/* actually switchable */
	vcc-pg-supply = <&reg_bldo1>;
	vcc-ph-supply = <&reg_cldo3>;	/* via VCC-IO */
	vcc-pi-supply = <&reg_dcdc4>;
	vcc-pj-supply = <&reg_dcdc4>;
	vcc-pk-supply = <&reg_bldo3>;
};

&r_i2c0 {
	status = "okay";

	axp717: pmic@34 {
		compatible = "x-powers,axp717";
		reg = <0x34>;
		interrupt-controller;
		#interrupt-cells = <1>;
		interrupt-parent = <&nmi_intc>;
		interrupts = <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>;

		regulators {
			/* Supplies the "little" cluster (1.0(?) 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 = <1360000>;
				regulator-max-microvolt = <1360000>;
				regulator-name = "vdd-dram";
			};

			reg_dcdc4: dcdc4 {
				regulator-min-microvolt = <1000000>;
				regulator-max-microvolt = <1000000>;
				regulator-name = "vdd-dcdc4";
			};

			reg_aldo1: aldo1 {
				/* not connected */
			};

			reg_aldo2: aldo2 {
				/* not connected */
			};

			reg_aldo3: aldo3 {
				regulator-always-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc-aldo3";
			};

			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-lvds";
			};

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

			reg_bldo3: bldo3 {
				regulator-min-microvolt = <2800000>;
				regulator-max-microvolt = <2800000>;
				regulator-name = "vcc-bldo3";
			};

			reg_bldo4: bldo4 {
				/* not connected */
			};

			reg_cldo1: cldo1 {
				regulator-always-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-name = "vcc-codec-sd";
			};

			reg_cldo2: cldo2 {
			};

			reg_cldo3: cldo3 {
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc-codec-eth-sd";
			};

			reg_cldo4: cldo4 {
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc-eth-phy";
			};

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

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

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

		regulators {
			aldo1 {
				/* not connected */
			};

			dldo1 {
				/* not connected */
			};

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

			/* DCDC2 is polyphased with DCDC1 */

			reg_dcdc3_323: dcdc3 {
				regulator-always-on;
				regulator-min-microvolt = <1050000>;
				regulator-max-microvolt = <1050000>;
				regulator-name = "vdd-dcdc3";
			};
		};
	};
};

&r_ir {
	pinctrl-names = "default";
	pinctrl-0 = <&r_ir_rx_pin>;
	status = "okay";
};

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

&spdif {
	pinctrl-names = "default";
	pinctrl-0 = <&spdif_out_ph_pin>;
	status = "okay";
};

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

&usb_otg {
	/* USB0 is a USB-A receptacle, always powered, so force host mode. */
	dr_mode = "host";
	status = "okay";
};

&usbphy {
	status = "okay";
};