1 // SPDX-License-Identifier: GPL-2.0
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>
16 struct devfreq_dev_profile profile;
17 struct devfreq *devfreq;
19 struct platform_device *icc_pdev;
22 static int imx_bus_target(struct device *dev,
23 unsigned long *freq, u32 flags)
25 struct dev_pm_opp *new_opp;
28 new_opp = devfreq_recommended_opp(dev, freq, flags);
29 if (IS_ERR(new_opp)) {
30 ret = PTR_ERR(new_opp);
31 dev_err(dev, "failed to get recommended opp: %d\n", ret);
34 dev_pm_opp_put(new_opp);
36 return dev_pm_opp_set_rate(dev, *freq);
39 static int imx_bus_get_cur_freq(struct device *dev, unsigned long *freq)
41 struct imx_bus *priv = dev_get_drvdata(dev);
43 *freq = clk_get_rate(priv->clk);
48 static void imx_bus_exit(struct device *dev)
50 struct imx_bus *priv = dev_get_drvdata(dev);
52 dev_pm_opp_of_remove_table(dev);
53 platform_device_unregister(priv->icc_pdev);
56 /* imx_bus_init_icc() - register matching icc provider if required */
57 static int imx_bus_init_icc(struct device *dev)
59 struct imx_bus *priv = dev_get_drvdata(dev);
60 const char *icc_driver_name;
62 if (!of_get_property(dev->of_node, "#interconnect-cells", NULL))
64 if (!IS_ENABLED(CONFIG_INTERCONNECT_IMX)) {
65 dev_warn(dev, "imx interconnect drivers disabled\n");
69 icc_driver_name = of_device_get_match_data(dev);
70 if (!icc_driver_name) {
71 dev_err(dev, "unknown interconnect driver\n");
75 priv->icc_pdev = platform_device_register_data(
76 dev, icc_driver_name, -1, NULL, 0);
77 if (IS_ERR(priv->icc_pdev)) {
78 dev_err(dev, "failed to register icc provider %s: %ld\n",
79 icc_driver_name, PTR_ERR(priv->icc_pdev));
80 return PTR_ERR(priv->icc_pdev);
86 static int imx_bus_probe(struct platform_device *pdev)
88 struct device *dev = &pdev->dev;
90 const char *gov = DEVFREQ_GOV_USERSPACE;
93 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
98 * Fetch the clock to adjust but don't explicitly enable.
100 * For imx bus clock clk_set_rate is safe no matter if the clock is on
101 * or off and some peripheral side-buses might be off unless enabled by
102 * drivers for devices on those specific buses.
104 * Rate adjustment on a disabled bus clock just takes effect later.
106 priv->clk = devm_clk_get(dev, NULL);
107 if (IS_ERR(priv->clk)) {
108 ret = PTR_ERR(priv->clk);
109 dev_err(dev, "failed to fetch clk: %d\n", ret);
112 platform_set_drvdata(pdev, priv);
114 ret = dev_pm_opp_of_add_table(dev);
116 dev_err(dev, "failed to get OPP table\n");
120 priv->profile.target = imx_bus_target;
121 priv->profile.exit = imx_bus_exit;
122 priv->profile.get_cur_freq = imx_bus_get_cur_freq;
123 priv->profile.initial_freq = clk_get_rate(priv->clk);
125 priv->devfreq = devm_devfreq_add_device(dev, &priv->profile,
127 if (IS_ERR(priv->devfreq)) {
128 ret = PTR_ERR(priv->devfreq);
129 dev_err(dev, "failed to add devfreq device: %d\n", ret);
133 ret = imx_bus_init_icc(dev);
140 dev_pm_opp_of_remove_table(dev);
144 static const struct of_device_id imx_bus_of_match[] = {
145 { .compatible = "fsl,imx8mq-noc", .data = "imx8mq-interconnect", },
146 { .compatible = "fsl,imx8mm-noc", .data = "imx8mm-interconnect", },
147 { .compatible = "fsl,imx8mn-noc", .data = "imx8mn-interconnect", },
148 { .compatible = "fsl,imx8mp-noc", .data = "imx8mp-interconnect", },
149 { .compatible = "fsl,imx8m-noc", },
150 { .compatible = "fsl,imx8m-nic", },
153 MODULE_DEVICE_TABLE(of, imx_bus_of_match);
155 static struct platform_driver imx_bus_platdrv = {
156 .probe = imx_bus_probe,
158 .name = "imx-bus-devfreq",
159 .of_match_table = imx_bus_of_match,
162 module_platform_driver(imx_bus_platdrv);
164 MODULE_DESCRIPTION("Generic i.MX bus frequency scaling driver");
165 MODULE_AUTHOR("Leonard Crestez <leonard.crestez@nxp.com>");
166 MODULE_LICENSE("GPL v2");