xref: /OK3568_Linux_fs/kernel/drivers/regulator/of_regulator.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * OF helpers for regulator framework
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2011 Texas Instruments, Inc.
6*4882a593Smuzhiyun  * Rajendra Nayak <rnayak@ti.com>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/of.h>
12*4882a593Smuzhiyun #include <linux/regulator/machine.h>
13*4882a593Smuzhiyun #include <linux/regulator/driver.h>
14*4882a593Smuzhiyun #include <linux/regulator/of_regulator.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include "internal.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun static const char *const regulator_states[PM_SUSPEND_MAX + 1] = {
19*4882a593Smuzhiyun 	[PM_SUSPEND_STANDBY]	= "regulator-state-standby",
20*4882a593Smuzhiyun 	[PM_SUSPEND_MEM]	= "regulator-state-mem",
21*4882a593Smuzhiyun 	[PM_SUSPEND_MAX]	= "regulator-state-disk",
22*4882a593Smuzhiyun };
23*4882a593Smuzhiyun 
of_get_regulation_constraints(struct device * dev,struct device_node * np,struct regulator_init_data ** init_data,const struct regulator_desc * desc)24*4882a593Smuzhiyun static int of_get_regulation_constraints(struct device *dev,
25*4882a593Smuzhiyun 					struct device_node *np,
26*4882a593Smuzhiyun 					struct regulator_init_data **init_data,
27*4882a593Smuzhiyun 					const struct regulator_desc *desc)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	struct regulation_constraints *constraints = &(*init_data)->constraints;
30*4882a593Smuzhiyun 	struct regulator_state *suspend_state;
31*4882a593Smuzhiyun 	struct device_node *suspend_np;
32*4882a593Smuzhiyun 	unsigned int mode;
33*4882a593Smuzhiyun 	int ret, i, len;
34*4882a593Smuzhiyun 	int n_phandles;
35*4882a593Smuzhiyun 	u32 pval;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	n_phandles = of_count_phandle_with_args(np, "regulator-coupled-with",
38*4882a593Smuzhiyun 						NULL);
39*4882a593Smuzhiyun 	n_phandles = max(n_phandles, 0);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	constraints->name = of_get_property(np, "regulator-name", NULL);
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
44*4882a593Smuzhiyun 		constraints->min_uV = pval;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	if (!of_property_read_u32(np, "regulator-max-microvolt", &pval))
47*4882a593Smuzhiyun 		constraints->max_uV = pval;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	/* Voltage change possible? */
50*4882a593Smuzhiyun 	if (constraints->min_uV != constraints->max_uV)
51*4882a593Smuzhiyun 		constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	/* Do we have a voltage range, if so try to apply it? */
54*4882a593Smuzhiyun 	if (constraints->min_uV && constraints->max_uV)
55*4882a593Smuzhiyun 		constraints->apply_uV = true;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval))
58*4882a593Smuzhiyun 		constraints->uV_offset = pval;
59*4882a593Smuzhiyun 	if (!of_property_read_u32(np, "regulator-min-microamp", &pval))
60*4882a593Smuzhiyun 		constraints->min_uA = pval;
61*4882a593Smuzhiyun 	if (!of_property_read_u32(np, "regulator-max-microamp", &pval))
62*4882a593Smuzhiyun 		constraints->max_uA = pval;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
65*4882a593Smuzhiyun 				  &pval))
66*4882a593Smuzhiyun 		constraints->ilim_uA = pval;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	/* Current change possible? */
69*4882a593Smuzhiyun 	if (constraints->min_uA != constraints->max_uA)
70*4882a593Smuzhiyun 		constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	constraints->boot_on = of_property_read_bool(np, "regulator-boot-on");
73*4882a593Smuzhiyun 	constraints->always_on = of_property_read_bool(np, "regulator-always-on");
74*4882a593Smuzhiyun 	if (!constraints->always_on) /* status change should be possible. */
75*4882a593Smuzhiyun 		constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	if (of_property_read_bool(np, "regulator-allow-bypass"))
80*4882a593Smuzhiyun 		constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	if (of_property_read_bool(np, "regulator-allow-set-load"))
83*4882a593Smuzhiyun 		constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
86*4882a593Smuzhiyun 	if (!ret) {
87*4882a593Smuzhiyun 		if (pval)
88*4882a593Smuzhiyun 			constraints->ramp_delay = pval;
89*4882a593Smuzhiyun 		else
90*4882a593Smuzhiyun 			constraints->ramp_disable = true;
91*4882a593Smuzhiyun 	}
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
94*4882a593Smuzhiyun 	if (!ret)
95*4882a593Smuzhiyun 		constraints->settling_time = pval;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval);
98*4882a593Smuzhiyun 	if (!ret)
99*4882a593Smuzhiyun 		constraints->settling_time_up = pval;
100*4882a593Smuzhiyun 	if (constraints->settling_time_up && constraints->settling_time) {
101*4882a593Smuzhiyun 		pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
102*4882a593Smuzhiyun 			np);
103*4882a593Smuzhiyun 		constraints->settling_time_up = 0;
104*4882a593Smuzhiyun 	}
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	ret = of_property_read_u32(np, "regulator-settling-time-down-us",
107*4882a593Smuzhiyun 				   &pval);
108*4882a593Smuzhiyun 	if (!ret)
109*4882a593Smuzhiyun 		constraints->settling_time_down = pval;
110*4882a593Smuzhiyun 	if (constraints->settling_time_down && constraints->settling_time) {
111*4882a593Smuzhiyun 		pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
112*4882a593Smuzhiyun 			np);
113*4882a593Smuzhiyun 		constraints->settling_time_down = 0;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
117*4882a593Smuzhiyun 	if (!ret)
118*4882a593Smuzhiyun 		constraints->enable_time = pval;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	constraints->soft_start = of_property_read_bool(np,
121*4882a593Smuzhiyun 					"regulator-soft-start");
122*4882a593Smuzhiyun 	ret = of_property_read_u32(np, "regulator-active-discharge", &pval);
123*4882a593Smuzhiyun 	if (!ret) {
124*4882a593Smuzhiyun 		constraints->active_discharge =
125*4882a593Smuzhiyun 				(pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE :
126*4882a593Smuzhiyun 					REGULATOR_ACTIVE_DISCHARGE_DISABLE;
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) {
130*4882a593Smuzhiyun 		if (desc && desc->of_map_mode) {
131*4882a593Smuzhiyun 			mode = desc->of_map_mode(pval);
132*4882a593Smuzhiyun 			if (mode == REGULATOR_MODE_INVALID)
133*4882a593Smuzhiyun 				pr_err("%pOFn: invalid mode %u\n", np, pval);
134*4882a593Smuzhiyun 			else
135*4882a593Smuzhiyun 				constraints->initial_mode = mode;
136*4882a593Smuzhiyun 		} else {
137*4882a593Smuzhiyun 			pr_warn("%pOFn: mapping for mode %d not defined\n",
138*4882a593Smuzhiyun 				np, pval);
139*4882a593Smuzhiyun 		}
140*4882a593Smuzhiyun 	}
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	len = of_property_count_elems_of_size(np, "regulator-allowed-modes",
143*4882a593Smuzhiyun 						sizeof(u32));
144*4882a593Smuzhiyun 	if (len > 0) {
145*4882a593Smuzhiyun 		if (desc && desc->of_map_mode) {
146*4882a593Smuzhiyun 			for (i = 0; i < len; i++) {
147*4882a593Smuzhiyun 				ret = of_property_read_u32_index(np,
148*4882a593Smuzhiyun 					"regulator-allowed-modes", i, &pval);
149*4882a593Smuzhiyun 				if (ret) {
150*4882a593Smuzhiyun 					pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
151*4882a593Smuzhiyun 						np, i, ret);
152*4882a593Smuzhiyun 					break;
153*4882a593Smuzhiyun 				}
154*4882a593Smuzhiyun 				mode = desc->of_map_mode(pval);
155*4882a593Smuzhiyun 				if (mode == REGULATOR_MODE_INVALID)
156*4882a593Smuzhiyun 					pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
157*4882a593Smuzhiyun 						np, pval);
158*4882a593Smuzhiyun 				else
159*4882a593Smuzhiyun 					constraints->valid_modes_mask |= mode;
160*4882a593Smuzhiyun 			}
161*4882a593Smuzhiyun 			if (constraints->valid_modes_mask)
162*4882a593Smuzhiyun 				constraints->valid_ops_mask
163*4882a593Smuzhiyun 					|= REGULATOR_CHANGE_MODE;
164*4882a593Smuzhiyun 		} else {
165*4882a593Smuzhiyun 			pr_warn("%pOFn: mode mapping not defined\n", np);
166*4882a593Smuzhiyun 		}
167*4882a593Smuzhiyun 	}
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	if (!of_property_read_u32(np, "regulator-system-load", &pval))
170*4882a593Smuzhiyun 		constraints->system_load = pval;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	if (n_phandles) {
173*4882a593Smuzhiyun 		constraints->max_spread = devm_kzalloc(dev,
174*4882a593Smuzhiyun 				sizeof(*constraints->max_spread) * n_phandles,
175*4882a593Smuzhiyun 				GFP_KERNEL);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 		if (!constraints->max_spread)
178*4882a593Smuzhiyun 			return -ENOMEM;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 		of_property_read_u32_array(np, "regulator-coupled-max-spread",
181*4882a593Smuzhiyun 					   constraints->max_spread, n_phandles);
182*4882a593Smuzhiyun 	}
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	if (!of_property_read_u32(np, "regulator-max-step-microvolt",
185*4882a593Smuzhiyun 				  &pval))
186*4882a593Smuzhiyun 		constraints->max_uV_step = pval;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	constraints->over_current_protection = of_property_read_bool(np,
189*4882a593Smuzhiyun 					"regulator-over-current-protection");
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
192*4882a593Smuzhiyun 		switch (i) {
193*4882a593Smuzhiyun 		case PM_SUSPEND_MEM:
194*4882a593Smuzhiyun 			suspend_state = &constraints->state_mem;
195*4882a593Smuzhiyun 			break;
196*4882a593Smuzhiyun 		case PM_SUSPEND_MAX:
197*4882a593Smuzhiyun 			suspend_state = &constraints->state_disk;
198*4882a593Smuzhiyun 			break;
199*4882a593Smuzhiyun 		case PM_SUSPEND_STANDBY:
200*4882a593Smuzhiyun 			suspend_state = &constraints->state_standby;
201*4882a593Smuzhiyun 			break;
202*4882a593Smuzhiyun 		case PM_SUSPEND_ON:
203*4882a593Smuzhiyun 		case PM_SUSPEND_TO_IDLE:
204*4882a593Smuzhiyun 		default:
205*4882a593Smuzhiyun 			continue;
206*4882a593Smuzhiyun 		}
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 		suspend_np = of_get_child_by_name(np, regulator_states[i]);
209*4882a593Smuzhiyun 		if (!suspend_np)
210*4882a593Smuzhiyun 			continue;
211*4882a593Smuzhiyun 		if (!suspend_state) {
212*4882a593Smuzhiyun 			of_node_put(suspend_np);
213*4882a593Smuzhiyun 			continue;
214*4882a593Smuzhiyun 		}
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 		if (!of_property_read_u32(suspend_np, "regulator-mode",
217*4882a593Smuzhiyun 					  &pval)) {
218*4882a593Smuzhiyun 			if (desc && desc->of_map_mode) {
219*4882a593Smuzhiyun 				mode = desc->of_map_mode(pval);
220*4882a593Smuzhiyun 				if (mode == REGULATOR_MODE_INVALID)
221*4882a593Smuzhiyun 					pr_err("%pOFn: invalid mode %u\n",
222*4882a593Smuzhiyun 					       np, pval);
223*4882a593Smuzhiyun 				else
224*4882a593Smuzhiyun 					suspend_state->mode = mode;
225*4882a593Smuzhiyun 			} else {
226*4882a593Smuzhiyun 				pr_warn("%pOFn: mapping for mode %d not defined\n",
227*4882a593Smuzhiyun 					np, pval);
228*4882a593Smuzhiyun 			}
229*4882a593Smuzhiyun 		}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 		if (of_property_read_bool(suspend_np,
232*4882a593Smuzhiyun 					"regulator-on-in-suspend"))
233*4882a593Smuzhiyun 			suspend_state->enabled = ENABLE_IN_SUSPEND;
234*4882a593Smuzhiyun 		else if (of_property_read_bool(suspend_np,
235*4882a593Smuzhiyun 					"regulator-off-in-suspend"))
236*4882a593Smuzhiyun 			suspend_state->enabled = DISABLE_IN_SUSPEND;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 		if (!of_property_read_u32(suspend_np,
239*4882a593Smuzhiyun 				"regulator-suspend-min-microvolt", &pval))
240*4882a593Smuzhiyun 			suspend_state->min_uV = pval;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 		if (!of_property_read_u32(suspend_np,
243*4882a593Smuzhiyun 				"regulator-suspend-max-microvolt", &pval))
244*4882a593Smuzhiyun 			suspend_state->max_uV = pval;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 		if (!of_property_read_u32(suspend_np,
247*4882a593Smuzhiyun 					"regulator-suspend-microvolt", &pval))
248*4882a593Smuzhiyun 			suspend_state->uV = pval;
249*4882a593Smuzhiyun 		else /* otherwise use min_uV as default suspend voltage */
250*4882a593Smuzhiyun 			suspend_state->uV = suspend_state->min_uV;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 		if (of_property_read_bool(suspend_np,
253*4882a593Smuzhiyun 					"regulator-changeable-in-suspend"))
254*4882a593Smuzhiyun 			suspend_state->changeable = true;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 		if (i == PM_SUSPEND_MEM)
257*4882a593Smuzhiyun 			constraints->initial_state = PM_SUSPEND_MEM;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 		of_node_put(suspend_np);
260*4882a593Smuzhiyun 		suspend_state = NULL;
261*4882a593Smuzhiyun 		suspend_np = NULL;
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	return 0;
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun /**
268*4882a593Smuzhiyun  * of_get_regulator_init_data - extract regulator_init_data structure info
269*4882a593Smuzhiyun  * @dev: device requesting for regulator_init_data
270*4882a593Smuzhiyun  * @node: regulator device node
271*4882a593Smuzhiyun  * @desc: regulator description
272*4882a593Smuzhiyun  *
273*4882a593Smuzhiyun  * Populates regulator_init_data structure by extracting data from device
274*4882a593Smuzhiyun  * tree node, returns a pointer to the populated structure or NULL if memory
275*4882a593Smuzhiyun  * alloc fails.
276*4882a593Smuzhiyun  */
of_get_regulator_init_data(struct device * dev,struct device_node * node,const struct regulator_desc * desc)277*4882a593Smuzhiyun struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
278*4882a593Smuzhiyun 					  struct device_node *node,
279*4882a593Smuzhiyun 					  const struct regulator_desc *desc)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun 	struct regulator_init_data *init_data;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	if (!node)
284*4882a593Smuzhiyun 		return NULL;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
287*4882a593Smuzhiyun 	if (!init_data)
288*4882a593Smuzhiyun 		return NULL; /* Out of memory? */
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	if (of_get_regulation_constraints(dev, node, &init_data, desc))
291*4882a593Smuzhiyun 		return NULL;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	return init_data;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun struct devm_of_regulator_matches {
298*4882a593Smuzhiyun 	struct of_regulator_match *matches;
299*4882a593Smuzhiyun 	unsigned int num_matches;
300*4882a593Smuzhiyun };
301*4882a593Smuzhiyun 
devm_of_regulator_put_matches(struct device * dev,void * res)302*4882a593Smuzhiyun static void devm_of_regulator_put_matches(struct device *dev, void *res)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun 	struct devm_of_regulator_matches *devm_matches = res;
305*4882a593Smuzhiyun 	int i;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	for (i = 0; i < devm_matches->num_matches; i++)
308*4882a593Smuzhiyun 		of_node_put(devm_matches->matches[i].of_node);
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun /**
312*4882a593Smuzhiyun  * of_regulator_match - extract multiple regulator init data from device tree.
313*4882a593Smuzhiyun  * @dev: device requesting the data
314*4882a593Smuzhiyun  * @node: parent device node of the regulators
315*4882a593Smuzhiyun  * @matches: match table for the regulators
316*4882a593Smuzhiyun  * @num_matches: number of entries in match table
317*4882a593Smuzhiyun  *
318*4882a593Smuzhiyun  * This function uses a match table specified by the regulator driver to
319*4882a593Smuzhiyun  * parse regulator init data from the device tree. @node is expected to
320*4882a593Smuzhiyun  * contain a set of child nodes, each providing the init data for one
321*4882a593Smuzhiyun  * regulator. The data parsed from a child node will be matched to a regulator
322*4882a593Smuzhiyun  * based on either the deprecated property regulator-compatible if present,
323*4882a593Smuzhiyun  * or otherwise the child node's name. Note that the match table is modified
324*4882a593Smuzhiyun  * in place and an additional of_node reference is taken for each matched
325*4882a593Smuzhiyun  * regulator.
326*4882a593Smuzhiyun  *
327*4882a593Smuzhiyun  * Returns the number of matches found or a negative error code on failure.
328*4882a593Smuzhiyun  */
of_regulator_match(struct device * dev,struct device_node * node,struct of_regulator_match * matches,unsigned int num_matches)329*4882a593Smuzhiyun int of_regulator_match(struct device *dev, struct device_node *node,
330*4882a593Smuzhiyun 		       struct of_regulator_match *matches,
331*4882a593Smuzhiyun 		       unsigned int num_matches)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	unsigned int count = 0;
334*4882a593Smuzhiyun 	unsigned int i;
335*4882a593Smuzhiyun 	const char *name;
336*4882a593Smuzhiyun 	struct device_node *child;
337*4882a593Smuzhiyun 	struct devm_of_regulator_matches *devm_matches;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	if (!dev || !node)
340*4882a593Smuzhiyun 		return -EINVAL;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	devm_matches = devres_alloc(devm_of_regulator_put_matches,
343*4882a593Smuzhiyun 				    sizeof(struct devm_of_regulator_matches),
344*4882a593Smuzhiyun 				    GFP_KERNEL);
345*4882a593Smuzhiyun 	if (!devm_matches)
346*4882a593Smuzhiyun 		return -ENOMEM;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	devm_matches->matches = matches;
349*4882a593Smuzhiyun 	devm_matches->num_matches = num_matches;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	devres_add(dev, devm_matches);
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	for (i = 0; i < num_matches; i++) {
354*4882a593Smuzhiyun 		struct of_regulator_match *match = &matches[i];
355*4882a593Smuzhiyun 		match->init_data = NULL;
356*4882a593Smuzhiyun 		match->of_node = NULL;
357*4882a593Smuzhiyun 	}
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	for_each_child_of_node(node, child) {
360*4882a593Smuzhiyun 		name = of_get_property(child,
361*4882a593Smuzhiyun 					"regulator-compatible", NULL);
362*4882a593Smuzhiyun 		if (!name)
363*4882a593Smuzhiyun 			name = child->name;
364*4882a593Smuzhiyun 		for (i = 0; i < num_matches; i++) {
365*4882a593Smuzhiyun 			struct of_regulator_match *match = &matches[i];
366*4882a593Smuzhiyun 			if (match->of_node)
367*4882a593Smuzhiyun 				continue;
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 			if (strcmp(match->name, name))
370*4882a593Smuzhiyun 				continue;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 			match->init_data =
373*4882a593Smuzhiyun 				of_get_regulator_init_data(dev, child,
374*4882a593Smuzhiyun 							   match->desc);
375*4882a593Smuzhiyun 			if (!match->init_data) {
376*4882a593Smuzhiyun 				dev_err(dev,
377*4882a593Smuzhiyun 					"failed to parse DT for regulator %pOFn\n",
378*4882a593Smuzhiyun 					child);
379*4882a593Smuzhiyun 				of_node_put(child);
380*4882a593Smuzhiyun 				return -EINVAL;
381*4882a593Smuzhiyun 			}
382*4882a593Smuzhiyun 			match->of_node = of_node_get(child);
383*4882a593Smuzhiyun 			count++;
384*4882a593Smuzhiyun 			break;
385*4882a593Smuzhiyun 		}
386*4882a593Smuzhiyun 	}
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	return count;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_regulator_match);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun static struct
regulator_of_get_init_node(struct device * dev,const struct regulator_desc * desc)393*4882a593Smuzhiyun device_node *regulator_of_get_init_node(struct device *dev,
394*4882a593Smuzhiyun 					const struct regulator_desc *desc)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun 	struct device_node *search, *child;
397*4882a593Smuzhiyun 	const char *name;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	if (!dev->of_node || !desc->of_match)
400*4882a593Smuzhiyun 		return NULL;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	if (desc->regulators_node) {
403*4882a593Smuzhiyun 		search = of_get_child_by_name(dev->of_node,
404*4882a593Smuzhiyun 					      desc->regulators_node);
405*4882a593Smuzhiyun 	} else {
406*4882a593Smuzhiyun 		search = of_node_get(dev->of_node);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 		if (!strcmp(desc->of_match, search->name))
409*4882a593Smuzhiyun 			return search;
410*4882a593Smuzhiyun 	}
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	if (!search) {
413*4882a593Smuzhiyun 		dev_dbg(dev, "Failed to find regulator container node '%s'\n",
414*4882a593Smuzhiyun 			desc->regulators_node);
415*4882a593Smuzhiyun 		return NULL;
416*4882a593Smuzhiyun 	}
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	for_each_available_child_of_node(search, child) {
419*4882a593Smuzhiyun 		name = of_get_property(child, "regulator-compatible", NULL);
420*4882a593Smuzhiyun 		if (!name) {
421*4882a593Smuzhiyun 			if (!desc->of_match_full_name)
422*4882a593Smuzhiyun 				name = child->name;
423*4882a593Smuzhiyun 			else
424*4882a593Smuzhiyun 				name = child->full_name;
425*4882a593Smuzhiyun 		}
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 		if (!strcmp(desc->of_match, name)) {
428*4882a593Smuzhiyun 			of_node_put(search);
429*4882a593Smuzhiyun 			return of_node_get(child);
430*4882a593Smuzhiyun 		}
431*4882a593Smuzhiyun 	}
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	of_node_put(search);
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	return NULL;
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun 
regulator_of_get_init_data(struct device * dev,const struct regulator_desc * desc,struct regulator_config * config,struct device_node ** node)438*4882a593Smuzhiyun struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
439*4882a593Smuzhiyun 					    const struct regulator_desc *desc,
440*4882a593Smuzhiyun 					    struct regulator_config *config,
441*4882a593Smuzhiyun 					    struct device_node **node)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	struct device_node *child;
444*4882a593Smuzhiyun 	struct regulator_init_data *init_data = NULL;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	child = regulator_of_get_init_node(dev, desc);
447*4882a593Smuzhiyun 	if (!child)
448*4882a593Smuzhiyun 		return NULL;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	init_data = of_get_regulator_init_data(dev, child, desc);
451*4882a593Smuzhiyun 	if (!init_data) {
452*4882a593Smuzhiyun 		dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
453*4882a593Smuzhiyun 		goto error;
454*4882a593Smuzhiyun 	}
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	if (desc->of_parse_cb) {
457*4882a593Smuzhiyun 		int ret;
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 		ret = desc->of_parse_cb(child, desc, config);
460*4882a593Smuzhiyun 		if (ret) {
461*4882a593Smuzhiyun 			if (ret == -EPROBE_DEFER) {
462*4882a593Smuzhiyun 				of_node_put(child);
463*4882a593Smuzhiyun 				return ERR_PTR(-EPROBE_DEFER);
464*4882a593Smuzhiyun 			}
465*4882a593Smuzhiyun 			dev_err(dev,
466*4882a593Smuzhiyun 				"driver callback failed to parse DT for regulator %pOFn\n",
467*4882a593Smuzhiyun 				child);
468*4882a593Smuzhiyun 			goto error;
469*4882a593Smuzhiyun 		}
470*4882a593Smuzhiyun 	}
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	*node = child;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	return init_data;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun error:
477*4882a593Smuzhiyun 	of_node_put(child);
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	return NULL;
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun 
of_find_regulator_by_node(struct device_node * np)482*4882a593Smuzhiyun struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun 	struct device *dev;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	dev = class_find_device_by_of_node(&regulator_class, np);
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	return dev ? dev_to_rdev(dev) : NULL;
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun /*
492*4882a593Smuzhiyun  * Returns number of regulators coupled with rdev.
493*4882a593Smuzhiyun  */
of_get_n_coupled(struct regulator_dev * rdev)494*4882a593Smuzhiyun int of_get_n_coupled(struct regulator_dev *rdev)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun 	struct device_node *node = rdev->dev.of_node;
497*4882a593Smuzhiyun 	int n_phandles;
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	n_phandles = of_count_phandle_with_args(node,
500*4882a593Smuzhiyun 						"regulator-coupled-with",
501*4882a593Smuzhiyun 						NULL);
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	return (n_phandles > 0) ? n_phandles : 0;
504*4882a593Smuzhiyun }
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun /* 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)507*4882a593Smuzhiyun static bool of_coupling_find_node(struct device_node *src,
508*4882a593Smuzhiyun 				  struct device_node *to_find,
509*4882a593Smuzhiyun 				  int *index)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun 	int n_phandles, i;
512*4882a593Smuzhiyun 	bool found = false;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	n_phandles = of_count_phandle_with_args(src,
515*4882a593Smuzhiyun 						"regulator-coupled-with",
516*4882a593Smuzhiyun 						NULL);
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	for (i = 0; i < n_phandles; i++) {
519*4882a593Smuzhiyun 		struct device_node *tmp = of_parse_phandle(src,
520*4882a593Smuzhiyun 					   "regulator-coupled-with", i);
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 		if (!tmp)
523*4882a593Smuzhiyun 			break;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 		/* found */
526*4882a593Smuzhiyun 		if (tmp == to_find)
527*4882a593Smuzhiyun 			found = true;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 		of_node_put(tmp);
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 		if (found) {
532*4882a593Smuzhiyun 			*index = i;
533*4882a593Smuzhiyun 			break;
534*4882a593Smuzhiyun 		}
535*4882a593Smuzhiyun 	}
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	return found;
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun /**
541*4882a593Smuzhiyun  * of_check_coupling_data - Parse rdev's coupling properties and check data
542*4882a593Smuzhiyun  *			    consistency
543*4882a593Smuzhiyun  * @rdev: pointer to regulator_dev whose data is checked
544*4882a593Smuzhiyun  *
545*4882a593Smuzhiyun  * Function checks if all the following conditions are met:
546*4882a593Smuzhiyun  * - rdev's max_spread is greater than 0
547*4882a593Smuzhiyun  * - all coupled regulators have the same max_spread
548*4882a593Smuzhiyun  * - all coupled regulators have the same number of regulator_dev phandles
549*4882a593Smuzhiyun  * - all regulators are linked to each other
550*4882a593Smuzhiyun  *
551*4882a593Smuzhiyun  * Returns true if all conditions are met.
552*4882a593Smuzhiyun  */
of_check_coupling_data(struct regulator_dev * rdev)553*4882a593Smuzhiyun bool of_check_coupling_data(struct regulator_dev *rdev)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun 	struct device_node *node = rdev->dev.of_node;
556*4882a593Smuzhiyun 	int n_phandles = of_get_n_coupled(rdev);
557*4882a593Smuzhiyun 	struct device_node *c_node;
558*4882a593Smuzhiyun 	int index;
559*4882a593Smuzhiyun 	int i;
560*4882a593Smuzhiyun 	bool ret = true;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	/* iterate over rdev's phandles */
563*4882a593Smuzhiyun 	for (i = 0; i < n_phandles; i++) {
564*4882a593Smuzhiyun 		int max_spread = rdev->constraints->max_spread[i];
565*4882a593Smuzhiyun 		int c_max_spread, c_n_phandles;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 		if (max_spread <= 0) {
568*4882a593Smuzhiyun 			dev_err(&rdev->dev, "max_spread value invalid\n");
569*4882a593Smuzhiyun 			return false;
570*4882a593Smuzhiyun 		}
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 		c_node = of_parse_phandle(node,
573*4882a593Smuzhiyun 					  "regulator-coupled-with", i);
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 		if (!c_node)
576*4882a593Smuzhiyun 			ret = false;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 		c_n_phandles = of_count_phandle_with_args(c_node,
579*4882a593Smuzhiyun 							  "regulator-coupled-with",
580*4882a593Smuzhiyun 							  NULL);
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 		if (c_n_phandles != n_phandles) {
583*4882a593Smuzhiyun 			dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
584*4882a593Smuzhiyun 			ret = false;
585*4882a593Smuzhiyun 			goto clean;
586*4882a593Smuzhiyun 		}
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 		if (!of_coupling_find_node(c_node, node, &index)) {
589*4882a593Smuzhiyun 			dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
590*4882a593Smuzhiyun 			ret = false;
591*4882a593Smuzhiyun 			goto clean;
592*4882a593Smuzhiyun 		}
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 		if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread",
595*4882a593Smuzhiyun 					       index, &c_max_spread)) {
596*4882a593Smuzhiyun 			ret = false;
597*4882a593Smuzhiyun 			goto clean;
598*4882a593Smuzhiyun 		}
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 		if (c_max_spread != max_spread) {
601*4882a593Smuzhiyun 			dev_err(&rdev->dev,
602*4882a593Smuzhiyun 				"coupled regulators max_spread mismatch\n");
603*4882a593Smuzhiyun 			ret = false;
604*4882a593Smuzhiyun 			goto clean;
605*4882a593Smuzhiyun 		}
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun clean:
608*4882a593Smuzhiyun 		of_node_put(c_node);
609*4882a593Smuzhiyun 		if (!ret)
610*4882a593Smuzhiyun 			break;
611*4882a593Smuzhiyun 	}
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	return ret;
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun /**
617*4882a593Smuzhiyun  * of_parse_coupled regulator - Get regulator_dev pointer from rdev's property
618*4882a593Smuzhiyun  * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
619*4882a593Smuzhiyun  *	  "regulator-coupled-with" property
620*4882a593Smuzhiyun  * @index: Index in phandles array
621*4882a593Smuzhiyun  *
622*4882a593Smuzhiyun  * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
623*4882a593Smuzhiyun  * registered, returns NULL
624*4882a593Smuzhiyun  */
of_parse_coupled_regulator(struct regulator_dev * rdev,int index)625*4882a593Smuzhiyun struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
626*4882a593Smuzhiyun 						 int index)
627*4882a593Smuzhiyun {
628*4882a593Smuzhiyun 	struct device_node *node = rdev->dev.of_node;
629*4882a593Smuzhiyun 	struct device_node *c_node;
630*4882a593Smuzhiyun 	struct regulator_dev *c_rdev;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	c_node = of_parse_phandle(node, "regulator-coupled-with", index);
633*4882a593Smuzhiyun 	if (!c_node)
634*4882a593Smuzhiyun 		return NULL;
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	c_rdev = of_find_regulator_by_node(c_node);
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	of_node_put(c_node);
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	return c_rdev;
641*4882a593Smuzhiyun }
642