All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] gpio: add gpio open-drain & open-source emulation
@ 2020-05-05  8:43 Neil Armstrong
  2020-05-05  8:43 ` [PATCH v2 1/2] gpio: emulate open drain & open source in dm_gpio_set_value() Neil Armstrong
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Neil Armstrong @ 2020-05-05  8:43 UTC (permalink / raw)
  To: u-boot

When a line is in open-drain or open-source mode, U-Boot doesn't handle it
directly.

Let's add gpio open-drain & open-source emulation in gpio u-class to make
it transparent to driver using a gpio line from DT.

Changes since v1:
- fixed all open-drain/open source cases
- updated dm gpio test

Neil Armstrong (2):
  gpio: emulate open drain & open source in dm_gpio_set_value()
  test: dm: update test for open-drain/open-source emulation in
    gpio-uclass

 arch/sandbox/dts/test.dts  |  4 +-
 drivers/gpio/gpio-uclass.c | 15 +++++++
 test/dm/gpio.c             | 89 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 107 insertions(+), 1 deletion(-)

-- 
2.22.0

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/2] gpio: emulate open drain & open source in dm_gpio_set_value()
  2020-05-05  8:43 [PATCH v2 0/2] gpio: add gpio open-drain & open-source emulation Neil Armstrong
@ 2020-05-05  8:43 ` Neil Armstrong
  2020-05-06 14:47   ` Simon Glass
  2020-05-05  8:43 ` [PATCH v2 2/2] test: dm: update test for open-drain/open-source emulation in gpio-uclass Neil Armstrong
  2020-05-11 13:09 ` [PATCH v2 0/2] gpio: add gpio open-drain & open-source emulation Neil Armstrong
  2 siblings, 1 reply; 7+ messages in thread
From: Neil Armstrong @ 2020-05-05  8:43 UTC (permalink / raw)
  To: u-boot

Handle the GPIOD_OPEN_DRAIN & GPIOD_OPEN_SOURCE flags to emulate open drain
and open source by setting the GPIO line as input depending on the
requested value.

The behaviour is taken from the Linux gpiolib.

Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
---
 drivers/gpio/gpio-uclass.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/drivers/gpio/gpio-uclass.c b/drivers/gpio/gpio-uclass.c
index 757ab7106e..d3cea11f76 100644
--- a/drivers/gpio/gpio-uclass.c
+++ b/drivers/gpio/gpio-uclass.c
@@ -526,6 +526,21 @@ int dm_gpio_set_value(const struct gpio_desc *desc, int value)
 
 	if (desc->flags & GPIOD_ACTIVE_LOW)
 		value = !value;
+
+	/*
+	 * Emulate open drain by not actively driving the line high or
+	 * Emulate open source by not actively driving the line low
+	 */
+	if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
+	    (desc->flags & GPIOD_OPEN_SOURCE && !value))
+		return gpio_get_ops(desc->dev)->direction_input(desc->dev,
+								desc->offset);
+	else if (desc->flags & GPIOD_OPEN_DRAIN ||
+		 desc->flags & GPIOD_OPEN_SOURCE)
+		return gpio_get_ops(desc->dev)->direction_output(desc->dev,
+								desc->offset,
+								value);
+
 	gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
 	return 0;
 }
-- 
2.22.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/2] test: dm: update test for open-drain/open-source emulation in gpio-uclass
  2020-05-05  8:43 [PATCH v2 0/2] gpio: add gpio open-drain & open-source emulation Neil Armstrong
  2020-05-05  8:43 ` [PATCH v2 1/2] gpio: emulate open drain & open source in dm_gpio_set_value() Neil Armstrong
