]> git.sur5r.net Git - u-boot/blobdiff - drivers/gpio/omap_gpio.c
gpio: omap_gpio: Convert to auto-alloc feature when DT is supported
[u-boot] / drivers / gpio / omap_gpio.c
index b423e34ca4bfc4ad3d2a6e0aa42eebae2b8e4e90..651f6994e41a6a609add5c9eb82964212ae2b576 100644 (file)
@@ -1,9 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (c) 2009 Wind River Systems, Inc.
  * Tom Rix <Tom.Rix@windriver.com>
  *
- * SPDX-License-Identifier:    GPL-2.0
- *
  * This work is derived from the linux 2.6.27 kernel source
  * To fetch, use the kernel repository
  * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
@@ -289,17 +288,24 @@ static int omap_gpio_probe(struct udevice *dev)
        struct gpio_bank *bank = dev_get_priv(dev);
        struct omap_gpio_platdata *plat = dev_get_platdata(dev);
        struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
+       int banknum;
+       char name[18], *str;
 
-       uc_priv->bank_name = plat->port_name;
+       banknum = plat->bank_index;
+       sprintf(name, "GPIO%d_", banknum + 1);
+       str = strdup(name);
+       if (!str)
+               return -ENOMEM;
+       uc_priv->bank_name = str;
        uc_priv->gpio_count = GPIO_PER_BANK;
        bank->base = (void *)plat->base;
-
        return 0;
 }
 
+#if !CONFIG_IS_ENABLED(OF_CONTROL)
 static int omap_gpio_bind(struct udevice *dev)
 {
-       struct omap_gpio_platdata *plat = dev->platdata;
+       struct omap_gpio_platdata *plat = dev_get_platdata(dev);
        fdt_addr_t base_addr;
 
        if (plat)
@@ -307,13 +313,17 @@ static int omap_gpio_bind(struct udevice *dev)
 
        base_addr = devfdt_get_addr(dev);
        if (base_addr == FDT_ADDR_T_NONE)
-               return -ENODEV;
+               return -EINVAL;
 
        /*
        * TODO:
        * When every board is converted to driver model and DT is
        * supported, this can be done by auto-alloc feature, but
        * not using calloc to alloc memory for platdata.
+       *
+       * For example am33xx_gpio uses platform data rather than device tree.
+       *
+       * NOTE: DO NOT COPY this code if you are using device tree.
        */
        plat = calloc(1, sizeof(*plat));
        if (!plat)
@@ -325,6 +335,7 @@ static int omap_gpio_bind(struct udevice *dev)
 
        return 0;
 }
+#endif
 
 static const struct udevice_id omap_gpio_ids[] = {
        { .compatible = "ti,omap3-gpio" },
@@ -333,14 +344,36 @@ static const struct udevice_id omap_gpio_ids[] = {
        { }
 };
 
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+static int omap_gpio_ofdata_to_platdata(struct udevice *dev)
+{
+       struct omap_gpio_platdata *plat = dev_get_platdata(dev);
+       fdt_addr_t addr;
+
+       addr = devfdt_get_addr(dev);
+       if (addr == FDT_ADDR_T_NONE)
+               return -EINVAL;
+
+       plat->base = addr;
+       return 0;
+}
+#endif
+
 U_BOOT_DRIVER(gpio_omap) = {
        .name   = "gpio_omap",
        .id     = UCLASS_GPIO,
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+       .ofdata_to_platdata = of_match_ptr(omap_gpio_ofdata_to_platdata),
+       .bind   = dm_scan_fdt_dev,
+       .platdata_auto_alloc_size = sizeof(struct omap_gpio_platdata),
+#else
+       .bind   = omap_gpio_bind,
+#endif
        .ops    = &gpio_omap_ops,
        .of_match = omap_gpio_ids,
-       .bind   = omap_gpio_bind,
        .probe  = omap_gpio_probe,
        .priv_auto_alloc_size = sizeof(struct gpio_bank),
+       .flags = DM_FLAG_PRE_RELOC,
 };
 
 #endif /* CONFIG_DM_GPIO */