Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * drivers/cpufreq/spear-cpufreq.c |
| 3 | * |
| 4 | * CPU Frequency Scaling for SPEAr platform |
| 5 | * |
| 6 | * Copyright (C) 2012 ST Microelectronics |
| 7 | * Deepak Sikri <deepak.sikri@st.com> |
| 8 | * |
| 9 | * This file is licensed under the terms of the GNU General Public |
| 10 | * License version 2. This program is licensed "as is" without any |
| 11 | * warranty of any kind, whether express or implied. |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/cpufreq.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/module.h> |
Sudeep KarkadaNagesha | c0e4694 | 2013-06-17 15:09:15 +0100 | [diff] [blame] | 21 | #include <linux/of_device.h> |
Viresh Kumar | 2449d33 | 2014-03-10 15:43:18 +0530 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | #include <linux/types.h> |
| 25 | |
| 26 | /* SPEAr CPUFreq driver data structure */ |
| 27 | static struct { |
| 28 | struct clk *clk; |
| 29 | unsigned int transition_latency; |
| 30 | struct cpufreq_frequency_table *freq_tbl; |
| 31 | u32 cnt; |
| 32 | } spear_cpufreq; |
| 33 | |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 34 | static struct clk *spear1340_cpu_get_possible_parent(unsigned long newfreq) |
| 35 | { |
| 36 | struct clk *sys_pclk; |
| 37 | int pclk; |
| 38 | /* |
| 39 | * In SPEAr1340, cpu clk's parent sys clk can take input from |
| 40 | * following sources |
| 41 | */ |
| 42 | const char *sys_clk_src[] = { |
| 43 | "sys_syn_clk", |
| 44 | "pll1_clk", |
| 45 | "pll2_clk", |
| 46 | "pll3_clk", |
| 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * As sys clk can have multiple source with their own range |
| 51 | * limitation so we choose possible sources accordingly |
| 52 | */ |
| 53 | if (newfreq <= 300000000) |
| 54 | pclk = 0; /* src is sys_syn_clk */ |
| 55 | else if (newfreq > 300000000 && newfreq <= 500000000) |
| 56 | pclk = 3; /* src is pll3_clk */ |
| 57 | else if (newfreq == 600000000) |
| 58 | pclk = 1; /* src is pll1_clk */ |
| 59 | else |
| 60 | return ERR_PTR(-EINVAL); |
| 61 | |
| 62 | /* Get parent to sys clock */ |
| 63 | sys_pclk = clk_get(NULL, sys_clk_src[pclk]); |
| 64 | if (IS_ERR(sys_pclk)) |
| 65 | pr_err("Failed to get %s clock\n", sys_clk_src[pclk]); |
| 66 | |
| 67 | return sys_pclk; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * In SPEAr1340, we cannot use newfreq directly because we need to actually |
| 72 | * access a source clock (clk) which might not be ancestor of cpu at present. |
| 73 | * Hence in SPEAr1340 we would operate on source clock directly before switching |
| 74 | * cpu clock to it. |
| 75 | */ |
| 76 | static int spear1340_set_cpu_rate(struct clk *sys_pclk, unsigned long newfreq) |
| 77 | { |
| 78 | struct clk *sys_clk; |
| 79 | int ret = 0; |
| 80 | |
| 81 | sys_clk = clk_get_parent(spear_cpufreq.clk); |
| 82 | if (IS_ERR(sys_clk)) { |
| 83 | pr_err("failed to get cpu's parent (sys) clock\n"); |
| 84 | return PTR_ERR(sys_clk); |
| 85 | } |
| 86 | |
| 87 | /* Set the rate of the source clock before changing the parent */ |
| 88 | ret = clk_set_rate(sys_pclk, newfreq); |
| 89 | if (ret) { |
| 90 | pr_err("Failed to set sys clk rate to %lu\n", newfreq); |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | ret = clk_set_parent(sys_clk, sys_pclk); |
| 95 | if (ret) { |
| 96 | pr_err("Failed to set sys clk parent\n"); |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int spear_cpufreq_target(struct cpufreq_policy *policy, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 104 | unsigned int index) |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 105 | { |
Sachin Kamat | bb25f13 | 2013-09-26 10:56:42 +0530 | [diff] [blame] | 106 | long newfreq; |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 107 | struct clk *srcclk; |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 108 | int ret, mult = 1; |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 109 | |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 110 | newfreq = spear_cpufreq.freq_tbl[index].frequency * 1000; |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 111 | |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 112 | if (of_machine_is_compatible("st,spear1340")) { |
| 113 | /* |
| 114 | * SPEAr1340 is special in the sense that due to the possibility |
| 115 | * of multiple clock sources for cpu clk's parent we can have |
| 116 | * different clock source for different frequency of cpu clk. |
| 117 | * Hence we need to choose one from amongst these possible clock |
| 118 | * sources. |
| 119 | */ |
| 120 | srcclk = spear1340_cpu_get_possible_parent(newfreq); |
| 121 | if (IS_ERR(srcclk)) { |
| 122 | pr_err("Failed to get src clk\n"); |
| 123 | return PTR_ERR(srcclk); |
| 124 | } |
| 125 | |
| 126 | /* SPEAr1340: src clk is always 2 * intended cpu clk */ |
| 127 | mult = 2; |
| 128 | } else { |
| 129 | /* |
| 130 | * src clock to be altered is ancestor of cpu clock. Hence we |
| 131 | * can directly work on cpu clk |
| 132 | */ |
| 133 | srcclk = spear_cpufreq.clk; |
| 134 | } |
| 135 | |
| 136 | newfreq = clk_round_rate(srcclk, newfreq * mult); |
Paul Walmsley | 189c9b8 | 2013-11-25 18:04:34 -0800 | [diff] [blame] | 137 | if (newfreq <= 0) { |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 138 | pr_err("clk_round_rate failed for cpu src clock\n"); |
| 139 | return newfreq; |
| 140 | } |
| 141 | |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 142 | if (mult == 2) |
| 143 | ret = spear1340_set_cpu_rate(srcclk, newfreq); |
| 144 | else |
| 145 | ret = clk_set_rate(spear_cpufreq.clk, newfreq); |
| 146 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 147 | if (ret) |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 148 | pr_err("CPU Freq: cpu clk_set_rate failed: %d\n", ret); |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 149 | |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | static int spear_cpufreq_init(struct cpufreq_policy *policy) |
| 154 | { |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 155 | policy->clk = spear_cpufreq.clk; |
Viresh Kumar | 7a936bd | 2013-10-03 20:42:10 +0530 | [diff] [blame] | 156 | return cpufreq_generic_init(policy, spear_cpufreq.freq_tbl, |
| 157 | spear_cpufreq.transition_latency); |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 160 | static struct cpufreq_driver spear_cpufreq_driver = { |
| 161 | .name = "cpufreq-spear", |
Viresh Kumar | ae6b427 | 2013-12-03 11:20:45 +0530 | [diff] [blame] | 162 | .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
Viresh Kumar | e2132fa | 2013-10-03 20:28:27 +0530 | [diff] [blame] | 163 | .verify = cpufreq_generic_frequency_table_verify, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 164 | .target_index = spear_cpufreq_target, |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 165 | .get = cpufreq_generic_get, |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 166 | .init = spear_cpufreq_init, |
Viresh Kumar | e2132fa | 2013-10-03 20:28:27 +0530 | [diff] [blame] | 167 | .attr = cpufreq_generic_attr, |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 168 | }; |
| 169 | |
Viresh Kumar | 2449d33 | 2014-03-10 15:43:18 +0530 | [diff] [blame] | 170 | static int spear_cpufreq_probe(struct platform_device *pdev) |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 171 | { |
| 172 | struct device_node *np; |
| 173 | const struct property *prop; |
| 174 | struct cpufreq_frequency_table *freq_tbl; |
| 175 | const __be32 *val; |
| 176 | int cnt, i, ret; |
| 177 | |
Sudeep KarkadaNagesha | c0e4694 | 2013-06-17 15:09:15 +0100 | [diff] [blame] | 178 | np = of_cpu_device_node_get(0); |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 179 | if (!np) { |
| 180 | pr_err("No cpu node found"); |
| 181 | return -ENODEV; |
| 182 | } |
| 183 | |
| 184 | if (of_property_read_u32(np, "clock-latency", |
| 185 | &spear_cpufreq.transition_latency)) |
| 186 | spear_cpufreq.transition_latency = CPUFREQ_ETERNAL; |
| 187 | |
| 188 | prop = of_find_property(np, "cpufreq_tbl", NULL); |
| 189 | if (!prop || !prop->value) { |
| 190 | pr_err("Invalid cpufreq_tbl"); |
| 191 | ret = -ENODEV; |
| 192 | goto out_put_node; |
| 193 | } |
| 194 | |
| 195 | cnt = prop->length / sizeof(u32); |
| 196 | val = prop->value; |
| 197 | |
Viresh Kumar | 71508a1 | 2014-03-28 19:11:46 +0530 | [diff] [blame] | 198 | freq_tbl = kzalloc(sizeof(*freq_tbl) * (cnt + 1), GFP_KERNEL); |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 199 | if (!freq_tbl) { |
| 200 | ret = -ENOMEM; |
| 201 | goto out_put_node; |
| 202 | } |
| 203 | |
Viresh Kumar | 71508a1 | 2014-03-28 19:11:46 +0530 | [diff] [blame] | 204 | for (i = 0; i < cnt; i++) |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 205 | freq_tbl[i].frequency = be32_to_cpup(val++); |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 206 | |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 207 | freq_tbl[i].frequency = CPUFREQ_TABLE_END; |
| 208 | |
| 209 | spear_cpufreq.freq_tbl = freq_tbl; |
| 210 | |
| 211 | of_node_put(np); |
| 212 | |
| 213 | spear_cpufreq.clk = clk_get(NULL, "cpu_clk"); |
| 214 | if (IS_ERR(spear_cpufreq.clk)) { |
| 215 | pr_err("Unable to get CPU clock\n"); |
| 216 | ret = PTR_ERR(spear_cpufreq.clk); |
| 217 | goto out_put_mem; |
| 218 | } |
| 219 | |
| 220 | ret = cpufreq_register_driver(&spear_cpufreq_driver); |
| 221 | if (!ret) |
| 222 | return 0; |
| 223 | |
| 224 | pr_err("failed register driver: %d\n", ret); |
| 225 | clk_put(spear_cpufreq.clk); |
| 226 | |
| 227 | out_put_mem: |
| 228 | kfree(freq_tbl); |
| 229 | return ret; |
| 230 | |
| 231 | out_put_node: |
| 232 | of_node_put(np); |
| 233 | return ret; |
| 234 | } |
Viresh Kumar | 2449d33 | 2014-03-10 15:43:18 +0530 | [diff] [blame] | 235 | |
| 236 | static struct platform_driver spear_cpufreq_platdrv = { |
| 237 | .driver = { |
| 238 | .name = "spear-cpufreq", |
| 239 | .owner = THIS_MODULE, |
| 240 | }, |
| 241 | .probe = spear_cpufreq_probe, |
| 242 | }; |
| 243 | module_platform_driver(spear_cpufreq_platdrv); |
Deepak Sikri | 4209932 | 2012-11-27 14:05:26 +0100 | [diff] [blame] | 244 | |
| 245 | MODULE_AUTHOR("Deepak Sikri <deepak.sikri@st.com>"); |
| 246 | MODULE_DESCRIPTION("SPEAr CPUFreq driver"); |
| 247 | MODULE_LICENSE("GPL"); |