summaryrefstree
path: root/kernel/drivers/media/platform/exynos/camera/is-dvfs.c
blob: 69e612881a93325a934bdf71f327ab4f1797d1d9 (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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/*
 * 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>
#if defined(CONFIG_SCHED_EHMP)
#include <linux/ehmp.h>
#elif defined(CONFIG_SCHED_EMS)
#include <linux/ems.h>
#endif
#include "is-core.h"
#include "is-dvfs.h"
#include "is-hw-dvfs.h"
#include <linux/videodev2_exynos_camera.h>

#ifdef CONFIG_PM_DEVFREQ

#if defined(QOS_INTCAM)
extern struct pm_qos_request exynos_isp_qos_int_cam;
#endif
#if defined(QOS_TNR)
extern struct pm_qos_request exynos_isp_qos_tnr;
#endif
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_hpg;

#if defined(CONFIG_SCHED_EHMP) || defined(CONFIG_SCHED_EMS)
#if defined(CONFIG_SCHED_EMS_TUNE)
extern struct emstune_mode_request emstune_req;
#else
extern struct gb_qos_request gb_req;
#endif
#endif

static inline int is_get_start_sensor_cnt(struct is_core *core)
{
	int i, sensor_cnt = 0;

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

	return sensor_cnt;
}

#ifdef BDS_DVFS
static int is_get_bds_size(struct is_device_ischain *device, struct is_dual_info *dual_info)
{
	int resol = 0;
	struct is_group *group;

	group = &device->group_3aa;

	if (!test_bit(IS_GROUP_INIT, &group->state))
		return resol;

	if (dual_info->mode == IS_DUAL_MODE_SYNC)
		resol = dual_info->max_bds_width * dual_info->max_bds_height;
	else
		resol = device->txp.output.width * device->txp.output.height;

	return resol;
}
#else
static int is_get_target_resol(struct is_device_ischain *device)
{
	int resol = 0;
#ifdef SOC_MCS
	int i = 0;
	struct is_group *group;

	group = &device->group_mcs;

	if (!test_bit(IS_GROUP_INIT, &group->state))
		return resol;

	for (i = ENTRY_M0P; i <= ENTRY_M4P; i++)
		if (group->subdev[i] && test_bit(IS_SUBDEV_START, &(group->subdev[i]->state)))
			resol = max_t(int, resol, group->subdev[i]->output.width * group->subdev[i]->output.height);
#else
	resol = device->scp.output.width * device->scp.output.height;
#endif
	return resol;
}
#endif

bool is_dvfs_is_fast_ae(struct is_dvfs_ctrl *dvfs_ctrl)
{
	return (dvfs_ctrl->static_ctrl->cur_scenario_id != IS_SN_PREVIEW_HIGH_SPEED_FPS
			&& dvfs_ctrl->static_ctrl->cur_scenario_id != IS_SN_FRONT_PREVIEW_HIGH_SPEED_FPS
			&& dvfs_ctrl->dynamic_ctrl->cur_scenario_id == IS_SN_PREVIEW_HIGH_SPEED_FPS
			&& dvfs_ctrl->dynamic_ctrl->cur_frame_tick > 0);
}

int is_dvfs_init(struct is_resourcemgr *resourcemgr)
{
	int ret = 0;
	struct is_dvfs_ctrl *dvfs_ctrl;

	FIMC_BUG(!resourcemgr);

	dvfs_ctrl = &resourcemgr->dvfs_ctrl;

#if defined(QOS_INTCAM)
	dvfs_ctrl->cur_int_cam_qos = 0;
#endif
#if defined(QOS_TNR)
	dvfs_ctrl->cur_tnr_qos = 0;
#endif
	dvfs_ctrl->cur_int_qos = 0;
	dvfs_ctrl->cur_mif_qos = 0;
	dvfs_ctrl->cur_cam_qos = 0;
	dvfs_ctrl->cur_i2c_qos = 0;
	dvfs_ctrl->cur_disp_qos = 0;
	dvfs_ctrl->cur_hpg_qos = 0;
	dvfs_ctrl->cur_hmp_bst = 0;

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

	if (!(dvfs_ctrl->static_ctrl))
		dvfs_ctrl->static_ctrl =
			kzalloc(sizeof(struct is_dvfs_scenario_ctrl), GFP_KERNEL);
	if (!(dvfs_ctrl->dynamic_ctrl))
		dvfs_ctrl->dynamic_ctrl =
			kzalloc(sizeof(struct is_dvfs_scenario_ctrl), GFP_KERNEL);
	if (!(dvfs_ctrl->external_ctrl))
		dvfs_ctrl->external_ctrl =
			kzalloc(sizeof(struct is_dvfs_scenario_ctrl), GFP_KERNEL);

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

	/* assign static / dynamic scenario check logic data */
	ret = is_hw_dvfs_init((void *)dvfs_ctrl);
	if (ret) {
		err("is_hw_dvfs_init is failed(%d)\n", ret);
		return -EINVAL;
	}

	/* default value is 0 */
	dvfs_ctrl->dvfs_table_idx = 0;
	clear_bit(IS_DVFS_SEL_TABLE, &dvfs_ctrl->state);

	return 0;
}

