2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/sched.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/proc_fs.h>
15 #include <linux/miscdevice.h>
16 #include <linux/platform_device.h>
17 #include <linux/earlysuspend.h>
18 #include <linux/spinlock.h>
19 #include <linux/kthread.h>
20 #include <linux/hrtimer.h>
21 #include <linux/ktime.h>
22 #include <linux/xlog.h>
23 #include <linux/jiffies.h>
24 #ifdef GPU_CLOCK_RATIO
25 #include <linux/time.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
31 #include "mach/mt_typedefs.h"
32 #include "mach/mt_clkmgr.h"
33 #include "mach/mt_gpufreq.h"
34 #include "mach/upmu_common.h"
35 #include "mach/sync_write.h"
37 /***************************
39 ****************************/
40 #define dprintk(fmt, args...) \
42 if (mt_gpufreq_debug) { \
43 xlog_printk(ANDROID_LOG_INFO, "Power/GPU_DVFS", fmt, ##args); \
47 #ifdef CONFIG_HAS_EARLYSUSPEND
48 static struct early_suspend mt_gpufreq_early_suspend_handler
=
50 .level
= EARLY_SUSPEND_LEVEL_DISABLE_FB
+ 200,
56 /***************************
57 * MT6589 GPU Power Table
58 ****************************/
59 static struct mt_gpufreq_power_info mt_gpufreqs_golden_power
[] = {
60 {.gpufreq_khz
= GPU_DVFS_F2
, .gpufreq_power
= 761},
61 {.gpufreq_khz
= GPU_DVFS_F3
, .gpufreq_power
= 625},
62 {.gpufreq_khz
= GPU_DVFS_F4
, .gpufreq_power
= 581},
63 {.gpufreq_khz
= GPU_DVFS_F5
, .gpufreq_power
= 456},
64 {.gpufreq_khz
= GPU_DVFS_F7
, .gpufreq_power
= 414},
65 {.gpufreq_khz
= GPU_DVFS_F8
, .gpufreq_power
= 336},
68 /**************************
69 * enable GPU DVFS count
70 ***************************/
71 static int g_gpufreq_dvfs_disable_count
= 0;
73 static unsigned int g_cur_freq
= 286000;
74 static unsigned int g_cur_volt
= 0;
75 static unsigned int g_cur_load
= 0;
77 static unsigned int g_cur_freq_init_keep
= 0;
78 static unsigned int g_volt_set_init_step_1
= 0;
79 static unsigned int g_volt_set_init_step_2
= 0;
80 static unsigned int g_volt_set_init_step_3
= 0;
81 static unsigned int g_freq_new_init_keep
= 0;
82 static unsigned int g_volt_new_init_keep
= 0;
84 /* In default settiing, freq_table[0] is max frequency, freq_table[num-1] is min frequency,*/
85 static unsigned int g_gpufreq_max_id
= 0;
87 /* If not limited, it should be set to freq_table[0] (MAX frequency) */
88 static unsigned int g_limited_max_id
;
89 static unsigned int g_limited_min_id
;
91 static bool mt_gpufreq_debug
= false;
92 static bool mt_gpufreq_pause
= false;
93 static bool mt_gpufreq_keep_max_frequency
= false;
94 static bool mt_gpufreq_keep_specific_frequency
= false;
95 static unsigned int mt_gpufreq_fixed_frequency
= 0;
96 static unsigned int mt_gpufreq_fixed_voltage
= 0;
98 static DEFINE_SPINLOCK(mt_gpufreq_lock
);
100 static unsigned int mt_gpufreqs_num
= 0;
101 static struct mt_gpufreq_info
*mt_gpufreqs
;
102 static struct mt_gpufreq_power_info
*mt_gpufreqs_power
;
103 static struct mt_gpufreq_power_info
*mt_gpufreqs_default_power
;
105 static bool mt_gpufreq_registered
= false;
106 static bool mt_gpufreq_registered_statewrite
= false;
107 static bool mt_gpufreq_already_non_registered
= false;
109 #ifdef GPU_CLOCK_RATIO
110 //static DEFINE_SPINLOCK(mt_gpufreq_load_lock);
111 //static struct mt_gpufreq_info *mt_gpufreqs_test;
113 static int mt_gpufreq_clock_on
= 0;
114 static unsigned int mt_gpufreq_cur_wall_time
= 0;
115 static unsigned int mt_gpufreq_prev_wall_time
= 0;
116 static unsigned int mt_gpufreq_period_time
= 0;
117 static unsigned int mt_gpu_clock_on_time_start
= 0;
118 static unsigned int mt_gpu_clock_total_on_time
= 0;
121 static unsigned int mt_gpufreq_enable_mainpll
= 0;
122 static unsigned int mt_gpufreq_enable_mmpll
= 0;
124 /******************************
125 * Extern Function Declaration
126 *******************************/
127 extern int mtk_gpufreq_register(struct mt_gpufreq_power_info
*freqs
, int num
);
129 /**************************
130 * GPU DVFS timer & thread
131 ***************************/
132 static struct hrtimer mt_gpufreq_timer
;
133 struct task_struct
*mt_gpufreq_thread
= NULL
;
134 static DECLARE_WAIT_QUEUE_HEAD(mt_gpufreq_timer_waiter
);
136 static int mt_gpufreq_sample_s
= 0;
137 static int mt_gpufreq_sample_ns
= 30000000; // 30ms
138 static int mt_gpufreq_timer_flag
= 0;
140 /******************************
141 * Extern Function Declaration
142 *******************************/
143 extern int pmic_get_gpu_status_bit_info(void);
145 /******************************
146 * Internal prototypes
147 *******************************/
148 enum hrtimer_restart
mt_gpufreq_timer_func(struct hrtimer
*timer
);
149 static int mt_gpufreq_thread_handler(void *unused
);
152 /*****************************************************************
153 * Check GPU current frequency and enable pll in initial and late resume.
154 *****************************************************************/
155 static void mt_gpufreq_check_freq_and_set_pll(void)
157 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreq_check_freq_and_set_pll, g_cur_freq = %d\n", g_cur_freq
);
161 case GPU_MMPLL_D3
: // 476Mhz
162 if(mt_gpufreq_enable_mmpll
== 0)
164 enable_pll(MMPLL
, "GPU_DVFS");
165 mt_gpufreq_enable_mmpll
= 1;
169 case GPU_SYSPLL_D2
: // 403Mhz
170 if(mt_gpufreq_enable_mainpll
== 0)
172 enable_pll(MAINPLL
, "GPU_DVFS");
173 mt_gpufreq_enable_mainpll
= 1;
177 case GPU_MMPLL_D4
: // 357Mhz
178 if(mt_gpufreq_enable_mmpll
== 0)
180 enable_pll(MMPLL
, "GPU_DVFS");
181 mt_gpufreq_enable_mmpll
= 1;
185 case GPU_UNIVPLL1_D2
: // 312Mhz
188 case GPU_MMPLL_D5
: // 286Mhz
189 if(mt_gpufreq_enable_mmpll
== 0)
191 enable_pll(MMPLL
, "GPU_DVFS");
192 mt_gpufreq_enable_mmpll
= 1;
196 case GPU_SYSPLL_D3
: // 268Mhz
197 if(mt_gpufreq_enable_mainpll
== 0)
199 enable_pll(MAINPLL
, "GPU_DVFS");
200 mt_gpufreq_enable_mainpll
= 1;
204 case GPU_MMPLL_D6
: // 238Mhz
205 if(mt_gpufreq_enable_mmpll
== 0)
207 enable_pll(MMPLL
, "GPU_DVFS");
208 mt_gpufreq_enable_mmpll
= 1;
212 case GPU_UNIVPLL1_D4
: // 156Mhz
220 /**************************************
221 * check if maximum frequency is needed
222 ***************************************/
223 static int mt_gpufreq_keep_max_freq(unsigned int freq_old
, unsigned int freq_new
)
225 if (mt_gpufreq_keep_max_frequency
== true)
231 /*****************************************************************
232 * Check if gpufreq registration is done
233 *****************************************************************/
234 bool mt_gpufreq_is_registered_get(void)
236 if((mt_gpufreq_registered
== true) || (mt_gpufreq_already_non_registered
== true))
241 EXPORT_SYMBOL(mt_gpufreq_is_registered_get
);
243 #ifdef GPU_CLOCK_RATIO
244 /*****************************************************************
245 * When gpu dvfs initialize or re-enable, record period time start,
246 * and reset total clock on time, period time.
247 *****************************************************************/
248 static void mt_gpufreq_period_time_reset(void)
251 do_gettimeofday(&t1
);
253 mt_gpufreq_prev_wall_time
= (unsigned int)t1
.tv_usec
;
254 mt_gpu_clock_total_on_time
= 0;
255 mt_gpufreq_period_time
= 0;
257 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreq_period_time_reset(), mt_gpufreq_prev_wall_time = %d\n", mt_gpufreq_prev_wall_time
);
260 /*****************************************************************
261 * Function to calculate clock on time ratio
262 *****************************************************************/
263 unsigned int mt_gpufreq_gpu_clock_ratio(enum mt_gpufreq_clock_ratio act
)
266 unsigned int load
= 0;
269 spin_lock_irqsave(&mt_gpufreq_lock
, flags
);
271 do_gettimeofday(&t2
);
273 if(act
== GPU_DVFS_CLOCK_RATIO_ON
&& mt_gpufreq_clock_on
== 0)
275 /* Remember clock on starting time. */
276 mt_gpufreq_clock_on
= 1;
277 mt_gpu_clock_on_time_start
= (unsigned int)t2
.tv_usec
;
278 dprintk("ON = %d\n", mt_gpu_clock_on_time_start
);
280 else if(act
== GPU_DVFS_CLOCK_RATIO_OFF
&& mt_gpufreq_clock_on
== 1)
282 /* Calculate this clock on time and add it into total clock on time. */
283 mt_gpufreq_clock_on
= 0;
284 mt_gpufreq_cur_wall_time
= (unsigned int)t2
.tv_usec
;
286 if(mt_gpufreq_cur_wall_time
>= mt_gpu_clock_on_time_start
)
287 mt_gpu_clock_total_on_time
+= (mt_gpufreq_cur_wall_time
- mt_gpu_clock_on_time_start
);
289 mt_gpu_clock_total_on_time
+= ((GPU_COUNT_OVERFLOW
- mt_gpu_clock_on_time_start
) + mt_gpufreq_cur_wall_time
);
291 dprintk("OFF = %d, mt_gpu_clock_total_on_time = %d\n", mt_gpufreq_cur_wall_time
, mt_gpu_clock_total_on_time
);
293 else if(act
== GPU_DVFS_CLOCK_RATIO_GET
)
295 mt_gpufreq_cur_wall_time
= (unsigned int)t2
.tv_usec
;
296 dprintk("GET 1, mt_gpufreq_cur_wall_time = %d\n", mt_gpufreq_cur_wall_time
);
298 /* If polling timeout and clock still on. */
299 if(mt_gpufreq_clock_on
== 1)
301 /* Calculate this clock on time and add it into total clock on time. */
302 if(mt_gpufreq_cur_wall_time
>= mt_gpu_clock_on_time_start
)
303 mt_gpu_clock_total_on_time
+= (mt_gpufreq_cur_wall_time
- mt_gpu_clock_on_time_start
);
305 mt_gpu_clock_total_on_time
+= ((GPU_COUNT_OVERFLOW
- mt_gpu_clock_on_time_start
) + mt_gpufreq_cur_wall_time
);
307 /* Use current time as next clock on starting time */
308 mt_gpu_clock_on_time_start
= mt_gpufreq_cur_wall_time
;
309 dprintk("GET, clock is still ON!\n");
312 /* Calculate period time. */
313 if(mt_gpufreq_cur_wall_time
>= mt_gpufreq_prev_wall_time
)
314 mt_gpufreq_period_time
= mt_gpufreq_cur_wall_time
- mt_gpufreq_prev_wall_time
;
316 mt_gpufreq_period_time
= (GPU_COUNT_OVERFLOW
- mt_gpufreq_prev_wall_time
) + mt_gpufreq_cur_wall_time
;
317 dprintk("GET 2, mt_gpu_clock_on_time_start = %d, mt_gpu_clock_total_on_time = %d\n", mt_gpu_clock_on_time_start
, mt_gpu_clock_total_on_time
);
318 dprintk("GET 3, mt_gpufreq_prev_wall_time = %d, mt_gpufreq_period_time = %d\n", mt_gpufreq_prev_wall_time
, mt_gpufreq_period_time
);
319 mt_gpufreq_prev_wall_time
= mt_gpufreq_cur_wall_time
;
321 /* Calculate ratio. */
322 load
= 100 * mt_gpu_clock_total_on_time
/ mt_gpufreq_period_time
;
323 mt_gpu_clock_total_on_time
= 0;
324 mt_gpufreq_period_time
= 0;
325 dprintk("GET 4, ratio = %d\n", load
);
328 spin_unlock_irqrestore(&mt_gpufreq_lock
, flags
);
332 EXPORT_SYMBOL(mt_gpufreq_gpu_clock_ratio
);
335 /* Default power table when mt_gpufreq_non_register() */
336 static void mt_setup_gpufreqs_default_power_table(int num
)
340 mt_gpufreqs_default_power
= kzalloc((1) * sizeof(struct mt_gpufreq_power_info
), GFP_KERNEL
);
341 if (mt_gpufreqs_default_power
== NULL
)
343 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "GPU default power table memory allocation fail\n");
347 mt_gpufreqs_default_power
[0].gpufreq_khz
= g_cur_freq
;
349 for (j
= 0; j
< ARRAY_SIZE(mt_gpufreqs_golden_power
); j
++)
351 if (g_cur_freq
== mt_gpufreqs_golden_power
[j
].gpufreq_khz
)
353 mt_gpufreqs_default_power
[0].gpufreq_power
= mt_gpufreqs_golden_power
[j
].gpufreq_power
;
358 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreqs_default_power[0].gpufreq_khz = %u\n", mt_gpufreqs_default_power
[0].gpufreq_khz
);
359 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreqs_default_power[0].gpufreq_power = %u\n", mt_gpufreqs_default_power
[0].gpufreq_power
);
361 #ifdef CONFIG_THERMAL
362 mtk_gpufreq_register(mt_gpufreqs_default_power
, 1);
366 static void mt_setup_gpufreqs_power_table(int num
)
370 mt_gpufreqs_power
= kzalloc((num
) * sizeof(struct mt_gpufreq_power_info
), GFP_KERNEL
);
371 if (mt_gpufreqs_power
== NULL
)
373 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "GPU power table memory allocation fail\n");
377 for (i
= 0; i
< num
; i
++) {
378 mt_gpufreqs_power
[i
].gpufreq_khz
= mt_gpufreqs
[i
].gpufreq_khz
;
380 for (j
= 0; j
< ARRAY_SIZE(mt_gpufreqs_golden_power
); j
++)
382 if (mt_gpufreqs
[i
].gpufreq_khz
== mt_gpufreqs_golden_power
[j
].gpufreq_khz
)
384 mt_gpufreqs_power
[i
].gpufreq_power
= mt_gpufreqs_golden_power
[j
].gpufreq_power
;
389 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreqs_power[%d].gpufreq_khz = %u\n", i
, mt_gpufreqs_power
[i
].gpufreq_khz
);
390 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreqs_power[%d].gpufreq_power = %u\n", i
, mt_gpufreqs_power
[i
].gpufreq_power
);
393 #ifdef CONFIG_THERMAL
394 mtk_gpufreq_register(mt_gpufreqs_power
, num
);
398 /***********************************************
399 * register frequency table to gpufreq subsystem
400 ************************************************/
401 static int mt_setup_gpufreqs_table(struct mt_gpufreq_info
*freqs
, int num
)
405 mt_gpufreqs
= kzalloc((num
) * sizeof(*freqs
), GFP_KERNEL
);
406 if (mt_gpufreqs
== NULL
)
409 for (i
= 0; i
< num
; i
++) {
410 mt_gpufreqs
[i
].gpufreq_khz
= freqs
[i
].gpufreq_khz
;
411 mt_gpufreqs
[i
].gpufreq_lower_bound
= freqs
[i
].gpufreq_lower_bound
;
412 mt_gpufreqs
[i
].gpufreq_upper_bound
= freqs
[i
].gpufreq_upper_bound
;
413 mt_gpufreqs
[i
].gpufreq_volt
= freqs
[i
].gpufreq_volt
;
414 mt_gpufreqs
[i
].gpufreq_remap
= freqs
[i
].gpufreq_remap
;
416 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "freqs[%d].gpufreq_khz = %u\n", i
, freqs
[i
].gpufreq_khz
);
417 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "freqs[%d].gpufreq_lower_bound = %u\n", i
, freqs
[i
].gpufreq_lower_bound
);
418 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "freqs[%d].gpufreq_upper_bound = %u\n", i
, freqs
[i
].gpufreq_upper_bound
);
419 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "freqs[%d].gpufreq_volt = %u\n", i
, freqs
[i
].gpufreq_volt
);
420 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "freqs[%d].gpufreq_remap = %u\n", i
, freqs
[i
].gpufreq_remap
);
423 mt_gpufreqs_num
= num
;
425 /* Initial frequency and voltage was already set in mt_gpufreq_set_initial() */
427 g_cur_freq
= freqs
[0].gpufreq_khz
;
428 g_cur_volt
= freqs
[0].gpufreq_volt
;
430 g_limited_max_id
= 0;
431 g_limited_min_id
= mt_gpufreqs_num
- 1;
433 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_setup_gpufreqs_table, g_cur_freq = %d, g_cur_volt = %d\n", g_cur_freq
, g_cur_volt
);
435 mt_setup_gpufreqs_power_table(num
);
440 /*****************************
441 * set GPU DVFS status
442 ******************************/
443 int mt_gpufreq_state_set(int enabled
)
445 ktime_t ktime
= ktime_set(mt_gpufreq_sample_s
, mt_gpufreq_sample_ns
);
449 if (!mt_gpufreq_pause
)
451 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "gpufreq already enabled\n");
458 g_gpufreq_dvfs_disable_count
--;
459 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "enable GPU DVFS: g_gpufreq_dvfs_disable_count = %d\n", g_gpufreq_dvfs_disable_count
);
461 /***********************************************
462 * enable DVFS if no any module still disable it
463 ************************************************/
464 if (g_gpufreq_dvfs_disable_count
<= 0)
466 mt_gpufreq_pause
= false;
467 hrtimer_start(&mt_gpufreq_timer
, ktime
, HRTIMER_MODE_REL
);
468 #ifdef GPU_CLOCK_RATIO
469 mt_gpufreq_period_time_reset();
474 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "someone still disable gpufreq, cannot enable it\n");
482 g_gpufreq_dvfs_disable_count
++;
483 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "disable GPU DVFS: g_gpufreq_dvfs_disable_count = %d\n", g_gpufreq_dvfs_disable_count
);
485 if (mt_gpufreq_pause
)
487 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "gpufreq already disabled\n");
491 mt_gpufreq_pause
= true;
492 hrtimer_cancel(&mt_gpufreq_timer
);
497 EXPORT_SYMBOL(mt_gpufreq_state_set
);
499 static void mt_gpu_clock_switch(unsigned int sel
)
501 unsigned int clk_cfg_0
= 0, clk_cfg_4
= 0;
503 clk_cfg_0
= DRV_Reg32(CLK_CFG_0
);
504 clk_cfg_4
= DRV_Reg32(CLK_CFG_4
);
508 case GPU_MMPLL_D3
: // 476Mhz
509 if(mt_gpufreq_enable_mmpll
== 0)
511 enable_pll(MMPLL
, "GPU_DVFS");
512 mt_gpufreq_enable_mmpll
= 1;
515 clk_cfg_0
= (clk_cfg_0
& 0xFFF8FFFF) | (0x5 << 16);
516 mt65xx_reg_sync_writel(clk_cfg_0
, CLK_CFG_0
);
517 dprintk("mt_gpu_clock_switch: switch MFG clock to GPU_MMPLL_D3\n");
519 clk_cfg_4
= (clk_cfg_4
& 0xFFFFFFF8) | (0x5);
520 mt65xx_reg_sync_writel(clk_cfg_4
, CLK_CFG_4
);
521 dprintk("mt_gpu_clock_switch: switch HYD clock to GPU_MMPLL_D3\n");
523 if(mt_gpufreq_enable_mainpll
== 1)
525 disable_pll(MAINPLL
, "GPU_DVFS");
526 mt_gpufreq_enable_mainpll
= 0;
529 case GPU_SYSPLL_D2
: // 403Mhz
530 if(mt_gpufreq_enable_mainpll
== 0)
532 enable_pll(MAINPLL
, "GPU_DVFS");
533 mt_gpufreq_enable_mainpll
= 1;
536 clk_cfg_0
= (clk_cfg_0
& 0xFFF8FFFF) | (0x2 << 16);
537 mt65xx_reg_sync_writel(clk_cfg_0
, CLK_CFG_0
);
538 dprintk("mt_gpu_clock_switch: switch MFG clock to GPU_SYSPLL_D2\n");
540 clk_cfg_4
= (clk_cfg_4
& 0xFFFFFFF8) | (0x2);
541 mt65xx_reg_sync_writel(clk_cfg_4
, CLK_CFG_4
);
542 dprintk("mt_gpu_clock_switch: switch HYD clock to GPU_SYSPLL_D2\n");
544 if(mt_gpufreq_enable_mmpll
== 1)
546 disable_pll(MMPLL
, "GPU_DVFS");
547 mt_gpufreq_enable_mmpll
= 0;
550 case GPU_MMPLL_D4
: // 357Mhz
551 if(mt_gpufreq_enable_mmpll
== 0)
553 enable_pll(MMPLL
, "GPU_DVFS");
554 mt_gpufreq_enable_mmpll
= 1;
557 clk_cfg_0
= (clk_cfg_0
& 0xFFF8FFFF) | (0x6 << 16);
558 mt65xx_reg_sync_writel(clk_cfg_0
, CLK_CFG_0
);
559 dprintk("mt_gpu_clock_switch: switch MFG clock to GPU_MMPLL_D4\n");
561 clk_cfg_4
= (clk_cfg_4
& 0xFFFFFFF8) | (0x6);
562 mt65xx_reg_sync_writel(clk_cfg_4
, CLK_CFG_4
);
563 dprintk("mt_gpu_clock_switch: switch HYD clock to GPU_MMPLL_D4\n");
565 if(mt_gpufreq_enable_mainpll
== 1)
567 disable_pll(MAINPLL
, "GPU_DVFS");
568 mt_gpufreq_enable_mainpll
= 0;
571 case GPU_UNIVPLL1_D2
: // 312Mhz
572 clk_cfg_0
= (clk_cfg_0
& 0xFFF8FFFF) | (0x4 << 16);
573 mt65xx_reg_sync_writel(clk_cfg_0
, CLK_CFG_0
);
574 dprintk("mt_gpu_clock_switch: switch MFG clock to GPU_UNIVPLL1_D2\n");
576 clk_cfg_4
= (clk_cfg_4
& 0xFFFFFFF8) | (0x4);
577 mt65xx_reg_sync_writel(clk_cfg_4
, CLK_CFG_4
);
578 dprintk("mt_gpu_clock_switch: switch HYD clock to GPU_UNIVPLL1_D2\n");
580 if(mt_gpufreq_enable_mainpll
== 1)
582 disable_pll(MAINPLL
, "GPU_DVFS");
583 mt_gpufreq_enable_mainpll
= 0;
586 if(mt_gpufreq_enable_mmpll
== 1)
588 disable_pll(MMPLL
, "GPU_DVFS");
589 mt_gpufreq_enable_mmpll
= 0;
592 case GPU_MMPLL_D5
: // 286Mhz
593 if(mt_gpufreq_enable_mmpll
== 0)
595 enable_pll(MMPLL
, "GPU_DVFS");
596 mt_gpufreq_enable_mmpll
= 1;
599 clk_cfg_0
= (clk_cfg_0
& 0xFFF8FFFF) | (0x7 << 16);
600 mt65xx_reg_sync_writel(clk_cfg_0
, CLK_CFG_0
);
601 dprintk("mt_gpu_clock_switch: switch MFG clock to GPU_MMPLL_D5\n");
603 clk_cfg_4
= (clk_cfg_4
& 0xFFFFFFF8) | (0x7);
604 mt65xx_reg_sync_writel(clk_cfg_4
, CLK_CFG_4
);
605 dprintk("mt_gpu_clock_switch: switch HYD clock to GPU_MMPLL_D5\n");
607 if(mt_gpufreq_enable_mainpll
== 1)
609 disable_pll(MAINPLL
, "GPU_DVFS");
610 mt_gpufreq_enable_mainpll
= 0;
613 case GPU_SYSPLL_D3
: // 268Mhz
614 if(mt_gpufreq_enable_mainpll
== 0)
616 enable_pll(MAINPLL
, "GPU_DVFS");
617 mt_gpufreq_enable_mainpll
= 1;
620 clk_cfg_0
= (clk_cfg_0
& 0xFFF8FFFF) | (0x3 << 16);
621 mt65xx_reg_sync_writel(clk_cfg_0
, CLK_CFG_0
);
622 dprintk("mt_gpu_clock_switch: switch MFG clock to GPU_SYSPLL_D3\n");
624 clk_cfg_4
= (clk_cfg_4
& 0xFFFFFFF8) | (0x7);
625 mt65xx_reg_sync_writel(clk_cfg_4
, CLK_CFG_4
);
626 dprintk("mt_gpu_clock_switch: switch HYD clock to GPU_MMPLL_D5\n");
628 if(mt_gpufreq_enable_mmpll
== 1)
630 disable_pll(MMPLL
, "GPU_DVFS");
631 mt_gpufreq_enable_mmpll
= 0;
634 case GPU_MMPLL_D6
: // 238Mhz
635 if(mt_gpufreq_enable_mmpll
== 0)
637 enable_pll(MMPLL
, "GPU_DVFS");
638 mt_gpufreq_enable_mmpll
= 1;
641 clk_cfg_0
= (clk_cfg_0
& 0xFFF8FFFF) | (0x1 << 16);
642 mt65xx_reg_sync_writel(clk_cfg_0
, CLK_CFG_0
);
643 dprintk("mt_gpu_clock_switch: switch MFG clock to GPU_MMPLL_D6\n");
645 clk_cfg_4
= (clk_cfg_4
& 0xFFFFFFF8) | (0x7);
646 mt65xx_reg_sync_writel(clk_cfg_4
, CLK_CFG_4
);
647 dprintk("mt_gpu_clock_switch: switch HYD clock to GPU_MMPLL_D5\n");
649 if(mt_gpufreq_enable_mainpll
== 1)
651 disable_pll(MAINPLL
, "GPU_DVFS");
652 mt_gpufreq_enable_mainpll
= 0;
655 case GPU_UNIVPLL1_D4
: // 156Mhz
656 clk_cfg_0
= (clk_cfg_0
& 0xFFF8FFFF) | (0x0 << 16);
657 mt65xx_reg_sync_writel(clk_cfg_0
, CLK_CFG_0
);
658 dprintk("mt_gpu_clock_switch: switch MFG clock to GPU_UNIVPLL1_D4\n");
660 clk_cfg_4
= (clk_cfg_4
& 0xFFFFFFF8) | (0x7);
661 mt65xx_reg_sync_writel(clk_cfg_4
, CLK_CFG_4
);
662 dprintk("mt_gpu_clock_switch: switch HYD clock to GPU_MMPLL_D5\n");
664 if(mt_gpufreq_enable_mainpll
== 1)
666 disable_pll(MAINPLL
, "GPU_DVFS");
667 mt_gpufreq_enable_mainpll
= 0;
670 if(mt_gpufreq_enable_mmpll
== 1)
672 disable_pll(MMPLL
, "GPU_DVFS");
673 mt_gpufreq_enable_mmpll
= 0;
677 dprintk("mt_gpu_clock_switch: do not support specified clock (%d)\n", sel
);
681 dprintk("CLK_CFG_0 = 0x%x, CLK_CFG_4 = 0x%x\n", DRV_Reg32(CLK_CFG_0
), DRV_Reg32(CLK_CFG_4
));
684 static void mt_gpu_volt_switch(unsigned int volt_old
, unsigned int volt_new
)
688 upmu_set_vrf18_2_vosel_ctrl(0); // SW control mode
690 if (volt_new
== GPU_POWER_VRF18_1_15V
)
692 dprintk("mt_gpu_volt_switch: switch MFG power to GPU_POWER_VRF18_1_15V\n");
694 else if (volt_new
== GPU_POWER_VRF18_1_10V
)
696 dprintk("mt_gpu_volt_switch: switch MFG power to GPU_POWER_VRF18_1_10V\n");
698 else if (volt_new
== GPU_POWER_VRF18_1_05V
)
700 dprintk("mt_gpu_volt_switch: switch MFG power to GPU_POWER_VRF18_1_05V\n");
702 else if (volt_new
== GPU_POWER_VCORE_1_05V
)
704 dprintk("mt_gpu_volt_switch: use VCORE, not support voltage scaling\n");
709 dprintk("mt_gpu_volt_switch: do not support specified volt (%d)\n", volt_new
);
713 if (volt_old
< volt_new
)
715 for (i
= ((int)volt_old
+ 1); i
<= (int)volt_new
; i
++)
717 dprintk("mt_gpu_volt_switch: volt_old < volt_new, i=(%d)\n", i
);
718 upmu_set_vrf18_2_vosel(i
);
719 udelay(PMIC_SETTLE_TIME
);
724 for (i
= ((int)volt_old
- 1); i
>= (int)volt_new
; i
--)
726 dprintk("mt_gpu_volt_switch: volt_old > volt_new, i=(%d)\n", i
);
727 upmu_set_vrf18_2_vosel(i
);
728 udelay(PMIC_SETTLE_TIME
);
733 static void mt_gpu_volt_switch_initial(unsigned int volt_new
)
735 upmu_set_vrf18_2_vosel_ctrl(0); // SW control mode
737 if (volt_new
== GPU_POWER_VRF18_1_15V
)
739 upmu_set_vrf18_2_vosel(4);
740 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpu_volt_switch_initial: switch MFG power to GPU_POWER_VRF18_1_15V\n");
742 else if (volt_new
== GPU_POWER_VRF18_1_10V
)
744 upmu_set_vrf18_2_vosel(2);
745 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpu_volt_switch_initial: switch MFG power to GPU_POWER_VRF18_1_10V\n");
747 else if (volt_new
== GPU_POWER_VRF18_1_05V
)
749 upmu_set_vrf18_2_vosel(0);
750 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpu_volt_switch_initial: switch MFG power to GPU_POWER_VRF18_1_05V\n");
752 else if (volt_new
== GPU_POWER_VCORE_1_05V
)
754 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpu_volt_switch_initial: use VCORE, not support voltage scaling\n");
759 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpu_volt_switch_initial: do not support specified volt (%d)\n");
765 /***********************************************************
766 * 1. 3D driver will check efuse and set initial frequency and voltage
767 * 2. When GPU idle in intial, voltage could be set directly.
768 ************************************************************/
769 void mt_gpufreq_set_initial(unsigned int freq_new
, unsigned int volt_new
)
771 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreq_set_initial, freq_new = %d, volt_new = %d, g_cur_freq = %d\n", freq_new
, volt_new
, g_cur_freq
);
772 g_freq_new_init_keep
= freq_new
;
773 g_volt_new_init_keep
= volt_new
;
775 mt_gpufreq_check_freq_and_set_pll();
777 if(freq_new
== g_cur_freq
)
779 g_volt_set_init_step_1
= 1;
783 if (freq_new
> g_cur_freq
)
785 #ifdef MT_BUCK_ADJUST
786 if (pmic_get_gpu_status_bit_info() == 0) // 1: VCORE, 0: VRF18_2
788 g_volt_set_init_step_2
= 1;
789 mt_gpu_volt_switch_initial(volt_new
);
790 udelay(PMIC_SETTLE_TIME
);
794 mt_gpu_clock_switch(freq_new
);
798 mt_gpu_clock_switch(freq_new
);
800 #ifdef MT_BUCK_ADJUST
801 if (pmic_get_gpu_status_bit_info() == 0) // 1: VCORE, 0: VRF18_2
803 g_volt_set_init_step_3
= 1;
804 mt_gpu_volt_switch_initial(volt_new
);
809 g_cur_freq
= freq_new
;
810 g_cur_volt
= volt_new
;
811 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreq_set_initial, g_cur_freq = %d, g_cur_volt = %d\n", g_cur_freq
, g_cur_volt
);
813 EXPORT_SYMBOL(mt_gpufreq_set_initial
);
816 /*****************************************
817 * frequency ramp up and ramp down handler
818 ******************************************/
819 /***********************************************************
821 * 1. frequency ramp up need to wait voltage settle
822 * 2. frequency ramp down do not need to wait voltage settle
823 ************************************************************/
824 static void mt_gpufreq_set(unsigned int freq_old
, unsigned int freq_new
, unsigned int volt_old
, unsigned int volt_new
)
826 if (freq_new
> freq_old
)
828 #ifdef MT_BUCK_ADJUST
829 if (pmic_get_gpu_status_bit_info() == 0) // 1: VCORE, 0: VRF18_2
831 if (volt_old
!= volt_new
)
833 mt_gpu_volt_switch(volt_old
, volt_new
);
838 mt_gpu_clock_switch(freq_new
);
842 mt_gpu_clock_switch(freq_new
);
844 #ifdef MT_BUCK_ADJUST
845 if (pmic_get_gpu_status_bit_info() == 0) // 1: VCORE, 0: VRF18_2
847 if (volt_old
!= volt_new
)
849 mt_gpu_volt_switch(volt_old
, volt_new
);
855 g_cur_freq
= freq_new
;
856 g_cur_volt
= volt_new
;
859 static int mt_gpufreq_look_up(unsigned int load
)
861 int i
= 0, remap
= 100;
863 /**************************
864 * look up the remap value
865 ***************************/
866 for (i
= 0; i
< mt_gpufreqs_num
; i
++)
868 if (mt_gpufreqs
[i
].gpufreq_khz
== g_cur_freq
)
870 remap
= mt_gpufreqs
[i
].gpufreq_remap
;
875 load
= (load
* remap
) / 100;
877 dprintk("GPU Loading = %d\n", load
);
879 /******************************
880 * look up the target frequency
881 *******************************/
882 for (i
= 0; i
< mt_gpufreqs_num
; i
++)
884 if (load
> mt_gpufreqs
[i
].gpufreq_lower_bound
&& load
<= mt_gpufreqs
[i
].gpufreq_upper_bound
)
890 return (mt_gpufreqs_num
- 1);
893 /**********************************
894 * gpufreq target callback function
895 ***********************************/
896 /*************************************************
898 * 1. handle frequency change request
899 * 2. call mt_gpufreq_set to set target frequency
900 **************************************************/
901 static int mt_gpufreq_target(int idx
)
903 unsigned long flags
, target_freq
, target_volt
;
905 spin_lock_irqsave(&mt_gpufreq_lock
, flags
);
907 /**********************************
908 * look up for the target GPU OPP
909 ***********************************/
910 target_freq
= mt_gpufreqs
[idx
].gpufreq_khz
;
911 target_volt
= mt_gpufreqs
[idx
].gpufreq_volt
;
913 /**********************************
914 * Check if need to keep max frequency
915 ***********************************/
916 if (mt_gpufreq_keep_max_freq(g_cur_freq
, target_freq
))
918 target_freq
= mt_gpufreqs
[g_gpufreq_max_id
].gpufreq_khz
;
919 target_volt
= mt_gpufreqs
[g_gpufreq_max_id
].gpufreq_volt
;
920 dprintk("Keep MAX frequency %d !\n", target_freq
);
923 /****************************************************
924 * If need to raise frequency, raise to max frequency
925 *****************************************************/
926 if(target_freq
> g_cur_freq
)
928 target_freq
= mt_gpufreqs
[g_gpufreq_max_id
].gpufreq_khz
;
929 target_volt
= mt_gpufreqs
[g_gpufreq_max_id
].gpufreq_volt
;
930 dprintk("Need to raise frequency, raise to MAX frequency %d !\n", target_freq
);
933 if (target_freq
> mt_gpufreqs
[g_limited_max_id
].gpufreq_khz
)
935 /*********************************************
936 * target_freq > limited_freq, need to adjust
937 **********************************************/
938 target_freq
= mt_gpufreqs
[g_limited_max_id
].gpufreq_khz
;
939 target_volt
= mt_gpufreqs
[g_limited_max_id
].gpufreq_volt
;
940 dprintk("Limit! Target freq %d > Thermal limit frequency %d\n", target_freq
, mt_gpufreqs
[g_limited_max_id
].gpufreq_khz
);
943 /************************************************
944 * If /proc command fix the frequency.
945 *************************************************/
946 if(mt_gpufreq_keep_specific_frequency
== true)
948 target_freq
= mt_gpufreq_fixed_frequency
;
949 target_volt
= mt_gpufreq_fixed_voltage
;
950 dprintk("Fixed! fixed frequency %d, fixed voltage %d\n", target_freq
, target_volt
);
953 /************************************************
954 * target frequency == current frequency, skip it
955 *************************************************/
956 if (g_cur_freq
== target_freq
)
958 spin_unlock_irqrestore(&mt_gpufreq_lock
, flags
);
959 dprintk("GPU frequency from %d MHz to %d MHz (skipped) due to same frequency\n", g_cur_freq
/ 1000, target_freq
/ 1000);
963 dprintk("GPU current frequency %d MHz, target frequency %d MHz\n", g_cur_freq
/ 1000, target_freq
/ 1000);
965 /******************************
966 * set to the target freeuency
967 *******************************/
968 mt_gpufreq_set(g_cur_freq
, target_freq
, g_cur_volt
, target_volt
);
970 spin_unlock_irqrestore(&mt_gpufreq_lock
, flags
);
975 /*********************************
976 * early suspend callback function
977 **********************************/
978 void mt_gpufreq_early_suspend(struct early_suspend
*h
)
980 mt_gpufreq_state_set(0);
982 if(mt_gpufreq_enable_mainpll
== 1)
984 disable_pll(MAINPLL
, "GPU_DVFS");
985 mt_gpufreq_enable_mainpll
= 0;
988 if(mt_gpufreq_enable_mmpll
== 1)
990 disable_pll(MMPLL
, "GPU_DVFS");
991 mt_gpufreq_enable_mmpll
= 0;
995 /*******************************
996 * late resume callback function
997 ********************************/
998 void mt_gpufreq_late_resume(struct early_suspend
*h
)
1000 mt_gpufreq_check_freq_and_set_pll();
1002 mt_gpufreq_state_set(1);
1005 /************************************************
1006 * frequency adjust interface for thermal protect
1007 *************************************************/
1008 /******************************************************
1009 * parameter: target power
1010 *******************************************************/
1011 void mt_gpufreq_thermal_protect(unsigned int limited_power
)
1014 unsigned int limited_freq
= 0;
1016 if (mt_gpufreqs_num
== 0)
1019 if (limited_power
== 0)
1021 g_limited_max_id
= 0;
1025 g_limited_max_id
= mt_gpufreqs_num
- 1;
1027 for (i
= 0; i
< ARRAY_SIZE(mt_gpufreqs_golden_power
); i
++)
1029 if (mt_gpufreqs_golden_power
[i
].gpufreq_power
<= limited_power
)
1031 limited_freq
= mt_gpufreqs_golden_power
[i
].gpufreq_khz
;
1036 for (i
= 0; i
< mt_gpufreqs_num
; i
++)
1038 if (mt_gpufreqs
[i
].gpufreq_khz
<= limited_freq
)
1040 g_limited_max_id
= i
;
1046 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "limit frequency upper bound to id = %d, frequency = %d\n", g_limited_max_id
, mt_gpufreqs
[g_limited_max_id
].gpufreq_khz
);
1047 mt_gpufreq_target(g_limited_max_id
);
1051 EXPORT_SYMBOL(mt_gpufreq_thermal_protect
);
1053 /************************************************
1054 * return current GPU frequency
1055 *************************************************/
1056 unsigned int mt_gpufreq_cur_freq(void)
1058 dprintk("current GPU frequency is %d MHz\n", g_cur_freq
/ 1000);
1061 EXPORT_SYMBOL(mt_gpufreq_cur_freq
);
1063 /************************************************
1064 * return current GPU loading
1065 *************************************************/
1066 unsigned int mt_gpufreq_cur_load(void)
1068 dprintk("current GPU load is %d\n", g_cur_load
);
1071 EXPORT_SYMBOL(mt_gpufreq_cur_load
);
1073 /******************************
1074 * show current GPU DVFS stauts
1075 *******************************/
1076 static int mt_gpufreq_state_read(char *buf
, char **start
, off_t off
, int count
, int *eof
, void *data
)
1081 if (!mt_gpufreq_pause
)
1082 p
+= sprintf(p
, "GPU DVFS enabled\n");
1084 p
+= sprintf(p
, "GPU DVFS disabled\n");
1090 /****************************************
1091 * set GPU DVFS stauts by sysfs interface
1092 *****************************************/
1093 static ssize_t
mt_gpufreq_state_write(struct file
*file
, const char *buffer
, unsigned long count
, void *data
)
1097 /* If 3D not registered, it need to init timer and create kthread. */
1098 if((mt_gpufreq_registered
== false) && (mt_gpufreq_registered_statewrite
== false))
1100 mt_gpufreq_pause
= true;
1102 mt_gpufreqs
= kzalloc((1) * sizeof(struct mt_gpufreq_info
), GFP_KERNEL
);
1103 mt_gpufreqs
[0].gpufreq_khz
= g_cur_freq
;
1104 mt_gpufreqs
[0].gpufreq_lower_bound
= 0;
1105 mt_gpufreqs
[0].gpufreq_upper_bound
= 100;
1106 mt_gpufreqs
[0].gpufreq_volt
= 0;
1107 mt_gpufreqs
[0].gpufreq_remap
= 100;
1108 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "freqs[0].gpufreq_khz = %u\n", mt_gpufreqs
[0].gpufreq_khz
);
1109 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "freqs[0].gpufreq_lower_bound = %u\n", mt_gpufreqs
[0].gpufreq_lower_bound
);
1110 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "freqs[0].gpufreq_upper_bound = %u\n", mt_gpufreqs
[0].gpufreq_upper_bound
);
1111 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "freqs[0].gpufreq_volt = %u\n", mt_gpufreqs
[0].gpufreq_volt
);
1112 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "freqs[0].gpufreq_remap = %u\n", mt_gpufreqs
[0].gpufreq_remap
);
1114 mt_gpufreq_timer
.function
= mt_gpufreq_timer_func
;
1115 mt_gpufreq_thread
= kthread_run(mt_gpufreq_thread_handler
, 0, "mt_gpufreq");
1116 if (IS_ERR(mt_gpufreq_thread
))
1118 printk("[%s]: failed to create mt_gpufreq thread\n", __FUNCTION__
);
1120 mt_gpufreq_registered_statewrite
= true;
1123 if (sscanf(buffer
, "%d", &enabled
) == 1)
1127 mt_gpufreq_keep_max_frequency
= false;
1128 mt_gpufreq_state_set(1);
1130 else if (enabled
== 0)
1132 /* Keep MAX frequency when GPU DVFS disabled. */
1133 mt_gpufreq_keep_max_frequency
= true;
1134 mt_gpufreq_target(g_gpufreq_max_id
);
1135 mt_gpufreq_state_set(0);
1139 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "bad argument!! argument should be \"1\" or \"0\"\n");
1144 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "bad argument!! argument should be \"1\" or \"0\"\n");
1150 /****************************
1151 * show current limited power
1152 *****************************/
1153 static int mt_gpufreq_limited_power_read(char *buf
, char **start
, off_t off
, int count
, int *eof
, void *data
)
1158 p
+= sprintf(p
, "g_limited_max_id = %d, frequency = %d\n", g_limited_max_id
, mt_gpufreqs
[g_limited_max_id
].gpufreq_khz
);
1164 /**********************************
1165 * limited power for thermal protect
1166 ***********************************/
1167 static ssize_t
mt_gpufreq_limited_power_write(struct file
*file
, const char *buffer
, unsigned long count
, void *data
)
1169 unsigned int power
= 0;
1171 if (sscanf(buffer
, "%u", &power
) == 1)
1173 mt_gpufreq_thermal_protect(power
);
1178 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "bad argument!! please provide the maximum limited power\n");
1184 /***************************
1185 * show current debug status
1186 ****************************/
1187 static int mt_gpufreq_debug_read(char *buf
, char **start
, off_t off
, int count
, int *eof
, void *data
)
1192 if (mt_gpufreq_debug
)
1193 p
+= sprintf(p
, "gpufreq debug enabled\n");
1195 p
+= sprintf(p
, "gpufreq debug disabled\n");
1201 /***********************
1202 * enable debug message
1203 ************************/
1204 static ssize_t
mt_gpufreq_debug_write(struct file
*file
, const char *buffer
, unsigned long count
, void *data
)
1208 if (sscanf(buffer
, "%d", &debug
) == 1)
1212 mt_gpufreq_debug
= 0;
1215 else if (debug
== 1)
1217 mt_gpufreq_debug
= 1;
1222 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "bad argument!! should be 0 or 1 [0: disable, 1: enable]\n");
1227 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "bad argument!! should be 0 or 1 [0: disable, 1: enable]\n");
1233 /********************
1234 * show sampling rate
1235 *********************/
1236 static int mt_gpufreq_sampling_rate_read(char *buf
, char **start
, off_t off
, int count
, int *eof
, void *data
)
1241 p
+= sprintf(p
, "get sampling rate = %d (s) %d (ns)\n", mt_gpufreq_sample_s
, mt_gpufreq_sample_ns
);
1247 /********************
1249 *********************/
1250 static ssize_t
mt_gpufreq_sampling_rate_write(struct file
*file
, const char *buffer
, unsigned long count
, void *data
)
1252 int len
= 0, s
= 0, ns
= 0;
1255 len
= (count
< (sizeof(desc
) - 1)) ? count
: (sizeof(desc
) - 1);
1256 if (copy_from_user(desc
, buffer
, len
))
1262 if (sscanf(desc
, "%d %d", &s
, &ns
) == 2)
1264 printk("[%s]: set sampling rate = %d (s), %d (ns)\n", __FUNCTION__
, s
, ns
);
1265 mt_gpufreq_sample_s
= s
;
1266 mt_gpufreq_sample_ns
= ns
;
1271 printk("[%s]: bad argument!! should be \"[s]\" or \"[ns]\"\n", __FUNCTION__
);
1277 /********************
1278 * show GPU OPP table
1279 *********************/
1280 static int mt_gpufreq_opp_dump_read(char *buf
, char **start
, off_t off
, int count
, int *eof
, void *data
)
1282 int i
= 0, j
= 0, len
= 0;
1285 for (i
= 0; i
< mt_gpufreqs_num
; i
++)
1287 p
+= sprintf(p
, "[%d] ", i
);
1288 p
+= sprintf(p
, "freq = %d, ", mt_gpufreqs
[i
].gpufreq_khz
);
1289 p
+= sprintf(p
, "lower_bound = %d, ", mt_gpufreqs
[i
].gpufreq_lower_bound
);
1290 p
+= sprintf(p
, "upper_bound = %d, ", mt_gpufreqs
[i
].gpufreq_upper_bound
);
1291 p
+= sprintf(p
, "volt = %d, ", mt_gpufreqs
[i
].gpufreq_volt
);
1292 p
+= sprintf(p
, "remap = %d, ", mt_gpufreqs
[i
].gpufreq_remap
);
1294 for (j
= 0; j
< ARRAY_SIZE(mt_gpufreqs_golden_power
); j
++)
1296 if (mt_gpufreqs_golden_power
[j
].gpufreq_khz
== mt_gpufreqs
[i
].gpufreq_khz
)
1298 p
+= sprintf(p
, "power = %d\n", mt_gpufreqs_golden_power
[j
].gpufreq_power
);
1308 /***************************
1309 * show current specific frequency status
1310 ****************************/
1311 static int mt_gpufreq_fixed_frequency_read(char *buf
, char **start
, off_t off
, int count
, int *eof
, void *data
)
1316 if (mt_gpufreq_keep_specific_frequency
)
1317 p
+= sprintf(p
, "gpufreq fixed frequency enabled\n");
1319 p
+= sprintf(p
, "gpufreq fixed frequency disabled\n");
1325 /***********************
1326 * enable specific frequency
1327 ************************/
1328 static ssize_t
mt_gpufreq_fixed_frequency_write(struct file
*file
, const char *buffer
, unsigned long count
, void *data
)
1334 if (sscanf(buffer
, "%d %d %d", &enable
, &fixed_freq
, &fixed_volt
) == 3)
1338 mt_gpufreq_keep_specific_frequency
= false;
1341 else if (enable
== 1)
1343 mt_gpufreq_keep_specific_frequency
= true;
1344 mt_gpufreq_fixed_frequency
= fixed_freq
;
1345 mt_gpufreq_fixed_voltage
= fixed_volt
;
1350 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "bad argument!! should be [enable fixed_freq fixed_volt]\n");
1355 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "bad argument!! should be [enable fixed_freq fixed_volt]\n");
1361 /********************
1362 * show variable dump
1363 *********************/
1364 static int mt_gpufreq_var_dump(char *buf
, char **start
, off_t off
, int count
, int *eof
, void *data
)
1369 unsigned int clk_cfg_0
= 0;
1370 clk_cfg_0
= DRV_Reg32(CLK_CFG_0
);
1371 clk_cfg_0
= (clk_cfg_0
& 0x00070000) >> 16;
1372 p
+= sprintf(p
, "clk_cfg_0 = %d\n", clk_cfg_0
);
1373 p
+= sprintf(p
, "pmic_get_gpu_status_bit_info() = %d\n", pmic_get_gpu_status_bit_info());
1374 p
+= sprintf(p
, "mt_gpufreq_enable_mainpll = %d\n", mt_gpufreq_enable_mainpll
);
1375 p
+= sprintf(p
, "mt_gpufreq_enable_mmpll = %d\n", mt_gpufreq_enable_mmpll
);
1377 p
+= sprintf(p
, "g_cur_freq_init_keep = %d\n", g_cur_freq_init_keep
);
1378 p
+= sprintf(p
, "g_volt_set_init_step_1 = %d, g_volt_set_init_step_2 = %d, g_volt_set_init_step_3 = %d\n", g_volt_set_init_step_1
, g_volt_set_init_step_2
, g_volt_set_init_step_3
);
1379 p
+= sprintf(p
, "g_freq_new_init_keep = %d, g_volt_new_init_keep = %d\n", g_freq_new_init_keep
, g_volt_new_init_keep
);
1380 p
+= sprintf(p
, "mt_gpufreq_registered = %d, mt_gpufreq_already_non_registered = %d\n", mt_gpufreq_registered
, mt_gpufreq_already_non_registered
);
1386 #ifdef GPU_CLOCK_RATIO
1387 /**********************************
1388 * Clock on ratio with mdelay
1389 ***********************************/
1390 static ssize_t
mt_gpufreq_clock_on_ratio_write(struct file
*file
, const char *buffer
, unsigned long count
, void *data
)
1392 unsigned int delay
= 0;
1394 if (sscanf(buffer
, "%u", &delay
) == 1)
1396 mt_gpufreq_gpu_clock_ratio(GPU_DVFS_CLOCK_RATIO_ON
);
1398 mt_gpufreq_gpu_clock_ratio(GPU_DVFS_CLOCK_RATIO_OFF
);
1404 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "bad argument!! please provide proper mdelay time\n");
1411 enum hrtimer_restart
mt_gpufreq_timer_func(struct hrtimer
*timer
)
1413 mt_gpufreq_timer_flag
= 1; wake_up_interruptible(&mt_gpufreq_timer_waiter
);
1415 return HRTIMER_NORESTART
;
1418 static int mt_gpufreq_thread_handler(void *unused
)
1421 unsigned int load
= 0;
1425 ktime_t ktime
= ktime_set(mt_gpufreq_sample_s
, mt_gpufreq_sample_ns
);
1427 wait_event_interruptible(mt_gpufreq_timer_waiter
, mt_gpufreq_timer_flag
!= 0);
1428 mt_gpufreq_timer_flag
= 0;
1430 /**********************************
1432 ***********************************/
1433 #ifdef GPU_CLOCK_RATIO
1434 load
= mt_gpufreq_gpu_clock_ratio(GPU_DVFS_CLOCK_RATIO_GET
);
1437 idx
= mt_gpufreq_look_up(load
);
1439 mt_gpufreq_target(idx
);
1441 if(mt_gpufreq_pause
== false)
1442 hrtimer_start(&mt_gpufreq_timer
, ktime
, HRTIMER_MODE_REL
);
1444 } while (!kthread_should_stop());
1449 /*********************************
1450 * mediatek gpufreq registration
1451 **********************************/
1452 int mt_gpufreq_register(struct mt_gpufreq_info
*freqs
, int num
)
1454 if(mt_gpufreq_registered
== false)
1456 ktime_t ktime
= ktime_set(mt_gpufreq_sample_s
, mt_gpufreq_sample_ns
);
1458 mt_gpufreq_registered
= true;
1460 #ifdef CONFIG_HAS_EARLYSUSPEND
1461 mt_gpufreq_early_suspend_handler
.suspend
= mt_gpufreq_early_suspend
;
1462 mt_gpufreq_early_suspend_handler
.resume
= mt_gpufreq_late_resume
;
1463 register_early_suspend(&mt_gpufreq_early_suspend_handler
);
1466 /**********************
1467 * setup gpufreq table
1468 ***********************/
1469 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "setup gpufreqs table\n");
1471 mt_setup_gpufreqs_table(freqs
, num
);
1473 /************************************
1474 * launch a timer for period sampling
1475 *************************************/
1476 hrtimer_init(&mt_gpufreq_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1477 mt_gpufreq_timer
.function
= mt_gpufreq_timer_func
;
1479 mt_gpufreq_thread
= kthread_run(mt_gpufreq_thread_handler
, 0, "mt_gpufreq");
1480 if (IS_ERR(mt_gpufreq_thread
))
1482 printk("[%s]: failed to create mt_gpufreq thread\n", __FUNCTION__
);
1485 #ifdef GPU_DVFS_DEFAULT_DISABLED
1486 mt_gpufreq_state_set(0);
1489 if(mt_gpufreq_pause
== false)
1491 hrtimer_start(&mt_gpufreq_timer
, ktime
, HRTIMER_MODE_REL
);
1492 #ifdef GPU_CLOCK_RATIO
1493 mt_gpufreq_period_time_reset();
1497 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mediatek gpufreq registration done\n");
1501 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mediatek gpufreq already registered !\n");
1505 EXPORT_SYMBOL(mt_gpufreq_register
);
1507 /*********************************
1508 * mediatek gpufreq non registration
1509 **********************************/
1510 int mt_gpufreq_non_register(void)
1512 if(mt_gpufreq_already_non_registered
== false)
1514 mt_gpufreq_already_non_registered
= true;
1515 mt_setup_gpufreqs_default_power_table(1);
1517 hrtimer_init(&mt_gpufreq_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1518 mt_gpufreq_state_set(0);
1519 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreq_non_register() done\n");
1523 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreq_non_register() already called !\n");
1527 EXPORT_SYMBOL(mt_gpufreq_non_register
);
1529 /**********************************
1530 * mediatek gpufreq initialization
1531 ***********************************/
1532 static int __init
mt_gpufreq_init(void)
1534 struct proc_dir_entry
*mt_entry
= NULL
;
1535 struct proc_dir_entry
*mt_gpufreq_dir
= NULL
;
1536 unsigned int clk_cfg_0
= 0;
1538 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreq_init\n");
1540 mt_gpufreq_dir
= proc_mkdir("gpufreq", NULL
);
1541 if (!mt_gpufreq_dir
)
1543 pr_err("[%s]: mkdir /proc/gpufreq failed\n", __FUNCTION__
);
1547 mt_entry
= create_proc_entry("gpufreq_debug", S_IRUGO
| S_IWUSR
| S_IWGRP
, mt_gpufreq_dir
);
1550 mt_entry
->read_proc
= mt_gpufreq_debug_read
;
1551 mt_entry
->write_proc
= mt_gpufreq_debug_write
;
1554 mt_entry
= create_proc_entry("gpufreq_limited_power", S_IRUGO
| S_IWUSR
| S_IWGRP
, mt_gpufreq_dir
);
1557 mt_entry
->read_proc
= mt_gpufreq_limited_power_read
;
1558 mt_entry
->write_proc
= mt_gpufreq_limited_power_write
;
1561 mt_entry
= create_proc_entry("gpufreq_state", S_IRUGO
| S_IWUSR
| S_IWGRP
, mt_gpufreq_dir
);
1564 mt_entry
->read_proc
= mt_gpufreq_state_read
;
1565 mt_entry
->write_proc
= mt_gpufreq_state_write
;
1568 mt_entry
= create_proc_entry("gpufreq_sampling_rate", S_IRUGO
| S_IWUSR
| S_IWGRP
, mt_gpufreq_dir
);
1571 mt_entry
->read_proc
= mt_gpufreq_sampling_rate_read
;
1572 mt_entry
->write_proc
= mt_gpufreq_sampling_rate_write
;
1575 mt_entry
= create_proc_entry("gpufreq_opp_dump", S_IRUGO
, mt_gpufreq_dir
);
1578 mt_entry
->read_proc
= mt_gpufreq_opp_dump_read
;
1581 #ifdef GPU_CLOCK_RATIO
1582 mt_entry
= create_proc_entry("gpufreq_clock_on", S_IRUGO
| S_IWUSR
| S_IWGRP
, mt_gpufreq_dir
);
1585 mt_entry
->write_proc
= mt_gpufreq_clock_on_ratio_write
;
1589 mt_entry
= create_proc_entry("gpufreq_fix_frequency", S_IRUGO
| S_IWUSR
| S_IWGRP
, mt_gpufreq_dir
);
1592 mt_entry
->read_proc
= mt_gpufreq_fixed_frequency_read
;
1593 mt_entry
->write_proc
= mt_gpufreq_fixed_frequency_write
;
1596 mt_entry
= create_proc_entry("gpufreq_var_dump", S_IRUGO
| S_IWUSR
| S_IWGRP
, mt_gpufreq_dir
);
1599 mt_entry
->read_proc
= mt_gpufreq_var_dump
;
1603 clk_cfg_0
= DRV_Reg32(CLK_CFG_0
);
1604 clk_cfg_0
= (clk_cfg_0
& 0x00070000) >> 16;
1606 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreq_init, clk_cfg_0 = %d\n", clk_cfg_0
);
1611 g_cur_freq
= GPU_MMPLL_D3
;
1614 g_cur_freq
= GPU_SYSPLL_D2
;
1617 g_cur_freq
= GPU_MMPLL_D4
;
1620 g_cur_freq
= GPU_UNIVPLL1_D2
;
1623 g_cur_freq
= GPU_MMPLL_D5
;
1626 g_cur_freq
= GPU_SYSPLL_D3
;
1629 g_cur_freq
= GPU_MMPLL_D6
;
1632 g_cur_freq
= GPU_UNIVPLL1_D4
;
1638 g_cur_freq_init_keep
= g_cur_freq
;
1639 xlog_printk(ANDROID_LOG_INFO
, "Power/GPU_DVFS", "mt_gpufreq_init, g_cur_freq_init_keep = %d\n", g_cur_freq_init_keep
);
1644 static void __exit
mt_gpufreq_exit(void)
1649 module_init(mt_gpufreq_init
);
1650 module_exit(mt_gpufreq_exit
);
1652 MODULE_DESCRIPTION("MediaTek GPU Frequency Scaling driver");
1653 MODULE_LICENSE("GPL");