Merge tag 'lsk-v3.10-android-14.11'
[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/unistd.h>
33 #include <asm/uaccess.h>
34 #include <asm/system_misc.h>
35 #include "../../../drivers/clk/rockchip/clk-pd.h"
36
37 extern void dvfs_disable_temp_limit(void);
38
39 #define VERSION "1.0"
40
41 #ifdef DEBUG
42 #define FREQ_DBG(fmt, args...) pr_debug(fmt, ## args)
43 #define FREQ_LOG(fmt, args...) pr_debug(fmt, ## args)
44 #else
45 #define FREQ_DBG(fmt, args...) do {} while(0)
46 #define FREQ_LOG(fmt, args...) do {} while(0)
47 #endif
48 #define FREQ_ERR(fmt, args...) pr_err(fmt, ## args)
49
50 /* Frequency table index must be sequential starting at 0 */
51 static struct cpufreq_frequency_table default_freq_table[] = {
52         {.frequency = 312 * 1000,       .index = 875 * 1000},
53         {.frequency = 504 * 1000,       .index = 925 * 1000},
54         {.frequency = 816 * 1000,       .index = 975 * 1000},
55         {.frequency = 1008 * 1000,      .index = 1075 * 1000},
56         {.frequency = 1200 * 1000,      .index = 1150 * 1000},
57         {.frequency = 1416 * 1000,      .index = 1250 * 1000},
58         {.frequency = 1608 * 1000,      .index = 1350 * 1000},
59         {.frequency = CPUFREQ_TABLE_END},
60 };
61 static struct cpufreq_frequency_table *freq_table = default_freq_table;
62 /*********************************************************/
63 /* additional symantics for "relation" in cpufreq with pm */
64 #define DISABLE_FURTHER_CPUFREQ         0x10
65 #define ENABLE_FURTHER_CPUFREQ          0x20
66 #define MASK_FURTHER_CPUFREQ            0x30
67 /* With 0x00(NOCHANGE), it depends on the previous "further" status */
68 #define CPUFREQ_PRIVATE                 0x100
69 static unsigned int no_cpufreq_access = 0;
70 static unsigned int suspend_freq = 816 * 1000;
71 static unsigned int suspend_volt = 1100000;
72 static unsigned int low_battery_freq = 600 * 1000;
73 static unsigned int low_battery_capacity = 5; // 5%
74 static bool is_booting = true;
75 static DEFINE_MUTEX(cpufreq_mutex);
76 static bool gpu_is_mali400;
77 struct dvfs_node *clk_cpu_dvfs_node = NULL;
78 struct dvfs_node *clk_gpu_dvfs_node = NULL;
79 struct dvfs_node *aclk_vio1_dvfs_node = NULL;
80 struct dvfs_node *clk_ddr_dvfs_node = NULL;
81 /*******************************************************/
82 static unsigned int cpufreq_get_rate(unsigned int cpu)
83 {
84         if (clk_cpu_dvfs_node)
85                 return clk_get_rate(clk_cpu_dvfs_node->clk) / 1000;
86
87         return 0;
88 }
89
90 static bool cpufreq_is_ondemand(struct cpufreq_policy *policy)
91 {
92         char c = 0;
93         if (policy && policy->governor)
94                 c = policy->governor->name[0];
95         return (c == 'o' || c == 'i' || c == 'c' || c == 'h');
96 }
97
98 static unsigned int get_freq_from_table(unsigned int max_freq)
99 {
100         unsigned int i;
101         unsigned int target_freq = 0;
102         for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
103                 unsigned int freq = freq_table[i].frequency;
104                 if (freq <= max_freq && target_freq < freq) {
105                         target_freq = freq;
106                 }
107         }
108         if (!target_freq)
109                 target_freq = max_freq;
110         return target_freq;
111 }
112
113 static int cpufreq_notifier_policy(struct notifier_block *nb, unsigned long val, void *data)
114 {
115         static unsigned int min_rate=0, max_rate=-1;
116         struct cpufreq_policy *policy = data;
117
118         if (val != CPUFREQ_ADJUST)
119                 return 0;
120
121         if (cpufreq_is_ondemand(policy)) {
122                 FREQ_DBG("queue work\n");
123                 dvfs_clk_enable_limit(clk_cpu_dvfs_node, min_rate, max_rate);
124         } else {
125                 FREQ_DBG("cancel work\n");
126                 dvfs_clk_get_limit(clk_cpu_dvfs_node, &min_rate, &max_rate);
127         }
128
129         return 0;
130 }
131
132 static struct notifier_block notifier_policy_block = {
133         .notifier_call = cpufreq_notifier_policy
134 };
135
136 static int cpufreq_verify(struct cpufreq_policy *policy)
137 {
138         if (!freq_table)
139                 return -EINVAL;
140         return cpufreq_frequency_table_verify(policy, freq_table);
141 }
142
143 static int cpufreq_scale_rate_for_dvfs(struct clk *clk, unsigned long rate)
144 {
145         int ret;
146         struct cpufreq_freqs freqs;
147         struct cpufreq_policy *policy;
148         
149         freqs.new = rate / 1000;
150         freqs.old = clk_get_rate(clk) / 1000;
151         
152         for_each_online_cpu(freqs.cpu) {
153                 policy = cpufreq_cpu_get(freqs.cpu);
154                 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
155                 cpufreq_cpu_put(policy);
156         }
157         
158         FREQ_DBG("cpufreq_scale_rate_for_dvfs(%lu)\n", rate);
159         
160         ret = clk_set_rate(clk, rate);
161
162         freqs.new = clk_get_rate(clk) / 1000;
163         /* notifiers */
164         for_each_online_cpu(freqs.cpu) {
165                 policy = cpufreq_cpu_get(freqs.cpu);
166                 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
167                 cpufreq_cpu_put(policy);
168         }
169
170         return ret;
171         
172 }
173
174 static int cpufreq_init_cpu0(struct cpufreq_policy *policy)
175 {
176         unsigned int i;
177         int ret;
178         struct regulator *vdd_gpu_regulator;
179
180         gpu_is_mali400 = cpu_is_rk3188();
181
182         clk_gpu_dvfs_node = clk_get_dvfs_node("clk_gpu");
183         if (clk_gpu_dvfs_node){
184                 clk_enable_dvfs(clk_gpu_dvfs_node);
185                 vdd_gpu_regulator = dvfs_get_regulator("vdd_gpu");
186                 if (!IS_ERR_OR_NULL(vdd_gpu_regulator)) {
187                         if (!regulator_is_enabled(vdd_gpu_regulator)) {
188                                 ret = regulator_enable(vdd_gpu_regulator);
189                                 arm_pm_restart('h', NULL);
190                         }
191                         /* make sure vdd_gpu_regulator is in use,
192                         so it will not be disable by regulator_init_complete*/
193                         ret = regulator_enable(vdd_gpu_regulator);
194                         if (ret != 0)
195                                 arm_pm_restart('h', NULL);
196                 }
197                 if (gpu_is_mali400)
198                         dvfs_clk_enable_limit(clk_gpu_dvfs_node, 133000000, 600000000); 
199         }
200
201         clk_ddr_dvfs_node = clk_get_dvfs_node("clk_ddr");
202         if (clk_ddr_dvfs_node){
203                 clk_enable_dvfs(clk_ddr_dvfs_node);
204         }
205
206         clk_cpu_dvfs_node = clk_get_dvfs_node("clk_core");
207         if (!clk_cpu_dvfs_node){
208                 return -EINVAL;
209         }
210         dvfs_clk_register_set_rate_callback(clk_cpu_dvfs_node, cpufreq_scale_rate_for_dvfs);
211         freq_table = dvfs_get_freq_volt_table(clk_cpu_dvfs_node);
212         if (freq_table == NULL) {
213                 freq_table = default_freq_table;
214         } else {
215                 int v = INT_MAX;
216                 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
217                         if (freq_table[i].index >= suspend_volt && v > freq_table[i].index) {
218                                 suspend_freq = freq_table[i].frequency;
219                                 v = freq_table[i].index;
220                         }
221                 }
222         }
223         low_battery_freq = get_freq_from_table(low_battery_freq);
224         clk_enable_dvfs(clk_cpu_dvfs_node);
225
226         cpufreq_register_notifier(&notifier_policy_block, CPUFREQ_POLICY_NOTIFIER);
227
228         printk("cpufreq version " VERSION ", suspend freq %d MHz\n", suspend_freq / 1000);
229         return 0;
230 }
231
232 static int cpufreq_init(struct cpufreq_policy *policy)
233 {
234         static int cpu0_err;
235         
236         if (policy->cpu == 0) {
237                 cpu0_err = cpufreq_init_cpu0(policy);
238         }
239         
240         if (cpu0_err)
241                 return cpu0_err;
242         
243         //set freq min max
244         cpufreq_frequency_table_cpuinfo(policy, freq_table);
245         //sys nod
246         cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
247
248
249         policy->cur = clk_get_rate(clk_cpu_dvfs_node->clk) / 1000;
250
251         policy->cpuinfo.transition_latency = 40 * NSEC_PER_USEC;        // make ondemand default sampling_rate to 40000
252
253         cpumask_setall(policy->cpus);
254
255         return 0;
256
257 }
258
259 static int cpufreq_exit(struct cpufreq_policy *policy)
260 {
261         if (policy->cpu != 0)
262                 return 0;
263
264         cpufreq_frequency_table_cpuinfo(policy, freq_table);
265         clk_put_dvfs_node(clk_cpu_dvfs_node);
266         cpufreq_unregister_notifier(&notifier_policy_block, CPUFREQ_POLICY_NOTIFIER);
267
268         return 0;
269 }
270
271 static struct freq_attr *cpufreq_attr[] = {
272         &cpufreq_freq_attr_scaling_available_freqs,
273         NULL,
274 };
275
276 #ifdef CONFIG_CHARGER_DISPLAY
277 extern int rk_get_system_battery_capacity(void);
278 #else
279 static int rk_get_system_battery_capacity(void) { return 100; }
280 #endif
281
282 static unsigned int cpufreq_scale_limit(unsigned int target_freq, struct cpufreq_policy *policy, bool is_private)
283 {
284         bool is_ondemand = cpufreq_is_ondemand(policy);
285
286         if (!is_ondemand)
287                 return target_freq;
288
289         if (is_booting) {
290                 s64 boottime_ms = ktime_to_ms(ktime_get_boottime());
291                 if (boottime_ms > 60 * MSEC_PER_SEC) {
292                         is_booting = false;
293                 } else if (target_freq > low_battery_freq &&
294                            rk_get_system_battery_capacity() <= low_battery_capacity) {
295                         target_freq = low_battery_freq;
296                 }
297         }
298
299         return target_freq;
300 }
301
302 static int cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)
303 {
304         unsigned int i, new_freq = target_freq, new_rate, cur_rate;
305         int ret = 0;
306         bool is_private;
307
308         if (!freq_table) {
309                 FREQ_ERR("no freq table!\n");
310                 return -EINVAL;
311         }
312
313         mutex_lock(&cpufreq_mutex);
314
315         is_private = relation & CPUFREQ_PRIVATE;
316         relation &= ~CPUFREQ_PRIVATE;
317
318         if ((relation & ENABLE_FURTHER_CPUFREQ) && no_cpufreq_access)
319                 no_cpufreq_access--;
320         if (no_cpufreq_access) {
321                 FREQ_LOG("denied access to %s as it is disabled temporarily\n", __func__);
322                 ret = -EINVAL;
323                 goto out;
324         }
325         if (relation & DISABLE_FURTHER_CPUFREQ)
326                 no_cpufreq_access++;
327         relation &= ~MASK_FURTHER_CPUFREQ;
328
329         ret = cpufreq_frequency_table_target(policy, freq_table, target_freq, relation, &i);
330         if (ret) {
331                 FREQ_ERR("no freq match for %d(ret=%d)\n", target_freq, ret);
332                 goto out;
333         }
334         new_freq = freq_table[i].frequency;
335         if (!no_cpufreq_access)
336                 new_freq = cpufreq_scale_limit(new_freq, policy, is_private);
337
338         new_rate = new_freq * 1000;
339         cur_rate = dvfs_clk_get_rate(clk_cpu_dvfs_node);
340         FREQ_LOG("req = %7u new = %7u (was = %7u)\n", target_freq, new_freq, cur_rate / 1000);
341         if (new_rate == cur_rate)
342                 goto out;
343         ret = dvfs_clk_set_rate(clk_cpu_dvfs_node, new_rate);
344
345 out:
346         FREQ_DBG("set freq (%7u) end, ret %d\n", new_freq, ret);
347         mutex_unlock(&cpufreq_mutex);
348         return ret;
349
350 }
351
352 static int cpufreq_pm_notifier_event(struct notifier_block *this, unsigned long event, void *ptr)
353 {
354         int ret = NOTIFY_DONE;
355         struct cpufreq_policy *policy = cpufreq_cpu_get(0);
356
357         if (!policy)
358                 return ret;
359
360         if (!cpufreq_is_ondemand(policy))
361                 goto out;
362
363         switch (event) {
364         case PM_SUSPEND_PREPARE:
365                 policy->cur++;
366                 ret = cpufreq_driver_target(policy, suspend_freq, DISABLE_FURTHER_CPUFREQ | CPUFREQ_RELATION_H);
367                 if (ret < 0) {
368                         ret = NOTIFY_BAD;
369                         goto out;
370                 }
371                 ret = NOTIFY_OK;
372                 break;
373         case PM_POST_RESTORE:
374         case PM_POST_SUSPEND:
375                 //if (target_freq == policy->cur) then cpufreq_driver_target
376                 //will return, and our target will not be called, it casue
377                 //ENABLE_FURTHER_CPUFREQ flag invalid, avoid that.
378                 policy->cur++;
379                 cpufreq_driver_target(policy, suspend_freq, ENABLE_FURTHER_CPUFREQ | CPUFREQ_RELATION_H);
380                 ret = NOTIFY_OK;
381                 break;
382         }
383 out:
384         cpufreq_cpu_put(policy);
385         return ret;
386 }
387
388 static struct notifier_block cpufreq_pm_notifier = {
389         .notifier_call = cpufreq_pm_notifier_event,
390 };
391 #if 0
392 static int cpufreq_reboot_notifier_event(struct notifier_block *this, unsigned long event, void *ptr)
393 {
394         struct cpufreq_policy *policy = cpufreq_cpu_get(0);
395
396         if (policy) {
397                 is_booting = false;
398                 policy->cur++;
399                 cpufreq_driver_target(policy, suspend_freq, DISABLE_FURTHER_CPUFREQ | CPUFREQ_RELATION_H);
400                 cpufreq_cpu_put(policy);
401         }
402
403         return NOTIFY_OK;
404 }
405 #endif
406 int rockchip_cpufreq_reboot_limit_freq(void)
407 {
408         struct regulator *regulator;
409         int volt = 0;
410         u32 rate;
411
412         dvfs_disable_temp_limit();
413         dvfs_clk_enable_limit(clk_cpu_dvfs_node, 1000*suspend_freq, 1000*suspend_freq);
414
415         rate = dvfs_clk_get_rate(clk_cpu_dvfs_node);
416         regulator = dvfs_get_regulator("vdd_arm");
417         if (regulator)
418                 volt = regulator_get_voltage(regulator);
419         else
420                 pr_info("cpufreq: get arm regulator failed\n");
421         pr_info("cpufreq: reboot set core rate=%lu, volt=%d\n",
422                 dvfs_clk_get_rate(clk_cpu_dvfs_node), volt);
423
424         return 0;
425 }
426 #if 0
427 static struct notifier_block cpufreq_reboot_notifier = {
428         .notifier_call = cpufreq_reboot_notifier_event,
429 };
430 #endif
431 static int clk_pd_vio_notifier_call(struct notifier_block *nb, unsigned long event, void *ptr)
432 {
433         switch (event) {
434         case RK_CLK_PD_PREPARE:
435                 if (aclk_vio1_dvfs_node)
436                         clk_enable_dvfs(aclk_vio1_dvfs_node);
437                 break;
438         case RK_CLK_PD_UNPREPARE:
439                 if (aclk_vio1_dvfs_node)
440                         clk_disable_dvfs(aclk_vio1_dvfs_node);
441                 break;
442         }
443         return NOTIFY_OK;
444 }
445
446 static struct notifier_block clk_pd_vio_notifier = {
447         .notifier_call = clk_pd_vio_notifier_call,
448 };
449
450
451 static struct cpufreq_driver cpufreq_driver = {
452         .flags = CPUFREQ_CONST_LOOPS,
453         .verify = cpufreq_verify,
454         .target = cpufreq_target,
455         .get = cpufreq_get_rate,
456         .init = cpufreq_init,
457         .exit = cpufreq_exit,
458         .name = "rockchip",
459         .attr = cpufreq_attr,
460 };
461
462 static int __init cpufreq_driver_init(void)
463 {
464         struct clk *clk;
465
466         clk = clk_get(NULL, "pd_vio");
467         if (clk) {
468                 rk_clk_pd_notifier_register(clk, &clk_pd_vio_notifier);
469                 aclk_vio1_dvfs_node = clk_get_dvfs_node("aclk_vio1");
470                 if (aclk_vio1_dvfs_node && __clk_is_enabled(clk)){
471                         clk_enable_dvfs(aclk_vio1_dvfs_node);
472                 }
473         }
474         register_pm_notifier(&cpufreq_pm_notifier);
475         return cpufreq_register_driver(&cpufreq_driver);
476 }
477
478 device_initcall(cpufreq_driver_init);