2 * OF helpers for regulator framework
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Rajendra Nayak <rnayak@ti.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/slab.h>
16 #include <linux/regulator/machine.h>
17 #include <linux/regulator/of_regulator.h>
19 static void set_regulator_state_constraints(struct device_node *np,
20 struct regulator_state *state)
22 of_property_read_u32(np, "regulator-state-uv", &state->uV);
23 of_property_read_u32(np, "regulator-state-mode", &state->mode);
24 state->enabled = of_property_read_bool(np, "regulator-state-enabled");
25 state->disabled = of_property_read_bool(np, "regulator-state-disabled");
28 static void of_get_regulation_constraints(struct device_node *np,
29 struct regulator_init_data **init_data)
31 const __be32 *min_uV, *max_uV, *uV_offset;
32 const __be32 *min_uA, *max_uA, *ramp_delay;
33 struct device_node *state;
34 struct regulation_constraints *constraints = &(*init_data)->constraints;
36 constraints->name = of_get_property(np, "regulator-name", NULL);
38 min_uV = of_get_property(np, "regulator-min-microvolt", NULL);
40 constraints->min_uV = be32_to_cpu(*min_uV);
41 max_uV = of_get_property(np, "regulator-max-microvolt", NULL);
43 constraints->max_uV = be32_to_cpu(*max_uV);
45 /* Voltage change possible? */
46 if (constraints->min_uV != constraints->max_uV)
47 constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
48 /* Only one voltage? Then make sure it's set. */
49 if (min_uV && max_uV && constraints->min_uV == constraints->max_uV)
50 constraints->apply_uV = true;
52 uV_offset = of_get_property(np, "regulator-microvolt-offset", NULL);
54 constraints->uV_offset = be32_to_cpu(*uV_offset);
55 min_uA = of_get_property(np, "regulator-min-microamp", NULL);
57 constraints->min_uA = be32_to_cpu(*min_uA);
58 max_uA = of_get_property(np, "regulator-max-microamp", NULL);
60 constraints->max_uA = be32_to_cpu(*max_uA);
62 /* Current change possible? */
63 if (constraints->min_uA != constraints->max_uA)
64 constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
66 if (of_find_property(np, "regulator-boot-on", NULL))
67 constraints->boot_on = true;
69 if (of_find_property(np, "regulator-always-on", NULL))
70 constraints->always_on = true;
71 else /* status change should be possible if not always on. */
72 constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
74 ramp_delay = of_get_property(np, "regulator-ramp-delay", NULL);
76 constraints->ramp_delay = be32_to_cpu(*ramp_delay);
78 of_property_read_u32(np, "regulator-valid-modes-mask",
79 &constraints->valid_modes_mask);
80 if (constraints->valid_modes_mask)
81 constraints->valid_ops_mask |= REGULATOR_CHANGE_MODE;
83 of_property_read_u32(np, "regulator-input-uv",
84 &constraints->input_uV);
85 of_property_read_u32(np, "regulator-initial-mode",
86 &constraints->initial_mode);
87 of_property_read_u32(np, "regulator-initial-state",
88 &constraints->initial_state);
90 /* regulator state during low power system states */
91 state = of_find_node_by_name(np, "regulator-state-mem");
93 set_regulator_state_constraints(state,
94 &constraints->state_mem);
96 state = of_find_node_by_name(np, "regulator-state-disk");
98 set_regulator_state_constraints(state,
99 &constraints->state_disk);
101 state = of_find_node_by_name(np, "regulator-state-standby");
103 set_regulator_state_constraints(state,
104 &constraints->state_standby);
110 * of_get_regulator_init_data - extract regulator_init_data structure info
111 * @dev: device requesting for regulator_init_data
113 * Populates regulator_init_data structure by extracting data from device
114 * tree node, returns a pointer to the populated struture or NULL if memory
117 struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
118 struct device_node *node)
120 struct regulator_init_data *init_data;
125 init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
127 return NULL; /* Out of memory? */
129 of_get_regulation_constraints(node, &init_data);
132 EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
135 * of_regulator_match - extract multiple regulator init data from device tree.
136 * @dev: device requesting the data
137 * @node: parent device node of the regulators
138 * @matches: match table for the regulators
139 * @num_matches: number of entries in match table
141 * This function uses a match table specified by the regulator driver to
142 * parse regulator init data from the device tree. @node is expected to
143 * contain a set of child nodes, each providing the init data for one
144 * regulator. The data parsed from a child node will be matched to a regulator
145 * based on either the deprecated property regulator-compatible if present,
146 * or otherwise the child node's name. Note that the match table is modified
149 * Returns the number of matches found or a negative error code on failure.
151 int of_regulator_match(struct device *dev, struct device_node *node,
152 struct of_regulator_match *matches,
153 unsigned int num_matches)
155 unsigned int count = 0;
158 struct device_node *child;
163 for (i = 0; i < num_matches; i++) {
164 struct of_regulator_match *match = &matches[i];
165 match->init_data = NULL;
166 match->of_node = NULL;
169 for_each_child_of_node(node, child) {
170 name = of_get_property(child,
171 "regulator-compatible", NULL);
174 for (i = 0; i < num_matches; i++) {
175 struct of_regulator_match *match = &matches[i];
179 if (strcmp(match->name, name))
183 of_get_regulator_init_data(dev, child);
184 if (!match->init_data) {
186 "failed to parse DT for regulator %s\n",
190 match->of_node = child;
198 EXPORT_SYMBOL_GPL(of_regulator_match);