Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Linaro Ltd. |
| 3 | * Author: Pi-Cheng Chen <pi-cheng.chen@linaro.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/cpu.h> |
| 17 | #include <linux/cpu_cooling.h> |
| 18 | #include <linux/cpufreq.h> |
| 19 | #include <linux/cpumask.h> |
Arnd Bergmann | 3c2002a | 2016-02-29 17:04:21 +0100 | [diff] [blame] | 20 | #include <linux/module.h> |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 21 | #include <linux/of.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/pm_opp.h> |
| 24 | #include <linux/regulator/consumer.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/thermal.h> |
| 27 | |
| 28 | #define MIN_VOLT_SHIFT (100000) |
| 29 | #define MAX_VOLT_SHIFT (200000) |
| 30 | #define MAX_VOLT_LIMIT (1150000) |
| 31 | #define VOLT_TOL (10000) |
| 32 | |
| 33 | /* |
| 34 | * The struct mtk_cpu_dvfs_info holds necessary information for doing CPU DVFS |
| 35 | * on each CPU power/clock domain of Mediatek SoCs. Each CPU cluster in |
| 36 | * Mediatek SoCs has two voltage inputs, Vproc and Vsram. In some cases the two |
| 37 | * voltage inputs need to be controlled under a hardware limitation: |
| 38 | * 100mV < Vsram - Vproc < 200mV |
| 39 | * |
| 40 | * When scaling the clock frequency of a CPU clock domain, the clock source |
| 41 | * needs to be switched to another stable PLL clock temporarily until |
| 42 | * the original PLL becomes stable at target frequency. |
| 43 | */ |
| 44 | struct mtk_cpu_dvfs_info { |
Pi-Cheng Chen | 89b5604 | 2015-12-10 11:48:13 +0800 | [diff] [blame] | 45 | struct cpumask cpus; |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 46 | struct device *cpu_dev; |
| 47 | struct regulator *proc_reg; |
| 48 | struct regulator *sram_reg; |
| 49 | struct clk *cpu_clk; |
| 50 | struct clk *inter_clk; |
| 51 | struct thermal_cooling_device *cdev; |
Pi-Cheng Chen | 89b5604 | 2015-12-10 11:48:13 +0800 | [diff] [blame] | 52 | struct list_head list_head; |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 53 | int intermediate_voltage; |
| 54 | bool need_voltage_tracking; |
| 55 | }; |
| 56 | |
Pi-Cheng Chen | 89b5604 | 2015-12-10 11:48:13 +0800 | [diff] [blame] | 57 | static LIST_HEAD(dvfs_info_list); |
| 58 | |
| 59 | static struct mtk_cpu_dvfs_info *mtk_cpu_dvfs_info_lookup(int cpu) |
| 60 | { |
| 61 | struct mtk_cpu_dvfs_info *info; |
Pi-Cheng Chen | 89b5604 | 2015-12-10 11:48:13 +0800 | [diff] [blame] | 62 | |
Geliang Tang | d2499d0 | 2016-04-05 10:38:06 +0800 | [diff] [blame] | 63 | list_for_each_entry(info, &dvfs_info_list, list_head) { |
Pi-Cheng Chen | 89b5604 | 2015-12-10 11:48:13 +0800 | [diff] [blame] | 64 | if (cpumask_test_cpu(cpu, &info->cpus)) |
| 65 | return info; |
| 66 | } |
| 67 | |
| 68 | return NULL; |
| 69 | } |
| 70 | |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 71 | static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info, |
| 72 | int new_vproc) |
| 73 | { |
| 74 | struct regulator *proc_reg = info->proc_reg; |
| 75 | struct regulator *sram_reg = info->sram_reg; |
| 76 | int old_vproc, old_vsram, new_vsram, vsram, vproc, ret; |
| 77 | |
| 78 | old_vproc = regulator_get_voltage(proc_reg); |
Pi-Cheng Chen | 40be4c3 | 2015-11-29 16:31:37 +0800 | [diff] [blame] | 79 | if (old_vproc < 0) { |
| 80 | pr_err("%s: invalid Vproc value: %d\n", __func__, old_vproc); |
| 81 | return old_vproc; |
| 82 | } |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 83 | /* Vsram should not exceed the maximum allowed voltage of SoC. */ |
| 84 | new_vsram = min(new_vproc + MIN_VOLT_SHIFT, MAX_VOLT_LIMIT); |
| 85 | |
| 86 | if (old_vproc < new_vproc) { |
| 87 | /* |
| 88 | * When scaling up voltages, Vsram and Vproc scale up step |
| 89 | * by step. At each step, set Vsram to (Vproc + 200mV) first, |
| 90 | * then set Vproc to (Vsram - 100mV). |
| 91 | * Keep doing it until Vsram and Vproc hit target voltages. |
| 92 | */ |
| 93 | do { |
| 94 | old_vsram = regulator_get_voltage(sram_reg); |
Pi-Cheng Chen | 40be4c3 | 2015-11-29 16:31:37 +0800 | [diff] [blame] | 95 | if (old_vsram < 0) { |
| 96 | pr_err("%s: invalid Vsram value: %d\n", |
| 97 | __func__, old_vsram); |
| 98 | return old_vsram; |
| 99 | } |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 100 | old_vproc = regulator_get_voltage(proc_reg); |
Pi-Cheng Chen | 40be4c3 | 2015-11-29 16:31:37 +0800 | [diff] [blame] | 101 | if (old_vproc < 0) { |
| 102 | pr_err("%s: invalid Vproc value: %d\n", |
| 103 | __func__, old_vproc); |
| 104 | return old_vproc; |
| 105 | } |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 106 | |
| 107 | vsram = min(new_vsram, old_vproc + MAX_VOLT_SHIFT); |
| 108 | |
| 109 | if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) { |
| 110 | vsram = MAX_VOLT_LIMIT; |
| 111 | |
| 112 | /* |
| 113 | * If the target Vsram hits the maximum voltage, |
| 114 | * try to set the exact voltage value first. |
| 115 | */ |
| 116 | ret = regulator_set_voltage(sram_reg, vsram, |
| 117 | vsram); |
| 118 | if (ret) |
| 119 | ret = regulator_set_voltage(sram_reg, |
| 120 | vsram - VOLT_TOL, |
| 121 | vsram); |
| 122 | |
| 123 | vproc = new_vproc; |
| 124 | } else { |
| 125 | ret = regulator_set_voltage(sram_reg, vsram, |
| 126 | vsram + VOLT_TOL); |
| 127 | |
| 128 | vproc = vsram - MIN_VOLT_SHIFT; |
| 129 | } |
| 130 | if (ret) |
| 131 | return ret; |
| 132 | |
| 133 | ret = regulator_set_voltage(proc_reg, vproc, |
| 134 | vproc + VOLT_TOL); |
| 135 | if (ret) { |
| 136 | regulator_set_voltage(sram_reg, old_vsram, |
| 137 | old_vsram); |
| 138 | return ret; |
| 139 | } |
| 140 | } while (vproc < new_vproc || vsram < new_vsram); |
| 141 | } else if (old_vproc > new_vproc) { |
| 142 | /* |
| 143 | * When scaling down voltages, Vsram and Vproc scale down step |
| 144 | * by step. At each step, set Vproc to (Vsram - 200mV) first, |
| 145 | * then set Vproc to (Vproc + 100mV). |
| 146 | * Keep doing it until Vsram and Vproc hit target voltages. |
| 147 | */ |
| 148 | do { |
| 149 | old_vproc = regulator_get_voltage(proc_reg); |
Pi-Cheng Chen | 40be4c3 | 2015-11-29 16:31:37 +0800 | [diff] [blame] | 150 | if (old_vproc < 0) { |
| 151 | pr_err("%s: invalid Vproc value: %d\n", |
| 152 | __func__, old_vproc); |
| 153 | return old_vproc; |
| 154 | } |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 155 | old_vsram = regulator_get_voltage(sram_reg); |
Pi-Cheng Chen | 40be4c3 | 2015-11-29 16:31:37 +0800 | [diff] [blame] | 156 | if (old_vsram < 0) { |
| 157 | pr_err("%s: invalid Vsram value: %d\n", |
| 158 | __func__, old_vsram); |
| 159 | return old_vsram; |
| 160 | } |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 161 | |
| 162 | vproc = max(new_vproc, old_vsram - MAX_VOLT_SHIFT); |
| 163 | ret = regulator_set_voltage(proc_reg, vproc, |
| 164 | vproc + VOLT_TOL); |
| 165 | if (ret) |
| 166 | return ret; |
| 167 | |
| 168 | if (vproc == new_vproc) |
| 169 | vsram = new_vsram; |
| 170 | else |
| 171 | vsram = max(new_vsram, vproc + MIN_VOLT_SHIFT); |
| 172 | |
| 173 | if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) { |
| 174 | vsram = MAX_VOLT_LIMIT; |
| 175 | |
| 176 | /* |
| 177 | * If the target Vsram hits the maximum voltage, |
| 178 | * try to set the exact voltage value first. |
| 179 | */ |
| 180 | ret = regulator_set_voltage(sram_reg, vsram, |
| 181 | vsram); |
| 182 | if (ret) |
| 183 | ret = regulator_set_voltage(sram_reg, |
| 184 | vsram - VOLT_TOL, |
| 185 | vsram); |
| 186 | } else { |
| 187 | ret = regulator_set_voltage(sram_reg, vsram, |
| 188 | vsram + VOLT_TOL); |
| 189 | } |
| 190 | |
| 191 | if (ret) { |
| 192 | regulator_set_voltage(proc_reg, old_vproc, |
| 193 | old_vproc); |
| 194 | return ret; |
| 195 | } |
| 196 | } while (vproc > new_vproc + VOLT_TOL || |
| 197 | vsram > new_vsram + VOLT_TOL); |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int mtk_cpufreq_set_voltage(struct mtk_cpu_dvfs_info *info, int vproc) |
| 204 | { |
| 205 | if (info->need_voltage_tracking) |
| 206 | return mtk_cpufreq_voltage_tracking(info, vproc); |
| 207 | else |
| 208 | return regulator_set_voltage(info->proc_reg, vproc, |
| 209 | vproc + VOLT_TOL); |
| 210 | } |
| 211 | |
| 212 | static int mtk_cpufreq_set_target(struct cpufreq_policy *policy, |
| 213 | unsigned int index) |
| 214 | { |
| 215 | struct cpufreq_frequency_table *freq_table = policy->freq_table; |
| 216 | struct clk *cpu_clk = policy->clk; |
| 217 | struct clk *armpll = clk_get_parent(cpu_clk); |
| 218 | struct mtk_cpu_dvfs_info *info = policy->driver_data; |
| 219 | struct device *cpu_dev = info->cpu_dev; |
| 220 | struct dev_pm_opp *opp; |
| 221 | long freq_hz, old_freq_hz; |
| 222 | int vproc, old_vproc, inter_vproc, target_vproc, ret; |
| 223 | |
| 224 | inter_vproc = info->intermediate_voltage; |
| 225 | |
| 226 | old_freq_hz = clk_get_rate(cpu_clk); |
| 227 | old_vproc = regulator_get_voltage(info->proc_reg); |
Pi-Cheng Chen | 40be4c3 | 2015-11-29 16:31:37 +0800 | [diff] [blame] | 228 | if (old_vproc < 0) { |
| 229 | pr_err("%s: invalid Vproc value: %d\n", __func__, old_vproc); |
| 230 | return old_vproc; |
| 231 | } |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 232 | |
| 233 | freq_hz = freq_table[index].frequency * 1000; |
| 234 | |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 235 | opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz); |
| 236 | if (IS_ERR(opp)) { |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 237 | pr_err("cpu%d: failed to find OPP for %ld\n", |
| 238 | policy->cpu, freq_hz); |
| 239 | return PTR_ERR(opp); |
| 240 | } |
| 241 | vproc = dev_pm_opp_get_voltage(opp); |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 242 | dev_pm_opp_put(opp); |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * If the new voltage or the intermediate voltage is higher than the |
| 246 | * current voltage, scale up voltage first. |
| 247 | */ |
| 248 | target_vproc = (inter_vproc > vproc) ? inter_vproc : vproc; |
| 249 | if (old_vproc < target_vproc) { |
| 250 | ret = mtk_cpufreq_set_voltage(info, target_vproc); |
| 251 | if (ret) { |
| 252 | pr_err("cpu%d: failed to scale up voltage!\n", |
| 253 | policy->cpu); |
| 254 | mtk_cpufreq_set_voltage(info, old_vproc); |
| 255 | return ret; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* Reparent the CPU clock to intermediate clock. */ |
| 260 | ret = clk_set_parent(cpu_clk, info->inter_clk); |
| 261 | if (ret) { |
| 262 | pr_err("cpu%d: failed to re-parent cpu clock!\n", |
| 263 | policy->cpu); |
| 264 | mtk_cpufreq_set_voltage(info, old_vproc); |
| 265 | WARN_ON(1); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | /* Set the original PLL to target rate. */ |
| 270 | ret = clk_set_rate(armpll, freq_hz); |
| 271 | if (ret) { |
| 272 | pr_err("cpu%d: failed to scale cpu clock rate!\n", |
| 273 | policy->cpu); |
| 274 | clk_set_parent(cpu_clk, armpll); |
| 275 | mtk_cpufreq_set_voltage(info, old_vproc); |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | /* Set parent of CPU clock back to the original PLL. */ |
| 280 | ret = clk_set_parent(cpu_clk, armpll); |
| 281 | if (ret) { |
| 282 | pr_err("cpu%d: failed to re-parent cpu clock!\n", |
| 283 | policy->cpu); |
| 284 | mtk_cpufreq_set_voltage(info, inter_vproc); |
| 285 | WARN_ON(1); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * If the new voltage is lower than the intermediate voltage or the |
| 291 | * original voltage, scale down to the new voltage. |
| 292 | */ |
| 293 | if (vproc < inter_vproc || vproc < old_vproc) { |
| 294 | ret = mtk_cpufreq_set_voltage(info, vproc); |
| 295 | if (ret) { |
| 296 | pr_err("cpu%d: failed to scale down voltage!\n", |
| 297 | policy->cpu); |
| 298 | clk_set_parent(cpu_clk, info->inter_clk); |
| 299 | clk_set_rate(armpll, old_freq_hz); |
| 300 | clk_set_parent(cpu_clk, armpll); |
| 301 | return ret; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
Dawei Chien | d290160 | 2015-12-16 21:29:14 +0800 | [diff] [blame] | 308 | #define DYNAMIC_POWER "dynamic-power-coefficient" |
| 309 | |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 310 | static void mtk_cpufreq_ready(struct cpufreq_policy *policy) |
| 311 | { |
| 312 | struct mtk_cpu_dvfs_info *info = policy->driver_data; |
| 313 | struct device_node *np = of_node_get(info->cpu_dev->of_node); |
Dawei Chien | d290160 | 2015-12-16 21:29:14 +0800 | [diff] [blame] | 314 | u32 capacitance = 0; |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 315 | |
| 316 | if (WARN_ON(!np)) |
| 317 | return; |
| 318 | |
| 319 | if (of_find_property(np, "#cooling-cells", NULL)) { |
Dawei Chien | d290160 | 2015-12-16 21:29:14 +0800 | [diff] [blame] | 320 | of_property_read_u32(np, DYNAMIC_POWER, &capacitance); |
| 321 | |
| 322 | info->cdev = of_cpufreq_power_cooling_register(np, |
| 323 | policy->related_cpus, |
| 324 | capacitance, |
| 325 | NULL); |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 326 | |
| 327 | if (IS_ERR(info->cdev)) { |
| 328 | dev_err(info->cpu_dev, |
| 329 | "running cpufreq without cooling device: %ld\n", |
| 330 | PTR_ERR(info->cdev)); |
| 331 | |
| 332 | info->cdev = NULL; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | of_node_put(np); |
| 337 | } |
| 338 | |
| 339 | static int mtk_cpu_dvfs_info_init(struct mtk_cpu_dvfs_info *info, int cpu) |
| 340 | { |
| 341 | struct device *cpu_dev; |
| 342 | struct regulator *proc_reg = ERR_PTR(-ENODEV); |
| 343 | struct regulator *sram_reg = ERR_PTR(-ENODEV); |
| 344 | struct clk *cpu_clk = ERR_PTR(-ENODEV); |
| 345 | struct clk *inter_clk = ERR_PTR(-ENODEV); |
| 346 | struct dev_pm_opp *opp; |
| 347 | unsigned long rate; |
| 348 | int ret; |
| 349 | |
| 350 | cpu_dev = get_cpu_device(cpu); |
| 351 | if (!cpu_dev) { |
| 352 | pr_err("failed to get cpu%d device\n", cpu); |
| 353 | return -ENODEV; |
| 354 | } |
| 355 | |
| 356 | cpu_clk = clk_get(cpu_dev, "cpu"); |
| 357 | if (IS_ERR(cpu_clk)) { |
| 358 | if (PTR_ERR(cpu_clk) == -EPROBE_DEFER) |
| 359 | pr_warn("cpu clk for cpu%d not ready, retry.\n", cpu); |
| 360 | else |
| 361 | pr_err("failed to get cpu clk for cpu%d\n", cpu); |
| 362 | |
| 363 | ret = PTR_ERR(cpu_clk); |
| 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | inter_clk = clk_get(cpu_dev, "intermediate"); |
| 368 | if (IS_ERR(inter_clk)) { |
| 369 | if (PTR_ERR(inter_clk) == -EPROBE_DEFER) |
| 370 | pr_warn("intermediate clk for cpu%d not ready, retry.\n", |
| 371 | cpu); |
| 372 | else |
| 373 | pr_err("failed to get intermediate clk for cpu%d\n", |
| 374 | cpu); |
| 375 | |
| 376 | ret = PTR_ERR(inter_clk); |
| 377 | goto out_free_resources; |
| 378 | } |
| 379 | |
| 380 | proc_reg = regulator_get_exclusive(cpu_dev, "proc"); |
| 381 | if (IS_ERR(proc_reg)) { |
| 382 | if (PTR_ERR(proc_reg) == -EPROBE_DEFER) |
| 383 | pr_warn("proc regulator for cpu%d not ready, retry.\n", |
| 384 | cpu); |
| 385 | else |
| 386 | pr_err("failed to get proc regulator for cpu%d\n", |
| 387 | cpu); |
| 388 | |
| 389 | ret = PTR_ERR(proc_reg); |
| 390 | goto out_free_resources; |
| 391 | } |
| 392 | |
| 393 | /* Both presence and absence of sram regulator are valid cases. */ |
| 394 | sram_reg = regulator_get_exclusive(cpu_dev, "sram"); |
| 395 | |
Pi-Cheng Chen | a889331 | 2015-12-27 14:21:57 +0800 | [diff] [blame] | 396 | /* Get OPP-sharing information from "operating-points-v2" bindings */ |
| 397 | ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, &info->cpus); |
| 398 | if (ret) { |
| 399 | pr_err("failed to get OPP-sharing information for cpu%d\n", |
| 400 | cpu); |
| 401 | goto out_free_resources; |
| 402 | } |
| 403 | |
| 404 | ret = dev_pm_opp_of_cpumask_add_table(&info->cpus); |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 405 | if (ret) { |
| 406 | pr_warn("no OPP table for cpu%d\n", cpu); |
| 407 | goto out_free_resources; |
| 408 | } |
| 409 | |
| 410 | /* Search a safe voltage for intermediate frequency. */ |
| 411 | rate = clk_get_rate(inter_clk); |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 412 | opp = dev_pm_opp_find_freq_ceil(cpu_dev, &rate); |
| 413 | if (IS_ERR(opp)) { |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 414 | pr_err("failed to get intermediate opp for cpu%d\n", cpu); |
| 415 | ret = PTR_ERR(opp); |
| 416 | goto out_free_opp_table; |
| 417 | } |
| 418 | info->intermediate_voltage = dev_pm_opp_get_voltage(opp); |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 419 | dev_pm_opp_put(opp); |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 420 | |
| 421 | info->cpu_dev = cpu_dev; |
| 422 | info->proc_reg = proc_reg; |
| 423 | info->sram_reg = IS_ERR(sram_reg) ? NULL : sram_reg; |
| 424 | info->cpu_clk = cpu_clk; |
| 425 | info->inter_clk = inter_clk; |
| 426 | |
| 427 | /* |
| 428 | * If SRAM regulator is present, software "voltage tracking" is needed |
| 429 | * for this CPU power domain. |
| 430 | */ |
| 431 | info->need_voltage_tracking = !IS_ERR(sram_reg); |
| 432 | |
| 433 | return 0; |
| 434 | |
| 435 | out_free_opp_table: |
Pi-Cheng Chen | a889331 | 2015-12-27 14:21:57 +0800 | [diff] [blame] | 436 | dev_pm_opp_of_cpumask_remove_table(&info->cpus); |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 437 | |
| 438 | out_free_resources: |
| 439 | if (!IS_ERR(proc_reg)) |
| 440 | regulator_put(proc_reg); |
| 441 | if (!IS_ERR(sram_reg)) |
| 442 | regulator_put(sram_reg); |
| 443 | if (!IS_ERR(cpu_clk)) |
| 444 | clk_put(cpu_clk); |
| 445 | if (!IS_ERR(inter_clk)) |
| 446 | clk_put(inter_clk); |
| 447 | |
| 448 | return ret; |
| 449 | } |
| 450 | |
| 451 | static void mtk_cpu_dvfs_info_release(struct mtk_cpu_dvfs_info *info) |
| 452 | { |
| 453 | if (!IS_ERR(info->proc_reg)) |
| 454 | regulator_put(info->proc_reg); |
| 455 | if (!IS_ERR(info->sram_reg)) |
| 456 | regulator_put(info->sram_reg); |
| 457 | if (!IS_ERR(info->cpu_clk)) |
| 458 | clk_put(info->cpu_clk); |
| 459 | if (!IS_ERR(info->inter_clk)) |
| 460 | clk_put(info->inter_clk); |
| 461 | |
Pi-Cheng Chen | a889331 | 2015-12-27 14:21:57 +0800 | [diff] [blame] | 462 | dev_pm_opp_of_cpumask_remove_table(&info->cpus); |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static int mtk_cpufreq_init(struct cpufreq_policy *policy) |
| 466 | { |
| 467 | struct mtk_cpu_dvfs_info *info; |
| 468 | struct cpufreq_frequency_table *freq_table; |
| 469 | int ret; |
| 470 | |
Pi-Cheng Chen | 89b5604 | 2015-12-10 11:48:13 +0800 | [diff] [blame] | 471 | info = mtk_cpu_dvfs_info_lookup(policy->cpu); |
| 472 | if (!info) { |
| 473 | pr_err("dvfs info for cpu%d is not initialized.\n", |
| 474 | policy->cpu); |
| 475 | return -EINVAL; |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | ret = dev_pm_opp_init_cpufreq_table(info->cpu_dev, &freq_table); |
| 479 | if (ret) { |
| 480 | pr_err("failed to init cpufreq table for cpu%d: %d\n", |
| 481 | policy->cpu, ret); |
Pi-Cheng Chen | 89b5604 | 2015-12-10 11:48:13 +0800 | [diff] [blame] | 482 | return ret; |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | ret = cpufreq_table_validate_and_show(policy, freq_table); |
| 486 | if (ret) { |
| 487 | pr_err("%s: invalid frequency table: %d\n", __func__, ret); |
| 488 | goto out_free_cpufreq_table; |
| 489 | } |
| 490 | |
Pi-Cheng Chen | 89b5604 | 2015-12-10 11:48:13 +0800 | [diff] [blame] | 491 | cpumask_copy(policy->cpus, &info->cpus); |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 492 | policy->driver_data = info; |
| 493 | policy->clk = info->cpu_clk; |
| 494 | |
| 495 | return 0; |
| 496 | |
| 497 | out_free_cpufreq_table: |
| 498 | dev_pm_opp_free_cpufreq_table(info->cpu_dev, &freq_table); |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 499 | return ret; |
| 500 | } |
| 501 | |
| 502 | static int mtk_cpufreq_exit(struct cpufreq_policy *policy) |
| 503 | { |
| 504 | struct mtk_cpu_dvfs_info *info = policy->driver_data; |
| 505 | |
| 506 | cpufreq_cooling_unregister(info->cdev); |
| 507 | dev_pm_opp_free_cpufreq_table(info->cpu_dev, &policy->freq_table); |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static struct cpufreq_driver mt8173_cpufreq_driver = { |
Pi-Cheng Chen | 9bb46b8 | 2015-11-29 16:31:35 +0800 | [diff] [blame] | 513 | .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK | |
| 514 | CPUFREQ_HAVE_GOVERNOR_PER_POLICY, |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 515 | .verify = cpufreq_generic_frequency_table_verify, |
| 516 | .target_index = mtk_cpufreq_set_target, |
| 517 | .get = cpufreq_generic_get, |
| 518 | .init = mtk_cpufreq_init, |
| 519 | .exit = mtk_cpufreq_exit, |
| 520 | .ready = mtk_cpufreq_ready, |
| 521 | .name = "mtk-cpufreq", |
| 522 | .attr = cpufreq_generic_attr, |
| 523 | }; |
| 524 | |
| 525 | static int mt8173_cpufreq_probe(struct platform_device *pdev) |
| 526 | { |
Geliang Tang | d2499d0 | 2016-04-05 10:38:06 +0800 | [diff] [blame] | 527 | struct mtk_cpu_dvfs_info *info, *tmp; |
Pi-Cheng Chen | 89b5604 | 2015-12-10 11:48:13 +0800 | [diff] [blame] | 528 | int cpu, ret; |
| 529 | |
| 530 | for_each_possible_cpu(cpu) { |
| 531 | info = mtk_cpu_dvfs_info_lookup(cpu); |
| 532 | if (info) |
| 533 | continue; |
| 534 | |
| 535 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 536 | if (!info) { |
| 537 | ret = -ENOMEM; |
| 538 | goto release_dvfs_info_list; |
| 539 | } |
| 540 | |
| 541 | ret = mtk_cpu_dvfs_info_init(info, cpu); |
| 542 | if (ret) { |
| 543 | dev_err(&pdev->dev, |
| 544 | "failed to initialize dvfs info for cpu%d\n", |
| 545 | cpu); |
| 546 | goto release_dvfs_info_list; |
| 547 | } |
| 548 | |
| 549 | list_add(&info->list_head, &dvfs_info_list); |
| 550 | } |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 551 | |
| 552 | ret = cpufreq_register_driver(&mt8173_cpufreq_driver); |
Pi-Cheng Chen | 89b5604 | 2015-12-10 11:48:13 +0800 | [diff] [blame] | 553 | if (ret) { |
| 554 | dev_err(&pdev->dev, "failed to register mtk cpufreq driver\n"); |
| 555 | goto release_dvfs_info_list; |
| 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | |
| 560 | release_dvfs_info_list: |
Geliang Tang | d2499d0 | 2016-04-05 10:38:06 +0800 | [diff] [blame] | 561 | list_for_each_entry_safe(info, tmp, &dvfs_info_list, list_head) { |
Pi-Cheng Chen | 89b5604 | 2015-12-10 11:48:13 +0800 | [diff] [blame] | 562 | mtk_cpu_dvfs_info_release(info); |
Geliang Tang | d2499d0 | 2016-04-05 10:38:06 +0800 | [diff] [blame] | 563 | list_del(&info->list_head); |
Pi-Cheng Chen | 89b5604 | 2015-12-10 11:48:13 +0800 | [diff] [blame] | 564 | } |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 565 | |
| 566 | return ret; |
| 567 | } |
| 568 | |
| 569 | static struct platform_driver mt8173_cpufreq_platdrv = { |
| 570 | .driver = { |
| 571 | .name = "mt8173-cpufreq", |
| 572 | }, |
| 573 | .probe = mt8173_cpufreq_probe, |
| 574 | }; |
| 575 | |
Daniel Kurtz | cf9a243 | 2017-03-02 19:03:45 +0800 | [diff] [blame] | 576 | /* List of machines supported by this driver */ |
| 577 | static const struct of_device_id mt8173_cpufreq_machines[] __initconst = { |
| 578 | { .compatible = "mediatek,mt817x", }, |
| 579 | { .compatible = "mediatek,mt8173", }, |
| 580 | { .compatible = "mediatek,mt8176", }, |
| 581 | |
| 582 | { } |
| 583 | }; |
| 584 | |
Daniel Kurtz | 08a74cb | 2017-03-02 19:08:58 +0800 | [diff] [blame] | 585 | static int __init mt8173_cpufreq_driver_init(void) |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 586 | { |
Daniel Kurtz | cf9a243 | 2017-03-02 19:03:45 +0800 | [diff] [blame] | 587 | struct device_node *np; |
| 588 | const struct of_device_id *match; |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 589 | struct platform_device *pdev; |
| 590 | int err; |
| 591 | |
Daniel Kurtz | cf9a243 | 2017-03-02 19:03:45 +0800 | [diff] [blame] | 592 | np = of_find_node_by_path("/"); |
| 593 | if (!np) |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 594 | return -ENODEV; |
| 595 | |
Daniel Kurtz | cf9a243 | 2017-03-02 19:03:45 +0800 | [diff] [blame] | 596 | match = of_match_node(mt8173_cpufreq_machines, np); |
| 597 | of_node_put(np); |
| 598 | if (!match) { |
| 599 | pr_warn("Machine is not compatible with mt8173-cpufreq\n"); |
| 600 | return -ENODEV; |
| 601 | } |
| 602 | |
Pi-Cheng Chen | 1453863 | 2015-08-19 10:05:06 +0800 | [diff] [blame] | 603 | err = platform_driver_register(&mt8173_cpufreq_platdrv); |
| 604 | if (err) |
| 605 | return err; |
| 606 | |
| 607 | /* |
| 608 | * Since there's no place to hold device registration code and no |
| 609 | * device tree based way to match cpufreq driver yet, both the driver |
| 610 | * and the device registration codes are put here to handle defer |
| 611 | * probing. |
| 612 | */ |
| 613 | pdev = platform_device_register_simple("mt8173-cpufreq", -1, NULL, 0); |
| 614 | if (IS_ERR(pdev)) { |
| 615 | pr_err("failed to register mtk-cpufreq platform device\n"); |
| 616 | return PTR_ERR(pdev); |
| 617 | } |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | device_initcall(mt8173_cpufreq_driver_init); |