浏览代码

dm: gpio: Add gpio_get_function() and friends

Add helpers to the uclass to allow finding out the pin function.

Signed-off-by: Simon Glass <sjg@chromium.org>
Simon Glass 10 年之前
父节点
当前提交
6449a506d6
共有 2 个文件被更改,包括 80 次插入1 次删除
  1. 47 0
      drivers/gpio/gpio-uclass.c
  2. 33 1
      include/asm-generic/gpio.h

+ 47 - 0
drivers/gpio/gpio-uclass.c

@@ -283,6 +283,53 @@ const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
 	return priv->bank_name;
 }
 
+static const char * const gpio_function[GPIOF_COUNT] = {
+	"input",
+	"output",
+	"unused",
+	"unknown",
+	"func",
+};
+
+int get_function(struct udevice *dev, int offset, bool skip_unused,
+		 const char **namep)
+{
+	struct gpio_dev_priv *uc_priv = dev->uclass_priv;
+	struct dm_gpio_ops *ops = gpio_get_ops(dev);
+
+	BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
+	if (!device_active(dev))
+		return -ENODEV;
+	if (offset < 0 || offset >= uc_priv->gpio_count)
+		return -EINVAL;
+	if (namep)
+		*namep = uc_priv->name[offset];
+	if (skip_unused && !uc_priv->name[offset])
+		return GPIOF_UNUSED;
+	if (ops->get_function) {
+		int ret;
+
+		ret = ops->get_function(dev, offset);
+		if (ret < 0)
+			return ret;
+		if (ret >= ARRAY_SIZE(gpio_function))
+			return -ENODATA;
+		return ret;
+	}
+
+	return GPIOF_UNKNOWN;
+}
+
+int gpio_get_function(struct udevice *dev, int offset, const char **namep)
+{
+	return get_function(dev, offset, true, namep);
+}
+
+int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
+{
+	return get_function(dev, offset, false, namep);
+}
+
 /* We need to renumber the GPIOs when any driver is probed/removed */
 static int gpio_renumber(struct udevice *removed_dev)
 {

+ 33 - 1
include/asm-generic/gpio.h

@@ -83,7 +83,7 @@ int gpio_get_value(unsigned gpio);
 int gpio_set_value(unsigned gpio, int value);
 
 /* State of a GPIO, as reported by get_function() */
-enum {
+enum gpio_func_t {
 	GPIOF_INPUT = 0,
 	GPIOF_OUTPUT,
 	GPIOF_UNUSED,		/* Not claimed */
@@ -95,6 +95,38 @@ enum {
 
 struct udevice;
 
+/**
+ * gpio_get_function() - get the current function for a GPIO pin
+ *
+ * Note this returns GPIOF_UNUSED if the GPIO is not requested.
+ *
+ * @dev:	Device to check
+ * @offset:	Offset of device GPIO to check
+ * @namep:	If non-NULL, this is set to the nane given when the GPIO
+ *		was requested, or -1 if it has not been requested
+ * @return  -ENODATA if the driver returned an unknown function,
+ * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
+ * GPIOF_UNUSED if the GPIO has not been requested. Otherwise returns the
+ * function from enum gpio_func_t.
+ */
+int gpio_get_function(struct udevice *dev, int offset, const char **namep);
+
+/**
+ * gpio_get_raw_function() - get the current raw function for a GPIO pin
+ *
+ * Note this does not return GPIOF_UNUSED - it will always return the GPIO
+ * driver's view of a pin function, even if it is not correctly set up.
+ *
+ * @dev:	Device to check
+ * @offset:	Offset of device GPIO to check
+ * @namep:	If non-NULL, this is set to the nane given when the GPIO
+ *		was requested, or -1 if it has not been requested
+ * @return  -ENODATA if the driver returned an unknown function,
+ * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
+ * Otherwise returns the function from enum gpio_func_t.
+ */
+int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep);
+
 /**
  * struct struct dm_gpio_ops - Driver model GPIO operations
  *