Skip to content

Commit 6d591c4

Browse files
author
Linus Walleij
committed
tools/gpio: create GPIO tools
This creates GPIO tools under tools/gpio/* and adds a single example program to list the GPIOs on a system. When proper devices are created it provides this minimal output: Cc: Johan Hovold <johan@kernel.org> Cc: Michael Welling <mwelling@ieee.org> Cc: Markus Pargmann <mpa@pengutronix.de> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 3c702e9 commit 6d591c4

6 files changed

Lines changed: 182 additions & 3 deletions

File tree

‎MAINTAINERS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4820,6 +4820,7 @@ F: include/linux/gpio/
48204820
F: include/linux/gpio.h
48214821
F: include/asm-generic/gpio.h
48224822
F: include/uapi/linux/gpio.h
4823+
F: tools/gpio/
48234824

48244825
GRE DEMULTIPLEXER DRIVER
48254826
M: Dmitry Kozlov <xeb@mail.ru>

‎tools/Makefile‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ help:
1313
@echo ' cpupower - a tool for all things x86 CPU power'
1414
@echo ' firewire - the userspace part of nosy, an IEEE-1394 traffic sniffer'
1515
@echo ' freefall - laptop accelerometer program for disk protection'
16+
@echo ' gpio - GPIO tools'
1617
@echo ' hv - tools used when in Hyper-V clients'
1718
@echo ' iio - IIO tools'
1819
@echo ' lguest - a minimal 32-bit x86 hypervisor'
@@ -53,7 +54,7 @@ acpi: FORCE
5354
cpupower: FORCE
5455
$(call descend,power/$@)
5556

56-
cgroup firewire hv guest spi usb virtio vm net iio: FORCE
57+
cgroup firewire hv guest spi usb virtio vm net iio gpio: FORCE
5758
$(call descend,$@)
5859

5960
liblockdep: FORCE
@@ -119,7 +120,7 @@ acpi_clean:
119120
cpupower_clean:
120121
$(call descend,power/cpupower,clean)
121122

122-
cgroup_clean hv_clean firewire_clean lguest_clean spi_clean usb_clean virtio_clean vm_clean net_clean iio_clean:
123+
cgroup_clean hv_clean firewire_clean lguest_clean spi_clean usb_clean virtio_clean vm_clean net_clean iio_clean gpio_clean:
123124
$(call descend,$(@:_clean=),clean)
124125

125126
liblockdep_clean:
@@ -155,6 +156,7 @@ build_clean:
155156
clean: acpi_clean cgroup_clean cpupower_clean hv_clean firewire_clean lguest_clean \
156157
perf_clean selftests_clean turbostat_clean spi_clean usb_clean virtio_clean \
157158
vm_clean net_clean iio_clean x86_energy_perf_policy_clean tmon_clean \
158-
freefall_clean build_clean libbpf_clean libsubcmd_clean liblockdep_clean
159+
freefall_clean build_clean libbpf_clean libsubcmd_clean liblockdep_clean \
160+
gpio_clean
159161

160162
.PHONY: FORCE

‎tools/gpio/Makefile‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CC = $(CROSS_COMPILE)gcc
2+
CFLAGS += -Wall -g -D_GNU_SOURCE
3+
4+
all: lsgpio
5+
6+
lsgpio: lsgpio.o gpio-utils.o
7+
8+
%.o: %.c gpio-utils.h
9+
10+
.PHONY: clean
11+
clean:
12+
rm -f *.o lsgpio

‎tools/gpio/gpio-utils.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* GPIO tools - helpers library for the GPIO tools
3+
*
4+
* 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.
9+
*/
10+
11+
#include "gpio-utils.h"

