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