// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2023 Neil Armstrong <neil.armstrong@linaro.org>
 */

#include "meson-g12b-a311d.dtsi"
#include <dt-bindings/gpio/meson-g12a-gpio.h>

/ {
	aliases {
		serial0 = &uart_AO;
		rtc1 = &vrtc;
	};

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

	emmc_pwrseq: emmc-pwrseq {
		compatible = "mmc-pwrseq-emmc";
		reset-gpios = <&gpio BOOT_12 GPIO_ACTIVE_LOW>;
	};

	memory@0 {
		device_type = "memory";
		reg = <0x0 0x0 0x0 0x40000000>;
	};

	sdio_pwrseq: sdio-pwrseq {
		compatible = "mmc-pwrseq-simple";
		reset-gpios = <&gpio GPIOAO_6 GPIO_ACTIVE_LOW>;
		clocks = <&wifi32k>;
		clock-names = "ext_clock";
	};

	emmc_1v8: regulator-emmc-1v8 {
		compatible = "regulator-fixed";
		regulator-name = "EMMC_1V8";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		vin-supply = <&vddao_3v3>;
		regulator-always-on;
	};

	dc_in: regulator-dc-in {
		compatible = "regulator-fixed";
		regulator-name = "DC_IN";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		regulator-always-on;
	};

	vddio_c: regulator-vddio-c {
		compatible = "regulator-gpio";
		regulator-name = "VDDIO_C";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <3300000>;

		enable-gpios = <&gpio_ao GPIOAO_3 GPIO_OPEN_DRAIN>;
		enable-active-high;
		regulator-always-on;

		gpios = <&gpio_ao GPIOAO_9 GPIO_OPEN_DRAIN>;
		gpios-states = <1>;

		states = <1800000 0>,
			 <3300000 1>;
	};

	vddao_1v8: regulator-vddao-1v8 {
		compatible = "regulator-fixed";
		regulator-name = "VDDAO_1V8";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		vin-supply = <&vddao_3v3>;
		regulator-always-on;
	};

	vddao_3v3: regulator-vddao-3v3 {
		compatible = "regulator-fixed";
		regulator-name = "VDDAO_3V3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&dc_in>;
		regulator-always-on;
	};

	vddcpu_a: regulator-vddcpu-a {
		/*
		 * MP8756GD DC/DC Regulator.
		 */
		compatible = "pwm-regulator";

		regulator-name = "VDDCPU_A";
		regulator-min-microvolt = <680000>;
		regulator-max-microvolt = <1040000>;

		pwm-supply = <&dc_in>;

		pwms = <&pwm_ab 0 1250 0>;
		pwm-dutycycle-range = <100 0>;

		regulator-boot-on;
		regulator-always-on;
	};

	vddcpu_b: regulator-vddcpu-b {
		/*
		 * SY8120B1ABC DC/DC Regulator.
		 */
		compatible = "pwm-regulator";

		regulator-name = "VDDCPU_B";
		regulator-min-microvolt = <680000>;
		regulator-max-microvolt = <1040000>;

		pwm-supply = <&dc_in>;

		pwms = <&pwm_AO_cd 1 1250 0>;
		pwm-dutycycle-range = <100 0>;

		regulator-boot-on;
		regulator-always-on;
	};

	wifi32k: wifi32k {
		compatible = "pwm-clock";
		#clock-cells = <0>;
		clock-frequency = <32768>;
		pwms = <&pwm_ef 0 30518 0>; /* PWM_E at 32.768KHz */
	};
};

&arb {
	status = "okay";
};

&clkc_audio {
	status = "okay";
};

&cec_AO {
	pinctrl-0 = <&cec_ao_a_h_pins>;
	pinctrl-names = "default";
	hdmi-phandle = <&hdmi_tx>;
};

&cecb_AO {
	pinctrl-0 = <&cec_ao_b_h_pins>;
	pinctrl-names = "default";
	hdmi-phandle = <&hdmi_tx>;
};

&cpu0 {
	cpu-supply = <&vddcpu_b>;
	operating-points-v2 = <&cpu_opp_table_0>;
	clocks = <&clkc CLKID_CPU_CLK>;
	clock-latency = <50000>;
};

&cpu1 {
	cpu-supply = <&vddcpu_b>;
	operating-points-v2 = <&cpu_opp_table_0>;
	clocks = <&clkc CLKID_CPU_CLK>;
	clock-latency = <50000>;
};

&cpu100 {
	cpu-supply = <&vddcpu_a>;
	operating-points-v2 = <&cpub_opp_table_1>;
	clocks = <&clkc CLKID_CPUB_CLK>;
	clock-latency = <50000>;
};

&cpu101 {
	cpu-supply = <&vddcpu_a>;
	operating-points-v2 = <&cpub_opp_table_1>;
	clocks = <&clkc CLKID_CPUB_CLK>;
	clock-latency = <50000>;
};

&cpu102 {
	cpu-supply = <&vddcpu_a>;
	operating-points-v2 = <&cpub_opp_table_1>;
	clocks = <&clkc CLKID_CPUB_CLK>;
	clock-latency = <50000>;
};

&cpu103 {
	cpu-supply = <&vddcpu_a>;
	operating-points-v2 = <&cpub_opp_table_1>;
	clocks = <&clkc CLKID_CPUB_CLK>;
	clock-latency = <50000>;
};

