diff --git a/cm4/default.nix b/cm4/default.nix new file mode 100644 index 0000000..d77fff3 --- /dev/null +++ b/cm4/default.nix @@ -0,0 +1,37 @@ +{ + config, + lib, + fn, + nixos-hardware, + ... +}: let + inherit (lib) mkIf mkEnableOption mkOption types; + cfg = config.hardware.cm4; +in { + options.hardware.cm4 = { + enable = mkEnableOption "custom CM4"; + }; + + imports = [nixos-hardware.nixosModules.raspberry-pi-4] ++ (fn.scanPaths ./.); + + config = mkIf cfg.enable { + hardware.raspberry-pi."4" = { + apply-overlays-dtmerge.enable = true; + xhci.enable = true; + overlays = { + audremap.enable = true; + spi-gpio40-45.enable = true; + }; + }; + + hardware.deviceTree = { + enable = true; + filter = "*-rpi-cm4.dtb"; + }; + + users.groups.spi = {}; + services.udev.extraRules = '' + SUBSYSTEM=="spidev", KERNEL=="spidev0.0", GROUP="spi", MODE="0660" + ''; + }; +} diff --git a/cm4/dtb-audremap.nix b/cm4/dtb-audremap.nix new file mode 100644 index 0000000..641a7cd --- /dev/null +++ b/cm4/dtb-audremap.nix @@ -0,0 +1,51 @@ +{ + config, + lib, + ... +}: let + inherit (lib) mkEnableOption mkIf mkMerge; + cfg = config.hardware.raspberry-pi."4".overlays; +in { + options.hardware = { + raspberry-pi."4".overlays.audremap.enable = mkEnableOption ''audremap enable ''; + }; + + config = mkMerge [ + (mkIf cfg.audremap.enable { + hardware.deviceTree.overlays = [ + { + name = "audremap.overlay"; + filter = "*rpi-cm4*"; + dtsText = '' + /dts-v1/; + /plugin/; + / { + compatible = "brcm,bcm2711"; + fragment@0 { + target = <&audio_pins>; + frag0: __overlay__ { + brcm,pins = <12 13>; + brcm,function = <4>; /* alt0 alt0 */ + }; + }; + fragment@1 { + target = <&chosen>; + __overlay__ { + bootargs = "snd_bcm2835.enable_headphones=1"; + }; + }; + __overrides__ { + swap_lr = <&frag0>, "swap_lr?"; + enable_jack = <&frag0>, "enable_jack?"; + pins_12_13 = <&frag0>,"brcm,pins:0=12", <&frag0>,"brcm,pins:4=13", <&frag0>,"brcm,function:0=4"; + pins_18_19 = <&frag0>,"brcm,pins:0=18", <&frag0>,"brcm,pins:4=19", <&frag0>,"brcm,function:0=2"; + pins_40_41 = <&frag0>,"brcm,pins:0=40", <&frag0>,"brcm,pins:4=41", <&frag0>,"brcm,function:0=4"; + pins_40_45 = <&frag0>,"brcm,pins:0=40", <&frag0>,"brcm,pins:4=45", <&frag0>,"brcm,function:0=4"; + }; + }; + ''; + } + ]; + }) + ]; +} diff --git a/cm4/dtb-gpio40-45.nix b/cm4/dtb-gpio40-45.nix new file mode 100644 index 0000000..c30a0c4 --- /dev/null +++ b/cm4/dtb-gpio40-45.nix @@ -0,0 +1,55 @@ +{ + config, + lib, + ... +}: let + inherit (lib) mkEnableOption mkIf mkMerge; + cfg = config.hardware.raspberry-pi."4".overlays; +in { + options.hardware = { + raspberry-pi."4".overlays.spi-gpio40-45.enable = mkEnableOption ''spi-gpio40-45 enable ''; + }; + + config = mkMerge [ + (mkIf cfg.spi-gpio40-45.enable { + hardware.deviceTree = { + overlays = [ + { + name = "spi-gpio40-45.overlay"; + filter = "*rpi-cm4*"; + dtsText = '' + /dts-v1/; + /plugin/; + / { + compatible = "brcm,bcm2711"; + fragment@0 { + target = <&spi0>; + __overlay__ { + cs-gpios = <&gpio 43 1>, <&gpio 44 1>, <&gpio 45 1>; + status = "okay"; + }; + }; + fragment@1 { + target = <&spi0_cs_pins>; + __overlay__ { + brcm,pins = <45 44 43>; + brcm,function = <1>; /* output */ + status = "okay"; + }; + }; + fragment@2 { + target = <&spi0_pins>; + __overlay__ { + brcm,pins = <40 41 42>; + brcm,function = <3>; /* alt4 */ + status = "okay"; + }; + }; + }; + ''; + } + ]; + }; + }) + ]; +}