Skip to contentSkip to conversation

cpufreq: Add i.mx8mq support

PATCH In review Abel Vesa 2 participants 5 messages Feb 12, 2019

Subsystems (2)
lore
Share & tools
lore

Abel Vesa

author
#0 lore raw

PATCH 1/2 cpufreq: Add i.mx8mq support

From: Anson Huang <[email protected]>

Add i.MX8MQ cpufreq support, current version of EVK board does NOT support voltage scale, but next version will add this support, so this driver only supports cpu frequency scale, voltage scale will be added later once new board available.

A53 CPU clock normally is from ARM_PLL, but during ARM_PLL relock window, it will be switched to SYS1_PLL_800M to avoid clock missing, and after arm pll relock done, it will be switched back.

---
 drivers/cpufreq/Kconfig.arm      |   8 ++
 drivers/cpufreq/Makefile         |   1 +
 drivers/cpufreq/imx8mq-cpufreq.c | 223 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 232 insertions(+)
 create mode 100644 drivers/cpufreq/imx8mq-cpufreq.c
drivers/cpufreq/Kconfig.arm +8 -0
index 179a1d3..9d8001c 100644
@@ -92,6 +92,14 @@ config ARM_IMX6Q_CPUFREQ
 
 	  If in doubt, say N.
 
+config ARM_IMX8MQ_CPUFREQ
+	tristate "NXP i.MX8MQ cpufreq support"
+	select PM_OPP
+	help
+	  This adds cpufreq driver support for NXP i.MX8MQ series SoCs.
+
+	  If in doubt, say N.
+
 config ARM_KIRKWOOD_CPUFREQ
 	def_bool MACH_KIRKWOOD
 	help
drivers/cpufreq/Makefile +1 -0
index 689b26c..fe5416c 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_ACPI_CPPC_CPUFREQ)		+= cppc_cpufreq.o
 obj-$(CONFIG_ARCH_DAVINCI)		+= davinci-cpufreq.o
 obj-$(CONFIG_ARM_HIGHBANK_CPUFREQ)	+= highbank-cpufreq.o
 obj-$(CONFIG_ARM_IMX6Q_CPUFREQ)		+= imx6q-cpufreq.o
+obj-$(CONFIG_ARM_IMX8MQ_CPUFREQ)	+= imx8mq-cpufreq.o
 obj-$(CONFIG_ARM_KIRKWOOD_CPUFREQ)	+= kirkwood-cpufreq.o
 obj-$(CONFIG_ARM_MEDIATEK_CPUFREQ)	+= mediatek-cpufreq.o
 obj-$(CONFIG_MACH_MVEBU_V7)		+= mvebu-cpufreq.o
drivers/cpufreq/imx8mq-cpufreq.c new file +223 -0

223 lines not on this page · raw message

Signature · 1 line
2.7.4
author
#1 lore raw

PATCH 2/2 soc: imx8: Add cpufreq registering and speed grading check for i.MX8MQ

From: Anson Huang <[email protected]>

Register cpu-freq platform driver for i.MX8.
i.MX8MQ has different parts like consumer, industrial and auto etc., different parts have different cpufreq set-points, this patch adds fuse check to select correct cpufreq set-points for each part. The default dtb has all set-points available, then kernel will check fuse to disable those unused set-points, definition as below:

OCOTP offset 0x440, bit [7:6]

'00' - Consumer 0C to 95C
'01' - Ext. Consumer -20C to 105C
'10' - Industrial -40C to 105C
'11' - Automotive -40C to 125C

cpu-freq set-points definition as below (datasheet Rev-E):

Normal Over-Drive
Consumer [email protected] 1.5GHz@1V
Industrial [email protected] 1.3GHz@1V

---
 drivers/soc/imx/Makefile   |   1 +
 drivers/soc/imx/soc-imx8.c | 107 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+)
 create mode 100644 drivers/soc/imx/soc-imx8.c
drivers/soc/imx/Makefile +1 -0
index 506a6f3..d6b529e0 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_HAVE_IMX_GPC) += gpc.o
 obj-$(CONFIG_IMX_GPCV2_PM_DOMAINS) += gpcv2.o
+obj-$(CONFIG_ARCH_MXC) += soc-imx8.o
drivers/soc/imx/soc-imx8.c new file +107 -0

107 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH 2/2] soc: imx8: Add cpufreq registering and speed grading check for i.MX8MQ

Hi Abel,

Am Dienstag, den 12.02.2019, 12:21 +0000 schrieb Abel Vesa:

