blob: 46254e5839829cd25971efe1b8d91879e9bb3fc7 [file] [log] [blame]
Russell King9e2697f2007-12-14 13:30:14 +00001/*
Russell King9e2697f2007-12-14 13:30:14 +00002 * Copyright (C) 2002,2003 Intrinsyc Software
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * History:
19 * 31-Jul-2002 : Initial version [FB]
20 * 29-Jan-2003 : added PXA255 support [FB]
21 * 20-Apr-2003 : ported to v2.5 (Dustin McIntire, Sensoria Corp.)
22 *
23 * Note:
24 * This driver may change the memory bus clock rate, but will not do any
25 * platform specific access timing changes... for example if you have flash
26 * memory connected to CS0, you will need to register a platform specific
27 * notifier which will adjust the memory access strobes to maintain a
28 * minimum strobe width.
29 *
30 */
31
Joe Perches1c5864e2016-04-05 13:28:25 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Russell King9e2697f2007-12-14 13:30:14 +000034#include <linux/kernel.h>
35#include <linux/module.h>
36#include <linux/sched.h>
37#include <linux/init.h>
38#include <linux/cpufreq.h>
Robert Jarzmik3dbeef22009-05-17 23:03:55 +020039#include <linux/err.h>
40#include <linux/regulator/consumer.h>
Marek Vasutad68bb92010-11-03 16:29:35 +010041#include <linux/io.h>
Russell King9e2697f2007-12-14 13:30:14 +000042
Russell Kinga09e64f2008-08-05 16:14:15 +010043#include <mach/pxa2xx-regs.h>
Marek Vasutad68bb92010-11-03 16:29:35 +010044#include <mach/smemc.h>
Russell King9e2697f2007-12-14 13:30:14 +000045
46#ifdef DEBUG
47static unsigned int freq_debug;
Randy Dunlapc710e392008-02-27 12:11:16 -080048module_param(freq_debug, uint, 0);
Russell King9e2697f2007-12-14 13:30:14 +000049MODULE_PARM_DESC(freq_debug, "Set the debug messages to on=1/off=0");
50#else
51#define freq_debug 0
52#endif
53
Robert Jarzmik3dbeef22009-05-17 23:03:55 +020054static struct regulator *vcc_core;
55
Robert Jarzmik592eb992008-05-07 20:39:06 +010056static unsigned int pxa27x_maxfreq;
57module_param(pxa27x_maxfreq, uint, 0);
58MODULE_PARM_DESC(pxa27x_maxfreq, "Set the pxa27x maxfreq in MHz"
59 "(typically 624=>pxa270, 416=>pxa271, 520=>pxa272)");
60
Robert Jarzmikd9278072017-10-14 23:51:02 +020061struct pxa_cpufreq_data {
62 struct clk *clk_core;
63};
64static struct pxa_cpufreq_data pxa_cpufreq_data;
65
Fabian Frederick52352552015-05-01 10:34:00 +020066struct pxa_freqs {
Russell King9e2697f2007-12-14 13:30:14 +000067 unsigned int khz;
Robert Jarzmik3dbeef22009-05-17 23:03:55 +020068 int vmin;
69 int vmax;
Fabian Frederick52352552015-05-01 10:34:00 +020070};
Russell King9e2697f2007-12-14 13:30:14 +000071
Robert Jarzmik592eb992008-05-07 20:39:06 +010072/*
73 * PXA255 definitions
74 */
Fabian Frederick03c22992015-05-01 10:34:01 +020075static const struct pxa_freqs pxa255_run_freqs[] =
Russell King9e2697f2007-12-14 13:30:14 +000076{
Robert Jarzmikd9278072017-10-14 23:51:02 +020077 /* CPU MEMBUS run turbo PXbus SDRAM */
78 { 99500, -1, -1}, /* 99, 99, 50, 50 */
79 {132700, -1, -1}, /* 133, 133, 66, 66 */
80 {199100, -1, -1}, /* 199, 199, 99, 99 */
81 {265400, -1, -1}, /* 265, 265, 133, 66 */
82 {331800, -1, -1}, /* 331, 331, 166, 83 */
83 {398100, -1, -1}, /* 398, 398, 196, 99 */
Russell King9e2697f2007-12-14 13:30:14 +000084};
Russell King9e2697f2007-12-14 13:30:14 +000085
86/* Use the turbo mode frequencies for the CPUFREQ_POLICY_POWERSAVE policy */
Fabian Frederick03c22992015-05-01 10:34:01 +020087static const struct pxa_freqs pxa255_turbo_freqs[] =
Russell King9e2697f2007-12-14 13:30:14 +000088{
Robert Jarzmikd9278072017-10-14 23:51:02 +020089 /* CPU run turbo PXbus SDRAM */
90 { 99500, -1, -1}, /* 99, 99, 50, 50 */
91 {199100, -1, -1}, /* 99, 199, 50, 99 */
92 {298500, -1, -1}, /* 99, 287, 50, 99 */
93 {298600, -1, -1}, /* 199, 287, 99, 99 */
94 {398100, -1, -1}, /* 199, 398, 99, 99 */
Russell King9e2697f2007-12-14 13:30:14 +000095};
Robert Jarzmik592eb992008-05-07 20:39:06 +010096
97#define NUM_PXA25x_RUN_FREQS ARRAY_SIZE(pxa255_run_freqs)
98#define NUM_PXA25x_TURBO_FREQS ARRAY_SIZE(pxa255_turbo_freqs)
Russell King9e2697f2007-12-14 13:30:14 +000099
Robert Jarzmik36793892008-05-07 20:36:34 +0100100static struct cpufreq_frequency_table
Robert Jarzmik592eb992008-05-07 20:39:06 +0100101 pxa255_run_freq_table[NUM_PXA25x_RUN_FREQS+1];
102static struct cpufreq_frequency_table
103 pxa255_turbo_freq_table[NUM_PXA25x_TURBO_FREQS+1];
104
Marc Zyngier65587f72008-11-04 13:33:25 +0100105static unsigned int pxa255_turbo_table;
106module_param(pxa255_turbo_table, uint, 0);
107MODULE_PARM_DESC(pxa255_turbo_table, "Selects the frequency table (0 = run table, !0 = turbo table)");
108
Fabian Frederick52352552015-05-01 10:34:00 +0200109static struct pxa_freqs pxa27x_freqs[] = {
Robert Jarzmikd9278072017-10-14 23:51:02 +0200110 {104000, 900000, 1705000 },
111 {156000, 1000000, 1705000 },
112 {208000, 1180000, 1705000 },
113 {312000, 1250000, 1705000 },
114 {416000, 1350000, 1705000 },
115 {520000, 1450000, 1705000 },
116 {624000, 1550000, 1705000 }
Robert Jarzmik592eb992008-05-07 20:39:06 +0100117};
118
119#define NUM_PXA27x_FREQS ARRAY_SIZE(pxa27x_freqs)
120static struct cpufreq_frequency_table
121 pxa27x_freq_table[NUM_PXA27x_FREQS+1];
Russell King9e2697f2007-12-14 13:30:14 +0000122
123extern unsigned get_clk_frequency_khz(int info);
124
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200125#ifdef CONFIG_REGULATOR
126
Fabian Frederick03c22992015-05-01 10:34:01 +0200127static int pxa_cpufreq_change_voltage(const struct pxa_freqs *pxa_freq)
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200128{
129 int ret = 0;
130 int vmin, vmax;
131
132 if (!cpu_is_pxa27x())
133 return 0;
134
135 vmin = pxa_freq->vmin;
136 vmax = pxa_freq->vmax;
137 if ((vmin == -1) || (vmax == -1))
138 return 0;
139
140 ret = regulator_set_voltage(vcc_core, vmin, vmax);
141 if (ret)
Joe Perches1c5864e2016-04-05 13:28:25 -0700142 pr_err("Failed to set vcc_core in [%dmV..%dmV]\n", vmin, vmax);
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200143 return ret;
144}
145
Hanjun Guo295dbde2013-08-13 18:20:09 +0800146static void __init pxa_cpufreq_init_voltages(void)
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200147{
148 vcc_core = regulator_get(NULL, "vcc_core");
149 if (IS_ERR(vcc_core)) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700150 pr_info("Didn't find vcc_core regulator\n");
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200151 vcc_core = NULL;
152 } else {
Joe Perches1c5864e2016-04-05 13:28:25 -0700153 pr_info("Found vcc_core regulator\n");
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200154 }
155}
156#else
Arnd Bergmannfb2a24a2016-01-25 16:44:38 +0100157static int pxa_cpufreq_change_voltage(const struct pxa_freqs *pxa_freq)
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200158{
159 return 0;
160}
161
Hanjun Guo295dbde2013-08-13 18:20:09 +0800162static void __init pxa_cpufreq_init_voltages(void) { }
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200163#endif
164
Marc Zyngier65587f72008-11-04 13:33:25 +0100165static void find_freq_tables(struct cpufreq_frequency_table **freq_table,
Fabian Frederick03c22992015-05-01 10:34:01 +0200166 const struct pxa_freqs **pxa_freqs)
Robert Jarzmik592eb992008-05-07 20:39:06 +0100167{
168 if (cpu_is_pxa25x()) {
Marc Zyngier65587f72008-11-04 13:33:25 +0100169 if (!pxa255_turbo_table) {
Robert Jarzmik592eb992008-05-07 20:39:06 +0100170 *pxa_freqs = pxa255_run_freqs;
171 *freq_table = pxa255_run_freq_table;
Marc Zyngier65587f72008-11-04 13:33:25 +0100172 } else {
Robert Jarzmik592eb992008-05-07 20:39:06 +0100173 *pxa_freqs = pxa255_turbo_freqs;
174 *freq_table = pxa255_turbo_freq_table;
Robert Jarzmik592eb992008-05-07 20:39:06 +0100175 }
Arnd Bergmann7264a2b2013-04-25 19:28:57 +0200176 } else if (cpu_is_pxa27x()) {
Robert Jarzmik592eb992008-05-07 20:39:06 +0100177 *pxa_freqs = pxa27x_freqs;
178 *freq_table = pxa27x_freq_table;
Arnd Bergmann7264a2b2013-04-25 19:28:57 +0200179 } else {
180 BUG();
Robert Jarzmik592eb992008-05-07 20:39:06 +0100181 }
182}
183
184static void pxa27x_guess_max_freq(void)
185{
186 if (!pxa27x_maxfreq) {
187 pxa27x_maxfreq = 416000;
Joe Perchesb49c22a2016-04-05 13:28:24 -0700188 pr_info("PXA CPU 27x max frequency not defined (pxa27x_maxfreq), assuming pxa271 with %dkHz maxfreq\n",
189 pxa27x_maxfreq);
Robert Jarzmik592eb992008-05-07 20:39:06 +0100190 } else {
191 pxa27x_maxfreq *= 1000;
192 }
193}
194
Robert Jarzmik592eb992008-05-07 20:39:06 +0100195static unsigned int pxa_cpufreq_get(unsigned int cpu)
196{
Robert Jarzmikd9278072017-10-14 23:51:02 +0200197 struct pxa_cpufreq_data *data = cpufreq_get_driver_data();
198
199 return (unsigned int) clk_get_rate(data->clk_core) / 1000;
Robert Jarzmik592eb992008-05-07 20:39:06 +0100200}
201
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530202static int pxa_set_target(struct cpufreq_policy *policy, unsigned int idx)
Russell King9e2697f2007-12-14 13:30:14 +0000203{
204 struct cpufreq_frequency_table *pxa_freqs_table;
Fabian Frederick03c22992015-05-01 10:34:01 +0200205 const struct pxa_freqs *pxa_freq_settings;
Robert Jarzmikd9278072017-10-14 23:51:02 +0200206 struct pxa_cpufreq_data *data = cpufreq_get_driver_data();
207 unsigned int new_freq_cpu;
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200208 int ret = 0;
Russell King9e2697f2007-12-14 13:30:14 +0000209
210 /* Get the current policy */
Marc Zyngier65587f72008-11-04 13:33:25 +0100211 find_freq_tables(&pxa_freqs_table, &pxa_freq_settings);
Russell King9e2697f2007-12-14 13:30:14 +0000212
Robert Jarzmik592eb992008-05-07 20:39:06 +0100213 new_freq_cpu = pxa_freq_settings[idx].khz;
Russell King9e2697f2007-12-14 13:30:14 +0000214
215 if (freq_debug)
Robert Jarzmikd9278072017-10-14 23:51:02 +0200216 pr_debug("Changing CPU frequency from %d Mhz to %d Mhz\n",
217 policy->cur / 1000, new_freq_cpu / 1000);
Russell King9e2697f2007-12-14 13:30:14 +0000218
Viresh Kumard4019f02013-08-14 19:38:24 +0530219 if (vcc_core && new_freq_cpu > policy->cur) {
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200220 ret = pxa_cpufreq_change_voltage(&pxa_freq_settings[idx]);
Viresh Kumard4019f02013-08-14 19:38:24 +0530221 if (ret)
222 return ret;
223 }
Russell King9e2697f2007-12-14 13:30:14 +0000224
Robert Jarzmikd9278072017-10-14 23:51:02 +0200225 clk_set_rate(data->clk_core, new_freq_cpu * 1000);
Russell King9e2697f2007-12-14 13:30:14 +0000226
227 /*
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200228 * Even if voltage setting fails, we don't report it, as the frequency
229 * change succeeded. The voltage reduction is not a critical failure,
230 * only power savings will suffer from this.
231 *
232 * Note: if the voltage change fails, and a return value is returned, a
233 * bug is triggered (seems a deadlock). Should anybody find out where,
234 * the "return 0" should become a "return ret".
235 */
Viresh Kumard4019f02013-08-14 19:38:24 +0530236 if (vcc_core && new_freq_cpu < policy->cur)
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200237 ret = pxa_cpufreq_change_voltage(&pxa_freq_settings[idx]);
238
Russell King9e2697f2007-12-14 13:30:14 +0000239 return 0;
240}
241
Eric Miao50e77fc2010-08-18 11:51:13 +0800242static int pxa_cpufreq_init(struct cpufreq_policy *policy)
Russell King9e2697f2007-12-14 13:30:14 +0000243{
244 int i;
Robert Jarzmik592eb992008-05-07 20:39:06 +0100245 unsigned int freq;
Marc Zyngier65587f72008-11-04 13:33:25 +0100246 struct cpufreq_frequency_table *pxa255_freq_table;
Fabian Frederick03c22992015-05-01 10:34:01 +0200247 const struct pxa_freqs *pxa255_freqs;
Robert Jarzmik592eb992008-05-07 20:39:06 +0100248
249 /* try to guess pxa27x cpu */
250 if (cpu_is_pxa27x())
251 pxa27x_guess_max_freq();
Russell King9e2697f2007-12-14 13:30:14 +0000252
Robert Jarzmik3dbeef22009-05-17 23:03:55 +0200253 pxa_cpufreq_init_voltages();
254
Russell King9e2697f2007-12-14 13:30:14 +0000255 /* set default policy and cpuinfo */
Russell King9e2697f2007-12-14 13:30:14 +0000256 policy->cpuinfo.transition_latency = 1000; /* FIXME: 1 ms, assumed */
Russell King9e2697f2007-12-14 13:30:14 +0000257
Robert Jarzmik592eb992008-05-07 20:39:06 +0100258 /* Generate pxa25x the run cpufreq_frequency_table struct */
259 for (i = 0; i < NUM_PXA25x_RUN_FREQS; i++) {
Russell King9e2697f2007-12-14 13:30:14 +0000260 pxa255_run_freq_table[i].frequency = pxa255_run_freqs[i].khz;
Viresh Kumar50701582013-03-30 16:25:15 +0530261 pxa255_run_freq_table[i].driver_data = i;
Russell King9e2697f2007-12-14 13:30:14 +0000262 }
Russell King9e2697f2007-12-14 13:30:14 +0000263 pxa255_run_freq_table[i].frequency = CPUFREQ_TABLE_END;
Robert Jarzmik592eb992008-05-07 20:39:06 +0100264
265 /* Generate pxa25x the turbo cpufreq_frequency_table struct */
266 for (i = 0; i < NUM_PXA25x_TURBO_FREQS; i++) {
Robert Jarzmik36793892008-05-07 20:36:34 +0100267 pxa255_turbo_freq_table[i].frequency =
268 pxa255_turbo_freqs[i].khz;
Viresh Kumar50701582013-03-30 16:25:15 +0530269 pxa255_turbo_freq_table[i].driver_data = i;
Russell King9e2697f2007-12-14 13:30:14 +0000270 }
271 pxa255_turbo_freq_table[i].frequency = CPUFREQ_TABLE_END;
272
Marc Zyngier65587f72008-11-04 13:33:25 +0100273 pxa255_turbo_table = !!pxa255_turbo_table;
274
Robert Jarzmik592eb992008-05-07 20:39:06 +0100275 /* Generate the pxa27x cpufreq_frequency_table struct */
276 for (i = 0; i < NUM_PXA27x_FREQS; i++) {
277 freq = pxa27x_freqs[i].khz;
278 if (freq > pxa27x_maxfreq)
279 break;
280 pxa27x_freq_table[i].frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530281 pxa27x_freq_table[i].driver_data = i;
Robert Jarzmik592eb992008-05-07 20:39:06 +0100282 }
Viresh Kumar50701582013-03-30 16:25:15 +0530283 pxa27x_freq_table[i].driver_data = i;
Robert Jarzmik592eb992008-05-07 20:39:06 +0100284 pxa27x_freq_table[i].frequency = CPUFREQ_TABLE_END;
285
286 /*
287 * Set the policy's minimum and maximum frequencies from the tables
288 * just constructed. This sets cpuinfo.mxx_freq, min and max.
289 */
Marc Zyngier65587f72008-11-04 13:33:25 +0100290 if (cpu_is_pxa25x()) {
291 find_freq_tables(&pxa255_freq_table, &pxa255_freqs);
Joe Perches1c5864e2016-04-05 13:28:25 -0700292 pr_info("using %s frequency table\n",
Marc Zyngier65587f72008-11-04 13:33:25 +0100293 pxa255_turbo_table ? "turbo" : "run");
Viresh Kumar6a77a1e2013-09-16 18:56:04 +0530294
Viresh Kumar8ed5a212018-02-26 10:39:00 +0530295 policy->freq_table = pxa255_freq_table;
Marc Zyngier65587f72008-11-04 13:33:25 +0100296 }
Viresh Kumar6a77a1e2013-09-16 18:56:04 +0530297 else if (cpu_is_pxa27x()) {
Viresh Kumar8ed5a212018-02-26 10:39:00 +0530298 policy->freq_table = pxa27x_freq_table;
Viresh Kumar6a77a1e2013-09-16 18:56:04 +0530299 }
Robert Jarzmik592eb992008-05-07 20:39:06 +0100300
Joe Perches1c5864e2016-04-05 13:28:25 -0700301 pr_info("frequency change support initialized\n");
Russell King9e2697f2007-12-14 13:30:14 +0000302
303 return 0;
304}
305
306static struct cpufreq_driver pxa_cpufreq_driver = {
Viresh Kumarae6b4272013-12-03 11:20:45 +0530307 .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Viresh Kumarbf36e482013-10-03 20:28:20 +0530308 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530309 .target_index = pxa_set_target,
Russell King9e2697f2007-12-14 13:30:14 +0000310 .init = pxa_cpufreq_init,
Holger Schurigea833f02008-02-11 16:53:15 +0100311 .get = pxa_cpufreq_get,
Robert Jarzmik592eb992008-05-07 20:39:06 +0100312 .name = "PXA2xx",
Robert Jarzmikd9278072017-10-14 23:51:02 +0200313 .driver_data = &pxa_cpufreq_data,
Russell King9e2697f2007-12-14 13:30:14 +0000314};
315
316static int __init pxa_cpu_init(void)
317{
318 int ret = -ENODEV;
Robert Jarzmikd9278072017-10-14 23:51:02 +0200319
320 pxa_cpufreq_data.clk_core = clk_get_sys(NULL, "core");
321 if (IS_ERR(pxa_cpufreq_data.clk_core))
322 return PTR_ERR(pxa_cpufreq_data.clk_core);
323
Robert Jarzmik592eb992008-05-07 20:39:06 +0100324 if (cpu_is_pxa25x() || cpu_is_pxa27x())
Russell King9e2697f2007-12-14 13:30:14 +0000325 ret = cpufreq_register_driver(&pxa_cpufreq_driver);
326 return ret;
327}
328
329static void __exit pxa_cpu_exit(void)
330{
Robert Jarzmik592eb992008-05-07 20:39:06 +0100331 cpufreq_unregister_driver(&pxa_cpufreq_driver);
Russell King9e2697f2007-12-14 13:30:14 +0000332}
333
334
Robert Jarzmik36793892008-05-07 20:36:34 +0100335MODULE_AUTHOR("Intrinsyc Software Inc.");
336MODULE_DESCRIPTION("CPU frequency changing driver for the PXA architecture");
Russell King9e2697f2007-12-14 13:30:14 +0000337MODULE_LICENSE("GPL");
338module_init(pxa_cpu_init);
339module_exit(pxa_cpu_exit);