diff options
author | Ahmad Fatoum <a.fatoum@pengutronix.de> | 2022-09-05 12:35:45 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2022-09-13 12:07:55 +0200 |
commit | 04443dc5fc0ea0b0bd7eb40408774b6db8a0c399 (patch) | |
tree | 438341d50648622d2e58a722f5f002826f6cd1f5 /commands | |
parent | 3f26660186b41c55538c4abb62c77d8541adc738 (diff) | |
download | barebox-04443dc5fc0ea0b0bd7eb40408774b6db8a0c399.tar.gz |
commands: gpio: add -d argument to set/get commands
For debugging, it can be useful to reference GPIOs relative to a
controller, e.g.:
gpio_direction_output -d gpio4 20 1
instead of calculating the global gpio number. Extend the GPIO
functions to support that. The argument to -d may be either a device
name, a full path to a device tree node or an alias as in the example
above.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Link: https://lore.barebox.org/20220905103546.1476277-5-a.fatoum@pengutronix.de
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands')
-rw-r--r-- | commands/gpio.c | 51 |
1 files changed, 41 insertions, 10 deletions
diff --git a/commands/gpio.c b/commands/gpio.c index 955b60e91b..d04fd65bc8 100644 --- a/commands/gpio.c +++ b/commands/gpio.c @@ -4,27 +4,58 @@ #include <command.h> #include <errno.h> #include <gpio.h> +#include <getopt.h> static int get_gpio_and_value(int argc, char *argv[], int *gpio, int *value) { - const int count = value ? 3 : 2; + struct gpio_chip *chip = NULL; + struct device_d *dev; + int count = 2; int ret = 0; + int opt; + + while ((opt = getopt(argc, argv, "d:")) > 0) { + switch (opt) { + case 'd': + dev = find_device(optarg); + if (!dev) + return -ENODEV; + + chip = gpio_get_chip_by_dev(dev); + if (!chip) + return -EINVAL; + break; + default: + return COMMAND_ERROR_USAGE; + } + } + + if (value) + count++; - if (argc < count) + if (optind < count) return COMMAND_ERROR_USAGE; - *gpio = gpio_find_by_name(argv[1]); + *gpio = gpio_find_by_name(argv[optind]); if (*gpio < 0) - *gpio = gpio_find_by_label(argv[1]); + *gpio = gpio_find_by_label(argv[optind]); if (*gpio < 0) { - ret = kstrtoint(argv[1], 0, gpio); + ret = kstrtoint(argv[optind], 0, gpio); if (ret < 0) return ret; + + if (chip) + *gpio += chip->base; + } else if (chip) { + if (gpio_get_chip(*gpio) != chip) { + printf("%s: not exporting pin %u\n", dev_name(chip->dev), *gpio); + return -EINVAL; + } } if (value) - ret = kstrtoint(argv[2], 0, value); + ret = kstrtoint(argv[optind + 1], 0, value); return ret; } @@ -47,7 +78,7 @@ static int do_gpio_get_value(int argc, char *argv[]) BAREBOX_CMD_START(gpio_get_value) .cmd = do_gpio_get_value, BAREBOX_CMD_DESC("return value of a GPIO pin") - BAREBOX_CMD_OPTS("GPIO") + BAREBOX_CMD_OPTS("[-d CONTROLLER] GPIO") BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP) BAREBOX_CMD_END @@ -67,7 +98,7 @@ static int do_gpio_set_value(int argc, char *argv[]) BAREBOX_CMD_START(gpio_set_value) .cmd = do_gpio_set_value, BAREBOX_CMD_DESC("set a GPIO's output value") - BAREBOX_CMD_OPTS("GPIO VALUE") + BAREBOX_CMD_OPTS("[-d CONTROLLER] GPIO VALUE") BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP) BAREBOX_CMD_END @@ -89,7 +120,7 @@ static int do_gpio_direction_input(int argc, char *argv[]) BAREBOX_CMD_START(gpio_direction_input) .cmd = do_gpio_direction_input, BAREBOX_CMD_DESC("set direction of a GPIO pin to input") - BAREBOX_CMD_OPTS("GPIO") + BAREBOX_CMD_OPTS("[-d CONTROLLER] GPIO") BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP) BAREBOX_CMD_END @@ -111,6 +142,6 @@ static int do_gpio_direction_output(int argc, char *argv[]) BAREBOX_CMD_START(gpio_direction_output) .cmd = do_gpio_direction_output, BAREBOX_CMD_DESC("set direction of a GPIO pin to output") - BAREBOX_CMD_OPTS("GPIO VALUE") + BAREBOX_CMD_OPTS("[-d CONTROLLER] GPIO VALUE") BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP) BAREBOX_CMD_END |