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>
27 * Returns opp descriptor node for a device node, caller must
30 static struct device_node
*_opp_of_get_opp_desc_node(struct device_node
*np
,
33 /* "operating-points-v2" can be an array for power domain providers */
34 return of_parse_phandle(np
, "operating-points-v2", index
);
37 /* Returns opp descriptor node for a device, caller must do of_node_put() */
38 struct device_node
*dev_pm_opp_of_get_opp_desc_node(struct device
*dev
)
40 return _opp_of_get_opp_desc_node(dev
->of_node
, 0);
42 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node
);
44 struct opp_table
*_managed_opp(struct device
*dev
, int index
)
46 struct opp_table
*opp_table
, *managed_table
= NULL
;
47 struct device_node
*np
;
49 np
= _opp_of_get_opp_desc_node(dev
->of_node
, index
);
53 list_for_each_entry(opp_table
, &opp_tables
, node
) {
54 if (opp_table
->np
== np
) {
56 * Multiple devices can point to the same OPP table and
57 * so will have same node-pointer, np.
59 * But the OPPs will be considered as shared only if the
60 * OPP table contains a "opp-shared" property.
62 if (opp_table
->shared_opp
== OPP_TABLE_ACCESS_SHARED
) {
63 _get_opp_table_kref(opp_table
);
64 managed_table
= opp_table
;
76 void _of_init_opp_table(struct opp_table
*opp_table
, struct device
*dev
,
79 struct device_node
*np
, *opp_np
;
83 * Only required for backward compatibility with v1 bindings, but isn't
84 * harmful for other cases. And so we do it unconditionally.
86 np
= of_node_get(dev
->of_node
);
90 if (!of_property_read_u32(np
, "clock-latency", &val
))
91 opp_table
->clock_latency_ns_max
= val
;
92 of_property_read_u32(np
, "voltage-tolerance",
93 &opp_table
->voltage_tolerance_v1
);
95 /* Get OPP table node */
96 opp_np
= _opp_of_get_opp_desc_node(np
, index
);
102 if (of_property_read_bool(opp_np
, "opp-shared"))
103 opp_table
->shared_opp
= OPP_TABLE_ACCESS_SHARED
;
105 opp_table
->shared_opp
= OPP_TABLE_ACCESS_EXCLUSIVE
;
107 opp_table
->np
= opp_np
;
112 static bool _opp_is_supported(struct device
*dev
, struct opp_table
*opp_table
,
113 struct device_node
*np
)
115 unsigned int count
= opp_table
->supported_hw_count
;
119 if (!opp_table
->supported_hw
) {
121 * In the case that no supported_hw has been set by the
122 * platform but there is an opp-supported-hw value set for
123 * an OPP then the OPP should not be enabled as there is
124 * no way to see if the hardware supports it.
126 if (of_find_property(np
, "opp-supported-hw", NULL
))
133 ret
= of_property_read_u32_index(np
, "opp-supported-hw", count
,
136 dev_warn(dev
, "%s: failed to read opp-supported-hw property at index %d: %d\n",
137 __func__
, count
, ret
);
141 /* Both of these are bitwise masks of the versions */
142 if (!(version
& opp_table
->supported_hw
[count
]))
149 static int opp_parse_supplies(struct dev_pm_opp
*opp
, struct device
*dev
,
150 struct opp_table
*opp_table
)
152 u32
*microvolt
, *microamp
= NULL
;
153 int supplies
, vcount
, icount
, ret
, i
, j
;
154 struct property
*prop
= NULL
;
157 supplies
= opp_table
->regulator_count
? opp_table
->regulator_count
: 1;
159 /* Search for "opp-microvolt-<name>" */
160 if (opp_table
->prop_name
) {
161 snprintf(name
, sizeof(name
), "opp-microvolt-%s",
162 opp_table
->prop_name
);
163 prop
= of_find_property(opp
->np
, name
, NULL
);
167 /* Search for "opp-microvolt" */
168 sprintf(name
, "opp-microvolt");
169 prop
= of_find_property(opp
->np
, name
, NULL
);
171 /* Missing property isn't a problem, but an invalid entry is */
173 if (!opp_table
->regulator_count
)
176 dev_err(dev
, "%s: opp-microvolt missing although OPP managing regulators\n",
182 vcount
= of_property_count_u32_elems(opp
->np
, name
);
184 dev_err(dev
, "%s: Invalid %s property (%d)\n",
185 __func__
, name
, vcount
);
189 /* There can be one or three elements per supply */
190 if (vcount
!= supplies
&& vcount
!= supplies
* 3) {
191 dev_err(dev
, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
192 __func__
, name
, vcount
, supplies
);
196 microvolt
= kmalloc_array(vcount
, sizeof(*microvolt
), GFP_KERNEL
);
200 ret
= of_property_read_u32_array(opp
->np
, name
, microvolt
, vcount
);
202 dev_err(dev
, "%s: error parsing %s: %d\n", __func__
, name
, ret
);
207 /* Search for "opp-microamp-<name>" */
209 if (opp_table
->prop_name
) {
210 snprintf(name
, sizeof(name
), "opp-microamp-%s",
211 opp_table
->prop_name
);
212 prop
= of_find_property(opp
->np
, name
, NULL
);
216 /* Search for "opp-microamp" */
217 sprintf(name
, "opp-microamp");
218 prop
= of_find_property(opp
->np
, name
, NULL
);
222 icount
= of_property_count_u32_elems(opp
->np
, name
);
224 dev_err(dev
, "%s: Invalid %s property (%d)\n", __func__
,
230 if (icount
!= supplies
) {
231 dev_err(dev
, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
232 __func__
, name
, icount
, supplies
);
237 microamp
= kmalloc_array(icount
, sizeof(*microamp
), GFP_KERNEL
);
243 ret
= of_property_read_u32_array(opp
->np
, name
, microamp
,
246 dev_err(dev
, "%s: error parsing %s: %d\n", __func__
,
253 for (i
= 0, j
= 0; i
< supplies
; i
++) {
254 opp
->supplies
[i
].u_volt
= microvolt
[j
++];
256 if (vcount
== supplies
) {
257 opp
->supplies
[i
].u_volt_min
= opp
->supplies
[i
].u_volt
;
258 opp
->supplies
[i
].u_volt_max
= opp
->supplies
[i
].u_volt
;
260 opp
->supplies
[i
].u_volt_min
= microvolt
[j
++];
261 opp
->supplies
[i
].u_volt_max
= microvolt
[j
++];
265 opp
->supplies
[i
].u_amp
= microamp
[i
];
277 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
279 * @dev: device pointer used to lookup OPP table.
281 * Free OPPs created using static entries present in DT.
283 void dev_pm_opp_of_remove_table(struct device
*dev
)
285 _dev_pm_opp_find_and_remove_table(dev
);
287 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table
);
290 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
291 * @opp_table: OPP table
292 * @dev: device for which we do this operation
295 * This function adds an opp definition to the opp table and returns status. The
296 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
297 * removed by dev_pm_opp_remove.
303 * Duplicate OPPs (both freq and volt are same) and opp->available
304 * OR if the OPP is not supported by hardware.
306 * Freq are same and volt are different OR
307 * Duplicate OPPs (both freq and volt are same) and !opp->available
309 * Memory allocation failure
311 * Failed parsing the OPP node
313 static struct dev_pm_opp
*_opp_add_static_v2(struct opp_table
*opp_table
,
314 struct device
*dev
, struct device_node
*np
)
316 struct dev_pm_opp
*new_opp
;
320 bool rate_not_available
= false;
322 new_opp
= _opp_allocate(opp_table
);
324 return ERR_PTR(-ENOMEM
);
326 ret
= of_property_read_u64(np
, "opp-hz", &rate
);
328 /* "opp-hz" is optional for devices like power domains. */
329 if (!of_find_property(dev
->of_node
, "#power-domain-cells",
331 dev_err(dev
, "%s: opp-hz not found\n", __func__
);
335 rate_not_available
= true;
338 * Rate is defined as an unsigned long in clk API, and so
339 * casting explicitly to its type. Must be fixed once rate is 64
340 * bit guaranteed in clk API.
342 new_opp
->rate
= (unsigned long)rate
;
345 /* Check if the OPP supports hardware's hierarchy of versions or not */
346 if (!_opp_is_supported(dev
, opp_table
, np
)) {
347 dev_dbg(dev
, "OPP not supported by hardware: %llu\n", rate
);
351 new_opp
->turbo
= of_property_read_bool(np
, "turbo-mode");
354 new_opp
->dynamic
= false;
355 new_opp
->available
= true;
357 if (!of_property_read_u32(np
, "clock-latency-ns", &val
))
358 new_opp
->clock_latency_ns
= val
;
360 new_opp
->pstate
= of_genpd_opp_to_performance_state(dev
, np
);
362 ret
= opp_parse_supplies(new_opp
, dev
, opp_table
);
366 ret
= _opp_add(dev
, new_opp
, opp_table
, rate_not_available
);
368 /* Don't return error for duplicate OPPs */
374 /* OPP to select on device suspend */
375 if (of_property_read_bool(np
, "opp-suspend")) {
376 if (opp_table
->suspend_opp
) {
377 dev_warn(dev
, "%s: Multiple suspend OPPs found (%lu %lu)\n",
378 __func__
, opp_table
->suspend_opp
->rate
,
381 new_opp
->suspend
= true;
382 opp_table
->suspend_opp
= new_opp
;
386 if (new_opp
->clock_latency_ns
> opp_table
->clock_latency_ns_max
)
387 opp_table
->clock_latency_ns_max
= new_opp
->clock_latency_ns
;
389 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
390 __func__
, new_opp
->turbo
, new_opp
->rate
,
391 new_opp
->supplies
[0].u_volt
, new_opp
->supplies
[0].u_volt_min
,
392 new_opp
->supplies
[0].u_volt_max
, new_opp
->clock_latency_ns
);
395 * Notify the changes in the availability of the operable
396 * frequency/voltage list.
398 blocking_notifier_call_chain(&opp_table
->head
, OPP_EVENT_ADD
, new_opp
);
407 /* Initializes OPP tables based on new bindings */
408 static int _of_add_opp_table_v2(struct device
*dev
, struct opp_table
*opp_table
)
410 struct device_node
*np
;
411 int ret
, count
= 0, pstate_count
= 0;
412 struct dev_pm_opp
*opp
;
414 /* OPP table is already initialized for the device */
415 if (opp_table
->parsed_static_opps
) {
416 kref_get(&opp_table
->list_kref
);
420 kref_init(&opp_table
->list_kref
);
422 /* We have opp-table node now, iterate over it and add OPPs */
423 for_each_available_child_of_node(opp_table
->np
, np
) {
424 opp
= _opp_add_static_v2(opp_table
, dev
, np
);
427 dev_err(dev
, "%s: Failed to add OPP, %d\n", __func__
,
436 /* There should be one of more OPP defined */
437 if (WARN_ON(!count
)) {
442 list_for_each_entry(opp
, &opp_table
->opp_list
, node
)
443 pstate_count
+= !!opp
->pstate
;
445 /* Either all or none of the nodes shall have performance state set */
446 if (pstate_count
&& pstate_count
!= count
) {
447 dev_err(dev
, "Not all nodes have performance state set (%d: %d)\n",
448 count
, pstate_count
);
454 opp_table
->genpd_performance_state
= true;
456 opp_table
->parsed_static_opps
= true;
461 _put_opp_list_kref(opp_table
);
466 /* Initializes OPP tables based on old-deprecated bindings */
467 static int _of_add_opp_table_v1(struct device
*dev
, struct opp_table
*opp_table
)
469 const struct property
*prop
;
473 prop
= of_find_property(dev
->of_node
, "operating-points", NULL
);
480 * Each OPP is a set of tuples consisting of frequency and
481 * voltage like <freq-kHz vol-uV>.
483 nr
= prop
->length
/ sizeof(u32
);
485 dev_err(dev
, "%s: Invalid OPP table\n", __func__
);
489 kref_init(&opp_table
->list_kref
);
493 unsigned long freq
= be32_to_cpup(val
++) * 1000;
494 unsigned long volt
= be32_to_cpup(val
++);
496 ret
= _opp_add_v1(opp_table
, dev
, freq
, volt
, false);
498 dev_err(dev
, "%s: Failed to add OPP %ld (%d)\n",
499 __func__
, freq
, ret
);
500 _put_opp_list_kref(opp_table
);
510 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
511 * @dev: device pointer used to lookup OPP table.
513 * Register the initial OPP table with the OPP library for given device.
517 * Duplicate OPPs (both freq and volt are same) and opp->available
518 * -EEXIST Freq are same and volt are different OR
519 * Duplicate OPPs (both freq and volt are same) and !opp->available
520 * -ENOMEM Memory allocation failure
521 * -ENODEV when 'operating-points' property is not found or is invalid data
523 * -ENODATA when empty 'operating-points' property is found
524 * -EINVAL when invalid entries are found in opp-v2 table
526 int dev_pm_opp_of_add_table(struct device
*dev
)
528 struct opp_table
*opp_table
;
531 opp_table
= dev_pm_opp_get_opp_table_indexed(dev
, 0);
536 * OPPs have two version of bindings now. Also try the old (v1)
537 * bindings for backward compatibility with older dtbs.
540 ret
= _of_add_opp_table_v2(dev
, opp_table
);
542 ret
= _of_add_opp_table_v1(dev
, opp_table
);
545 dev_pm_opp_put_opp_table(opp_table
);
549 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table
);
552 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
553 * @dev: device pointer used to lookup OPP table.
554 * @index: Index number.
556 * Register the initial OPP table with the OPP library for given device only
557 * using the "operating-points-v2" property.
561 * Duplicate OPPs (both freq and volt are same) and opp->available
562 * -EEXIST Freq are same and volt are different OR
563 * Duplicate OPPs (both freq and volt are same) and !opp->available
564 * -ENOMEM Memory allocation failure
565 * -ENODEV when 'operating-points' property is not found or is invalid data
567 * -ENODATA when empty 'operating-points' property is found
568 * -EINVAL when invalid entries are found in opp-v2 table
570 int dev_pm_opp_of_add_table_indexed(struct device
*dev
, int index
)
572 struct opp_table
*opp_table
;
577 * If only one phandle is present, then the same OPP table
578 * applies for all index requests.
580 count
= of_count_phandle_with_args(dev
->of_node
,
581 "operating-points-v2", NULL
);
586 opp_table
= dev_pm_opp_get_opp_table_indexed(dev
, index
);
590 ret
= _of_add_opp_table_v2(dev
, opp_table
);
592 dev_pm_opp_put_opp_table(opp_table
);
596 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed
);
598 /* CPU device specific helpers */
601 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
602 * @cpumask: cpumask for which OPP table needs to be removed
604 * This removes the OPP tables for CPUs present in the @cpumask.
605 * This should be used only to remove static entries created from DT.
607 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask
*cpumask
)
609 _dev_pm_opp_cpumask_remove_table(cpumask
, -1);
611 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table
);
614 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
615 * @cpumask: cpumask for which OPP table needs to be added.
617 * This adds the OPP tables for CPUs present in the @cpumask.
619 int dev_pm_opp_of_cpumask_add_table(const struct cpumask
*cpumask
)
621 struct device
*cpu_dev
;
624 if (WARN_ON(cpumask_empty(cpumask
)))
627 for_each_cpu(cpu
, cpumask
) {
628 cpu_dev
= get_cpu_device(cpu
);
630 pr_err("%s: failed to get cpu%d device\n", __func__
,
636 ret
= dev_pm_opp_of_add_table(cpu_dev
);
639 * OPP may get registered dynamically, don't print error
642 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
652 /* Free all other OPPs */
653 _dev_pm_opp_cpumask_remove_table(cpumask
, cpu
);
657 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table
);
660 * Works only for OPP v2 bindings.
662 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
665 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
666 * @cpu_dev using operating-points-v2
669 * @cpu_dev: CPU device for which we do this operation
670 * @cpumask: cpumask to update with information of sharing CPUs
672 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
674 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
676 int dev_pm_opp_of_get_sharing_cpus(struct device
*cpu_dev
,
677 struct cpumask
*cpumask
)
679 struct device_node
*np
, *tmp_np
, *cpu_np
;
682 /* Get OPP descriptor node */
683 np
= dev_pm_opp_of_get_opp_desc_node(cpu_dev
);
685 dev_dbg(cpu_dev
, "%s: Couldn't find opp node.\n", __func__
);
689 cpumask_set_cpu(cpu_dev
->id
, cpumask
);
691 /* OPPs are shared ? */
692 if (!of_property_read_bool(np
, "opp-shared"))
695 for_each_possible_cpu(cpu
) {
696 if (cpu
== cpu_dev
->id
)
699 cpu_np
= of_cpu_device_node_get(cpu
);
701 dev_err(cpu_dev
, "%s: failed to get cpu%d node\n",
707 /* Get OPP descriptor node */
708 tmp_np
= _opp_of_get_opp_desc_node(cpu_np
, 0);
711 pr_err("%pOF: Couldn't find opp node\n", cpu_np
);
716 /* CPUs are sharing opp node */
718 cpumask_set_cpu(cpu
, cpumask
);
727 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus
);
730 * of_dev_pm_opp_find_required_opp() - Search for required OPP.
731 * @dev: The device whose OPP node is referenced by the 'np' DT node.
732 * @np: Node that contains the "required-opps" property.
734 * Returns the OPP of the device 'dev', whose phandle is present in the "np"
735 * node. Although the "required-opps" property supports having multiple
736 * phandles, this helper routine only parses the very first phandle in the list.
738 * Return: Matching opp, else returns ERR_PTR in case of error and should be
739 * handled using IS_ERR.
741 * The callers are required to call dev_pm_opp_put() for the returned OPP after
744 struct dev_pm_opp
*of_dev_pm_opp_find_required_opp(struct device
*dev
,
745 struct device_node
*np
)
747 struct dev_pm_opp
*temp_opp
, *opp
= ERR_PTR(-ENODEV
);
748 struct device_node
*required_np
;
749 struct opp_table
*opp_table
;
751 opp_table
= _find_opp_table(dev
);
752 if (IS_ERR(opp_table
))
753 return ERR_CAST(opp_table
);
755 required_np
= of_parse_phandle(np
, "required-opps", 0);
756 if (unlikely(!required_np
)) {
757 dev_err(dev
, "Unable to parse required-opps\n");
761 mutex_lock(&opp_table
->lock
);
763 list_for_each_entry(temp_opp
, &opp_table
->opp_list
, node
) {
764 if (temp_opp
->available
&& temp_opp
->np
== required_np
) {
767 /* Increment the reference count of OPP */
773 mutex_unlock(&opp_table
->lock
);
775 of_node_put(required_np
);
777 dev_pm_opp_put_opp_table(opp_table
);
781 EXPORT_SYMBOL_GPL(of_dev_pm_opp_find_required_opp
);
784 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
785 * @opp: opp for which DT node has to be returned for
787 * Return: DT node corresponding to the opp, else 0 on success.
789 * The caller needs to put the node with of_node_put() after using it.
791 struct device_node
*dev_pm_opp_get_of_node(struct dev_pm_opp
*opp
)
793 if (IS_ERR_OR_NULL(opp
)) {
794 pr_err("%s: Invalid parameters\n", __func__
);
798 return of_node_get(opp
->np
);
800 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node
);