Commit 6a17b387 authored by Jia-Wei Chang's avatar Jia-Wei Chang Committed by Viresh Kumar
Browse files

cpufreq: mediatek: Refine mtk_cpufreq_voltage_tracking()



Because the difference of sram and proc should in a range of min_volt_shift
and max_volt_shift. We need to adjust the sram and proc step by step.

We replace VOLT_TOL (voltage tolerance) with the platform data and update the
logic to determine the voltage boundary and invoking regulator_set_voltage.

- Use 'sram_min_volt' and 'sram_max_volt' to determine the voltage boundary
  of sram regulator.
- Use (sram_min_volt - min_volt_shift) and 'proc_max_volt' to determine the
  voltage boundary of vproc regulator.

Moreover, to prevent infinite loop when tracking voltage, we calculate the
maximum value for each platform data.
We assume min voltage is 0 and tracking target voltage using
min_volt_shift for each iteration.
The retry_max is 3 times of expeted iteration count.

Signed-off-by: default avatarJia-Wei Chang <jia-wei.chang@mediatek.com>
Signed-off-by: default avatarRex-BC Chen <rex-bc.chen@mediatek.com>
Reviewed-by: default avatarAngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Signed-off-by: default avatarViresh Kumar <viresh.kumar@linaro.org>
parent ead858bd
Loading
Loading
Loading
Loading
+51 −96
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
#include <linux/cpu.h>
#include <linux/cpufreq.h>
#include <linux/cpumask.h>
#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_platform.h>
@@ -15,8 +16,6 @@
#include <linux/pm_opp.h>
#include <linux/regulator/consumer.h>

#define VOLT_TOL		(10000)

struct mtk_cpufreq_platform_data {
	int min_volt_shift;
	int max_volt_shift;
@@ -48,6 +47,7 @@ struct mtk_cpu_dvfs_info {
	bool need_voltage_tracking;
	int pre_vproc;
	const struct mtk_cpufreq_platform_data *soc_data;
	int vtrack_max;
};

static struct platform_device *cpufreq_pdev;
@@ -73,6 +73,7 @@ static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
	struct regulator *proc_reg = info->proc_reg;
	struct regulator *sram_reg = info->sram_reg;
	int pre_vproc, pre_vsram, new_vsram, vsram, vproc, ret;
	int retry = info->vtrack_max;

