From f0748371bf31324a22454631df3c36440158b862 Mon Sep 17 00:00:00 2001
From: robert jakub <rj@project2.pl>
Date: Sat, 19 Oct 2024 00:14:47 +0200
Subject: [PATCH] reformat code

---
 .../apply-overlays-dtmerge.nix                | 22 +++++---
 .../apply-overlays}/default.nix               |  0
 .../overlays/default.nix                      |  1 +
 .../overlays/dtb-audremap.nix                 |  0
 .../overlays/dtb-cpi-bluetooth.nix            |  0
 .../overlays/dtb-cpi-i2c1.nix                 |  0
 .../overlays/dtb-cpi-pmu.nix                  |  0
 .../overlays/dtb-cpi-spi4.nix                 |  0
 .../overlays/dtb-cpu-revision.nix             |  0
 .../overlays/dtb-disable-genet.nix            |  0
 .../overlays/dtb-disable-pcie.nix             |  0
 .../overlays/dtb-panel-uc.nix                 |  0
 .../overlays/dtb-rpi4-disable-pwrled.nix      | 30 +++++++++++
 .../overlays/dtb-vc4-kms-v3d.nix              |  0
 .../overlays/source/audremap.dts              |  0
 .../overlays/source/cpi-bluetooth.dts         |  0
 .../overlays/source/cpi-i2c1.dts              |  0
 .../overlays/source/cpi-pmu.dts               |  0
 .../overlays/source/cpi-spi4.dts              |  0
 .../overlays/source/cpu-revision.dts          |  0
 .../overlays/source/disable-genet.dts         |  0
 .../overlays/source/disable-pcie.dts          |  0
 .../overlays/source/panel-uc.dts              |  0
 .../overlays/source/rpi4-disable-pwrled.dts   | 12 +++++
 .../overlays/source/vc4-kms-v3d.dts           |  0
 .../packages/rpi-utils/default.nix            |  0
 uconsole/{audio-patch.nix => audio.nix}       |  2 +-
 uconsole/default.nix                          | 52 ++++++++++---------
 28 files changed, 86 insertions(+), 33 deletions(-)
 rename {uconsole/packages/overlays => raspberry-pi/apply-overlays}/apply-overlays-dtmerge.nix (78%)
 rename {uconsole/packages/overlays => raspberry-pi/apply-overlays}/default.nix (100%)
 rename {uconsole => raspberry-pi}/overlays/default.nix (89%)
 rename {uconsole => raspberry-pi}/overlays/dtb-audremap.nix (100%)
 rename {uconsole => raspberry-pi}/overlays/dtb-cpi-bluetooth.nix (100%)
 rename {uconsole => raspberry-pi}/overlays/dtb-cpi-i2c1.nix (100%)
 rename {uconsole => raspberry-pi}/overlays/dtb-cpi-pmu.nix (100%)
 rename {uconsole => raspberry-pi}/overlays/dtb-cpi-spi4.nix (100%)
 rename {uconsole => raspberry-pi}/overlays/dtb-cpu-revision.nix (100%)
 rename {uconsole => raspberry-pi}/overlays/dtb-disable-genet.nix (100%)
 rename {uconsole => raspberry-pi}/overlays/dtb-disable-pcie.nix (100%)
 rename {uconsole => raspberry-pi}/overlays/dtb-panel-uc.nix (100%)
 create mode 100644 raspberry-pi/overlays/dtb-rpi4-disable-pwrled.nix
 rename {uconsole => raspberry-pi}/overlays/dtb-vc4-kms-v3d.nix (100%)
 rename {uconsole => raspberry-pi}/overlays/source/audremap.dts (100%)
 rename {uconsole => raspberry-pi}/overlays/source/cpi-bluetooth.dts (100%)
 rename {uconsole => raspberry-pi}/overlays/source/cpi-i2c1.dts (100%)
 rename {uconsole => raspberry-pi}/overlays/source/cpi-pmu.dts (100%)
 rename {uconsole => raspberry-pi}/overlays/source/cpi-spi4.dts (100%)
 rename {uconsole => raspberry-pi}/overlays/source/cpu-revision.dts (100%)
 rename {uconsole => raspberry-pi}/overlays/source/disable-genet.dts (100%)
 rename {uconsole => raspberry-pi}/overlays/source/disable-pcie.dts (100%)
 rename {uconsole => raspberry-pi}/overlays/source/panel-uc.dts (100%)
 create mode 100644 raspberry-pi/overlays/source/rpi4-disable-pwrled.dts
 rename {uconsole => raspberry-pi}/overlays/source/vc4-kms-v3d.dts (100%)
 rename {uconsole => raspberry-pi}/packages/rpi-utils/default.nix (100%)
 rename uconsole/{audio-patch.nix => audio.nix} (94%)

