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 void fill_limit(int *limit
, int val
)
28 *limit
= REGULATOR_NOTIF_LIMIT_ENABLE
;
32 *limit
= REGULATOR_NOTIF_LIMIT_DISABLE
;
35 static void of_get_regulator_prot_limits(struct device_node
*np
,
36 struct regulation_constraints
*constraints
)
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",
46 struct notification_limit
*limits
[] = {
47 &constraints
->over_curr_limits
,
48 &constraints
->over_voltage_limits
,
49 &constraints
->temp_limits
,
50 &constraints
->under_voltage_limits
,
54 /* Protection limits: */
55 for (i
= 0; i
< ARRAY_SIZE(props
); i
++) {
59 static const char *const lvl
[] = {
60 "protection", "error", "warn"
63 &limits
[i
]->prot
, &limits
[i
]->err
, &limits
[i
]->warn
,
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
);
70 fill_limit(l
[j
], pval
);
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];
80 static int of_get_regulation_constraints(struct device
*dev
,
81 struct device_node
*np
,
82 struct regulator_init_data
**init_data
,
83 const struct regulator_desc
*desc
)
85 struct regulation_constraints
*constraints
= &(*init_data
)->constraints
;
86 struct regulator_state
*suspend_state
;
87 struct device_node
*suspend_np
;
93 n_phandles
= of_count_phandle_with_args(np
, "regulator-coupled-with",
95 n_phandles
= max(n_phandles
, 0);
97 constraints
->name
= of_get_property(np
, "regulator-name", NULL
);
99 if (!of_property_read_u32(np
, "regulator-min-microvolt", &pval
))
100 constraints
->min_uV
= pval
;
102 if (!of_property_read_u32(np
, "regulator-max-microvolt", &pval
))
103 constraints
->max_uV
= pval
;
105 /* Voltage change possible? */
106 if (constraints
->min_uV
!= constraints
->max_uV
)
107 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_VOLTAGE
;
109 /* Do we have a voltage range, if so try to apply it? */
110 if (constraints
->min_uV
&& constraints
->max_uV
)
111 constraints
->apply_uV
= true;
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
;
120 if (!of_property_read_u32(np
, "regulator-input-current-limit-microamp",
122 constraints
->ilim_uA
= pval
;
124 /* Current change possible? */
125 if (constraints
->min_uA
!= constraints
->max_uA
)
126 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_CURRENT
;
128 constraints
->boot_on
= of_property_read_bool(np
, "regulator-boot-on");
129 constraints
->always_on
= of_property_read_bool(np
, "regulator-always-on");
130 if (!constraints
->always_on
) /* status change should be possible. */
131 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_STATUS
;
133 constraints
->pull_down
= of_property_read_bool(np
, "regulator-pull-down");
135 if (of_property_read_bool(np
, "regulator-allow-bypass"))
136 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_BYPASS
;
138 if (of_property_read_bool(np
, "regulator-allow-set-load"))
139 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_DRMS
;
141 ret
= of_property_read_u32(np
, "regulator-ramp-delay", &pval
);
144 constraints
->ramp_delay
= pval
;
146 constraints
->ramp_disable
= true;
149 ret
= of_property_read_u32(np
, "regulator-settling-time-us", &pval
);
151 constraints
->settling_time
= pval
;
153 ret
= of_property_read_u32(np
, "regulator-settling-time-up-us", &pval
);
155 constraints
->settling_time_up
= pval
;
156 if (constraints
->settling_time_up
&& constraints
->settling_time
) {
157 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
159 constraints
->settling_time_up
= 0;
162 ret
= of_property_read_u32(np
, "regulator-settling-time-down-us",
165 constraints
->settling_time_down
= pval
;
166 if (constraints
->settling_time_down
&& constraints
->settling_time
) {
167 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
169 constraints
->settling_time_down
= 0;
172 ret
= of_property_read_u32(np
, "regulator-enable-ramp-delay", &pval
);
174 constraints
->enable_time
= pval
;
176 constraints
->soft_start
= of_property_read_bool(np
,
177 "regulator-soft-start");
178 ret
= of_property_read_u32(np
, "regulator-active-discharge", &pval
);
180 constraints
->active_discharge
=
181 (pval
) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE
:
182 REGULATOR_ACTIVE_DISCHARGE_DISABLE
;
185 if (!of_property_read_u32(np
, "regulator-initial-mode", &pval
)) {
186 if (desc
&& desc
->of_map_mode
) {
187 mode
= desc
->of_map_mode(pval
);
188 if (mode
== REGULATOR_MODE_INVALID
)
189 pr_err("%pOFn: invalid mode %u\n", np
, pval
);
191 constraints
->initial_mode
= mode
;
193 pr_warn("%pOFn: mapping for mode %d not defined\n",
198 len
= of_property_count_elems_of_size(np
, "regulator-allowed-modes",
201 if (desc
&& desc
->of_map_mode
) {
202 for (i
= 0; i
< len
; i
++) {
203 ret
= of_property_read_u32_index(np
,
204 "regulator-allowed-modes", i
, &pval
);
206 pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
210 mode
= desc
->of_map_mode(pval
);
211 if (mode
== REGULATOR_MODE_INVALID
)
212 pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
215 constraints
->valid_modes_mask
|= mode
;
217 if (constraints
->valid_modes_mask
)
218 constraints
->valid_ops_mask
219 |= REGULATOR_CHANGE_MODE
;
221 pr_warn("%pOFn: mode mapping not defined\n", np
);
225 if (!of_property_read_u32(np
, "regulator-system-load", &pval
))
226 constraints
->system_load
= pval
;
229 constraints
->max_spread
= devm_kzalloc(dev
,
230 sizeof(*constraints
->max_spread
) * n_phandles
,
233 if (!constraints
->max_spread
)
236 of_property_read_u32_array(np
, "regulator-coupled-max-spread",
237 constraints
->max_spread
, n_phandles
);
240 if (!of_property_read_u32(np
, "regulator-max-step-microvolt",
242 constraints
->max_uV_step
= pval
;
244 constraints
->over_current_protection
= of_property_read_bool(np
,
245 "regulator-over-current-protection");
247 of_get_regulator_prot_limits(np
, constraints
);
249 for (i
= 0; i
< ARRAY_SIZE(regulator_states
); i
++) {
252 suspend_state
= &constraints
->state_mem
;
255 suspend_state
= &constraints
->state_disk
;
257 case PM_SUSPEND_STANDBY
:
258 suspend_state
= &constraints
->state_standby
;
261 case PM_SUSPEND_TO_IDLE
:
266 suspend_np
= of_get_child_by_name(np
, regulator_states
[i
]);
267 if (!suspend_np
|| !suspend_state
)
270 if (!of_property_read_u32(suspend_np
, "regulator-mode",
272 if (desc
&& desc
->of_map_mode
) {
273 mode
= desc
->of_map_mode(pval
);
274 if (mode
== REGULATOR_MODE_INVALID
)
275 pr_err("%pOFn: invalid mode %u\n",
278 suspend_state
->mode
= mode
;
280 pr_warn("%pOFn: mapping for mode %d not defined\n",
285 if (of_property_read_bool(suspend_np
,
286 "regulator-on-in-suspend"))
287 suspend_state
->enabled
= ENABLE_IN_SUSPEND
;
288 else if (of_property_read_bool(suspend_np
,
289 "regulator-off-in-suspend"))
290 suspend_state
->enabled
= DISABLE_IN_SUSPEND
;
292 if (!of_property_read_u32(suspend_np
,
293 "regulator-suspend-min-microvolt", &pval
))
294 suspend_state
->min_uV
= pval
;
296 if (!of_property_read_u32(suspend_np
,
297 "regulator-suspend-max-microvolt", &pval
))
298 suspend_state
->max_uV
= pval
;
300 if (!of_property_read_u32(suspend_np
,
301 "regulator-suspend-microvolt", &pval
))
302 suspend_state
->uV
= pval
;
303 else /* otherwise use min_uV as default suspend voltage */
304 suspend_state
->uV
= suspend_state
->min_uV
;
306 if (of_property_read_bool(suspend_np
,
307 "regulator-changeable-in-suspend"))
308 suspend_state
->changeable
= true;
310 if (i
== PM_SUSPEND_MEM
)
311 constraints
->initial_state
= PM_SUSPEND_MEM
;
313 of_node_put(suspend_np
);
314 suspend_state
= NULL
;
322 * of_get_regulator_init_data - extract regulator_init_data structure info
323 * @dev: device requesting for regulator_init_data
324 * @node: regulator device node
325 * @desc: regulator description
327 * Populates regulator_init_data structure by extracting data from device
328 * tree node, returns a pointer to the populated structure or NULL if memory
331 struct regulator_init_data
*of_get_regulator_init_data(struct device
*dev
,
332 struct device_node
*node
,
333 const struct regulator_desc
*desc
)
335 struct regulator_init_data
*init_data
;
340 init_data
= devm_kzalloc(dev
, sizeof(*init_data
), GFP_KERNEL
);
342 return NULL
; /* Out of memory? */
344 if (of_get_regulation_constraints(dev
, node
, &init_data
, desc
))
349 EXPORT_SYMBOL_GPL(of_get_regulator_init_data
);
351 struct devm_of_regulator_matches
{
352 struct of_regulator_match
*matches
;
353 unsigned int num_matches
;
356 static void devm_of_regulator_put_matches(struct device
*dev
, void *res
)
358 struct devm_of_regulator_matches
*devm_matches
= res
;
361 for (i
= 0; i
< devm_matches
->num_matches
; i
++)
362 of_node_put(devm_matches
->matches
[i
].of_node
);
366 * of_regulator_match - extract multiple regulator init data from device tree.
367 * @dev: device requesting the data
368 * @node: parent device node of the regulators
369 * @matches: match table for the regulators
370 * @num_matches: number of entries in match table
372 * This function uses a match table specified by the regulator driver to
373 * parse regulator init data from the device tree. @node is expected to
374 * contain a set of child nodes, each providing the init data for one
375 * regulator. The data parsed from a child node will be matched to a regulator
376 * based on either the deprecated property regulator-compatible if present,
377 * or otherwise the child node's name. Note that the match table is modified
378 * in place and an additional of_node reference is taken for each matched
381 * Returns the number of matches found or a negative error code on failure.
383 int of_regulator_match(struct device
*dev
, struct device_node
*node
,
384 struct of_regulator_match
*matches
,
385 unsigned int num_matches
)
387 unsigned int count
= 0;
390 struct device_node
*child
;
391 struct devm_of_regulator_matches
*devm_matches
;
396 devm_matches
= devres_alloc(devm_of_regulator_put_matches
,
397 sizeof(struct devm_of_regulator_matches
),
402 devm_matches
->matches
= matches
;
403 devm_matches
->num_matches
= num_matches
;
405 devres_add(dev
, devm_matches
);
407 for (i
= 0; i
< num_matches
; i
++) {
408 struct of_regulator_match
*match
= &matches
[i
];
409 match
->init_data
= NULL
;
410 match
->of_node
= NULL
;
413 for_each_child_of_node(node
, child
) {
414 name
= of_get_property(child
,
415 "regulator-compatible", NULL
);
418 for (i
= 0; i
< num_matches
; i
++) {
419 struct of_regulator_match
*match
= &matches
[i
];
423 if (strcmp(match
->name
, name
))
427 of_get_regulator_init_data(dev
, child
,
429 if (!match
->init_data
) {
431 "failed to parse DT for regulator %pOFn\n",
436 match
->of_node
= of_node_get(child
);
444 EXPORT_SYMBOL_GPL(of_regulator_match
);
447 device_node
*regulator_of_get_init_node(struct device
*dev
,
448 const struct regulator_desc
*desc
)
450 struct device_node
*search
, *child
;
453 if (!dev
->of_node
|| !desc
->of_match
)
456 if (desc
->regulators_node
) {
457 search
= of_get_child_by_name(dev
->of_node
,
458 desc
->regulators_node
);
460 search
= of_node_get(dev
->of_node
);
462 if (!strcmp(desc
->of_match
, search
->name
))
467 dev_dbg(dev
, "Failed to find regulator container node '%s'\n",
468 desc
->regulators_node
);
472 for_each_available_child_of_node(search
, child
) {
473 name
= of_get_property(child
, "regulator-compatible", NULL
);
475 if (!desc
->of_match_full_name
)
478 name
= child
->full_name
;
481 if (!strcmp(desc
->of_match
, name
)) {
484 * 'of_node_get(child)' is already performed by the
496 struct regulator_init_data
*regulator_of_get_init_data(struct device
*dev
,
497 const struct regulator_desc
*desc
,
498 struct regulator_config
*config
,
499 struct device_node
**node
)
501 struct device_node
*child
;
502 struct regulator_init_data
*init_data
= NULL
;
504 child
= regulator_of_get_init_node(dev
, desc
);
508 init_data
= of_get_regulator_init_data(dev
, child
, desc
);
510 dev_err(dev
, "failed to parse DT for regulator %pOFn\n", child
);
514 if (desc
->of_parse_cb
) {
517 ret
= desc
->of_parse_cb(child
, desc
, config
);
519 if (ret
== -EPROBE_DEFER
) {
521 return ERR_PTR(-EPROBE_DEFER
);
524 "driver callback failed to parse DT for regulator %pOFn\n",
540 struct regulator_dev
*of_find_regulator_by_node(struct device_node
*np
)
544 dev
= class_find_device_by_of_node(®ulator_class
, np
);
546 return dev
? dev_to_rdev(dev
) : NULL
;
550 * Returns number of regulators coupled with rdev.
552 int of_get_n_coupled(struct regulator_dev
*rdev
)
554 struct device_node
*node
= rdev
->dev
.of_node
;
557 n_phandles
= of_count_phandle_with_args(node
,
558 "regulator-coupled-with",
561 return (n_phandles
> 0) ? n_phandles
: 0;
564 /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
565 static bool of_coupling_find_node(struct device_node
*src
,
566 struct device_node
*to_find
,
572 n_phandles
= of_count_phandle_with_args(src
,
573 "regulator-coupled-with",
576 for (i
= 0; i
< n_phandles
; i
++) {
577 struct device_node
*tmp
= of_parse_phandle(src
,
578 "regulator-coupled-with", i
);
599 * of_check_coupling_data - Parse rdev's coupling properties and check data
601 * @rdev: pointer to regulator_dev whose data is checked
603 * Function checks if all the following conditions are met:
604 * - rdev's max_spread is greater than 0
605 * - all coupled regulators have the same max_spread
606 * - all coupled regulators have the same number of regulator_dev phandles
607 * - all regulators are linked to each other
609 * Returns true if all conditions are met.
611 bool of_check_coupling_data(struct regulator_dev
*rdev
)
613 struct device_node
*node
= rdev
->dev
.of_node
;
614 int n_phandles
= of_get_n_coupled(rdev
);
615 struct device_node
*c_node
;
620 /* iterate over rdev's phandles */
621 for (i
= 0; i
< n_phandles
; i
++) {
622 int max_spread
= rdev
->constraints
->max_spread
[i
];
623 int c_max_spread
, c_n_phandles
;
625 if (max_spread
<= 0) {
626 dev_err(&rdev
->dev
, "max_spread value invalid\n");
630 c_node
= of_parse_phandle(node
,
631 "regulator-coupled-with", i
);
636 c_n_phandles
= of_count_phandle_with_args(c_node
,
637 "regulator-coupled-with",
640 if (c_n_phandles
!= n_phandles
) {
641 dev_err(&rdev
->dev
, "number of coupled reg phandles mismatch\n");
646 if (!of_coupling_find_node(c_node
, node
, &index
)) {
647 dev_err(&rdev
->dev
, "missing 2-way linking for coupled regulators\n");
652 if (of_property_read_u32_index(c_node
, "regulator-coupled-max-spread",
653 index
, &c_max_spread
)) {
658 if (c_max_spread
!= max_spread
) {
660 "coupled regulators max_spread mismatch\n");
675 * of_parse_coupled regulator - Get regulator_dev pointer from rdev's property
676 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
677 * "regulator-coupled-with" property
678 * @index: Index in phandles array
680 * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
681 * registered, returns NULL
683 struct regulator_dev
*of_parse_coupled_regulator(struct regulator_dev
*rdev
,
686 struct device_node
*node
= rdev
->dev
.of_node
;
687 struct device_node
*c_node
;
688 struct regulator_dev
*c_rdev
;
690 c_node
= of_parse_phandle(node
, "regulator-coupled-with", index
);
694 c_rdev
= of_find_regulator_by_node(c_node
);