2 * Generic OPP OF helpers
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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 #include <linux/energy_model.h>
28 * Returns opp descriptor node for a device node, caller must
31 static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
34 /* "operating-points-v2" can be an array for power domain providers */
35 return of_parse_phandle(np, "operating-points-v2", index);
38 /* Returns opp descriptor node for a device, caller must do of_node_put() */
39 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
41 return _opp_of_get_opp_desc_node(dev->of_node, 0);
43 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
45 struct opp_table *_managed_opp(struct device *dev, int index)
47 struct opp_table *opp_table, *managed_table = NULL;
48 struct device_node *np;
50 np = _opp_of_get_opp_desc_node(dev->of_node, index);
54 list_for_each_entry(opp_table, &opp_tables, node) {
55 if (opp_table->np == np) {
57 * Multiple devices can point to the same OPP table and
58 * so will have same node-pointer, np.
60 * But the OPPs will be considered as shared only if the
61 * OPP table contains a "opp-shared" property.
63 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
64 _get_opp_table_kref(opp_table);
65 managed_table = opp_table;
77 /* The caller must call dev_pm_opp_put() after the OPP is used */
78 static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table,
79 struct device_node *opp_np)
81 struct dev_pm_opp *opp;
83 lockdep_assert_held(&opp_table_lock);
85 mutex_lock(&opp_table->lock);
87 list_for_each_entry(opp, &opp_table->opp_list, node) {
88 if (opp->np == opp_np) {
90 mutex_unlock(&opp_table->lock);
95 mutex_unlock(&opp_table->lock);
100 static struct device_node *of_parse_required_opp(struct device_node *np,
103 struct device_node *required_np;
105 required_np = of_parse_phandle(np, "required-opps", index);
106 if (unlikely(!required_np)) {
107 pr_err("%s: Unable to parse required-opps: %pOF, index: %d\n",
108 __func__, np, index);
114 /* The caller must call dev_pm_opp_put_opp_table() after the table is used */
115 static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np)
117 struct opp_table *opp_table;
118 struct device_node *opp_table_np;
120 lockdep_assert_held(&opp_table_lock);
122 opp_table_np = of_get_parent(opp_np);
126 /* It is safe to put the node now as all we need now is its address */
127 of_node_put(opp_table_np);
129 list_for_each_entry(opp_table, &opp_tables, node) {
130 if (opp_table_np == opp_table->np) {
131 _get_opp_table_kref(opp_table);
137 return ERR_PTR(-ENODEV);
140 /* Free resources previously acquired by _opp_table_alloc_required_tables() */
141 static void _opp_table_free_required_tables(struct opp_table *opp_table)
143 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
144 struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
147 if (!required_opp_tables)
150 for (i = 0; i < opp_table->required_opp_count; i++) {
151 if (IS_ERR_OR_NULL(required_opp_tables[i]))
154 dev_pm_opp_put_opp_table(required_opp_tables[i]);
157 kfree(required_opp_tables);
158 kfree(genpd_virt_devs);
160 opp_table->required_opp_count = 0;
161 opp_table->genpd_virt_devs = NULL;
162 opp_table->required_opp_tables = NULL;
166 * Populate all devices and opp tables which are part of "required-opps" list.
167 * Checking only the first OPP node should be enough.
169 static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
171 struct device_node *opp_np)
173 struct opp_table **required_opp_tables;
174 struct device **genpd_virt_devs = NULL;
175 struct device_node *required_np, *np;
178 /* Traversing the first OPP node is all we need */
179 np = of_get_next_available_child(opp_np, NULL);
181 dev_err(dev, "Empty OPP table\n");
185 count = of_count_phandle_with_args(np, "required-opps", NULL);
190 genpd_virt_devs = kcalloc(count, sizeof(*genpd_virt_devs),
192 if (!genpd_virt_devs)
196 required_opp_tables = kcalloc(count, sizeof(*required_opp_tables),
198 if (!required_opp_tables) {
199 kfree(genpd_virt_devs);
203 opp_table->genpd_virt_devs = genpd_virt_devs;
204 opp_table->required_opp_tables = required_opp_tables;
205 opp_table->required_opp_count = count;
207 for (i = 0; i < count; i++) {
208 required_np = of_parse_required_opp(np, i);
210 goto free_required_tables;
212 required_opp_tables[i] = _find_table_of_opp_np(required_np);
213 of_node_put(required_np);
215 if (IS_ERR(required_opp_tables[i]))
216 goto free_required_tables;
219 * We only support genpd's OPPs in the "required-opps" for now,
220 * as we don't know how much about other cases. Error out if the
221 * required OPP doesn't belong to a genpd.
223 if (!required_opp_tables[i]->is_genpd) {
224 dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n",
226 goto free_required_tables;
232 free_required_tables:
233 _opp_table_free_required_tables(opp_table);
238 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
241 struct device_node *np, *opp_np;
245 * Only required for backward compatibility with v1 bindings, but isn't
246 * harmful for other cases. And so we do it unconditionally.
248 np = of_node_get(dev->of_node);
252 if (!of_property_read_u32(np, "clock-latency", &val))
253 opp_table->clock_latency_ns_max = val;
254 of_property_read_u32(np, "voltage-tolerance",
255 &opp_table->voltage_tolerance_v1);
257 if (of_find_property(np, "#power-domain-cells", NULL))
258 opp_table->is_genpd = true;
260 /* Get OPP table node */
261 opp_np = _opp_of_get_opp_desc_node(np, index);
267 if (of_property_read_bool(opp_np, "opp-shared"))
268 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
270 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
272 opp_table->np = opp_np;
274 _opp_table_alloc_required_tables(opp_table, dev, opp_np);
278 void _of_clear_opp_table(struct opp_table *opp_table)
280 _opp_table_free_required_tables(opp_table);
284 * Release all resources previously acquired with a call to
285 * _of_opp_alloc_required_opps().
287 void _of_opp_free_required_opps(struct opp_table *opp_table,
288 struct dev_pm_opp *opp)
290 struct dev_pm_opp **required_opps = opp->required_opps;
296 for (i = 0; i < opp_table->required_opp_count; i++) {
297 if (!required_opps[i])
300 /* Put the reference back */
301 dev_pm_opp_put(required_opps[i]);
304 kfree(required_opps);
305 opp->required_opps = NULL;
308 /* Populate all required OPPs which are part of "required-opps" list */
309 static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
310 struct dev_pm_opp *opp)
312 struct dev_pm_opp **required_opps;
313 struct opp_table *required_table;
314 struct device_node *np;
315 int i, ret, count = opp_table->required_opp_count;
320 required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL);
324 opp->required_opps = required_opps;
326 for (i = 0; i < count; i++) {
327 required_table = opp_table->required_opp_tables[i];
329 np = of_parse_required_opp(opp->np, i);
332 goto free_required_opps;
335 required_opps[i] = _find_opp_of_np(required_table, np);
338 if (!required_opps[i]) {
339 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
340 __func__, opp->np, i);
342 goto free_required_opps;
349 _of_opp_free_required_opps(opp_table, opp);
354 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
355 struct device_node *np)
357 unsigned int count = opp_table->supported_hw_count;
361 if (!opp_table->supported_hw) {
363 * In the case that no supported_hw has been set by the
364 * platform but there is an opp-supported-hw value set for
365 * an OPP then the OPP should not be enabled as there is
366 * no way to see if the hardware supports it.
368 if (of_find_property(np, "opp-supported-hw", NULL))
375 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
378 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
379 __func__, count, ret);
383 /* Both of these are bitwise masks of the versions */
384 if (!(version & opp_table->supported_hw[count]))
391 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
392 struct opp_table *opp_table)
394 u32 *microvolt, *microamp = NULL;
395 int supplies = opp_table->regulator_count, vcount, icount, ret, i, j;
396 struct property *prop = NULL;
399 /* Search for "opp-microvolt-<name>" */
400 if (opp_table->prop_name) {
401 snprintf(name, sizeof(name), "opp-microvolt-%s",
402 opp_table->prop_name);
403 prop = of_find_property(opp->np, name, NULL);
407 /* Search for "opp-microvolt" */
408 sprintf(name, "opp-microvolt");
409 prop = of_find_property(opp->np, name, NULL);
411 /* Missing property isn't a problem, but an invalid entry is */
413 if (unlikely(supplies == -1)) {
414 /* Initialize regulator_count */
415 opp_table->regulator_count = 0;
422 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
428 if (unlikely(supplies == -1)) {
429 /* Initialize regulator_count */
430 supplies = opp_table->regulator_count = 1;
431 } else if (unlikely(!supplies)) {
432 dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__);
436 vcount = of_property_count_u32_elems(opp->np, name);
438 dev_err(dev, "%s: Invalid %s property (%d)\n",
439 __func__, name, vcount);
443 /* There can be one or three elements per supply */
444 if (vcount != supplies && vcount != supplies * 3) {
445 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
446 __func__, name, vcount, supplies);
450 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
454 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
456 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
461 /* Search for "opp-microamp-<name>" */
463 if (opp_table->prop_name) {
464 snprintf(name, sizeof(name), "opp-microamp-%s",
465 opp_table->prop_name);
466 prop = of_find_property(opp->np, name, NULL);
470 /* Search for "opp-microamp" */
471 sprintf(name, "opp-microamp");
472 prop = of_find_property(opp->np, name, NULL);
476 icount = of_property_count_u32_elems(opp->np, name);
478 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
484 if (icount != supplies) {
485 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
486 __func__, name, icount, supplies);
491 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
497 ret = of_property_read_u32_array(opp->np, name, microamp,
500 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
507 for (i = 0, j = 0; i < supplies; i++) {
508 opp->supplies[i].u_volt = microvolt[j++];
510 if (vcount == supplies) {
511 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
512 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
514 opp->supplies[i].u_volt_min = microvolt[j++];
515 opp->supplies[i].u_volt_max = microvolt[j++];
519 opp->supplies[i].u_amp = microamp[i];
531 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
533 * @dev: device pointer used to lookup OPP table.
535 * Free OPPs created using static entries present in DT.
537 void dev_pm_opp_of_remove_table(struct device *dev)
539 _dev_pm_opp_find_and_remove_table(dev);
541 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
544 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
545 * @opp_table: OPP table
546 * @dev: device for which we do this operation
549 * This function adds an opp definition to the opp table and returns status. The
550 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
551 * removed by dev_pm_opp_remove.
557 * Duplicate OPPs (both freq and volt are same) and opp->available
558 * OR if the OPP is not supported by hardware.
560 * Freq are same and volt are different OR
561 * Duplicate OPPs (both freq and volt are same) and !opp->available
563 * Memory allocation failure
565 * Failed parsing the OPP node
567 static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
568 struct device *dev, struct device_node *np)
570 struct dev_pm_opp *new_opp;
574 bool rate_not_available = false;
576 new_opp = _opp_allocate(opp_table);
578 return ERR_PTR(-ENOMEM);
580 ret = of_property_read_u64(np, "opp-hz", &rate);
582 /* "opp-hz" is optional for devices like power domains. */
583 if (!opp_table->is_genpd) {
584 dev_err(dev, "%s: opp-hz not found\n", __func__);
588 rate_not_available = true;
591 * Rate is defined as an unsigned long in clk API, and so
592 * casting explicitly to its type. Must be fixed once rate is 64
593 * bit guaranteed in clk API.
595 new_opp->rate = (unsigned long)rate;
598 of_property_read_u32(np, "opp-level", &new_opp->level);
600 /* Check if the OPP supports hardware's hierarchy of versions or not */
601 if (!_opp_is_supported(dev, opp_table, np)) {
602 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
606 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
609 new_opp->dynamic = false;
610 new_opp->available = true;
612 ret = _of_opp_alloc_required_opps(opp_table, new_opp);
616 if (!of_property_read_u32(np, "clock-latency-ns", &val))
617 new_opp->clock_latency_ns = val;
619 ret = opp_parse_supplies(new_opp, dev, opp_table);
621 goto free_required_opps;
623 if (opp_table->is_genpd)
624 new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp);
626 ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
628 /* Don't return error for duplicate OPPs */
631 goto free_required_opps;
634 /* OPP to select on device suspend */
635 if (of_property_read_bool(np, "opp-suspend")) {
636 if (opp_table->suspend_opp) {
637 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
638 __func__, opp_table->suspend_opp->rate,
641 new_opp->suspend = true;
642 opp_table->suspend_opp = new_opp;
646 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
647 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
649 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
650 __func__, new_opp->turbo, new_opp->rate,
651 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
652 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
655 * Notify the changes in the availability of the operable
656 * frequency/voltage list.
658 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
662 _of_opp_free_required_opps(opp_table, new_opp);
669 /* Initializes OPP tables based on new bindings */
670 static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
672 struct device_node *np;
673 int ret, count = 0, pstate_count = 0;
674 struct dev_pm_opp *opp;
676 /* OPP table is already initialized for the device */
677 if (opp_table->parsed_static_opps) {
678 kref_get(&opp_table->list_kref);
682 kref_init(&opp_table->list_kref);
684 /* We have opp-table node now, iterate over it and add OPPs */
685 for_each_available_child_of_node(opp_table->np, np) {
686 opp = _opp_add_static_v2(opp_table, dev, np);
689 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
698 /* There should be one of more OPP defined */
699 if (WARN_ON(!count)) {
704 list_for_each_entry(opp, &opp_table->opp_list, node)
705 pstate_count += !!opp->pstate;
707 /* Either all or none of the nodes shall have performance state set */
708 if (pstate_count && pstate_count != count) {
709 dev_err(dev, "Not all nodes have performance state set (%d: %d)\n",
710 count, pstate_count);
716 opp_table->genpd_performance_state = true;
718 opp_table->parsed_static_opps = true;
723 _put_opp_list_kref(opp_table);
728 /* Initializes OPP tables based on old-deprecated bindings */
729 static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
731 const struct property *prop;
735 prop = of_find_property(dev->of_node, "operating-points", NULL);
742 * Each OPP is a set of tuples consisting of frequency and
743 * voltage like <freq-kHz vol-uV>.
745 nr = prop->length / sizeof(u32);
747 dev_err(dev, "%s: Invalid OPP table\n", __func__);
751 kref_init(&opp_table->list_kref);
755 unsigned long freq = be32_to_cpup(val++) * 1000;
756 unsigned long volt = be32_to_cpup(val++);
758 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
760 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
761 __func__, freq, ret);
762 _put_opp_list_kref(opp_table);
772 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
773 * @dev: device pointer used to lookup OPP table.
775 * Register the initial OPP table with the OPP library for given device.
779 * Duplicate OPPs (both freq and volt are same) and opp->available
780 * -EEXIST Freq are same and volt are different OR
781 * Duplicate OPPs (both freq and volt are same) and !opp->available
782 * -ENOMEM Memory allocation failure
783 * -ENODEV when 'operating-points' property is not found or is invalid data
785 * -ENODATA when empty 'operating-points' property is found
786 * -EINVAL when invalid entries are found in opp-v2 table
788 int dev_pm_opp_of_add_table(struct device *dev)
790 struct opp_table *opp_table;
793 opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0);
798 * OPPs have two version of bindings now. Also try the old (v1)
799 * bindings for backward compatibility with older dtbs.
802 ret = _of_add_opp_table_v2(dev, opp_table);
804 ret = _of_add_opp_table_v1(dev, opp_table);
807 dev_pm_opp_put_opp_table(opp_table);
811 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
814 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
815 * @dev: device pointer used to lookup OPP table.
816 * @index: Index number.
818 * Register the initial OPP table with the OPP library for given device only
819 * using the "operating-points-v2" property.
823 * Duplicate OPPs (both freq and volt are same) and opp->available
824 * -EEXIST Freq are same and volt are different OR
825 * Duplicate OPPs (both freq and volt are same) and !opp->available
826 * -ENOMEM Memory allocation failure
827 * -ENODEV when 'operating-points' property is not found or is invalid data
829 * -ENODATA when empty 'operating-points' property is found
830 * -EINVAL when invalid entries are found in opp-v2 table
832 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
834 struct opp_table *opp_table;
839 * If only one phandle is present, then the same OPP table
840 * applies for all index requests.
842 count = of_count_phandle_with_args(dev->of_node,
843 "operating-points-v2", NULL);
848 opp_table = dev_pm_opp_get_opp_table_indexed(dev, index);
852 ret = _of_add_opp_table_v2(dev, opp_table);
854 dev_pm_opp_put_opp_table(opp_table);
858 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
860 /* CPU device specific helpers */
863 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
864 * @cpumask: cpumask for which OPP table needs to be removed
866 * This removes the OPP tables for CPUs present in the @cpumask.
867 * This should be used only to remove static entries created from DT.
869 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
871 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
873 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
876 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
877 * @cpumask: cpumask for which OPP table needs to be added.
879 * This adds the OPP tables for CPUs present in the @cpumask.
881 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
883 struct device *cpu_dev;
886 if (WARN_ON(cpumask_empty(cpumask)))
889 for_each_cpu(cpu, cpumask) {
890 cpu_dev = get_cpu_device(cpu);
892 pr_err("%s: failed to get cpu%d device\n", __func__,
898 ret = dev_pm_opp_of_add_table(cpu_dev);
901 * OPP may get registered dynamically, don't print error
904 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
914 /* Free all other OPPs */
915 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
919 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
922 * Works only for OPP v2 bindings.
924 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
927 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
928 * @cpu_dev using operating-points-v2
931 * @cpu_dev: CPU device for which we do this operation
932 * @cpumask: cpumask to update with information of sharing CPUs
934 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
936 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
938 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
939 struct cpumask *cpumask)
941 struct device_node *np, *tmp_np, *cpu_np;
944 /* Get OPP descriptor node */
945 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
947 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
951 cpumask_set_cpu(cpu_dev->id, cpumask);
953 /* OPPs are shared ? */
954 if (!of_property_read_bool(np, "opp-shared"))
957 for_each_possible_cpu(cpu) {
958 if (cpu == cpu_dev->id)
961 cpu_np = of_cpu_device_node_get(cpu);
963 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
969 /* Get OPP descriptor node */
970 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
973 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
978 /* CPUs are sharing opp node */
980 cpumask_set_cpu(cpu, cpumask);
989 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
992 * of_get_required_opp_performance_state() - Search for required OPP and return its performance state.
993 * @np: Node that contains the "required-opps" property.
994 * @index: Index of the phandle to parse.
996 * Returns the performance state of the OPP pointed out by the "required-opps"
997 * property at @index in @np.
999 * Return: Zero or positive performance state on success, otherwise negative
1002 int of_get_required_opp_performance_state(struct device_node *np, int index)
1004 struct dev_pm_opp *opp;
1005 struct device_node *required_np;
1006 struct opp_table *opp_table;
1007 int pstate = -EINVAL;
1009 required_np = of_parse_required_opp(np, index);
1013 opp_table = _find_table_of_opp_np(required_np);
1014 if (IS_ERR(opp_table)) {
1015 pr_err("%s: Failed to find required OPP table %pOF: %ld\n",
1016 __func__, np, PTR_ERR(opp_table));
1017 goto put_required_np;
1020 opp = _find_opp_of_np(opp_table, required_np);
1022 pstate = opp->pstate;
1023 dev_pm_opp_put(opp);
1026 dev_pm_opp_put_opp_table(opp_table);
1029 of_node_put(required_np);
1033 EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
1036 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
1037 * @opp: opp for which DT node has to be returned for
1039 * Return: DT node corresponding to the opp, else 0 on success.
1041 * The caller needs to put the node with of_node_put() after using it.
1043 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
1045 if (IS_ERR_OR_NULL(opp)) {
1046 pr_err("%s: Invalid parameters\n", __func__);
1050 return of_node_get(opp->np);
1052 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
1055 * Callback function provided to the Energy Model framework upon registration.
1056 * This computes the power estimated by @CPU at @kHz if it is the frequency
1057 * of an existing OPP, or at the frequency of the first OPP above @kHz otherwise
1058 * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
1059 * frequency and @mW to the associated power. The power is estimated as
1060 * P = C * V^2 * f with C being the CPU's capacitance and V and f respectively
1061 * the voltage and frequency of the OPP.
1063 * Returns -ENODEV if the CPU device cannot be found, -EINVAL if the power
1064 * calculation failed because of missing parameters, 0 otherwise.
1066 static int __maybe_unused _get_cpu_power(unsigned long *mW, unsigned long *kHz,
1069 struct device *cpu_dev;
1070 struct dev_pm_opp *opp;
1071 struct device_node *np;
1072 unsigned long mV, Hz;
1077 cpu_dev = get_cpu_device(cpu);
1081 np = of_node_get(cpu_dev->of_node);
1085 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1091 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &Hz);
1095 mV = dev_pm_opp_get_voltage(opp) / 1000;
1096 dev_pm_opp_put(opp);
1100 tmp = (u64)cap * mV * mV * (Hz / 1000000);
1101 do_div(tmp, 1000000000);
1103 *mW = (unsigned long)tmp;
1110 * dev_pm_opp_of_register_em() - Attempt to register an Energy Model
1111 * @cpus : CPUs for which an Energy Model has to be registered
1113 * This checks whether the "dynamic-power-coefficient" devicetree property has
1114 * been specified, and tries to register an Energy Model with it if it has.
1116 void dev_pm_opp_of_register_em(struct cpumask *cpus)
1118 struct em_data_callback em_cb = EM_DATA_CB(_get_cpu_power);
1119 int ret, nr_opp, cpu = cpumask_first(cpus);
1120 struct device *cpu_dev;
1121 struct device_node *np;
1124 cpu_dev = get_cpu_device(cpu);
1128 nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
1132 np = of_node_get(cpu_dev->of_node);
1137 * Register an EM only if the 'dynamic-power-coefficient' property is
1138 * set in devicetree. It is assumed the voltage values are known if that
1139 * property is set since it is useless otherwise. If voltages are not
1140 * known, just let the EM registration fail with an error to alert the
1141 * user about the inconsistent configuration.
1143 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1148 em_register_perf_domain(cpus, nr_opp, &em_cb);
1150 EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);