linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rex-BC Chen <rex-bc.chen@mediatek.com>
To: <rafael@kernel.org>, <viresh.kumar@linaro.org>,
	<robh+dt@kernel.org>, <krzk+dt@kernel.org>
Cc: <matthias.bgg@gmail.com>, <jia-wei.chang@mediatek.com>,
	<roger.lu@mediatek.com>, <hsinyi@google.com>,
	<linux-pm@vger.kernel.org>, <devicetree@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-mediatek@lists.infradead.org>,
	<Project_Global_Chrome_Upstream_Group@mediatek.com>,
	Rex-BC Chen <rex-bc.chen@mediatek.com>
Subject: [PATCH V2 08/15] cpufreq: mediatek: Move voltage limits to platform data
Date: Fri, 8 Apr 2022 12:59:01 +0800	[thread overview]
Message-ID: <20220408045908.21671-9-rex-bc.chen@mediatek.com> (raw)
In-Reply-To: <20220408045908.21671-1-rex-bc.chen@mediatek.com>

From: Jia-Wei Chang <jia-wei.chang@mediatek.com>

Voltages and shifts are defined as macros originally.
There are different requirements of these values for each MediaTek SoCs.
Therefore, we add the platform data and move these values into it.

Signed-off-by: Jia-Wei Chang <jia-wei.chang@mediatek.com>
Signed-off-by: Rex-BC Chen <rex-bc.chen@mediatek.com>
---
 drivers/cpufreq/mediatek-cpufreq.c | 90 ++++++++++++++++++++----------
 1 file changed, 61 insertions(+), 29 deletions(-)

diff --git a/drivers/cpufreq/mediatek-cpufreq.c b/drivers/cpufreq/mediatek-cpufreq.c
index 1369da62780a..0be5609ee82e 100644
--- a/drivers/cpufreq/mediatek-cpufreq.c
+++ b/drivers/cpufreq/mediatek-cpufreq.c
@@ -10,15 +10,21 @@
 #include <linux/cpumask.h>
 #include <linux/module.h>
 #include <linux/of.h>
+#include <linux/of_platform.h>
 #include <linux/platform_device.h>
 #include <linux/pm_opp.h>
 #include <linux/regulator/consumer.h>
 
-#define MIN_VOLT_SHIFT		(100000)
-#define MAX_VOLT_SHIFT		(200000)
-#define MAX_VOLT_LIMIT		(1150000)
 #define VOLT_TOL		(10000)
 
+struct mtk_cpufreq_platform_data {
+	int min_volt_shift;
+	int max_volt_shift;
+	int proc_max_volt;
+	int sram_min_volt;
+	int sram_max_volt;
+};
+
 /*
  * The struct mtk_cpu_dvfs_info holds necessary information for doing CPU DVFS
  * on each CPU power/clock domain of Mediatek SoCs. Each CPU cluster in
@@ -46,8 +52,11 @@ struct mtk_cpu_dvfs_info {
 	struct notifier_block opp_nb;
 	int opp_cpu;
 	unsigned long opp_freq;
+	const struct mtk_cpufreq_platform_data *soc_data;
 };
 
+static struct platform_device *cpufreq_pdev;
+
 static LIST_HEAD(dvfs_info_list);
 
 static struct mtk_cpu_dvfs_info *mtk_cpu_dvfs_info_lookup(int cpu)
@@ -65,6 +74,7 @@ static struct mtk_cpu_dvfs_info *mtk_cpu_dvfs_info_lookup(int cpu)
 static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
 					int new_vproc)
 {
+	const struct mtk_cpufreq_platform_data *soc_data = info->soc_data;
 	struct regulator *proc_reg = info->proc_reg;
 	struct regulator *sram_reg = info->sram_reg;
 	int old_vproc, old_vsram, new_vsram, vsram, vproc, ret;
@@ -75,7 +85,8 @@ static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
 		return old_vproc;
 	}
 	/* Vsram should not exceed the maximum allowed voltage of SoC. */
