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