@ 2020-05-05  8:43 ` Neil Armstrong
  2020-05-06 14:47   ` Simon Glass
  2020-05-11 13:09 ` [PATCH v2 0/2] gpio: add gpio open-drain & open-source emulation Neil Armstrong
  2 siblings, 1 reply; 7+ messages in thread
From: Neil Armstrong @ 2020-05-05  8:43 UTC (permalink / raw)
  To: u-boot

Add tests for testing open-drain/open-source emulation in gpio-uclass.
It also adds two test3-gpios configured as GPIO_ACTIVE_LOW.

Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
---
 arch/sandbox/dts/test.dts |  4 +-
 test/dm/gpio.c            | 89 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+), 1 deletion(-)

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 4bccfbe6e1..e9b9404363 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -104,7 +104,9 @@
 			<&gpio_c 2 GPIO_OUT>,
 			<&gpio_c 3 (GPIO_IN|GPIO_PULL_UP)>,
 			<&gpio_c 4 (GPIO_IN|GPIO_PULL_DOWN)>,
-			<&gpio_c 5 GPIO_IN>;
+			<&gpio_c 5 GPIO_IN>,
+			<&gpio_c 6 (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN)>,
+			<&gpio_c 7 (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE)>;
 		int-value = <1234>;
 		uint-value = <(-1234)>;
 		int64-value = /bits/ 64 <0x1111222233334444>;
diff --git a/test/dm/gpio.c b/test/dm/gpio.c
index f5c7aaf3bc..7c18e5c411 100644
--- a/test/dm/gpio.c
+++ b/test/dm/gpio.c
@@ -112,6 +112,95 @@ static int dm_test_gpio(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
 
+/* Test that GPIO open-drain/open-source emulation works correctly */
+static int dm_test_gpio_opendrain_opensource(struct unit_test_state *uts)
+{
+	struct gpio_desc desc_list[8];
+	struct udevice *dev, *gpio_c;
+	char buf[80];
+
+	ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
+	ut_asserteq_str("a-test", dev->name);
+
+	ut_assertok(uclass_get_device(UCLASS_GPIO, 3, &gpio_c));
+	ut_asserteq_str("pinmux-gpios", gpio_c->name);
+
+	ut_asserteq(8, gpio_request_list_by_name(dev, "test3-gpios", desc_list,
+						 ARRAY_SIZE(desc_list), 0))
+
+	ut_asserteq(true, !!device_active(gpio_c));
+	ut_asserteq_ptr(gpio_c, desc_list[0].dev);
+	ut_asserteq_ptr(gpio_c, desc_list[1].dev);
+	ut_asserteq_ptr(gpio_c, desc_list[2].dev);
+	ut_asserteq_ptr(gpio_c, desc_list[3].dev);
+	ut_asserteq_ptr(gpio_c, desc_list[4].dev);
+	ut_asserteq_ptr(gpio_c, desc_list[5].dev);
+	ut_asserteq_ptr(gpio_c, desc_list[6].dev);
+	ut_asserteq_ptr(gpio_c, desc_list[7].dev);
+
+	/* GPIO 0 is (GPIO_OUT|GPIO_OPEN_DRAIN) */
+	ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN,
+		    sandbox_gpio_get_dir_flags(gpio_c, 0));
+
+	/* Set it as output high, should become an input */
+	ut_assertok(dm_gpio_set_value(&desc_list[0], 1));
+	ut_assertok(gpio_get_status(gpio_c, 0, buf, sizeof(buf)));
+	ut_asserteq_str("c0: input: 0 [x] a-test.test3-gpios0", buf);
+
+	/* Set it as output low, should become output low */
+	ut_assertok(dm_gpio_set_value(&desc_list[0], 0));
+	ut_assertok(gpio_get_status(gpio_c, 0, buf, sizeof(buf)));
+	ut_asserteq_str("c0: output: 0 [x] a-test.test3-gpios0", buf);
+
+	/* GPIO 1 is (GPIO_OUT|GPIO_OPEN_SOURCE) */
+	ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE,
+		    sandbox_gpio_get_dir_flags(gpio_c, 1));
+
+	/* Set it as output high, should become output high */
+	ut_assertok(dm_gpio_set_value(&desc_list[1], 1));
+	ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf)));
+	ut_asserteq_str("c1: output: 1 [x] a-test.test3-gpios1", buf);
+
+	/* Set it as output low, should become an input */
+	ut_assertok(dm_gpio_set_value(&desc_list[1], 0));
+	ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf)));
+	ut_asserteq_str("c1: input: 1 [x] a-test.test3-gpios1", buf);
+
+	/* GPIO 6 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN) */
+	ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN,
+		    sandbox_gpio_get_dir_flags(gpio_c, 6));
+
+	/* Set it as output high, should become output low */
+	ut_assertok(dm_gpio_set_value(&desc_list[6], 1));
+	ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf)));
+	ut_asserteq_str("c6: output: 0 [x] a-test.test3-gpios6", buf);
+
+	/* Set it as output low, should become an input */
+	ut_assertok(dm_gpio_set_value(&desc_list[6], 0));
+	ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf)));
+	ut_asserteq_str("c6: input: 0 [x] a-test.test3-gpios6", buf);
+
+	/* GPIO 7 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE) */
+	ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_SOURCE,
+		    sandbox_gpio_get_dir_flags(gpio_c, 7));
+
+	/* Set it as output high, should become an input */
+	ut_assertok(dm_gpio_set_value(&desc_list[7], 1));
+	ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf)));
+	ut_asserteq_str("c7: input: 0 [x] a-test.test3-gpios7", buf);
+
+	/* Set it as output low, should become output high */
+	ut_assertok(dm_gpio_set_value(&desc_list[7], 0));
+	ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf)));
+	ut_asserteq_str("c7: output: 1 [x] a-test.test3-gpios7", buf);
+
+	ut_assertok(gpio_free_list(dev, desc_list, 8));
+
+	return 0;
+}
+DM_TEST(dm_test_gpio_opendrain_opensource,
+	DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
 /* Test that sandbox anonymous GPIOs work correctly */
 static int dm_test_gpio_anon(struct unit_test_state *uts)
 {
-- 
2.22.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/2] test: dm: update test for open-drain/open-source emulation in gpio-uclass
  2020-05-05  8:43 ` [PATCH v2 2/2] test: dm: update test for open-drain/open-source emulation in gpio-uclass Neil Armstrong
