xref: /linux/drivers/opp/of.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Generic OPP OF helpers
4  *
5  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
6  *	Nishanth Menon
7  *	Romit Dasgupta
8  *	Kevin Hilman
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/cpu.h>
14 #include <linux/errno.h>
15 #include <linux/device.h>
16 #include <linux/of.h>
17 #include <linux/pm_domain.h>
18 #include <linux/slab.h>
19 #include <linux/export.h>
20 #include <linux/energy_model.h>
21 
22 #include "opp.h"
23 
24 /* OPP tables with uninitialized required OPPs, protected by opp_table_lock */
25 static LIST_HEAD(lazy_opp_tables);
26 
27 /*
28  * Returns opp descriptor node for a device node, caller must
29  * do of_node_put().
30  */
31 static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
32 						     int index)
33 {
34 	/* "operating-points-v2" can be an array for power domain providers */
35 	return of_parse_phandle(np, "operating-points-v2", index);
36 }
37 
38 /* Returns opp descriptor node for a device, caller must do of_node_put() */
39 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
40 {
41 	return _opp_of_get_opp_desc_node(dev->of_node, 0);
42 }
43 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
44 
45 struct opp_table *_managed_opp(struct device *dev, int index)
46 {
47 	struct opp_table *opp_table, *managed_table = NULL;
48 
49 	struct device_node *np __free(device_node) =
50 		_opp_of_get_opp_desc_node(dev->of_node, index);
51 
52 	if (!np)
53 		return NULL;
54 
55 	list_for_each_entry(opp_table, &opp_tables, node) {
56 		if (opp_table->np == np) {
57 			/*
58 			 * Multiple devices can point to the same OPP table and
59 			 * so will have same node-pointer, np.
60 			 *
61 			 * But the OPPs will be considered as shared only if the
62 			 * OPP table contains a "opp-shared" property.
63 			 */
64 			if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED)
65 				managed_table = dev_pm_opp_get_opp_table_ref(opp_table);
66 
67 			break;
68 		}
69 	}
70 
71 	return managed_table;
72 }
73 
74 /* The caller must call dev_pm_opp_put() after the OPP is used */
75 static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table,
76 					  struct device_node *opp_np)
77 {
78 	struct dev_pm_opp *opp;
79 
80 	guard(mutex)(&opp_table->lock);
81 
82 	list_for_each_entry(opp, &opp_table->opp_list, node) {
83 		if (opp->np == opp_np)
84 			return dev_pm_opp_get(opp);
85 	}
86 
87 	return NULL;
88 }
89 
90 static struct device_node *of_parse_required_opp(struct device_node *np,
91 						 int index)
92 {
93 	return of_parse_phandle(np, "required-opps", index);
94 }
95 
96 /* The caller must call dev_pm_opp_put_opp_table() after the table is used */
97 static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np)
98 {
99 	struct opp_table *opp_table;
100 
101 	struct device_node *opp_table_np __free(device_node) =
102 		of_get_parent(opp_np);
103 
104 	if (!opp_table_np)
105 		return ERR_PTR(-ENODEV);
106 
107 	guard(mutex)(&opp_table_lock);
108 
109 	list_for_each_entry(opp_table, &opp_tables, node) {
110 		if (opp_table_np == opp_table->np)
111 			return dev_pm_opp_get_opp_table_ref(opp_table);
112 	}
113 
114 	return ERR_PTR(-ENODEV);
115 }
116 
117 /* Free resources previously acquired by _opp_table_alloc_required_tables() */
118 static void _opp_table_free_required_tables(struct opp_table *opp_table)
119 {
120 	struct opp_table **required_opp_tables = opp_table->required_opp_tables;
121 	int i;
122 
123 	if (!required_opp_tables)
124 		return;
125 
126 	for (i = 0; i < opp_table->required_opp_count; i++) {
127 		if (IS_ERR_OR_NULL(required_opp_tables[i]))
128 			continue;
129 
130 		dev_pm_opp_put_opp_table(required_opp_tables[i]);
131 	}
132 
133 	kfree(required_opp_tables);
134 
135 	opp_table->required_opp_count = 0;
136 	opp_table->required_opp_tables = NULL;
137 
138 	guard(mutex)(&opp_table_lock);
139 	list_del(&opp_table->lazy);
140 }
141 
142 /*
143  * Populate all devices and opp tables which are part of "required-opps" list.
144  * Checking only the first OPP node should be enough.
145  */
146 static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
147 					     struct device *dev,
148 					     struct device_node *opp_np)
149 {
150 	struct opp_table **required_opp_tables;
151 	bool lazy = false;
152 	int count, i, size;
153 
154 	/* Traversing the first OPP node is all we need */
155 	struct device_node *np __free(device_node) =
156 		of_get_next_available_child(opp_np, NULL);
157 
158 	if (!np) {
159 		dev_warn(dev, "Empty OPP table\n");
160 		return;
161 	}
162 
163 	count = of_count_phandle_with_args(np, "required-opps", NULL);
164 	if (count <= 0)
165 		return;
166 
167 	size = sizeof(*required_opp_tables) + sizeof(*opp_table->required_devs);
168 	required_opp_tables = kcalloc(count, size, GFP_KERNEL);
169 	if (!required_opp_tables)
170 		return;
171 
172 	opp_table->required_opp_tables = required_opp_tables;
173 	opp_table->required_devs = (void *)(required_opp_tables + count);
174 	opp_table->required_opp_count = count;
175 
176 	for (i = 0; i < count; i++) {
177 		struct device_node *required_np __free(device_node) =
178 			of_parse_required_opp(np, i);
179 
180 		if (!required_np) {
181 			_opp_table_free_required_tables(opp_table);
182 			return;
183 		}
184 
185 		required_opp_tables[i] = _find_table_of_opp_np(required_np);
186 
187 		if (IS_ERR(required_opp_tables[i]))
188 			lazy = true;
189 	}
190 
191 	/* Let's do the linking later on */
192 	if (lazy) {
193 		/*
194 		 * The OPP table is not held while allocating the table, take it
195 		 * now to avoid corruption to the lazy_opp_tables list.
196 		 */
197 		guard(mutex)(&opp_table_lock);
198 		list_add(&opp_table->lazy, &lazy_opp_tables);
199 	}
200 }
201 
202 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
203 			int index)
204 {
205 	struct device_node *opp_np;
206 	u32 val;
207 
208 	/*
209 	 * Only required for backward compatibility with v1 bindings, but isn't
210 	 * harmful for other cases. And so we do it unconditionally.
211 	 */
212 	struct device_node *np __free(device_node) = of_node_get(dev->of_node);
213 
214 	if (!np)
215 		return;
216 
217 	if (!of_property_read_u32(np, "clock-latency", &val))
218 		opp_table->clock_latency_ns_max = val;
219 	of_property_read_u32(np, "voltage-tolerance",
220 			     &opp_table->voltage_tolerance_v1);
221 
222 	if (of_property_present(np, "#power-domain-cells"))
223 		opp_table->is_genpd = true;
224 
225 	/* Get OPP table node */
226 	opp_np = _opp_of_get_opp_desc_node(np, index);
227 	if (!opp_np)
228 		return;
229 
230 	if (of_property_read_bool(opp_np, "opp-shared"))
231 		opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
232 	else
233 		opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
234 
235 	opp_table->np = opp_np;
236 
237 	_opp_table_alloc_required_tables(opp_table, dev, opp_np);
238 }
239 
240 void _of_clear_opp_table(struct opp_table *opp_table)
241 {
242 	_opp_table_free_required_tables(opp_table);
243 	of_node_put(opp_table->np);
244 }
245 
246 /*
247  * Release all resources previously acquired with a call to
248  * _of_opp_alloc_required_opps().
249  */
250 static void _of_opp_free_required_opps(struct opp_table *opp_table,
251 				       struct dev_pm_opp *opp)
252 {
253 	struct dev_pm_opp **required_opps = opp->required_opps;
254 	int i;
255 
256 	if (!required_opps)
257 		return;
258 
259 	for (i = 0; i < opp_table->required_opp_count; i++) {
260 		if (!required_opps[i])
261 			continue;
262 
263 		/* Put the reference back */
264 		dev_pm_opp_put(required_opps[i]);
265 	}
266 
267 	opp->required_opps = NULL;
268 	kfree(required_opps);
269 }
270 
271 void _of_clear_opp(struct opp_table *opp_table, struct dev_pm_opp *opp)
272 {
273 	_of_opp_free_required_opps(opp_table, opp);
274 	of_node_put(opp->np);
275 }
276 
277 static int _link_required_opps(struct dev_pm_opp *opp,
278 			       struct opp_table *required_table, int index)
279 {
280 	struct device_node *np __free(device_node) =
281 		of_parse_required_opp(opp->np, index);
282 
283 	if (unlikely(!np))
284 		return -ENODEV;
285 
286 	opp->required_opps[index] = _find_opp_of_np(required_table, np);
287 	if (!opp->required_opps[index]) {
288 		pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
289 		       __func__, opp->np, index);
290 		return -ENODEV;
291 	}
292 
293 	return 0;
294 }
295 
296 /* Populate all required OPPs which are part of "required-opps" list */
297 static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
298 				       struct dev_pm_opp *opp)
299 {
300 	struct opp_table *required_table;
301 	int i, ret, count = opp_table->required_opp_count;
302 
303 	if (!count)
304 		return 0;
305 
306 	opp->required_opps = kzalloc_objs(*opp->required_opps, count,
307 					  GFP_KERNEL);
308 	if (!opp->required_opps)
309 		return -ENOMEM;
310 
311 	for (i = 0; i < count; i++) {
312 		required_table = opp_table->required_opp_tables[i];
313 
314 		/* Required table not added yet, we will link later */
315 		if (IS_ERR_OR_NULL(required_table))
316 			continue;
317 
318 		ret = _link_required_opps(opp, required_table, i);
319 		if (ret)
320 			goto free_required_opps;
321 	}
322 
323 	return 0;
324 
325 free_required_opps:
326 	_of_opp_free_required_opps(opp_table, opp);
327 
328 	return ret;
329 }
330 
331 /* Link required OPPs for an individual OPP */
332 static int lazy_link_required_opps(struct opp_table *opp_table,
333 				   struct opp_table *new_table, int index)
334 {
335 	struct dev_pm_opp *opp;
336 	int ret;
337 
338 	list_for_each_entry(opp, &opp_table->opp_list, node) {
339 		ret = _link_required_opps(opp, new_table, index);
340 		if (ret)
341 			return ret;
342 	}
343 
344 	return 0;
345 }
346 
347 /* Link required OPPs for all OPPs of the newly added OPP table */
348 static void lazy_link_required_opp_table(struct opp_table *new_table)
349 {
350 	struct opp_table *opp_table, *temp, **required_opp_tables;
351 	struct dev_pm_opp *opp;
352 	int i, ret;
353 
354 	guard(mutex)(&opp_table_lock);
355 
356 	list_for_each_entry_safe(opp_table, temp, &lazy_opp_tables, lazy) {
357 		bool lazy = false;
358 
359 		/* opp_np can't be invalid here */
360 		struct device_node *opp_np __free(device_node) =
361 			of_get_next_available_child(opp_table->np, NULL);
362 
363 		for (i = 0; i < opp_table->required_opp_count; i++) {
364 			required_opp_tables = opp_table->required_opp_tables;
365 
366 			/* Required opp-table is already parsed */
367 			if (!IS_ERR(required_opp_tables[i]))
368 				continue;
369 
370 			/* required_np can't be invalid here */
371 			struct device_node *required_np __free(device_node) =
372 				of_parse_required_opp(opp_np, i);
373 			struct device_node *required_table_np __free(device_node) =
374 				of_get_parent(required_np);
375 
376 			/*
377 			 * Newly added table isn't the required opp-table for
378 			 * opp_table.
379 			 */
380 			if (required_table_np != new_table->np) {
381 				lazy = true;
382 				continue;
383 			}
384 
385 			required_opp_tables[i] = dev_pm_opp_get_opp_table_ref(new_table);
386 
387 			/* Link OPPs now */
388 			ret = lazy_link_required_opps(opp_table, new_table, i);
389 			if (ret) {
390 				/* The OPPs will be marked unusable */
391 				lazy = false;
392 				break;
393 			}
394 		}
395 
396 		/* All required opp-tables found, remove from lazy list */
397 		if (!lazy) {
398 			list_del_init(&opp_table->lazy);
399 
400 			list_for_each_entry(opp, &opp_table->opp_list, node)
401 				_required_opps_available(opp, opp_table->required_opp_count);
402 		}
403 	}
404 }
405 
406 static int _bandwidth_supported(struct device *dev, struct opp_table *opp_table)
407 {
408 	struct device_node *opp_np __free(device_node) = NULL;
409 	struct property *prop;
410 
411 	if (!opp_table) {
412 		struct device_node *np __free(device_node) =
413 			of_node_get(dev->of_node);
414 
415 		if (!np)
416 			return -ENODEV;
417 
418 		opp_np = _opp_of_get_opp_desc_node(np, 0);
419 	} else {
420 		opp_np = of_node_get(opp_table->np);
421 	}
422 
423 	/* Lets not fail in case we are parsing opp-v1 bindings */
424 	if (!opp_np)
425 		return 0;
426 
427 	/* Checking only first OPP is sufficient */
428 	struct device_node *np __free(device_node) =
429 		of_get_next_available_child(opp_np, NULL);
430 
431 	if (!np) {
432 		dev_err(dev, "OPP table empty\n");
433 		return -EINVAL;
434 	}
435 
436 	prop = of_find_property(np, "opp-peak-kBps", NULL);
437 	if (!prop || !prop->length)
438 		return 0;
439 
440 	return 1;
441 }
442 
443 int dev_pm_opp_of_find_icc_paths(struct device *dev,
444 				 struct opp_table *opp_table)
445 {
446 	struct device_node *np __free(device_node) = of_node_get(dev->of_node);
447 	int ret, i, count, num_paths;
448 	struct icc_path **paths;
449 
450 	ret = _bandwidth_supported(dev, opp_table);
451 	if (ret == -EINVAL)
452 		return 0; /* Empty OPP table is a valid corner-case, let's not fail */
453 	else if (ret <= 0)
454 		return ret;
455 
456 	if (!np)
457 		return 0;
458 
459 	ret = 0;
460 
461 	count = of_count_phandle_with_args(np, "interconnects",
462 					   "#interconnect-cells");
463 	if (count < 0)
464 		return 0;
465 
466 	/* two phandles when #interconnect-cells = <1> */
467 	if (count % 2) {
468 		dev_err(dev, "%s: Invalid interconnects values\n", __func__);
469 		return -EINVAL;
470 	}
471 
472 	num_paths = count / 2;
473 	paths = kzalloc_objs(*paths, num_paths, GFP_KERNEL);
474 	if (!paths)
475 		return -ENOMEM;
476 
477 	for (i = 0; i < num_paths; i++) {
478 		paths[i] = of_icc_get_by_index(dev, i);
479 		if (IS_ERR(paths[i])) {
480 			ret = dev_err_probe(dev, PTR_ERR(paths[i]), "%s: Unable to get path%d\n", __func__, i);
481 			goto err;
482 		}
483 	}
484 
485 	if (opp_table) {
486 		opp_table->paths = paths;
487 		opp_table->path_count = num_paths;
488 		return 0;
489 	}
490 
491 err:
492 	while (i--)
493 		icc_put(paths[i]);
494 
495 	kfree(paths);
496 
497 	return ret;
498 }
499 EXPORT_SYMBOL_GPL(dev_pm_opp_of_find_icc_paths);
500 
501 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
502 			      struct device_node *np)
503 {
504 	unsigned int levels = opp_table->supported_hw_count;
505 	int count, versions, ret, i, j;
506 	u32 val;
507 
508 	if (!opp_table->supported_hw) {
509 		/*
510 		 * In the case that no supported_hw has been set by the
511 		 * platform but there is an opp-supported-hw value set for
512 		 * an OPP then the OPP should not be enabled as there is
513 		 * no way to see if the hardware supports it.
514 		 */
515 		if (of_property_present(np, "opp-supported-hw"))
516 			return false;
517 		else
518 			return true;
519 	}
520 
521 	count = of_property_count_u32_elems(np, "opp-supported-hw");
522 	if (count <= 0 || count % levels) {
523 		dev_err(dev, "%s: Invalid opp-supported-hw property (%d)\n",
524 			__func__, count);
525 		return false;
526 	}
527 
528 	versions = count / levels;
529 
530 	/* All levels in at least one of the versions should match */
531 	for (i = 0; i < versions; i++) {
532 		bool supported = true;
533 
534 		for (j = 0; j < levels; j++) {
535 			ret = of_property_read_u32_index(np, "opp-supported-hw",
536 							 i * levels + j, &val);
537 			if (ret) {
538 				dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
539 					 __func__, i * levels + j, ret);
540 				return false;
541 			}
542 
543 			/* Check if the level is supported */
544 			if (!(val & opp_table->supported_hw[j])) {
545 				supported = false;
546 				break;
547 			}
548 		}
549 
550 		if (supported)
551 			return true;
552 	}
553 
554 	return false;
555 }
556 
557 static u32 *_parse_named_prop(struct dev_pm_opp *opp, struct device *dev,
558 			      struct opp_table *opp_table,
559 			      const char *prop_type, bool *triplet)
560 {
561 	struct property *prop = NULL;
562 	char name[NAME_MAX];
563 	int count, ret;
564 	u32 *out;
565 
566 	/* Search for "opp-<prop_type>-<name>" */
567 	if (opp_table->prop_name) {
568 		snprintf(name, sizeof(name), "opp-%s-%s", prop_type,
569 			 opp_table->prop_name);
570 		prop = of_find_property(opp->np, name, NULL);
571 	}
572 
573 	if (!prop) {
574 		/* Search for "opp-<prop_type>" */
575 		snprintf(name, sizeof(name), "opp-%s", prop_type);
576 		prop = of_find_property(opp->np, name, NULL);
577 		if (!prop)
578 			return NULL;
579 	}
580 
581 	count = of_property_count_u32_elems(opp->np, name);
582 	if (count < 0) {
583 		dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, name,
584 			count);
585 		return ERR_PTR(count);
586 	}
587 
588 	/*
589 	 * Initialize regulator_count, if regulator information isn't provided
590 	 * by the platform. Now that one of the properties is available, fix the
591 	 * regulator_count to 1.
592 	 */
593 	if (unlikely(opp_table->regulator_count == -1))
594 		opp_table->regulator_count = 1;
595 
596 	if (count != opp_table->regulator_count &&
597 	    (!triplet || count != opp_table->regulator_count * 3)) {
598 		dev_err(dev, "%s: Invalid number of elements in %s property (%u) with supplies (%d)\n",
599 			__func__, prop_type, count, opp_table->regulator_count);
600 		return ERR_PTR(-EINVAL);
601 	}
602 
603 	out = kmalloc_array(count, sizeof(*out), GFP_KERNEL);
604 	if (!out)
605 		return ERR_PTR(-EINVAL);
606 
607 	ret = of_property_read_u32_array(opp->np, name, out, count);
608 	if (ret) {
609 		dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
610 		kfree(out);
611 		return ERR_PTR(-EINVAL);
612 	}
613 
614 	if (triplet)
615 		*triplet = count != opp_table->regulator_count;
616 
617 	return out;
618 }
619 
620 static u32 *opp_parse_microvolt(struct dev_pm_opp *opp, struct device *dev,
621 				struct opp_table *opp_table, bool *triplet)
622 {
623 	u32 *microvolt;
624 
625 	microvolt = _parse_named_prop(opp, dev, opp_table, "microvolt", triplet);
626 	if (IS_ERR(microvolt))
627 		return microvolt;
628 
629 	if (!microvolt) {
630 		/*
631 		 * Missing property isn't a problem, but an invalid
632 		 * entry is. This property isn't optional if regulator
633 		 * information is provided. Check only for the first OPP, as
634 		 * regulator_count may get initialized after that to a valid
635 		 * value.
636 		 */
637 		if (list_empty(&opp_table->opp_list) &&
638 		    opp_table->regulator_count > 0) {
639 			dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
640 				__func__);
641 			return ERR_PTR(-EINVAL);
642 		}
643 	}
644 
645 	return microvolt;
646 }
647 
648 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
649 			      struct opp_table *opp_table)
650 {
651 	u32 *microvolt, *microamp, *microwatt;
652 	int ret = 0, i, j;
653 	bool triplet;
654 
655 	microvolt = opp_parse_microvolt(opp, dev, opp_table, &triplet);
656 	if (IS_ERR(microvolt))
657 		return PTR_ERR(microvolt);
658 
659 	microamp = _parse_named_prop(opp, dev, opp_table, "microamp", NULL);
660 	if (IS_ERR(microamp)) {
661 		ret = PTR_ERR(microamp);
662 		goto free_microvolt;
663 	}
664 
665 	microwatt = _parse_named_prop(opp, dev, opp_table, "microwatt", NULL);
666 	if (IS_ERR(microwatt)) {
667 		ret = PTR_ERR(microwatt);
668 		goto free_microamp;
669 	}
670 
671 	/*
672 	 * Initialize regulator_count if it is uninitialized and no properties
673 	 * are found.
674 	 */
675 	if (unlikely(opp_table->regulator_count == -1)) {
676 		opp_table->regulator_count = 0;
677 		return 0;
678 	}
679 
680 	for (i = 0, j = 0; i < opp_table->regulator_count; i++) {
681 		if (microvolt) {
682 			opp->supplies[i].u_volt = microvolt[j++];
683 
684 			if (triplet) {
685 				opp->supplies[i].u_volt_min = microvolt[j++];
686 				opp->supplies[i].u_volt_max = microvolt[j++];
687 			} else {
688 				opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
689 				opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
690 			}
691 		}
692 
693 		if (microamp)
694 			opp->supplies[i].u_amp = microamp[i];
695 
696 		if (microwatt)
697 			opp->supplies[i].u_watt = microwatt[i];
698 	}
699 
700 	kfree(microwatt);
701 free_microamp:
702 	kfree(microamp);
703 free_microvolt:
704 	kfree(microvolt);
705 
706 	return ret;
707 }
708 
709 /**
710  * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
711  *				  entries
712  * @dev:	device pointer used to lookup OPP table.
713  *
714  * Free OPPs created using static entries present in DT.
715  */
716 void dev_pm_opp_of_remove_table(struct device *dev)
717 {
718 	dev_pm_opp_remove_table(dev);
719 }
720 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
721 
722 static int _read_rate(struct dev_pm_opp *new_opp, struct opp_table *opp_table,
723 		      struct device_node *np)
724 {
725 	struct property *prop;
726 	int i, count, ret;
727 	u64 *rates;
728 
729 	prop = of_find_property(np, "opp-hz", NULL);
730 	if (!prop)
731 		return -ENODEV;
732 
733 	count = prop->length / sizeof(u64);
734 	if (opp_table->clk_count != count) {
735 		pr_err("%s: Count mismatch between opp-hz and clk_count (%d %d)\n",
736 		       __func__, count, opp_table->clk_count);
737 		return -EINVAL;
738 	}
739 
740 	rates = kmalloc_array(count, sizeof(*rates), GFP_KERNEL);
741 	if (!rates)
742 		return -ENOMEM;
743 
744 	ret = of_property_read_u64_array(np, "opp-hz", rates, count);
745 	if (ret) {
746 		pr_err("%s: Error parsing opp-hz: %d\n", __func__, ret);
747 	} else {
748 		/*
749 		 * Rate is defined as an unsigned long in clk API, and so
750 		 * casting explicitly to its type. Must be fixed once rate is 64
751 		 * bit guaranteed in clk API.
752 		 */
753 		for (i = 0; i < count; i++) {
754 			new_opp->rates[i] = (unsigned long)rates[i];
755 
756 			/* This will happen for frequencies > 4.29 GHz */
757 			WARN_ON(new_opp->rates[i] != rates[i]);
758 		}
759 	}
760 
761 	kfree(rates);
762 
763 	return ret;
764 }
765 
766 static int _read_bw(struct dev_pm_opp *new_opp, struct opp_table *opp_table,
767 		    struct device_node *np, bool peak)
768 {
769 	const char *name = peak ? "opp-peak-kBps" : "opp-avg-kBps";
770 	struct property *prop;
771 	int i, count, ret;
772 	u32 *bw;
773 
774 	prop = of_find_property(np, name, NULL);
775 	if (!prop)
776 		return -ENODEV;
777 
778 	count = prop->length / sizeof(u32);
779 	if (opp_table->path_count != count) {
780 		pr_err("%s: Mismatch between %s and paths (%d %d)\n",
781 				__func__, name, count, opp_table->path_count);
782 		return -EINVAL;
783 	}
784 
785 	bw = kmalloc_array(count, sizeof(*bw), GFP_KERNEL);
786 	if (!bw)
787 		return -ENOMEM;
788 
789 	ret = of_property_read_u32_array(np, name, bw, count);
790 	if (ret) {
791 		pr_err("%s: Error parsing %s: %d\n", __func__, name, ret);
792 		goto out;
793 	}
794 
795 	for (i = 0; i < count; i++) {
796 		if (peak)
797 			new_opp->bandwidth[i].peak = kBps_to_icc(bw[i]);
798 		else
799 			new_opp->bandwidth[i].avg = kBps_to_icc(bw[i]);
800 	}
801 
802 out:
803 	kfree(bw);
804 	return ret;
805 }
806 
807 static int _read_opp_key(struct dev_pm_opp *new_opp,
808 			 struct opp_table *opp_table, struct device_node *np)
809 {
810 	bool found = false;
811 	int ret;
812 
813 	ret = _read_rate(new_opp, opp_table, np);
814 	if (!ret)
815 		found = true;
816 	else if (ret != -ENODEV)
817 		return ret;
818 
819 	/*
820 	 * Bandwidth consists of peak and average (optional) values:
821 	 * opp-peak-kBps = <path1_value path2_value>;
822 	 * opp-avg-kBps = <path1_value path2_value>;
823 	 */
824 	ret = _read_bw(new_opp, opp_table, np, true);
825 	if (!ret) {
826 		found = true;
827 		ret = _read_bw(new_opp, opp_table, np, false);
828 	}
829 
830 	/* The properties were found but we failed to parse them */
831 	if (ret && ret != -ENODEV)
832 		return ret;
833 
834 	if (!of_property_read_u32(np, "opp-level", &new_opp->level))
835 		found = true;
836 
837 	if (found)
838 		return 0;
839 
840 	return ret;
841 }
842 
843 /**
844  * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
845  * @opp_table:	OPP table
846  * @dev:	device for which we do this operation
847  * @np:		device node
848  *
849  * This function adds an opp definition to the opp table and returns status. The
850  * opp can be controlled using dev_pm_opp_enable/disable functions and may be
851  * removed by dev_pm_opp_remove.
852  *
853  * Return:
854  * Valid OPP pointer:
855  *		On success
856  * NULL:
857  *		Duplicate OPPs (both freq and volt are same) and opp->available
858  *		OR if the OPP is not supported by hardware.
859  * ERR_PTR(-EEXIST):
860  *		Freq are same and volt are different OR
861  *		Duplicate OPPs (both freq and volt are same) and !opp->available
862  * ERR_PTR(-ENOMEM):
863  *		Memory allocation failure
864  * ERR_PTR(-EINVAL):
865  *		Failed parsing the OPP node
866  */
867 static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
868 		struct device *dev, struct device_node *np)
869 {
870 	struct dev_pm_opp *new_opp;
871 	u32 val;
872 	int ret;
873 
874 	new_opp = _opp_allocate(opp_table);
875 	if (!new_opp)
876 		return ERR_PTR(-ENOMEM);
877 
878 	ret = _read_opp_key(new_opp, opp_table, np);
879 	if (ret < 0) {
880 		dev_err(dev, "%s: opp key field not found\n", __func__);
881 		goto free_opp;
882 	}
883 
884 	/* Check if the OPP supports hardware's hierarchy of versions or not */
885 	if (!_opp_is_supported(dev, opp_table, np)) {
886 		dev_dbg(dev, "OPP not supported by hardware: %s\n",
887 			of_node_full_name(np));
888 		goto free_opp;
889 	}
890 
891 	new_opp->turbo = of_property_read_bool(np, "turbo-mode");
892 
893 	new_opp->np = of_node_get(np);
894 	new_opp->dynamic = false;
895 	new_opp->available = true;
896 
897 	ret = _of_opp_alloc_required_opps(opp_table, new_opp);
898 	if (ret)
899 		goto put_node;
900 
901 	if (!of_property_read_u32(np, "clock-latency-ns", &val))
902 		new_opp->clock_latency_ns = val;
903 
904 	ret = opp_parse_supplies(new_opp, dev, opp_table);
905 	if (ret)
906 		goto free_required_opps;
907 
908 	ret = _opp_add(dev, new_opp, opp_table);
909 	if (ret) {
910 		/* Don't return error for duplicate OPPs */
911 		if (ret == -EBUSY)
912 			ret = 0;
913 		goto free_required_opps;
914 	}
915 
916 	/* OPP to select on device suspend */
917 	if (of_property_read_bool(np, "opp-suspend")) {
918 		if (opp_table->suspend_opp) {
919 			/* Pick the OPP with higher rate/bw/level as suspend OPP */
920 			if (_opp_compare_key(opp_table, new_opp, opp_table->suspend_opp) == 1) {
921 				opp_table->suspend_opp->suspend = false;
922 				new_opp->suspend = true;
923 				opp_table->suspend_opp = new_opp;
924 			}
925 		} else {
926 			new_opp->suspend = true;
927 			opp_table->suspend_opp = new_opp;
928 		}
929 	}
930 
931 	if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
932 		opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
933 
934 	pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu level:%u\n",
935 		 __func__, new_opp->turbo, new_opp->rates[0],
936 		 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
937 		 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns,
938 		 new_opp->level);
939 
940 	/*
941 	 * Notify the changes in the availability of the operable
942 	 * frequency/voltage list.
943 	 */
944 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
945 	return new_opp;
946 
947 free_required_opps:
948 	_of_opp_free_required_opps(opp_table, new_opp);
949 put_node:
950 	of_node_put(np);
951 free_opp:
952 	_opp_free(new_opp);
953 
954 	return ret ? ERR_PTR(ret) : NULL;
955 }
956 
957 /* Initializes OPP tables based on new bindings */
958 static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
959 {
960 	int ret, count = 0;
961 	struct dev_pm_opp *opp;
962 
963 	/* OPP table is already initialized for the device */
964 	scoped_guard(mutex, &opp_table->lock) {
965 		if (opp_table->parsed_static_opps) {
966 			opp_table->parsed_static_opps++;
967 			return 0;
968 		}
969 
970 		opp_table->parsed_static_opps = 1;
971 	}
972 
973 	/* We have opp-table node now, iterate over it and add OPPs */
974 	for_each_available_child_of_node_scoped(opp_table->np, np) {
975 		opp = _opp_add_static_v2(opp_table, dev, np);
976 		if (IS_ERR(opp)) {
977 			ret = PTR_ERR(opp);
978 			dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
979 				ret);
980 			goto remove_static_opp;
981 		} else if (opp) {
982 			count++;
983 		}
984 	}
985 
986 	/* There should be one or more OPPs defined */
987 	if (!count) {
988 		dev_err(dev, "%s: no supported OPPs", __func__);
989 		ret = -ENOENT;
990 		goto remove_static_opp;
991 	}
992 
993 	lazy_link_required_opp_table(opp_table);
994 
995 	return 0;
996 
997 remove_static_opp:
998 	_opp_remove_all_static(opp_table);
999 
1000 	return ret;
1001 }
1002 
1003 /* Initializes OPP tables based on old-deprecated bindings */
1004 static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
1005 {
1006 	const struct property *prop;
1007 	const __be32 *val;
1008 	int nr, ret = 0;
1009 
1010 	scoped_guard(mutex, &opp_table->lock) {
1011 		if (opp_table->parsed_static_opps) {
1012 			opp_table->parsed_static_opps++;
1013 			return 0;
1014 		}
1015 
1016 		opp_table->parsed_static_opps = 1;
1017 	}
1018 
1019 	prop = of_find_property(dev->of_node, "operating-points", NULL);
1020 	if (!prop) {
1021 		ret = -ENODEV;
1022 		goto remove_static_opp;
1023 	}
1024 	if (!prop->value) {
1025 		ret = -ENODATA;
1026 		goto remove_static_opp;
1027 	}
1028 
1029 	/*
1030 	 * Each OPP is a set of tuples consisting of frequency and
1031 	 * voltage like <freq-kHz vol-uV>.
1032 	 */
1033 	nr = prop->length / sizeof(u32);
1034 	if (nr % 2) {
1035 		dev_err(dev, "%s: Invalid OPP table\n", __func__);
1036 		ret = -EINVAL;
1037 		goto remove_static_opp;
1038 	}
1039 
1040 	val = prop->value;
1041 	while (nr) {
1042 		unsigned long freq = be32_to_cpup(val++) * 1000;
1043 		unsigned long volt = be32_to_cpup(val++);
1044 		struct dev_pm_opp_data data = {
1045 			.freq = freq,
1046 			.u_volt = volt,
1047 		};
1048 
1049 		ret = _opp_add_v1(opp_table, dev, &data, false);
1050 		if (ret) {
1051 			dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
1052 				__func__, data.freq, ret);
1053 			goto remove_static_opp;
1054 		}
1055 		nr -= 2;
1056 	}
1057 
1058 	return 0;
1059 
1060 remove_static_opp:
1061 	_opp_remove_all_static(opp_table);
1062 
1063 	return ret;
1064 }
1065 
1066 static int _of_add_table_indexed(struct device *dev, int index)
1067 {
1068 	struct opp_table *opp_table;
1069 	int ret, count;
1070 
1071 	if (index) {
1072 		/*
1073 		 * If only one phandle is present, then the same OPP table
1074 		 * applies for all index requests.
1075 		 */
1076 		count = of_count_phandle_with_args(dev->of_node,
1077 						   "operating-points-v2", NULL);
1078 		if (count == 1)
1079 			index = 0;
1080 	}
1081 
1082 	opp_table = _add_opp_table_indexed(dev, index, true);
1083 	if (IS_ERR(opp_table))
1084 		return PTR_ERR(opp_table);
1085 
1086 	/*
1087 	 * OPPs have two version of bindings now. Also try the old (v1)
1088 	 * bindings for backward compatibility with older dtbs.
1089 	 */
1090 	if (opp_table->np)
1091 		ret = _of_add_opp_table_v2(dev, opp_table);
1092 	else
1093 		ret = _of_add_opp_table_v1(dev, opp_table);
1094 
1095 	if (ret)
1096 		dev_pm_opp_put_opp_table(opp_table);
1097 
1098 	return ret;
1099 }
1100 
1101 static void devm_pm_opp_of_table_release(void *data)
1102 {
1103 	dev_pm_opp_of_remove_table(data);
1104 }
1105 
1106 static int _devm_of_add_table_indexed(struct device *dev, int index)
1107 {
1108 	int ret;
1109 
1110 	ret = _of_add_table_indexed(dev, index);
1111 	if (ret)
1112 		return ret;
1113 
1114 	return devm_add_action_or_reset(dev, devm_pm_opp_of_table_release, dev);
1115 }
1116 
1117 /**
1118  * devm_pm_opp_of_add_table() - Initialize opp table from device tree
1119  * @dev:	device pointer used to lookup OPP table.
1120  *
1121  * Register the initial OPP table with the OPP library for given device.
1122  *
1123  * The opp_table structure will be freed after the device is destroyed.
1124  *
1125  * Return:
1126  * 0		On success OR
1127  *		Duplicate OPPs (both freq and volt are same) and opp->available
1128  * -EEXIST	Freq are same and volt are different OR
1129  *		Duplicate OPPs (both freq and volt are same) and !opp->available
1130  * -ENOMEM	Memory allocation failure
1131  * -ENODEV	when 'operating-points' property is not found or is invalid data
1132  *		in device node.
1133  * -ENODATA	when empty 'operating-points' property is found
1134  * -EINVAL	when invalid entries are found in opp-v2 table
1135  */
1136 int devm_pm_opp_of_add_table(struct device *dev)
1137 {
1138 	return _devm_of_add_table_indexed(dev, 0);
1139 }
1140 EXPORT_SYMBOL_GPL(devm_pm_opp_of_add_table);
1141 
1142 /**
1143  * dev_pm_opp_of_add_table() - Initialize opp table from device tree
1144  * @dev:	device pointer used to lookup OPP table.
1145  *
1146  * Register the initial OPP table with the OPP library for given device.
1147  *
1148  * Return:
1149  * 0		On success OR
1150  *		Duplicate OPPs (both freq and volt are same) and opp->available
1151  * -EEXIST	Freq are same and volt are different OR
1152  *		Duplicate OPPs (both freq and volt are same) and !opp->available
1153  * -ENOMEM	Memory allocation failure
1154  * -ENODEV	when 'operating-points' property is not found or is invalid data
1155  *		in device node.
1156  * -ENODATA	when empty 'operating-points' property is found
1157  * -EINVAL	when invalid entries are found in opp-v2 table
1158  */
1159 int dev_pm_opp_of_add_table(struct device *dev)
1160 {
1161 	return _of_add_table_indexed(dev, 0);
1162 }
1163 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
1164 
1165 /**
1166  * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
1167  * @dev:	device pointer used to lookup OPP table.
1168  * @index:	Index number.
1169  *
1170  * Register the initial OPP table with the OPP library for given device only
1171  * using the "operating-points-v2" property.
1172  *
1173  * Return: Refer to dev_pm_opp_of_add_table() for return values.
1174  */
1175 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
1176 {
1177 	return _of_add_table_indexed(dev, index);
1178 }
1179 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
1180 
1181 /**
1182  * devm_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
1183  * @dev:	device pointer used to lookup OPP table.
1184  * @index:	Index number.
1185  *
1186  * This is a resource-managed variant of dev_pm_opp_of_add_table_indexed().
1187  */
1188 int devm_pm_opp_of_add_table_indexed(struct device *dev, int index)
1189 {
1190 	return _devm_of_add_table_indexed(dev, index);
1191 }
1192 EXPORT_SYMBOL_GPL(devm_pm_opp_of_add_table_indexed);
1193 
1194 /* CPU device specific helpers */
1195 
1196 /**
1197  * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
1198  * @cpumask:	cpumask for which OPP table needs to be removed
1199  *
1200  * This removes the OPP tables for CPUs present in the @cpumask.
1201  * This should be used only to remove static entries created from DT.
1202  */
1203 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
1204 {
1205 	_dev_pm_opp_cpumask_remove_table(cpumask, -1);
1206 }
1207 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
1208 
1209 /**
1210  * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
1211  * @cpumask:	cpumask for which OPP table needs to be added.
1212  *
1213  * This adds the OPP tables for CPUs present in the @cpumask.
1214  */
1215 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
1216 {
1217 	struct device *cpu_dev;
1218 	int cpu, ret;
1219 
1220 	if (WARN_ON(cpumask_empty(cpumask)))
1221 		return -ENODEV;
1222 
1223 	for_each_cpu(cpu, cpumask) {
1224 		cpu_dev = get_cpu_device(cpu);
1225 		if (!cpu_dev) {
1226 			pr_err("%s: failed to get cpu%d device\n", __func__,
1227 			       cpu);
1228 			ret = -ENODEV;
1229 			goto remove_table;
1230 		}
1231 
1232 		ret = dev_pm_opp_of_add_table(cpu_dev);
1233 		if (ret) {
1234 			/*
1235 			 * OPP may get registered dynamically, don't print error
1236 			 * message here.
1237 			 */
1238 			pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
1239 				 __func__, cpu, ret);
1240 
1241 			goto remove_table;
1242 		}
1243 	}
1244 
1245 	return 0;
1246 
1247 remove_table:
1248 	/* Free all other OPPs */
1249 	_dev_pm_opp_cpumask_remove_table(cpumask, cpu);
1250 
1251 	return ret;
1252 }
1253 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
1254 
1255 /*
1256  * Works only for OPP v2 bindings.
1257  *
1258  * Returns -ENOENT if operating-points-v2 bindings aren't supported.
1259  */
1260 /**
1261  * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
1262  *				      @cpu_dev using operating-points-v2
1263  *				      bindings.
1264  *
1265  * @cpu_dev:	CPU device for which we do this operation
1266  * @cpumask:	cpumask to update with information of sharing CPUs
1267  *
1268  * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
1269  *
1270  * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
1271  */
1272 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
1273 				   struct cpumask *cpumask)
1274 {
1275 	int cpu;
1276 
1277 	/* Get OPP descriptor node */
1278 	struct device_node *np __free(device_node) =
1279 		dev_pm_opp_of_get_opp_desc_node(cpu_dev);
1280 
1281 	if (!np) {
1282 		dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
1283 		return -ENOENT;
1284 	}
1285 
1286 	cpumask_set_cpu(cpu_dev->id, cpumask);
1287 
1288 	/* OPPs are shared ? */
1289 	if (!of_property_read_bool(np, "opp-shared"))
1290 		return 0;
1291 
1292 	for_each_possible_cpu(cpu) {
1293 		if (cpu == cpu_dev->id)
1294 			continue;
1295 
1296 		struct device_node *cpu_np __free(device_node) =
1297 			of_cpu_device_node_get(cpu);
1298 
1299 		if (!cpu_np) {
1300 			dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
1301 				__func__, cpu);
1302 			return -ENOENT;
1303 		}
1304 
1305 		/* Get OPP descriptor node */
1306 		struct device_node *tmp_np __free(device_node) =
1307 			_opp_of_get_opp_desc_node(cpu_np, 0);
1308 
1309 		if (!tmp_np) {
1310 			pr_err("%pOF: Couldn't find opp node\n", cpu_np);
1311 			return -ENOENT;
1312 		}
1313 
1314 		/* CPUs are sharing opp node */
1315 		if (np == tmp_np)
1316 			cpumask_set_cpu(cpu, cpumask);
1317 	}
1318 
1319 	return 0;
1320 }
1321 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
1322 
1323 /**
1324  * of_get_required_opp_performance_state() - Search for required OPP and return its performance state.
1325  * @np: Node that contains the "required-opps" property.
1326  * @index: Index of the phandle to parse.
1327  *
1328  * Returns the performance state of the OPP pointed out by the "required-opps"
1329  * property at @index in @np.
1330  *
1331  * Return: Zero or positive performance state on success, otherwise negative
1332  * value on errors.
1333  */
1334 int of_get_required_opp_performance_state(struct device_node *np, int index)
1335 {
1336 	int pstate = -EINVAL;
1337 
1338 	struct device_node *required_np __free(device_node) =
1339 		of_parse_required_opp(np, index);
1340 
1341 	if (!required_np)
1342 		return -ENODEV;
1343 
1344 	struct opp_table *opp_table __free(put_opp_table) =
1345 		_find_table_of_opp_np(required_np);
1346 
1347 	if (IS_ERR(opp_table)) {
1348 		pr_err("%s: Failed to find required OPP table %pOF: %ld\n",
1349 		       __func__, np, PTR_ERR(opp_table));
1350 		return PTR_ERR(opp_table);
1351 	}
1352 
1353 	/* The OPP tables must belong to a genpd */
1354 	if (unlikely(!opp_table->is_genpd)) {
1355 		pr_err("%s: Performance state is only valid for genpds.\n", __func__);
1356 		return -EINVAL;
1357 	}
1358 
1359 	struct dev_pm_opp *opp __free(put_opp) =
1360 		_find_opp_of_np(opp_table, required_np);
1361 
1362 	if (opp) {
1363 		if (opp->level == OPP_LEVEL_UNSET) {
1364 			pr_err("%s: OPP levels aren't available for %pOF\n",
1365 			       __func__, np);
1366 		} else {
1367 			pstate = opp->level;
1368 		}
1369 	}
1370 
1371 	return pstate;
1372 }
1373 EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
1374 
1375 /**
1376  * dev_pm_opp_of_has_required_opp - Find out if a required-opps exists.
1377  * @dev: The device to investigate.
1378  *
1379  * Returns true if the device's node has a "operating-points-v2" property and if
1380  * the corresponding node for the opp-table describes opp nodes that uses the
1381  * "required-opps" property.
1382  *
1383  * Return: True if a required-opps is present, else false.
1384  */
1385 bool dev_pm_opp_of_has_required_opp(struct device *dev)
1386 {
1387 	int count;
1388 
1389 	struct device_node *opp_np __free(device_node) =
1390 		_opp_of_get_opp_desc_node(dev->of_node, 0);
1391 
1392 	if (!opp_np)
1393 		return false;
1394 
1395 	struct device_node *np __free(device_node) =
1396 		of_get_next_available_child(opp_np, NULL);
1397 
1398 	if (!np) {
1399 		dev_warn(dev, "Empty OPP table\n");
1400 		return false;
1401 	}
1402 
1403 	count = of_count_phandle_with_args(np, "required-opps", NULL);
1404 
1405 	return count > 0;
1406 }
1407 
1408 /**
1409  * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
1410  * @opp:	opp for which DT node has to be returned for
1411  *
1412  * Return: DT node corresponding to the opp, else 0 on success.
1413  *
1414  * The caller needs to put the node with of_node_put() after using it.
1415  */
1416 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
1417 {
1418 	if (IS_ERR_OR_NULL(opp)) {
1419 		pr_err("%s: Invalid parameters\n", __func__);
1420 		return NULL;
1421 	}
1422 
1423 	return of_node_get(opp->np);
1424 }
1425 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
1426 
1427 /*
1428  * Callback function provided to the Energy Model framework upon registration.
1429  * It provides the power used by @dev at @kHz if it is the frequency of an
1430  * existing OPP, or at the frequency of the first OPP above @kHz otherwise
1431  * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
1432  * frequency and @uW to the associated power.
1433  *
1434  * Returns 0 on success or a proper -EINVAL value in case of error.
1435  */
1436 static int __maybe_unused
1437 _get_dt_power(struct device *dev, unsigned long *uW, unsigned long *kHz)
1438 {
1439 	unsigned long opp_freq, opp_power;
1440 
1441 	/* Find the right frequency and related OPP */
1442 	opp_freq = *kHz * 1000;
1443 
1444 	struct dev_pm_opp *opp __free(put_opp) =
1445 		dev_pm_opp_find_freq_ceil(dev, &opp_freq);
1446 
1447 	if (IS_ERR(opp))
1448 		return -EINVAL;
1449 
1450 	opp_power = dev_pm_opp_get_power(opp);
1451 	if (!opp_power)
1452 		return -EINVAL;
1453 
1454 	*kHz = opp_freq / 1000;
1455 	*uW = opp_power;
1456 
1457 	return 0;
1458 }
1459 
1460 /**
1461  * dev_pm_opp_calc_power() - Calculate power value for device with EM
1462  * @dev		: Device for which an Energy Model has to be registered
1463  * @uW		: New power value that is calculated
1464  * @kHz		: Frequency for which the new power is calculated
1465  *
1466  * This computes the power estimated by @dev at @kHz if it is the frequency
1467  * of an existing OPP, or at the frequency of the first OPP above @kHz otherwise
1468  * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
1469  * frequency and @uW to the associated power. The power is estimated as
1470  * P = C * V^2 * f with C being the device's capacitance and V and f
1471  * respectively the voltage and frequency of the OPP.
1472  * It is also used as a callback function provided to the Energy Model
1473  * framework upon registration.
1474  *
1475  * Returns -EINVAL if the power calculation failed because of missing
1476  * parameters, 0 otherwise.
1477  */
1478 int dev_pm_opp_calc_power(struct device *dev, unsigned long *uW,
1479 			  unsigned long *kHz)
1480 {
1481 	unsigned long mV, Hz;
1482 	u32 cap;
1483 	u64 tmp;
1484 	int ret;
1485 
1486 	struct device_node *np __free(device_node) = of_node_get(dev->of_node);
1487 
1488 	if (!np)
1489 		return -EINVAL;
1490 
1491 	ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1492 	if (ret)
1493 		return -EINVAL;
1494 
1495 	Hz = *kHz * 1000;
1496 
1497 	struct dev_pm_opp *opp __free(put_opp) =
1498 		dev_pm_opp_find_freq_ceil(dev, &Hz);
1499 
1500 	if (IS_ERR(opp))
1501 		return -EINVAL;
1502 
1503 	mV = dev_pm_opp_get_voltage(opp) / 1000;
1504 	if (!mV)
1505 		return -EINVAL;
1506 
1507 	tmp = (u64)cap * mV * mV * (Hz / 1000000);
1508 	/* Provide power in micro-Watts */
1509 	do_div(tmp, 1000000);
1510 
1511 	*uW = (unsigned long)tmp;
1512 	*kHz = Hz / 1000;
1513 
1514 	return 0;
1515 }
1516 EXPORT_SYMBOL_GPL(dev_pm_opp_calc_power);
1517 
1518 static bool _of_has_opp_microwatt_property(struct device *dev)
1519 {
1520 	unsigned long freq = 0;
1521 
1522 	/* Check if at least one OPP has needed property */
1523 	struct dev_pm_opp *opp __free(put_opp) =
1524 		dev_pm_opp_find_freq_ceil(dev, &freq);
1525 
1526 	if (IS_ERR(opp))
1527 		return false;
1528 
1529 	return !!dev_pm_opp_get_power(opp);
1530 }
1531 
1532 /**
1533  * dev_pm_opp_of_register_em() - Attempt to register an Energy Model
1534  * @dev		: Device for which an Energy Model has to be registered
1535  * @cpus	: CPUs for which an Energy Model has to be registered. For
1536  *		other type of devices it should be set to NULL.
1537  *
1538  * This checks whether the "dynamic-power-coefficient" devicetree property has
1539  * been specified, and tries to register an Energy Model with it if it has.
1540  * Having this property means the voltages are known for OPPs and the EM
1541  * might be calculated.
1542  */
1543 int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus)
1544 {
1545 	struct em_data_callback em_cb;
1546 	int ret, nr_opp;
1547 	u32 cap;
1548 
1549 	if (IS_ERR_OR_NULL(dev))
1550 		return -EINVAL;
1551 
1552 	struct device_node *np __free(device_node) = of_node_get(dev->of_node);
1553 
1554 	if (!np) {
1555 		ret = -EINVAL;
1556 		goto failed;
1557 	}
1558 
1559 	nr_opp = dev_pm_opp_get_opp_count(dev);
1560 	if (nr_opp <= 0) {
1561 		ret = -EINVAL;
1562 		goto failed;
1563 	}
1564 
1565 	/* First, try to find more precised Energy Model in DT */
1566 	if (_of_has_opp_microwatt_property(dev)) {
1567 		EM_SET_ACTIVE_POWER_CB(em_cb, _get_dt_power);
1568 		goto register_em;
1569 	}
1570 
1571 	/*
1572 	 * Register an EM only if the 'dynamic-power-coefficient' property is
1573 	 * set in devicetree. It is assumed the voltage values are known if that
1574 	 * property is set since it is useless otherwise. If voltages are not
1575 	 * known, just let the EM registration fail with an error to alert the
1576 	 * user about the inconsistent configuration.
1577 	 */
1578 	ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1579 	if (ret || !cap) {
1580 		dev_dbg(dev, "Couldn't find proper 'dynamic-power-coefficient' in DT\n");
1581 		ret = -EINVAL;
1582 		goto failed;
1583 	}
1584 
1585 	EM_SET_ACTIVE_POWER_CB(em_cb, dev_pm_opp_calc_power);
1586 
1587 register_em:
1588 	ret = em_dev_register_perf_domain(dev, nr_opp, &em_cb, cpus, true);
1589 	if (ret)
1590 		goto failed;
1591 
1592 	return 0;
1593 
1594 failed:
1595 	dev_dbg(dev, "Couldn't register Energy Model %d\n", ret);
1596 	return ret;
1597 }
1598 EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);
1599