summaryrefstree
path: root/kernel/drivers/media/platform/exynos/fimc-is/fimc-is-dvfs.c
blob: 4aac771704de59b42e37a04b16e1352075171b56 (plain)
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/*
 * Samsung Exynos5 SoC series FIMC-IS driver
 *
 * exynos5 fimc-is core functions
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd
 *
 * 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.
 */

#include <linux/slab.h>
#include "fimc-is-core.h"
#include "fimc-is-dvfs.h"

extern struct pm_qos_request exynos_isp_qos_cpu_min;
extern struct pm_qos_request exynos_isp_qos_cpu_max;
extern struct pm_qos_request exynos_isp_qos_int;
extern struct pm_qos_request exynos_isp_qos_mem;
extern struct pm_qos_request exynos_isp_qos_cam;
extern struct pm_qos_request exynos_isp_qos_disp;

DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_DUAL_CAPTURE);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_DUAL_CAMCORDING);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_DUAL_PREVIEW);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_HIGH_SPEED_FPS);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAMCORDING_FHD_BNS_OFF);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAMCORDING_FHD);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAMCORDING_WHD);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAMCORDING_UHD);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_PREVIEW_FHD_BNS_OFF);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_PREVIEW_FHD);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_PREVIEW_WHD);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_PREVIEW_UHD);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_FRONT_VT1);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_FRONT_VT2);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_FRONT_PREVIEW);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAPTURE);
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_DIS_ENABLE);

#if defined(ENABLE_DVFS)
/*
 * Static Scenario Set
 * You should describe static scenario by priorities of scenario.
 * And you should name array 'static_scenarios'
 */
static struct fimc_is_dvfs_scenario static_scenarios[] = {
	{
		.scenario_id		= FIMC_IS_SN_DUAL_CAMCORDING,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_DUAL_CAMCORDING),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_DUAL_CAMCORDING),
	}, {
		.scenario_id		= FIMC_IS_SN_DUAL_PREVIEW,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_DUAL_PREVIEW),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_DUAL_PREVIEW),
	}, {
		.scenario_id		= FIMC_IS_SN_HIGH_SPEED_FPS,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_HIGH_SPEED_FPS),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_HIGH_SPEED_FPS),
	}, {
		.scenario_id		= FIMC_IS_SN_REAR_CAMCORDING_FHD_BNS_OFF,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_REAR_CAMCORDING_FHD_BNS_OFF),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAMCORDING_FHD_BNS_OFF),
	}, {
		.scenario_id		= FIMC_IS_SN_REAR_CAMCORDING_FHD,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_REAR_CAMCORDING_FHD),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAMCORDING_FHD),
	}, {
		.scenario_id		= FIMC_IS_SN_REAR_CAMCORDING_WHD,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_REAR_CAMCORDING_WHD),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAMCORDING_WHD),
	}, {
		.scenario_id		= FIMC_IS_SN_REAR_CAMCORDING_UHD,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_REAR_CAMCORDING_UHD),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAMCORDING_UHD),
	}, {
		.scenario_id		= FIMC_IS_SN_REAR_PREVIEW_FHD_BNS_OFF,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_REAR_PREVIEW_FHD_BNS_OFF),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_PREVIEW_FHD_BNS_OFF),
	}, {
		.scenario_id		= FIMC_IS_SN_REAR_PREVIEW_FHD,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_REAR_PREVIEW_FHD),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_PREVIEW_FHD),
	}, {
		.scenario_id		= FIMC_IS_SN_REAR_PREVIEW_WHD,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_REAR_PREVIEW_WHD),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_PREVIEW_WHD),
	}, {
		.scenario_id		= FIMC_IS_SN_REAR_PREVIEW_UHD,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_REAR_PREVIEW_UHD),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_PREVIEW_UHD),
	}, {
		.scenario_id		= FIMC_IS_SN_FRONT_VT1,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_FRONT_VT1),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_FRONT_VT1),
	}, {
		.scenario_id		= FIMC_IS_SN_FRONT_VT2,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_FRONT_VT2),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_FRONT_VT2),
	}, {
		.scenario_id		= FIMC_IS_SN_FRONT_PREVIEW,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_FRONT_PREVIEW),
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_FRONT_PREVIEW),
	},
};

/*
 * Dynamic Scenario Set
 * You should describe static scenario by priorities of scenario.
 * And you should name array 'dynamic_scenarios'
 */
