Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / devfreq / imx-bus.c
blob4f38455ad74239550ceec80078a6874e45e42a25
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2019 NXP
4 */
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>
15 struct imx_bus {
16 struct devfreq_dev_profile profile;
17 struct devfreq *devfreq;
18 struct clk *clk;
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;
26 int ret;
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);
32 return 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);
45 return 0;
48 static int imx_bus_get_dev_status(struct device *dev,
49 struct devfreq_dev_status *stat)
51 struct imx_bus *priv = dev_get_drvdata(dev);
53 stat->busy_time = 0;
54 stat->total_time = 0;
55 stat->current_frequency = clk_get_rate(priv->clk);
57 return 0;
60 static void imx_bus_exit(struct device *dev)
62 struct imx_bus *priv = dev_get_drvdata(dev);
64 dev_pm_opp_of_remove_table(dev);
65 platform_device_unregister(priv->icc_pdev);
68 /* imx_bus_init_icc() - register matching icc provider if required */
69 static int imx_bus_init_icc(struct device *dev)
71 struct imx_bus *priv = dev_get_drvdata(dev);
72 const char *icc_driver_name;
74 if (!of_get_property(dev->of_node, "#interconnect-cells", 0))
75 return 0;
76 if (!IS_ENABLED(CONFIG_INTERCONNECT_IMX)) {
77 dev_warn(dev, "imx interconnect drivers disabled\n");
78 return 0;
81 icc_driver_name = of_device_get_match_data(dev);
82 if (!icc_driver_name) {
83 dev_err(dev, "unknown interconnect driver\n");
84 return 0;
87 priv->icc_pdev = platform_device_register_data(
88 dev, icc_driver_name, -1, NULL, 0);
89 if (IS_ERR(priv->icc_pdev)) {
90 dev_err(dev, "failed to register icc provider %s: %ld\n",
91 icc_driver_name, PTR_ERR(priv->icc_pdev));
92 return PTR_ERR(priv->icc_pdev);
95 return 0;
98 static int imx_bus_probe(struct platform_device *pdev)
100 struct device *dev = &pdev->dev;
101 struct imx_bus *priv;
102 const char *gov = DEVFREQ_GOV_USERSPACE;
103 int ret;
105 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
106 if (!priv)
107 return -ENOMEM;
110 * Fetch the clock to adjust but don't explicitly enable.
112 * For imx bus clock clk_set_rate is safe no matter if the clock is on
113 * or off and some peripheral side-buses might be off unless enabled by
114 * drivers for devices on those specific buses.
116 * Rate adjustment on a disabled bus clock just takes effect later.
118 priv->clk = devm_clk_get(dev, NULL);
119 if (IS_ERR(priv->clk)) {
120 ret = PTR_ERR(priv->clk);
121 dev_err(dev, "failed to fetch clk: %d\n", ret);
122 return ret;
124 platform_set_drvdata(pdev, priv);
126 ret = dev_pm_opp_of_add_table(dev);
127 if (ret < 0) {
128 dev_err(dev, "failed to get OPP table\n");
129 return ret;
132 priv->profile.polling_ms = 1000;
133 priv->profile.target = imx_bus_target;
134 priv->profile.get_dev_status = imx_bus_get_dev_status;
135 priv->profile.exit = imx_bus_exit;
136 priv->profile.get_cur_freq = imx_bus_get_cur_freq;
137 priv->profile.initial_freq = clk_get_rate(priv->clk);
139 priv->devfreq = devm_devfreq_add_device(dev, &priv->profile,
140 gov, NULL);
141 if (IS_ERR(priv->devfreq)) {
142 ret = PTR_ERR(priv->devfreq);
143 dev_err(dev, "failed to add devfreq device: %d\n", ret);
144 goto err;
147 ret = imx_bus_init_icc(dev);
148 if (ret)
149 goto err;
151 return 0;
153 err:
154 dev_pm_opp_of_remove_table(dev);
155 return ret;
158 static const struct of_device_id imx_bus_of_match[] = {
159 { .compatible = "fsl,imx8mq-noc", .data = "imx8mq-interconnect", },
160 { .compatible = "fsl,imx8mm-noc", .data = "imx8mm-interconnect", },
161 { .compatible = "fsl,imx8mn-noc", .data = "imx8mn-interconnect", },
162 { .compatible = "fsl,imx8m-noc", },
163 { .compatible = "fsl,imx8m-nic", },
164 { /* sentinel */ },
166 MODULE_DEVICE_TABLE(of, imx_bus_of_match);
168 static struct platform_driver imx_bus_platdrv = {
169 .probe = imx_bus_probe,
170 .driver = {
171 .name = "imx-bus-devfreq",
172 .of_match_table = of_match_ptr(imx_bus_of_match),
175 module_platform_driver(imx_bus_platdrv);
177 MODULE_DESCRIPTION("Generic i.MX bus frequency scaling driver");
178 MODULE_AUTHOR("Leonard Crestez <leonard.crestez@nxp.com>");
179 MODULE_LICENSE("GPL v2");