diff --git a/uconsole/packages/overlays/apply-overlays-dtmerge.nix b/raspberry-pi/apply-overlays/apply-overlays-dtmerge.nix
similarity index 78%
rename from uconsole/packages/overlays/apply-overlays-dtmerge.nix
rename to raspberry-pi/apply-overlays/apply-overlays-dtmerge.nix
index 0355819..3806673 100644
--- a/uconsole/packages/overlays/apply-overlays-dtmerge.nix
+++ b/raspberry-pi/apply-overlays/apply-overlays-dtmerge.nix
@@ -3,6 +3,7 @@ paramsPerOverlayMap: {
   stdenvNoCC,
   dtc,
   libraspberrypi,
+  pkgs,
 }:
 with lib; (base: overlays':
     stdenvNoCC.mkDerivation {
@@ -10,21 +11,28 @@ with lib; (base: overlays':
       nativeBuildInputs = [dtc libraspberrypi];
       buildCommand = let
         overlays = toList overlays';
+        baseDTBs =
+          map (x: (builtins.unsafeDiscardStringContext (lib.removeSuffix ".dtb" (builtins.baseNameOf x))))
+          (lib.filesystem.listFilesRecursive "${base}");
       in ''
         mkdir -p $out
         cd "${base}"
         find . -type f -name '*.dtb' -print0 \
           | xargs -0 cp -v --no-preserve=mode --target-directory "$out" --parents
 
-        for dtb in $(find "$out" -type f -name '*.dtb'); do
-
-          echo -n "Applying params to $(basename "$dtb")... "
-          echo ${concatStringsSep " " (mapAttrsToList (name: value: "${name}=${value}") (paramsPerOverlayMap.bcm2711-rpi-cm4 or {}))}
+        echo baseDTBs: ${toString baseDTBs}
 
+        ${flip (concatMapStringsSep "\n") baseDTBs (o: ''
+          dtb=$(find "$out" -type f -name '${o}.dtb')
+          echo -n "Applying params to ${o}.dtb... "
+          echo -n ${concatStringsSep " " (mapAttrsToList (name: value: "${name}=${value}") (paramsPerOverlayMap.${o} or {}))} " "
           mv "$dtb"{,.in}
-          dtmerge "$dtb.in" "$dtb" - ${concatStringsSep " " (mapAttrsToList (name: value: "${name}=${value}") (paramsPerOverlayMap.bcm2711-rpi-cm4 or {}))}
+          dtmerge "$dtb.in" "$dtb" - ${concatStringsSep " " (mapAttrsToList (name: value: "${name}=${value}") (paramsPerOverlayMap.${o} or {}))}
           rm "$dtb.in"
+          echo "ok"
+        '')}
 
+        for dtb in $(find "$out" -type f -name '*.dtb'); do
           dtbCompat=$(fdtget -t s "$dtb" / compatible 2>/dev/null || true)
           # skip files without `compatible` string
           test -z "$dtbCompat" && continue
@@ -59,6 +67,6 @@ with lib; (base: overlays':
             rm "$dtb.in" "$dtboWithExt"
           fi
         '')}
-
-        done'';
+        done
+      '';
     })
