From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/5] commands: gpio: add -d argument to set/get commands
Date: Mon, 5 Sep 2022 12:35:45 +0200 [thread overview]
Message-ID: <20220905103546.1476277-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20220905103546.1476277-1-a.fatoum@pengutronix.de>
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>
---
commands/gpio.c | 51 +++++++++++++++++++++++++++++++++++++++----------
1 file changed, 41 insertions(+), 10 deletions(-)
diff --git a/commands/gpio.c b/commands/gpio.c
index 955b60e91bce..d04fd65bc8e0 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
--
2.30.2
next prev parent reply other threads:[~2022-09-05 13:32 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-05 10:35 [PATCH 0/5] commands: gpio: add controller as optional argument Ahmad Fatoum
2022-09-05 10:35 ` [PATCH 1/5] gpiolib: implement gpio_get_chip_by_dev() Ahmad Fatoum
2022-09-05 10:35 ` [PATCH 2/5] of: platform: optimize of_find_device_by_node when deep probing Ahmad Fatoum
2022-09-05 10:35 ` [PATCH 3/5] driver: implement find_device() helper Ahmad Fatoum
2022-09-05 10:35 ` Ahmad Fatoum [this message]
2022-09-28 13:37 ` [PATCH 4/5] commands: gpio: add -d argument to set/get commands Enrico Scholz
2022-09-28 14:36 ` Sascha Hauer
2022-09-29 9:50 ` Ahmad Fatoum
2022-09-05 10:35 ` [PATCH 5/5] gpiolib: gpioinfo: add optional CONTROLLER command line argument Ahmad Fatoum
2022-09-28 13:22 ` Enrico Scholz
2022-09-28 14:24 ` Sascha Hauer
2022-09-12 9:13 ` [PATCH 0/5] commands: gpio: add controller as optional argument Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220905103546.1476277-5-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox