1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic OPP Interface
5 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/clk.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/device.h>
17 #include <linux/export.h>
18 #include <linux/pm_domain.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/xarray.h>
26 * The root of the list of all opp-tables. All opp_table structures branch off
27 * from here, with each opp_table containing the list of opps it supports in
28 * various states of availability.
30 LIST_HEAD(opp_tables);
32 /* Lock to allow exclusive modification to the device and opp lists */
33 DEFINE_MUTEX(opp_table_lock);
34 /* Flag indicating that opp_tables list is being updated at the moment */
35 static bool opp_tables_busy;
37 /* OPP ID allocator */
38 static DEFINE_XARRAY_ALLOC1(opp_configs);
40 static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
42 struct opp_device *opp_dev;
45 mutex_lock(&opp_table->lock);
46 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
47 if (opp_dev->dev == dev) {
52 mutex_unlock(&opp_table->lock);
56 static struct opp_table *_find_opp_table_unlocked(struct device *dev)
58 struct opp_table *opp_table;
60 list_for_each_entry(opp_table, &opp_tables, node) {
61 if (_find_opp_dev(dev, opp_table)) {
62 _get_opp_table_kref(opp_table);
67 return ERR_PTR(-ENODEV);
71 * _find_opp_table() - find opp_table struct using device pointer
72 * @dev: device pointer used to lookup OPP table
74 * Search OPP table for one containing matching device.
76 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
77 * -EINVAL based on type of error.
79 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
81 struct opp_table *_find_opp_table(struct device *dev)
83 struct opp_table *opp_table;
85 if (IS_ERR_OR_NULL(dev)) {
86 pr_err("%s: Invalid parameters\n", __func__);
87 return ERR_PTR(-EINVAL);
90 mutex_lock(&opp_table_lock);
91 opp_table = _find_opp_table_unlocked(dev);
92 mutex_unlock(&opp_table_lock);
98 * Returns true if multiple clocks aren't there, else returns false with WARN.
100 * We don't force clk_count == 1 here as there are users who don't have a clock
101 * representation in the OPP table and manage the clock configuration themselves
102 * in an platform specific way.
104 static bool assert_single_clk(struct opp_table *opp_table)
106 return !WARN_ON(opp_table->clk_count > 1);
110 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
111 * @opp: opp for which voltage has to be returned for
113 * Return: voltage in micro volt corresponding to the opp, else
116 * This is useful only for devices with single power supply.
118 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
120 if (IS_ERR_OR_NULL(opp)) {
121 pr_err("%s: Invalid parameters\n", __func__);
125 return opp->supplies[0].u_volt;
127 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
130 * dev_pm_opp_get_supplies() - Gets the supply information corresponding to an opp
131 * @opp: opp for which voltage has to be returned for
132 * @supplies: Placeholder for copying the supply information.
134 * Return: negative error number on failure, 0 otherwise on success after
137 * This can be used for devices with any number of power supplies. The caller
138 * must ensure the @supplies array must contain space for each regulator.
140 int dev_pm_opp_get_supplies(struct dev_pm_opp *opp,
141 struct dev_pm_opp_supply *supplies)
143 if (IS_ERR_OR_NULL(opp) || !supplies) {
144 pr_err("%s: Invalid parameters\n", __func__);
148 memcpy(supplies, opp->supplies,
149 sizeof(*supplies) * opp->opp_table->regulator_count);
152 EXPORT_SYMBOL_GPL(dev_pm_opp_get_supplies);
155 * dev_pm_opp_get_power() - Gets the power corresponding to an opp
156 * @opp: opp for which power has to be returned for
158 * Return: power in micro watt corresponding to the opp, else
161 * This is useful only for devices with single power supply.
163 unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
165 unsigned long opp_power = 0;
168 if (IS_ERR_OR_NULL(opp)) {
169 pr_err("%s: Invalid parameters\n", __func__);
172 for (i = 0; i < opp->opp_table->regulator_count; i++)
173 opp_power += opp->supplies[i].u_watt;
177 EXPORT_SYMBOL_GPL(dev_pm_opp_get_power);
180 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
181 * @opp: opp for which frequency has to be returned for
183 * Return: frequency in hertz corresponding to the opp, else
186 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
188 if (IS_ERR_OR_NULL(opp)) {
189 pr_err("%s: Invalid parameters\n", __func__);
193 if (!assert_single_clk(opp->opp_table))
196 return opp->rates[0];
198 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
201 * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
202 * @opp: opp for which level value has to be returned for
204 * Return: level read from device tree corresponding to the opp, else
207 unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
209 if (IS_ERR_OR_NULL(opp) || !opp->available) {
210 pr_err("%s: Invalid parameters\n", __func__);
216 EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
219 * dev_pm_opp_get_required_pstate() - Gets the required performance state
220 * corresponding to an available opp
221 * @opp: opp for which performance state has to be returned for
222 * @index: index of the required opp
224 * Return: performance state read from device tree corresponding to the
225 * required opp, else return 0.
227 unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
230 struct opp_table *opp_table = opp->opp_table;
232 if (IS_ERR_OR_NULL(opp) || !opp->available ||
233 index >= opp_table->required_opp_count) {
234 pr_err("%s: Invalid parameters\n", __func__);
238 /* required-opps not fully initialized yet */
239 if (lazy_linking_pending(opp_table))
242 /* The required OPP table must belong to a genpd */
243 if (unlikely(!opp_table->required_opp_tables[index]->is_genpd)) {
244 pr_err("%s: Performance state is only valid for genpds.\n", __func__);
248 return opp->required_opps[index]->level;
250 EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
253 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
254 * @opp: opp for which turbo mode is being verified
256 * Turbo OPPs are not for normal use, and can be enabled (under certain
257 * conditions) for short duration of times to finish high throughput work
258 * quickly. Running on them for longer times may overheat the chip.
260 * Return: true if opp is turbo opp, else false.
262 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
264 if (IS_ERR_OR_NULL(opp) || !opp->available) {
265 pr_err("%s: Invalid parameters\n", __func__);
271 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
274 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
275 * @dev: device for which we do this operation
277 * Return: This function returns the max clock latency in nanoseconds.
279 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
281 struct opp_table *opp_table;
282 unsigned long clock_latency_ns;
284 opp_table = _find_opp_table(dev);
285 if (IS_ERR(opp_table))
288 clock_latency_ns = opp_table->clock_latency_ns_max;
290 dev_pm_opp_put_opp_table(opp_table);
292 return clock_latency_ns;
294 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
297 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
298 * @dev: device for which we do this operation
300 * Return: This function returns the max voltage latency in nanoseconds.
302 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
304 struct opp_table *opp_table;
305 struct dev_pm_opp *opp;
306 struct regulator *reg;
307 unsigned long latency_ns = 0;
314 opp_table = _find_opp_table(dev);
315 if (IS_ERR(opp_table))
318 /* Regulator may not be required for the device */
319 if (!opp_table->regulators)
322 count = opp_table->regulator_count;
324 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
328 mutex_lock(&opp_table->lock);
330 for (i = 0; i < count; i++) {
334 list_for_each_entry(opp, &opp_table->opp_list, node) {
338 if (opp->supplies[i].u_volt_min < uV[i].min)
339 uV[i].min = opp->supplies[i].u_volt_min;
340 if (opp->supplies[i].u_volt_max > uV[i].max)
341 uV[i].max = opp->supplies[i].u_volt_max;
345 mutex_unlock(&opp_table->lock);
348 * The caller needs to ensure that opp_table (and hence the regulator)
349 * isn't freed, while we are executing this routine.
351 for (i = 0; i < count; i++) {
352 reg = opp_table->regulators[i];
353 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
355 latency_ns += ret * 1000;
360 dev_pm_opp_put_opp_table(opp_table);
364 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
367 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
369 * @dev: device for which we do this operation
371 * Return: This function returns the max transition latency, in nanoseconds, to
372 * switch from one OPP to other.
374 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
376 return dev_pm_opp_get_max_volt_latency(dev) +
377 dev_pm_opp_get_max_clock_latency(dev);
379 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
382 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
383 * @dev: device for which we do this operation
385 * Return: This function returns the frequency of the OPP marked as suspend_opp
386 * if one is available, else returns 0;
388 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
390 struct opp_table *opp_table;
391 unsigned long freq = 0;
393 opp_table = _find_opp_table(dev);
394 if (IS_ERR(opp_table))
397 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
398 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
400 dev_pm_opp_put_opp_table(opp_table);
404 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
406 int _get_opp_count(struct opp_table *opp_table)
408 struct dev_pm_opp *opp;
411 mutex_lock(&opp_table->lock);
413 list_for_each_entry(opp, &opp_table->opp_list, node) {
418 mutex_unlock(&opp_table->lock);
424 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
425 * @dev: device for which we do this operation
427 * Return: This function returns the number of available opps if there are any,
428 * else returns 0 if none or the corresponding error value.
430 int dev_pm_opp_get_opp_count(struct device *dev)
432 struct opp_table *opp_table;
435 opp_table = _find_opp_table(dev);
436 if (IS_ERR(opp_table)) {
437 count = PTR_ERR(opp_table);
438 dev_dbg(dev, "%s: OPP table not found (%d)\n",
443 count = _get_opp_count(opp_table);
444 dev_pm_opp_put_opp_table(opp_table);
448 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
450 /* Helpers to read keys */
451 static unsigned long _read_freq(struct dev_pm_opp *opp, int index)
453 return opp->rates[0];
456 static unsigned long _read_level(struct dev_pm_opp *opp, int index)
461 static unsigned long _read_bw(struct dev_pm_opp *opp, int index)
463 return opp->bandwidth[index].peak;
466 /* Generic comparison helpers */
467 static bool _compare_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
468 unsigned long opp_key, unsigned long key)
470 if (opp_key == key) {
478 static bool _compare_ceil(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
479 unsigned long opp_key, unsigned long key)
481 if (opp_key >= key) {
489 static bool _compare_floor(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
490 unsigned long opp_key, unsigned long key)
499 /* Generic key finding helpers */
500 static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table,
501 unsigned long *key, int index, bool available,
502 unsigned long (*read)(struct dev_pm_opp *opp, int index),
503 bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
504 unsigned long opp_key, unsigned long key),
505 bool (*assert)(struct opp_table *opp_table))
507 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
509 /* Assert that the requirement is met */
510 if (assert && !assert(opp_table))
511 return ERR_PTR(-EINVAL);
513 mutex_lock(&opp_table->lock);
515 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
516 if (temp_opp->available == available) {
517 if (compare(&opp, temp_opp, read(temp_opp, index), *key))
522 /* Increment the reference count of OPP */
524 *key = read(opp, index);
528 mutex_unlock(&opp_table->lock);
533 static struct dev_pm_opp *
534 _find_key(struct device *dev, unsigned long *key, int index, bool available,
535 unsigned long (*read)(struct dev_pm_opp *opp, int index),
536 bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
537 unsigned long opp_key, unsigned long key),
538 bool (*assert)(struct opp_table *opp_table))
540 struct opp_table *opp_table;
541 struct dev_pm_opp *opp;
543 opp_table = _find_opp_table(dev);
544 if (IS_ERR(opp_table)) {
545 dev_err(dev, "%s: OPP table not found (%ld)\n", __func__,
547 return ERR_CAST(opp_table);
550 opp = _opp_table_find_key(opp_table, key, index, available, read,
553 dev_pm_opp_put_opp_table(opp_table);
558 static struct dev_pm_opp *_find_key_exact(struct device *dev,
559 unsigned long key, int index, bool available,
560 unsigned long (*read)(struct dev_pm_opp *opp, int index),
561 bool (*assert)(struct opp_table *opp_table))
564 * The value of key will be updated here, but will be ignored as the
565 * caller doesn't need it.
567 return _find_key(dev, &key, index, available, read, _compare_exact,
571 static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table,
572 unsigned long *key, int index, bool available,
573 unsigned long (*read)(struct dev_pm_opp *opp, int index),
574 bool (*assert)(struct opp_table *opp_table))
576 return _opp_table_find_key(opp_table, key, index, available, read,
577 _compare_ceil, assert);
580 static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key,
581 int index, bool available,
582 unsigned long (*read)(struct dev_pm_opp *opp, int index),
583 bool (*assert)(struct opp_table *opp_table))
585 return _find_key(dev, key, index, available, read, _compare_ceil,
589 static struct dev_pm_opp *_find_key_floor(struct device *dev,
590 unsigned long *key, int index, bool available,
591 unsigned long (*read)(struct dev_pm_opp *opp, int index),
592 bool (*assert)(struct opp_table *opp_table))
594 return _find_key(dev, key, index, available, read, _compare_floor,
599 * dev_pm_opp_find_freq_exact() - search for an exact frequency
600 * @dev: device for which we do this operation
601 * @freq: frequency to search for
602 * @available: true/false - match for available opp
604 * Return: Searches for exact match in the opp table and returns pointer to the
605 * matching opp if found, else returns ERR_PTR in case of error and should
606 * be handled using IS_ERR. Error return values can be:
607 * EINVAL: for bad pointer
608 * ERANGE: no match found for search
609 * ENODEV: if device not found in list of registered devices
611 * Note: available is a modifier for the search. if available=true, then the
612 * match is for exact matching frequency and is available in the stored OPP
613 * table. if false, the match is for exact frequency which is not available.
615 * This provides a mechanism to enable an opp which is not available currently
616 * or the opposite as well.
618 * The callers are required to call dev_pm_opp_put() for the returned OPP after
621 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
622 unsigned long freq, bool available)
624 return _find_key_exact(dev, freq, 0, available, _read_freq,
627 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
629 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
632 return _opp_table_find_key_ceil(opp_table, freq, 0, true, _read_freq,
637 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
638 * @dev: device for which we do this operation
639 * @freq: Start frequency
641 * Search for the matching ceil *available* OPP from a starting freq
644 * Return: matching *opp and refreshes *freq accordingly, else returns
645 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
647 * EINVAL: for bad pointer
648 * ERANGE: no match found for search
649 * ENODEV: if device not found in list of registered devices
651 * The callers are required to call dev_pm_opp_put() for the returned OPP after
654 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
657 return _find_key_ceil(dev, freq, 0, true, _read_freq, assert_single_clk);
659 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
662 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
663 * @dev: device for which we do this operation
664 * @freq: Start frequency
666 * Search for the matching floor *available* OPP from a starting freq
669 * Return: matching *opp and refreshes *freq accordingly, else returns
670 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
672 * EINVAL: for bad pointer
673 * ERANGE: no match found for search
674 * ENODEV: if device not found in list of registered devices
676 * The callers are required to call dev_pm_opp_put() for the returned OPP after
679 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
682 return _find_key_floor(dev, freq, 0, true, _read_freq, assert_single_clk);
684 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
687 * dev_pm_opp_find_level_exact() - search for an exact level
688 * @dev: device for which we do this operation
689 * @level: level to search for
691 * Return: Searches for exact match in the opp table and returns pointer to the
692 * matching opp if found, else returns ERR_PTR in case of error and should
693 * be handled using IS_ERR. Error return values can be:
694 * EINVAL: for bad pointer
695 * ERANGE: no match found for search
696 * ENODEV: if device not found in list of registered devices
698 * The callers are required to call dev_pm_opp_put() for the returned OPP after
701 struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
704 return _find_key_exact(dev, level, 0, true, _read_level, NULL);
706 EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
709 * dev_pm_opp_find_level_ceil() - search for an rounded up level
710 * @dev: device for which we do this operation
711 * @level: level to search for
713 * Return: Searches for rounded up match in the opp table and returns pointer
714 * to the matching opp if found, else returns ERR_PTR in case of error and
715 * should be handled using IS_ERR. Error return values can be:
716 * EINVAL: for bad pointer
717 * ERANGE: no match found for search
718 * ENODEV: if device not found in list of registered devices
720 * The callers are required to call dev_pm_opp_put() for the returned OPP after
723 struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
726 unsigned long temp = *level;
727 struct dev_pm_opp *opp;
729 opp = _find_key_ceil(dev, &temp, 0, true, _read_level, NULL);
733 EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
736 * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth
737 * @dev: device for which we do this operation
738 * @bw: start bandwidth
739 * @index: which bandwidth to compare, in case of OPPs with several values
741 * Search for the matching floor *available* OPP from a starting bandwidth
744 * Return: matching *opp and refreshes *bw accordingly, else returns
745 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
747 * EINVAL: for bad pointer
748 * ERANGE: no match found for search
749 * ENODEV: if device not found in list of registered devices
751 * The callers are required to call dev_pm_opp_put() for the returned OPP after
754 struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, unsigned int *bw,
757 unsigned long temp = *bw;
758 struct dev_pm_opp *opp;
760 opp = _find_key_ceil(dev, &temp, index, true, _read_bw, NULL);
764 EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil);
767 * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth
768 * @dev: device for which we do this operation
769 * @bw: start bandwidth
770 * @index: which bandwidth to compare, in case of OPPs with several values
772 * Search for the matching floor *available* OPP from a starting bandwidth
775 * Return: matching *opp and refreshes *bw accordingly, else returns
776 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
778 * EINVAL: for bad pointer
779 * ERANGE: no match found for search
780 * ENODEV: if device not found in list of registered devices
782 * The callers are required to call dev_pm_opp_put() for the returned OPP after
785 struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
786 unsigned int *bw, int index)
788 unsigned long temp = *bw;
789 struct dev_pm_opp *opp;
791 opp = _find_key_floor(dev, &temp, index, true, _read_bw, NULL);
795 EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor);
797 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
798 struct dev_pm_opp_supply *supply)
802 /* Regulator not available for device */
804 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
809 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
810 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
812 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
813 supply->u_volt, supply->u_volt_max);
815 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
816 __func__, supply->u_volt_min, supply->u_volt,
817 supply->u_volt_max, ret);
823 _opp_config_clk_single(struct device *dev, struct opp_table *opp_table,
824 struct dev_pm_opp *opp, void *data, bool scaling_down)
826 unsigned long *target = data;
830 /* One of target and opp must be available */
834 freq = opp->rates[0];
840 ret = clk_set_rate(opp_table->clk, freq);
842 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
845 opp_table->rate_clk_single = freq;
852 * Simple implementation for configuring multiple clocks. Configure clocks in
853 * the order in which they are present in the array while scaling up.
855 int dev_pm_opp_config_clks_simple(struct device *dev,
856 struct opp_table *opp_table, struct dev_pm_opp *opp, void *data,
862 for (i = opp_table->clk_count - 1; i >= 0; i--) {
863 ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
865 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
871 for (i = 0; i < opp_table->clk_count; i++) {
872 ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
874 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
883 EXPORT_SYMBOL_GPL(dev_pm_opp_config_clks_simple);
885 static int _opp_config_regulator_single(struct device *dev,
886 struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
887 struct regulator **regulators, unsigned int count)
889 struct regulator *reg = regulators[0];
892 /* This function only supports single regulator per device */
893 if (WARN_ON(count > 1)) {
894 dev_err(dev, "multiple regulators are not supported\n");
898 ret = _set_opp_voltage(dev, reg, new_opp->supplies);
903 * Enable the regulator after setting its voltages, otherwise it breaks
904 * some boot-enabled regulators.
906 if (unlikely(!new_opp->opp_table->enabled)) {
907 ret = regulator_enable(reg);
909 dev_warn(dev, "Failed to enable regulator: %d", ret);
915 static int _set_opp_bw(const struct opp_table *opp_table,
916 struct dev_pm_opp *opp, struct device *dev)
921 if (!opp_table->paths)
924 for (i = 0; i < opp_table->path_count; i++) {
929 avg = opp->bandwidth[i].avg;
930 peak = opp->bandwidth[i].peak;
932 ret = icc_set_bw(opp_table->paths[i], avg, peak);
934 dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
935 opp ? "set" : "remove", i, ret);
943 static int _set_performance_state(struct device *dev, struct device *pd_dev,
944 struct dev_pm_opp *opp, int i)
946 unsigned int pstate = likely(opp) ? opp->required_opps[i]->level: 0;
952 ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
954 dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
955 dev_name(pd_dev), pstate, ret);
961 static int _opp_set_required_opps_generic(struct device *dev,
962 struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down)
964 dev_err(dev, "setting required-opps isn't supported for non-genpd devices\n");
968 static int _opp_set_required_opps_genpd(struct device *dev,
969 struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down)
971 struct device **genpd_virt_devs =
972 opp_table->genpd_virt_devs ? opp_table->genpd_virt_devs : &dev;
976 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
977 * after it is freed from another thread.
979 mutex_lock(&opp_table->genpd_virt_dev_lock);
981 /* Scaling up? Set required OPPs in normal order, else reverse */
983 for (i = 0; i < opp_table->required_opp_count; i++) {
984 ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i);
989 for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
990 ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i);
996 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1001 /* This is only called for PM domain for now */
1002 static int _set_required_opps(struct device *dev, struct opp_table *opp_table,
1003 struct dev_pm_opp *opp, bool up)
1005 /* required-opps not fully initialized yet */
1006 if (lazy_linking_pending(opp_table))
1009 if (opp_table->set_required_opps)
1010 return opp_table->set_required_opps(dev, opp_table, opp, up);
1015 /* Update set_required_opps handler */
1016 void _update_set_required_opps(struct opp_table *opp_table)
1019 if (opp_table->set_required_opps)
1022 /* All required OPPs will belong to genpd or none */
1023 if (opp_table->required_opp_tables[0]->is_genpd)
1024 opp_table->set_required_opps = _opp_set_required_opps_genpd;
1026 opp_table->set_required_opps = _opp_set_required_opps_generic;
1029 static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
1031 struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
1034 if (!IS_ERR(opp_table->clk)) {
1035 freq = clk_get_rate(opp_table->clk);
1036 opp = _find_freq_ceil(opp_table, &freq);
1040 * Unable to find the current OPP ? Pick the first from the list since
1041 * it is in ascending order, otherwise rest of the code will need to
1042 * make special checks to validate current_opp.
1045 mutex_lock(&opp_table->lock);
1046 opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
1047 dev_pm_opp_get(opp);
1048 mutex_unlock(&opp_table->lock);
1051 opp_table->current_opp = opp;
1054 static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
1058 if (!opp_table->enabled)
1062 * Some drivers need to support cases where some platforms may
1063 * have OPP table for the device, while others don't and
1064 * opp_set_rate() just needs to behave like clk_set_rate().
1066 if (!_get_opp_count(opp_table))
1069 ret = _set_opp_bw(opp_table, NULL, dev);
1073 if (opp_table->regulators)
1074 regulator_disable(opp_table->regulators[0]);
1076 ret = _set_required_opps(dev, opp_table, NULL, false);
1078 opp_table->enabled = false;
1082 static int _set_opp(struct device *dev, struct opp_table *opp_table,
1083 struct dev_pm_opp *opp, void *clk_data, bool forced)
1085 struct dev_pm_opp *old_opp;
1086 int scaling_down, ret;
1089 return _disable_opp_table(dev, opp_table);
1091 /* Find the currently set OPP if we don't know already */
1092 if (unlikely(!opp_table->current_opp))
1093 _find_current_opp(dev, opp_table);
1095 old_opp = opp_table->current_opp;
1097 /* Return early if nothing to do */
1098 if (!forced && old_opp == opp && opp_table->enabled) {
1099 dev_dbg_ratelimited(dev, "%s: OPPs are same, nothing to do\n", __func__);
1103 dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
1104 __func__, old_opp->rates[0], opp->rates[0], old_opp->level,
1105 opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1106 opp->bandwidth ? opp->bandwidth[0].peak : 0);
1108 scaling_down = _opp_compare_key(opp_table, old_opp, opp);
1109 if (scaling_down == -1)
1112 /* Scaling up? Configure required OPPs before frequency */
1113 if (!scaling_down) {
1114 ret = _set_required_opps(dev, opp_table, opp, true);
1116 dev_err(dev, "Failed to set required opps: %d\n", ret);
1120 ret = _set_opp_bw(opp_table, opp, dev);
1122 dev_err(dev, "Failed to set bw: %d\n", ret);
1126 if (opp_table->config_regulators) {
1127 ret = opp_table->config_regulators(dev, old_opp, opp,
1128 opp_table->regulators,
1129 opp_table->regulator_count);
1131 dev_err(dev, "Failed to set regulator voltages: %d\n",
1138 if (opp_table->config_clks) {
1139 ret = opp_table->config_clks(dev, opp_table, opp, clk_data, scaling_down);
1144 /* Scaling down? Configure required OPPs after frequency */
1146 if (opp_table->config_regulators) {
1147 ret = opp_table->config_regulators(dev, old_opp, opp,
1148 opp_table->regulators,
1149 opp_table->regulator_count);
1151 dev_err(dev, "Failed to set regulator voltages: %d\n",
1157 ret = _set_opp_bw(opp_table, opp, dev);
1159 dev_err(dev, "Failed to set bw: %d\n", ret);
1163 ret = _set_required_opps(dev, opp_table, opp, false);
1165 dev_err(dev, "Failed to set required opps: %d\n", ret);
1170 opp_table->enabled = true;
1171 dev_pm_opp_put(old_opp);
1173 /* Make sure current_opp doesn't get freed */
1174 dev_pm_opp_get(opp);
1175 opp_table->current_opp = opp;
1181 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1182 * @dev: device for which we do this operation
1183 * @target_freq: frequency to achieve
1185 * This configures the power-supplies to the levels specified by the OPP
1186 * corresponding to the target_freq, and programs the clock to a value <=
1187 * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1188 * provided by the opp, should have already rounded to the target OPP's
1191 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1193 struct opp_table *opp_table;
1194 unsigned long freq = 0, temp_freq;
1195 struct dev_pm_opp *opp = NULL;
1196 bool forced = false;
1199 opp_table = _find_opp_table(dev);
1200 if (IS_ERR(opp_table)) {
1201 dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1202 return PTR_ERR(opp_table);
1207 * For IO devices which require an OPP on some platforms/SoCs
1208 * while just needing to scale the clock on some others
1209 * we look for empty OPP tables with just a clock handle and
1210 * scale only the clk. This makes dev_pm_opp_set_rate()
1211 * equivalent to a clk_set_rate()
1213 if (!_get_opp_count(opp_table)) {
1214 ret = opp_table->config_clks(dev, opp_table, NULL,
1215 &target_freq, false);
1219 freq = clk_round_rate(opp_table->clk, target_freq);
1220 if ((long)freq <= 0)
1224 * The clock driver may support finer resolution of the
1225 * frequencies than the OPP table, don't update the frequency we
1226 * pass to clk_set_rate() here.
1229 opp = _find_freq_ceil(opp_table, &temp_freq);
1232 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1233 __func__, freq, ret);
1238 * An OPP entry specifies the highest frequency at which other
1239 * properties of the OPP entry apply. Even if the new OPP is
1240 * same as the old one, we may still reach here for a different
1241 * value of the frequency. In such a case, do not abort but
1242 * configure the hardware to the desired frequency forcefully.
1244 forced = opp_table->rate_clk_single != target_freq;
1247 ret = _set_opp(dev, opp_table, opp, &target_freq, forced);
1250 dev_pm_opp_put(opp);
1253 dev_pm_opp_put_opp_table(opp_table);
1256 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
1259 * dev_pm_opp_set_opp() - Configure device for OPP
1260 * @dev: device for which we do this operation
1261 * @opp: OPP to set to
1263 * This configures the device based on the properties of the OPP passed to this
1266 * Return: 0 on success, a negative error number otherwise.
1268 int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1270 struct opp_table *opp_table;
1273 opp_table = _find_opp_table(dev);
1274 if (IS_ERR(opp_table)) {
1275 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1276 return PTR_ERR(opp_table);
1279 ret = _set_opp(dev, opp_table, opp, NULL, false);
1280 dev_pm_opp_put_opp_table(opp_table);
1284 EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1286 /* OPP-dev Helpers */
1287 static void _remove_opp_dev(struct opp_device *opp_dev,
1288 struct opp_table *opp_table)
1290 opp_debug_unregister(opp_dev, opp_table);
1291 list_del(&opp_dev->node);
1295 struct opp_device *_add_opp_dev(const struct device *dev,
1296 struct opp_table *opp_table)
1298 struct opp_device *opp_dev;
1300 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
1304 /* Initialize opp-dev */
1307 mutex_lock(&opp_table->lock);
1308 list_add(&opp_dev->node, &opp_table->dev_list);
1309 mutex_unlock(&opp_table->lock);
1311 /* Create debugfs entries for the opp_table */
1312 opp_debug_register(opp_dev, opp_table);
1317 static struct opp_table *_allocate_opp_table(struct device *dev, int index)
1319 struct opp_table *opp_table;
1320 struct opp_device *opp_dev;
1324 * Allocate a new OPP table. In the infrequent case where a new
1325 * device is needed to be added, we pay this penalty.
1327 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
1329 return ERR_PTR(-ENOMEM);
1331 mutex_init(&opp_table->lock);
1332 mutex_init(&opp_table->genpd_virt_dev_lock);
1333 INIT_LIST_HEAD(&opp_table->dev_list);
1334 INIT_LIST_HEAD(&opp_table->lazy);
1336 opp_table->clk = ERR_PTR(-ENODEV);
1338 /* Mark regulator count uninitialized */
1339 opp_table->regulator_count = -1;
1341 opp_dev = _add_opp_dev(dev, opp_table);
1347 _of_init_opp_table(opp_table, dev, index);
1349 /* Find interconnect path(s) for the device */
1350 ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1352 if (ret == -EPROBE_DEFER)
1353 goto remove_opp_dev;
1355 dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
1359 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
1360 INIT_LIST_HEAD(&opp_table->opp_list);
1361 kref_init(&opp_table->kref);
1366 _of_clear_opp_table(opp_table);
1367 _remove_opp_dev(opp_dev, opp_table);
1368 mutex_destroy(&opp_table->genpd_virt_dev_lock);
1369 mutex_destroy(&opp_table->lock);
1372 return ERR_PTR(ret);
1375 void _get_opp_table_kref(struct opp_table *opp_table)
1377 kref_get(&opp_table->kref);
1380 static struct opp_table *_update_opp_table_clk(struct device *dev,
1381 struct opp_table *opp_table,
1387 * Return early if we don't need to get clk or we have already done it
1390 if (!getclk || IS_ERR(opp_table) || !IS_ERR(opp_table->clk) ||
1394 /* Find clk for the device */
1395 opp_table->clk = clk_get(dev, NULL);
1397 ret = PTR_ERR_OR_ZERO(opp_table->clk);
1399 opp_table->config_clks = _opp_config_clk_single;
1400 opp_table->clk_count = 1;
1404 if (ret == -ENOENT) {
1406 * There are few platforms which don't want the OPP core to
1407 * manage device's clock settings. In such cases neither the
1408 * platform provides the clks explicitly to us, nor the DT
1409 * contains a valid clk entry. The OPP nodes in DT may still
1410 * contain "opp-hz" property though, which we need to parse and
1411 * allow the platform to find an OPP based on freq later on.
1413 * This is a simple solution to take care of such corner cases,
1414 * i.e. make the clk_count 1, which lets us allocate space for
1415 * frequency in opp->rates and also parse the entries in DT.
1417 opp_table->clk_count = 1;
1419 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1423 dev_pm_opp_put_opp_table(opp_table);
1424 dev_err_probe(dev, ret, "Couldn't find clock\n");
1426 return ERR_PTR(ret);
1430 * We need to make sure that the OPP table for a device doesn't get added twice,
1431 * if this routine gets called in parallel with the same device pointer.
1433 * The simplest way to enforce that is to perform everything (find existing
1434 * table and if not found, create a new one) under the opp_table_lock, so only
1435 * one creator gets access to the same. But that expands the critical section
1436 * under the lock and may end up causing circular dependencies with frameworks
1437 * like debugfs, interconnect or clock framework as they may be direct or
1438 * indirect users of OPP core.
1440 * And for that reason we have to go for a bit tricky implementation here, which
1441 * uses the opp_tables_busy flag to indicate if another creator is in the middle
1442 * of adding an OPP table and others should wait for it to finish.
1444 struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
1447 struct opp_table *opp_table;
1450 mutex_lock(&opp_table_lock);
1452 opp_table = _find_opp_table_unlocked(dev);
1453 if (!IS_ERR(opp_table))
1457 * The opp_tables list or an OPP table's dev_list is getting updated by
1458 * another user, wait for it to finish.
1460 if (unlikely(opp_tables_busy)) {
1461 mutex_unlock(&opp_table_lock);
1466 opp_tables_busy = true;
1467 opp_table = _managed_opp(dev, index);
1469 /* Drop the lock to reduce the size of critical section */
1470 mutex_unlock(&opp_table_lock);
1473 if (!_add_opp_dev(dev, opp_table)) {
1474 dev_pm_opp_put_opp_table(opp_table);
1475 opp_table = ERR_PTR(-ENOMEM);
1478 mutex_lock(&opp_table_lock);
1480 opp_table = _allocate_opp_table(dev, index);
1482 mutex_lock(&opp_table_lock);
1483 if (!IS_ERR(opp_table))
1484 list_add(&opp_table->node, &opp_tables);
1487 opp_tables_busy = false;
1490 mutex_unlock(&opp_table_lock);
1492 return _update_opp_table_clk(dev, opp_table, getclk);
1495 static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1497 return _add_opp_table_indexed(dev, 0, getclk);
1500 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1502 return _find_opp_table(dev);
1504 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
1506 static void _opp_table_kref_release(struct kref *kref)
1508 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1509 struct opp_device *opp_dev, *temp;
1512 /* Drop the lock as soon as we can */
1513 list_del(&opp_table->node);
1514 mutex_unlock(&opp_table_lock);
1516 if (opp_table->current_opp)
1517 dev_pm_opp_put(opp_table->current_opp);
1519 _of_clear_opp_table(opp_table);
1521 /* Release automatically acquired single clk */
1522 if (!IS_ERR(opp_table->clk))
1523 clk_put(opp_table->clk);
1525 if (opp_table->paths) {
1526 for (i = 0; i < opp_table->path_count; i++)
1527 icc_put(opp_table->paths[i]);
1528 kfree(opp_table->paths);
1531 WARN_ON(!list_empty(&opp_table->opp_list));
1533 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node)
1534 _remove_opp_dev(opp_dev, opp_table);
1536 mutex_destroy(&opp_table->genpd_virt_dev_lock);
1537 mutex_destroy(&opp_table->lock);
1541 void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1543 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1546 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1548 void _opp_free(struct dev_pm_opp *opp)
1553 static void _opp_kref_release(struct kref *kref)
1555 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1556 struct opp_table *opp_table = opp->opp_table;
1558 list_del(&opp->node);
1559 mutex_unlock(&opp_table->lock);
1562 * Notify the changes in the availability of the operable
1563 * frequency/voltage list.
1565 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1566 _of_clear_opp(opp_table, opp);
1567 opp_debug_remove_one(opp);
1571 void dev_pm_opp_get(struct dev_pm_opp *opp)
1573 kref_get(&opp->kref);
1576 void dev_pm_opp_put(struct dev_pm_opp *opp)
1578 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
1580 EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1583 * dev_pm_opp_remove() - Remove an OPP from OPP table
1584 * @dev: device for which we do this operation
1585 * @freq: OPP to remove with matching 'freq'
1587 * This function removes an opp from the opp table.
1589 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1591 struct dev_pm_opp *opp = NULL, *iter;
1592 struct opp_table *opp_table;
1594 opp_table = _find_opp_table(dev);
1595 if (IS_ERR(opp_table))
1598 if (!assert_single_clk(opp_table))
1601 mutex_lock(&opp_table->lock);
1603 list_for_each_entry(iter, &opp_table->opp_list, node) {
1604 if (iter->rates[0] == freq) {
1610 mutex_unlock(&opp_table->lock);
1613 dev_pm_opp_put(opp);
1615 /* Drop the reference taken by dev_pm_opp_add() */
1616 dev_pm_opp_put_opp_table(opp_table);
1618 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1623 /* Drop the reference taken by _find_opp_table() */
1624 dev_pm_opp_put_opp_table(opp_table);
1626 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1628 static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1631 struct dev_pm_opp *opp = NULL, *temp;
1633 mutex_lock(&opp_table->lock);
1634 list_for_each_entry(temp, &opp_table->opp_list, node) {
1636 * Refcount must be dropped only once for each OPP by OPP core,
1637 * do that with help of "removed" flag.
1639 if (!temp->removed && dynamic == temp->dynamic) {
1645 mutex_unlock(&opp_table->lock);
1650 * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1651 * happen lock less to avoid circular dependency issues. This routine must be
1652 * called without the opp_table->lock held.
1654 static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
1656 struct dev_pm_opp *opp;
1658 while ((opp = _opp_get_next(opp_table, dynamic))) {
1659 opp->removed = true;
1660 dev_pm_opp_put(opp);
1662 /* Drop the references taken by dev_pm_opp_add() */
1664 dev_pm_opp_put_opp_table(opp_table);
1668 bool _opp_remove_all_static(struct opp_table *opp_table)
1670 mutex_lock(&opp_table->lock);
1672 if (!opp_table->parsed_static_opps) {
1673 mutex_unlock(&opp_table->lock);
1677 if (--opp_table->parsed_static_opps) {
1678 mutex_unlock(&opp_table->lock);
1682 mutex_unlock(&opp_table->lock);
1684 _opp_remove_all(opp_table, false);
1689 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1690 * @dev: device for which we do this operation
1692 * This function removes all dynamically created OPPs from the opp table.
1694 void dev_pm_opp_remove_all_dynamic(struct device *dev)
1696 struct opp_table *opp_table;
1698 opp_table = _find_opp_table(dev);
1699 if (IS_ERR(opp_table))
1702 _opp_remove_all(opp_table, true);
1704 /* Drop the reference taken by _find_opp_table() */
1705 dev_pm_opp_put_opp_table(opp_table);
1707 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1709 struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table)
1711 struct dev_pm_opp *opp;
1712 int supply_count, supply_size, icc_size, clk_size;
1714 /* Allocate space for at least one supply */
1715 supply_count = opp_table->regulator_count > 0 ?
1716 opp_table->regulator_count : 1;
1717 supply_size = sizeof(*opp->supplies) * supply_count;
1718 clk_size = sizeof(*opp->rates) * opp_table->clk_count;
1719 icc_size = sizeof(*opp->bandwidth) * opp_table->path_count;
1721 /* allocate new OPP node and supplies structures */
1722 opp = kzalloc(sizeof(*opp) + supply_size + clk_size + icc_size, GFP_KERNEL);
1726 /* Put the supplies, bw and clock at the end of the OPP structure */
1727 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1729 opp->rates = (unsigned long *)(opp->supplies + supply_count);
1732 opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->rates + opp_table->clk_count);
1734 INIT_LIST_HEAD(&opp->node);
1739 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
1740 struct opp_table *opp_table)
1742 struct regulator *reg;
1745 if (!opp_table->regulators)
1748 for (i = 0; i < opp_table->regulator_count; i++) {
1749 reg = opp_table->regulators[i];
1751 if (!regulator_is_supported_voltage(reg,
1752 opp->supplies[i].u_volt_min,
1753 opp->supplies[i].u_volt_max)) {
1754 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1755 __func__, opp->supplies[i].u_volt_min,
1756 opp->supplies[i].u_volt_max);
1764 static int _opp_compare_rate(struct opp_table *opp_table,
1765 struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
1769 for (i = 0; i < opp_table->clk_count; i++) {
1770 if (opp1->rates[i] != opp2->rates[i])
1771 return opp1->rates[i] < opp2->rates[i] ? -1 : 1;
1774 /* Same rates for both OPPs */
1778 static int _opp_compare_bw(struct opp_table *opp_table, struct dev_pm_opp *opp1,
1779 struct dev_pm_opp *opp2)
1783 for (i = 0; i < opp_table->path_count; i++) {
1784 if (opp1->bandwidth[i].peak != opp2->bandwidth[i].peak)
1785 return opp1->bandwidth[i].peak < opp2->bandwidth[i].peak ? -1 : 1;
1788 /* Same bw for both OPPs */
1798 int _opp_compare_key(struct opp_table *opp_table, struct dev_pm_opp *opp1,
1799 struct dev_pm_opp *opp2)
1803 ret = _opp_compare_rate(opp_table, opp1, opp2);
1807 ret = _opp_compare_bw(opp_table, opp1, opp2);
1811 if (opp1->level != opp2->level)
1812 return opp1->level < opp2->level ? -1 : 1;
1814 /* Duplicate OPPs */
1818 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1819 struct opp_table *opp_table,
1820 struct list_head **head)
1822 struct dev_pm_opp *opp;
1826 * Insert new OPP in order of increasing frequency and discard if
1829 * Need to use &opp_table->opp_list in the condition part of the 'for'
1830 * loop, don't replace it with head otherwise it will become an infinite
1833 list_for_each_entry(opp, &opp_table->opp_list, node) {
1834 opp_cmp = _opp_compare_key(opp_table, new_opp, opp);
1843 /* Duplicate OPPs */
1844 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1845 __func__, opp->rates[0], opp->supplies[0].u_volt,
1846 opp->available, new_opp->rates[0],
1847 new_opp->supplies[0].u_volt, new_opp->available);
1849 /* Should we compare voltages for all regulators here ? */
1850 return opp->available &&
1851 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1857 void _required_opps_available(struct dev_pm_opp *opp, int count)
1861 for (i = 0; i < count; i++) {
1862 if (opp->required_opps[i]->available)
1865 opp->available = false;
1866 pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
1867 __func__, opp->required_opps[i]->np, opp->rates[0]);
1874 * 0: On success. And appropriate error message for duplicate OPPs.
1875 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1876 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1877 * sure we don't print error messages unnecessarily if different parts of
1878 * kernel try to initialize the OPP table.
1879 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1880 * should be considered an error by the callers of _opp_add().
1882 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1883 struct opp_table *opp_table)
1885 struct list_head *head;
1888 mutex_lock(&opp_table->lock);
1889 head = &opp_table->opp_list;
1891 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1893 mutex_unlock(&opp_table->lock);
1897 list_add(&new_opp->node, head);
1898 mutex_unlock(&opp_table->lock);
1900 new_opp->opp_table = opp_table;
1901 kref_init(&new_opp->kref);
1903 opp_debug_create_one(new_opp, opp_table);
1905 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1906 new_opp->available = false;
1907 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1908 __func__, new_opp->rates[0]);
1911 /* required-opps not fully initialized yet */
1912 if (lazy_linking_pending(opp_table))
1915 _required_opps_available(new_opp, opp_table->required_opp_count);
1921 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1922 * @opp_table: OPP table
1923 * @dev: device for which we do this operation
1924 * @freq: Frequency in Hz for this OPP
1925 * @u_volt: Voltage in uVolts for this OPP
1926 * @dynamic: Dynamically added OPPs.
1928 * This function adds an opp definition to the opp table and returns status.
1929 * The opp is made available by default and it can be controlled using
1930 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1932 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1933 * and freed by dev_pm_opp_of_remove_table.
1937 * Duplicate OPPs (both freq and volt are same) and opp->available
1938 * -EEXIST Freq are same and volt are different OR
1939 * Duplicate OPPs (both freq and volt are same) and !opp->available
1940 * -ENOMEM Memory allocation failure
1942 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1943 unsigned long freq, long u_volt, bool dynamic)
1945 struct dev_pm_opp *new_opp;
1949 if (!assert_single_clk(opp_table))
1952 new_opp = _opp_allocate(opp_table);
1956 /* populate the opp table */
1957 new_opp->rates[0] = freq;
1958 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1959 new_opp->supplies[0].u_volt = u_volt;
1960 new_opp->supplies[0].u_volt_min = u_volt - tol;
1961 new_opp->supplies[0].u_volt_max = u_volt + tol;
1962 new_opp->available = true;
1963 new_opp->dynamic = dynamic;
1965 ret = _opp_add(dev, new_opp, opp_table);
1967 /* Don't return error for duplicate OPPs */
1974 * Notify the changes in the availability of the operable
1975 * frequency/voltage list.
1977 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
1987 * _opp_set_supported_hw() - Set supported platforms
1988 * @dev: Device for which supported-hw has to be set.
1989 * @versions: Array of hierarchy of versions to match.
1990 * @count: Number of elements in the array.
1992 * This is required only for the V2 bindings, and it enables a platform to
1993 * specify the hierarchy of versions it supports. OPP layer will then enable
1994 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1997 static int _opp_set_supported_hw(struct opp_table *opp_table,
1998 const u32 *versions, unsigned int count)
2000 /* Another CPU that shares the OPP table has set the property ? */
2001 if (opp_table->supported_hw)
2004 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
2006 if (!opp_table->supported_hw)
2009 opp_table->supported_hw_count = count;
2015 * _opp_put_supported_hw() - Releases resources blocked for supported hw
2016 * @opp_table: OPP table returned by _opp_set_supported_hw().
2018 * This is required only for the V2 bindings, and is called for a matching
2019 * _opp_set_supported_hw(). Until this is called, the opp_table structure
2020 * will not be freed.
2022 static void _opp_put_supported_hw(struct opp_table *opp_table)
2024 if (opp_table->supported_hw) {
2025 kfree(opp_table->supported_hw);
2026 opp_table->supported_hw = NULL;
2027 opp_table->supported_hw_count = 0;
2032 * _opp_set_prop_name() - Set prop-extn name
2033 * @dev: Device for which the prop-name has to be set.
2034 * @name: name to postfix to properties.
2036 * This is required only for the V2 bindings, and it enables a platform to
2037 * specify the extn to be used for certain property names. The properties to
2038 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
2039 * should postfix the property name with -<name> while looking for them.
2041 static int _opp_set_prop_name(struct opp_table *opp_table, const char *name)
2043 /* Another CPU that shares the OPP table has set the property ? */
2044 if (!opp_table->prop_name) {
2045 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
2046 if (!opp_table->prop_name)
2054 * _opp_put_prop_name() - Releases resources blocked for prop-name
2055 * @opp_table: OPP table returned by _opp_set_prop_name().
2057 * This is required only for the V2 bindings, and is called for a matching
2058 * _opp_set_prop_name(). Until this is called, the opp_table structure
2059 * will not be freed.
2061 static void _opp_put_prop_name(struct opp_table *opp_table)
2063 if (opp_table->prop_name) {
2064 kfree(opp_table->prop_name);
2065 opp_table->prop_name = NULL;
2070 * _opp_set_regulators() - Set regulator names for the device
2071 * @dev: Device for which regulator name is being set.
2072 * @names: Array of pointers to the names of the regulator.
2073 * @count: Number of regulators.
2075 * In order to support OPP switching, OPP layer needs to know the name of the
2076 * device's regulators, as the core would be required to switch voltages as
2079 * This must be called before any OPPs are initialized for the device.
2081 static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev,
2082 const char * const names[])
2084 const char * const *temp = names;
2085 struct regulator *reg;
2086 int count = 0, ret, i;
2088 /* Count number of regulators */
2095 /* Another CPU that shares the OPP table has set the regulators ? */
2096 if (opp_table->regulators)
2099 opp_table->regulators = kmalloc_array(count,
2100 sizeof(*opp_table->regulators),
2102 if (!opp_table->regulators)
2105 for (i = 0; i < count; i++) {
2106 reg = regulator_get_optional(dev, names[i]);
2108 ret = dev_err_probe(dev, PTR_ERR(reg),
2109 "%s: no regulator (%s) found\n",
2110 __func__, names[i]);
2111 goto free_regulators;
2114 opp_table->regulators[i] = reg;
2117 opp_table->regulator_count = count;
2119 /* Set generic config_regulators() for single regulators here */
2121 opp_table->config_regulators = _opp_config_regulator_single;
2127 regulator_put(opp_table->regulators[--i]);
2129 kfree(opp_table->regulators);
2130 opp_table->regulators = NULL;
2131 opp_table->regulator_count = -1;
2137 * _opp_put_regulators() - Releases resources blocked for regulator
2138 * @opp_table: OPP table returned from _opp_set_regulators().
2140 static void _opp_put_regulators(struct opp_table *opp_table)
2144 if (!opp_table->regulators)
2147 if (opp_table->enabled) {
2148 for (i = opp_table->regulator_count - 1; i >= 0; i--)
2149 regulator_disable(opp_table->regulators[i]);
2152 for (i = opp_table->regulator_count - 1; i >= 0; i--)
2153 regulator_put(opp_table->regulators[i]);
2155 kfree(opp_table->regulators);
2156 opp_table->regulators = NULL;
2157 opp_table->regulator_count = -1;
2160 static void _put_clks(struct opp_table *opp_table, int count)
2164 for (i = count - 1; i >= 0; i--)
2165 clk_put(opp_table->clks[i]);
2167 kfree(opp_table->clks);
2168 opp_table->clks = NULL;
2172 * _opp_set_clknames() - Set clk names for the device
2173 * @dev: Device for which clk names is being set.
2174 * @names: Clk names.
2176 * In order to support OPP switching, OPP layer needs to get pointers to the
2177 * clocks for the device. Simple cases work fine without using this routine
2178 * (i.e. by passing connection-id as NULL), but for a device with multiple
2179 * clocks available, the OPP core needs to know the exact names of the clks to
2182 * This must be called before any OPPs are initialized for the device.
2184 static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev,
2185 const char * const names[],
2186 config_clks_t config_clks)
2188 const char * const *temp = names;
2189 int count = 0, ret, i;
2192 /* Count number of clks */
2197 * This is a special case where we have a single clock, whose connection
2198 * id name is NULL, i.e. first two entries are NULL in the array.
2200 if (!count && !names[1])
2203 /* Fail early for invalid configurations */
2204 if (!count || (!config_clks && count > 1))
2207 /* Another CPU that shares the OPP table has set the clkname ? */
2208 if (opp_table->clks)
2211 opp_table->clks = kmalloc_array(count, sizeof(*opp_table->clks),
2213 if (!opp_table->clks)
2216 /* Find clks for the device */
2217 for (i = 0; i < count; i++) {
2218 clk = clk_get(dev, names[i]);
2220 ret = dev_err_probe(dev, PTR_ERR(clk),
2221 "%s: Couldn't find clock with name: %s\n",
2222 __func__, names[i]);
2226 opp_table->clks[i] = clk;
2229 opp_table->clk_count = count;
2230 opp_table->config_clks = config_clks;
2232 /* Set generic single clk set here */
2234 if (!opp_table->config_clks)
2235 opp_table->config_clks = _opp_config_clk_single;
2238 * We could have just dropped the "clk" field and used "clks"
2239 * everywhere. Instead we kept the "clk" field around for
2240 * following reasons:
2242 * - avoiding clks[0] everywhere else.
2243 * - not running single clk helpers for multiple clk usecase by
2246 * Since this is single-clk case, just update the clk pointer
2249 opp_table->clk = opp_table->clks[0];
2255 _put_clks(opp_table, i);
2260 * _opp_put_clknames() - Releases resources blocked for clks.
2261 * @opp_table: OPP table returned from _opp_set_clknames().
2263 static void _opp_put_clknames(struct opp_table *opp_table)
2265 if (!opp_table->clks)
2268 opp_table->config_clks = NULL;
2269 opp_table->clk = ERR_PTR(-ENODEV);
2271 _put_clks(opp_table, opp_table->clk_count);
2275 * _opp_set_config_regulators_helper() - Register custom set regulator helper.
2276 * @dev: Device for which the helper is getting registered.
2277 * @config_regulators: Custom set regulator helper.
2279 * This is useful to support platforms with multiple regulators per device.
2281 * This must be called before any OPPs are initialized for the device.
2283 static int _opp_set_config_regulators_helper(struct opp_table *opp_table,
2284 struct device *dev, config_regulators_t config_regulators)
2286 /* Another CPU that shares the OPP table has set the helper ? */
2287 if (!opp_table->config_regulators)
2288 opp_table->config_regulators = config_regulators;
2294 * _opp_put_config_regulators_helper() - Releases resources blocked for
2295 * config_regulators helper.
2296 * @opp_table: OPP table returned from _opp_set_config_regulators_helper().
2298 * Release resources blocked for platform specific config_regulators helper.
2300 static void _opp_put_config_regulators_helper(struct opp_table *opp_table)
2302 if (opp_table->config_regulators)
2303 opp_table->config_regulators = NULL;
2306 static void _detach_genpd(struct opp_table *opp_table)
2310 if (!opp_table->genpd_virt_devs)
2313 for (index = 0; index < opp_table->required_opp_count; index++) {
2314 if (!opp_table->genpd_virt_devs[index])
2317 dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
2318 opp_table->genpd_virt_devs[index] = NULL;
2321 kfree(opp_table->genpd_virt_devs);
2322 opp_table->genpd_virt_devs = NULL;
2326 * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
2327 * @dev: Consumer device for which the genpd is getting attached.
2328 * @names: Null terminated array of pointers containing names of genpd to attach.
2329 * @virt_devs: Pointer to return the array of virtual devices.
2331 * Multiple generic power domains for a device are supported with the help of
2332 * virtual genpd devices, which are created for each consumer device - genpd
2333 * pair. These are the device structures which are attached to the power domain
2334 * and are required by the OPP core to set the performance state of the genpd.
2335 * The same API also works for the case where single genpd is available and so
2336 * we don't need to support that separately.
2338 * This helper will normally be called by the consumer driver of the device
2339 * "dev", as only that has details of the genpd names.
2341 * This helper needs to be called once with a list of all genpd to attach.
2342 * Otherwise the original device structure will be used instead by the OPP core.
2344 * The order of entries in the names array must match the order in which
2345 * "required-opps" are added in DT.
2347 static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev,
2348 const char * const *names, struct device ***virt_devs)
2350 struct device *virt_dev;
2351 int index = 0, ret = -EINVAL;
2352 const char * const *name = names;
2354 if (opp_table->genpd_virt_devs)
2358 * If the genpd's OPP table isn't already initialized, parsing of the
2359 * required-opps fail for dev. We should retry this after genpd's OPP
2362 if (!opp_table->required_opp_count)
2363 return -EPROBE_DEFER;
2365 mutex_lock(&opp_table->genpd_virt_dev_lock);
2367 opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2368 sizeof(*opp_table->genpd_virt_devs),
2370 if (!opp_table->genpd_virt_devs)
2374 if (index >= opp_table->required_opp_count) {
2375 dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
2376 *name, opp_table->required_opp_count, index);
2380 virt_dev = dev_pm_domain_attach_by_name(dev, *name);
2381 if (IS_ERR_OR_NULL(virt_dev)) {
2382 ret = PTR_ERR(virt_dev) ? : -ENODEV;
2383 dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
2387 opp_table->genpd_virt_devs[index] = virt_dev;
2393 *virt_devs = opp_table->genpd_virt_devs;
2394 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2399 _detach_genpd(opp_table);
2401 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2407 * _opp_detach_genpd() - Detach genpd(s) from the device.
2408 * @opp_table: OPP table returned by _opp_attach_genpd().
2410 * This detaches the genpd(s), resets the virtual device pointers, and puts the
2413 static void _opp_detach_genpd(struct opp_table *opp_table)
2416 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
2419 mutex_lock(&opp_table->genpd_virt_dev_lock);
2420 _detach_genpd(opp_table);
2421 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2424 static void _opp_clear_config(struct opp_config_data *data)
2426 if (data->flags & OPP_CONFIG_GENPD)
2427 _opp_detach_genpd(data->opp_table);
2428 if (data->flags & OPP_CONFIG_REGULATOR)
2429 _opp_put_regulators(data->opp_table);
2430 if (data->flags & OPP_CONFIG_SUPPORTED_HW)
2431 _opp_put_supported_hw(data->opp_table);
2432 if (data->flags & OPP_CONFIG_REGULATOR_HELPER)
2433 _opp_put_config_regulators_helper(data->opp_table);
2434 if (data->flags & OPP_CONFIG_PROP_NAME)
2435 _opp_put_prop_name(data->opp_table);
2436 if (data->flags & OPP_CONFIG_CLK)
2437 _opp_put_clknames(data->opp_table);
2439 dev_pm_opp_put_opp_table(data->opp_table);
2444 * dev_pm_opp_set_config() - Set OPP configuration for the device.
2445 * @dev: Device for which configuration is being set.
2446 * @config: OPP configuration.
2448 * This allows all device OPP configurations to be performed at once.
2450 * This must be called before any OPPs are initialized for the device. This may
2451 * be called multiple times for the same OPP table, for example once for each
2452 * CPU that share the same table. This must be balanced by the same number of
2453 * calls to dev_pm_opp_clear_config() in order to free the OPP table properly.
2455 * This returns a token to the caller, which must be passed to
2456 * dev_pm_opp_clear_config() to free the resources later. The value of the
2457 * returned token will be >= 1 for success and negative for errors. The minimum
2458 * value of 1 is chosen here to make it easy for callers to manage the resource.
2460 int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
2462 struct opp_table *opp_table;
2463 struct opp_config_data *data;
2467 data = kmalloc(sizeof(*data), GFP_KERNEL);
2471 opp_table = _add_opp_table(dev, false);
2472 if (IS_ERR(opp_table)) {
2474 return PTR_ERR(opp_table);
2477 data->opp_table = opp_table;
2480 /* This should be called before OPPs are initialized */
2481 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2486 /* Configure clocks */
2487 if (config->clk_names) {
2488 ret = _opp_set_clknames(opp_table, dev, config->clk_names,
2489 config->config_clks);
2493 data->flags |= OPP_CONFIG_CLK;
2494 } else if (config->config_clks) {
2495 /* Don't allow config callback without clocks */
2500 /* Configure property names */
2501 if (config->prop_name) {
2502 ret = _opp_set_prop_name(opp_table, config->prop_name);
2506 data->flags |= OPP_CONFIG_PROP_NAME;
2509 /* Configure config_regulators helper */
2510 if (config->config_regulators) {
2511 ret = _opp_set_config_regulators_helper(opp_table, dev,
2512 config->config_regulators);
2516 data->flags |= OPP_CONFIG_REGULATOR_HELPER;
2519 /* Configure supported hardware */
2520 if (config->supported_hw) {
2521 ret = _opp_set_supported_hw(opp_table, config->supported_hw,
2522 config->supported_hw_count);
2526 data->flags |= OPP_CONFIG_SUPPORTED_HW;
2529 /* Configure supplies */
2530 if (config->regulator_names) {
2531 ret = _opp_set_regulators(opp_table, dev,
2532 config->regulator_names);
2536 data->flags |= OPP_CONFIG_REGULATOR;
2540 if (config->genpd_names) {
2541 ret = _opp_attach_genpd(opp_table, dev, config->genpd_names,
2546 data->flags |= OPP_CONFIG_GENPD;
2549 ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX),
2557 _opp_clear_config(data);
2560 EXPORT_SYMBOL_GPL(dev_pm_opp_set_config);
2563 * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration.
2564 * @opp_table: OPP table returned from dev_pm_opp_set_config().
2566 * This allows all device OPP configurations to be cleared at once. This must be
2567 * called once for each call made to dev_pm_opp_set_config(), in order to free
2568 * the OPPs properly.
2570 * Currently the first call itself ends up freeing all the OPP configurations,
2571 * while the later ones only drop the OPP table reference. This works well for
2572 * now as we would never want to use an half initialized OPP table and want to
2573 * remove the configurations together.
2575 void dev_pm_opp_clear_config(int token)
2577 struct opp_config_data *data;
2580 * This lets the callers call this unconditionally and keep their code
2583 if (unlikely(token <= 0))
2586 data = xa_erase(&opp_configs, token);
2590 _opp_clear_config(data);
2592 EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config);
2594 static void devm_pm_opp_config_release(void *token)
2596 dev_pm_opp_clear_config((unsigned long)token);
2600 * devm_pm_opp_set_config() - Set OPP configuration for the device.
2601 * @dev: Device for which configuration is being set.
2602 * @config: OPP configuration.
2604 * This allows all device OPP configurations to be performed at once.
2605 * This is a resource-managed variant of dev_pm_opp_set_config().
2607 * Return: 0 on success and errorno otherwise.
2609 int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
2611 int token = dev_pm_opp_set_config(dev, config);
2616 return devm_add_action_or_reset(dev, devm_pm_opp_config_release,
2617 (void *) ((unsigned long) token));
2619 EXPORT_SYMBOL_GPL(devm_pm_opp_set_config);
2622 * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
2623 * @src_table: OPP table which has @dst_table as one of its required OPP table.
2624 * @dst_table: Required OPP table of the @src_table.
2625 * @src_opp: OPP from the @src_table.
2627 * This function returns the OPP (present in @dst_table) pointed out by the
2628 * "required-opps" property of the @src_opp (present in @src_table).
2630 * The callers are required to call dev_pm_opp_put() for the returned OPP after
2633 * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
2635 struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
2636 struct opp_table *dst_table,
2637 struct dev_pm_opp *src_opp)
2639 struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
2642 if (!src_table || !dst_table || !src_opp ||
2643 !src_table->required_opp_tables)
2644 return ERR_PTR(-EINVAL);
2646 /* required-opps not fully initialized yet */
2647 if (lazy_linking_pending(src_table))
2648 return ERR_PTR(-EBUSY);
2650 for (i = 0; i < src_table->required_opp_count; i++) {
2651 if (src_table->required_opp_tables[i] == dst_table) {
2652 mutex_lock(&src_table->lock);
2654 list_for_each_entry(opp, &src_table->opp_list, node) {
2655 if (opp == src_opp) {
2656 dest_opp = opp->required_opps[i];
2657 dev_pm_opp_get(dest_opp);
2662 mutex_unlock(&src_table->lock);
2667 if (IS_ERR(dest_opp)) {
2668 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
2669 src_table, dst_table);
2674 EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
2677 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2678 * @src_table: OPP table which has dst_table as one of its required OPP table.
2679 * @dst_table: Required OPP table of the src_table.
2680 * @pstate: Current performance state of the src_table.
2682 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2683 * "required-opps" property of the OPP (present in @src_table) which has
2684 * performance state set to @pstate.
2686 * Return: Zero or positive performance state on success, otherwise negative
2689 int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2690 struct opp_table *dst_table,
2691 unsigned int pstate)
2693 struct dev_pm_opp *opp;
2694 int dest_pstate = -EINVAL;
2698 * Normally the src_table will have the "required_opps" property set to
2699 * point to one of the OPPs in the dst_table, but in some cases the
2700 * genpd and its master have one to one mapping of performance states
2701 * and so none of them have the "required-opps" property set. Return the
2702 * pstate of the src_table as it is in such cases.
2704 if (!src_table || !src_table->required_opp_count)
2707 /* Both OPP tables must belong to genpds */
2708 if (unlikely(!src_table->is_genpd || !dst_table->is_genpd)) {
2709 pr_err("%s: Performance state is only valid for genpds.\n", __func__);
2713 /* required-opps not fully initialized yet */
2714 if (lazy_linking_pending(src_table))
2717 for (i = 0; i < src_table->required_opp_count; i++) {
2718 if (src_table->required_opp_tables[i]->np == dst_table->np)
2722 if (unlikely(i == src_table->required_opp_count)) {
2723 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2724 __func__, src_table, dst_table);
2728 mutex_lock(&src_table->lock);
2730 list_for_each_entry(opp, &src_table->opp_list, node) {
2731 if (opp->level == pstate) {
2732 dest_pstate = opp->required_opps[i]->level;
2737 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2741 mutex_unlock(&src_table->lock);
2747 * dev_pm_opp_add() - Add an OPP table from a table definitions
2748 * @dev: device for which we do this operation
2749 * @freq: Frequency in Hz for this OPP
2750 * @u_volt: Voltage in uVolts for this OPP
2752 * This function adds an opp definition to the opp table and returns status.
2753 * The opp is made available by default and it can be controlled using
2754 * dev_pm_opp_enable/disable functions.
2758 * Duplicate OPPs (both freq and volt are same) and opp->available
2759 * -EEXIST Freq are same and volt are different OR
2760 * Duplicate OPPs (both freq and volt are same) and !opp->available
2761 * -ENOMEM Memory allocation failure
2763 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
2765 struct opp_table *opp_table;
2768 opp_table = _add_opp_table(dev, true);
2769 if (IS_ERR(opp_table))
2770 return PTR_ERR(opp_table);
2772 /* Fix regulator count for dynamic OPPs */
2773 opp_table->regulator_count = 1;
2775 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
2777 dev_pm_opp_put_opp_table(opp_table);
2781 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
2784 * _opp_set_availability() - helper to set the availability of an opp
2785 * @dev: device for which we do this operation
2786 * @freq: OPP frequency to modify availability
2787 * @availability_req: availability status requested for this opp
2789 * Set the availability of an OPP, opp_{enable,disable} share a common logic
2790 * which is isolated here.
2792 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2793 * copy operation, returns 0 if no modification was done OR modification was
2796 static int _opp_set_availability(struct device *dev, unsigned long freq,
2797 bool availability_req)
2799 struct opp_table *opp_table;
2800 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2803 /* Find the opp_table */
2804 opp_table = _find_opp_table(dev);
2805 if (IS_ERR(opp_table)) {
2806 r = PTR_ERR(opp_table);
2807 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2811 if (!assert_single_clk(opp_table)) {
2816 mutex_lock(&opp_table->lock);
2818 /* Do we have the frequency? */
2819 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2820 if (tmp_opp->rates[0] == freq) {
2831 /* Is update really needed? */
2832 if (opp->available == availability_req)
2835 opp->available = availability_req;
2837 dev_pm_opp_get(opp);
2838 mutex_unlock(&opp_table->lock);
2840 /* Notify the change of the OPP availability */
2841 if (availability_req)
2842 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
2845 blocking_notifier_call_chain(&opp_table->head,
2846 OPP_EVENT_DISABLE, opp);
2848 dev_pm_opp_put(opp);
2852 mutex_unlock(&opp_table->lock);
2854 dev_pm_opp_put_opp_table(opp_table);
2859 * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
2860 * @dev: device for which we do this operation
2861 * @freq: OPP frequency to adjust voltage of
2862 * @u_volt: new OPP target voltage
2863 * @u_volt_min: new OPP min voltage
2864 * @u_volt_max: new OPP max voltage
2866 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2867 * copy operation, returns 0 if no modifcation was done OR modification was
2870 int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
2871 unsigned long u_volt, unsigned long u_volt_min,
2872 unsigned long u_volt_max)
2875 struct opp_table *opp_table;
2876 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2879 /* Find the opp_table */
2880 opp_table = _find_opp_table(dev);
2881 if (IS_ERR(opp_table)) {
2882 r = PTR_ERR(opp_table);
2883 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2887 if (!assert_single_clk(opp_table)) {
2892 mutex_lock(&opp_table->lock);
2894 /* Do we have the frequency? */
2895 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2896 if (tmp_opp->rates[0] == freq) {
2907 /* Is update really needed? */
2908 if (opp->supplies->u_volt == u_volt)
2911 opp->supplies->u_volt = u_volt;
2912 opp->supplies->u_volt_min = u_volt_min;
2913 opp->supplies->u_volt_max = u_volt_max;
2915 dev_pm_opp_get(opp);
2916 mutex_unlock(&opp_table->lock);
2918 /* Notify the voltage change of the OPP */
2919 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
2922 dev_pm_opp_put(opp);
2926 mutex_unlock(&opp_table->lock);
2928 dev_pm_opp_put_opp_table(opp_table);
2931 EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
2934 * dev_pm_opp_enable() - Enable a specific OPP
2935 * @dev: device for which we do this operation
2936 * @freq: OPP frequency to enable
2938 * Enables a provided opp. If the operation is valid, this returns 0, else the
2939 * corresponding error value. It is meant to be used for users an OPP available
2940 * after being temporarily made unavailable with dev_pm_opp_disable.
2942 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2943 * copy operation, returns 0 if no modification was done OR modification was
2946 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
2948 return _opp_set_availability(dev, freq, true);
2950 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
2953 * dev_pm_opp_disable() - Disable a specific OPP
2954 * @dev: device for which we do this operation
2955 * @freq: OPP frequency to disable
2957 * Disables a provided opp. If the operation is valid, this returns
2958 * 0, else the corresponding error value. It is meant to be a temporary
2959 * control by users to make this OPP not available until the circumstances are
2960 * right to make it available again (with a call to dev_pm_opp_enable).
2962 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2963 * copy operation, returns 0 if no modification was done OR modification was
2966 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
2968 return _opp_set_availability(dev, freq, false);
2970 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
2973 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2974 * @dev: Device for which notifier needs to be registered
2975 * @nb: Notifier block to be registered
2977 * Return: 0 on success or a negative error value.
2979 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
2981 struct opp_table *opp_table;
2984 opp_table = _find_opp_table(dev);
2985 if (IS_ERR(opp_table))
2986 return PTR_ERR(opp_table);
2988 ret = blocking_notifier_chain_register(&opp_table->head, nb);
2990 dev_pm_opp_put_opp_table(opp_table);
2994 EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2997 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2998 * @dev: Device for which notifier needs to be unregistered
2999 * @nb: Notifier block to be unregistered
3001 * Return: 0 on success or a negative error value.
3003 int dev_pm_opp_unregister_notifier(struct device *dev,
3004 struct notifier_block *nb)
3006 struct opp_table *opp_table;
3009 opp_table = _find_opp_table(dev);
3010 if (IS_ERR(opp_table))
3011 return PTR_ERR(opp_table);
3013 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
3015 dev_pm_opp_put_opp_table(opp_table);
3019 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
3022 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
3023 * @dev: device pointer used to lookup OPP table.
3025 * Free both OPPs created using static entries present in DT and the
3026 * dynamically added entries.
3028 void dev_pm_opp_remove_table(struct device *dev)
3030 struct opp_table *opp_table;
3032 /* Check for existing table for 'dev' */
3033 opp_table = _find_opp_table(dev);
3034 if (IS_ERR(opp_table)) {
3035 int error = PTR_ERR(opp_table);
3037 if (error != -ENODEV)
3038 WARN(1, "%s: opp_table: %d\n",
3039 IS_ERR_OR_NULL(dev) ?
3040 "Invalid device" : dev_name(dev),
3046 * Drop the extra reference only if the OPP table was successfully added
3047 * with dev_pm_opp_of_add_table() earlier.
3049 if (_opp_remove_all_static(opp_table))
3050 dev_pm_opp_put_opp_table(opp_table);
3052 /* Drop reference taken by _find_opp_table() */
3053 dev_pm_opp_put_opp_table(opp_table);
3055 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
3058 * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
3059 * @dev: device for which we do this operation
3061 * Sync voltage state of the OPP table regulators.
3063 * Return: 0 on success or a negative error value.
3065 int dev_pm_opp_sync_regulators(struct device *dev)
3067 struct opp_table *opp_table;
3068 struct regulator *reg;
3071 /* Device may not have OPP table */
3072 opp_table = _find_opp_table(dev);
3073 if (IS_ERR(opp_table))
3076 /* Regulator may not be required for the device */
3077 if (unlikely(!opp_table->regulators))
3080 /* Nothing to sync if voltage wasn't changed */
3081 if (!opp_table->enabled)
3084 for (i = 0; i < opp_table->regulator_count; i++) {
3085 reg = opp_table->regulators[i];
3086 ret = regulator_sync_voltage(reg);
3091 /* Drop reference taken by _find_opp_table() */
3092 dev_pm_opp_put_opp_table(opp_table);
3096 EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);