From 128dad1c8d358c4941fa7cd14cf5a2715eea94d6 Mon Sep 17 00:00:00 2001 From: Karolis Stasaitis Date: Sun, 7 Aug 2022 11:16:37 +0200 Subject: [PATCH 1/6] raspberry-pi-4: add poe-plus-hat, update poe-hat, use dtmerge instead of ftoverlay --- raspberry-pi/4/apply-overlays-dtmerge.nix | 30 +++ raspberry-pi/4/default.nix | 2 + raspberry-pi/4/pkgs-overlays.nix | 22 +++ raspberry-pi/4/poe-hat.nix | 81 ++++++-- raspberry-pi/4/poe-plus-hat.nix | 223 ++++++++++++++++++++++ 5 files changed, 343 insertions(+), 15 deletions(-) create mode 100644 raspberry-pi/4/apply-overlays-dtmerge.nix create mode 100644 raspberry-pi/4/pkgs-overlays.nix create mode 100644 raspberry-pi/4/poe-plus-hat.nix diff --git a/raspberry-pi/4/apply-overlays-dtmerge.nix b/raspberry-pi/4/apply-overlays-dtmerge.nix new file mode 100644 index 0000000..0d17bb8 --- /dev/null +++ b/raspberry-pi/4/apply-overlays-dtmerge.nix @@ -0,0 +1,30 @@ +{ lib, pkgs, stdenvNoCC, dtc, libraspberrypi }: + +with lib; { + applyOverlays = (base: overlays': stdenvNoCC.mkDerivation { + name = "device-tree-overlays"; + nativeBuildInputs = [ dtc ]; + buildCommand = let + overlays = toList overlays'; + 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 + dtbCompat="$( fdtget -t s $dtb / compatible )" + ${flip (concatMapStringsSep "\n") overlays (o: '' + overlayCompat="$( fdtget -t s ${o.dtboFile} / compatible )" + # overlayCompat in dtbCompat + if [[ "$dtbCompat" =~ "$overlayCompat" ]]; then + echo "Applying overlay ${o.name} to $( basename $dtb )" + mv $dtb{,.in} + cp ${o.dtboFile}{,.dtbo} + ${libraspberrypi}/bin/dtmerge "$dtb.in" "$dtb" ${o.dtboFile}.dtbo; + rm $dtb.in ${o.dtboFile}.dtbo + fi + '')} + done + ''; + }); +} diff --git a/raspberry-pi/4/default.nix b/raspberry-pi/4/default.nix index fcaf5ad..d85063a 100644 --- a/raspberry-pi/4/default.nix +++ b/raspberry-pi/4/default.nix @@ -7,8 +7,10 @@ ./i2c.nix ./modesetting.nix ./poe-hat.nix + ./poe-plus-hat.nix ./tc358743.nix ./pwm0.nix + ./pkgs-overlays.nix ]; boot = { diff --git a/raspberry-pi/4/pkgs-overlays.nix b/raspberry-pi/4/pkgs-overlays.nix new file mode 100644 index 0000000..4b3c16d --- /dev/null +++ b/raspberry-pi/4/pkgs-overlays.nix @@ -0,0 +1,22 @@ +{ config, lib, pkgs, modulesPath, ... }: +let + callPackage = path: overrides: + let f = import path; + in f ((builtins.intersectAttrs (builtins.functionArgs f) pkgs) // overrides); + cfg = config.hardware.raspberry-pi."4".apply-overlays-dtmerge; + dt_ao_overlay = (final: prev: { + deviceTree.applyOverlays = (prev.callPackage ./apply-overlays-dtmerge.nix { }).applyOverlays; + }); +in { + options.hardware = { + raspberry-pi."4".apply-overlays-dtmerge = { + enable = lib.mkEnableOption '' + replace deviceTree.applyOverlays implementation to use dtmerge from libraspberrypi. + ''; + }; + }; + + config = lib.mkIf cfg.enable { + nixpkgs.overlays = [ dt_ao_overlay ]; + }; +} diff --git a/raspberry-pi/4/poe-hat.nix b/raspberry-pi/4/poe-hat.nix index ea19129..bda9925 100644 --- a/raspberry-pi/4/poe-hat.nix +++ b/raspberry-pi/4/poe-hat.nix @@ -1,6 +1,6 @@ { config, lib, pkgs, ... }: -let +let cfg = config.hardware.raspberry-pi."4".poe-hat; in { options.hardware = { @@ -12,10 +12,11 @@ in { }; config = lib.mkIf cfg.enable { - # Configure for modesetting in the device tree + hardware.raspberry-pi."4".apply-overlays-dtmerge.enable = lib.mkDefault true; + hardware.deviceTree = { overlays = [ - # Equivalent to: https://github.com/raspberrypi/linux/blob/rpi-5.10.y/arch/arm/boot/dts/overlays/rpi-poe-overlay.dts + # Equivalent to: https://github.com/raspberrypi/linux/blob/rpi-5.15.y/arch/arm/boot/dts/overlays/rpi-poe-overlay.dts { name = "rpi-poe-overlay"; dtsText = '' @@ -31,14 +32,11 @@ in { fragment@0 { target-path = "/"; __overlay__ { - fan0: rpi-poe-fan@0 { - compatible = "raspberrypi,rpi-poe-fan"; - firmware = <&firmware>; - cooling-min-state = <0>; - cooling-max-state = <4>; - #cooling-cells = <2>; + fan: pwm-fan { + compatible = "pwm-fan"; cooling-levels = <0 1 10 100 255>; - status = "okay"; + #cooling-cells = <2>; + pwms = <&fwpwm 0 80000>; }; }; }; @@ -71,19 +69,19 @@ in { cooling-maps { map0 { trip = <&trip0>; - cooling-device = <&fan0 0 1>; + cooling-device = <&fan 0 1>; }; map1 { trip = <&trip1>; - cooling-device = <&fan0 1 2>; + cooling-device = <&fan 1 2>; }; map2 { trip = <&trip2>; - cooling-device = <&fan0 2 3>; + cooling-device = <&fan 2 3>; }; map3 { trip = <&trip3>; - cooling-device = <&fan0 3 4>; + cooling-device = <&fan 3 4>; }; }; }; @@ -91,7 +89,7 @@ in { fragment@2 { target-path = "/__overrides__"; - __overlay__ { + params: __overlay__ { poe_fan_temp0 = <&trip0>,"temperature:0"; poe_fan_temp0_hyst = <&trip0>,"hysteresis:0"; poe_fan_temp1 = <&trip1>,"temperature:0"; @@ -100,6 +98,54 @@ in { poe_fan_temp2_hyst = <&trip2>,"hysteresis:0"; poe_fan_temp3 = <&trip3>,"temperature:0"; poe_fan_temp3_hyst = <&trip3>,"hysteresis:0"; + poe_fan_i2c = <&fwpwm>,"status=disabled", + <&poe_mfd>,"status=okay", + <&fan>,"pwms:0=",<&poe_mfd_pwm>; + }; + }; + + fragment@3 { + target = <&firmware>; + __overlay__ { + fwpwm: pwm { + compatible = "raspberrypi,firmware-poe-pwm"; + #pwm-cells = <2>; + }; + }; + }; + + fragment@4 { + target = <&i2c0>; + i2c_bus: __overlay__ { + #address-cells = <1>; + #size-cells = <0>; + + poe_mfd: poe@51 { + compatible = "raspberrypi,poe-core"; + reg = <0x51>; + status = "disabled"; + + poe_mfd_pwm: poe_pwm@f0 { + compatible = "raspberrypi,poe-pwm"; + reg = <0xf0>; + status = "okay"; + #pwm-cells = <2>; + }; + }; + }; + }; + + fragment@5 { + target = <&i2c0if>; + __dormant__ { + status = "okay"; + }; + }; + + fragment@6 { + target = <&i2c0mux>; + __dormant__ { + status = "okay"; }; }; @@ -112,6 +158,11 @@ in { poe_fan_temp2_hyst = <&trip2>,"hysteresis:0"; poe_fan_temp3 = <&trip3>,"temperature:0"; poe_fan_temp3_hyst = <&trip3>,"hysteresis:0"; + i2c = <0>, "+5+6", + <&fwpwm>,"status=disabled", + <&i2c_bus>,"status=okay", + <&poe_mfd>,"status=okay", + <&fan>,"pwms:0=",<&poe_mfd_pwm>; }; }; ''; diff --git a/raspberry-pi/4/poe-plus-hat.nix b/raspberry-pi/4/poe-plus-hat.nix new file mode 100644 index 0000000..79177fe --- /dev/null +++ b/raspberry-pi/4/poe-plus-hat.nix @@ -0,0 +1,223 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.hardware.raspberry-pi."4".poe-plus-hat; +in { + options.hardware = { + raspberry-pi."4".poe-plus-hat = { + enable = lib.mkEnableOption '' + support for the Raspberry Pi PoE+ HAT. + ''; + }; + }; + + config = lib.mkIf cfg.enable { + hardware.raspberry-pi."4".apply-overlays-dtmerge.enable = lib.mkDefault true; + + hardware.deviceTree = { + overlays = [ + # Combined equivalent to: + # * https://github.com/raspberrypi/linux/blob/rpi-5.15.y/arch/arm/boot/dts/overlays/rpi-poe-overlay.dts + # * https://github.com/raspberrypi/linux/blob/rpi-5.15.y/arch/arm/boot/dts/overlays/rpi-poe-plus-overlay.dts + { + name = "rpi-poe-plus-overlay"; + dtsText = '' + /* + * Overlay for the Raspberry Pi POE HAT. + */ + /dts-v1/; + /plugin/; + + / { + compatible = "brcm,bcm2711"; + + fragment@0 { + target-path = "/"; + __overlay__ { + fan: pwm-fan { + compatible = "pwm-fan"; + cooling-levels = <0 1 10 100 255>; + #cooling-cells = <2>; + pwms = <&fwpwm 0 80000>; + }; + }; + }; + + fragment@1 { + target = <&cpu_thermal>; + __overlay__ { + trips { + trip0: trip0 { + temperature = <40000>; + hysteresis = <2000>; + type = "active"; + }; + trip1: trip1 { + temperature = <45000>; + hysteresis = <2000>; + type = "active"; + }; + trip2: trip2 { + temperature = <50000>; + hysteresis = <2000>; + type = "active"; + }; + trip3: trip3 { + temperature = <55000>; + hysteresis = <5000>; + type = "active"; + }; + }; + cooling-maps { + map0 { + trip = <&trip0>; + cooling-device = <&fan 0 1>; + }; + map1 { + trip = <&trip1>; + cooling-device = <&fan 1 2>; + }; + map2 { + trip = <&trip2>; + cooling-device = <&fan 2 3>; + }; + map3 { + trip = <&trip3>; + cooling-device = <&fan 3 4>; + }; + }; + }; + }; + + fragment@2 { + target-path = "/__overrides__"; + params: __overlay__ { + poe_fan_temp0 = <&trip0>,"temperature:0"; + poe_fan_temp0_hyst = <&trip0>,"hysteresis:0"; + poe_fan_temp1 = <&trip1>,"temperature:0"; + poe_fan_temp1_hyst = <&trip1>,"hysteresis:0"; + poe_fan_temp2 = <&trip2>,"temperature:0"; + poe_fan_temp2_hyst = <&trip2>,"hysteresis:0"; + poe_fan_temp3 = <&trip3>,"temperature:0"; + poe_fan_temp3_hyst = <&trip3>,"hysteresis:0"; + poe_fan_i2c = <&fwpwm>,"status=disabled", + <&poe_mfd>,"status=okay", + <&fan>,"pwms:0=",<&poe_mfd_pwm>; + }; + }; + + fragment@3 { + target = <&firmware>; + __overlay__ { + fwpwm: pwm { + compatible = "raspberrypi,firmware-poe-pwm"; + #pwm-cells = <2>; + }; + }; + }; + + fragment@4 { + target = <&i2c0>; + i2c_bus: __overlay__ { + #address-cells = <1>; + #size-cells = <0>; + + poe_mfd: poe@51 { + compatible = "raspberrypi,poe-core"; + reg = <0x51>; + status = "disabled"; + + poe_mfd_pwm: poe_pwm@f0 { + compatible = "raspberrypi,poe-pwm"; + reg = <0xf0>; + status = "okay"; + #pwm-cells = <2>; + }; + }; + }; + }; + + fragment@5 { + target = <&i2c0if>; + __dormant__ { + status = "okay"; + }; + }; + + fragment@6 { + target = <&i2c0mux>; + __dormant__ { + status = "okay"; + }; + }; + + __overrides__ { + poe_fan_temp0 = <&trip0>,"temperature:0"; + poe_fan_temp0_hyst = <&trip0>,"hysteresis:0"; + poe_fan_temp1 = <&trip1>,"temperature:0"; + poe_fan_temp1_hyst = <&trip1>,"hysteresis:0"; + poe_fan_temp2 = <&trip2>,"temperature:0"; + poe_fan_temp2_hyst = <&trip2>,"hysteresis:0"; + poe_fan_temp3 = <&trip3>,"temperature:0"; + poe_fan_temp3_hyst = <&trip3>,"hysteresis:0"; + i2c = <0>, "+5+6", + <&fwpwm>,"status=disabled", + <&i2c_bus>,"status=okay", + <&poe_mfd>,"status=okay", + <&fan>,"pwms:0=",<&poe_mfd_pwm>; + }; + }; + + // SPDX-License-Identifier: (GPL-2.0 OR MIT) + // Overlay for the Raspberry Pi PoE+ HAT. + + / { + compatible = "brcm,bcm2711"; + + fragment@10 { + target-path = "/"; + __overlay__ { + rpi_poe_power_supply: rpi-poe-power-supply { + compatible = "raspberrypi,rpi-poe-power-supply"; + firmware = <&firmware>; + status = "okay"; + }; + }; + }; + fragment@11 { + target = <&poe_mfd>; + __overlay__ { + rpi-poe-power-supply@f2 { + compatible = "raspberrypi,rpi-poe-power-supply"; + reg = <0xf2>; + status = "okay"; + }; + }; + }; + + __overrides__ { + i2c = <0>, "+5+6", + <&fwpwm>,"status=disabled", + <&rpi_poe_power_supply>,"status=disabled", + <&i2c_bus>,"status=okay", + <&poe_mfd>,"status=okay", + <&fan>,"pwms:0=",<&poe_mfd_pwm>; + }; + }; + + &fan { + cooling-levels = <0 32 64 128 255>; + }; + + ¶ms { + poe_fan_i2c = <&fwpwm>,"status=disabled", + <&rpi_poe_power_supply>,"status=disabled", + <&poe_mfd>,"status=okay", + <&fan>,"pwms:0=",<&poe_mfd_pwm>; + }; + ''; + } + ]; + }; + }; +} From 245d8f9f97d35a607822ed51bafd7c105c0e819a Mon Sep 17 00:00:00 2001 From: Karolis Stasaitis Date: Mon, 8 Aug 2022 18:27:49 +0200 Subject: [PATCH 2/6] apply review suggestions from @06kellyjac Co-authored-by: j-k --- raspberry-pi/4/apply-overlays-dtmerge.nix | 4 +++- raspberry-pi/4/pkgs-overlays.nix | 6 ++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/raspberry-pi/4/apply-overlays-dtmerge.nix b/raspberry-pi/4/apply-overlays-dtmerge.nix index 0d17bb8..8a325ab 100644 --- a/raspberry-pi/4/apply-overlays-dtmerge.nix +++ b/raspberry-pi/4/apply-overlays-dtmerge.nix @@ -1,9 +1,11 @@ +# modification of nixpkgs deviceTree.applyOverlays to resolve https://github.com/NixOS/nixpkgs/issues/125354 +# https://github.com/NixOS/nixpkgs/blob/master/pkgs/os-specific/linux/device-tree/default.nix { lib, pkgs, stdenvNoCC, dtc, libraspberrypi }: with lib; { applyOverlays = (base: overlays': stdenvNoCC.mkDerivation { name = "device-tree-overlays"; - nativeBuildInputs = [ dtc ]; + nativeBuildInputs = [ dtc libraspberrypi ]; buildCommand = let overlays = toList overlays'; in '' diff --git a/raspberry-pi/4/pkgs-overlays.nix b/raspberry-pi/4/pkgs-overlays.nix index 4b3c16d..c290d2a 100644 --- a/raspberry-pi/4/pkgs-overlays.nix +++ b/raspberry-pi/4/pkgs-overlays.nix @@ -1,17 +1,15 @@ { config, lib, pkgs, modulesPath, ... }: let - callPackage = path: overrides: - let f = import path; - in f ((builtins.intersectAttrs (builtins.functionArgs f) pkgs) // overrides); cfg = config.hardware.raspberry-pi."4".apply-overlays-dtmerge; dt_ao_overlay = (final: prev: { - deviceTree.applyOverlays = (prev.callPackage ./apply-overlays-dtmerge.nix { }).applyOverlays; + deviceTree.applyOverlays = (prev.callPackage ./apply-overlays-dtmerge.nix { }) }); in { options.hardware = { raspberry-pi."4".apply-overlays-dtmerge = { enable = lib.mkEnableOption '' replace deviceTree.applyOverlays implementation to use dtmerge from libraspberrypi. + this can resolve issues with applying dtbs for the pi. ''; }; }; From 3f0991b531648e1a529d8eb14b483d628c29846b Mon Sep 17 00:00:00 2001 From: Karolis Stasaitis Date: Mon, 8 Aug 2022 18:41:30 +0200 Subject: [PATCH 3/6] export a single function from apply-overlays-dtmerge --- raspberry-pi/4/apply-overlays-dtmerge.nix | 54 +++++++++++------------ 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/raspberry-pi/4/apply-overlays-dtmerge.nix b/raspberry-pi/4/apply-overlays-dtmerge.nix index 8a325ab..7117509 100644 --- a/raspberry-pi/4/apply-overlays-dtmerge.nix +++ b/raspberry-pi/4/apply-overlays-dtmerge.nix @@ -2,31 +2,29 @@ # https://github.com/NixOS/nixpkgs/blob/master/pkgs/os-specific/linux/device-tree/default.nix { lib, pkgs, stdenvNoCC, dtc, libraspberrypi }: -with lib; { - applyOverlays = (base: overlays': stdenvNoCC.mkDerivation { - name = "device-tree-overlays"; - nativeBuildInputs = [ dtc libraspberrypi ]; - buildCommand = let - overlays = toList overlays'; - 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 - dtbCompat="$( fdtget -t s $dtb / compatible )" - ${flip (concatMapStringsSep "\n") overlays (o: '' - overlayCompat="$( fdtget -t s ${o.dtboFile} / compatible )" - # overlayCompat in dtbCompat - if [[ "$dtbCompat" =~ "$overlayCompat" ]]; then - echo "Applying overlay ${o.name} to $( basename $dtb )" - mv $dtb{,.in} - cp ${o.dtboFile}{,.dtbo} - ${libraspberrypi}/bin/dtmerge "$dtb.in" "$dtb" ${o.dtboFile}.dtbo; - rm $dtb.in ${o.dtboFile}.dtbo - fi - '')} - done - ''; - }); -} +with lib; (base: overlays': stdenvNoCC.mkDerivation { + name = "device-tree-overlays"; + nativeBuildInputs = [ dtc libraspberrypi ]; + buildCommand = let + overlays = toList overlays'; + 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 + dtbCompat="$( fdtget -t s $dtb / compatible )" + ${flip (concatMapStringsSep "\n") overlays (o: '' + overlayCompat="$( fdtget -t s ${o.dtboFile} / compatible )" + # overlayCompat in dtbCompat + if [[ "$dtbCompat" =~ "$overlayCompat" ]]; then + echo "Applying overlay ${o.name} to $( basename $dtb )" + mv $dtb{,.in} + cp ${o.dtboFile}{,.dtbo} + ${libraspberrypi}/bin/dtmerge "$dtb.in" "$dtb" ${o.dtboFile}.dtbo; + rm $dtb.in ${o.dtboFile}.dtbo + fi + '')} + done + ''; +}); From f410badac50c0710c1b98ad7de23c48848b3a84f Mon Sep 17 00:00:00 2001 From: Karolis Stasaitis Date: Mon, 8 Aug 2022 18:46:26 +0200 Subject: [PATCH 4/6] added missing semicolon --- raspberry-pi/4/pkgs-overlays.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raspberry-pi/4/pkgs-overlays.nix b/raspberry-pi/4/pkgs-overlays.nix index c290d2a..71db89f 100644 --- a/raspberry-pi/4/pkgs-overlays.nix +++ b/raspberry-pi/4/pkgs-overlays.nix @@ -2,7 +2,7 @@ let cfg = config.hardware.raspberry-pi."4".apply-overlays-dtmerge; dt_ao_overlay = (final: prev: { - deviceTree.applyOverlays = (prev.callPackage ./apply-overlays-dtmerge.nix { }) + deviceTree.applyOverlays = (prev.callPackage ./apply-overlays-dtmerge.nix { }); }); in { options.hardware = { From bfc438a27590e225d4eddd180a7d332ceadb67ea Mon Sep 17 00:00:00 2001 From: Karolis Stasaitis Date: Mon, 8 Aug 2022 18:56:33 +0200 Subject: [PATCH 5/6] misplaced semicolon --- raspberry-pi/4/apply-overlays-dtmerge.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raspberry-pi/4/apply-overlays-dtmerge.nix b/raspberry-pi/4/apply-overlays-dtmerge.nix index 7117509..c890e5a 100644 --- a/raspberry-pi/4/apply-overlays-dtmerge.nix +++ b/raspberry-pi/4/apply-overlays-dtmerge.nix @@ -27,4 +27,4 @@ with lib; (base: overlays': stdenvNoCC.mkDerivation { '')} done ''; -}); +}) From f217e0c09ae7a951e6fe5ed08c2a87a8729118cf Mon Sep 17 00:00:00 2001 From: Karolis Stasaitis Date: Mon, 8 Aug 2022 19:07:05 +0200 Subject: [PATCH 6/6] use dtmerge from nativeBuildInputs --- raspberry-pi/4/apply-overlays-dtmerge.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raspberry-pi/4/apply-overlays-dtmerge.nix b/raspberry-pi/4/apply-overlays-dtmerge.nix index c890e5a..4e4bc7b 100644 --- a/raspberry-pi/4/apply-overlays-dtmerge.nix +++ b/raspberry-pi/4/apply-overlays-dtmerge.nix @@ -21,7 +21,7 @@ with lib; (base: overlays': stdenvNoCC.mkDerivation { echo "Applying overlay ${o.name} to $( basename $dtb )" mv $dtb{,.in} cp ${o.dtboFile}{,.dtbo} - ${libraspberrypi}/bin/dtmerge "$dtb.in" "$dtb" ${o.dtboFile}.dtbo; + dtmerge "$dtb.in" "$dtb" ${o.dtboFile}.dtbo; rm $dtb.in ${o.dtboFile}.dtbo fi '')}