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 struct device_node *required_np;
100 required_np = of_parse_phandle(np, "required-opps", index);
101 if (unlikely(!required_np)) {
102 pr_err("%s: Unable to parse required-opps: %pOF, index: %d\n",
103 __func__, np, index);
109 /* The caller must call dev_pm_opp_put_opp_table() after the table is used */
110 static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np)
112 struct opp_table *opp_table;
113 struct device_node *opp_table_np;
115 opp_table_np = of_get_parent(opp_np);
119 /* It is safe to put the node now as all we need now is its address */
120 of_node_put(opp_table_np);
122 mutex_lock(&opp_table_lock);
123 list_for_each_entry(opp_table, &opp_tables, node) {
124 if (opp_table_np == opp_table->np) {
125 _get_opp_table_kref(opp_table);
126 mutex_unlock(&opp_table_lock);
130 mutex_unlock(&opp_table_lock);
133 return ERR_PTR(-ENODEV);
136 /* Free resources previously acquired by _opp_table_alloc_required_tables() */
137 static void _opp_table_free_required_tables(struct opp_table *opp_table)
139 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
142 if (!required_opp_tables)
145 for (i = 0; i < opp_table->required_opp_count; i++) {
146 if (IS_ERR_OR_NULL(required_opp_tables[i]))
149 dev_pm_opp_put_opp_table(required_opp_tables[i]);
152 kfree(required_opp_tables);
154 opp_table->required_opp_count = 0;
155 opp_table->required_opp_tables = NULL;
159 * Populate all devices and opp tables which are part of "required-opps" list.
160 * Checking only the first OPP node should be enough.
162 static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
164 struct device_node *opp_np)
166 struct opp_table **required_opp_tables;
167 struct device_node *required_np, *np;
170 /* Traversing the first OPP node is all we need */
171 np = of_get_next_available_child(opp_np, NULL);
173 dev_err(dev, "Empty OPP table\n");
177 count = of_count_phandle_with_args(np, "required-opps", NULL);
181 required_opp_tables = kcalloc(count, sizeof(*required_opp_tables),
183 if (!required_opp_tables)
186 opp_table->required_opp_tables = required_opp_tables;
187 opp_table->required_opp_count = count;
189 for (i = 0; i < count; i++) {
190 required_np = of_parse_required_opp(np, i);
192 goto free_required_tables;
194 required_opp_tables[i] = _find_table_of_opp_np(required_np);
195 of_node_put(required_np);
197 if (IS_ERR(required_opp_tables[i]))
198 goto free_required_tables;
201 * We only support genpd's OPPs in the "required-opps" for now,
202 * as we don't know how much about other cases. Error out if the
203 * required OPP doesn't belong to a genpd.
205 if (!required_opp_tables[i]->is_genpd) {
206 dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n",
208 goto free_required_tables;
214 free_required_tables:
215 _opp_table_free_required_tables(opp_table);
220 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
223 struct device_node *np, *opp_np;
227 * Only required for backward compatibility with v1 bindings, but isn't
228 * harmful for other cases. And so we do it unconditionally.
230 np = of_node_get(dev->of_node);
234 if (!of_property_read_u32(np, "clock-latency", &val))
235 opp_table->clock_latency_ns_max = val;
236 of_property_read_u32(np, "voltage-tolerance",
237 &opp_table->voltage_tolerance_v1);
239 if (of_find_property(np, "#power-domain-cells", NULL))
240 opp_table->is_genpd = true;
242 /* Get OPP table node */
243 opp_np = _opp_of_get_opp_desc_node(np, index);
249 if (of_property_read_bool(opp_np, "opp-shared"))
250 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
252 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
254 opp_table->np = opp_np;
256 _opp_table_alloc_required_tables(opp_table, dev, opp_np);
260 void _of_clear_opp_table(struct opp_table *opp_table)
262 _opp_table_free_required_tables(opp_table);
266 * Release all resources previously acquired with a call to
267 * _of_opp_alloc_required_opps().
269 void _of_opp_free_required_opps(struct opp_table *opp_table,
270 struct dev_pm_opp *opp)
272 struct dev_pm_opp **required_opps = opp->required_opps;
278 for (i = 0; i < opp_table->required_opp_count; i++) {
279 if (!required_opps[i])
282 /* Put the reference back */
283 dev_pm_opp_put(required_opps[i]);
286 kfree(required_opps);
287 opp->required_opps = NULL;
290 /* Populate all required OPPs which are part of "required-opps" list */
291 static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
292 struct dev_pm_opp *opp)
294 struct dev_pm_opp **required_opps;
295 struct opp_table *required_table;
296 struct device_node *np;
297 int i, ret, count = opp_table->required_opp_count;
302 required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL);
306 opp->required_opps = required_opps;
308 for (i = 0; i < count; i++) {
309 required_table = opp_table->required_opp_tables[i];
311 np = of_parse_required_opp(opp->np, i);
314 goto free_required_opps;
317 required_opps[i] = _find_opp_of_np(required_table, np);
320 if (!required_opps[i]) {
321 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
322 __func__, opp->np, i);
324 goto free_required_opps;
331 _of_opp_free_required_opps(opp_table, opp);
336 static int _bandwidth_supported(struct device *dev, struct opp_table *opp_table)
338 struct device_node *np, *opp_np;
339 struct property *prop;
342 np = of_node_get(dev->of_node);
346 opp_np = _opp_of_get_opp_desc_node(np, 0);
349 opp_np = of_node_get(opp_table->np);
352 /* Lets not fail in case we are parsing opp-v1 bindings */
356 /* Checking only first OPP is sufficient */
357 np = of_get_next_available_child(opp_np, NULL);
359 dev_err(dev, "OPP table empty\n");
364 prop = of_find_property(np, "opp-peak-kBps", NULL);
367 if (!prop || !prop->length)
373 int dev_pm_opp_of_find_icc_paths(struct device *dev,
374 struct opp_table *opp_table)
376 struct device_node *np;
377 int ret, i, count, num_paths;
378 struct icc_path **paths;
380 ret = _bandwidth_supported(dev, opp_table);
386 np = of_node_get(dev->of_node);
390 count = of_count_phandle_with_args(np, "interconnects",
391 "#interconnect-cells");
396 /* two phandles when #interconnect-cells = <1> */
398 dev_err(dev, "%s: Invalid interconnects values\n", __func__);
402 num_paths = count / 2;
403 paths = kcalloc(num_paths, sizeof(*paths), GFP_KERNEL);
407 for (i = 0; i < num_paths; i++) {
408 paths[i] = of_icc_get_by_index(dev, i);
409 if (IS_ERR(paths[i])) {
410 ret = PTR_ERR(paths[i]);
411 if (ret != -EPROBE_DEFER) {
412 dev_err(dev, "%s: Unable to get path%d: %d\n",
420 opp_table->paths = paths;
421 opp_table->path_count = num_paths;
433 EXPORT_SYMBOL_GPL(dev_pm_opp_of_find_icc_paths);
435 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
436 struct device_node *np)
438 unsigned int levels = opp_table->supported_hw_count;
439 int count, versions, ret, i, j;
442 if (!opp_table->supported_hw) {
444 * In the case that no supported_hw has been set by the
445 * platform but there is an opp-supported-hw value set for
446 * an OPP then the OPP should not be enabled as there is
447 * no way to see if the hardware supports it.
449 if (of_find_property(np, "opp-supported-hw", NULL))
455 count = of_property_count_u32_elems(np, "opp-supported-hw");
456 if (count <= 0 || count % levels) {
457 dev_err(dev, "%s: Invalid opp-supported-hw property (%d)\n",
462 versions = count / levels;
464 /* All levels in at least one of the versions should match */
465 for (i = 0; i < versions; i++) {
466 bool supported = true;
468 for (j = 0; j < levels; j++) {
469 ret = of_property_read_u32_index(np, "opp-supported-hw",
470 i * levels + j, &val);
472 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
473 __func__, i * levels + j, ret);
477 /* Check if the level is supported */
478 if (!(val & opp_table->supported_hw[j])) {
491 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
492 struct opp_table *opp_table)
494 u32 *microvolt, *microamp = NULL;
495 int supplies = opp_table->regulator_count, vcount, icount, ret, i, j;
496 struct property *prop = NULL;
499 /* Search for "opp-microvolt-<name>" */
500 if (opp_table->prop_name) {
501 snprintf(name, sizeof(name), "opp-microvolt-%s",
502 opp_table->prop_name);
503 prop = of_find_property(opp->np, name, NULL);
507 /* Search for "opp-microvolt" */
508 sprintf(name, "opp-microvolt");
509 prop = of_find_property(opp->np, name, NULL);
511 /* Missing property isn't a problem, but an invalid entry is */
513 if (unlikely(supplies == -1)) {
514 /* Initialize regulator_count */
515 opp_table->regulator_count = 0;
522 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
528 if (unlikely(supplies == -1)) {
529 /* Initialize regulator_count */
530 supplies = opp_table->regulator_count = 1;
531 } else if (unlikely(!supplies)) {
532 dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__);
536 vcount = of_property_count_u32_elems(opp->np, name);
538 dev_err(dev, "%s: Invalid %s property (%d)\n",
539 __func__, name, vcount);
543 /* There can be one or three elements per supply */
544 if (vcount != supplies && vcount != supplies * 3) {
545 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
546 __func__, name, vcount, supplies);
550 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
554 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
556 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
561 /* Search for "opp-microamp-<name>" */
563 if (opp_table->prop_name) {
564 snprintf(name, sizeof(name), "opp-microamp-%s",
565 opp_table->prop_name);
566 prop = of_find_property(opp->np, name, NULL);
570 /* Search for "opp-microamp" */
571 sprintf(name, "opp-microamp");
572 prop = of_find_property(opp->np, name, NULL);
576 icount = of_property_count_u32_elems(opp->np, name);
578 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
584 if (icount != supplies) {
585 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
586 __func__, name, icount, supplies);
591 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
597 ret = of_property_read_u32_array(opp->np, name, microamp,
600 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
607 for (i = 0, j = 0; i < supplies; i++) {
608 opp->supplies[i].u_volt = microvolt[j++];
610 if (vcount == supplies) {
611 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
612 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
614 opp->supplies[i].u_volt_min = microvolt[j++];
615 opp->supplies[i].u_volt_max = microvolt[j++];
619 opp->supplies[i].u_amp = microamp[i];
631 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
633 * @dev: device pointer used to lookup OPP table.
635 * Free OPPs created using static entries present in DT.
637 void dev_pm_opp_of_remove_table(struct device *dev)
639 dev_pm_opp_remove_table(dev);
641 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
643 static int _read_bw(struct dev_pm_opp *new_opp, struct opp_table *table,
644 struct device_node *np, bool peak)
646 const char *name = peak ? "opp-peak-kBps" : "opp-avg-kBps";
647 struct property *prop;
651 prop = of_find_property(np, name, NULL);
655 count = prop->length / sizeof(u32);
656 if (table->path_count != count) {
657 pr_err("%s: Mismatch between %s and paths (%d %d)\n",
658 __func__, name, count, table->path_count);
662 bw = kmalloc_array(count, sizeof(*bw), GFP_KERNEL);
666 ret = of_property_read_u32_array(np, name, bw, count);
668 pr_err("%s: Error parsing %s: %d\n", __func__, name, ret);
672 for (i = 0; i < count; i++) {
674 new_opp->bandwidth[i].peak = kBps_to_icc(bw[i]);
676 new_opp->bandwidth[i].avg = kBps_to_icc(bw[i]);
684 static int _read_opp_key(struct dev_pm_opp *new_opp, struct opp_table *table,
685 struct device_node *np, bool *rate_not_available)
691 ret = of_property_read_u64(np, "opp-hz", &rate);
694 * Rate is defined as an unsigned long in clk API, and so
695 * casting explicitly to its type. Must be fixed once rate is 64
696 * bit guaranteed in clk API.
698 new_opp->rate = (unsigned long)rate;
701 *rate_not_available = !!ret;
704 * Bandwidth consists of peak and average (optional) values:
705 * opp-peak-kBps = <path1_value path2_value>;
706 * opp-avg-kBps = <path1_value path2_value>;
708 ret = _read_bw(new_opp, table, np, true);
711 ret = _read_bw(new_opp, table, np, false);
714 /* The properties were found but we failed to parse them */
715 if (ret && ret != -ENODEV)
718 if (!of_property_read_u32(np, "opp-level", &new_opp->level))
728 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
729 * @opp_table: OPP table
730 * @dev: device for which we do this operation
733 * This function adds an opp definition to the opp table and returns status. The
734 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
735 * removed by dev_pm_opp_remove.
741 * Duplicate OPPs (both freq and volt are same) and opp->available
742 * OR if the OPP is not supported by hardware.
744 * Freq are same and volt are different OR
745 * Duplicate OPPs (both freq and volt are same) and !opp->available
747 * Memory allocation failure
749 * Failed parsing the OPP node
751 static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
752 struct device *dev, struct device_node *np)
754 struct dev_pm_opp *new_opp;
758 bool rate_not_available = false;
760 new_opp = _opp_allocate(opp_table);
762 return ERR_PTR(-ENOMEM);
764 ret = _read_opp_key(new_opp, opp_table, np, &rate_not_available);
765 if (ret < 0 && !opp_table->is_genpd) {
766 dev_err(dev, "%s: opp key field not found\n", __func__);
770 /* Check if the OPP supports hardware's hierarchy of versions or not */
771 if (!_opp_is_supported(dev, opp_table, np)) {
772 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
776 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
779 new_opp->dynamic = false;
780 new_opp->available = true;
782 ret = _of_opp_alloc_required_opps(opp_table, new_opp);
786 if (!of_property_read_u32(np, "clock-latency-ns", &val))
787 new_opp->clock_latency_ns = val;
789 ret = opp_parse_supplies(new_opp, dev, opp_table);
791 goto free_required_opps;
793 if (opp_table->is_genpd)
794 new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp);
796 ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
798 /* Don't return error for duplicate OPPs */
801 goto free_required_opps;
804 /* OPP to select on device suspend */
805 if (of_property_read_bool(np, "opp-suspend")) {
806 if (opp_table->suspend_opp) {
807 /* Pick the OPP with higher rate as suspend OPP */
808 if (new_opp->rate > opp_table->suspend_opp->rate) {
809 opp_table->suspend_opp->suspend = false;
810 new_opp->suspend = true;
811 opp_table->suspend_opp = new_opp;
814 new_opp->suspend = true;
815 opp_table->suspend_opp = new_opp;
819 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
820 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
822 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
823 __func__, new_opp->turbo, new_opp->rate,
824 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
825 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
828 * Notify the changes in the availability of the operable
829 * frequency/voltage list.
831 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
835 _of_opp_free_required_opps(opp_table, new_opp);
842 /* Initializes OPP tables based on new bindings */
843 static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
845 struct device_node *np;
847 struct dev_pm_opp *opp;
849 /* OPP table is already initialized for the device */
850 mutex_lock(&opp_table->lock);
851 if (opp_table->parsed_static_opps) {
852 opp_table->parsed_static_opps++;
853 mutex_unlock(&opp_table->lock);
857 opp_table->parsed_static_opps = 1;
858 mutex_unlock(&opp_table->lock);
860 /* We have opp-table node now, iterate over it and add OPPs */
861 for_each_available_child_of_node(opp_table->np, np) {
862 opp = _opp_add_static_v2(opp_table, dev, np);
865 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
868 goto remove_static_opp;
874 /* There should be one of more OPP defined */
875 if (WARN_ON(!count)) {
877 goto remove_static_opp;
880 list_for_each_entry(opp, &opp_table->opp_list, node) {
881 /* Any non-zero performance state would enable the feature */
883 opp_table->genpd_performance_state = true;
891 _opp_remove_all_static(opp_table);
896 /* Initializes OPP tables based on old-deprecated bindings */
897 static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
899 const struct property *prop;
903 mutex_lock(&opp_table->lock);
904 if (opp_table->parsed_static_opps) {
905 opp_table->parsed_static_opps++;
906 mutex_unlock(&opp_table->lock);
910 opp_table->parsed_static_opps = 1;
911 mutex_unlock(&opp_table->lock);
913 prop = of_find_property(dev->of_node, "operating-points", NULL);
916 goto remove_static_opp;
920 goto remove_static_opp;
924 * Each OPP is a set of tuples consisting of frequency and
925 * voltage like <freq-kHz vol-uV>.
927 nr = prop->length / sizeof(u32);
929 dev_err(dev, "%s: Invalid OPP table\n", __func__);
931 goto remove_static_opp;
936 unsigned long freq = be32_to_cpup(val++) * 1000;
937 unsigned long volt = be32_to_cpup(val++);
939 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
941 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
942 __func__, freq, ret);
943 goto remove_static_opp;
951 _opp_remove_all_static(opp_table);
957 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
958 * @dev: device pointer used to lookup OPP table.
960 * Register the initial OPP table with the OPP library for given device.
964 * Duplicate OPPs (both freq and volt are same) and opp->available
965 * -EEXIST Freq are same and volt are different OR
966 * Duplicate OPPs (both freq and volt are same) and !opp->available
967 * -ENOMEM Memory allocation failure
968 * -ENODEV when 'operating-points' property is not found or is invalid data
970 * -ENODATA when empty 'operating-points' property is found
971 * -EINVAL when invalid entries are found in opp-v2 table
973 int dev_pm_opp_of_add_table(struct device *dev)
975 struct opp_table *opp_table;
978 opp_table = _add_opp_table_indexed(dev, 0);
979 if (IS_ERR(opp_table))
980 return PTR_ERR(opp_table);
983 * OPPs have two version of bindings now. Also try the old (v1)
984 * bindings for backward compatibility with older dtbs.
987 ret = _of_add_opp_table_v2(dev, opp_table);
989 ret = _of_add_opp_table_v1(dev, opp_table);
992 dev_pm_opp_put_opp_table(opp_table);
996 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
999 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
1000 * @dev: device pointer used to lookup OPP table.
1001 * @index: Index number.
1003 * Register the initial OPP table with the OPP library for given device only
1004 * using the "operating-points-v2" property.
1008 * Duplicate OPPs (both freq and volt are same) and opp->available
1009 * -EEXIST Freq are same and volt are different OR
1010 * Duplicate OPPs (both freq and volt are same) and !opp->available
1011 * -ENOMEM Memory allocation failure
1012 * -ENODEV when 'operating-points' property is not found or is invalid data
1014 * -ENODATA when empty 'operating-points' property is found
1015 * -EINVAL when invalid entries are found in opp-v2 table
1017 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
1019 struct opp_table *opp_table;
1024 * If only one phandle is present, then the same OPP table
1025 * applies for all index requests.
1027 count = of_count_phandle_with_args(dev->of_node,
1028 "operating-points-v2", NULL);
1033 opp_table = _add_opp_table_indexed(dev, index);
1034 if (IS_ERR(opp_table))
1035 return PTR_ERR(opp_table);
1037 ret = _of_add_opp_table_v2(dev, opp_table);
1039 dev_pm_opp_put_opp_table(opp_table);
1043 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
1045 /* CPU device specific helpers */
1048 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
1049 * @cpumask: cpumask for which OPP table needs to be removed
1051 * This removes the OPP tables for CPUs present in the @cpumask.
1052 * This should be used only to remove static entries created from DT.
1054 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
1056 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
1058 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
1061 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
1062 * @cpumask: cpumask for which OPP table needs to be added.
1064 * This adds the OPP tables for CPUs present in the @cpumask.
1066 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
1068 struct device *cpu_dev;
1071 if (WARN_ON(cpumask_empty(cpumask)))
1074 for_each_cpu(cpu, cpumask) {
1075 cpu_dev = get_cpu_device(cpu);
1077 pr_err("%s: failed to get cpu%d device\n", __func__,
1083 ret = dev_pm_opp_of_add_table(cpu_dev);
1086 * OPP may get registered dynamically, don't print error
1089 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
1090 __func__, cpu, ret);
1099 /* Free all other OPPs */
1100 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
1104 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
1107 * Works only for OPP v2 bindings.
1109 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
1112 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
1113 * @cpu_dev using operating-points-v2
1116 * @cpu_dev: CPU device for which we do this operation
1117 * @cpumask: cpumask to update with information of sharing CPUs
1119 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
1121 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
1123 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
1124 struct cpumask *cpumask)
1126 struct device_node *np, *tmp_np, *cpu_np;
1129 /* Get OPP descriptor node */
1130 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
1132 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
1136 cpumask_set_cpu(cpu_dev->id, cpumask);
1138 /* OPPs are shared ? */
1139 if (!of_property_read_bool(np, "opp-shared"))
1142 for_each_possible_cpu(cpu) {
1143 if (cpu == cpu_dev->id)
1146 cpu_np = of_cpu_device_node_get(cpu);
1148 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
1154 /* Get OPP descriptor node */
1155 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
1156 of_node_put(cpu_np);
1158 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
1163 /* CPUs are sharing opp node */
1165 cpumask_set_cpu(cpu, cpumask);
1167 of_node_put(tmp_np);
1174 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
1177 * of_get_required_opp_performance_state() - Search for required OPP and return its performance state.
1178 * @np: Node that contains the "required-opps" property.
1179 * @index: Index of the phandle to parse.
1181 * Returns the performance state of the OPP pointed out by the "required-opps"
1182 * property at @index in @np.
1184 * Return: Zero or positive performance state on success, otherwise negative
1187 int of_get_required_opp_performance_state(struct device_node *np, int index)
1189 struct dev_pm_opp *opp;
1190 struct device_node *required_np;
1191 struct opp_table *opp_table;
1192 int pstate = -EINVAL;
1194 required_np = of_parse_required_opp(np, index);
1198 opp_table = _find_table_of_opp_np(required_np);
1199 if (IS_ERR(opp_table)) {
1200 pr_err("%s: Failed to find required OPP table %pOF: %ld\n",
1201 __func__, np, PTR_ERR(opp_table));
1202 goto put_required_np;
1205 opp = _find_opp_of_np(opp_table, required_np);
1207 pstate = opp->pstate;
1208 dev_pm_opp_put(opp);
1211 dev_pm_opp_put_opp_table(opp_table);
1214 of_node_put(required_np);
1218 EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
1221 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
1222 * @opp: opp for which DT node has to be returned for
1224 * Return: DT node corresponding to the opp, else 0 on success.
1226 * The caller needs to put the node with of_node_put() after using it.
1228 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
1230 if (IS_ERR_OR_NULL(opp)) {
1231 pr_err("%s: Invalid parameters\n", __func__);
1235 return of_node_get(opp->np);
1237 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
1240 * Callback function provided to the Energy Model framework upon registration.
1241 * This computes the power estimated by @dev at @kHz if it is the frequency
1242 * of an existing OPP, or at the frequency of the first OPP above @kHz otherwise
1243 * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
1244 * frequency and @mW to the associated power. The power is estimated as
1245 * P = C * V^2 * f with C being the device's capacitance and V and f
1246 * respectively the voltage and frequency of the OPP.
1248 * Returns -EINVAL if the power calculation failed because of missing
1249 * parameters, 0 otherwise.
1251 static int __maybe_unused _get_power(unsigned long *mW, unsigned long *kHz,
1254 struct dev_pm_opp *opp;
1255 struct device_node *np;
1256 unsigned long mV, Hz;
1261 np = of_node_get(dev->of_node);
1265 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1271 opp = dev_pm_opp_find_freq_ceil(dev, &Hz);
1275 mV = dev_pm_opp_get_voltage(opp) / 1000;
1276 dev_pm_opp_put(opp);
1280 tmp = (u64)cap * mV * mV * (Hz / 1000000);
1281 do_div(tmp, 1000000000);
1283 *mW = (unsigned long)tmp;
1290 * dev_pm_opp_of_register_em() - Attempt to register an Energy Model
1291 * @dev : Device for which an Energy Model has to be registered
1292 * @cpus : CPUs for which an Energy Model has to be registered. For
1293 * other type of devices it should be set to NULL.
1295 * This checks whether the "dynamic-power-coefficient" devicetree property has
1296 * been specified, and tries to register an Energy Model with it if it has.
1297 * Having this property means the voltages are known for OPPs and the EM
1298 * might be calculated.
1300 int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus)
1302 struct em_data_callback em_cb = EM_DATA_CB(_get_power);
1303 struct device_node *np;
1307 if (IS_ERR_OR_NULL(dev)) {
1312 nr_opp = dev_pm_opp_get_opp_count(dev);
1318 np = of_node_get(dev->of_node);
1325 * Register an EM only if the 'dynamic-power-coefficient' property is
1326 * set in devicetree. It is assumed the voltage values are known if that
1327 * property is set since it is useless otherwise. If voltages are not
1328 * known, just let the EM registration fail with an error to alert the
1329 * user about the inconsistent configuration.
1331 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1334 dev_dbg(dev, "Couldn't find proper 'dynamic-power-coefficient' in DT\n");
1339 ret = em_dev_register_perf_domain(dev, nr_opp, &em_cb, cpus);
1346 dev_dbg(dev, "Couldn't register Energy Model %d\n", ret);
1349 EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);