1 // SPDX-License-Identifier: GPL-2.0-only
3 * lsgpio - example on how to list the GPIO lines on a system
5 * Copyright (C) 2015 Linus Walleij
8 * lsgpio <-n device-name>
21 #include <sys/ioctl.h>
22 #include <linux/gpio.h>
24 #include "gpio-utils.h"
28 unsigned long long mask;
31 struct gpio_flag flagnames[] = {
34 .mask = GPIO_V2_LINE_FLAG_USED,
38 .mask = GPIO_V2_LINE_FLAG_INPUT,
42 .mask = GPIO_V2_LINE_FLAG_OUTPUT,
46 .mask = GPIO_V2_LINE_FLAG_ACTIVE_LOW,
50 .mask = GPIO_V2_LINE_FLAG_OPEN_DRAIN,
53 .name = "open-source",
54 .mask = GPIO_V2_LINE_FLAG_OPEN_SOURCE,
58 .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_UP,
62 .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN,
65 .name = "bias-disabled",
66 .mask = GPIO_V2_LINE_FLAG_BIAS_DISABLED,
69 .name = "clock-realtime",
70 .mask = GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME,
74 static void print_attributes(struct gpio_v2_line_info *info)
77 const char *field_format = "%s";
79 for (i = 0; i < ARRAY_SIZE(flagnames); i++) {
80 if (info->flags & flagnames[i].mask) {
81 fprintf(stdout, field_format, flagnames[i].name);
82 field_format = ", %s";
86 if ((info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) &&
87 (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING))
88 fprintf(stdout, field_format, "both-edges");
89 else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING)
90 fprintf(stdout, field_format, "rising-edge");
91 else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
92 fprintf(stdout, field_format, "falling-edge");
94 for (i = 0; i < info->num_attrs; i++) {
95 if (info->attrs[i].id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE)
96 fprintf(stdout, ", debounce_period=%dusec",
97 info->attrs[i].debounce_period_us);
101 int list_device(const char *device_name)
103 struct gpiochip_info cinfo;
109 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
113 fd = open(chrdev_name, 0);
116 fprintf(stderr, "Failed to open %s\n", chrdev_name);
120 /* Inspect this GPIO chip */
121 ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &cinfo);
124 perror("Failed to issue CHIPINFO IOCTL\n");
125 goto exit_close_error;
127 fprintf(stdout, "GPIO chip: %s, \"%s\", %u GPIO lines\n",
128 cinfo.name, cinfo.label, cinfo.lines);
130 /* Loop over the lines and print info */
131 for (i = 0; i < cinfo.lines; i++) {
132 struct gpio_v2_line_info linfo;
134 memset(&linfo, 0, sizeof(linfo));
137 ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &linfo);
140 perror("Failed to issue LINEINFO IOCTL\n");
141 goto exit_close_error;
143 fprintf(stdout, "\tline %2d:", linfo.offset);
145 fprintf(stdout, " \"%s\"", linfo.name);
147 fprintf(stdout, " unnamed");
148 if (linfo.consumer[0])
149 fprintf(stdout, " \"%s\"", linfo.consumer);
151 fprintf(stdout, " unused");
153 fprintf(stdout, " [");
154 print_attributes(&linfo);
155 fprintf(stdout, "]");
157 fprintf(stdout, "\n");
163 perror("Failed to close GPIO character device file");
169 void print_usage(void)
171 fprintf(stderr, "Usage: lsgpio [options]...\n"
172 "List GPIO chips, lines and states\n"
173 " -n <name> List GPIOs on a named device\n"
174 " -? This helptext\n"
178 int main(int argc, char **argv)
180 const char *device_name = NULL;
184 while ((c = getopt(argc, argv, "n:")) != -1) {
187 device_name = optarg;
196 ret = list_device(device_name);
198 const struct dirent *ent;
201 /* List all GPIO devices one at a time */
202 dp = opendir("/dev");
209 while (ent = readdir(dp), ent) {
210 if (check_prefix(ent->d_name, "gpiochip")) {
211 ret = list_device(ent->d_name);
218 if (closedir(dp) == -1) {
219 perror("scanning devices: Failed to close directory");