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;
156 list_del(&opp_table->lazy);
160 * Populate all devices and opp tables which are part of "required-opps" list.
161 * Checking only the first OPP node should be enough.
163 static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
165 struct device_node *opp_np)
167 struct opp_table **required_opp_tables;
168 struct device_node *required_np, *np;
172 /* Traversing the first OPP node is all we need */
173 np = of_get_next_available_child(opp_np, NULL);
175 dev_warn(dev, "Empty OPP table\n");
180 count = of_count_phandle_with_args(np, "required-opps", NULL);
184 required_opp_tables = kcalloc(count, sizeof(*required_opp_tables),
186 if (!required_opp_tables)
189 opp_table->required_opp_tables = required_opp_tables;
190 opp_table->required_opp_count = count;
192 for (i = 0; i < count; i++) {
193 required_np = of_parse_required_opp(np, i);
195 goto free_required_tables;
197 required_opp_tables[i] = _find_table_of_opp_np(required_np);
198 of_node_put(required_np);
200 if (IS_ERR(required_opp_tables[i])) {
206 * We only support genpd's OPPs in the "required-opps" for now,
207 * as we don't know how much about other cases. Error out if the
208 * required OPP doesn't belong to a genpd.
210 if (!required_opp_tables[i]->is_genpd) {
211 dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n",
213 goto free_required_tables;
217 /* Let's do the linking later on */
219 list_add(&opp_table->lazy, &lazy_opp_tables);
223 free_required_tables:
224 _opp_table_free_required_tables(opp_table);
229 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
232 struct device_node *np, *opp_np;
236 * Only required for backward compatibility with v1 bindings, but isn't
237 * harmful for other cases. And so we do it unconditionally.
239 np = of_node_get(dev->of_node);
243 if (!of_property_read_u32(np, "clock-latency", &val))
244 opp_table->clock_latency_ns_max = val;
245 of_property_read_u32(np, "voltage-tolerance",
246 &opp_table->voltage_tolerance_v1);
248 if (of_find_property(np, "#power-domain-cells", NULL))
249 opp_table->is_genpd = true;
251 /* Get OPP table node */
252 opp_np = _opp_of_get_opp_desc_node(np, index);
258 if (of_property_read_bool(opp_np, "opp-shared"))
259 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
261 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
263 opp_table->np = opp_np;
265 _opp_table_alloc_required_tables(opp_table, dev, opp_np);
269 void _of_clear_opp_table(struct opp_table *opp_table)
271 _opp_table_free_required_tables(opp_table);
275 * Release all resources previously acquired with a call to
276 * _of_opp_alloc_required_opps().
278 void _of_opp_free_required_opps(struct opp_table *opp_table,
279 struct dev_pm_opp *opp)
281 struct dev_pm_opp **required_opps = opp->required_opps;
287 for (i = 0; i < opp_table->required_opp_count; i++) {
288 if (!required_opps[i])
291 /* Put the reference back */
292 dev_pm_opp_put(required_opps[i]);
295 opp->required_opps = NULL;
296 kfree(required_opps);
299 /* Populate all required OPPs which are part of "required-opps" list */
300 static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
301 struct dev_pm_opp *opp)
303 struct dev_pm_opp **required_opps;
304 struct opp_table *required_table;
305 struct device_node *np;
306 int i, ret, count = opp_table->required_opp_count;
311 required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL);
315 opp->required_opps = required_opps;
317 for (i = 0; i < count; i++) {
318 required_table = opp_table->required_opp_tables[i];
320 /* Required table not added yet, we will link later */
321 if (IS_ERR_OR_NULL(required_table))
324 np = of_parse_required_opp(opp->np, i);
327 goto free_required_opps;
330 required_opps[i] = _find_opp_of_np(required_table, np);
333 if (!required_opps[i]) {
334 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
335 __func__, opp->np, i);
337 goto free_required_opps;
344 _of_opp_free_required_opps(opp_table, opp);
349 /* Link required OPPs for an individual OPP */
350 static int lazy_link_required_opps(struct opp_table *opp_table,
351 struct opp_table *new_table, int index)
353 struct device_node *required_np;
354 struct dev_pm_opp *opp;
356 list_for_each_entry(opp, &opp_table->opp_list, node) {
357 required_np = of_parse_required_opp(opp->np, index);
358 if (unlikely(!required_np))
361 opp->required_opps[index] = _find_opp_of_np(new_table, required_np);
362 of_node_put(required_np);
364 if (!opp->required_opps[index]) {
365 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
366 __func__, opp->np, index);
374 /* Link required OPPs for all OPPs of the newly added OPP table */
375 static void lazy_link_required_opp_table(struct opp_table *new_table)
377 struct opp_table *opp_table, *temp, **required_opp_tables;
378 struct device_node *required_np, *opp_np, *required_table_np;
379 struct dev_pm_opp *opp;
383 * We only support genpd's OPPs in the "required-opps" for now,
384 * as we don't know much about other cases.
386 if (!new_table->is_genpd)
389 mutex_lock(&opp_table_lock);
391 list_for_each_entry_safe(opp_table, temp, &lazy_opp_tables, lazy) {
394 /* opp_np can't be invalid here */
395 opp_np = of_get_next_available_child(opp_table->np, NULL);
397 for (i = 0; i < opp_table->required_opp_count; i++) {
398 required_opp_tables = opp_table->required_opp_tables;
400 /* Required opp-table is already parsed */
401 if (!IS_ERR(required_opp_tables[i]))
404 /* required_np can't be invalid here */
405 required_np = of_parse_required_opp(opp_np, i);
406 required_table_np = of_get_parent(required_np);
408 of_node_put(required_table_np);
409 of_node_put(required_np);
412 * Newly added table isn't the required opp-table for
415 if (required_table_np != new_table->np) {
420 required_opp_tables[i] = new_table;
421 _get_opp_table_kref(new_table);
424 ret = lazy_link_required_opps(opp_table, new_table, i);
426 /* The OPPs will be marked unusable */
434 /* All required opp-tables found, remove from lazy list */
436 list_del(&opp_table->lazy);
437 INIT_LIST_HEAD(&opp_table->lazy);
439 list_for_each_entry(opp, &opp_table->opp_list, node)
440 _required_opps_available(opp, opp_table->required_opp_count);
444 mutex_unlock(&opp_table_lock);
447 static int _bandwidth_supported(struct device *dev, struct opp_table *opp_table)
449 struct device_node *np, *opp_np;
450 struct property *prop;
453 np = of_node_get(dev->of_node);
457 opp_np = _opp_of_get_opp_desc_node(np, 0);
460 opp_np = of_node_get(opp_table->np);
463 /* Lets not fail in case we are parsing opp-v1 bindings */
467 /* Checking only first OPP is sufficient */
468 np = of_get_next_available_child(opp_np, NULL);
470 dev_err(dev, "OPP table empty\n");
475 prop = of_find_property(np, "opp-peak-kBps", NULL);
478 if (!prop || !prop->length)
484 int dev_pm_opp_of_find_icc_paths(struct device *dev,
485 struct opp_table *opp_table)
487 struct device_node *np;
488 int ret, i, count, num_paths;
489 struct icc_path **paths;
491 ret = _bandwidth_supported(dev, opp_table);
493 return 0; /* Empty OPP table is a valid corner-case, let's not fail */
499 np = of_node_get(dev->of_node);
503 count = of_count_phandle_with_args(np, "interconnects",
504 "#interconnect-cells");
509 /* two phandles when #interconnect-cells = <1> */
511 dev_err(dev, "%s: Invalid interconnects values\n", __func__);
515 num_paths = count / 2;
516 paths = kcalloc(num_paths, sizeof(*paths), GFP_KERNEL);
520 for (i = 0; i < num_paths; i++) {
521 paths[i] = of_icc_get_by_index(dev, i);
522 if (IS_ERR(paths[i])) {
523 ret = PTR_ERR(paths[i]);
524 if (ret != -EPROBE_DEFER) {
525 dev_err(dev, "%s: Unable to get path%d: %d\n",
533 opp_table->paths = paths;
534 opp_table->path_count = num_paths;
546 EXPORT_SYMBOL_GPL(dev_pm_opp_of_find_icc_paths);
548 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
549 struct device_node *np)
551 unsigned int levels = opp_table->supported_hw_count;
552 int count, versions, ret, i, j;
555 if (!opp_table->supported_hw) {
557 * In the case that no supported_hw has been set by the
558 * platform but there is an opp-supported-hw value set for
559 * an OPP then the OPP should not be enabled as there is
560 * no way to see if the hardware supports it.
562 if (of_find_property(np, "opp-supported-hw", NULL))
568 count = of_property_count_u32_elems(np, "opp-supported-hw");
569 if (count <= 0 || count % levels) {
570 dev_err(dev, "%s: Invalid opp-supported-hw property (%d)\n",
575 versions = count / levels;
577 /* All levels in at least one of the versions should match */
578 for (i = 0; i < versions; i++) {
579 bool supported = true;
581 for (j = 0; j < levels; j++) {
582 ret = of_property_read_u32_index(np, "opp-supported-hw",
583 i * levels + j, &val);
585 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
586 __func__, i * levels + j, ret);
590 /* Check if the level is supported */
591 if (!(val & opp_table->supported_hw[j])) {
604 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
605 struct opp_table *opp_table)
607 u32 *microvolt, *microamp = NULL;
608 int supplies = opp_table->regulator_count, vcount, icount, ret, i, j;
609 struct property *prop = NULL;
612 /* Search for "opp-microvolt-<name>" */
613 if (opp_table->prop_name) {
614 snprintf(name, sizeof(name), "opp-microvolt-%s",
615 opp_table->prop_name);
616 prop = of_find_property(opp->np, name, NULL);
620 /* Search for "opp-microvolt" */
621 sprintf(name, "opp-microvolt");
622 prop = of_find_property(opp->np, name, NULL);
624 /* Missing property isn't a problem, but an invalid entry is */
626 if (unlikely(supplies == -1)) {
627 /* Initialize regulator_count */
628 opp_table->regulator_count = 0;
635 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
641 if (unlikely(supplies == -1)) {
642 /* Initialize regulator_count */
643 supplies = opp_table->regulator_count = 1;
644 } else if (unlikely(!supplies)) {
645 dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__);
649 vcount = of_property_count_u32_elems(opp->np, name);
651 dev_err(dev, "%s: Invalid %s property (%d)\n",
652 __func__, name, vcount);
656 /* There can be one or three elements per supply */
657 if (vcount != supplies && vcount != supplies * 3) {
658 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
659 __func__, name, vcount, supplies);
663 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
667 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
669 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
674 /* Search for "opp-microamp-<name>" */
676 if (opp_table->prop_name) {
677 snprintf(name, sizeof(name), "opp-microamp-%s",
678 opp_table->prop_name);
679 prop = of_find_property(opp->np, name, NULL);
683 /* Search for "opp-microamp" */
684 sprintf(name, "opp-microamp");
685 prop = of_find_property(opp->np, name, NULL);
689 icount = of_property_count_u32_elems(opp->np, name);
691 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
697 if (icount != supplies) {
698 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
699 __func__, name, icount, supplies);
704 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
710 ret = of_property_read_u32_array(opp->np, name, microamp,
713 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
720 for (i = 0, j = 0; i < supplies; i++) {
721 opp->supplies[i].u_volt = microvolt[j++];
723 if (vcount == supplies) {
724 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
725 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
727 opp->supplies[i].u_volt_min = microvolt[j++];
728 opp->supplies[i].u_volt_max = microvolt[j++];
732 opp->supplies[i].u_amp = microamp[i];
744 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
746 * @dev: device pointer used to lookup OPP table.
748 * Free OPPs created using static entries present in DT.
750 void dev_pm_opp_of_remove_table(struct device *dev)
752 dev_pm_opp_remove_table(dev);
754 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
756 static int _read_bw(struct dev_pm_opp *new_opp, struct opp_table *table,
757 struct device_node *np, bool peak)
759 const char *name = peak ? "opp-peak-kBps" : "opp-avg-kBps";
760 struct property *prop;
764 prop = of_find_property(np, name, NULL);
768 count = prop->length / sizeof(u32);
769 if (table->path_count != count) {
770 pr_err("%s: Mismatch between %s and paths (%d %d)\n",
771 __func__, name, count, table->path_count);
775 bw = kmalloc_array(count, sizeof(*bw), GFP_KERNEL);
779 ret = of_property_read_u32_array(np, name, bw, count);
781 pr_err("%s: Error parsing %s: %d\n", __func__, name, ret);
785 for (i = 0; i < count; i++) {
787 new_opp->bandwidth[i].peak = kBps_to_icc(bw[i]);
789 new_opp->bandwidth[i].avg = kBps_to_icc(bw[i]);
797 static int _read_opp_key(struct dev_pm_opp *new_opp, struct opp_table *table,
798 struct device_node *np, bool *rate_not_available)
804 ret = of_property_read_u64(np, "opp-hz", &rate);
807 * Rate is defined as an unsigned long in clk API, and so
808 * casting explicitly to its type. Must be fixed once rate is 64
809 * bit guaranteed in clk API.
811 new_opp->rate = (unsigned long)rate;
814 *rate_not_available = !!ret;
817 * Bandwidth consists of peak and average (optional) values:
818 * opp-peak-kBps = <path1_value path2_value>;
819 * opp-avg-kBps = <path1_value path2_value>;
821 ret = _read_bw(new_opp, table, np, true);
824 ret = _read_bw(new_opp, table, np, false);
827 /* The properties were found but we failed to parse them */
828 if (ret && ret != -ENODEV)
831 if (!of_property_read_u32(np, "opp-level", &new_opp->level))
841 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
842 * @opp_table: OPP table
843 * @dev: device for which we do this operation
846 * This function adds an opp definition to the opp table and returns status. The
847 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
848 * removed by dev_pm_opp_remove.
854 * Duplicate OPPs (both freq and volt are same) and opp->available
855 * OR if the OPP is not supported by hardware.
857 * Freq are same and volt are different OR
858 * Duplicate OPPs (both freq and volt are same) and !opp->available
860 * Memory allocation failure
862 * Failed parsing the OPP node
864 static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
865 struct device *dev, struct device_node *np)
867 struct dev_pm_opp *new_opp;
870 bool rate_not_available = false;
872 new_opp = _opp_allocate(opp_table);
874 return ERR_PTR(-ENOMEM);
876 ret = _read_opp_key(new_opp, opp_table, np, &rate_not_available);
877 if (ret < 0 && !opp_table->is_genpd) {
878 dev_err(dev, "%s: opp key field not found\n", __func__);
882 /* Check if the OPP supports hardware's hierarchy of versions or not */
883 if (!_opp_is_supported(dev, opp_table, np)) {
884 dev_dbg(dev, "OPP not supported by hardware: %lu\n",
889 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
892 new_opp->dynamic = false;
893 new_opp->available = true;
895 ret = _of_opp_alloc_required_opps(opp_table, new_opp);
899 if (!of_property_read_u32(np, "clock-latency-ns", &val))
900 new_opp->clock_latency_ns = val;
902 ret = opp_parse_supplies(new_opp, dev, opp_table);
904 goto free_required_opps;
906 if (opp_table->is_genpd)
907 new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp);
909 ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
911 /* Don't return error for duplicate OPPs */
914 goto free_required_opps;
917 /* OPP to select on device suspend */
918 if (of_property_read_bool(np, "opp-suspend")) {
919 if (opp_table->suspend_opp) {
920 /* Pick the OPP with higher rate as suspend OPP */
921 if (new_opp->rate > opp_table->suspend_opp->rate) {
922 opp_table->suspend_opp->suspend = false;
923 new_opp->suspend = true;
924 opp_table->suspend_opp = new_opp;
927 new_opp->suspend = true;
928 opp_table->suspend_opp = new_opp;
932 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
933 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
935 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu level:%u\n",
936 __func__, new_opp->turbo, new_opp->rate,
937 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
938 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns,
942 * Notify the changes in the availability of the operable
943 * frequency/voltage list.
945 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
949 _of_opp_free_required_opps(opp_table, new_opp);
956 /* Initializes OPP tables based on new bindings */
957 static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
959 struct device_node *np;
961 struct dev_pm_opp *opp;
963 /* OPP table is already initialized for the device */
964 mutex_lock(&opp_table->lock);
965 if (opp_table->parsed_static_opps) {
966 opp_table->parsed_static_opps++;
967 mutex_unlock(&opp_table->lock);
971 opp_table->parsed_static_opps = 1;
972 mutex_unlock(&opp_table->lock);
974 /* We have opp-table node now, iterate over it and add OPPs */
975 for_each_available_child_of_node(opp_table->np, np) {
976 opp = _opp_add_static_v2(opp_table, dev, np);
979 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
982 goto remove_static_opp;
988 /* There should be one of more OPP defined */
989 if (WARN_ON(!count)) {
991 goto remove_static_opp;
994 list_for_each_entry(opp, &opp_table->opp_list, node) {
995 /* Any non-zero performance state would enable the feature */
997 opp_table->genpd_performance_state = true;
1002 lazy_link_required_opp_table(opp_table);
1007 _opp_remove_all_static(opp_table);
1012 /* Initializes OPP tables based on old-deprecated bindings */
1013 static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
1015 const struct property *prop;
1019 mutex_lock(&opp_table->lock);
1020 if (opp_table->parsed_static_opps) {
1021 opp_table->parsed_static_opps++;
1022 mutex_unlock(&opp_table->lock);
1026 opp_table->parsed_static_opps = 1;
1027 mutex_unlock(&opp_table->lock);
1029 prop = of_find_property(dev->of_node, "operating-points", NULL);
1032 goto remove_static_opp;
1036 goto remove_static_opp;
1040 * Each OPP is a set of tuples consisting of frequency and
1041 * voltage like <freq-kHz vol-uV>.
1043 nr = prop->length / sizeof(u32);
1045 dev_err(dev, "%s: Invalid OPP table\n", __func__);
1047 goto remove_static_opp;
1052 unsigned long freq = be32_to_cpup(val++) * 1000;
1053 unsigned long volt = be32_to_cpup(val++);
1055 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
1057 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
1058 __func__, freq, ret);
1059 goto remove_static_opp;
1067 _opp_remove_all_static(opp_table);
1072 static int _of_add_table_indexed(struct device *dev, int index, bool getclk)
1074 struct opp_table *opp_table;
1079 * If only one phandle is present, then the same OPP table
1080 * applies for all index requests.
1082 count = of_count_phandle_with_args(dev->of_node,
1083 "operating-points-v2", NULL);
1088 opp_table = _add_opp_table_indexed(dev, index, getclk);
1089 if (IS_ERR(opp_table))
1090 return PTR_ERR(opp_table);
1093 * OPPs have two version of bindings now. Also try the old (v1)
1094 * bindings for backward compatibility with older dtbs.
1097 ret = _of_add_opp_table_v2(dev, opp_table);
1099 ret = _of_add_opp_table_v1(dev, opp_table);
1102 dev_pm_opp_put_opp_table(opp_table);
1108 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
1109 * @dev: device pointer used to lookup OPP table.
1111 * Register the initial OPP table with the OPP library for given device.
1115 * Duplicate OPPs (both freq and volt are same) and opp->available
1116 * -EEXIST Freq are same and volt are different OR
1117 * Duplicate OPPs (both freq and volt are same) and !opp->available
1118 * -ENOMEM Memory allocation failure
1119 * -ENODEV when 'operating-points' property is not found or is invalid data
1121 * -ENODATA when empty 'operating-points' property is found
1122 * -EINVAL when invalid entries are found in opp-v2 table
1124 int dev_pm_opp_of_add_table(struct device *dev)
1126 return _of_add_table_indexed(dev, 0, true);
1128 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
1131 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
1132 * @dev: device pointer used to lookup OPP table.
1133 * @index: Index number.
1135 * Register the initial OPP table with the OPP library for given device only
1136 * using the "operating-points-v2" property.
1138 * Return: Refer to dev_pm_opp_of_add_table() for return values.
1140 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
1142 return _of_add_table_indexed(dev, index, true);
1144 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
1147 * dev_pm_opp_of_add_table_noclk() - Initialize indexed opp table from device
1148 * tree without getting clk for device.
1149 * @dev: device pointer used to lookup OPP table.
1150 * @index: Index number.
1152 * Register the initial OPP table with the OPP library for given device only
1153 * using the "operating-points-v2" property. Do not try to get the clk for the
1156 * Return: Refer to dev_pm_opp_of_add_table() for return values.
1158 int dev_pm_opp_of_add_table_noclk(struct device *dev, int index)
1160 return _of_add_table_indexed(dev, index, false);
1162 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_noclk);
1164 /* CPU device specific helpers */
1167 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
1168 * @cpumask: cpumask for which OPP table needs to be removed
1170 * This removes the OPP tables for CPUs present in the @cpumask.
1171 * This should be used only to remove static entries created from DT.
1173 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
1175 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
1177 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
1180 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
1181 * @cpumask: cpumask for which OPP table needs to be added.
1183 * This adds the OPP tables for CPUs present in the @cpumask.
1185 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
1187 struct device *cpu_dev;
1190 if (WARN_ON(cpumask_empty(cpumask)))
1193 for_each_cpu(cpu, cpumask) {
1194 cpu_dev = get_cpu_device(cpu);
1196 pr_err("%s: failed to get cpu%d device\n", __func__,
1202 ret = dev_pm_opp_of_add_table(cpu_dev);
1205 * OPP may get registered dynamically, don't print error
1208 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
1209 __func__, cpu, ret);
1218 /* Free all other OPPs */
1219 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
1223 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
1226 * Works only for OPP v2 bindings.
1228 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
1231 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
1232 * @cpu_dev using operating-points-v2
1235 * @cpu_dev: CPU device for which we do this operation
1236 * @cpumask: cpumask to update with information of sharing CPUs
1238 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
1240 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
1242 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
1243 struct cpumask *cpumask)
1245 struct device_node *np, *tmp_np, *cpu_np;
1248 /* Get OPP descriptor node */
1249 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
1251 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
1255 cpumask_set_cpu(cpu_dev->id, cpumask);
1257 /* OPPs are shared ? */
1258 if (!of_property_read_bool(np, "opp-shared"))
1261 for_each_possible_cpu(cpu) {
1262 if (cpu == cpu_dev->id)
1265 cpu_np = of_cpu_device_node_get(cpu);
1267 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
1273 /* Get OPP descriptor node */
1274 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
1275 of_node_put(cpu_np);
1277 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
1282 /* CPUs are sharing opp node */
1284 cpumask_set_cpu(cpu, cpumask);
1286 of_node_put(tmp_np);
1293 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
1296 * of_get_required_opp_performance_state() - Search for required OPP and return its performance state.
1297 * @np: Node that contains the "required-opps" property.
1298 * @index: Index of the phandle to parse.
1300 * Returns the performance state of the OPP pointed out by the "required-opps"
1301 * property at @index in @np.
1303 * Return: Zero or positive performance state on success, otherwise negative
1306 int of_get_required_opp_performance_state(struct device_node *np, int index)
1308 struct dev_pm_opp *opp;
1309 struct device_node *required_np;
1310 struct opp_table *opp_table;
1311 int pstate = -EINVAL;
1313 required_np = of_parse_required_opp(np, index);
1317 opp_table = _find_table_of_opp_np(required_np);
1318 if (IS_ERR(opp_table)) {
1319 pr_err("%s: Failed to find required OPP table %pOF: %ld\n",
1320 __func__, np, PTR_ERR(opp_table));
1321 goto put_required_np;
1324 opp = _find_opp_of_np(opp_table, required_np);
1326 pstate = opp->pstate;
1327 dev_pm_opp_put(opp);
1330 dev_pm_opp_put_opp_table(opp_table);
1333 of_node_put(required_np);
1337 EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
1340 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
1341 * @opp: opp for which DT node has to be returned for
1343 * Return: DT node corresponding to the opp, else 0 on success.
1345 * The caller needs to put the node with of_node_put() after using it.
1347 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
1349 if (IS_ERR_OR_NULL(opp)) {
1350 pr_err("%s: Invalid parameters\n", __func__);
1354 return of_node_get(opp->np);
1356 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
1359 * Callback function provided to the Energy Model framework upon registration.
1360 * This computes the power estimated by @dev at @kHz if it is the frequency
1361 * of an existing OPP, or at the frequency of the first OPP above @kHz otherwise
1362 * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
1363 * frequency and @mW to the associated power. The power is estimated as
1364 * P = C * V^2 * f with C being the device's capacitance and V and f
1365 * respectively the voltage and frequency of the OPP.
1367 * Returns -EINVAL if the power calculation failed because of missing
1368 * parameters, 0 otherwise.
1370 static int __maybe_unused _get_power(unsigned long *mW, unsigned long *kHz,
1373 struct dev_pm_opp *opp;
1374 struct device_node *np;
1375 unsigned long mV, Hz;
1380 np = of_node_get(dev->of_node);
1384 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1390 opp = dev_pm_opp_find_freq_ceil(dev, &Hz);
1394 mV = dev_pm_opp_get_voltage(opp) / 1000;
1395 dev_pm_opp_put(opp);
1399 tmp = (u64)cap * mV * mV * (Hz / 1000000);
1400 do_div(tmp, 1000000000);
1402 *mW = (unsigned long)tmp;
1409 * dev_pm_opp_of_register_em() - Attempt to register an Energy Model
1410 * @dev : Device for which an Energy Model has to be registered
1411 * @cpus : CPUs for which an Energy Model has to be registered. For
1412 * other type of devices it should be set to NULL.
1414 * This checks whether the "dynamic-power-coefficient" devicetree property has
1415 * been specified, and tries to register an Energy Model with it if it has.
1416 * Having this property means the voltages are known for OPPs and the EM
1417 * might be calculated.
1419 int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus)
1421 struct em_data_callback em_cb = EM_DATA_CB(_get_power);
1422 struct device_node *np;
1426 if (IS_ERR_OR_NULL(dev)) {
1431 nr_opp = dev_pm_opp_get_opp_count(dev);
1437 np = of_node_get(dev->of_node);
1444 * Register an EM only if the 'dynamic-power-coefficient' property is
1445 * set in devicetree. It is assumed the voltage values are known if that
1446 * property is set since it is useless otherwise. If voltages are not
1447 * known, just let the EM registration fail with an error to alert the
1448 * user about the inconsistent configuration.
1450 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1453 dev_dbg(dev, "Couldn't find proper 'dynamic-power-coefficient' in DT\n");
1458 ret = em_dev_register_perf_domain(dev, nr_opp, &em_cb, cpus, true);
1465 dev_dbg(dev, "Couldn't register Energy Model %d\n", ret);
1468 EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);