diff --git a/uconsole/packages/overlays/default.nix b/raspberry-pi/apply-overlays/default.nix
similarity index 100%
rename from uconsole/packages/overlays/default.nix
rename to raspberry-pi/apply-overlays/default.nix
diff --git a/uconsole/overlays/default.nix b/raspberry-pi/overlays/default.nix
similarity index 89%
rename from uconsole/overlays/default.nix
rename to raspberry-pi/overlays/default.nix
index 60c01d5..2668b44 100644
--- a/uconsole/overlays/default.nix
+++ b/raspberry-pi/overlays/default.nix
@@ -10,5 +10,6 @@
     ./dtb-cpi-bluetooth.nix
     ./dtb-vc4-kms-v3d.nix
     ./dtb-cpi-spi4.nix
+    ./dtb-rpi4-disable-pwrled.nix
   ];
 }
diff --git a/uconsole/overlays/dtb-audremap.nix b/raspberry-pi/overlays/dtb-audremap.nix
similarity index 100%
rename from uconsole/overlays/dtb-audremap.nix
rename to raspberry-pi/overlays/dtb-audremap.nix
diff --git a/uconsole/overlays/dtb-cpi-bluetooth.nix b/raspberry-pi/overlays/dtb-cpi-bluetooth.nix
similarity index 100%
rename from uconsole/overlays/dtb-cpi-bluetooth.nix
rename to raspberry-pi/overlays/dtb-cpi-bluetooth.nix
diff --git a/uconsole/overlays/dtb-cpi-i2c1.nix b/raspberry-pi/overlays/dtb-cpi-i2c1.nix
similarity index 100%
rename from uconsole/overlays/dtb-cpi-i2c1.nix
rename to raspberry-pi/overlays/dtb-cpi-i2c1.nix
diff --git a/uconsole/overlays/dtb-cpi-pmu.nix b/raspberry-pi/overlays/dtb-cpi-pmu.nix
similarity index 100%
rename from uconsole/overlays/dtb-cpi-pmu.nix
rename to raspberry-pi/overlays/dtb-cpi-pmu.nix
diff --git a/uconsole/overlays/dtb-cpi-spi4.nix b/raspberry-pi/overlays/dtb-cpi-spi4.nix
similarity index 100%
rename from uconsole/overlays/dtb-cpi-spi4.nix
rename to raspberry-pi/overlays/dtb-cpi-spi4.nix
diff --git a/uconsole/overlays/dtb-cpu-revision.nix b/raspberry-pi/overlays/dtb-cpu-revision.nix
similarity index 100%
rename from uconsole/overlays/dtb-cpu-revision.nix
rename to raspberry-pi/overlays/dtb-cpu-revision.nix
diff --git a/uconsole/overlays/dtb-disable-genet.nix b/raspberry-pi/overlays/dtb-disable-genet.nix
similarity index 100%
rename from uconsole/overlays/dtb-disable-genet.nix
rename to raspberry-pi/overlays/dtb-disable-genet.nix
diff --git a/uconsole/overlays/dtb-disable-pcie.nix b/raspberry-pi/overlays/dtb-disable-pcie.nix
similarity index 100%
rename from uconsole/overlays/dtb-disable-pcie.nix
rename to raspberry-pi/overlays/dtb-disable-pcie.nix
diff --git a/uconsole/overlays/dtb-panel-uc.nix b/raspberry-pi/overlays/dtb-panel-uc.nix
similarity index 100%
rename from uconsole/overlays/dtb-panel-uc.nix
rename to raspberry-pi/overlays/dtb-panel-uc.nix
diff --git a/raspberry-pi/overlays/dtb-rpi4-disable-pwrled.nix b/raspberry-pi/overlays/dtb-rpi4-disable-pwrled.nix
new file mode 100644
index 0000000..2e530ce
--- /dev/null
+++ b/raspberry-pi/overlays/dtb-rpi4-disable-pwrled.nix
@@ -0,0 +1,30 @@
+{
+  config,
+  lib,
+  ...
+}: let
+  inherit (lib) mkEnableOption mkIf mkMerge mkOption types;
+  cfg = config.hardware.raspberry-pi."4".overlays.rpi4-disable-pwrled;
+in {
+  options.hardware.raspberry-pi."4".overlays.rpi4-disable-pwrled = {
+    enable = mkEnableOption ''overlay enable'';
+    name = mkOption {
+      type = types.str;
+      default = "rpi4-disable-pwrled";
+    };
+  };
+
+  config = mkMerge [
+    (mkIf cfg.enable {
+      hardware.deviceTree = {
+        overlays = [
+          {
+            name = "${cfg.name}";
+            filter = "bcm2711-rpi-4-b.dtb";
+            dtsFile = ./source/rpi4-disable-pwrled.dts;
+          }
+        ];
+      };
+    })
+  ];
+}
diff --git a/uconsole/overlays/dtb-vc4-kms-v3d.nix b/raspberry-pi/overlays/dtb-vc4-kms-v3d.nix
similarity index 100%
rename from uconsole/overlays/dtb-vc4-kms-v3d.nix
rename to raspberry-pi/overlays/dtb-vc4-kms-v3d.nix
diff --git a/uconsole/overlays/source/audremap.dts b/raspberry-pi/overlays/source/audremap.dts
similarity index 100%
rename from uconsole/overlays/source/audremap.dts
rename to raspberry-pi/overlays/source/audremap.dts
diff --git a/uconsole/overlays/source/cpi-bluetooth.dts b/raspberry-pi/overlays/source/cpi-bluetooth.dts
similarity index 100%
rename from uconsole/overlays/source/cpi-bluetooth.dts
rename to raspberry-pi/overlays/source/cpi-bluetooth.dts
diff --git a/uconsole/overlays/source/cpi-i2c1.dts b/raspberry-pi/overlays/source/cpi-i2c1.dts
similarity index 100%
rename from uconsole/overlays/source/cpi-i2c1.dts
rename to raspberry-pi/overlays/source/cpi-i2c1.dts
diff --git a/uconsole/overlays/source/cpi-pmu.dts b/raspberry-pi/overlays/source/cpi-pmu.dts
similarity index 100%
rename from uconsole/overlays/source/cpi-pmu.dts
rename to raspberry-pi/overlays/source/cpi-pmu.dts
diff --git a/uconsole/overlays/source/cpi-spi4.dts b/raspberry-pi/overlays/source/cpi-spi4.dts
similarity index 100%
rename from uconsole/overlays/source/cpi-spi4.dts
rename to raspberry-pi/overlays/source/cpi-spi4.dts
diff --git a/uconsole/overlays/source/cpu-revision.dts b/raspberry-pi/overlays/source/cpu-revision.dts
similarity index 100%
rename from uconsole/overlays/source/cpu-revision.dts
rename to raspberry-pi/overlays/source/cpu-revision.dts
diff --git a/uconsole/overlays/source/disable-genet.dts b/raspberry-pi/overlays/source/disable-genet.dts
similarity index 100%
rename from uconsole/overlays/source/disable-genet.dts
rename to raspberry-pi/overlays/source/disable-genet.dts
diff --git a/uconsole/overlays/source/disable-pcie.dts b/raspberry-pi/overlays/source/disable-pcie.dts
similarity index 100%
rename from uconsole/overlays/source/disable-pcie.dts
rename to raspberry-pi/overlays/source/disable-pcie.dts
diff --git a/uconsole/overlays/source/panel-uc.dts b/raspberry-pi/overlays/source/panel-uc.dts
similarity index 100%
rename from uconsole/overlays/source/panel-uc.dts
rename to raspberry-pi/overlays/source/panel-uc.dts
diff --git a/raspberry-pi/overlays/source/rpi4-disable-pwrled.dts b/raspberry-pi/overlays/source/rpi4-disable-pwrled.dts
new file mode 100644
index 0000000..8685eac
--- /dev/null
+++ b/raspberry-pi/overlays/source/rpi4-disable-pwrled.dts
@@ -0,0 +1,12 @@
+/dts-v1/;
+/plugin/;
+/{
+  compatible = "raspberrypi,4-model-b";
+  fragment@0 {
+    target = <&led_pwr>;
+    __overlay__ {
+      gpios = <&expgpio 2 0>; /* first two values copied from bcm2711-rpi-4-b.dts */
+      linux,default-trigger = "default-on";
+    };
+  };
+};
\ No newline at end of file
diff --git a/uconsole/overlays/source/vc4-kms-v3d.dts b/raspberry-pi/overlays/source/vc4-kms-v3d.dts
similarity index 100%
rename from uconsole/overlays/source/vc4-kms-v3d.dts
rename to raspberry-pi/overlays/source/vc4-kms-v3d.dts
diff --git a/uconsole/packages/rpi-utils/default.nix b/raspberry-pi/packages/rpi-utils/default.nix
similarity index 100%
rename from uconsole/packages/rpi-utils/default.nix
rename to raspberry-pi/packages/rpi-utils/default.nix
diff --git a/uconsole/audio-patch.nix b/uconsole/audio.nix
similarity index 94%
rename from uconsole/audio-patch.nix
rename to uconsole/audio.nix
index 769849e..292a5e2 100644
--- a/uconsole/audio-patch.nix
+++ b/uconsole/audio.nix
@@ -1,5 +1,5 @@
 {pkgs, ...}: let
