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");
134 constraints
->system_critical
= of_property_read_bool(np
,
135 "system-critical-regulator");
137 if (of_property_read_bool(np
, "regulator-allow-bypass"))
138 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_BYPASS
;
140 if (of_property_read_bool(np
, "regulator-allow-set-load"))
141 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_DRMS
;
143 ret
= of_property_read_u32(np
, "regulator-ramp-delay", &pval
);
146 constraints
->ramp_delay
= pval
;
148 constraints
->ramp_disable
= true;
151 ret
= of_property_read_u32(np
, "regulator-settling-time-us", &pval
);
153 constraints
->settling_time
= pval
;
155 ret
= of_property_read_u32(np
, "regulator-settling-time-up-us", &pval
);
157 constraints
->settling_time_up
= pval
;
158 if (constraints
->settling_time_up
&& constraints
->settling_time
) {
159 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
161 constraints
->settling_time_up
= 0;
164 ret
= of_property_read_u32(np
, "regulator-settling-time-down-us",
167 constraints
->settling_time_down
= pval
;
168 if (constraints
->settling_time_down
&& constraints
->settling_time
) {
169 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
171 constraints
->settling_time_down
= 0;
174 ret
= of_property_read_u32(np
, "regulator-enable-ramp-delay", &pval
);
176 constraints
->enable_time
= pval
;
178 ret
= of_property_read_u32(np
, "regulator-uv-survival-time-ms", &pval
);
180 constraints
->uv_less_critical_window_ms
= pval
;
182 constraints
->uv_less_critical_window_ms
=
183 REGULATOR_DEF_UV_LESS_CRITICAL_WINDOW_MS
;
185 constraints
->soft_start
= of_property_read_bool(np
,
186 "regulator-soft-start");
187 ret
= of_property_read_u32(np
, "regulator-active-discharge", &pval
);
189 constraints
->active_discharge
=
190 (pval
) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE
:
191 REGULATOR_ACTIVE_DISCHARGE_DISABLE
;
194 if (!of_property_read_u32(np
, "regulator-initial-mode", &pval
)) {
195 if (desc
&& desc
->of_map_mode
) {
196 mode
= desc
->of_map_mode(pval
);
197 if (mode
== REGULATOR_MODE_INVALID
)
198 pr_err("%pOFn: invalid mode %u\n", np
, pval
);
200 constraints
->initial_mode
= mode
;
202 pr_warn("%pOFn: mapping for mode %d not defined\n",
207 len
= of_property_count_elems_of_size(np
, "regulator-allowed-modes",
210 if (desc
&& desc
->of_map_mode
) {
211 for (i
= 0; i
< len
; i
++) {
212 ret
= of_property_read_u32_index(np
,
213 "regulator-allowed-modes", i
, &pval
);
215 pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
219 mode
= desc
->of_map_mode(pval
);
220 if (mode
== REGULATOR_MODE_INVALID
)
221 pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
224 constraints
->valid_modes_mask
|= mode
;
226 if (constraints
->valid_modes_mask
)
227 constraints
->valid_ops_mask
228 |= REGULATOR_CHANGE_MODE
;
230 pr_warn("%pOFn: mode mapping not defined\n", np
);
234 if (!of_property_read_u32(np
, "regulator-system-load", &pval
))
235 constraints
->system_load
= pval
;
238 constraints
->max_spread
= devm_kzalloc(dev
,
239 sizeof(*constraints
->max_spread
) * n_phandles
,
242 if (!constraints
->max_spread
)
245 of_property_read_u32_array(np
, "regulator-coupled-max-spread",
246 constraints
->max_spread
, n_phandles
);
249 if (!of_property_read_u32(np
, "regulator-max-step-microvolt",
251 constraints
->max_uV_step
= pval
;
253 constraints
->over_current_protection
= of_property_read_bool(np
,
254 "regulator-over-current-protection");
256 of_get_regulator_prot_limits(np
, constraints
);
258 for (i
= 0; i
< ARRAY_SIZE(regulator_states
); i
++) {
261 suspend_state
= &constraints
->state_mem
;
264 suspend_state
= &constraints
->state_disk
;
266 case PM_SUSPEND_STANDBY
:
267 suspend_state
= &constraints
->state_standby
;
270 case PM_SUSPEND_TO_IDLE
:
275 suspend_np
= of_get_child_by_name(np
, regulator_states
[i
]);
278 if (!suspend_state
) {
279 of_node_put(suspend_np
);
283 if (!of_property_read_u32(suspend_np
, "regulator-mode",
285 if (desc
&& desc
->of_map_mode
) {
286 mode
= desc
->of_map_mode(pval
);
287 if (mode
== REGULATOR_MODE_INVALID
)
288 pr_err("%pOFn: invalid mode %u\n",
291 suspend_state
->mode
= mode
;
293 pr_warn("%pOFn: mapping for mode %d not defined\n",
298 if (of_property_read_bool(suspend_np
,
299 "regulator-on-in-suspend"))
300 suspend_state
->enabled
= ENABLE_IN_SUSPEND
;
301 else if (of_property_read_bool(suspend_np
,
302 "regulator-off-in-suspend"))
303 suspend_state
->enabled
= DISABLE_IN_SUSPEND
;
305 if (!of_property_read_u32(suspend_np
,
306 "regulator-suspend-min-microvolt", &pval
))
307 suspend_state
->min_uV
= pval
;
309 if (!of_property_read_u32(suspend_np
,
310 "regulator-suspend-max-microvolt", &pval
))
311 suspend_state
->max_uV
= pval
;
313 if (!of_property_read_u32(suspend_np
,
314 "regulator-suspend-microvolt", &pval
))
315 suspend_state
->uV
= pval
;
316 else /* otherwise use min_uV as default suspend voltage */
317 suspend_state
->uV
= suspend_state
->min_uV
;
319 if (of_property_read_bool(suspend_np
,
320 "regulator-changeable-in-suspend"))
321 suspend_state
->changeable
= true;
323 if (i
== PM_SUSPEND_MEM
)
324 constraints
->initial_state
= PM_SUSPEND_MEM
;
326 of_node_put(suspend_np
);
327 suspend_state
= NULL
;
335 * of_get_regulator_init_data - extract regulator_init_data structure info
336 * @dev: device requesting for regulator_init_data
337 * @node: regulator device node
338 * @desc: regulator description
340 * Populates regulator_init_data structure by extracting data from device
341 * tree node, returns a pointer to the populated structure or NULL if memory
344 struct regulator_init_data
*of_get_regulator_init_data(struct device
*dev
,
345 struct device_node
*node
,
346 const struct regulator_desc
*desc
)
348 struct regulator_init_data
*init_data
;
353 init_data
= devm_kzalloc(dev
, sizeof(*init_data
), GFP_KERNEL
);
355 return NULL
; /* Out of memory? */
357 if (of_get_regulation_constraints(dev
, node
, &init_data
, desc
))
362 EXPORT_SYMBOL_GPL(of_get_regulator_init_data
);
364 struct devm_of_regulator_matches
{
365 struct of_regulator_match
*matches
;
366 unsigned int num_matches
;
369 static void devm_of_regulator_put_matches(struct device
*dev
, void *res
)
371 struct devm_of_regulator_matches
*devm_matches
= res
;
374 for (i
= 0; i
< devm_matches
->num_matches
; i
++)
375 of_node_put(devm_matches
->matches
[i
].of_node
);
379 * of_regulator_match - extract multiple regulator init data from device tree.
380 * @dev: device requesting the data
381 * @node: parent device node of the regulators
382 * @matches: match table for the regulators
383 * @num_matches: number of entries in match table
385 * This function uses a match table specified by the regulator driver to
386 * parse regulator init data from the device tree. @node is expected to
387 * contain a set of child nodes, each providing the init data for one
388 * regulator. The data parsed from a child node will be matched to a regulator
389 * based on either the deprecated property regulator-compatible if present,
390 * or otherwise the child node's name. Note that the match table is modified
391 * in place and an additional of_node reference is taken for each matched
394 * Returns the number of matches found or a negative error code on failure.
396 int of_regulator_match(struct device
*dev
, struct device_node
*node
,
397 struct of_regulator_match
*matches
,
398 unsigned int num_matches
)
400 unsigned int count
= 0;
403 struct device_node
*child
;
404 struct devm_of_regulator_matches
*devm_matches
;
409 devm_matches
= devres_alloc(devm_of_regulator_put_matches
,
410 sizeof(struct devm_of_regulator_matches
),
415 devm_matches
->matches
= matches
;
416 devm_matches
->num_matches
= num_matches
;
418 devres_add(dev
, devm_matches
);
420 for (i
= 0; i
< num_matches
; i
++) {
421 struct of_regulator_match
*match
= &matches
[i
];
422 match
->init_data
= NULL
;
423 match
->of_node
= NULL
;
426 for_each_child_of_node(node
, child
) {
427 name
= of_get_property(child
,
428 "regulator-compatible", NULL
);
431 for (i
= 0; i
< num_matches
; i
++) {
432 struct of_regulator_match
*match
= &matches
[i
];
436 if (strcmp(match
->name
, name
))
440 of_get_regulator_init_data(dev
, child
,
442 if (!match
->init_data
) {
444 "failed to parse DT for regulator %pOFn\n",
449 match
->of_node
= of_node_get(child
);
457 EXPORT_SYMBOL_GPL(of_regulator_match
);
460 device_node
*regulator_of_get_init_node(struct device
*dev
,
461 const struct regulator_desc
*desc
)
463 struct device_node
*search
, *child
;
466 if (!dev
->of_node
|| !desc
->of_match
)
469 if (desc
->regulators_node
) {
470 search
= of_get_child_by_name(dev
->of_node
,
471 desc
->regulators_node
);
473 search
= of_node_get(dev
->of_node
);
475 if (!strcmp(desc
->of_match
, search
->name
))
480 dev_dbg(dev
, "Failed to find regulator container node '%s'\n",
481 desc
->regulators_node
);
485 for_each_available_child_of_node(search
, child
) {
486 name
= of_get_property(child
, "regulator-compatible", NULL
);
488 if (!desc
->of_match_full_name
)
491 name
= child
->full_name
;
494 if (!strcmp(desc
->of_match
, name
)) {
497 * 'of_node_get(child)' is already performed by the
509 struct regulator_init_data
*regulator_of_get_init_data(struct device
*dev
,
510 const struct regulator_desc
*desc
,
511 struct regulator_config
*config
,
512 struct device_node
**node
)
514 struct device_node
*child
;
515 struct regulator_init_data
*init_data
= NULL
;
517 child
= regulator_of_get_init_node(config
->dev
, desc
);
521 init_data
= of_get_regulator_init_data(dev
, child
, desc
);
523 dev_err(dev
, "failed to parse DT for regulator %pOFn\n", child
);
527 if (desc
->of_parse_cb
) {
530 ret
= desc
->of_parse_cb(child
, desc
, config
);
532 if (ret
== -EPROBE_DEFER
) {
534 return ERR_PTR(-EPROBE_DEFER
);
537 "driver callback failed to parse DT for regulator %pOFn\n",
553 struct regulator_dev
*of_find_regulator_by_node(struct device_node
*np
)
557 dev
= class_find_device_by_of_node(®ulator_class
, np
);
559 return dev
? dev_to_rdev(dev
) : NULL
;
563 * Returns number of regulators coupled with rdev.
565 int of_get_n_coupled(struct regulator_dev
*rdev
)
567 struct device_node
*node
= rdev
->dev
.of_node
;
570 n_phandles
= of_count_phandle_with_args(node
,
571 "regulator-coupled-with",
574 return (n_phandles
> 0) ? n_phandles
: 0;
577 /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
578 static bool of_coupling_find_node(struct device_node
*src
,
579 struct device_node
*to_find
,
585 n_phandles
= of_count_phandle_with_args(src
,
586 "regulator-coupled-with",
589 for (i
= 0; i
< n_phandles
; i
++) {
590 struct device_node
*tmp
= of_parse_phandle(src
,
591 "regulator-coupled-with", i
);
612 * of_check_coupling_data - Parse rdev's coupling properties and check data
614 * @rdev: pointer to regulator_dev whose data is checked
616 * Function checks if all the following conditions are met:
617 * - rdev's max_spread is greater than 0
618 * - all coupled regulators have the same max_spread
619 * - all coupled regulators have the same number of regulator_dev phandles
620 * - all regulators are linked to each other
622 * Returns true if all conditions are met.
624 bool of_check_coupling_data(struct regulator_dev
*rdev
)
626 struct device_node
*node
= rdev
->dev
.of_node
;
627 int n_phandles
= of_get_n_coupled(rdev
);
628 struct device_node
*c_node
;
633 /* iterate over rdev's phandles */
634 for (i
= 0; i
< n_phandles
; i
++) {
635 int max_spread
= rdev
->constraints
->max_spread
[i
];
636 int c_max_spread
, c_n_phandles
;
638 if (max_spread
<= 0) {
639 dev_err(&rdev
->dev
, "max_spread value invalid\n");
643 c_node
= of_parse_phandle(node
,
644 "regulator-coupled-with", i
);
649 c_n_phandles
= of_count_phandle_with_args(c_node
,
650 "regulator-coupled-with",
653 if (c_n_phandles
!= n_phandles
) {
654 dev_err(&rdev
->dev
, "number of coupled reg phandles mismatch\n");
659 if (!of_coupling_find_node(c_node
, node
, &index
)) {
660 dev_err(&rdev
->dev
, "missing 2-way linking for coupled regulators\n");
665 if (of_property_read_u32_index(c_node
, "regulator-coupled-max-spread",
666 index
, &c_max_spread
)) {
671 if (c_max_spread
!= max_spread
) {
673 "coupled regulators max_spread mismatch\n");
688 * of_parse_coupled_regulator() - Get regulator_dev pointer from rdev's property
689 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
690 * "regulator-coupled-with" property
691 * @index: Index in phandles array
693 * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
694 * registered, returns NULL
696 struct regulator_dev
*of_parse_coupled_regulator(struct regulator_dev
*rdev
,
699 struct device_node
*node
= rdev
->dev
.of_node
;
700 struct device_node
*c_node
;
701 struct regulator_dev
*c_rdev
;
703 c_node
= of_parse_phandle(node
, "regulator-coupled-with", index
);
707 c_rdev
= of_find_regulator_by_node(c_node
);
715 * Check if name is a supply name according to the '*-supply' pattern
717 * return length of supply name without the -supply
719 static int is_supply_name(const char *name
)
724 /* string need to be at minimum len(x-supply) */
727 for (i
= strs
- 6; i
> 0; i
--) {
728 /* find first '-' and check if right part is supply */
731 if (strcmp(name
+ i
+ 1, "supply") != 0)
739 * of_regulator_bulk_get_all - get multiple regulator consumers
741 * @dev: Device to supply
742 * @np: device node to search for consumers
743 * @consumers: Configuration of consumers; clients are stored here.
745 * @return number of regulators on success, an errno on failure.
747 * This helper function allows drivers to get several regulator
748 * consumers in one operation. If any of the regulators cannot be
749 * acquired then any regulators that were allocated will be freed
750 * before returning to the caller.
752 int of_regulator_bulk_get_all(struct device
*dev
, struct device_node
*np
,
753 struct regulator_bulk_data
**consumers
)
755 int num_consumers
= 0;
756 struct regulator
*tmp
;
757 struct property
*prop
;
764 * first pass: get numbers of xxx-supply
765 * second pass: fill consumers
768 for_each_property_of_node(np
, prop
) {
769 i
= is_supply_name(prop
->name
);
776 memcpy(name
, prop
->name
, i
);
778 tmp
= regulator_get(dev
, name
);
783 (*consumers
)[n
].consumer
= tmp
;
789 return num_consumers
;
790 if (num_consumers
== 0)
792 *consumers
= kmalloc_array(num_consumers
,
793 sizeof(struct regulator_bulk_data
),
801 regulator_put(consumers
[n
]->consumer
);
804 EXPORT_SYMBOL_GPL(of_regulator_bulk_get_all
);