	pre_vproc = regulator_get_voltage(proc_reg);
	if (pre_vproc < 0) {
@@ -80,91 +81,44 @@ static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
			"invalid Vproc value: %d\n", pre_vproc);
		return pre_vproc;
	}
	/* Vsram should not exceed the maximum allowed voltage of SoC. */
	new_vsram = min(new_vproc + soc_data->min_volt_shift,
			soc_data->sram_max_volt);

	if (pre_vproc < new_vproc) {
		/*
		 * When scaling up voltages, Vsram and Vproc scale up step
		 * by step. At each step, set Vsram to (Vproc + 200mV) first,
		 * then set Vproc to (Vsram - 100mV).
		 * Keep doing it until Vsram and Vproc hit target voltages.
		 */
		do {
	pre_vsram = regulator_get_voltage(sram_reg);
	if (pre_vsram < 0) {
				dev_err(info->cpu_dev,
					"invalid Vsram value: %d\n", pre_vsram);
		dev_err(info->cpu_dev, "invalid Vsram value: %d\n", pre_vsram);
		return pre_vsram;
	}
			pre_vproc = regulator_get_voltage(proc_reg);
			if (pre_vproc < 0) {
				dev_err(info->cpu_dev,
					"invalid Vproc value: %d\n", pre_vproc);
				return pre_vproc;
			}

			vsram = min(new_vsram,
				    pre_vproc + soc_data->min_volt_shift);
	new_vsram = clamp(new_vproc + soc_data->min_volt_shift,
			  soc_data->sram_min_volt, soc_data->sram_max_volt);

			if (vsram + VOLT_TOL >= soc_data->sram_max_volt) {
				vsram = soc_data->sram_max_volt;

				/*
				 * If the target Vsram hits the maximum voltage,
				 * try to set the exact voltage value first.
				 */
	do {
		if (pre_vproc <= new_vproc) {
			vsram = clamp(pre_vproc + soc_data->max_volt_shift,
				      soc_data->sram_min_volt, new_vsram);
			ret = regulator_set_voltage(sram_reg, vsram,
							    vsram);
						    soc_data->sram_max_volt);

			if (ret)
					ret = regulator_set_voltage(sram_reg,
							vsram - VOLT_TOL,
							vsram);
				return ret;

			if (vsram == soc_data->sram_max_volt ||
			    new_vsram == soc_data->sram_min_volt)
				vproc = new_vproc;
			} else {
				ret = regulator_set_voltage(sram_reg, vsram,
							    vsram + VOLT_TOL);

			else
				vproc = vsram - soc_data->min_volt_shift;
			}
			if (ret)
				return ret;

			ret = regulator_set_voltage(proc_reg, vproc,
						    vproc + VOLT_TOL);
						    soc_data->proc_max_volt);
			if (ret) {
				regulator_set_voltage(sram_reg, pre_vsram,
						      pre_vsram);
						      soc_data->sram_max_volt);
				return ret;
			}
		} while (vproc < new_vproc || vsram < new_vsram);
		} else if (pre_vproc > new_vproc) {
		/*
		 * When scaling down voltages, Vsram and Vproc scale down step
		 * by step. At each step, set Vproc to (Vsram - 200mV) first,
		 * then set Vproc to (Vproc + 100mV).
		 * Keep doing it until Vsram and Vproc hit target voltages.
		 */
		do {
			pre_vproc = regulator_get_voltage(proc_reg);
			if (pre_vproc < 0) {
				dev_err(info->cpu_dev,
					"invalid Vproc value: %d\n", pre_vproc);
				return pre_vproc;
			}
			pre_vsram = regulator_get_voltage(sram_reg);
			if (pre_vsram < 0) {
				dev_err(info->cpu_dev,
					"invalid Vsram value: %d\n", pre_vsram);
				return pre_vsram;
			}

			vproc = max(new_vproc,
				    pre_vsram - soc_data->max_volt_shift);
			ret = regulator_set_voltage(proc_reg, vproc,
						    vproc + VOLT_TOL);
						    soc_data->proc_max_volt);
			if (ret)
				return ret;

@@ -174,33 +128,25 @@ static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
				vsram = max(new_vsram,
					    vproc + soc_data->min_volt_shift);

			if (vsram + VOLT_TOL >= soc_data->sram_max_volt) {
				vsram = soc_data->sram_max_volt;

				/*
				 * If the target Vsram hits the maximum voltage,
				 * try to set the exact voltage value first.
				 */
				ret = regulator_set_voltage(sram_reg, vsram,
							    vsram);
				if (ret)
					ret = regulator_set_voltage(sram_reg,
							vsram - VOLT_TOL,
							vsram);
			} else {
			ret = regulator_set_voltage(sram_reg, vsram,
							    vsram + VOLT_TOL);
			}

						    soc_data->sram_max_volt);
			if (ret) {
				regulator_set_voltage(proc_reg, pre_vproc,
						      pre_vproc);
						      soc_data->proc_max_volt);
				return ret;
			}
		} while (vproc > new_vproc + VOLT_TOL ||
			 vsram > new_vsram + VOLT_TOL);
		}

		pre_vproc = vproc;
		pre_vsram = vsram;

		if (--retry < 0) {
			dev_err(info->cpu_dev,
				"over loop count, failed to set voltage\n");
			return -EINVAL;
		}
	} while (vproc != new_vproc || vsram != new_vsram);

	return 0;
}

@@ -261,8 +207,8 @@ static int mtk_cpufreq_set_target(struct cpufreq_policy *policy,
	 * If the new voltage or the intermediate voltage is higher than the
	 * current voltage, scale up voltage first.
	 */
	target_vproc = (inter_vproc > vproc) ? inter_vproc : vproc;
	if (pre_vproc < target_vproc) {
	target_vproc = max(inter_vproc, vproc);
	if (pre_vproc <= target_vproc) {
		ret = mtk_cpufreq_set_voltage(info, target_vproc);
		if (ret) {
			dev_err(cpu_dev,
@@ -417,6 +363,15 @@ static int mtk_cpu_dvfs_info_init(struct mtk_cpu_dvfs_info *info, int cpu)
	 */
	info->need_voltage_tracking = (info->sram_reg != NULL);

	/*
	 * We assume min voltage is 0 and tracking target voltage using
	 * min_volt_shift for each iteration.
	 * The vtrack_max is 3 times of expeted iteration count.
	 */
	info->vtrack_max = 3 * DIV_ROUND_UP(max(info->soc_data->sram_max_volt,
						info->soc_data->proc_max_volt),
					    info->soc_data->min_volt_shift);

	return 0;

out_disable_inter_clock: