1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OF helpers for regulator framework
5 * Copyright (C) 2011 Texas Instruments, Inc.
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-less-critical-window-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
343 * Return: Pointer to a populated &struct regulator_init_data or NULL if
344 * memory allocation fails.
346 struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
347 struct device_node *node,
348 const struct regulator_desc *desc)
350 struct regulator_init_data *init_data;
355 init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
357 return NULL; /* Out of memory? */
359 if (of_get_regulation_constraints(dev, node, &init_data, desc))
364 EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
366 struct devm_of_regulator_matches {
367 struct of_regulator_match *matches;
368 unsigned int num_matches;
371 static void devm_of_regulator_put_matches(struct device *dev, void *res)
373 struct devm_of_regulator_matches *devm_matches = res;
376 for (i = 0; i < devm_matches->num_matches; i++)
377 of_node_put(devm_matches->matches[i].of_node);
381 * of_regulator_match - extract multiple regulator init data from device tree.
382 * @dev: device requesting the data
383 * @node: parent device node of the regulators
384 * @matches: match table for the regulators
385 * @num_matches: number of entries in match table
387 * This function uses a match table specified by the regulator driver to
388 * parse regulator init data from the device tree. @node is expected to
389 * contain a set of child nodes, each providing the init data for one
390 * regulator. The data parsed from a child node will be matched to a regulator
391 * based on either the deprecated property regulator-compatible if present,
392 * or otherwise the child node's name. Note that the match table is modified
393 * in place and an additional of_node reference is taken for each matched
396 * Return: The number of matches found or a negative error number on failure.
398 int of_regulator_match(struct device *dev, struct device_node *node,
399 struct of_regulator_match *matches,
400 unsigned int num_matches)
402 unsigned int count = 0;
405 struct device_node *child;
406 struct devm_of_regulator_matches *devm_matches;
411 devm_matches = devres_alloc(devm_of_regulator_put_matches,
412 sizeof(struct devm_of_regulator_matches),
417 devm_matches->matches = matches;
418 devm_matches->num_matches = num_matches;
420 devres_add(dev, devm_matches);
422 for (i = 0; i < num_matches; i++) {
423 struct of_regulator_match *match = &matches[i];
424 match->init_data = NULL;
425 match->of_node = NULL;
428 for_each_child_of_node(node, child) {
429 name = of_get_property(child,
430 "regulator-compatible", NULL);
433 for (i = 0; i < num_matches; i++) {
434 struct of_regulator_match *match = &matches[i];
438 if (strcmp(match->name, name))
442 of_get_regulator_init_data(dev, child,
444 if (!match->init_data) {
446 "failed to parse DT for regulator %pOFn\n",
451 match->of_node = of_node_get(child);
459 EXPORT_SYMBOL_GPL(of_regulator_match);
462 device_node *regulator_of_get_init_node(struct device *dev,
463 const struct regulator_desc *desc)
465 struct device_node *search, *child;
468 if (!dev->of_node || !desc->of_match)
471 if (desc->regulators_node) {
472 search = of_get_child_by_name(dev->of_node,
473 desc->regulators_node);
475 search = of_node_get(dev->of_node);
477 if (!strcmp(desc->of_match, search->name))
482 dev_dbg(dev, "Failed to find regulator container node '%s'\n",
483 desc->regulators_node);
487 for_each_available_child_of_node(search, child) {
488 name = of_get_property(child, "regulator-compatible", NULL);
490 if (!desc->of_match_full_name)
493 name = child->full_name;
496 if (!strcmp(desc->of_match, name)) {
499 * 'of_node_get(child)' is already performed by the
511 struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
512 const struct regulator_desc *desc,
513 struct regulator_config *config,
514 struct device_node **node)
516 struct device_node *child;
517 struct regulator_init_data *init_data = NULL;
519 child = regulator_of_get_init_node(config->dev, desc);
523 init_data = of_get_regulator_init_data(dev, child, desc);
525 dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
529 if (desc->of_parse_cb) {
532 ret = desc->of_parse_cb(child, desc, config);
534 if (ret == -EPROBE_DEFER) {
536 return ERR_PTR(-EPROBE_DEFER);
539 "driver callback failed to parse DT for regulator %pOFn\n",
556 * of_get_child_regulator - get a child regulator device node
557 * based on supply name
558 * @parent: Parent device node
559 * @prop_name: Combination regulator supply name and "-supply"
561 * Traverse all child nodes.
562 * Extract the child regulator device node corresponding to the supply name.
564 * Return: Pointer to the &struct device_node corresponding to the regulator
565 * if found, or %NULL if not found.
567 static struct device_node *of_get_child_regulator(struct device_node *parent,
568 const char *prop_name)
570 struct device_node *regnode = NULL;
571 struct device_node *child = NULL;
573 for_each_child_of_node(parent, child) {
574 regnode = of_parse_phandle(child, prop_name, 0);
578 regnode = of_get_child_regulator(child, prop_name);
590 * of_get_regulator - get a regulator device node based on supply name
591 * @dev: Device pointer for dev_printk() messages
592 * @node: Device node pointer for supply property lookup
593 * @supply: regulator supply name
595 * Extract the regulator device node corresponding to the supply name.
597 * Return: Pointer to the &struct device_node corresponding to the regulator
598 * if found, or %NULL if not found.
600 static struct device_node *of_get_regulator(struct device *dev, struct device_node *node,
603 struct device_node *regnode = NULL;
604 char prop_name[64]; /* 64 is max size of property name */
606 dev_dbg(dev, "Looking up %s-supply from device node %pOF\n", supply, node);
608 snprintf(prop_name, 64, "%s-supply", supply);
609 regnode = of_parse_phandle(node, prop_name, 0);
613 regnode = of_get_child_regulator(dev->of_node, prop_name);
617 dev_dbg(dev, "Looking up %s property in node %pOF failed\n", prop_name, dev->of_node);
621 static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
625 dev = class_find_device_by_of_node(®ulator_class, np);
627 return dev ? dev_to_rdev(dev) : NULL;
631 * of_regulator_dev_lookup - lookup a regulator device with device tree only
632 * @dev: Device pointer for regulator supply lookup.
633 * @np: Device node pointer for regulator supply lookup.
634 * @supply: Supply name or regulator ID.
636 * Return: Pointer to the &struct regulator_dev on success, or ERR_PTR()
637 * encoded value on error.
639 * If successful, returns a pointer to the &struct regulator_dev that
640 * corresponds to the name @supply and with the embedded &struct device
641 * refcount incremented by one. The refcount must be dropped by calling
644 * On failure one of the following ERR_PTR() encoded values is returned:
645 * * -%ENODEV if lookup fails permanently.
646 * * -%EPROBE_DEFER if lookup could succeed in the future.
648 struct regulator_dev *of_regulator_dev_lookup(struct device *dev, struct device_node *np,
651 struct regulator_dev *r;
652 struct device_node *node;
654 node = of_get_regulator(dev, np, supply);
656 r = of_find_regulator_by_node(node);
662 * We have a node, but there is no device.
663 * assume it has not registered yet.
665 return ERR_PTR(-EPROBE_DEFER);
668 return ERR_PTR(-ENODEV);
671 struct regulator *_of_regulator_get(struct device *dev, struct device_node *node,
672 const char *id, enum regulator_get_type get_type)
674 struct regulator_dev *r;
677 ret = _regulator_get_common_check(dev, id, get_type);
681 r = of_regulator_dev_lookup(dev, node, id);
682 return _regulator_get_common(r, dev, id, get_type);
686 * of_regulator_get_optional - get optional regulator via device tree lookup
687 * @dev: device used for dev_printk() messages
688 * @node: device node for regulator "consumer"
691 * Return: pointer to struct regulator corresponding to the regulator producer,
692 * or PTR_ERR() encoded error number.
694 * This is intended for use by consumers that want to get a regulator
695 * supply directly from a device node, and can and want to deal with
696 * absence of such supplies. This will _not_ consider supply aliases.
697 * See regulator_dev_lookup().
699 struct regulator *of_regulator_get_optional(struct device *dev,
700 struct device_node *node,
703 return _of_regulator_get(dev, node, id, OPTIONAL_GET);
705 EXPORT_SYMBOL_GPL(of_regulator_get_optional);
708 * Returns number of regulators coupled with rdev.
710 int of_get_n_coupled(struct regulator_dev *rdev)
712 struct device_node *node = rdev->dev.of_node;
715 n_phandles = of_count_phandle_with_args(node,
716 "regulator-coupled-with",
719 return (n_phandles > 0) ? n_phandles : 0;
722 /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
723 static bool of_coupling_find_node(struct device_node *src,
724 struct device_node *to_find,
730 n_phandles = of_count_phandle_with_args(src,
731 "regulator-coupled-with",
734 for (i = 0; i < n_phandles; i++) {
735 struct device_node *tmp = of_parse_phandle(src,
736 "regulator-coupled-with", i);
757 * of_check_coupling_data - Parse rdev's coupling properties and check data
759 * @rdev: pointer to regulator_dev whose data is checked
761 * Function checks if all the following conditions are met:
762 * - rdev's max_spread is greater than 0
763 * - all coupled regulators have the same max_spread
764 * - all coupled regulators have the same number of regulator_dev phandles
765 * - all regulators are linked to each other
767 * Return: True if all conditions are met; false otherwise.
769 bool of_check_coupling_data(struct regulator_dev *rdev)
771 struct device_node *node = rdev->dev.of_node;
772 int n_phandles = of_get_n_coupled(rdev);
773 struct device_node *c_node;
778 /* iterate over rdev's phandles */
779 for (i = 0; i < n_phandles; i++) {
780 int max_spread = rdev->constraints->max_spread[i];
781 int c_max_spread, c_n_phandles;
783 if (max_spread <= 0) {
784 dev_err(&rdev->dev, "max_spread value invalid\n");
788 c_node = of_parse_phandle(node,
789 "regulator-coupled-with", i);
794 c_n_phandles = of_count_phandle_with_args(c_node,
795 "regulator-coupled-with",
798 if (c_n_phandles != n_phandles) {
799 dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
804 if (!of_coupling_find_node(c_node, node, &index)) {
805 dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
810 if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread",
811 index, &c_max_spread)) {
816 if (c_max_spread != max_spread) {
818 "coupled regulators max_spread mismatch\n");
833 * of_parse_coupled_regulator() - Get regulator_dev pointer from rdev's property
834 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
835 * "regulator-coupled-with" property
836 * @index: Index in phandles array
838 * Return: Pointer to the &struct regulator_dev parsed from DTS, or %NULL if
839 * it has not yet been registered.
841 struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
844 struct device_node *node = rdev->dev.of_node;
845 struct device_node *c_node;
846 struct regulator_dev *c_rdev;
848 c_node = of_parse_phandle(node, "regulator-coupled-with", index);
852 c_rdev = of_find_regulator_by_node(c_node);
860 * Check if name is a supply name according to the '*-supply' pattern
862 * return length of supply name without the -supply
864 static int is_supply_name(const char *name)
869 /* string need to be at minimum len(x-supply) */
872 for (i = strs - 6; i > 0; i--) {
873 /* find first '-' and check if right part is supply */
876 if (strcmp(name + i + 1, "supply") != 0)
884 * of_regulator_bulk_get_all - get multiple regulator consumers
886 * @dev: Device to supply
887 * @np: device node to search for consumers
888 * @consumers: Configuration of consumers; clients are stored here.
890 * This helper function allows drivers to get several regulator
891 * consumers in one operation. If any of the regulators cannot be
892 * acquired then any regulators that were allocated will be freed
893 * before returning to the caller, and @consumers will not be
896 * Return: Number of regulators on success, or a negative error number
899 int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
900 struct regulator_bulk_data **consumers)
902 int num_consumers = 0;
903 struct regulator *tmp;
904 struct regulator_bulk_data *_consumers = NULL;
905 struct property *prop;
910 * first pass: get numbers of xxx-supply
911 * second pass: fill consumers
914 for_each_property_of_node(np, prop) {
915 i = is_supply_name(prop->name);
922 memcpy(name, prop->name, i);
924 tmp = regulator_get(dev, name);
929 _consumers[n].consumer = tmp;
935 *consumers = _consumers;
936 return num_consumers;
938 if (num_consumers == 0)
940 _consumers = kmalloc_array(num_consumers,
941 sizeof(struct regulator_bulk_data),
949 regulator_put(_consumers[n].consumer);
953 EXPORT_SYMBOL_GPL(of_regulator_bulk_get_all);