phy: Fix possible NULL pointer deference
[oweals/u-boot.git] / drivers / gpio / dwapb_gpio.c
index 0f6574d5da1554e436efc6b9edd6e27ae4d910b4..e5e35181940ce2a41ad6458dea74c7b668e26503 100644 (file)
@@ -6,18 +6,20 @@
  */
 
 #include <common.h>
+#include <log.h>
 #include <malloc.h>
 #include <asm/arch/gpio.h>
 #include <asm/gpio.h>
 #include <asm/io.h>
 #include <dm.h>
 #include <dm/device-internal.h>
+#include <dm/device_compat.h>
+#include <dm/devres.h>
 #include <dm/lists.h>
 #include <dm/root.h>
 #include <errno.h>
 #include <reset.h>
-
-DECLARE_GLOBAL_DATA_PTR;
+#include <linux/bitops.h>
 
 #define GPIO_SWPORT_DR(p)      (0x00 + (p) * 0xc)
 #define GPIO_SWPORT_DDR(p)     (0x04 + (p) * 0xc)
@@ -150,10 +152,10 @@ static int gpio_dwapb_probe(struct udevice *dev)
 static int gpio_dwapb_bind(struct udevice *dev)
 {
        struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
-       const void *blob = gd->fdt_blob;
        struct udevice *subdev;
        fdt_addr_t base;
-       int ret, node, bank = 0;
+       int ret, bank = 0;
+       ofnode node;
 
        /* If this is a child device, there is nothing to do here */
        if (plat)
@@ -165,39 +167,37 @@ static int gpio_dwapb_bind(struct udevice *dev)
                return -ENXIO;
        }
 
-       for (node = fdt_first_subnode(blob, dev_of_offset(dev));
-            node > 0;
-            node = fdt_next_subnode(blob, node)) {
-               if (!fdtdec_get_bool(blob, node, "gpio-controller"))
+       for (node = dev_read_first_subnode(dev); ofnode_valid(node);
+            node = dev_read_next_subnode(node)) {
+               if (!ofnode_read_bool(node, "gpio-controller"))
                        continue;
 
-               plat = NULL;
-               plat = calloc(1, sizeof(*plat));
+               plat = devm_kcalloc(dev, 1, sizeof(*plat), GFP_KERNEL);
                if (!plat)
                        return -ENOMEM;
 
                plat->base = base;
                plat->bank = bank;
-               plat->pins = fdtdec_get_int(blob, node, "snps,nr-gpios", 0);
-               plat->name = fdt_stringlist_get(blob, node, "bank-name", 0,
-                                               NULL);
-               if (ret)
-                       goto err;
-
-               ret = device_bind(dev, dev->driver, plat->name,
-                                 plat, -1, &subdev);
+               plat->pins = ofnode_read_u32_default(node, "snps,nr-gpios", 0);
+
+               if (ofnode_read_string_index(node, "bank-name", 0,
+                                            &plat->name)) {
+                       /*
+                        * Fall back to node name. This means accessing pins
+                        * via bank name won't work.
+                        */
+                       plat->name = ofnode_get_name(node);
+               }
+
+               ret = device_bind_ofnode(dev, dev->driver, plat->name,
+                                        plat, node, &subdev);
                if (ret)
-                       goto err;
+                       return ret;
 
-               dev_set_of_offset(subdev, node);
                bank++;
        }
 
        return 0;
-
-err:
-       free(plat);
-       return ret;
 }
 
 static int gpio_dwapb_remove(struct udevice *dev)