Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

regulator: Add generic DT parsing for regulators

Looking up init data for regulators found on chips is a common operation
that can be handled in a generic way. The new helper function introduced
by this patch looks up the children of a given node by names specified
in a match table and fills that match table with information parsed from
the DT.

This is based on work by Rhyland Klein <rklein@nvidia.com>.

Signed-off-by: Thierry Reding <thierry.reding@avionic-design.de>
Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>

authored by

Thierry Reding and committed by
Mark Brown
1c8fa58f 82caa978

+65
+47
drivers/regulator/of_regulator.c
··· 14 14 #include <linux/slab.h> 15 15 #include <linux/of.h> 16 16 #include <linux/regulator/machine.h> 17 + #include <linux/regulator/of_regulator.h> 17 18 18 19 static void of_get_regulation_constraints(struct device_node *np, 19 20 struct regulator_init_data **init_data) ··· 86 85 return init_data; 87 86 } 88 87 EXPORT_SYMBOL_GPL(of_get_regulator_init_data); 88 + 89 + /** 90 + * of_regulator_match - extract regulator init data 91 + * @dev: device requesting the data 92 + * @node: parent device node of the regulators 93 + * @matches: match table for the regulators 94 + * @num_matches: number of entries in match table 95 + * 96 + * This function uses a match table specified by the regulator driver and 97 + * looks up the corresponding init data in the device tree. Note that the 98 + * match table is modified in place. 99 + * 100 + * Returns the number of matches found or a negative error code on failure. 101 + */ 102 + int of_regulator_match(struct device *dev, struct device_node *node, 103 + struct of_regulator_match *matches, 104 + unsigned int num_matches) 105 + { 106 + unsigned int count = 0; 107 + unsigned int i; 108 + 109 + if (!dev || !node) 110 + return -EINVAL; 111 + 112 + for (i = 0; i < num_matches; i++) { 113 + struct of_regulator_match *match = &matches[i]; 114 + struct device_node *child; 115 + 116 + child = of_find_node_by_name(node, match->name); 117 + if (!child) 118 + continue; 119 + 120 + match->init_data = of_get_regulator_init_data(dev, child); 121 + if (!match->init_data) { 122 + dev_err(dev, "failed to parse DT for regulator %s\n", 123 + child->name); 124 + return -EINVAL; 125 + } 126 + 127 + match->of_node = child; 128 + count++; 129 + } 130 + 131 + return count; 132 + } 133 + EXPORT_SYMBOL_GPL(of_regulator_match);
+18
include/linux/regulator/of_regulator.h
··· 6 6 #ifndef __LINUX_OF_REG_H 7 7 #define __LINUX_OF_REG_H 8 8 9 + struct of_regulator_match { 10 + const char *name; 11 + void *driver_data; 12 + struct regulator_init_data *init_data; 13 + struct device_node *of_node; 14 + }; 15 + 9 16 #if defined(CONFIG_OF) 10 17 extern struct regulator_init_data 11 18 *of_get_regulator_init_data(struct device *dev, 12 19 struct device_node *node); 20 + extern int of_regulator_match(struct device *dev, struct device_node *node, 21 + struct of_regulator_match *matches, 22 + unsigned int num_matches); 13 23 #else 14 24 static inline struct regulator_init_data 15 25 *of_get_regulator_init_data(struct device *dev, 16 26 struct device_node *node) 17 27 { 18 28 return NULL; 29 + } 30 + 31 + static inline int of_regulator_match(struct device *dev, 32 + struct device_node *node, 33 + struct of_regulator_match *matches, 34 + unsigned int num_matches) 35 + { 36 + return 0; 19 37 } 20 38 #endif /* CONFIG_OF */ 21 39