2 * OF helpers for regulator framework
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Rajendra Nayak <rnayak@ti.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/slab.h>
16 #include <linux/regulator/machine.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/of_regulator.h>
22 static const char *const regulator_states
[PM_SUSPEND_MAX
+ 1] = {
23 [PM_SUSPEND_STANDBY
] = "regulator-state-standby",
24 [PM_SUSPEND_MEM
] = "regulator-state-mem",
25 [PM_SUSPEND_MAX
] = "regulator-state-disk",
28 static void of_get_regulation_constraints(struct device_node
*np
,
29 struct regulator_init_data
**init_data
,
30 const struct regulator_desc
*desc
)
32 struct regulation_constraints
*constraints
= &(*init_data
)->constraints
;
33 struct regulator_state
*suspend_state
;
34 struct device_node
*suspend_np
;
39 constraints
->name
= of_get_property(np
, "regulator-name", NULL
);
41 if (!of_property_read_u32(np
, "regulator-min-microvolt", &pval
))
42 constraints
->min_uV
= pval
;
44 if (!of_property_read_u32(np
, "regulator-max-microvolt", &pval
))
45 constraints
->max_uV
= pval
;
47 /* Voltage change possible? */
48 if (constraints
->min_uV
!= constraints
->max_uV
)
49 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_VOLTAGE
;
51 /* Do we have a voltage range, if so try to apply it? */
52 if (constraints
->min_uV
&& constraints
->max_uV
)
53 constraints
->apply_uV
= true;
55 if (!of_property_read_u32(np
, "regulator-microvolt-offset", &pval
))
56 constraints
->uV_offset
= pval
;
57 if (!of_property_read_u32(np
, "regulator-min-microamp", &pval
))
58 constraints
->min_uA
= pval
;
59 if (!of_property_read_u32(np
, "regulator-max-microamp", &pval
))
60 constraints
->max_uA
= pval
;
62 if (!of_property_read_u32(np
, "regulator-input-current-limit-microamp",
64 constraints
->ilim_uA
= pval
;
66 /* Current change possible? */
67 if (constraints
->min_uA
!= constraints
->max_uA
)
68 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_CURRENT
;
70 constraints
->boot_on
= of_property_read_bool(np
, "regulator-boot-on");
71 constraints
->always_on
= of_property_read_bool(np
, "regulator-always-on");
72 if (!constraints
->always_on
) /* status change should be possible. */
73 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_STATUS
;
75 constraints
->pull_down
= of_property_read_bool(np
, "regulator-pull-down");
77 if (of_property_read_bool(np
, "regulator-allow-bypass"))
78 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_BYPASS
;
80 if (of_property_read_bool(np
, "regulator-allow-set-load"))
81 constraints
->valid_ops_mask
|= REGULATOR_CHANGE_DRMS
;
83 ret
= of_property_read_u32(np
, "regulator-ramp-delay", &pval
);
86 constraints
->ramp_delay
= pval
;
88 constraints
->ramp_disable
= true;
91 ret
= of_property_read_u32(np
, "regulator-settling-time-us", &pval
);
93 constraints
->settling_time
= pval
;
95 ret
= of_property_read_u32(np
, "regulator-settling-time-up-us", &pval
);
97 constraints
->settling_time_up
= pval
;
98 if (constraints
->settling_time_up
&& constraints
->settling_time
) {
99 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
101 constraints
->settling_time_up
= 0;
104 ret
= of_property_read_u32(np
, "regulator-settling-time-down-us",
107 constraints
->settling_time_down
= pval
;
108 if (constraints
->settling_time_down
&& constraints
->settling_time
) {
109 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
111 constraints
->settling_time_down
= 0;
114 ret
= of_property_read_u32(np
, "regulator-enable-ramp-delay", &pval
);
116 constraints
->enable_time
= pval
;
118 constraints
->soft_start
= of_property_read_bool(np
,
119 "regulator-soft-start");
120 ret
= of_property_read_u32(np
, "regulator-active-discharge", &pval
);
122 constraints
->active_discharge
=
123 (pval
) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE
:
124 REGULATOR_ACTIVE_DISCHARGE_DISABLE
;
127 if (!of_property_read_u32(np
, "regulator-initial-mode", &pval
)) {
128 if (desc
&& desc
->of_map_mode
) {
129 mode
= desc
->of_map_mode(pval
);
130 if (mode
== REGULATOR_MODE_INVALID
)
131 pr_err("%pOFn: invalid mode %u\n", np
, pval
);
133 constraints
->initial_mode
= mode
;
135 pr_warn("%pOFn: mapping for mode %d not defined\n",
140 len
= of_property_count_elems_of_size(np
, "regulator-allowed-modes",
143 if (desc
&& desc
->of_map_mode
) {
144 for (i
= 0; i
< len
; i
++) {
145 ret
= of_property_read_u32_index(np
,
146 "regulator-allowed-modes", i
, &pval
);
148 pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
152 mode
= desc
->of_map_mode(pval
);
153 if (mode
== REGULATOR_MODE_INVALID
)
154 pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
157 constraints
->valid_modes_mask
|= mode
;
159 if (constraints
->valid_modes_mask
)
160 constraints
->valid_ops_mask
161 |= REGULATOR_CHANGE_MODE
;
163 pr_warn("%pOFn: mode mapping not defined\n", np
);
167 if (!of_property_read_u32(np
, "regulator-system-load", &pval
))
168 constraints
->system_load
= pval
;
170 if (!of_property_read_u32(np
, "regulator-coupled-max-spread",
172 constraints
->max_spread
= pval
;
174 if (!of_property_read_u32(np
, "regulator-max-step-microvolt",
176 constraints
->max_uV_step
= pval
;
178 constraints
->over_current_protection
= of_property_read_bool(np
,
179 "regulator-over-current-protection");
181 for (i
= 0; i
< ARRAY_SIZE(regulator_states
); i
++) {
184 suspend_state
= &constraints
->state_mem
;
187 suspend_state
= &constraints
->state_disk
;
189 case PM_SUSPEND_STANDBY
:
190 suspend_state
= &constraints
->state_standby
;
193 case PM_SUSPEND_TO_IDLE
:
198 suspend_np
= of_get_child_by_name(np
, regulator_states
[i
]);
199 if (!suspend_np
|| !suspend_state
)
202 if (!of_property_read_u32(suspend_np
, "regulator-mode",
204 if (desc
&& desc
->of_map_mode
) {
205 mode
= desc
->of_map_mode(pval
);
206 if (mode
== REGULATOR_MODE_INVALID
)
207 pr_err("%pOFn: invalid mode %u\n",
210 suspend_state
->mode
= mode
;
212 pr_warn("%pOFn: mapping for mode %d not defined\n",
217 if (of_property_read_bool(suspend_np
,
218 "regulator-on-in-suspend"))
219 suspend_state
->enabled
= ENABLE_IN_SUSPEND
;
220 else if (of_property_read_bool(suspend_np
,
221 "regulator-off-in-suspend"))
222 suspend_state
->enabled
= DISABLE_IN_SUSPEND
;
224 if (!of_property_read_u32(np
, "regulator-suspend-min-microvolt",
226 suspend_state
->min_uV
= pval
;
228 if (!of_property_read_u32(np
, "regulator-suspend-max-microvolt",
230 suspend_state
->max_uV
= pval
;
232 if (!of_property_read_u32(suspend_np
,
233 "regulator-suspend-microvolt", &pval
))
234 suspend_state
->uV
= pval
;
235 else /* otherwise use min_uV as default suspend voltage */
236 suspend_state
->uV
= suspend_state
->min_uV
;
238 if (of_property_read_bool(suspend_np
,
239 "regulator-changeable-in-suspend"))
240 suspend_state
->changeable
= true;
242 if (i
== PM_SUSPEND_MEM
)
243 constraints
->initial_state
= PM_SUSPEND_MEM
;
245 of_node_put(suspend_np
);
246 suspend_state
= NULL
;
252 * of_get_regulator_init_data - extract regulator_init_data structure info
253 * @dev: device requesting for regulator_init_data
254 * @node: regulator device node
255 * @desc: regulator description
257 * Populates regulator_init_data structure by extracting data from device
258 * tree node, returns a pointer to the populated structure or NULL if memory
261 struct regulator_init_data
*of_get_regulator_init_data(struct device
*dev
,
262 struct device_node
*node
,
263 const struct regulator_desc
*desc
)
265 struct regulator_init_data
*init_data
;
270 init_data
= devm_kzalloc(dev
, sizeof(*init_data
), GFP_KERNEL
);
272 return NULL
; /* Out of memory? */
274 of_get_regulation_constraints(node
, &init_data
, desc
);
277 EXPORT_SYMBOL_GPL(of_get_regulator_init_data
);
279 struct devm_of_regulator_matches
{
280 struct of_regulator_match
*matches
;
281 unsigned int num_matches
;
284 static void devm_of_regulator_put_matches(struct device
*dev
, void *res
)
286 struct devm_of_regulator_matches
*devm_matches
= res
;
289 for (i
= 0; i
< devm_matches
->num_matches
; i
++)
290 of_node_put(devm_matches
->matches
[i
].of_node
);
294 * of_regulator_match - extract multiple regulator init data from device tree.
295 * @dev: device requesting the data
296 * @node: parent device node of the regulators
297 * @matches: match table for the regulators
298 * @num_matches: number of entries in match table
300 * This function uses a match table specified by the regulator driver to
301 * parse regulator init data from the device tree. @node is expected to
302 * contain a set of child nodes, each providing the init data for one
303 * regulator. The data parsed from a child node will be matched to a regulator
304 * based on either the deprecated property regulator-compatible if present,
305 * or otherwise the child node's name. Note that the match table is modified
306 * in place and an additional of_node reference is taken for each matched
309 * Returns the number of matches found or a negative error code on failure.
311 int of_regulator_match(struct device
*dev
, struct device_node
*node
,
312 struct of_regulator_match
*matches
,
313 unsigned int num_matches
)
315 unsigned int count
= 0;
318 struct device_node
*child
;
319 struct devm_of_regulator_matches
*devm_matches
;
324 devm_matches
= devres_alloc(devm_of_regulator_put_matches
,
325 sizeof(struct devm_of_regulator_matches
),
330 devm_matches
->matches
= matches
;
331 devm_matches
->num_matches
= num_matches
;
333 devres_add(dev
, devm_matches
);
335 for (i
= 0; i
< num_matches
; i
++) {
336 struct of_regulator_match
*match
= &matches
[i
];
337 match
->init_data
= NULL
;
338 match
->of_node
= NULL
;
341 for_each_child_of_node(node
, child
) {
342 name
= of_get_property(child
,
343 "regulator-compatible", NULL
);
346 for (i
= 0; i
< num_matches
; i
++) {
347 struct of_regulator_match
*match
= &matches
[i
];
351 if (strcmp(match
->name
, name
))
355 of_get_regulator_init_data(dev
, child
,
357 if (!match
->init_data
) {
359 "failed to parse DT for regulator %pOFn\n",
364 match
->of_node
= of_node_get(child
);
372 EXPORT_SYMBOL_GPL(of_regulator_match
);
374 struct device_node
*regulator_of_get_init_node(struct device
*dev
,
375 const struct regulator_desc
*desc
)
377 struct device_node
*search
, *child
;
380 if (!dev
->of_node
|| !desc
->of_match
)
383 if (desc
->regulators_node
) {
384 search
= of_get_child_by_name(dev
->of_node
,
385 desc
->regulators_node
);
387 search
= of_node_get(dev
->of_node
);
389 if (!strcmp(desc
->of_match
, search
->name
))
394 dev_dbg(dev
, "Failed to find regulator container node '%s'\n",
395 desc
->regulators_node
);
399 for_each_available_child_of_node(search
, child
) {
400 name
= of_get_property(child
, "regulator-compatible", NULL
);
404 if (!strcmp(desc
->of_match
, name
))
405 return of_node_get(child
);
413 struct regulator_init_data
*regulator_of_get_init_data(struct device
*dev
,
414 const struct regulator_desc
*desc
,
415 struct regulator_config
*config
,
416 struct device_node
**node
)
418 struct device_node
*child
;
419 struct regulator_init_data
*init_data
= NULL
;
421 child
= regulator_of_get_init_node(dev
, desc
);
425 init_data
= of_get_regulator_init_data(dev
, child
, desc
);
427 dev_err(dev
, "failed to parse DT for regulator %pOFn\n", child
);
431 if (desc
->of_parse_cb
&& desc
->of_parse_cb(child
, desc
, config
)) {
433 "driver callback failed to parse DT for regulator %pOFn\n",
448 static int of_node_match(struct device
*dev
, const void *data
)
450 return dev
->of_node
== data
;
453 struct regulator_dev
*of_find_regulator_by_node(struct device_node
*np
)
457 dev
= class_find_device(®ulator_class
, NULL
, np
, of_node_match
);
459 return dev
? dev_to_rdev(dev
) : NULL
;
463 * Returns number of regulators coupled with rdev.
465 int of_get_n_coupled(struct regulator_dev
*rdev
)
467 struct device_node
*node
= rdev
->dev
.of_node
;
470 n_phandles
= of_count_phandle_with_args(node
,
471 "regulator-coupled-with",
474 return (n_phandles
> 0) ? n_phandles
: 0;
477 /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
478 static bool of_coupling_find_node(struct device_node
*src
,
479 struct device_node
*to_find
)
484 n_phandles
= of_count_phandle_with_args(src
,
485 "regulator-coupled-with",
488 for (i
= 0; i
< n_phandles
; i
++) {
489 struct device_node
*tmp
= of_parse_phandle(src
,
490 "regulator-coupled-with", i
);
509 * of_check_coupling_data - Parse rdev's coupling properties and check data
511 * @rdev - pointer to regulator_dev whose data is checked
513 * Function checks if all the following conditions are met:
514 * - rdev's max_spread is greater than 0
515 * - all coupled regulators have the same max_spread
516 * - all coupled regulators have the same number of regulator_dev phandles
517 * - all regulators are linked to each other
519 * Returns true if all conditions are met.
521 bool of_check_coupling_data(struct regulator_dev
*rdev
)
523 int max_spread
= rdev
->constraints
->max_spread
;
524 struct device_node
*node
= rdev
->dev
.of_node
;
525 int n_phandles
= of_get_n_coupled(rdev
);
526 struct device_node
*c_node
;
530 if (max_spread
<= 0) {
531 dev_err(&rdev
->dev
, "max_spread value invalid\n");
535 /* iterate over rdev's phandles */
536 for (i
= 0; i
< n_phandles
; i
++) {
537 int c_max_spread
, c_n_phandles
;
539 c_node
= of_parse_phandle(node
,
540 "regulator-coupled-with", i
);
545 c_n_phandles
= of_count_phandle_with_args(c_node
,
546 "regulator-coupled-with",
549 if (c_n_phandles
!= n_phandles
) {
550 dev_err(&rdev
->dev
, "number of coupled reg phandles mismatch\n");
555 if (of_property_read_u32(c_node
, "regulator-coupled-max-spread",
561 if (c_max_spread
!= max_spread
) {
563 "coupled regulators max_spread mismatch\n");
568 if (!of_coupling_find_node(c_node
, node
)) {
569 dev_err(&rdev
->dev
, "missing 2-way linking for coupled regulators\n");
583 * of_parse_coupled regulator - Get regulator_dev pointer from rdev's property
584 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
585 * "regulator-coupled-with" property
586 * @index: Index in phandles array
588 * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
589 * registered, returns NULL
591 struct regulator_dev
*of_parse_coupled_regulator(struct regulator_dev
*rdev
,
594 struct device_node
*node
= rdev
->dev
.of_node
;
595 struct device_node
*c_node
;
596 struct regulator_dev
*c_rdev
;
598 c_node
= of_parse_phandle(node
, "regulator-coupled-with", index
);
602 c_rdev
= of_find_regulator_by_node(c_node
);