blob: 7c2e943c5500eb92a64c0db2ee5b38bab6360c25 [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
33#include "clock.h"
34
35struct davinci_cpufreq {
36 struct device *dev;
37 struct clk *armclk;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +053038 struct clk *asyncclk;
39 unsigned long asyncrate;
Sekhar Nori6601b802009-09-22 21:14:00 +053040};
41static struct davinci_cpufreq cpufreq;
42
43static int davinci_verify_speed(struct cpufreq_policy *policy)
44{
45 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
46 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
47 struct clk *armclk = cpufreq.armclk;
48
49 if (freq_table)
50 return cpufreq_frequency_table_verify(policy, freq_table);
51
52 if (policy->cpu)
53 return -EINVAL;
54
55 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
56 policy->cpuinfo.max_freq);
57
58 policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000;
59 policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000;
60 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
61 policy->cpuinfo.max_freq);
62 return 0;
63}
64
65static unsigned int davinci_getspeed(unsigned int cpu)
66{
67 if (cpu)
68 return 0;
69
70 return clk_get_rate(cpufreq.armclk) / 1000;
71}
72
73static int davinci_target(struct cpufreq_policy *policy,
74 unsigned int target_freq, unsigned int relation)
75{
76 int ret = 0;
77 unsigned int idx;
78 struct cpufreq_freqs freqs;
79 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
80 struct clk *armclk = cpufreq.armclk;
81
Sekhar Nori6601b802009-09-22 21:14:00 +053082 freqs.old = davinci_getspeed(0);
83 freqs.new = clk_round_rate(armclk, target_freq * 1000) / 1000;
Sekhar Nori6601b802009-09-22 21:14:00 +053084
85 if (freqs.old == freqs.new)
86 return ret;
87
Uwe Kleine-Königd870df62012-03-08 20:36:44 +010088 dev_dbg(cpufreq.dev, "transition: %u --> %u\n", freqs.old, freqs.new);
Sekhar Nori6601b802009-09-22 21:14:00 +053089
90 ret = cpufreq_frequency_table_target(policy, pdata->freq_table,
91 freqs.new, relation, &idx);
92 if (ret)
93 return -EINVAL;
94
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053095 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Sekhar Nori6601b802009-09-22 21:14:00 +053096
97 /* if moving to higher frequency, up the voltage beforehand */
Sekhar Norifca97b32010-07-20 16:46:48 +053098 if (pdata->set_voltage && freqs.new > freqs.old) {
99 ret = pdata->set_voltage(idx);
100 if (ret)
101 goto out;
102 }
Sekhar Nori6601b802009-09-22 21:14:00 +0530103
104 ret = clk_set_rate(armclk, idx);
Sekhar Norifca97b32010-07-20 16:46:48 +0530105 if (ret)
106 goto out;
Sekhar Nori6601b802009-09-22 21:14:00 +0530107
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530108 if (cpufreq.asyncclk) {
109 ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
110 if (ret)
111 goto out;
112 }
113
Sekhar Nori6601b802009-09-22 21:14:00 +0530114 /* if moving to lower freq, lower the voltage after lowering freq */
115 if (pdata->set_voltage && freqs.new < freqs.old)
116 pdata->set_voltage(idx);
117
Sekhar Norifca97b32010-07-20 16:46:48 +0530118out:
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530119 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Sekhar Nori6601b802009-09-22 21:14:00 +0530120
121 return ret;
122}
123
Axel Lin079db592011-02-28 15:51:33 +0530124static int davinci_cpu_init(struct cpufreq_policy *policy)
Sekhar Nori6601b802009-09-22 21:14:00 +0530125{
126 int result = 0;
127 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
128 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
129
130 if (policy->cpu != 0)
131 return -EINVAL;
132
Sekhar Nori13d5e272009-10-22 15:12:16 +0530133 /* Finish platform specific initialization */
134 if (pdata->init) {
135 result = pdata->init();
136 if (result)
137 return result;
138 }
139
Viresh Kumareb2f50f2013-04-01 12:57:48 +0000140 policy->cur = davinci_getspeed(0);
Sekhar Nori6601b802009-09-22 21:14:00 +0530141
Viresh Kumareb2f50f2013-04-01 12:57:48 +0000142 result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
143 if (result) {
144 pr_err("%s: cpufreq_frequency_table_cpuinfo() failed",
145 __func__);
146 return result;
Sekhar Nori6601b802009-09-22 21:14:00 +0530147 }
148
Viresh Kumareb2f50f2013-04-01 12:57:48 +0000149 cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
Sekhar Nori6601b802009-09-22 21:14:00 +0530150
151 /*
152 * Time measurement across the target() function yields ~1500-1800us
153 * time taken with no drivers on notification list.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300154 * Setting the latency to 2000 us to accommodate addition of drivers
Sekhar Nori6601b802009-09-22 21:14:00 +0530155 * to pre/post change notification list.
156 */
157 policy->cpuinfo.transition_latency = 2000 * 1000;
158 return 0;
159}
160
161static int davinci_cpu_exit(struct cpufreq_policy *policy)
162{
163 cpufreq_frequency_table_put_attr(policy->cpu);
164 return 0;
165}
166
167static struct freq_attr *davinci_cpufreq_attr[] = {
168 &cpufreq_freq_attr_scaling_available_freqs,
169 NULL,
170};
171
172static struct cpufreq_driver davinci_driver = {
173 .flags = CPUFREQ_STICKY,
174 .verify = davinci_verify_speed,
175 .target = davinci_target,
176 .get = davinci_getspeed,
177 .init = davinci_cpu_init,
178 .exit = davinci_cpu_exit,
179 .name = "davinci",
180 .attr = davinci_cpufreq_attr,
181};
182
183static int __init davinci_cpufreq_probe(struct platform_device *pdev)
184{
185 struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530186 struct clk *asyncclk;
Sekhar Nori6601b802009-09-22 21:14:00 +0530187
188 if (!pdata)
189 return -EINVAL;
190 if (!pdata->freq_table)
191 return -EINVAL;
192
193 cpufreq.dev = &pdev->dev;
194
195 cpufreq.armclk = clk_get(NULL, "arm");
196 if (IS_ERR(cpufreq.armclk)) {
197 dev_err(cpufreq.dev, "Unable to get ARM clock\n");
198 return PTR_ERR(cpufreq.armclk);
199 }
200
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530201 asyncclk = clk_get(cpufreq.dev, "async");
202 if (!IS_ERR(asyncclk)) {
203 cpufreq.asyncclk = asyncclk;
204 cpufreq.asyncrate = clk_get_rate(asyncclk);
205 }
206
Sekhar Nori6601b802009-09-22 21:14:00 +0530207 return cpufreq_register_driver(&davinci_driver);
208}
209
210static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
211{
212 clk_put(cpufreq.armclk);
213
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530214 if (cpufreq.asyncclk)
215 clk_put(cpufreq.asyncclk);
216
Sekhar Nori6601b802009-09-22 21:14:00 +0530217 return cpufreq_unregister_driver(&davinci_driver);
218}
219
220static struct platform_driver davinci_cpufreq_driver = {
221 .driver = {
222 .name = "cpufreq-davinci",
223 .owner = THIS_MODULE,
224 },
225 .remove = __exit_p(davinci_cpufreq_remove),
226};
227
Shawn Guo3aa3e842012-04-26 09:45:39 +0800228int __init davinci_cpufreq_init(void)
Sekhar Nori6601b802009-09-22 21:14:00 +0530229{
230 return platform_driver_probe(&davinci_cpufreq_driver,
231 davinci_cpufreq_probe);
232}
Sekhar Nori6601b802009-09-22 21:14:00 +0530233