diff mbox series

[v2,1/2] gpio: emulate open drain & open source in dm_gpio_set_value()

Message ID 20200505084318.15307-2-narmstrong@baylibre.com
State Accepted
Commit 47bd533e9dd0f967ff7b62f3edfd6c97131e1501
Headers show
Series gpio: add gpio open-drain & open-source emulation | expand

Commit Message

Neil Armstrong May 5, 2020, 8:43 a.m. UTC
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 at baylibre.com>
---
 drivers/gpio/gpio-uclass.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

Comments

Simon Glass May 6, 2020, 2:47 p.m. UTC | #1
Hi Neil,

+Tom Rini

On Tue, 5 May 2020 at 02:43, Neil Armstrong <narmstrong at 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 at baylibre.com>
> ---
>  drivers/gpio/gpio-uclass.c | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
>

Reviewed-by: Simon Glass <sjg at 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
Tom Rini May 6, 2020, 2:54 p.m. UTC | #2
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 at 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 at baylibre.com>
> > ---
> >  drivers/gpio/gpio-uclass.c | 15 +++++++++++++++
> >  1 file changed, 15 insertions(+)
> >
> 
> Reviewed-by: Simon Glass <sjg at 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.
diff mbox series

Patch

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;
 }