Quoted 9 lines

From: Anson Huang <[email protected]>

Register cpu-freq platform driver for i.MX8.
i.MX8MQ has different parts like consumer, industrial and auto etc., different parts have different cpufreq set-points, this patch adds fuse check to select correct cpufreq set-points for each part. The default dtb has all set-points available, then kernel will check fuse to disable those unused set-points, definition as below:

Quoted 6 lines

OCOTP offset 0x440, bit [7:6]

'00' - Consumer 0C to 95C
'01' - Ext. Consumer -20C to 105C
'10' - Industrial -40C to 105C
'11' - Automotive -40C to 125C

Shouldn't this read 0x440 [9:8] (SPEED_GRADING) instead? According to the RM those 2 bits directly encode the maximum ARM core frequency, so we don't need to guess from the target market.

Regards,
Lucas

#3 lore raw
In reply to Abel Vesa

Hi Abel,

we really don't want another platform specific cpufreq driver in mainline. The CPU clock change dance should be abstracted in the imx8mq-clk driver, just like we do on i.MX5 and i.MX7. This would allow us to reuse the cpufreq-dt driver, like we do on those platforms.

Regards,
Lucas

Am Dienstag, den 12.02.2019, 12:21 +0000 schrieb Abel Vesa:

Quoted 282 lines
Quoted 1 line

From: Anson Huang <[email protected]>

Add i.MX8MQ cpufreq support, current version of EVK board does NOT support voltage scale, but next version will add this support, so this driver only supports cpu frequency scale, voltage scale will be added later once new board available.

A53 CPU clock normally is from ARM_PLL, but during ARM_PLL relock window, it will be switched to SYS1_PLL_800M to avoid clock missing, and after arm pll relock done, it will be switched back.

Quoted 2 lines

---
 drivers/cpufreq/Kconfig.arm      |   8 ++ drivers/cpufreq/Makefile         |   1 + drivers/cpufreq/imx8mq-cpufreq.c | 223 +++++++++++++++++++++++++++++++++++++++

 3 files changed, 232 insertions(+)

 create mode 100644 drivers/cpufreq/imx8mq-cpufreq.c

drivers/cpufreq/Kconfig.arm +0 -0
index 179a1d3..9d8001c 100644
@@ -92,6 +92,14 @@ config ARM_IMX6Q_CPUFREQ

Quoted 1 line

    If in doubt, say N.


+config ARM_IMX8MQ_CPUFREQ

drivers/cpufreq/Kconfig.arm config ARM_IMX6Q_CPUFREQ · line 99

Quoted 4 lines
drivers/cpufreq/Kconfig.arm +4 -0
+	tristate "NXP i.MX8MQ cpufreq support"
+	select PM_OPP
+	help
+	  This adds cpufreq driver support for NXP i.MX8MQ series SoCs.

+

drivers/cpufreq/Kconfig.arm config ARM_IMX6Q_CPUFREQ · line 101

Quoted 1 line
drivers/cpufreq/Kconfig.arm +1 -0
+	  If in doubt, say N.

+
 config ARM_KIRKWOOD_CPUFREQ

Quoted 2 lines

  def_bool MACH_KIRKWOOD
  help

drivers/cpufreq/Makefile +0 -0
index 689b26c..fe5416c 100644
Quoted 8 lines
(quoted diff) +0 -0
@@ -56,6 +56,7 @@ obj-$(CONFIG_ACPI_CPPC_CPUFREQ)		+= cppc_cpufreq.o

 obj-$(CONFIG_ARCH_DAVINCI) += davinci-cpufreq.o
 obj-$(CONFIG_ARM_HIGHBANK_CPUFREQ) += highbank-cpufreq.o
 obj-$(CONFIG_ARM_IMX6Q_CPUFREQ) += imx6q-cpufreq.o
+obj-$(CONFIG_ARM_IMX8MQ_CPUFREQ) += imx8mq-cpufreq.o
 obj-$(CONFIG_ARM_KIRKWOOD_CPUFREQ) += kirkwood-cpufreq.o
 obj-$(CONFIG_ARM_MEDIATEK_CPUFREQ) += mediatek-cpufreq.o
 obj-$(CONFIG_MACH_MVEBU_V7) += mvebu-cpufreq.o