static struct fimc_is_dvfs_scenario dynamic_scenarios[] = {
	{
		.scenario_id		= FIMC_IS_SN_DUAL_CAPTURE,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_DUAL_CAPTURE),
		.keep_frame_tick	= KEEP_FRAME_TICK_DEFAULT,
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_DUAL_CAPTURE),
	},
	{
		.scenario_id		= FIMC_IS_SN_REAR_CAPTURE,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_REAR_CAPTURE),
		.keep_frame_tick	= KEEP_FRAME_TICK_DEFAULT,
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAPTURE),
	},
	{
		.scenario_id		= FIMC_IS_SN_DIS_ENABLE,
		.scenario_nm		= DVFS_SN_STR(FIMC_IS_SN_DIS_ENABLE),
		.keep_frame_tick	= KEEP_FRAME_TICK_DEFAULT,
		.check_func		= GET_DVFS_CHK_FUNC(FIMC_IS_SN_DIS_ENABLE),
	},
};
#else
/*
 * Default Scenario can not be seleted, this declaration is for static variable.
 */
static struct fimc_is_dvfs_scenario static_scenarios[] = {
	{
		.scenario_id		= FIMC_IS_SN_DEFAULT,
		.scenario_nm		= NULL,
		.keep_frame_tick	= 0,
		.check_func		= NULL,
	},
};
static struct fimc_is_dvfs_scenario dynamic_scenarios[] = {
	{
		.scenario_id		= FIMC_IS_SN_DEFAULT,
		.scenario_nm		= NULL,
		.keep_frame_tick	= 0,
		.check_func		= NULL,
	},
};
#endif

static inline int fimc_is_get_open_sensor_cnt(struct fimc_is_core *core)
{
	int i, sensor_cnt = 0;

	for (i = 0; i < FIMC_IS_MAX_NODES; i++)
		if (test_bit(FIMC_IS_SENSOR_OPEN, &(core->sensor[i].state)))
			sensor_cnt++;

	return sensor_cnt;
}

static inline u32 fimc_is_chk_req(struct fimc_is_frame *frame, enum fimc_is_video_dev_num vid)
{
	int i;
	struct camera2_node *node;
	u32 ret = 0;

	if (frame == NULL)
		return ret;

	for (i = 0; i < CAPTURE_NODE_MAX; i++) {
		node = &frame->shot_ext->node_group.capture[i];
		if (node->vid == vid) {
			ret = node->request;
			break;
		}
	}

	return ret;
}

/* dual capture */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_DUAL_CAPTURE)
{
	struct fimc_is_core *core;
	int sensor_cnt = 0;
	core = (struct fimc_is_core *)device->interface->core;
	sensor_cnt = fimc_is_get_open_sensor_cnt(core);

	if ((sensor_cnt >= 2) &&
			(test_bit(FIMC_IS_ISCHAIN_REPROCESSING, &device->state)))
		return 1;
	else
		return 0;
}

/* dual camcording */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_DUAL_CAMCORDING)
{
	struct fimc_is_core *core;
	int sensor_cnt = 0;
	core = (struct fimc_is_core *)device->interface->core;
	sensor_cnt = fimc_is_get_open_sensor_cnt(core);

	if ((sensor_cnt >= 2) &&
			((device->setfile & FIMC_IS_SETFILE_MASK) \
			 == ISS_SUB_SCENARIO_DUAL_VIDEO))
		return 1;
	else
		return 0;
}

/* dual preview */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_DUAL_PREVIEW)
{
	struct fimc_is_core *core;
	int sensor_cnt = 0;
	core = (struct fimc_is_core *)device->interface->core;
	sensor_cnt = fimc_is_get_open_sensor_cnt(core);

	if ((sensor_cnt >= 2) &&
			((device->setfile & FIMC_IS_SETFILE_MASK) \
			 != ISS_SUB_SCENARIO_DUAL_VIDEO))
		return 1;
	else
		return 0;
}

/* high speed fps */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_HIGH_SPEED_FPS)
{
	if ((device->sensor->pdev->id == SENSOR_POSITION_REAR) &&
			(fimc_is_sensor_g_framerate(device->sensor) > 30))
		return 1;
	else
		return 0;
}

