aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/vithar/kbase/src/platform/mali_kbase_dvfs.c
blob: a8f7c2efa3377c31430df4b698abc694a348046f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* drivers/gpu/vithar/kbase/src/platform/mali_kbase_dvfs.c
 *
 * Copyright 2011 by S.LSI. Samsung Electronics Inc.
 * San#24, Nongseo-Dong, Giheung-Gu, Yongin, Korea
 *
 * Samsung SoC Mali-T604 DVFS driver
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software FoundatIon.
 */

/**
 * @file mali_kbase_dvfs.c
 * DVFS
 */

#include <osk/mali_osk.h>
#include <kbase/src/common/mali_kbase.h>
#include <kbase/src/common/mali_kbase_uku.h>
#include <kbase/src/common/mali_kbase_mem.h>
#include <kbase/src/common/mali_midg_regmap.h>
#include <kbase/src/linux/mali_kbase_mem_linux.h>
#include <uk/mali_ukk.h>

#include <linux/module.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/platform_device.h>
#include <linux/pci.h>
#include <linux/miscdevice.h>
#include <linux/list.h>
#include <linux/semaphore.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/interrupt.h>
#include <linux/io.h>

#include <mach/map.h>
#include <linux/fb.h>
#include <linux/clk.h>
#include <mach/regs-clock.h>
#include <asm/delay.h>
#include <linux/regulator/consumer.h>
#include <linux/regulator/driver.h>

#include <kbase/src/platform/mali_kbase_dvfs.h>

#ifdef CONFIG_REGULATOR
static struct regulator *g3d_regulator=NULL;
static int mali_gpu_vol = 1050000; /* 1.05V  */
#endif

#ifdef CONFIG_VITHAR_DVFS

typedef struct _mali_dvfs_info{
	unsigned int voltage;
	unsigned int clock;
	int min_threshold;
	int	max_threshold;
}mali_dvfs_info;

typedef struct _mali_dvfs_status_type{
	kbase_device *kbdev;
	int step;
	int utilisation;
	int keepcnt;
	uint noutilcnt;
}mali_dvfs_status;

static struct workqueue_struct *mali_dvfs_wq = 0;
int mali_dvfs_control=0;
osk_spinlock mali_dvfs_spinlock;


/*dvfs status*/
static mali_dvfs_status mali_dvfs_status_current;
static const mali_dvfs_info mali_dvfs_infotbl[MALI_DVFS_STEP]=
{
#if (MALI_DVFS_STEP == 5)
	{900000, 100, 0, 25},
	{950000, 160, 20, 40},
	{1000000, 200, 35, 65},
	{1050000, 266, 55, 85},
	{1150000, 400, 70, 100}
#elif (MALI_DVFS_STEP == 4)
	{900000, 100, 0, 25},
	{950000, 160, 20, 40},
	{1000000, 200, 35, 65},
	{1050000, 266, 55, 100},
#else
#error no table
#endif
};

static void kbase_platform_dvfs_set_clock(kbase_device *kbdev, int freq)
{
	static int _freq = -1;
	unsigned long rate = 0;

	if(kbdev->sclk_g3d == 0)
		return;

	if (freq == _freq)
		return;

	switch(freq)
	{
	case 400:
		rate = 400000000;
		break;
	case 266:
		rate = 267000000;
		break;
	case 200:
		rate = 200000000;
		break;
	case 160:
		rate = 160000000;
		break;
	case 133:
		rate = 134000000;
		break;
	case 100:
		rate = 100000000;
		break;
	case 50:
		rate = 50000000;
		break;
	default:
		return;
	}

	_freq = freq;
	clk_set_rate(kbdev->sclk_g3d, rate);

#if MALI_DVFS_DEBUG
	printk("dvfs_set_clock %dMhz\n", freq);
#endif
	return;
}

static void kbase_platform_dvfs_set_vol(int vol)
{
	static int _vol = -1;

	if (_vol == vol)
		return;


	switch(vol)
	{
	case 1150000:
	case 1050000:
	case 1000000:
	case 950000:
	case 900000:
		kbase_platform_set_voltage(NULL, vol);
		break;
	default:
		return;
	}

	_vol = vol;

#if MALI_DVFS_DEBUG
	printk("dvfs_set_vol %dmV\n", vol);
#endif
	return;
}

static void mali_dvfs_event_proc(struct work_struct *w)
{
	mali_dvfs_status dvfs_status;

	osk_spinlock_lock(&mali_dvfs_spinlock);
	dvfs_status = mali_dvfs_status_current;
	osk_spinlock_unlock(&mali_dvfs_spinlock);

#if MALI_DVFS_START_MAX_STEP
	/*If no input is for longtime, first step will be max step. */
	if (dvfs_status.utilisation > 10 && dvfs_status.noutilcnt > 20) {
		dvfs_status.step=MALI_DVFS_STEP-2;
		dvfs_status.utilisation = 100;
	}
#endif

	if (dvfs_status.utilisation > mali_dvfs_infotbl[dvfs_status.step].max_threshold)
	{
		OSK_ASSERT(dvfs_status.step < MALI_DVFS_STEP);
		dvfs_status.step++;
		dvfs_status.keepcnt=0;
		kbase_platform_dvfs_set_vol(mali_dvfs_infotbl[dvfs_status.step].voltage);
		kbase_platform_dvfs_set_clock(mali_dvfs_status_current.kbdev, mali_dvfs_infotbl[dvfs_status.step].clock);
	}else if ((dvfs_status.step>0) &&
			(dvfs_status.utilisation < mali_dvfs_infotbl[dvfs_status.step].min_threshold)) {
		dvfs_status.keepcnt++;
		if (dvfs_status.keepcnt > MALI_DVFS_KEEP_STAY_CNT)
		{
			OSK_ASSERT(dvfs_status.step > 0);
			dvfs_status.step--;
			dvfs_status.keepcnt=0;
			kbase_platform_dvfs_set_clock(mali_dvfs_status_current.kbdev, mali_dvfs_infotbl[dvfs_status.step].clock);
			kbase_platform_dvfs_set_vol(mali_dvfs_infotbl[dvfs_status.step].voltage);
		}
	}else{
		dvfs_status.keepcnt=0;
	}

#if MALI_DVFS_START_MAX_STEP
	if (dvfs_status.utilisation == 0) {
		dvfs_status.noutilcnt++;
	} else {
		dvfs_status.noutilcnt=0;
	}
#endif

#if MALI_DVFS_DEBUG
	printk("[mali_dvfs] utilisation: %d  step: %d[%d,%d] cnt: %d\n",
			dvfs_status.utilisation, dvfs_status.step,
			mali_dvfs_infotbl[dvfs_status.step].min_threshold,
			mali_dvfs_infotbl[dvfs_status.step].max_threshold, dvfs_status.keepcnt);
#endif

	osk_spinlock_lock(&mali_dvfs_spinlock);
	mali_dvfs_status_current=dvfs_status;
	osk_spinlock_unlock(&mali_dvfs_spinlock);

}

