From 6e1ccf7cfe1b758963743b88fa1b078b011c77b3 Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dave.stevenson@raspberrypi.com>
Date: Fri, 14 Apr 2023 13:57:52 +0100
Subject: [PATCH] dtoverlays: Add an overlay for the Waveshare DSI
 screens

They come in varying sizes, but all have the same MCU for
power control and touch controller.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
---
 arch/arm/boot/dts/overlays/Makefile           |   1 +
 arch/arm/boot/dts/overlays/README             |  28 ++++
 .../vc4-kms-dsi-waveshare-panel-overlay.dts   | 123 ++++++++++++++++++
 3 files changed, 152 insertions(+)
 create mode 100644 arch/arm/boot/dts/overlays/vc4-kms-dsi-waveshare-panel-overlay.dts

--- a/arch/arm/boot/dts/overlays/Makefile
+++ b/arch/arm/boot/dts/overlays/Makefile
@@ -272,6 +272,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \
 	vc4-kms-dsi-7inch.dtbo \
 	vc4-kms-dsi-lt070me05000.dtbo \
 	vc4-kms-dsi-lt070me05000-v2.dtbo \
+	vc4-kms-dsi-waveshare-panel.dtbo \
 	vc4-kms-kippah-7inch.dtbo \
 	vc4-kms-v3d.dtbo \
 	vc4-kms-v3d-pi4.dtbo \
--- a/arch/arm/boot/dts/overlays/README
+++ b/arch/arm/boot/dts/overlays/README
@@ -4493,6 +4493,34 @@ Load:   dtoverlay=vc4-kms-dsi-lt070me050
 Params: <None>
 
 
+Name:   vc4-kms-dsi-waveshare-panel
+Info:   Enable a Waveshare DSI touchscreen
+        Includes the Goodix driver for the touchscreen element.
+        The default is for the display to be using the I2C0 option for control.
+        Use the i2c1 override if using the I2C1 wiring with jumper wires from
+        GPIOs 2&3 (pins 3&5).
+        invx/invy/swapxy should be used with caution as the panel specifier will
+        set the default inversions for that panel. Always use them after the
+        panel specifier, and be aware that you may need to set them as =0, not
+        just adding it.
+        Requires vc4-kms-v3d to be loaded.
+Load:   dtoverlay=vc4-kms-dsi-waveshare-panel,<param>=<val>
+Params: 2_8_inch                2.8" 480x640
+        3_4_inch                3.4" 800x800 round
+        4_0_inch                4.0" 480x800
+        7_0_inchC               7.0" C 1024x600
+        7_9_inch                7.9" 400x1280
+        8_0_inch                8.0" 1280x800
+        10_1_inch               10.1" 1280x800
+        11_9_inch               11.9" 320x1480
+        i2c1                    Use i2c-1 with jumper wires from GPIOs 2&3
+        disable_touch           Disable the touch controller
+        rotation                Set the panel orientation property
+        invx                    Touchscreen inverted x axis
+        invy                    Touchscreen inverted y axis
+        swapxy                  Touchscreen swapped x y axis
+
+
 Name:   vc4-kms-kippah-7inch
 Info:   This overlay is now deprecated - see vc4-kms-dpi-panel,kippah-7inch
 Load:   <Deprecated>