/* rear camcording FHD with BNS off */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAMCORDING_FHD_BNS_OFF)
{
	u32 mask = (device->setfile & FIMC_IS_SETFILE_MASK);
	bool setfile_flag = (mask == ISS_SUB_SCENARIO_VIDEO) ||
			(mask == ISS_SUB_SCENARIO_VIDEO_WDR);

	if ((device->sensor->pdev->id == SENSOR_POSITION_REAR) &&
			(fimc_is_sensor_g_framerate(device->sensor) <= 30) &&
			(device->chain3_width * device->chain3_height <= SIZE_FHD) &&
			(fimc_is_sensor_g_bns_ratio(device->sensor) <= 1000) &&
			setfile_flag)
		return 1;
	else
		return 0;
}

/* rear camcording FHD*/
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAMCORDING_FHD)
{
	u32 mask = (device->setfile & FIMC_IS_SETFILE_MASK);
	bool setfile_flag = (mask == ISS_SUB_SCENARIO_VIDEO) ||
			(mask == ISS_SUB_SCENARIO_VIDEO_WDR);

	if ((device->sensor->pdev->id == SENSOR_POSITION_REAR) &&
			(fimc_is_sensor_g_framerate(device->sensor) <= 30) &&
			(device->chain3_width * device->chain3_height <= SIZE_FHD) &&
			(fimc_is_sensor_g_bns_ratio(device->sensor) > 1000) &&
			setfile_flag)
		return 1;
	else
		return 0;
}

/* rear camcording WHD*/
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAMCORDING_WHD)
{
	u32 mask = (device->setfile & FIMC_IS_SETFILE_MASK);
	bool setfile_flag = (mask == ISS_SUB_SCENARIO_VIDEO) ||
			(mask == ISS_SUB_SCENARIO_VIDEO_WDR);

	if ((device->sensor->pdev->id == SENSOR_POSITION_REAR) &&
			(fimc_is_sensor_g_framerate(device->sensor) <= 30) &&
			(device->chain3_width * device->chain3_height > SIZE_FHD) &&
			(device->chain3_width * device->chain3_height <= SIZE_WHD) &&
			setfile_flag)
		return 1;
	else
		return 0;
}

/* rear camcording UHD*/
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAMCORDING_UHD)
{
	u32 mask = (device->setfile & FIMC_IS_SETFILE_MASK);
	bool setfile_flag = (mask == ISS_SUB_SCENARIO_UHD_30FPS) ||
			(mask == ISS_SUB_SCENARIO_UHD_30FPS_WDR);

	if ((device->sensor->pdev->id == SENSOR_POSITION_REAR) &&
			(fimc_is_sensor_g_framerate(device->sensor) <= 30) &&
			(device->chain3_width * device->chain3_height > SIZE_WHD) &&
			(device->chain3_width * device->chain3_height <= SIZE_UHD) &&
			setfile_flag)
		return 1;
	else
		return 0;
}

/* rear preview FHD with BNS off */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_PREVIEW_FHD_BNS_OFF)
{
	if ((device->sensor->pdev->id == SENSOR_POSITION_REAR) &&
			(fimc_is_sensor_g_framerate(device->sensor) <= 30) &&
			(device->chain3_width * device->chain3_height <= SIZE_FHD) &&
			(fimc_is_sensor_g_bns_ratio(device->sensor) <= 1000) &&
			((device->setfile & FIMC_IS_SETFILE_MASK) \
			 != ISS_SUB_SCENARIO_VIDEO))

		return 1;
	else
		return 0;
}

/* rear preview FHD */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_PREVIEW_FHD)
{
	if ((device->sensor->pdev->id == SENSOR_POSITION_REAR) &&
			(fimc_is_sensor_g_framerate(device->sensor) <= 30) &&
			(device->chain3_width * device->chain3_height <= SIZE_FHD) &&
			(fimc_is_sensor_g_bns_ratio(device->sensor) > 1000) &&
			((device->setfile & FIMC_IS_SETFILE_MASK) \
			 != ISS_SUB_SCENARIO_VIDEO))

		return 1;
	else
		return 0;
}

/* rear preview WHD */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_PREVIEW_WHD)
{
	if ((device->sensor->pdev->id == SENSOR_POSITION_REAR) &&
			(fimc_is_sensor_g_framerate(device->sensor) <= 30) &&
			(device->chain3_width * device->chain3_height > SIZE_FHD) &&
			(device->chain3_width * device->chain3_height <= SIZE_WHD) &&
			((device->setfile & FIMC_IS_SETFILE_MASK) \
			 != ISS_SUB_SCENARIO_VIDEO))
		return 1;
	else
		return 0;
}

/* rear preview UHD */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_PREVIEW_UHD)
{
	if ((device->sensor->pdev->id == SENSOR_POSITION_REAR) &&
			(fimc_is_sensor_g_framerate(device->sensor) <= 30) &&
			(device->chain3_width * device->chain3_height > SIZE_WHD) &&
			((device->setfile & FIMC_IS_SETFILE_MASK) \
			 != ISS_SUB_SCENARIO_VIDEO))
		return 1;
	else
		return 0;
}

/* front vt1 */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_FRONT_VT1)
{
	if ((device->sensor->pdev->id == SENSOR_POSITION_FRONT) &&
			((device->setfile & FIMC_IS_SETFILE_MASK) \
			 == ISS_SUB_SCENARIO_FRONT_VT1))
		return 1;
	else
		return 0;
}

/* front vt2 */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_FRONT_VT2)
{
	if ((device->sensor->pdev->id == SENSOR_POSITION_FRONT) &&
			((device->setfile & FIMC_IS_SETFILE_MASK) \
			 == ISS_SUB_SCENARIO_FRONT_VT2))
		return 1;
	else
		return 0;
}

/* front preview */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_FRONT_PREVIEW)
{
	if ((device->sensor->pdev->id == SENSOR_POSITION_FRONT) &&
			!(((device->setfile & FIMC_IS_SETFILE_MASK) \
					== ISS_SUB_SCENARIO_FRONT_VT1) ||
				((device->setfile & FIMC_IS_SETFILE_MASK) \
				 == ISS_SUB_SCENARIO_FRONT_VT2)))
		return 1;
	else
		return 0;
}

/* rear capture */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_REAR_CAPTURE)
{
	if ((device->sensor->pdev->id == SENSOR_POSITION_REAR) &&
			((test_bit(FIMC_IS_ISCHAIN_REPROCESSING, &device->state)) ||
			fimc_is_chk_req(frame, FIMC_IS_VIDEO_SCC_NUM)))
		return 1;
	else
		return 0;
}

/* dis */
DECLARE_DVFS_CHK_FUNC(FIMC_IS_SN_DIS_ENABLE)
{
	if (test_bit(FIMC_IS_SUBDEV_START, &device->dis.state))
		return 1;
	else
		return 0;
}

static int fimc_is_set_pwm(struct fimc_is_device_ischain *device, u32 pwm_qos)
{
	int ret = 0;
	u32 base_addr;
	void __iomem *addr;

	base_addr = GET_FIMC_IS_ADDR_OF_SUBIP2(device, pwm);

	if (base_addr) {
		addr = ioremap(base_addr + FIMC_IS_PWM_TCNTB0, SZ_4);
		writel(pwm_qos, addr);
		dbg("PWM SFR Read(%08X), pwm_qos(%08X)\n", readl(addr), pwm_qos);
		iounmap(addr);
	}

	return ret;
}