-	new_vsram = min(new_vproc + MIN_VOLT_SHIFT, MAX_VOLT_LIMIT);
+	new_vsram = min(new_vproc + soc_data->min_volt_shift,
+			soc_data->sram_max_volt);
 
 	if (old_vproc < new_vproc) {
 		/*
@@ -98,10 +109,11 @@ static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
 				return old_vproc;
 			}
 
-			vsram = min(new_vsram, old_vproc + MAX_VOLT_SHIFT);
+			vsram = min(new_vsram,
+				    old_vproc + soc_data->min_volt_shift);
 
-			if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) {
-				vsram = MAX_VOLT_LIMIT;
+			if (vsram + VOLT_TOL >= soc_data->sram_max_volt) {
+				vsram = soc_data->sram_max_volt;
 
 				/*
 				 * If the target Vsram hits the maximum voltage,
@@ -119,7 +131,7 @@ static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
 				ret = regulator_set_voltage(sram_reg, vsram,
 							    vsram + VOLT_TOL);
 
-				vproc = vsram - MIN_VOLT_SHIFT;
+				vproc = vsram - soc_data->min_volt_shift;
 			}
 			if (ret)
 				return ret;
@@ -153,7 +165,8 @@ static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
 				return old_vsram;
 			}
 
-			vproc = max(new_vproc, old_vsram - MAX_VOLT_SHIFT);
+			vproc = max(new_vproc,
+				    old_vsram - soc_data->max_volt_shift);
 			ret = regulator_set_voltage(proc_reg, vproc,
 						    vproc + VOLT_TOL);
 			if (ret)
@@ -162,10 +175,11 @@ static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
 			if (vproc == new_vproc)
 				vsram = new_vsram;
 			else
-				vsram = max(new_vsram, vproc + MIN_VOLT_SHIFT);
+				vsram = max(new_vsram,
+					    vproc + soc_data->min_volt_shift);
 
-			if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) {
-				vsram = MAX_VOLT_LIMIT;
+			if (vsram + VOLT_TOL >= soc_data->sram_max_volt) {
+				vsram = soc_data->sram_max_volt;
 
 				/*
 				 * If the target Vsram hits the maximum voltage,
@@ -196,13 +210,14 @@ static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
 
 static int mtk_cpufreq_set_voltage(struct mtk_cpu_dvfs_info *info, int vproc)
 {
+	const struct mtk_cpufreq_platform_data *soc_data = info->soc_data;
 	int ret;
 
 	if (info->need_voltage_tracking)
 		ret = mtk_cpufreq_voltage_tracking(info, vproc);
 	else
 		ret = regulator_set_voltage(info->proc_reg, vproc,
-					    MAX_VOLT_LIMIT);
+					    soc_data->proc_max_volt);
 	if (!ret)
 		info->old_vproc = vproc;
 
@@ -576,9 +591,17 @@ static struct cpufreq_driver mtk_cpufreq_driver = {
 
 static int mtk_cpufreq_probe(struct platform_device *pdev)
 {
+	const struct of_device_id *match;
 	struct mtk_cpu_dvfs_info *info, *tmp;
 	int cpu, ret;
 
+	match = dev_get_platdata(&pdev->dev);
+	if (!match || !match->data) {
+		dev_err(&pdev->dev,
+			"failed to get mtk cpufreq platform data\n");
+		return -ENODEV;
+	}
+
 	for_each_possible_cpu(cpu) {
 		info = mtk_cpu_dvfs_info_lookup(cpu);
 		if (info)
@@ -590,6 +613,7 @@ static int mtk_cpufreq_probe(struct platform_device *pdev)
 			goto release_dvfs_info_list;
 		}
 
+		info->soc_data = match->data;
 		ret = mtk_cpu_dvfs_info_init(info, cpu);
 		if (ret) {
 			dev_err(&pdev->dev,
@@ -625,20 +649,27 @@ static struct platform_driver mtk_cpufreq_platdrv = {
 	.probe		= mtk_cpufreq_probe,
 };
 
+static const struct mtk_cpufreq_platform_data mtk_platform_data = {
+	.min_volt_shift = 100000,
+	.max_volt_shift = 200000,
+	.proc_max_volt = 1150000,
+	.sram_min_volt = 0,
+	.sram_max_volt = 1150000,
+};
+
 /* List of machines supported by this driver */
 static const struct of_device_id mtk_cpufreq_machines[] __initconst = {
-	{ .compatible = "mediatek,mt2701", },
-	{ .compatible = "mediatek,mt2712", },
-	{ .compatible = "mediatek,mt7622", },
-	{ .compatible = "mediatek,mt7623", },
-	{ .compatible = "mediatek,mt8167", },
-	{ .compatible = "mediatek,mt817x", },
-	{ .compatible = "mediatek,mt8173", },
-	{ .compatible = "mediatek,mt8176", },
-	{ .compatible = "mediatek,mt8183", },
-	{ .compatible = "mediatek,mt8365", },
-	{ .compatible = "mediatek,mt8516", },
-
+	{ .compatible = "mediatek,mt2701", .data = &mtk_platform_data },
+	{ .compatible = "mediatek,mt2712", .data = &mtk_platform_data },
+	{ .compatible = "mediatek,mt7622", .data = &mtk_platform_data },
+	{ .compatible = "mediatek,mt7623", .data = &mtk_platform_data },
+	{ .compatible = "mediatek,mt8167", .data = &mtk_platform_data },
+	{ .compatible = "mediatek,mt817x", .data = &mtk_platform_data },
+	{ .compatible = "mediatek,mt8173", .data = &mtk_platform_data },
+	{ .compatible = "mediatek,mt8176", .data = &mtk_platform_data },
+	{ .compatible = "mediatek,mt8183", .data = &mtk_platform_data },
+	{ .compatible = "mediatek,mt8365", .data = &mtk_platform_data },
+	{ .compatible = "mediatek,mt8516", .data = &mtk_platform_data },
 	{ }
 };
 MODULE_DEVICE_TABLE(of, mtk_cpufreq_machines);
@@ -647,7 +678,6 @@ static int __init mtk_cpufreq_driver_init(void)
 {
 	struct device_node *np;
 	const struct of_device_id *match;
-	struct platform_device *pdev;
 	int err;
 
 	np = of_find_node_by_path("/");
@@ -671,11 +701,12 @@ static int __init mtk_cpufreq_driver_init(void)
 	 * and the device registration codes are put here to handle defer
 	 * probing.
 	 */
-	pdev = platform_device_register_simple("mtk-cpufreq", -1, NULL, 0);
-	if (IS_ERR(pdev)) {
+	cpufreq_pdev = platform_device_register_data(NULL, "mtk-cpufreq", -1,
+						     match, sizeof(*match));
+	if (IS_ERR(cpufreq_pdev)) {
 		pr_err("failed to register mtk-cpufreq platform device\n");
 		platform_driver_unregister(&mtk_cpufreq_platdrv);
-		return PTR_ERR(pdev);
+		return PTR_ERR(cpufreq_pdev);
 	}
 
 	return 0;
@@ -684,6 +715,7 @@ module_init(mtk_cpufreq_driver_init)
 
 static void __exit mtk_cpufreq_driver_exit(void)
 {
+	platform_device_unregister(cpufreq_pdev);
 	platform_driver_unregister(&mtk_cpufreq_platdrv);
 }
 module_exit(mtk_cpufreq_driver_exit)
-- 
2.18.0


  parent reply	other threads:[~2022-04-08  5:00 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-08  4:58 [PATCH V2 00/15] cpufreq: mediatek: Cleanup and support MT8183 and MT8186 Rex-BC Chen
2022-04-08  4:58 ` [PATCH V2 01/15] dt-bindings: cpufreq: mediatek: Add MediaTek CCI property Rex-BC Chen
2022-04-08  8:10   ` Krzysztof Kozlowski
2022-04-08 10:24     ` Rex-BC Chen
2022-04-08 11:49       ` Krzysztof Kozlowski
2022-04-11  6:48         ` Rex-BC Chen
2022-04-08  4:58 ` [PATCH V2 02/15] cpufreq: mediatek: Use module_init and add module_exit Rex-BC Chen
2022-04-08 13:36   ` AngeloGioacchino Del Regno
2022-04-11  3:17     ` Viresh Kumar
2022-04-08  4:58 ` [PATCH V2 03/15] cpufreq: mediatek: Cleanup variables and error handling in mtk_cpu_dvfs_info_init() Rex-BC Chen
2022-04-08 13:36   ` AngeloGioacchino Del Regno
2022-04-11  3:20     ` Viresh Kumar
2022-04-08  4:58 ` [PATCH V2 04/15] cpufreq: mediatek: Remove unused headers Rex-BC Chen
2022-04-08 13:36   ` AngeloGioacchino Del Regno
2022-04-11  3:21     ` Viresh Kumar
2022-04-08  4:58 ` [PATCH V2 05/15] cpufreq: mediatek: Enable clocks and regulators Rex-BC Chen
2022-04-08 13:36   ` AngeloGioacchino Del Regno
2022-04-11  3:22     ` Viresh Kumar
2022-04-08  4:58 ` [PATCH V2 06/15] cpufreq: mediatek: Record previous target vproc value Rex-BC Chen
2022-04-08 13:36   ` AngeloGioacchino Del Regno
2022-04-11 11:35     ` Rex-BC Chen
2022-04-11  3:26   ` Viresh Kumar
2022-04-11 11:33     ` Rex-BC Chen
2022-04-08  4:59 ` Rex-BC Chen [this message]
2022-04-08 13:36   ` [PATCH V2 08/15] cpufreq: mediatek: Move voltage limits to platform data AngeloGioacchino Del Regno
2022-04-11 11:18     ` Rex-BC Chen
2022-04-08  4:59 ` [PATCH V2 09/15] cpufreq: mediatek: Add .get function Rex-BC Chen
2022-04-08  4:59 ` [PATCH V2 10/15] cpufreq: mediatek: Make sram regulator optional Rex-BC Chen
2022-04-08 13:37   ` AngeloGioacchino Del Regno
2022-04-08 20:32   ` Kevin Hilman
2022-04-14 10:53     ` Rex-BC Chen
2022-04-14 17:20       ` Kevin Hilman
2022-04-08  4:59 ` [PATCH V2 11/15] cpufreq: mediatek: Update logic of voltage_tracking() Rex-BC Chen
2022-04-08 21:08   ` Kevin Hilman
2022-04-14 11:30     ` Rex-BC Chen
2022-04-08  4:59 ` [PATCH V2 12/15] cpufreq: mediatek: Use maximum voltage in init stage Rex-BC Chen
2022-04-08 13:37   ` AngeloGioacchino Del Regno
2022-04-12 11:24     ` Rex-BC Chen
2022-04-14  3:40     ` Rex-BC Chen
2022-04-08  4:59 ` [PATCH V2 13/15] cpufreq: mediatek: Link CCI device to CPU Rex-BC Chen
2022-04-08 13:37   ` AngeloGioacchino Del Regno
2022-04-11 11:50     ` Rex-BC Chen
2022-04-08 20:54   ` Kevin Hilman
2022-04-11 11:51     ` Rex-BC Chen
2022-04-11 12:31     ` Rex-BC Chen
2022-04-11 18:13       ` Kevin Hilman
2022-04-12 12:26         ` Rex-BC Chen
2022-04-12 18:50           ` Kevin Hilman
2022-04-13 11:32             ` Rex-BC Chen
2022-04-13 21:41               ` Kevin Hilman
2022-04-14  2:32                 ` Rex-BC Chen
2022-04-14 21:48                   ` Kevin Hilman
2022-04-15  2:31                     ` Rex-BC Chen
2022-04-19 18:16                       ` Kevin Hilman
2022-04-08  4:59 ` [PATCH V2 14/15] cpufreq: mediatek: Add support for MT8186 Rex-BC Chen
2022-04-08 13:37   ` AngeloGioacchino Del Regno
2022-04-08 21:10   ` Kevin Hilman
2022-04-11 11:14     ` Rex-BC Chen
2022-04-08  4:59 ` [PATCH V2 15/15] cpufreq: mediatek: Use device print to show logs Rex-BC Chen
2022-04-08 13:37   ` AngeloGioacchino Del Regno
2022-04-11  3:29   ` Viresh Kumar
2022-04-11 11:09     ` Rex-BC Chen
     [not found] ` <20220408045908.21671-8-rex-bc.chen@mediatek.com>
2022-04-08 13:36   ` [PATCH V2 07/15] cpufreq: mediatek: Add opp notification for SVS support AngeloGioacchino Del Regno
2022-04-08 20:29   ` Kevin Hilman
     [not found]     ` <3b7bf25a3da6c8f780c87784c1f796bf1e464238.camel@mediatek.com>
2022-04-11 18:09       ` Kevin Hilman
     [not found]         ` <dfe2d3e3401a6f2a7be9db4e8a0590d3dd9a6969.camel@mediatek.com>
2022-04-12 18:04           ` Kevin Hilman
2022-04-08 21:11 ` [PATCH V2 00/15] cpufreq: mediatek: Cleanup and support MT8183 and MT8186 Kevin Hilman
2022-04-09  1:05   ` Hsin-Yi Wang
2022-04-11 11:37     ` Rex-BC Chen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220408045908.21671-9-rex-bc.chen@mediatek.com \
    --to=rex-bc.chen@mediatek.com \
    --cc=Project_Global_Chrome_Upstream_Group@mediatek.com \
    --cc=devicetree@vger.kernel.org \
    --cc=hsinyi@google.com \
    --cc=jia-wei.chang@mediatek.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=matthias.bgg@gmail.com \
    --cc=rafael@kernel.org \
    --cc=robh+dt@kernel.org \
    --cc=roger.lu@mediatek.com \
    --cc=viresh.kumar@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).