hc
2024-05-10 cde9070d9970eef1f7ec2360586c802a16230ad8
kernel/tools/gpio/lsgpio.c
....@@ -1,11 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * lsgpio - example on how to list the GPIO lines on a system
34 *
45 * Copyright (C) 2015 Linus Walleij
5
- *
6
- * This program is free software; you can redistribute it and/or modify it
7
- * under the terms of the GNU General Public License version 2 as published by
8
- * the Free Software Foundation.
96 *
107 * Usage:
118 * lsgpio <-n device-name>
....@@ -28,44 +25,72 @@
2825
2926 struct gpio_flag {
3027 char *name;
31
- unsigned long mask;
28
+ unsigned long long mask;
3229 };
3330
3431 struct gpio_flag flagnames[] = {
3532 {
36
- .name = "kernel",
37
- .mask = GPIOLINE_FLAG_KERNEL,
33
+ .name = "used",
34
+ .mask = GPIO_V2_LINE_FLAG_USED,
35
+ },
36
+ {
37
+ .name = "input",
38
+ .mask = GPIO_V2_LINE_FLAG_INPUT,
3839 },
3940 {
4041 .name = "output",
41
- .mask = GPIOLINE_FLAG_IS_OUT,
42
+ .mask = GPIO_V2_LINE_FLAG_OUTPUT,
4243 },
4344 {
4445 .name = "active-low",
45
- .mask = GPIOLINE_FLAG_ACTIVE_LOW,
46
+ .mask = GPIO_V2_LINE_FLAG_ACTIVE_LOW,
4647 },
4748 {
4849 .name = "open-drain",
49
- .mask = GPIOLINE_FLAG_OPEN_DRAIN,
50
+ .mask = GPIO_V2_LINE_FLAG_OPEN_DRAIN,
5051 },
5152 {
5253 .name = "open-source",
53
- .mask = GPIOLINE_FLAG_OPEN_SOURCE,
54
+ .mask = GPIO_V2_LINE_FLAG_OPEN_SOURCE,
55
+ },
56
+ {
57
+ .name = "pull-up",
58
+ .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_UP,
59
+ },
60
+ {
61
+ .name = "pull-down",
62
+ .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN,
63
+ },
64
+ {
65
+ .name = "bias-disabled",
66
+ .mask = GPIO_V2_LINE_FLAG_BIAS_DISABLED,
5467 },
5568 };
5669
57
-void print_flags(unsigned long flags)
70
+static void print_attributes(struct gpio_v2_line_info *info)
5871 {
5972 int i;
60
- int printed = 0;
73
+ const char *field_format = "%s";
6174
6275 for (i = 0; i < ARRAY_SIZE(flagnames); i++) {
63
- if (flags & flagnames[i].mask) {
64
- if (printed)
65
- fprintf(stdout, " ");
66
- fprintf(stdout, "%s", flagnames[i].name);
67
- printed++;
76
+ if (info->flags & flagnames[i].mask) {
77
+ fprintf(stdout, field_format, flagnames[i].name);
78
+ field_format = ", %s";
6879 }
80
+ }
81
+
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");
89
+
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);
6994 }
7095 }
7196
....@@ -85,7 +110,7 @@
85110 if (fd == -1) {
86111 ret = -errno;
87112 fprintf(stderr, "Failed to open %s\n", chrdev_name);
88
- goto exit_close_error;
113
+ goto exit_free_name;
89114 }
90115
91116 /* Inspect this GPIO chip */
....@@ -100,18 +125,18 @@
100125
101126 /* Loop over the lines and print info */
102127 for (i = 0; i < cinfo.lines; i++) {
103
- struct gpioline_info linfo;
128
+ struct gpio_v2_line_info linfo;
104129
105130 memset(&linfo, 0, sizeof(linfo));
106
- linfo.line_offset = i;
131
+ linfo.offset = i;
107132
108
- ret = ioctl(fd, GPIO_GET_LINEINFO_IOCTL, &linfo);
133
+ ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &linfo);
109134 if (ret == -1) {
110135 ret = -errno;
111136 perror("Failed to issue LINEINFO IOCTL\n");
112137 goto exit_close_error;
113138 }
114
- fprintf(stdout, "\tline %2d:", linfo.line_offset);
139
+ fprintf(stdout, "\tline %2d:", linfo.offset);
115140 if (linfo.name[0])
116141 fprintf(stdout, " \"%s\"", linfo.name);
117142 else
....@@ -122,7 +147,7 @@
122147 fprintf(stdout, " unused");
123148 if (linfo.flags) {
124149 fprintf(stdout, " [");
125
- print_flags(linfo.flags);
150
+ print_attributes(&linfo);
126151 fprintf(stdout, "]");
127152 }
128153 fprintf(stdout, "\n");
....@@ -132,6 +157,7 @@
132157 exit_close_error:
133158 if (close(fd) == -1)
134159 perror("Failed to close GPIO character device file");
160
+exit_free_name:
135161 free(chrdev_name);
136162 return ret;
137163 }