regulator: core: Use devres for releasing of_regulator_match of_nodes

Rather than requiring individual drivers to put the of_nodes returned
from of_regulator_match use devres to put them. This also has the
benefit it makes the life-time of the of_nodes match the lifetime of
the init data also contained in the of_regulator_match structure, which
seems more consistent.

Signed-off-by: Charles Keepax <ckeepax@opensource.wolfsonmicro.com>
Signed-off-by: Mark Brown <broonie@linaro.org>
diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regulator.c
index 188e0cb..4672cd2f 100644
--- a/drivers/regulator/of_regulator.c
+++ b/drivers/regulator/of_regulator.c
@@ -106,6 +106,20 @@
 }
 EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
 
+struct devm_of_regulator_matches {
+	struct of_regulator_match *matches;
+	unsigned int num_matches;
+};
+
+static void devm_of_regulator_put_matches(struct device *dev, void *res)
+{
+	struct devm_of_regulator_matches *devm_matches = res;
+	int i;
+
+	for (i = 0; i < devm_matches->num_matches; i++)
+		of_node_put(devm_matches->matches[i].of_node);
+}
+
 /**
  * of_regulator_match - extract multiple regulator init data from device tree.
  * @dev: device requesting the data
@@ -132,10 +146,22 @@
 	unsigned int i;
 	const char *name;
 	struct device_node *child;
+	struct devm_of_regulator_matches *devm_matches;
 
 	if (!dev || !node)
 		return -EINVAL;
 
+	devm_matches = devres_alloc(devm_of_regulator_put_matches,
+				    sizeof(struct devm_of_regulator_matches),
+				    GFP_KERNEL);
+	if (!devm_matches)
+		return -ENOMEM;
+
+	devm_matches->matches = matches;
+	devm_matches->num_matches = num_matches;
+
+	devres_add(dev, devm_matches);
+
 	for (i = 0; i < num_matches; i++) {
 		struct of_regulator_match *match = &matches[i];
 		match->init_data = NULL;
@@ -172,24 +198,3 @@
 	return count;
 }
 EXPORT_SYMBOL_GPL(of_regulator_match);
-
-/**
- * of_regulator_put_match - put the of_node references from an
- *                          of_regulator_match structure
- * @matches: match table for the regulators
- * @num_matches: number of entries in match table
- *
- * This function goes through a match table and calls of_node_put on each
- * of_node.
- */
-int of_regulator_put_match(struct of_regulator_match *matches,
-			   unsigned int num_matches)
-{
-	int i;
-
-	for (i = 0; i < num_matches; i++)
-		of_node_put(matches[i].of_node);
-
-	return 0;
-}
-EXPORT_SYMBOL_GPL(of_regulator_put_match);