Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * OF helpers for regulator framework
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Rajendra Nayak <rnayak@ti.com>
7 */
8
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>
15
16#include "internal.h"
17
18static 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",
22};
23
24static void fill_limit(int *limit, int val)
25{
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;
33}
34
35static void of_get_regulator_prot_limits(struct device_node *np,
36 struct regulation_constraints *constraints)
37{
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",
45 };
46 struct notification_limit *limits[] = {
47 &constraints->over_curr_limits,
48 &constraints->over_voltage_limits,
49 &constraints->temp_limits,
50 &constraints->under_voltage_limits,
51 };
52 bool set[4] = {0};
53
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"
61 };
62 int *l[] = {
63 &limits[i]->prot, &limits[i]->err, &limits[i]->warn,
64 };
65
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;
72 }
73 }
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];
78}
79
80static 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)
84{
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;
92
93 n_phandles = of_count_phandle_with_args(np, "regulator-coupled-with",
94 NULL);
95 n_phandles = max(n_phandles, 0);
96
97 constraints->name = of_get_property(np, "regulator-name", NULL);
98
99 if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
100 constraints->min_uV = pval;
101
102 if (!of_property_read_u32(np, "regulator-max-microvolt", &pval))
103 constraints->max_uV = pval;
104
105 /* Voltage change possible? */
106 if (constraints->min_uV != constraints->max_uV)
107 constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
108
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;
112
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;
119
120 if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
121 &pval))
122 constraints->ilim_uA = pval;
123
124 /* Current change possible? */
125 if (constraints->min_uA != constraints->max_uA)
126 constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
127
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;
132
133 constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
134
135 if (of_property_read_bool(np, "regulator-allow-bypass"))
136 constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
137
138 if (of_property_read_bool(np, "regulator-allow-set-load"))
139 constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
140
141 ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
142 if (!ret) {
143 if (pval)
144 constraints->ramp_delay = pval;
145 else
146 constraints->ramp_disable = true;
147 }
148
149 ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
150 if (!ret)
151 constraints->settling_time = pval;
152
153 ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval);
154 if (!ret)
155 constraints->settling_time_up = pval;
156 if (constraints->settling_time_up && constraints->settling_time) {
157 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
158 np);
159 constraints->settling_time_up = 0;
160 }
161
162 ret = of_property_read_u32(np, "regulator-settling-time-down-us",
163 &pval);
164 if (!ret)
165 constraints->settling_time_down = pval;
166 if (constraints->settling_time_down && constraints->settling_time) {
167 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
168 np);
169 constraints->settling_time_down = 0;
170 }
171
172 ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
173 if (!ret)
174 constraints->enable_time = pval;
175
176 constraints->soft_start = of_property_read_bool(np,
177 "regulator-soft-start");
178 ret = of_property_read_u32(np, "regulator-active-discharge", &pval);
179 if (!ret) {
180 constraints->active_discharge =
181 (pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE :
182 REGULATOR_ACTIVE_DISCHARGE_DISABLE;
183 }
184
185 if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) {
186 if (desc && desc->of_map_mode) {
187 mode = desc->of_map_mode(pval);
188 if (mode == REGULATOR_MODE_INVALID)
189 pr_err("%pOFn: invalid mode %u\n", np, pval);
190 else
191 constraints->initial_mode = mode;
192 } else {
193 pr_warn("%pOFn: mapping for mode %d not defined\n",
194 np, pval);
195 }
196 }
197
198 len = of_property_count_elems_of_size(np, "regulator-allowed-modes",
199 sizeof(u32));
200 if (len > 0) {
201 if (desc && desc->of_map_mode) {
202 for (i = 0; i < len; i++) {
203 ret = of_property_read_u32_index(np,
204 "regulator-allowed-modes", i, &pval);
205 if (ret) {
206 pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
207 np, i, ret);
208 break;
209 }
210 mode = desc->of_map_mode(pval);
211 if (mode == REGULATOR_MODE_INVALID)
212 pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
213 np, pval);
214 else
215 constraints->valid_modes_mask |= mode;
216 }
217 if (constraints->valid_modes_mask)
218 constraints->valid_ops_mask
219 |= REGULATOR_CHANGE_MODE;
220 } else {
221 pr_warn("%pOFn: mode mapping not defined\n", np);
222 }
223 }
224
225 if (!of_property_read_u32(np, "regulator-system-load", &pval))
226 constraints->system_load = pval;
227
228 if (n_phandles) {
229 constraints->max_spread = devm_kzalloc(dev,
230 sizeof(*constraints->max_spread) * n_phandles,
231 GFP_KERNEL);
232
233 if (!constraints->max_spread)
234 return -ENOMEM;
235
236 of_property_read_u32_array(np, "regulator-coupled-max-spread",
237 constraints->max_spread, n_phandles);
238 }
239
240 if (!of_property_read_u32(np, "regulator-max-step-microvolt",
241 &pval))
242 constraints->max_uV_step = pval;
243
244 constraints->over_current_protection = of_property_read_bool(np,
245 "regulator-over-current-protection");
246
247 of_get_regulator_prot_limits(np, constraints);
248
249 for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
250 switch (i) {
251 case PM_SUSPEND_MEM:
252 suspend_state = &constraints->state_mem;
253 break;
254 case PM_SUSPEND_MAX:
255 suspend_state = &constraints->state_disk;
256 break;
257 case PM_SUSPEND_STANDBY:
258 suspend_state = &constraints->state_standby;
259 break;
260 case PM_SUSPEND_ON:
261 case PM_SUSPEND_TO_IDLE:
262 default:
263 continue;
264 }
265
266 suspend_np = of_get_child_by_name(np, regulator_states[i]);
267 if (!suspend_np)
268 continue;
269 if (!suspend_state) {
270 of_node_put(suspend_np);
271 continue;
272 }
273
274 if (!of_property_read_u32(suspend_np, "regulator-mode",
275 &pval)) {
276 if (desc && desc->of_map_mode) {
277 mode = desc->of_map_mode(pval);
278 if (mode == REGULATOR_MODE_INVALID)
279 pr_err("%pOFn: invalid mode %u\n",
280 np, pval);
281 else
282 suspend_state->mode = mode;
283 } else {
284 pr_warn("%pOFn: mapping for mode %d not defined\n",
285 np, pval);
286 }
287 }
288
289 if (of_property_read_bool(suspend_np,
290 "regulator-on-in-suspend"))
291 suspend_state->enabled = ENABLE_IN_SUSPEND;
292 else if (of_property_read_bool(suspend_np,
293 "regulator-off-in-suspend"))
294 suspend_state->enabled = DISABLE_IN_SUSPEND;
295
296 if (!of_property_read_u32(suspend_np,
297 "regulator-suspend-min-microvolt", &pval))
298 suspend_state->min_uV = pval;
299
300 if (!of_property_read_u32(suspend_np,
301 "regulator-suspend-max-microvolt", &pval))
302 suspend_state->max_uV = pval;
303
304 if (!of_property_read_u32(suspend_np,
305 "regulator-suspend-microvolt", &pval))
306 suspend_state->uV = pval;
307 else /* otherwise use min_uV as default suspend voltage */
308 suspend_state->uV = suspend_state->min_uV;
309
310 if (of_property_read_bool(suspend_np,
311 "regulator-changeable-in-suspend"))
312 suspend_state->changeable = true;
313
314 if (i == PM_SUSPEND_MEM)
315 constraints->initial_state = PM_SUSPEND_MEM;
316
317 of_node_put(suspend_np);
318 suspend_state = NULL;
319 suspend_np = NULL;
320 }
321
322 return 0;
323}
324
325/**
326 * of_get_regulator_init_data - extract regulator_init_data structure info
327 * @dev: device requesting for regulator_init_data
328 * @node: regulator device node
329 * @desc: regulator description
330 *
331 * Populates regulator_init_data structure by extracting data from device
332 * tree node, returns a pointer to the populated structure or NULL if memory
333 * alloc fails.
334 */
335struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
336 struct device_node *node,
337 const struct regulator_desc *desc)
338{
339 struct regulator_init_data *init_data;
340
341 if (!node)
342 return NULL;
343
344 init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
345 if (!init_data)
346 return NULL; /* Out of memory? */
347
348 if (of_get_regulation_constraints(dev, node, &init_data, desc))
349 return NULL;
350
351 return init_data;
352}
353EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
354
355struct devm_of_regulator_matches {
356 struct of_regulator_match *matches;
357 unsigned int num_matches;
358};
359
360static void devm_of_regulator_put_matches(struct device *dev, void *res)
361{
362 struct devm_of_regulator_matches *devm_matches = res;
363 int i;
364
365 for (i = 0; i < devm_matches->num_matches; i++)
366 of_node_put(devm_matches->matches[i].of_node);
367}
368
369/**
370 * of_regulator_match - extract multiple regulator init data from device tree.
371 * @dev: device requesting the data
372 * @node: parent device node of the regulators
373 * @matches: match table for the regulators
374 * @num_matches: number of entries in match table
375 *
376 * This function uses a match table specified by the regulator driver to
377 * parse regulator init data from the device tree. @node is expected to
378 * contain a set of child nodes, each providing the init data for one
379 * regulator. The data parsed from a child node will be matched to a regulator
380 * based on either the deprecated property regulator-compatible if present,
381 * or otherwise the child node's name. Note that the match table is modified
382 * in place and an additional of_node reference is taken for each matched
383 * regulator.
384 *
385 * Returns the number of matches found or a negative error code on failure.
386 */
387int of_regulator_match(struct device *dev, struct device_node *node,
388 struct of_regulator_match *matches,
389 unsigned int num_matches)
390{
391 unsigned int count = 0;
392 unsigned int i;
393 const char *name;
394 struct device_node *child;
395 struct devm_of_regulator_matches *devm_matches;
396
397 if (!dev || !node)
398 return -EINVAL;
399
400 devm_matches = devres_alloc(devm_of_regulator_put_matches,
401 sizeof(struct devm_of_regulator_matches),
402 GFP_KERNEL);
403 if (!devm_matches)
404 return -ENOMEM;
405
406 devm_matches->matches = matches;
407 devm_matches->num_matches = num_matches;
408
409 devres_add(dev, devm_matches);
410
411 for (i = 0; i < num_matches; i++) {
412 struct of_regulator_match *match = &matches[i];
413 match->init_data = NULL;
414 match->of_node = NULL;
415 }
416
417 for_each_child_of_node(node, child) {
418 name = of_get_property(child,
419 "regulator-compatible", NULL);
420 if (!name)
421 name = child->name;
422 for (i = 0; i < num_matches; i++) {
423 struct of_regulator_match *match = &matches[i];
424 if (match->of_node)
425 continue;
426
427 if (strcmp(match->name, name))
428 continue;
429
430 match->init_data =
431 of_get_regulator_init_data(dev, child,
432 match->desc);
433 if (!match->init_data) {
434 dev_err(dev,
435 "failed to parse DT for regulator %pOFn\n",
436 child);
437 of_node_put(child);
438 return -EINVAL;
439 }
440 match->of_node = of_node_get(child);
441 count++;
442 break;
443 }
444 }
445
446 return count;
447}
448EXPORT_SYMBOL_GPL(of_regulator_match);
449
450static struct
451device_node *regulator_of_get_init_node(struct device *dev,
452 const struct regulator_desc *desc)
453{
454 struct device_node *search, *child;
455 const char *name;
456
457 if (!dev->of_node || !desc->of_match)
458 return NULL;
459
460 if (desc->regulators_node) {
461 search = of_get_child_by_name(dev->of_node,
462 desc->regulators_node);
463 } else {
464 search = of_node_get(dev->of_node);
465
466 if (!strcmp(desc->of_match, search->name))
467 return search;
468 }
469
470 if (!search) {
471 dev_dbg(dev, "Failed to find regulator container node '%s'\n",
472 desc->regulators_node);
473 return NULL;
474 }
475
476 for_each_available_child_of_node(search, child) {
477 name = of_get_property(child, "regulator-compatible", NULL);
478 if (!name) {
479 if (!desc->of_match_full_name)
480 name = child->name;
481 else
482 name = child->full_name;
483 }
484
485 if (!strcmp(desc->of_match, name)) {
486 of_node_put(search);
487 /*
488 * 'of_node_get(child)' is already performed by the
489 * for_each loop.
490 */
491 return child;
492 }
493 }
494
495 of_node_put(search);
496
497 return NULL;
498}
499
500struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
501 const struct regulator_desc *desc,
502 struct regulator_config *config,
503 struct device_node **node)
504{
505 struct device_node *child;
506 struct regulator_init_data *init_data = NULL;
507
508 child = regulator_of_get_init_node(config->dev, desc);
509 if (!child)
510 return NULL;
511
512 init_data = of_get_regulator_init_data(dev, child, desc);
513 if (!init_data) {
514 dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
515 goto error;
516 }
517
518 if (desc->of_parse_cb) {
519 int ret;
520
521 ret = desc->of_parse_cb(child, desc, config);
522 if (ret) {
523 if (ret == -EPROBE_DEFER) {
524 of_node_put(child);
525 return ERR_PTR(-EPROBE_DEFER);
526 }
527 dev_err(dev,
528 "driver callback failed to parse DT for regulator %pOFn\n",
529 child);
530 goto error;
531 }
532 }
533
534 *node = child;
535
536 return init_data;
537
538error:
539 of_node_put(child);
540
541 return NULL;
542}
543
544struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
545{
546 struct device *dev;
547
548 dev = class_find_device_by_of_node(®ulator_class, np);
549
550 return dev ? dev_to_rdev(dev) : NULL;
551}
552
553/*
554 * Returns number of regulators coupled with rdev.
555 */
556int of_get_n_coupled(struct regulator_dev *rdev)
557{
558 struct device_node *node = rdev->dev.of_node;
559 int n_phandles;
560
561 n_phandles = of_count_phandle_with_args(node,
562 "regulator-coupled-with",
563 NULL);
564
565 return (n_phandles > 0) ? n_phandles : 0;
566}
567
568/* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
569static bool of_coupling_find_node(struct device_node *src,
570 struct device_node *to_find,
571 int *index)
572{
573 int n_phandles, i;
574 bool found = false;
575
576 n_phandles = of_count_phandle_with_args(src,
577 "regulator-coupled-with",
578 NULL);
579
580 for (i = 0; i < n_phandles; i++) {
581 struct device_node *tmp = of_parse_phandle(src,
582 "regulator-coupled-with", i);
583
584 if (!tmp)
585 break;
586
587 /* found */
588 if (tmp == to_find)
589 found = true;
590
591 of_node_put(tmp);
592
593 if (found) {
594 *index = i;
595 break;
596 }
597 }
598
599 return found;
600}
601
602/**
603 * of_check_coupling_data - Parse rdev's coupling properties and check data
604 * consistency
605 * @rdev: pointer to regulator_dev whose data is checked
606 *
607 * Function checks if all the following conditions are met:
608 * - rdev's max_spread is greater than 0
609 * - all coupled regulators have the same max_spread
610 * - all coupled regulators have the same number of regulator_dev phandles
611 * - all regulators are linked to each other
612 *
613 * Returns true if all conditions are met.
614 */
615bool of_check_coupling_data(struct regulator_dev *rdev)
616{
617 struct device_node *node = rdev->dev.of_node;
618 int n_phandles = of_get_n_coupled(rdev);
619 struct device_node *c_node;
620 int index;
621 int i;
622 bool ret = true;
623
624 /* iterate over rdev's phandles */
625 for (i = 0; i < n_phandles; i++) {
626 int max_spread = rdev->constraints->max_spread[i];
627 int c_max_spread, c_n_phandles;
628
629 if (max_spread <= 0) {
630 dev_err(&rdev->dev, "max_spread value invalid\n");
631 return false;
632 }
633
634 c_node = of_parse_phandle(node,
635 "regulator-coupled-with", i);
636
637 if (!c_node)
638 ret = false;
639
640 c_n_phandles = of_count_phandle_with_args(c_node,
641 "regulator-coupled-with",
642 NULL);
643
644 if (c_n_phandles != n_phandles) {
645 dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
646 ret = false;
647 goto clean;
648 }
649
650 if (!of_coupling_find_node(c_node, node, &index)) {
651 dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
652 ret = false;
653 goto clean;
654 }
655
656 if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread",
657 index, &c_max_spread)) {
658 ret = false;
659 goto clean;
660 }
661
662 if (c_max_spread != max_spread) {
663 dev_err(&rdev->dev,
664 "coupled regulators max_spread mismatch\n");
665 ret = false;
666 goto clean;
667 }
668
669clean:
670 of_node_put(c_node);
671 if (!ret)
672 break;
673 }
674
675 return ret;
676}
677
678/**
679 * of_parse_coupled_regulator() - Get regulator_dev pointer from rdev's property
680 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
681 * "regulator-coupled-with" property
682 * @index: Index in phandles array
683 *
684 * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
685 * registered, returns NULL
686 */
687struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
688 int index)
689{
690 struct device_node *node = rdev->dev.of_node;
691 struct device_node *c_node;
692 struct regulator_dev *c_rdev;
693
694 c_node = of_parse_phandle(node, "regulator-coupled-with", index);
695 if (!c_node)
696 return NULL;
697
698 c_rdev = of_find_regulator_by_node(c_node);
699
700 of_node_put(c_node);
701
702 return c_rdev;
703}
704
705/*
706 * Check if name is a supply name according to the '*-supply' pattern
707 * return 0 if false
708 * return length of supply name without the -supply
709 */
710static int is_supply_name(const char *name)
711{
712 int strs, i;
713
714 strs = strlen(name);
715 /* string need to be at minimum len(x-supply) */
716 if (strs < 8)
717 return 0;
718 for (i = strs - 6; i > 0; i--) {
719 /* find first '-' and check if right part is supply */
720 if (name[i] != '-')
721 continue;
722 if (strcmp(name + i + 1, "supply") != 0)
723 return 0;
724 return i;
725 }
726 return 0;
727}
728
729/*
730 * of_regulator_bulk_get_all - get multiple regulator consumers
731 *
732 * @dev: Device to supply
733 * @np: device node to search for consumers
734 * @consumers: Configuration of consumers; clients are stored here.
735 *
736 * @return number of regulators on success, an errno on failure.
737 *
738 * This helper function allows drivers to get several regulator
739 * consumers in one operation. If any of the regulators cannot be
740 * acquired then any regulators that were allocated will be freed
741 * before returning to the caller.
742 */
743int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
744 struct regulator_bulk_data **consumers)
745{
746 int num_consumers = 0;
747 struct regulator *tmp;
748 struct property *prop;
749 int i, n = 0, ret;
750 char name[64];
751
752 *consumers = NULL;
753
754 /*
755 * first pass: get numbers of xxx-supply
756 * second pass: fill consumers
757 */
758restart:
759 for_each_property_of_node(np, prop) {
760 i = is_supply_name(prop->name);
761 if (i == 0)
762 continue;
763 if (!*consumers) {
764 num_consumers++;
765 continue;
766 } else {
767 memcpy(name, prop->name, i);
768 name[i] = '\0';
769 tmp = regulator_get(dev, name);
770 if (IS_ERR(tmp)) {
771 ret = -EINVAL;
772 goto error;
773 }
774 (*consumers)[n].consumer = tmp;
775 n++;
776 continue;
777 }
778 }
779 if (*consumers)
780 return num_consumers;
781 if (num_consumers == 0)
782 return 0;
783 *consumers = kmalloc_array(num_consumers,
784 sizeof(struct regulator_bulk_data),
785 GFP_KERNEL);
786 if (!*consumers)
787 return -ENOMEM;
788 goto restart;
789
790error:
791 while (--n >= 0)
792 regulator_put(consumers[n]->consumer);
793 return ret;
794}
795EXPORT_SYMBOL_GPL(of_regulator_bulk_get_all);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * OF helpers for regulator framework
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Rajendra Nayak <rnayak@ti.com>
7 */
8
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>
15
16#include "internal.h"
17
18static 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",
22};
23
24static void fill_limit(int *limit, int val)
25{
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;
33}
34
35static void of_get_regulator_prot_limits(struct device_node *np,
36 struct regulation_constraints *constraints)
37{
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",
45 };
46 struct notification_limit *limits[] = {
47 &constraints->over_curr_limits,
48 &constraints->over_voltage_limits,
49 &constraints->temp_limits,
50 &constraints->under_voltage_limits,
51 };
52 bool set[4] = {0};
53
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"
61 };
62 int *l[] = {
63 &limits[i]->prot, &limits[i]->err, &limits[i]->warn,
64 };
65
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;
72 }
73 }
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];
78}
79
80static 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)
84{
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;
92
93 n_phandles = of_count_phandle_with_args(np, "regulator-coupled-with",
94 NULL);
95 n_phandles = max(n_phandles, 0);
96
97 constraints->name = of_get_property(np, "regulator-name", NULL);
98
99 if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
100 constraints->min_uV = pval;
101
102 if (!of_property_read_u32(np, "regulator-max-microvolt", &pval))
103 constraints->max_uV = pval;
104
105 /* Voltage change possible? */
106 if (constraints->min_uV != constraints->max_uV)
107 constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
108
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;
112
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;
119
120 if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
121 &pval))
122 constraints->ilim_uA = pval;
123
124 /* Current change possible? */
125 if (constraints->min_uA != constraints->max_uA)
126 constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
127
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;
132
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");
136
137 if (of_property_read_bool(np, "regulator-allow-bypass"))
138 constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
139
140 if (of_property_read_bool(np, "regulator-allow-set-load"))
141 constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
142
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;
149 }
150
151 ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
152 if (!ret)
153 constraints->settling_time = pval;
154
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;
162 }
163
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;
172 }
173
174 ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
175 if (!ret)
176 constraints->enable_time = pval;
177
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;
184
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;
192 }
193
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);
204 }
205 }
206
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;
218 }
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;
225 }
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);
231 }
232 }
233
234 if (!of_property_read_u32(np, "regulator-system-load", &pval))
235 constraints->system_load = pval;
236
237 if (n_phandles) {
238 constraints->max_spread = devm_kzalloc(dev,
239 sizeof(*constraints->max_spread) * n_phandles,
240 GFP_KERNEL);
241
242 if (!constraints->max_spread)
243 return -ENOMEM;
244
245 of_property_read_u32_array(np, "regulator-coupled-max-spread",
246 constraints->max_spread, n_phandles);
247 }
248
249 if (!of_property_read_u32(np, "regulator-max-step-microvolt",
250 &pval))
251 constraints->max_uV_step = pval;
252
253 constraints->over_current_protection = of_property_read_bool(np,
254 "regulator-over-current-protection");
255
256 of_get_regulator_prot_limits(np, constraints);
257
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;
273 }
274
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;
281 }
282
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);
295 }
296 }
297
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;
304
305 if (!of_property_read_u32(suspend_np,
306 "regulator-suspend-min-microvolt", &pval))
307 suspend_state->min_uV = pval;
308
309 if (!of_property_read_u32(suspend_np,
310 "regulator-suspend-max-microvolt", &pval))
311 suspend_state->max_uV = pval;
312
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;
318
319 if (of_property_read_bool(suspend_np,
320 "regulator-changeable-in-suspend"))
321 suspend_state->changeable = true;
322
323 if (i == PM_SUSPEND_MEM)
324 constraints->initial_state = PM_SUSPEND_MEM;
325
326 of_node_put(suspend_np);
327 suspend_state = NULL;
328 suspend_np = NULL;
329 }
330
331 return 0;
332}
333
334/**
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
339 *
340 * Populates regulator_init_data structure by extracting data from device
341 * tree node, returns a pointer to the populated structure or NULL if memory
342 * alloc fails.
343 */
344struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
345 struct device_node *node,
346 const struct regulator_desc *desc)
347{
348 struct regulator_init_data *init_data;
349
350 if (!node)
351 return NULL;
352
353 init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
354 if (!init_data)
355 return NULL; /* Out of memory? */
356
357 if (of_get_regulation_constraints(dev, node, &init_data, desc))
358 return NULL;
359
360 return init_data;
361}
362EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
363
364struct devm_of_regulator_matches {
365 struct of_regulator_match *matches;
366 unsigned int num_matches;
367};
368
369static void devm_of_regulator_put_matches(struct device *dev, void *res)
370{
371 struct devm_of_regulator_matches *devm_matches = res;
372 int i;
373
374 for (i = 0; i < devm_matches->num_matches; i++)
375 of_node_put(devm_matches->matches[i].of_node);
376}
377
378/**
379 * of_regulator_match - extract multiple regulator init data from device tree.
380 * @dev: device requesting the data
381 * @node: parent device node of the regulators
382 * @matches: match table for the regulators
383 * @num_matches: number of entries in match table
384 *
385 * This function uses a match table specified by the regulator driver to
386 * parse regulator init data from the device tree. @node is expected to
387 * contain a set of child nodes, each providing the init data for one
388 * regulator. The data parsed from a child node will be matched to a regulator
389 * based on either the deprecated property regulator-compatible if present,
390 * or otherwise the child node's name. Note that the match table is modified
391 * in place and an additional of_node reference is taken for each matched
392 * regulator.
393 *
394 * Returns the number of matches found or a negative error code on failure.
395 */
396int of_regulator_match(struct device *dev, struct device_node *node,
397 struct of_regulator_match *matches,
398 unsigned int num_matches)
399{
400 unsigned int count = 0;
401 unsigned int i;
402 const char *name;
403 struct device_node *child;
404 struct devm_of_regulator_matches *devm_matches;
405
406 if (!dev || !node)
407 return -EINVAL;
408
409 devm_matches = devres_alloc(devm_of_regulator_put_matches,
410 sizeof(struct devm_of_regulator_matches),
411 GFP_KERNEL);
412 if (!devm_matches)
413 return -ENOMEM;
414
415 devm_matches->matches = matches;
416 devm_matches->num_matches = num_matches;
417
418 devres_add(dev, devm_matches);
419
420 for (i = 0; i < num_matches; i++) {
421 struct of_regulator_match *match = &matches[i];
422 match->init_data = NULL;
423 match->of_node = NULL;
424 }
425
426 for_each_child_of_node(node, child) {
427 name = of_get_property(child,
428 "regulator-compatible", NULL);
429 if (!name)
430 name = child->name;
431 for (i = 0; i < num_matches; i++) {
432 struct of_regulator_match *match = &matches[i];
433 if (match->of_node)
434 continue;
435
436 if (strcmp(match->name, name))
437 continue;
438
439 match->init_data =
440 of_get_regulator_init_data(dev, child,
441 match->desc);
442 if (!match->init_data) {
443 dev_err(dev,
444 "failed to parse DT for regulator %pOFn\n",
445 child);
446 of_node_put(child);
447 return -EINVAL;
448 }
449 match->of_node = of_node_get(child);
450 count++;
451 break;
452 }
453 }
454
455 return count;
456}
457EXPORT_SYMBOL_GPL(of_regulator_match);
458
459static struct
460device_node *regulator_of_get_init_node(struct device *dev,
461 const struct regulator_desc *desc)
462{
463 struct device_node *search, *child;
464 const char *name;
465
466 if (!dev->of_node || !desc->of_match)
467 return NULL;
468
469 if (desc->regulators_node) {
470 search = of_get_child_by_name(dev->of_node,
471 desc->regulators_node);
472 } else {
473 search = of_node_get(dev->of_node);
474
475 if (!strcmp(desc->of_match, search->name))
476 return search;
477 }
478
479 if (!search) {
480 dev_dbg(dev, "Failed to find regulator container node '%s'\n",
481 desc->regulators_node);
482 return NULL;
483 }
484
485 for_each_available_child_of_node(search, child) {
486 name = of_get_property(child, "regulator-compatible", NULL);
487 if (!name) {
488 if (!desc->of_match_full_name)
489 name = child->name;
490 else
491 name = child->full_name;
492 }
493
494 if (!strcmp(desc->of_match, name)) {
495 of_node_put(search);
496 /*
497 * 'of_node_get(child)' is already performed by the
498 * for_each loop.
499 */
500 return child;
501 }
502 }
503
504 of_node_put(search);
505
506 return NULL;
507}
508
509struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
510 const struct regulator_desc *desc,
511 struct regulator_config *config,
512 struct device_node **node)
513{
514 struct device_node *child;
515 struct regulator_init_data *init_data = NULL;
516
517 child = regulator_of_get_init_node(config->dev, desc);
518 if (!child)
519 return NULL;
520
521 init_data = of_get_regulator_init_data(dev, child, desc);
522 if (!init_data) {
523 dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
524 goto error;
525 }
526
527 if (desc->of_parse_cb) {
528 int ret;
529
530 ret = desc->of_parse_cb(child, desc, config);
531 if (ret) {
532 if (ret == -EPROBE_DEFER) {
533 of_node_put(child);
534 return ERR_PTR(-EPROBE_DEFER);
535 }
536 dev_err(dev,
537 "driver callback failed to parse DT for regulator %pOFn\n",
538 child);
539 goto error;
540 }
541 }
542
543 *node = child;
544
545 return init_data;
546
547error:
548 of_node_put(child);
549
550 return NULL;
551}
552
553struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
554{
555 struct device *dev;
556
557 dev = class_find_device_by_of_node(®ulator_class, np);
558
559 return dev ? dev_to_rdev(dev) : NULL;
560}
561
562/*
563 * Returns number of regulators coupled with rdev.
564 */
565int of_get_n_coupled(struct regulator_dev *rdev)
566{
567 struct device_node *node = rdev->dev.of_node;
568 int n_phandles;
569
570 n_phandles = of_count_phandle_with_args(node,
571 "regulator-coupled-with",
572 NULL);
573
574 return (n_phandles > 0) ? n_phandles : 0;
575}
576
577/* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
578static bool of_coupling_find_node(struct device_node *src,
579 struct device_node *to_find,
580 int *index)
581{
582 int n_phandles, i;
583 bool found = false;
584
585 n_phandles = of_count_phandle_with_args(src,
586 "regulator-coupled-with",
587 NULL);
588
589 for (i = 0; i < n_phandles; i++) {
590 struct device_node *tmp = of_parse_phandle(src,
591 "regulator-coupled-with", i);
592
593 if (!tmp)
594 break;
595
596 /* found */
597 if (tmp == to_find)
598 found = true;
599
600 of_node_put(tmp);
601
602 if (found) {
603 *index = i;
604 break;
605 }
606 }
607
608 return found;
609}
610
611/**
612 * of_check_coupling_data - Parse rdev's coupling properties and check data
613 * consistency
614 * @rdev: pointer to regulator_dev whose data is checked
615 *
616 * Function checks if all the following conditions are met:
617 * - rdev's max_spread is greater than 0
618 * - all coupled regulators have the same max_spread
619 * - all coupled regulators have the same number of regulator_dev phandles
620 * - all regulators are linked to each other
621 *
622 * Returns true if all conditions are met.
623 */
624bool of_check_coupling_data(struct regulator_dev *rdev)
625{
626 struct device_node *node = rdev->dev.of_node;
627 int n_phandles = of_get_n_coupled(rdev);
628 struct device_node *c_node;
629 int index;
630 int i;
631 bool ret = true;
632
633 /* iterate over rdev's phandles */
634 for (i = 0; i < n_phandles; i++) {
635 int max_spread = rdev->constraints->max_spread[i];
636 int c_max_spread, c_n_phandles;
637
638 if (max_spread <= 0) {
639 dev_err(&rdev->dev, "max_spread value invalid\n");
640 return false;
641 }
642
643 c_node = of_parse_phandle(node,
644 "regulator-coupled-with", i);
645
646 if (!c_node)
647 ret = false;
648
649 c_n_phandles = of_count_phandle_with_args(c_node,
650 "regulator-coupled-with",
651 NULL);
652
653 if (c_n_phandles != n_phandles) {
654 dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
655 ret = false;
656 goto clean;
657 }
658
659 if (!of_coupling_find_node(c_node, node, &index)) {
660 dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
661 ret = false;
662 goto clean;
663 }
664
665 if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread",
666 index, &c_max_spread)) {
667 ret = false;
668 goto clean;
669 }
670
671 if (c_max_spread != max_spread) {
672 dev_err(&rdev->dev,
673 "coupled regulators max_spread mismatch\n");
674 ret = false;
675 goto clean;
676 }
677
678clean:
679 of_node_put(c_node);
680 if (!ret)
681 break;
682 }
683
684 return ret;
685}
686
687/**
688 * of_parse_coupled_regulator() - Get regulator_dev pointer from rdev's property
689 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
690 * "regulator-coupled-with" property
691 * @index: Index in phandles array
692 *
693 * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
694 * registered, returns NULL
695 */
696struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
697 int index)
698{
699 struct device_node *node = rdev->dev.of_node;
700 struct device_node *c_node;
701 struct regulator_dev *c_rdev;
702
703 c_node = of_parse_phandle(node, "regulator-coupled-with", index);
704 if (!c_node)
705 return NULL;
706
707 c_rdev = of_find_regulator_by_node(c_node);
708
709 of_node_put(c_node);
710
711 return c_rdev;
712}
713
714/*
715 * Check if name is a supply name according to the '*-supply' pattern
716 * return 0 if false
717 * return length of supply name without the -supply
718 */
719static int is_supply_name(const char *name)
720{
721 int strs, i;
722
723 strs = strlen(name);
724 /* string need to be at minimum len(x-supply) */
725 if (strs < 8)
726 return 0;
727 for (i = strs - 6; i > 0; i--) {
728 /* find first '-' and check if right part is supply */
729 if (name[i] != '-')
730 continue;
731 if (strcmp(name + i + 1, "supply") != 0)
732 return 0;
733 return i;
734 }
735 return 0;
736}
737
738/*
739 * of_regulator_bulk_get_all - get multiple regulator consumers
740 *
741 * @dev: Device to supply
742 * @np: device node to search for consumers
743 * @consumers: Configuration of consumers; clients are stored here.
744 *
745 * @return number of regulators on success, an errno on failure.
746 *
747 * This helper function allows drivers to get several regulator
748 * consumers in one operation. If any of the regulators cannot be
749 * acquired then any regulators that were allocated will be freed
750 * before returning to the caller.
751 */
752int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
753 struct regulator_bulk_data **consumers)
754{
755 int num_consumers = 0;
756 struct regulator *tmp;
757 struct property *prop;
758 int i, n = 0, ret;
759 char name[64];
760
761 *consumers = NULL;
762
763 /*
764 * first pass: get numbers of xxx-supply
765 * second pass: fill consumers
766 */
767restart:
768 for_each_property_of_node(np, prop) {
769 i = is_supply_name(prop->name);
770 if (i == 0)
771 continue;
772 if (!*consumers) {
773 num_consumers++;
774 continue;
775 } else {
776 memcpy(name, prop->name, i);
777 name[i] = '\0';
778 tmp = regulator_get(dev, name);
779 if (IS_ERR(tmp)) {
780 ret = -EINVAL;
781 goto error;
782 }
783 (*consumers)[n].consumer = tmp;
784 n++;
785 continue;
786 }
787 }
788 if (*consumers)
789 return num_consumers;
790 if (num_consumers == 0)
791 return 0;
792 *consumers = kmalloc_array(num_consumers,
793 sizeof(struct regulator_bulk_data),
794 GFP_KERNEL);
795 if (!*consumers)
796 return -ENOMEM;
797 goto restart;
798
799error:
800 while (--n >= 0)
801 regulator_put(consumers[n]->consumer);
802 return ret;
803}
804EXPORT_SYMBOL_GPL(of_regulator_bulk_get_all);