int is_dvfs_sel_table(struct is_resourcemgr *resourcemgr)
{
	int ret = 0;
	struct is_dvfs_ctrl *dvfs_ctrl;
	u32 dvfs_table_idx = 0;

	FIMC_BUG(!resourcemgr);

	dvfs_ctrl = &resourcemgr->dvfs_ctrl;

	if (test_bit(IS_DVFS_SEL_TABLE, &dvfs_ctrl->state))
		return 0;

#if defined(EXPANSION_DVFS_TABLE)
	switch(resourcemgr->hal_version) {
	case IS_HAL_VER_1_0:
		dvfs_table_idx = 0;
		break;
	case IS_HAL_VER_3_2:
		dvfs_table_idx = 1;
		break;
	default:
		err("hal version is unknown");
		dvfs_table_idx = 0;
		ret = -EINVAL;
		break;
	}
#endif

	if (dvfs_table_idx >= dvfs_ctrl->dvfs_table_max) {
		err("dvfs index(%d) is invalid", dvfs_table_idx);
		ret = -EINVAL;
		goto p_err;
	}

	resourcemgr->dvfs_ctrl.dvfs_table_idx = dvfs_table_idx;
	set_bit(IS_DVFS_SEL_TABLE, &dvfs_ctrl->state);

p_err:
	info("[RSC] %s(%d):%d\n", __func__, dvfs_table_idx, ret);
	return ret;
}

