1 // SPDX-License-Identifier: GPL-2.0-only
3 * gpio-event-mon - monitor GPIO line events from userspace
5 * Copyright (C) 2016 Linus Walleij
8 * gpio-event-mon -n <device-name> -o <offset>
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #include <linux/gpio.h>
26 #include "gpio-utils.h"
28 int monitor_device(const char *device_name
,
30 unsigned int num_lines
,
31 struct gpio_v2_line_config
*config
,
34 struct gpio_v2_line_values values
;
40 ret
= asprintf(&chrdev_name
, "/dev/%s", device_name
);
44 cfd
= open(chrdev_name
, 0);
47 fprintf(stderr
, "Failed to open %s\n", chrdev_name
);
51 ret
= gpiotools_request_line(device_name
, lines
, num_lines
, config
,
54 goto exit_device_close
;
58 /* Read initial states */
61 for (i
= 0; i
< num_lines
; i
++)
62 gpiotools_set_bit(&values
.mask
, i
);
63 ret
= gpiotools_get_values(lfd
, &values
);
66 "Failed to issue GPIO LINE GET VALUES IOCTL (%d)\n",
72 fprintf(stdout
, "Monitoring line %u on %s\n", lines
[0], device_name
);
73 fprintf(stdout
, "Initial line value: %d\n",
74 gpiotools_test_bit(values
.bits
, 0));
76 fprintf(stdout
, "Monitoring lines %u", lines
[0]);
77 for (i
= 1; i
< num_lines
- 1; i
++)
78 fprintf(stdout
, ", %u", lines
[i
]);
79 fprintf(stdout
, " and %u on %s\n", lines
[i
], device_name
);
80 fprintf(stdout
, "Initial line values: %d",
81 gpiotools_test_bit(values
.bits
, 0));
82 for (i
= 1; i
< num_lines
- 1; i
++)
83 fprintf(stdout
, ", %d",
84 gpiotools_test_bit(values
.bits
, i
));
85 fprintf(stdout
, " and %d\n",
86 gpiotools_test_bit(values
.bits
, i
));
91 struct gpio_v2_line_event event
;
93 ret
= read(lfd
, &event
, sizeof(event
));
95 if (errno
== -EAGAIN
) {
96 fprintf(stderr
, "nothing available\n");
100 fprintf(stderr
, "Failed to read event (%d)\n",
106 if (ret
!= sizeof(event
)) {
107 fprintf(stderr
, "Reading event failed\n");
111 fprintf(stdout
, "GPIO EVENT at %" PRIu64
" on line %d (%d|%d) ",
112 (uint64_t)event
.timestamp_ns
, event
.offset
, event
.line_seqno
,
115 case GPIO_V2_LINE_EVENT_RISING_EDGE
:
116 fprintf(stdout
, "rising edge");
118 case GPIO_V2_LINE_EVENT_FALLING_EDGE
:
119 fprintf(stdout
, "falling edge");
122 fprintf(stdout
, "unknown event");
124 fprintf(stdout
, "\n");
132 if (close(lfd
) == -1)
133 perror("Failed to close line file");
135 if (close(cfd
) == -1)
136 perror("Failed to close GPIO character device file");
142 void print_usage(void)
144 fprintf(stderr
, "Usage: gpio-event-mon [options]...\n"
145 "Listen to events on GPIO lines, 0->1 1->0\n"
146 " -n <name> Listen on GPIOs on a named device (must be stated)\n"
147 " -o <n> Offset of line to monitor (may be repeated)\n"
148 " -d Set line as open drain\n"
149 " -s Set line as open source\n"
150 " -r Listen for rising edges\n"
151 " -f Listen for falling edges\n"
152 " -w Report the wall-clock time for events\n"
153 " -t Report the hardware timestamp for events\n"
154 " -b <n> Debounce the line with period n microseconds\n"
155 " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
156 " -? This helptext\n"
159 "gpio-event-mon -n gpiochip0 -o 4 -r -f -b 10000\n"
164 (GPIO_V2_LINE_FLAG_EDGE_RISING | \
165 GPIO_V2_LINE_FLAG_EDGE_FALLING)
167 int main(int argc
, char **argv
)
169 const char *device_name
= NULL
;
170 unsigned int lines
[GPIO_V2_LINES_MAX
];
171 unsigned int num_lines
= 0;
172 unsigned int loops
= 0;
173 struct gpio_v2_line_config config
;
175 unsigned long debounce_period_us
= 0;
177 memset(&config
, 0, sizeof(config
));
178 config
.flags
= GPIO_V2_LINE_FLAG_INPUT
;
179 while ((c
= getopt(argc
, argv
, "c:n:o:b:dsrfwt?")) != -1) {
182 loops
= strtoul(optarg
, NULL
, 10);
185 device_name
= optarg
;
188 if (num_lines
>= GPIO_V2_LINES_MAX
) {
192 lines
[num_lines
] = strtoul(optarg
, NULL
, 10);
196 debounce_period_us
= strtoul(optarg
, NULL
, 10);
199 config
.flags
|= GPIO_V2_LINE_FLAG_OPEN_DRAIN
;
202 config
.flags
|= GPIO_V2_LINE_FLAG_OPEN_SOURCE
;
205 config
.flags
|= GPIO_V2_LINE_FLAG_EDGE_RISING
;
208 config
.flags
|= GPIO_V2_LINE_FLAG_EDGE_FALLING
;
211 config
.flags
|= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME
;
214 config
.flags
|= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE
;
222 if (debounce_period_us
) {
223 attr
= config
.num_attrs
;
225 for (i
= 0; i
< num_lines
; i
++)
226 gpiotools_set_bit(&config
.attrs
[attr
].mask
, i
);
227 config
.attrs
[attr
].attr
.id
= GPIO_V2_LINE_ATTR_ID_DEBOUNCE
;
228 config
.attrs
[attr
].attr
.debounce_period_us
= debounce_period_us
;
231 if (!device_name
|| num_lines
== 0) {
235 if (!(config
.flags
& EDGE_FLAGS
)) {
236 printf("No flags specified, listening on both rising and "
238 config
.flags
|= EDGE_FLAGS
;
240 return monitor_device(device_name
, lines
, num_lines
, &config
, loops
);