// SPDX-License-Identifier: (GPL-2.0+ or MIT)
// Copyright (C) 2021-2022 Samuel Holland <samuel@sholland.org>

/*
 * gpio line names
 *
 * The Nezha-D1 has a 40-pin IO header. Some of these pins are routed
 * directly to pads on the SoC, others come from an 8-bit pcf857x IO
 * expander. Therefore, these line names are specified in two places:
 * one set for the pcf857x, and one set for the pio controller.
 *
 * Lines which are routed to the 40-pin header are named as follows:
 *	<pin#> [<pin name>]
 * where:
 *	<pin#>		is the actual pin number of the 40-pin header
 *	<pin name>	is the name of the pin by function/gpio#
 *
 * For details regarding pin numbers and names see the schematics (under
 * "IO EXPAND"):
 * http://dl.linux-sunxi.org/D1/D1_Nezha_development_board_schematic_diagram_20210224.pdf
 */

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

/dts-v1/;

#include "sun20i-d1.dtsi"
#include "sun20i-common-regulators.dtsi"

/ {
	model = "Allwinner D1 Nezha";
	compatible = "allwinner,d1-nezha", "allwinner,sun20i-d1";

	aliases {
		ethernet0 = &emac;
		ethernet1 = &xr829;
		serial0 = &uart0;
	};

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

	reg_usbvbus: usbvbus {
		compatible = "regulator-fixed";
		regulator-name = "usbvbus";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		gpio = <&pio 3 19 GPIO_ACTIVE_HIGH>; /* PD19 */
		enable-active-high;
		vin-supply = <&reg_vcc>;
	};

	/*
	 * This regulator is PWM-controlled, but the PWM controller is not
	 * yet supported, so fix the regulator to its default voltage.
	 */
	reg_vdd_cpu: vdd-cpu {
		compatible = "regulator-fixed";
		regulator-name = "vdd-cpu";
		regulator-min-microvolt = <1100000>;
		regulator-max-microvolt = <1100000>;
		vin-supply = <&reg_vcc>;
	};

	wifi_pwrseq: wifi-pwrseq {
		compatible = "mmc-pwrseq-simple";
		reset-gpios = <&pio 6 12 GPIO_ACTIVE_LOW>; /* PG12 */
	};
};

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

&dcxo {
	clock-frequency = <24000000>;
};

&ehci0 {
	status = "okay";
};

&ehci1 {
	status = "okay";
};

&emac {
	pinctrl-0 = <&rgmii_pe_pins>;
	pinctrl-names = "default";
	phy-handle = <&ext_rgmii_phy>;
	phy-mode = "rgmii-id";
	phy-supply = <&reg_vcc_3v3>;
	status = "okay";
};

&i2c2 {
	pinctrl-0 = <&i2c2_pb0_pins>;
	pinctrl-names = "default";
	status = "okay";

	pcf8574a: gpio@38 {
		compatible = "nxp,pcf8574a";
		reg = <0x38>;
		interrupt-parent = <&pio>;
		interrupts = <1 2 IRQ_TYPE_LEVEL_LOW>; /* PB2 */
		interrupt-controller;
		gpio-controller;
		#gpio-cells = <2>;
		#interrupt-cells = <2>;
		gpio-line-names =
			"pin13 [gpio8]",
			"pin16 [gpio10]",
			"pin18 [gpio11]",
			"pin26 [gpio17]",
			"pin22 [gpio14]",
			"pin28 [gpio19]",
			"pin37 [gpio23]",
			"pin11 [gpio6]";
	};
};

&mdio {
	ext_rgmii_phy: ethernet-phy@1 {
		compatible = "ethernet-phy-ieee802.3-c22";
		reg = <1>;
	};
};

&mmc0 {
	bus-width = <4>;
	cd-gpios = <&pio 5 6 GPIO_ACTIVE_HIGH>; /* PF6 */
	disable-wp;
	vmmc-supply = <&reg_vcc_3v3>;
	vqmmc-supply = <&reg_vcc_3v3>;
	pinctrl-0 = <&mmc0_pins>;
	pinctrl-names = "default";
	status = "okay";
};

&mmc1 {
	bus-width = <4>;
	mmc-pwrseq = <&wifi_pwrseq>;
	non-removable;
	vmmc-supply = <&reg_vcc_3v3>;
	vqmmc-supply = <&reg_vcc_3v3>;
	pinctrl-0 = <&mmc1_pins>;
	pinctrl-names = "default";
	status = "okay";

	xr829: wifi@1 {
		reg = <1>;
		interrupt-parent = <&pio>;
		interrupts = <6 10 IRQ_TYPE_LEVEL_LOW>; /* PG10 */
		interrupt-names = "host-wake";
	};
};

&ohci0 {
	status = "okay";
};

&ohci1 {
	status = "okay";
};

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

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

	/* XR829 bluetooth is connected here */
};

&usb_otg {
	dr_mode = "otg";
	status = "okay";
};

&usbphy {
	usb0_id_det-gpios = <&pio 3 21 GPIO_ACTIVE_HIGH>; /* PD21 */
	usb0_vbus_det-gpios = <&pio 3 20 GPIO_ACTIVE_HIGH>; /* PD20 */
	usb0_vbus-supply = <&reg_usbvbus>;
	usb1_vbus-supply = <&reg_vcc>;
	status = "okay";
};

&pio {
	gpio-line-names =
		/* Port A */
		"", "", "", "", "", "", "", "",
		"", "", "", "", "", "", "", "",
		"", "", "", "", "", "", "", "",
		"", "", "", "", "", "", "", "",
		/* Port B */
		"pin5 [gpio2/twi2-sck]",
		"pin3 [gpio1/twi2-sda]",
		"",
		"pin38 [gpio24/i2s2-din]",
		"pin40 [gpio25/i2s2-dout]",
		"pin12 [gpio7/i2s-clk]",
		"pin35 [gpio22/i2s2-lrck]",
		"",
		"pin8 [gpio4/uart0-txd]",
		"pin10 [gpio5/uart0-rxd]",
		"",
		"",
		"pin15 [gpio9]",
		"", "", "", "",
		"", "", "", "", "", "", "", "",
		"", "", "", "", "", "", "", "",
		/* Port C */
		"",
		"pin31 [gpio21]",
		"", "", "", "", "", "",
		"", "", "", "", "", "", "", "",
		"", "", "", "", "", "", "", "",
		"", "", "", "", "", "", "", "",
		/* Port D */
		"", "", "", "", "", "", "", "",
		"", "",
		"pin24 [gpio16/spi1-ce0]",
		"pin23 [gpio15/spi1-clk]",
		"pin19 [gpio12/spi1-mosi]",
		"pin21 [gpio13/spi1-miso]",
		"pin27 [gpio18/spi1-hold]",
		"pin29 [gpio20/spi1-wp]",
		"", "", "", "", "", "",
		"pin7 [gpio3/pwm]";
};