Clean kernel driver for S/PDIF on a Pi GPIO (no HAT), plus lip-synced multi-room and light -- add-ons, and a question

  • Hi,

    S/PDIF on GPIO21 of a Raspberry Pi is not a new idea: raspdif and rpi-i2s-spdif do it, and I learned a lot from both. What I was missing was a clean kernel driver that behaves like any other sound card on a current kernel, without /dev/mem and without a daemon next to Kodi. So I wrote one, and packaged it as a LibreELEC add-on.

    https://github.com/ehb/rpi-gpio-spdif -- an ordinary ALSA card `SPDIFout` using the kernel's dmaengine. Hardware is just a TOSLINK transmitter module on GPIO21 (pin 40), 5 V and GND. In Kodi: AC3/DTS passthrough through the standard IEC958 controls, AC3 transcoding, and correct lip sync because the driver reports its delay.

    It grew into a small set of independent pieces. How they fit together, with a recipe: https://github.com/ehb/pi-lockstep

    - alsa-leadtap (https://github.com/ehb/alsa-leadtap): an ALSA plugin, also a Kodi add-on, that works in front of any sound card including HDMI. It hands out a copy of the audio a fixed time before it is heard, so Snapcast rooms stay in sync with the local output and the picture without hand-tuned latencies.

    - wled-audiosync (https://github.com/ehb/wled-audiosync): sound-reactive WLED from that copy, no microphone, timed to what you hear.

    - tvlight (https://github.com/ehb/tvlight): Kodi add-on for ambient light on WLED strips from the screen edges, without an HDMI grabber (Pi 4).

    Everything is installable through "Install from zip file" from the Releases pages of the repositories; the driver zip for LibreELEC 12.2.1 is for the RPi4 image. After installing alsa-leadtap, restart Kodi once.

    The question is about the driver: a self-built add-on breaks with every kernel update. For now GitHub Actions watches for new LibreELEC releases and builds a matching zip against that release's kernel. The driver also compiles against the kernel of current master (6.18), but has not been run there.

    1. Would you consider it for `packages/linux-driver-addons` (or as a module in the RPi image)? I would maintain it for now, as long as I run it on a Pi myself. CONTRIBUTING says third-party drivers should come from whoever maintains them, so that may be a reason against it.

    2. If not: is a zip per release from my own GitHub the right way for an out-of-tree module, or is there a preferred one?

    Known limits: tested only on one Pi 4B with one receiver; not for the Pi 5 (different I2S block); only GPIO21.

    Disclosure: the code was written with Claude Code (AI assistant) and reviewed by other models; I tested it on my hardware but have not reviewed every line myself.

  • Try to get it included in the official RPi kernel trees https://github.com/raspberrypi/linux then we'll pick it from there and can enable the module build.

    We won't be adding any out-of-tree kernel drivers to our images (also not as a driver addon) as that creates a lot of support burden on our side on kernel updates (often even patch-version updates).

    so long,

    Hias

  • Thanks Hias, that makes sense, and it's the answer I needed. I'll keep the add-on zips going for now and look into submitting it to raspberrypi/linux. That will need some more work first (device tree binding, and probably a closer look at how it fits with ASoC), and I'll have to see whether the AI-assisted origin of the code is acceptable there. If it ever lands in the RPi tree, I'll come back here.

  • An update, in case it is useful to anyone here: after the advice in this thread and from the Raspberry Pi engineers, the driver is gone. S/PDIF on GPIO21 is now an ALSA plugin on top of the stock bcm2835-i2s driver and the hifiberry-dac overlay, so it no longer depends on the kernel version and should survive LibreELEC updates as is.

    It is packaged as a Kodi add-on, service.rpi-gpio-spdif, in a small add-on repository together with the other two: https://github.com/ehb/pi-lockstep (repository zip: https://ehb.github.io/pi-lockstep/repository.ehb-1.0.0.zip). After installing the add-on, restart Kodi once so that it sees the new device, and keep E-AC3, TrueHD and DTS-HD passthrough switched off: Kodi offers them because of the device name, but they do not fit through S/PDIF.

    Tested on one Pi 4B with LibreELEC 12.2.1: PCM, AC3 and DTS passthrough, lip sync. No dmix, and passthrough relies on the device name beginning with hdmi. The Pi 5 is untested; if someone tries it, I would like to hear how it goes.

    Hias, thanks for the clear answer, it got this onto a much better path.