.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (C) 2013 Freescale Semiconductor, Inc. |
---|
3 | | - * |
---|
4 | | - * This program is free software; you can redistribute it and/or modify |
---|
5 | | - * it under the terms of the GNU General Public License version 2 as |
---|
6 | | - * published by the Free Software Foundation. |
---|
7 | 4 | */ |
---|
8 | 5 | |
---|
9 | 6 | #include <linux/clk.h> |
---|
10 | 7 | #include <linux/cpu.h> |
---|
11 | 8 | #include <linux/cpufreq.h> |
---|
12 | | -#include <linux/cpu_cooling.h> |
---|
13 | 9 | #include <linux/err.h> |
---|
14 | 10 | #include <linux/module.h> |
---|
15 | 11 | #include <linux/nvmem-consumer.h> |
---|
.. | .. |
---|
52 | 48 | }; |
---|
53 | 49 | |
---|
54 | 50 | static struct device *cpu_dev; |
---|
55 | | -static struct thermal_cooling_device *cdev; |
---|
56 | | -static bool free_opp; |
---|
57 | 51 | static struct cpufreq_frequency_table *freq_table; |
---|
58 | 52 | static unsigned int max_freq; |
---|
59 | 53 | static unsigned int transition_latency; |
---|
.. | .. |
---|
177 | 171 | /* scaling down? scale voltage after frequency */ |
---|
178 | 172 | if (new_freq < old_freq) { |
---|
179 | 173 | ret = regulator_set_voltage_tol(arm_reg, volt, 0); |
---|
180 | | - if (ret) { |
---|
| 174 | + if (ret) |
---|
181 | 175 | dev_warn(cpu_dev, |
---|
182 | 176 | "failed to scale vddarm down: %d\n", ret); |
---|
183 | | - ret = 0; |
---|
184 | | - } |
---|
185 | 177 | ret = regulator_set_voltage_tol(soc_reg, imx6_soc_volt[index], 0); |
---|
186 | | - if (ret) { |
---|
| 178 | + if (ret) |
---|
187 | 179 | dev_warn(cpu_dev, "failed to scale vddsoc down: %d\n", ret); |
---|
188 | | - ret = 0; |
---|
189 | | - } |
---|
190 | 180 | if (!IS_ERR(pu_reg)) { |
---|
191 | 181 | ret = regulator_set_voltage_tol(pu_reg, imx6_soc_volt[index], 0); |
---|
192 | | - if (ret) { |
---|
| 182 | + if (ret) |
---|
193 | 183 | dev_warn(cpu_dev, "failed to scale vddpu down: %d\n", ret); |
---|
194 | | - ret = 0; |
---|
195 | | - } |
---|
196 | 184 | } |
---|
197 | 185 | } |
---|
198 | 186 | |
---|
199 | 187 | return 0; |
---|
200 | 188 | } |
---|
201 | 189 | |
---|
202 | | -static void imx6q_cpufreq_ready(struct cpufreq_policy *policy) |
---|
203 | | -{ |
---|
204 | | - cdev = of_cpufreq_cooling_register(policy); |
---|
205 | | - |
---|
206 | | - if (!cdev) |
---|
207 | | - dev_err(cpu_dev, |
---|
208 | | - "running cpufreq without cooling device: %ld\n", |
---|
209 | | - PTR_ERR(cdev)); |
---|
210 | | -} |
---|
211 | | - |
---|
212 | 190 | static int imx6q_cpufreq_init(struct cpufreq_policy *policy) |
---|
213 | 191 | { |
---|
214 | | - int ret; |
---|
215 | | - |
---|
216 | 192 | policy->clk = clks[ARM].clk; |
---|
217 | | - ret = cpufreq_generic_init(policy, freq_table, transition_latency); |
---|
| 193 | + cpufreq_generic_init(policy, freq_table, transition_latency); |
---|
218 | 194 | policy->suspend_freq = max_freq; |
---|
219 | | - |
---|
220 | | - return ret; |
---|
221 | | -} |
---|
222 | | - |
---|
223 | | -static int imx6q_cpufreq_exit(struct cpufreq_policy *policy) |
---|
224 | | -{ |
---|
225 | | - cpufreq_cooling_unregister(cdev); |
---|
| 195 | + dev_pm_opp_of_register_em(cpu_dev, policy->cpus); |
---|
226 | 196 | |
---|
227 | 197 | return 0; |
---|
228 | 198 | } |
---|
229 | 199 | |
---|
230 | 200 | static struct cpufreq_driver imx6q_cpufreq_driver = { |
---|
231 | | - .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
---|
| 201 | + .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK | |
---|
| 202 | + CPUFREQ_IS_COOLING_DEV, |
---|
232 | 203 | .verify = cpufreq_generic_frequency_table_verify, |
---|
233 | 204 | .target_index = imx6q_set_target, |
---|
234 | 205 | .get = cpufreq_generic_get, |
---|
235 | 206 | .init = imx6q_cpufreq_init, |
---|
236 | | - .exit = imx6q_cpufreq_exit, |
---|
237 | 207 | .name = "imx6q-cpufreq", |
---|
238 | | - .ready = imx6q_cpufreq_ready, |
---|
239 | 208 | .attr = cpufreq_generic_attr, |
---|
240 | 209 | .suspend = cpufreq_generic_suspend, |
---|
241 | 210 | }; |
---|
.. | .. |
---|
246 | 215 | #define OCOTP_CFG3_SPEED_996MHZ 0x2 |
---|
247 | 216 | #define OCOTP_CFG3_SPEED_852MHZ 0x1 |
---|
248 | 217 | |
---|
249 | | -static void imx6q_opp_check_speed_grading(struct device *dev) |
---|
| 218 | +static int imx6q_opp_check_speed_grading(struct device *dev) |
---|
250 | 219 | { |
---|
251 | 220 | struct device_node *np; |
---|
252 | 221 | void __iomem *base; |
---|
253 | 222 | u32 val; |
---|
| 223 | + int ret; |
---|
254 | 224 | |
---|
255 | | - np = of_find_compatible_node(NULL, NULL, "fsl,imx6q-ocotp"); |
---|
256 | | - if (!np) |
---|
257 | | - return; |
---|
| 225 | + if (of_find_property(dev->of_node, "nvmem-cells", NULL)) { |
---|
| 226 | + ret = nvmem_cell_read_u32(dev, "speed_grade", &val); |
---|
| 227 | + if (ret) |
---|
| 228 | + return ret; |
---|
| 229 | + } else { |
---|
| 230 | + np = of_find_compatible_node(NULL, NULL, "fsl,imx6q-ocotp"); |
---|
| 231 | + if (!np) |
---|
| 232 | + return -ENOENT; |
---|
258 | 233 | |
---|
259 | | - base = of_iomap(np, 0); |
---|
260 | | - if (!base) { |
---|
261 | | - dev_err(dev, "failed to map ocotp\n"); |
---|
262 | | - goto put_node; |
---|
| 234 | + base = of_iomap(np, 0); |
---|
| 235 | + of_node_put(np); |
---|
| 236 | + if (!base) { |
---|
| 237 | + dev_err(dev, "failed to map ocotp\n"); |
---|
| 238 | + return -EFAULT; |
---|
| 239 | + } |
---|
| 240 | + |
---|
| 241 | + /* |
---|
| 242 | + * SPEED_GRADING[1:0] defines the max speed of ARM: |
---|
| 243 | + * 2b'11: 1200000000Hz; |
---|
| 244 | + * 2b'10: 996000000Hz; |
---|
| 245 | + * 2b'01: 852000000Hz; -- i.MX6Q Only, exclusive with 996MHz. |
---|
| 246 | + * 2b'00: 792000000Hz; |
---|
| 247 | + * We need to set the max speed of ARM according to fuse map. |
---|
| 248 | + */ |
---|
| 249 | + val = readl_relaxed(base + OCOTP_CFG3); |
---|
| 250 | + iounmap(base); |
---|
263 | 251 | } |
---|
264 | 252 | |
---|
265 | | - /* |
---|
266 | | - * SPEED_GRADING[1:0] defines the max speed of ARM: |
---|
267 | | - * 2b'11: 1200000000Hz; |
---|
268 | | - * 2b'10: 996000000Hz; |
---|
269 | | - * 2b'01: 852000000Hz; -- i.MX6Q Only, exclusive with 996MHz. |
---|
270 | | - * 2b'00: 792000000Hz; |
---|
271 | | - * We need to set the max speed of ARM according to fuse map. |
---|
272 | | - */ |
---|
273 | | - val = readl_relaxed(base + OCOTP_CFG3); |
---|
274 | 253 | val >>= OCOTP_CFG3_SPEED_SHIFT; |
---|
275 | 254 | val &= 0x3; |
---|
276 | 255 | |
---|
.. | .. |
---|
287 | 266 | if (dev_pm_opp_disable(dev, 1200000000)) |
---|
288 | 267 | dev_warn(dev, "failed to disable 1.2GHz OPP\n"); |
---|
289 | 268 | } |
---|
290 | | - iounmap(base); |
---|
291 | | -put_node: |
---|
292 | | - of_node_put(np); |
---|
| 269 | + |
---|
| 270 | + return 0; |
---|
293 | 271 | } |
---|
294 | 272 | |
---|
295 | 273 | #define OCOTP_CFG3_6UL_SPEED_696MHZ 0x2 |
---|
.. | .. |
---|
414 | 392 | if (of_machine_is_compatible("fsl,imx6ul") || |
---|
415 | 393 | of_machine_is_compatible("fsl,imx6ull")) { |
---|
416 | 394 | ret = imx6ul_opp_check_speed_grading(cpu_dev); |
---|
417 | | - if (ret == -EPROBE_DEFER) |
---|
418 | | - return ret; |
---|
419 | | - if (ret) { |
---|
| 395 | + } else { |
---|
| 396 | + ret = imx6q_opp_check_speed_grading(cpu_dev); |
---|
| 397 | + } |
---|
| 398 | + if (ret) { |
---|
| 399 | + if (ret != -EPROBE_DEFER) |
---|
420 | 400 | dev_err(cpu_dev, "failed to read ocotp: %d\n", |
---|
421 | 401 | ret); |
---|
422 | | - return ret; |
---|
423 | | - } |
---|
424 | | - } else { |
---|
425 | | - imx6q_opp_check_speed_grading(cpu_dev); |
---|
| 402 | + goto out_free_opp; |
---|
426 | 403 | } |
---|
427 | 404 | |
---|
428 | | - /* Because we have added the OPPs here, we must free them */ |
---|
429 | | - free_opp = true; |
---|
430 | 405 | num = dev_pm_opp_get_opp_count(cpu_dev); |
---|
431 | 406 | if (num < 0) { |
---|
432 | 407 | ret = num; |
---|
.. | .. |
---|
528 | 503 | free_freq_table: |
---|
529 | 504 | dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table); |
---|
530 | 505 | out_free_opp: |
---|
531 | | - if (free_opp) |
---|
532 | | - dev_pm_opp_of_remove_table(cpu_dev); |
---|
| 506 | + dev_pm_opp_of_remove_table(cpu_dev); |
---|
533 | 507 | put_reg: |
---|
534 | 508 | if (!IS_ERR(arm_reg)) |
---|
535 | 509 | regulator_put(arm_reg); |
---|
.. | .. |
---|
549 | 523 | { |
---|
550 | 524 | cpufreq_unregister_driver(&imx6q_cpufreq_driver); |
---|
551 | 525 | dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table); |
---|
552 | | - if (free_opp) |
---|
553 | | - dev_pm_opp_of_remove_table(cpu_dev); |
---|
| 526 | + dev_pm_opp_of_remove_table(cpu_dev); |
---|
554 | 527 | regulator_put(arm_reg); |
---|
555 | 528 | if (!IS_ERR(pu_reg)) |
---|
556 | 529 | regulator_put(pu_reg); |
---|