2 * CPU frequency scaling for DaVinci
4 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
6 * Based on linux/arch/arm/plat-omap/cpu-omap.c. Original Copyright follows:
8 * Copyright (C) 2005 Nokia Corporation
9 * Written by Tony Lindgren <tony@atomide.com>
11 * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
13 * Copyright (C) 2007-2008 Texas Instruments, Inc.
14 * Updated to support OMAP3
15 * Rajendra Nayak <rnayak@ti.com>
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.
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>
28 #include <mach/hardware.h>
29 #include <mach/cpufreq.h>
30 #include <mach/common.h>
34 struct davinci_cpufreq
{
38 static struct davinci_cpufreq cpufreq
;
40 static int davinci_verify_speed(struct cpufreq_policy
*policy
)
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
;
47 return cpufreq_frequency_table_verify(policy
, freq_table
);
52 cpufreq_verify_within_limits(policy
, policy
->cpuinfo
.min_freq
,
53 policy
->cpuinfo
.max_freq
);
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
);
62 static unsigned int davinci_getspeed(unsigned int cpu
)
67 return clk_get_rate(cpufreq
.armclk
) / 1000;
70 static int davinci_target(struct cpufreq_policy
*policy
,
71 unsigned int target_freq
, unsigned int relation
)
75 struct cpufreq_freqs freqs
;
76 struct davinci_cpufreq_config
*pdata
= cpufreq
.dev
->platform_data
;
77 struct clk
*armclk
= cpufreq
.armclk
;
80 * Ensure desired rate is within allowed range. Some govenors
81 * (ondemand) will just pass target_freq=0 to get the minimum.
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
;
88 freqs
.old
= davinci_getspeed(0);
89 freqs
.new = clk_round_rate(armclk
, target_freq
* 1000) / 1000;
92 if (freqs
.old
== freqs
.new)
95 cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER
,
96 dev_driver_string(cpufreq
.dev
),
97 "transition: %u --> %u\n", freqs
.old
, freqs
.new);
99 ret
= cpufreq_frequency_table_target(policy
, pdata
->freq_table
,
100 freqs
.new, relation
, &idx
);
104 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
106 /* if moving to higher frequency, up the voltage beforehand */
107 if (pdata
->set_voltage
&& freqs
.new > freqs
.old
)
108 pdata
->set_voltage(idx
);
110 ret
= clk_set_rate(armclk
, idx
);
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
);
116 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
121 static int __init
davinci_cpu_init(struct cpufreq_policy
*policy
)
124 struct davinci_cpufreq_config
*pdata
= cpufreq
.dev
->platform_data
;
125 struct cpufreq_frequency_table
*freq_table
= pdata
->freq_table
;
127 if (policy
->cpu
!= 0)
130 /* Finish platform specific initialization */
132 result
= pdata
->init();
137 policy
->cur
= policy
->min
= policy
->max
= davinci_getspeed(0);
140 result
= cpufreq_frequency_table_cpuinfo(policy
, freq_table
);
142 cpufreq_frequency_table_get_attr(freq_table
,
145 policy
->cpuinfo
.min_freq
= policy
->min
;
146 policy
->cpuinfo
.max_freq
= policy
->max
;
149 policy
->min
= policy
->cpuinfo
.min_freq
;
150 policy
->max
= policy
->cpuinfo
.max_freq
;
151 policy
->cur
= davinci_getspeed(0);
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.
159 policy
->cpuinfo
.transition_latency
= 2000 * 1000;
163 static int davinci_cpu_exit(struct cpufreq_policy
*policy
)
165 cpufreq_frequency_table_put_attr(policy
->cpu
);
169 static struct freq_attr
*davinci_cpufreq_attr
[] = {
170 &cpufreq_freq_attr_scaling_available_freqs
,
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
,
182 .attr
= davinci_cpufreq_attr
,
185 static int __init
davinci_cpufreq_probe(struct platform_device
*pdev
)
187 struct davinci_cpufreq_config
*pdata
= pdev
->dev
.platform_data
;
191 if (!pdata
->freq_table
)
194 cpufreq
.dev
= &pdev
->dev
;
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
);
202 return cpufreq_register_driver(&davinci_driver
);
205 static int __exit
davinci_cpufreq_remove(struct platform_device
*pdev
)
207 clk_put(cpufreq
.armclk
);
209 return cpufreq_unregister_driver(&davinci_driver
);
212 static struct platform_driver davinci_cpufreq_driver
= {
214 .name
= "cpufreq-davinci",
215 .owner
= THIS_MODULE
,
217 .remove
= __exit_p(davinci_cpufreq_remove
),
220 static int __init
davinci_cpufreq_init(void)
222 return platform_driver_probe(&davinci_cpufreq_driver
,
223 davinci_cpufreq_probe
);
225 late_initcall(davinci_cpufreq_init
);