Skip to content

Commit 5173a97

Browse files
cdleonardchanwoochoi
authored andcommitted
PM / devfreq: Add generic imx bus scaling driver
Add initial support for dynamic frequency switching on pieces of the imx interconnect fabric. All this driver does is set a clk rate based on an opp table, it does not map register areas. Signed-off-by: Leonard Crestez <[email protected]> Tested-by: Martin Kepplinger <[email protected]> Acked-by: Chanwoo Choi <[email protected]> Signed-off-by: Chanwoo Choi <[email protected]>
1 parent 0716f9f commit 5173a97

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

drivers/devfreq/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ config ARM_EXYNOS_BUS_DEVFREQ
9191
and adjusts the operating frequencies and voltages with OPP support.
9292
This does not yet operate with optimal voltages.
9393

94+
config ARM_IMX_BUS_DEVFREQ
95+
tristate "i.MX Generic Bus DEVFREQ Driver"
96+
depends on ARCH_MXC || COMPILE_TEST
97+
select DEVFREQ_GOV_USERSPACE
98+
help
99+
This adds the generic DEVFREQ driver for i.MX interconnects. It
100+
allows adjusting NIC/NOC frequency.
101+
94102
config ARM_IMX8M_DDRC_DEVFREQ
95103
tristate "i.MX8M DDRC DEVFREQ Driver"
96104
depends on (ARCH_MXC && HAVE_ARM_SMCCC) || \

drivers/devfreq/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ obj-$(CONFIG_DEVFREQ_GOV_PASSIVE) += governor_passive.o
99

1010
# DEVFREQ Drivers
1111
obj-$(CONFIG_ARM_EXYNOS_BUS_DEVFREQ) += exynos-bus.o
12+
obj-$(CONFIG_ARM_IMX_BUS_DEVFREQ) += imx-bus.o
1213
obj-$(CONFIG_ARM_IMX8M_DDRC_DEVFREQ) += imx8m-ddrc.o
1314
obj-$(CONFIG_ARM_RK3399_DMC_DEVFREQ) += rk3399_dmc.o
1415
obj-$(CONFIG_ARM_TEGRA_DEVFREQ) += tegra30-devfreq.o

drivers/devfreq/imx-bus.c

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright 2019 NXP
4+
*/
5+
6+
#include <linux/clk.h>
7+
#include <linux/devfreq.h>
8+
#include <linux/device.h>
9+
#include <linux/module.h>
10+
#include <linux/of_device.h>
11+
#include <linux/pm_opp.h>
12+
#include <linux/platform_device.h>
13+
#include <linux/slab.h>
14+
15+
struct imx_bus {
16+
struct devfreq_dev_profile profile;
17+
struct devfreq *devfreq;
18+
struct clk *clk;
19+
};
20+
21+
static int imx_bus_target(struct device *dev,
22+
unsigned long *freq, u32 flags)
23+
{
24+
struct dev_pm_opp *new_opp;
25+
int ret;
26+
27+
new_opp = devfreq_recommended_opp(dev, freq, flags);
28+
if (IS_ERR(new_opp)) {
29+
ret = PTR_ERR(new_opp);
30+
dev_err(dev, "failed to get recommended opp: %d\n", ret);
31+
return ret;
32+
}
33+
dev_pm_opp_put(new_opp);
34+
35+
return dev_pm_opp_set_rate(dev, *freq);
36+
}
37+
38+
static int imx_bus_get_cur_freq(struct device *dev, unsigned long *freq)
39+
{
40+
struct imx_bus *priv = dev_get_drvdata(dev);
41+
42+
*freq = clk_get_rate(priv->clk);
43+
44+
return 0;
45+
}
46+
47+
static int imx_bus_get_dev_status(struct device *dev,
48+
struct devfreq_dev_status *stat)
49+
{
50+
struct imx_bus *priv = dev_get_drvdata(dev);
51+
52+
stat->busy_time = 0;
53+
stat->total_time = 0;
54+
stat->current_frequency = clk_get_rate(priv->clk);
55+
56+
return 0;
57+
}
58+
59+
static void imx_bus_exit(struct device *dev)
60+
{
61+
dev_pm_opp_of_remove_table(dev);
62+
}
63+
64+
static int imx_bus_probe(struct platform_device *pdev)
65+
{
66+
struct device *dev = &pdev->dev;
67+
struct imx_bus *priv;
68+
const char *gov = DEVFREQ_GOV_USERSPACE;
69+
int ret;
70+
71+
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
72+
if (!priv)
73+
return -ENOMEM;
74+
75+
/*
76+
* Fetch the clock to adjust but don't explicitly enable.
77+
*
78+
* For imx bus clock clk_set_rate is safe no matter if the clock is on
79+
* or off and some peripheral side-buses might be off unless enabled by
80+
* drivers for devices on those specific buses.
81+
*
82+
* Rate adjustment on a disabled bus clock just takes effect later.
83+
*/
84+
priv->clk = devm_clk_get(dev, NULL);
85+
if (IS_ERR(priv->clk)) {
86+
ret = PTR_ERR(priv->clk);
87+
dev_err(dev, "failed to fetch clk: %d\n", ret);
88+
return ret;
89+
}
90+
platform_set_drvdata(pdev, priv);
91+
92+
ret = dev_pm_opp_of_add_table(dev);
93+
if (ret < 0) {
94+
dev_err(dev, "failed to get OPP table\n");
95+
return ret;
96+
}
97+
98+
priv->profile.polling_ms = 1000;
99+
priv->profile.target = imx_bus_target;
100+
priv->profile.get_dev_status = imx_bus_get_dev_status;
101+
priv->profile.exit = imx_bus_exit;
102+
priv->profile.get_cur_freq = imx_bus_get_cur_freq;
103+
priv->profile.initial_freq = clk_get_rate(priv->clk);
104+
105+
priv->devfreq = devm_devfreq_add_device(dev, &priv->profile,
106+
gov, NULL);
107+
if (IS_ERR(priv->devfreq)) {
108+
ret = PTR_ERR(priv->devfreq);
109+
dev_err(dev, "failed to add devfreq device: %d\n", ret);
110+
goto err;
111+
}
112+
113+
return 0;
114+
115+
err:
116+
dev_pm_opp_of_remove_table(dev);
117+
return ret;
118+
}
119+
120+
static const struct of_device_id imx_bus_of_match[] = {
121+
{ .compatible = "fsl,imx8m-noc", },
122+
{ .compatible = "fsl,imx8m-nic", },
123+
{ /* sentinel */ },
124+
};
125+
MODULE_DEVICE_TABLE(of, imx_bus_of_match);
126+
127+
static struct platform_driver imx_bus_platdrv = {
128+
.probe = imx_bus_probe,
129+
.driver = {
130+
.name = "imx-bus-devfreq",
131+
.of_match_table = of_match_ptr(imx_bus_of_match),
132+
},
133+
};
134+
module_platform_driver(imx_bus_platdrv);
135+
136+
MODULE_DESCRIPTION("Generic i.MX bus frequency scaling driver");
137+
MODULE_AUTHOR("Leonard Crestez <[email protected]>");
138+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)