Fix dedicated cpufreq for kernel 5.15 as they changed module
order and now it can happen that cpufreq probe after cache driver.
Also add lock between cache scaling in set_target as it's now required
by opp functions.
Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
##################################################################################
--- /dev/null
+++ b/drivers/cpufreq/qcom-cpufreq-krait.c
##################################################################################
--- /dev/null
+++ b/drivers/cpufreq/qcom-cpufreq-krait.c
+// SPDX-License-Identifier: GPL-2.0
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+// SPDX-License-Identifier: GPL-2.0
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include "cpufreq-dt.h"
+
+
+#include "cpufreq-dt.h"
+
-+static struct platform_device *l2_pdev;
++static struct device *l2_dev;
++static struct mutex lock;
+
+struct private_data {
+ struct opp_table *opp_table;
+ struct device *cpu_dev;
+
+struct private_data {
+ struct opp_table *opp_table;
+ struct device *cpu_dev;
++ struct device *l2_dev;
+ const char *reg_name;
+ bool have_static_opps;
+};
+ const char *reg_name;
+ bool have_static_opps;
+};
+ unsigned int level;
+ int cpu, ret;
+
+ unsigned int level;
+ int cpu, ret;
+
+ int policy_cpu = policy->cpu;
+
+ int policy_cpu = policy->cpu;
+
+ /* find the max freq across all core */
+ for_each_present_cpu(cpu)
+ if (cpu != policy_cpu)
+ /* find the max freq across all core */
+ for_each_present_cpu(cpu)
+ if (cpu != policy_cpu)
+ opp = dev_pm_opp_find_freq_exact(priv->cpu_dev, target_freq,
+ true);
+ if (IS_ERR(opp)) {
+ opp = dev_pm_opp_find_freq_exact(priv->cpu_dev, target_freq,
+ true);
+ if (IS_ERR(opp)) {
-+ dev_err(&l2_pdev->dev, "failed to find OPP for %ld\n",
++ dev_err(l2_dev, "failed to find OPP for %ld\n",
++ ret = PTR_ERR(opp);
++ goto l2_scale_fail;
+ }
+ level = dev_pm_opp_get_level(opp);
+ dev_pm_opp_put(opp);
+ }
+ level = dev_pm_opp_get_level(opp);
+ dev_pm_opp_put(opp);
+ */
+ if (unlikely(index == 0 && level != 0)) {
+ dev_err(priv->cpu_dev, "Krait CPU can't operate at idle freq with L2 at 1GHz");
+ */
+ if (unlikely(index == 0 && level != 0)) {
+ dev_err(priv->cpu_dev, "Krait CPU can't operate at idle freq with L2 at 1GHz");
++ ret = -EINVAL;
++ goto l2_scale_fail;
-+ opp = dev_pm_opp_find_level_exact(&l2_pdev->dev, level);
++ opp = dev_pm_opp_find_level_exact(l2_dev, level);
-+ dev_err(&l2_pdev->dev,
+ "failed to find level OPP for %d\n", level);
+ "failed to find level OPP for %d\n", level);
++ ret = PTR_ERR(opp);
++ goto l2_scale_fail;
+ }
+ target_freq = dev_pm_opp_get_freq(opp);
+ dev_pm_opp_put(opp);
+
+ }
+ target_freq = dev_pm_opp_get_freq(opp);
+ dev_pm_opp_put(opp);
+
-+ ret = dev_pm_opp_set_rate(&l2_pdev->dev, target_freq);
++ ret = dev_pm_opp_set_rate(l2_dev, target_freq);
++ goto l2_scale_fail;
++
++ mutex_unlock(&lock);
+ }
+
+ ret = dev_pm_opp_set_rate(priv->cpu_dev, freq * 1000);
+ }
+
+ ret = dev_pm_opp_set_rate(priv->cpu_dev, freq * 1000);
+ policy->cpuinfo.max_freq);
+
+ return 0;
+ policy->cpuinfo.max_freq);
+
+ return 0;
++l2_scale_fail:
++ mutex_unlock(&lock);
++
++ return ret;
+ * give platform code chance to provide it for us.
+ */
+ ret = dev_pm_opp_get_opp_count(cpu_dev);
+ * give platform code chance to provide it for us.
+ */
+ ret = dev_pm_opp_get_opp_count(cpu_dev);
+ dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
+ ret = -EPROBE_DEFER;
+ goto out_free_opp;
+ dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
+ ret = -EPROBE_DEFER;
+ goto out_free_opp;
+ dev_pm_opp_put(opp);
+
+ /*
+ dev_pm_opp_put(opp);
+
+ /*
++ * Check if we have at least opp-level 1, 0 should always be set to
++ * the idle freq
++ */
++ opp = dev_pm_opp_find_level_exact(dev, 1);
++ if (IS_ERR(opp)) {
++ ret = PTR_ERR(opp);
++ dev_err(dev,
++ "Invalid configuration found of l2 opp. Can't find opp-level 1");
++ goto invalid_conf;
++ }
++ dev_pm_opp_put(opp);
++
++ /*
+ * Check opp-level configuration
+ * At least 2 level must be set or the cache will always be scaled
+ * the idle freq causing some performance problem
+ * Check opp-level configuration
+ * At least 2 level must be set or the cache will always be scaled
+ * the idle freq causing some performance problem
-+ /*
-+ * Check if we have at least opp-level 1, 0 should always be set to
-+ * the idle freq
-+ */
-+ opp = dev_pm_opp_find_level_exact(dev, 1);
-+ if (IS_ERR(opp)) {
-+ dev_err(dev,
-+ "Invalid configuration found of l2 opp. Can't find opp-level 1");
++ /* With opp error assume cpufreq still has to be registred. Defer probe. */
++ ret = dev_pm_opp_get_opp_count(cpu_dev);
++ if (ret < 0) {
++ ret = -EPROBE_DEFER;
+
+ /*
+ * Check if we have at least opp-level 1 in the cpu opp, 0 should always
+
+ /*
+ * Check if we have at least opp-level 1 in the cpu opp, 0 should always
+ */
+ opp = dev_pm_opp_find_level_exact(cpu_dev, 1);
+ if (IS_ERR(opp)) {
+ */
+ opp = dev_pm_opp_find_level_exact(cpu_dev, 1);
+ if (IS_ERR(opp)) {
-+ dev_err(dev,
-+ "Invalid configuration found of cpu opp. Can't find opp-level 1");
++ ret = PTR_ERR(opp);
++ if (ret != -EPROBE_DEFER)
++ dev_err(dev,
++ "Invalid configuration found of cpu opp. Can't find opp-level 1");
+ goto invalid_conf;
+ }
+ dev_pm_opp_put(opp);
+
+ platform_set_drvdata(pdev, data);
+
+ goto invalid_conf;
+ }
+ dev_pm_opp_put(opp);
+
+ platform_set_drvdata(pdev, data);
+
+ /* The l2 scaling is enabled by linking the cpufreq driver */
+ /* The l2 scaling is enabled by linking the cpufreq driver */
+ dev_pm_opp_put_regulators(table);
+ dev_pm_opp_unregister_set_opp_helper(table);
+
+ dev_pm_opp_put_regulators(table);
+ dev_pm_opp_unregister_set_opp_helper(table);
+
+};
+
+static int krait_cache_remove(struct platform_device *pdev)
+};
+
+static int krait_cache_remove(struct platform_device *pdev)
+ .probe = krait_cpufreq_probe,
+ .remove = krait_cpufreq_remove,
+};
+ .probe = krait_cpufreq_probe,
+ .remove = krait_cpufreq_remove,
+};
+module_platform_driver(krait_cpufreq_platdrv);
+
+MODULE_ALIAS("platform:krait-cpufreq");
+module_platform_driver(krait_cpufreq_platdrv);
+
+MODULE_ALIAS("platform:krait-cpufreq");
#include "cpufreq-dt.h"
@@ -68,6 +69,13 @@ static int set_target(struct cpufreq_pol
#include "cpufreq-dt.h"
@@ -68,6 +69,13 @@ static int set_target(struct cpufreq_pol
+ */
+ ret = scale_fabrics(target_freq);
+ if (ret)
+ */
+ ret = scale_fabrics(target_freq);
+ if (ret)
- opp = dev_pm_opp_find_level_exact(&l2_pdev->dev, level);
+ opp = dev_pm_opp_find_level_exact(l2_dev, level);