1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OF helpers for regulator framework
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Rajendra Nayak <rnayak@ti.com>
9 #include <linux/module.h>
10 #include <linux/slab.h>
12 #include <linux/regulator/machine.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/regulator/of_regulator.h>
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",
24 static int of_get_regulation_constraints(struct device
*dev
,
25 struct device_node
*np
,
26 struct regulator_init_data
**init_data
,
27 const struct regulator_desc
*desc
)
29 struct regulation_constraints
*constraints
= &(*init_data
)->constraints
;
30 struct regulator_state
*suspend_state
;
31 struct device_node
*suspend_np
;
37 n_phandles
= of_count_phandle_with_args(np
, "regulator-coupled-with",
39 n_phandles
= max(n_phandles
, 0);
41 constraints
->name
= of_get_property(np
, "regulator-name", NULL
);
43 if (!of_property_read_u32(np
, "regulator-min-microvolt", &pval
))
44 constraints
->min_uV
= pval
;
46 if (!of_property_read_u32(np
, "regulator-max-microvolt", &pval
))
47 constraints
->max_uV
= pval
;
49 /* Voltage change possible? */
50 if (constraints
->min_uV
!= constraints
->max_uV
)
51 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_VOLTAGE
;
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;
57 if (!of_property_read_u32(np
, "regulator-microvolt-offset", &pval
))
58 constraints
->uV_offset
= pval
;
59 if (!of_property_read_u32(np
, "regulator-min-microamp", &pval
))
60 constraints
->min_uA
= pval
;
61 if (!of_property_read_u32(np
, "regulator-max-microamp", &pval
))
62 constraints
->max_uA
= pval
;
64 if (!of_property_read_u32(np
, "regulator-input-current-limit-microamp",
66 constraints
->ilim_uA
= pval
;
68 /* Current change possible? */
69 if (constraints
->min_uA
!= constraints
->max_uA
)
70 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_CURRENT
;
72 constraints
->boot_on
= of_property_read_bool(np
, "regulator-boot-on");
73 constraints
->always_on
= of_property_read_bool(np
, "regulator-always-on");
74 if (!constraints
->always_on
) /* status change should be possible. */
75 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_STATUS
;
77 constraints
->pull_down
= of_property_read_bool(np
, "regulator-pull-down");
79 if (of_property_read_bool(np
, "regulator-allow-bypass"))
80 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_BYPASS
;
82 if (of_property_read_bool(np
, "regulator-allow-set-load"))
83 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_DRMS
;
85 ret
= of_property_read_u32(np
, "regulator-ramp-delay", &pval
);
88 constraints
->ramp_delay
= pval
;
90 constraints
->ramp_disable
= true;
93 ret
= of_property_read_u32(np
, "regulator-settling-time-us", &pval
);
95 constraints
->settling_time
= pval
;
97 ret
= of_property_read_u32(np
, "regulator-settling-time-up-us", &pval
);
99 constraints
->settling_time_up
= pval
;
100 if (constraints
->settling_time_up
&& constraints
->settling_time
) {
101 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
103 constraints
->settling_time_up
= 0;
106 ret
= of_property_read_u32(np
, "regulator-settling-time-down-us",
109 constraints
->settling_time_down
= pval
;
110 if (constraints
->settling_time_down
&& constraints
->settling_time
) {
111 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
113 constraints
->settling_time_down
= 0;
116 ret
= of_property_read_u32(np
, "regulator-enable-ramp-delay", &pval
);
118 constraints
->enable_time
= pval
;
120 constraints
->soft_start
= of_property_read_bool(np
,
121 "regulator-soft-start");
122 ret
= of_property_read_u32(np
, "regulator-active-discharge", &pval
);
124 constraints
->active_discharge
=
125 (pval
) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE
:
126 REGULATOR_ACTIVE_DISCHARGE_DISABLE
;
129 if (!of_property_read_u32(np
, "regulator-initial-mode", &pval
)) {
130 if (desc
&& desc
->of_map_mode
) {
131 mode
= desc
->of_map_mode(pval
);
132 if (mode
== REGULATOR_MODE_INVALID
)
133 pr_err("%pOFn: invalid mode %u\n", np
, pval
);
135 constraints
->initial_mode
= mode
;
137 pr_warn("%pOFn: mapping for mode %d not defined\n",
142 len
= of_property_count_elems_of_size(np
, "regulator-allowed-modes",
145 if (desc
&& desc
->of_map_mode
) {
146 for (i
= 0; i
< len
; i
++) {
147 ret
= of_property_read_u32_index(np
,
148 "regulator-allowed-modes", i
, &pval
);
150 pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
154 mode
= desc
->of_map_mode(pval
);
155 if (mode
== REGULATOR_MODE_INVALID
)
156 pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
159 constraints
->valid_modes_mask
|= mode
;
161 if (constraints
->valid_modes_mask
)
162 constraints
->valid_ops_mask
163 |= REGULATOR_CHANGE_MODE
;
165 pr_warn("%pOFn: mode mapping not defined\n", np
);
169 if (!of_property_read_u32(np
, "regulator-system-load", &pval
))
170 constraints
->system_load
= pval
;
173 constraints
->max_spread
= devm_kzalloc(dev
,
174 sizeof(*constraints
->max_spread
) * n_phandles
,
177 if (!constraints
->max_spread
)
180 of_property_read_u32_array(np
, "regulator-coupled-max-spread",
181 constraints
->max_spread
, n_phandles
);
184 if (!of_property_read_u32(np
, "regulator-max-step-microvolt",
186 constraints
->max_uV_step
= pval
;
188 constraints
->over_current_protection
= of_property_read_bool(np
,
189 "regulator-over-current-protection");
191 for (i
= 0; i
< ARRAY_SIZE(regulator_states
); i
++) {
194 suspend_state
= &constraints
->state_mem
;
197 suspend_state
= &constraints
->state_disk
;
199 case PM_SUSPEND_STANDBY
:
200 suspend_state
= &constraints
->state_standby
;
203 case PM_SUSPEND_TO_IDLE
:
208 suspend_np
= of_get_child_by_name(np
, regulator_states
[i
]);
209 if (!suspend_np
|| !suspend_state
)
212 if (!of_property_read_u32(suspend_np
, "regulator-mode",
214 if (desc
&& desc
->of_map_mode
) {
215 mode
= desc
->of_map_mode(pval
);
216 if (mode
== REGULATOR_MODE_INVALID
)
217 pr_err("%pOFn: invalid mode %u\n",
220 suspend_state
->mode
= mode
;
222 pr_warn("%pOFn: mapping for mode %d not defined\n",
227 if (of_property_read_bool(suspend_np
,
228 "regulator-on-in-suspend"))
229 suspend_state
->enabled
= ENABLE_IN_SUSPEND
;
230 else if (of_property_read_bool(suspend_np
,
231 "regulator-off-in-suspend"))
232 suspend_state
->enabled
= DISABLE_IN_SUSPEND
;
234 if (!of_property_read_u32(suspend_np
,
235 "regulator-suspend-min-microvolt", &pval
))
236 suspend_state
->min_uV
= pval
;
238 if (!of_property_read_u32(suspend_np
,
239 "regulator-suspend-max-microvolt", &pval
))
240 suspend_state
->max_uV
= pval
;
242 if (!of_property_read_u32(suspend_np
,
243 "regulator-suspend-microvolt", &pval
))
244 suspend_state
->uV
= pval
;
245 else /* otherwise use min_uV as default suspend voltage */
246 suspend_state
->uV
= suspend_state
->min_uV
;
248 if (of_property_read_bool(suspend_np
,
249 "regulator-changeable-in-suspend"))
250 suspend_state
->changeable
= true;
252 if (i
== PM_SUSPEND_MEM
)
253 constraints
->initial_state
= PM_SUSPEND_MEM
;
255 of_node_put(suspend_np
);
256 suspend_state
= NULL
;
264 * of_get_regulator_init_data - extract regulator_init_data structure info
265 * @dev: device requesting for regulator_init_data
266 * @node: regulator device node
267 * @desc: regulator description
269 * Populates regulator_init_data structure by extracting data from device
270 * tree node, returns a pointer to the populated structure or NULL if memory
273 struct regulator_init_data
*of_get_regulator_init_data(struct device
*dev
,
274 struct device_node
*node
,
275 const struct regulator_desc
*desc
)
277 struct regulator_init_data
*init_data
;
282 init_data
= devm_kzalloc(dev
, sizeof(*init_data
), GFP_KERNEL
);
284 return NULL
; /* Out of memory? */
286 if (of_get_regulation_constraints(dev
, node
, &init_data
, desc
))
291 EXPORT_SYMBOL_GPL(of_get_regulator_init_data
);
293 struct devm_of_regulator_matches
{
294 struct of_regulator_match
*matches
;
295 unsigned int num_matches
;
298 static void devm_of_regulator_put_matches(struct device
*dev
, void *res
)
300 struct devm_of_regulator_matches
*devm_matches
= res
;
303 for (i
= 0; i
< devm_matches
->num_matches
; i
++)
304 of_node_put(devm_matches
->matches
[i
].of_node
);
308 * of_regulator_match - extract multiple regulator init data from device tree.
309 * @dev: device requesting the data
310 * @node: parent device node of the regulators
311 * @matches: match table for the regulators
312 * @num_matches: number of entries in match table
314 * This function uses a match table specified by the regulator driver to
315 * parse regulator init data from the device tree. @node is expected to
316 * contain a set of child nodes, each providing the init data for one
317 * regulator. The data parsed from a child node will be matched to a regulator
318 * based on either the deprecated property regulator-compatible if present,
319 * or otherwise the child node's name. Note that the match table is modified
320 * in place and an additional of_node reference is taken for each matched
323 * Returns the number of matches found or a negative error code on failure.
325 int of_regulator_match(struct device
*dev
, struct device_node
*node
,
326 struct of_regulator_match
*matches
,
327 unsigned int num_matches
)
329 unsigned int count
= 0;
332 struct device_node
*child
;
333 struct devm_of_regulator_matches
*devm_matches
;
338 devm_matches
= devres_alloc(devm_of_regulator_put_matches
,
339 sizeof(struct devm_of_regulator_matches
),
344 devm_matches
->matches
= matches
;
345 devm_matches
->num_matches
= num_matches
;
347 devres_add(dev
, devm_matches
);
349 for (i
= 0; i
< num_matches
; i
++) {
350 struct of_regulator_match
*match
= &matches
[i
];
351 match
->init_data
= NULL
;
352 match
->of_node
= NULL
;
355 for_each_child_of_node(node
, child
) {
356 name
= of_get_property(child
,
357 "regulator-compatible", NULL
);
360 for (i
= 0; i
< num_matches
; i
++) {
361 struct of_regulator_match
*match
= &matches
[i
];
365 if (strcmp(match
->name
, name
))
369 of_get_regulator_init_data(dev
, child
,
371 if (!match
->init_data
) {
373 "failed to parse DT for regulator %pOFn\n",
378 match
->of_node
= of_node_get(child
);
386 EXPORT_SYMBOL_GPL(of_regulator_match
);
389 device_node
*regulator_of_get_init_node(struct device
*dev
,
390 const struct regulator_desc
*desc
)
392 struct device_node
*search
, *child
;
395 if (!dev
->of_node
|| !desc
->of_match
)
398 if (desc
->regulators_node
) {
399 search
= of_get_child_by_name(dev
->of_node
,
400 desc
->regulators_node
);
402 search
= of_node_get(dev
->of_node
);
404 if (!strcmp(desc
->of_match
, search
->name
))
409 dev_dbg(dev
, "Failed to find regulator container node '%s'\n",
410 desc
->regulators_node
);
414 for_each_available_child_of_node(search
, child
) {
415 name
= of_get_property(child
, "regulator-compatible", NULL
);
417 if (!desc
->of_match_full_name
)
420 name
= child
->full_name
;
423 if (!strcmp(desc
->of_match
, name
)) {
425 return of_node_get(child
);
434 struct regulator_init_data
*regulator_of_get_init_data(struct device
*dev
,
435 const struct regulator_desc
*desc
,
436 struct regulator_config
*config
,
437 struct device_node
**node
)
439 struct device_node
*child
;
440 struct regulator_init_data
*init_data
= NULL
;
442 child
= regulator_of_get_init_node(dev
, desc
);
446 init_data
= of_get_regulator_init_data(dev
, child
, desc
);
448 dev_err(dev
, "failed to parse DT for regulator %pOFn\n", child
);
452 if (desc
->of_parse_cb
) {
455 ret
= desc
->of_parse_cb(child
, desc
, config
);
457 if (ret
== -EPROBE_DEFER
) {
459 return ERR_PTR(-EPROBE_DEFER
);
462 "driver callback failed to parse DT for regulator %pOFn\n",
478 struct regulator_dev
*of_find_regulator_by_node(struct device_node
*np
)
482 dev
= class_find_device_by_of_node(®ulator_class
, np
);
484 return dev
? dev_to_rdev(dev
) : NULL
;
488 * Returns number of regulators coupled with rdev.
490 int of_get_n_coupled(struct regulator_dev
*rdev
)
492 struct device_node
*node
= rdev
->dev
.of_node
;
495 n_phandles
= of_count_phandle_with_args(node
,
496 "regulator-coupled-with",
499 return (n_phandles
> 0) ? n_phandles
: 0;
502 /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
503 static bool of_coupling_find_node(struct device_node
*src
,
504 struct device_node
*to_find
,
510 n_phandles
= of_count_phandle_with_args(src
,
511 "regulator-coupled-with",
514 for (i
= 0; i
< n_phandles
; i
++) {
515 struct device_node
*tmp
= of_parse_phandle(src
,
516 "regulator-coupled-with", i
);
537 * of_check_coupling_data - Parse rdev's coupling properties and check data
539 * @rdev: pointer to regulator_dev whose data is checked
541 * Function checks if all the following conditions are met:
542 * - rdev's max_spread is greater than 0
543 * - all coupled regulators have the same max_spread
544 * - all coupled regulators have the same number of regulator_dev phandles
545 * - all regulators are linked to each other
547 * Returns true if all conditions are met.
549 bool of_check_coupling_data(struct regulator_dev
*rdev
)
551 struct device_node
*node
= rdev
->dev
.of_node
;
552 int n_phandles
= of_get_n_coupled(rdev
);
553 struct device_node
*c_node
;
558 /* iterate over rdev's phandles */
559 for (i
= 0; i
< n_phandles
; i
++) {
560 int max_spread
= rdev
->constraints
->max_spread
[i
];
561 int c_max_spread
, c_n_phandles
;
563 if (max_spread
<= 0) {
564 dev_err(&rdev
->dev
, "max_spread value invalid\n");
568 c_node
= of_parse_phandle(node
,
569 "regulator-coupled-with", i
);
574 c_n_phandles
= of_count_phandle_with_args(c_node
,
575 "regulator-coupled-with",
578 if (c_n_phandles
!= n_phandles
) {
579 dev_err(&rdev
->dev
, "number of coupled reg phandles mismatch\n");
584 if (!of_coupling_find_node(c_node
, node
, &index
)) {
585 dev_err(&rdev
->dev
, "missing 2-way linking for coupled regulators\n");
590 if (of_property_read_u32_index(c_node
, "regulator-coupled-max-spread",
591 index
, &c_max_spread
)) {
596 if (c_max_spread
!= max_spread
) {
598 "coupled regulators max_spread mismatch\n");
613 * of_parse_coupled regulator - Get regulator_dev pointer from rdev's property
614 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
615 * "regulator-coupled-with" property
616 * @index: Index in phandles array
618 * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
619 * registered, returns NULL
621 struct regulator_dev
*of_parse_coupled_regulator(struct regulator_dev
*rdev
,
624 struct device_node
*node
= rdev
->dev
.of_node
;
625 struct device_node
*c_node
;
626 struct regulator_dev
*c_rdev
;
628 c_node
= of_parse_phandle(node
, "regulator-coupled-with", index
);
632 c_rdev
= of_find_regulator_by_node(c_node
);