Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * System Control and Power Interface (SCPI) based CPUFreq Interface driver |
| 3 | * |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 4 | * Copyright (C) 2015 ARM Ltd. |
| 5 | * Sudeep Holla <sudeep.holla@arm.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 12 | * kind, whether express or implied; without even the implied warranty |
| 13 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 18 | |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 19 | #include <linux/clk.h> |
Sudeep Holla | d9975b0 | 2016-05-03 15:05:05 +0100 | [diff] [blame] | 20 | #include <linux/cpu.h> |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 21 | #include <linux/cpufreq.h> |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 22 | #include <linux/cpumask.h> |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 23 | #include <linux/export.h> |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 24 | #include <linux/module.h> |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 25 | #include <linux/of_platform.h> |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 26 | #include <linux/pm_opp.h> |
| 27 | #include <linux/scpi_protocol.h> |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 28 | #include <linux/slab.h> |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 29 | #include <linux/types.h> |
| 30 | |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 31 | struct scpi_data { |
| 32 | struct clk *clk; |
| 33 | struct device *cpu_dev; |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 34 | }; |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 35 | |
| 36 | static struct scpi_ops *scpi_ops; |
| 37 | |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 38 | static unsigned int scpi_cpufreq_get_rate(unsigned int cpu) |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 39 | { |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 40 | struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); |
| 41 | struct scpi_data *priv = policy->driver_data; |
| 42 | unsigned long rate = clk_get_rate(priv->clk); |
| 43 | |
| 44 | return rate / 1000; |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 47 | static int |
| 48 | scpi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index) |
| 49 | { |
Ionela Voinescu | 1a0419b | 2020-09-01 21:55:46 +0100 | [diff] [blame] | 50 | u64 rate = policy->freq_table[index].frequency * 1000; |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 51 | struct scpi_data *priv = policy->driver_data; |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 52 | int ret; |
| 53 | |
| 54 | ret = clk_set_rate(priv->clk, rate); |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 55 | |
Dietmar Eggemann | 9326fdf | 2018-02-26 13:11:03 +0000 | [diff] [blame] | 56 | if (ret) |
| 57 | return ret; |
| 58 | |
| 59 | if (clk_get_rate(priv->clk) != rate) |
| 60 | return -EIO; |
| 61 | |
Dietmar Eggemann | 9326fdf | 2018-02-26 13:11:03 +0000 | [diff] [blame] | 62 | return 0; |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static int |
| 66 | scpi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask) |
| 67 | { |
| 68 | int cpu, domain, tdomain; |
| 69 | struct device *tcpu_dev; |
| 70 | |
| 71 | domain = scpi_ops->device_domain_id(cpu_dev); |
| 72 | if (domain < 0) |
| 73 | return domain; |
| 74 | |
| 75 | for_each_possible_cpu(cpu) { |
| 76 | if (cpu == cpu_dev->id) |
| 77 | continue; |
| 78 | |
| 79 | tcpu_dev = get_cpu_device(cpu); |
| 80 | if (!tcpu_dev) |
| 81 | continue; |
| 82 | |
| 83 | tdomain = scpi_ops->device_domain_id(tcpu_dev); |
| 84 | if (tdomain == domain) |
| 85 | cpumask_set_cpu(cpu, cpumask); |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int scpi_cpufreq_init(struct cpufreq_policy *policy) |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 92 | { |
Sudeep Holla | c0f2e21 | 2017-04-27 15:20:37 +0100 | [diff] [blame] | 93 | int ret; |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 94 | unsigned int latency; |
| 95 | struct device *cpu_dev; |
| 96 | struct scpi_data *priv; |
| 97 | struct cpufreq_frequency_table *freq_table; |
| 98 | |
| 99 | cpu_dev = get_cpu_device(policy->cpu); |
| 100 | if (!cpu_dev) { |
| 101 | pr_err("failed to get cpu%d device\n", policy->cpu); |
| 102 | return -ENODEV; |
| 103 | } |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 104 | |
Sudeep Holla | c0f2e21 | 2017-04-27 15:20:37 +0100 | [diff] [blame] | 105 | ret = scpi_ops->add_opps_to_device(cpu_dev); |
| 106 | if (ret) { |
| 107 | dev_warn(cpu_dev, "failed to add opps to the device\n"); |
| 108 | return ret; |
Sudeep Holla | d9975b0 | 2016-05-03 15:05:05 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 111 | ret = scpi_get_sharing_cpus(cpu_dev, policy->cpus); |
| 112 | if (ret) { |
| 113 | dev_warn(cpu_dev, "failed to get sharing cpumask\n"); |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus); |
| 118 | if (ret) { |
Sudeep Holla | d9975b0 | 2016-05-03 15:05:05 +0100 | [diff] [blame] | 119 | dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n", |
| 120 | __func__, ret); |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | ret = dev_pm_opp_get_opp_count(cpu_dev); |
| 125 | if (ret <= 0) { |
| 126 | dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n"); |
| 127 | ret = -EPROBE_DEFER; |
| 128 | goto out_free_opp; |
| 129 | } |
| 130 | |
| 131 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 132 | if (!priv) { |
| 133 | ret = -ENOMEM; |
| 134 | goto out_free_opp; |
| 135 | } |
| 136 | |
| 137 | ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table); |
| 138 | if (ret) { |
| 139 | dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret); |
| 140 | goto out_free_priv; |
| 141 | } |
| 142 | |
| 143 | priv->cpu_dev = cpu_dev; |
| 144 | priv->clk = clk_get(cpu_dev, NULL); |
| 145 | if (IS_ERR(priv->clk)) { |
| 146 | dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d\n", |
| 147 | __func__, cpu_dev->id); |
Wei Yongjun | 0725390 | 2018-01-23 02:09:02 +0000 | [diff] [blame] | 148 | ret = PTR_ERR(priv->clk); |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 149 | goto out_free_cpufreq_table; |
| 150 | } |
| 151 | |
| 152 | policy->driver_data = priv; |
Viresh Kumar | 472ada6 | 2018-02-26 10:39:04 +0530 | [diff] [blame] | 153 | policy->freq_table = freq_table; |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 154 | |
| 155 | /* scpi allows DVFS request for any domain from any CPU */ |
| 156 | policy->dvfs_possible_from_any_cpu = true; |
| 157 | |
| 158 | latency = scpi_ops->get_transition_latency(cpu_dev); |
| 159 | if (!latency) |
| 160 | latency = CPUFREQ_ETERNAL; |
| 161 | |
| 162 | policy->cpuinfo.transition_latency = latency; |
| 163 | |
| 164 | policy->fast_switch_possible = false; |
Quentin Perret | 6915d7a | 2019-02-04 11:09:50 +0000 | [diff] [blame] | 165 | |
Lukasz Luba | 0e0ffa8 | 2020-05-27 10:58:54 +0100 | [diff] [blame] | 166 | dev_pm_opp_of_register_em(cpu_dev, policy->cpus); |
Quentin Perret | 6915d7a | 2019-02-04 11:09:50 +0000 | [diff] [blame] | 167 | |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 168 | return 0; |
| 169 | |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 170 | out_free_cpufreq_table: |
| 171 | dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table); |
| 172 | out_free_priv: |
| 173 | kfree(priv); |
| 174 | out_free_opp: |
Viresh Kumar | 1690d8b | 2019-01-04 15:14:33 +0530 | [diff] [blame] | 175 | dev_pm_opp_remove_all_dynamic(cpu_dev); |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 176 | |
Sudeep Holla | d9975b0 | 2016-05-03 15:05:05 +0100 | [diff] [blame] | 177 | return ret; |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 180 | static int scpi_cpufreq_exit(struct cpufreq_policy *policy) |
| 181 | { |
| 182 | struct scpi_data *priv = policy->driver_data; |
| 183 | |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 184 | clk_put(priv->clk); |
| 185 | dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table); |
Viresh Kumar | 1690d8b | 2019-01-04 15:14:33 +0530 | [diff] [blame] | 186 | dev_pm_opp_remove_all_dynamic(priv->cpu_dev); |
Vincent Stehlé | 31d4c52 | 2019-03-27 23:06:42 +0100 | [diff] [blame] | 187 | kfree(priv); |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 192 | static struct cpufreq_driver scpi_cpufreq_driver = { |
| 193 | .name = "scpi-cpufreq", |
Viresh Kumar | 5ae4a4b | 2021-02-02 10:25:11 +0530 | [diff] [blame] | 194 | .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY | |
Amit Kucheria | cb772b8 | 2019-01-29 10:25:15 +0530 | [diff] [blame] | 195 | CPUFREQ_NEED_INITIAL_FREQ_CHECK | |
| 196 | CPUFREQ_IS_COOLING_DEV, |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 197 | .verify = cpufreq_generic_frequency_table_verify, |
| 198 | .attr = cpufreq_generic_attr, |
| 199 | .get = scpi_cpufreq_get_rate, |
| 200 | .init = scpi_cpufreq_init, |
| 201 | .exit = scpi_cpufreq_exit, |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 202 | .target_index = scpi_cpufreq_set_target, |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | static int scpi_cpufreq_probe(struct platform_device *pdev) |
| 206 | { |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 207 | int ret; |
| 208 | |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 209 | scpi_ops = get_scpi_ops(); |
| 210 | if (!scpi_ops) |
| 211 | return -EIO; |
| 212 | |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 213 | ret = cpufreq_register_driver(&scpi_cpufreq_driver); |
| 214 | if (ret) |
| 215 | dev_err(&pdev->dev, "%s: registering cpufreq failed, err: %d\n", |
| 216 | __func__, ret); |
| 217 | return ret; |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static int scpi_cpufreq_remove(struct platform_device *pdev) |
| 221 | { |
Sudeep Holla | 343a8d17 | 2018-01-10 16:44:15 +0000 | [diff] [blame] | 222 | cpufreq_unregister_driver(&scpi_cpufreq_driver); |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 223 | scpi_ops = NULL; |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static struct platform_driver scpi_cpufreq_platdrv = { |
| 228 | .driver = { |
| 229 | .name = "scpi-cpufreq", |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 230 | }, |
| 231 | .probe = scpi_cpufreq_probe, |
| 232 | .remove = scpi_cpufreq_remove, |
| 233 | }; |
| 234 | module_platform_driver(scpi_cpufreq_platdrv); |
| 235 | |
Pali Rohár | c0382d0 | 2020-11-03 16:11:38 +0100 | [diff] [blame] | 236 | MODULE_ALIAS("platform:scpi-cpufreq"); |
Sudeep Holla | 8def310 | 2015-03-30 10:59:52 +0100 | [diff] [blame] | 237 | MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>"); |
| 238 | MODULE_DESCRIPTION("ARM SCPI CPUFreq interface driver"); |
| 239 | MODULE_LICENSE("GPL v2"); |