drivers/cpufreq/imx8mq-cpufreq.c new file +30 -0
new file mode 100644
index 0000000..ee24fab
@@ -0,0 +1,223 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2019 NXP
+ */
+
+#include <linux/clk.h>
+#include <linux/cpu.h>
+#include <linux/cpufreq.h>
+#include <linux/err.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/of.h>
+#include <linux/pm_opp.h>
+#include <linux/platform_device.h>
+#include <linux/suspend.h>
+
+static struct device *cpu_dev;
+static bool free_opp;
+static struct cpufreq_frequency_table *freq_table;
+static struct mutex set_cpufreq_lock;
+static unsigned int transition_latency;
+static unsigned int suspend_freq;
+static struct clk *a53_clk;
+static struct clk *arm_a53_src_clk;
+static struct clk *arm_pll_clk;
+static struct clk *arm_pll_out_clk;
+static struct clk *sys1_pll_800m_clk;
+
+static int imx8mq_set_target(struct cpufreq_policy *policy, unsigned int index)
+{

drivers/cpufreq/imx8mq-cpufreq.c line 34

Quoted 4 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +4 -0
+	struct dev_pm_opp *opp;
+	unsigned long freq_hz;
+	unsigned int old_freq, new_freq;
+	int ret;

+

drivers/cpufreq/imx8mq-cpufreq.c line 36

Quoted 1 line
drivers/cpufreq/imx8mq-cpufreq.c new file +1 -0
+	mutex_lock(&set_cpufreq_lock);

+

drivers/cpufreq/imx8mq-cpufreq.c line 40

Quoted 3 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +3 -0
+	new_freq = freq_table[index].frequency;
+	freq_hz = new_freq * 1000;
+	old_freq = policy->cur;

+

drivers/cpufreq/imx8mq-cpufreq.c line 50

Quoted 9 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +9 -0
+	rcu_read_lock();
+	opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);
+	if (IS_ERR(opp)) {
+		rcu_read_unlock();
+		dev_err(cpu_dev, "failed to find OPP for %ld\n", freq_hz);
+		mutex_unlock(&set_cpufreq_lock);
+		return PTR_ERR(opp);
+	}
+	rcu_read_unlock();

+

drivers/cpufreq/imx8mq-cpufreq.c line 53

Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+	dev_dbg(cpu_dev, "%u MHz --> %u MHz\n",
+		old_freq / 1000, new_freq / 1000);

+

drivers/cpufreq/imx8mq-cpufreq.c line 57

Quoted 3 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +3 -0
+	clk_set_parent(arm_a53_src_clk, sys1_pll_800m_clk);
+	clk_set_rate(arm_pll_clk, new_freq * 1000);
+	clk_set_parent(arm_a53_src_clk, arm_pll_out_clk);

+

drivers/cpufreq/imx8mq-cpufreq.c line 62

Quoted 4 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +4 -0
+	/* Ensure the arm clock divider is what we expect */
+	ret = clk_set_rate(a53_clk, new_freq * 1000);
+	if (ret)
+		dev_err(cpu_dev, "failed to set clock rate: %d\n", ret);

+