--- /dev/null
+++ b/arch/arm/boot/dts/overlays/vc4-kms-dsi-waveshare-panel-overlay.dts
@@ -0,0 +1,123 @@
+/*
+ * Device Tree overlay for Waveshare DSI Touchscreens
+ *
+ */
+
+/dts-v1/;
+/plugin/;
+
+/ {
+	compatible = "brcm,bcm2835";
+
+	fragment@0 {
+		target = <&dsi1>;
+		__overlay__ {
+			#address-cells = <1>;
+			#size-cells = <0>;
+			status = "okay";
+			port {
+				dsi_out: endpoint {
+					remote-endpoint = <&panel_in>;
+				};
+			};
+		};
+	};
+
+	fragment@1 {
+		target-path = "/";
+		__overlay__ {
+		};
+	};
+
+	frag2: fragment@2 {
+		target = <&i2c_csi_dsi>;
+		__overlay__ {
+			#address-cells = <1>;
+			#size-cells = <0>;
+			status = "okay";
+
+			panel: panel_disp1@45 {
+				reg = <0x45>;
+				compatible = "waveshare,10.1inch-panel";
+
+				port {
+					panel_in: endpoint {
+						remote-endpoint = <&dsi_out>;
+					};
+				};
+			};
+
+			touch: goodix@14 {
+				reg = <0x14>;
+				compatible = "goodix,gt911";
+			};
+		};
+	};
+
+	fragment@3 {
+		target = <&i2c0if>;
+		__overlay__ {
+			status = "okay";
+		};
+	};
+
+	fragment@4 {
+		target = <&i2c0mux>;
+		__overlay__ {
+			status = "okay";
+		};
+	};
+
+	fragment@5 {
+		target = <&i2c_arm>;
+		__dormant__ {
+			status = "okay";
+		};
+	};
+
+	__overrides__ {
+		2_8_inch = <&panel>, "compatible=waveshare,2.8inch-panel",
+				   <&touch>, "touchscreen-size-x:0=640",
+				   <&touch>, "touchscreen-size-y:0=480",
+				   <&touch>, "touchscreen-inverted-y?",
+				   <&touch>, "touchscreen-swapped-x-y?";
+		3_4_inch = <&panel>, "compatible=waveshare,3.4inch-panel",
+				   <&touch>, "touchscreen-size-x:0=800",
+				   <&touch>, "touchscreen-size-y:0=800";
+		4_0_inch = <&panel>, "compatible=waveshare,4.0inch-panel",
+				   <&touch>, "touchscreen-size-x:0=800",
+				   <&touch>, "touchscreen-size-y:0=480",
+				   <&touch>, "touchscreen-inverted-x?",
+				   <&touch>, "touchscreen-swapped-x-y?";
+		7_0_inchC = <&panel>, "compatible=waveshare,7.0inch-c-panel",
+				   <&touch>, "touchscreen-size-x:0=800",
+				   <&touch>, "touchscreen-size-y:0=480";
+		7_9_inch = <&panel>, "compatible=waveshare,7.9inch-panel",
+				   <&touch>, "touchscreen-size-x:0=400",
+				   <&touch>, "touchscreen-size-y:0=1280",
+				   <&touch>, "touchscreen-inverted-x?",
+				   <&touch>, "touchscreen-inverted-y?";
+		8_0_inch = <&panel>, "compatible=waveshare,8.0inch-panel",
+				   <&touch>, "touchscreen-size-x:0=800",
+				   <&touch>, "touchscreen-size-y:0=1280",
+				   <&touch>, "touchscreen-inverted-x?",
+				   <&touch>, "touchscreen-swapped-x-y?";
+		10_1_inch = <&panel>, "compatible=waveshare,10.1inch-panel",
+				   <&touch>, "touchscreen-size-x:0=800",
+				   <&touch>, "touchscreen-size-y:0=1280",
+				   <&touch>, "touchscreen-inverted-x?",
+				   <&touch>, "touchscreen-swapped-x-y?";
+		11_9_inch = <&panel>, "compatible=waveshare,11.9inch-panel",
+				   <&touch>, "touchscreen-size-x:0=320",
+				   <&touch>, "touchscreen-size-y:0=1480",
+				   <&touch>, "touchscreen-inverted-x?",
+				   <&touch>, "touchscreen-swapped-x-y?";
+		i2c1 = <&frag2>, "target:0=",<&i2c1>,
+		       <0>, "-3-4+5";
+		disable_touch = <&touch>, "status=disabled";
+		rotation = <&panel>, "rotation:0";
+		invx = <&touch>,"touchscreen-inverted-x?";
+		invy = <&touch>,"touchscreen-inverted-y?";
+		swapxy = <&touch>,"touchscreen-swapped-x-y?";
+	};
+};
