diff mbox series

[14/15] gpio: sandbox: Track whether a GPIO is driven

Message ID 20210115140500.846307-15-sjg@chromium.org
State Superseded
Delegated to: Simon Glass
Headers show
Series gpio: Update and simplify the uclass API | expand

Commit Message

Simon Glass Jan. 15, 2021, 2:04 p.m. UTC
Add a new flag to keep track of whether sandbox is driving the pin, or
whether it is expecting an input signal. If it is driving, then the value
of the pin is the value being driven (0 or 1). If not driving, then we
consider the value 0, since we don't currently handle things like pull-ups
yet.

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

 arch/sandbox/include/asm/gpio.h |  3 ++-
 drivers/gpio/sandbox.c          | 21 +++++++++++++++------
 2 files changed, 17 insertions(+), 7 deletions(-)

Comments

Patrick DELAUNAY Jan. 21, 2021, 10:19 a.m. UTC | #1
Hi Simon,

On 1/15/21 3:04 PM, Simon Glass wrote:
> Add a new flag to keep track of whether sandbox is driving the pin, or
> whether it is expecting an input signal. If it is driving, then the value
> of the pin is the value being driven (0 or 1). If not driving, then we
> consider the value 0, since we don't currently handle things like pull-ups
> yet.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>   arch/sandbox/include/asm/gpio.h |  3 ++-
>   drivers/gpio/sandbox.c          | 21 +++++++++++++++------
>   2 files changed, 17 insertions(+), 7 deletions(-)
>

Reviewed-by: Patrick Delaunay <patrick.delaunay@foss.st.com>

Thanks

Patrick
diff mbox series

Patch

diff --git a/arch/sandbox/include/asm/gpio.h b/arch/sandbox/include/asm/gpio.h
index 3f267797644..edf78cb4131 100644
--- a/arch/sandbox/include/asm/gpio.h
+++ b/arch/sandbox/include/asm/gpio.h
@@ -25,8 +25,9 @@ 
 
 /* Our own private GPIO flags, which musn't conflict with GPIOD_... */
 #define GPIOD_EXT_HIGH		BIT(20)	/* external source is high (else low) */
+#define GPIOD_EXT_DRIVEN	BIT(21)	/* external source is driven */
 
-#define GPIOD_SANDBOX_MASK	BIT(20)
+#define GPIOD_SANDBOX_MASK	GENMASK(21, 20)
 
 /**
  * Return the simulated value of a GPIO (used only in sandbox test code)
diff --git a/drivers/gpio/sandbox.c b/drivers/gpio/sandbox.c
index 11d4757abbb..b32f7ac529b 100644
--- a/drivers/gpio/sandbox.c
+++ b/drivers/gpio/sandbox.c
@@ -76,16 +76,22 @@  static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
 int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
 {
 	struct gpio_state *state = get_gpio_state(dev, offset);
+	bool val;
 
 	if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
 		debug("sandbox_gpio: get_value on output gpio %u\n", offset);
 
-	return state->flags & GPIOD_EXT_HIGH ? true : false;
+	if (state->flags & GPIOD_EXT_DRIVEN)
+		val = state->flags & GPIOD_EXT_HIGH;
+	else
+		val = false;
+
+	return val;
 }
 
 int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
 {
-	set_gpio_flag(dev, offset, GPIOD_EXT_HIGH, value);
+	set_gpio_flag(dev, offset, GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
 
 	return 0;
 }
@@ -142,8 +148,8 @@  static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
 	ret = sandbox_gpio_set_direction(dev, offset, 1);
 	if (ret)
 		return ret;
-	ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE | GPIOD_EXT_HIGH,
-			    value);
+	ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE |
+			    GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
 	if (ret)
 		return ret;
 
@@ -171,8 +177,8 @@  static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
 		return -1;
 	}
 
-	ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE | GPIOD_EXT_HIGH,
-			    value);
+	ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE |
+			    GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
 	if (ret)
 		return ret;
 
@@ -217,10 +223,13 @@  static int sb_gpio_update_flags(struct udevice *dev, uint offset, ulong flags)
 	struct gpio_state *state = get_gpio_state(dev, offset);
 
 	if (flags & GPIOD_IS_OUT) {
+		flags |= GPIOD_EXT_DRIVEN;
 		if (flags & GPIOD_IS_OUT_ACTIVE)
 			flags |= GPIOD_EXT_HIGH;
 		else
 			flags &= ~GPIOD_EXT_HIGH;
+	} else {
+		flags |= state->flags & GPIOD_SANDBOX_MASK;
 	}
 	state->flags = flags;