rk3288: add limit clk rate by temperature
[firefly-linux-kernel-4.4.55.git] / drivers / cpufreq / rockchip-cpufreq.c
1 /*
2  * Copyright (C) 2013 ROCKCHIP, Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14 #define pr_fmt(fmt) "cpufreq: " fmt
15 #include <linux/clk.h>
16 #include <linux/cpufreq.h>
17 #include <linux/err.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/init.h>
20 #include <linux/reboot.h>
21 #include <linux/suspend.h>
22 #include <linux/tick.h>
23 #include <linux/workqueue.h>
24 #include <linux/delay.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/fs.h>
27 #include <linux/miscdevice.h>
28 #include <linux/string.h>
29 #include <linux/rockchip/cpu.h>
30 #include <linux/rockchip/dvfs.h>
31 #include <asm/smp_plat.h>
32 #include <asm/cpu.h>
33 #include <asm/unistd.h>
34 #include <asm/uaccess.h>
35
36 #define VERSION "1.0"
37
38 #ifdef DEBUG
39 #define FREQ_DBG(fmt, args...) pr_debug(fmt, ## args)
40 #define FREQ_LOG(fmt, args...) pr_debug(fmt, ## args)
41 #else
42 #define FREQ_DBG(fmt, args...) do {} while(0)
43 #define FREQ_LOG(fmt, args...) do {} while(0)
44 #endif
45 #define FREQ_ERR(fmt, args...) pr_err(fmt, ## args)
46
47 /* Frequency table index must be sequential starting at 0 */
48 static struct cpufreq_frequency_table default_freq_table[] = {
49         {.frequency = 312 * 1000,       .index = 875 * 1000},
50         {.frequency = 504 * 1000,       .index = 925 * 1000},
51         {.frequency = 816 * 1000,       .index = 975 * 1000},
52         {.frequency = 1008 * 1000,      .index = 1075 * 1000},
53         {.frequency = 1200 * 1000,      .index = 1150 * 1000},
54         {.frequency = 1416 * 1000,      .index = 1250 * 1000},
55         {.frequency = 1608 * 1000,      .index = 1350 * 1000},
56         {.frequency = CPUFREQ_TABLE_END},
57 };
58 static struct cpufreq_frequency_table *freq_table = default_freq_table;
59 /*********************************************************/
60 /* additional symantics for "relation" in cpufreq with pm */
61 #define DISABLE_FURTHER_CPUFREQ         0x10
62 #define ENABLE_FURTHER_CPUFREQ          0x20
63 #define MASK_FURTHER_CPUFREQ            0x30
64 /* With 0x00(NOCHANGE), it depends on the previous "further" status */
65 #define CPUFREQ_PRIVATE                 0x100
66 static int no_cpufreq_access;
67 static unsigned int suspend_freq = 816 * 1000;
68 static unsigned int suspend_volt = 1000000; // 1V
69 static unsigned int low_battery_freq = 600 * 1000;
70 static unsigned int low_battery_capacity = 5; // 5%
71 static bool is_booting = true;
72 static DEFINE_MUTEX(cpufreq_mutex);
73 static bool gpu_is_mali400;
74 struct dvfs_node *clk_cpu_dvfs_node = NULL;
75 struct dvfs_node *clk_gpu_dvfs_node = NULL;
76 struct dvfs_node *clk_vepu_dvfs_node = NULL;
77 /*******************************************************/
78 static unsigned int cpufreq_get_rate(unsigned int cpu)
79 {
80         if (clk_cpu_dvfs_node)
81                 return clk_get_rate(clk_cpu_dvfs_node->clk) / 1000;
82
83         return 0;
84 }
85
86 static bool cpufreq_is_ondemand(struct cpufreq_policy *policy)
87 {
88         char c = 0;
89         if (policy && policy->governor)
90                 c = policy->governor->name[0];
91         return (c == 'o' || c == 'i' || c == 'c' || c == 'h');
92 }
93
94 static unsigned int get_freq_from_table(unsigned int max_freq)
95 {
96         unsigned int i;
97         unsigned int target_freq = 0;
98         for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
99                 unsigned int freq = freq_table[i].frequency;
100                 if (freq <= max_freq && target_freq < freq) {
101                         target_freq = freq;
102                 }
103         }
104         if (!target_freq)
105                 target_freq = max_freq;
106         return target_freq;
107 }
108
109 static int cpufreq_notifier_policy(struct notifier_block *nb, unsigned long val, void *data)
110 {
111         struct cpufreq_policy *policy = data;
112
113         if (val != CPUFREQ_ADJUST)
114                 return 0;
115
116         if (cpufreq_is_ondemand(policy)) {
117                 FREQ_DBG("queue work\n");
118                 dvfs_clk_enable_limit(clk_cpu_dvfs_node, 0, ~0);
119         } else {
120                 FREQ_DBG("cancel work\n");
121                 dvfs_clk_disable_limit(clk_cpu_dvfs_node);
122         }
123
124         return 0;
125 }
126
127 static struct notifier_block notifier_policy_block = {
128         .notifier_call = cpufreq_notifier_policy
129 };
130
131 static int cpufreq_verify(struct cpufreq_policy *policy)
132 {
133         if (!freq_table)
134                 return -EINVAL;
135         return cpufreq_frequency_table_verify(policy, freq_table);
136 }
137
138 static int cpufreq_scale_rate_for_dvfs(struct clk *clk, unsigned long rate)
139 {
140         int ret;
141         struct cpufreq_freqs freqs;
142         struct cpufreq_policy *policy;
143         
144         freqs.new = rate / 1000;
145         freqs.old = clk_get_rate(clk) / 1000;
146         
147         for_each_online_cpu(freqs.cpu) {
148                 policy = cpufreq_cpu_get(freqs.cpu);
149                 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
150                 cpufreq_cpu_put(policy);
151         }
152         
153         FREQ_DBG("cpufreq_scale_rate_for_dvfs(%lu)\n", rate);
154         
155         ret = clk_set_rate(clk, rate);
156
157         freqs.new = clk_get_rate(clk) / 1000;
158         /* notifiers */
159         for_each_online_cpu(freqs.cpu) {
160                 policy = cpufreq_cpu_get(freqs.cpu);
161                 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
162                 cpufreq_cpu_put(policy);
163         }
164
165         return ret;
166         
167 }
168
169 static int cpufreq_init_cpu0(struct cpufreq_policy *policy)
170 {
171         unsigned int i;
172         gpu_is_mali400 = cpu_is_rk3188();
173
174         clk_gpu_dvfs_node = clk_get_dvfs_node("clk_gpu");
175         if (clk_gpu_dvfs_node){
176                 clk_enable_dvfs(clk_gpu_dvfs_node);
177                 if (gpu_is_mali400)
178                         dvfs_clk_enable_limit(clk_gpu_dvfs_node, 133000000, 600000000); 
179         }
180
181         clk_vepu_dvfs_node = clk_get_dvfs_node("clk_vepu");
182         if (clk_vepu_dvfs_node){
183                 clk_enable_dvfs(clk_vepu_dvfs_node);
184         }
185
186         clk_cpu_dvfs_node = clk_get_dvfs_node("clk_core");
187         if (!clk_cpu_dvfs_node){
188                 return -EINVAL;
189         }
190         dvfs_clk_register_set_rate_callback(clk_cpu_dvfs_node, cpufreq_scale_rate_for_dvfs);
191         freq_table = dvfs_get_freq_volt_table(clk_cpu_dvfs_node);
192         if (freq_table == NULL) {
193                 freq_table = default_freq_table;
194         } else {
195                 int v = INT_MAX;
196                 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
197                         if (freq_table[i].index >= suspend_volt && v > freq_table[i].index) {
198                                 suspend_freq = freq_table[i].frequency;
199                                 v = freq_table[i].index;
200                         }
201                 }
202         }
203         low_battery_freq = get_freq_from_table(low_battery_freq);
204         clk_enable_dvfs(clk_cpu_dvfs_node);
205
206         cpufreq_register_notifier(&notifier_policy_block, CPUFREQ_POLICY_NOTIFIER);
207
208         printk("cpufreq version " VERSION ", suspend freq %d MHz\n", suspend_freq / 1000);
209         return 0;
210 }
211
212 static int cpufreq_init(struct cpufreq_policy *policy)
213 {
214         static int cpu0_err;
215         
216         if (policy->cpu == 0) {
217                 cpu0_err = cpufreq_init_cpu0(policy);
218         }
219         
220         if (cpu0_err)
221                 return cpu0_err;
222         
223         //set freq min max
224         cpufreq_frequency_table_cpuinfo(policy, freq_table);
225         //sys nod
226         cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
227
228
229         policy->cur = clk_get_rate(clk_cpu_dvfs_node->clk) / 1000;
230
231         policy->cpuinfo.transition_latency = 40 * NSEC_PER_USEC;        // make ondemand default sampling_rate to 40000
232
233         /*
234          * On SMP configuartion, both processors share the voltage
235          * and clock. So both CPUs needs to be scaled together and hence
236          * needs software co-ordination. Use cpufreq affected_cpus
237          * interface to handle this scenario. Additional is_smp() check
238          * is to keep SMP_ON_UP build working.
239          */
240         if (is_smp())
241                 cpumask_setall(policy->cpus);
242
243         return 0;
244
245 }
246
247 static int cpufreq_exit(struct cpufreq_policy *policy)
248 {
249         if (policy->cpu != 0)
250                 return 0;
251
252         cpufreq_frequency_table_cpuinfo(policy, freq_table);
253         clk_put_dvfs_node(clk_cpu_dvfs_node);
254         cpufreq_unregister_notifier(&notifier_policy_block, CPUFREQ_POLICY_NOTIFIER);
255
256         return 0;
257 }
258
259 static struct freq_attr *cpufreq_attr[] = {
260         &cpufreq_freq_attr_scaling_available_freqs,
261         NULL,
262 };
263
264 #ifdef CONFIG_CHARGER_DISPLAY
265 extern int rk_get_system_battery_capacity(void);
266 #else
267 static int rk_get_system_battery_capacity(void) { return 100; }
268 #endif
269
270 static unsigned int cpufreq_scale_limit(unsigned int target_freq, struct cpufreq_policy *policy, bool is_private)
271 {
272         bool is_ondemand = cpufreq_is_ondemand(policy);
273
274         if (!is_ondemand)
275                 return target_freq;
276
277         if (is_booting) {
278                 s64 boottime_ms = ktime_to_ms(ktime_get_boottime());
279                 if (boottime_ms > 60 * MSEC_PER_SEC) {
280                         is_booting = false;
281                 } else if (target_freq > low_battery_freq &&
282                            rk_get_system_battery_capacity() <= low_battery_capacity) {
283                         target_freq = low_battery_freq;
284                 }
285         }
286
287         return target_freq;
288 }
289
290 static int cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)
291 {
292         unsigned int i, new_freq = target_freq, new_rate, cur_rate;
293         int ret = 0;
294         bool is_private;
295
296         if (!freq_table) {
297                 FREQ_ERR("no freq table!\n");
298                 return -EINVAL;
299         }
300
301         mutex_lock(&cpufreq_mutex);
302
303         is_private = relation & CPUFREQ_PRIVATE;
304         relation &= ~CPUFREQ_PRIVATE;
305
306         if (relation & ENABLE_FURTHER_CPUFREQ)
307                 no_cpufreq_access--;
308         if (no_cpufreq_access) {
309                 FREQ_LOG("denied access to %s as it is disabled temporarily\n", __func__);
310                 ret = -EINVAL;
311                 goto out;
312         }
313         if (relation & DISABLE_FURTHER_CPUFREQ)
314                 no_cpufreq_access++;
315         relation &= ~MASK_FURTHER_CPUFREQ;
316
317         ret = cpufreq_frequency_table_target(policy, freq_table, target_freq, relation, &i);
318         if (ret) {
319                 FREQ_ERR("no freq match for %d(ret=%d)\n", target_freq, ret);
320                 goto out;
321         }
322         new_freq = freq_table[i].frequency;
323         if (!no_cpufreq_access)
324                 new_freq = cpufreq_scale_limit(new_freq, policy, is_private);
325
326         new_rate = new_freq * 1000;
327         cur_rate = dvfs_clk_get_rate(clk_cpu_dvfs_node);
328         FREQ_LOG("req = %7u new = %7u (was = %7u)\n", target_freq, new_freq, cur_rate / 1000);
329         if (new_rate == cur_rate)
330                 goto out;
331         ret = dvfs_clk_set_rate(clk_cpu_dvfs_node, new_rate);
332
333 out:
334         FREQ_DBG("set freq (%7u) end, ret %d\n", new_freq, ret);
335         mutex_unlock(&cpufreq_mutex);
336         return ret;
337
338 }
339
340 static int cpufreq_pm_notifier_event(struct notifier_block *this, unsigned long event, void *ptr)
341 {
342         int ret = NOTIFY_DONE;
343         struct cpufreq_policy *policy = cpufreq_cpu_get(0);
344
345         if (!policy)
346                 return ret;
347
348         if (!cpufreq_is_ondemand(policy))
349                 goto out;
350
351         switch (event) {
352         case PM_SUSPEND_PREPARE:
353                 ret = cpufreq_driver_target(policy, suspend_freq, DISABLE_FURTHER_CPUFREQ | CPUFREQ_RELATION_H);
354                 if (ret < 0) {
355                         ret = NOTIFY_BAD;
356                         goto out;
357                 }
358                 ret = NOTIFY_OK;
359                 break;
360         case PM_POST_RESTORE:
361         case PM_POST_SUSPEND:
362                 cpufreq_driver_target(policy, suspend_freq, ENABLE_FURTHER_CPUFREQ | CPUFREQ_RELATION_H);
363                 ret = NOTIFY_OK;
364                 break;
365         }
366 out:
367         cpufreq_cpu_put(policy);
368         return ret;
369 }
370
371 static struct notifier_block cpufreq_pm_notifier = {
372         .notifier_call = cpufreq_pm_notifier_event,
373 };
374
375 static int cpufreq_reboot_notifier_event(struct notifier_block *this, unsigned long event, void *ptr)
376 {
377         struct cpufreq_policy *policy = cpufreq_cpu_get(0);
378
379         if (policy) {
380                 is_booting = false;
381                 cpufreq_driver_target(policy, suspend_freq, DISABLE_FURTHER_CPUFREQ | CPUFREQ_RELATION_H);
382                 cpufreq_cpu_put(policy);
383         }
384
385         return NOTIFY_OK;
386 }
387
388 static struct notifier_block cpufreq_reboot_notifier = {
389         .notifier_call = cpufreq_reboot_notifier_event,
390 };
391
392 static struct cpufreq_driver cpufreq_driver = {
393         .flags = CPUFREQ_CONST_LOOPS,
394         .verify = cpufreq_verify,
395         .target = cpufreq_target,
396         .get = cpufreq_get_rate,
397         .init = cpufreq_init,
398         .exit = cpufreq_exit,
399         .name = "rockchip",
400         .attr = cpufreq_attr,
401 };
402
403 static int __init cpufreq_driver_init(void)
404 {
405         register_pm_notifier(&cpufreq_pm_notifier);
406         register_reboot_notifier(&cpufreq_reboot_notifier);
407         return cpufreq_register_driver(&cpufreq_driver);
408 }
409
410 device_initcall(cpufreq_driver_init);