]> git.karo-electronics.de Git - linux-beck.git/commitdiff
regulator: core: Provide managed regulator registration
authorMark Brown <broonie@linaro.org>
Sat, 31 Aug 2013 10:58:26 +0000 (11:58 +0100)
committerMark Brown <broonie@linaro.org>
Mon, 16 Sep 2013 23:27:53 +0000 (00:27 +0100)
Many regulator drivers have a remove function that consists solely of
calling regulator_unregister() so provide a devm_regulator_register()
in order to allow this repeated code to be removed and help eliminate
error handling code.

Signed-off-by: Mark Brown <broonie@linaro.org>
Documentation/driver-model/devres.txt
drivers/regulator/core.c
include/linux/regulator/driver.h

index fcb34a5697eaa4ec2c44a46791b711d5a78f1c7b..3d9c2a766230a5d7d24164675a4bee3b4e67dd67 100644 (file)
@@ -283,6 +283,7 @@ REGULATOR
   devm_regulator_get()
   devm_regulator_put()
   devm_regulator_bulk_get()
+  devm_regulator_register()
 
 CLOCK
   devm_clk_get()
index a01b8b3b70ca2fc3adb4e81d7f96358282115ba4..5f995d281672fa4f78ef81681709cb821ba1f1fe 100644 (file)
@@ -3492,6 +3492,44 @@ clean:
 }
 EXPORT_SYMBOL_GPL(regulator_register);
 
+static void devm_rdev_release(struct device *dev, void *res)
+{
+       regulator_unregister(*(struct regulator_dev **)res);
+}
+
+/**
+ * devm_regulator_register - Resource managed regulator_register()
+ * @regulator_desc: regulator to register
+ * @config: runtime configuration for regulator
+ *
+ * Called by regulator drivers to register a regulator.  Returns a
+ * valid pointer to struct regulator_dev on success or an ERR_PTR() on
+ * error.  The regulator will automaticall be released when the device
+ * is unbound.
+ */
+struct regulator_dev *devm_regulator_register(struct device *dev,
+                                 const struct regulator_desc *regulator_desc,
+                                 const struct regulator_config *config)
+{
+       struct regulator_dev **ptr, *rdev;
+
+       ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
+                          GFP_KERNEL);
+       if (!ptr)
+               return ERR_PTR(-ENOMEM);
+
+       rdev = regulator_register(regulator_desc, config);
+       if (!IS_ERR(rdev)) {
+               *ptr = rdev;
+               devres_add(dev, ptr);
+       } else {
+               devres_free(ptr);
+       }
+
+       return rdev;
+}
+EXPORT_SYMBOL_GPL(devm_regulator_register);
+
 /**
  * regulator_unregister - unregister regulator
  * @rdev: regulator to unregister
@@ -3521,6 +3559,34 @@ void regulator_unregister(struct regulator_dev *rdev)
 }
 EXPORT_SYMBOL_GPL(regulator_unregister);
 
+static int devm_rdev_match(struct device *dev, void *res, void *data)
+{
+       struct regulator_dev **r = res;
+       if (!r || !*r) {
+               WARN_ON(!r || !*r);
+               return 0;
+       }
+       return *r == data;
+}
+
+/**
+ * devm_regulator_unregister - Resource managed regulator_unregister()
+ * @regulator: regulator to free
+ *
+ * Unregister a regulator registered with devm_regulator_register().
+ * Normally this function will not need to be called and the resource
+ * management code will ensure that the resource is freed.
+ */
+void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev)
+{
+       int rc;
+
+       rc = devres_release(dev, devm_rdev_release, devm_rdev_match, rdev);
+       if (rc != 0)
+               WARN_ON(rc);
+}
+EXPORT_SYMBOL_GPL(devm_regulator_unregister);
+
 /**
  * regulator_suspend_prepare - prepare regulators for system wide suspend
  * @state: system suspend state
index 67e13aa5a4781d2c8fdfa8fc902d2bbc90f2a3a0..8474c7f88745fc79f2f331aa10efb0573abb85e7 100644 (file)
@@ -334,7 +334,12 @@ struct regulator_dev {
 struct regulator_dev *
 regulator_register(const struct regulator_desc *regulator_desc,
                   const struct regulator_config *config);
+struct regulator_dev *
+devm_regulator_register(struct device *dev,
+                       const struct regulator_desc *regulator_desc,
+                       const struct regulator_config *config);
 void regulator_unregister(struct regulator_dev *rdev);
+void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev);
 
 int regulator_notifier_call_chain(struct regulator_dev *rdev,
                                  unsigned long event, void *data);