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