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