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