8d5cfcd8cc80e9ddcd03d02bbea17559ae37ab25
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-rockchip / rk_pm_tests / clk_rate.c
1 #include <linux/clk.h>
2 #include <linux/kobject.h>
3 #include <linux/string.h>
4 #include <linux/resume-trace.h>
5 #include <linux/workqueue.h>
6 #include <linux/mutex.h>
7 #include <linux/module.h>
8 #include <linux/syscalls.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/delay.h>
12 #include <linux/cpu.h>
13 #include <linux/slab.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/regulator/machine.h>
16 #include <linux/mfd/wm831x/core.h>
17 #include <linux/sysfs.h>
18 #include <linux/err.h>
19
20 #include <linux/fs.h>
21 #include <asm/unistd.h>
22 #include <asm/uaccess.h>
23 #include <linux/rockchip/dvfs.h>
24
25 #include "rk_pm_tests.h"
26 #include "clk_rate.h"
27 /***************************************************************************/
28 #define FILE_GOV_MODE "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
29
30 #if 0
31 static int file_read(char *file_path, char *buf)
32 {
33         struct file *file = NULL;
34         mm_segment_t old_fs;
35         loff_t offset = 0;
36
37         PM_DBG("%s: read %s\n", __func__, file_path);
38         file = filp_open(file_path, O_RDONLY, 0);
39
40         if (IS_ERR(file)) {
41                 PM_ERR("%s: error open file  %s\n", __func__, file_path);
42                 return -1;
43         }
44
45         old_fs = get_fs();
46         set_fs(KERNEL_DS);
47
48         file->f_op->read(file, (char *)buf, 32, &offset);
49         sscanf(buf, "%s", buf);
50
51         set_fs(old_fs);
52         filp_close(file, NULL);  
53
54         file = NULL;
55
56         return 0;
57
58 }
59 #endif
60
61 static int file_write(char *file_path, char *buf)
62 {
63         struct file *file = NULL;
64         mm_segment_t old_fs;
65         loff_t offset = 0;
66
67         PM_DBG("%s: write %s %s size = %d\n", __func__,
68                file_path, buf, (int)strlen(buf));
69         file = filp_open(file_path, O_RDWR, 0);
70
71         if (IS_ERR(file)) {
72                 PM_ERR("%s: error open file  %s\n", __func__, file_path);
73                 return -1;
74         }
75
76         old_fs = get_fs();
77         set_fs(KERNEL_DS);
78
79         file->f_op->write(file, (char *)buf, strlen(buf), &offset);
80
81         set_fs(old_fs);
82         filp_close(file, NULL);  
83
84         file = NULL;
85
86         return 0;
87
88 }
89
90 ssize_t clk_rate_show(struct kobject *kobj, struct kobj_attribute *attr,
91                 char *buf)
92 {
93         char *str = buf;
94         str += sprintf(str, "set [clk_name] [rate(Hz)]\n"
95                         "rawset [clk_name] [rate(Hz)]\n");
96         if (str != buf)
97                 *(str - 1) = '\n';
98         return (str - buf);
99
100 }
101
102 ssize_t clk_rate_store(struct kobject *kobj, struct kobj_attribute *attr,
103                 const char *buf, size_t n)
104 {
105         struct dvfs_node *clk_dvfs_node = NULL;
106         struct clk *clk;
107         char cmd[20], clk_name[20];
108         unsigned long rate=0;
109         int ret=0;
110                 
111         sscanf(buf, "%s %s %lu", cmd, clk_name, &rate);
112         
113         PM_DBG("%s: cmd(%s), clk_name(%s), rate(%lu)\n", __func__, cmd, clk_name, rate);
114
115         if (!strncmp(cmd, "set", strlen("set"))) {
116                 clk_dvfs_node = clk_get_dvfs_node(clk_name);
117                 if (!clk_dvfs_node) {
118                         PM_ERR("%s: clk(%s) get dvfs node error\n", __func__, clk_name);
119                         return n;
120                 }
121                 
122                 if (!strncmp(clk_name, "clk_core", strlen("clk_core"))) {
123                         if (file_write(FILE_GOV_MODE, "userspace") != 0) {
124                                 PM_ERR("%s: set current governor error\n", __func__);
125                                 return n;
126                         }
127                 }
128
129                 ret = dvfs_clk_enable_limit(clk_dvfs_node, rate, rate);
130         } else {
131                 clk = clk_get(NULL, clk_name);
132                 if (IS_ERR_OR_NULL(clk)) {
133                         PM_ERR("%s: get clk(%s) err(%ld)\n", 
134                                 __func__, clk_name, PTR_ERR(clk));
135                         return n;
136                 }
137                 
138                 if (!strncmp(cmd, "rawset", strlen("rawset"))) {
139                         ret = clk_set_rate(clk, rate);
140                 } else if (!strncmp(cmd, "open", strlen("open"))) {
141                         ret = clk_prepare_enable(clk);
142                 } else if (!strncmp(cmd, "close", strlen("close"))) {   
143                         clk_disable_unprepare(clk);
144                 }
145         }
146
147         if (ret) {
148                 PM_ERR("%s: set rate err(%d)", __func__, ret);
149         }
150         return n;
151 }
152