Quoted 2 lines
(quoted diff) +2 -0
+	mutex_unlock(&set_cpufreq_lock);
+	return ret;
drivers/cpufreq/imx8mq-cpufreq.c new file +4 -0
+}
+
+static int imx8mq_cpufreq_init(struct cpufreq_policy *policy)
+{

Review

Quoted 1 line
(quoted diff) +1 -0
+	int ret;

+

drivers/cpufreq/imx8mq-cpufreq.c line 74

Quoted 3 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +3 -0
+	policy->clk = a53_clk;
+	policy->cur = clk_get_rate(a53_clk) / 1000;
+	policy->suspend_freq = suspend_freq;

+

drivers/cpufreq/imx8mq-cpufreq.c line 80

Quoted 5 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +5 -0
+	ret = cpufreq_generic_init(policy, freq_table, transition_latency);
+	if (ret) {
+		dev_err(cpu_dev, "imx8mq cpufreq init failed!\n");
+		return ret;
+	}

+

Quoted 1 line
(quoted diff) +1 -0
+	return 0;
drivers/cpufreq/imx8mq-cpufreq.c new file +3 -0
+}
+
+static struct cpufreq_driver imx8mq_cpufreq_driver = {

drivers/cpufreq/imx8mq-cpufreq.c line 92

Quoted 7 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +7 -0
+	.flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
+	.verify = cpufreq_generic_frequency_table_verify,
+	.target_index = imx8mq_set_target,
+	.get = cpufreq_generic_get,
+	.init = imx8mq_cpufreq_init,
+	.name = "imx8mq-cpufreq",
+	.attr = cpufreq_generic_attr,

+#ifdef CONFIG_PM

Quoted 1 line
drivers/cpufreq/imx8mq-cpufreq.c new file +1 -0
+	.suspend = cpufreq_generic_suspend,
drivers/cpufreq/imx8mq-cpufreq.c new file +5 -0
+#endif
+};
+
+static int imx8mq_cpufreq_probe(struct platform_device *pdev)
+{

drivers/cpufreq/imx8mq-cpufreq.c line 101

Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+	struct device_node *np;
+	int num, ret;

+

drivers/cpufreq/imx8mq-cpufreq.c line 107

Quoted 5 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +5 -0
+	cpu_dev = get_cpu_device(0);
+	if (!cpu_dev) {
+		pr_err("failed to get cpu0 device\n");
+		return -ENODEV;
+	}

+

drivers/cpufreq/imx8mq-cpufreq.c line 113

Quoted 5 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +5 -0
+	np = of_node_get(cpu_dev->of_node);
+	if (!np) {
+		dev_err(cpu_dev, "failed to find cpu0 node\n");
+		return -ENOENT;
+	}

+

drivers/cpufreq/imx8mq-cpufreq.c line 126

Quoted 12 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +12 -0
+	a53_clk = clk_get(cpu_dev, "a53");
+	arm_a53_src_clk = clk_get(cpu_dev, "arm_a53_src");
+	arm_pll_clk = clk_get(cpu_dev, "arm_pll");
+	arm_pll_out_clk = clk_get(cpu_dev, "arm_pll_out");
+	sys1_pll_800m_clk = clk_get(cpu_dev, "sys1_pll_800m");
+	if (IS_ERR(a53_clk) || IS_ERR(arm_a53_src_clk)
+		|| IS_ERR(arm_pll_out_clk) || IS_ERR(arm_pll_clk)
+		|| IS_ERR(sys1_pll_800m_clk)) {
+		dev_err(cpu_dev, "failed to get clocks\n");
+		ret = -ENOENT;
+		goto put_clk;
+	}

+

drivers/cpufreq/imx8mq-cpufreq.c line 139

Quoted 12 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +12 -0
+	/*
+	 * We expect an OPP table supplied by platform.
+	 * Just, incase the platform did not supply the OPP
+	 * table, it will try to get it.
+	 */
+	num = dev_pm_opp_get_opp_count(cpu_dev);
+	if (num < 0) {
+		ret = dev_pm_opp_of_add_table(cpu_dev);
+		if (ret < 0) {
+			dev_err(cpu_dev, "failed to init OPP table: %d\n", ret);
+			goto put_clk;
+		}

+

drivers/cpufreq/imx8mq-cpufreq.c line 142

Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+		/* Because we have added the OPPs here, we must free them */
+		free_opp = true;

+

drivers/cpufreq/imx8mq-cpufreq.c line 150

Quoted 7 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +7 -0
+		num = dev_pm_opp_get_opp_count(cpu_dev);
+		if (num < 0) {
+			ret = num;
+			dev_err(cpu_dev, "no OPP table is found: %d\n", ret);
+			goto out_free_opp;
+		}
+	}

+

drivers/cpufreq/imx8mq-cpufreq.c line 156

Quoted 5 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +5 -0
+	ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
+	if (ret) {
+		dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
+		goto out_free_opp;
+	}

+

drivers/cpufreq/imx8mq-cpufreq.c line 159

Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+	/* use MAX freq to suspend */
+	suspend_freq = freq_table[num - 1].frequency;

+

drivers/cpufreq/imx8mq-cpufreq.c line 162

Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+	if (of_property_read_u32(np, "clock-latency", &transition_latency))
+		transition_latency = CPUFREQ_ETERNAL;

+

drivers/cpufreq/imx8mq-cpufreq.c line 164

Quoted 1 line
drivers/cpufreq/imx8mq-cpufreq.c new file +1 -0
+	mutex_init(&set_cpufreq_lock);

+

drivers/cpufreq/imx8mq-cpufreq.c line 170

Quoted 5 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +5 -0
+	ret = cpufreq_register_driver(&imx8mq_cpufreq_driver);
+	if (ret) {
+		dev_err(cpu_dev, "failed register driver: %d\n", ret);
+		goto free_freq_table;
+	}

+

drivers/cpufreq/imx8mq-cpufreq.c line 173

Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+	of_node_put(np);
+	dev_info(cpu_dev, "registered imx8mq-cpufreq\n");

+

Quoted 1 line
(quoted diff) +1 -0
+	return 0;
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+
+free_freq_table:

Review

Quoted 1 line
(quoted diff) +1 -0
+	dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);

