blob: 972583baf9e8d605b14b830b40b2c459ae1414b5 [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
Viresh Kumarbe49e342013-10-02 14:13:19 +053053 cpufreq_verify_within_cpu_limits(policy);
Sekhar Nori6601b802009-09-22 21:14:00 +053054 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
61static 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
69static int davinci_target(struct cpufreq_policy *policy,
70 unsigned int target_freq, unsigned int relation)
71{
72 int ret = 0;
73 unsigned int idx;
74 struct cpufreq_freqs freqs;
75 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
76 struct clk *armclk = cpufreq.armclk;
77
Sekhar Nori6601b802009-09-22 21:14:00 +053078 freqs.old = davinci_getspeed(0);
79 freqs.new = clk_round_rate(armclk, target_freq * 1000) / 1000;
Sekhar Nori6601b802009-09-22 21:14:00 +053080
81 if (freqs.old == freqs.new)
82 return ret;
83
Uwe Kleine-Königd870df62012-03-08 20:36:44 +010084 dev_dbg(cpufreq.dev, "transition: %u --> %u\n", freqs.old, freqs.new);
Sekhar Nori6601b802009-09-22 21:14:00 +053085
86 ret = cpufreq_frequency_table_target(policy, pdata->freq_table,
87 freqs.new, relation, &idx);
88 if (ret)
89 return -EINVAL;
90
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053091 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Sekhar Nori6601b802009-09-22 21:14:00 +053092
93 /* if moving to higher frequency, up the voltage beforehand */
Sekhar Norifca97b32010-07-20 16:46:48 +053094 if (pdata->set_voltage && freqs.new > freqs.old) {
95 ret = pdata->set_voltage(idx);
96 if (ret)
97 goto out;
98 }
Sekhar Nori6601b802009-09-22 21:14:00 +053099
100 ret = clk_set_rate(armclk, idx);
Sekhar Norifca97b32010-07-20 16:46:48 +0530101 if (ret)
102 goto out;
Sekhar Nori6601b802009-09-22 21:14:00 +0530103
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530104 if (cpufreq.asyncclk) {
105 ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
106 if (ret)
107 goto out;
108 }
109
Sekhar Nori6601b802009-09-22 21:14:00 +0530110 /* if moving to lower freq, lower the voltage after lowering freq */
111 if (pdata->set_voltage && freqs.new < freqs.old)
112 pdata->set_voltage(idx);
113
Sekhar Norifca97b32010-07-20 16:46:48 +0530114out:
Viresh Kumarf20b97d2013-06-19 11:18:20 +0530115 if (ret)
116 freqs.new = freqs.old;
117
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530118 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Sekhar Nori6601b802009-09-22 21:14:00 +0530119
120 return ret;
121}
122
Axel Lin079db592011-02-28 15:51:33 +0530123static int davinci_cpu_init(struct cpufreq_policy *policy)
Sekhar Nori6601b802009-09-22 21:14:00 +0530124{
125 int result = 0;
126 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
127 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
128
129 if (policy->cpu != 0)
130 return -EINVAL;
131
Sekhar Nori13d5e272009-10-22 15:12:16 +0530132 /* Finish platform specific initialization */
133 if (pdata->init) {
134 result = pdata->init();
135 if (result)
136 return result;
137 }
138
Sekhar Nori6601b802009-09-22 21:14:00 +0530139 /*
140 * Time measurement across the target() function yields ~1500-1800us
141 * time taken with no drivers on notification list.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300142 * Setting the latency to 2000 us to accommodate addition of drivers
Sekhar Nori6601b802009-09-22 21:14:00 +0530143 * to pre/post change notification list.
144 */
Viresh Kumaraf8c4cf2013-10-03 20:29:11 +0530145 return cpufreq_generic_init(policy, freq_table, 2000 * 1000);
Sekhar Nori6601b802009-09-22 21:14:00 +0530146}
147
Sekhar Nori6601b802009-09-22 21:14:00 +0530148static struct cpufreq_driver davinci_driver = {
149 .flags = CPUFREQ_STICKY,
150 .verify = davinci_verify_speed,
151 .target = davinci_target,
152 .get = davinci_getspeed,
153 .init = davinci_cpu_init,
Viresh Kumar39d0c362013-10-03 20:28:02 +0530154 .exit = cpufreq_generic_exit,
Sekhar Nori6601b802009-09-22 21:14:00 +0530155 .name = "davinci",
Viresh Kumar39d0c362013-10-03 20:28:02 +0530156 .attr = cpufreq_generic_attr,
Sekhar Nori6601b802009-09-22 21:14:00 +0530157};
158
159static int __init davinci_cpufreq_probe(struct platform_device *pdev)
160{
161 struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530162 struct clk *asyncclk;
Sekhar Nori6601b802009-09-22 21:14:00 +0530163
164 if (!pdata)
165 return -EINVAL;
166 if (!pdata->freq_table)
167 return -EINVAL;
168
169 cpufreq.dev = &pdev->dev;
170
171 cpufreq.armclk = clk_get(NULL, "arm");
172 if (IS_ERR(cpufreq.armclk)) {
173 dev_err(cpufreq.dev, "Unable to get ARM clock\n");
174 return PTR_ERR(cpufreq.armclk);
175 }
176
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530177 asyncclk = clk_get(cpufreq.dev, "async");
178 if (!IS_ERR(asyncclk)) {
179 cpufreq.asyncclk = asyncclk;
180 cpufreq.asyncrate = clk_get_rate(asyncclk);
181 }
182
Sekhar Nori6601b802009-09-22 21:14:00 +0530183 return cpufreq_register_driver(&davinci_driver);
184}
185
186static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
187{
188 clk_put(cpufreq.armclk);
189
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530190 if (cpufreq.asyncclk)
191 clk_put(cpufreq.asyncclk);
192
Sekhar Nori6601b802009-09-22 21:14:00 +0530193 return cpufreq_unregister_driver(&davinci_driver);
194}
195
196static struct platform_driver davinci_cpufreq_driver = {
197 .driver = {
198 .name = "cpufreq-davinci",
199 .owner = THIS_MODULE,
200 },
201 .remove = __exit_p(davinci_cpufreq_remove),
202};
203
Shawn Guo3aa3e842012-04-26 09:45:39 +0800204int __init davinci_cpufreq_init(void)
Sekhar Nori6601b802009-09-22 21:14:00 +0530205{
206 return platform_driver_probe(&davinci_cpufreq_driver,
207 davinci_cpufreq_probe);
208}
Sekhar Nori6601b802009-09-22 21:14:00 +0530209