Commit 81c4d8a3 authored by Viresh Kumar's avatar Viresh Kumar
Browse files

opp: Keep track of currently programmed OPP

The dev_pm_opp_set_rate() helper needs to know the currently programmed
OPP to make few decisions and currently we try to find it on every
invocation of this routine.

Lets start keeping track of the current_opp programmed for the devices
of the opp table, that will be quite useful going forward.

If we fail to find the current OPP, we pick the first one available in
the list, as the list is in ascending order of frequencies, level, or
bandwidth and that's the best guess we can make anyway.

Note that we used to do the frequency comparison a bit early in
dev_pm_opp_set_rate() previously, and now instead we check the target
opp, which shall be more accurate anyway.

We need to make sure that current_opp's memory doesn't get freed while
it is being used and so we keep a reference of it until the time it is

Now that current_opp will always be set, we can drop some unnecessary
checks as well.
Signed-off-by: default avatarViresh Kumar <>
Tested-by: default avatarDmitry Osipenko <>
parent 1d3c42ca
......@@ -788,8 +788,7 @@ restore_freq:
__func__, old_freq);
/* This shouldn't harm even if the voltages weren't updated earlier */
if (old_supply)
_set_opp_voltage(dev, reg, old_supply);
_set_opp_voltage(dev, reg, old_supply);
return ret;
......@@ -837,11 +836,7 @@ static int _set_opp_custom(const struct opp_table *opp_table,
if (opp_table->sod_supplies) {
size = sizeof(*old_supply) * opp_table->regulator_count;
if (!old_supply)
memset(data->, 0, size);
memcpy(data->, old_supply, size);
memcpy(data->, old_supply, size);
memcpy(data->, new_supply, size);
data->regulator_count = opp_table->regulator_count;
} else {
......@@ -950,6 +945,31 @@ int dev_pm_opp_set_bw(struct device *dev, struct dev_pm_opp *opp)
static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
unsigned long freq;
if (!IS_ERR(opp_table->clk)) {
freq = clk_get_rate(opp_table->clk);
opp = _find_freq_ceil(opp_table, &freq);
* Unable to find the current OPP ? Pick the first from the list since
* it is in ascending order, otherwise rest of the code will need to
* make special checks to validate current_opp.
if (IS_ERR(opp)) {
opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
opp_table->current_opp = opp;
static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
int ret;
......@@ -1011,16 +1031,6 @@ int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
if ((long)freq <= 0)
freq = target_freq;
old_freq = clk_get_rate(opp_table->clk);
/* Return early if nothing to do */
if (opp_table->enabled && old_freq == freq) {
dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
__func__, freq);
ret = 0;
goto put_opp_table;
* For IO devices which require an OPP on some platforms/SoCs
* while just needing to scale the clock on some others
......@@ -1033,12 +1043,9 @@ int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
goto put_opp_table;
temp_freq = old_freq;
old_opp = _find_freq_ceil(opp_table, &temp_freq);
if (IS_ERR(old_opp)) {
dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
__func__, old_freq, PTR_ERR(old_opp));
/* Find the currently set OPP if we don't know already */
if (unlikely(!opp_table->current_opp))
_find_current_opp(dev, opp_table);
temp_freq = freq;
opp = _find_freq_ceil(opp_table, &temp_freq);
......@@ -1046,7 +1053,17 @@ int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
ret = PTR_ERR(opp);
dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
__func__, freq, ret);
goto put_old_opp;
goto put_opp_table;
old_opp = opp_table->current_opp;
old_freq = old_opp->rate;
/* Return early if nothing to do */
if (opp_table->enabled && old_opp == opp) {
dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
ret = 0;
goto put_opp;
dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
......@@ -1061,11 +1078,10 @@ int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
if (opp_table->set_opp) {
ret = _set_opp_custom(opp_table, dev, old_freq, freq,
IS_ERR(old_opp) ? NULL : old_opp->supplies,
old_opp->supplies, opp->supplies);
} else if (opp_table->regulators) {
ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
IS_ERR(old_opp) ? NULL : old_opp->supplies,
} else {
/* Only frequency scaling */
......@@ -1081,15 +1097,18 @@ int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
if (!ret) {
ret = _set_opp_bw(opp_table, opp, dev, false);
if (!ret)
if (!ret) {
opp_table->enabled = true;
/* Make sure current_opp doesn't get freed */
opp_table->current_opp = opp;
if (!IS_ERR(old_opp))
return ret;
......@@ -1298,6 +1317,9 @@ static void _opp_table_kref_release(struct kref *kref)
if (opp_table->current_opp)
/* Release clk */
......@@ -135,6 +135,7 @@ enum opp_table_access {
* @clock_latency_ns_max: Max clock latency in nanoseconds.
* @parsed_static_opps: Count of devices for which OPPs are initialized from DT.
* @shared_opp: OPP is shared between multiple devices.
* @current_opp: Currently configured OPP for the table.
* @suspend_opp: Pointer to OPP to be used during device suspend.
* @genpd_virt_dev_lock: Mutex protecting the genpd virtual device pointers.
* @genpd_virt_devs: List of virtual devices for multiple genpd support.
......@@ -183,6 +184,7 @@ struct opp_table {
unsigned int parsed_static_opps;
enum opp_table_access shared_opp;
struct dev_pm_opp *current_opp;
struct dev_pm_opp *suspend_opp;
struct mutex genpd_virt_dev_lock;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment