Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | * CPU frequency scaling for DaVinci |
| 3 | * |
| 4 | * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/ |
| 5 | * |
| 6 | * Based on linux/arch/arm/plat-omap/cpu-omap.c. Original Copyright follows: |
| 7 | * |
| 8 | * Copyright (C) 2005 Nokia Corporation |
| 9 | * Written by Tony Lindgren <tony@atomide.com> |
| 10 | * |
| 11 | * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King |
| 12 | * |
| 13 | * Copyright (C) 2007-2008 Texas Instruments, Inc. |
| 14 | * Updated to support OMAP3 |
| 15 | * Rajendra Nayak <rnayak@ti.com> |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or modify |
| 18 | * it under the terms of the GNU General Public License version 2 as |
| 19 | * published by the Free Software Foundation. |
| 20 | */ |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/cpufreq.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/clk.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | |
| 28 | #include <mach/hardware.h> |
| 29 | #include <mach/cpufreq.h> |
| 30 | #include <mach/common.h> |
| 31 | |
| 32 | #include "clock.h" |
| 33 | |
| 34 | struct davinci_cpufreq { |
| 35 | struct device *dev; |
| 36 | struct clk *armclk; |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 37 | struct clk *asyncclk; |
| 38 | unsigned long asyncrate; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 39 | }; |
| 40 | static struct davinci_cpufreq cpufreq; |
| 41 | |
| 42 | static int davinci_verify_speed(struct cpufreq_policy *policy) |
| 43 | { |
| 44 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 45 | struct cpufreq_frequency_table *freq_table = pdata->freq_table; |
| 46 | struct clk *armclk = cpufreq.armclk; |
| 47 | |
| 48 | if (freq_table) |
| 49 | return cpufreq_frequency_table_verify(policy, freq_table); |
| 50 | |
| 51 | if (policy->cpu) |
| 52 | return -EINVAL; |
| 53 | |
| 54 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 55 | policy->cpuinfo.max_freq); |
| 56 | |
| 57 | policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000; |
| 58 | policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000; |
| 59 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 60 | policy->cpuinfo.max_freq); |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static unsigned int davinci_getspeed(unsigned int cpu) |
| 65 | { |
| 66 | if (cpu) |
| 67 | return 0; |
| 68 | |
| 69 | return clk_get_rate(cpufreq.armclk) / 1000; |
| 70 | } |
| 71 | |
| 72 | static int davinci_target(struct cpufreq_policy *policy, |
| 73 | unsigned int target_freq, unsigned int relation) |
| 74 | { |
| 75 | int ret = 0; |
| 76 | unsigned int idx; |
| 77 | struct cpufreq_freqs freqs; |
| 78 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 79 | struct clk *armclk = cpufreq.armclk; |
| 80 | |
| 81 | /* |
| 82 | * Ensure desired rate is within allowed range. Some govenors |
| 83 | * (ondemand) will just pass target_freq=0 to get the minimum. |
| 84 | */ |
| 85 | if (target_freq < policy->cpuinfo.min_freq) |
| 86 | target_freq = policy->cpuinfo.min_freq; |
| 87 | if (target_freq > policy->cpuinfo.max_freq) |
| 88 | target_freq = policy->cpuinfo.max_freq; |
| 89 | |
| 90 | freqs.old = davinci_getspeed(0); |
| 91 | freqs.new = clk_round_rate(armclk, target_freq * 1000) / 1000; |
| 92 | freqs.cpu = 0; |
| 93 | |
| 94 | if (freqs.old == freqs.new) |
| 95 | return ret; |
| 96 | |
| 97 | cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, |
| 98 | dev_driver_string(cpufreq.dev), |
| 99 | "transition: %u --> %u\n", freqs.old, freqs.new); |
| 100 | |
| 101 | ret = cpufreq_frequency_table_target(policy, pdata->freq_table, |
| 102 | freqs.new, relation, &idx); |
| 103 | if (ret) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); |
| 107 | |
| 108 | /* if moving to higher frequency, up the voltage beforehand */ |
Sekhar Nori | fca97b3 | 2010-07-20 16:46:48 +0530 | [diff] [blame] | 109 | if (pdata->set_voltage && freqs.new > freqs.old) { |
| 110 | ret = pdata->set_voltage(idx); |
| 111 | if (ret) |
| 112 | goto out; |
| 113 | } |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 114 | |
| 115 | ret = clk_set_rate(armclk, idx); |
Sekhar Nori | fca97b3 | 2010-07-20 16:46:48 +0530 | [diff] [blame] | 116 | if (ret) |
| 117 | goto out; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 118 | |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 119 | if (cpufreq.asyncclk) { |
| 120 | ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate); |
| 121 | if (ret) |
| 122 | goto out; |
| 123 | } |
| 124 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 125 | /* if moving to lower freq, lower the voltage after lowering freq */ |
| 126 | if (pdata->set_voltage && freqs.new < freqs.old) |
| 127 | pdata->set_voltage(idx); |
| 128 | |
Sekhar Nori | fca97b3 | 2010-07-20 16:46:48 +0530 | [diff] [blame] | 129 | out: |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 130 | cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); |
| 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
Axel Lin | 079db59 | 2011-02-28 15:51:33 +0530 | [diff] [blame] | 135 | static int davinci_cpu_init(struct cpufreq_policy *policy) |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 136 | { |
| 137 | int result = 0; |
| 138 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 139 | struct cpufreq_frequency_table *freq_table = pdata->freq_table; |
| 140 | |
| 141 | if (policy->cpu != 0) |
| 142 | return -EINVAL; |
| 143 | |
Sekhar Nori | 13d5e27 | 2009-10-22 15:12:16 +0530 | [diff] [blame] | 144 | /* Finish platform specific initialization */ |
| 145 | if (pdata->init) { |
| 146 | result = pdata->init(); |
| 147 | if (result) |
| 148 | return result; |
| 149 | } |
| 150 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 151 | policy->cur = policy->min = policy->max = davinci_getspeed(0); |
| 152 | |
| 153 | if (freq_table) { |
| 154 | result = cpufreq_frequency_table_cpuinfo(policy, freq_table); |
| 155 | if (!result) |
| 156 | cpufreq_frequency_table_get_attr(freq_table, |
| 157 | policy->cpu); |
| 158 | } else { |
| 159 | policy->cpuinfo.min_freq = policy->min; |
| 160 | policy->cpuinfo.max_freq = policy->max; |
| 161 | } |
| 162 | |
| 163 | policy->min = policy->cpuinfo.min_freq; |
| 164 | policy->max = policy->cpuinfo.max_freq; |
| 165 | policy->cur = davinci_getspeed(0); |
| 166 | |
| 167 | /* |
| 168 | * Time measurement across the target() function yields ~1500-1800us |
| 169 | * time taken with no drivers on notification list. |
| 170 | * Setting the latency to 2000 us to accomodate addition of drivers |
| 171 | * to pre/post change notification list. |
| 172 | */ |
| 173 | policy->cpuinfo.transition_latency = 2000 * 1000; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int davinci_cpu_exit(struct cpufreq_policy *policy) |
| 178 | { |
| 179 | cpufreq_frequency_table_put_attr(policy->cpu); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static struct freq_attr *davinci_cpufreq_attr[] = { |
| 184 | &cpufreq_freq_attr_scaling_available_freqs, |
| 185 | NULL, |
| 186 | }; |
| 187 | |
| 188 | static struct cpufreq_driver davinci_driver = { |
| 189 | .flags = CPUFREQ_STICKY, |
| 190 | .verify = davinci_verify_speed, |
| 191 | .target = davinci_target, |
| 192 | .get = davinci_getspeed, |
| 193 | .init = davinci_cpu_init, |
| 194 | .exit = davinci_cpu_exit, |
| 195 | .name = "davinci", |
| 196 | .attr = davinci_cpufreq_attr, |
| 197 | }; |
| 198 | |
| 199 | static int __init davinci_cpufreq_probe(struct platform_device *pdev) |
| 200 | { |
| 201 | struct davinci_cpufreq_config *pdata = pdev->dev.platform_data; |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 202 | struct clk *asyncclk; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 203 | |
| 204 | if (!pdata) |
| 205 | return -EINVAL; |
| 206 | if (!pdata->freq_table) |
| 207 | return -EINVAL; |
| 208 | |
| 209 | cpufreq.dev = &pdev->dev; |
| 210 | |
| 211 | cpufreq.armclk = clk_get(NULL, "arm"); |
| 212 | if (IS_ERR(cpufreq.armclk)) { |
| 213 | dev_err(cpufreq.dev, "Unable to get ARM clock\n"); |
| 214 | return PTR_ERR(cpufreq.armclk); |
| 215 | } |
| 216 | |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 217 | asyncclk = clk_get(cpufreq.dev, "async"); |
| 218 | if (!IS_ERR(asyncclk)) { |
| 219 | cpufreq.asyncclk = asyncclk; |
| 220 | cpufreq.asyncrate = clk_get_rate(asyncclk); |
| 221 | } |
| 222 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 223 | return cpufreq_register_driver(&davinci_driver); |
| 224 | } |
| 225 | |
| 226 | static int __exit davinci_cpufreq_remove(struct platform_device *pdev) |
| 227 | { |
| 228 | clk_put(cpufreq.armclk); |
| 229 | |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 230 | if (cpufreq.asyncclk) |
| 231 | clk_put(cpufreq.asyncclk); |
| 232 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 233 | return cpufreq_unregister_driver(&davinci_driver); |
| 234 | } |
| 235 | |
| 236 | static struct platform_driver davinci_cpufreq_driver = { |
| 237 | .driver = { |
| 238 | .name = "cpufreq-davinci", |
| 239 | .owner = THIS_MODULE, |
| 240 | }, |
| 241 | .remove = __exit_p(davinci_cpufreq_remove), |
| 242 | }; |
| 243 | |
| 244 | static int __init davinci_cpufreq_init(void) |
| 245 | { |
| 246 | return platform_driver_probe(&davinci_cpufreq_driver, |
| 247 | davinci_cpufreq_probe); |
| 248 | } |
| 249 | late_initcall(davinci_cpufreq_init); |
| 250 | |