@ 2020-05-06 14:47   ` Simon Glass
  0 siblings, 0 replies; 7+ messages in thread
From: Simon Glass @ 2020-05-06 14:47 UTC (permalink / raw)
  To: u-boot

On Tue, 5 May 2020 at 02:43, Neil Armstrong <narmstrong@baylibre.com> wrote:
>
> Add tests for testing open-drain/open-source emulation in gpio-uclass.
> It also adds two test3-gpios configured as GPIO_ACTIVE_LOW.
>
> Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
> ---
>  arch/sandbox/dts/test.dts |  4 +-
>  test/dm/gpio.c            | 89 +++++++++++++++++++++++++++++++++++++++
>  2 files changed, 92 insertions(+), 1 deletion(-)
>

Reviewed-by: Simon Glass <sjg@chromium.org>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/2] gpio: emulate open drain & open source in dm_gpio_set_value()
  2020-05-05  8:43 ` [PATCH v2 1/2] gpio: emulate open drain & open source in dm_gpio_set_value() Neil Armstrong
@ 2020-05-06 14:47   ` Simon Glass
  2020-05-06 14:54     ` Tom Rini
  0 siblings, 1 reply; 7+ messages in thread
From: Simon Glass @ 2020-05-06 14:47 UTC (permalink / raw)
  To: u-boot

Hi Neil,

+Tom Rini

On Tue, 5 May 2020 at 02:43, Neil Armstrong <narmstrong@baylibre.com> wrote:
>
> Handle the GPIOD_OPEN_DRAIN & GPIOD_OPEN_SOURCE flags to emulate open drain
> and open source by setting the GPIO line as input depending on the
> requested value.
>
> The behaviour is taken from the Linux gpiolib.
>
> Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
> ---
>  drivers/gpio/gpio-uclass.c | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
>

Reviewed-by: Simon Glass <sjg@chromium.org>

