cpufreq: rockchip_big_little: add rockchip_bl_ prefix and indent
[firefly-linux-kernel-4.4.55.git] / drivers / cpufreq / rockchip_big_little.c
1 /*
2  * Copyright (C) 2015 Fuzhou Rockchip Electronics Co., Ltd
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
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17 #include <linux/clk.h>
18 #include <linux/cpufreq.h>
19 #include <linux/err.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/init.h>
22 #include <linux/reboot.h>
23 #include <linux/suspend.h>
24 #include <linux/tick.h>
25 #include <linux/workqueue.h>
26 #include <linux/delay.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/fs.h>
29 #include <linux/miscdevice.h>
30 #include <linux/string.h>
31 #include <linux/rockchip/cpu.h>
32 #include <linux/rockchip/dvfs.h>
33 #include <asm/smp_plat.h>
34 #include <asm/unistd.h>
35 #include <linux/uaccess.h>
36 #include <asm/system_misc.h>
37 #include <linux/cpu.h>
38 #include <linux/of.h>
39 #include <linux/mfd/syscon.h>
40 #include <linux/regmap.h>
41 #include <linux/rockchip/common.h>
42 #include <dt-bindings/clock/rk_system_status.h>
43 #include <linux/platform_device.h>
44 #include <linux/module.h>
45 #include "../../../drivers/clk/rockchip/clk-pd.h"
46
47 #define RK3368_GRF_CPU_CON(n) (0x500 + 4*n)
48
49 #define VERSION "1.0"
50 #define RK_MAX_CLUSTERS 2
51
52 #ifdef DEBUG
53 #define FREQ_DBG(fmt, args...) pr_debug(fmt, ## args)
54 #define FREQ_LOG(fmt, args...) pr_debug(fmt, ## args)
55 #else
56 #define FREQ_DBG(fmt, args...) do {} while (0)
57 #define FREQ_LOG(fmt, args...) do {} while (0)
58 #endif
59 #define FREQ_ERR(fmt, args...) pr_err(fmt, ## args)
60
61 /* Frequency table index must be sequential starting at 0 */
62 static struct cpufreq_frequency_table default_freq_table[] = {
63         {.frequency = 312 * 1000,       .index = 875 * 1000},
64         {.frequency = 504 * 1000,       .index = 925 * 1000},
65         {.frequency = 816 * 1000,       .index = 975 * 1000},
66         {.frequency = 1008 * 1000,      .index = 1075 * 1000},
67         {.frequency = 1200 * 1000,      .index = 1150 * 1000},
68         {.frequency = 1416 * 1000,      .index = 1250 * 1000},
69         {.frequency = 1608 * 1000,      .index = 1350 * 1000},
70         {.frequency = CPUFREQ_TABLE_END},
71 };
72
73 static struct cpufreq_frequency_table *freq_table[RK_MAX_CLUSTERS + 1];
74 /*********************************************************/
75 /* additional symantics for "relation" in cpufreq with pm */
76 #define DISABLE_FURTHER_CPUFREQ         0x10
77 #define ENABLE_FURTHER_CPUFREQ          0x20
78 #define MASK_FURTHER_CPUFREQ            0x30
79 /* With 0x00(NOCHANGE), it depends on the previous "further" status */
80 #define CPUFREQ_PRIVATE                 0x100
81 static unsigned int no_cpufreq_access[RK_MAX_CLUSTERS] = { 0 };
82 static unsigned int suspend_freq[RK_MAX_CLUSTERS] = { 816 * 1000, 816 * 1000 };
83 static unsigned int suspend_volt = 1100000;
84 static unsigned int low_battery_freq[RK_MAX_CLUSTERS] = { 600 * 1000,
85         600 * 1000 };
86 static unsigned int low_battery_capacity = 5;
87 static bool is_booting = true;
88 static DEFINE_MUTEX(cpufreq_mutex);
89 static struct dvfs_node *clk_cpu_dvfs_node[RK_MAX_CLUSTERS];
90 static struct dvfs_node *clk_gpu_dvfs_node;
91 static struct dvfs_node *clk_ddr_dvfs_node;
92 static u32 cluster_policy_cpu[RK_MAX_CLUSTERS];
93 static unsigned int big_little = 1;
94
95 /*******************************************************/
96 static inline int cpu_to_cluster(int cpu)
97 {
98         return topology_physical_package_id(cpu);
99 }
100
101 static unsigned int rockchip_bl_cpufreq_get_rate(unsigned int cpu)
102 {
103         u32 cur_cluster = cpu_to_cluster(cpu);
104
105         if (clk_cpu_dvfs_node[cur_cluster])
106                 return clk_get_rate(clk_cpu_dvfs_node[cur_cluster]->clk) / 1000;
107
108         return 0;
109 }
110
111 static bool cpufreq_is_ondemand(struct cpufreq_policy *policy)
112 {
113         char c = 0;
114
115         if (policy && policy->governor)
116                 c = policy->governor->name[0];
117         return (c == 'o' || c == 'i' || c == 'c' || c == 'h');
118 }
119
120 static unsigned int get_freq_from_table(unsigned int max_freq,
121                                         unsigned int cluster)
122 {
123         unsigned int i;
124         unsigned int target_freq = 0;
125
126         for (i = 0; freq_table[cluster][i].frequency != CPUFREQ_TABLE_END;
127              i++) {
128                 unsigned int freq = freq_table[cluster][i].frequency;
129
130                 if (freq <= max_freq && target_freq < freq)
131                         target_freq = freq;
132         }
133         if (!target_freq)
134                 target_freq = max_freq;
135         return target_freq;
136 }
137
138 static int rockchip_bl_cpufreq_notifier_policy(struct notifier_block *nb,
139                                                unsigned long val,
140                                                void *data)
141 {
142         static unsigned int min_rate = 0, max_rate = -1;
143         struct cpufreq_policy *policy = data;
144         u32 cur_cluster = cpu_to_cluster(policy->cpu);
145
146         if (val != CPUFREQ_ADJUST)
147                 return 0;
148
149         if (cpufreq_is_ondemand(policy)) {
150                 FREQ_DBG("queue work\n");
151                 dvfs_clk_enable_limit(clk_cpu_dvfs_node[cur_cluster],
152                                       min_rate, max_rate);
153         } else {
154                 FREQ_DBG("cancel work\n");
155                 dvfs_clk_get_limit(clk_cpu_dvfs_node[cur_cluster],
156                                    &min_rate, &max_rate);
157         }
158
159         return 0;
160 }
161
162 static struct notifier_block notifier_policy_block = {
163         .notifier_call = rockchip_bl_cpufreq_notifier_policy
164 };
165
166 static int rockchip_bl_cpufreq_verify(struct cpufreq_policy *policy)
167 {
168         u32 cur_cluster = cpu_to_cluster(policy->cpu);
169
170         if (!freq_table[cur_cluster])
171                 return -EINVAL;
172         return cpufreq_frequency_table_verify(policy, freq_table[cur_cluster]);
173 }
174
175 static int clk_node_get_cluster_id(struct clk *clk)
176 {
177         int i;
178
179         for (i = 0; i < RK_MAX_CLUSTERS; i++) {
180                 if (clk_cpu_dvfs_node[i]->clk == clk)
181                         return i;
182         }
183         return 0;
184 }
185
186 static int rockchip_bl_cpufreq_scale_rate_for_dvfs(struct clk *clk,
187                                                    unsigned long rate)
188 {
189         int ret;
190         struct cpufreq_freqs freqs;
191         struct cpufreq_policy *policy;
192         u32 cur_cluster;
193
194         cur_cluster = clk_node_get_cluster_id(clk);
195         policy = cpufreq_cpu_get(cluster_policy_cpu[cur_cluster]);
196
197         freqs.new = rate / 1000;
198         freqs.old = clk_get_rate(clk) / 1000;
199
200         cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
201
202         FREQ_DBG("cpufreq_scale_rate_for_dvfs(%lu)\n", rate);
203
204         ret = clk_set_rate(clk, rate);
205
206         freqs.new = clk_get_rate(clk) / 1000;
207         /* notifiers */
208         cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
209
210         cpufreq_cpu_put(policy);
211         return ret;
212 }
213
214 static int cluster_cpus_freq_dvfs_init(u32 cluster_id, char *dvfs_name)
215 {
216         clk_cpu_dvfs_node[cluster_id] = clk_get_dvfs_node(dvfs_name);
217
218         if (!clk_cpu_dvfs_node[cluster_id]) {
219                 FREQ_ERR("%s:cluster_id=%d,get dvfs err\n",
220                          __func__, cluster_id);
221                 return -EINVAL;
222         }
223         dvfs_clk_register_set_rate_callback(
224                 clk_cpu_dvfs_node[cluster_id],
225                 rockchip_bl_cpufreq_scale_rate_for_dvfs);
226         freq_table[cluster_id] =
227                 dvfs_get_freq_volt_table(clk_cpu_dvfs_node[cluster_id]);
228         if (freq_table[cluster_id] == NULL) {
229                 freq_table[cluster_id] = default_freq_table;
230         } else {
231                 int v = INT_MAX;
232                 int i;
233
234                 for (i = 0; freq_table[cluster_id][i].frequency !=
235                      CPUFREQ_TABLE_END; i++) {
236                         if (freq_table[cluster_id][i].index >= suspend_volt &&
237                             v > freq_table[cluster_id][i].index) {
238                                 suspend_freq[cluster_id] =
239                                         freq_table[cluster_id][i].frequency;
240                                 v = freq_table[cluster_id][i].index;
241                         }
242                 }
243         }
244         low_battery_freq[cluster_id] =
245                 get_freq_from_table(low_battery_freq[cluster_id], cluster_id);
246         clk_enable_dvfs(clk_cpu_dvfs_node[cluster_id]);
247         return 0;
248 }
249
250 static int rockchip_bl_cpufreq_init_cpu0(struct cpufreq_policy *policy)
251 {
252         clk_gpu_dvfs_node = clk_get_dvfs_node("clk_gpu");
253         if (clk_gpu_dvfs_node)
254                 clk_enable_dvfs(clk_gpu_dvfs_node);
255
256         clk_ddr_dvfs_node = clk_get_dvfs_node("clk_ddr");
257         if (clk_ddr_dvfs_node)
258                 clk_enable_dvfs(clk_ddr_dvfs_node);
259
260         cluster_cpus_freq_dvfs_init(0, "clk_core_b");
261         cluster_cpus_freq_dvfs_init(1, "clk_core_l");
262
263         cpufreq_register_notifier(&notifier_policy_block,
264                                   CPUFREQ_POLICY_NOTIFIER);
265
266         pr_info("version " VERSION ", suspend freq %d %d MHz\n",
267                 suspend_freq[0] / 1000, suspend_freq[1] / 1000);
268         return 0;
269 }
270
271 static int rockchip_bl_cpufreq_init(struct cpufreq_policy *policy)
272 {
273         static int cpu0_err;
274         u32 cur_cluster = cpu_to_cluster(policy->cpu);
275
276         if (policy->cpu == 0)
277                 cpu0_err = rockchip_bl_cpufreq_init_cpu0(policy);
278         if (cpu0_err)
279                 return cpu0_err;
280
281         cluster_policy_cpu[cur_cluster] = policy->cpu;
282
283         /* set freq min max */
284         cpufreq_frequency_table_cpuinfo(policy, freq_table[cur_cluster]);
285         /* sys nod */
286         cpufreq_frequency_table_get_attr(freq_table[cur_cluster], policy->cpu);
287
288         if (cur_cluster < RK_MAX_CLUSTERS)
289                 cpumask_copy(policy->cpus, topology_core_cpumask(policy->cpu));
290
291         policy->cur = clk_get_rate(clk_cpu_dvfs_node[cur_cluster]->clk) / 1000;
292
293         /* make ondemand default sampling_rate to 40000 */
294         policy->cpuinfo.transition_latency = 40 * NSEC_PER_USEC;
295
296         return 0;
297 }
298
299 static int rockchip_bl_cpufreq_exit(struct cpufreq_policy *policy)
300 {
301         u32 cur_cluster = cpu_to_cluster(policy->cpu);
302
303         if (policy->cpu == 0) {
304                 cpufreq_unregister_notifier(&notifier_policy_block,
305                                             CPUFREQ_POLICY_NOTIFIER);
306         }
307         cpufreq_frequency_table_cpuinfo(policy, freq_table[cur_cluster]);
308         clk_put_dvfs_node(clk_cpu_dvfs_node[cur_cluster]);
309
310         return 0;
311 }
312
313 static struct freq_attr *rockchip_bl_cpufreq_attr[] = {
314         &cpufreq_freq_attr_scaling_available_freqs,
315         NULL,
316 };
317
318 #ifdef CONFIG_CHARGER_DISPLAY
319 extern int rk_get_system_battery_capacity(void);
320 #else
321 static int rk_get_system_battery_capacity(void)
322 {
323         return 100;
324 }
325 #endif
326
327 static unsigned int
328 rockchip_bl_cpufreq_scale_limit(unsigned int target_freq,
329                                 struct cpufreq_policy *policy, bool is_private)
330 {
331         bool is_ondemand = cpufreq_is_ondemand(policy);
332         u32 cur_cluster = cpu_to_cluster(policy->cpu);
333
334         if (!is_ondemand)
335                 return target_freq;
336
337         if (is_booting) {
338                 s64 boottime_ms = ktime_to_ms(ktime_get_boottime());
339
340                 if (boottime_ms > 60 * MSEC_PER_SEC) {
341                         is_booting = false;
342                 } else if (target_freq > low_battery_freq[cur_cluster] &&
343                            rk_get_system_battery_capacity() <=
344                            low_battery_capacity) {
345                         target_freq = low_battery_freq[cur_cluster];
346                 }
347         }
348
349         return target_freq;
350 }
351
352 static int rockchip_bl_cpufreq_target(struct cpufreq_policy *policy,
353                                       unsigned int target_freq,
354                                       unsigned int relation)
355 {
356         unsigned int i, new_freq = target_freq, new_rate, cur_rate;
357         int ret = 0;
358         bool is_private;
359         u32 cur_cluster = cpu_to_cluster(policy->cpu);
360
361         if (!freq_table[cur_cluster]) {
362                 FREQ_ERR("no freq table!\n");
363                 return -EINVAL;
364         }
365
366         mutex_lock(&cpufreq_mutex);
367
368         is_private = relation & CPUFREQ_PRIVATE;
369         relation &= ~CPUFREQ_PRIVATE;
370
371         if ((relation & ENABLE_FURTHER_CPUFREQ) &&
372             no_cpufreq_access[cur_cluster])
373                 no_cpufreq_access[cur_cluster]--;
374         if (no_cpufreq_access[cur_cluster]) {
375                 FREQ_LOG("denied access to %s as it is disabled temporarily\n",
376                          __func__);
377                 ret = -EINVAL;
378                 goto out;
379         }
380         if (relation & DISABLE_FURTHER_CPUFREQ)
381                 no_cpufreq_access[cur_cluster]++;
382         relation &= ~MASK_FURTHER_CPUFREQ;
383
384         ret = cpufreq_frequency_table_target(policy, freq_table[cur_cluster],
385                                              target_freq, relation, &i);
386         if (ret) {
387                 FREQ_ERR("no freq match for %d(ret=%d)\n", target_freq, ret);
388                 goto out;
389         }
390         new_freq = freq_table[cur_cluster][i].frequency;
391         if (!no_cpufreq_access[cur_cluster])
392                 new_freq =
393                     rockchip_bl_cpufreq_scale_limit(new_freq, policy,
394                                                     is_private);
395
396         new_rate = new_freq * 1000;
397         cur_rate = dvfs_clk_get_rate(clk_cpu_dvfs_node[cur_cluster]);
398         FREQ_LOG("req = %7u new = %7u (was = %7u)\n", target_freq,
399                  new_freq, cur_rate / 1000);
400         if (new_rate == cur_rate)
401                 goto out;
402         ret = dvfs_clk_set_rate(clk_cpu_dvfs_node[cur_cluster], new_rate);
403
404 out:
405         FREQ_DBG("set freq (%7u) end, ret %d\n", new_freq, ret);
406         mutex_unlock(&cpufreq_mutex);
407         return ret;
408 }
409
410 static int rockchip_bl_cpufreq_pm_notifier_event(struct notifier_block *this,
411                                                  unsigned long event, void *ptr)
412 {
413         int ret = NOTIFY_DONE;
414         int i;
415
416         for (i = 0; i < RK_MAX_CLUSTERS; i++) {
417                 struct cpufreq_policy *policy =
418                         cpufreq_cpu_get(cluster_policy_cpu[i]);
419
420                 if (!policy)
421                         return ret;
422
423                 if (!cpufreq_is_ondemand(policy))
424                         goto out;
425
426                 switch (event) {
427                 case PM_SUSPEND_PREPARE:
428                         policy->cur++;
429                         ret = cpufreq_driver_target(policy, suspend_freq[i],
430                                                     DISABLE_FURTHER_CPUFREQ |
431                                                     CPUFREQ_RELATION_H);
432                         if (ret < 0) {
433                                 ret = NOTIFY_BAD;
434                                 goto out;
435                         }
436                         ret = NOTIFY_OK;
437                         break;
438                 case PM_POST_RESTORE:
439                 case PM_POST_SUSPEND:
440                         /* if (target_freq == policy->cur) then
441                            cpufreq_driver_target will return, and
442                            our target will not be called, it casue
443                            ENABLE_FURTHER_CPUFREQ flag invalid,
444                            avoid that. */
445                         policy->cur++;
446                         cpufreq_driver_target(policy, suspend_freq[i],
447                                               ENABLE_FURTHER_CPUFREQ |
448                                               CPUFREQ_RELATION_H);
449                         ret = NOTIFY_OK;
450                         break;
451                 }
452 out:
453                 cpufreq_cpu_put(policy);
454         }
455         return ret;
456 }
457
458 static struct notifier_block rockchip_bl_cpufreq_pm_notifier = {
459         .notifier_call = rockchip_bl_cpufreq_pm_notifier_event,
460 };
461
462 static int rockchip_bl_cpufreq_reboot_limit_freq(void)
463 {
464         struct regulator *regulator;
465         int volt = 0;
466         u32 rate;
467         int i;
468
469         dvfs_disable_temp_limit();
470
471         for (i = 0; i < RK_MAX_CLUSTERS; i++) {
472                 dvfs_clk_enable_limit(clk_cpu_dvfs_node[i],
473                                       1000 * suspend_freq[i],
474                                       1000 * suspend_freq[i]);
475                 rate = dvfs_clk_get_rate(clk_cpu_dvfs_node[i]);
476         }
477
478         regulator = dvfs_get_regulator("vdd_arm");
479         if (regulator)
480                 volt = regulator_get_voltage(regulator);
481         else
482                 pr_info("get arm regulator failed\n");
483         pr_info("reboot set cluster0 rate=%lu, cluster1 rate=%lu, volt=%d\n",
484                 dvfs_clk_get_rate(clk_cpu_dvfs_node[0]),
485                 dvfs_clk_get_rate(clk_cpu_dvfs_node[1]), volt);
486
487         return 0;
488 }
489
490 static int rockchip_bl_cpufreq_reboot_notifier_event(struct notifier_block
491                                                      *this, unsigned long event,
492                                                      void *ptr)
493 {
494         rockchip_set_system_status(SYS_STATUS_REBOOT);
495         rockchip_bl_cpufreq_reboot_limit_freq();
496
497         return NOTIFY_OK;
498 };
499
500 static struct notifier_block rockchip_bl_cpufreq_reboot_notifier = {
501         .notifier_call = rockchip_bl_cpufreq_reboot_notifier_event,
502 };
503
504 static struct cpufreq_driver rockchip_bl_cpufreq_driver = {
505         .flags = CPUFREQ_CONST_LOOPS,
506         .verify = rockchip_bl_cpufreq_verify,
507         .target = rockchip_bl_cpufreq_target,
508         .get = rockchip_bl_cpufreq_get_rate,
509         .init = rockchip_bl_cpufreq_init,
510         .exit = rockchip_bl_cpufreq_exit,
511         .name = "rockchip-bl",
512         .have_governor_per_policy = true,
513         .attr = rockchip_bl_cpufreq_attr,
514 };
515
516 static const struct of_device_id rockchip_bl_cpufreq_match[] = {
517         {
518                 .compatible = "rockchip,rk3368-cpufreq",
519         },
520         {},
521 };
522 MODULE_DEVICE_TABLE(of, rockchip_bl_cpufreq_match);
523
524 static int rockchip_bl_cpufreq_probe(struct platform_device *pdev)
525 {
526         struct device_node *np;
527         struct regmap *grf_regmap;
528         unsigned int big_bits, litt_bits;
529         int ret;
530
531         np = pdev->dev.of_node;
532         if (!np)
533                 return -ENODEV;
534
535         grf_regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
536         if (IS_ERR(grf_regmap)) {
537                 FREQ_ERR("Cpufreq couldn't find grf regmap\n");
538                 return PTR_ERR(grf_regmap);
539         }
540         ret = regmap_read(grf_regmap, RK3368_GRF_CPU_CON(3), &big_bits);
541         if (ret != 0) {
542                 FREQ_ERR("Cpufreq couldn't read to GRF\n");
543                 return -1;
544         }
545         ret = regmap_read(grf_regmap, RK3368_GRF_CPU_CON(1), &litt_bits);
546         if (ret != 0) {
547                 FREQ_ERR("Cpufreq couldn't read to GRF\n");
548                 return -1;
549         }
550
551         big_bits = (big_bits >> 8) & 0x03;
552         litt_bits = (litt_bits >> 8) & 0x03;
553
554         if (big_bits == 0x01 && litt_bits == 0x00)
555                 big_little = 1;
556         else if (big_bits == 0x0 && litt_bits == 0x01)
557                 big_little = 0;
558         pr_info("boot from %d\n", big_little);
559
560         register_reboot_notifier(&rockchip_bl_cpufreq_reboot_notifier);
561         register_pm_notifier(&rockchip_bl_cpufreq_pm_notifier);
562
563         return cpufreq_register_driver(&rockchip_bl_cpufreq_driver);
564 }
565
566 static int rockchip_bl_cpufreq_remove(struct platform_device *pdev)
567 {
568         cpufreq_unregister_driver(&rockchip_bl_cpufreq_driver);
569         return 0;
570 }
571
572 static struct platform_driver rockchip_bl_cpufreq_platdrv = {
573         .driver = {
574                 .name   = "rockchip-bl-cpufreq",
575                 .owner  = THIS_MODULE,
576                 .of_match_table = rockchip_bl_cpufreq_match,
577         },
578         .probe          = rockchip_bl_cpufreq_probe,
579         .remove         = rockchip_bl_cpufreq_remove,
580 };
581
582 module_platform_driver(rockchip_bl_cpufreq_platdrv);
583
584 MODULE_AUTHOR("Xiao Feng <xf@rock-chips.com>");
585 MODULE_LICENSE("GPL");