gpio: remove the open_drain API and ops
[oweals/u-boot.git] / drivers / gpio / gpio-uclass.c
index 308d0863ada30a1ca1dc83004e0bcd003b6eac75..2515df4e7c7dfa21fbd4f9552e028a0a89a2e8ba 100644 (file)
@@ -144,7 +144,7 @@ static int gpio_find_and_xlate(struct gpio_desc *desc,
                return gpio_xlate_offs_flags(desc->dev, desc, args);
 }
 
-#if defined(CONFIG_DM_GPIO_HOG)
+#if defined(CONFIG_GPIO_HOG)
 
 struct gpio_hog_priv {
        struct gpio_desc gpiod;
@@ -181,9 +181,8 @@ static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
                return ret;
        }
        nodename = dev_read_string(dev, "line-name");
-       if (!nodename)
-               nodename = dev_read_name(dev);
-       device_set_name(dev, nodename);
+       if (nodename)
+               device_set_name(dev, nodename);
 
        return 0;
 }
@@ -202,9 +201,15 @@ static int gpio_hog_probe(struct udevice *dev)
                      dev->name);
                return ret;
        }
-       dm_gpio_set_dir(&priv->gpiod);
-       if (plat->gpiod_flags == GPIOD_IS_OUT)
-               dm_gpio_set_value(&priv->gpiod, plat->value);
+
+       if (plat->gpiod_flags == GPIOD_IS_OUT) {
+               ret = dm_gpio_set_value(&priv->gpiod, plat->value);
+               if (ret < 0) {
+                       debug("%s: node %s could not set gpio.\n", __func__,
+                             dev->name);
+                       return ret;
+               }
+       }
 
        return 0;
 }
@@ -213,32 +218,38 @@ int gpio_hog_probe_all(void)
 {
        struct udevice *dev;
        int ret;
+       int retval = 0;
 
        for (uclass_first_device(UCLASS_NOP, &dev);
             dev;
             uclass_find_next_device(&dev)) {
                if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
                        ret = device_probe(dev);
-                       if (ret)
-                               return ret;
+                       if (ret) {
+                               printf("Failed to probe device %s err: %d\n",
+                                      dev->name, ret);
+                               retval = ret;
+                       }
                }
        }
 
-       return 0;
+       return retval;
 }
 
-struct gpio_desc *gpio_hog_lookup_name(const char *name)
+int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
 {
        struct udevice *dev;
 
+       *desc = NULL;
        gpio_hog_probe_all();
        if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
                struct gpio_hog_priv *priv = dev_get_priv(dev);
 
-               return &priv->gpiod;
+               *desc = &priv->gpiod;
+               return 0;
        }
 
