From c44262d784d66e468d9333ffb7f00cbbeb399ebf Mon Sep 17 00:00:00 2001
From: Phil Elwell <phil@raspberrypi.com>
Date: Thu, 7 Nov 2024 11:41:33 +0000
Subject: [PATCH] overlays: Add pwm-pio overlay

Add an overlay to enable a single-channel PIO-assisted PWM interface on any
header pin.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
---
 arch/arm/boot/dts/overlays/Makefile           |  1 +
 arch/arm/boot/dts/overlays/README             |  8 ++++
 arch/arm/boot/dts/overlays/overlay_map.dts    |  4 ++
 .../arm/boot/dts/overlays/pwm-pio-overlay.dts | 39 +++++++++++++++++++
 4 files changed, 52 insertions(+)
 create mode 100644 arch/arm/boot/dts/overlays/pwm-pio-overlay.dts

--- a/arch/arm/boot/dts/overlays/Makefile
+++ b/arch/arm/boot/dts/overlays/Makefile
@@ -220,6 +220,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \
 	pwm-2chan.dtbo \
 	pwm-gpio.dtbo \
 	pwm-ir-tx.dtbo \
+	pwm-pio.dtbo \
 	pwm1.dtbo \
 	qca7000.dtbo \
 	qca7000-uart0.dtbo \
--- a/arch/arm/boot/dts/overlays/README
+++ b/arch/arm/boot/dts/overlays/README
@@ -3960,6 +3960,14 @@ Params: gpio_pin                Output G
         func                    Pin function (default 2 = Alt5)
 
 
+Name:   pwm-pio
+Info:   Configures a GPIO pin as PIO-assisted PWM output. Unlike hardware PWM,
+        this can be used on any RP1 GPIO in bank 0 (0-27). Up to 4 are
+        supported, assuming nothing else is using PIO. Pi 5 only.
+Load:   dtoverlay=pwm-pio,<param>=<val>
+Params: gpio                    Output GPIO (0-27, default 4)
+
+
 Name:   pwm1
 Info:   Configures one or two PWM channel on PWM1 (BCM2711 only)
         N.B.:
--- a/arch/arm/boot/dts/overlays/overlay_map.dts
+++ b/arch/arm/boot/dts/overlays/overlay_map.dts
@@ -240,6 +240,10 @@
 		bcm2712;
 	};
 
+	pwm-pio {
+		bcm2712;
+	};
+
 	pwm1 {
 		bcm2711;
 	};
--- /dev/null
+++ b/arch/arm/boot/dts/overlays/pwm-pio-overlay.dts
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0
+// Device tree overlay for RP1 PIO PWM.
+/dts-v1/;
+/plugin/;
+
+/ {
+	compatible = "brcm,bcm2712";
+
+	fragment@0 {
+		target = <&gpio>;
+		__overlay__ {
+			pwm_pio_pins: pwm_pio_pins@4 {
+				brcm,pins = <4>; /* gpio 4 */
+				function = "pio";
+				bias-disable;
+			};
+		};
+	};
+
+	fragment@1 {
+		target-path = "/";
+		__overlay__ {
+			pwm_pio: pwm_pio@4 {
+				  compatible = "raspberrypi,pwm-pio-rp1";
+				  pinctrl-names = "default";
+				  pinctrl-0 = <&pwm_pio_pins>;
+				  gpios = <&gpio 4 0>;
+			};
+		};
+	};
+
+	__overrides__ {
+		gpio = <&pwm_pio>,"gpios:4",
+		       <&pwm_pio_pins>,"brcm,pins:0",
+		       /* modify reg values to allow multiple instantiation */
+		       <&pwm_pio>,"reg:0",
+		       <&pwm_pio_pins>,"reg:0";
+	};
+};