+out_free_opp:

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (free_opp)
+		dev_pm_opp_of_remove_table(cpu_dev);

+put_clk:

Quoted 12 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +12 -0
+	if (!IS_ERR(a53_clk))
+		clk_put(a53_clk);
+	if (!IS_ERR(arm_a53_src_clk))
+		clk_put(arm_a53_src_clk);
+	if (!IS_ERR(arm_pll_clk))
+		clk_put(arm_pll_clk);
+	if (!IS_ERR(arm_pll_out_clk))
+		clk_put(arm_pll_out_clk);
+	if (!IS_ERR(sys1_pll_800m_clk))
+		clk_put(sys1_pll_800m_clk);
+	of_node_put(np);
+	return ret;
drivers/cpufreq/imx8mq-cpufreq.c new file +4 -0
+}
+
+static int imx8mq_cpufreq_remove(struct platform_device *pdev)
+{

drivers/cpufreq/imx8mq-cpufreq.c line 207

Quoted 9 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +9 -0
+	cpufreq_unregister_driver(&imx8mq_cpufreq_driver);
+	dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
+	if (free_opp)
+		dev_pm_opp_of_remove_table(cpu_dev);
+	clk_put(a53_clk);
+	clk_put(arm_a53_src_clk);
+	clk_put(arm_pll_clk);
+	clk_put(arm_pll_out_clk);
+	clk_put(sys1_pll_800m_clk);

+

Quoted 1 line
(quoted diff) +1 -0
+	return 0;
drivers/cpufreq/imx8mq-cpufreq.c new file +3 -0
+}
+
+static struct platform_driver imx8mq_cpufreq_platdrv = {
Quoted 5 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +1 -0
+	.driver = {
Quoted 1 line
(quoted diff) +1 -0
+		.name	= "imx8mq-cpufreq",
drivers/cpufreq/imx8mq-cpufreq.c new file +1 -0
+	},
Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+	.probe		= imx8mq_cpufreq_probe,
+	.remove		= imx8mq_cpufreq_remove,
drivers/cpufreq/imx8mq-cpufreq.c new file +3 -0
+};
+module_platform_driver(imx8mq_cpufreq_platdrv);
+
Quoted 1 line

+MODULE_AUTHOR("Anson Huang <[email protected]>");

drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+MODULE_DESCRIPTION("Freescale i.MX8MQ cpufreq driver");
+MODULE_LICENSE("GPL");
author
#4 lore raw
Quoted 7 lines · Lucas Stach

Hi Abel,

we really don't want another platform specific cpufreq driver in mainline. The CPU clock change dance should be abstracted in the imx8mq-clk driver, just like we do on i.MX5 and i.MX7. This would allow us to reuse the cpufreq-dt driver, like we do on those platforms.

Fair enough. This is what we had in our internal tree. Will rewrite as suggested then.
Thanks.

Quoted 286 lines

Regards,
Lucas

Am Dienstag, den 12.02.2019, 12:21 +0000 schrieb Abel Vesa:

Quoted 282 lines
Quoted 1 line

From: Anson Huang <[email protected]>

Add i.MX8MQ cpufreq support, current version of EVK board does NOT support voltage scale, but next version will add this support, so this driver only supports cpu frequency scale, voltage scale will be added later once new board available.

A53 CPU clock normally is from ARM_PLL, but during ARM_PLL relock window, it will be switched to SYS1_PLL_800M to avoid clock missing, and after arm pll relock done, it will be switched back.

Quoted 2 lines

---
 drivers/cpufreq/Kconfig.arm      |   8 ++ drivers/cpufreq/Makefile         |   1 + drivers/cpufreq/imx8mq-cpufreq.c | 223 +++++++++++++++++++++++++++++++++++++++

 3 files changed, 232 insertions(+)

 create mode 100644 drivers/cpufreq/imx8mq-cpufreq.c

drivers/cpufreq/Kconfig.arm +0 -0
index 179a1d3..9d8001c 100644
@@ -92,6 +92,14 @@ config ARM_IMX6Q_CPUFREQ

Quoted 1 line

    If in doubt, say N.


+config ARM_IMX8MQ_CPUFREQ

drivers/cpufreq/Kconfig.arm config ARM_IMX6Q_CPUFREQ · line 99

