Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * 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 | */ |
| 8 | |
| 9 | #include <linux/clk.h> |
Sudeep KarkadaNagesha | b494b48 | 2013-09-10 18:59:47 +0100 | [diff] [blame] | 10 | #include <linux/cpu.h> |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 11 | #include <linux/cpufreq.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/of.h> |
Nishanth Menon | e4db1c7 | 2013-09-19 16:03:52 -0500 | [diff] [blame] | 16 | #include <linux/pm_opp.h> |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/regulator/consumer.h> |
| 19 | |
| 20 | #define PU_SOC_VOLTAGE_NORMAL 1250000 |
| 21 | #define PU_SOC_VOLTAGE_HIGH 1275000 |
| 22 | #define FREQ_1P2_GHZ 1200000000 |
| 23 | |
| 24 | static struct regulator *arm_reg; |
| 25 | static struct regulator *pu_reg; |
| 26 | static struct regulator *soc_reg; |
| 27 | |
| 28 | static struct clk *arm_clk; |
| 29 | static struct clk *pll1_sys_clk; |
| 30 | static struct clk *pll1_sw_clk; |
| 31 | static struct clk *step_clk; |
| 32 | static struct clk *pll2_pfd2_396m_clk; |
| 33 | |
| 34 | static struct device *cpu_dev; |
| 35 | static struct cpufreq_frequency_table *freq_table; |
| 36 | static unsigned int transition_latency; |
| 37 | |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 38 | static unsigned int imx6q_get_speed(unsigned int cpu) |
| 39 | { |
| 40 | return clk_get_rate(arm_clk) / 1000; |
| 41 | } |
| 42 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 43 | static int imx6q_set_target(struct cpufreq_policy *policy, unsigned int index) |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 44 | { |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 45 | struct dev_pm_opp *opp; |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 46 | unsigned long freq_hz, volt, volt_old; |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame^] | 47 | unsigned int old_freq, new_freq; |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 48 | int ret; |
| 49 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame^] | 50 | new_freq = freq_table[index].frequency; |
| 51 | freq_hz = new_freq * 1000; |
| 52 | old_freq = clk_get_rate(arm_clk) / 1000; |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 53 | |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 54 | rcu_read_lock(); |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 55 | opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz); |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 56 | if (IS_ERR(opp)) { |
| 57 | rcu_read_unlock(); |
| 58 | dev_err(cpu_dev, "failed to find OPP for %ld\n", freq_hz); |
| 59 | return PTR_ERR(opp); |
| 60 | } |
| 61 | |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 62 | volt = dev_pm_opp_get_voltage(opp); |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 63 | rcu_read_unlock(); |
| 64 | volt_old = regulator_get_voltage(arm_reg); |
| 65 | |
| 66 | dev_dbg(cpu_dev, "%u MHz, %ld mV --> %u MHz, %ld mV\n", |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame^] | 67 | old_freq / 1000, volt_old / 1000, |
| 68 | new_freq / 1000, volt / 1000); |
Viresh Kumar | 5a571c3 | 2013-06-19 11:18:20 +0530 | [diff] [blame] | 69 | |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 70 | /* scaling up? scale voltage before frequency */ |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame^] | 71 | if (new_freq > old_freq) { |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 72 | ret = regulator_set_voltage_tol(arm_reg, volt, 0); |
| 73 | if (ret) { |
| 74 | dev_err(cpu_dev, |
| 75 | "failed to scale vddarm up: %d\n", ret); |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame^] | 76 | return ret; |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Need to increase vddpu and vddsoc for safety |
| 81 | * if we are about to run at 1.2 GHz. |
| 82 | */ |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame^] | 83 | if (new_freq == FREQ_1P2_GHZ / 1000) { |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 84 | regulator_set_voltage_tol(pu_reg, |
| 85 | PU_SOC_VOLTAGE_HIGH, 0); |
| 86 | regulator_set_voltage_tol(soc_reg, |
| 87 | PU_SOC_VOLTAGE_HIGH, 0); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * The setpoints are selected per PLL/PDF frequencies, so we need to |
| 93 | * reprogram PLL for frequency scaling. The procedure of reprogramming |
| 94 | * PLL1 is as below. |
| 95 | * |
| 96 | * - Enable pll2_pfd2_396m_clk and reparent pll1_sw_clk to it |
| 97 | * - Reprogram pll1_sys_clk and reparent pll1_sw_clk back to it |
| 98 | * - Disable pll2_pfd2_396m_clk |
| 99 | */ |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 100 | clk_set_parent(step_clk, pll2_pfd2_396m_clk); |
| 101 | clk_set_parent(pll1_sw_clk, step_clk); |
| 102 | if (freq_hz > clk_get_rate(pll2_pfd2_396m_clk)) { |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame^] | 103 | clk_set_rate(pll1_sys_clk, new_freq * 1000); |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 104 | clk_set_parent(pll1_sw_clk, pll1_sys_clk); |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* Ensure the arm clock divider is what we expect */ |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame^] | 108 | ret = clk_set_rate(arm_clk, new_freq * 1000); |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 109 | if (ret) { |
| 110 | dev_err(cpu_dev, "failed to set clock rate: %d\n", ret); |
| 111 | regulator_set_voltage_tol(arm_reg, volt_old, 0); |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame^] | 112 | return ret; |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* scaling down? scale voltage after frequency */ |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame^] | 116 | if (new_freq < old_freq) { |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 117 | ret = regulator_set_voltage_tol(arm_reg, volt, 0); |
Viresh Kumar | 5a571c3 | 2013-06-19 11:18:20 +0530 | [diff] [blame] | 118 | if (ret) { |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 119 | dev_warn(cpu_dev, |
| 120 | "failed to scale vddarm down: %d\n", ret); |
Viresh Kumar | 5a571c3 | 2013-06-19 11:18:20 +0530 | [diff] [blame] | 121 | ret = 0; |
| 122 | } |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 123 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame^] | 124 | if (old_freq == FREQ_1P2_GHZ / 1000) { |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 125 | regulator_set_voltage_tol(pu_reg, |
| 126 | PU_SOC_VOLTAGE_NORMAL, 0); |
| 127 | regulator_set_voltage_tol(soc_reg, |
| 128 | PU_SOC_VOLTAGE_NORMAL, 0); |
| 129 | } |
| 130 | } |
| 131 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame^] | 132 | return 0; |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static int imx6q_cpufreq_init(struct cpufreq_policy *policy) |
| 136 | { |
Viresh Kumar | 17922dd | 2013-10-03 20:29:14 +0530 | [diff] [blame] | 137 | return cpufreq_generic_init(policy, freq_table, transition_latency); |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 140 | static struct cpufreq_driver imx6q_cpufreq_driver = { |
Viresh Kumar | 4f6ba38 | 2013-10-03 20:28:08 +0530 | [diff] [blame] | 141 | .verify = cpufreq_generic_frequency_table_verify, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 142 | .target_index = imx6q_set_target, |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 143 | .get = imx6q_get_speed, |
| 144 | .init = imx6q_cpufreq_init, |
Viresh Kumar | 4f6ba38 | 2013-10-03 20:28:08 +0530 | [diff] [blame] | 145 | .exit = cpufreq_generic_exit, |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 146 | .name = "imx6q-cpufreq", |
Viresh Kumar | 4f6ba38 | 2013-10-03 20:28:08 +0530 | [diff] [blame] | 147 | .attr = cpufreq_generic_attr, |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static int imx6q_cpufreq_probe(struct platform_device *pdev) |
| 151 | { |
| 152 | struct device_node *np; |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 153 | struct dev_pm_opp *opp; |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 154 | unsigned long min_volt, max_volt; |
| 155 | int num, ret; |
| 156 | |
Sudeep KarkadaNagesha | b494b48 | 2013-09-10 18:59:47 +0100 | [diff] [blame] | 157 | cpu_dev = get_cpu_device(0); |
| 158 | if (!cpu_dev) { |
| 159 | pr_err("failed to get cpu0 device\n"); |
| 160 | return -ENODEV; |
| 161 | } |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 162 | |
Sudeep KarkadaNagesha | cdc58d6 | 2013-06-17 14:58:48 +0100 | [diff] [blame] | 163 | np = of_node_get(cpu_dev->of_node); |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 164 | if (!np) { |
| 165 | dev_err(cpu_dev, "failed to find cpu0 node\n"); |
| 166 | return -ENOENT; |
| 167 | } |
| 168 | |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 169 | arm_clk = devm_clk_get(cpu_dev, "arm"); |
| 170 | pll1_sys_clk = devm_clk_get(cpu_dev, "pll1_sys"); |
| 171 | pll1_sw_clk = devm_clk_get(cpu_dev, "pll1_sw"); |
| 172 | step_clk = devm_clk_get(cpu_dev, "step"); |
| 173 | pll2_pfd2_396m_clk = devm_clk_get(cpu_dev, "pll2_pfd2_396m"); |
| 174 | if (IS_ERR(arm_clk) || IS_ERR(pll1_sys_clk) || IS_ERR(pll1_sw_clk) || |
| 175 | IS_ERR(step_clk) || IS_ERR(pll2_pfd2_396m_clk)) { |
| 176 | dev_err(cpu_dev, "failed to get clocks\n"); |
| 177 | ret = -ENOENT; |
| 178 | goto put_node; |
| 179 | } |
| 180 | |
| 181 | arm_reg = devm_regulator_get(cpu_dev, "arm"); |
| 182 | pu_reg = devm_regulator_get(cpu_dev, "pu"); |
| 183 | soc_reg = devm_regulator_get(cpu_dev, "soc"); |
Wei Yongjun | 3a3656d | 2013-02-22 04:39:30 +0000 | [diff] [blame] | 184 | if (IS_ERR(arm_reg) || IS_ERR(pu_reg) || IS_ERR(soc_reg)) { |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 185 | dev_err(cpu_dev, "failed to get regulators\n"); |
| 186 | ret = -ENOENT; |
| 187 | goto put_node; |
| 188 | } |
| 189 | |
| 190 | /* We expect an OPP table supplied by platform */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 191 | num = dev_pm_opp_get_opp_count(cpu_dev); |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 192 | if (num < 0) { |
| 193 | ret = num; |
| 194 | dev_err(cpu_dev, "no OPP table is found: %d\n", ret); |
| 195 | goto put_node; |
| 196 | } |
| 197 | |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 198 | ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table); |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 199 | if (ret) { |
| 200 | dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret); |
| 201 | goto put_node; |
| 202 | } |
| 203 | |
| 204 | if (of_property_read_u32(np, "clock-latency", &transition_latency)) |
| 205 | transition_latency = CPUFREQ_ETERNAL; |
| 206 | |
| 207 | /* |
| 208 | * OPP is maintained in order of increasing frequency, and |
| 209 | * freq_table initialised from OPP is therefore sorted in the |
| 210 | * same order. |
| 211 | */ |
| 212 | rcu_read_lock(); |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 213 | opp = dev_pm_opp_find_freq_exact(cpu_dev, |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 214 | freq_table[0].frequency * 1000, true); |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 215 | min_volt = dev_pm_opp_get_voltage(opp); |
| 216 | opp = dev_pm_opp_find_freq_exact(cpu_dev, |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 217 | freq_table[--num].frequency * 1000, true); |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 218 | max_volt = dev_pm_opp_get_voltage(opp); |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 219 | rcu_read_unlock(); |
| 220 | ret = regulator_set_voltage_time(arm_reg, min_volt, max_volt); |
| 221 | if (ret > 0) |
| 222 | transition_latency += ret * 1000; |
| 223 | |
| 224 | /* Count vddpu and vddsoc latency in for 1.2 GHz support */ |
| 225 | if (freq_table[num].frequency == FREQ_1P2_GHZ / 1000) { |
| 226 | ret = regulator_set_voltage_time(pu_reg, PU_SOC_VOLTAGE_NORMAL, |
| 227 | PU_SOC_VOLTAGE_HIGH); |
| 228 | if (ret > 0) |
| 229 | transition_latency += ret * 1000; |
| 230 | ret = regulator_set_voltage_time(soc_reg, PU_SOC_VOLTAGE_NORMAL, |
| 231 | PU_SOC_VOLTAGE_HIGH); |
| 232 | if (ret > 0) |
| 233 | transition_latency += ret * 1000; |
| 234 | } |
| 235 | |
| 236 | ret = cpufreq_register_driver(&imx6q_cpufreq_driver); |
| 237 | if (ret) { |
| 238 | dev_err(cpu_dev, "failed register driver: %d\n", ret); |
| 239 | goto free_freq_table; |
| 240 | } |
| 241 | |
| 242 | of_node_put(np); |
| 243 | return 0; |
| 244 | |
| 245 | free_freq_table: |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 246 | dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table); |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 247 | put_node: |
| 248 | of_node_put(np); |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | static int imx6q_cpufreq_remove(struct platform_device *pdev) |
| 253 | { |
| 254 | cpufreq_unregister_driver(&imx6q_cpufreq_driver); |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 255 | dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table); |
Shawn Guo | 1dd538f | 2013-02-04 05:46:29 +0000 | [diff] [blame] | 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static struct platform_driver imx6q_cpufreq_platdrv = { |
| 261 | .driver = { |
| 262 | .name = "imx6q-cpufreq", |
| 263 | .owner = THIS_MODULE, |
| 264 | }, |
| 265 | .probe = imx6q_cpufreq_probe, |
| 266 | .remove = imx6q_cpufreq_remove, |
| 267 | }; |
| 268 | module_platform_driver(imx6q_cpufreq_platdrv); |
| 269 | |
| 270 | MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>"); |
| 271 | MODULE_DESCRIPTION("Freescale i.MX6Q cpufreq driver"); |
| 272 | MODULE_LICENSE("GPL"); |