Junjie Wu | b8be333 | 2015-02-03 14:31:09 -0800 | [diff] [blame] | 1 | /* drivers/cpufreq/qcom-cpufreq.c |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 2 | * |
| 3 | * MSM architecture cpufreq driver |
| 4 | * |
| 5 | * Copyright (C) 2007 Google, Inc. |
Santosh Mardi | efe6367 | 2017-01-12 16:48:53 +0530 | [diff] [blame] | 6 | * Copyright (c) 2007-2017, The Linux Foundation. All rights reserved. |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 7 | * Author: Mike A. Chan <mikechan@google.com> |
| 8 | * |
| 9 | * This software is licensed under the terms of the GNU General Public |
| 10 | * License version 2, as published by the Free Software Foundation, and |
| 11 | * may be copied, distributed, and modified under those terms. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/cpufreq.h> |
| 23 | #include <linux/cpu.h> |
| 24 | #include <linux/cpumask.h> |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 25 | #include <linux/suspend.h> |
| 26 | #include <linux/clk.h> |
| 27 | #include <linux/err.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/of.h> |
| 30 | #include <trace/events/power.h> |
| 31 | |
| 32 | static DEFINE_MUTEX(l2bw_lock); |
| 33 | |
| 34 | static struct clk *cpu_clk[NR_CPUS]; |
| 35 | static struct clk *l2_clk; |
| 36 | static DEFINE_PER_CPU(struct cpufreq_frequency_table *, freq_table); |
| 37 | static bool hotplug_ready; |
| 38 | |
| 39 | struct cpufreq_suspend_t { |
| 40 | struct mutex suspend_mutex; |
| 41 | int device_suspended; |
| 42 | }; |
| 43 | |
| 44 | static DEFINE_PER_CPU(struct cpufreq_suspend_t, suspend_data); |
Pavankumar Kondeti | d9d13ae | 2017-10-18 08:28:16 +0530 | [diff] [blame] | 45 | static DEFINE_PER_CPU(int, cached_resolve_idx); |
| 46 | static DEFINE_PER_CPU(unsigned int, cached_resolve_freq); |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 47 | |
| 48 | static int set_cpu_freq(struct cpufreq_policy *policy, unsigned int new_freq, |
| 49 | unsigned int index) |
| 50 | { |
| 51 | int ret = 0; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 52 | struct cpufreq_freqs freqs; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 53 | unsigned long rate; |
| 54 | |
| 55 | freqs.old = policy->cur; |
| 56 | freqs.new = new_freq; |
| 57 | freqs.cpu = policy->cpu; |
| 58 | |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 59 | trace_cpu_frequency_switch_start(freqs.old, freqs.new, policy->cpu); |
| 60 | cpufreq_freq_transition_begin(policy, &freqs); |
| 61 | |
| 62 | rate = new_freq * 1000; |
| 63 | rate = clk_round_rate(cpu_clk[policy->cpu], rate); |
| 64 | ret = clk_set_rate(cpu_clk[policy->cpu], rate); |
| 65 | cpufreq_freq_transition_end(policy, &freqs, ret); |
| 66 | if (!ret) |
| 67 | trace_cpu_frequency_switch_end(policy->cpu); |
| 68 | |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | static int msm_cpufreq_target(struct cpufreq_policy *policy, |
| 73 | unsigned int target_freq, |
| 74 | unsigned int relation) |
| 75 | { |
Junjie Wu | 7288107 | 2015-06-10 17:57:07 -0700 | [diff] [blame] | 76 | int ret = 0; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 77 | int index; |
| 78 | struct cpufreq_frequency_table *table; |
Pavankumar Kondeti | d9d13ae | 2017-10-18 08:28:16 +0530 | [diff] [blame] | 79 | int first_cpu = cpumask_first(policy->related_cpus); |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 80 | |
| 81 | mutex_lock(&per_cpu(suspend_data, policy->cpu).suspend_mutex); |
| 82 | |
Junjie Wu | 7288107 | 2015-06-10 17:57:07 -0700 | [diff] [blame] | 83 | if (target_freq == policy->cur) |
| 84 | goto done; |
| 85 | |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 86 | if (per_cpu(suspend_data, policy->cpu).device_suspended) { |
| 87 | pr_debug("cpufreq: cpu%d scheduling frequency change in suspend.\n", |
| 88 | policy->cpu); |
| 89 | ret = -EFAULT; |
| 90 | goto done; |
| 91 | } |
| 92 | |
Stephen Boyd | cf930d6 | 2017-03-02 14:22:37 -0800 | [diff] [blame] | 93 | table = policy->freq_table; |
Pavankumar Kondeti | d9d13ae | 2017-10-18 08:28:16 +0530 | [diff] [blame] | 94 | if (per_cpu(cached_resolve_freq, first_cpu) == target_freq) |
| 95 | index = per_cpu(cached_resolve_idx, first_cpu); |
| 96 | else |
| 97 | index = cpufreq_frequency_table_target(policy, target_freq, |
| 98 | relation); |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 99 | |
| 100 | pr_debug("CPU[%d] target %d relation %d (%d-%d) selected %d\n", |
| 101 | policy->cpu, target_freq, relation, |
| 102 | policy->min, policy->max, table[index].frequency); |
| 103 | |
| 104 | ret = set_cpu_freq(policy, table[index].frequency, |
| 105 | table[index].driver_data); |
| 106 | done: |
| 107 | mutex_unlock(&per_cpu(suspend_data, policy->cpu).suspend_mutex); |
| 108 | return ret; |
| 109 | } |
| 110 | |
Pavankumar Kondeti | 59386c8 | 2017-09-04 11:51:19 +0530 | [diff] [blame] | 111 | static unsigned int msm_cpufreq_resolve_freq(struct cpufreq_policy *policy, |
| 112 | unsigned int target_freq) |
| 113 | { |
| 114 | int index; |
Pavankumar Kondeti | d9d13ae | 2017-10-18 08:28:16 +0530 | [diff] [blame] | 115 | int first_cpu = cpumask_first(policy->related_cpus); |
| 116 | unsigned int freq; |
Pavankumar Kondeti | 59386c8 | 2017-09-04 11:51:19 +0530 | [diff] [blame] | 117 | |
| 118 | index = cpufreq_frequency_table_target(policy, target_freq, |
| 119 | CPUFREQ_RELATION_L); |
Pavankumar Kondeti | d9d13ae | 2017-10-18 08:28:16 +0530 | [diff] [blame] | 120 | freq = policy->freq_table[index].frequency; |
| 121 | |
| 122 | per_cpu(cached_resolve_idx, first_cpu) = index; |
| 123 | per_cpu(cached_resolve_freq, first_cpu) = freq; |
| 124 | |
| 125 | return freq; |
Pavankumar Kondeti | 59386c8 | 2017-09-04 11:51:19 +0530 | [diff] [blame] | 126 | } |
| 127 | |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 128 | static int msm_cpufreq_verify(struct cpufreq_policy *policy) |
| 129 | { |
| 130 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 131 | policy->cpuinfo.max_freq); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static unsigned int msm_cpufreq_get_freq(unsigned int cpu) |
| 136 | { |
| 137 | return clk_get_rate(cpu_clk[cpu]) / 1000; |
| 138 | } |
| 139 | |
| 140 | static int msm_cpufreq_init(struct cpufreq_policy *policy) |
| 141 | { |
| 142 | int cur_freq; |
| 143 | int index; |
| 144 | int ret = 0; |
| 145 | struct cpufreq_frequency_table *table = |
| 146 | per_cpu(freq_table, policy->cpu); |
| 147 | int cpu; |
| 148 | |
| 149 | /* |
| 150 | * In some SoC, some cores are clocked by same source, and their |
| 151 | * frequencies can not be changed independently. Find all other |
| 152 | * CPUs that share same clock, and mark them as controlled by |
| 153 | * same policy. |
| 154 | */ |
| 155 | for_each_possible_cpu(cpu) |
| 156 | if (cpu_clk[cpu] == cpu_clk[policy->cpu]) |
| 157 | cpumask_set_cpu(cpu, policy->cpus); |
| 158 | |
| 159 | ret = cpufreq_table_validate_and_show(policy, table); |
| 160 | if (ret) { |
| 161 | pr_err("cpufreq: failed to get policy min/max\n"); |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | cur_freq = clk_get_rate(cpu_clk[policy->cpu])/1000; |
| 166 | |
Stephen Boyd | 4ade523 | 2017-03-02 14:24:13 -0800 | [diff] [blame] | 167 | index = cpufreq_frequency_table_target(policy, cur_freq, |
| 168 | CPUFREQ_RELATION_H); |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 169 | /* |
| 170 | * Call set_cpu_freq unconditionally so that when cpu is set to |
| 171 | * online, frequency limit will always be updated. |
| 172 | */ |
| 173 | ret = set_cpu_freq(policy, table[index].frequency, |
| 174 | table[index].driver_data); |
| 175 | if (ret) |
| 176 | return ret; |
| 177 | pr_debug("cpufreq: cpu%d init at %d switching to %d\n", |
| 178 | policy->cpu, cur_freq, table[index].frequency); |
| 179 | policy->cur = table[index].frequency; |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
Stephen Boyd | 6208a58 | 2017-03-02 15:09:13 -0800 | [diff] [blame] | 184 | static int qcom_cpufreq_dead_cpu(unsigned int cpu) |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 185 | { |
Stephen Boyd | 6208a58 | 2017-03-02 15:09:13 -0800 | [diff] [blame] | 186 | /* Fail hotplug until this driver can get CPU clocks */ |
| 187 | if (!hotplug_ready) |
| 188 | return -EINVAL; |
| 189 | |
| 190 | clk_unprepare(cpu_clk[cpu]); |
| 191 | clk_unprepare(l2_clk); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int qcom_cpufreq_up_cpu(unsigned int cpu) |
| 196 | { |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 197 | int rc; |
| 198 | |
| 199 | /* Fail hotplug until this driver can get CPU clocks */ |
| 200 | if (!hotplug_ready) |
Stephen Boyd | 6208a58 | 2017-03-02 15:09:13 -0800 | [diff] [blame] | 201 | return -EINVAL; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 202 | |
Stephen Boyd | 6208a58 | 2017-03-02 15:09:13 -0800 | [diff] [blame] | 203 | rc = clk_prepare(l2_clk); |
| 204 | if (rc < 0) |
| 205 | return rc; |
| 206 | rc = clk_prepare(cpu_clk[cpu]); |
| 207 | if (rc < 0) |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 208 | clk_unprepare(l2_clk); |
Stephen Boyd | 6208a58 | 2017-03-02 15:09:13 -0800 | [diff] [blame] | 209 | return rc; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Stephen Boyd | 6208a58 | 2017-03-02 15:09:13 -0800 | [diff] [blame] | 212 | static int qcom_cpufreq_dying_cpu(unsigned int cpu) |
| 213 | { |
| 214 | /* Fail hotplug until this driver can get CPU clocks */ |
| 215 | if (!hotplug_ready) |
| 216 | return -EINVAL; |
| 217 | |
| 218 | clk_disable(cpu_clk[cpu]); |
| 219 | clk_disable(l2_clk); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static int qcom_cpufreq_starting_cpu(unsigned int cpu) |
| 224 | { |
| 225 | int rc; |
| 226 | |
| 227 | /* Fail hotplug until this driver can get CPU clocks */ |
| 228 | if (!hotplug_ready) |
| 229 | return -EINVAL; |
| 230 | |
| 231 | rc = clk_enable(l2_clk); |
| 232 | if (rc < 0) |
| 233 | return rc; |
| 234 | rc = clk_enable(cpu_clk[cpu]); |
| 235 | if (rc < 0) |
| 236 | clk_disable(l2_clk); |
| 237 | return rc; |
| 238 | } |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 239 | |
| 240 | static int msm_cpufreq_suspend(void) |
| 241 | { |
| 242 | int cpu; |
| 243 | |
| 244 | for_each_possible_cpu(cpu) { |
| 245 | mutex_lock(&per_cpu(suspend_data, cpu).suspend_mutex); |
| 246 | per_cpu(suspend_data, cpu).device_suspended = 1; |
| 247 | mutex_unlock(&per_cpu(suspend_data, cpu).suspend_mutex); |
| 248 | } |
| 249 | |
| 250 | return NOTIFY_DONE; |
| 251 | } |
| 252 | |
| 253 | static int msm_cpufreq_resume(void) |
| 254 | { |
Junjie Wu | d5261b0 | 2014-12-01 21:21:00 -0800 | [diff] [blame] | 255 | int cpu, ret; |
| 256 | struct cpufreq_policy policy; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 257 | |
| 258 | for_each_possible_cpu(cpu) { |
| 259 | per_cpu(suspend_data, cpu).device_suspended = 0; |
| 260 | } |
| 261 | |
Junjie Wu | d5261b0 | 2014-12-01 21:21:00 -0800 | [diff] [blame] | 262 | /* |
| 263 | * Freq request might be rejected during suspend, resulting |
| 264 | * in policy->cur violating min/max constraint. |
| 265 | * Correct the frequency as soon as possible. |
| 266 | */ |
| 267 | get_online_cpus(); |
| 268 | for_each_online_cpu(cpu) { |
| 269 | ret = cpufreq_get_policy(&policy, cpu); |
| 270 | if (ret) |
| 271 | continue; |
| 272 | if (policy.cur <= policy.max && policy.cur >= policy.min) |
| 273 | continue; |
| 274 | ret = cpufreq_update_policy(cpu); |
| 275 | if (ret) |
| 276 | pr_info("cpufreq: Current frequency violates policy min/max for CPU%d\n", |
| 277 | cpu); |
| 278 | else |
| 279 | pr_info("cpufreq: Frequency violation fixed for CPU%d\n", |
| 280 | cpu); |
| 281 | } |
| 282 | put_online_cpus(); |
| 283 | |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 284 | return NOTIFY_DONE; |
| 285 | } |
| 286 | |
| 287 | static int msm_cpufreq_pm_event(struct notifier_block *this, |
| 288 | unsigned long event, void *ptr) |
| 289 | { |
| 290 | switch (event) { |
| 291 | case PM_POST_HIBERNATION: |
| 292 | case PM_POST_SUSPEND: |
| 293 | return msm_cpufreq_resume(); |
| 294 | case PM_HIBERNATION_PREPARE: |
| 295 | case PM_SUSPEND_PREPARE: |
| 296 | return msm_cpufreq_suspend(); |
| 297 | default: |
| 298 | return NOTIFY_DONE; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static struct notifier_block msm_cpufreq_pm_notifier = { |
| 303 | .notifier_call = msm_cpufreq_pm_event, |
| 304 | }; |
| 305 | |
| 306 | static struct freq_attr *msm_freq_attr[] = { |
| 307 | &cpufreq_freq_attr_scaling_available_freqs, |
| 308 | NULL, |
| 309 | }; |
| 310 | |
| 311 | static struct cpufreq_driver msm_cpufreq_driver = { |
| 312 | /* lps calculations are handled here. */ |
Stephen Boyd | 4ade523 | 2017-03-02 14:24:13 -0800 | [diff] [blame] | 313 | .flags = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 314 | .init = msm_cpufreq_init, |
| 315 | .verify = msm_cpufreq_verify, |
| 316 | .target = msm_cpufreq_target, |
Pavankumar Kondeti | 59386c8 | 2017-09-04 11:51:19 +0530 | [diff] [blame] | 317 | .resolve_freq = msm_cpufreq_resolve_freq, |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 318 | .get = msm_cpufreq_get_freq, |
| 319 | .name = "msm", |
| 320 | .attr = msm_freq_attr, |
| 321 | }; |
| 322 | |
| 323 | static struct cpufreq_frequency_table *cpufreq_parse_dt(struct device *dev, |
| 324 | char *tbl_name, int cpu) |
| 325 | { |
Rohit Gupta | 13cd56e | 2016-08-22 16:50:59 -0700 | [diff] [blame] | 326 | int ret, nf, i, j; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 327 | u32 *data; |
| 328 | struct cpufreq_frequency_table *ftbl; |
| 329 | |
| 330 | /* Parse list of usable CPU frequencies. */ |
| 331 | if (!of_find_property(dev->of_node, tbl_name, &nf)) |
| 332 | return ERR_PTR(-EINVAL); |
| 333 | nf /= sizeof(*data); |
| 334 | |
| 335 | if (nf == 0) |
| 336 | return ERR_PTR(-EINVAL); |
| 337 | |
| 338 | data = devm_kzalloc(dev, nf * sizeof(*data), GFP_KERNEL); |
| 339 | if (!data) |
| 340 | return ERR_PTR(-ENOMEM); |
| 341 | |
| 342 | ret = of_property_read_u32_array(dev->of_node, tbl_name, data, nf); |
| 343 | if (ret) |
| 344 | return ERR_PTR(ret); |
| 345 | |
| 346 | ftbl = devm_kzalloc(dev, (nf + 1) * sizeof(*ftbl), GFP_KERNEL); |
| 347 | if (!ftbl) |
| 348 | return ERR_PTR(-ENOMEM); |
| 349 | |
Rohit Gupta | 13cd56e | 2016-08-22 16:50:59 -0700 | [diff] [blame] | 350 | j = 0; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 351 | for (i = 0; i < nf; i++) { |
| 352 | unsigned long f; |
| 353 | |
| 354 | f = clk_round_rate(cpu_clk[cpu], data[i] * 1000); |
| 355 | if (IS_ERR_VALUE(f)) |
| 356 | break; |
| 357 | f /= 1000; |
| 358 | |
| 359 | /* |
Rohit Gupta | 13cd56e | 2016-08-22 16:50:59 -0700 | [diff] [blame] | 360 | * Don't repeat frequencies if they round up to the same clock |
| 361 | * frequency. |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 362 | * |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 363 | */ |
Rohit Gupta | 13cd56e | 2016-08-22 16:50:59 -0700 | [diff] [blame] | 364 | if (j > 0 && f <= ftbl[j - 1].frequency) |
| 365 | continue; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 366 | |
Rohit Gupta | 13cd56e | 2016-08-22 16:50:59 -0700 | [diff] [blame] | 367 | ftbl[j].driver_data = j; |
| 368 | ftbl[j].frequency = f; |
| 369 | j++; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Rohit Gupta | 13cd56e | 2016-08-22 16:50:59 -0700 | [diff] [blame] | 372 | ftbl[j].driver_data = j; |
| 373 | ftbl[j].frequency = CPUFREQ_TABLE_END; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 374 | |
| 375 | devm_kfree(dev, data); |
| 376 | |
| 377 | return ftbl; |
| 378 | } |
| 379 | |
| 380 | static int msm_cpufreq_probe(struct platform_device *pdev) |
| 381 | { |
| 382 | struct device *dev = &pdev->dev; |
| 383 | char clk_name[] = "cpu??_clk"; |
| 384 | char tbl_name[] = "qcom,cpufreq-table-??"; |
| 385 | struct clk *c; |
Stephen Boyd | 072b57e | 2017-03-22 18:12:45 -0700 | [diff] [blame] | 386 | int cpu, ret; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 387 | struct cpufreq_frequency_table *ftbl; |
| 388 | |
| 389 | l2_clk = devm_clk_get(dev, "l2_clk"); |
| 390 | if (IS_ERR(l2_clk)) |
| 391 | l2_clk = NULL; |
| 392 | |
| 393 | for_each_possible_cpu(cpu) { |
| 394 | snprintf(clk_name, sizeof(clk_name), "cpu%d_clk", cpu); |
| 395 | c = devm_clk_get(dev, clk_name); |
Santosh Mardi | efe6367 | 2017-01-12 16:48:53 +0530 | [diff] [blame] | 396 | if (cpu == 0 && IS_ERR(c)) |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 397 | return PTR_ERR(c); |
Santosh Mardi | efe6367 | 2017-01-12 16:48:53 +0530 | [diff] [blame] | 398 | else if (IS_ERR(c)) |
| 399 | c = cpu_clk[cpu-1]; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 400 | cpu_clk[cpu] = c; |
| 401 | } |
| 402 | hotplug_ready = true; |
| 403 | |
| 404 | /* Use per-policy governor tunable for some targets */ |
| 405 | if (of_property_read_bool(dev->of_node, "qcom,governor-per-policy")) |
| 406 | msm_cpufreq_driver.flags |= CPUFREQ_HAVE_GOVERNOR_PER_POLICY; |
| 407 | |
| 408 | /* Parse commong cpufreq table for all CPUs */ |
| 409 | ftbl = cpufreq_parse_dt(dev, "qcom,cpufreq-table", 0); |
| 410 | if (!IS_ERR(ftbl)) { |
| 411 | for_each_possible_cpu(cpu) |
| 412 | per_cpu(freq_table, cpu) = ftbl; |
Maria Yu | 1f6dee7 | 2017-12-01 13:40:49 +0800 | [diff] [blame] | 413 | goto out_register; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* |
| 417 | * No common table. Parse individual tables for each unique |
| 418 | * CPU clock. |
| 419 | */ |
| 420 | for_each_possible_cpu(cpu) { |
| 421 | snprintf(tbl_name, sizeof(tbl_name), |
| 422 | "qcom,cpufreq-table-%d", cpu); |
| 423 | ftbl = cpufreq_parse_dt(dev, tbl_name, cpu); |
| 424 | |
| 425 | /* CPU0 must contain freq table */ |
| 426 | if (cpu == 0 && IS_ERR(ftbl)) { |
| 427 | dev_err(dev, "Failed to parse CPU0's freq table\n"); |
| 428 | return PTR_ERR(ftbl); |
| 429 | } |
| 430 | if (cpu == 0) { |
| 431 | per_cpu(freq_table, cpu) = ftbl; |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | if (cpu_clk[cpu] != cpu_clk[cpu - 1] && IS_ERR(ftbl)) { |
| 436 | dev_err(dev, "Failed to parse CPU%d's freq table\n", |
| 437 | cpu); |
| 438 | return PTR_ERR(ftbl); |
| 439 | } |
| 440 | |
| 441 | /* Use previous CPU's table if it shares same clock */ |
| 442 | if (cpu_clk[cpu] == cpu_clk[cpu - 1]) { |
| 443 | if (!IS_ERR(ftbl)) { |
| 444 | dev_warn(dev, "Conflicting tables for CPU%d\n", |
| 445 | cpu); |
| 446 | devm_kfree(dev, ftbl); |
| 447 | } |
| 448 | ftbl = per_cpu(freq_table, cpu - 1); |
| 449 | } |
| 450 | per_cpu(freq_table, cpu) = ftbl; |
| 451 | } |
| 452 | |
Maria Yu | 1f6dee7 | 2017-12-01 13:40:49 +0800 | [diff] [blame] | 453 | out_register: |
Stephen Boyd | 072b57e | 2017-03-22 18:12:45 -0700 | [diff] [blame] | 454 | ret = register_pm_notifier(&msm_cpufreq_pm_notifier); |
| 455 | if (ret) |
| 456 | return ret; |
| 457 | |
| 458 | ret = cpufreq_register_driver(&msm_cpufreq_driver); |
| 459 | if (ret) |
| 460 | unregister_pm_notifier(&msm_cpufreq_pm_notifier); |
| 461 | |
| 462 | return ret; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static const struct of_device_id msm_cpufreq_match_table[] = { |
| 466 | { .compatible = "qcom,msm-cpufreq" }, |
| 467 | {} |
| 468 | }; |
| 469 | |
| 470 | static struct platform_driver msm_cpufreq_plat_driver = { |
| 471 | .probe = msm_cpufreq_probe, |
| 472 | .driver = { |
| 473 | .name = "msm-cpufreq", |
| 474 | .of_match_table = msm_cpufreq_match_table, |
| 475 | }, |
| 476 | }; |
| 477 | |
| 478 | static int __init msm_cpufreq_register(void) |
| 479 | { |
| 480 | int cpu, rc; |
| 481 | |
| 482 | for_each_possible_cpu(cpu) { |
| 483 | mutex_init(&(per_cpu(suspend_data, cpu).suspend_mutex)); |
| 484 | per_cpu(suspend_data, cpu).device_suspended = 0; |
Pavankumar Kondeti | d9d13ae | 2017-10-18 08:28:16 +0530 | [diff] [blame] | 485 | per_cpu(cached_resolve_freq, cpu) = UINT_MAX; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | rc = platform_driver_register(&msm_cpufreq_plat_driver); |
| 489 | if (rc < 0) { |
| 490 | /* Unblock hotplug if msm-cpufreq probe fails */ |
Stephen Boyd | 6208a58 | 2017-03-02 15:09:13 -0800 | [diff] [blame] | 491 | cpuhp_remove_state_nocalls(CPUHP_QCOM_CPUFREQ_PREPARE); |
| 492 | cpuhp_remove_state_nocalls(CPUHP_AP_QCOM_CPUFREQ_STARTING); |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 493 | for_each_possible_cpu(cpu) |
| 494 | mutex_destroy(&(per_cpu(suspend_data, cpu). |
| 495 | suspend_mutex)); |
| 496 | return rc; |
| 497 | } |
| 498 | |
Stephen Boyd | 072b57e | 2017-03-22 18:12:45 -0700 | [diff] [blame] | 499 | return 0; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | subsys_initcall(msm_cpufreq_register); |
| 503 | |
| 504 | static int __init msm_cpufreq_early_register(void) |
| 505 | { |
Stephen Boyd | 6208a58 | 2017-03-02 15:09:13 -0800 | [diff] [blame] | 506 | int ret; |
| 507 | |
| 508 | ret = cpuhp_setup_state_nocalls(CPUHP_AP_QCOM_CPUFREQ_STARTING, |
| 509 | "AP_QCOM_CPUFREQ_STARTING", |
| 510 | qcom_cpufreq_starting_cpu, |
| 511 | qcom_cpufreq_dying_cpu); |
| 512 | if (ret) |
| 513 | return ret; |
| 514 | |
| 515 | ret = cpuhp_setup_state_nocalls(CPUHP_QCOM_CPUFREQ_PREPARE, |
| 516 | "QCOM_CPUFREQ_PREPARE", |
| 517 | qcom_cpufreq_up_cpu, |
| 518 | qcom_cpufreq_dead_cpu); |
| 519 | if (!ret) |
| 520 | return ret; |
| 521 | cpuhp_remove_state_nocalls(CPUHP_AP_QCOM_CPUFREQ_STARTING); |
| 522 | return ret; |
Stephen Boyd | ce5a578 | 2014-08-01 15:39:54 -0700 | [diff] [blame] | 523 | } |
| 524 | core_initcall(msm_cpufreq_early_register); |