I wonder how we should handle small increases in functionality, like
this. If a board doesn't want the code-stize hit in SPL, should we
have it behind a Kconfig, like CONFIG_GPIO_EXTRA?

Regards,
Simon

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/2] gpio: emulate open drain & open source in dm_gpio_set_value()
  2020-05-06 14:47   ` Simon Glass
@ 2020-05-06 14:54     ` Tom Rini
  0 siblings, 0 replies; 7+ messages in thread
From: Tom Rini @ 2020-05-06 14:54 UTC (permalink / raw)
  To: u-boot

On Wed, May 06, 2020 at 08:47:20AM -0600, Simon Glass wrote:
> Hi Neil,
> 
> +Tom Rini
> 
> On Tue, 5 May 2020 at 02:43, Neil Armstrong <narmstrong@baylibre.com> wrote:
> >
> > Handle the GPIOD_OPEN_DRAIN & GPIOD_OPEN_SOURCE flags to emulate open drain
> > and open source by setting the GPIO line as input depending on the
> > requested value.
> >
> > The behaviour is taken from the Linux gpiolib.
> >
> > Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
> > ---
> >  drivers/gpio/gpio-uclass.c | 15 +++++++++++++++
> >  1 file changed, 15 insertions(+)
> >
> 
> Reviewed-by: Simon Glass <sjg@chromium.org>
> 
> I wonder how we should handle small increases in functionality, like
> this. If a board doesn't want the code-stize hit in SPL, should we
> have it behind a Kconfig, like CONFIG_GPIO_EXTRA?

I suppose it'll come down to how much growth and where.

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200506/36460744/attachment.sig>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 0/2] gpio: add gpio open-drain & open-source emulation
  2020-05-05  8:43 [PATCH v2 0/2] gpio: add gpio open-drain & open-source emulation Neil Armstrong
  2020-05-05  8:43 ` [PATCH v2 1/2] gpio: emulate open drain & open source in dm_gpio_set_value() Neil Armstrong
  2020-05-05  8:43 ` [PATCH v2 2/2] test: dm: update test for open-drain/open-source emulation in gpio-uclass Neil Armstrong
@ 2020-05-11 13:09 ` Neil Armstrong
  2 siblings, 0 replies; 7+ messages in thread
From: Neil Armstrong @ 2020-05-11 13:09 UTC (permalink / raw)
  To: u-boot

On 05/05/2020 10:43, Neil Armstrong wrote:
> When a line is in open-drain or open-source mode, U-Boot doesn't handle it
> directly.
> 
> Let's add gpio open-drain & open-source emulation in gpio u-class to make
> it transparent to driver using a gpio line from DT.
> 
> Changes since v1:
> - fixed all open-drain/open source cases
> - updated dm gpio test
> 
> Neil Armstrong (2):
>   gpio: emulate open drain & open source in dm_gpio_set_value()
>   test: dm: update test for open-drain/open-source emulation in
>     gpio-uclass
> 
>  arch/sandbox/dts/test.dts  |  4 +-
>  drivers/gpio/gpio-uclass.c | 15 +++++++
>  test/dm/gpio.c             | 89 ++++++++++++++++++++++++++++++++++++++
>  3 files changed, 107 insertions(+), 1 deletion(-)
> 

Applying to u-boot-amlogic

Thanks for reviewing.

Neil

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-05-11 13:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-05  8:43 [PATCH v2 0/2] gpio: add gpio open-drain & open-source emulation Neil Armstrong
2020-05-05  8:43 ` [PATCH v2 1/2] gpio: emulate open drain & open source in dm_gpio_set_value() Neil Armstrong
2020-05-06 14:47   ` Simon Glass
2020-05-06 14:54     ` Tom Rini
2020-05-05  8:43 ` [PATCH v2 2/2] test: dm: update test for open-drain/open-source emulation in gpio-uclass Neil Armstrong
2020-05-06 14:47   ` Simon Glass
2020-05-11 13:09 ` [PATCH v2 0/2] gpio: add gpio open-drain & open-source emulation Neil Armstrong

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.