Quoted 4 lines
drivers/cpufreq/Kconfig.arm +4 -0
+	tristate "NXP i.MX8MQ cpufreq support"
+	select PM_OPP
+	help
+	  This adds cpufreq driver support for NXP i.MX8MQ series SoCs.

+

drivers/cpufreq/Kconfig.arm config ARM_IMX6Q_CPUFREQ · line 101

Quoted 1 line
drivers/cpufreq/Kconfig.arm +1 -0
+	  If in doubt, say N.

+
 config ARM_KIRKWOOD_CPUFREQ

Quoted 2 lines

  def_bool MACH_KIRKWOOD
  help

drivers/cpufreq/Makefile +0 -0
index 689b26c..fe5416c 100644
Quoted 8 lines
(quoted diff) +0 -0
@@ -56,6 +56,7 @@ obj-$(CONFIG_ACPI_CPPC_CPUFREQ)		+= cppc_cpufreq.o

 obj-$(CONFIG_ARCH_DAVINCI) += davinci-cpufreq.o
 obj-$(CONFIG_ARM_HIGHBANK_CPUFREQ) += highbank-cpufreq.o
 obj-$(CONFIG_ARM_IMX6Q_CPUFREQ) += imx6q-cpufreq.o
+obj-$(CONFIG_ARM_IMX8MQ_CPUFREQ) += imx8mq-cpufreq.o
 obj-$(CONFIG_ARM_KIRKWOOD_CPUFREQ) += kirkwood-cpufreq.o
 obj-$(CONFIG_ARM_MEDIATEK_CPUFREQ) += mediatek-cpufreq.o
 obj-$(CONFIG_MACH_MVEBU_V7) += mvebu-cpufreq.o

drivers/cpufreq/imx8mq-cpufreq.c new file +30 -0
new file mode 100644
index 0000000..ee24fab
@@ -0,0 +1,223 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2019 NXP
+ */
+
+#include <linux/clk.h>
+#include <linux/cpu.h>
+#include <linux/cpufreq.h>
+#include <linux/err.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/of.h>
+#include <linux/pm_opp.h>
+#include <linux/platform_device.h>
+#include <linux/suspend.h>
+
+static struct device *cpu_dev;
+static bool free_opp;
+static struct cpufreq_frequency_table *freq_table;
+static struct mutex set_cpufreq_lock;
+static unsigned int transition_latency;
+static unsigned int suspend_freq;
+static struct clk *a53_clk;
+static struct clk *arm_a53_src_clk;
+static struct clk *arm_pll_clk;
+static struct clk *arm_pll_out_clk;
+static struct clk *sys1_pll_800m_clk;
+
+static int imx8mq_set_target(struct cpufreq_policy *policy, unsigned int index)
+{

drivers/cpufreq/imx8mq-cpufreq.c line 34

Quoted 4 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +4 -0
+	struct dev_pm_opp *opp;
+	unsigned long freq_hz;
+	unsigned int old_freq, new_freq;
+	int ret;

+

drivers/cpufreq/imx8mq-cpufreq.c line 36

Quoted 1 line
drivers/cpufreq/imx8mq-cpufreq.c new file +1 -0
+	mutex_lock(&set_cpufreq_lock);

+

drivers/cpufreq/imx8mq-cpufreq.c line 40

Quoted 3 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +3 -0
+	new_freq = freq_table[index].frequency;
+	freq_hz = new_freq * 1000;
+	old_freq = policy->cur;

+

drivers/cpufreq/imx8mq-cpufreq.c line 50

Quoted 9 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +9 -0
+	rcu_read_lock();
+	opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);
+	if (IS_ERR(opp)) {
+		rcu_read_unlock();
+		dev_err(cpu_dev, "failed to find OPP for %ld\n", freq_hz);
+		mutex_unlock(&set_cpufreq_lock);
+		return PTR_ERR(opp);
+	}
+	rcu_read_unlock();

+

drivers/cpufreq/imx8mq-cpufreq.c line 53

Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+	dev_dbg(cpu_dev, "%u MHz --> %u MHz\n",
+		old_freq / 1000, new_freq / 1000);

+

drivers/cpufreq/imx8mq-cpufreq.c line 57

Quoted 3 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +3 -0
+	clk_set_parent(arm_a53_src_clk, sys1_pll_800m_clk);
+	clk_set_rate(arm_pll_clk, new_freq * 1000);
+	clk_set_parent(arm_a53_src_clk, arm_pll_out_clk);

+

drivers/cpufreq/imx8mq-cpufreq.c line 62