int fimc_is_dvfs_init(struct fimc_is_resourcemgr *resourcemgr)
{
	int i;
	pr_info("%s\n",	__func__);

	BUG_ON(!resourcemgr);

	resourcemgr->dvfs_ctrl.cur_cpu_min_qos = 0;
	resourcemgr->dvfs_ctrl.cur_cpu_max_qos = 0;
	resourcemgr->dvfs_ctrl.cur_int_qos = 0;
	resourcemgr->dvfs_ctrl.cur_mif_qos = 0;
	resourcemgr->dvfs_ctrl.cur_cam_qos = 0;
	resourcemgr->dvfs_ctrl.cur_i2c_qos = 0;
	resourcemgr->dvfs_ctrl.cur_disp_qos = 0;

	/* init spin_lock for clock gating */
	mutex_init(&resourcemgr->dvfs_ctrl.lock);

	if (!(resourcemgr->dvfs_ctrl.static_ctrl))
		resourcemgr->dvfs_ctrl.static_ctrl =
			kzalloc(sizeof(struct fimc_is_dvfs_scenario_ctrl), GFP_KERNEL);
	if (!(resourcemgr->dvfs_ctrl.dynamic_ctrl))
		resourcemgr->dvfs_ctrl.dynamic_ctrl =
			kzalloc(sizeof(struct fimc_is_dvfs_scenario_ctrl), GFP_KERNEL);

	if (!resourcemgr->dvfs_ctrl.static_ctrl || !resourcemgr->dvfs_ctrl.dynamic_ctrl) {
		err("dvfs_ctrl alloc is failed!!\n");
		return -ENOMEM;
	}

	/* set priority by order */
	for (i = 0; i < ARRAY_SIZE(static_scenarios); i++)
		static_scenarios[i].priority = i;
	for (i = 0; i < ARRAY_SIZE(dynamic_scenarios); i++)
		dynamic_scenarios[i].priority = i;

	resourcemgr->dvfs_ctrl.static_ctrl->cur_scenario_id	= -1;
	resourcemgr->dvfs_ctrl.static_ctrl->cur_scenario_idx	= -1;
	resourcemgr->dvfs_ctrl.static_ctrl->scenarios		= static_scenarios;
	if (static_scenarios[0].scenario_id == FIMC_IS_SN_DEFAULT)
		resourcemgr->dvfs_ctrl.static_ctrl->scenario_cnt	= 0;
	else
		resourcemgr->dvfs_ctrl.static_ctrl->scenario_cnt	= ARRAY_SIZE(static_scenarios);

	resourcemgr->dvfs_ctrl.dynamic_ctrl->cur_scenario_id	= -1;
	resourcemgr->dvfs_ctrl.dynamic_ctrl->cur_scenario_idx	= -1;
	resourcemgr->dvfs_ctrl.dynamic_ctrl->cur_frame_tick	= -1;
	resourcemgr->dvfs_ctrl.dynamic_ctrl->scenarios		= dynamic_scenarios;
	if (static_scenarios[0].scenario_id == FIMC_IS_SN_DEFAULT)
		resourcemgr->dvfs_ctrl.dynamic_ctrl->scenario_cnt	= 0;
	else
		resourcemgr->dvfs_ctrl.dynamic_ctrl->scenario_cnt	= ARRAY_SIZE(dynamic_scenarios);

	/* default value is 0 */
	resourcemgr->dvfs_ctrl.dvfs_table_idx = 0;

	return 0;
}

