diff mbox series

[libgpiod] tools: gpioget: add new --dir-as-is option for GPO read-back

Message ID 20210427154224.30372-1-a.fatoum@pengutronix.de
State New
Headers show
Series [libgpiod] tools: gpioget: add new --dir-as-is option for GPO read-back | expand

Commit Message

Ahmad Fatoum April 27, 2021, 3:42 p.m. UTC
Both legacy sysfs and new character device API support querying line
state of a GPIO configured as output. But while sysfs /value can
be read for these output GPIOs, gpioget unconditionally muxes the
line as input. To ease migration to the new user API, add a new
--dir-as-is parameter that doesn't force the line to input.

This is especially useful for GPIO controllers that maintain their
last configured output state.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
-n is chosen for the short option because it's the customary short
option for dry runs, which sounds similar to what a gpio get without
line state configuration is doing.
---
 tools/gpioget.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

Comments

Bartosz Golaszewski April 30, 2021, 8:02 p.m. UTC | #1
On Tue, Apr 27, 2021 at 5:42 PM Ahmad Fatoum <a.fatoum@pengutronix.de> wrote:
>

> Both legacy sysfs and new character device API support querying line

> state of a GPIO configured as output. But while sysfs /value can

> be read for these output GPIOs, gpioget unconditionally muxes the

> line as input. To ease migration to the new user API, add a new

> --dir-as-is parameter that doesn't force the line to input.

>

> This is especially useful for GPIO controllers that maintain their

> last configured output state.

>

> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

> ---

> -n is chosen for the short option because it's the customary short

> option for dry runs, which sounds similar to what a gpio get without

> line state configuration is doing.

> ---

>  tools/gpioget.c | 10 ++++++++--

>  1 file changed, 8 insertions(+), 2 deletions(-)

>

> diff --git a/tools/gpioget.c b/tools/gpioget.c

> index ceeec566683a..bfbf5ea748be 100644

> --- a/tools/gpioget.c

> +++ b/tools/gpioget.c

> @@ -13,11 +13,12 @@ static const struct option longopts[] = {

>         { "help",       no_argument,            NULL,   'h' },

>         { "version",    no_argument,            NULL,   'v' },

>         { "active-low", no_argument,            NULL,   'l' },

> +       { "dir-as-is",  no_argument,            NULL,   'n' },

>         { "bias",       required_argument,      NULL,   'B' },

>         { GETOPT_NULL_LONGOPT },

>  };

>

> -static const char *const shortopts = "+hvlB:";

> +static const char *const shortopts = "+hvlnB:";

>

>  static void print_help(void)

>  {

> @@ -30,6 +31,7 @@ static void print_help(void)

>         printf("  -h, --help:\t\tdisplay this message and exit\n");

>         printf("  -v, --version:\tdisplay the version and exit\n");

>         printf("  -l, --active-low:\tset the line active state to low\n");

> +       printf("  -n, --dir-as-is:\tdon't force-reconfigure line direction\n");

>         printf("  -B, --bias=[as-is|disable|pull-down|pull-up] (defaults to 'as-is'):\n");

>         printf("                set the line bias\n");

>         printf("\n");

> @@ -40,6 +42,7 @@ int main(int argc, char **argv)

>  {

>         struct gpiod_line_request_config config;

>         int *values, optc, opti, rv, flags = 0;

> +       int request_type = GPIOD_LINE_REQUEST_DIRECTION_INPUT;

>         unsigned int *offsets, i, num_lines;

>         struct gpiod_line_bulk *lines;

>         struct gpiod_chip *chip;

> @@ -60,6 +63,9 @@ int main(int argc, char **argv)

>                 case 'l':

>                         flags |= GPIOD_LINE_REQUEST_FLAG_ACTIVE_LOW;

>                         break;

> +               case 'n':

> +                       request_type = GPIOD_LINE_REQUEST_DIRECTION_AS_IS;

> +                       break;

>                 case 'B':

>                         flags |= bias_flags(optarg);

>                         break;

> @@ -104,7 +110,7 @@ int main(int argc, char **argv)

>         memset(&config, 0, sizeof(config));

>

>         config.consumer = "gpioget";

> -       config.request_type = GPIOD_LINE_REQUEST_DIRECTION_INPUT;

> +       config.request_type = request_type;

>         config.flags = flags;

>

>         rv = gpiod_line_request_bulk(lines, &config, NULL);

> --

> 2.29.2

>


Makes sense, applied to master.

It'll take some time until this is released though, libgpiod v2 is in
the making and I no longer add new features to the v1.6.x branch
anymore.

Thanks!
Bartosz
diff mbox series

Patch

diff --git a/tools/gpioget.c b/tools/gpioget.c
index ceeec566683a..bfbf5ea748be 100644
--- a/tools/gpioget.c
+++ b/tools/gpioget.c
@@ -13,11 +13,12 @@  static const struct option longopts[] = {
 	{ "help",	no_argument,		NULL,	'h' },
 	{ "version",	no_argument,		NULL,	'v' },
 	{ "active-low",	no_argument,		NULL,	'l' },
+	{ "dir-as-is",	no_argument,		NULL,	'n' },
 	{ "bias",	required_argument,	NULL,	'B' },
 	{ GETOPT_NULL_LONGOPT },
 };
 
-static const char *const shortopts = "+hvlB:";
+static const char *const shortopts = "+hvlnB:";
 
 static void print_help(void)
 {
@@ -30,6 +31,7 @@  static void print_help(void)
 	printf("  -h, --help:\t\tdisplay this message and exit\n");
 	printf("  -v, --version:\tdisplay the version and exit\n");
 	printf("  -l, --active-low:\tset the line active state to low\n");
+	printf("  -n, --dir-as-is:\tdon't force-reconfigure line direction\n");
 	printf("  -B, --bias=[as-is|disable|pull-down|pull-up] (defaults to 'as-is'):\n");
 	printf("		set the line bias\n");
 	printf("\n");
@@ -40,6 +42,7 @@  int main(int argc, char **argv)
 {
 	struct gpiod_line_request_config config;
 	int *values, optc, opti, rv, flags = 0;
+	int request_type = GPIOD_LINE_REQUEST_DIRECTION_INPUT;
 	unsigned int *offsets, i, num_lines;
 	struct gpiod_line_bulk *lines;
 	struct gpiod_chip *chip;
@@ -60,6 +63,9 @@  int main(int argc, char **argv)
 		case 'l':
 			flags |= GPIOD_LINE_REQUEST_FLAG_ACTIVE_LOW;
 			break;
+		case 'n':
+			request_type = GPIOD_LINE_REQUEST_DIRECTION_AS_IS;
+			break;
 		case 'B':
 			flags |= bias_flags(optarg);
 			break;
@@ -104,7 +110,7 @@  int main(int argc, char **argv)
 	memset(&config, 0, sizeof(config));
 
 	config.consumer = "gpioget";
-	config.request_type = GPIOD_LINE_REQUEST_DIRECTION_INPUT;
+	config.request_type = request_type;
 	config.flags = flags;
 
 	rv = gpiod_line_request_bulk(lines, &config, NULL);