GPIO Character Device Userspace API
This is latest version (v2) of the character device API, as defined in include/uapi/linux/gpio.h.
First added in 5.10.
Basic examples using the character device API can be found in tools/gpio/*.
The API is based around two major objects, the Chip and the Line Request.
Chip #
The Chip represents a single GPIO chip and is exposed to userspace using device files of the form /dev/gpiochipX.
Each chip supports a number of GPIO lines, chip.lines<gpiochip_info>. Lines on the chip are identified by an offset in the range from 0 to chip.lines - 1, i.e. [0,chip.lines).
Lines are requested from the chip using gpio-v2-get-line-ioctl.rst and the resulting line request is used to access the GPIO chip's lines or monitor the lines for edge events.
Within this documentation, the file descriptor returned by calling open() on the GPIO device file is referred to as chip_fd.
Operations #
The following operations may be performed on the chip:
- Get Line
- Get Chip Info
- Get Line Info
- Watch Line Info
- Unwatch Line Info
- Read Line Info Changed Events
Line Request #
Line requests are created by gpio-v2-get-line-ioctl.rst and provide access to a set of requested lines. The line request is exposed to userspace via the anonymous file descriptor returned in request.fd<gpio_v2_line_request> by gpio-v2-get-line-ioctl.rst.
Within this documentation, the line request file descriptor is referred to as req_fd.
Operations #
The following operations may be performed on the line request:
Types #
This section contains the structs and enums that are referenced by the API v2, as defined in include/uapi/linux/gpio.h.
struct gpiochip_info #
Information about a certain GPIO chip
Definition
;
Members
name: the Linux kernel name of this GPIO chiplabel: a functional name for this GPIO chip, such as a product number, may be empty (i.e. label[0] == '\0')lines: number of GPIO lines on this chip
enum gpio_v2_line_flag #
struct gpio_v2_line_attribute.flags values
Constants
GPIO_V2_LINE_FLAG_USED: line is not available for requestGPIO_V2_LINE_FLAG_ACTIVE_LOW: line active state is physical lowGPIO_V2_LINE_FLAG_INPUT: line is an inputGPIO_V2_LINE_FLAG_OUTPUT: line is an outputGPIO_V2_LINE_FLAG_EDGE_RISING: line detects rising (inactive to active) edgesGPIO_V2_LINE_FLAG_EDGE_FALLING: line detects falling (active to inactive) edgesGPIO_V2_LINE_FLAG_OPEN_DRAIN: line is an open drain outputGPIO_V2_LINE_FLAG_OPEN_SOURCE: line is an open source outputGPIO_V2_LINE_FLAG_BIAS_PULL_UP: line has pull-up bias enabledGPIO_V2_LINE_FLAG_BIAS_PULL_DOWN: line has pull-down bias enabledGPIO_V2_LINE_FLAG_BIAS_DISABLED: line has bias disabledGPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME: line events contain REALTIME timestampsGPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE: line events contain timestamps from the hardware timestamping engine (HTE) subsystem
struct gpio_v2_line_values #
Values of GPIO lines
Definition
;
Members
bits: a bitmap containing the value of the lines, set to 1 for active and 0 for inactivemask: a bitmap identifying the lines to get or set, with each bit number corresponding to the index intostruct gpio_v2_line_request.offsets
enum gpio_v2_line_attr_id #
struct gpio_v2_line_attribute.id values identifying which field of the attribute union is in use.
Constants
GPIO_V2_LINE_ATTR_ID_FLAGS: flags field is in useGPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES: values field is in useGPIO_V2_LINE_ATTR_ID_DEBOUNCE: debounce_period_us field is in use
struct gpio_v2_line_attribute #
a configurable attribute of a line
Definition
;
Members
id: attribute identifier with value fromenum gpio_v2_line_attr_idpadding: reserved for future use and must be zero filled{unnamed_union}: anonymousflags: if id isGPIO_V2_LINE_ATTR_ID_FLAGS, the flags for the GPIO line, with values fromenum gpio_v2_line_flag, such asGPIO_V2_LINE_FLAG_ACTIVE_LOW,GPIO_V2_LINE_FLAG_OUTPUTetc, added together. This overrides the default flags contained in thestruct gpio_v2_line_configfor the associated line.values: if id isGPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES, a bitmap containing the values to which the lines will be set, with each bit number corresponding to the index intostruct gpio_v2_line_request.offsetsdebounce_period_us: if id isGPIO_V2_LINE_ATTR_ID_DEBOUNCE, the desired debounce period, in microseconds
struct gpio_v2_line_config_attribute #
a configuration attribute associated with one or more of the requested lines.
Definition
;
Members
attr: the configurable attributemask: a bitmap identifying the lines to which the attribute applies, with each bit number corresponding to the index intostruct gpio_v2_line_request.offsets
struct gpio_v2_line_config #
Configuration for GPIO lines
Definition
;
Members
flags: flags for the GPIO lines, with values fromenum gpio_v2_line_flag, such asGPIO_V2_LINE_FLAG_ACTIVE_LOW,GPIO_V2_LINE_FLAG_OUTPUTetc, added together. This is the default for all requested lines but may be overridden for particular lines using attrs.num_attrs: the number of attributes in attrspadding: reserved for future use and must be zero filledattrs: the configuration attributes associated with the requested lines. Any attribute should only be associated with a particular line once. If an attribute is associated with a line multiple times then the first occurrence (i.e. lowest index) has precedence.
struct gpio_v2_line_request #
Information about a request for GPIO lines
Definition
;
Members
offsets: an array of desired lines, specified by offset index for the associated GPIO chipconsumer: a desired consumer label for the selected GPIO lines such as "my-bitbanged-relay"config: requested configuration for the linesnum_lines: number of lines requested in this request, i.e. the number of valid fields in theGPIO_V2_LINES_MAXsized arrays, set to 1 to request a single lineevent_buffer_size: a suggested minimum number of line events that the kernel should buffer. This is only relevant if edge detection is enabled in the configuration. Note that this is only a suggested value and the kernel may allocate a larger buffer or cap the size of the buffer. If this field is zero then the buffer size defaults to a minimum of num_lines * 16.padding: reserved for future use and must be zero filledfd: after a successfulGPIO_V2_GET_LINE_IOCTLoperation, contains a valid anonymous file descriptor representing the request
struct gpio_v2_line_info #
Information about a certain GPIO line
Definition
;
Members
name: the name of this GPIO line, such as the output pin of the line on the chip, a rail or a pin header name on a board, as specified by the GPIO chip, may be empty (i.e. name[0] == '\0')consumer: a functional name for the consumer of this GPIO line as set by whatever is using it, will be empty if there is no current user but may also be empty if the consumer doesn't set this upoffset: the local offset on this GPIO chip, fill this in when requesting the line information from the kernelnum_attrs: the number of attributes in attrsflags: flags for this GPIO line, with values fromenum gpio_v2_line_flag, such asGPIO_V2_LINE_FLAG_ACTIVE_LOW,GPIO_V2_LINE_FLAG_OUTPUTetc, added togetherattrs: the configuration attributes associated with the linepadding: reserved for future use
enum gpio_v2_line_changed_type #
struct gpio_v2_line_changed.event_type values
Constants
GPIO_V2_LINE_CHANGED_REQUESTED: line has been requestedGPIO_V2_LINE_CHANGED_RELEASED: line has been releasedGPIO_V2_LINE_CHANGED_CONFIG: line has been reconfigured
struct gpio_v2_line_info_changed #
Information about a change in status of a GPIO line
Definition
;
Members
info: updated line informationtimestamp_ns: estimate of time of status change occurrence, in nanosecondsevent_type: the type of change with a value fromenum gpio_v2_line_changed_typepadding: reserved for future use
enum gpio_v2_line_event_id #
struct gpio_v2_line_event.id values
Constants
GPIO_V2_LINE_EVENT_RISING_EDGE: event triggered by a rising edgeGPIO_V2_LINE_EVENT_FALLING_EDGE: event triggered by a falling edge
struct gpio_v2_line_event #
The actual event being pushed to userspace
Definition
;
Members
timestamp_ns: best estimate of time of event occurrence, in nanosecondsid: event identifier with value fromenum gpio_v2_line_event_idoffset: the offset of the line that triggered the eventseqno: the sequence number for this event in the sequence of events for all the lines in this line requestline_seqno: the sequence number for this event in the sequence of events on this particular linepadding: reserved for future use
Description
By default the timestamp_ns is read from CLOCK_MONOTONIC and is
intended to allow the accurate measurement of the time between events.
It does not provide the wall-clock time.
If the GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME flag is set then the
timestamp_ns is read from CLOCK_REALTIME.
If the GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE flag is set then the
timestamp_ns is provided by the hardware timestamping engine (HTE)
subsystem.