-  rpi-utils = pkgs.callPackage ./packages/rpi-utils {};
+  rpi-utils = pkgs.callPackage ../raspberry-pi/packages/rpi-utils {};
   audio-patch = pkgs.writeText "audio_3.5_patch.py" ''
     import os
     import time
diff --git a/uconsole/default.nix b/uconsole/default.nix
index 77290b4..38256ae 100644
--- a/uconsole/default.nix
+++ b/uconsole/default.nix
@@ -1,35 +1,37 @@
 {
   pkgs,
   nixos-hardware,
+  lib,
   ...
 }: let
-  rpi-utils = pkgs.callPackage ./packages/rpi-utils {};
+  inherit (lib) mkDefault;
+  rpi-utils = pkgs.callPackage ../raspberry-pi/packages/rpi-utils {};
 in {
   imports =
     [nixos-hardware.nixosModules.raspberry-pi-4]
     ++ [./kernel]
-    ++ [./overlays]
-    ++ [./packages/overlays]
-    ++ [./audio-patch.nix];
+    ++ [../raspberry-pi/overlays]
+    ++ [../raspberry-pi/apply-overlays]
+    ++ [./audio.nix];
 
   config = {
     environment.systemPackages = [rpi-utils];
 
     hardware.raspberry-pi."4" = {
-      xhci.enable = false;
-      dwc2.enable = true;
-      dwc2.dr_mode = "host";
+      xhci.enable = mkDefault false;
+      dwc2.enable = mkDefault true;
+      dwc2.dr_mode = mkDefault "host";
       overlays = {
-        cpu-revision.enable = true;
-        audremap.enable = true;
-        vc4-kms-v3d.enable = true;
-        disable-pcie.enable = true;
-        disable-genet.enable = true;
-        panel-uc.enable = true;
-        cpi-pmu.enable = true;
-        cpi-i2c1.enable = false;
-        cpi-spi4.enable = false;
-        cpi-bluetooth.enable = true;
+        cpu-revision.enable = mkDefault true;
+        audremap.enable = mkDefault true;
+        vc4-kms-v3d.enable = mkDefault true;
+        disable-pcie.enable = mkDefault true;
+        disable-genet.enable = mkDefault true;
+        panel-uc.enable = mkDefault true;
+        cpi-pmu.enable = mkDefault true;
+        cpi-i2c1.enable = mkDefault false;
+        cpi-spi4.enable = mkDefault false;
+        cpi-bluetooth.enable = mkDefault true;
       };
     };
 
@@ -40,25 +42,25 @@ in {
         {
           name = "bcm2711-rpi-cm4";
           params = {
-            # ant2 = "on";
-            audio = "on";
-            spi = "off";
-            i2c_arm = "on";
+            ant2 = mkDefault "off";
+            audio = mkDefault "on";
+            spi = mkDefault "off";
+            i2c_arm = mkDefault "on";
           };
         }
         {
           name = "cpu-revision";
-          params = {cm4-8 = "on";};
+          params = {cm4-8 = mkDefault "on";};
         }
         {
           name = "audremap";
-          params = {pins_12_13 = "on";};
+          params = {pins_12_13 = mkDefault "on";};
         }
         {
           name = "vc4-kms-v3d";
           params = {
-            cma-384 = "on";
-            nohdmi1 = "on";
+            cma-384 = mkDefault "on";
+            nohdmi1 = mkDefault "on";
           };
         }
       ];