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