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>
26 static struct opp_table *_managed_opp(const struct device_node *np)
28 struct opp_table *opp_table, *managed_table = NULL;
30 mutex_lock(&opp_table_lock);
32 list_for_each_entry(opp_table, &opp_tables, node) {
33 if (opp_table->np == np) {
35 * Multiple devices can point to the same OPP table and
36 * so will have same node-pointer, np.
38 * But the OPPs will be considered as shared only if the
39 * OPP table contains a "opp-shared" property.
41 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
42 _get_opp_table_kref(opp_table);
43 managed_table = opp_table;
50 mutex_unlock(&opp_table_lock);
55 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
58 struct device_node *np;
61 * Only required for backward compatibility with v1 bindings, but isn't
62 * harmful for other cases. And so we do it unconditionally.
64 np = of_node_get(dev->of_node);
68 if (!of_property_read_u32(np, "clock-latency", &val))
69 opp_table->clock_latency_ns_max = val;
70 of_property_read_u32(np, "voltage-tolerance",
71 &opp_table->voltage_tolerance_v1);
76 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
77 struct device_node *np)
79 unsigned int count = opp_table->supported_hw_count;
83 if (!opp_table->supported_hw) {
85 * In the case that no supported_hw has been set by the
86 * platform but there is an opp-supported-hw value set for
87 * an OPP then the OPP should not be enabled as there is
88 * no way to see if the hardware supports it.
90 if (of_find_property(np, "opp-supported-hw", NULL))
97 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
100 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
101 __func__, count, ret);
105 /* Both of these are bitwise masks of the versions */
106 if (!(version & opp_table->supported_hw[count]))
113 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
114 struct opp_table *opp_table)
116 u32 *microvolt, *microamp = NULL;
117 int supplies, vcount, icount, ret, i, j;
118 struct property *prop = NULL;
121 supplies = opp_table->regulator_count ? opp_table->regulator_count : 1;
123 /* Search for "opp-microvolt-<name>" */
124 if (opp_table->prop_name) {
125 snprintf(name, sizeof(name), "opp-microvolt-%s",
126 opp_table->prop_name);
127 prop = of_find_property(opp->np, name, NULL);
131 /* Search for "opp-microvolt" */
132 sprintf(name, "opp-microvolt");
133 prop = of_find_property(opp->np, name, NULL);
135 /* Missing property isn't a problem, but an invalid entry is */
137 if (!opp_table->regulator_count)
140 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
146 vcount = of_property_count_u32_elems(opp->np, name);
148 dev_err(dev, "%s: Invalid %s property (%d)\n",
149 __func__, name, vcount);
153 /* There can be one or three elements per supply */
154 if (vcount != supplies && vcount != supplies * 3) {
155 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
156 __func__, name, vcount, supplies);
160 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
164 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
166 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
171 /* Search for "opp-microamp-<name>" */
173 if (opp_table->prop_name) {
174 snprintf(name, sizeof(name), "opp-microamp-%s",
175 opp_table->prop_name);
176 prop = of_find_property(opp->np, name, NULL);
180 /* Search for "opp-microamp" */
181 sprintf(name, "opp-microamp");
182 prop = of_find_property(opp->np, name, NULL);
186 icount = of_property_count_u32_elems(opp->np, name);
188 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
194 if (icount != supplies) {
195 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
196 __func__, name, icount, supplies);
201 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
207 ret = of_property_read_u32_array(opp->np, name, microamp,
210 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
217 for (i = 0, j = 0; i < supplies; i++) {
218 opp->supplies[i].u_volt = microvolt[j++];
220 if (vcount == supplies) {
221 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
222 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
224 opp->supplies[i].u_volt_min = microvolt[j++];
225 opp->supplies[i].u_volt_max = microvolt[j++];
229 opp->supplies[i].u_amp = microamp[i];
241 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
243 * @dev: device pointer used to lookup OPP table.
245 * Free OPPs created using static entries present in DT.
247 void dev_pm_opp_of_remove_table(struct device *dev)
249 _dev_pm_opp_find_and_remove_table(dev, false);
251 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
253 /* Returns opp descriptor node for a device node, caller must
254 * do of_node_put() */
255 static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
258 /* "operating-points-v2" can be an array for power domain providers */
259 return of_parse_phandle(np, "operating-points-v2", index);
262 /* Returns opp descriptor node for a device, caller must do of_node_put() */
263 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
265 return _opp_of_get_opp_desc_node(dev->of_node, 0);
267 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
270 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
271 * @opp_table: OPP table
272 * @dev: device for which we do this operation
275 * This function adds an opp definition to the opp table and returns status. The
276 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
277 * removed by dev_pm_opp_remove.
281 * Duplicate OPPs (both freq and volt are same) and opp->available
282 * -EEXIST Freq are same and volt are different OR
283 * Duplicate OPPs (both freq and volt are same) and !opp->available
284 * -ENOMEM Memory allocation failure
285 * -EINVAL Failed parsing the OPP node
287 static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev,
288 struct device_node *np)
290 struct dev_pm_opp *new_opp;
294 bool rate_not_available = false;
296 new_opp = _opp_allocate(opp_table);
300 ret = of_property_read_u64(np, "opp-hz", &rate);
302 /* "opp-hz" is optional for devices like power domains. */
303 if (!of_find_property(dev->of_node, "#power-domain-cells",
305 dev_err(dev, "%s: opp-hz not found\n", __func__);
309 rate_not_available = true;
312 * Rate is defined as an unsigned long in clk API, and so
313 * casting explicitly to its type. Must be fixed once rate is 64
314 * bit guaranteed in clk API.
316 new_opp->rate = (unsigned long)rate;
319 /* Check if the OPP supports hardware's hierarchy of versions or not */
320 if (!_opp_is_supported(dev, opp_table, np)) {
321 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
325 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
328 new_opp->dynamic = false;
329 new_opp->available = true;
331 if (!of_property_read_u32(np, "clock-latency-ns", &val))
332 new_opp->clock_latency_ns = val;
334 new_opp->pstate = of_genpd_opp_to_performance_state(dev, np);
336 ret = opp_parse_supplies(new_opp, dev, opp_table);
340 ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
342 /* Don't return error for duplicate OPPs */
348 /* OPP to select on device suspend */
349 if (of_property_read_bool(np, "opp-suspend")) {
350 if (opp_table->suspend_opp) {
351 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
352 __func__, opp_table->suspend_opp->rate,
355 new_opp->suspend = true;
356 opp_table->suspend_opp = new_opp;
360 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
361 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
363 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
364 __func__, new_opp->turbo, new_opp->rate,
365 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
366 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
369 * Notify the changes in the availability of the operable
370 * frequency/voltage list.
372 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
381 /* Initializes OPP tables based on new bindings */
382 static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np,
385 struct device_node *np;
386 struct opp_table *opp_table;
387 int ret = 0, count = 0, pstate_count = 0;
388 struct dev_pm_opp *opp;
390 opp_table = _managed_opp(opp_np);
392 /* OPPs are already managed */
393 if (!_add_opp_dev(dev, opp_table))
398 opp_table = dev_pm_opp_get_opp_table_indexed(dev, index);
402 /* We have opp-table node now, iterate over it and add OPPs */
403 for_each_available_child_of_node(opp_np, np) {
406 ret = _opp_add_static_v2(opp_table, dev, np);
408 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
410 _dev_pm_opp_remove_table(opp_table, dev, false);
416 /* There should be one of more OPP defined */
417 if (WARN_ON(!count)) {
422 list_for_each_entry(opp, &opp_table->opp_list, node)
423 pstate_count += !!opp->pstate;
425 /* Either all or none of the nodes shall have performance state set */
426 if (pstate_count && pstate_count != count) {
427 dev_err(dev, "Not all nodes have performance state set (%d: %d)\n",
428 count, pstate_count);
430 _dev_pm_opp_remove_table(opp_table, dev, false);
435 opp_table->genpd_performance_state = true;
437 opp_table->np = opp_np;
438 if (of_property_read_bool(opp_np, "opp-shared"))
439 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
441 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
444 dev_pm_opp_put_opp_table(opp_table);
449 /* Initializes OPP tables based on old-deprecated bindings */
450 static int _of_add_opp_table_v1(struct device *dev)
452 struct opp_table *opp_table;
453 const struct property *prop;
457 prop = of_find_property(dev->of_node, "operating-points", NULL);
464 * Each OPP is a set of tuples consisting of frequency and
465 * voltage like <freq-kHz vol-uV>.
467 nr = prop->length / sizeof(u32);
469 dev_err(dev, "%s: Invalid OPP table\n", __func__);
473 opp_table = dev_pm_opp_get_opp_table(dev);
479 unsigned long freq = be32_to_cpup(val++) * 1000;
480 unsigned long volt = be32_to_cpup(val++);
482 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
484 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
485 __func__, freq, ret);
486 _dev_pm_opp_remove_table(opp_table, dev, false);
492 dev_pm_opp_put_opp_table(opp_table);
497 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
498 * @dev: device pointer used to lookup OPP table.
500 * Register the initial OPP table with the OPP library for given device.
504 * Duplicate OPPs (both freq and volt are same) and opp->available
505 * -EEXIST Freq are same and volt are different OR
506 * Duplicate OPPs (both freq and volt are same) and !opp->available
507 * -ENOMEM Memory allocation failure
508 * -ENODEV when 'operating-points' property is not found or is invalid data
510 * -ENODATA when empty 'operating-points' property is found
511 * -EINVAL when invalid entries are found in opp-v2 table
513 int dev_pm_opp_of_add_table(struct device *dev)
515 struct device_node *opp_np;
519 * OPPs have two version of bindings now. The older one is deprecated,
520 * try for the new binding first.
522 opp_np = dev_pm_opp_of_get_opp_desc_node(dev);
525 * Try old-deprecated bindings for backward compatibility with
528 return _of_add_opp_table_v1(dev);
531 ret = _of_add_opp_table_v2(dev, opp_np, 0);
536 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
539 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
540 * @dev: device pointer used to lookup OPP table.
541 * @index: Index number.
543 * Register the initial OPP table with the OPP library for given device only
544 * using the "operating-points-v2" property.
548 * Duplicate OPPs (both freq and volt are same) and opp->available
549 * -EEXIST Freq are same and volt are different OR
550 * Duplicate OPPs (both freq and volt are same) and !opp->available
551 * -ENOMEM Memory allocation failure
552 * -ENODEV when 'operating-points' property is not found or is invalid data
554 * -ENODATA when empty 'operating-points' property is found
555 * -EINVAL when invalid entries are found in opp-v2 table
557 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
559 struct device_node *opp_np;
563 opp_np = _opp_of_get_opp_desc_node(dev->of_node, index);
566 * If only one phandle is present, then the same OPP table
567 * applies for all index requests.
569 count = of_count_phandle_with_args(dev->of_node,
570 "operating-points-v2", NULL);
571 if (count == 1 && index) {
579 ret = _of_add_opp_table_v2(dev, opp_np, index);
584 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
586 /* CPU device specific helpers */
589 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
590 * @cpumask: cpumask for which OPP table needs to be removed
592 * This removes the OPP tables for CPUs present in the @cpumask.
593 * This should be used only to remove static entries created from DT.
595 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
597 _dev_pm_opp_cpumask_remove_table(cpumask, true, -1);
599 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
602 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
603 * @cpumask: cpumask for which OPP table needs to be added.
605 * This adds the OPP tables for CPUs present in the @cpumask.
607 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
609 struct device *cpu_dev;
612 WARN_ON(cpumask_empty(cpumask));
614 for_each_cpu(cpu, cpumask) {
615 cpu_dev = get_cpu_device(cpu);
617 pr_err("%s: failed to get cpu%d device\n", __func__,
622 ret = dev_pm_opp_of_add_table(cpu_dev);
625 * OPP may get registered dynamically, don't print error
628 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
631 /* Free all other OPPs */
632 _dev_pm_opp_cpumask_remove_table(cpumask, true, cpu);
639 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
642 * Works only for OPP v2 bindings.
644 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
647 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
648 * @cpu_dev using operating-points-v2
651 * @cpu_dev: CPU device for which we do this operation
652 * @cpumask: cpumask to update with information of sharing CPUs
654 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
656 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
658 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
659 struct cpumask *cpumask)
661 struct device_node *np, *tmp_np, *cpu_np;
664 /* Get OPP descriptor node */
665 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
667 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
671 cpumask_set_cpu(cpu_dev->id, cpumask);
673 /* OPPs are shared ? */
674 if (!of_property_read_bool(np, "opp-shared"))
677 for_each_possible_cpu(cpu) {
678 if (cpu == cpu_dev->id)
681 cpu_np = of_cpu_device_node_get(cpu);
683 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
689 /* Get OPP descriptor node */
690 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
693 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
698 /* CPUs are sharing opp node */
700 cpumask_set_cpu(cpu, cpumask);
709 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
712 * of_dev_pm_opp_find_required_opp() - Search for required OPP.
713 * @dev: The device whose OPP node is referenced by the 'np' DT node.
714 * @np: Node that contains the "required-opps" property.
716 * Returns the OPP of the device 'dev', whose phandle is present in the "np"
717 * node. Although the "required-opps" property supports having multiple
718 * phandles, this helper routine only parses the very first phandle in the list.
720 * Return: Matching opp, else returns ERR_PTR in case of error and should be
721 * handled using IS_ERR.
723 * The callers are required to call dev_pm_opp_put() for the returned OPP after
726 struct dev_pm_opp *of_dev_pm_opp_find_required_opp(struct device *dev,
727 struct device_node *np)
729 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ENODEV);
730 struct device_node *required_np;
731 struct opp_table *opp_table;
733 opp_table = _find_opp_table(dev);
734 if (IS_ERR(opp_table))
735 return ERR_CAST(opp_table);
737 required_np = of_parse_phandle(np, "required-opps", 0);
738 if (unlikely(!required_np)) {
739 dev_err(dev, "Unable to parse required-opps\n");
743 mutex_lock(&opp_table->lock);
745 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
746 if (temp_opp->available && temp_opp->np == required_np) {
749 /* Increment the reference count of OPP */
755 mutex_unlock(&opp_table->lock);
757 of_node_put(required_np);
759 dev_pm_opp_put_opp_table(opp_table);
763 EXPORT_SYMBOL_GPL(of_dev_pm_opp_find_required_opp);
766 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
767 * @opp: opp for which DT node has to be returned for
769 * Return: DT node corresponding to the opp, else 0 on success.
771 * The caller needs to put the node with of_node_put() after using it.
773 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
775 if (IS_ERR_OR_NULL(opp)) {
776 pr_err("%s: Invalid parameters\n", __func__);
780 return of_node_get(opp->np);
782 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);