int is_dvfs_sel_static(struct is_device_ischain *device)
{
	struct is_core *core;
	struct is_dvfs_ctrl *dvfs_ctrl;
	struct is_dvfs_scenario_ctrl *static_ctrl, *dynamic_ctrl;
	struct is_dvfs_scenario *scenarios;
	struct is_resourcemgr *resourcemgr;
	struct is_dual_info *dual_info;
	int i, scenario_id, scenario_cnt, streaming_cnt;
	int position, resol, fps, stream_cnt;
	unsigned long sensor_map;
	int sel;
	struct is_device_sensor *sensor;
	struct is_sensor_cfg *sensor_cfg;

	FIMC_BUG(!device);
	FIMC_BUG(!device->interface);
	FIMC_BUG(!device->sensor);

	core = (struct is_core *)device->interface->core;
	resourcemgr = device->resourcemgr;
	dvfs_ctrl = &(resourcemgr->dvfs_ctrl);
	static_ctrl = dvfs_ctrl->static_ctrl;
	dynamic_ctrl = dvfs_ctrl->dynamic_ctrl;

	if (!test_bit(IS_DVFS_SEL_TABLE, &dvfs_ctrl->state)) {
		err("dvfs table is NOT selected");
		return -EINVAL;
	}

	/* static scenario */
	if (!static_ctrl) {
		err("static_dvfs_ctrl is NULL");
		return -EINVAL;
	}

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

	sensor = device->sensor;
	sensor_cfg = sensor->cfg;
	if (!sensor_cfg) {
		merr("sensor_cfg is NULL", device);
		return -EINVAL;
	}

	scenarios = static_ctrl->scenarios;
	scenario_cnt = static_ctrl->scenario_cnt;
	position = is_sensor_g_position(device->sensor);
	fps = sensor_cfg->max_fps;

	/*
	 * stream_cnt : number of open sensors
	 * streaming_cnt : number of sensors in operation
	 */
	stream_cnt = is_get_start_sensor_cnt(core);
	streaming_cnt = resourcemgr->streaming_cnt;
	sensor_map = core->sensor_map;
	dual_info = &core->dual_info;
#ifdef BDS_DVFS
	resol = is_get_bds_size(device, dual_info);
#else
	resol = is_get_target_resol(device);
#endif

	dbg_dvfs(1, "pos(%d), res(%d), fps(%d), sc(%d, %d), map(%d), dual(%d, %d, %d, %d, %d)(%d), setfile(%d),\
		limited_fps(%d)\n",
		device,	position, resol, fps, stream_cnt, streaming_cnt, sensor_map,
		dual_info->max_fps[SENSOR_POSITION_REAR],
		dual_info->max_fps[SENSOR_POSITION_REAR2],
		dual_info->max_fps[SENSOR_POSITION_REAR3],
		dual_info->max_fps[SENSOR_POSITION_FRONT],
		dual_info->max_fps[SENSOR_POSITION_REAR_TOF],
		dual_info->mode,
		(device->setfile & IS_SETFILE_MASK),
		resourcemgr->limited_fps);

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

		sel = scenarios[i].check_func(device, NULL, position, resol, fps,
			stream_cnt, streaming_cnt, sensor_map, dual_info);

		dbg_dvfs(1, "sel(%d) idx(%d) [%s]\n",
			device, sel, i, scenarios[i].scenario_nm);

		switch (sel) {
		case DVFS_MATCHED:
			scenario_id = scenarios[i].scenario_id;
			static_ctrl->cur_scenario_id = scenario_id;
			static_ctrl->cur_scenario_idx = i;
			static_ctrl->cur_frame_tick = scenarios[i].keep_frame_tick;
			/* use dynaimc control to improve launching time */
			if (scenario_id == IS_SN_PREVIEW_HIGH_SPEED_FPS
					|| scenario_id == IS_SN_FRONT_PREVIEW_HIGH_SPEED_FPS) {
				dynamic_ctrl->cur_scenario_id = IS_SN_PREVIEW_HIGH_SPEED_FPS;
				dynamic_ctrl->cur_frame_tick = scenarios[i].keep_frame_tick;
			}
			return scenario_id;
		case DVFS_SKIP:
			return -EAGAIN;
		case DVFS_NOT_MATCHED:
		default:
			continue;
		}
	}

	warn("couldn't find static dvfs scenario [sensor:(%d/%d)/fps:%d/setfile:%d/resol:(%d)]\n",
		is_get_start_sensor_cnt(core),
		device->sensor->pdev->id,
		fps, (device->setfile & IS_SETFILE_MASK), resol);

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

	return IS_SN_DEFAULT;
}

int is_dvfs_sel_dynamic(struct is_device_ischain *device, struct is_group *group,
	struct is_frame *frame)
{
	int ret;
	struct is_core *core;
	struct is_dvfs_ctrl *dvfs_ctrl;
	struct is_dvfs_scenario_ctrl *dynamic_ctrl;
	struct is_dvfs_scenario_ctrl *static_ctrl;
	struct is_dvfs_scenario *scenarios;
	struct is_resourcemgr *resourcemgr;
	struct is_dual_info *dual_info;
	int i, scenario_id, scenario_cnt;
	int position, resol, fps;
	unsigned long sensor_map;
	struct is_device_sensor *sensor;
	struct is_sensor_cfg *sensor_cfg;

	FIMC_BUG(!device);
	FIMC_BUG(!device->sensor);
	FIMC_BUG(!frame);

	core = (struct is_core *)device->interface->core;
	resourcemgr = device->resourcemgr;
	dvfs_ctrl = &(resourcemgr->dvfs_ctrl);
	dynamic_ctrl = dvfs_ctrl->dynamic_ctrl;
	static_ctrl = dvfs_ctrl->static_ctrl;

