]> Pileus Git - ~andy/linux/blob - drivers/cpufreq/omap-cpufreq.c
Merge remote-tracking branch 'wireless-next/master' into mac80211-next
[~andy/linux] / drivers / cpufreq / omap-cpufreq.c
1 /*
2  *  CPU frequency scaling for OMAP using OPP information
3  *
4  *  Copyright (C) 2005 Nokia Corporation
5  *  Written by Tony Lindgren <tony@atomide.com>
6  *
7  *  Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
8  *
9  * Copyright (C) 2007-2011 Texas Instruments, Inc.
10  * - OMAP3/4 support by Rajendra Nayak, Santosh Shilimkar
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  */
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/cpufreq.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/err.h>
23 #include <linux/clk.h>
24 #include <linux/io.h>
25 #include <linux/pm_opp.h>
26 #include <linux/cpu.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/regulator/consumer.h>
30
31 #include <asm/smp_plat.h>
32 #include <asm/cpu.h>
33
34 /* OPP tolerance in percentage */
35 #define OPP_TOLERANCE   4
36
37 static struct cpufreq_frequency_table *freq_table;
38 static atomic_t freq_table_users = ATOMIC_INIT(0);
39 static struct clk *mpu_clk;
40 static struct device *mpu_dev;
41 static struct regulator *mpu_reg;
42
43 static unsigned int omap_getspeed(unsigned int cpu)
44 {
45         unsigned long rate;
46
47         if (cpu >= NR_CPUS)
48                 return 0;
49
50         rate = clk_get_rate(mpu_clk) / 1000;
51         return rate;
52 }
53
54 static int omap_target(struct cpufreq_policy *policy, unsigned int index)
55 {
56         struct dev_pm_opp *opp;
57         unsigned long freq, volt = 0, volt_old = 0, tol = 0;
58         unsigned int old_freq, new_freq;
59
60         old_freq = omap_getspeed(policy->cpu);
61         new_freq = freq_table[index].frequency;
62
63         freq = new_freq * 1000;
64         ret = clk_round_rate(mpu_clk, freq);
65         if (IS_ERR_VALUE(ret)) {
66                 dev_warn(mpu_dev,
67                          "CPUfreq: Cannot find matching frequency for %lu\n",
68                          freq);
69                 return ret;
70         }
71         freq = ret;
72
73         if (mpu_reg) {
74                 rcu_read_lock();
75                 opp = dev_pm_opp_find_freq_ceil(mpu_dev, &freq);
76                 if (IS_ERR(opp)) {
77                         rcu_read_unlock();
78                         dev_err(mpu_dev, "%s: unable to find MPU OPP for %d\n",
79                                 __func__, new_freq);
80                         return -EINVAL;
81                 }
82                 volt = dev_pm_opp_get_voltage(opp);
83                 rcu_read_unlock();
84                 tol = volt * OPP_TOLERANCE / 100;
85                 volt_old = regulator_get_voltage(mpu_reg);
86         }
87
88         dev_dbg(mpu_dev, "cpufreq-omap: %u MHz, %ld mV --> %u MHz, %ld mV\n", 
89                 old_freq / 1000, volt_old ? volt_old / 1000 : -1,
90                 new_freq / 1000, volt ? volt / 1000 : -1);
91
92         /* scaling up?  scale voltage before frequency */
93         if (mpu_reg && (new_freq > old_freq)) {
94                 r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
95                 if (r < 0) {
96                         dev_warn(mpu_dev, "%s: unable to scale voltage up.\n",
97                                  __func__);
98                         return r;
99                 }
100         }
101
102         ret = clk_set_rate(mpu_clk, new_freq * 1000);
103
104         /* scaling down?  scale voltage after frequency */
105         if (mpu_reg && (new_freq < old_freq)) {
106                 r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
107                 if (r < 0) {
108                         dev_warn(mpu_dev, "%s: unable to scale voltage down.\n",
109                                  __func__);
110                         clk_set_rate(mpu_clk, old_freq * 1000);
111                         return r;
112                 }
113         }
114
115         return ret;
116 }
117
118 static inline void freq_table_free(void)
119 {
120         if (atomic_dec_and_test(&freq_table_users))
121                 dev_pm_opp_free_cpufreq_table(mpu_dev, &freq_table);
122 }
123
124 static int omap_cpu_init(struct cpufreq_policy *policy)
125 {
126         int result;
127
128         mpu_clk = clk_get(NULL, "cpufreq_ck");
129         if (IS_ERR(mpu_clk))
130                 return PTR_ERR(mpu_clk);
131
132         if (!freq_table) {
133                 result = dev_pm_opp_init_cpufreq_table(mpu_dev, &freq_table);
134                 if (result) {
135                         dev_err(mpu_dev,
136                                 "%s: cpu%d: failed creating freq table[%d]\n",
137                                 __func__, policy->cpu, result);
138                         goto fail;
139                 }
140         }
141
142         atomic_inc_return(&freq_table_users);
143
144         /* FIXME: what's the actual transition time? */
145         result = cpufreq_generic_init(policy, freq_table, 300 * 1000);
146         if (!result)
147                 return 0;
148
149         freq_table_free();
150 fail:
151         clk_put(mpu_clk);
152         return result;
153 }
154
155 static int omap_cpu_exit(struct cpufreq_policy *policy)
156 {
157         cpufreq_frequency_table_put_attr(policy->cpu);
158         freq_table_free();
159         clk_put(mpu_clk);
160         return 0;
161 }
162
163 static struct cpufreq_driver omap_driver = {
164         .flags          = CPUFREQ_STICKY,
165         .verify         = cpufreq_generic_frequency_table_verify,
166         .target_index   = omap_target,
167         .get            = omap_getspeed,
168         .init           = omap_cpu_init,
169         .exit           = omap_cpu_exit,
170         .name           = "omap",
171         .attr           = cpufreq_generic_attr,
172 };
173
174 static int omap_cpufreq_probe(struct platform_device *pdev)
175 {
176         mpu_dev = get_cpu_device(0);
177         if (!mpu_dev) {
178                 pr_warning("%s: unable to get the mpu device\n", __func__);
179                 return -EINVAL;
180         }
181
182         mpu_reg = regulator_get(mpu_dev, "vcc");
183         if (IS_ERR(mpu_reg)) {
184                 pr_warning("%s: unable to get MPU regulator\n", __func__);
185                 mpu_reg = NULL;
186         } else {
187                 /* 
188                  * Ensure physical regulator is present.
189                  * (e.g. could be dummy regulator.)
190                  */
191                 if (regulator_get_voltage(mpu_reg) < 0) {
192                         pr_warn("%s: physical regulator not present for MPU\n",
193                                 __func__);
194                         regulator_put(mpu_reg);
195                         mpu_reg = NULL;
196                 }
197         }
198
199         return cpufreq_register_driver(&omap_driver);
200 }
201
202 static int omap_cpufreq_remove(struct platform_device *pdev)
203 {
204         return cpufreq_unregister_driver(&omap_driver);
205 }
206
207 static struct platform_driver omap_cpufreq_platdrv = {
208         .driver = {
209                 .name   = "omap-cpufreq",
210                 .owner  = THIS_MODULE,
211         },
212         .probe          = omap_cpufreq_probe,
213         .remove         = omap_cpufreq_remove,
214 };
215 module_platform_driver(omap_cpufreq_platdrv);
216
217 MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs");
218 MODULE_LICENSE("GPL");