// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
// Copyright (C) 2019 Ondřej Jirman <megous@megous.com>

/dts-v1/;

#include "sun50i-h6.dtsi"
#include "sun50i-h6-cpu-opp.dtsi"

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

/ {
	model = "OrangePi 3";
	compatible = "xunlong,orangepi-3", "allwinner,sun50i-h6";

	aliases {
		serial0 = &uart0;
		serial1 = &uart1;
	};

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

	connector {
		compatible = "hdmi-connector";
		ddc-en-gpios = <&pio 7 2 GPIO_ACTIVE_HIGH>; /* PH2 */
		type = "a";

		port {
			hdmi_con_in: endpoint {
				remote-endpoint = <&hdmi_out_con>;
			};
		};
	};

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

	leds {
		compatible = "gpio-leds";

		led-0 {
			label = "orangepi:red:power";
			gpios = <&r_pio 0 4 GPIO_ACTIVE_HIGH>; /* PL4 */
			default-state = "on";
		};

		led-1 {
			label = "orangepi:green:status";
			gpios = <&r_pio 0 7 GPIO_ACTIVE_HIGH>; /* PL7 */
		};
	};

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

	reg_vcc33_wifi: vcc33-wifi {
		/* Always on 3.3V regulator for WiFi and BT */
		compatible = "regulator-fixed";
		regulator-name = "vcc33-wifi";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-always-on;
		vin-supply = <&reg_vcc5v>;
	};

	reg_vcc_wifi_io: vcc-wifi-io {
		/* Always on 1.8V/300mA regulator for WiFi and BT IO */
		compatible = "regulator-fixed";
		regulator-name = "vcc-wifi-io";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		regulator-always-on;
		vin-supply = <&reg_vcc33_wifi>;
	};

	wifi_pwrseq: wifi-pwrseq {
		compatible = "mmc-pwrseq-simple";
		clocks = <&rtc CLK_OSC32K_FANOUT>;
		clock-names = "ext_clock";
		reset-gpios = <&r_pio 1 3 GPIO_ACTIVE_LOW>; /* PM3 */
		post-power-on-delay-ms = <200>;
	};
};

&cpu0 {
	cpu-supply = <&reg_dcdca>;
};

&de {
	status = "okay";
};

&dwc3 {
	status = "okay";
};

&ehci0 {
	status = "okay";
};

&ehci3 {
	status = "okay";
};

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

&hdmi {
	status = "okay";
};

&hdmi_out {
	hdmi_out_con: endpoint {
		remote-endpoint = <&hdmi_con_in>;
	};
};

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

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

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

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

&ohci0 {
	status = "okay";
};

&ohci3 {
	status = "okay";
};

&pio {
	vcc-pc-supply = <&reg_bldo2>;
	vcc-pd-supply = <&reg_cldo1>;
	vcc-pg-supply = <&reg_vcc_wifi_io>;
};

&r_ir {
	status = "okay";
};

&r_rsb {
	status = "okay";

	axp805: pmic@745 {
		compatible = "x-powers,axp805", "x-powers,axp806";
		reg = <0x745>;
		interrupt-parent = <&r_intc>;
		interrupts = <GIC_SPI 96 IRQ_TYPE_LEVEL_LOW>;
		interrupt-controller;
		#interrupt-cells = <1>;
		x-powers,self-working-mode;
		vina-supply = <&reg_vcc5v>;
		vinb-supply = <&reg_vcc5v>;
		vinc-supply = <&reg_vcc5v>;
		vind-supply = <&reg_vcc5v>;
		vine-supply = <&reg_vcc5v>;
		aldoin-supply = <&reg_vcc5v>;
		bldoin-supply = <&reg_vcc5v>;
		cldoin-supply = <&reg_vcc5v>;

		regulators {
			reg_aldo1: aldo1 {
				regulator-always-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc-pl-led-ir";
			};

			reg_aldo2: aldo2 {
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc33-audio-tv-ephy-mac";
			};

			/* ALDO3 is shorted to CLDO1 */
			reg_aldo3: aldo3 {
				regulator-always-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc33-io-pd-emmc-sd-usb-uart-1";
			};

			reg_bldo1: bldo1 {
				regulator-always-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-name = "vcc18-dram-bias-pll";
			};

			reg_bldo2: bldo2 {
				regulator-always-on;
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-name = "vcc-efuse-pcie-hdmi-pc";
			};

			bldo3 {
				/* unused */
			};

			bldo4 {
				/* unused */
			};

			reg_cldo1: cldo1 {
				regulator-always-on;
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-name = "vcc33-io-pd-emmc-sd-usb-uart-2";
			};

			cldo2 {
				/* unused */
			};

			cldo3 {
				/* unused */
			};

			reg_dcdca: dcdca {
				regulator-always-on;
				regulator-min-microvolt = <800000>;
				regulator-max-microvolt = <1160000>;
				regulator-ramp-delay = <2500>;
				regulator-name = "vdd-cpu";
			};

			reg_dcdcc: dcdcc {
				regulator-enable-ramp-delay = <32000>;
				regulator-min-microvolt = <810000>;
				regulator-max-microvolt = <1080000>;
				regulator-ramp-delay = <2500>;
				regulator-name = "vdd-gpu";
			};

			reg_dcdcd: dcdcd {
				regulator-always-on;
				regulator-min-microvolt = <960000>;
				regulator-max-microvolt = <960000>;
				regulator-name = "vdd-sys";
			};

			reg_dcdce: dcdce {
				regulator-always-on;
				regulator-min-microvolt = <1200000>;
				regulator-max-microvolt = <1200000>;
				regulator-name = "vcc-dram";
			};

			sw {
				/* unused */
			};
		};
	};
};

&rtc {
	clocks = <&ext_osc32k>;
};

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

/* There's the BT part of the AP6256 connected to that UART */
&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";
		device-wakeup-gpios = <&r_pio 1 2 GPIO_ACTIVE_HIGH>; /* PM2 */
		host-wakeup-gpios = <&r_pio 1 1 GPIO_ACTIVE_HIGH>; /* PM1 */
		shutdown-gpios = <&r_pio 1 4 GPIO_ACTIVE_HIGH>; /* PM4 */
		max-speed = <1500000>;
	};
};

&usb2otg {
	/*
	 * This board doesn't have a controllable VBUS even though it
	 * does have an ID pin. Using it as anything but a USB host is
	 * unsafe.
	 */
	dr_mode = "host";
	status = "okay";
};

&usb2phy {
	usb0_id_det-gpios = <&pio 2 15 GPIO_ACTIVE_HIGH>; /* PC15 */
	usb0_vbus-supply = <&reg_vcc5v>;
	usb3_vbus-supply = <&reg_vcc5v>;
	status = "okay";
};

&usb3phy {
	status = "okay";
};