From 5713601cc9b8c7e03ff6013342a1f912ae0bd6be Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dave.stevenson@raspberrypi.com>
Date: Tue, 15 Apr 2025 19:02:21 +0100
Subject: [PATCH] dtoverlays: Create Pi5 variant of tc358743 overlay

The Pi0-4 variant wants to change the compatible string for
the CSI2 node to "brcm,bcm2835-unicam-legacy" to disable Media
Controller by default, which would switch to Unicam when Pi5 needs
the CFE driver.

Split the overlay into an include and base pi0-4 and pi5 overlay
files

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
---
 arch/arm/boot/dts/overlays/Makefile           |   1 +
 arch/arm/boot/dts/overlays/README             |  15 +++
 arch/arm/boot/dts/overlays/overlay_map.dts    |   6 +
 .../boot/dts/overlays/tc358743-overlay.dts    | 105 +-----------------
 .../dts/overlays/tc358743-pi5-overlay.dts     |  10 ++
 arch/arm/boot/dts/overlays/tc358743.dtsi      | 101 +++++++++++++++++
 6 files changed, 138 insertions(+), 100 deletions(-)
 create mode 100644 arch/arm/boot/dts/overlays/tc358743-pi5-overlay.dts
 create mode 100644 arch/arm/boot/dts/overlays/tc358743.dtsi

--- a/arch/arm/boot/dts/overlays/Makefile
+++ b/arch/arm/boot/dts/overlays/Makefile
@@ -302,6 +302,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \
 	sx150x.dtbo \
 	tc358743.dtbo \
 	tc358743-audio.dtbo \
+	tc358743-pi5.dtbo \
 	tinylcd35.dtbo \
 	tpm-slb9670.dtbo \
 	tpm-slb9673.dtbo \
--- a/arch/arm/boot/dts/overlays/README
+++ b/arch/arm/boot/dts/overlays/README
@@ -5078,6 +5078,21 @@ Load:   dtoverlay=tc358743-audio,<param>
 Params: card-name               Override the default, "tc358743", card name.
 
 
+Name:   tc358743-pi5
+Info:   Toshiba TC358743 HDMI to CSI-2 bridge chip for Pi5.
+        Uses Unicam 1, which is the standard camera connector on most Pi
+        variants.
+Load:   dtoverlay=tc358743-pi5,<param>=<val>
+Params: 4lane                   Use 4 lanes (only applicable to Compute Modules
+                                CAM1 connector).
+
+        link-frequency          Set the link frequency. Only values of 297000000
+                                (574Mbit/s) and 486000000 (972Mbit/s - default)
+                                are supported by the driver.
+        cam0                    Adopt the default configuration for CAM0 on a
+                                Compute Module (CSI0, i2c_vc, and cam0_reg).
+
+
 Name:   tinylcd35
 Info:   3.5" Color TFT Display by www.tinylcd.com
         Options: Touch, RTC, keypad
--- a/arch/arm/boot/dts/overlays/overlay_map.dts
+++ b/arch/arm/boot/dts/overlays/overlay_map.dts
@@ -402,6 +402,12 @@
 		bcm2711;
 	};
 
