blob: c33c76c360faa98c92e3232374609875e022c541 [file] [log] [blame]
Sekhar Nori6601b802009-09-22 21:14:00 +05301/*
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>
Paul Gortmakerdc280942011-07-31 16:17:29 -040027#include <linux/export.h>
Sekhar Nori6601b802009-09-22 21:14:00 +053028
29#include <mach/hardware.h>
30#include <mach/cpufreq.h>
31#include <mach/common.h>
32
Sekhar Nori6601b802009-09-22 21:14:00 +053033struct davinci_cpufreq {
34 struct device *dev;
35 struct clk *armclk;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +053036 struct clk *asyncclk;
37 unsigned long asyncrate;
Sekhar Nori6601b802009-09-22 21:14:00 +053038};
39static struct davinci_cpufreq cpufreq;
40
41static 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_limits(policy, policy->cpuinfo.min_freq,
54 policy->cpuinfo.max_freq);
55
56 policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000;
57 policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000;
58 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
59 policy->cpuinfo.max_freq);
60 return 0;
61}
62
63static unsigned int davinci_getspeed(unsigned int cpu)
64{
65 if (cpu)
66 return 0;
67
68 return clk_get_rate(cpufreq.armclk) / 1000;
69}
70
71static int davinci_target(struct cpufreq_policy *policy,
72 unsigned int target_freq, unsigned int relation)
73{
74 int ret = 0;
75 unsigned int idx;
76 struct cpufreq_freqs freqs;
77 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
78 struct clk *armclk = cpufreq.armclk;
79
Sekhar Nori6601b802009-09-22 21:14:00 +053080 freqs.old = davinci_getspeed(0);
81 freqs.new = clk_round_rate(armclk, target_freq * 1000) / 1000;
Sekhar Nori6601b802009-09-22 21:14:00 +053082
83 if (freqs.old == freqs.new)
84 return ret;
85
Uwe Kleine-Königd870df62012-03-08 20:36:44 +010086 dev_dbg(cpufreq.dev, "transition: %u --> %u\n", freqs.old, freqs.new);
Sekhar Nori6601b802009-09-22 21:14:00 +053087
88 ret = cpufreq_frequency_table_target(policy, pdata->freq_table,
89 freqs.new, relation, &idx);
90 if (ret)
91 return -EINVAL;
92
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053093 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Sekhar Nori6601b802009-09-22 21:14:00 +053094
95 /* if moving to higher frequency, up the voltage beforehand */
Sekhar Norifca97b32010-07-20 16:46:48 +053096 if (pdata->set_voltage && freqs.new > freqs.old) {
97 ret = pdata->set_voltage(idx);
98 if (ret)
99 goto out;
100 }
Sekhar Nori6601b802009-09-22 21:14:00 +0530101
102 ret = clk_set_rate(armclk, idx);
Sekhar Norifca97b32010-07-20 16:46:48 +0530103 if (ret)
104 goto out;
Sekhar Nori6601b802009-09-22 21:14:00 +0530105
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530106 if (cpufreq.asyncclk) {
107 ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
108 if (ret)
109 goto out;
110 }
111
Sekhar Nori6601b802009-09-22 21:14:00 +0530112 /* 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
Sekhar Norifca97b32010-07-20 16:46:48 +0530116out:
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530117 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Sekhar Nori6601b802009-09-22 21:14:00 +0530118
119 return ret;
120}
121
Axel Lin079db592011-02-28 15:51:33 +0530122static int davinci_cpu_init(struct cpufreq_policy *policy)
Sekhar Nori6601b802009-09-22 21:14:00 +0530123{
124 int result = 0;
125 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
126 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
127
128 if (policy->cpu != 0)
129 return -EINVAL;
130
Sekhar Nori13d5e272009-10-22 15:12:16 +0530131 /* Finish platform specific initialization */
132 if (pdata->init) {
133 result = pdata->init();
134 if (result)
135 return result;
136 }
137
Viresh Kumareb2f50f2013-04-01 12:57:48 +0000138 policy->cur = davinci_getspeed(0);
Sekhar Nori6601b802009-09-22 21:14:00 +0530139
Viresh Kumareb2f50f2013-04-01 12:57:48 +0000140 result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
141 if (result) {
142 pr_err("%s: cpufreq_frequency_table_cpuinfo() failed",
143 __func__);
144 return result;
Sekhar Nori6601b802009-09-22 21:14:00 +0530145 }
146
Viresh Kumareb2f50f2013-04-01 12:57:48 +0000147 cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
Sekhar Nori6601b802009-09-22 21:14:00 +0530148
149 /*
150 * Time measurement across the target() function yields ~1500-1800us
151 * time taken with no drivers on notification list.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300152 * Setting the latency to 2000 us to accommodate addition of drivers
Sekhar Nori6601b802009-09-22 21:14:00 +0530153 * to pre/post change notification list.
154 */
155 policy->cpuinfo.transition_latency = 2000 * 1000;
156 return 0;
157}
158
159static int davinci_cpu_exit(struct cpufreq_policy *policy)
160{
161 cpufreq_frequency_table_put_attr(policy->cpu);
162 return 0;
163}
164
165static struct freq_attr *davinci_cpufreq_attr[] = {
166 &cpufreq_freq_attr_scaling_available_freqs,
167 NULL,
168};
169
170static struct cpufreq_driver davinci_driver = {
171 .flags = CPUFREQ_STICKY,
172 .verify = davinci_verify_speed,
173 .target = davinci_target,
174 .get = davinci_getspeed,
175 .init = davinci_cpu_init,
176 .exit = davinci_cpu_exit,
177 .name = "davinci",
178 .attr = davinci_cpufreq_attr,
179};
180
181static int __init davinci_cpufreq_probe(struct platform_device *pdev)
182{
183 struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530184 struct clk *asyncclk;
Sekhar Nori6601b802009-09-22 21:14:00 +0530185
186 if (!pdata)
187 return -EINVAL;
188 if (!pdata->freq_table)
189 return -EINVAL;
190
191 cpufreq.dev = &pdev->dev;
192
193 cpufreq.armclk = clk_get(NULL, "arm");
194 if (IS_ERR(cpufreq.armclk)) {
195 dev_err(cpufreq.dev, "Unable to get ARM clock\n");
196 return PTR_ERR(cpufreq.armclk);
197 }
198
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530199 asyncclk = clk_get(cpufreq.dev, "async");
200 if (!IS_ERR(asyncclk)) {
201 cpufreq.asyncclk = asyncclk;
202 cpufreq.asyncrate = clk_get_rate(asyncclk);
203 }
204
Sekhar Nori6601b802009-09-22 21:14:00 +0530205 return cpufreq_register_driver(&davinci_driver);
206}
207
208static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
209{
210 clk_put(cpufreq.armclk);
211
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530212 if (cpufreq.asyncclk)
213 clk_put(cpufreq.asyncclk);
214
Sekhar Nori6601b802009-09-22 21:14:00 +0530215 return cpufreq_unregister_driver(&davinci_driver);
216}
217
218static struct platform_driver davinci_cpufreq_driver = {
219 .driver = {
220 .name = "cpufreq-davinci",
221 .owner = THIS_MODULE,
222 },
223 .remove = __exit_p(davinci_cpufreq_remove),
224};
225
Shawn Guo3aa3e842012-04-26 09:45:39 +0800226int __init davinci_cpufreq_init(void)
Sekhar Nori6601b802009-09-22 21:14:00 +0530227{
228 return platform_driver_probe(&davinci_cpufreq_driver,
229 davinci_cpufreq_probe);
230}
Sekhar Nori6601b802009-09-22 21:14:00 +0530231