[U-Boot] [PATCH v3 5/5] test: dm: Add gpio get_alt_function ops test

Patrice Chotard patrice.chotard at st.com
Mon Sep 10 16:11:10 UTC 2018


In order to test get_alt_function ops, add
sandbox_gpio_set_function() and sandbox_gpio_set_alternate_function()
to be able to configure a pin not as a GPIO and to select the
alternate function index.

Signed-off-by: Patrice Chotard <patrice.chotard at st.com>
Reviewed-by: Simon Glass <sjg at chromium.org>
---

Changes in v3:
  - Rename get_alternate_function to get_alt_function

Changes in v2:
  - Rename get_function_number to get_alternate_function
    in test/dm/gpio.c, drivers/gpio/sandbox.c and in
    arch/sandbox/include/asm/gpio.h

 arch/sandbox/include/asm/gpio.h | 24 ++++++++++++++++++++++++
 drivers/gpio/sandbox.c          | 29 ++++++++++++++++++++++++++++-
 test/dm/gpio.c                  | 13 +++++++++++++
 3 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/arch/sandbox/include/asm/gpio.h b/arch/sandbox/include/asm/gpio.h
index de8ac37f4262..217f6435b8bd 100644
--- a/arch/sandbox/include/asm/gpio.h
+++ b/arch/sandbox/include/asm/gpio.h
@@ -82,4 +82,28 @@ int sandbox_gpio_get_direction(struct udevice *dev, unsigned int offset);
 int sandbox_gpio_set_direction(struct udevice *dev, unsigned int offset,
 			       int output);
 
+/**
+ * Set the simulated usage of a pin, as a GPIO or not (used only in sandbox
+ * test code)
+ *
+ * @param dev		device to use
+ * @param offset	GPIO offset within bank
+ * @param value		0 to set as GPIO, 1 to set as not a GPIO
+ * @return -1 on error, 0 if ok
+ */
+int sandbox_gpio_set_function(struct udevice *dev, unsigned int offset,
+			      int value);
+
+/**
+ * Set the simulated alt function of a pin when pin is not used as a GPIO
+ * (used only in sandbox test code)
+ *
+ * @param dev		device to use
+ * @param offset	GPIO offset within bank
+ * @param value		pin alternate function number
+ * @return -1 on error, 0 if ok
+ */
+int sandbox_gpio_set_alt_function(struct udevice *dev,
+				  unsigned int offset,
+				  int value);
 #endif
diff --git a/drivers/gpio/sandbox.c b/drivers/gpio/sandbox.c
index e7bed17d0db0..55b7a2579db6 100644
--- a/drivers/gpio/sandbox.c
+++ b/drivers/gpio/sandbox.c
@@ -15,6 +15,8 @@
 #define SANDBOX_GPIO_OUTPUT	BIT(0)	/* Currently set as an output */
 #define SANDBOX_GPIO_HIGH	BIT(1)	/* Currently set high */
 #define SANDBOX_GPIO_ODR	BIT(2)	/* Currently set to open drain mode */
+#define SANDBOX_GPIO_FUNC	BIT(3)	/* Currently set as not used as GPIO */
+#define SANDBOX_GPIO_FUNC_NB	BIT(4)	/* Currently set as function number 1 */
 
 struct gpio_state {
 	const char *label;	/* label given by requester */
@@ -90,6 +92,19 @@ int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
 	return set_gpio_flag(dev, offset, SANDBOX_GPIO_OUTPUT, output);
 }
 
+int sandbox_gpio_set_function(struct udevice *dev, unsigned int offset,
+			      int value)
+{
+	return set_gpio_flag(dev, offset, SANDBOX_GPIO_FUNC, value);
+}
+
+int sandbox_gpio_set_alt_function(struct udevice *dev,
+				  unsigned int offset,
+				  int value)
+{
+	return set_gpio_flag(dev, offset, SANDBOX_GPIO_FUNC_NB, value);
+}
+
 /*
  * These functions implement the public interface within U-Boot
  */
@@ -158,11 +173,22 @@ static int sb_gpio_set_open_drain(struct udevice *dev, unsigned offset, int valu
 
 static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
 {
-	if (get_gpio_flag(dev, offset, SDBX_GPIO_OUTPUT))
+	if (get_gpio_flag(dev, offset, SANDBOX_GPIO_FUNC))
+		return GPIOF_FUNC;
+
+	if (get_gpio_flag(dev, offset, SANDBOX_GPIO_OUTPUT))
 		return GPIOF_OUTPUT;
 	return GPIOF_INPUT;
 }
 
+static int sb_gpio_get_alt_function(struct udevice *dev,
+				    unsigned int offset)
+{
+	debug("%s: offset:%u\n", __func__, offset);
+
+	return get_gpio_flag(dev, offset, SANDBOX_GPIO_FUNC_NB);
+}
+
 static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
 			 struct ofnode_phandle_args *args)
 {
@@ -189,6 +215,7 @@ static const struct dm_gpio_ops gpio_sandbox_ops = {
 	.get_open_drain		= sb_gpio_get_open_drain,
 	.set_open_drain		= sb_gpio_set_open_drain,
 	.get_function		= sb_gpio_get_function,
+	.get_alt_function	= sb_gpio_get_alt_function,
 	.xlate			= sb_gpio_xlate,
 };
 
diff --git a/test/dm/gpio.c b/test/dm/gpio.c
index bb4b20cea938..302342c2e0ce 100644
--- a/test/dm/gpio.c
+++ b/test/dm/gpio.c
@@ -92,6 +92,19 @@ static int dm_test_gpio(struct unit_test_state *uts)
 	ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
 	ut_asserteq_str("b4: input: 0 [ ]", buf);
 
+	/*
+	 * Make it not used as GPIO, select function
+	 * (by default function number 0 is active)
+	 */
+	sandbox_gpio_set_function(dev, offset, 1);
+	ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
+	ut_asserteq_str("b4: func: 0", buf);
+
+	/* select function number 1 */
+	sandbox_gpio_set_alt_function(dev, offset, 1);
+	ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
+	ut_asserteq_str("b4: func: 1", buf);
+
 	/* Check the 'a' bank also */
 	ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio));
 	ut_asserteq_str(dev->name, "base-gpios");
-- 
1.9.1



More information about the U-Boot mailing list