+	tc358743 {
+		bcm2835;
+		bcm2711;
+		bcm2712 = "tc358743-pi5";
+	};
+
 	uart0 {
 		bcm2835;
 		bcm2711;
--- a/arch/arm/boot/dts/overlays/tc358743-overlay.dts
+++ b/arch/arm/boot/dts/overlays/tc358743-overlay.dts
@@ -3,101 +3,12 @@
 /dts-v1/;
 /plugin/;
 
-/{
-	compatible = "brcm,bcm2835";
-
-	i2c_frag: fragment@0 {
-		target = <&i2c_csi_dsi>;
-		__overlay__ {
-			#address-cells = <1>;
-			#size-cells = <0>;
-			status = "okay";
-
-			tc358743: tc358743@f {
-				compatible = "toshiba,tc358743";
-				reg = <0x0f>;
-				status = "okay";
-
-				clocks = <&cam1_clk>;
-				clock-names = "refclk";
-
-				port {
-					tc358743_0: endpoint {
-						remote-endpoint = <&csi1_ep>;
-						clock-lanes = <0>;
-						clock-noncontinuous;
-						link-frequencies =
-							/bits/ 64 <486000000>;
-					};
-				};
-			};
-		};
-	};
-
-	csi_frag: fragment@1 {
-		target = <&csi1>;
-		csi: __overlay__ {
-			status = "okay";
-
-			port {
-				csi1_ep: endpoint {
-					remote-endpoint = <&tc358743_0>;
-				};
-			};
-		};
-	};
-
-	fragment@2 {
-		target = <&tc358743_0>;
-		__overlay__ {
-			data-lanes = <1 2>;
-		};
-	};
-
-	fragment@3 {
-		target = <&tc358743_0>;
-		__dormant__ {
-			data-lanes = <1 2 3 4>;
-		};
-	};
+#include "tc358743.dtsi"
 
-	fragment@4 {
-		target = <&i2c0if>;
-		__overlay__ {
-			status = "okay";
-		};
-	};
-
-	fragment@5 {
-		target = <&i2c0mux>;
-		__overlay__ {
-			status = "okay";
-		};
-	};
-
-	clk_frag: fragment@6 {
-		target = <&cam1_clk>;
-		__overlay__ {
-			status = "okay";
-			clock-frequency = <27000000>;
-		};
-	};
-
-	fragment@7 {
-		target = <&csi1_ep>;
-		__overlay__ {
-			data-lanes = <1 2>;
-		};
-	};
-
-	fragment@8 {
-		target = <&csi1_ep>;
-		__dormant__ {
-			data-lanes = <1 2 3 4>;
-		};
-	};
+/ {
+	compatible = "brcm,bcm2835";
 
-	fragment@9 {
+	fragment@100 {
 		target = <&csi1>;
 		__overlay__ {
 			compatible = "brcm,bcm2835-unicam-legacy";
@@ -105,12 +16,6 @@
 	};
 
 	__overrides__ {
-		4lane = <0>, "-2+3-7+8";
-		link-frequency = <&tc358743_0>,"link-frequencies#0";
-		media-controller = <0>,"!9";
-		cam0 = <&i2c_frag>, "target:0=",<&i2c_csi_dsi0>,
-		       <&csi_frag>, "target:0=",<&csi0>,
-		       <&clk_frag>, "target:0=",<&cam0_clk>,
-		       <&tc358743>, "clocks:0=",<&cam0_clk>;
+		media-controller = <0>,"!100";
 	};
 };
--- /dev/null
+++ b/arch/arm/boot/dts/overlays/tc358743-pi5-overlay.dts
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Definitions for Toshiba TC358743 HDMI to CSI2 bridge on VC I2C bus. Pi5 variant
+/dts-v1/;
+/plugin/;
+
+#include "tc358743.dtsi"
+
+/ {
+	compatible = "brcm,bcm2712";
+};
--- /dev/null
+++ b/arch/arm/boot/dts/overlays/tc358743.dtsi
@@ -0,0 +1,101 @@
+/ {
+	i2c_frag: fragment@0 {
+		target = <&i2c_csi_dsi>;
+		__overlay__ {
+			#address-cells = <1>;
+			#size-cells = <0>;
+			status = "okay";
+
+			tc358743: tc358743@f {
+				compatible = "toshiba,tc358743";
+				reg = <0x0f>;
+				status = "okay";
+
+				clocks = <&cam1_clk>;
+				clock-names = "refclk";
+
+				port {
+					tc358743_0: endpoint {
+						remote-endpoint = <&csi1_ep>;
+						clock-lanes = <0>;
+						clock-noncontinuous;
+						link-frequencies =
+							/bits/ 64 <486000000>;
+					};
+				};
+			};
+		};
+	};
+
+	csi_frag: fragment@1 {
+		target = <&csi1>;
+		csi: __overlay__ {
+			status = "okay";
+
+			port {
+				csi1_ep: endpoint {
+					remote-endpoint = <&tc358743_0>;
+				};
+			};
+		};
+	};
+
+	fragment@2 {
+		target = <&tc358743_0>;
+		__overlay__ {
+			data-lanes = <1 2>;
+		};
+	};
+
+	fragment@3 {
+		target = <&tc358743_0>;
+		__dormant__ {
+			data-lanes = <1 2 3 4>;
+		};
+	};
+
+	fragment@4 {
+		target = <&i2c0if>;
+		__overlay__ {
+			status = "okay";
+		};
+	};
+
+	fragment@5 {
+		target = <&i2c0mux>;
+		__overlay__ {
+			status = "okay";
+		};
+	};
+
+	clk_frag: fragment@6 {
+		target = <&cam1_clk>;
+		__overlay__ {
+			status = "okay";
+			clock-frequency = <27000000>;
+		};
+	};
+
+	fragment@7 {
+		target = <&csi1_ep>;
+		__overlay__ {
+			data-lanes = <1 2>;
+		};
+	};
+
+	fragment@8 {
+		target = <&csi1_ep>;
+		__dormant__ {
+			data-lanes = <1 2 3 4>;
+		};
+	};
+
+	__overrides__ {
+		4lane = <0>, "-2+3-7+8";
+		link-frequency = <&tc358743_0>,"link-frequencies#0";
+		cam0 = <&i2c_frag>, "target:0=",<&i2c_csi_dsi0>,
+		       <&csi_frag>, "target:0=",<&csi0>,
+		       <&clk_frag>, "target:0=",<&cam0_clk>,
+		       <&tc358743>, "clocks:0=",<&cam0_clk>;
+	};
+};