	if (static_ctrl->cur_frame_tick == IS_DVFS_SKIP_DYNAMIC)
		return 0;

	if (!test_bit(IS_DVFS_SEL_TABLE, &dvfs_ctrl->state)) {
		err("dvfs table is NOT selected");
		return -EINVAL;
	}

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

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

	scenarios = dynamic_ctrl->scenarios;

	if (test_bit(IS_ISCHAIN_REPROCESSING, &device->state))
		scenario_cnt = dynamic_ctrl->scenario_cnt;
	else
		scenario_cnt = dynamic_ctrl->fixed_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;
		}
	}

	if (group->id == GROUP_ID_VRA0)
		return -EAGAIN;

	sensor = device->sensor;
	sensor_cfg = sensor->cfg;
	if (!sensor_cfg) {
		merr("sensor_cfg is NULL", device);
		return -EINVAL;
	}

	position = is_sensor_g_position(device->sensor);
	fps = sensor_cfg->max_fps;
	sensor_map = core->sensor_map;
	dual_info = &core->dual_info;
#ifdef BDS_DVFS
	resol = is_get_bds_size(device, dual_info);
#else
	resol = is_get_target_resol(device);
#endif

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

		ret = scenarios[i].check_func(device, frame->shot, position, resol, fps, 0, 0, sensor_map, dual_info);
		switch (ret) {
		case DVFS_MATCHED:
			scenario_id = scenarios[i].scenario_id;
			dynamic_ctrl->cur_scenario_id = scenario_id;
			dynamic_ctrl->cur_scenario_idx = i;
			dynamic_ctrl->cur_frame_tick = scenarios[i].keep_frame_tick;

			return scenario_id;
		case DVFS_SKIP:
			goto p_again;
		case DVFS_NOT_MATCHED:
		default:
			continue;
		}
	}

p_again:
	return  -EAGAIN;
}

int is_dvfs_sel_external(struct is_device_sensor *device)
{
	int ret;
	struct is_core *core;
	struct is_dvfs_ctrl *dvfs_ctrl;
	struct is_dvfs_scenario_ctrl *external_ctrl;
	struct is_dvfs_scenario *scenarios;
	struct is_resourcemgr *resourcemgr;
	struct is_dual_info *dual_info;
	int i, scenario_id, scenario_cnt;
	int position, resol, fps, stream_cnt, streaming_cnt;
	unsigned long sensor_map;

	FIMC_BUG(!device);

	core = device->private_data;
	resourcemgr = device->resourcemgr;
	dvfs_ctrl = &(resourcemgr->dvfs_ctrl);
	external_ctrl = dvfs_ctrl->external_ctrl;

	if (!test_bit(IS_DVFS_SEL_TABLE, &dvfs_ctrl->state)) {
		err("dvfs table is NOT selected");
		return -EINVAL;
	}

	/* external scenario */
	if (!external_ctrl) {
		warn("external_dvfs_ctrl is NULL, default max dvfs lv");
		return IS_SN_MAX;
	}

	scenarios = external_ctrl->scenarios;
	scenario_cnt = external_ctrl->scenario_cnt;
	position = is_sensor_g_position(device);
	resol = is_sensor_g_width(device) * is_sensor_g_height(device);
	fps = is_sensor_g_framerate(device);

	/*
	 * stream_cnt : number of open sensors
	 * streaming_cnt : number of sensors in operation
	 */
	stream_cnt = is_get_start_sensor_cnt(core);
	streaming_cnt = resourcemgr->streaming_cnt;
	sensor_map = core->sensor_map;
	dual_info = &core->dual_info;

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

		ret = scenarios[i].ext_check_func(device, position, resol, fps,
				stream_cnt, streaming_cnt, sensor_map, dual_info);
		switch (ret) {
		case DVFS_MATCHED:
			scenario_id = scenarios[i].scenario_id;
			external_ctrl->cur_scenario_id = scenario_id;
			external_ctrl->cur_scenario_idx = i;
			external_ctrl->cur_frame_tick = scenarios[i].keep_frame_tick;
			return scenario_id;
		case DVFS_SKIP:
			return -EAGAIN;
		case DVFS_NOT_MATCHED:
		default:
			continue;
		}
	}

	warn("couldn't find external dvfs scenario [sensor:(%d/%d/%d)/fps:%d/resol:(%d)]\n",
		stream_cnt, streaming_cnt, position, fps, resol);

	external_ctrl->cur_scenario_id = IS_SN_MAX;
	external_ctrl->cur_scenario_idx = -1;
	external_ctrl->cur_frame_tick = -1;

	return IS_SN_MAX;
}


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

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

	if (type >= IS_DVFS_END) {
		err("Cannot find DVFS value");
		return -EINVAL;
	}

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

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

	return qos;
}

