Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  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-less-critical-window-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.
342 *
343 * Return: Pointer to a populated &struct regulator_init_data or NULL if
344 *	   memory allocation fails.
345 */
346struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
347					  struct device_node *node,
348					  const struct regulator_desc *desc)
349{
350	struct regulator_init_data *init_data;
351
352	if (!node)
353		return NULL;
354
355	init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
356	if (!init_data)
357		return NULL; /* Out of memory? */
358
359	if (of_get_regulation_constraints(dev, node, &init_data, desc))
360		return NULL;
361
362	return init_data;
363}
364EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
365
366struct devm_of_regulator_matches {
367	struct of_regulator_match *matches;
368	unsigned int num_matches;
369};
370
371static void devm_of_regulator_put_matches(struct device *dev, void *res)
372{
373	struct devm_of_regulator_matches *devm_matches = res;
374	int i;
375
376	for (i = 0; i < devm_matches->num_matches; i++)
377		of_node_put(devm_matches->matches[i].of_node);
378}
379
380/**
381 * of_regulator_match - extract multiple regulator init data from device tree.
382 * @dev: device requesting the data
383 * @node: parent device node of the regulators
384 * @matches: match table for the regulators
385 * @num_matches: number of entries in match table
386 *
387 * This function uses a match table specified by the regulator driver to
388 * parse regulator init data from the device tree. @node is expected to
389 * contain a set of child nodes, each providing the init data for one
390 * regulator. The data parsed from a child node will be matched to a regulator
391 * based on either the deprecated property regulator-compatible if present,
392 * or otherwise the child node's name. Note that the match table is modified
393 * in place and an additional of_node reference is taken for each matched
394 * regulator.
395 *
396 * Return: The number of matches found or a negative error number on failure.
397 */
398int of_regulator_match(struct device *dev, struct device_node *node,
399		       struct of_regulator_match *matches,
400		       unsigned int num_matches)
401{
402	unsigned int count = 0;
403	unsigned int i;
404	const char *name;
405	struct device_node *child;
406	struct devm_of_regulator_matches *devm_matches;
407
408	if (!dev || !node)
409		return -EINVAL;
410
411	devm_matches = devres_alloc(devm_of_regulator_put_matches,
412				    sizeof(struct devm_of_regulator_matches),
413				    GFP_KERNEL);
414	if (!devm_matches)
415		return -ENOMEM;
416
417	devm_matches->matches = matches;
418	devm_matches->num_matches = num_matches;
419
420	devres_add(dev, devm_matches);
421
422	for (i = 0; i < num_matches; i++) {
423		struct of_regulator_match *match = &matches[i];
424		match->init_data = NULL;
425		match->of_node = NULL;
426	}
427
428	for_each_child_of_node(node, child) {
429		name = of_get_property(child,
430					"regulator-compatible", NULL);
431		if (!name)
432			name = child->name;
433		for (i = 0; i < num_matches; i++) {
434			struct of_regulator_match *match = &matches[i];
435			if (match->of_node)
436				continue;
437
438			if (strcmp(match->name, name))
439				continue;
440
441			match->init_data =
442				of_get_regulator_init_data(dev, child,
443							   match->desc);
444			if (!match->init_data) {
445				dev_err(dev,
446					"failed to parse DT for regulator %pOFn\n",
447					child);
448				of_node_put(child);
449				goto err_put;
450			}
451			match->of_node = of_node_get(child);
452			count++;
453			break;
454		}
455	}
456
457	return count;
458
459err_put:
460	for (i = 0; i < num_matches; i++) {
461		struct of_regulator_match *match = &matches[i];
462
463		match->init_data = NULL;
464		if (match->of_node) {
465			of_node_put(match->of_node);
466			match->of_node = NULL;
467		}
468	}
469	return -EINVAL;
470}
471EXPORT_SYMBOL_GPL(of_regulator_match);
472
473static struct
474device_node *regulator_of_get_init_node(struct device *dev,
475					const struct regulator_desc *desc)
476{
477	struct device_node *search, *child;
478	const char *name;
479
480	if (!dev->of_node || !desc->of_match)
481		return NULL;
482
483	if (desc->regulators_node) {
484		search = of_get_child_by_name(dev->of_node,
485					      desc->regulators_node);
486	} else {
487		search = of_node_get(dev->of_node);
488
489		if (!strcmp(desc->of_match, search->name))
490			return search;
491	}
492
493	if (!search) {
494		dev_dbg(dev, "Failed to find regulator container node '%s'\n",
495			desc->regulators_node);
496		return NULL;
497	}
498
499	for_each_available_child_of_node(search, child) {
500		name = of_get_property(child, "regulator-compatible", NULL);
501		if (!name) {
502			if (!desc->of_match_full_name)
503				name = child->name;
504			else
505				name = child->full_name;
506		}
507
508		if (!strcmp(desc->of_match, name)) {
509			of_node_put(search);
510			/*
511			 * 'of_node_get(child)' is already performed by the
512			 * for_each loop.
513			 */
514			return child;
515		}
516	}
517
518	of_node_put(search);
519
520	return NULL;
521}
522
523struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
524					    const struct regulator_desc *desc,
525					    struct regulator_config *config,
526					    struct device_node **node)
527{
528	struct device_node *child;
529	struct regulator_init_data *init_data = NULL;
530
531	child = regulator_of_get_init_node(config->dev, desc);
532	if (!child)
533		return NULL;
534
535	init_data = of_get_regulator_init_data(dev, child, desc);
536	if (!init_data) {
537		dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
538		goto error;
539	}
540
541	if (desc->of_parse_cb) {
542		int ret;
543
544		ret = desc->of_parse_cb(child, desc, config);
545		if (ret) {
546			if (ret == -EPROBE_DEFER) {
547				of_node_put(child);
548				return ERR_PTR(-EPROBE_DEFER);
549			}
550			dev_err(dev,
551				"driver callback failed to parse DT for regulator %pOFn\n",
552				child);
553			goto error;
554		}
555	}
556
557	*node = child;
558
559	return init_data;
560
561error:
562	of_node_put(child);
563
564	return NULL;
565}
566
567/**
568 * of_get_child_regulator - get a child regulator device node
569 * based on supply name
570 * @parent: Parent device node
571 * @prop_name: Combination regulator supply name and "-supply"
572 *
573 * Traverse all child nodes.
574 * Extract the child regulator device node corresponding to the supply name.
575 *
576 * Return: Pointer to the &struct device_node corresponding to the regulator
577 *	   if found, or %NULL if not found.
578 */
579static struct device_node *of_get_child_regulator(struct device_node *parent,
580						  const char *prop_name)
581{
582	struct device_node *regnode = NULL;
583	struct device_node *child = NULL;
584
585	for_each_child_of_node(parent, child) {
586		regnode = of_parse_phandle(child, prop_name, 0);
587		if (regnode)
588			goto err_node_put;
589
590		regnode = of_get_child_regulator(child, prop_name);
591		if (regnode)
592			goto err_node_put;
593	}
594	return NULL;
595
596err_node_put:
597	of_node_put(child);
598	return regnode;
599}
600
601/**
602 * of_get_regulator - get a regulator device node based on supply name
603 * @dev: Device pointer for dev_printk() messages
604 * @node: Device node pointer for supply property lookup
605 * @supply: regulator supply name
606 *
607 * Extract the regulator device node corresponding to the supply name.
608 *
609 * Return: Pointer to the &struct device_node corresponding to the regulator
610 *	   if found, or %NULL if not found.
611 */
612static struct device_node *of_get_regulator(struct device *dev, struct device_node *node,
613					    const char *supply)
614{
615	struct device_node *regnode = NULL;
616	char prop_name[64]; /* 64 is max size of property name */
617
618	dev_dbg(dev, "Looking up %s-supply from device node %pOF\n", supply, node);
619
620	snprintf(prop_name, 64, "%s-supply", supply);
621	regnode = of_parse_phandle(node, prop_name, 0);
622	if (regnode)
623		return regnode;
624
625	regnode = of_get_child_regulator(dev->of_node, prop_name);
626	if (regnode)
627		return regnode;
628
629	dev_dbg(dev, "Looking up %s property in node %pOF failed\n", prop_name, dev->of_node);
630	return NULL;
631}
632
633static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
634{
635	struct device *dev;
636
637	dev = class_find_device_by_of_node(&regulator_class, np);
638
639	return dev ? dev_to_rdev(dev) : NULL;
640}
641
642/**
643 * of_regulator_dev_lookup - lookup a regulator device with device tree only
644 * @dev: Device pointer for regulator supply lookup.
645 * @np: Device node pointer for regulator supply lookup.
646 * @supply: Supply name or regulator ID.
647 *
648 * Return: Pointer to the &struct regulator_dev on success, or ERR_PTR()
649 *	   encoded value on error.
650 *
651 * If successful, returns a pointer to the &struct regulator_dev that
652 * corresponds to the name @supply and with the embedded &struct device
653 * refcount incremented by one. The refcount must be dropped by calling
654 * put_device().
655 *
656 * On failure one of the following ERR_PTR() encoded values is returned:
657 * * -%ENODEV if lookup fails permanently.
658 * * -%EPROBE_DEFER if lookup could succeed in the future.
659 */
660struct regulator_dev *of_regulator_dev_lookup(struct device *dev, struct device_node *np,
661					      const char *supply)
662{
663	struct regulator_dev *r;
664	struct device_node *node;
665
666	node = of_get_regulator(dev, np, supply);
667	if (node) {
668		r = of_find_regulator_by_node(node);
669		of_node_put(node);
670		if (r)
671			return r;
672
673		/*
674		 * We have a node, but there is no device.
675		 * assume it has not registered yet.
676		 */
677		return ERR_PTR(-EPROBE_DEFER);
678	}
679
680	return ERR_PTR(-ENODEV);
681}
682
683struct regulator *_of_regulator_get(struct device *dev, struct device_node *node,
684				    const char *id, enum regulator_get_type get_type)
685{
686	struct regulator_dev *r;
687	int ret;
688
689	ret = _regulator_get_common_check(dev, id, get_type);
690	if (ret)
691		return ERR_PTR(ret);
692
693	r = of_regulator_dev_lookup(dev, node, id);
694	return _regulator_get_common(r, dev, id, get_type);
695}
696
697/**
698 * of_regulator_get_optional - get optional regulator via device tree lookup
699 * @dev: device used for dev_printk() messages
700 * @node: device node for regulator "consumer"
701 * @id: Supply name
702 *
703 * Return: pointer to struct regulator corresponding to the regulator producer,
704 *	   or PTR_ERR() encoded error number.
705 *
706 * This is intended for use by consumers that want to get a regulator
707 * supply directly from a device node, and can and want to deal with
708 * absence of such supplies. This will _not_ consider supply aliases.
709 * See regulator_dev_lookup().
710 */
711struct regulator *of_regulator_get_optional(struct device *dev,
712					    struct device_node *node,
713					    const char *id)
714{
715	return _of_regulator_get(dev, node, id, OPTIONAL_GET);
716}
717EXPORT_SYMBOL_GPL(of_regulator_get_optional);
718
719/*
720 * Returns number of regulators coupled with rdev.
721 */
722int of_get_n_coupled(struct regulator_dev *rdev)
723{
724	struct device_node *node = rdev->dev.of_node;
725	int n_phandles;
726
727	n_phandles = of_count_phandle_with_args(node,
728						"regulator-coupled-with",
729						NULL);
730
731	return (n_phandles > 0) ? n_phandles : 0;
732}
733
734/* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
735static bool of_coupling_find_node(struct device_node *src,
736				  struct device_node *to_find,
737				  int *index)
738{
739	int n_phandles, i;
740	bool found = false;
741
742	n_phandles = of_count_phandle_with_args(src,
743						"regulator-coupled-with",
744						NULL);
745
746	for (i = 0; i < n_phandles; i++) {
747		struct device_node *tmp = of_parse_phandle(src,
748					   "regulator-coupled-with", i);
749
750		if (!tmp)
751			break;
752
753		/* found */
754		if (tmp == to_find)
755			found = true;
756
757		of_node_put(tmp);
758
759		if (found) {
760			*index = i;
761			break;
762		}
763	}
764
765	return found;
766}
767
768/**
769 * of_check_coupling_data - Parse rdev's coupling properties and check data
770 *			    consistency
771 * @rdev: pointer to regulator_dev whose data is checked
772 *
773 * Function checks if all the following conditions are met:
774 * - rdev's max_spread is greater than 0
775 * - all coupled regulators have the same max_spread
776 * - all coupled regulators have the same number of regulator_dev phandles
777 * - all regulators are linked to each other
778 *
779 * Return: True if all conditions are met; false otherwise.
780 */
781bool of_check_coupling_data(struct regulator_dev *rdev)
782{
783	struct device_node *node = rdev->dev.of_node;
784	int n_phandles = of_get_n_coupled(rdev);
785	struct device_node *c_node;
786	int index;
787	int i;
788	bool ret = true;
789
790	/* iterate over rdev's phandles */
791	for (i = 0; i < n_phandles; i++) {
792		int max_spread = rdev->constraints->max_spread[i];
793		int c_max_spread, c_n_phandles;
794
795		if (max_spread <= 0) {
796			dev_err(&rdev->dev, "max_spread value invalid\n");
797			return false;
798		}
799
800		c_node = of_parse_phandle(node,
801					  "regulator-coupled-with", i);
802
803		if (!c_node)
804			ret = false;
805
806		c_n_phandles = of_count_phandle_with_args(c_node,
807							  "regulator-coupled-with",
808							  NULL);
809
810		if (c_n_phandles != n_phandles) {
811			dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
812			ret = false;
813			goto clean;
814		}
815
816		if (!of_coupling_find_node(c_node, node, &index)) {
817			dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
818			ret = false;
819			goto clean;
820		}
821
822		if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread",
823					       index, &c_max_spread)) {
824			ret = false;
825			goto clean;
826		}
827
828		if (c_max_spread != max_spread) {
829			dev_err(&rdev->dev,
830				"coupled regulators max_spread mismatch\n");
831			ret = false;
832			goto clean;
833		}
834
835clean:
836		of_node_put(c_node);
837		if (!ret)
838			break;
839	}
840
841	return ret;
842}
843
844/**
845 * of_parse_coupled_regulator() - Get regulator_dev pointer from rdev's property
846 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
847 *	  "regulator-coupled-with" property
848 * @index: Index in phandles array
849 *
850 * Return: Pointer to the &struct regulator_dev parsed from DTS, or %NULL if
851 *	   it has not yet been registered.
852 */
853struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
854						 int index)
855{
856	struct device_node *node = rdev->dev.of_node;
857	struct device_node *c_node;
858	struct regulator_dev *c_rdev;
859
860	c_node = of_parse_phandle(node, "regulator-coupled-with", index);
861	if (!c_node)
862		return NULL;
863
864	c_rdev = of_find_regulator_by_node(c_node);
865
866	of_node_put(c_node);
867
868	return c_rdev;
869}
870
871/*
872 * Check if name is a supply name according to the '*-supply' pattern
873 * return 0 if false
874 * return length of supply name without the -supply
875 */
876static int is_supply_name(const char *name)
877{
878	int strs, i;
879
880	strs = strlen(name);
881	/* string need to be at minimum len(x-supply) */
882	if (strs < 8)
883		return 0;
884	for (i = strs - 6; i > 0; i--) {
885		/* find first '-' and check if right part is supply */
886		if (name[i] != '-')
887			continue;
888		if (strcmp(name + i + 1, "supply") != 0)
889			return 0;
890		return i;
891	}
892	return 0;
893}
894
895/**
896 * of_regulator_bulk_get_all - get multiple regulator consumers
897 *
898 * @dev:	Device to supply
899 * @np:		device node to search for consumers
900 * @consumers:  Configuration of consumers; clients are stored here.
901 *
902 * This helper function allows drivers to get several regulator
903 * consumers in one operation.  If any of the regulators cannot be
904 * acquired then any regulators that were allocated will be freed
905 * before returning to the caller, and @consumers will not be
906 * changed.
907 *
908 * Return: Number of regulators on success, or a negative error number
909 *	   on failure.
910 */
911int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
912			      struct regulator_bulk_data **consumers)
913{
914	int num_consumers = 0;
915	struct regulator *tmp;
916	struct regulator_bulk_data *_consumers = NULL;
917	struct property *prop;
918	int i, n = 0, ret;
919	char name[64];
920
921	/*
922	 * first pass: get numbers of xxx-supply
923	 * second pass: fill consumers
924	 */
925restart:
926	for_each_property_of_node(np, prop) {
927		i = is_supply_name(prop->name);
928		if (i == 0)
929			continue;
930		if (!_consumers) {
931			num_consumers++;
932			continue;
933		} else {
934			memcpy(name, prop->name, i);
935			name[i] = '\0';
936			tmp = regulator_get(dev, name);
937			if (IS_ERR(tmp)) {
938				ret = PTR_ERR(tmp);
939				goto error;
940			}
941			_consumers[n].consumer = tmp;
942			n++;
943			continue;
944		}
945	}
946	if (_consumers) {
947		*consumers = _consumers;
948		return num_consumers;
949	}
950	if (num_consumers == 0)
951		return 0;
952	_consumers = kmalloc_array(num_consumers,
953				   sizeof(struct regulator_bulk_data),
954				   GFP_KERNEL);
955	if (!_consumers)
956		return -ENOMEM;
957	goto restart;
958
959error:
960	while (--n >= 0)
961		regulator_put(_consumers[n].consumer);
962	kfree(_consumers);
963	return ret;
964}
965EXPORT_SYMBOL_GPL(of_regulator_bulk_get_all);