xref: /OK3568_Linux_fs/kernel/drivers/regulator/of_regulator.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * OF helpers for regulator framework
4  *
5  * Copyright (C) 2011 Texas Instruments, Inc.
6  * Rajendra Nayak <rnayak@ti.com>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/of.h>
12 #include <linux/regulator/machine.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/regulator/of_regulator.h>
15 
16 #include "internal.h"
17 
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",
22 };
23 
of_get_regulation_constraints(struct device * dev,struct device_node * np,struct regulator_init_data ** init_data,const struct regulator_desc * desc)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)
28 {
29 	struct regulation_constraints *constraints = &(*init_data)->constraints;
30 	struct regulator_state *suspend_state;
31 	struct device_node *suspend_np;
32 	unsigned int mode;
33 	int ret, i, len;
34 	int n_phandles;
35 	u32 pval;
36 
37 	n_phandles = of_count_phandle_with_args(np, "regulator-coupled-with",
38 						NULL);
39 	n_phandles = max(n_phandles, 0);
40 
41 	constraints->name = of_get_property(np, "regulator-name", NULL);
42 
43 	if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
44 		constraints->min_uV = pval;
45 
46 	if (!of_property_read_u32(np, "regulator-max-microvolt", &pval))
47 		constraints->max_uV = pval;
48 
49 	/* Voltage change possible? */
50 	if (constraints->min_uV != constraints->max_uV)
51 		constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
52 
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;
56 
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;
63 
64 	if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
65 				  &pval))
66 		constraints->ilim_uA = pval;
67 
68 	/* Current change possible? */
69 	if (constraints->min_uA != constraints->max_uA)
70 		constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
71 
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;
76 
77 	constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
78 
79 	if (of_property_read_bool(np, "regulator-allow-bypass"))
80 		constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
81 
82 	if (of_property_read_bool(np, "regulator-allow-set-load"))
83 		constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
84 
85 	ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
86 	if (!ret) {
87 		if (pval)
88 			constraints->ramp_delay = pval;
89 		else
90 			constraints->ramp_disable = true;
91 	}
92 
93 	ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
94 	if (!ret)
95 		constraints->settling_time = pval;
96 
97 	ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval);
98 	if (!ret)
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",
102 			np);
103 		constraints->settling_time_up = 0;
104 	}
105 
106 	ret = of_property_read_u32(np, "regulator-settling-time-down-us",
107 				   &pval);
108 	if (!ret)
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",
112 			np);
113 		constraints->settling_time_down = 0;
114 	}
115 
116 	ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
117 	if (!ret)
118 		constraints->enable_time = pval;
119 
120 	constraints->soft_start = of_property_read_bool(np,
121 					"regulator-soft-start");
122 	ret = of_property_read_u32(np, "regulator-active-discharge", &pval);
123 	if (!ret) {
124 		constraints->active_discharge =
125 				(pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE :
126 					REGULATOR_ACTIVE_DISCHARGE_DISABLE;
127 	}
128 
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);
134 			else
135 				constraints->initial_mode = mode;
136 		} else {
137 			pr_warn("%pOFn: mapping for mode %d not defined\n",
138 				np, pval);
139 		}
140 	}
141 
142 	len = of_property_count_elems_of_size(np, "regulator-allowed-modes",
143 						sizeof(u32));
144 	if (len > 0) {
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);
149 				if (ret) {
150 					pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
151 						np, i, ret);
152 					break;
153 				}
154 				mode = desc->of_map_mode(pval);
155 				if (mode == REGULATOR_MODE_INVALID)
156 					pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
157 						np, pval);
158 				else
159 					constraints->valid_modes_mask |= mode;
160 			}
161 			if (constraints->valid_modes_mask)
162 				constraints->valid_ops_mask
163 					|= REGULATOR_CHANGE_MODE;
164 		} else {
165 			pr_warn("%pOFn: mode mapping not defined\n", np);
166 		}
167 	}
168 
169 	if (!of_property_read_u32(np, "regulator-system-load", &pval))
170 		constraints->system_load = pval;
171 
172 	if (n_phandles) {
173 		constraints->max_spread = devm_kzalloc(dev,
174 				sizeof(*constraints->max_spread) * n_phandles,
175 				GFP_KERNEL);
176 
177 		if (!constraints->max_spread)
178 			return -ENOMEM;
179 
180 		of_property_read_u32_array(np, "regulator-coupled-max-spread",
181 					   constraints->max_spread, n_phandles);
182 	}
183 
184 	if (!of_property_read_u32(np, "regulator-max-step-microvolt",
185 				  &pval))
186 		constraints->max_uV_step = pval;
187 
188 	constraints->over_current_protection = of_property_read_bool(np,
189 					"regulator-over-current-protection");
190 
191 	for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
192 		switch (i) {
193 		case PM_SUSPEND_MEM:
194 			suspend_state = &constraints->state_mem;
195 			break;
196 		case PM_SUSPEND_MAX:
197 			suspend_state = &constraints->state_disk;
198 			break;
199 		case PM_SUSPEND_STANDBY:
200 			suspend_state = &constraints->state_standby;
201 			break;
202 		case PM_SUSPEND_ON:
203 		case PM_SUSPEND_TO_IDLE:
204 		default:
205 			continue;
206 		}
207 
208 		suspend_np = of_get_child_by_name(np, regulator_states[i]);
209 		if (!suspend_np)
210 			continue;
211 		if (!suspend_state) {
212 			of_node_put(suspend_np);
213 			continue;
214 		}
215 
216 		if (!of_property_read_u32(suspend_np, "regulator-mode",
217 					  &pval)) {
218 			if (desc && desc->of_map_mode) {
219 				mode = desc->of_map_mode(pval);
220 				if (mode == REGULATOR_MODE_INVALID)
221 					pr_err("%pOFn: invalid mode %u\n",
222 					       np, pval);
223 				else
224 					suspend_state->mode = mode;
225 			} else {
226 				pr_warn("%pOFn: mapping for mode %d not defined\n",
227 					np, pval);
228 			}
229 		}
230 
231 		if (of_property_read_bool(suspend_np,
232 					"regulator-on-in-suspend"))
233 			suspend_state->enabled = ENABLE_IN_SUSPEND;
234 		else if (of_property_read_bool(suspend_np,
235 					"regulator-off-in-suspend"))
236 			suspend_state->enabled = DISABLE_IN_SUSPEND;
237 
238 		if (!of_property_read_u32(suspend_np,
239 				"regulator-suspend-min-microvolt", &pval))
240 			suspend_state->min_uV = pval;
241 
242 		if (!of_property_read_u32(suspend_np,
243 				"regulator-suspend-max-microvolt", &pval))
244 			suspend_state->max_uV = pval;
245 
246 		if (!of_property_read_u32(suspend_np,
247 					"regulator-suspend-microvolt", &pval))
248 			suspend_state->uV = pval;
249 		else /* otherwise use min_uV as default suspend voltage */
250 			suspend_state->uV = suspend_state->min_uV;
251 
252 		if (of_property_read_bool(suspend_np,
253 					"regulator-changeable-in-suspend"))
254 			suspend_state->changeable = true;
255 
256 		if (i == PM_SUSPEND_MEM)
257 			constraints->initial_state = PM_SUSPEND_MEM;
258 
259 		of_node_put(suspend_np);
260 		suspend_state = NULL;
261 		suspend_np = NULL;
262 	}
263 
264 	return 0;
265 }
266 
267 /**
268  * of_get_regulator_init_data - extract regulator_init_data structure info
269  * @dev: device requesting for regulator_init_data
270  * @node: regulator device node
271  * @desc: regulator description
272  *
273  * Populates regulator_init_data structure by extracting data from device
274  * tree node, returns a pointer to the populated structure or NULL if memory
275  * alloc fails.
276  */
of_get_regulator_init_data(struct device * dev,struct device_node * node,const struct regulator_desc * desc)277 struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
278 					  struct device_node *node,
279 					  const struct regulator_desc *desc)
280 {
281 	struct regulator_init_data *init_data;
282 
283 	if (!node)
284 		return NULL;
285 
286 	init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
287 	if (!init_data)
288 		return NULL; /* Out of memory? */
289 
290 	if (of_get_regulation_constraints(dev, node, &init_data, desc))
291 		return NULL;
292 
293 	return init_data;
294 }
295 EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
296 
297 struct devm_of_regulator_matches {
298 	struct of_regulator_match *matches;
299 	unsigned int num_matches;
300 };
301 
devm_of_regulator_put_matches(struct device * dev,void * res)302 static void devm_of_regulator_put_matches(struct device *dev, void *res)
303 {
304 	struct devm_of_regulator_matches *devm_matches = res;
305 	int i;
306 
307 	for (i = 0; i < devm_matches->num_matches; i++)
308 		of_node_put(devm_matches->matches[i].of_node);
309 }
310 
311 /**
312  * of_regulator_match - extract multiple regulator init data from device tree.
313  * @dev: device requesting the data
314  * @node: parent device node of the regulators
315  * @matches: match table for the regulators
316  * @num_matches: number of entries in match table
317  *
318  * This function uses a match table specified by the regulator driver to
319  * parse regulator init data from the device tree. @node is expected to
320  * contain a set of child nodes, each providing the init data for one
321  * regulator. The data parsed from a child node will be matched to a regulator
322  * based on either the deprecated property regulator-compatible if present,
323  * or otherwise the child node's name. Note that the match table is modified
324  * in place and an additional of_node reference is taken for each matched
325  * regulator.
326  *
327  * Returns the number of matches found or a negative error code on failure.
328  */
of_regulator_match(struct device * dev,struct device_node * node,struct of_regulator_match * matches,unsigned int num_matches)329 int of_regulator_match(struct device *dev, struct device_node *node,
330 		       struct of_regulator_match *matches,
331 		       unsigned int num_matches)
332 {
333 	unsigned int count = 0;
334 	unsigned int i;
335 	const char *name;
336 	struct device_node *child;
337 	struct devm_of_regulator_matches *devm_matches;
338 
339 	if (!dev || !node)
340 		return -EINVAL;
341 
342 	devm_matches = devres_alloc(devm_of_regulator_put_matches,
343 				    sizeof(struct devm_of_regulator_matches),
344 				    GFP_KERNEL);
345 	if (!devm_matches)
346 		return -ENOMEM;
347 
348 	devm_matches->matches = matches;
349 	devm_matches->num_matches = num_matches;
350 
351 	devres_add(dev, devm_matches);
352 
353 	for (i = 0; i < num_matches; i++) {
354 		struct of_regulator_match *match = &matches[i];
355 		match->init_data = NULL;
356 		match->of_node = NULL;
357 	}
358 
359 	for_each_child_of_node(node, child) {
360 		name = of_get_property(child,
361 					"regulator-compatible", NULL);
362 		if (!name)
363 			name = child->name;
364 		for (i = 0; i < num_matches; i++) {
365 			struct of_regulator_match *match = &matches[i];
366 			if (match->of_node)
367 				continue;
368 
369 			if (strcmp(match->name, name))
370 				continue;
371 
372 			match->init_data =
373 				of_get_regulator_init_data(dev, child,
374 							   match->desc);
375 			if (!match->init_data) {
376 				dev_err(dev,
377 					"failed to parse DT for regulator %pOFn\n",
378 					child);
379 				of_node_put(child);
380 				return -EINVAL;
381 			}
382 			match->of_node = of_node_get(child);
383 			count++;
384 			break;
385 		}
386 	}
387 
388 	return count;
389 }
390 EXPORT_SYMBOL_GPL(of_regulator_match);
391 
392 static struct
regulator_of_get_init_node(struct device * dev,const struct regulator_desc * desc)393 device_node *regulator_of_get_init_node(struct device *dev,
394 					const struct regulator_desc *desc)
395 {
396 	struct device_node *search, *child;
397 	const char *name;
398 
399 	if (!dev->of_node || !desc->of_match)
400 		return NULL;
401 
402 	if (desc->regulators_node) {
403 		search = of_get_child_by_name(dev->of_node,
404 					      desc->regulators_node);
405 	} else {
406 		search = of_node_get(dev->of_node);
407 
408 		if (!strcmp(desc->of_match, search->name))
409 			return search;
410 	}
411 
412 	if (!search) {
413 		dev_dbg(dev, "Failed to find regulator container node '%s'\n",
414 			desc->regulators_node);
415 		return NULL;
416 	}
417 
418 	for_each_available_child_of_node(search, child) {
419 		name = of_get_property(child, "regulator-compatible", NULL);
420 		if (!name) {
421 			if (!desc->of_match_full_name)
422 				name = child->name;
423 			else
424 				name = child->full_name;
425 		}
426 
427 		if (!strcmp(desc->of_match, name)) {
428 			of_node_put(search);
429 			return of_node_get(child);
430 		}
431 	}
432 
433 	of_node_put(search);
434 
435 	return NULL;
436 }
437 
regulator_of_get_init_data(struct device * dev,const struct regulator_desc * desc,struct regulator_config * config,struct device_node ** node)438 struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
439 					    const struct regulator_desc *desc,
440 					    struct regulator_config *config,
441 					    struct device_node **node)
442 {
443 	struct device_node *child;
444 	struct regulator_init_data *init_data = NULL;
445 
446 	child = regulator_of_get_init_node(dev, desc);
447 	if (!child)
448 		return NULL;
449 
450 	init_data = of_get_regulator_init_data(dev, child, desc);
451 	if (!init_data) {
452 		dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
453 		goto error;
454 	}
455 
456 	if (desc->of_parse_cb) {
457 		int ret;
458 
459 		ret = desc->of_parse_cb(child, desc, config);
460 		if (ret) {
461 			if (ret == -EPROBE_DEFER) {
462 				of_node_put(child);
463 				return ERR_PTR(-EPROBE_DEFER);
464 			}
465 			dev_err(dev,
466 				"driver callback failed to parse DT for regulator %pOFn\n",
467 				child);
468 			goto error;
469 		}
470 	}
471 
472 	*node = child;
473 
474 	return init_data;
475 
476 error:
477 	of_node_put(child);
478 
479 	return NULL;
480 }
481 
of_find_regulator_by_node(struct device_node * np)482 struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
483 {
484 	struct device *dev;
485 
486 	dev = class_find_device_by_of_node(&regulator_class, np);
487 
488 	return dev ? dev_to_rdev(dev) : NULL;
489 }
490 
491 /*
492  * Returns number of regulators coupled with rdev.
493  */
of_get_n_coupled(struct regulator_dev * rdev)494 int of_get_n_coupled(struct regulator_dev *rdev)
495 {
496 	struct device_node *node = rdev->dev.of_node;
497 	int n_phandles;
498 
499 	n_phandles = of_count_phandle_with_args(node,
500 						"regulator-coupled-with",
501 						NULL);
502 
503 	return (n_phandles > 0) ? n_phandles : 0;
504 }
505 
506 /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
of_coupling_find_node(struct device_node * src,struct device_node * to_find,int * index)507 static bool of_coupling_find_node(struct device_node *src,
508 				  struct device_node *to_find,
509 				  int *index)
510 {
511 	int n_phandles, i;
512 	bool found = false;
513 
514 	n_phandles = of_count_phandle_with_args(src,
515 						"regulator-coupled-with",
516 						NULL);
517 
518 	for (i = 0; i < n_phandles; i++) {
519 		struct device_node *tmp = of_parse_phandle(src,
520 					   "regulator-coupled-with", i);
521 
522 		if (!tmp)
523 			break;
524 
525 		/* found */
526 		if (tmp == to_find)
527 			found = true;
528 
529 		of_node_put(tmp);
530 
531 		if (found) {
532 			*index = i;
533 			break;
534 		}
535 	}
536 
537 	return found;
538 }
539 
540 /**
541  * of_check_coupling_data - Parse rdev's coupling properties and check data
542  *			    consistency
543  * @rdev: pointer to regulator_dev whose data is checked
544  *
545  * Function checks if all the following conditions are met:
546  * - rdev's max_spread is greater than 0
547  * - all coupled regulators have the same max_spread
548  * - all coupled regulators have the same number of regulator_dev phandles
549  * - all regulators are linked to each other
550  *
551  * Returns true if all conditions are met.
552  */
of_check_coupling_data(struct regulator_dev * rdev)553 bool of_check_coupling_data(struct regulator_dev *rdev)
554 {
555 	struct device_node *node = rdev->dev.of_node;
556 	int n_phandles = of_get_n_coupled(rdev);
557 	struct device_node *c_node;
558 	int index;
559 	int i;
560 	bool ret = true;
561 
562 	/* iterate over rdev's phandles */
563 	for (i = 0; i < n_phandles; i++) {
564 		int max_spread = rdev->constraints->max_spread[i];
565 		int c_max_spread, c_n_phandles;
566 
567 		if (max_spread <= 0) {
568 			dev_err(&rdev->dev, "max_spread value invalid\n");
569 			return false;
570 		}
571 
572 		c_node = of_parse_phandle(node,
573 					  "regulator-coupled-with", i);
574 
575 		if (!c_node)
576 			ret = false;
577 
578 		c_n_phandles = of_count_phandle_with_args(c_node,
579 							  "regulator-coupled-with",
580 							  NULL);
581 
582 		if (c_n_phandles != n_phandles) {
583 			dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
584 			ret = false;
585 			goto clean;
586 		}
587 
588 		if (!of_coupling_find_node(c_node, node, &index)) {
589 			dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
590 			ret = false;
591 			goto clean;
592 		}
593 
594 		if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread",
595 					       index, &c_max_spread)) {
596 			ret = false;
597 			goto clean;
598 		}
599 
600 		if (c_max_spread != max_spread) {
601 			dev_err(&rdev->dev,
602 				"coupled regulators max_spread mismatch\n");
603 			ret = false;
604 			goto clean;
605 		}
606 
607 clean:
608 		of_node_put(c_node);
609 		if (!ret)
610 			break;
611 	}
612 
613 	return ret;
614 }
615 
616 /**
617  * of_parse_coupled regulator - Get regulator_dev pointer from rdev's property
618  * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
619  *	  "regulator-coupled-with" property
620  * @index: Index in phandles array
621  *
622  * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
623  * registered, returns NULL
624  */
of_parse_coupled_regulator(struct regulator_dev * rdev,int index)625 struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
626 						 int index)
627 {
628 	struct device_node *node = rdev->dev.of_node;
629 	struct device_node *c_node;
630 	struct regulator_dev *c_rdev;
631 
632 	c_node = of_parse_phandle(node, "regulator-coupled-with", index);
633 	if (!c_node)
634 		return NULL;
635 
636 	c_rdev = of_find_regulator_by_node(c_node);
637 
638 	of_node_put(c_node);
639 
640 	return c_rdev;
641 }
642