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 static struct opp_table *_managed_opp(const struct device_node *np)
46 struct opp_table *opp_table, *managed_table = NULL;
48 mutex_lock(&opp_table_lock);
50 list_for_each_entry(opp_table, &opp_tables, node) {
51 if (opp_table->np == np) {
53 * Multiple devices can point to the same OPP table and
54 * so will have same node-pointer, np.
56 * But the OPPs will be considered as shared only if the
57 * OPP table contains a "opp-shared" property.
59 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
60 _get_opp_table_kref(opp_table);
61 managed_table = opp_table;
68 mutex_unlock(&opp_table_lock);
73 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
76 struct device_node *np, *opp_np;
80 * Only required for backward compatibility with v1 bindings, but isn't
81 * harmful for other cases. And so we do it unconditionally.
83 np = of_node_get(dev->of_node);
87 if (!of_property_read_u32(np, "clock-latency", &val))
88 opp_table->clock_latency_ns_max = val;
89 of_property_read_u32(np, "voltage-tolerance",
90 &opp_table->voltage_tolerance_v1);
92 /* Get OPP table node */
93 opp_np = _opp_of_get_opp_desc_node(np, index);
99 if (of_property_read_bool(opp_np, "opp-shared"))
100 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
102 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
104 opp_table->np = opp_np;
109 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
110 struct device_node *np)
112 unsigned int count = opp_table->supported_hw_count;
116 if (!opp_table->supported_hw) {
118 * In the case that no supported_hw has been set by the
119 * platform but there is an opp-supported-hw value set for
120 * an OPP then the OPP should not be enabled as there is
121 * no way to see if the hardware supports it.
123 if (of_find_property(np, "opp-supported-hw", NULL))
130 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
133 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
134 __func__, count, ret);
138 /* Both of these are bitwise masks of the versions */
139 if (!(version & opp_table->supported_hw[count]))
146 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
147 struct opp_table *opp_table)
149 u32 *microvolt, *microamp = NULL;
150 int supplies, vcount, icount, ret, i, j;
151 struct property *prop = NULL;
154 supplies = opp_table->regulator_count ? opp_table->regulator_count : 1;
156 /* Search for "opp-microvolt-<name>" */
157 if (opp_table->prop_name) {
158 snprintf(name, sizeof(name), "opp-microvolt-%s",
159 opp_table->prop_name);
160 prop = of_find_property(opp->np, name, NULL);
164 /* Search for "opp-microvolt" */
165 sprintf(name, "opp-microvolt");
166 prop = of_find_property(opp->np, name, NULL);
168 /* Missing property isn't a problem, but an invalid entry is */
170 if (!opp_table->regulator_count)
173 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
179 vcount = of_property_count_u32_elems(opp->np, name);
181 dev_err(dev, "%s: Invalid %s property (%d)\n",
182 __func__, name, vcount);
186 /* There can be one or three elements per supply */
187 if (vcount != supplies && vcount != supplies * 3) {
188 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
189 __func__, name, vcount, supplies);
193 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
197 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
199 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
204 /* Search for "opp-microamp-<name>" */
206 if (opp_table->prop_name) {
207 snprintf(name, sizeof(name), "opp-microamp-%s",
208 opp_table->prop_name);
209 prop = of_find_property(opp->np, name, NULL);
213 /* Search for "opp-microamp" */
214 sprintf(name, "opp-microamp");
215 prop = of_find_property(opp->np, name, NULL);
219 icount = of_property_count_u32_elems(opp->np, name);
221 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
227 if (icount != supplies) {
228 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
229 __func__, name, icount, supplies);
234 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
240 ret = of_property_read_u32_array(opp->np, name, microamp,
243 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
250 for (i = 0, j = 0; i < supplies; i++) {
251 opp->supplies[i].u_volt = microvolt[j++];
253 if (vcount == supplies) {
254 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
255 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
257 opp->supplies[i].u_volt_min = microvolt[j++];
258 opp->supplies[i].u_volt_max = microvolt[j++];
262 opp->supplies[i].u_amp = microamp[i];
274 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
276 * @dev: device pointer used to lookup OPP table.
278 * Free OPPs created using static entries present in DT.
280 void dev_pm_opp_of_remove_table(struct device *dev)
282 _dev_pm_opp_find_and_remove_table(dev);
284 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
287 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
288 * @opp_table: OPP table
289 * @dev: device for which we do this operation
292 * This function adds an opp definition to the opp table and returns status. The
293 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
294 * removed by dev_pm_opp_remove.
298 * Duplicate OPPs (both freq and volt are same) and opp->available
299 * -EEXIST Freq are same and volt are different OR
300 * Duplicate OPPs (both freq and volt are same) and !opp->available
301 * -ENOMEM Memory allocation failure
302 * -EINVAL Failed parsing the OPP node
304 static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev,
305 struct device_node *np)
307 struct dev_pm_opp *new_opp;
311 bool rate_not_available = false;
313 new_opp = _opp_allocate(opp_table);
317 ret = of_property_read_u64(np, "opp-hz", &rate);
319 /* "opp-hz" is optional for devices like power domains. */
320 if (!of_find_property(dev->of_node, "#power-domain-cells",
322 dev_err(dev, "%s: opp-hz not found\n", __func__);
326 rate_not_available = true;
329 * Rate is defined as an unsigned long in clk API, and so
330 * casting explicitly to its type. Must be fixed once rate is 64
331 * bit guaranteed in clk API.
333 new_opp->rate = (unsigned long)rate;
336 /* Check if the OPP supports hardware's hierarchy of versions or not */
337 if (!_opp_is_supported(dev, opp_table, np)) {
338 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
342 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
345 new_opp->dynamic = false;
346 new_opp->available = true;
348 if (!of_property_read_u32(np, "clock-latency-ns", &val))
349 new_opp->clock_latency_ns = val;
351 new_opp->pstate = of_genpd_opp_to_performance_state(dev, np);
353 ret = opp_parse_supplies(new_opp, dev, opp_table);
357 ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
359 /* Don't return error for duplicate OPPs */
365 /* OPP to select on device suspend */
366 if (of_property_read_bool(np, "opp-suspend")) {
367 if (opp_table->suspend_opp) {
368 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
369 __func__, opp_table->suspend_opp->rate,
372 new_opp->suspend = true;
373 opp_table->suspend_opp = new_opp;
377 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
378 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
380 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
381 __func__, new_opp->turbo, new_opp->rate,
382 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
383 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
386 * Notify the changes in the availability of the operable
387 * frequency/voltage list.
389 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
398 /* Initializes OPP tables based on new bindings */
399 static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np,
402 struct device_node *np;
403 struct opp_table *opp_table;
404 int ret = 0, count = 0, pstate_count = 0;
405 struct dev_pm_opp *opp;
407 opp_table = _managed_opp(opp_np);
409 /* OPPs are already managed */
410 if (!_add_opp_dev(dev, opp_table))
412 else if (!opp_table->parsed_static_opps)
413 goto initialize_static_opps;
415 kref_get(&opp_table->list_kref);
420 opp_table = dev_pm_opp_get_opp_table_indexed(dev, index);
424 initialize_static_opps:
425 kref_init(&opp_table->list_kref);
427 /* We have opp-table node now, iterate over it and add OPPs */
428 for_each_available_child_of_node(opp_np, np) {
431 ret = _opp_add_static_v2(opp_table, dev, np);
433 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
435 _dev_pm_opp_remove_table(opp_table, dev);
441 /* There should be one of more OPP defined */
442 if (WARN_ON(!count)) {
444 _put_opp_list_kref(opp_table);
448 list_for_each_entry(opp, &opp_table->opp_list, node)
449 pstate_count += !!opp->pstate;
451 /* Either all or none of the nodes shall have performance state set */
452 if (pstate_count && pstate_count != count) {
453 dev_err(dev, "Not all nodes have performance state set (%d: %d)\n",
454 count, pstate_count);
456 _dev_pm_opp_remove_table(opp_table, dev);
461 opp_table->genpd_performance_state = true;
463 opp_table->parsed_static_opps = true;
466 dev_pm_opp_put_opp_table(opp_table);
471 /* Initializes OPP tables based on old-deprecated bindings */
472 static int _of_add_opp_table_v1(struct device *dev)
474 struct opp_table *opp_table;
475 const struct property *prop;
479 prop = of_find_property(dev->of_node, "operating-points", NULL);
486 * Each OPP is a set of tuples consisting of frequency and
487 * voltage like <freq-kHz vol-uV>.
489 nr = prop->length / sizeof(u32);
491 dev_err(dev, "%s: Invalid OPP table\n", __func__);
495 opp_table = dev_pm_opp_get_opp_table(dev);
499 kref_init(&opp_table->list_kref);
503 unsigned long freq = be32_to_cpup(val++) * 1000;
504 unsigned long volt = be32_to_cpup(val++);
506 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
508 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
509 __func__, freq, ret);
510 _dev_pm_opp_remove_table(opp_table, dev);
516 dev_pm_opp_put_opp_table(opp_table);
521 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
522 * @dev: device pointer used to lookup OPP table.
524 * Register the initial OPP table with the OPP library for given device.
528 * Duplicate OPPs (both freq and volt are same) and opp->available
529 * -EEXIST Freq are same and volt are different OR
530 * Duplicate OPPs (both freq and volt are same) and !opp->available
531 * -ENOMEM Memory allocation failure
532 * -ENODEV when 'operating-points' property is not found or is invalid data
534 * -ENODATA when empty 'operating-points' property is found
535 * -EINVAL when invalid entries are found in opp-v2 table
537 int dev_pm_opp_of_add_table(struct device *dev)
539 struct device_node *opp_np;
543 * OPPs have two version of bindings now. The older one is deprecated,
544 * try for the new binding first.
546 opp_np = dev_pm_opp_of_get_opp_desc_node(dev);
549 * Try old-deprecated bindings for backward compatibility with
552 return _of_add_opp_table_v1(dev);
555 ret = _of_add_opp_table_v2(dev, opp_np, 0);
560 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
563 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
564 * @dev: device pointer used to lookup OPP table.
565 * @index: Index number.
567 * Register the initial OPP table with the OPP library for given device only
568 * using the "operating-points-v2" property.
572 * Duplicate OPPs (both freq and volt are same) and opp->available
573 * -EEXIST Freq are same and volt are different OR
574 * Duplicate OPPs (both freq and volt are same) and !opp->available
575 * -ENOMEM Memory allocation failure
576 * -ENODEV when 'operating-points' property is not found or is invalid data
578 * -ENODATA when empty 'operating-points' property is found
579 * -EINVAL when invalid entries are found in opp-v2 table
581 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
583 struct device_node *opp_np;
587 opp_np = _opp_of_get_opp_desc_node(dev->of_node, index);
590 * If only one phandle is present, then the same OPP table
591 * applies for all index requests.
593 count = of_count_phandle_with_args(dev->of_node,
594 "operating-points-v2", NULL);
595 if (count == 1 && index) {
603 ret = _of_add_opp_table_v2(dev, opp_np, index);
608 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
610 /* CPU device specific helpers */
613 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
614 * @cpumask: cpumask for which OPP table needs to be removed
616 * This removes the OPP tables for CPUs present in the @cpumask.
617 * This should be used only to remove static entries created from DT.
619 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
621 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
623 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
626 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
627 * @cpumask: cpumask for which OPP table needs to be added.
629 * This adds the OPP tables for CPUs present in the @cpumask.
631 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
633 struct device *cpu_dev;
636 WARN_ON(cpumask_empty(cpumask));
638 for_each_cpu(cpu, cpumask) {
639 cpu_dev = get_cpu_device(cpu);
641 pr_err("%s: failed to get cpu%d device\n", __func__,
646 ret = dev_pm_opp_of_add_table(cpu_dev);
649 * OPP may get registered dynamically, don't print error
652 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
655 /* Free all other OPPs */
656 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
663 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
666 * Works only for OPP v2 bindings.
668 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
671 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
672 * @cpu_dev using operating-points-v2
675 * @cpu_dev: CPU device for which we do this operation
676 * @cpumask: cpumask to update with information of sharing CPUs
678 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
680 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
682 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
683 struct cpumask *cpumask)
685 struct device_node *np, *tmp_np, *cpu_np;
688 /* Get OPP descriptor node */
689 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
691 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
695 cpumask_set_cpu(cpu_dev->id, cpumask);
697 /* OPPs are shared ? */
698 if (!of_property_read_bool(np, "opp-shared"))
701 for_each_possible_cpu(cpu) {
702 if (cpu == cpu_dev->id)
705 cpu_np = of_cpu_device_node_get(cpu);
707 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
713 /* Get OPP descriptor node */
714 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
717 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
722 /* CPUs are sharing opp node */
724 cpumask_set_cpu(cpu, cpumask);
733 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
736 * of_dev_pm_opp_find_required_opp() - Search for required OPP.
737 * @dev: The device whose OPP node is referenced by the 'np' DT node.
738 * @np: Node that contains the "required-opps" property.
740 * Returns the OPP of the device 'dev', whose phandle is present in the "np"
741 * node. Although the "required-opps" property supports having multiple
742 * phandles, this helper routine only parses the very first phandle in the list.
744 * Return: Matching opp, else returns ERR_PTR in case of error and should be
745 * handled using IS_ERR.
747 * The callers are required to call dev_pm_opp_put() for the returned OPP after
750 struct dev_pm_opp *of_dev_pm_opp_find_required_opp(struct device *dev,
751 struct device_node *np)
753 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ENODEV);
754 struct device_node *required_np;
755 struct opp_table *opp_table;
757 opp_table = _find_opp_table(dev);
758 if (IS_ERR(opp_table))
759 return ERR_CAST(opp_table);
761 required_np = of_parse_phandle(np, "required-opps", 0);
762 if (unlikely(!required_np)) {
763 dev_err(dev, "Unable to parse required-opps\n");
767 mutex_lock(&opp_table->lock);
769 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
770 if (temp_opp->available && temp_opp->np == required_np) {
773 /* Increment the reference count of OPP */
779 mutex_unlock(&opp_table->lock);
781 of_node_put(required_np);
783 dev_pm_opp_put_opp_table(opp_table);
787 EXPORT_SYMBOL_GPL(of_dev_pm_opp_find_required_opp);
790 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
791 * @opp: opp for which DT node has to be returned for
793 * Return: DT node corresponding to the opp, else 0 on success.
795 * The caller needs to put the node with of_node_put() after using it.
797 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
799 if (IS_ERR_OR_NULL(opp)) {
800 pr_err("%s: Invalid parameters\n", __func__);
804 return of_node_get(opp->np);
806 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);