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