int fimc_is_dvfs_sel_scenario(u32 type, struct fimc_is_device_ischain *device, struct fimc_is_frame *frame)
{
	struct fimc_is_dvfs_ctrl *dvfs_ctrl;
	struct fimc_is_dvfs_scenario_ctrl *static_ctrl, *dynamic_ctrl;
	struct fimc_is_dvfs_scenario *scenarios;
	struct fimc_is_dvfs_scenario *cur_scenario;
	struct fimc_is_resourcemgr *resourcemgr;
	int i, scenario_id, scenario_cnt;

	if (device == NULL) {
		err("device is NULL\n");
		return -EINVAL;
	}

	resourcemgr = device->resourcemgr;
	dvfs_ctrl = &(resourcemgr->dvfs_ctrl);
	static_ctrl = dvfs_ctrl->static_ctrl;
	dynamic_ctrl = dvfs_ctrl->dynamic_ctrl;

	if (type == FIMC_IS_DYNAMIC_SN) {
		/* dynamic scenario */
		if (!dynamic_ctrl) {
			err("dynamic_dvfs_ctrl is NULL\n");
			return -EINVAL;
		}

		if (dynamic_ctrl->scenario_cnt == 0) {
			pr_debug("dynamic_scenario's count is jero\n");
			return -EINVAL;
		}

		scenarios = dynamic_ctrl->scenarios;
		scenario_cnt = dynamic_ctrl->scenario_cnt;

		if (dynamic_ctrl->cur_frame_tick >= 0) {
			(dynamic_ctrl->cur_frame_tick)--;
			/*
			 * when cur_frame_tick is lower than 0, clear current scenario.
			 * This means that current frame tick to keep dynamic scenario
			 * was expired.
			 */
			if (dynamic_ctrl->cur_frame_tick < 0) {
				dynamic_ctrl->cur_scenario_id = -1;
				dynamic_ctrl->cur_scenario_idx = -1;
			}
		}
	} else {
		/* static scenario */
		if (!static_ctrl) {
			err("static_dvfs_ctrl is NULL\n");
			return -EINVAL;
		}

		if (static_ctrl->scenario_cnt == 0) {
			pr_debug("static_scenario's count is jero\n");
			return -EINVAL;
		}

		scenarios = static_ctrl->scenarios;
		scenario_cnt = static_ctrl->scenario_cnt;
	}

	for (i = 0; i < scenario_cnt; i++) {
		if (!scenarios[i].check_func) {
			warn("check_func[%d] is NULL\n", i);
			continue;
		}

		if ((scenarios[i].check_func(device, frame)) > 0) {
			scenario_id = scenarios[i].scenario_id;

			if (type == FIMC_IS_DYNAMIC_SN) {
				cur_scenario = &scenarios[dynamic_ctrl->cur_scenario_idx];

				/*
				 * if condition 1 or 2 is true
				 * condition 1 : There's no dynamic scenario applied.
				 * condition 2 : Finded scenario's prority was higher than current
				 */
				if ((dynamic_ctrl->cur_scenario_id <= 0) ||
						(scenarios[i].priority < (cur_scenario->priority))) {
					dynamic_ctrl->cur_scenario_id = scenarios[i].scenario_id;
					dynamic_ctrl->cur_scenario_idx = i;
					dynamic_ctrl->cur_frame_tick = scenarios[i].keep_frame_tick;
				} else {
					/* if finded scenario is same */
					if (scenarios[i].priority == (cur_scenario->priority))
						dynamic_ctrl->cur_frame_tick = scenarios[i].keep_frame_tick;
					return -EAGAIN;
				}
			} else {
				static_ctrl->cur_scenario_id = scenario_id;
				static_ctrl->cur_scenario_idx = i;
				static_ctrl->cur_frame_tick = scenarios[i].keep_frame_tick;
			}

			return scenario_id;
		}
	}

	if (type == FIMC_IS_DYNAMIC_SN)
		return -EAGAIN;

	{
		struct fimc_is_core *core;
		int sensor_cnt = 0;
		core = (struct fimc_is_core *)device->interface->core;
		sensor_cnt = fimc_is_get_open_sensor_cnt(core);

		warn("couldn't find static dvfs scenario [sensor:(%d/%d)/fps:%d/setfile:%d/scp size:(%d/%d)]\n",
			sensor_cnt,
			device->sensor->pdev->id,
			fimc_is_sensor_g_framerate(device->sensor),
			(device->setfile & FIMC_IS_SETFILE_MASK),
			device->chain3_width,
			device->chain3_height);
	}

	static_ctrl->cur_scenario_id = FIMC_IS_SN_DEFAULT;
	static_ctrl->cur_scenario_idx = -1;
	static_ctrl->cur_frame_tick = -1;

	return FIMC_IS_SN_DEFAULT;
}

int fimc_is_get_qos(struct fimc_is_core *core, u32 type, u32 scenario_id)
{
	struct exynos_platform_fimc_is	*pdata = NULL;
	int qos = 0;
	int dvfs_idx = core->resourcemgr.dvfs_ctrl.dvfs_table_idx;

	pdata = core->pdata;
	if (pdata == NULL) {
		err("pdata is NULL\n");
		return -EINVAL;
	}

	if (!pdata->get_int_qos || !pdata->get_mif_qos)
		goto struct_qos;

	switch (type) {
		case FIMC_IS_DVFS_INT:
			qos = pdata->get_int_qos(scenario_id);
			break;
		case FIMC_IS_DVFS_MIF:
			qos = pdata->get_mif_qos(scenario_id);
			break;
		case FIMC_IS_DVFS_I2C:
			if (pdata->get_i2c_qos)
				qos = pdata->get_i2c_qos(scenario_id);
			break;
	}
	goto exit;

struct_qos:
	if (max(0, (int)type) >= FIMC_IS_DVFS_END) {
		err("Cannot find DVFS value");
		return -EINVAL;
	}

	if (dvfs_idx < 0 || dvfs_idx > FIMC_IS_DVFS_TABLE_IDX_MAX) {
		err("invalid dvfs index(%d)", dvfs_idx);
		dvfs_idx = 0;
	}

	qos = pdata->dvfs_data[dvfs_idx][scenario_id][type];

exit:
	return qos;
}

