From e0c2bdb80a3118e3401886b5084921ffbd3881ce Mon Sep 17 00:00:00 2001
From: coolneng <akasroua@gmail.com>
Date: Wed, 4 May 2022 19:28:18 +0200
Subject: [PATCH] Enable hardware PWM timers and w1-gpio

---
 configuration.nix    | 29 +++++++++++++++++++++++++++++
 modules/periodic.nix | 17 +++++++++++++----
 scripts/SATA-hat.sh  |  0
 3 files changed, 42 insertions(+), 4 deletions(-)
 mode change 100644 => 100755 scripts/SATA-hat.sh

diff --git a/configuration.nix b/configuration.nix
index e21f5ad..c89e671 100644
--- a/configuration.nix
+++ b/configuration.nix
@@ -16,13 +16,42 @@ with pkgs;
     "iomem=relaxed"
     "strict-devmem=0"
   ];
+
+  # Enable SATA-HAT GPIO features
   boot.loader.raspberryPi = {
     enable = true;
     version = 4;
+    firmwareConfig = ''
+      iomem=relaxed
+      strict-devmem=0
+      dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4
+      dtoverlay=w1-gpio
+      dtparam=i2c1=on
+    '';
   };
 
   environment.systemPackages = [ libraspberrypi htop vim ];
 
+  # Load PWM hardware timers
+  boot.kernelModules = [ "pwm_bcm2835" "w1-gpio" "w1-therm" ];
+  hardware.deviceTree = {
+    enable = true;
+    filter = "*-rpi-*.dtb";
+    overlays = [
+      {
+        name = "pwm-2chan";
+        dtboFile = "${device-tree_rpi.overlays}/pwm-2chan.dtbo";
+      }
+      {
+        name = "w1-gpio";
+        dtboFile = "${device-tree_rpi.overlays}/w1-gpio.dtbo";
+      }
+    ];
+  };
+
+  # Enable I2C
+  hardware.i2c.enable = true;
+
   # Add a swap file
   swapDevices = [{
     device = "/swapfile";
diff --git a/modules/periodic.nix b/modules/periodic.nix
index 7347c11..eea644d 100644
--- a/modules/periodic.nix
+++ b/modules/periodic.nix
@@ -30,18 +30,27 @@ in {
   };
 
   # Enable SATA HAT
-  systemd.services.sata-hat = {
+  systemd.services.sata-hat = let
+    overlay-directory =
+      "/boot/nixos/4mamyanz1hlc4wz3c427qjh6rabngwvj-linux-5.10.17-1.20210303-dtbs/overlays/";
+  in {
     description = "Enable software support for SATA Hat";
     wantedBy = [ "zfs-import.target" ];
     script = ''
       ${pkgs.bash}/bin/bash -c "/etc/nixos/scripts/SATA-hat.sh on"
     '';
+    preStart = ''
+      ${pkgs.libraspberrypi}/bin/dtoverlay -d ${overlay-directory} pwm-2chan pin=12 func=4 pin2=13 func2=4
+      ${pkgs.libraspberrypi}/bin/dtoverlay -d ${overlay-directory} w1-gpio
+    '';
+    preStop = ''
+      ${pkgs.libraspberrypi}/bin/dtoverlay -r -d ${overlay-directory} pwm-2chan
+      ${pkgs.libraspberrypi}/bin/dtoverlay -r -d ${overlay-directory} w1-gpio
+      ${pkgs.bash}/bin/bash -c "/etc/nixos/scripts/SATA-hat.sh off"
+    '';
     serviceConfig = {
       Type = "oneshot";
       RemainAfterExit = "yes";
-      ExecStop = ''
-        ${pkgs.bash}/bin/bash -c "/etc/nixos/scripts/SATA-hat.sh off"
-      '';
     };
     before = [ "zfs-import.target" "zfs-import-vault.service" "umount.target" ];
     requires = [ "systemd-udev-settle.service" ];
diff --git a/scripts/SATA-hat.sh b/scripts/SATA-hat.sh
old mode 100644
new mode 100755