davinci: make it easier to identify SoC init failures
[safe/jmp/linux-2.6] / arch / arm / mach-davinci / cpufreq.c
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
130         policy->cur = policy->min = policy->max = davinci_getspeed(0);
131
132         if (freq_table) {
133                 result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
134                 if (!result)
135                         cpufreq_frequency_table_get_attr(freq_table,
136                                                         policy->cpu);
137         } else {
138                 policy->cpuinfo.min_freq = policy->min;
139                 policy->cpuinfo.max_freq = policy->max;
140         }
141
142         policy->min = policy->cpuinfo.min_freq;
143         policy->max = policy->cpuinfo.max_freq;
144         policy->cur = davinci_getspeed(0);
145
146         /*
147          * Time measurement across the target() function yields ~1500-1800us
148          * time taken with no drivers on notification list.
149          * Setting the latency to 2000 us to accomodate addition of drivers
150          * to pre/post change notification list.
151          */
152         policy->cpuinfo.transition_latency = 2000 * 1000;
153         return 0;
154 }
155
156 static int davinci_cpu_exit(struct cpufreq_policy *policy)
157 {
158         cpufreq_frequency_table_put_attr(policy->cpu);
159         return 0;
160 }
161
162 static struct freq_attr *davinci_cpufreq_attr[] = {
163         &cpufreq_freq_attr_scaling_available_freqs,
164         NULL,
165 };
166
167 static struct cpufreq_driver davinci_driver = {
168         .flags          = CPUFREQ_STICKY,
169         .verify         = davinci_verify_speed,
170         .target         = davinci_target,
171         .get            = davinci_getspeed,
172         .init           = davinci_cpu_init,
173         .exit           = davinci_cpu_exit,
174         .name           = "davinci",
175         .attr           = davinci_cpufreq_attr,
176 };
177
178 static int __init davinci_cpufreq_probe(struct platform_device *pdev)
179 {
180         struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
181
182         if (!pdata)
183                 return -EINVAL;
184         if (!pdata->freq_table)
185                 return -EINVAL;
186
187         cpufreq.dev = &pdev->dev;
188
189         cpufreq.armclk = clk_get(NULL, "arm");
190         if (IS_ERR(cpufreq.armclk)) {
191                 dev_err(cpufreq.dev, "Unable to get ARM clock\n");
192                 return PTR_ERR(cpufreq.armclk);
193         }
194
195         return cpufreq_register_driver(&davinci_driver);
196 }
197
198 static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
199 {
200         clk_put(cpufreq.armclk);
201
202         return cpufreq_unregister_driver(&davinci_driver);
203 }
204
205 static struct platform_driver davinci_cpufreq_driver = {
206         .driver = {
207                 .name    = "cpufreq-davinci",
208                 .owner   = THIS_MODULE,
209         },
210         .remove = __exit_p(davinci_cpufreq_remove),
211 };
212
213 static int __init davinci_cpufreq_init(void)
214 {
215         return platform_driver_probe(&davinci_cpufreq_driver,
216                                                         davinci_cpufreq_probe);
217 }
218 late_initcall(davinci_cpufreq_init);
219