Skip to content

Lothar/gpiod#1849

Closed
Rubusch wants to merge 4 commits intolabgrid-project:masterfrom
Rubusch:lothar/gpiod
Closed

Lothar/gpiod#1849
Rubusch wants to merge 4 commits intolabgrid-project:masterfrom
Rubusch:lothar/gpiod

Conversation

@Rubusch
Copy link
Copy Markdown

@Rubusch Rubusch commented Apr 5, 2026

Add descriptor based GPIO support.


To be honest I did not exactly follow the discussion, but I'm pretty sure there is one. I'm probably not the first one asking for descriptor based GPIO handling (and probably this won't be the last patch set on it, to be honest).

I had several solutions in the past where the GPIO needed to be actively hold. At work, I implemented holding their state using systemd jobs and gpiod tools with the wait option for v1.x of the lib. With libgpiod v2.x around, Bartosz implemented some DBUS server solution, AFAIK. The tools changed a bit and since then, one can hold them in daemon mode with a registered "customer" tag. I tried implementing direct control using the python3 gpiod library, but unfortunately this would require an active daemon holding state. As I understand labgrid, an agent callling to activate/deactivate GPIO lines is a pretty stateless implementation (which I appreciate). I did not wanted to introduce a daemon implementation holding and owning the GPIO. I'm mainly here on RPi hardware for the Exporters, but also did not want to rely only on RPi libs.
The solution now - the perhaps ugly part of this series - I implemented holding GPIOs using the gpiod utils and registering "labgrid_" as customer. I capture the PID and signal the process at state transition. It allows this only for the owner of the process, which means if the GPIO was held by another process labgrid will complain already at claiming the GPIO initialy, which is good IMHO. To be honest I was not happy with any other approach I tried, and this is the one I ended up using since. It worked out pretty stable for us.

For compatibility I implemented it aside Sysfsgpio. Therefore pretty every class around for something with Sysfsgpio, is added with a brother for Gpiodgpio. One exception is MatchedGpios which I do not use, therefore did not test, therefore did not add to the implementation (I guess just few additional lines of code).

A second part of the patches is pre-initialization. I'm doing boot mode and boot media switching on some soldered IO lines. When powering the device in some cases this has to be in defined state, so I added a kind of optional pre-initialization support. The idea is, you can on the client side configure initially value, high or low. When acquiring the Place it will set the GPIOs accordingly.

If you think this could be somehow useful feel free to take it, or drop it or discuss it for further upstreaming.

Rubusch added 4 commits April 5, 2026 12:21
Labgrid's existing GPIO implementation relies on the deprecated sysfs
interface. Modern descriptor-based GPIO handling (via character devices)
requires a process to maintain an active line request to keep the GPIO
state stable.

With libgpiod v2.x, the tools can now hold a GPIO state via a persistent
DBUS implementation. By registering a "consumer", the GPIO remains
claimed by the utility, shifting the daemon/persistence handling from
Labgrid to the underlying GPIO tools.

This allows Labgrid to support modern GPIO descriptors while maintaining
its current architecture. This implementation runs in parallel to the
existing sysfs support to ensure backward compatibility.

Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
Adds description and documentation of GpiodGPIO and
GpiodDigitalOutputDriver.

The approach implements descriptor based GPIO handling aside of Sysfs
based GPIO handling. In this way both won't interfere. Compatibility
with the sysfs appraoch is there, and on platforms which are switching
more and more towards modern descriptor based GPIO handling will be
enabled.

Starting with v2.0 the DBUS hold GPIO implementation of the GPIO tools
is around. So the easiest approach is to rely on the GPIO utils process
to be named as "labgrid_<offset>" and hold the IO line in a stable
state. Also since python's GPIO libs do not implement directly means of
holding the IO line stable. In case of labgrid where an agent / wrapper
only is used for activation / deactivation an internal python
implementation might require quite som arquitectural impact, basically
for setting up a process as daemon. GPIO utils are external processes
performing exactly the same, cross dependency is low and impact for
labgrid is reasonable.

Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
Add a pre-initialization for gpiod based gpios implementing
DigitalOutputProtocol. By specifying the optional property
`initval: bool` in the Place config of the client to the
GpiodDigitalOutputDriver configuration, the GPIO line will
be set accordingly at acquiring the place.

This is supposed to help in situations where a pre-initialization
is crucial to avoid damage or is simply convenient. This
property is optional.

Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
Document preinitialization argument "initval" for descriptor based GPIO
handling.

Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
@jluebbe
Copy link
Copy Markdown
Member

jluebbe commented Apr 7, 2026

gpiod is not the correct approach for labgrid, see #1715 (comment) and #1818 (comment).

A PR implementing support for the new chip-based sysfs API would be more useful than another PR for gpiod support. :) It would need Resources, as the current SysfsGPIO only uses an index property (not chip and index).

@Rubusch
Copy link
Copy Markdown
Author

Rubusch commented Apr 8, 2026

Thank you for the feedback and clarification. For the SysfsGPIO(chip+offset) thing, are you aware of currently active development on this topic?

Another part of my commits was, for (my rather) simple and static case, I'd like to see something like a pre-initialization of GPIOs at acquire. Would such thing might have a chance to go upstream? Or, could you just briefly outline why definitely not?

@Rubusch Rubusch closed this Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants