1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
3 * <linux/gpio.h> - userspace ABI for the GPIO character devices
5 * Copyright (C) 2016 Linus Walleij
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
14 #include <linux/const.h>
15 #include <linux/ioctl.h>
16 #include <linux/types.h>
19 * The maximum size of name and label arrays.
21 * Must be a multiple of 8 to ensure 32/64-bit alignment of structs.
23 #define GPIO_MAX_NAME_SIZE 32
26 * struct gpiochip_info - Information about a certain GPIO chip
27 * @name: the Linux kernel name of this GPIO chip
28 * @label: a functional name for this GPIO chip, such as a product
29 * number, may be empty
30 * @lines: number of GPIO lines on this chip
32 struct gpiochip_info {
33 char name[GPIO_MAX_NAME_SIZE];
34 char label[GPIO_MAX_NAME_SIZE];
39 * Maximum number of requested lines.
41 * Must be no greater than 64, as bitmaps are restricted here to 64-bits
42 * for simplicity, and a multiple of 2 to ensure 32/64-bit alignment of
45 #define GPIO_V2_LINES_MAX 64
48 * The maximum number of configuration attributes associated with a line
51 #define GPIO_V2_LINE_NUM_ATTRS_MAX 10
54 * enum gpio_v2_line_flag - &struct gpio_v2_line_attribute.flags values
55 * @GPIO_V2_LINE_FLAG_USED: line is not available for request
56 * @GPIO_V2_LINE_FLAG_ACTIVE_LOW: line active state is physical low
57 * @GPIO_V2_LINE_FLAG_INPUT: line is an input
58 * @GPIO_V2_LINE_FLAG_OUTPUT: line is an output
59 * @GPIO_V2_LINE_FLAG_EDGE_RISING: line detects rising (inactive to active)
61 * @GPIO_V2_LINE_FLAG_EDGE_FALLING: line detects falling (active to
63 * @GPIO_V2_LINE_FLAG_OPEN_DRAIN: line is an open drain output
64 * @GPIO_V2_LINE_FLAG_OPEN_SOURCE: line is an open source output
65 * @GPIO_V2_LINE_FLAG_BIAS_PULL_UP: line has pull-up bias enabled
66 * @GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN: line has pull-down bias enabled
67 * @GPIO_V2_LINE_FLAG_BIAS_DISABLED: line has bias disabled
69 enum gpio_v2_line_flag {
70 GPIO_V2_LINE_FLAG_USED = _BITULL(0),
71 GPIO_V2_LINE_FLAG_ACTIVE_LOW = _BITULL(1),
72 GPIO_V2_LINE_FLAG_INPUT = _BITULL(2),
73 GPIO_V2_LINE_FLAG_OUTPUT = _BITULL(3),
74 GPIO_V2_LINE_FLAG_EDGE_RISING = _BITULL(4),
75 GPIO_V2_LINE_FLAG_EDGE_FALLING = _BITULL(5),
76 GPIO_V2_LINE_FLAG_OPEN_DRAIN = _BITULL(6),
77 GPIO_V2_LINE_FLAG_OPEN_SOURCE = _BITULL(7),
78 GPIO_V2_LINE_FLAG_BIAS_PULL_UP = _BITULL(8),
79 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN = _BITULL(9),
80 GPIO_V2_LINE_FLAG_BIAS_DISABLED = _BITULL(10),
84 * struct gpio_v2_line_values - Values of GPIO lines
85 * @bits: a bitmap containing the value of the lines, set to 1 for active
87 * @mask: a bitmap identifying the lines to get or set, with each bit
88 * number corresponding to the index into &struct
89 * gpio_v2_line_request.offsets.
91 struct gpio_v2_line_values {
97 * enum gpio_v2_line_attr_id - &struct gpio_v2_line_attribute.id values
98 * identifying which field of the attribute union is in use.
99 * @GPIO_V2_LINE_ATTR_ID_FLAGS: flags field is in use
100 * @GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES: values field is in use
101 * @GPIO_V2_LINE_ATTR_ID_DEBOUNCE: debounce_period_us is in use
103 enum gpio_v2_line_attr_id {
104 GPIO_V2_LINE_ATTR_ID_FLAGS = 1,
105 GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES = 2,
106 GPIO_V2_LINE_ATTR_ID_DEBOUNCE = 3,
110 * struct gpio_v2_line_attribute - a configurable attribute of a line
111 * @id: attribute identifier with value from &enum gpio_v2_line_attr_id
112 * @padding: reserved for future use and must be zero filled
113 * @flags: if id is GPIO_V2_LINE_ATTR_ID_FLAGS, the flags for the GPIO
114 * line, with values from enum gpio_v2_line_flag, such as
115 * GPIO_V2_LINE_FLAG_ACTIVE_LOW, GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed
116 * together. This overrides the default flags contained in the &struct
117 * gpio_v2_line_config for the associated line.
118 * @values: if id is GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES, a bitmap
119 * containing the values to which the lines will be set, with each bit
120 * number corresponding to the index into &struct
121 * gpio_v2_line_request.offsets.
122 * @debounce_period_us: if id is GPIO_V2_LINE_ATTR_ID_DEBOUNCE, the desired
123 * debounce period, in microseconds
125 struct gpio_v2_line_attribute {
130 __aligned_u64 values;
131 __u32 debounce_period_us;
136 * struct gpio_v2_line_config_attribute - a configuration attribute
137 * associated with one or more of the requested lines.
138 * @attr: the configurable attribute
139 * @mask: a bitmap identifying the lines to which the attribute applies,
140 * with each bit number corresponding to the index into &struct
141 * gpio_v2_line_request.offsets.
143 struct gpio_v2_line_config_attribute {
144 struct gpio_v2_line_attribute attr;
149 * struct gpio_v2_line_config - Configuration for GPIO lines
150 * @flags: flags for the GPIO lines, with values from enum
151 * gpio_v2_line_flag, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW,
152 * GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed together. This is the default for
153 * all requested lines but may be overridden for particular lines using
155 * @num_attrs: the number of attributes in attrs
156 * @padding: reserved for future use and must be zero filled
157 * @attrs: the configuration attributes associated with the requested
158 * lines. Any attribute should only be associated with a particular line
159 * once. If an attribute is associated with a line multiple times then the
160 * first occurrence (i.e. lowest index) has precedence.
162 struct gpio_v2_line_config {
165 /* Pad to fill implicit padding and reserve space for future use. */
167 struct gpio_v2_line_config_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX];
171 * struct gpio_v2_line_request - Information about a request for GPIO lines
172 * @offsets: an array of desired lines, specified by offset index for the
173 * associated GPIO chip
174 * @consumer: a desired consumer label for the selected GPIO lines such as
175 * "my-bitbanged-relay"
176 * @config: requested configuration for the lines.
177 * @num_lines: number of lines requested in this request, i.e. the number
178 * of valid fields in the GPIO_V2_LINES_MAX sized arrays, set to 1 to
179 * request a single line
180 * @event_buffer_size: a suggested minimum number of line events that the
181 * kernel should buffer. This is only relevant if edge detection is
182 * enabled in the configuration. Note that this is only a suggested value
183 * and the kernel may allocate a larger buffer or cap the size of the
184 * buffer. If this field is zero then the buffer size defaults to a minimum
186 * @padding: reserved for future use and must be zero filled
187 * @fd: if successful this field will contain a valid anonymous file handle
188 * after a GPIO_GET_LINE_IOCTL operation, zero or negative value means
191 struct gpio_v2_line_request {
192 __u32 offsets[GPIO_V2_LINES_MAX];
193 char consumer[GPIO_MAX_NAME_SIZE];
194 struct gpio_v2_line_config config;
196 __u32 event_buffer_size;
197 /* Pad to fill implicit padding and reserve space for future use. */
203 * struct gpio_v2_line_info - Information about a certain GPIO line
204 * @name: the name of this GPIO line, such as the output pin of the line on
205 * the chip, a rail or a pin header name on a board, as specified by the
206 * GPIO chip, may be empty
207 * @consumer: a functional name for the consumer of this GPIO line as set
208 * by whatever is using it, will be empty if there is no current user but
209 * may also be empty if the consumer doesn't set this up
210 * @flags: flags for the GPIO line, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW,
211 * GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed together
212 * @offset: the local offset on this GPIO chip, fill this in when
213 * requesting the line information from the kernel
214 * @num_attrs: the number of attributes in attrs
215 * @attrs: the configuration attributes associated with the line
216 * @padding: reserved for future use
218 struct gpio_v2_line_info {
219 char name[GPIO_MAX_NAME_SIZE];
220 char consumer[GPIO_MAX_NAME_SIZE];
224 struct gpio_v2_line_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX];
225 /* Space reserved for future use. */
230 * enum gpio_v2_line_changed_type - &struct gpio_v2_line_changed.event_type
232 * @GPIO_V2_LINE_CHANGED_REQUESTED: line has been requested
233 * @GPIO_V2_LINE_CHANGED_RELEASED: line has been released
234 * @GPIO_V2_LINE_CHANGED_CONFIG: line has been reconfigured
236 enum gpio_v2_line_changed_type {
237 GPIO_V2_LINE_CHANGED_REQUESTED = 1,
238 GPIO_V2_LINE_CHANGED_RELEASED = 2,
239 GPIO_V2_LINE_CHANGED_CONFIG = 3,
243 * struct gpio_v2_line_info_changed - Information about a change in status
245 * @info: updated line information
246 * @timestamp_ns: estimate of time of status change occurrence, in nanoseconds
247 * @event_type: the type of change with a value from enum
248 * gpio_v2_line_changed_type
249 * @padding: reserved for future use
251 struct gpio_v2_line_info_changed {
252 struct gpio_v2_line_info info;
253 __aligned_u64 timestamp_ns;
255 /* Pad struct to 64-bit boundary and reserve space for future use. */
260 * enum gpio_v2_line_event_id - &struct gpio_v2_line_event.id values
261 * @GPIO_V2_LINE_EVENT_RISING_EDGE: event triggered by a rising edge
262 * @GPIO_V2_LINE_EVENT_FALLING_EDGE: event triggered by a falling edge
264 enum gpio_v2_line_event_id {
265 GPIO_V2_LINE_EVENT_RISING_EDGE = 1,
266 GPIO_V2_LINE_EVENT_FALLING_EDGE = 2,
270 * struct gpio_v2_line_event - The actual event being pushed to userspace
271 * @timestamp_ns: best estimate of time of event occurrence, in nanoseconds.
272 * The timestamp_ns is read from CLOCK_MONOTONIC and is intended to allow the
273 * accurate measurement of the time between events. It does not provide
274 * the wall-clock time.
275 * @id: event identifier with value from enum gpio_v2_line_event_id
276 * @offset: the offset of the line that triggered the event
277 * @seqno: the sequence number for this event in the sequence of events for
278 * all the lines in this line request
279 * @line_seqno: the sequence number for this event in the sequence of
280 * events on this particular line
281 * @padding: reserved for future use
283 struct gpio_v2_line_event {
284 __aligned_u64 timestamp_ns;
289 /* Space reserved for future use. */
296 * This version of the ABI is deprecated.
297 * Use the latest version of the ABI, defined above, instead.
300 /* Informational flags */
301 #define GPIOLINE_FLAG_KERNEL (1UL << 0) /* Line used by the kernel */
302 #define GPIOLINE_FLAG_IS_OUT (1UL << 1)
303 #define GPIOLINE_FLAG_ACTIVE_LOW (1UL << 2)
304 #define GPIOLINE_FLAG_OPEN_DRAIN (1UL << 3)
305 #define GPIOLINE_FLAG_OPEN_SOURCE (1UL << 4)
306 #define GPIOLINE_FLAG_BIAS_PULL_UP (1UL << 5)
307 #define GPIOLINE_FLAG_BIAS_PULL_DOWN (1UL << 6)
308 #define GPIOLINE_FLAG_BIAS_DISABLE (1UL << 7)
311 * struct gpioline_info - Information about a certain GPIO line
312 * @line_offset: the local offset on this GPIO device, fill this in when
313 * requesting the line information from the kernel
314 * @flags: various flags for this line
315 * @name: the name of this GPIO line, such as the output pin of the line on the
316 * chip, a rail or a pin header name on a board, as specified by the gpio
318 * @consumer: a functional name for the consumer of this GPIO line as set by
319 * whatever is using it, will be empty if there is no current user but may
320 * also be empty if the consumer doesn't set this up
322 * This struct is part of ABI v1 and is deprecated.
323 * Use struct gpio_v2_line_info instead.
325 struct gpioline_info {
328 char name[GPIO_MAX_NAME_SIZE];
329 char consumer[GPIO_MAX_NAME_SIZE];
332 /* Maximum number of requested handles */
333 #define GPIOHANDLES_MAX 64
335 /* Possible line status change events */
337 GPIOLINE_CHANGED_REQUESTED = 1,
338 GPIOLINE_CHANGED_RELEASED,
339 GPIOLINE_CHANGED_CONFIG,
343 * struct gpioline_info_changed - Information about a change in status
345 * @info: updated line information
346 * @timestamp: estimate of time of status change occurrence, in nanoseconds
347 * @event_type: one of GPIOLINE_CHANGED_REQUESTED, GPIOLINE_CHANGED_RELEASED
348 * and GPIOLINE_CHANGED_CONFIG
350 * Note: struct gpioline_info embedded here has 32-bit alignment on its own,
351 * but it works fine with 64-bit alignment too. With its 72 byte size, we can
352 * guarantee there are no implicit holes between it and subsequent members.
353 * The 20-byte padding at the end makes sure we don't add any implicit padding
354 * at the end of the structure on 64-bit architectures.
356 * This struct is part of ABI v1 and is deprecated.
357 * Use struct gpio_v2_line_info_changed instead.
359 struct gpioline_info_changed {
360 struct gpioline_info info;
363 __u32 padding[5]; /* for future use */
366 /* Linerequest flags */
367 #define GPIOHANDLE_REQUEST_INPUT (1UL << 0)
368 #define GPIOHANDLE_REQUEST_OUTPUT (1UL << 1)
369 #define GPIOHANDLE_REQUEST_ACTIVE_LOW (1UL << 2)
370 #define GPIOHANDLE_REQUEST_OPEN_DRAIN (1UL << 3)
371 #define GPIOHANDLE_REQUEST_OPEN_SOURCE (1UL << 4)
372 #define GPIOHANDLE_REQUEST_BIAS_PULL_UP (1UL << 5)
373 #define GPIOHANDLE_REQUEST_BIAS_PULL_DOWN (1UL << 6)
374 #define GPIOHANDLE_REQUEST_BIAS_DISABLE (1UL << 7)
377 * struct gpiohandle_request - Information about a GPIO handle request
378 * @lineoffsets: an array of desired lines, specified by offset index for the
379 * associated GPIO device
380 * @flags: desired flags for the desired GPIO lines, such as
381 * GPIOHANDLE_REQUEST_OUTPUT, GPIOHANDLE_REQUEST_ACTIVE_LOW etc, OR:ed
382 * together. Note that even if multiple lines are requested, the same flags
383 * must be applicable to all of them, if you want lines with individual
384 * flags set, request them one by one. It is possible to select
385 * a batch of input or output lines, but they must all have the same
386 * characteristics, i.e. all inputs or all outputs, all active low etc
387 * @default_values: if the GPIOHANDLE_REQUEST_OUTPUT is set for a requested
388 * line, this specifies the default output value, should be 0 (low) or
389 * 1 (high), anything else than 0 or 1 will be interpreted as 1 (high)
390 * @consumer_label: a desired consumer label for the selected GPIO line(s)
391 * such as "my-bitbanged-relay"
392 * @lines: number of lines requested in this request, i.e. the number of
393 * valid fields in the above arrays, set to 1 to request a single line
394 * @fd: if successful this field will contain a valid anonymous file handle
395 * after a GPIO_GET_LINEHANDLE_IOCTL operation, zero or negative value
398 * This struct is part of ABI v1 and is deprecated.
399 * Use struct gpio_v2_line_request instead.
401 struct gpiohandle_request {
402 __u32 lineoffsets[GPIOHANDLES_MAX];
404 __u8 default_values[GPIOHANDLES_MAX];
405 char consumer_label[GPIO_MAX_NAME_SIZE];
411 * struct gpiohandle_config - Configuration for a GPIO handle request
412 * @flags: updated flags for the requested GPIO lines, such as
413 * GPIOHANDLE_REQUEST_OUTPUT, GPIOHANDLE_REQUEST_ACTIVE_LOW etc, OR:ed
415 * @default_values: if the GPIOHANDLE_REQUEST_OUTPUT is set in flags,
416 * this specifies the default output value, should be 0 (low) or
417 * 1 (high), anything else than 0 or 1 will be interpreted as 1 (high)
418 * @padding: reserved for future use and should be zero filled
420 * This struct is part of ABI v1 and is deprecated.
421 * Use struct gpio_v2_line_config instead.
423 struct gpiohandle_config {
425 __u8 default_values[GPIOHANDLES_MAX];
426 __u32 padding[4]; /* padding for future use */
430 * struct gpiohandle_data - Information of values on a GPIO handle
431 * @values: when getting the state of lines this contains the current
432 * state of a line, when setting the state of lines these should contain
433 * the desired target state
435 * This struct is part of ABI v1 and is deprecated.
436 * Use struct gpio_v2_line_values instead.
438 struct gpiohandle_data {
439 __u8 values[GPIOHANDLES_MAX];
442 /* Eventrequest flags */
443 #define GPIOEVENT_REQUEST_RISING_EDGE (1UL << 0)
444 #define GPIOEVENT_REQUEST_FALLING_EDGE (1UL << 1)
445 #define GPIOEVENT_REQUEST_BOTH_EDGES ((1UL << 0) | (1UL << 1))
448 * struct gpioevent_request - Information about a GPIO event request
449 * @lineoffset: the desired line to subscribe to events from, specified by
450 * offset index for the associated GPIO device
451 * @handleflags: desired handle flags for the desired GPIO line, such as
452 * GPIOHANDLE_REQUEST_ACTIVE_LOW or GPIOHANDLE_REQUEST_OPEN_DRAIN
453 * @eventflags: desired flags for the desired GPIO event line, such as
454 * GPIOEVENT_REQUEST_RISING_EDGE or GPIOEVENT_REQUEST_FALLING_EDGE
455 * @consumer_label: a desired consumer label for the selected GPIO line(s)
456 * such as "my-listener"
457 * @fd: if successful this field will contain a valid anonymous file handle
458 * after a GPIO_GET_LINEEVENT_IOCTL operation, zero or negative value
461 * This struct is part of ABI v1 and is deprecated.
462 * Use struct gpio_v2_line_request instead.
464 struct gpioevent_request {
468 char consumer_label[GPIO_MAX_NAME_SIZE];
475 #define GPIOEVENT_EVENT_RISING_EDGE 0x01
476 #define GPIOEVENT_EVENT_FALLING_EDGE 0x02
479 * struct gpioevent_data - The actual event being pushed to userspace
480 * @timestamp: best estimate of time of event occurrence, in nanoseconds
481 * @id: event identifier
483 * This struct is part of ABI v1 and is deprecated.
484 * Use struct gpio_v2_line_event instead.
486 struct gpioevent_data {
494 #define GPIO_GET_CHIPINFO_IOCTL _IOR(0xB4, 0x01, struct gpiochip_info)
495 #define GPIO_GET_LINEINFO_UNWATCH_IOCTL _IOWR(0xB4, 0x0C, __u32)
500 #define GPIO_V2_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x05, struct gpio_v2_line_info)
501 #define GPIO_V2_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x06, struct gpio_v2_line_info)
502 #define GPIO_V2_GET_LINE_IOCTL _IOWR(0xB4, 0x07, struct gpio_v2_line_request)
503 #define GPIO_V2_LINE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0D, struct gpio_v2_line_config)
504 #define GPIO_V2_LINE_GET_VALUES_IOCTL _IOWR(0xB4, 0x0E, struct gpio_v2_line_values)
505 #define GPIO_V2_LINE_SET_VALUES_IOCTL _IOWR(0xB4, 0x0F, struct gpio_v2_line_values)
510 * These ioctl()s are deprecated. Use the v2 equivalent instead.
512 #define GPIO_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x02, struct gpioline_info)
513 #define GPIO_GET_LINEHANDLE_IOCTL _IOWR(0xB4, 0x03, struct gpiohandle_request)
514 #define GPIO_GET_LINEEVENT_IOCTL _IOWR(0xB4, 0x04, struct gpioevent_request)
515 #define GPIOHANDLE_GET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x08, struct gpiohandle_data)
516 #define GPIOHANDLE_SET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x09, struct gpiohandle_data)
517 #define GPIOHANDLE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0A, struct gpiohandle_config)
518 #define GPIO_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x0B, struct gpioline_info)
520 #endif /* _UAPI_GPIO_H_ */