int is_set_dvfs(struct is_core *core, struct is_device_ischain *device, u32 scenario_id)
{
	int ret = 0;
#if defined(QOS_INTCAM)
	int int_cam_qos;
#endif
#if defined(QOS_TNR)
	int tnr_qos;
#endif
	int int_qos, mif_qos, i2c_qos, cam_qos, disp_qos, hpg_qos;
	char *qos_info;
	struct is_resourcemgr *resourcemgr;
	struct is_dvfs_ctrl *dvfs_ctrl;

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

	resourcemgr = &core->resourcemgr;
	dvfs_ctrl = &(resourcemgr->dvfs_ctrl);

#if defined(QOS_INTCAM)
	int_cam_qos = is_get_qos(core, IS_DVFS_INT_CAM, scenario_id);
#endif
#if defined(QOS_TNR)
	tnr_qos = is_get_qos(core, IS_DVFS_TNR, scenario_id);
#endif
	int_qos = is_get_qos(core, IS_DVFS_INT, scenario_id);
	mif_qos = is_get_qos(core, IS_DVFS_MIF, scenario_id);
	i2c_qos = is_get_qos(core, IS_DVFS_I2C, scenario_id);
	cam_qos = is_get_qos(core, IS_DVFS_CAM, scenario_id);
	disp_qos = is_get_qos(core, IS_DVFS_DISP, scenario_id);
	hpg_qos = is_get_qos(core, IS_DVFS_HPG, scenario_id);

#if defined(QOS_INTCAM)
	if (int_cam_qos < 0) {
		err("getting qos value is failed!!\n");
		return -EINVAL;
	}
#endif
#if defined(QOS_TNR)
	if (tnr_qos < 0) {
		err("getting qos value is failed!!\n");
		return -EINVAL;
	}
#endif
	if ((int_qos < 0) || (mif_qos < 0) || (i2c_qos < 0)
	|| (cam_qos < 0) || (disp_qos < 0)) {
		err("getting qos value is failed!!\n");
		return -EINVAL;
	}

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

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

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

	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 defined(QOS_INTCAM)
	/* check current qos */
	if (int_cam_qos && dvfs_ctrl->cur_int_cam_qos != int_cam_qos) {
		pm_qos_update_request(&exynos_isp_qos_int_cam, int_cam_qos);
		dvfs_ctrl->cur_int_cam_qos = int_cam_qos;
	}
#endif
#if defined(QOS_TNR)
	/* check current qos */
	if (tnr_qos && dvfs_ctrl->cur_tnr_qos != tnr_qos) {
		pm_qos_update_request(&exynos_isp_qos_tnr, tnr_qos);
		dvfs_ctrl->cur_tnr_qos = tnr_qos;
	}
#endif

	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 defined(ENABLE_HMP_BOOST)
	/* hpg_qos : number of minimum online CPU */
	if (hpg_qos && device && (dvfs_ctrl->cur_hpg_qos != hpg_qos)
		&& !test_bit(IS_ISCHAIN_REPROCESSING, &device->state)) {
		pm_qos_update_request(&exynos_isp_qos_hpg, hpg_qos);
		dvfs_ctrl->cur_hpg_qos = hpg_qos;

#if defined(CONFIG_HMP_VARIABLE_SCALE)
		/* for migration to big core */
		if (hpg_qos > 4) {
			if (!dvfs_ctrl->cur_hmp_bst) {
				set_hmp_boost(1);
				dvfs_ctrl->cur_hmp_bst = 1;
			}
		} else {
			if (dvfs_ctrl->cur_hmp_bst) {
				set_hmp_boost(0);
				dvfs_ctrl->cur_hmp_bst = 0;
			}
		}
#elif defined(CONFIG_SCHED_EHMP) || defined(CONFIG_SCHED_EMS)
		/* for migration to big core */
		if (hpg_qos > 4) {
			if (!dvfs_ctrl->cur_hmp_bst) {
#if defined(CONFIG_SCHED_EMS_TUNE)
				emstune_boost(&emstune_req, 1);
#else
				gb_qos_update_request(&gb_req, 100);
#endif
				dvfs_ctrl->cur_hmp_bst = 1;
			}
		} else {
			if (dvfs_ctrl->cur_hmp_bst) {
#if defined(CONFIG_SCHED_EMS_TUNE)
				emstune_boost(&emstune_req, 0);
#else
				gb_qos_update_request(&gb_req, 0);
#endif
				dvfs_ctrl->cur_hmp_bst = 0;
			}
		}
#endif
	}
#endif

	qos_info = __getname();
	if (unlikely(!qos_info)) {
		ret = -ENOMEM;
		goto exit;
	}
	snprintf(qos_info, PATH_MAX, "[RSC:%d]: New QoS [", device ? device->instance : 0);
#if defined(QOS_INTCAM)
	snprintf(qos_info + strlen(qos_info),
				PATH_MAX, " INT_CAM(%d),", int_cam_qos);
#endif
#if defined(QOS_TNR)
	snprintf(qos_info + strlen(qos_info),
				PATH_MAX, " TNR(%d),", tnr_qos);
#endif
	info("%s INT(%d), MIF(%d), CAM(%d), DISP(%d), I2C(%d), HPG(%d, %d)]\n",
			qos_info, int_qos, mif_qos, cam_qos, disp_qos,
			i2c_qos, hpg_qos, dvfs_ctrl->cur_hmp_bst);
	__putname(qos_info);
exit:
	return ret;
}

