Merge branches 'pm-cpufreq' and 'pm-cpuidle'
[cascardo/linux.git] / drivers / cpufreq / cpufreq-dt.c
index f657c57..fde97d6 100644 (file)
@@ -58,6 +58,8 @@ static int set_target(struct cpufreq_policy *policy, unsigned int index)
        old_freq = clk_get_rate(cpu_clk) / 1000;
 
        if (!IS_ERR(cpu_reg)) {
+               unsigned long opp_freq;
+
                rcu_read_lock();
                opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_Hz);
                if (IS_ERR(opp)) {
@@ -67,13 +69,16 @@ static int set_target(struct cpufreq_policy *policy, unsigned int index)
                        return PTR_ERR(opp);
                }
                volt = dev_pm_opp_get_voltage(opp);
+               opp_freq = dev_pm_opp_get_freq(opp);
                rcu_read_unlock();
                tol = volt * priv->voltage_tolerance / 100;
                volt_old = regulator_get_voltage(cpu_reg);
+               dev_dbg(cpu_dev, "Found OPP: %ld kHz, %ld uV\n",
+                       opp_freq / 1000, volt);
        }
 
        dev_dbg(cpu_dev, "%u MHz, %ld mV --> %u MHz, %ld mV\n",
-               old_freq / 1000, volt_old ? volt_old / 1000 : -1,
+               old_freq / 1000, (volt_old > 0) ? volt_old / 1000 : -1,
                new_freq / 1000, volt ? volt / 1000 : -1);
 
        /* scaling up?  scale voltage before frequency */
@@ -89,7 +94,7 @@ static int set_target(struct cpufreq_policy *policy, unsigned int index)
        ret = clk_set_rate(cpu_clk, freq_exact);
        if (ret) {
                dev_err(cpu_dev, "failed to set clock rate: %d\n", ret);
-               if (!IS_ERR(cpu_reg))
+               if (!IS_ERR(cpu_reg) && volt_old > 0)
                        regulator_set_voltage_tol(cpu_reg, volt_old, tol);
                return ret;
        }
@@ -181,7 +186,6 @@ static int cpufreq_init(struct cpufreq_policy *policy)
 {
        struct cpufreq_dt_platform_data *pd;
        struct cpufreq_frequency_table *freq_table;
-       struct thermal_cooling_device *cdev;
        struct device_node *np;
        struct private_data *priv;
        struct device *cpu_dev;
@@ -193,7 +197,7 @@ static int cpufreq_init(struct cpufreq_policy *policy)
 
        ret = allocate_resources(policy->cpu, &cpu_dev, &cpu_reg, &cpu_clk);
        if (ret) {
-               pr_err("%s: Failed to allocate resources\n: %d", __func__, ret);
+               pr_err("%s: Failed to allocate resources: %d\n", __func__, ret);
                return ret;
        }
 
@@ -207,10 +211,21 @@ static int cpufreq_init(struct cpufreq_policy *policy)
        /* OPPs might be populated at runtime, don't check for error here */
        of_init_opp_table(cpu_dev);
 
+       /*
+        * But we need OPP table to function so if it is not there let's
+        * give platform code chance to provide it for us.
+        */
+       ret = dev_pm_opp_get_opp_count(cpu_dev);
+       if (ret <= 0) {
+               pr_debug("OPP table is not ready, deferring probe\n");
+               ret = -EPROBE_DEFER;
+               goto out_free_opp;
+       }
+
        priv = kzalloc(sizeof(*priv), GFP_KERNEL);
        if (!priv) {
                ret = -ENOMEM;
-               goto out_put_node;
+               goto out_free_opp;
        }
 
        of_property_read_u32(np, "voltage-tolerance", &priv->voltage_tolerance);
@@ -264,20 +279,6 @@ static int cpufreq_init(struct cpufreq_policy *policy)
                goto out_free_priv;
        }
 
-       /*
-        * For now, just loading the cooling device;
-        * thermal DT code takes care of matching them.
-        */
-       if (of_find_property(np, "#cooling-cells", NULL)) {
-               cdev = of_cpufreq_cooling_register(np, cpu_present_mask);
-               if (IS_ERR(cdev))
-                       dev_err(cpu_dev,
-                               "running cpufreq without cooling device: %ld\n",
-                               PTR_ERR(cdev));
-               else
-                       priv->cdev = cdev;
-       }
-
        priv->cpu_dev = cpu_dev;
        priv->cpu_reg = cpu_reg;
        policy->driver_data = priv;
@@ -287,7 +288,7 @@ static int cpufreq_init(struct cpufreq_policy *policy)
        if (ret) {
                dev_err(cpu_dev, "%s: invalid frequency table: %d\n", __func__,
                        ret);
-               goto out_cooling_unregister;
+               goto out_free_cpufreq_table;
        }
 
        policy->cpuinfo.transition_latency = transition_latency;
@@ -300,12 +301,12 @@ static int cpufreq_init(struct cpufreq_policy *policy)
 
        return 0;
 
-out_cooling_unregister:
-       cpufreq_cooling_unregister(priv->cdev);
+out_free_cpufreq_table:
        dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
 out_free_priv:
        kfree(priv);
-out_put_node:
+out_free_opp:
+       of_free_opp_table(cpu_dev);
        of_node_put(np);
 out_put_reg_clk:
        clk_put(cpu_clk);
@@ -319,8 +320,10 @@ static int cpufreq_exit(struct cpufreq_policy *policy)
 {
        struct private_data *priv = policy->driver_data;
 
-       cpufreq_cooling_unregister(priv->cdev);
+       if (priv->cdev)
+               cpufreq_cooling_unregister(priv->cdev);
        dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
+       of_free_opp_table(priv->cpu_dev);
        clk_put(policy->clk);
        if (!IS_ERR(priv->cpu_reg))
                regulator_put(priv->cpu_reg);
@@ -329,6 +332,33 @@ static int cpufreq_exit(struct cpufreq_policy *policy)
        return 0;
 }
 
+static void cpufreq_ready(struct cpufreq_policy *policy)
+{
+       struct private_data *priv = policy->driver_data;
+       struct device_node *np = of_node_get(priv->cpu_dev->of_node);
+
+       if (WARN_ON(!np))
+               return;
+
+       /*
+        * For now, just loading the cooling device;
+        * thermal DT code takes care of matching them.
+        */
+       if (of_find_property(np, "#cooling-cells", NULL)) {
+               priv->cdev = of_cpufreq_cooling_register(np,
+                                                        policy->related_cpus);
+               if (IS_ERR(priv->cdev)) {
+                       dev_err(priv->cpu_dev,
+                               "running cpufreq without cooling device: %ld\n",
+                               PTR_ERR(priv->cdev));
+
+                       priv->cdev = NULL;
+               }
+       }
+
+       of_node_put(np);
+}
+
 static struct cpufreq_driver dt_cpufreq_driver = {
        .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
        .verify = cpufreq_generic_frequency_table_verify,
@@ -336,6 +366,7 @@ static struct cpufreq_driver dt_cpufreq_driver = {
        .get = cpufreq_generic_get,
        .init = cpufreq_init,
        .exit = cpufreq_exit,
+       .ready = cpufreq_ready,
        .name = "cpufreq-dt",
        .attr = cpufreq_generic_attr,
 };
@@ -380,7 +411,6 @@ static int dt_cpufreq_remove(struct platform_device *pdev)
 static struct platform_driver dt_cpufreq_platdrv = {
        .driver = {
                .name   = "cpufreq-dt",
-               .owner  = THIS_MODULE,
        },
        .probe          = dt_cpufreq_probe,
        .remove         = dt_cpufreq_remove,