From 48881099e2a7b77aa04cba7effd35aef9c4627ea Mon Sep 17 00:00:00 2001
From: Alan <Alan>
Date: Tue, 30 May 2023 17:36:29 +0800
Subject: arm64: dts: sun50i-h616-bigtreetech-cb1(sd, emmc)

> X-Git-Archeology: > recovered message: > Signed-off-by: Alan.Ma from BigTreeTech tech@biqu3d.com
> X-Git-Archeology: - Revision 9d69bb1da699f5729dc5c1dcc2d8d5f4590152fd: https://github.com/armbian/build/commit/9d69bb1da699f5729dc5c1dcc2d8d5f4590152fd
> X-Git-Archeology:   Date: Tue, 30 May 2023 17:36:29 +0800
> X-Git-Archeology:   From: Alan <Alan>
> X-Git-Archeology:   Subject: Patches: add current(6.1) & edge(6.2) patch for BigTreeTech CB1
> X-Git-Archeology:
> X-Git-Archeology: - Revision 8562428877003ddd46045cb3429d7fdc9df4acee: https://github.com/armbian/build/commit/8562428877003ddd46045cb3429d7fdc9df4acee
> X-Git-Archeology:   Date: Sat, 02 Sep 2023 03:34:28 +0518
> X-Git-Archeology:   From: Gunjan Gupta <viraniac@gmail.com>
> X-Git-Archeology:   Subject: Allwinner: Kernel patches for 6.5-rc7 kernel
> X-Git-Archeology:
> X-Git-Archeology: - Revision bb78fac2fadb40a43a9ccb36266dbd21746c2eb6: https://github.com/armbian/build/commit/bb78fac2fadb40a43a9ccb36266dbd21746c2eb6
> X-Git-Archeology:   Date: Fri, 29 Sep 2023 08:46:52 +0200
> X-Git-Archeology:   From: Ricardo Pardini <ricardo@pardini.net>
> X-Git-Archeology:   Subject: `sunxi`/`sunxi64` `edge` 6.5.y: rebase/rewrite all patches for v6.5.5; fix UTF-8 "From:" encoding
> X-Git-Archeology:
> X-Git-Archeology: - Revision d1186b8a0e3d44d5b285e94772ebe2c17a014193: https://github.com/armbian/build/commit/d1186b8a0e3d44d5b285e94772ebe2c17a014193
> X-Git-Archeology:   Date: Mon, 30 Oct 2023 22:46:11 +0518
> X-Git-Archeology:   From: Gunjan Gupta <viraniac@gmail.com>
> X-Git-Archeology:   Subject: kernel: sunxi: Add patches for 6.6 kernel
> X-Git-Archeology:
> X-Git-Archeology: - Revision a269c9a1c5784bd89630df1652923f0d76278b61: https://github.com/armbian/build/commit/a269c9a1c5784bd89630df1652923f0d76278b61
> X-Git-Archeology:   Date: Sun, 24 Dec 2023 09:13:08 +0100
> X-Git-Archeology:   From: Gunjan Gupta <viraniac@gmail.com>
> X-Git-Archeology:   Subject: Refreshed patches using rewrite-kernel-patches command
> X-Git-Archeology:
> X-Git-Archeology: - Revision 30ef1a3e149f96200bffaf1c3138a4467b6f0aa7: https://github.com/armbian/build/commit/30ef1a3e149f96200bffaf1c3138a4467b6f0aa7
> X-Git-Archeology:   Date: Sun, 24 Dec 2023 09:13:08 +0100
> X-Git-Archeology:   From: Gunjan Gupta <viraniac@gmail.com>
> X-Git-Archeology:   Subject: Allwinner: Add kernel patches for 6.7 kernel
> X-Git-Archeology:
---
 arch/arm64/boot/dts/allwinner/Makefile        |   2 +
 .../sun50i-h616-bigtreetech-cb1-emmc.dts      |  44 ++++
 .../sun50i-h616-bigtreetech-cb1-sd.dts        |  35 +++
 .../sun50i-h616-bigtreetech-cb1.dtsi          | 223 ++++++++++++++++--
 4 files changed, 287 insertions(+), 17 deletions(-)
 create mode 100644 arch/arm64/boot/dts/allwinner/sun50i-h616-bigtreetech-cb1-emmc.dts
 create mode 100644 arch/arm64/boot/dts/allwinner/sun50i-h616-bigtreetech-cb1-sd.dts