void is_dual_mode_update(struct is_device_ischain *device,
	struct is_group *group,
	struct is_frame *frame)
{
	struct is_core *core = (struct is_core *)device->interface->core;
	struct is_dual_info *dual_info = &core->dual_info;
	struct is_device_sensor *sensor = device->sensor;
	struct is_resourcemgr *resourcemgr;
	int i, streaming_cnt = 0;

	if (group->head->device_type != IS_DEVICE_SENSOR)
		return;

	/* Update max fps of dual sensor device with reference to shot meta. */
	dual_info->max_fps[sensor->position] = frame->shot->ctl.aa.aeTargetFpsRange[1];

	resourcemgr = sensor->resourcemgr;
	/* Check the number of active sensors for all PIP sensor combinations */
	for (i = 0; i < SENSOR_POSITION_REAR_TOF; i++) {
		if (resourcemgr->limited_fps && dual_info->max_fps[i])
			streaming_cnt++;
		else if (dual_info->max_fps[i] >= 10)
			streaming_cnt++;
	}

	resourcemgr->streaming_cnt = streaming_cnt;

	/* Continue if wide and tele/s-wide complete is_sensor_s_input(). */
	if (!(test_bit(SENSOR_POSITION_REAR, &core->sensor_map) &&
		(test_bit(SENSOR_POSITION_REAR2, &core->sensor_map) ||
		 test_bit(SENSOR_POSITION_REAR3, &core->sensor_map))))
		return;

	/*
	 * bypass - master_max_fps : 30fps, slave_max_fps : 0fps (sensor standby)
	 * sync - master_max_fps : 30fps, slave_max_fps : 30fps (fusion)
	 * switch - master_max_fps : 5ps, slave_max_fps : 30fps (post standby)
	 * nothing - invalid mode
	 */
	if (streaming_cnt == 1)
		dual_info->mode = IS_DUAL_MODE_BYPASS;
	else if (streaming_cnt >= 2)
		dual_info->mode = IS_DUAL_MODE_SYNC;
	else
		dual_info->mode = IS_DUAL_MODE_NOTHING;

