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