diff --git a/arch/arm64/boot/dts/allwinner/Makefile b/arch/arm64/boot/dts/allwinner/Makefile
index 3280927df6e0..54665bbb468d 100644
--- a/arch/arm64/boot/dts/allwinner/Makefile
+++ b/arch/arm64/boot/dts/allwinner/Makefile
@@ -51,6 +51,8 @@ dtb-$(CONFIG_ARCH_SUNXI) += sun50i-h6-tanix-tx6.dtb
 dtb-$(CONFIG_ARCH_SUNXI) += sun50i-h6-tanix-tx6-mini.dtb
 dtb-$(CONFIG_ARCH_SUNXI) += sun50i-h313-tanix-tx1.dtb
 dtb-$(CONFIG_ARCH_SUNXI) += sun50i-h616-bigtreetech-cb1-manta.dtb
+dtb-$(CONFIG_ARCH_SUNXI) += sun50i-h616-bigtreetech-cb1-sd.dtb
+dtb-$(CONFIG_ARCH_SUNXI) += sun50i-h616-bigtreetech-cb1-emmc.dtb
 dtb-$(CONFIG_ARCH_SUNXI) += sun50i-h616-bigtreetech-pi.dtb
 dtb-$(CONFIG_ARCH_SUNXI) += sun50i-h616-orangepi-zero2.dtb
 dtb-$(CONFIG_ARCH_SUNXI) += sun50i-h616-x96-mate.dtb
diff --git a/arch/arm64/boot/dts/allwinner/sun50i-h616-bigtreetech-cb1-emmc.dts b/arch/arm64/boot/dts/allwinner/sun50i-h616-bigtreetech-cb1-emmc.dts
new file mode 100644
index 000000000000..f878c23f1d90
--- /dev/null
+++ b/arch/arm64/boot/dts/allwinner/sun50i-h616-bigtreetech-cb1-emmc.dts
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: (GPL-2.0+ or MIT)
+/*
+ * Copyright (C) 2023 Alan.Ma <tech@biqu3d.com>.
+ */
+
+/dts-v1/;
+
+#include "sun50i-h616-bigtreetech-cb1.dtsi"
+
+&mmc2 {
+	vmmc-supply = <&reg_dldo1>;
+
+	no-1-8-v;
+	bus-width = <8>;
+	non-removable;
+	status = "okay";
+};
+
+&ws2812 {
+	gpios = <&pio 8 15 GPIO_ACTIVE_LOW>; /* PI15 */
+};
+
+&i2c_gpio {
+	gpios = <&pio 8 6 GPIO_ACTIVE_HIGH>, /* SDA PI6 */
+			<&pio 8 4 GPIO_ACTIVE_HIGH>; /* SCL PI4 */
+};
+
+&can0_pin_irq {
+	pins = "PI3";
+};
+
+&can {
+	interrupts = <8 3 0x08>; /* PI3 IRQ_TYPE_LEVEL_LOW */
+};
+
+&tft_35 {
+	dc-gpios  = <&pio 8 15 GPIO_ACTIVE_HIGH>; /* PI15 */
+};
+
+&spi1 {
+	cs-gpios = <&pio 8 5 GPIO_ACTIVE_HIGH>, /* PI5 */
+			   <&pio 8 14 GPIO_ACTIVE_HIGH>, /* PI14 */
+			   <&pio 8 7 GPIO_ACTIVE_HIGH>; /* PI7 */
+};
diff --git a/arch/arm64/boot/dts/allwinner/sun50i-h616-bigtreetech-cb1-sd.dts b/arch/arm64/boot/dts/allwinner/sun50i-h616-bigtreetech-cb1-sd.dts
new file mode 100644
index 000000000000..e18dd854d74b
--- /dev/null
+++ b/arch/arm64/boot/dts/allwinner/sun50i-h616-bigtreetech-cb1-sd.dts
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: (GPL-2.0+ or MIT)
+/*
+ * Copyright (C) 2023 Alan.Ma <tech@biqu3d.com>.
+ */
+
+/dts-v1/;
+
+#include "sun50i-h616-bigtreetech-cb1.dtsi"
+
+&ws2812 {
+	gpios = <&pio 2 14 GPIO_ACTIVE_LOW>; /* PC14 */
+};
+
+&i2c_gpio {
+	gpios = <&pio 2 12 GPIO_ACTIVE_HIGH>, /* SDA PC12 */
+			<&pio 2 10 GPIO_ACTIVE_HIGH>; /* SCL PC10 */
+};
+
+&can0_pin_irq {
+	pins = "PC9";
+};
+
+&can {
+	interrupts = <2 9 0x08>; /* PC9 IRQ_TYPE_LEVEL_LOW */
+};
+
+&tft_35 {
+	dc-gpios  = <&pio 2 14 GPIO_ACTIVE_HIGH>; /* PC14 */
+};
+
+&spi1 {
+	cs-gpios = <&pio 2 11 GPIO_ACTIVE_HIGH>, /* PC11 */
+			   <&pio 2 7 GPIO_ACTIVE_HIGH>, /* PC7 */
+			   <&pio 2 13 GPIO_ACTIVE_HIGH>; /* PC13 */
+};
diff --git a/arch/arm64/boot/dts/allwinner/sun50i-h616-bigtreetech-cb1.dtsi b/arch/arm64/boot/dts/allwinner/sun50i-h616-bigtreetech-cb1.dtsi
index d12b01c5f41b..0da8f2e6e3d4 100644
--- a/arch/arm64/boot/dts/allwinner/sun50i-h616-bigtreetech-cb1.dtsi
+++ b/arch/arm64/boot/dts/allwinner/sun50i-h616-bigtreetech-cb1.dtsi
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
 /*
  * Copyright (C) 2023 Martin Botka <martin.botka@somainline.org>.
+ * Copyright (C) 2023 Alan.Ma <tech@biqu3d.com>.
  */
 
 /dts-v1/;
