2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
5 * Copyright (c) 2014, Rui Paulo <rpaulo@FreeBSD.org>
6 * Copyright (c) 2015, Emmanuel Vadot <manu@bidouilliste.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
44 #define PIN_TYPE_NUMBER 1
45 #define PIN_TYPE_NAME 2
52 static struct flag_desc gpio_flags[] = {
53 { "IN", GPIO_PIN_INPUT },
54 { "OUT", GPIO_PIN_OUTPUT },
55 { "OD", GPIO_PIN_OPENDRAIN },
56 { "PP", GPIO_PIN_PUSHPULL },
57 { "TS", GPIO_PIN_TRISTATE },
58 { "PU", GPIO_PIN_PULLUP },
59 { "PD", GPIO_PIN_PULLDOWN },
60 { "II", GPIO_PIN_INVIN },
61 { "IO", GPIO_PIN_INVOUT },
62 { "PULSE", GPIO_PIN_PULSATE },
63 { "INTRLL", GPIO_INTR_LEVEL_LOW},
64 { "INTRLH", GPIO_INTR_LEVEL_HIGH},
65 { "INTRER", GPIO_INTR_EDGE_RISING},
66 { "INTREF", GPIO_INTR_EDGE_FALLING},
67 { "INTREB", GPIO_INTR_EDGE_BOTH},
71 int str2cap(const char *str);
76 fprintf(stderr, "Usage:\n");
77 fprintf(stderr, "\tgpioctl [-f ctldev] -l [-v]\n");
78 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -t pin\n");
79 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -c pin flag ...\n");
80 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -n pin pin-name\n");
81 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] pin [0|1]\n");
88 struct flag_desc * pdesc = gpio_flags;
90 if (pdesc->flag == cap)
99 str2cap(const char *str)
101 struct flag_desc * pdesc = gpio_flags;
102 while (pdesc->name) {
103 if (strcasecmp(str, pdesc->name) == 0)
112 * Our handmade function for converting string to number
115 str2int(const char *s, int *ok)
118 int res = strtod(s, &endptr);
119 if (endptr != s + strlen(s) )
134 for (i = 0; i < 32; i++) {
135 if (caps & (1 << i)) {
138 printf("%s", cap2str(1 << i));
146 dump_pins(gpio_handle_t handle, int verbose)
152 maxpin = gpio_pin_list(handle, &cfgs);
154 perror("gpio_pin_list");
158 for (i = 0; i <= maxpin; i++) {
160 pinv = gpio_pin_get(handle, pin->g_pin);
161 printf("pin %02d:\t%d\t%s", pin->g_pin, pinv,
164 print_caps(pin->g_flags);
168 print_caps(pin->g_caps);
176 get_pinnum_by_name(gpio_handle_t handle, const char *name) {
182 maxpin = gpio_pin_list(handle, &cfgs);
184 perror("gpio_pin_list");
188 for (i = 0; i <= maxpin; i++) {
190 gpio_pin_get(handle, pin->g_pin);
191 if (!strcmp(name, pin->g_name)) {
202 fail(const char *fmt, ...)
207 vfprintf(stderr, fmt, ap);
213 main(int argc, char **argv)
217 gpio_handle_t handle;
218 char *ctlfile = NULL;
219 int pinn, pinv, pin_type, ch;
221 int config, list, name, toggle, verbose;
223 config = toggle = verbose = list = name = pin_type = 0;
225 while ((ch = getopt(argc, argv, "cf:lntvNp")) != -1) {
240 pin_type = PIN_TYPE_NAME;
243 pin_type = PIN_TYPE_NUMBER;
259 handle = gpio_open(0);
261 handle = gpio_open_device(ctlfile);
262 if (handle == GPIO_INVALID_HANDLE) {
268 dump_pins(handle, verbose);
276 /* Find the pin number by the name */
279 /* First test if it is a pin number */
280 pinn = str2int(argv[0], &ok);
282 /* Test if we have any pin named by this number and tell the user */
283 if (get_pinnum_by_name(handle, argv[0]) != -1)
284 fail("%s is also a pin name, use -p or -N\n", argv[0]);
286 /* Test if it is a name */
287 if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1)
288 fail("Can't find pin named \"%s\"\n", argv[0]);
291 case PIN_TYPE_NUMBER:
292 pinn = str2int(argv[0], &ok);
294 fail("Invalid pin number: %s\n", argv[0]);
297 if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1)
298 fail("Can't find pin named \"%s\"\n", argv[0]);
302 /* Set the pin name. */
306 if (gpio_pin_set_name(handle, pinn, argv[1]) < 0) {
307 perror("gpio_pin_set_name");
315 * -t pin assumes no additional arguments
319 if (gpio_pin_toggle(handle, pinn) < 0) {
320 perror("gpio_pin_toggle");
329 for (i = 1; i < argc; i++) {
330 flag = str2cap(argv[i]);
332 fail("Invalid flag: %s\n", argv[i]);
333 else if ((flag & GPIO_INTR_MASK) != 0)
334 fail("Interrupt capability %s cannot be set as configuration flag\n", argv[i]);
339 if (gpio_pin_set_flags(handle, &pin) < 0) {
340 perror("gpio_pin_set_flags");
347 * Last two cases - set value or print value
349 if ((argc == 0) || (argc > 2))
356 pinv = gpio_pin_get(handle, pinn);
358 perror("gpio_pin_get");
361 printf("%d\n", pinv);
365 /* Is it valid number (0 or 1) ? */
366 pinv = str2int(argv[1], &ok);
367 if (ok == 0 || ((pinv != 0) && (pinv != 1)))
368 fail("Invalid pin value: %s\n", argv[1]);
373 if (gpio_pin_set(handle, pinn, pinv) < 0) {
374 perror("gpio_pin_set");