gpio: sandbox: Rename GPIO dir_flags to flags

Adjust the terminology in this driver to reflect that fact that all flags
are handled, not just direction flags.

Create a new access function to get the full GPIO state, not just the
direction flags. Drop the static invalid_dir_flags since we can rely on a
segfault if something is wrong.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Patrick Delaunay <patrick.delaunay@foss.st.com>
This commit is contained in:
Simon Glass 2021-02-04 21:21:59 -07:00 committed by Tom Rini
parent c0c1e62c6e
commit a03a0aa7e8
3 changed files with 47 additions and 39 deletions

View file

@ -69,17 +69,17 @@ int sandbox_gpio_set_direction(struct udevice *dev, unsigned int offset,
* @param offset GPIO offset within bank * @param offset GPIO offset within bank
* @return dir_flags: bitfield accesses by GPIOD_ defines * @return dir_flags: bitfield accesses by GPIOD_ defines
*/ */
ulong sandbox_gpio_get_dir_flags(struct udevice *dev, unsigned int offset); ulong sandbox_gpio_get_flags(struct udevice *dev, unsigned int offset);
/** /**
* Set the simulated flags of a GPIO (used only in sandbox test code) * Set the simulated flags of a GPIO (used only in sandbox test code)
* *
* @param dev device to use * @param dev device to use
* @param offset GPIO offset within bank * @param offset GPIO offset within bank
* @param flags dir_flags: bitfield accesses by GPIOD_ defines * @param flags bitfield accesses by GPIOD_ defines
* @return -1 on error, 0 if ok * @return -1 on error, 0 if ok
*/ */
int sandbox_gpio_set_dir_flags(struct udevice *dev, unsigned int offset, int sandbox_gpio_set_flags(struct udevice *dev, unsigned int offset,
ulong flags); ulong flags);
#endif #endif

View file

@ -22,34 +22,44 @@
struct gpio_state { struct gpio_state {
const char *label; /* label given by requester */ const char *label; /* label given by requester */
ulong dir_flags; /* dir_flags (GPIOD_...) */ ulong flags; /* flags (GPIOD_...) */
}; };
/* Access routines for GPIO dir flags */ /* Access routines for GPIO info */
static ulong *get_gpio_dir_flags(struct udevice *dev, unsigned int offset) static struct gpio_state *get_gpio_state(struct udevice *dev, uint offset)
{ {
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
struct gpio_state *state = dev_get_priv(dev); struct gpio_state *state = dev_get_priv(dev);
if (offset >= uc_priv->gpio_count) { if (offset >= uc_priv->gpio_count) {
static ulong invalid_dir_flags;
printf("sandbox_gpio: error: invalid gpio %u\n", offset); printf("sandbox_gpio: error: invalid gpio %u\n", offset);
return &invalid_dir_flags; return NULL;
} }
return &state[offset].dir_flags; return &state[offset];
}
/* Access routines for GPIO flags */
static ulong *get_gpio_flags(struct udevice *dev, unsigned int offset)
{
struct gpio_state *state = get_gpio_state(dev, offset);
if (!state)
return NULL;
return &state->flags;
} }
static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag) static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag)
{ {
return (*get_gpio_dir_flags(dev, offset) & flag) != 0; return (*get_gpio_flags(dev, offset) & flag) != 0;
} }
static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag, static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
int value) int value)
{ {
ulong *gpio = get_gpio_dir_flags(dev, offset); ulong *gpio = get_gpio_flags(dev, offset);
if (value) if (value)
*gpio |= flag; *gpio |= flag;
@ -88,15 +98,14 @@ int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
return 0; return 0;
} }
ulong sandbox_gpio_get_dir_flags(struct udevice *dev, unsigned int offset) ulong sandbox_gpio_get_flags(struct udevice *dev, uint offset)
{ {
return *get_gpio_dir_flags(dev, offset); return *get_gpio_flags(dev, offset);
} }
int sandbox_gpio_set_dir_flags(struct udevice *dev, unsigned int offset, int sandbox_gpio_set_flags(struct udevice *dev, uint offset, ulong flags)
ulong flags)
{ {
*get_gpio_dir_flags(dev, offset) = flags; *get_gpio_flags(dev, offset) = flags;
return 0; return 0;
} }
@ -180,30 +189,29 @@ static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
static int sb_gpio_set_flags(struct udevice *dev, unsigned int offset, static int sb_gpio_set_flags(struct udevice *dev, unsigned int offset,
ulong flags) ulong flags)
{ {
ulong *dir_flags; ulong *newf;
debug("%s: offset:%u, dir_flags = %lx\n", __func__, offset, flags); debug("%s: offset:%u, flags = %lx\n", __func__, offset, flags);
dir_flags = get_gpio_dir_flags(dev, offset); newf = get_gpio_flags(dev, offset);
/* /*
* For testing purposes keep the output value when switching to input. * For testing purposes keep the output value when switching to input.
* This allows us to manipulate the input value via the gpio command. * This allows us to manipulate the input value via the gpio command.
*/ */
if (flags & GPIOD_IS_IN) if (flags & GPIOD_IS_IN)
*dir_flags = (flags & ~GPIOD_IS_OUT_ACTIVE) | *newf = (flags & ~GPIOD_IS_OUT_ACTIVE) |
(*dir_flags & GPIOD_IS_OUT_ACTIVE); (*newf & GPIOD_IS_OUT_ACTIVE);
else else
*dir_flags = flags; *newf = flags;
return 0; return 0;
} }
static int sb_gpio_get_flags(struct udevice *dev, unsigned int offset, static int sb_gpio_get_flags(struct udevice *dev, uint offset, ulong *flagsp)
ulong *flagsp)
{ {
debug("%s: offset:%u\n", __func__, offset); debug("%s: offset:%u\n", __func__, offset);
*flagsp = *get_gpio_dir_flags(dev, offset); *flagsp = *get_gpio_flags(dev, offset);
return 0; return 0;
} }
@ -456,7 +464,7 @@ static const char *sb_pinctrl_get_pin_name(struct udevice *dev,
return pin_name; return pin_name;
} }
static char *get_dir_flags_string(ulong flags) static char *get_flags_string(ulong flags)
{ {
if (flags & GPIOD_OPEN_DRAIN) if (flags & GPIOD_OPEN_DRAIN)
return "drive-open-drain"; return "drive-open-drain";
@ -475,7 +483,7 @@ static int sb_pinctrl_get_pin_muxing(struct udevice *dev,
{ {
struct udevice *gpio_dev; struct udevice *gpio_dev;
unsigned int gpio_idx; unsigned int gpio_idx;
ulong dir_flags; ulong flags;
int function; int function;
/* look up for the bank which owns the requested pin */ /* look up for the bank which owns the requested pin */
@ -484,11 +492,11 @@ static int sb_pinctrl_get_pin_muxing(struct udevice *dev,
snprintf(buf, size, "Error"); snprintf(buf, size, "Error");
} else { } else {
function = sb_gpio_get_function(gpio_dev, gpio_idx); function = sb_gpio_get_function(gpio_dev, gpio_idx);
dir_flags = *get_gpio_dir_flags(gpio_dev, gpio_idx); flags = *get_gpio_flags(gpio_dev, gpio_idx);
snprintf(buf, size, "gpio %s %s", snprintf(buf, size, "gpio %s %s",
function == GPIOF_OUTPUT ? "output" : "input", function == GPIOF_OUTPUT ? "output" : "input",
get_dir_flags_string(dir_flags)); get_flags_string(flags));
} }
return 0; return 0;

View file

@ -80,15 +80,15 @@ static int dm_test_gpio(struct unit_test_state *uts)
/* Make it an open drain output, and reset it */ /* Make it an open drain output, and reset it */
ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE,
sandbox_gpio_get_dir_flags(dev, offset)); sandbox_gpio_get_flags(dev, offset));
ut_assertok(ops->set_flags(dev, offset, ut_assertok(ops->set_flags(dev, offset,
GPIOD_IS_OUT | GPIOD_OPEN_DRAIN)); GPIOD_IS_OUT | GPIOD_OPEN_DRAIN));
ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN,
sandbox_gpio_get_dir_flags(dev, offset)); sandbox_gpio_get_flags(dev, offset));
ut_assertok(ops->set_flags(dev, offset, ut_assertok(ops->set_flags(dev, offset,
GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE)); GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE));
ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE,
sandbox_gpio_get_dir_flags(dev, offset)); sandbox_gpio_get_flags(dev, offset));
/* Make it an input */ /* Make it an input */
ut_assertok(ops->direction_input(dev, offset)); ut_assertok(ops->direction_input(dev, offset));
@ -176,7 +176,7 @@ static int dm_test_gpio_opendrain_opensource(struct unit_test_state *uts)
/* GPIO 0 is (GPIO_OUT|GPIO_OPEN_DRAIN) */ /* GPIO 0 is (GPIO_OUT|GPIO_OPEN_DRAIN) */
ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN,
sandbox_gpio_get_dir_flags(gpio_c, 0)); sandbox_gpio_get_flags(gpio_c, 0));
/* Set it as output high, should become an input */ /* Set it as output high, should become an input */
ut_assertok(dm_gpio_set_value(&desc_list[0], 1)); ut_assertok(dm_gpio_set_value(&desc_list[0], 1));
@ -190,7 +190,7 @@ static int dm_test_gpio_opendrain_opensource(struct unit_test_state *uts)
/* GPIO 1 is (GPIO_OUT|GPIO_OPEN_SOURCE) */ /* GPIO 1 is (GPIO_OUT|GPIO_OPEN_SOURCE) */
ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE,
sandbox_gpio_get_dir_flags(gpio_c, 1)); sandbox_gpio_get_flags(gpio_c, 1));
/* Set it as output high, should become output high */ /* Set it as output high, should become output high */
ut_assertok(dm_gpio_set_value(&desc_list[1], 1)); ut_assertok(dm_gpio_set_value(&desc_list[1], 1));
@ -204,7 +204,7 @@ static int dm_test_gpio_opendrain_opensource(struct unit_test_state *uts)
/* GPIO 6 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN) */ /* GPIO 6 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN) */
ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN,
sandbox_gpio_get_dir_flags(gpio_c, 6)); sandbox_gpio_get_flags(gpio_c, 6));
/* Set it as output high, should become output low */ /* Set it as output high, should become output low */
ut_assertok(dm_gpio_set_value(&desc_list[6], 1)); ut_assertok(dm_gpio_set_value(&desc_list[6], 1));
@ -218,7 +218,7 @@ static int dm_test_gpio_opendrain_opensource(struct unit_test_state *uts)
/* GPIO 7 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE) */ /* GPIO 7 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE) */
ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_SOURCE,
sandbox_gpio_get_dir_flags(gpio_c, 7)); sandbox_gpio_get_flags(gpio_c, 7));
/* Set it as output high, should become an input */ /* Set it as output high, should become an input */
ut_assertok(dm_gpio_set_value(&desc_list[7], 1)); ut_assertok(dm_gpio_set_value(&desc_list[7], 1));
@ -363,12 +363,12 @@ static int dm_test_gpio_phandles(struct unit_test_state *uts)
ut_assertok(gpio_free_list(dev, desc_list, 3)); ut_assertok(gpio_free_list(dev, desc_list, 3));
ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE,
sandbox_gpio_get_dir_flags(gpio_a, 1)); sandbox_gpio_get_flags(gpio_a, 1));
ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list, ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list,
ARRAY_SIZE(desc_list), 0)); ARRAY_SIZE(desc_list), 0));
/* This was set to output previously but flags resetted to 0 = INPUT */ /* This was set to output previously but flags resetted to 0 = INPUT */
ut_asserteq(0, sandbox_gpio_get_dir_flags(gpio_a, 1)); ut_asserteq(0, sandbox_gpio_get_flags(gpio_a, 1));
ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 1, NULL)); ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 1, NULL));
/* Active low should invert the input value */ /* Active low should invert the input value */