diff --git a/raspberry-pi/4/audio.nix b/raspberry-pi/4/audio.nix index 7241b2cf..df2703a6 100644 --- a/raspberry-pi/4/audio.nix +++ b/raspberry-pi/4/audio.nix @@ -1,50 +1,11 @@ +{ lib, ... }: + { - config, - lib, - pkgs, - ... -}: - -let - cfg = config.hardware.raspberry-pi."4".audio; -in -{ - options.hardware = { - raspberry-pi."4".audio = { - enable = lib.mkEnableOption "configuration for audio"; - }; - }; - - config = lib.mkIf cfg.enable { - hardware.deviceTree = { - overlays = [ - # Equivalent to dtparam=audio=on - { - name = "audio-on-overlay"; - dtsText = '' - /dts-v1/; - /plugin/; - / { - compatible = "brcm,bcm2711"; - fragment@0 { - target = <&audio>; - - __overlay__ { - status = "okay"; - }; - }; - }; - ''; - } - ]; - }; - - # set tsched=0 in pulseaudio config to avoid audio glitches - # see https://wiki.archlinux.org/title/PulseAudio/Troubleshooting#Glitches,_skips_or_crackling - hardware.pulseaudio.configFile = lib.mkOverride 990 ( - pkgs.runCommand "default.pa" { } '' - sed 's/module-udev-detect$/module-udev-detect tsched=0/' ${config.hardware.pulseaudio.package}/etc/pulse/default.pa > $out - '' - ); - }; + imports = [ + ../common + (lib.mkRenamedOptionModule + [ "hardware" "raspberry-pi" "4" "audio" "enable" ] + [ "hardware" "raspberry-pi" "audio" "enable" ] + ) + ]; } diff --git a/raspberry-pi/README.md b/raspberry-pi/README.md index 8f7fa228..4ce148e0 100644 --- a/raspberry-pi/README.md +++ b/raspberry-pi/README.md @@ -171,6 +171,19 @@ Use `board-type=0x0d` for the Pi 3B+ and `board-type=0x11` for the Pi 4B. Replac To supply your own file, set `configtxt.file`. The module then ignores `settings` and `deviceTreeOverlays`. +## Audio + +To enable the headphone jack on Raspberry Pi 2, 3, or 4: + +```nix +{ + hardware.raspberry-pi.audio.enable = true; +} +``` + +The audio module loads the [`snd_bcm2835` kernel module](https://github.com/raspberrypi/linux/blob/rpi-6.18.y/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c) to enable headphone audio. +If you use PulseAudio, the module also provides a default configuration to avoid audio glitches. + ## Current limits - No bootloader module: There's no `boot.loader.raspberry-pi` here. Boards rely on `generic-extlinux-compatible` plus U-Boot. Raspberry Pi OS has the GPU firmware load the kernel directly; we go through U-Boot so it reads `extlinux.conf`, which is what gives you the NixOS boot-generation menu and rollbacks. The firmware install module just stages the boot code and (optionally) U-Boot; it doesn't add a firmware-level direct-boot path. Pi 5 boots from SD via U-Boot, but USB, PCIe, and the RP1 don't come up until Linux takes over, so a USB keyboard at the U-Boot prompt won't work on Pi 5 today. diff --git a/raspberry-pi/common/audio.nix b/raspberry-pi/common/audio.nix new file mode 100644 index 00000000..9fa975ed --- /dev/null +++ b/raspberry-pi/common/audio.nix @@ -0,0 +1,71 @@ +{ + config, + lib, + options, + pkgs, + ... +}: + +let + cfg = config.hardware.raspberry-pi.audio; +in +{ + options.hardware.raspberry-pi.audio = { + enable = lib.mkEnableOption "onboard audio through snd_bcm2835 on Raspberry Pi 2, 3, and 4"; + }; + + config = lib.mkIf cfg.enable { + assertions = [ + { + assertion = config.hardware.deviceTree.overlays == [ ]; + message = '' + hardware.raspberry-pi.audio.enable uses the firmware-provided device tree + and cannot be combined with hardware.deviceTree.overlays. + Migrate those overlays to hardware.raspberry-pi.configtxt.deviceTreeOverlays. + See "Device tree overlays" in raspberry-pi/README.md for migration and + firmware installation. + ''; + } + { + assertion = !config.boot.loader.generic-extlinux-compatible.useGenerationDeviceTree; + message = '' + hardware.raspberry-pi.audio.enable requires + boot.loader.generic-extlinux-compatible.useGenerationDeviceTree = false + so U-Boot keeps the firmware's audio configuration. + Remove the conflicting override or set it to false. + ''; + } + { + assertion = config.hardware.raspberry-pi.firmware.enable || (options ? sdImage); + message = '' + Outside SD-image builds, hardware.raspberry-pi.audio.enable requires + hardware.raspberry-pi.firmware.enable = true to install the firmware configuration. + Enable automatic firmware installation as described in "Firmware install" in raspberry-pi/README.md. + ''; + } + ]; + + # Omit FDT/FDTDIR so U-Boot keeps the firmware's audio=on change. + boot.loader.generic-extlinux-compatible.useGenerationDeviceTree = lib.mkDefault false; + # The board profiles use KMS for HDMI audio. Leave firmware-driven HDMI + # kernel parameters to users. + boot.kernelModules = [ "snd_bcm2835" ]; + + services.pulseaudio.configFile = + lib.pipe + { + name = "default.pa"; + src = "${lib.getBin config.services.pulseaudio.package}/etc/pulse/default.pa"; + substitutions = [ + "--replace-fail" + "load-module module-udev-detect" + "load-module module-udev-detect tsched=0" + ]; + } + [ + pkgs.substitute + # Override NixOS's mkDefault (1000), but let explicit user configuration win. + (lib.mkOverride 990) + ]; + }; +} diff --git a/raspberry-pi/common/default.nix b/raspberry-pi/common/default.nix index 8fc9f02f..1fbb61ed 100644 --- a/raspberry-pi/common/default.nix +++ b/raspberry-pi/common/default.nix @@ -1,5 +1,6 @@ { imports = [ + ./audio.nix ./config-txt.nix ./config-txt-defaults.nix ./firmware.nix