-       return NULL;
+       return -ENODEV;
 }
 
 U_BOOT_DRIVER(gpio_hog) = {
@@ -250,9 +261,9 @@ U_BOOT_DRIVER(gpio_hog) = {
        .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
 };
 #else
-struct gpio_desc *gpio_hog_lookup_name(const char *name)
+int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
 {
-       return NULL;
+       return 0;
 }
 #endif
 
@@ -283,7 +294,7 @@ int dm_gpio_request(struct gpio_desc *desc, const char *label)
 
 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
 {
-#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
+#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
        va_list args;
        char buf[40];
 
@@ -332,7 +343,7 @@ int gpio_request(unsigned gpio, const char *label)
  */
 int gpio_requestf(unsigned gpio, const char *fmt, ...)
 {
-#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
+#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
        va_list args;
        char buf[40];
 
@@ -353,8 +364,8 @@ int _dm_gpio_free(struct udevice *dev, uint offset)
        uc_priv = dev_get_uclass_priv(dev);
        if (!uc_priv->name[offset])
                return -ENXIO;
-       if (gpio_get_ops(dev)->free) {
-               ret = gpio_get_ops(dev)->free(dev, offset);
+       if (gpio_get_ops(dev)->rfree) {
+               ret = gpio_get_ops(dev)->rfree(dev, offset);
                if (ret)
                        return ret;
        }
@@ -480,38 +491,6 @@ int dm_gpio_set_value(const struct gpio_desc *desc, int value)
        return 0;
 }
 
-int dm_gpio_get_open_drain(struct gpio_desc *desc)
-{
-       struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
-       int ret;
-
-       ret = check_reserved(desc, "get_open_drain");
-       if (ret)
-               return ret;
-
-       if (ops->set_open_drain)
-               return ops->get_open_drain(desc->dev, desc->offset);
-       else
-               return -ENOSYS;
-}
-
-int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
-{
-       struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
-       int ret;
-
-       ret = check_reserved(desc, "set_open_drain");
-       if (ret)
-               return ret;
-
-       if (ops->set_open_drain)
-               ret = ops->set_open_drain(desc->dev, desc->offset, value);
-       else
-               return 0; /* feature not supported -> ignore setting */
-
-       return ret;
-}
-
 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
 {
        struct udevice *dev = desc->dev;
@@ -755,13 +734,45 @@ int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
        return vector;
 }
 
+/**
+ * gpio_request_tail: common work for requesting a gpio.
+ *
+ * ret:                return value from previous work in function which calls
+ *             this function.
+ *             This seems bogus (why calling this function instead not
+ *             calling it and end caller function instead?).
+ *             Because on error in caller function we want to set some
+ *             default values in gpio desc and have a common error
+ *             debug message, which provides this function.
+ * nodename:   Name of node for which gpio gets requested
+ *             used for gpio label name.
+ * args:       pointer to output arguments structure
+ * list_name:  Name of GPIO list
+ *             used for gpio label name.
+ * index:      gpio index in gpio list
+ *             used for gpio label name.
+ * desc:       pointer to gpio descriptor, filled from this
+ *             function.
+ * flags:      gpio flags to use.
+ * add_index:  should index added to gpio label name
+ * gpio_dev:   pointer to gpio device from which the gpio
+ *             will be requested. If NULL try to get the
+ *             gpio device with uclass_get_device_by_ofnode()
+ *
+ * return:     In error case this function sets default values in
+ *             gpio descriptor, also emmits a debug message.
+ *             On success it returns 0 else the error code from
+ *             function calls, or the error code passed through
+ *             ret to this function.
+ *
+ */
 static int gpio_request_tail(int ret, const char *nodename,
                             struct ofnode_phandle_args *args,
                             const char *list_name, int index,
                             struct gpio_desc *desc, int flags,
-                            bool add_index, struct udevice *dev)
+                            bool add_index, struct udevice *gpio_dev)
 {
-       desc->dev = dev;
+       desc->dev = gpio_dev;
        desc->offset = 0;
        desc->flags = 0;
        if (ret)
@@ -771,7 +782,8 @@ static int gpio_request_tail(int ret, const char *nodename,
                ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
                                                  &desc->dev);
                if (ret) {
-                       debug("%s: uclass_get_device_by_ofnode failed\n", __func__);
+                       debug("%s: uclass_get_device_by_ofnode failed\n",
+                             __func__);
                        goto err;
                }
        }
@@ -989,10 +1001,8 @@ int gpio_dev_request_index(struct udevice *dev, const char *nodename,
 
 static int gpio_post_bind(struct udevice *dev)
 {
-#if defined(CONFIG_DM_GPIO_HOG)
        struct udevice *child;
        ofnode node;
-#endif
 
 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
        struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
@@ -1001,8 +1011,8 @@ static int gpio_post_bind(struct udevice *dev)
        if (!reloc_done) {
                if (ops->request)
                        ops->request += gd->reloc_off;
-               if (ops->free)
-                       ops->free += gd->reloc_off;
+               if (ops->rfree)
+                       ops->rfree += gd->reloc_off;
                if (ops->direction_input)
                        ops->direction_input += gd->reloc_off;
                if (ops->direction_output)
@@ -1011,10 +1021,6 @@ static int gpio_post_bind(struct udevice *dev)
                        ops->get_value += gd->reloc_off;
                if (ops->set_value)
                        ops->set_value += gd->reloc_off;
-               if (ops->get_open_drain)
-                       ops->get_open_drain += gd->reloc_off;
-               if (ops->set_open_drain)
-                       ops->set_open_drain += gd->reloc_off;
                if (ops->get_function)
                        ops->get_function += gd->reloc_off;
                if (ops->xlate)
@@ -1024,16 +1030,21 @@ static int gpio_post_bind(struct udevice *dev)
        }
 #endif
 
-#if defined(CONFIG_DM_GPIO_HOG)
-       dev_for_each_subnode(node, dev) {
-               if (ofnode_read_bool(node, "gpio-hog")) {
-                       const char *name = ofnode_get_name(node);
-
-                       device_bind_driver_to_node(dev, "gpio_hog", name,
-                                                  node, &child);
+       if (IS_ENABLED(CONFIG_GPIO_HOG)) {
+               dev_for_each_subnode(node, dev) {
+                       if (ofnode_read_bool(node, "gpio-hog")) {
+                               const char *name = ofnode_get_name(node);
+                               int ret;
+
+                               ret = device_bind_driver_to_node(dev,
+                                                                "gpio_hog",
+                                                                name, node,
+                                                                &child);
+                               if (ret)
+                                       return ret;
+                       }
                }
        }
-#endif
        return 0;
 }