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; |
| 37 | }; |
| 38 | static struct davinci_cpufreq cpufreq; |
| 39 | |
| 40 | static int davinci_verify_speed(struct cpufreq_policy *policy) |
| 41 | { |
| 42 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 43 | struct cpufreq_frequency_table *freq_table = pdata->freq_table; |
| 44 | struct clk *armclk = cpufreq.armclk; |
| 45 | |
| 46 | if (freq_table) |
| 47 | return cpufreq_frequency_table_verify(policy, freq_table); |
| 48 | |
| 49 | if (policy->cpu) |
| 50 | return -EINVAL; |
| 51 | |
| 52 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 53 | policy->cpuinfo.max_freq); |
| 54 | |
| 55 | policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000; |
| 56 | policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000; |
| 57 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 58 | policy->cpuinfo.max_freq); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static unsigned int davinci_getspeed(unsigned int cpu) |
| 63 | { |
| 64 | if (cpu) |
| 65 | return 0; |
| 66 | |
| 67 | return clk_get_rate(cpufreq.armclk) / 1000; |
| 68 | } |
| 69 | |
| 70 | static int davinci_target(struct cpufreq_policy *policy, |
| 71 | unsigned int target_freq, unsigned int relation) |
| 72 | { |
| 73 | int ret = 0; |
| 74 | unsigned int idx; |
| 75 | struct cpufreq_freqs freqs; |
| 76 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 77 | struct clk *armclk = cpufreq.armclk; |
| 78 | |
| 79 | /* |
| 80 | * Ensure desired rate is within allowed range. Some govenors |
| 81 | * (ondemand) will just pass target_freq=0 to get the minimum. |
| 82 | */ |
| 83 | if (target_freq < policy->cpuinfo.min_freq) |
| 84 | target_freq = policy->cpuinfo.min_freq; |
| 85 | if (target_freq > policy->cpuinfo.max_freq) |
| 86 | target_freq = policy->cpuinfo.max_freq; |
| 87 | |
| 88 | freqs.old = davinci_getspeed(0); |
| 89 | freqs.new = clk_round_rate(armclk, target_freq * 1000) / 1000; |
| 90 | freqs.cpu = 0; |
| 91 | |
| 92 | if (freqs.old == freqs.new) |
| 93 | return ret; |
| 94 | |
| 95 | cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, |
| 96 | dev_driver_string(cpufreq.dev), |
| 97 | "transition: %u --> %u\n", freqs.old, freqs.new); |
| 98 | |
| 99 | ret = cpufreq_frequency_table_target(policy, pdata->freq_table, |
| 100 | freqs.new, relation, &idx); |
| 101 | if (ret) |
| 102 | return -EINVAL; |
| 103 | |
| 104 | cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); |
| 105 | |
| 106 | /* if moving to higher frequency, up the voltage beforehand */ |
| 107 | if (pdata->set_voltage && freqs.new > freqs.old) |
| 108 | pdata->set_voltage(idx); |
| 109 | |
| 110 | ret = clk_set_rate(armclk, idx); |
| 111 | |
| 112 | /* if moving to lower freq, lower the voltage after lowering freq */ |
| 113 | if (pdata->set_voltage && freqs.new < freqs.old) |
| 114 | pdata->set_voltage(idx); |
| 115 | |
| 116 | cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); |
| 117 | |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | static int __init davinci_cpu_init(struct cpufreq_policy *policy) |
| 122 | { |
| 123 | int result = 0; |
| 124 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 125 | struct cpufreq_frequency_table *freq_table = pdata->freq_table; |
| 126 | |
| 127 | if (policy->cpu != 0) |
| 128 | return -EINVAL; |
| 129 | |
Sekhar Nori | 13d5e27 | 2009-10-22 15:12:16 +0530 | [diff] [blame] | 130 | /* Finish platform specific initialization */ |
| 131 | if (pdata->init) { |
| 132 | result = pdata->init(); |
| 133 | if (result) |
| 134 | return result; |
| 135 | } |
| 136 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 137 | policy->cur = policy->min = policy->max = davinci_getspeed(0); |
| 138 | |
| 139 | if (freq_table) { |
| 140 | result = cpufreq_frequency_table_cpuinfo(policy, freq_table); |
| 141 | if (!result) |
| 142 | cpufreq_frequency_table_get_attr(freq_table, |
| 143 | policy->cpu); |
| 144 | } else { |
| 145 | policy->cpuinfo.min_freq = policy->min; |
| 146 | policy->cpuinfo.max_freq = policy->max; |
| 147 | } |
| 148 | |
| 149 | policy->min = policy->cpuinfo.min_freq; |
| 150 | policy->max = policy->cpuinfo.max_freq; |
| 151 | policy->cur = davinci_getspeed(0); |
| 152 | |
| 153 | /* |
| 154 | * Time measurement across the target() function yields ~1500-1800us |
| 155 | * time taken with no drivers on notification list. |
| 156 | * Setting the latency to 2000 us to accomodate addition of drivers |
| 157 | * to pre/post change notification list. |
| 158 | */ |
| 159 | policy->cpuinfo.transition_latency = 2000 * 1000; |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static int davinci_cpu_exit(struct cpufreq_policy *policy) |
| 164 | { |
| 165 | cpufreq_frequency_table_put_attr(policy->cpu); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static struct freq_attr *davinci_cpufreq_attr[] = { |
| 170 | &cpufreq_freq_attr_scaling_available_freqs, |
| 171 | NULL, |
| 172 | }; |
| 173 | |
| 174 | static struct cpufreq_driver davinci_driver = { |
| 175 | .flags = CPUFREQ_STICKY, |
| 176 | .verify = davinci_verify_speed, |
| 177 | .target = davinci_target, |
| 178 | .get = davinci_getspeed, |
| 179 | .init = davinci_cpu_init, |
| 180 | .exit = davinci_cpu_exit, |
| 181 | .name = "davinci", |
| 182 | .attr = davinci_cpufreq_attr, |
| 183 | }; |
| 184 | |
| 185 | static int __init davinci_cpufreq_probe(struct platform_device *pdev) |
| 186 | { |
| 187 | struct davinci_cpufreq_config *pdata = pdev->dev.platform_data; |
| 188 | |
| 189 | if (!pdata) |
| 190 | return -EINVAL; |
| 191 | if (!pdata->freq_table) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | cpufreq.dev = &pdev->dev; |
| 195 | |
| 196 | cpufreq.armclk = clk_get(NULL, "arm"); |
| 197 | if (IS_ERR(cpufreq.armclk)) { |
| 198 | dev_err(cpufreq.dev, "Unable to get ARM clock\n"); |
| 199 | return PTR_ERR(cpufreq.armclk); |
| 200 | } |
| 201 | |
| 202 | return cpufreq_register_driver(&davinci_driver); |
| 203 | } |
| 204 | |
| 205 | static int __exit davinci_cpufreq_remove(struct platform_device *pdev) |
| 206 | { |
| 207 | clk_put(cpufreq.armclk); |
| 208 | |
| 209 | return cpufreq_unregister_driver(&davinci_driver); |
| 210 | } |
| 211 | |
| 212 | static struct platform_driver davinci_cpufreq_driver = { |
| 213 | .driver = { |
| 214 | .name = "cpufreq-davinci", |
| 215 | .owner = THIS_MODULE, |
| 216 | }, |
| 217 | .remove = __exit_p(davinci_cpufreq_remove), |
| 218 | }; |
| 219 | |
| 220 | static int __init davinci_cpufreq_init(void) |
| 221 | { |
| 222 | return platform_driver_probe(&davinci_cpufreq_driver, |
| 223 | davinci_cpufreq_probe); |
| 224 | } |
| 225 | late_initcall(davinci_cpufreq_init); |
| 226 | |