Quoted 4 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +4 -0
+	/* Ensure the arm clock divider is what we expect */
+	ret = clk_set_rate(a53_clk, new_freq * 1000);
+	if (ret)
+		dev_err(cpu_dev, "failed to set clock rate: %d\n", ret);

+

Quoted 2 lines
(quoted diff) +2 -0
+	mutex_unlock(&set_cpufreq_lock);
+	return ret;
drivers/cpufreq/imx8mq-cpufreq.c new file +4 -0
+}
+
+static int imx8mq_cpufreq_init(struct cpufreq_policy *policy)
+{

Review

Quoted 1 line
(quoted diff) +1 -0
+	int ret;

+

drivers/cpufreq/imx8mq-cpufreq.c line 74

Quoted 3 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +3 -0
+	policy->clk = a53_clk;
+	policy->cur = clk_get_rate(a53_clk) / 1000;
+	policy->suspend_freq = suspend_freq;

+

drivers/cpufreq/imx8mq-cpufreq.c line 80

Quoted 5 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +5 -0
+	ret = cpufreq_generic_init(policy, freq_table, transition_latency);
+	if (ret) {
+		dev_err(cpu_dev, "imx8mq cpufreq init failed!\n");
+		return ret;
+	}

+

Quoted 1 line
(quoted diff) +1 -0
+	return 0;
drivers/cpufreq/imx8mq-cpufreq.c new file +3 -0
+}
+
+static struct cpufreq_driver imx8mq_cpufreq_driver = {

drivers/cpufreq/imx8mq-cpufreq.c line 92

Quoted 7 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +7 -0
+	.flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
+	.verify = cpufreq_generic_frequency_table_verify,
+	.target_index = imx8mq_set_target,
+	.get = cpufreq_generic_get,
+	.init = imx8mq_cpufreq_init,
+	.name = "imx8mq-cpufreq",
+	.attr = cpufreq_generic_attr,

+#ifdef CONFIG_PM

Quoted 1 line
drivers/cpufreq/imx8mq-cpufreq.c new file +1 -0
+	.suspend = cpufreq_generic_suspend,
drivers/cpufreq/imx8mq-cpufreq.c new file +5 -0
+#endif
+};
+
+static int imx8mq_cpufreq_probe(struct platform_device *pdev)
+{

drivers/cpufreq/imx8mq-cpufreq.c line 101

Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+	struct device_node *np;
+	int num, ret;

+

drivers/cpufreq/imx8mq-cpufreq.c line 107

Quoted 5 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +5 -0
+	cpu_dev = get_cpu_device(0);
+	if (!cpu_dev) {
+		pr_err("failed to get cpu0 device\n");
+		return -ENODEV;
+	}

+

drivers/cpufreq/imx8mq-cpufreq.c line 113

Quoted 5 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +5 -0
+	np = of_node_get(cpu_dev->of_node);
+	if (!np) {
+		dev_err(cpu_dev, "failed to find cpu0 node\n");
+		return -ENOENT;
+	}

+

drivers/cpufreq/imx8mq-cpufreq.c line 126

Quoted 12 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +12 -0
+	a53_clk = clk_get(cpu_dev, "a53");
+	arm_a53_src_clk = clk_get(cpu_dev, "arm_a53_src");
+	arm_pll_clk = clk_get(cpu_dev, "arm_pll");
+	arm_pll_out_clk = clk_get(cpu_dev, "arm_pll_out");
+	sys1_pll_800m_clk = clk_get(cpu_dev, "sys1_pll_800m");
+	if (IS_ERR(a53_clk) || IS_ERR(arm_a53_src_clk)
+		|| IS_ERR(arm_pll_out_clk) || IS_ERR(arm_pll_clk)
+		|| IS_ERR(sys1_pll_800m_clk)) {
+		dev_err(cpu_dev, "failed to get clocks\n");
+		ret = -ENOENT;
+		goto put_clk;
+	}

+

drivers/cpufreq/imx8mq-cpufreq.c line 139

Quoted 12 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +12 -0
+	/*
+	 * We expect an OPP table supplied by platform.
+	 * Just, incase the platform did not supply the OPP
+	 * table, it will try to get it.
+	 */
+	num = dev_pm_opp_get_opp_count(cpu_dev);
+	if (num < 0) {
+		ret = dev_pm_opp_of_add_table(cpu_dev);
+		if (ret < 0) {
+			dev_err(cpu_dev, "failed to init OPP table: %d\n", ret);
+			goto put_clk;
+		}

+

drivers/cpufreq/imx8mq-cpufreq.c line 142

Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+		/* Because we have added the OPPs here, we must free them */
+		free_opp = true;

+

drivers/cpufreq/imx8mq-cpufreq.c line 150

Quoted 7 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +7 -0
+		num = dev_pm_opp_get_opp_count(cpu_dev);
+		if (num < 0) {
+			ret = num;
+			dev_err(cpu_dev, "no OPP table is found: %d\n", ret);
+			goto out_free_opp;
+		}
+	}

+

drivers/cpufreq/imx8mq-cpufreq.c line 156

Quoted 5 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +5 -0
+	ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
+	if (ret) {
+		dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
+		goto out_free_opp;
+	}

+

drivers/cpufreq/imx8mq-cpufreq.c line 159

Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+	/* use MAX freq to suspend */
+	suspend_freq = freq_table[num - 1].frequency;

+

drivers/cpufreq/imx8mq-cpufreq.c line 162

Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+	if (of_property_read_u32(np, "clock-latency", &transition_latency))
+		transition_latency = CPUFREQ_ETERNAL;

+

drivers/cpufreq/imx8mq-cpufreq.c line 164

Quoted 1 line
drivers/cpufreq/imx8mq-cpufreq.c new file +1 -0
+	mutex_init(&set_cpufreq_lock);

+

drivers/cpufreq/imx8mq-cpufreq.c line 170

Quoted 5 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +5 -0
+	ret = cpufreq_register_driver(&imx8mq_cpufreq_driver);
+	if (ret) {
+		dev_err(cpu_dev, "failed register driver: %d\n", ret);
+		goto free_freq_table;
+	}

+

drivers/cpufreq/imx8mq-cpufreq.c line 173

Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+	of_node_put(np);
+	dev_info(cpu_dev, "registered imx8mq-cpufreq\n");

+

Quoted 1 line
(quoted diff) +1 -0
+	return 0;
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+
+free_freq_table:

Review

Quoted 1 line
(quoted diff) +1 -0
+	dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);

