blob: 74b830b635a65d48260e4b5e9ff4a9aa889dbf97 [file] [log] [blame]
Martin Persson7c1a70e2010-12-08 15:13:42 +01001/*
Martin Persson7c1a70e2010-12-08 15:13:42 +01002 * Copyright (C) STMicroelectronics 2009
3 * Copyright (C) ST-Ericsson SA 2010
4 *
5 * License Terms: GNU General Public License v2
Martin Persson7c1a70e2010-12-08 15:13:42 +01006 * Author: Sundar Iyer <sundar.iyer@stericsson.com>
7 * Author: Martin Persson <martin.persson@stericsson.com>
8 * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
9 *
10 */
Martin Persson7c1a70e2010-12-08 15:13:42 +010011#include <linux/kernel.h>
12#include <linux/cpufreq.h>
13#include <linux/delay.h>
Linus Walleij72b2fd52011-05-15 19:19:51 +020014#include <linux/slab.h>
Mattias Nilsson73180f82011-08-12 10:28:10 +020015#include <linux/mfd/dbx500-prcmu.h>
Linus Walleij72b2fd52011-05-15 19:19:51 +020016#include <mach/id.h>
Martin Persson7c1a70e2010-12-08 15:13:42 +010017
18static struct cpufreq_frequency_table freq_table[] = {
19 [0] = {
20 .index = 0,
Linus Walleij6283e322011-09-02 08:52:10 +020021 .frequency = 200000,
Martin Persson7c1a70e2010-12-08 15:13:42 +010022 },
23 [1] = {
24 .index = 1,
Daniel Willerudc72fe852012-01-13 16:20:03 +010025 .frequency = 400000,
Martin Persson7c1a70e2010-12-08 15:13:42 +010026 },
Linus Walleij72b2fd52011-05-15 19:19:51 +020027 [2] = {
Linus Walleij72b2fd52011-05-15 19:19:51 +020028 .index = 2,
Daniel Willerudc72fe852012-01-13 16:20:03 +010029 .frequency = 800000,
Linus Walleij72b2fd52011-05-15 19:19:51 +020030 },
Martin Persson7c1a70e2010-12-08 15:13:42 +010031 [3] = {
Linus Walleij6283e322011-09-02 08:52:10 +020032 /* Used for MAX_OPP, if available */
Martin Persson7c1a70e2010-12-08 15:13:42 +010033 .index = 3,
34 .frequency = CPUFREQ_TABLE_END,
35 },
Linus Walleij6283e322011-09-02 08:52:10 +020036 [4] = {
37 .index = 4,
38 .frequency = CPUFREQ_TABLE_END,
39 },
Martin Persson7c1a70e2010-12-08 15:13:42 +010040};
41
Linus Walleij72b2fd52011-05-15 19:19:51 +020042static enum arm_opp idx2opp[] = {
Linus Walleij6283e322011-09-02 08:52:10 +020043 ARM_EXTCLK,
Linus Walleij72b2fd52011-05-15 19:19:51 +020044 ARM_50_OPP,
45 ARM_100_OPP,
46 ARM_MAX_OPP
Martin Persson7c1a70e2010-12-08 15:13:42 +010047};
48
Linus Walleij72b2fd52011-05-15 19:19:51 +020049static struct freq_attr *db8500_cpufreq_attr[] = {
50 &cpufreq_freq_attr_scaling_available_freqs,
51 NULL,
52};
53
54static int db8500_cpufreq_verify_speed(struct cpufreq_policy *policy)
Martin Persson7c1a70e2010-12-08 15:13:42 +010055{
56 return cpufreq_frequency_table_verify(policy, freq_table);
57}
58
Linus Walleij72b2fd52011-05-15 19:19:51 +020059static int db8500_cpufreq_target(struct cpufreq_policy *policy,
Martin Persson7c1a70e2010-12-08 15:13:42 +010060 unsigned int target_freq,
61 unsigned int relation)
62{
63 struct cpufreq_freqs freqs;
Linus Walleij72b2fd52011-05-15 19:19:51 +020064 unsigned int idx;
Martin Persson7c1a70e2010-12-08 15:13:42 +010065
Linus Walleij72b2fd52011-05-15 19:19:51 +020066 /* scale the target frequency to one of the extremes supported */
Martin Persson7c1a70e2010-12-08 15:13:42 +010067 if (target_freq < policy->cpuinfo.min_freq)
68 target_freq = policy->cpuinfo.min_freq;
69 if (target_freq > policy->cpuinfo.max_freq)
70 target_freq = policy->cpuinfo.max_freq;
71
Linus Walleij72b2fd52011-05-15 19:19:51 +020072 /* Lookup the next frequency */
73 if (cpufreq_frequency_table_target
74 (policy, freq_table, target_freq, relation, &idx)) {
75 return -EINVAL;
Martin Persson7c1a70e2010-12-08 15:13:42 +010076 }
77
78 freqs.old = policy->cur;
Linus Walleij72b2fd52011-05-15 19:19:51 +020079 freqs.new = freq_table[idx].frequency;
Martin Persson7c1a70e2010-12-08 15:13:42 +010080
Linus Walleij72b2fd52011-05-15 19:19:51 +020081 if (freqs.old == freqs.new)
Martin Persson7c1a70e2010-12-08 15:13:42 +010082 return 0;
Martin Persson7c1a70e2010-12-08 15:13:42 +010083
Linus Walleij72b2fd52011-05-15 19:19:51 +020084 /* pre-change notification */
Vincent Guittot8efd0722011-08-25 08:31:20 +020085 for_each_cpu(freqs.cpu, policy->cpus)
86 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
Martin Persson7c1a70e2010-12-08 15:13:42 +010087
Linus Walleij72b2fd52011-05-15 19:19:51 +020088 /* request the PRCM unit for opp change */
89 if (prcmu_set_arm_opp(idx2opp[idx])) {
90 pr_err("db8500-cpufreq: Failed to set OPP level\n");
91 return -EINVAL;
Martin Persson7c1a70e2010-12-08 15:13:42 +010092 }
93
Linus Walleij72b2fd52011-05-15 19:19:51 +020094 /* post change notification */
Vincent Guittot8efd0722011-08-25 08:31:20 +020095 for_each_cpu(freqs.cpu, policy->cpus)
96 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
Martin Persson7c1a70e2010-12-08 15:13:42 +010097
Linus Walleij72b2fd52011-05-15 19:19:51 +020098 return 0;
Martin Persson7c1a70e2010-12-08 15:13:42 +010099}
100
Linus Walleij72b2fd52011-05-15 19:19:51 +0200101static unsigned int db8500_cpufreq_getspeed(unsigned int cpu)
Martin Persson7c1a70e2010-12-08 15:13:42 +0100102{
103 int i;
Linus Walleij72b2fd52011-05-15 19:19:51 +0200104 /* request the prcm to get the current ARM opp */
105 for (i = 0; prcmu_get_arm_opp() != idx2opp[i]; i++)
Martin Persson7c1a70e2010-12-08 15:13:42 +0100106 ;
107 return freq_table[i].frequency;
108}
109
Linus Walleij72b2fd52011-05-15 19:19:51 +0200110static int __cpuinit db8500_cpufreq_init(struct cpufreq_policy *policy)
Martin Persson7c1a70e2010-12-08 15:13:42 +0100111{
Axel Lineb0b38a2011-11-04 20:04:41 +0800112 int i, res;
Martin Persson7c1a70e2010-12-08 15:13:42 +0100113
Linus Walleij72b2fd52011-05-15 19:19:51 +0200114 BUILD_BUG_ON(ARRAY_SIZE(idx2opp) + 1 != ARRAY_SIZE(freq_table));
Martin Persson7c1a70e2010-12-08 15:13:42 +0100115
Daniel Willerudc72fe852012-01-13 16:20:03 +0100116 if (prcmu_has_arm_maxopp())
117 freq_table[3].frequency = 1000000;
118
Linus Walleij6283e322011-09-02 08:52:10 +0200119 pr_info("db8500-cpufreq : Available frequencies:\n");
Axel Lineb0b38a2011-11-04 20:04:41 +0800120 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++)
121 pr_info(" %d Mhz\n", freq_table[i].frequency/1000);
Martin Persson7c1a70e2010-12-08 15:13:42 +0100122
123 /* get policy fields based on the table */
124 res = cpufreq_frequency_table_cpuinfo(policy, freq_table);
125 if (!res)
126 cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
127 else {
Linus Walleij72b2fd52011-05-15 19:19:51 +0200128 pr_err("db8500-cpufreq : Failed to read policy table\n");
Martin Persson7c1a70e2010-12-08 15:13:42 +0100129 return res;
130 }
131
132 policy->min = policy->cpuinfo.min_freq;
133 policy->max = policy->cpuinfo.max_freq;
Linus Walleij72b2fd52011-05-15 19:19:51 +0200134 policy->cur = db8500_cpufreq_getspeed(policy->cpu);
Martin Persson7c1a70e2010-12-08 15:13:42 +0100135 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
136
137 /*
138 * FIXME : Need to take time measurement across the target()
139 * function with no/some/all drivers in the notification
140 * list.
141 */
Linus Walleij72b2fd52011-05-15 19:19:51 +0200142 policy->cpuinfo.transition_latency = 20 * 1000; /* in ns */
Martin Persson7c1a70e2010-12-08 15:13:42 +0100143
144 /* policy sharing between dual CPUs */
Rusty Russell88d8cd52012-03-29 15:38:30 +1030145 cpumask_copy(policy->cpus, cpu_present_mask);
Martin Persson7c1a70e2010-12-08 15:13:42 +0100146
147 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
148
Martin Persson7c1a70e2010-12-08 15:13:42 +0100149 return 0;
150}
151
Linus Walleij72b2fd52011-05-15 19:19:51 +0200152static struct cpufreq_driver db8500_cpufreq_driver = {
153 .flags = CPUFREQ_STICKY,
154 .verify = db8500_cpufreq_verify_speed,
155 .target = db8500_cpufreq_target,
156 .get = db8500_cpufreq_getspeed,
157 .init = db8500_cpufreq_init,
158 .name = "DB8500",
159 .attr = db8500_cpufreq_attr,
Martin Persson7c1a70e2010-12-08 15:13:42 +0100160};
161
Linus Walleij72b2fd52011-05-15 19:19:51 +0200162static int __init db8500_cpufreq_register(void)
Martin Persson7c1a70e2010-12-08 15:13:42 +0100163{
Linus Walleijbc71c092012-01-23 11:54:44 +0100164 if (!cpu_is_u8500_family())
Linus Walleij72b2fd52011-05-15 19:19:51 +0200165 return -ENODEV;
166
167 pr_info("cpufreq for DB8500 started\n");
168 return cpufreq_register_driver(&db8500_cpufreq_driver);
Martin Persson7c1a70e2010-12-08 15:13:42 +0100169}
Linus Walleij72b2fd52011-05-15 19:19:51 +0200170device_initcall(db8500_cpufreq_register);