From: Kent Gibson <warthog618@gmail.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Linus Walleij <linus.walleij@linaro.org>,
Andy Shevchenko <andy@kernel.org>,
linux-gpio@vger.kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: Re: [PATCH libgpiod] tests: uapi: add test-cases for open-drain and open-source emulation
Date: Fri, 11 Apr 2025 09:33:58 +0800 [thread overview]
Message-ID: <20250411013358.GA47403@rigel> (raw)
In-Reply-To: <20250410-open-drain-source-tests-v1-1-a062d2280cc5@linaro.org>
On Thu, Apr 10, 2025 at 11:17:47AM +0200, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> The kernel GPIO subsystem can emulate open-drain and open-source by not
> actively driving the line for active and inactive output values
> respectively. The kernel does it by setting the line to input in these
> cases but this still must be reported as output to user-space. Add new
> test-cases that verify this behavior.
>
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> ---
> Andy's comment on a GPIOLIB patch made me realize it's a good idea to
> add tests for open-drain and open-source emulation in the kernel where
> we don't actively drive the line for active and inactive values
> respectively.
> ---
> tests/tests-kernel-uapi.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 87 insertions(+)
>
> diff --git a/tests/tests-kernel-uapi.c b/tests/tests-kernel-uapi.c
> index ff220fc..5955fac 100644
> --- a/tests/tests-kernel-uapi.c
> +++ b/tests/tests-kernel-uapi.c
> @@ -110,3 +110,90 @@ GPIOD_TEST_CASE(enable_debounce_then_edge_detection)
>
> g_assert_cmpuint(ts_falling, >, ts_rising);
> }
> +
> +GPIOD_TEST_CASE(open_drain_emulation)
> +{
> + static const guint offset = 2;
> +
> + g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
> + g_autoptr(struct_gpiod_chip) chip = NULL;
> + g_autoptr(struct_gpiod_line_settings) settings = NULL;
> + g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
> + g_autoptr(struct_gpiod_line_request) request = NULL;
> + g_autoptr(struct_gpiod_line_info) info = NULL;
> + gint ret;
> +
> + chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
> + settings = gpiod_test_create_line_settings_or_fail();
> + line_cfg = gpiod_test_create_line_config_or_fail();
> +
> + gpiod_line_settings_set_direction(settings,
> + GPIOD_LINE_DIRECTION_OUTPUT);
> + gpiod_line_settings_set_drive(settings, GPIOD_LINE_DRIVE_OPEN_DRAIN);
> + gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
> + settings);
> + request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
> +
> + ret = gpiod_line_request_set_value(request, offset,
> + GPIOD_LINE_VALUE_ACTIVE);
> + g_assert_cmpint(ret, ==, 0);
> + gpiod_test_return_if_failed();
> +
> + /*
> + * The open-drain emulation in the kernel will set the line's direction
> + * to input but NOT set FLAG_IS_OUT. Let's verify the direction is
> + * still reported as output.
> + */
My understanding is that FLAG_IS_OUT is always set for output lines,
even if the direction is set to input for the emulation.
To quote gpiod_direction_output():
set_output_flag:
/*
* When emulating open-source or open-drain functionalities by not
* actively driving the line (setting mode to input) we still need to
* set the IS_OUT flag or otherwise we won't be able to set the line
* value anymore.
*/
if (ret == 0)
set_bit(FLAG_IS_OUT, &desc->flags);
return ret;
Cheers,
Kent.
next prev parent reply other threads:[~2025-04-11 1:34 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-10 9:17 [PATCH libgpiod] tests: uapi: add test-cases for open-drain and open-source emulation Bartosz Golaszewski
2025-04-10 17:34 ` Andy Shevchenko
2025-04-11 2:12 ` Kent Gibson
2025-04-11 1:33 ` Kent Gibson [this message]
2025-04-11 7:32 ` Bartosz Golaszewski
2025-04-16 16:00 ` Bartosz Golaszewski
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=20250411013358.GA47403@rigel \
--to=warthog618@gmail.com \
--cc=andy@kernel.org \
--cc=bartosz.golaszewski@linaro.org \
--cc=brgl@bgdev.pl \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.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