cpufreq: Implement light weight ->target_index() routine
[linux-drm-fsl-dcu.git] / drivers / cpufreq / 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 #include <linux/export.h>
28
29 #include <mach/hardware.h>
30 #include <mach/cpufreq.h>
31 #include <mach/common.h>
32
33 struct davinci_cpufreq {
34         struct device *dev;
35         struct clk *armclk;
36         struct clk *asyncclk;
37         unsigned long asyncrate;
38 };
39 static struct davinci_cpufreq cpufreq;
40
41 static int davinci_verify_speed(struct cpufreq_policy *policy)
42 {
43         struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
44         struct cpufreq_frequency_table *freq_table = pdata->freq_table;
45         struct clk *armclk = cpufreq.armclk;
46
47         if (freq_table)
48                 return cpufreq_frequency_table_verify(policy, freq_table);
49
50         if (policy->cpu)
51                 return -EINVAL;
52
53         cpufreq_verify_within_cpu_limits(policy);
54         policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000;
55         policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000;
56         cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
57                                                 policy->cpuinfo.max_freq);
58         return 0;
59 }
60
61 static unsigned int davinci_getspeed(unsigned int cpu)
62 {
63         if (cpu)
64                 return 0;
65
66         return clk_get_rate(cpufreq.armclk) / 1000;
67 }
68
69 static int davinci_target(struct cpufreq_policy *policy, unsigned int idx)
70 {
71         int ret = 0;
72         struct cpufreq_freqs freqs;
73         struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
74         struct clk *armclk = cpufreq.armclk;
75
76         freqs.old = davinci_getspeed(0);
77         freqs.new = pdata->freq_table[idx].frequency;
78
79         dev_dbg(cpufreq.dev, "transition: %u --> %u\n", freqs.old, freqs.new);
80
81         cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
82
83         /* if moving to higher frequency, up the voltage beforehand */
84         if (pdata->set_voltage && freqs.new > freqs.old) {
85                 ret = pdata->set_voltage(idx);
86                 if (ret)
87                         goto out;
88         }
89
90         ret = clk_set_rate(armclk, idx);
91         if (ret)
92                 goto out;
93
94         if (cpufreq.asyncclk) {
95                 ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
96                 if (ret)
97                         goto out;
98         }
99
100         /* if moving to lower freq, lower the voltage after lowering freq */
101         if (pdata->set_voltage && freqs.new < freqs.old)
102                 pdata->set_voltage(idx);
103
104 out:
105         if (ret)
106                 freqs.new = freqs.old;
107
108         cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
109
110         return ret;
111 }
112
113 static int davinci_cpu_init(struct cpufreq_policy *policy)
114 {
115         int result = 0;
116         struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
117         struct cpufreq_frequency_table *freq_table = pdata->freq_table;
118
119         if (policy->cpu != 0)
120                 return -EINVAL;
121
122         /* Finish platform specific initialization */
123         if (pdata->init) {
124                 result = pdata->init();
125                 if (result)
126                         return result;
127         }
128
129         /*
130          * Time measurement across the target() function yields ~1500-1800us
131          * time taken with no drivers on notification list.
132          * Setting the latency to 2000 us to accommodate addition of drivers
133          * to pre/post change notification list.
134          */
135         return cpufreq_generic_init(policy, freq_table, 2000 * 1000);
136 }
137
138 static struct cpufreq_driver davinci_driver = {
139         .flags          = CPUFREQ_STICKY,
140         .verify         = davinci_verify_speed,
141         .target_index   = davinci_target,
142         .get            = davinci_getspeed,
143         .init           = davinci_cpu_init,
144         .exit           = cpufreq_generic_exit,
145         .name           = "davinci",
146         .attr           = cpufreq_generic_attr,
147 };
148
149 static int __init davinci_cpufreq_probe(struct platform_device *pdev)
150 {
151         struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
152         struct clk *asyncclk;
153
154         if (!pdata)
155                 return -EINVAL;
156         if (!pdata->freq_table)
157                 return -EINVAL;
158
159         cpufreq.dev = &pdev->dev;
160
161         cpufreq.armclk = clk_get(NULL, "arm");
162         if (IS_ERR(cpufreq.armclk)) {
163                 dev_err(cpufreq.dev, "Unable to get ARM clock\n");
164                 return PTR_ERR(cpufreq.armclk);
165         }
166
167         asyncclk = clk_get(cpufreq.dev, "async");
168         if (!IS_ERR(asyncclk)) {
169                 cpufreq.asyncclk = asyncclk;
170                 cpufreq.asyncrate = clk_get_rate(asyncclk);
171         }
172
173         return cpufreq_register_driver(&davinci_driver);
174 }
175
176 static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
177 {
178         clk_put(cpufreq.armclk);
179
180         if (cpufreq.asyncclk)
181                 clk_put(cpufreq.asyncclk);
182
183         return cpufreq_unregister_driver(&davinci_driver);
184 }
185
186 static struct platform_driver davinci_cpufreq_driver = {
187         .driver = {
188                 .name    = "cpufreq-davinci",
189                 .owner   = THIS_MODULE,
190         },
191         .remove = __exit_p(davinci_cpufreq_remove),
192 };
193
194 int __init davinci_cpufreq_init(void)
195 {
196         return platform_driver_probe(&davinci_cpufreq_driver,
197                                                         davinci_cpufreq_probe);
198 }
199