Leonard Crestez | 5173a97 | 2020-04-06 15:03:07 +0300 | [diff] [blame] | 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; |
Leonard Crestez | 0235521 | 2020-04-06 15:03:08 +0300 | [diff] [blame] | 19 | struct platform_device *icc_pdev; |
Leonard Crestez | 5173a97 | 2020-04-06 15:03:07 +0300 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | static int imx_bus_target(struct device *dev, |
| 23 | unsigned long *freq, u32 flags) |
| 24 | { |
| 25 | struct dev_pm_opp *new_opp; |
| 26 | int ret; |
| 27 | |
| 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; |
| 33 | } |
| 34 | dev_pm_opp_put(new_opp); |
| 35 | |
| 36 | return dev_pm_opp_set_rate(dev, *freq); |
| 37 | } |
| 38 | |
| 39 | static int imx_bus_get_cur_freq(struct device *dev, unsigned long *freq) |
| 40 | { |
| 41 | struct imx_bus *priv = dev_get_drvdata(dev); |
| 42 | |
| 43 | *freq = clk_get_rate(priv->clk); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static int imx_bus_get_dev_status(struct device *dev, |
| 49 | struct devfreq_dev_status *stat) |
| 50 | { |
| 51 | struct imx_bus *priv = dev_get_drvdata(dev); |
| 52 | |
| 53 | stat->busy_time = 0; |
| 54 | stat->total_time = 0; |
| 55 | stat->current_frequency = clk_get_rate(priv->clk); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static void imx_bus_exit(struct device *dev) |
| 61 | { |
Leonard Crestez | 0235521 | 2020-04-06 15:03:08 +0300 | [diff] [blame] | 62 | struct imx_bus *priv = dev_get_drvdata(dev); |
| 63 | |
Leonard Crestez | 5173a97 | 2020-04-06 15:03:07 +0300 | [diff] [blame] | 64 | dev_pm_opp_of_remove_table(dev); |
Leonard Crestez | 0235521 | 2020-04-06 15:03:08 +0300 | [diff] [blame] | 65 | platform_device_unregister(priv->icc_pdev); |
| 66 | } |
| 67 | |
| 68 | /* imx_bus_init_icc() - register matching icc provider if required */ |
| 69 | static int imx_bus_init_icc(struct device *dev) |
| 70 | { |
| 71 | struct imx_bus *priv = dev_get_drvdata(dev); |
| 72 | const char *icc_driver_name; |
| 73 | |
| 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; |
| 79 | } |
| 80 | |
| 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; |
| 85 | } |
| 86 | |
| 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", |
Gustavo A. R. Silva | 48bbf63 | 2020-05-07 08:12:45 -0500 | [diff] [blame] | 91 | icc_driver_name, PTR_ERR(priv->icc_pdev)); |
| 92 | return PTR_ERR(priv->icc_pdev); |
Leonard Crestez | 0235521 | 2020-04-06 15:03:08 +0300 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return 0; |
Leonard Crestez | 5173a97 | 2020-04-06 15:03:07 +0300 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int imx_bus_probe(struct platform_device *pdev) |
| 99 | { |
| 100 | struct device *dev = &pdev->dev; |
| 101 | struct imx_bus *priv; |
| 102 | const char *gov = DEVFREQ_GOV_USERSPACE; |
| 103 | int ret; |
| 104 | |
| 105 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 106 | if (!priv) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | /* |
| 110 | * Fetch the clock to adjust but don't explicitly enable. |
| 111 | * |
| 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. |
| 115 | * |
| 116 | * Rate adjustment on a disabled bus clock just takes effect later. |
| 117 | */ |
| 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; |
| 123 | } |
| 124 | platform_set_drvdata(pdev, priv); |
| 125 | |
| 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; |
| 130 | } |
| 131 | |
| 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); |
| 138 | |
| 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; |
| 145 | } |
| 146 | |
Leonard Crestez | 0235521 | 2020-04-06 15:03:08 +0300 | [diff] [blame] | 147 | ret = imx_bus_init_icc(dev); |
| 148 | if (ret) |
| 149 | goto err; |
| 150 | |
Leonard Crestez | 5173a97 | 2020-04-06 15:03:07 +0300 | [diff] [blame] | 151 | return 0; |
| 152 | |
| 153 | err: |
| 154 | dev_pm_opp_of_remove_table(dev); |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | static const struct of_device_id imx_bus_of_match[] = { |
Leonard Crestez | 0235521 | 2020-04-06 15:03:08 +0300 | [diff] [blame] | 159 | { .compatible = "fsl,imx8mq-noc", .data = "imx8mq-interconnect", }, |
| 160 | { .compatible = "fsl,imx8mm-noc", .data = "imx8mm-interconnect", }, |
| 161 | { .compatible = "fsl,imx8mn-noc", .data = "imx8mn-interconnect", }, |
Leonard Crestez | 5173a97 | 2020-04-06 15:03:07 +0300 | [diff] [blame] | 162 | { .compatible = "fsl,imx8m-noc", }, |
| 163 | { .compatible = "fsl,imx8m-nic", }, |
| 164 | { /* sentinel */ }, |
| 165 | }; |
| 166 | MODULE_DEVICE_TABLE(of, imx_bus_of_match); |
| 167 | |
| 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), |
| 173 | }, |
| 174 | }; |
| 175 | module_platform_driver(imx_bus_platdrv); |
| 176 | |
| 177 | MODULE_DESCRIPTION("Generic i.MX bus frequency scaling driver"); |
| 178 | MODULE_AUTHOR("Leonard Crestez <leonard.crestez@nxp.com>"); |
| 179 | MODULE_LICENSE("GPL v2"); |