&ext_mdio {
	external_phy: ethernet-phy@0 {
		/* Realtek RTL8211F (0x001cc916) */
		reg = <0>;
		max-speed = <1000>;

		interrupt-parent = <&gpio_intc>;
		/* MAC_INTR on GPIOZ_14 */
		interrupts = <IRQID_GPIOZ_14 IRQ_TYPE_LEVEL_LOW>;
	};
};

/* Ethernet to be enabled in baseboard DT */
&ethmac {
	pinctrl-0 = <&eth_pins>, <&eth_rgmii_pins>;
	pinctrl-names = "default";
	phy-mode = "rgmii-txid";
	phy-handle = <&external_phy>;
};

&frddr_a {
	status = "okay";
};

&frddr_b {
	status = "okay";
};

&frddr_c {
	status = "okay";
};

/* HDMI to be enabled in baseboard DT */
&hdmi_tx {
	pinctrl-0 = <&hdmitx_hpd_pins>, <&hdmitx_ddc_pins>;
	pinctrl-names = "default";
	hdmi-supply = <&dc_in>;
};

/* "Camera" I2C bus */
&i2c1 {
	pinctrl-0 = <&i2c1_sda_h6_pins>, <&i2c1_sck_h7_pins>;
	pinctrl-names = "default";
};

/* Main I2C bus */
&i2c2 {
	pinctrl-0 = <&i2c2_sda_x_pins>, <&i2c2_sck_x_pins>;
	pinctrl-names = "default";
};

/* "ID" I2C bus */
&i2c3 {
	pinctrl-0 = <&i2c3_sda_a_pins>, <&i2c3_sck_a_pins>;
	pinctrl-names = "default";
};

&pcie {
	reset-gpios = <&gpio GPIOA_8 GPIO_ACTIVE_LOW>;
};

&pwm_ab {
	pinctrl-0 = <&pwm_a_e_pins>;
	pinctrl-names = "default";
	clocks = <&xtal>;
	clock-names = "clkin0";

	status = "okay";
};

&pwm_ef {
	pinctrl-0 = <&pwm_e_pins>;
	pinctrl-names = "default";

	status = "okay";
};

&pwm_AO_cd {
	pinctrl-0 = <&pwm_ao_d_e_pins>;
	pinctrl-names = "default";
	clocks = <&xtal>;
	clock-names = "clkin1";

	status = "okay";
};

&saradc {
	vref-supply = <&vddao_1v8>;

	status = "okay";
};

/* on-module SDIO WiFi */
&sd_emmc_a {
	pinctrl-0 = <&sdio_pins>;
	pinctrl-1 = <&sdio_clk_gate_pins>;
	pinctrl-names = "default", "clk-gate";
	#address-cells = <1>;
	#size-cells = <0>;

	bus-width = <4>;
	sd-uhs-sdr104;
	max-frequency = <50000000>;

	non-removable;
	disable-wp;

	/* WiFi firmware requires power in suspend */
	keep-power-in-suspend;

	mmc-pwrseq = <&sdio_pwrseq>;

	vmmc-supply = <&vddao_3v3>;
	vqmmc-supply = <&vddao_3v3>;

	status = "okay";

	rtl8822cs: wifi@1 {
		reg = <1>;
	};
};

/* SD card to be enabled in baseboard DT */
&sd_emmc_b {
	pinctrl-0 = <&sdcard_c_pins>;
	pinctrl-1 = <&sdcard_clk_gate_c_pins>;
	pinctrl-names = "default", "clk-gate";

	bus-width = <4>;
	cap-sd-highspeed;
	max-frequency = <50000000>;
	disable-wp;

	cd-gpios = <&gpio GPIOC_6 GPIO_ACTIVE_LOW>;
	vmmc-supply = <&vddao_3v3>;
	vqmmc-supply = <&vddio_c>;
};

/* on-module eMMC */
&sd_emmc_c {
	pinctrl-0 = <&emmc_ctrl_pins>, <&emmc_data_8b_pins>, <&emmc_ds_pins>;
	pinctrl-1 = <&emmc_clk_gate_pins>;
	pinctrl-names = "default", "clk-gate";

	bus-width = <8>;
	cap-mmc-highspeed;
	mmc-ddr-1_8v;
	mmc-hs200-1_8v;
	max-frequency = <200000000>;
	disable-wp;

	mmc-pwrseq = <&emmc_pwrseq>;
	vmmc-supply = <&vddao_3v3>;
	vqmmc-supply = <&vddao_1v8>;

	status = "okay";
};

&tdmif_b {
	status = "okay";
};

&tdmout_b {
	status = "okay";
};

/* on-module UART BT */
&uart_A {
	pinctrl-0 = <&uart_a_pins>, <&uart_a_cts_rts_pins>;
	pinctrl-names = "default";
	uart-has-rtscts;

	status = "okay";

	bluetooth {
		compatible = "realtek,rtl8822cs-bt";
		enable-gpios  = <&gpio GPIOX_17 GPIO_ACTIVE_HIGH>;
		host-wake-gpios = <&gpio GPIOX_19 GPIO_ACTIVE_HIGH>;
		device-wake-gpios = <&gpio GPIOX_18 GPIO_ACTIVE_HIGH>;
	};
};

&uart_AO {
	pinctrl-0 = <&uart_ao_a_pins>;
	pinctrl-names = "default";

	status = "okay";
};

&usb {
	phys = <&usb2_phy0>, <&usb2_phy1>;
	phy-names = "usb2-phy0", "usb2-phy1";
};