struct regulator_config *config = NULL;
        static atomic_t regulator_no = ATOMIC_INIT(-1);
        struct regulator_dev *rdev;
+       bool dangling_cfg_gpiod = false;
+       bool dangling_of_gpiod = false;
        struct device *dev;
        int ret, i;
 
-       if (regulator_desc == NULL || cfg == NULL)
+       if (cfg == NULL)
                return ERR_PTR(-EINVAL);
+       if (cfg->ena_gpiod)
+               dangling_cfg_gpiod = true;
+       if (regulator_desc == NULL) {
+               ret = -EINVAL;
+               goto rinse;
+       }
 
        dev = cfg->dev;
        WARN_ON(!dev);
 
-       if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
-               return ERR_PTR(-EINVAL);
+       if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
+               ret = -EINVAL;
+               goto rinse;
+       }
 
        if (regulator_desc->type != REGULATOR_VOLTAGE &&
-           regulator_desc->type != REGULATOR_CURRENT)
-               return ERR_PTR(-EINVAL);
+           regulator_desc->type != REGULATOR_CURRENT) {
+               ret = -EINVAL;
+               goto rinse;
+       }
 
        /* Only one of each should be implemented */
        WARN_ON(regulator_desc->ops->get_voltage &&
        /* If we're using selectors we must implement list_voltage. */
        if (regulator_desc->ops->get_voltage_sel &&
            !regulator_desc->ops->list_voltage) {
-               return ERR_PTR(-EINVAL);
+               ret = -EINVAL;
+               goto rinse;
        }
        if (regulator_desc->ops->set_voltage_sel &&
            !regulator_desc->ops->list_voltage) {
-               return ERR_PTR(-EINVAL);
+               ret = -EINVAL;
+               goto rinse;
        }
 
        rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
-       if (rdev == NULL)
-               return ERR_PTR(-ENOMEM);
+       if (rdev == NULL) {
+               ret = -ENOMEM;
+               goto rinse;
+       }
 
        /*
         * Duplicate the config so the driver could override it after
        config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
        if (config == NULL) {
                kfree(rdev);
-               return ERR_PTR(-ENOMEM);
+               ret = -ENOMEM;
+               goto rinse;
        }
 
        init_data = regulator_of_get_init_data(dev, regulator_desc, config,
                                               &rdev->dev.of_node);
+       /*
+        * We need to keep track of any GPIO descriptor coming from the
+        * device tree until we have handled it over to the core. If the
+        * config that was passed in to this function DOES NOT contain
+        * a descriptor, and the config after this call DOES contain
+        * a descriptor, we definately got one from parsing the device
+        * tree.
+        */
+       if (!cfg->ena_gpiod && config->ena_gpiod)
+               dangling_of_gpiod = true;
        if (!init_data) {
                init_data = config->init_data;
                rdev->dev.of_node = of_node_get(config->of_node);
                                 config->ena_gpio, ret);
                        goto clean;
                }
+               /* The regulator core took over the GPIO descriptor */
+               dangling_cfg_gpiod = false;
+               dangling_of_gpiod = false;
        }
 
        /* register with sysfs */
        regulator_ena_gpio_free(rdev);
        mutex_unlock(®ulator_list_mutex);
 clean:
+       if (dangling_of_gpiod)
+               gpiod_put(config->ena_gpiod);
        kfree(rdev);
        kfree(config);
+rinse:
+       if (dangling_cfg_gpiod)
+               gpiod_put(cfg->ena_gpiod);
        return ERR_PTR(ret);
 }
 EXPORT_SYMBOL_GPL(regulator_register);