Linux Audio

Check our new training course

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