‎tools/gpio/gpio-utils.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* GPIO tools - utility helpers library for the GPIO tools
3+
*
4+
* Copyright (C) 2015 Linus Walleij
5+
*
6+
* Portions copied from iio_utils and lssio:
7+
* Copyright (c) 2010 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
8+
* Copyright (c) 2008 Jonathan Cameron
9+
* *
10+
* This program is free software; you can redistribute it and/or modify it
11+
* under the terms of the GNU General Public License version 2 as published by
12+
* the Free Software Foundation.
13+
*/
14+
#ifndef _GPIO_UTILS_H_
15+
#define _GPIO_UTILS_H_
16+
17+
#include <string.h>
18+
19+
static inline int check_prefix(const char *str, const char *prefix)
20+
{
21+
return strlen(str) > strlen(prefix) &&
22+
strncmp(str, prefix, strlen(prefix)) == 0;
23+
}
24+
25+
#endif /* _GPIO_UTILS_H_ */

‎tools/gpio/lsgpio.c‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* lsgpio - example on how to list the GPIO lines on a system
3+
*
4+
* 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.
9+
*
10+
* Usage:
11+
* lsgpio <-n device-name>
12+
*/
13+
14+
#include <unistd.h>
15+
#include <stdlib.h>
16+
#include <stdbool.h>
17+
#include <stdio.h>
18+
#include <dirent.h>
19+
#include <errno.h>
20+
#include <string.h>
21+
#include <poll.h>
22+
#include <fcntl.h>
23+
#include <getopt.h>
24+
#include <sys/ioctl.h>
25+
#include <linux/gpio.h>
26+
27+
#include "gpio-utils.h"
28+
29+
int list_device(const char *device_name)
30+
{
31+
struct gpiochip_info cinfo;
32+
char *chrdev_name;
33+
int fd;
34+
int ret;
35+
36+
ret = asprintf(&chrdev_name, "/dev/%s", device_name);
37+
if (ret < 0)
38+
return -ENOMEM;
39+
40+
fd = open(chrdev_name, 0);
41+
if (fd == -1) {
42+
ret = -errno;
43+
fprintf(stderr, "Failed to open %s\n", chrdev_name);
44+
goto free_chrdev_name;
45+
}
46+
47+
/* Inspect this GPIO chip */
48+
ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &cinfo);
49+
if (ret == -1) {
50+
ret = -errno;
51+
fprintf(stderr, "Failed to retrieve GPIO fd\n");
52+
if (close(fd) == -1)
53+
perror("Failed to close GPIO character device file");
54+
55+
goto free_chrdev_name;
56+
}
57+
fprintf(stdout, "GPIO chip: %s, %u GPIO lines\n",
58+
cinfo.name, cinfo.lines);
59+
60+
if (close(fd) == -1) {
61+
ret = -errno;
62+
goto free_chrdev_name;
63+
}
64+
65+
free_chrdev_name:
66+
free(chrdev_name);
67+
68+
return ret;
69+
70+
}
71+
72+
void print_usage(void)
73+
{
74+
fprintf(stderr, "Usage: lsgpio [options]...\n"
75+
"List GPIO chips, lines and states\n"
76+
" -n <name> List GPIOs on a named device\n"
77+
" -? This helptext\n"
78+
);
79+
}
80+
81+
int main(int argc, char **argv)
82+
{
83+
const char *device_name;
84+
int ret;
85+
int c;
86+
87+
while ((c = getopt(argc, argv, "n:")) != -1) {
88+
switch (c) {
89+
case 'n':
90+
device_name = optarg;
91+
break;
92+
case '?':
93+
print_usage();
94+
return -1;
95+
}
96+
}
97+
98+
if (device_name)
99+
ret = list_device(device_name);
100+
else {
101+
const struct dirent *ent;
102+
DIR *dp;
103+
104+
/* List all GPIO devices one at a time */
105+
dp = opendir("/dev");
106+
if (!dp) {
107+
ret = -errno;
108+
goto error_out;
109+
}
110+
111+
ret = -ENOENT;
112+
while (ent = readdir(dp), ent) {
113+
if (check_prefix(ent->d_name, "gpiochip")) {
114+
ret = list_device(ent->d_name);
115+
if (ret)
116+
break;
117+
}
118+
}
119+
120+
ret = 0;
121+
if (closedir(dp) == -1) {
122+
perror("scanning devices: Failed to close directory");
123+
ret = -errno;
124+
}
125+
}
126+
error_out:
127+
return ret;
128+
}

0 commit comments

Comments
 (0)