blob: 7d61a6c3e7072a291153a9ed078bdce3417af24b [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 */
Ulf Hanssonb4689442012-10-10 13:42:24 +020011#include <linux/module.h>
Martin Persson7c1a70e2010-12-08 15:13:42 +010012#include <linux/kernel.h>
13#include <linux/cpufreq.h>
14#include <linux/delay.h>
Linus Walleij72b2fd52011-05-15 19:19:51 +020015#include <linux/slab.h>
Ulf Hanssonb4689442012-10-10 13:42:24 +020016#include <linux/platform_device.h>
Mattias Nilsson73180f82011-08-12 10:28:10 +020017#include <linux/mfd/dbx500-prcmu.h>
Linus Walleij72b2fd52011-05-15 19:19:51 +020018#include <mach/id.h>
Martin Persson7c1a70e2010-12-08 15:13:42 +010019
20static struct cpufreq_frequency_table freq_table[] = {
21 [0] = {
22 .index = 0,
Linus Walleij6283e322011-09-02 08:52:10 +020023 .frequency = 200000,
Martin Persson7c1a70e2010-12-08 15:13:42 +010024 },
25 [1] = {
26 .index = 1,
Daniel Willerudc72fe852012-01-13 16:20:03 +010027 .frequency = 400000,
Martin Persson7c1a70e2010-12-08 15:13:42 +010028 },
Linus Walleij72b2fd52011-05-15 19:19:51 +020029 [2] = {
Linus Walleij72b2fd52011-05-15 19:19:51 +020030 .index = 2,
Daniel Willerudc72fe852012-01-13 16:20:03 +010031 .frequency = 800000,
Linus Walleij72b2fd52011-05-15 19:19:51 +020032 },
Martin Persson7c1a70e2010-12-08 15:13:42 +010033 [3] = {
Linus Walleij6283e322011-09-02 08:52:10 +020034 /* Used for MAX_OPP, if available */
Martin Persson7c1a70e2010-12-08 15:13:42 +010035 .index = 3,
36 .frequency = CPUFREQ_TABLE_END,
37 },
Linus Walleij6283e322011-09-02 08:52:10 +020038 [4] = {
39 .index = 4,
40 .frequency = CPUFREQ_TABLE_END,
41 },
Martin Persson7c1a70e2010-12-08 15:13:42 +010042};
43
Linus Walleij72b2fd52011-05-15 19:19:51 +020044static enum arm_opp idx2opp[] = {
Linus Walleij6283e322011-09-02 08:52:10 +020045 ARM_EXTCLK,
Linus Walleij72b2fd52011-05-15 19:19:51 +020046 ARM_50_OPP,
47 ARM_100_OPP,
48 ARM_MAX_OPP
Martin Persson7c1a70e2010-12-08 15:13:42 +010049};
50
Linus Walleij72b2fd52011-05-15 19:19:51 +020051static struct freq_attr *db8500_cpufreq_attr[] = {
52 &cpufreq_freq_attr_scaling_available_freqs,
53 NULL,
54};
55
56static int db8500_cpufreq_verify_speed(struct cpufreq_policy *policy)
Martin Persson7c1a70e2010-12-08 15:13:42 +010057{
58 return cpufreq_frequency_table_verify(policy, freq_table);
59}
60
Linus Walleij72b2fd52011-05-15 19:19:51 +020061static int db8500_cpufreq_target(struct cpufreq_policy *policy,
Martin Persson7c1a70e2010-12-08 15:13:42 +010062 unsigned int target_freq,
63 unsigned int relation)
64{
65 struct cpufreq_freqs freqs;
Linus Walleij72b2fd52011-05-15 19:19:51 +020066 unsigned int idx;
Martin Persson7c1a70e2010-12-08 15:13:42 +010067
Linus Walleij72b2fd52011-05-15 19:19:51 +020068 /* scale the target frequency to one of the extremes supported */
Martin Persson7c1a70e2010-12-08 15:13:42 +010069 if (target_freq < policy->cpuinfo.min_freq)
70 target_freq = policy->cpuinfo.min_freq;
71 if (target_freq > policy->cpuinfo.max_freq)
72 target_freq = policy->cpuinfo.max_freq;
73
Linus Walleij72b2fd52011-05-15 19:19:51 +020074 /* Lookup the next frequency */
75 if (cpufreq_frequency_table_target
76 (policy, freq_table, target_freq, relation, &idx)) {
77 return -EINVAL;
Martin Persson7c1a70e2010-12-08 15:13:42 +010078 }
79
80 freqs.old = policy->cur;
Linus Walleij72b2fd52011-05-15 19:19:51 +020081 freqs.new = freq_table[idx].frequency;
Martin Persson7c1a70e2010-12-08 15:13:42 +010082
Linus Walleij72b2fd52011-05-15 19:19:51 +020083 if (freqs.old == freqs.new)
Martin Persson7c1a70e2010-12-08 15:13:42 +010084 return 0;
Martin Persson7c1a70e2010-12-08 15:13:42 +010085
Linus Walleij72b2fd52011-05-15 19:19:51 +020086 /* pre-change notification */
Vincent Guittot8efd0722011-08-25 08:31:20 +020087 for_each_cpu(freqs.cpu, policy->cpus)
88 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
Martin Persson7c1a70e2010-12-08 15:13:42 +010089
Linus Walleij72b2fd52011-05-15 19:19:51 +020090 /* request the PRCM unit for opp change */
91 if (prcmu_set_arm_opp(idx2opp[idx])) {
92 pr_err("db8500-cpufreq: Failed to set OPP level\n");
93 return -EINVAL;
Martin Persson7c1a70e2010-12-08 15:13:42 +010094 }
95
Linus Walleij72b2fd52011-05-15 19:19:51 +020096 /* post change notification */
Vincent Guittot8efd0722011-08-25 08:31:20 +020097 for_each_cpu(freqs.cpu, policy->cpus)
98 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
Martin Persson7c1a70e2010-12-08 15:13:42 +010099
Linus Walleij72b2fd52011-05-15 19:19:51 +0200100 return 0;
Martin Persson7c1a70e2010-12-08 15:13:42 +0100101}
102
Linus Walleij72b2fd52011-05-15 19:19:51 +0200103static unsigned int db8500_cpufreq_getspeed(unsigned int cpu)
Martin Persson7c1a70e2010-12-08 15:13:42 +0100104{
105 int i;
Linus Walleij72b2fd52011-05-15 19:19:51 +0200106 /* request the prcm to get the current ARM opp */
107 for (i = 0; prcmu_get_arm_opp() != idx2opp[i]; i++)
Martin Persson7c1a70e2010-12-08 15:13:42 +0100108 ;
109 return freq_table[i].frequency;
110}
111
Linus Walleij72b2fd52011-05-15 19:19:51 +0200112static int __cpuinit db8500_cpufreq_init(struct cpufreq_policy *policy)
Martin Persson7c1a70e2010-12-08 15:13:42 +0100113{
Axel Lineb0b38a2011-11-04 20:04:41 +0800114 int i, res;
Martin Persson7c1a70e2010-12-08 15:13:42 +0100115
Linus Walleij72b2fd52011-05-15 19:19:51 +0200116 BUILD_BUG_ON(ARRAY_SIZE(idx2opp) + 1 != ARRAY_SIZE(freq_table));
Martin Persson7c1a70e2010-12-08 15:13:42 +0100117
Daniel Willerudc72fe852012-01-13 16:20:03 +0100118 if (prcmu_has_arm_maxopp())
119 freq_table[3].frequency = 1000000;
120
Linus Walleij6283e322011-09-02 08:52:10 +0200121 pr_info("db8500-cpufreq : Available frequencies:\n");
Axel Lineb0b38a2011-11-04 20:04:41 +0800122 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++)
123 pr_info(" %d Mhz\n", freq_table[i].frequency/1000);
Martin Persson7c1a70e2010-12-08 15:13:42 +0100124
125 /* get policy fields based on the table */
126 res = cpufreq_frequency_table_cpuinfo(policy, freq_table);
127 if (!res)
128 cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
129 else {
Linus Walleij72b2fd52011-05-15 19:19:51 +0200130 pr_err("db8500-cpufreq : Failed to read policy table\n");
Martin Persson7c1a70e2010-12-08 15:13:42 +0100131 return res;
132 }
133
134 policy->min = policy->cpuinfo.min_freq;
135 policy->max = policy->cpuinfo.max_freq;
Linus Walleij72b2fd52011-05-15 19:19:51 +0200136 policy->cur = db8500_cpufreq_getspeed(policy->cpu);
Martin Persson7c1a70e2010-12-08 15:13:42 +0100137 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
138
139 /*
140 * FIXME : Need to take time measurement across the target()
141 * function with no/some/all drivers in the notification
142 * list.
143 */
Linus Walleij72b2fd52011-05-15 19:19:51 +0200144 policy->cpuinfo.transition_latency = 20 * 1000; /* in ns */
Martin Persson7c1a70e2010-12-08 15:13:42 +0100145
146 /* policy sharing between dual CPUs */
Rusty Russell88d8cd52012-03-29 15:38:30 +1030147 cpumask_copy(policy->cpus, cpu_present_mask);
Martin Persson7c1a70e2010-12-08 15:13:42 +0100148
149 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
150
Martin Persson7c1a70e2010-12-08 15:13:42 +0100151 return 0;
152}
153
Linus Walleij72b2fd52011-05-15 19:19:51 +0200154static struct cpufreq_driver db8500_cpufreq_driver = {
155 .flags = CPUFREQ_STICKY,
156 .verify = db8500_cpufreq_verify_speed,
157 .target = db8500_cpufreq_target,
158 .get = db8500_cpufreq_getspeed,
159 .init = db8500_cpufreq_init,
160 .name = "DB8500",
161 .attr = db8500_cpufreq_attr,
Martin Persson7c1a70e2010-12-08 15:13:42 +0100162};
163
Ulf Hanssonb4689442012-10-10 13:42:24 +0200164static int db8500_cpufreq_probe(struct platform_device *pdev)
165{
166 return cpufreq_register_driver(&db8500_cpufreq_driver);
167}
168
169static struct platform_driver db8500_cpufreq_plat_driver = {
170 .driver = {
171 .name = "cpufreq-u8500",
172 .owner = THIS_MODULE,
173 },
174 .probe = db8500_cpufreq_probe,
175};
176
Linus Walleij72b2fd52011-05-15 19:19:51 +0200177static int __init db8500_cpufreq_register(void)
Martin Persson7c1a70e2010-12-08 15:13:42 +0100178{
Linus Walleijbc71c092012-01-23 11:54:44 +0100179 if (!cpu_is_u8500_family())
Linus Walleij72b2fd52011-05-15 19:19:51 +0200180 return -ENODEV;
181
182 pr_info("cpufreq for DB8500 started\n");
Ulf Hanssonb4689442012-10-10 13:42:24 +0200183 return platform_driver_register(&db8500_cpufreq_plat_driver);
Martin Persson7c1a70e2010-12-08 15:13:42 +0100184}
Linus Walleij72b2fd52011-05-15 19:19:51 +0200185device_initcall(db8500_cpufreq_register);
Ulf Hanssonb4689442012-10-10 13:42:24 +0200186
187MODULE_LICENSE("GPL v2");
188MODULE_DESCRIPTION("cpufreq driver for DB8500");