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,
70 static void print_attributes(struct gpio_v2_line_info *info)
73 const char *field_format = "%s";
75 for (i = 0; i < ARRAY_SIZE(flagnames); i++) {
76 if (info->flags & flagnames[i].mask) {
77 fprintf(stdout, field_format, flagnames[i].name);
78 field_format = ", %s";
82 if ((info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) &&
83 (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING))
84 fprintf(stdout, field_format, "both-edges");
85 else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING)
86 fprintf(stdout, field_format, "rising-edge");
87 else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
88 fprintf(stdout, field_format, "falling-edge");
90 for (i = 0; i < info->num_attrs; i++) {
91 if (info->attrs[i].id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE)
92 fprintf(stdout, ", debounce_period=%dusec",
93 info->attrs[i].debounce_period_us);
97 int list_device(const char *device_name)
99 struct gpiochip_info cinfo;
105 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
109 fd = open(chrdev_name, 0);
112 fprintf(stderr, "Failed to open %s\n", chrdev_name);
116 /* Inspect this GPIO chip */
117 ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &cinfo);
120 perror("Failed to issue CHIPINFO IOCTL\n");
121 goto exit_close_error;
123 fprintf(stdout, "GPIO chip: %s, \"%s\", %u GPIO lines\n",
124 cinfo.name, cinfo.label, cinfo.lines);
126 /* Loop over the lines and print info */
127 for (i = 0; i < cinfo.lines; i++) {
128 struct gpio_v2_line_info linfo;
130 memset(&linfo, 0, sizeof(linfo));
133 ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &linfo);
136 perror("Failed to issue LINEINFO IOCTL\n");
137 goto exit_close_error;
139 fprintf(stdout, "\tline %2d:", linfo.offset);
141 fprintf(stdout, " \"%s\"", linfo.name);
143 fprintf(stdout, " unnamed");
144 if (linfo.consumer[0])
145 fprintf(stdout, " \"%s\"", linfo.consumer);
147 fprintf(stdout, " unused");
149 fprintf(stdout, " [");
150 print_attributes(&linfo);
151 fprintf(stdout, "]");
153 fprintf(stdout, "\n");
159 perror("Failed to close GPIO character device file");
165 void print_usage(void)
167 fprintf(stderr, "Usage: lsgpio [options]...\n"
168 "List GPIO chips, lines and states\n"
169 " -n <name> List GPIOs on a named device\n"
170 " -? This helptext\n"
174 int main(int argc, char **argv)
176 const char *device_name = NULL;
180 while ((c = getopt(argc, argv, "n:")) != -1) {
183 device_name = optarg;
192 ret = list_device(device_name);
194 const struct dirent *ent;
197 /* List all GPIO devices one at a time */
198 dp = opendir("/dev");
205 while (ent = readdir(dp), ent) {
206 if (check_prefix(ent->d_name, "gpiochip")) {
207 ret = list_device(ent->d_name);
214 if (closedir(dp) == -1) {
215 perror("scanning devices: Failed to close directory");