+out_free_opp:

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (free_opp)
+		dev_pm_opp_of_remove_table(cpu_dev);

+put_clk:

Quoted 12 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +12 -0
+	if (!IS_ERR(a53_clk))
+		clk_put(a53_clk);
+	if (!IS_ERR(arm_a53_src_clk))
+		clk_put(arm_a53_src_clk);
+	if (!IS_ERR(arm_pll_clk))
+		clk_put(arm_pll_clk);
+	if (!IS_ERR(arm_pll_out_clk))
+		clk_put(arm_pll_out_clk);
+	if (!IS_ERR(sys1_pll_800m_clk))
+		clk_put(sys1_pll_800m_clk);
+	of_node_put(np);
+	return ret;
drivers/cpufreq/imx8mq-cpufreq.c new file +4 -0
+}
+
+static int imx8mq_cpufreq_remove(struct platform_device *pdev)
+{

drivers/cpufreq/imx8mq-cpufreq.c line 207

Quoted 9 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +9 -0
+	cpufreq_unregister_driver(&imx8mq_cpufreq_driver);
+	dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
+	if (free_opp)
+		dev_pm_opp_of_remove_table(cpu_dev);
+	clk_put(a53_clk);
+	clk_put(arm_a53_src_clk);
+	clk_put(arm_pll_clk);
+	clk_put(arm_pll_out_clk);
+	clk_put(sys1_pll_800m_clk);

+

Quoted 1 line
(quoted diff) +1 -0
+	return 0;
drivers/cpufreq/imx8mq-cpufreq.c new file +3 -0
+}
+
+static struct platform_driver imx8mq_cpufreq_platdrv = {
Quoted 5 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +1 -0
+	.driver = {
Quoted 1 line
(quoted diff) +1 -0
+		.name	= "imx8mq-cpufreq",
drivers/cpufreq/imx8mq-cpufreq.c new file +1 -0
+	},
Quoted 2 lines
drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+	.probe		= imx8mq_cpufreq_probe,
+	.remove		= imx8mq_cpufreq_remove,
drivers/cpufreq/imx8mq-cpufreq.c new file +3 -0
+};
+module_platform_driver(imx8mq_cpufreq_platdrv);
+
Quoted 1 line

+MODULE_AUTHOR("Anson Huang <[email protected]>");

drivers/cpufreq/imx8mq-cpufreq.c new file +2 -0
+MODULE_DESCRIPTION("Freescale i.MX8MQ cpufreq driver");
+MODULE_LICENSE("GPL");