libgpio: Add and document gpio interrupt utility functions

Add utility functions for configuring gpio interrupts and document file
operations.

Reviewed by:	vexeduxr, ziaee
Differential Revision:	https://reviews.freebsd.org/D52102
This commit is contained in:
Evgenii Ivanov 2025-09-03 15:13:59 +03:00 committed by Ahmad Khalifa
parent e323aaaf39
commit ccc6cf3b67
4 changed files with 102 additions and 3 deletions

View file

@ -27,6 +27,8 @@ MLINKS= gpio.3 gpio_open.3 \
gpio.3 gpio_pin_pulldown.3 \
gpio.3 gpio_pin_invin.3 \
gpio.3 gpio_pin_invout.3 \
gpio.3 gpio_pin_pulsate.3
gpio.3 gpio_pin_pulsate.3 \
gpio.3 gpio_configure_events.3 \
gpio.3 gpio_fileno.3
.include <bsd.lib.mk>

View file

@ -23,7 +23,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd July 1, 2015
.Dd September 3, 2025
.Dt GPIO 3
.Os
.Sh NAME
@ -79,6 +79,10 @@
.Fn gpio_pin_invout "gpio_handle_t handle" "gpio_pin_t pin"
.Ft int
.Fn gpio_pin_pulsate "gpio_handle_t handle" "gpio_pin_t pin"
.Ft int
.Fn gpio_configure_events "gpio_handle_t handle" "uint32_t report_type" "uint32_t fifo_size"
.Ft int
.Fn gpio_fileno "gpio_handle_t handle"
.Sh DESCRIPTION
The
.Nm libgpio
@ -125,7 +129,7 @@ The pin number should also be passed in through the
variable.
All other structure members will be ignored by this function.
The list of flags can be found in
.Pa /usr/include/sys/gpio.h .
.In sys/gpio.h .
.Pp
The get or set the state of a GPIO pin, the functions
.Fn gpio_pin_get
@ -156,6 +160,66 @@ and
.Fn gpio_pin_pulsate
are wrappers around
.Fn gpio_pin_set_flags .
.Pp
The function
.Fn gpio_configure_events
configures the interrupt report type and FIFO size for buffered
gpio interrupts.
The report type is specified by one of the following values:
.Bl -tag -width indent
.It Dv GPIO_EVENT_REPORT_DETAIL
Events are reported using
.Ft struct gpio_event_detail .
.It Dv GPIO_EVENT_REPORT_SUMMARY
Events are reported using
.Ft struct gpio_event_summary .
.El
.Pp
By default, the report type is
.Dv GPIO_EVENT_REPORT_DETAIL ,
with a default FIFO size of 2 * number of pins belonging to the
.Ft gpio_device_t
instance.
The FIFO argument is only meaningful when
.Fa report_type
is
.Dv GPIO_EVENT_REPORT_DETAIL .
The structures associated with each report type are defined in
.In sys/gpio.h .
This setting is tracked on a per device instance basis.
The FIFO size cannot be reduced below the default value,
nor can it be decreased after it has been increased.
If any pin on the device has already been configured for interrupts,
.Fn gpio_configure_events
fails and returns -1.
On success 0 is returned.
.Pp
The function
.Fn gpio_fileno
returns the file descriptor associated with the
.Ft gpio_handle_t
instance.
.Pp
File operations have the following semantics:
.Bl -tag -width "read (2)"
.It Xr read 2
Read one or more gpio interrupts that have occured
since the last successful
.Xr read 2 .
The results are placed into the output buffer
of the type previously established via
.Fn gpio_configure_events .
If there are no pending interrupts,
.Xr read 2
blocks until an interrupt occurs, unless
.Dv O_NONBLOCK
is set.
.It Xr poll 2
When receiving notification via
.Xr poll 2
or similar interfaces, the file descriptor becomes readable when
one or more gpio interrupts are pending.
.El
.Sh EXAMPLES
The following example shows how to configure pin 16 as output and then
drive it high:

View file

@ -274,3 +274,23 @@ gpio_pin_pulsate(gpio_handle_t handle, gpio_pin_t pin)
{
return (gpio_pin_set_flag(handle, pin, GPIO_PIN_PULSATE));
}
int
gpio_configure_events(gpio_handle_t handle, uint32_t report_type,
uint32_t fifo_size)
{
struct gpio_event_config gpevent_config;
gpevent_config.gp_report_type = report_type;
gpevent_config.gp_fifo_size = fifo_size;
if (ioctl(handle, GPIOCONFIGEVENTS, &gpevent_config) < 0)
return (-1);
return (0);
}
int
gpio_fileno(gpio_handle_t handle)
{
return (handle);
}

View file

@ -102,6 +102,19 @@ int gpio_pin_pulldown(gpio_handle_t, gpio_pin_t);
int gpio_pin_invin(gpio_handle_t, gpio_pin_t);
int gpio_pin_invout(gpio_handle_t, gpio_pin_t);
int gpio_pin_pulsate(gpio_handle_t, gpio_pin_t);
/*
* GPIO event reporting configuration
*
* Set the event reporting type, the default being GPIO_EVENT_REPORT_DETAIL,
* and fifo size of 2 * number of pins belonging to the gpioc device instance.
* FIFO size can only be changed when report_type is GPIO_EVENT_REPORT_DETAIL.
*/
int gpio_configure_events(gpio_handle_t, uint32_t, uint32_t);
/*
* Retrieve the file descriptor associated with gpio_handle_t, which can
* be used for gpio interrupts.
*/
int gpio_fileno(gpio_handle_t);
__END_DECLS