Skip to content

Commit daa628a

Browse files
authored
Merge pull request #1490 from mcdonc/mcdonc-rpi4-gpio
add optional config that makes lgpio and pigpio work
2 parents b9d6921 + 50c1d00 commit daa628a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

raspberry-pi/4/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
./cpu-revision.nix
99
./digi-amp-plus.nix
1010
./dwc2.nix
11+
./gpio.nix
1112
./i2c.nix
1213
./leds.nix
1314
./modesetting.nix

raspberry-pi/4/gpio.nix

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{ pkgs, lib, config, ... }:
2+
{
3+
options.hardware.raspberry-pi."4".gpio = {
4+
enable = lib.mkOption {
5+
type = lib.types.bool;
6+
description =
7+
"Enable udev rules and kernelParams that make lgpio and pigpio work";
8+
default = false;
9+
};
10+
};
11+
config = let
12+
cfg = config.hardware.raspberry-pi."4".gpio;
13+
in
14+
lib.mkIf cfg.enable {
15+
users.groups.gpio = lib.mkDefault { };
16+
17+
# the bit that matters to lgpio here is
18+
# "${pkgs.coreutils}/bin/chgrp gpio /dev/%k && chmod 660 /dev/%k"
19+
# see https://github.com/NixOS/nixpkgs/pull/352308
20+
21+
# sudo udevadm test --action=add /dev/gpiochip0 to test
22+
23+
# import lgpio; lgpio.gpiochip_open(0) should show "1" and not raise
24+
# an exception
25+
26+
services.udev.extraRules = lib.mkBefore ''
27+
KERNEL=="gpiomem", GROUP="gpio", MODE="0660"
28+
SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="${pkgs.bash}/bin/bash -c '${pkgs.coreutils}/bin/chgrp gpio /dev/%k && chmod 660 /dev/%k && ${pkgs.coreutils}/bin/chgrp -R gpio /sys/class/gpio && ${pkgs.coreutils}/bin/chmod -R g=u /sys/class/gpio'"
29+
SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="${pkgs.bash}/bin/bash -c '${pkgs.coreutils}/bin/chgrp -R gpio /sys%p && ${pkgs.coreutils}/bin/chmod -R g=u /sys%p'"
30+
'';
31+
32+
boot.kernelParams = [
33+
"iomem=relaxed" # for pigpiod
34+
];
35+
};
36+
}

0 commit comments

Comments
 (0)