xref: /openbmc/linux/drivers/regulator/of_regulator.c (revision 060f35a317ef09101b128f399dce7ed13d019461)
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 	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  */
of_get_regulator_init_data(struct device * dev,struct device_node * node,const struct regulator_desc * desc)335 struct 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 }
353 EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
354 
355 struct devm_of_regulator_matches {
356 	struct of_regulator_match *matches;
357 	unsigned int num_matches;
358 };
359 
devm_of_regulator_put_matches(struct device * dev,void * res)360 static 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  */
of_regulator_match(struct device * dev,struct device_node * node,struct of_regulator_match * matches,unsigned int num_matches)387 int 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 				goto err_put;
439 			}
440 			match->of_node = of_node_get(child);
441 			count++;
442 			break;
443 		}
444 	}
445 
446 	return count;
447 
448 err_put:
449 	for (i = 0; i < num_matches; i++) {
450 		struct of_regulator_match *match = &matches[i];
451 
452 		match->init_data = NULL;
453 		if (match->of_node) {
454 			of_node_put(match->of_node);
455 			match->of_node = NULL;
456 		}
457 	}
458 	return -EINVAL;
459 }
460 EXPORT_SYMBOL_GPL(of_regulator_match);
461 
462 static struct
regulator_of_get_init_node(struct device * dev,const struct regulator_desc * desc)463 device_node *regulator_of_get_init_node(struct device *dev,
464 					const struct regulator_desc *desc)
465 {
466 	struct device_node *search, *child;
467 	const char *name;
468 
469 	if (!dev->of_node || !desc->of_match)
470 		return NULL;
471 
472 	if (desc->regulators_node) {
473 		search = of_get_child_by_name(dev->of_node,
474 					      desc->regulators_node);
475 	} else {
476 		search = of_node_get(dev->of_node);
477 
478 		if (!strcmp(desc->of_match, search->name))
479 			return search;
480 	}
481 
482 	if (!search) {
483 		dev_dbg(dev, "Failed to find regulator container node '%s'\n",
484 			desc->regulators_node);
485 		return NULL;
486 	}
487 
488 	for_each_available_child_of_node(search, child) {
489 		name = of_get_property(child, "regulator-compatible", NULL);
490 		if (!name) {
491 			if (!desc->of_match_full_name)
492 				name = child->name;
493 			else
494 				name = child->full_name;
495 		}
496 
497 		if (!strcmp(desc->of_match, name)) {
498 			of_node_put(search);
499 			/*
500 			 * 'of_node_get(child)' is already performed by the
501 			 * for_each loop.
502 			 */
503 			return child;
504 		}
505 	}
506 
507 	of_node_put(search);
508 
509 	return NULL;
510 }
511 
regulator_of_get_init_data(struct device * dev,const struct regulator_desc * desc,struct regulator_config * config,struct device_node ** node)512 struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
513 					    const struct regulator_desc *desc,
514 					    struct regulator_config *config,
515 					    struct device_node **node)
516 {
517 	struct device_node *child;
518 	struct regulator_init_data *init_data = NULL;
519 
520 	child = regulator_of_get_init_node(config->dev, desc);
521 	if (!child)
522 		return NULL;
523 
524 	init_data = of_get_regulator_init_data(dev, child, desc);
525 	if (!init_data) {
526 		dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
527 		goto error;
528 	}
529 
530 	if (desc->of_parse_cb) {
531 		int ret;
532 
533 		ret = desc->of_parse_cb(child, desc, config);
534 		if (ret) {
535 			if (ret == -EPROBE_DEFER) {
536 				of_node_put(child);
537 				return ERR_PTR(-EPROBE_DEFER);
538 			}
539 			dev_err(dev,
540 				"driver callback failed to parse DT for regulator %pOFn\n",
541 				child);
542 			goto error;
543 		}
544 	}
545 
546 	*node = child;
547 
548 	return init_data;
549 
550 error:
551 	of_node_put(child);
552 
553 	return NULL;
554 }
555 
of_find_regulator_by_node(struct device_node * np)556 struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
557 {
558 	struct device *dev;
559 
560 	dev = class_find_device_by_of_node(&regulator_class, np);
561 
562 	return dev ? dev_to_rdev(dev) : NULL;
563 }
564 
565 /*
566  * Returns number of regulators coupled with rdev.
567  */
of_get_n_coupled(struct regulator_dev * rdev)568 int of_get_n_coupled(struct regulator_dev *rdev)
569 {
570 	struct device_node *node = rdev->dev.of_node;
571 	int n_phandles;
572 
573 	n_phandles = of_count_phandle_with_args(node,
574 						"regulator-coupled-with",
575 						NULL);
576 
577 	return (n_phandles > 0) ? n_phandles : 0;
578 }
579 
580 /* 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)581 static bool of_coupling_find_node(struct device_node *src,
582 				  struct device_node *to_find,
583 				  int *index)
584 {
585 	int n_phandles, i;
586 	bool found = false;
587 
588 	n_phandles = of_count_phandle_with_args(src,
589 						"regulator-coupled-with",
590 						NULL);
591 
592 	for (i = 0; i < n_phandles; i++) {
593 		struct device_node *tmp = of_parse_phandle(src,
594 					   "regulator-coupled-with", i);
595 
596 		if (!tmp)
597 			break;
598 
599 		/* found */
600 		if (tmp == to_find)
601 			found = true;
602 
603 		of_node_put(tmp);
604 
605 		if (found) {
606 			*index = i;
607 			break;
608 		}
609 	}
610 
611 	return found;
612 }
613 
614 /**
615  * of_check_coupling_data - Parse rdev's coupling properties and check data
616  *			    consistency
617  * @rdev: pointer to regulator_dev whose data is checked
618  *
619  * Function checks if all the following conditions are met:
620  * - rdev's max_spread is greater than 0
621  * - all coupled regulators have the same max_spread
622  * - all coupled regulators have the same number of regulator_dev phandles
623  * - all regulators are linked to each other
624  *
625  * Returns true if all conditions are met.
626  */
of_check_coupling_data(struct regulator_dev * rdev)627 bool of_check_coupling_data(struct regulator_dev *rdev)
628 {
629 	struct device_node *node = rdev->dev.of_node;
630 	int n_phandles = of_get_n_coupled(rdev);
631 	struct device_node *c_node;
632 	int index;
633 	int i;
634 	bool ret = true;
635 
636 	/* iterate over rdev's phandles */
637 	for (i = 0; i < n_phandles; i++) {
638 		int max_spread = rdev->constraints->max_spread[i];
639 		int c_max_spread, c_n_phandles;
640 
641 		if (max_spread <= 0) {
642 			dev_err(&rdev->dev, "max_spread value invalid\n");
643 			return false;
644 		}
645 
646 		c_node = of_parse_phandle(node,
647 					  "regulator-coupled-with", i);
648 
649 		if (!c_node)
650 			ret = false;
651 
652 		c_n_phandles = of_count_phandle_with_args(c_node,
653 							  "regulator-coupled-with",
654 							  NULL);
655 
656 		if (c_n_phandles != n_phandles) {
657 			dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
658 			ret = false;
659 			goto clean;
660 		}
661 
662 		if (!of_coupling_find_node(c_node, node, &index)) {
663 			dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
664 			ret = false;
665 			goto clean;
666 		}
667 
668 		if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread",
669 					       index, &c_max_spread)) {
670 			ret = false;
671 			goto clean;
672 		}
673 
674 		if (c_max_spread != max_spread) {
675 			dev_err(&rdev->dev,
676 				"coupled regulators max_spread mismatch\n");
677 			ret = false;
678 			goto clean;
679 		}
680 
681 clean:
682 		of_node_put(c_node);
683 		if (!ret)
684 			break;
685 	}
686 
687 	return ret;
688 }
689 
690 /**
691  * of_parse_coupled_regulator() - Get regulator_dev pointer from rdev's property
692  * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
693  *	  "regulator-coupled-with" property
694  * @index: Index in phandles array
695  *
696  * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
697  * registered, returns NULL
698  */
of_parse_coupled_regulator(struct regulator_dev * rdev,int index)699 struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
700 						 int index)
701 {
702 	struct device_node *node = rdev->dev.of_node;
703 	struct device_node *c_node;
704 	struct regulator_dev *c_rdev;
705 
706 	c_node = of_parse_phandle(node, "regulator-coupled-with", index);
707 	if (!c_node)
708 		return NULL;
709 
710 	c_rdev = of_find_regulator_by_node(c_node);
711 
712 	of_node_put(c_node);
713 
714 	return c_rdev;
715 }
716 
717 /*
718  * Check if name is a supply name according to the '*-supply' pattern
719  * return 0 if false
720  * return length of supply name without the -supply
721  */
is_supply_name(const char * name)722 static int is_supply_name(const char *name)
723 {
724 	int strs, i;
725 
726 	strs = strlen(name);
727 	/* string need to be at minimum len(x-supply) */
728 	if (strs < 8)
729 		return 0;
730 	for (i = strs - 6; i > 0; i--) {
731 		/* find first '-' and check if right part is supply */
732 		if (name[i] != '-')
733 			continue;
734 		if (strcmp(name + i + 1, "supply") != 0)
735 			return 0;
736 		return i;
737 	}
738 	return 0;
739 }
740 
741 /*
742  * of_regulator_bulk_get_all - get multiple regulator consumers
743  *
744  * @dev:	Device to supply
745  * @np:		device node to search for consumers
746  * @consumers:  Configuration of consumers; clients are stored here.
747  *
748  * @return number of regulators on success, an errno on failure.
749  *
750  * This helper function allows drivers to get several regulator
751  * consumers in one operation.  If any of the regulators cannot be
752  * acquired then any regulators that were allocated will be freed
753  * before returning to the caller.
754  */
of_regulator_bulk_get_all(struct device * dev,struct device_node * np,struct regulator_bulk_data ** consumers)755 int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
756 			      struct regulator_bulk_data **consumers)
757 {
758 	int num_consumers = 0;
759 	struct regulator *tmp;
760 	struct property *prop;
761 	int i, n = 0, ret;
762 	char name[64];
763 
764 	*consumers = NULL;
765 
766 	/*
767 	 * first pass: get numbers of xxx-supply
768 	 * second pass: fill consumers
769 	 */
770 restart:
771 	for_each_property_of_node(np, prop) {
772 		i = is_supply_name(prop->name);
773 		if (i == 0)
774 			continue;
775 		if (!*consumers) {
776 			num_consumers++;
777 			continue;
778 		} else {
779 			memcpy(name, prop->name, i);
780 			name[i] = '\0';
781 			tmp = regulator_get(dev, name);
782 			if (IS_ERR(tmp)) {
783 				ret = PTR_ERR(tmp);
784 				goto error;
785 			}
786 			(*consumers)[n].consumer = tmp;
787 			n++;
788 			continue;
789 		}
790 	}
791 	if (*consumers)
792 		return num_consumers;
793 	if (num_consumers == 0)
794 		return 0;
795 	*consumers = kmalloc_array(num_consumers,
796 				   sizeof(struct regulator_bulk_data),
797 				   GFP_KERNEL);
798 	if (!*consumers)
799 		return -ENOMEM;
800 	goto restart;
801 
802 error:
803 	while (--n >= 0)
804 		regulator_put(consumers[n]->consumer);
805 	return ret;
806 }
807 EXPORT_SYMBOL_GPL(of_regulator_bulk_get_all);
808