1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic OPP OF helpers
5 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
25 * Returns opp descriptor node for a device node, caller must
28 static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
31 /* "operating-points-v2" can be an array for power domain providers */
32 return of_parse_phandle(np, "operating-points-v2", index);
35 /* Returns opp descriptor node for a device, caller must do of_node_put() */
36 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
38 return _opp_of_get_opp_desc_node(dev->of_node, 0);
40 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
42 struct opp_table *_managed_opp(struct device *dev, int index)
44 struct opp_table *opp_table, *managed_table = NULL;
45 struct device_node *np;
47 np = _opp_of_get_opp_desc_node(dev->of_node, index);
51 list_for_each_entry(opp_table, &opp_tables, node) {
52 if (opp_table->np == np) {
54 * Multiple devices can point to the same OPP table and
55 * so will have same node-pointer, np.
57 * But the OPPs will be considered as shared only if the
58 * OPP table contains a "opp-shared" property.
60 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
61 _get_opp_table_kref(opp_table);
62 managed_table = opp_table;
74 /* The caller must call dev_pm_opp_put() after the OPP is used */
75 static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table,
76 struct device_node *opp_np)
78 struct dev_pm_opp *opp;
80 mutex_lock(&opp_table->lock);
82 list_for_each_entry(opp, &opp_table->opp_list, node) {
83 if (opp->np == opp_np) {
85 mutex_unlock(&opp_table->lock);
90 mutex_unlock(&opp_table->lock);
95 static struct device_node *of_parse_required_opp(struct device_node *np,
98 return of_parse_phandle(np, "required-opps", index);
101 /* The caller must call dev_pm_opp_put_opp_table() after the table is used */
102 static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np)
104 struct opp_table *opp_table;
105 struct device_node *opp_table_np;
107 lockdep_assert_held(&opp_table_lock);
109 opp_table_np = of_get_parent(opp_np);
113 /* It is safe to put the node now as all we need now is its address */
114 of_node_put(opp_table_np);
116 list_for_each_entry(opp_table, &opp_tables, node) {
117 if (opp_table_np == opp_table->np) {
118 _get_opp_table_kref(opp_table);
124 return ERR_PTR(-ENODEV);
127 /* Free resources previously acquired by _opp_table_alloc_required_tables() */
128 static void _opp_table_free_required_tables(struct opp_table *opp_table)
130 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
133 if (!required_opp_tables)
136 for (i = 0; i < opp_table->required_opp_count; i++) {
137 if (IS_ERR_OR_NULL(required_opp_tables[i]))
140 dev_pm_opp_put_opp_table(required_opp_tables[i]);
143 kfree(required_opp_tables);
145 opp_table->required_opp_count = 0;
146 opp_table->required_opp_tables = NULL;
150 * Populate all devices and opp tables which are part of "required-opps" list.
151 * Checking only the first OPP node should be enough.
153 static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
155 struct device_node *opp_np)
157 struct opp_table **required_opp_tables;
158 struct device_node *required_np, *np;
161 /* Traversing the first OPP node is all we need */
162 np = of_get_next_available_child(opp_np, NULL);
164 dev_err(dev, "Empty OPP table\n");
168 count = of_count_phandle_with_args(np, "required-opps", NULL);
172 required_opp_tables = kcalloc(count, sizeof(*required_opp_tables),
174 if (!required_opp_tables)
177 opp_table->required_opp_tables = required_opp_tables;
178 opp_table->required_opp_count = count;
180 for (i = 0; i < count; i++) {
181 required_np = of_parse_required_opp(np, i);
183 goto free_required_tables;
185 required_opp_tables[i] = _find_table_of_opp_np(required_np);
186 of_node_put(required_np);
188 if (IS_ERR(required_opp_tables[i]))
189 goto free_required_tables;
192 * We only support genpd's OPPs in the "required-opps" for now,
193 * as we don't know how much about other cases. Error out if the
194 * required OPP doesn't belong to a genpd.
196 if (!required_opp_tables[i]->is_genpd) {
197 dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n",
199 goto free_required_tables;
205 free_required_tables:
206 _opp_table_free_required_tables(opp_table);
211 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
214 struct device_node *np, *opp_np;
218 * Only required for backward compatibility with v1 bindings, but isn't
219 * harmful for other cases. And so we do it unconditionally.
221 np = of_node_get(dev->of_node);
225 if (!of_property_read_u32(np, "clock-latency", &val))
226 opp_table->clock_latency_ns_max = val;
227 of_property_read_u32(np, "voltage-tolerance",
228 &opp_table->voltage_tolerance_v1);
230 if (of_find_property(np, "#power-domain-cells", NULL))
231 opp_table->is_genpd = true;
233 /* Get OPP table node */
234 opp_np = _opp_of_get_opp_desc_node(np, index);
240 if (of_property_read_bool(opp_np, "opp-shared"))
241 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
243 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
245 opp_table->np = opp_np;
247 _opp_table_alloc_required_tables(opp_table, dev, opp_np);
251 void _of_clear_opp_table(struct opp_table *opp_table)
253 _opp_table_free_required_tables(opp_table);
257 * Release all resources previously acquired with a call to
258 * _of_opp_alloc_required_opps().
260 void _of_opp_free_required_opps(struct opp_table *opp_table,
261 struct dev_pm_opp *opp)
263 struct dev_pm_opp **required_opps = opp->required_opps;
269 for (i = 0; i < opp_table->required_opp_count; i++) {
270 if (!required_opps[i])
273 /* Put the reference back */
274 dev_pm_opp_put(required_opps[i]);
277 kfree(required_opps);
278 opp->required_opps = NULL;
281 /* Populate all required OPPs which are part of "required-opps" list */
282 static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
283 struct dev_pm_opp *opp)
285 struct dev_pm_opp **required_opps;
286 struct opp_table *required_table;
287 struct device_node *np;
288 int i, ret, count = opp_table->required_opp_count;
293 required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL);
297 opp->required_opps = required_opps;
299 for (i = 0; i < count; i++) {
300 required_table = opp_table->required_opp_tables[i];
302 np = of_parse_required_opp(opp->np, i);
305 goto free_required_opps;
308 required_opps[i] = _find_opp_of_np(required_table, np);
311 if (!required_opps[i]) {
312 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
313 __func__, opp->np, i);
315 goto free_required_opps;
322 _of_opp_free_required_opps(opp_table, opp);
327 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
328 struct device_node *np)
330 unsigned int count = opp_table->supported_hw_count;
334 if (!opp_table->supported_hw) {
336 * In the case that no supported_hw has been set by the
337 * platform but there is an opp-supported-hw value set for
338 * an OPP then the OPP should not be enabled as there is
339 * no way to see if the hardware supports it.
341 if (of_find_property(np, "opp-supported-hw", NULL))
348 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
351 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
352 __func__, count, ret);
356 /* Both of these are bitwise masks of the versions */
357 if (!(version & opp_table->supported_hw[count]))
364 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
365 struct opp_table *opp_table)
367 u32 *microvolt, *microamp = NULL;
368 int supplies = opp_table->regulator_count, vcount, icount, ret, i, j;
369 struct property *prop = NULL;
372 /* Search for "opp-microvolt-<name>" */
373 if (opp_table->prop_name) {
374 snprintf(name, sizeof(name), "opp-microvolt-%s",
375 opp_table->prop_name);
376 prop = of_find_property(opp->np, name, NULL);
380 /* Search for "opp-microvolt" */
381 sprintf(name, "opp-microvolt");
382 prop = of_find_property(opp->np, name, NULL);
384 /* Missing property isn't a problem, but an invalid entry is */
386 if (unlikely(supplies == -1)) {
387 /* Initialize regulator_count */
388 opp_table->regulator_count = 0;
395 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
401 if (unlikely(supplies == -1)) {
402 /* Initialize regulator_count */
403 supplies = opp_table->regulator_count = 1;
404 } else if (unlikely(!supplies)) {
405 dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__);
409 vcount = of_property_count_u32_elems(opp->np, name);
411 dev_err(dev, "%s: Invalid %s property (%d)\n",
412 __func__, name, vcount);
416 /* There can be one or three elements per supply */
417 if (vcount != supplies && vcount != supplies * 3) {
418 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
419 __func__, name, vcount, supplies);
423 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
427 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
429 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
434 /* Search for "opp-microamp-<name>" */
436 if (opp_table->prop_name) {
437 snprintf(name, sizeof(name), "opp-microamp-%s",
438 opp_table->prop_name);
439 prop = of_find_property(opp->np, name, NULL);
443 /* Search for "opp-microamp" */
444 sprintf(name, "opp-microamp");
445 prop = of_find_property(opp->np, name, NULL);
449 icount = of_property_count_u32_elems(opp->np, name);
451 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
457 if (icount != supplies) {
458 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
459 __func__, name, icount, supplies);
464 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
470 ret = of_property_read_u32_array(opp->np, name, microamp,
473 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
480 for (i = 0, j = 0; i < supplies; i++) {
481 opp->supplies[i].u_volt = microvolt[j++];
483 if (vcount == supplies) {
484 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
485 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
487 opp->supplies[i].u_volt_min = microvolt[j++];
488 opp->supplies[i].u_volt_max = microvolt[j++];
492 opp->supplies[i].u_amp = microamp[i];
504 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
506 * @dev: device pointer used to lookup OPP table.
508 * Free OPPs created using static entries present in DT.
510 void dev_pm_opp_of_remove_table(struct device *dev)
512 _dev_pm_opp_find_and_remove_table(dev);
514 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
517 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
518 * @opp_table: OPP table
519 * @dev: device for which we do this operation
522 * This function adds an opp definition to the opp table and returns status. The
523 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
524 * removed by dev_pm_opp_remove.
530 * Duplicate OPPs (both freq and volt are same) and opp->available
531 * OR if the OPP is not supported by hardware.
533 * Freq are same and volt are different OR
534 * Duplicate OPPs (both freq and volt are same) and !opp->available
536 * Memory allocation failure
538 * Failed parsing the OPP node
540 static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
541 struct device *dev, struct device_node *np)
543 struct dev_pm_opp *new_opp;
547 bool rate_not_available = false;
549 new_opp = _opp_allocate(opp_table);
551 return ERR_PTR(-ENOMEM);
553 ret = of_property_read_u64(np, "opp-hz", &rate);
555 /* "opp-hz" is optional for devices like power domains. */
556 if (!opp_table->is_genpd) {
557 dev_err(dev, "%s: opp-hz not found\n", __func__);
561 rate_not_available = true;
564 * Rate is defined as an unsigned long in clk API, and so
565 * casting explicitly to its type. Must be fixed once rate is 64
566 * bit guaranteed in clk API.
568 new_opp->rate = (unsigned long)rate;
571 of_property_read_u32(np, "opp-level", &new_opp->level);
573 /* Check if the OPP supports hardware's hierarchy of versions or not */
574 if (!_opp_is_supported(dev, opp_table, np)) {
575 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
579 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
582 new_opp->dynamic = false;
583 new_opp->available = true;
585 ret = _of_opp_alloc_required_opps(opp_table, new_opp);
589 if (!of_property_read_u32(np, "clock-latency-ns", &val))
590 new_opp->clock_latency_ns = val;
592 ret = opp_parse_supplies(new_opp, dev, opp_table);
594 goto free_required_opps;
596 if (opp_table->is_genpd)
597 new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp);
599 ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
601 /* Don't return error for duplicate OPPs */
604 goto free_required_opps;
607 /* OPP to select on device suspend */
608 if (of_property_read_bool(np, "opp-suspend")) {
609 if (opp_table->suspend_opp) {
610 /* Pick the OPP with higher rate as suspend OPP */
611 if (new_opp->rate > opp_table->suspend_opp->rate) {
612 opp_table->suspend_opp->suspend = false;
613 new_opp->suspend = true;
614 opp_table->suspend_opp = new_opp;
617 new_opp->suspend = true;
618 opp_table->suspend_opp = new_opp;
622 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
623 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
625 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
626 __func__, new_opp->turbo, new_opp->rate,
627 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
628 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
631 * Notify the changes in the availability of the operable
632 * frequency/voltage list.
634 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
638 _of_opp_free_required_opps(opp_table, new_opp);
642 return ret ? ERR_PTR(ret) : NULL;
645 /* Initializes OPP tables based on new bindings */
646 static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
648 struct device_node *np;
649 int ret, count = 0, pstate_count = 0;
650 struct dev_pm_opp *opp;
652 /* OPP table is already initialized for the device */
653 mutex_lock(&opp_table->lock);
654 if (opp_table->parsed_static_opps) {
655 opp_table->parsed_static_opps++;
656 mutex_unlock(&opp_table->lock);
660 opp_table->parsed_static_opps = 1;
661 mutex_unlock(&opp_table->lock);
663 /* We have opp-table node now, iterate over it and add OPPs */
664 for_each_available_child_of_node(opp_table->np, np) {
665 opp = _opp_add_static_v2(opp_table, dev, np);
668 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
671 goto remove_static_opp;
677 /* There should be one or more OPPs defined */
679 dev_err(dev, "%s: no supported OPPs", __func__);
681 goto remove_static_opp;
684 list_for_each_entry(opp, &opp_table->opp_list, node)
685 pstate_count += !!opp->pstate;
687 /* Either all or none of the nodes shall have performance state set */
688 if (pstate_count && pstate_count != count) {
689 dev_err(dev, "Not all nodes have performance state set (%d: %d)\n",
690 count, pstate_count);
692 goto remove_static_opp;
696 opp_table->genpd_performance_state = true;
701 _opp_remove_all_static(opp_table);
706 /* Initializes OPP tables based on old-deprecated bindings */
707 static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
709 const struct property *prop;
713 prop = of_find_property(dev->of_node, "operating-points", NULL);
720 * Each OPP is a set of tuples consisting of frequency and
721 * voltage like <freq-kHz vol-uV>.
723 nr = prop->length / sizeof(u32);
725 dev_err(dev, "%s: Invalid OPP table\n", __func__);
729 mutex_lock(&opp_table->lock);
730 opp_table->parsed_static_opps = 1;
731 mutex_unlock(&opp_table->lock);
735 unsigned long freq = be32_to_cpup(val++) * 1000;
736 unsigned long volt = be32_to_cpup(val++);
738 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
740 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
741 __func__, freq, ret);
742 _opp_remove_all_static(opp_table);
752 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
753 * @dev: device pointer used to lookup OPP table.
755 * Register the initial OPP table with the OPP library for given device.
759 * Duplicate OPPs (both freq and volt are same) and opp->available
760 * -EEXIST Freq are same and volt are different OR
761 * Duplicate OPPs (both freq and volt are same) and !opp->available
762 * -ENOMEM Memory allocation failure
763 * -ENODEV when 'operating-points' property is not found or is invalid data
765 * -ENODATA when empty 'operating-points' property is found
766 * -EINVAL when invalid entries are found in opp-v2 table
768 int dev_pm_opp_of_add_table(struct device *dev)
770 struct opp_table *opp_table;
773 opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0);
778 * OPPs have two version of bindings now. Also try the old (v1)
779 * bindings for backward compatibility with older dtbs.
782 ret = _of_add_opp_table_v2(dev, opp_table);
784 ret = _of_add_opp_table_v1(dev, opp_table);
787 dev_pm_opp_put_opp_table(opp_table);
791 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
794 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
795 * @dev: device pointer used to lookup OPP table.
796 * @index: Index number.
798 * Register the initial OPP table with the OPP library for given device only
799 * using the "operating-points-v2" property.
803 * Duplicate OPPs (both freq and volt are same) and opp->available
804 * -EEXIST Freq are same and volt are different OR
805 * Duplicate OPPs (both freq and volt are same) and !opp->available
806 * -ENOMEM Memory allocation failure
807 * -ENODEV when 'operating-points' property is not found or is invalid data
809 * -ENODATA when empty 'operating-points' property is found
810 * -EINVAL when invalid entries are found in opp-v2 table
812 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
814 struct opp_table *opp_table;
819 * If only one phandle is present, then the same OPP table
820 * applies for all index requests.
822 count = of_count_phandle_with_args(dev->of_node,
823 "operating-points-v2", NULL);
828 opp_table = dev_pm_opp_get_opp_table_indexed(dev, index);
832 ret = _of_add_opp_table_v2(dev, opp_table);
834 dev_pm_opp_put_opp_table(opp_table);
838 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
840 /* CPU device specific helpers */
843 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
844 * @cpumask: cpumask for which OPP table needs to be removed
846 * This removes the OPP tables for CPUs present in the @cpumask.
847 * This should be used only to remove static entries created from DT.
849 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
851 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
853 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
856 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
857 * @cpumask: cpumask for which OPP table needs to be added.
859 * This adds the OPP tables for CPUs present in the @cpumask.
861 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
863 struct device *cpu_dev;
866 if (WARN_ON(cpumask_empty(cpumask)))
869 for_each_cpu(cpu, cpumask) {
870 cpu_dev = get_cpu_device(cpu);
872 pr_err("%s: failed to get cpu%d device\n", __func__,
878 ret = dev_pm_opp_of_add_table(cpu_dev);
881 * OPP may get registered dynamically, don't print error
884 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
894 /* Free all other OPPs */
895 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
899 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
902 * Works only for OPP v2 bindings.
904 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
907 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
908 * @cpu_dev using operating-points-v2
911 * @cpu_dev: CPU device for which we do this operation
912 * @cpumask: cpumask to update with information of sharing CPUs
914 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
916 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
918 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
919 struct cpumask *cpumask)
921 struct device_node *np, *tmp_np, *cpu_np;
924 /* Get OPP descriptor node */
925 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
927 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
931 cpumask_set_cpu(cpu_dev->id, cpumask);
933 /* OPPs are shared ? */
934 if (!of_property_read_bool(np, "opp-shared"))
937 for_each_possible_cpu(cpu) {
938 if (cpu == cpu_dev->id)
941 cpu_np = of_cpu_device_node_get(cpu);
943 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
949 /* Get OPP descriptor node */
950 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
953 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
958 /* CPUs are sharing opp node */
960 cpumask_set_cpu(cpu, cpumask);
969 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
972 * of_get_required_opp_performance_state() - Search for required OPP and return its performance state.
973 * @np: Node that contains the "required-opps" property.
974 * @index: Index of the phandle to parse.
976 * Returns the performance state of the OPP pointed out by the "required-opps"
977 * property at @index in @np.
979 * Return: Zero or positive performance state on success, otherwise negative
982 int of_get_required_opp_performance_state(struct device_node *np, int index)
984 struct dev_pm_opp *opp;
985 struct device_node *required_np;
986 struct opp_table *opp_table;
987 int pstate = -EINVAL;
989 required_np = of_parse_required_opp(np, index);
993 opp_table = _find_table_of_opp_np(required_np);
994 if (IS_ERR(opp_table)) {
995 pr_err("%s: Failed to find required OPP table %pOF: %ld\n",
996 __func__, np, PTR_ERR(opp_table));
997 goto put_required_np;
1000 opp = _find_opp_of_np(opp_table, required_np);
1002 pstate = opp->pstate;
1003 dev_pm_opp_put(opp);
1006 dev_pm_opp_put_opp_table(opp_table);
1009 of_node_put(required_np);
1013 EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
1016 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
1017 * @opp: opp for which DT node has to be returned for
1019 * Return: DT node corresponding to the opp, else 0 on success.
1021 * The caller needs to put the node with of_node_put() after using it.
1023 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
1025 if (IS_ERR_OR_NULL(opp)) {
1026 pr_err("%s: Invalid parameters\n", __func__);
1030 return of_node_get(opp->np);
1032 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
1035 * Callback function provided to the Energy Model framework upon registration.
1036 * This computes the power estimated by @CPU at @kHz if it is the frequency
1037 * of an existing OPP, or at the frequency of the first OPP above @kHz otherwise
1038 * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
1039 * frequency and @mW to the associated power. The power is estimated as
1040 * P = C * V^2 * f with C being the CPU's capacitance and V and f respectively
1041 * the voltage and frequency of the OPP.
1043 * Returns -ENODEV if the CPU device cannot be found, -EINVAL if the power
1044 * calculation failed because of missing parameters, 0 otherwise.
1046 static int __maybe_unused _get_cpu_power(unsigned long *mW, unsigned long *kHz,
1049 struct device *cpu_dev;
1050 struct dev_pm_opp *opp;
1051 struct device_node *np;
1052 unsigned long mV, Hz;
1057 cpu_dev = get_cpu_device(cpu);
1061 np = of_node_get(cpu_dev->of_node);
1065 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1071 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &Hz);
1075 mV = dev_pm_opp_get_voltage(opp) / 1000;
1076 dev_pm_opp_put(opp);
1080 tmp = (u64)cap * mV * mV * (Hz / 1000000);
1081 do_div(tmp, 1000000000);
1083 *mW = (unsigned long)tmp;
1090 * dev_pm_opp_of_register_em() - Attempt to register an Energy Model
1091 * @cpus : CPUs for which an Energy Model has to be registered
1093 * This checks whether the "dynamic-power-coefficient" devicetree property has
1094 * been specified, and tries to register an Energy Model with it if it has.
1096 void dev_pm_opp_of_register_em(struct cpumask *cpus)
1098 struct em_data_callback em_cb = EM_DATA_CB(_get_cpu_power);
1099 int ret, nr_opp, cpu = cpumask_first(cpus);
1100 struct device *cpu_dev;
1101 struct device_node *np;
1104 cpu_dev = get_cpu_device(cpu);
1108 nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
1112 np = of_node_get(cpu_dev->of_node);
1117 * Register an EM only if the 'dynamic-power-coefficient' property is
1118 * set in devicetree. It is assumed the voltage values are known if that
1119 * property is set since it is useless otherwise. If voltages are not
1120 * known, just let the EM registration fail with an error to alert the
1121 * user about the inconsistent configuration.
1123 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1128 em_register_perf_domain(cpus, nr_opp, &em_cb);
1130 EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);