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