@@ -13,22 +14,41 @@
 #include <dt-bindings/leds/common.h>
 
 / {
+	model = "BigTreeTech CB1";
+	compatible = "bigtreetech,cb1", "allwinner,sun50i-h616";
+
 	aliases {
-		ethernet0 = &rtl8189ftv;
+		ethernet0 = &emac1;
+		serial0 = &uart0;
+	};
+
+	chosen {
+		stdout-path = "serial0:115200n8";
 	};
 
 	leds {
 		compatible = "gpio-leds";
 
-		led-0 {
-			function = LED_FUNCTION_STATUS;
-			color = <LED_COLOR_ID_GREEN>;
-			gpios = <&pio 7 5 GPIO_ACTIVE_HIGH>; /* PH5 */
+		act_led: led-0 {
+			gpios = <&pio 7 5 GPIO_ACTIVE_LOW>; /* PH5 */
+			linux,default-trigger = "heartbeat";
+		};
+
+		gpio_1 {
+			function = "wifi_power";
+			gpios = <&pio 5 6 GPIO_ACTIVE_HIGH>; /* PF6 */
+			default-state = "on";
+		};
+
+		gpio_2 {
+			function = "wifi_wake";
+			gpios = <&pio 6 15 GPIO_ACTIVE_HIGH>; /* PG15 */
+			default-state = "on";
 		};
 	};
 
 	reg_vcc5v: regulator-vcc5v {
-		/* board wide 5V supply from carrier boards */
+		/* board wide 5V supply directly from the USB-C socket */
 		compatible = "regulator-fixed";
 		regulator-name = "vcc-5v";
 		regulator-min-microvolt = <5000000>;
@@ -36,7 +56,17 @@ reg_vcc5v: regulator-vcc5v {
 		regulator-always-on;
 	};
 
+	reg_usb1_vbus: regulator-usb1-vbus {
+		compatible = "regulator-fixed";
+		regulator-name = "usb1-vbus";
+		regulator-min-microvolt = <5000000>;
+		regulator-max-microvolt = <5000000>;
+		vin-supply = <&reg_vcc5v>;
+		enable-active-high;
+	};
+
 	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>;
@@ -46,6 +76,7 @@ reg_vcc33_wifi: vcc33-wifi {
 	};
 
 	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>;
@@ -57,10 +88,50 @@ reg_vcc_wifi_io: vcc-wifi-io {
 	wifi_pwrseq: wifi-pwrseq {
 		compatible = "mmc-pwrseq-simple";
 		clocks = <&rtc 1>;
-		clock-names = "ext_clock";
+		clock-names = "osc32k-out";
 		reset-gpios = <&pio 6 18 GPIO_ACTIVE_LOW>; /* PG18 */
 		post-power-on-delay-ms = <200>;
 	};
+
+	ws2812: ws2812 {
+		compatible = "rgb-ws2812";
+		pinctrl-names = "default";
+		rgb_cnt = <2>;
+		rgb_value = <0x010000 0x010000>;
+		status = "disabled";
+	};
+
+	i2c_gpio: i2c-gpio {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		compatible = "i2c-gpio";
+		status = "disabled";
+
+		i2c-gpio,delay-us = <3>; /* 100 kHz */
+
+		tft_tp: ns2009@48 {
+			compatible = "ti,tsc2007";
+			reg = <0x48>;
+			status = "disabled";
+			ti,x-plate-ohms = <660>;
+			ti,rt-thr = <3000>;
+			ti,fuzzx = <32>;
+			ti,fuzzy = <16>;
+			i2c,ignore-nak = <1>;
+		};
+
+		light: bh1750@5c {
+			compatible = "rohm,bh1750";
+			reg = <0x5c>;
+			status = "disabled";
+		};
+	};
+
+	mcp2515_clock: mcp2515_clock {
+		compatible = "fixed-clock";
+		#clock-cells = <0>;
+		clock-frequency  = <12000000>;
+	};
 };
 
 &cpu0 {
@@ -69,9 +140,9 @@ &cpu0 {
 
 &mmc0 {
 	vmmc-supply = <&reg_dldo1>;
-	/* Card detection pin is not connected */
 	broken-cd;
 	bus-width = <4>;
+	max-frequency = <50000000>;
 	status = "okay";
 };
 
@@ -80,12 +151,56 @@ &mmc1 {
 	vqmmc-supply = <&reg_vcc_wifi_io>;
 	mmc-pwrseq = <&wifi_pwrseq>;
 	bus-width = <4>;
+	max-frequency = <25000000>;
 	non-removable;
 	mmc-ddr-1_8v;
 	status = "okay";
+};
 
-	rtl8189ftv: wifi@1 {
+&pio {
+	can0_pin_irq: can0_pin_irq {
+		function = "irq";
+		bias-pull-up;
+	};
+};
+
+&spi1 {
+	#address-cells = <1>;
+	#size-cells = <0>;
+	status = "disabled";
+	pinctrl-names = "default";
+	pinctrl-0 = <&spi1_pins>;
+
+	can: mcp2515@0 {
+		status = "disabled";
+		compatible = "microchip,mcp2515";
+		reg = <0x0>;
+		clocks = <&mcp2515_clock>;
+		spi-max-frequency = <12500000>;
+		pinctrl-names = "default";
+		pinctrl-0 = <&can0_pin_irq>;
+		interrupt-parent = <&pio>;
+		vdd-supply = <&reg_vcc33_wifi>;
+		xceiver-supply = <&reg_vcc33_wifi>;
+	};
+
+	tft_35: st7789v@1 {
+		status = "disabled";
+		compatible = "sitronix,st7796s";
 		reg = <1>;
+		spi-max-frequency =<12500000>;
+		fps =<60>;
+		buswidth = <8>;
+		rotate =<0>;
+		width = <480>;
+		height = <320>;
+		bpp = <24>;
+		bgr;
+		regwidth = <8>;
+		debug = <0x00>;	 //0x20 show fps
+		txbuflen = <307200>;
+		spi-cpol;
+		spi-cpha;
 	};
 };
 
@@ -95,29 +210,34 @@ &r_i2c {
 	axp313a: pmic@36 {
 		compatible = "x-powers,axp313a";
 		reg = <0x36>;
-		interrupt-controller;
-		#interrupt-cells = <1>;
+		wakeup-source;
 
 		regulators {
 			reg_dcdc1: dcdc1 {
 				regulator-name = "vdd-gpu-sys";
-				regulator-min-microvolt = <810000>;
-				regulator-max-microvolt = <990000>;
+				regulator-min-microvolt = <500000>;
+				regulator-max-microvolt = <3400000>;
+				regulator-step-delay-us = <25>;
+				regulator-final-delay-us = <50>;
 				regulator-always-on;
 			};
 
 			reg_dcdc2: dcdc2 {
 				regulator-name = "vdd-cpu";
-				regulator-min-microvolt = <810000>;
-				regulator-max-microvolt = <1100000>;
+				regulator-min-microvolt = <500000>;
+				regulator-max-microvolt = <1540000>;
+				regulator-step-delay-us = <25>;
+				regulator-final-delay-us = <50>;
 				regulator-ramp-delay = <200>;
 				regulator-always-on;
 			};
 
 			reg_dcdc3: dcdc3 {
 				regulator-name = "vcc-dram";
-				regulator-min-microvolt = <1350000>;
-				regulator-max-microvolt = <1500000>;
+				regulator-min-microvolt = <500000>;
+				regulator-max-microvolt = <1840000>;
+				regulator-step-delay-us = <25>;
+				regulator-final-delay-us = <50>;
 				regulator-always-on;
 			};
 
@@ -125,6 +245,8 @@ reg_aldo1: aldo1 {
 				regulator-name = "vcc-1v8-pll";
 				regulator-min-microvolt = <1800000>;
 				regulator-max-microvolt = <1800000>;
+				regulator-step-delay-us = <25>;
+				regulator-final-delay-us = <50>;
 				regulator-always-on;
 			};
 
@@ -132,12 +254,79 @@ reg_dldo1: dldo1 {
 				regulator-name = "vcc-3v3-io";
 				regulator-min-microvolt = <3300000>;
 				regulator-max-microvolt = <3300000>;
+				regulator-step-delay-us = <25>;
+				regulator-final-delay-us = <50>;
 				regulator-always-on;
 			};
 		};
 	};
 };
 
+&cpu0 {
+	cpu-supply = <&reg_dcdc2>;
+	status = "okay";
+};
+
+&gpu {
+	mali-supply = <&reg_dcdc3>;
+	status = "okay";
+};
+
+&uart0 {
+	pinctrl-names = "default";
+	pinctrl-0 = <&uart0_ph_pins>;
+	status = "okay";
+};
+
 &usbphy {
+	usb1_vbus-supply = <&reg_usb1_vbus>;
+	status = "okay";
+};
+
+&ehci0 {
+	status = "okay";
+};
+
+&ohci0 {
+	status = "okay";
+};
+
+&ehci1 {
+	status = "okay";
+};
+
+&ohci1 {
+	status = "okay";
+};
+
+&ehci2 {
+	status = "okay";
+};
+
+&ohci2 {
+	status = "okay";
+};
+
+&ehci3 {
+	status = "okay";
+};
+
+&ohci3 {
+	status = "okay";
+};
+
+&usbotg {
+	/*
+	 * PHY0 pins are connected to a USB-C socket, but a role switch
+	 * is not implemented: both CC pins are pulled to GND.
+	 * The VBUS pins power the device, so a fixed peripheral mode
+	 * is the best choice.
+	 * The board can be powered via GPIOs, in this case port0 *can*
+	 * act as a host (with a cable/adapter ignoring CC), as VBUS is
+	 * then provided by the GPIOs. Any user of this setup would
+	 * need to adjust the DT accordingly: dr_mode set to "host",
+	 * enabling OHCI0 and EHCI0.
+	 */
+	dr_mode = "peripheral";
 	status = "okay";
 };
-- 
2.35.3

