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
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",
22 };
23
fill_limit(int * limit,int val)24 static 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
of_get_regulator_prot_limits(struct device_node * np,struct regulation_constraints * constraints)35 static 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
of_get_regulation_constraints(struct device * dev,struct device_node * np,struct regulator_init_data ** init_data,const struct regulator_desc * desc)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)
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 if (!of_property_read_u32(np, "regulator-power-budget-milliwatt", &pval))
129 constraints->pw_budget_mW = pval;
130
131 constraints->boot_on = of_property_read_bool(np, "regulator-boot-on");
132 constraints->always_on = of_property_read_bool(np, "regulator-always-on");
133 if (!constraints->always_on) /* status change should be possible. */
134 constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
135
136 constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
137 constraints->system_critical = of_property_read_bool(np,
138 "system-critical-regulator");
139
140 if (of_property_read_bool(np, "regulator-allow-bypass"))
141 constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
142
143 if (of_property_read_bool(np, "regulator-allow-set-load"))
144 constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
145
146 ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
147 if (!ret) {
148 if (pval)
149 constraints->ramp_delay = pval;
150 else
151 constraints->ramp_disable = true;
152 }
153
154 ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
155 if (!ret)
156 constraints->settling_time = pval;
157
158 ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval);
159 if (!ret)
160 constraints->settling_time_up = pval;
161 if (constraints->settling_time_up && constraints->settling_time) {
162 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
163 np);
164 constraints->settling_time_up = 0;
165 }
166
167 ret = of_property_read_u32(np, "regulator-settling-time-down-us",
168 &pval);
169 if (!ret)
170 constraints->settling_time_down = pval;
171 if (constraints->settling_time_down && constraints->settling_time) {
172 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
173 np);
174 constraints->settling_time_down = 0;
175 }
176
177 ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
178 if (!ret)
179 constraints->enable_time = pval;
180
181 ret = of_property_read_u32(np, "regulator-uv-less-critical-window-ms", &pval);
182 if (!ret)
183 constraints->uv_less_critical_window_ms = pval;
184 else
185 constraints->uv_less_critical_window_ms =
186 REGULATOR_DEF_UV_LESS_CRITICAL_WINDOW_MS;
187
188 constraints->soft_start = of_property_read_bool(np,
189 "regulator-soft-start");
190 ret = of_property_read_u32(np, "regulator-active-discharge", &pval);
191 if (!ret) {
192 constraints->active_discharge =
193 (pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE :
194 REGULATOR_ACTIVE_DISCHARGE_DISABLE;
195 }
196
197 if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) {
198 if (desc && desc->of_map_mode) {
199 mode = desc->of_map_mode(pval);
200 if (mode == REGULATOR_MODE_INVALID)
201 pr_err("%pOFn: invalid mode %u\n", np, pval);
202 else
203 constraints->initial_mode = mode;
204 } else {
205 pr_warn("%pOFn: mapping for mode %d not defined\n",
206 np, pval);
207 }
208 }
209
210 len = of_property_count_elems_of_size(np, "regulator-allowed-modes",
211 sizeof(u32));
212 if (len > 0) {
213 if (desc && desc->of_map_mode) {
214 for (i = 0; i < len; i++) {
215 ret = of_property_read_u32_index(np,
216 "regulator-allowed-modes", i, &pval);
217 if (ret) {
218 pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
219 np, i, ret);
220 break;
221 }
222 mode = desc->of_map_mode(pval);
223 if (mode == REGULATOR_MODE_INVALID)
224 pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
225 np, pval);
226 else
227 constraints->valid_modes_mask |= mode;
228 }
229 if (constraints->valid_modes_mask)
230 constraints->valid_ops_mask
231 |= REGULATOR_CHANGE_MODE;
232 } else {
233 pr_warn("%pOFn: mode mapping not defined\n", np);
234 }
235 }
236
237 if (!of_property_read_u32(np, "regulator-system-load", &pval))
238 constraints->system_load = pval;
239
240 if (n_phandles) {
241 constraints->max_spread = devm_kzalloc(dev,
242 sizeof(*constraints->max_spread) * n_phandles,
243 GFP_KERNEL);
244
245 if (!constraints->max_spread)
246 return -ENOMEM;
247
248 of_property_read_u32_array(np, "regulator-coupled-max-spread",
249 constraints->max_spread, n_phandles);
250 }
251
252 if (!of_property_read_u32(np, "regulator-max-step-microvolt",
253 &pval))
254 constraints->max_uV_step = pval;
255
256 constraints->over_current_protection = of_property_read_bool(np,
257 "regulator-over-current-protection");
258
259 of_get_regulator_prot_limits(np, constraints);
260
261 for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
262 switch (i) {
263 case PM_SUSPEND_MEM:
264 suspend_state = &constraints->state_mem;
265 break;
266 case PM_SUSPEND_MAX:
267 suspend_state = &constraints->state_disk;
268 break;
269 case PM_SUSPEND_STANDBY:
270 suspend_state = &constraints->state_standby;
271 break;
272 case PM_SUSPEND_ON:
273 case PM_SUSPEND_TO_IDLE:
274 default:
275 continue;
276 }
277
278 suspend_np = of_get_child_by_name(np, regulator_states[i]);
279 if (!suspend_np)
280 continue;
281 if (!suspend_state) {
282 of_node_put(suspend_np);
283 continue;
284 }
285
286 if (!of_property_read_u32(suspend_np, "regulator-mode",
287 &pval)) {
288 if (desc && desc->of_map_mode) {
289 mode = desc->of_map_mode(pval);
290 if (mode == REGULATOR_MODE_INVALID)
291 pr_err("%pOFn: invalid mode %u\n",
292 np, pval);
293 else
294 suspend_state->mode = mode;
295 } else {
296 pr_warn("%pOFn: mapping for mode %d not defined\n",
297 np, pval);
298 }
299 }
300
301 if (of_property_read_bool(suspend_np,
302 "regulator-on-in-suspend"))
303 suspend_state->enabled = ENABLE_IN_SUSPEND;
304 else if (of_property_read_bool(suspend_np,
305 "regulator-off-in-suspend"))
306 suspend_state->enabled = DISABLE_IN_SUSPEND;
307
308 if (!of_property_read_u32(suspend_np,
309 "regulator-suspend-min-microvolt", &pval))
310 suspend_state->min_uV = pval;
311
312 if (!of_property_read_u32(suspend_np,
313 "regulator-suspend-max-microvolt", &pval))
314 suspend_state->max_uV = pval;
315
316 if (!of_property_read_u32(suspend_np,
317 "regulator-suspend-microvolt", &pval))
318 suspend_state->uV = pval;
319 else /* otherwise use min_uV as default suspend voltage */
320 suspend_state->uV = suspend_state->min_uV;
321
322 if (of_property_read_bool(suspend_np,
323 "regulator-changeable-in-suspend"))
324 suspend_state->changeable = true;
325
326 if (i == PM_SUSPEND_MEM)
327 constraints->initial_state = PM_SUSPEND_MEM;
328
329 of_node_put(suspend_np);
330 suspend_state = NULL;
331 suspend_np = NULL;
332 }
333
334 return 0;
335 }
336
337 /**
338 * of_get_regulator_init_data - extract regulator_init_data structure info
339 * @dev: device requesting for regulator_init_data
340 * @node: regulator device node
341 * @desc: regulator description
342 *
343 * Populates regulator_init_data structure by extracting data from device
344 * tree node.
345 *
346 * Return: Pointer to a populated &struct regulator_init_data or NULL if
347 * memory allocation fails.
348 */
of_get_regulator_init_data(struct device * dev,struct device_node * node,const struct regulator_desc * desc)349 struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
350 struct device_node *node,
351 const struct regulator_desc *desc)
352 {
353 struct regulator_init_data *init_data;
354
355 if (!node)
356 return NULL;
357
358 init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
359 if (!init_data)
360 return NULL; /* Out of memory? */
361
362 if (of_get_regulation_constraints(dev, node, &init_data, desc))
363 return NULL;
364
365 return init_data;
366 }
367 EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
368
369 struct devm_of_regulator_matches {
370 struct of_regulator_match *matches;
371 unsigned int num_matches;
372 };
373
devm_of_regulator_put_matches(struct device * dev,void * res)374 static void devm_of_regulator_put_matches(struct device *dev, void *res)
375 {
376 struct devm_of_regulator_matches *devm_matches = res;
377 int i;
378
379 for (i = 0; i < devm_matches->num_matches; i++)
380 of_node_put(devm_matches->matches[i].of_node);
381 }
382
383 /**
384 * of_regulator_match - extract multiple regulator init data from device tree.
385 * @dev: device requesting the data
386 * @node: parent device node of the regulators
387 * @matches: match table for the regulators
388 * @num_matches: number of entries in match table
389 *
390 * This function uses a match table specified by the regulator driver to
391 * parse regulator init data from the device tree. @node is expected to
392 * contain a set of child nodes, each providing the init data for one
393 * regulator. The data parsed from a child node will be matched to a regulator
394 * based on either the deprecated property regulator-compatible if present,
395 * or otherwise the child node's name. Note that the match table is modified
396 * in place and an additional of_node reference is taken for each matched
397 * regulator.
398 *
399 * Return: The number of matches found or a negative error number on failure.
400 */
of_regulator_match(struct device * dev,struct device_node * node,struct of_regulator_match * matches,unsigned int num_matches)401 int of_regulator_match(struct device *dev, struct device_node *node,
402 struct of_regulator_match *matches,
403 unsigned int num_matches)
404 {
405 unsigned int count = 0;
406 unsigned int i;
407 const char *name;
408 struct device_node *child;
409 struct devm_of_regulator_matches *devm_matches;
410
411 if (!dev || !node)
412 return -EINVAL;
413
414 devm_matches = devres_alloc(devm_of_regulator_put_matches,
415 sizeof(struct devm_of_regulator_matches),
416 GFP_KERNEL);
417 if (!devm_matches)
418 return -ENOMEM;
419
420 devm_matches->matches = matches;
421 devm_matches->num_matches = num_matches;
422
423 devres_add(dev, devm_matches);
424
425 for (i = 0; i < num_matches; i++) {
426 struct of_regulator_match *match = &matches[i];
427 match->init_data = NULL;
428 match->of_node = NULL;
429 }
430
431 for_each_child_of_node(node, child) {
432 name = of_get_property(child,
433 "regulator-compatible", NULL);
434 if (!name)
435 name = child->name;
436 for (i = 0; i < num_matches; i++) {
437 struct of_regulator_match *match = &matches[i];
438 if (match->of_node)
439 continue;
440
441 if (strcmp(match->name, name))
442 continue;
443
444 match->init_data =
445 of_get_regulator_init_data(dev, child,
446 match->desc);
447 if (!match->init_data) {
448 dev_err(dev,
449 "failed to parse DT for regulator %pOFn\n",
450 child);
451 of_node_put(child);
452 goto err_put;
453 }
454 match->of_node = of_node_get(child);
455 count++;
456 break;
457 }
458 }
459
460 return count;
461
462 err_put:
463 for (i = 0; i < num_matches; i++) {
464 struct of_regulator_match *match = &matches[i];
465
466 match->init_data = NULL;
467 if (match->of_node) {
468 of_node_put(match->of_node);
469 match->of_node = NULL;
470 }
471 }
472 return -EINVAL;
473 }
474 EXPORT_SYMBOL_GPL(of_regulator_match);
475
476 static struct
regulator_of_get_init_node(struct device * dev,const struct regulator_desc * desc)477 device_node *regulator_of_get_init_node(struct device *dev,
478 const struct regulator_desc *desc)
479 {
480 struct device_node *search, *child;
481 const char *name;
482
483 if (!dev->of_node || !desc->of_match)
484 return NULL;
485
486 if (desc->regulators_node) {
487 search = of_get_child_by_name(dev->of_node,
488 desc->regulators_node);
489 } else {
490 search = of_node_get(dev->of_node);
491
492 if (!strcmp(desc->of_match, search->name))
493 return search;
494 }
495
496 if (!search) {
497 dev_dbg(dev, "Failed to find regulator container node '%s'\n",
498 desc->regulators_node);
499 return NULL;
500 }
501
502 for_each_available_child_of_node(search, child) {
503 name = of_get_property(child, "regulator-compatible", NULL);
504 if (!name) {
505 if (!desc->of_match_full_name)
506 name = child->name;
507 else
508 name = child->full_name;
509 }
510
511 if (!strcmp(desc->of_match, name)) {
512 of_node_put(search);
513 /*
514 * 'of_node_get(child)' is already performed by the
515 * for_each loop.
516 */
517 return child;
518 }
519 }
520
521 of_node_put(search);
522
523 return NULL;
524 }
525
regulator_of_get_init_data(struct device * dev,const struct regulator_desc * desc,struct regulator_config * config,struct device_node ** node)526 struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
527 const struct regulator_desc *desc,
528 struct regulator_config *config,
529 struct device_node **node)
530 {
531 struct device_node *child;
532 struct regulator_init_data *init_data = NULL;
533
534 child = regulator_of_get_init_node(config->dev, desc);
535 if (!child)
536 return NULL;
537
538 init_data = of_get_regulator_init_data(dev, child, desc);
539 if (!init_data) {
540 dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
541 goto error;
542 }
543
544 if (desc->of_parse_cb) {
545 int ret;
546
547 ret = desc->of_parse_cb(child, desc, config);
548 if (ret) {
549 if (ret == -EPROBE_DEFER) {
550 of_node_put(child);
551 return ERR_PTR(-EPROBE_DEFER);
552 }
553 dev_err(dev,
554 "driver callback failed to parse DT for regulator %pOFn\n",
555 child);
556 goto error;
557 }
558 }
559
560 *node = child;
561
562 return init_data;
563
564 error:
565 of_node_put(child);
566
567 return NULL;
568 }
569
570 /**
571 * of_get_child_regulator - get a child regulator device node
572 * based on supply name
573 * @parent: Parent device node
574 * @prop_name: Combination regulator supply name and "-supply"
575 *
576 * Traverse all child nodes.
577 * Extract the child regulator device node corresponding to the supply name.
578 *
579 * Return: Pointer to the &struct device_node corresponding to the regulator
580 * if found, or %NULL if not found.
581 */
of_get_child_regulator(struct device_node * parent,const char * prop_name)582 static struct device_node *of_get_child_regulator(struct device_node *parent,
583 const char *prop_name)
584 {
585 struct device_node *regnode = NULL;
586 struct device_node *child = NULL;
587
588 for_each_child_of_node(parent, child) {
589 regnode = of_parse_phandle(child, prop_name, 0);
590 if (regnode)
591 goto err_node_put;
592
593 regnode = of_get_child_regulator(child, prop_name);
594 if (regnode)
595 goto err_node_put;
596 }
597 return NULL;
598
599 err_node_put:
600 of_node_put(child);
601 return regnode;
602 }
603
604 /**
605 * of_get_regulator - get a regulator device node based on supply name
606 * @dev: Device pointer for dev_printk() messages
607 * @node: Device node pointer for supply property lookup
608 * @supply: regulator supply name
609 *
610 * Extract the regulator device node corresponding to the supply name.
611 *
612 * Return: Pointer to the &struct device_node corresponding to the regulator
613 * if found, or %NULL if not found.
614 */
of_get_regulator(struct device * dev,struct device_node * node,const char * supply)615 static struct device_node *of_get_regulator(struct device *dev, struct device_node *node,
616 const char *supply)
617 {
618 struct device_node *regnode = NULL;
619 char prop_name[64]; /* 64 is max size of property name */
620
621 dev_dbg(dev, "Looking up %s-supply from device node %pOF\n", supply, node);
622
623 snprintf(prop_name, 64, "%s-supply", supply);
624 regnode = of_parse_phandle(node, prop_name, 0);
625 if (regnode)
626 return regnode;
627
628 regnode = of_get_child_regulator(dev->of_node, prop_name);
629 if (regnode)
630 return regnode;
631
632 dev_dbg(dev, "Looking up %s property in node %pOF failed\n", prop_name, dev->of_node);
633 return NULL;
634 }
635
of_find_regulator_by_node(struct device_node * np)636 static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
637 {
638 struct device *dev;
639
640 dev = class_find_device_by_of_node(®ulator_class, np);
641
642 return dev ? dev_to_rdev(dev) : NULL;
643 }
644
645 /**
646 * of_regulator_dev_lookup - lookup a regulator device with device tree only
647 * @dev: Device pointer for regulator supply lookup.
648 * @np: Device node pointer for regulator supply lookup.
649 * @supply: Supply name or regulator ID.
650 *
651 * Return: Pointer to the &struct regulator_dev on success, or ERR_PTR()
652 * encoded value on error.
653 *
654 * If successful, returns a pointer to the &struct regulator_dev that
655 * corresponds to the name @supply and with the embedded &struct device
656 * refcount incremented by one. The refcount must be dropped by calling
657 * put_device().
658 *
659 * On failure one of the following ERR_PTR() encoded values is returned:
660 * * -%ENODEV if lookup fails permanently.
661 * * -%EPROBE_DEFER if lookup could succeed in the future.
662 */
of_regulator_dev_lookup(struct device * dev,struct device_node * np,const char * supply)663 struct regulator_dev *of_regulator_dev_lookup(struct device *dev, struct device_node *np,
664 const char *supply)
665 {
666 struct regulator_dev *r;
667 struct device_node *node;
668
669 node = of_get_regulator(dev, np, supply);
670 if (node) {
671 r = of_find_regulator_by_node(node);
672 of_node_put(node);
673 if (r)
674 return r;
675
676 /*
677 * We have a node, but there is no device.
678 * assume it has not registered yet.
679 */
680 return ERR_PTR(-EPROBE_DEFER);
681 }
682
683 return ERR_PTR(-ENODEV);
684 }
685
_of_regulator_get(struct device * dev,struct device_node * node,const char * id,enum regulator_get_type get_type)686 struct regulator *_of_regulator_get(struct device *dev, struct device_node *node,
687 const char *id, enum regulator_get_type get_type)
688 {
689 struct regulator_dev *r;
690 int ret;
691
692 ret = _regulator_get_common_check(dev, id, get_type);
693 if (ret)
694 return ERR_PTR(ret);
695
696 r = of_regulator_dev_lookup(dev, node, id);
697 return _regulator_get_common(r, dev, id, get_type);
698 }
699
700 /**
701 * of_regulator_get - get regulator via device tree lookup
702 * @dev: device used for dev_printk() messages
703 * @node: device node for regulator "consumer"
704 * @id: Supply name
705 *
706 * Return: pointer to struct regulator corresponding to the regulator producer,
707 * or PTR_ERR() encoded error number.
708 *
709 * This is intended for use by consumers that want to get a regulator
710 * supply directly from a device node. This will _not_ consider supply
711 * aliases. See regulator_dev_lookup().
712 */
of_regulator_get(struct device * dev,struct device_node * node,const char * id)713 struct regulator *of_regulator_get(struct device *dev,
714 struct device_node *node,
715 const char *id)
716 {
717 return _of_regulator_get(dev, node, id, NORMAL_GET);
718 }
719 EXPORT_SYMBOL_GPL(of_regulator_get);
720
721 /**
722 * of_regulator_get_optional - get optional regulator via device tree lookup
723 * @dev: device used for dev_printk() messages
724 * @node: device node for regulator "consumer"
725 * @id: Supply name
726 *
727 * Return: pointer to struct regulator corresponding to the regulator producer,
728 * or PTR_ERR() encoded error number.
729 *
730 * This is intended for use by consumers that want to get a regulator
731 * supply directly from a device node, and can and want to deal with
732 * absence of such supplies. This will _not_ consider supply aliases.
733 * See regulator_dev_lookup().
734 */
of_regulator_get_optional(struct device * dev,struct device_node * node,const char * id)735 struct regulator *of_regulator_get_optional(struct device *dev,
736 struct device_node *node,
737 const char *id)
738 {
739 return _of_regulator_get(dev, node, id, OPTIONAL_GET);
740 }
741 EXPORT_SYMBOL_GPL(of_regulator_get_optional);
742
743 /*
744 * Returns number of regulators coupled with rdev.
745 */
of_get_n_coupled(struct regulator_dev * rdev)746 int of_get_n_coupled(struct regulator_dev *rdev)
747 {
748 struct device_node *node = rdev->dev.of_node;
749 int n_phandles;
750
751 n_phandles = of_count_phandle_with_args(node,
752 "regulator-coupled-with",
753 NULL);
754
755 return (n_phandles > 0) ? n_phandles : 0;
756 }
757
758 /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
of_coupling_find_node(struct device_node * src,struct device_node * to_find,int * index)759 static bool of_coupling_find_node(struct device_node *src,
760 struct device_node *to_find,
761 int *index)
762 {
763 int n_phandles, i;
764 bool found = false;
765
766 n_phandles = of_count_phandle_with_args(src,
767 "regulator-coupled-with",
768 NULL);
769
770 for (i = 0; i < n_phandles; i++) {
771 struct device_node *tmp = of_parse_phandle(src,
772 "regulator-coupled-with", i);
773
774 if (!tmp)
775 break;
776
777 /* found */
778 if (tmp == to_find)
779 found = true;
780
781 of_node_put(tmp);
782
783 if (found) {
784 *index = i;
785 break;
786 }
787 }
788
789 return found;
790 }
791
792 /**
793 * of_check_coupling_data - Parse rdev's coupling properties and check data
794 * consistency
795 * @rdev: pointer to regulator_dev whose data is checked
796 *
797 * Function checks if all the following conditions are met:
798 * - rdev's max_spread is greater than 0
799 * - all coupled regulators have the same max_spread
800 * - all coupled regulators have the same number of regulator_dev phandles
801 * - all regulators are linked to each other
802 *
803 * Return: True if all conditions are met; false otherwise.
804 */
of_check_coupling_data(struct regulator_dev * rdev)805 bool of_check_coupling_data(struct regulator_dev *rdev)
806 {
807 struct device_node *node = rdev->dev.of_node;
808 int n_phandles = of_get_n_coupled(rdev);
809 struct device_node *c_node;
810 int index;
811 int i;
812 bool ret = true;
813
814 /* iterate over rdev's phandles */
815 for (i = 0; i < n_phandles; i++) {
816 int max_spread = rdev->constraints->max_spread[i];
817 int c_max_spread, c_n_phandles;
818
819 if (max_spread <= 0) {
820 dev_err(&rdev->dev, "max_spread value invalid\n");
821 return false;
822 }
823
824 c_node = of_parse_phandle(node,
825 "regulator-coupled-with", i);
826
827 if (!c_node)
828 ret = false;
829
830 c_n_phandles = of_count_phandle_with_args(c_node,
831 "regulator-coupled-with",
832 NULL);
833
834 if (c_n_phandles != n_phandles) {
835 dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
836 ret = false;
837 goto clean;
838 }
839
840 if (!of_coupling_find_node(c_node, node, &index)) {
841 dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
842 ret = false;
843 goto clean;
844 }
845
846 if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread",
847 index, &c_max_spread)) {
848 ret = false;
849 goto clean;
850 }
851
852 if (c_max_spread != max_spread) {
853 dev_err(&rdev->dev,
854 "coupled regulators max_spread mismatch\n");
855 ret = false;
856 goto clean;
857 }
858
859 clean:
860 of_node_put(c_node);
861 if (!ret)
862 break;
863 }
864
865 return ret;
866 }
867
868 /**
869 * of_parse_coupled_regulator() - Get regulator_dev pointer from rdev's property
870 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
871 * "regulator-coupled-with" property
872 * @index: Index in phandles array
873 *
874 * Return: Pointer to the &struct regulator_dev parsed from DTS, or %NULL if
875 * it has not yet been registered.
876 */
of_parse_coupled_regulator(struct regulator_dev * rdev,int index)877 struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
878 int index)
879 {
880 struct device_node *node = rdev->dev.of_node;
881 struct device_node *c_node;
882 struct regulator_dev *c_rdev;
883
884 c_node = of_parse_phandle(node, "regulator-coupled-with", index);
885 if (!c_node)
886 return NULL;
887
888 c_rdev = of_find_regulator_by_node(c_node);
889
890 of_node_put(c_node);
891
892 return c_rdev;
893 }
894
895 /*
896 * Check if name is a supply name according to the '*-supply' pattern
897 * return 0 if false
898 * return length of supply name without the -supply
899 */
is_supply_name(const char * name)900 static int is_supply_name(const char *name)
901 {
902 int strs, i;
903
904 strs = strlen(name);
905 /* string need to be at minimum len(x-supply) */
906 if (strs < 8)
907 return 0;
908 for (i = strs - 6; i > 0; i--) {
909 /* find first '-' and check if right part is supply */
910 if (name[i] != '-')
911 continue;
912 if (strcmp(name + i + 1, "supply") != 0)
913 return 0;
914 return i;
915 }
916 return 0;
917 }
918
919 /**
920 * of_regulator_bulk_get_all - get multiple regulator consumers
921 *
922 * @dev: Device to supply
923 * @np: device node to search for consumers
924 * @consumers: Configuration of consumers; clients are stored here.
925 *
926 * This helper function allows drivers to get several regulator
927 * consumers in one operation. If any of the regulators cannot be
928 * acquired then any regulators that were allocated will be freed
929 * before returning to the caller, and @consumers will not be
930 * changed.
931 *
932 * Return: Number of regulators on success, or a negative error number
933 * on failure.
934 */
of_regulator_bulk_get_all(struct device * dev,struct device_node * np,struct regulator_bulk_data ** consumers)935 int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
936 struct regulator_bulk_data **consumers)
937 {
938 int num_consumers = 0;
939 struct regulator *tmp;
940 struct regulator_bulk_data *_consumers = NULL;
941 struct property *prop;
942 int i, n = 0, ret;
943 char name[64];
944
945 /*
946 * first pass: get numbers of xxx-supply
947 * second pass: fill consumers
948 */
949 restart:
950 for_each_property_of_node(np, prop) {
951 i = is_supply_name(prop->name);
952 if (i == 0)
953 continue;
954 if (!_consumers) {
955 num_consumers++;
956 continue;
957 } else {
958 memcpy(name, prop->name, i);
959 name[i] = '\0';
960 tmp = regulator_get(dev, name);
961 if (IS_ERR(tmp)) {
962 ret = PTR_ERR(tmp);
963 goto error;
964 }
965 _consumers[n].consumer = tmp;
966 n++;
967 continue;
968 }
969 }
970 if (_consumers) {
971 *consumers = _consumers;
972 return num_consumers;
973 }
974 if (num_consumers == 0)
975 return 0;
976 _consumers = kmalloc_array(num_consumers,
977 sizeof(struct regulator_bulk_data),
978 GFP_KERNEL);
979 if (!_consumers)
980 return -ENOMEM;
981 goto restart;
982
983 error:
984 while (--n >= 0)
985 regulator_put(_consumers[n].consumer);
986 kfree(_consumers);
987 return ret;
988 }
989 EXPORT_SYMBOL_GPL(of_regulator_bulk_get_all);
990