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