xref: /openbmc/linux/drivers/opp/of.c (revision 5b4cb650)
1 /*
2  * Generic OPP OF helpers
3  *
4  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
5  *	Nishanth Menon
6  *	Romit Dasgupta
7  *	Kevin Hilman
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
16 #include <linux/cpu.h>
17 #include <linux/errno.h>
18 #include <linux/device.h>
19 #include <linux/of_device.h>
20 #include <linux/pm_domain.h>
21 #include <linux/slab.h>
22 #include <linux/export.h>
23 
24 #include "opp.h"
25 
26 /*
27  * Returns opp descriptor node for a device node, caller must
28  * do of_node_put().
29  */
30 static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
31 						     int index)
32 {
33 	/* "operating-points-v2" can be an array for power domain providers */
34 	return of_parse_phandle(np, "operating-points-v2", index);
35 }
36 
37 /* Returns opp descriptor node for a device, caller must do of_node_put() */
38 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
39 {
40 	return _opp_of_get_opp_desc_node(dev->of_node, 0);
41 }
42 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
43 
44 struct opp_table *_managed_opp(struct device *dev, int index)
45 {
46 	struct opp_table *opp_table, *managed_table = NULL;
47 	struct device_node *np;
48 
49 	np = _opp_of_get_opp_desc_node(dev->of_node, index);
50 	if (!np)
51 		return NULL;
52 
53 	list_for_each_entry(opp_table, &opp_tables, node) {
54 		if (opp_table->np == np) {
55 			/*
56 			 * Multiple devices can point to the same OPP table and
57 			 * so will have same node-pointer, np.
58 			 *
59 			 * But the OPPs will be considered as shared only if the
60 			 * OPP table contains a "opp-shared" property.
61 			 */
62 			if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
63 				_get_opp_table_kref(opp_table);
64 				managed_table = opp_table;
65 			}
66 
67 			break;
68 		}
69 	}
70 
71 	of_node_put(np);
72 
73 	return managed_table;
74 }
75 
76 /* The caller must call dev_pm_opp_put() after the OPP is used */
77 static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table,
78 					  struct device_node *opp_np)
79 {
80 	struct dev_pm_opp *opp;
81 
82 	lockdep_assert_held(&opp_table_lock);
83 
84 	mutex_lock(&opp_table->lock);
85 
86 	list_for_each_entry(opp, &opp_table->opp_list, node) {
87 		if (opp->np == opp_np) {
88 			dev_pm_opp_get(opp);
89 			mutex_unlock(&opp_table->lock);
90 			return opp;
91 		}
92 	}
93 
94 	mutex_unlock(&opp_table->lock);
95 
96 	return NULL;
97 }
98 
99 static struct device_node *of_parse_required_opp(struct device_node *np,
100 						 int index)
101 {
102 	struct device_node *required_np;
103 
104 	required_np = of_parse_phandle(np, "required-opps", index);
105 	if (unlikely(!required_np)) {
106 		pr_err("%s: Unable to parse required-opps: %pOF, index: %d\n",
107 		       __func__, np, index);
108 	}
109 
110 	return required_np;
111 }
112 
113 /* The caller must call dev_pm_opp_put_opp_table() after the table is used */
114 static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np)
115 {
116 	struct opp_table *opp_table;
117 	struct device_node *opp_table_np;
118 
119 	lockdep_assert_held(&opp_table_lock);
120 
121 	opp_table_np = of_get_parent(opp_np);
122 	if (!opp_table_np)
123 		goto err;
124 
125 	/* It is safe to put the node now as all we need now is its address */
126 	of_node_put(opp_table_np);
127 
128 	list_for_each_entry(opp_table, &opp_tables, node) {
129 		if (opp_table_np == opp_table->np) {
130 			_get_opp_table_kref(opp_table);
131 			return opp_table;
132 		}
133 	}
134 
135 err:
136 	return ERR_PTR(-ENODEV);
137 }
138 
139 /* Free resources previously acquired by _opp_table_alloc_required_tables() */
140 static void _opp_table_free_required_tables(struct opp_table *opp_table)
141 {
142 	struct opp_table **required_opp_tables = opp_table->required_opp_tables;
143 	struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
144 	int i;
145 
146 	if (!required_opp_tables)
147 		return;
148 
149 	for (i = 0; i < opp_table->required_opp_count; i++) {
150 		if (IS_ERR_OR_NULL(required_opp_tables[i]))
151 			break;
152 
153 		dev_pm_opp_put_opp_table(required_opp_tables[i]);
154 	}
155 
156 	kfree(required_opp_tables);
157 	kfree(genpd_virt_devs);
158 
159 	opp_table->required_opp_count = 0;
160 	opp_table->genpd_virt_devs = NULL;
161 	opp_table->required_opp_tables = NULL;
162 }
163 
164 /*
165  * Populate all devices and opp tables which are part of "required-opps" list.
166  * Checking only the first OPP node should be enough.
167  */
168 static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
169 					     struct device *dev,
170 					     struct device_node *opp_np)
171 {
172 	struct opp_table **required_opp_tables;
173 	struct device **genpd_virt_devs = NULL;
174 	struct device_node *required_np, *np;
175 	int count, i;
176 
177 	/* Traversing the first OPP node is all we need */
178 	np = of_get_next_available_child(opp_np, NULL);
179 	if (!np) {
180 		dev_err(dev, "Empty OPP table\n");
181 		return;
182 	}
183 
184 	count = of_count_phandle_with_args(np, "required-opps", NULL);
185 	if (!count)
186 		goto put_np;
187 
188 	if (count > 1) {
189 		genpd_virt_devs = kcalloc(count, sizeof(*genpd_virt_devs),
190 					GFP_KERNEL);
191 		if (!genpd_virt_devs)
192 			goto put_np;
193 	}
194 
195 	required_opp_tables = kcalloc(count, sizeof(*required_opp_tables),
196 				      GFP_KERNEL);
197 	if (!required_opp_tables) {
198 		kfree(genpd_virt_devs);
199 		goto put_np;
200 	}
201 
202 	opp_table->genpd_virt_devs = genpd_virt_devs;
203 	opp_table->required_opp_tables = required_opp_tables;
204 	opp_table->required_opp_count = count;
205 
206 	for (i = 0; i < count; i++) {
207 		required_np = of_parse_required_opp(np, i);
208 		if (!required_np)
209 			goto free_required_tables;
210 
211 		required_opp_tables[i] = _find_table_of_opp_np(required_np);
212 		of_node_put(required_np);
213 
214 		if (IS_ERR(required_opp_tables[i]))
215 			goto free_required_tables;
216 
217 		/*
218 		 * We only support genpd's OPPs in the "required-opps" for now,
219 		 * as we don't know how much about other cases. Error out if the
220 		 * required OPP doesn't belong to a genpd.
221 		 */
222 		if (!required_opp_tables[i]->is_genpd) {
223 			dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n",
224 				required_np);
225 			goto free_required_tables;
226 		}
227 	}
228 
229 	goto put_np;
230 
231 free_required_tables:
232 	_opp_table_free_required_tables(opp_table);
233 put_np:
234 	of_node_put(np);
235 }
236 
237 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
238 			int index)
239 {
240 	struct device_node *np, *opp_np;
241 	u32 val;
242 
243 	/*
244 	 * Only required for backward compatibility with v1 bindings, but isn't
245 	 * harmful for other cases. And so we do it unconditionally.
246 	 */
247 	np = of_node_get(dev->of_node);
248 	if (!np)
249 		return;
250 
251 	if (!of_property_read_u32(np, "clock-latency", &val))
252 		opp_table->clock_latency_ns_max = val;
253 	of_property_read_u32(np, "voltage-tolerance",
254 			     &opp_table->voltage_tolerance_v1);
255 
256 	if (of_find_property(np, "#power-domain-cells", NULL))
257 		opp_table->is_genpd = true;
258 
259 	/* Get OPP table node */
260 	opp_np = _opp_of_get_opp_desc_node(np, index);
261 	of_node_put(np);
262 
263 	if (!opp_np)
264 		return;
265 
266 	if (of_property_read_bool(opp_np, "opp-shared"))
267 		opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
268 	else
269 		opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
270 
271 	opp_table->np = opp_np;
272 
273 	_opp_table_alloc_required_tables(opp_table, dev, opp_np);
274 	of_node_put(opp_np);
275 }
276 
277 void _of_clear_opp_table(struct opp_table *opp_table)
278 {
279 	_opp_table_free_required_tables(opp_table);
280 }
281 
282 /*
283  * Release all resources previously acquired with a call to
284  * _of_opp_alloc_required_opps().
285  */
286 void _of_opp_free_required_opps(struct opp_table *opp_table,
287 				struct dev_pm_opp *opp)
288 {
289 	struct dev_pm_opp **required_opps = opp->required_opps;
290 	int i;
291 
292 	if (!required_opps)
293 		return;
294 
295 	for (i = 0; i < opp_table->required_opp_count; i++) {
296 		if (!required_opps[i])
297 			break;
298 
299 		/* Put the reference back */
300 		dev_pm_opp_put(required_opps[i]);
301 	}
302 
303 	kfree(required_opps);
304 	opp->required_opps = NULL;
305 }
306 
307 /* Populate all required OPPs which are part of "required-opps" list */
308 static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
309 				       struct dev_pm_opp *opp)
310 {
311 	struct dev_pm_opp **required_opps;
312 	struct opp_table *required_table;
313 	struct device_node *np;
314 	int i, ret, count = opp_table->required_opp_count;
315 
316 	if (!count)
317 		return 0;
318 
319 	required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL);
320 	if (!required_opps)
321 		return -ENOMEM;
322 
323 	opp->required_opps = required_opps;
324 
325 	for (i = 0; i < count; i++) {
326 		required_table = opp_table->required_opp_tables[i];
327 
328 		np = of_parse_required_opp(opp->np, i);
329 		if (unlikely(!np)) {
330 			ret = -ENODEV;
331 			goto free_required_opps;
332 		}
333 
334 		required_opps[i] = _find_opp_of_np(required_table, np);
335 		of_node_put(np);
336 
337 		if (!required_opps[i]) {
338 			pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
339 			       __func__, opp->np, i);
340 			ret = -ENODEV;
341 			goto free_required_opps;
342 		}
343 	}
344 
345 	return 0;
346 
347 free_required_opps:
348 	_of_opp_free_required_opps(opp_table, opp);
349 
350 	return ret;
351 }
352 
353 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
354 			      struct device_node *np)
355 {
356 	unsigned int count = opp_table->supported_hw_count;
357 	u32 version;
358 	int ret;
359 
360 	if (!opp_table->supported_hw) {
361 		/*
362 		 * In the case that no supported_hw has been set by the
363 		 * platform but there is an opp-supported-hw value set for
364 		 * an OPP then the OPP should not be enabled as there is
365 		 * no way to see if the hardware supports it.
366 		 */
367 		if (of_find_property(np, "opp-supported-hw", NULL))
368 			return false;
369 		else
370 			return true;
371 	}
372 
373 	while (count--) {
374 		ret = of_property_read_u32_index(np, "opp-supported-hw", count,
375 						 &version);
376 		if (ret) {
377 			dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
378 				 __func__, count, ret);
379 			return false;
380 		}
381 
382 		/* Both of these are bitwise masks of the versions */
383 		if (!(version & opp_table->supported_hw[count]))
384 			return false;
385 	}
386 
387 	return true;
388 }
389 
390 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
391 			      struct opp_table *opp_table)
392 {
393 	u32 *microvolt, *microamp = NULL;
394 	int supplies = opp_table->regulator_count, vcount, icount, ret, i, j;
395 	struct property *prop = NULL;
396 	char name[NAME_MAX];
397 
398 	/* Search for "opp-microvolt-<name>" */
399 	if (opp_table->prop_name) {
400 		snprintf(name, sizeof(name), "opp-microvolt-%s",
401 			 opp_table->prop_name);
402 		prop = of_find_property(opp->np, name, NULL);
403 	}
404 
405 	if (!prop) {
406 		/* Search for "opp-microvolt" */
407 		sprintf(name, "opp-microvolt");
408 		prop = of_find_property(opp->np, name, NULL);
409 
410 		/* Missing property isn't a problem, but an invalid entry is */
411 		if (!prop) {
412 			if (unlikely(supplies == -1)) {
413 				/* Initialize regulator_count */
414 				opp_table->regulator_count = 0;
415 				return 0;
416 			}
417 
418 			if (!supplies)
419 				return 0;
420 
421 			dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
422 				__func__);
423 			return -EINVAL;
424 		}
425 	}
426 
427 	if (unlikely(supplies == -1)) {
428 		/* Initialize regulator_count */
429 		supplies = opp_table->regulator_count = 1;
430 	} else if (unlikely(!supplies)) {
431 		dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__);
432 		return -EINVAL;
433 	}
434 
435 	vcount = of_property_count_u32_elems(opp->np, name);
436 	if (vcount < 0) {
437 		dev_err(dev, "%s: Invalid %s property (%d)\n",
438 			__func__, name, vcount);
439 		return vcount;
440 	}
441 
442 	/* There can be one or three elements per supply */
443 	if (vcount != supplies && vcount != supplies * 3) {
444 		dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
445 			__func__, name, vcount, supplies);
446 		return -EINVAL;
447 	}
448 
449 	microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
450 	if (!microvolt)
451 		return -ENOMEM;
452 
453 	ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
454 	if (ret) {
455 		dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
456 		ret = -EINVAL;
457 		goto free_microvolt;
458 	}
459 
460 	/* Search for "opp-microamp-<name>" */
461 	prop = NULL;
462 	if (opp_table->prop_name) {
463 		snprintf(name, sizeof(name), "opp-microamp-%s",
464 			 opp_table->prop_name);
465 		prop = of_find_property(opp->np, name, NULL);
466 	}
467 
468 	if (!prop) {
469 		/* Search for "opp-microamp" */
470 		sprintf(name, "opp-microamp");
471 		prop = of_find_property(opp->np, name, NULL);
472 	}
473 
474 	if (prop) {
475 		icount = of_property_count_u32_elems(opp->np, name);
476 		if (icount < 0) {
477 			dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
478 				name, icount);
479 			ret = icount;
480 			goto free_microvolt;
481 		}
482 
483 		if (icount != supplies) {
484 			dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
485 				__func__, name, icount, supplies);
486 			ret = -EINVAL;
487 			goto free_microvolt;
488 		}
489 
490 		microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
491 		if (!microamp) {
492 			ret = -EINVAL;
493 			goto free_microvolt;
494 		}
495 
496 		ret = of_property_read_u32_array(opp->np, name, microamp,
497 						 icount);
498 		if (ret) {
499 			dev_err(dev, "%s: error parsing %s: %d\n", __func__,
500 				name, ret);
501 			ret = -EINVAL;
502 			goto free_microamp;
503 		}
504 	}
505 
506 	for (i = 0, j = 0; i < supplies; i++) {
507 		opp->supplies[i].u_volt = microvolt[j++];
508 
509 		if (vcount == supplies) {
510 			opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
511 			opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
512 		} else {
513 			opp->supplies[i].u_volt_min = microvolt[j++];
514 			opp->supplies[i].u_volt_max = microvolt[j++];
515 		}
516 
517 		if (microamp)
518 			opp->supplies[i].u_amp = microamp[i];
519 	}
520 
521 free_microamp:
522 	kfree(microamp);
523 free_microvolt:
524 	kfree(microvolt);
525 
526 	return ret;
527 }
528 
529 /**
530  * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
531  *				  entries
532  * @dev:	device pointer used to lookup OPP table.
533  *
534  * Free OPPs created using static entries present in DT.
535  */
536 void dev_pm_opp_of_remove_table(struct device *dev)
537 {
538 	_dev_pm_opp_find_and_remove_table(dev);
539 }
540 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
541 
542 /**
543  * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
544  * @opp_table:	OPP table
545  * @dev:	device for which we do this operation
546  * @np:		device node
547  *
548  * This function adds an opp definition to the opp table and returns status. The
549  * opp can be controlled using dev_pm_opp_enable/disable functions and may be
550  * removed by dev_pm_opp_remove.
551  *
552  * Return:
553  * Valid OPP pointer:
554  *		On success
555  * NULL:
556  *		Duplicate OPPs (both freq and volt are same) and opp->available
557  *		OR if the OPP is not supported by hardware.
558  * ERR_PTR(-EEXIST):
559  *		Freq are same and volt are different OR
560  *		Duplicate OPPs (both freq and volt are same) and !opp->available
561  * ERR_PTR(-ENOMEM):
562  *		Memory allocation failure
563  * ERR_PTR(-EINVAL):
564  *		Failed parsing the OPP node
565  */
566 static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
567 		struct device *dev, struct device_node *np)
568 {
569 	struct dev_pm_opp *new_opp;
570 	u64 rate = 0;
571 	u32 val;
572 	int ret;
573 	bool rate_not_available = false;
574 
575 	new_opp = _opp_allocate(opp_table);
576 	if (!new_opp)
577 		return ERR_PTR(-ENOMEM);
578 
579 	ret = of_property_read_u64(np, "opp-hz", &rate);
580 	if (ret < 0) {
581 		/* "opp-hz" is optional for devices like power domains. */
582 		if (!opp_table->is_genpd) {
583 			dev_err(dev, "%s: opp-hz not found\n", __func__);
584 			goto free_opp;
585 		}
586 
587 		rate_not_available = true;
588 	} else {
589 		/*
590 		 * Rate is defined as an unsigned long in clk API, and so
591 		 * casting explicitly to its type. Must be fixed once rate is 64
592 		 * bit guaranteed in clk API.
593 		 */
594 		new_opp->rate = (unsigned long)rate;
595 	}
596 
597 	/* Check if the OPP supports hardware's hierarchy of versions or not */
598 	if (!_opp_is_supported(dev, opp_table, np)) {
599 		dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
600 		goto free_opp;
601 	}
602 
603 	new_opp->turbo = of_property_read_bool(np, "turbo-mode");
604 
605 	new_opp->np = np;
606 	new_opp->dynamic = false;
607 	new_opp->available = true;
608 
609 	ret = _of_opp_alloc_required_opps(opp_table, new_opp);
610 	if (ret)
611 		goto free_opp;
612 
613 	if (!of_property_read_u32(np, "clock-latency-ns", &val))
614 		new_opp->clock_latency_ns = val;
615 
616 	ret = opp_parse_supplies(new_opp, dev, opp_table);
617 	if (ret)
618 		goto free_required_opps;
619 
620 	if (opp_table->is_genpd)
621 		new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp);
622 
623 	ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
624 	if (ret) {
625 		/* Don't return error for duplicate OPPs */
626 		if (ret == -EBUSY)
627 			ret = 0;
628 		goto free_required_opps;
629 	}
630 
631 	/* OPP to select on device suspend */
632 	if (of_property_read_bool(np, "opp-suspend")) {
633 		if (opp_table->suspend_opp) {
634 			dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
635 				 __func__, opp_table->suspend_opp->rate,
636 				 new_opp->rate);
637 		} else {
638 			new_opp->suspend = true;
639 			opp_table->suspend_opp = new_opp;
640 		}
641 	}
642 
643 	if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
644 		opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
645 
646 	pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
647 		 __func__, new_opp->turbo, new_opp->rate,
648 		 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
649 		 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
650 
651 	/*
652 	 * Notify the changes in the availability of the operable
653 	 * frequency/voltage list.
654 	 */
655 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
656 	return new_opp;
657 
658 free_required_opps:
659 	_of_opp_free_required_opps(opp_table, new_opp);
660 free_opp:
661 	_opp_free(new_opp);
662 
663 	return ERR_PTR(ret);
664 }
665 
666 /* Initializes OPP tables based on new bindings */
667 static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
668 {
669 	struct device_node *np;
670 	int ret, count = 0, pstate_count = 0;
671 	struct dev_pm_opp *opp;
672 
673 	/* OPP table is already initialized for the device */
674 	if (opp_table->parsed_static_opps) {
675 		kref_get(&opp_table->list_kref);
676 		return 0;
677 	}
678 
679 	kref_init(&opp_table->list_kref);
680 
681 	/* We have opp-table node now, iterate over it and add OPPs */
682 	for_each_available_child_of_node(opp_table->np, np) {
683 		opp = _opp_add_static_v2(opp_table, dev, np);
684 		if (IS_ERR(opp)) {
685 			ret = PTR_ERR(opp);
686 			dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
687 				ret);
688 			of_node_put(np);
689 			goto put_list_kref;
690 		} else if (opp) {
691 			count++;
692 		}
693 	}
694 
695 	/* There should be one of more OPP defined */
696 	if (WARN_ON(!count)) {
697 		ret = -ENOENT;
698 		goto put_list_kref;
699 	}
700 
701 	list_for_each_entry(opp, &opp_table->opp_list, node)
702 		pstate_count += !!opp->pstate;
703 
704 	/* Either all or none of the nodes shall have performance state set */
705 	if (pstate_count && pstate_count != count) {
706 		dev_err(dev, "Not all nodes have performance state set (%d: %d)\n",
707 			count, pstate_count);
708 		ret = -ENOENT;
709 		goto put_list_kref;
710 	}
711 
712 	if (pstate_count)
713 		opp_table->genpd_performance_state = true;
714 
715 	opp_table->parsed_static_opps = true;
716 
717 	return 0;
718 
719 put_list_kref:
720 	_put_opp_list_kref(opp_table);
721 
722 	return ret;
723 }
724 
725 /* Initializes OPP tables based on old-deprecated bindings */
726 static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
727 {
728 	const struct property *prop;
729 	const __be32 *val;
730 	int nr, ret = 0;
731 
732 	prop = of_find_property(dev->of_node, "operating-points", NULL);
733 	if (!prop)
734 		return -ENODEV;
735 	if (!prop->value)
736 		return -ENODATA;
737 
738 	/*
739 	 * Each OPP is a set of tuples consisting of frequency and
740 	 * voltage like <freq-kHz vol-uV>.
741 	 */
742 	nr = prop->length / sizeof(u32);
743 	if (nr % 2) {
744 		dev_err(dev, "%s: Invalid OPP table\n", __func__);
745 		return -EINVAL;
746 	}
747 
748 	kref_init(&opp_table->list_kref);
749 
750 	val = prop->value;
751 	while (nr) {
752 		unsigned long freq = be32_to_cpup(val++) * 1000;
753 		unsigned long volt = be32_to_cpup(val++);
754 
755 		ret = _opp_add_v1(opp_table, dev, freq, volt, false);
756 		if (ret) {
757 			dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
758 				__func__, freq, ret);
759 			_put_opp_list_kref(opp_table);
760 			return ret;
761 		}
762 		nr -= 2;
763 	}
764 
765 	return ret;
766 }
767 
768 /**
769  * dev_pm_opp_of_add_table() - Initialize opp table from device tree
770  * @dev:	device pointer used to lookup OPP table.
771  *
772  * Register the initial OPP table with the OPP library for given device.
773  *
774  * Return:
775  * 0		On success OR
776  *		Duplicate OPPs (both freq and volt are same) and opp->available
777  * -EEXIST	Freq are same and volt are different OR
778  *		Duplicate OPPs (both freq and volt are same) and !opp->available
779  * -ENOMEM	Memory allocation failure
780  * -ENODEV	when 'operating-points' property is not found or is invalid data
781  *		in device node.
782  * -ENODATA	when empty 'operating-points' property is found
783  * -EINVAL	when invalid entries are found in opp-v2 table
784  */
785 int dev_pm_opp_of_add_table(struct device *dev)
786 {
787 	struct opp_table *opp_table;
788 	int ret;
789 
790 	opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0);
791 	if (!opp_table)
792 		return -ENOMEM;
793 
794 	/*
795 	 * OPPs have two version of bindings now. Also try the old (v1)
796 	 * bindings for backward compatibility with older dtbs.
797 	 */
798 	if (opp_table->np)
799 		ret = _of_add_opp_table_v2(dev, opp_table);
800 	else
801 		ret = _of_add_opp_table_v1(dev, opp_table);
802 
803 	if (ret)
804 		dev_pm_opp_put_opp_table(opp_table);
805 
806 	return ret;
807 }
808 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
809 
810 /**
811  * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
812  * @dev:	device pointer used to lookup OPP table.
813  * @index:	Index number.
814  *
815  * Register the initial OPP table with the OPP library for given device only
816  * using the "operating-points-v2" property.
817  *
818  * Return:
819  * 0		On success OR
820  *		Duplicate OPPs (both freq and volt are same) and opp->available
821  * -EEXIST	Freq are same and volt are different OR
822  *		Duplicate OPPs (both freq and volt are same) and !opp->available
823  * -ENOMEM	Memory allocation failure
824  * -ENODEV	when 'operating-points' property is not found or is invalid data
825  *		in device node.
826  * -ENODATA	when empty 'operating-points' property is found
827  * -EINVAL	when invalid entries are found in opp-v2 table
828  */
829 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
830 {
831 	struct opp_table *opp_table;
832 	int ret, count;
833 
834 	if (index) {
835 		/*
836 		 * If only one phandle is present, then the same OPP table
837 		 * applies for all index requests.
838 		 */
839 		count = of_count_phandle_with_args(dev->of_node,
840 						   "operating-points-v2", NULL);
841 		if (count == 1)
842 			index = 0;
843 	}
844 
845 	opp_table = dev_pm_opp_get_opp_table_indexed(dev, index);
846 	if (!opp_table)
847 		return -ENOMEM;
848 
849 	ret = _of_add_opp_table_v2(dev, opp_table);
850 	if (ret)
851 		dev_pm_opp_put_opp_table(opp_table);
852 
853 	return ret;
854 }
855 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
856 
857 /* CPU device specific helpers */
858 
859 /**
860  * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
861  * @cpumask:	cpumask for which OPP table needs to be removed
862  *
863  * This removes the OPP tables for CPUs present in the @cpumask.
864  * This should be used only to remove static entries created from DT.
865  */
866 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
867 {
868 	_dev_pm_opp_cpumask_remove_table(cpumask, -1);
869 }
870 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
871 
872 /**
873  * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
874  * @cpumask:	cpumask for which OPP table needs to be added.
875  *
876  * This adds the OPP tables for CPUs present in the @cpumask.
877  */
878 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
879 {
880 	struct device *cpu_dev;
881 	int cpu, ret;
882 
883 	if (WARN_ON(cpumask_empty(cpumask)))
884 		return -ENODEV;
885 
886 	for_each_cpu(cpu, cpumask) {
887 		cpu_dev = get_cpu_device(cpu);
888 		if (!cpu_dev) {
889 			pr_err("%s: failed to get cpu%d device\n", __func__,
890 			       cpu);
891 			ret = -ENODEV;
892 			goto remove_table;
893 		}
894 
895 		ret = dev_pm_opp_of_add_table(cpu_dev);
896 		if (ret) {
897 			/*
898 			 * OPP may get registered dynamically, don't print error
899 			 * message here.
900 			 */
901 			pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
902 				 __func__, cpu, ret);
903 
904 			goto remove_table;
905 		}
906 	}
907 
908 	return 0;
909 
910 remove_table:
911 	/* Free all other OPPs */
912 	_dev_pm_opp_cpumask_remove_table(cpumask, cpu);
913 
914 	return ret;
915 }
916 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
917 
918 /*
919  * Works only for OPP v2 bindings.
920  *
921  * Returns -ENOENT if operating-points-v2 bindings aren't supported.
922  */
923 /**
924  * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
925  *				      @cpu_dev using operating-points-v2
926  *				      bindings.
927  *
928  * @cpu_dev:	CPU device for which we do this operation
929  * @cpumask:	cpumask to update with information of sharing CPUs
930  *
931  * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
932  *
933  * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
934  */
935 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
936 				   struct cpumask *cpumask)
937 {
938 	struct device_node *np, *tmp_np, *cpu_np;
939 	int cpu, ret = 0;
940 
941 	/* Get OPP descriptor node */
942 	np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
943 	if (!np) {
944 		dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
945 		return -ENOENT;
946 	}
947 
948 	cpumask_set_cpu(cpu_dev->id, cpumask);
949 
950 	/* OPPs are shared ? */
951 	if (!of_property_read_bool(np, "opp-shared"))
952 		goto put_cpu_node;
953 
954 	for_each_possible_cpu(cpu) {
955 		if (cpu == cpu_dev->id)
956 			continue;
957 
958 		cpu_np = of_cpu_device_node_get(cpu);
959 		if (!cpu_np) {
960 			dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
961 				__func__, cpu);
962 			ret = -ENOENT;
963 			goto put_cpu_node;
964 		}
965 
966 		/* Get OPP descriptor node */
967 		tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
968 		of_node_put(cpu_np);
969 		if (!tmp_np) {
970 			pr_err("%pOF: Couldn't find opp node\n", cpu_np);
971 			ret = -ENOENT;
972 			goto put_cpu_node;
973 		}
974 
975 		/* CPUs are sharing opp node */
976 		if (np == tmp_np)
977 			cpumask_set_cpu(cpu, cpumask);
978 
979 		of_node_put(tmp_np);
980 	}
981 
982 put_cpu_node:
983 	of_node_put(np);
984 	return ret;
985 }
986 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
987 
988 /**
989  * of_get_required_opp_performance_state() - Search for required OPP and return its performance state.
990  * @np: Node that contains the "required-opps" property.
991  * @index: Index of the phandle to parse.
992  *
993  * Returns the performance state of the OPP pointed out by the "required-opps"
994  * property at @index in @np.
995  *
996  * Return: Zero or positive performance state on success, otherwise negative
997  * value on errors.
998  */
999 int of_get_required_opp_performance_state(struct device_node *np, int index)
1000 {
1001 	struct dev_pm_opp *opp;
1002 	struct device_node *required_np;
1003 	struct opp_table *opp_table;
1004 	int pstate = -EINVAL;
1005 
1006 	required_np = of_parse_required_opp(np, index);
1007 	if (!required_np)
1008 		return -EINVAL;
1009 
1010 	opp_table = _find_table_of_opp_np(required_np);
1011 	if (IS_ERR(opp_table)) {
1012 		pr_err("%s: Failed to find required OPP table %pOF: %ld\n",
1013 		       __func__, np, PTR_ERR(opp_table));
1014 		goto put_required_np;
1015 	}
1016 
1017 	opp = _find_opp_of_np(opp_table, required_np);
1018 	if (opp) {
1019 		pstate = opp->pstate;
1020 		dev_pm_opp_put(opp);
1021 	}
1022 
1023 	dev_pm_opp_put_opp_table(opp_table);
1024 
1025 put_required_np:
1026 	of_node_put(required_np);
1027 
1028 	return pstate;
1029 }
1030 EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
1031 
1032 /**
1033  * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
1034  * @opp:	opp for which DT node has to be returned for
1035  *
1036  * Return: DT node corresponding to the opp, else 0 on success.
1037  *
1038  * The caller needs to put the node with of_node_put() after using it.
1039  */
1040 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
1041 {
1042 	if (IS_ERR_OR_NULL(opp)) {
1043 		pr_err("%s: Invalid parameters\n", __func__);
1044 		return NULL;
1045 	}
1046 
1047 	return of_node_get(opp->np);
1048 }
1049 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
1050