static DECLARE_WORK(mali_dvfs_work, mali_dvfs_event_proc);

int kbase_platform_dvfs_event(struct kbase_device *kbdev, u32 utilisation)
{
	osk_spinlock_lock(&mali_dvfs_spinlock);
	mali_dvfs_status_current.utilisation = utilisation;
	osk_spinlock_unlock(&mali_dvfs_spinlock);
	queue_work_on(0, mali_dvfs_wq, &mali_dvfs_work);

	/*add error handle here*/
	return MALI_TRUE;
}

int kbase_platform_dvfs_get_control_status(void)
{
	return mali_dvfs_control;
}

int kbase_platform_dvfs_init(struct device *dev, int step)
{
	struct kbase_device *kbdev;
	kbdev = dev_get_drvdata(dev);

	/*default status
	add here with the right function to get initilization value.
	*/
	if (!mali_dvfs_wq)
		mali_dvfs_wq = create_singlethread_workqueue("mali_dvfs");

	osk_spinlock_init(&mali_dvfs_spinlock,OSK_LOCK_ORDER_PM_METRICS);

	/*add a error handling here*/
	osk_spinlock_lock(&mali_dvfs_spinlock);
	mali_dvfs_status_current.kbdev = kbdev;
	mali_dvfs_status_current.utilisation = 100;
	mali_dvfs_status_current.step = step;
	osk_spinlock_unlock(&mali_dvfs_spinlock);

	return MALI_TRUE;
}

void kbase_platform_dvfs_term(void)
{
	if (mali_dvfs_wq)
		destroy_workqueue(mali_dvfs_wq);

	mali_dvfs_wq = NULL;
}
#endif


int kbase_platform_regulator_init(struct device *dev)
{

#ifdef CONFIG_REGULATOR
    g3d_regulator = regulator_get(NULL, "vdd_g3d");
    if(IS_ERR(g3d_regulator))
    {
        printk("[kbase_platform_regulator_init] failed to get vithar regulator\n");
	return -1;
    }

    if(regulator_enable(g3d_regulator) != 0)
    {
        printk("[kbase_platform_regulator_init] failed to enable vithar regulator\n");
	return -1;
    }

    if(regulator_set_voltage(g3d_regulator, mali_gpu_vol, mali_gpu_vol) != 0)
    {
        printk("[kbase_platform_regulator_init] failed to set vithar operating voltage [%d]\n", mali_gpu_vol);
	return -1;
    }
#endif

    return 0;
}

int kbase_platform_regulator_disable(struct device *dev)
{
#ifdef CONFIG_REGULATOR
    if(!g3d_regulator)
    {
        printk("[kbase_platform_regulator_disable] g3d_regulator is not initialized\n");
	return -1;
    }

    if(regulator_disable(g3d_regulator) != 0)
    {
        printk("[kbase_platform_regulator_disable] failed to disable g3d regulator\n");
	return -1;
    }
#endif
    return 0;
}

int kbase_platform_regulator_enable(struct device *dev)
{
#ifdef CONFIG_REGULATOR
    if(!g3d_regulator)
    {
        printk("[kbase_platform_regulator_enable] g3d_regulator is not initialized\n");
	return -1;
    }

    if(regulator_enable(g3d_regulator) != 0)
    {
        printk("[kbase_platform_regulator_enable] failed to enable g3d regulator\n");
	return -1;
    }
#endif
    return 0;
}

int kbase_platform_get_default_voltage(struct device *dev, int *vol)
{
#ifdef CONFIG_REGULATOR
	*vol = mali_gpu_vol;
#else
	*vol = 0;
#endif
	return 0;
}

int kbase_platform_get_voltage(struct device *dev, int *vol)
{
#ifdef CONFIG_REGULATOR
    if(!g3d_regulator)
    {
        printk("[kbase_platform_get_voltage] g3d_regulator is not initialized\n");
	return -1;
    }

    *vol = regulator_get_voltage(g3d_regulator);
#else
    *vol = 0;
#endif
    return 0;
}

int kbase_platform_set_voltage(struct device *dev, int vol)
{
#ifdef CONFIG_REGULATOR
    if(!g3d_regulator)
    {
        printk("[kbase_platform_set_voltage] g3d_regulator is not initialized\n");
	return -1;
    }

    if(regulator_set_voltage(g3d_regulator, vol, vol) != 0)
    {
        printk("[kbase_platform_set_voltage] failed to set voltage\n");
	return -1;
    }
#endif
    return 0;
}