	if (dual_info->mode == IS_DUAL_MODE_SYNC) {
		dual_info->max_bds_width =
			max_t(int, dual_info->max_bds_width, device->txp.output.width);
		dual_info->max_bds_height =
			max_t(int, dual_info->max_bds_height, device->txp.output.height);
	} else {
		dual_info->max_bds_width = device->txp.output.width;
		dual_info->max_bds_height = device->txp.output.height;
	}
}

void is_dual_dvfs_update(struct is_device_ischain *device,
	struct is_group *group,
	struct is_frame *frame)
{
	struct is_core *core = (struct is_core *)device->interface->core;
	struct is_dual_info *dual_info = &core->dual_info;
	struct is_resourcemgr *resourcemgr = device->resourcemgr;
	struct is_dvfs_scenario_ctrl *static_ctrl
				= resourcemgr->dvfs_ctrl.static_ctrl;
	int scenario_id, pre_scenario_id;

	/* Continue if wide and tele/s-wide complete is_sensor_s_input(). */
	if (!(test_bit(SENSOR_POSITION_REAR, &core->sensor_map) &&
		(test_bit(SENSOR_POSITION_REAR2, &core->sensor_map) ||
		 test_bit(SENSOR_POSITION_REAR3, &core->sensor_map))))
		return;

	if (group->head->device_type != IS_DEVICE_SENSOR)
		return;

	/*
	 * tick_count : Add dvfs update margin for dvfs update when mode is changed
	 * from fusion(sync) to standby(bypass, switch) because H/W does not apply
	 * immediately even if mode is dropped from hal.
	 * tick_count == 0 : dvfs update
	 * tick_count > 0 : tick count decrease
	 * tick count < 0 : ignore
	 */
	if (dual_info->tick_count >= 0)
		dual_info->tick_count--;

	/* If pre_mode and mode are different, tick_count setup. */
	if (dual_info->pre_mode != dual_info->mode) {

		/* If current state is IS_DUAL_NOTHING, do not do DVFS update. */
		if (dual_info->mode == IS_DUAL_MODE_NOTHING)
			dual_info->tick_count = -1;

		switch (dual_info->pre_mode) {
		case IS_DUAL_MODE_BYPASS:
		case IS_DUAL_MODE_SWITCH:
		case IS_DUAL_MODE_NOTHING:
			dual_info->tick_count = 0;
			break;
		case IS_DUAL_MODE_SYNC:
			dual_info->tick_count = IS_DVFS_DUAL_TICK;
			break;
		default:
			err("invalid dual mode %d -> %d\n", dual_info->pre_mode, dual_info->mode);
			dual_info->tick_count = -1;
			dual_info->pre_mode = IS_DUAL_MODE_NOTHING;
			dual_info->mode = IS_DUAL_MODE_NOTHING;
			break;
		}
	}

	/* Only if tick_count is 0 dvfs update. */
	if (dual_info->tick_count == 0) {
		pre_scenario_id = static_ctrl->cur_scenario_id;
		scenario_id = is_dvfs_sel_static(device);
		if (scenario_id >= 0 && scenario_id != pre_scenario_id && !is_dvfs_is_fast_ae(&resourcemgr->dvfs_ctrl)) {
			struct is_dvfs_scenario_ctrl *static_ctrl = resourcemgr->dvfs_ctrl.static_ctrl;

			mgrinfo("tbl[%d] dual static scenario(%d)-[%s]\n", device, group, frame,
				resourcemgr->dvfs_ctrl.dvfs_table_idx,
				static_ctrl->cur_scenario_id,
				static_ctrl->scenarios[static_ctrl->cur_scenario_idx].scenario_nm);
			is_set_dvfs((struct is_core *)device->interface->core, device, scenario_id);
		} else {
			dual_info->tick_count++;
		}
	}

	/* Update current mode to pre_mode. */
	dual_info->pre_mode = dual_info->mode;
}

unsigned int is_get_bit_count(unsigned long bits)
{
	unsigned int count = 0;

	while (bits) {
		bits &= (bits - 1);
		count++;
	}

	return count;
}
#endif