1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OF helpers for regulator framework
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Rajendra Nayak <rnayak@ti.com>
9 #include <linux/module.h>
10 #include <linux/slab.h>
12 #include <linux/regulator/machine.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/regulator/of_regulator.h>
18 static const char *const regulator_states[PM_SUSPEND_MAX + 1] = {
19 [PM_SUSPEND_STANDBY] = "regulator-state-standby",
20 [PM_SUSPEND_MEM] = "regulator-state-mem",
21 [PM_SUSPEND_MAX] = "regulator-state-disk",
24 static void of_get_regulation_constraints(struct device_node *np,
25 struct regulator_init_data **init_data,
26 const struct regulator_desc *desc)
28 struct regulation_constraints *constraints = &(*init_data)->constraints;
29 struct regulator_state *suspend_state;
30 struct device_node *suspend_np;
35 constraints->name = of_get_property(np, "regulator-name", NULL);
37 if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
38 constraints->min_uV = pval;
40 if (!of_property_read_u32(np, "regulator-max-microvolt", &pval))
41 constraints->max_uV = pval;
43 /* Voltage change possible? */
44 if (constraints->min_uV != constraints->max_uV)
45 constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
47 /* Do we have a voltage range, if so try to apply it? */
48 if (constraints->min_uV && constraints->max_uV)
49 constraints->apply_uV = true;
51 if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval))
52 constraints->uV_offset = pval;
53 if (!of_property_read_u32(np, "regulator-min-microamp", &pval))
54 constraints->min_uA = pval;
55 if (!of_property_read_u32(np, "regulator-max-microamp", &pval))
56 constraints->max_uA = pval;
58 if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
60 constraints->ilim_uA = pval;
62 /* Current change possible? */
63 if (constraints->min_uA != constraints->max_uA)
64 constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
66 constraints->boot_on = of_property_read_bool(np, "regulator-boot-on");
67 constraints->always_on = of_property_read_bool(np, "regulator-always-on");
68 if (!constraints->always_on) /* status change should be possible. */
69 constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
71 constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
73 if (of_property_read_bool(np, "regulator-allow-bypass"))
74 constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
76 if (of_property_read_bool(np, "regulator-allow-set-load"))
77 constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
79 ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
82 constraints->ramp_delay = pval;
84 constraints->ramp_disable = true;
87 ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
89 constraints->settling_time = pval;
91 ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval);
93 constraints->settling_time_up = pval;
94 if (constraints->settling_time_up && constraints->settling_time) {
95 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
97 constraints->settling_time_up = 0;
100 ret = of_property_read_u32(np, "regulator-settling-time-down-us",
103 constraints->settling_time_down = pval;
104 if (constraints->settling_time_down && constraints->settling_time) {
105 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
107 constraints->settling_time_down = 0;
110 ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
112 constraints->enable_time = pval;
114 constraints->soft_start = of_property_read_bool(np,
115 "regulator-soft-start");
116 ret = of_property_read_u32(np, "regulator-active-discharge", &pval);
118 constraints->active_discharge =
119 (pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE :
120 REGULATOR_ACTIVE_DISCHARGE_DISABLE;
123 if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) {
124 if (desc && desc->of_map_mode) {
125 mode = desc->of_map_mode(pval);
126 if (mode == REGULATOR_MODE_INVALID)
127 pr_err("%pOFn: invalid mode %u\n", np, pval);
129 constraints->initial_mode = mode;
131 pr_warn("%pOFn: mapping for mode %d not defined\n",
136 len = of_property_count_elems_of_size(np, "regulator-allowed-modes",
139 if (desc && desc->of_map_mode) {
140 for (i = 0; i < len; i++) {
141 ret = of_property_read_u32_index(np,
142 "regulator-allowed-modes", i, &pval);
144 pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
148 mode = desc->of_map_mode(pval);
149 if (mode == REGULATOR_MODE_INVALID)
150 pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
153 constraints->valid_modes_mask |= mode;
155 if (constraints->valid_modes_mask)
156 constraints->valid_ops_mask
157 |= REGULATOR_CHANGE_MODE;
159 pr_warn("%pOFn: mode mapping not defined\n", np);
163 if (!of_property_read_u32(np, "regulator-system-load", &pval))
164 constraints->system_load = pval;
166 if (!of_property_read_u32(np, "regulator-coupled-max-spread",
168 constraints->max_spread = pval;
170 if (!of_property_read_u32(np, "regulator-max-step-microvolt",
172 constraints->max_uV_step = pval;
174 constraints->over_current_protection = of_property_read_bool(np,
175 "regulator-over-current-protection");
177 for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
180 suspend_state = &constraints->state_mem;
183 suspend_state = &constraints->state_disk;
185 case PM_SUSPEND_STANDBY:
186 suspend_state = &constraints->state_standby;
189 case PM_SUSPEND_TO_IDLE:
194 suspend_np = of_get_child_by_name(np, regulator_states[i]);
195 if (!suspend_np || !suspend_state)
198 if (!of_property_read_u32(suspend_np, "regulator-mode",
200 if (desc && desc->of_map_mode) {
201 mode = desc->of_map_mode(pval);
202 if (mode == REGULATOR_MODE_INVALID)
203 pr_err("%pOFn: invalid mode %u\n",
206 suspend_state->mode = mode;
208 pr_warn("%pOFn: mapping for mode %d not defined\n",
213 if (of_property_read_bool(suspend_np,
214 "regulator-on-in-suspend"))
215 suspend_state->enabled = ENABLE_IN_SUSPEND;
216 else if (of_property_read_bool(suspend_np,
217 "regulator-off-in-suspend"))
218 suspend_state->enabled = DISABLE_IN_SUSPEND;
220 if (!of_property_read_u32(np, "regulator-suspend-min-microvolt",
222 suspend_state->min_uV = pval;
224 if (!of_property_read_u32(np, "regulator-suspend-max-microvolt",
226 suspend_state->max_uV = pval;
228 if (!of_property_read_u32(suspend_np,
229 "regulator-suspend-microvolt", &pval))
230 suspend_state->uV = pval;
231 else /* otherwise use min_uV as default suspend voltage */
232 suspend_state->uV = suspend_state->min_uV;
234 if (of_property_read_bool(suspend_np,
235 "regulator-changeable-in-suspend"))
236 suspend_state->changeable = true;
238 if (i == PM_SUSPEND_MEM)
239 constraints->initial_state = PM_SUSPEND_MEM;
241 of_node_put(suspend_np);
242 suspend_state = NULL;
248 * of_get_regulator_init_data - extract regulator_init_data structure info
249 * @dev: device requesting for regulator_init_data
250 * @node: regulator device node
251 * @desc: regulator description
253 * Populates regulator_init_data structure by extracting data from device
254 * tree node, returns a pointer to the populated structure or NULL if memory
257 struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
258 struct device_node *node,
259 const struct regulator_desc *desc)
261 struct regulator_init_data *init_data;
266 init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
268 return NULL; /* Out of memory? */
270 of_get_regulation_constraints(node, &init_data, desc);
273 EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
275 struct devm_of_regulator_matches {
276 struct of_regulator_match *matches;
277 unsigned int num_matches;
280 static void devm_of_regulator_put_matches(struct device *dev, void *res)
282 struct devm_of_regulator_matches *devm_matches = res;
285 for (i = 0; i < devm_matches->num_matches; i++)
286 of_node_put(devm_matches->matches[i].of_node);
290 * of_regulator_match - extract multiple regulator init data from device tree.
291 * @dev: device requesting the data
292 * @node: parent device node of the regulators
293 * @matches: match table for the regulators
294 * @num_matches: number of entries in match table
296 * This function uses a match table specified by the regulator driver to
297 * parse regulator init data from the device tree. @node is expected to
298 * contain a set of child nodes, each providing the init data for one
299 * regulator. The data parsed from a child node will be matched to a regulator
300 * based on either the deprecated property regulator-compatible if present,
301 * or otherwise the child node's name. Note that the match table is modified
302 * in place and an additional of_node reference is taken for each matched
305 * Returns the number of matches found or a negative error code on failure.
307 int of_regulator_match(struct device *dev, struct device_node *node,
308 struct of_regulator_match *matches,
309 unsigned int num_matches)
311 unsigned int count = 0;
314 struct device_node *child;
315 struct devm_of_regulator_matches *devm_matches;
320 devm_matches = devres_alloc(devm_of_regulator_put_matches,
321 sizeof(struct devm_of_regulator_matches),
326 devm_matches->matches = matches;
327 devm_matches->num_matches = num_matches;
329 devres_add(dev, devm_matches);
331 for (i = 0; i < num_matches; i++) {
332 struct of_regulator_match *match = &matches[i];
333 match->init_data = NULL;
334 match->of_node = NULL;
337 for_each_child_of_node(node, child) {
338 name = of_get_property(child,
339 "regulator-compatible", NULL);
342 for (i = 0; i < num_matches; i++) {
343 struct of_regulator_match *match = &matches[i];
347 if (strcmp(match->name, name))
351 of_get_regulator_init_data(dev, child,
353 if (!match->init_data) {
355 "failed to parse DT for regulator %pOFn\n",
360 match->of_node = of_node_get(child);
368 EXPORT_SYMBOL_GPL(of_regulator_match);
371 device_node *regulator_of_get_init_node(struct device *dev,
372 const struct regulator_desc *desc)
374 struct device_node *search, *child;
377 if (!dev->of_node || !desc->of_match)
380 if (desc->regulators_node) {
381 search = of_get_child_by_name(dev->of_node,
382 desc->regulators_node);
384 search = of_node_get(dev->of_node);
386 if (!strcmp(desc->of_match, search->name))
391 dev_dbg(dev, "Failed to find regulator container node '%s'\n",
392 desc->regulators_node);
396 for_each_available_child_of_node(search, child) {
397 name = of_get_property(child, "regulator-compatible", NULL);
401 if (!strcmp(desc->of_match, name))
402 return of_node_get(child);
410 struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
411 const struct regulator_desc *desc,
412 struct regulator_config *config,
413 struct device_node **node)
415 struct device_node *child;
416 struct regulator_init_data *init_data = NULL;
418 child = regulator_of_get_init_node(dev, desc);
422 init_data = of_get_regulator_init_data(dev, child, desc);
424 dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
428 if (desc->of_parse_cb && desc->of_parse_cb(child, desc, config)) {
430 "driver callback failed to parse DT for regulator %pOFn\n",
445 static int of_node_match(struct device *dev, const void *data)
447 return dev->of_node == data;
450 struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
454 dev = class_find_device(®ulator_class, NULL, np, of_node_match);
456 return dev ? dev_to_rdev(dev) : NULL;
460 * Returns number of regulators coupled with rdev.
462 int of_get_n_coupled(struct regulator_dev *rdev)
464 struct device_node *node = rdev->dev.of_node;
467 n_phandles = of_count_phandle_with_args(node,
468 "regulator-coupled-with",
471 return (n_phandles > 0) ? n_phandles : 0;
474 /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
475 static bool of_coupling_find_node(struct device_node *src,
476 struct device_node *to_find)
481 n_phandles = of_count_phandle_with_args(src,
482 "regulator-coupled-with",
485 for (i = 0; i < n_phandles; i++) {
486 struct device_node *tmp = of_parse_phandle(src,
487 "regulator-coupled-with", i);
506 * of_check_coupling_data - Parse rdev's coupling properties and check data
508 * @rdev - pointer to regulator_dev whose data is checked
510 * Function checks if all the following conditions are met:
511 * - rdev's max_spread is greater than 0
512 * - all coupled regulators have the same max_spread
513 * - all coupled regulators have the same number of regulator_dev phandles
514 * - all regulators are linked to each other
516 * Returns true if all conditions are met.
518 bool of_check_coupling_data(struct regulator_dev *rdev)
520 int max_spread = rdev->constraints->max_spread;
521 struct device_node *node = rdev->dev.of_node;
522 int n_phandles = of_get_n_coupled(rdev);
523 struct device_node *c_node;
527 if (max_spread <= 0) {
528 dev_err(&rdev->dev, "max_spread value invalid\n");
532 /* iterate over rdev's phandles */
533 for (i = 0; i < n_phandles; i++) {
534 int c_max_spread, c_n_phandles;
536 c_node = of_parse_phandle(node,
537 "regulator-coupled-with", i);
542 c_n_phandles = of_count_phandle_with_args(c_node,
543 "regulator-coupled-with",
546 if (c_n_phandles != n_phandles) {
547 dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
552 if (of_property_read_u32(c_node, "regulator-coupled-max-spread",
558 if (c_max_spread != max_spread) {
560 "coupled regulators max_spread mismatch\n");
565 if (!of_coupling_find_node(c_node, node)) {
566 dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
580 * of_parse_coupled regulator - Get regulator_dev pointer from rdev's property
581 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
582 * "regulator-coupled-with" property
583 * @index: Index in phandles array
585 * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
586 * registered, returns NULL
588 struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
591 struct device_node *node = rdev->dev.of_node;
592 struct device_node *c_node;
593 struct regulator_dev *c_rdev;
595 c_node = of_parse_phandle(node, "regulator-coupled-with", index);
599 c_rdev = of_find_regulator_by_node(c_node);