int fimc_is_set_dvfs(struct fimc_is_device_ischain *device, u32 scenario_id)
{
	int ret = 0;
	int cpu_min_qos, cpu_max_qos, int_qos, mif_qos, i2c_qos, cam_qos, disp_qos, pwm_qos = 0;
	int refcount;
	struct fimc_is_core *core;
	struct fimc_is_resourcemgr *resourcemgr;
	struct fimc_is_dvfs_ctrl *dvfs_ctrl;

	if (device == NULL) {
		err("device is NULL\n");
		return -EINVAL;
	}

	core = (struct fimc_is_core *)device->interface->core;
	resourcemgr = device->resourcemgr;
	dvfs_ctrl = &(resourcemgr->dvfs_ctrl);

	refcount = atomic_read(&core->video_isp.refcount);
	if (refcount < 0) {
		err("invalid ischain refcount");
		goto exit;
	}

	cpu_min_qos = fimc_is_get_qos(core, FIMC_IS_DVFS_CPU_MIN, scenario_id);
	cpu_max_qos = fimc_is_get_qos(core, FIMC_IS_DVFS_CPU_MAX, scenario_id);
	int_qos = fimc_is_get_qos(core, FIMC_IS_DVFS_INT, scenario_id);
	mif_qos = fimc_is_get_qos(core, FIMC_IS_DVFS_MIF, scenario_id);
	i2c_qos = fimc_is_get_qos(core, FIMC_IS_DVFS_I2C, scenario_id);
	cam_qos = fimc_is_get_qos(core, FIMC_IS_DVFS_CAM, scenario_id);
	disp_qos = fimc_is_get_qos(core, FIMC_IS_DVFS_DISP, scenario_id);
	pwm_qos = fimc_is_get_qos(core, FIMC_IS_DVFS_PWM, scenario_id);

	if ((int_qos < 0) || (mif_qos < 0) || (i2c_qos < 0)
	|| (cam_qos < 0) || (disp_qos < 0) || (pwm_qos < 0)) {
		err("getting qos value is failed!!\n");
		return -EINVAL;
	}

	if (dvfs_ctrl->cur_cpu_min_qos != cpu_min_qos) {
		pm_qos_update_request(&exynos_isp_qos_cpu_min, cpu_min_qos);
		dvfs_ctrl->cur_cpu_min_qos = cpu_min_qos;
	}

	if (dvfs_ctrl->cur_cpu_max_qos != cpu_max_qos) {
		pm_qos_update_request(&exynos_isp_qos_cpu_max, cpu_max_qos);
		dvfs_ctrl->cur_cpu_max_qos = cpu_max_qos;
	}

	/* check current qos */
	if (int_qos && dvfs_ctrl->cur_int_qos != int_qos) {
		if (i2c_qos) {
			ret = fimc_is_itf_i2c_lock(device, i2c_qos, true);
			if (ret) {
				err("fimc_is_itf_i2_clock fail\n");
				goto exit;
			}
		}

		if (pwm_qos) {
			fimc_is_set_pwm(device, pwm_qos);
			if (ret) {
				err("fimc_is_set_pwm fail\n");
				goto exit;
			}
		}

		pm_qos_update_request(&exynos_isp_qos_int, int_qos);
		dvfs_ctrl->cur_int_qos = int_qos;

		if (i2c_qos) {
			/* i2c unlock */
			ret = fimc_is_itf_i2c_lock(device, i2c_qos, false);
			if (ret) {
				err("fimc_is_itf_i2c_unlock fail\n");
				goto exit;
			}
		}
	}

	if (mif_qos && dvfs_ctrl->cur_mif_qos != mif_qos) {
		pm_qos_update_request(&exynos_isp_qos_mem, mif_qos);
		dvfs_ctrl->cur_mif_qos = mif_qos;
	}

	if (cam_qos && dvfs_ctrl->cur_cam_qos != cam_qos) {
		pm_qos_update_request(&exynos_isp_qos_cam, cam_qos);
		dvfs_ctrl->cur_cam_qos = cam_qos;
	}

	if (disp_qos && dvfs_ctrl->cur_disp_qos != disp_qos) {
		pm_qos_update_request(&exynos_isp_qos_disp, disp_qos);
		dvfs_ctrl->cur_disp_qos = disp_qos;
	}

	dbg("[RSC:%d]: New QoS [INT(%d), MIF(%d), CAM(%d), DISP(%d), I2C(%d), PWM(%d) CPU(%d/%d)]\n",
			device->instance, int_qos, mif_qos,
			cam_qos, disp_qos, i2c_qos, pwm_qos, cpu_min_qos, cpu_max_qos);
exit:
	return ret;
}