drm/nouveau: fix kernel-doc comments
[drm/drm-misc.git] / drivers / regulator / of_regulator.c
blob3d85762beda636479f3b3b808602a0be221c0c77
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * OF helpers for regulator framework
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Rajendra Nayak <rnayak@ti.com>
7 */
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/of.h>
12 #include <linux/regulator/machine.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/regulator/of_regulator.h>
16 #include "internal.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 fill_limit(int *limit, int val)
26 if (val)
27 if (val == 1)
28 *limit = REGULATOR_NOTIF_LIMIT_ENABLE;
29 else
30 *limit = val;
31 else
32 *limit = REGULATOR_NOTIF_LIMIT_DISABLE;
35 static void of_get_regulator_prot_limits(struct device_node *np,
36 struct regulation_constraints *constraints)
38 u32 pval;
39 int i;
40 static const char *const props[] = {
41 "regulator-oc-%s-microamp",
42 "regulator-ov-%s-microvolt",
43 "regulator-temp-%s-kelvin",
44 "regulator-uv-%s-microvolt",
46 struct notification_limit *limits[] = {
47 &constraints->over_curr_limits,
48 &constraints->over_voltage_limits,
49 &constraints->temp_limits,
50 &constraints->under_voltage_limits,
52 bool set[4] = {0};
54 /* Protection limits: */
55 for (i = 0; i < ARRAY_SIZE(props); i++) {
56 char prop[255];
57 bool found;
58 int j;
59 static const char *const lvl[] = {
60 "protection", "error", "warn"
62 int *l[] = {
63 &limits[i]->prot, &limits[i]->err, &limits[i]->warn,
66 for (j = 0; j < ARRAY_SIZE(lvl); j++) {
67 snprintf(prop, 255, props[i], lvl[j]);
68 found = !of_property_read_u32(np, prop, &pval);
69 if (found)
70 fill_limit(l[j], pval);
71 set[i] |= found;
74 constraints->over_current_detection = set[0];
75 constraints->over_voltage_detection = set[1];
76 constraints->over_temp_detection = set[2];
77 constraints->under_voltage_detection = set[3];
80 static int of_get_regulation_constraints(struct device *dev,
81 struct device_node *np,
82 struct regulator_init_data **init_data,
83 const struct regulator_desc *desc)
85 struct regulation_constraints *constraints = &(*init_data)->constraints;
86 struct regulator_state *suspend_state;
87 struct device_node *suspend_np;
88 unsigned int mode;
89 int ret, i, len;
90 int n_phandles;
91 u32 pval;
93 n_phandles = of_count_phandle_with_args(np, "regulator-coupled-with",
94 NULL);
95 n_phandles = max(n_phandles, 0);
97 constraints->name = of_get_property(np, "regulator-name", NULL);
99 if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
100 constraints->min_uV = pval;
102 if (!of_property_read_u32(np, "regulator-max-microvolt", &pval))
103 constraints->max_uV = pval;
105 /* Voltage change possible? */
106 if (constraints->min_uV != constraints->max_uV)
107 constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
109 /* Do we have a voltage range, if so try to apply it? */
110 if (constraints->min_uV && constraints->max_uV)
111 constraints->apply_uV = true;
113 if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval))
114 constraints->uV_offset = pval;
115 if (!of_property_read_u32(np, "regulator-min-microamp", &pval))
116 constraints->min_uA = pval;
117 if (!of_property_read_u32(np, "regulator-max-microamp", &pval))
118 constraints->max_uA = pval;
120 if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
121 &pval))
122 constraints->ilim_uA = pval;
124 /* Current change possible? */
125 if (constraints->min_uA != constraints->max_uA)
126 constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
128 constraints->boot_on = of_property_read_bool(np, "regulator-boot-on");
129 constraints->always_on = of_property_read_bool(np, "regulator-always-on");
130 if (!constraints->always_on) /* status change should be possible. */
131 constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
133 constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
134 constraints->system_critical = of_property_read_bool(np,
135 "system-critical-regulator");
137 if (of_property_read_bool(np, "regulator-allow-bypass"))
138 constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
140 if (of_property_read_bool(np, "regulator-allow-set-load"))
141 constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
143 ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
144 if (!ret) {
145 if (pval)
146 constraints->ramp_delay = pval;
147 else
148 constraints->ramp_disable = true;
151 ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
152 if (!ret)
153 constraints->settling_time = pval;
155 ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval);
156 if (!ret)
157 constraints->settling_time_up = pval;
158 if (constraints->settling_time_up && constraints->settling_time) {
159 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
160 np);
161 constraints->settling_time_up = 0;
164 ret = of_property_read_u32(np, "regulator-settling-time-down-us",
165 &pval);
166 if (!ret)
167 constraints->settling_time_down = pval;
168 if (constraints->settling_time_down && constraints->settling_time) {
169 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
170 np);
171 constraints->settling_time_down = 0;
174 ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
175 if (!ret)
176 constraints->enable_time = pval;
178 ret = of_property_read_u32(np, "regulator-uv-survival-time-ms", &pval);
179 if (!ret)
180 constraints->uv_less_critical_window_ms = pval;
181 else
182 constraints->uv_less_critical_window_ms =
183 REGULATOR_DEF_UV_LESS_CRITICAL_WINDOW_MS;
185 constraints->soft_start = of_property_read_bool(np,
186 "regulator-soft-start");
187 ret = of_property_read_u32(np, "regulator-active-discharge", &pval);
188 if (!ret) {
189 constraints->active_discharge =
190 (pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE :
191 REGULATOR_ACTIVE_DISCHARGE_DISABLE;
194 if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) {
195 if (desc && desc->of_map_mode) {
196 mode = desc->of_map_mode(pval);
197 if (mode == REGULATOR_MODE_INVALID)
198 pr_err("%pOFn: invalid mode %u\n", np, pval);
199 else
200 constraints->initial_mode = mode;
201 } else {
202 pr_warn("%pOFn: mapping for mode %d not defined\n",
203 np, pval);
207 len = of_property_count_elems_of_size(np, "regulator-allowed-modes",
208 sizeof(u32));
209 if (len > 0) {
210 if (desc && desc->of_map_mode) {
211 for (i = 0; i < len; i++) {
212 ret = of_property_read_u32_index(np,
213 "regulator-allowed-modes", i, &pval);
214 if (ret) {
215 pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
216 np, i, ret);
217 break;
219 mode = desc->of_map_mode(pval);
220 if (mode == REGULATOR_MODE_INVALID)
221 pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
222 np, pval);
223 else
224 constraints->valid_modes_mask |= mode;
226 if (constraints->valid_modes_mask)
227 constraints->valid_ops_mask
228 |= REGULATOR_CHANGE_MODE;
229 } else {
230 pr_warn("%pOFn: mode mapping not defined\n", np);
234 if (!of_property_read_u32(np, "regulator-system-load", &pval))
235 constraints->system_load = pval;
237 if (n_phandles) {
238 constraints->max_spread = devm_kzalloc(dev,
239 sizeof(*constraints->max_spread) * n_phandles,
240 GFP_KERNEL);
242 if (!constraints->max_spread)
243 return -ENOMEM;
245 of_property_read_u32_array(np, "regulator-coupled-max-spread",
246 constraints->max_spread, n_phandles);
249 if (!of_property_read_u32(np, "regulator-max-step-microvolt",
250 &pval))
251 constraints->max_uV_step = pval;
253 constraints->over_current_protection = of_property_read_bool(np,
254 "regulator-over-current-protection");
256 of_get_regulator_prot_limits(np, constraints);
258 for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
259 switch (i) {
260 case PM_SUSPEND_MEM:
261 suspend_state = &constraints->state_mem;
262 break;
263 case PM_SUSPEND_MAX:
264 suspend_state = &constraints->state_disk;
265 break;
266 case PM_SUSPEND_STANDBY:
267 suspend_state = &constraints->state_standby;
268 break;
269 case PM_SUSPEND_ON:
270 case PM_SUSPEND_TO_IDLE:
271 default:
272 continue;
275 suspend_np = of_get_child_by_name(np, regulator_states[i]);
276 if (!suspend_np)
277 continue;
278 if (!suspend_state) {
279 of_node_put(suspend_np);
280 continue;
283 if (!of_property_read_u32(suspend_np, "regulator-mode",
284 &pval)) {
285 if (desc && desc->of_map_mode) {
286 mode = desc->of_map_mode(pval);
287 if (mode == REGULATOR_MODE_INVALID)
288 pr_err("%pOFn: invalid mode %u\n",
289 np, pval);
290 else
291 suspend_state->mode = mode;
292 } else {
293 pr_warn("%pOFn: mapping for mode %d not defined\n",
294 np, pval);
298 if (of_property_read_bool(suspend_np,
299 "regulator-on-in-suspend"))
300 suspend_state->enabled = ENABLE_IN_SUSPEND;
301 else if (of_property_read_bool(suspend_np,
302 "regulator-off-in-suspend"))
303 suspend_state->enabled = DISABLE_IN_SUSPEND;
305 if (!of_property_read_u32(suspend_np,
306 "regulator-suspend-min-microvolt", &pval))
307 suspend_state->min_uV = pval;
309 if (!of_property_read_u32(suspend_np,
310 "regulator-suspend-max-microvolt", &pval))
311 suspend_state->max_uV = pval;
313 if (!of_property_read_u32(suspend_np,
314 "regulator-suspend-microvolt", &pval))
315 suspend_state->uV = pval;
316 else /* otherwise use min_uV as default suspend voltage */
317 suspend_state->uV = suspend_state->min_uV;
319 if (of_property_read_bool(suspend_np,
320 "regulator-changeable-in-suspend"))
321 suspend_state->changeable = true;
323 if (i == PM_SUSPEND_MEM)
324 constraints->initial_state = PM_SUSPEND_MEM;
326 of_node_put(suspend_np);
327 suspend_state = NULL;
328 suspend_np = NULL;
331 return 0;
335 * of_get_regulator_init_data - extract regulator_init_data structure info
336 * @dev: device requesting for regulator_init_data
337 * @node: regulator device node
338 * @desc: regulator description
340 * Populates regulator_init_data structure by extracting data from device
341 * tree node.
343 * Return: Pointer to a populated &struct regulator_init_data or NULL if
344 * memory allocation fails.
346 struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
347 struct device_node *node,
348 const struct regulator_desc *desc)
350 struct regulator_init_data *init_data;
352 if (!node)
353 return NULL;
355 init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
356 if (!init_data)
357 return NULL; /* Out of memory? */
359 if (of_get_regulation_constraints(dev, node, &init_data, desc))
360 return NULL;
362 return init_data;
364 EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
366 struct devm_of_regulator_matches {
367 struct of_regulator_match *matches;
368 unsigned int num_matches;
371 static void devm_of_regulator_put_matches(struct device *dev, void *res)
373 struct devm_of_regulator_matches *devm_matches = res;
374 int i;
376 for (i = 0; i < devm_matches->num_matches; i++)
377 of_node_put(devm_matches->matches[i].of_node);
381 * of_regulator_match - extract multiple regulator init data from device tree.
382 * @dev: device requesting the data
383 * @node: parent device node of the regulators
384 * @matches: match table for the regulators
385 * @num_matches: number of entries in match table
387 * This function uses a match table specified by the regulator driver to
388 * parse regulator init data from the device tree. @node is expected to
389 * contain a set of child nodes, each providing the init data for one
390 * regulator. The data parsed from a child node will be matched to a regulator
391 * based on either the deprecated property regulator-compatible if present,
392 * or otherwise the child node's name. Note that the match table is modified
393 * in place and an additional of_node reference is taken for each matched
394 * regulator.
396 * Return: The number of matches found or a negative error number on failure.
398 int of_regulator_match(struct device *dev, struct device_node *node,
399 struct of_regulator_match *matches,
400 unsigned int num_matches)
402 unsigned int count = 0;
403 unsigned int i;
404 const char *name;
405 struct device_node *child;
406 struct devm_of_regulator_matches *devm_matches;
408 if (!dev || !node)
409 return -EINVAL;
411 devm_matches = devres_alloc(devm_of_regulator_put_matches,
412 sizeof(struct devm_of_regulator_matches),
413 GFP_KERNEL);
414 if (!devm_matches)
415 return -ENOMEM;
417 devm_matches->matches = matches;
418 devm_matches->num_matches = num_matches;
420 devres_add(dev, devm_matches);
422 for (i = 0; i < num_matches; i++) {
423 struct of_regulator_match *match = &matches[i];
424 match->init_data = NULL;
425 match->of_node = NULL;
428 for_each_child_of_node(node, child) {
429 name = of_get_property(child,
430 "regulator-compatible", NULL);
431 if (!name)
432 name = child->name;
433 for (i = 0; i < num_matches; i++) {
434 struct of_regulator_match *match = &matches[i];
435 if (match->of_node)
436 continue;
438 if (strcmp(match->name, name))
439 continue;
441 match->init_data =
442 of_get_regulator_init_data(dev, child,
443 match->desc);
444 if (!match->init_data) {
445 dev_err(dev,
446 "failed to parse DT for regulator %pOFn\n",
447 child);
448 of_node_put(child);
449 return -EINVAL;
451 match->of_node = of_node_get(child);
452 count++;
453 break;
457 return count;
459 EXPORT_SYMBOL_GPL(of_regulator_match);
461 static struct
462 device_node *regulator_of_get_init_node(struct device *dev,
463 const struct regulator_desc *desc)
465 struct device_node *search, *child;
466 const char *name;
468 if (!dev->of_node || !desc->of_match)
469 return NULL;
471 if (desc->regulators_node) {
472 search = of_get_child_by_name(dev->of_node,
473 desc->regulators_node);
474 } else {
475 search = of_node_get(dev->of_node);
477 if (!strcmp(desc->of_match, search->name))
478 return search;
481 if (!search) {
482 dev_dbg(dev, "Failed to find regulator container node '%s'\n",
483 desc->regulators_node);
484 return NULL;
487 for_each_available_child_of_node(search, child) {
488 name = of_get_property(child, "regulator-compatible", NULL);
489 if (!name) {
490 if (!desc->of_match_full_name)
491 name = child->name;
492 else
493 name = child->full_name;
496 if (!strcmp(desc->of_match, name)) {
497 of_node_put(search);
499 * 'of_node_get(child)' is already performed by the
500 * for_each loop.
502 return child;
506 of_node_put(search);
508 return NULL;
511 struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
512 const struct regulator_desc *desc,
513 struct regulator_config *config,
514 struct device_node **node)
516 struct device_node *child;
517 struct regulator_init_data *init_data = NULL;
519 child = regulator_of_get_init_node(config->dev, desc);
520 if (!child)
521 return NULL;
523 init_data = of_get_regulator_init_data(dev, child, desc);
524 if (!init_data) {
525 dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
526 goto error;
529 if (desc->of_parse_cb) {
530 int ret;
532 ret = desc->of_parse_cb(child, desc, config);
533 if (ret) {
534 if (ret == -EPROBE_DEFER) {
535 of_node_put(child);
536 return ERR_PTR(-EPROBE_DEFER);
538 dev_err(dev,
539 "driver callback failed to parse DT for regulator %pOFn\n",
540 child);
541 goto error;
545 *node = child;
547 return init_data;
549 error:
550 of_node_put(child);
552 return NULL;
556 * of_get_child_regulator - get a child regulator device node
557 * based on supply name
558 * @parent: Parent device node
559 * @prop_name: Combination regulator supply name and "-supply"
561 * Traverse all child nodes.
562 * Extract the child regulator device node corresponding to the supply name.
564 * Return: Pointer to the &struct device_node corresponding to the regulator
565 * if found, or %NULL if not found.
567 static struct device_node *of_get_child_regulator(struct device_node *parent,
568 const char *prop_name)
570 struct device_node *regnode = NULL;
571 struct device_node *child = NULL;
573 for_each_child_of_node(parent, child) {
574 regnode = of_parse_phandle(child, prop_name, 0);
575 if (regnode)
576 goto err_node_put;
578 regnode = of_get_child_regulator(child, prop_name);
579 if (regnode)
580 goto err_node_put;
582 return NULL;
584 err_node_put:
585 of_node_put(child);
586 return regnode;
590 * of_get_regulator - get a regulator device node based on supply name
591 * @dev: Device pointer for dev_printk() messages
592 * @node: Device node pointer for supply property lookup
593 * @supply: regulator supply name
595 * Extract the regulator device node corresponding to the supply name.
597 * Return: Pointer to the &struct device_node corresponding to the regulator
598 * if found, or %NULL if not found.
600 static struct device_node *of_get_regulator(struct device *dev, struct device_node *node,
601 const char *supply)
603 struct device_node *regnode = NULL;
604 char prop_name[64]; /* 64 is max size of property name */
606 dev_dbg(dev, "Looking up %s-supply from device node %pOF\n", supply, node);
608 snprintf(prop_name, 64, "%s-supply", supply);
609 regnode = of_parse_phandle(node, prop_name, 0);
610 if (regnode)
611 return regnode;
613 regnode = of_get_child_regulator(dev->of_node, prop_name);
614 if (regnode)
615 return regnode;
617 dev_dbg(dev, "Looking up %s property in node %pOF failed\n", prop_name, dev->of_node);
618 return NULL;
621 static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
623 struct device *dev;
625 dev = class_find_device_by_of_node(&regulator_class, np);
627 return dev ? dev_to_rdev(dev) : NULL;
631 * of_regulator_dev_lookup - lookup a regulator device with device tree only
632 * @dev: Device pointer for regulator supply lookup.
633 * @np: Device node pointer for regulator supply lookup.
634 * @supply: Supply name or regulator ID.
636 * Return: Pointer to the &struct regulator_dev on success, or ERR_PTR()
637 * encoded value on error.
639 * If successful, returns a pointer to the &struct regulator_dev that
640 * corresponds to the name @supply and with the embedded &struct device
641 * refcount incremented by one. The refcount must be dropped by calling
642 * put_device().
644 * On failure one of the following ERR_PTR() encoded values is returned:
645 * * -%ENODEV if lookup fails permanently.
646 * * -%EPROBE_DEFER if lookup could succeed in the future.
648 struct regulator_dev *of_regulator_dev_lookup(struct device *dev, struct device_node *np,
649 const char *supply)
651 struct regulator_dev *r;
652 struct device_node *node;
654 node = of_get_regulator(dev, np, supply);
655 if (node) {
656 r = of_find_regulator_by_node(node);
657 of_node_put(node);
658 if (r)
659 return r;
662 * We have a node, but there is no device.
663 * assume it has not registered yet.
665 return ERR_PTR(-EPROBE_DEFER);
668 return ERR_PTR(-ENODEV);
671 struct regulator *_of_regulator_get(struct device *dev, struct device_node *node,
672 const char *id, enum regulator_get_type get_type)
674 struct regulator_dev *r;
675 int ret;
677 ret = _regulator_get_common_check(dev, id, get_type);
678 if (ret)
679 return ERR_PTR(ret);
681 r = of_regulator_dev_lookup(dev, node, id);
682 return _regulator_get_common(r, dev, id, get_type);
686 * of_regulator_get_optional - get optional regulator via device tree lookup
687 * @dev: device used for dev_printk() messages
688 * @node: device node for regulator "consumer"
689 * @id: Supply name
691 * Return: pointer to struct regulator corresponding to the regulator producer,
692 * or PTR_ERR() encoded error number.
694 * This is intended for use by consumers that want to get a regulator
695 * supply directly from a device node, and can and want to deal with
696 * absence of such supplies. This will _not_ consider supply aliases.
697 * See regulator_dev_lookup().
699 struct regulator *of_regulator_get_optional(struct device *dev,
700 struct device_node *node,
701 const char *id)
703 return _of_regulator_get(dev, node, id, OPTIONAL_GET);
705 EXPORT_SYMBOL_GPL(of_regulator_get_optional);
708 * Returns number of regulators coupled with rdev.
710 int of_get_n_coupled(struct regulator_dev *rdev)
712 struct device_node *node = rdev->dev.of_node;
713 int n_phandles;
715 n_phandles = of_count_phandle_with_args(node,
716 "regulator-coupled-with",
717 NULL);
719 return (n_phandles > 0) ? n_phandles : 0;
722 /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
723 static bool of_coupling_find_node(struct device_node *src,
724 struct device_node *to_find,
725 int *index)
727 int n_phandles, i;
728 bool found = false;
730 n_phandles = of_count_phandle_with_args(src,
731 "regulator-coupled-with",
732 NULL);
734 for (i = 0; i < n_phandles; i++) {
735 struct device_node *tmp = of_parse_phandle(src,
736 "regulator-coupled-with", i);
738 if (!tmp)
739 break;
741 /* found */
742 if (tmp == to_find)
743 found = true;
745 of_node_put(tmp);
747 if (found) {
748 *index = i;
749 break;
753 return found;
757 * of_check_coupling_data - Parse rdev's coupling properties and check data
758 * consistency
759 * @rdev: pointer to regulator_dev whose data is checked
761 * Function checks if all the following conditions are met:
762 * - rdev's max_spread is greater than 0
763 * - all coupled regulators have the same max_spread
764 * - all coupled regulators have the same number of regulator_dev phandles
765 * - all regulators are linked to each other
767 * Return: True if all conditions are met; false otherwise.
769 bool of_check_coupling_data(struct regulator_dev *rdev)
771 struct device_node *node = rdev->dev.of_node;
772 int n_phandles = of_get_n_coupled(rdev);
773 struct device_node *c_node;
774 int index;
775 int i;
776 bool ret = true;
778 /* iterate over rdev's phandles */
779 for (i = 0; i < n_phandles; i++) {
780 int max_spread = rdev->constraints->max_spread[i];
781 int c_max_spread, c_n_phandles;
783 if (max_spread <= 0) {
784 dev_err(&rdev->dev, "max_spread value invalid\n");
785 return false;
788 c_node = of_parse_phandle(node,
789 "regulator-coupled-with", i);
791 if (!c_node)
792 ret = false;
794 c_n_phandles = of_count_phandle_with_args(c_node,
795 "regulator-coupled-with",
796 NULL);
798 if (c_n_phandles != n_phandles) {
799 dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
800 ret = false;
801 goto clean;
804 if (!of_coupling_find_node(c_node, node, &index)) {
805 dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
806 ret = false;
807 goto clean;
810 if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread",
811 index, &c_max_spread)) {
812 ret = false;
813 goto clean;
816 if (c_max_spread != max_spread) {
817 dev_err(&rdev->dev,
818 "coupled regulators max_spread mismatch\n");
819 ret = false;
820 goto clean;
823 clean:
824 of_node_put(c_node);
825 if (!ret)
826 break;
829 return ret;
833 * of_parse_coupled_regulator() - Get regulator_dev pointer from rdev's property
834 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
835 * "regulator-coupled-with" property
836 * @index: Index in phandles array
838 * Return: Pointer to the &struct regulator_dev parsed from DTS, or %NULL if
839 * it has not yet been registered.
841 struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
842 int index)
844 struct device_node *node = rdev->dev.of_node;
845 struct device_node *c_node;
846 struct regulator_dev *c_rdev;
848 c_node = of_parse_phandle(node, "regulator-coupled-with", index);
849 if (!c_node)
850 return NULL;
852 c_rdev = of_find_regulator_by_node(c_node);
854 of_node_put(c_node);
856 return c_rdev;
860 * Check if name is a supply name according to the '*-supply' pattern
861 * return 0 if false
862 * return length of supply name without the -supply
864 static int is_supply_name(const char *name)
866 int strs, i;
868 strs = strlen(name);
869 /* string need to be at minimum len(x-supply) */
870 if (strs < 8)
871 return 0;
872 for (i = strs - 6; i > 0; i--) {
873 /* find first '-' and check if right part is supply */
874 if (name[i] != '-')
875 continue;
876 if (strcmp(name + i + 1, "supply") != 0)
877 return 0;
878 return i;
880 return 0;
884 * of_regulator_bulk_get_all - get multiple regulator consumers
886 * @dev: Device to supply
887 * @np: device node to search for consumers
888 * @consumers: Configuration of consumers; clients are stored here.
890 * This helper function allows drivers to get several regulator
891 * consumers in one operation. If any of the regulators cannot be
892 * acquired then any regulators that were allocated will be freed
893 * before returning to the caller, and @consumers will not be
894 * changed.
896 * Return: Number of regulators on success, or a negative error number
897 * on failure.
899 int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
900 struct regulator_bulk_data **consumers)
902 int num_consumers = 0;
903 struct regulator *tmp;
904 struct regulator_bulk_data *_consumers = NULL;
905 struct property *prop;
906 int i, n = 0, ret;
907 char name[64];
910 * first pass: get numbers of xxx-supply
911 * second pass: fill consumers
913 restart:
914 for_each_property_of_node(np, prop) {
915 i = is_supply_name(prop->name);
916 if (i == 0)
917 continue;
918 if (!_consumers) {
919 num_consumers++;
920 continue;
921 } else {
922 memcpy(name, prop->name, i);
923 name[i] = '\0';
924 tmp = regulator_get(dev, name);
925 if (IS_ERR(tmp)) {
926 ret = PTR_ERR(tmp);
927 goto error;
929 _consumers[n].consumer = tmp;
930 n++;
931 continue;
934 if (_consumers) {
935 *consumers = _consumers;
936 return num_consumers;
938 if (num_consumers == 0)
939 return 0;
940 _consumers = kmalloc_array(num_consumers,
941 sizeof(struct regulator_bulk_data),
942 GFP_KERNEL);
943 if (!_consumers)
944 return -ENOMEM;
945 goto restart;
947 error:
948 while (--n >= 0)
949 regulator_put(_consumers[n].consumer);
950 kfree(_consumers);
951 return ret;
953 EXPORT_SYMBOL_GPL(of_regulator_bulk_get_all);