aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/mediatek/videox/mt6735/debug.c
blob: 8b5dc49ebecbde85b21ce97b80aa6ce0f7443578 (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
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
#include <linux/string.h>
#include <linux/time.h>
#include <linux/uaccess.h>
#include <linux/fb.h>
#include <linux/vmalloc.h>
#include <linux/sched.h>
#include <linux/debugfs.h>
#include <linux/wait.h>
#include <linux/time.h>
#include <linux/delay.h>

#include <mach/mt_typedefs.h>
#include <mach/m4u.h>
#include "disp_drv_log.h"
#include "mtkfb.h"
#include "debug.h"
#include "lcm_drv.h"
#include "ddp_ovl.h"
#include "ddp_path.h"
#include "ddp_reg.h"
#include "primary_display.h"
#include "display_recorder.h"
#include <mach/mt_gpio.h>
#include <cust_gpio_usage.h>
#include <mach/mt_clkmgr.h>
#include "mtkfb_fence.h"
#include "ddp_manager.h"

struct MTKFB_MMP_Events_t MTKFB_MMP_Events;

extern LCM_DRIVER *lcm_drv;
extern unsigned int EnableVSyncLog;
extern unsigned int gTriggerDispMode;  // 0: normal, 1: lcd only, 2: none of lcd and lcm

#define MTKFB_DEBUG_FS_CAPTURE_LAYER_CONTENT_SUPPORT

// ---------------------------------------------------------------------------
//  External variable declarations
// ---------------------------------------------------------------------------

extern long tpd_last_down_time;
extern int  tpd_start_profiling;
extern void mtkfb_log_enable(int enable);
extern void disp_log_enable(int enable);
extern void mtkfb_vsync_log_enable(int enable);
extern void mtkfb_capture_fb_only(bool enable);
extern void esd_recovery_pause(BOOL en);
extern int mtkfb_set_backlight_mode(unsigned int mode);
extern void mtkfb_pan_disp_test(void);
extern void mtkfb_show_sem_cnt(void);
extern void mtkfb_hang_test(bool en);
extern void mtkfb_switch_normal_to_factory(void);
extern void mtkfb_switch_factory_to_normal(void);
extern int mtkfb_get_debug_state(char* stringbuf, int buf_len);
extern unsigned int mtkfb_fm_auto_test(void);

extern unsigned int gCaptureLayerEnable;
extern unsigned int gCaptureLayerDownX;
extern unsigned int gCaptureLayerDownY;

extern unsigned int gCaptureOvlThreadEnable;
extern unsigned int gCaptureOvlDownX;
extern unsigned int gCaptureOvlDownY;
extern struct task_struct *captureovl_task;

extern unsigned int gCaptureFBEnable;
extern unsigned int gCaptureFBDownX;
extern unsigned int gCaptureFBDownY;
extern unsigned int gCaptureFBPeriod;
extern struct task_struct *capturefb_task;
extern wait_queue_head_t gCaptureFBWQ;

extern unsigned int gCapturePriLayerEnable;
extern unsigned int gCaptureWdmaLayerEnable;
extern unsigned int gCapturePriLayerDownX;
extern unsigned int gCapturePriLayerDownY;
extern unsigned int gCapturePriLayerNum;
#ifdef MTKFB_DEBUG_FS_CAPTURE_LAYER_CONTENT_SUPPORT
struct dentry *mtkfb_layer_dbgfs[DDP_OVL_LAYER_MUN];

extern OVL_CONFIG_STRUCT cached_layer_config[DDP_OVL_LAYER_MUN];

typedef struct {
    UINT32 layer_index;
    unsigned long working_buf;
    UINT32 working_size;
} MTKFB_LAYER_DBG_OPTIONS;

MTKFB_LAYER_DBG_OPTIONS mtkfb_layer_dbg_opt[DDP_OVL_LAYER_MUN];

#endif    
extern LCM_DRIVER *lcm_drv;
// ---------------------------------------------------------------------------
//  Debug Options
// ---------------------------------------------------------------------------

static const long int DEFAULT_LOG_FPS_WND_SIZE = 30;

typedef struct {
    unsigned int en_fps_log;
    unsigned int en_touch_latency_log;
    unsigned int log_fps_wnd_size;
    unsigned int force_dis_layers;
} DBG_OPTIONS;

static DBG_OPTIONS dbg_opt = {0};
static bool enable_ovl1_to_mem = true;


static char STR_HELP[] =
    "\n"
    "USAGE\n"
    "        echo [ACTION]... > /d/mtkfb\n"
    "\n"
    "ACTION\n"
	"        mtkfblog:[on|off]\n"
	"             enable/disable [MTKFB] log\n"
	"\n"
	"        displog:[on|off]\n"
	"             enable/disable [DISP] log\n"
	"\n"
	"        mtkfb_vsynclog:[on|off]\n"
	"             enable/disable [VSYNC] log\n"
	"\n"
	"        log:[on|off]\n"
	"             enable/disable above all log\n"
	"\n"
    "        fps:[on|off]\n"
    "             enable fps and lcd update time log\n"
	"\n"
    "        tl:[on|off]\n"
    "             enable touch latency log\n"
    "\n"
    "        layer\n"
    "             dump lcd layer information\n"
    "\n"
    "        suspend\n"
    "             enter suspend mode\n"
    "\n"
    "        resume\n"
    "             leave suspend mode\n"
    "\n"
    "        lcm:[on|off|init]\n"
    "             power on/off lcm\n"
    "\n"
    "        cabc:[ui|mov|still]\n"
    "             cabc mode, UI/Moving picture/Still picture\n"
    "\n"
    "        lcd:[on|off]\n"
    "             power on/off display engine\n"
    "\n"
    "        te:[on|off]\n"
    "             turn on/off tearing-free control\n"
    "\n"
    "        tv:[on|off]\n"
    "             turn on/off tv-out\n"
    "\n"
    "        tvsys:[ntsc|pal]\n"
    "             switch tv system\n"
    "\n"
    "        reg:[lcd|dpi|dsi|tvc|tve]\n"
    "             dump hw register values\n"
    "\n"
    "        regw:addr=val\n"
    "             write hw register\n"
    "\n"
    "        regr:addr\n"
    "             read hw register\n"
    "\n"     
    "       cpfbonly:[on|off]\n"
    "             capture UI layer only on/off\n"
    "\n"     
    "       esd:[on|off]\n"
    "             esd kthread on/off\n"
    "       HQA:[NormalToFactory|FactoryToNormal]\n"
    "             for HQA requirement\n"
    "\n"
    "       cmd:[value]\n"
    "             set cmd\n"
    "\n"     
    "       mmp\n"
    "             Register MMProfile events\n"
	"\n"
	"       dump_fb:[on|off[,down_sample_x[,down_sample_y,[delay]]]]\n"
	"             Start/end to capture framebuffer every delay(ms)\n"
	"\n"
	"       dump_ovl:[on|off[,down_sample_x[,down_sample_y]]]\n"
	"             Start to capture OVL only once\n"
	"\n"
	"       dump_layer:[on|off[,down_sample_x[,down_sample_y]][,layer(0:L0,1:L1,2:L2,3:L3,4:L0-3)]\n"
	"             Start/end to capture current enabled OVL layer every frame\n"
    ;


// ---------------------------------------------------------------------------
//  Information Dump Routines
// ---------------------------------------------------------------------------

void init_mtkfb_mmp_events(void)
{
    if (MTKFB_MMP_Events.MTKFB == 0)
    {
        MTKFB_MMP_Events.MTKFB = MMProfileRegisterEvent(MMP_RootEvent, "MTKFB");
        MTKFB_MMP_Events.PanDisplay = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "PanDisplay");
        MTKFB_MMP_Events.CreateSyncTimeline = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "CreateSyncTimeline");
        MTKFB_MMP_Events.SetOverlayLayer = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "SetOverlayLayer");
        MTKFB_MMP_Events.SetOverlayLayers = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "SetOverlayLayers");
        MTKFB_MMP_Events.SetMultipleLayers = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "SetMultipleLayers");
        MTKFB_MMP_Events.CreateSyncFence = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "CreateSyncFence");
        MTKFB_MMP_Events.IncSyncTimeline = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "IncSyncTimeline");
        MTKFB_MMP_Events.SignalSyncFence = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "SignalSyncFence");
        MTKFB_MMP_Events.TrigOverlayOut = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "TrigOverlayOut");
        MTKFB_MMP_Events.UpdateScreenImpl = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "UpdateScreenImpl");
        MTKFB_MMP_Events.VSync = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "VSync");
        MTKFB_MMP_Events.UpdateConfig = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "UpdateConfig");
        MTKFB_MMP_Events.EsdCheck = MMProfileRegisterEvent(MTKFB_MMP_Events.UpdateConfig, "EsdCheck");
        MTKFB_MMP_Events.ConfigOVL = MMProfileRegisterEvent(MTKFB_MMP_Events.UpdateConfig, "ConfigOVL");
        MTKFB_MMP_Events.ConfigAAL = MMProfileRegisterEvent(MTKFB_MMP_Events.UpdateConfig, "ConfigAAL");
        MTKFB_MMP_Events.ConfigMemOut = MMProfileRegisterEvent(MTKFB_MMP_Events.UpdateConfig, "ConfigMemOut");
        MTKFB_MMP_Events.ScreenUpdate = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "ScreenUpdate");
        MTKFB_MMP_Events.CaptureFramebuffer = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "CaptureFB");
        MTKFB_MMP_Events.RegUpdate = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "RegUpdate");
        MTKFB_MMP_Events.EarlySuspend = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "EarlySuspend");
        MTKFB_MMP_Events.DispDone = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "DispDone");
        MTKFB_MMP_Events.DSICmd = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "DSICmd");
        MTKFB_MMP_Events.DSIIRQ = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "DSIIrq");
        MTKFB_MMP_Events.WaitVSync = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "WaitVSync");
        MTKFB_MMP_Events.LayerDump = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "LayerDump");
        MTKFB_MMP_Events.Layer[0] = MMProfileRegisterEvent(MTKFB_MMP_Events.LayerDump, "Layer0");
        MTKFB_MMP_Events.Layer[1] = MMProfileRegisterEvent(MTKFB_MMP_Events.LayerDump, "Layer1");
        MTKFB_MMP_Events.Layer[2] = MMProfileRegisterEvent(MTKFB_MMP_Events.LayerDump, "Layer2");
        MTKFB_MMP_Events.Layer[3] = MMProfileRegisterEvent(MTKFB_MMP_Events.LayerDump, "Layer3");
        MTKFB_MMP_Events.OvlDump = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "OvlDump");
        MTKFB_MMP_Events.FBDump = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "FBDump");
        MTKFB_MMP_Events.DSIRead = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "DSIRead");
        MTKFB_MMP_Events.GetLayerInfo = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "GetLayerInfo");
        MTKFB_MMP_Events.LayerInfo[0] = MMProfileRegisterEvent(MTKFB_MMP_Events.GetLayerInfo, "LayerInfo0");
        MTKFB_MMP_Events.LayerInfo[1] = MMProfileRegisterEvent(MTKFB_MMP_Events.GetLayerInfo, "LayerInfo1");
        MTKFB_MMP_Events.LayerInfo[2] = MMProfileRegisterEvent(MTKFB_MMP_Events.GetLayerInfo, "LayerInfo2");
        MTKFB_MMP_Events.LayerInfo[3] = MMProfileRegisterEvent(MTKFB_MMP_Events.GetLayerInfo, "LayerInfo3");
        MTKFB_MMP_Events.IOCtrl = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "IOCtrl");
        MTKFB_MMP_Events.Debug = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "Debug");
		MTKFB_MMP_Events.primary_seq_config = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "primary_seq_config");
		MTKFB_MMP_Events.primary_seq_trigger=  MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "primary_seq_trigger");
		MTKFB_MMP_Events.primary_seq_wdma0_efo=  MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "primary_seq_wdma0_efo");
		MTKFB_MMP_Events.primary_seq_rdma0_sof=  MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "primary_seq_rdma0_sof");
		MTKFB_MMP_Events.primary_seq_rdma0_eof=  MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "primary_seq_rdma0_eof");
		MTKFB_MMP_Events.external_seq_config = MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "external_seq_config");
		MTKFB_MMP_Events.external_seq_trigger=	MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "external_seq_trigger");
		MTKFB_MMP_Events.external_seq_wdma0_efo=  MMProfileRegisterEvent(MTKFB_MMP_Events.MTKFB, "external_seq_wdma0_efo");
        MMProfileEnableEventRecursive(MTKFB_MMP_Events.MTKFB, 1);
    }
}

#include <linux/ftrace_event.h>

static met_log_map dprec_met_info[DISP_SESSION_MEMORY+2] =
{
    {"DSI-LCM",     0,  0},
    {"OVL0-DSI",    0,  0},
    {"OVL0-MHL",      0,  0},
    {"OVL0-SMS",      0,  0},
};

#define absabs(a) (((a) < 0) ? -(a) : (a))
#define DISP_INTERNAL_BUFFER_COUNT 3
#define FRM_UPDATE_SEQ_CACHE_NUM (DISP_INTERNAL_BUFFER_COUNT+1)
static disp_frm_seq_info frm_update_sequence[FRM_UPDATE_SEQ_CACHE_NUM];
static disp_frm_seq_info frm_update_sequence_mem[FRM_UPDATE_SEQ_CACHE_NUM];
static unsigned int frm_update_cnt;
static unsigned int frm_update_cnt_mem;

static unsigned long __read_mostly tracing_mark_write_addr = 0;
static void inline __mt_update_tracing_mark_write_addr(void)
{
    	if(unlikely(0 == tracing_mark_write_addr))
        	tracing_mark_write_addr = kallsyms_lookup_name("tracing_mark_write");
}

static void dprec_logger_frame_seq_begin(DISP_SESSION_TYPE session_type, unsigned frm_sequence)
{  
    if(frm_sequence <= 0 || session_type <0)
        return ;
	
    if(session_type > DISP_SESSION_MEMORY)
    {
        printk("seq_begin session_type(0x%x) error, seq(%d) \n",session_type, frm_sequence);
        return;
    }
   
    if(dprec_met_info[session_type].begin_frm_seq != frm_sequence)
	{
	    __mt_update_tracing_mark_write_addr();
        event_trace_printk(tracing_mark_write_addr, "S|%d|%s|%d\n", current->tgid, dprec_met_info[session_type].log_name, frm_sequence);
	    dprec_met_info[session_type].begin_frm_seq = frm_sequence;
    }
}

static void dprec_logger_frame_seq_end(DISP_SESSION_TYPE session_type, unsigned frm_sequence)
{
    if(frm_sequence <= 0 || session_type < 0)
        return ;

    if(session_type > DISP_SESSION_MEMORY)
    {
        printk("seq_end session_type(0x%x) , seq(%d) \n",session_type, frm_sequence);
        return;
    }
    
    if(dprec_met_info[session_type].end_frm_seq != frm_sequence)
	{
    	__mt_update_tracing_mark_write_addr();
        event_trace_printk(tracing_mark_write_addr, "F|%d|%s|%d\n", current->tgid, dprec_met_info[session_type].log_name, frm_sequence); 
    	dprec_met_info[session_type].end_frm_seq = frm_sequence;
	}
}

static char* get_frame_seq_state_string(DISP_FRM_SEQ_STATE state)
{
	switch(state)
	{
		case FRM_CONFIG:
			return "FRM_CONFIG";
		case FRM_TRIGGER:
			return "FRM_TRIGGER";
		case FRM_WDMA0_EOF:
			return "FRM_WDMA0_EOF";
		case FRM_RDMA0_SOF:
			return "FRM_RDMA0_SOF";
		case FRM_RDMA0_EOF:
			return "FRM_RDMA0_EOF";
		default:
			return "UNKNOW Frame State";
	}

	return "";
}

void update_frm_seq_info(unsigned int session_id, unsigned int addr, unsigned int addr_offset,unsigned int seq, DISP_FRM_SEQ_STATE state)
{
    int i= 0;
    unsigned device_type = DISP_SESSION_TYPE(session_id);
    //printk("update_frm_seq_info, 0x%x/0x%x/0x%x/%d/%s/%d\n", session_id, addr, addr_offset, seq, get_frame_seq_state_string(state), frm_update_cnt);
	
    if(seq < 0 || session_id < 0 || state > FRM_RDMA0_EOF)
        return ;
    if(device_type > DISP_SESSION_MEMORY)
    {
        printk("seq_end session_id(0x%x) , seq(%d) \n",session_id, seq);
        return;
    }   
	
	if(device_type == DISP_SESSION_PRIMARY || device_type == DISP_SESSION_PRIMARY - 1)
	{
		if(FRM_CONFIG == state)
		{
			frm_update_sequence[frm_update_cnt].state = state;
			frm_update_sequence[frm_update_cnt].session_type = device_type;
			frm_update_sequence[frm_update_cnt].mva= addr;
			frm_update_sequence[frm_update_cnt].max_offset= addr_offset;
			if(seq > 0)
				frm_update_sequence[frm_update_cnt].seq= seq;
			MMProfileLogEx(MTKFB_MMP_Events.primary_seq_config, MMProfileFlagPulse, addr, seq);
		}
		else if(FRM_TRIGGER == state)
		{
			frm_update_sequence[frm_update_cnt].state = FRM_TRIGGER;
			MMProfileLogEx(MTKFB_MMP_Events.primary_seq_trigger, MMProfileFlagPulse, addr, seq);
			dprec_logger_frame_seq_begin(device_type,  frm_update_sequence[frm_update_cnt].seq);
	
			frm_update_cnt++;
			frm_update_cnt%=FRM_UPDATE_SEQ_CACHE_NUM;
		}
		else if(FRM_WDMA0_EOF == state)
		{
			for(i= 0; i< FRM_UPDATE_SEQ_CACHE_NUM; i++)
			{			 
	//			 if((absabs(addr -frm_update_sequence[i].mva) <=  frm_update_sequence[i].max_offset) && (frm_update_sequence[i].state == FRM_TRIGGER))
				if(frm_update_sequence[i].state == FRM_TRIGGER)
				{
					frm_update_sequence[i].state = FRM_WDMA0_EOF;
					frm_update_sequence[i].mva = addr;
					MMProfileLogEx(MTKFB_MMP_Events.primary_seq_wdma0_efo, MMProfileFlagPulse, frm_update_sequence[i].mva, frm_update_sequence[i].seq);
					///break;
				}
			}
		}
		else if(FRM_RDMA0_SOF == state)
		{
			for(i= 0; i< FRM_UPDATE_SEQ_CACHE_NUM; i++)
			{
				if(FRM_WDMA0_EOF == frm_update_sequence[i].state && device_type == DISP_SESSION_PRIMARY && frm_update_sequence[i].mva == addr)
				{
					frm_update_sequence[i].state = FRM_RDMA0_SOF;
					dprec_logger_frame_seq_end(device_type,	frm_update_sequence[i].seq);
					dprec_logger_frame_seq_begin(DISP_SESSION_PRIMARY-1,  frm_update_sequence[i].seq);
					MMProfileLogEx(MTKFB_MMP_Events.primary_seq_rdma0_sof, MMProfileFlagPulse, frm_update_sequence[i].mva, frm_update_sequence[i].seq); 
				}
			}
		} 
		else if(FRM_RDMA0_EOF == state)
		{
			for(i= 0; i< FRM_UPDATE_SEQ_CACHE_NUM; i++)
			{
				if(FRM_RDMA0_SOF == frm_update_sequence[i].state && device_type == DISP_SESSION_PRIMARY && frm_update_sequence[i].mva == addr)
				{
					frm_update_sequence[i].state = FRM_RDMA0_EOF;
					dprec_logger_frame_seq_end(DISP_SESSION_PRIMARY-1,	frm_update_sequence[i].seq );
					MMProfileLogEx(MTKFB_MMP_Events.primary_seq_rdma0_eof, MMProfileFlagPulse, frm_update_sequence[i].mva, frm_update_sequence[i].seq);
					
				}
			}
		}
	}
	else if(device_type == DISP_SESSION_MEMORY)
	{
		if(FRM_CONFIG == state)
		{
			frm_update_sequence_mem[frm_update_cnt_mem].state = state;
			frm_update_sequence_mem[frm_update_cnt_mem].session_type = device_type;
			frm_update_sequence_mem[frm_update_cnt_mem].mva= addr;
			frm_update_sequence_mem[frm_update_cnt_mem].max_offset= addr_offset;
			if(seq > 0)
				frm_update_sequence_mem[frm_update_cnt_mem].seq= seq;
	
			MMProfileLogEx(MTKFB_MMP_Events.external_seq_config, MMProfileFlagPulse, addr, seq);
		}
		else if(FRM_TRIGGER == state)
		{
			frm_update_sequence_mem[frm_update_cnt_mem].state = FRM_TRIGGER;
			MMProfileLogEx(MTKFB_MMP_Events.external_seq_trigger, MMProfileFlagPulse, addr, seq);
			dprec_logger_frame_seq_begin(device_type,  frm_update_sequence_mem[frm_update_cnt_mem].seq);
	
			frm_update_cnt_mem++;
			frm_update_cnt_mem%=FRM_UPDATE_SEQ_CACHE_NUM;
		}
		else if(FRM_WDMA0_EOF == state)
		{
			for(i= 0; i< FRM_UPDATE_SEQ_CACHE_NUM; i++)
			{			 
	//			  if((absabs(addr -frm_update_sequence_mem[i].mva) <=  frm_update_sequence_mem[i].max_offset) && (frm_update_sequence_mem[i].state == FRM_TRIGGER))
				if(frm_update_sequence_mem[i].state == FRM_TRIGGER)
				{
					MMProfileLogEx(MTKFB_MMP_Events.external_seq_wdma0_efo, MMProfileFlagPulse, frm_update_sequence_mem[i].mva, frm_update_sequence_mem[i].seq);
	
					frm_update_sequence_mem[i].state = FRM_WDMA0_EOF;
					frm_update_sequence_mem[i].mva = addr;
					dprec_logger_frame_seq_end(device_type,	frm_update_sequence_mem[i].seq);
					///break;
				}
			}
		}
	}
}
static __inline int is_layer_enable(unsigned int roi_ctl, unsigned int layer)
{
    return (roi_ctl >> (31 - layer)) & 0x1;
}

static void dump_layer_info(void)
{
	unsigned int i;
	for(i=0;i<4;i++){
		printk("LayerInfo in LCD driver, layer=%d,layer_en=%d, source=%d, fmt=%d, addr=0x%lx, x=%d, y=%d \n\
		w=%d, h=%d, pitch=%d, keyEn=%d, key=%d, aen=%d, alpha=%d \n ", 
	    cached_layer_config[i].layer,   // layer
	    cached_layer_config[i].layer_en,
	    cached_layer_config[i].source,   // data source (0=memory)
	    cached_layer_config[i].fmt, 
	    cached_layer_config[i].addr, // addr 
	    cached_layer_config[i].dst_x,  // x
	    cached_layer_config[i].dst_y,  // y
	    cached_layer_config[i].dst_w, // width
	    cached_layer_config[i].dst_h, // height
	    cached_layer_config[i].src_pitch, //pitch, pixel number
	    cached_layer_config[i].keyEn,  //color key
	    cached_layer_config[i].key,  //color key
	    cached_layer_config[i].aen, // alpha enable
	    cached_layer_config[i].alpha);	
	}
}


// ---------------------------------------------------------------------------
//  FPS Log
// ---------------------------------------------------------------------------

typedef struct {
    long int current_lcd_time_us;
    long int current_te_delay_time_us;
    long int total_lcd_time_us;
    long int total_te_delay_time_us;
    long int start_time_us;
    long int trigger_lcd_time_us;
    unsigned int trigger_lcd_count;

    long int current_hdmi_time_us;
    long int total_hdmi_time_us;
    long int hdmi_start_time_us;
    long int trigger_hdmi_time_us;
    unsigned int trigger_hdmi_count;
} FPS_LOGGER;

static FPS_LOGGER fps = {0};
static FPS_LOGGER hdmi_fps = {0};

static long int get_current_time_us(void)
{
    struct timeval t;
    do_gettimeofday(&t);
    return (t.tv_sec & 0xFFF) * 1000000 + t.tv_usec;
}


static void __inline reset_fps_logger(void)
{
    memset(&fps, 0, sizeof(fps));
}

static void __inline reset_hdmi_fps_logger(void)
{
    memset(&hdmi_fps, 0, sizeof(hdmi_fps));
}

void DBG_OnTriggerLcd(void)
{
    if (!dbg_opt.en_fps_log && !dbg_opt.en_touch_latency_log) return;
    
    fps.trigger_lcd_time_us = get_current_time_us();
    if (fps.trigger_lcd_count == 0) {
        fps.start_time_us = fps.trigger_lcd_time_us;
    }
}

void DBG_OnTriggerHDMI(void)
{
    if (!dbg_opt.en_fps_log && !dbg_opt.en_touch_latency_log) return;
    
    hdmi_fps.trigger_hdmi_time_us = get_current_time_us();
    if (hdmi_fps.trigger_hdmi_count == 0) {
        hdmi_fps.hdmi_start_time_us = hdmi_fps.trigger_hdmi_time_us;
    }
}

void DBG_OnTeDelayDone(void)
{
    long int time;
    
    if (!dbg_opt.en_fps_log && !dbg_opt.en_touch_latency_log) return;

    time = get_current_time_us();
    fps.current_te_delay_time_us = (time - fps.trigger_lcd_time_us);
    fps.total_te_delay_time_us += fps.current_te_delay_time_us;
}


void DBG_OnLcdDone(void)
{
    long int time;

    if (!dbg_opt.en_fps_log && !dbg_opt.en_touch_latency_log) return;

    // deal with touch latency log

    time = get_current_time_us();
    fps.current_lcd_time_us = (time - fps.trigger_lcd_time_us);

#if 0   // FIXME
    if (dbg_opt.en_touch_latency_log && tpd_start_profiling) {

        pr_info("DISP/DBG " "Touch Latency: %ld ms\n", 
               (time - tpd_last_down_time) / 1000);

        pr_info("DISP/DBG " "LCD update time %ld ms (TE delay %ld ms + LCD %ld ms)\n",
               fps.current_lcd_time_us / 1000,
               fps.current_te_delay_time_us / 1000,
               (fps.current_lcd_time_us - fps.current_te_delay_time_us) / 1000);
        
        tpd_start_profiling = 0;
    }
#endif

    if (!dbg_opt.en_fps_log) return;

    // deal with fps log
        
    fps.total_lcd_time_us += fps.current_lcd_time_us;
    ++ fps.trigger_lcd_count;

    if (fps.trigger_lcd_count >= dbg_opt.log_fps_wnd_size) {

        long int f = fps.trigger_lcd_count * 100 * 1000 * 1000 
                     / (time - fps.start_time_us);

        long int update = fps.total_lcd_time_us * 100 
                          / (1000 * fps.trigger_lcd_count);

        long int te = fps.total_te_delay_time_us * 100 
                      / (1000 * fps.trigger_lcd_count);

        long int lcd = (fps.total_lcd_time_us - fps.total_te_delay_time_us) * 100
                       / (1000 * fps.trigger_lcd_count);

        pr_info("DISP/DBG " "MTKFB FPS: %ld.%02ld, Avg. update time: %ld.%02ld ms "
               "(TE delay %ld.%02ld ms, LCD %ld.%02ld ms)\n",
               f / 100, f % 100,
               update / 100, update % 100,
               te / 100, te % 100,
               lcd / 100, lcd % 100);
		reset_fps_logger();
	}
}

void DBG_OnHDMIDone(void)
{
    long int time;

    if (!dbg_opt.en_fps_log && !dbg_opt.en_touch_latency_log) return;

    // deal with touch latency log

    time = get_current_time_us();
    hdmi_fps.current_hdmi_time_us = (time - hdmi_fps.trigger_hdmi_time_us);


    if (!dbg_opt.en_fps_log) return;

    // deal with fps log
        
    hdmi_fps.total_hdmi_time_us += hdmi_fps.current_hdmi_time_us;
    ++ hdmi_fps.trigger_hdmi_count;

    if (hdmi_fps.trigger_hdmi_count >= dbg_opt.log_fps_wnd_size) {

        long int f = hdmi_fps.trigger_hdmi_count * 100 * 1000 * 1000 
                     / (time - hdmi_fps.hdmi_start_time_us);

        long int update = hdmi_fps.total_hdmi_time_us * 100 
                          / (1000 * hdmi_fps.trigger_hdmi_count);

        pr_info("DISP/DBG " "[HDMI] FPS: %ld.%02ld, Avg. update time: %ld.%02ld ms\n",
               f / 100, f % 100,
               update / 100, update % 100);
        
        reset_hdmi_fps_logger();
    }
}

// ---------------------------------------------------------------------------
//  Command Processor
// ---------------------------------------------------------------------------
extern void mtkfb_clear_lcm(void);
extern void hdmi_force_init(void);
extern int DSI_BIST_Pattern_Test(DISP_MODULE_ENUM module, cmdqRecHandle cmdq, bool enable, unsigned int color);

bool get_ovl1_to_mem_on()
{
    return enable_ovl1_to_mem;
}

void switch_ovl1_to_mem(bool on)
{
    enable_ovl1_to_mem = on;
    pr_info("DISP/DBG " "switch_ovl1_to_mem %d\n", enable_ovl1_to_mem);
}
static int _draw_line(unsigned long addr, int l, int t, int r, int b, int linepitch, unsigned int color)
{
	int i = 0;

	if(l>r || b<t) return -1;
	
	if(l == r)
	{ // vertical line
		for(i = 0;i<(b-t);i++)
		{
			*(unsigned long*)(addr+(t+i)*linepitch+l*4) = color;
		}
	}
	else if(t == b)
	{ // horizontal line
		for(i = 0;i<(r-l);i++)
		{
			*(unsigned long*)(addr+t*linepitch+(l+i)*4) = color;
		}
	}
	else
	{ // tile line, not support now
		return -1;
	}

	return 0;
}

static int _draw_rect(unsigned int addr, int l, int t, int r, int b, unsigned int linepitch, unsigned int color)
{
	int ret = 0;
	ret += _draw_line(addr, l, t, r, t, linepitch, color);
	ret += _draw_line(addr, l, t, l, b, linepitch, color);
	ret += _draw_line(addr, r, t, r, b, linepitch, color);
	ret += _draw_line(addr, l, b, r, b, linepitch, color);
	return ret;
}

static void _draw_block(unsigned long addr, unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned int linepitch, unsigned int color)
{
	int i = 0;
	int j = 0;
	unsigned long start_addr = addr+linepitch*y+x*4;
	DISPMSG("addr=0x%lx, start_addr=0x%lx, x=%d,y=%d,w=%d,h=%d,linepitch=%d, color=0x%08x\n", addr, start_addr, x,y,w,h,linepitch,color);
	for(j=0;j<h;j++)
	{
		for(i = 0;i<w;i++)
		{
			*(unsigned long*)(start_addr + i*4 + j*linepitch) = color;
		}
	}
}
extern void  smp_inner_dcache_flush_all(void);

static int g_display_debug_pattern_index = 0;
void _debug_pattern(unsigned long mva, unsigned long va, unsigned int w, unsigned int h, unsigned int linepitch, unsigned int color, unsigned int layerid, unsigned int bufidx)
{
	if(g_display_debug_pattern_index == 0)
		return;
	
	unsigned int addr = 0;
	unsigned int layer_size = 0;
	unsigned int mapped_size = 0;
	
	unsigned int bcolor = 0xff808080;
	if(layerid == 0) bcolor = 0x0000ffff;
	else if(layerid == 1) bcolor = 0x00ff00ff;
	else if(layerid == 2) bcolor = 0xff0000ff;
	else if(layerid == 3) bcolor = 0xffff00ff;

	if(va)
	{
		addr = va;
	}
	else
	{
		layer_size = linepitch*h;
		m4u_mva_map_kernel(mva, layer_size,&addr, &mapped_size);		
		if(mapped_size == 0)
		{
			DISPERR("m4u_mva_map_kernel failed\n");
			return;
		}
	}
	
	switch(g_display_debug_pattern_index)
	{
		case 1:
		{
			unsigned int resize_factor = layerid+1;

			_draw_rect(addr, w/10*resize_factor+0, h/10*resize_factor+0, w/10*(10-resize_factor)-0, h/10*(10-resize_factor)-0, linepitch, bcolor);
			_draw_rect(addr, w/10*resize_factor+1, h/10*resize_factor+1, w/10*(10-resize_factor)-1, h/10*(10-resize_factor)-1, linepitch, bcolor);
			_draw_rect(addr, w/10*resize_factor+2, h/10*resize_factor+2, w/10*(10-resize_factor)-2, h/10*(10-resize_factor)-2, linepitch, bcolor);
			_draw_rect(addr, w/10*resize_factor+3, h/10*resize_factor+3, w/10*(10-resize_factor)-3, h/10*(10-resize_factor)-3, linepitch, bcolor);
			break;
		}
		case 2:
		{
			int x = 0;
			int y = 0;
			int bw = 20;
			int bh = 20;
			_draw_block(addr, bufidx%(w/bw)*bw, bufidx%(w*h/bh/bh)/(w/bh)*bh, bw, bh, linepitch, bcolor);
			break;
		}
		case 3:
		{
			int x = 0;
			int y = 0;
			int bw = 20;
			int bh = 20;
			_draw_block(addr, bufidx%(w/bw)*bw, bufidx%(w*h/bh/bh)/(w/bh)*bh, bw, bh, linepitch, bcolor);
			break;
		}
	}
	
//	smp_inner_dcache_flush_all();
//	outer_flush_all();
	if(mapped_size)
	{
		m4u_mva_unmap_kernel(addr, layer_size, addr);
	}

	return;
}

#define DEBUG_FPS_METER_SHOW_COUNT	60
static int _fps_meter_array[DEBUG_FPS_METER_SHOW_COUNT]={0};
static unsigned long long _last_ts = 0;

void _debug_fps_meter(unsigned long mva, unsigned long va, unsigned int w, unsigned int h, unsigned int linepitch, unsigned int color, unsigned int layerid, unsigned int bufidx)
{
	int i = 0;
	unsigned long addr = 0;
	unsigned int layer_size = 0;
	unsigned int mapped_size = 0;
	unsigned long long current_ts = sched_clock();
	unsigned long long t = current_ts;
	unsigned long mod = 0;
	unsigned long current_idx = 0;
	unsigned long long l = _last_ts;
	
	if(g_display_debug_pattern_index != 3)
		return;
	DISPMSG("layerid=%d\n", layerid);
	_last_ts = current_ts;

	if(va)
	{
		addr = va;
	}
	else
	{
		layer_size = linepitch*h;
		m4u_mva_map_kernel(mva, layer_size,&addr, &mapped_size);		
		if(mapped_size == 0)
		{
			DISPERR("m4u_mva_map_kernel failed\n");
			return;
		}
	}

	mod = do_div(t, 1000*1000*1000);
	do_div(l, 1000*1000*1000);
	if(t!=l)
	{
		memset((void*)_fps_meter_array, 0, sizeof(_fps_meter_array));
		_draw_block(addr, 0, 10, w, 36, linepitch, 0x00000000);
	}

	current_idx = mod/1000/16666;
	DISPMSG("mod=%ld, current_idx=%ld\n", mod, current_idx);
	_fps_meter_array[current_idx] ++;
	for(i=0;i<DEBUG_FPS_METER_SHOW_COUNT;i++)
	{
		if(_fps_meter_array[i])
		{
			_draw_block(addr, i*18, 10, 18, 18*_fps_meter_array[i], linepitch, 0xff0000ff);
		}
		else
		{
			//_draw_block(addr, i*18, 10, 18, 18, linepitch, 0x00000000);
		}
	}
	
//	smp_inner_dcache_flush_all();
//	outer_flush_all();
	if(mapped_size)
	{
		m4u_mva_unmap_kernel(addr, layer_size, addr);
	}

	return;
}

extern int DAL_Clean(void);
extern int DAL_Printf(const char *fmt, ...);
extern void DSI_ChangeClk(DISP_MODULE_ENUM module,UINT32 clk);
extern int dpmgr_module_notify(DISP_MODULE_ENUM module, DISP_PATH_EVENT event);

static void process_dbg_opt(const char *opt)
{
	if (0 == strncmp(opt, "dsipattern", 10))
	{
		char *p = (char *)opt + 11;
		unsigned int pattern = (unsigned int) simple_strtoul(p, &p, 16);

		if (pattern) 
		{
			DSI_BIST_Pattern_Test(DISP_MODULE_DSI0,NULL,true,pattern);
			DISPMSG("enable dsi pattern: 0x%08x\n", pattern);
		}
		else 
		{
			primary_display_manual_lock();
			DSI_BIST_Pattern_Test(DISP_MODULE_DSI0,NULL,false,0);
			primary_display_manual_unlock();
			return;
		}
	}
	else if (0 == strncmp(opt, "trigger", 7))
	{
		int i = 0;
		for(i=0;i<1200;i++)
		{
			dpmgr_module_notify(DISP_MODULE_AAL, DISP_PATH_EVENT_TRIGGER);
		}
	}
	else if (0 == strncmp(opt, "diagnose", 8))
	{
		primary_display_diagnose();
		return;
	}
	else if (0 == strncmp(opt, "_efuse_test", 11))
	{
		primary_display_check_test();
	}
	else if (0 == strncmp(opt, "trigger", 7))
	{
		display_primary_path_context *ctx = primary_display_path_lock("debug");
		if(ctx)
		{
			dpmgr_signal_event(ctx->dpmgr_handle, DISP_PATH_EVENT_TRIGGER);
		}
		primary_display_path_unlock("debug");
		
		return;
	}
	else if (0 == strncmp(opt, "dprec_reset", 11))
	{
		dprec_logger_reset_all();
		return;
	}
	else if (0 == strncmp(opt, "suspend", 4))
    {
    	primary_display_suspend();
		return;
    }
else if (0 == strncmp(opt, "ata",3))
    {
    	mtkfb_fm_auto_test();
		return;
    }
    else if (0 == strncmp(opt, "resume", 4))
    {
    		primary_display_resume();
    }
    else if (0 == strncmp(opt, "dalprintf", 9))
    {
    		DAL_Printf("display aee layer test\n");
    }
    else if (0 == strncmp(opt, "dalclean", 8))
    {
    		DAL_Clean();
    }
	else if (0 == strncmp(opt, "DP", 2))
	{
		char *p = (char *)opt + 3;
		unsigned int pattern = (unsigned int) simple_strtoul(p, &p, 16);
		g_display_debug_pattern_index = pattern;
		return;
	}
	else if(0==strncmp(opt,"dsi0_clk:",9))
   	{
        char*p=(char*)opt+9;
        UINT32 clk=simple_strtoul(p, &p, 10);
        DSI_ChangeClk(DISP_MODULE_DSI0,clk);
    }
    else if (0 == strncmp(opt, "diagnose", 8))
    {
    	primary_display_diagnose();
		return;
    }
		else if (0 == strncmp(opt, "switch:", 7))
    	{
        	char*p=(char*)opt+7;
        	UINT32 mode=simple_strtoul(p, &p, 10);
    		primary_display_switch_dst_mode(mode%2);
			return;
    	}
		else if (0 == strncmp(opt, "disp_mode:", 10))
    	{
        	char*p=(char*)opt+10;
        	gTriggerDispMode=simple_strtoul(p, &p, 10);
        	DISPMSG("DDP: gTriggerDispMode=%d\n", gTriggerDispMode);    
    	}
    else if (0 == strncmp(opt, "cmmva_dprec", 11))
    {
		dprec_handle_option(0x7);
	}
    else if (0 == strncmp(opt, "cmmpa_dprec", 11))
    {
		dprec_handle_option(0x3);
	}
    else if (0 == strncmp(opt, "dprec", 5))
    {
		char *p = (char *)opt + 6;
		unsigned int option = (unsigned int) simple_strtoul(p, &p, 16);
		dprec_handle_option(option);
	}
    else if (0 == strncmp(opt, "cmdq", 4))
    {
		char *p = (char *)opt + 5;
		unsigned int option = (unsigned int) simple_strtoul(p, &p, 16);
		if(option)
			primary_display_switch_cmdq_cpu(CMDQ_ENABLE);
		else
			primary_display_switch_cmdq_cpu(CMDQ_DISABLE);
	}
    else if (0 == strncmp(opt, "maxlayer", 8))
    {
		char *p = (char *)opt + 9;
		unsigned int maxlayer = (unsigned int) simple_strtoul(p, &p, 10);
		if(maxlayer)
			primary_display_set_max_layer(maxlayer);
		else
			DISPERR("can't set max layer to 0\n");
	}
    else if (0 == strncmp(opt, "primary_reset", 13))
    {
		primary_display_reset();
	}
	else if(0 == strncmp(opt, "esd_check", 9))
	{
		char *p = (char *)opt + 10;
		unsigned int enable = (unsigned int) simple_strtoul(p, &p, 10);
		primary_display_esd_check_enable(enable);
	}
	else if(0 ==strncmp(opt, "cmd:", 4))
	{
		char *p = (char *)opt + 4;
		unsigned int value = (unsigned int) simple_strtoul(p, &p, 5);
		int lcm_cmd[5];
		unsigned int cmd_num=1;
		lcm_cmd[0]=value;
		primary_display_set_cmd(lcm_cmd,cmd_num);
	}
	else if(0 == strncmp(opt, "esd_recovery", 12))
	{
		primary_display_esd_recovery();
	}
	else if(0 == strncmp(opt, "lcm0_reset", 10))
	{
	#if 1
		DISP_CPU_REG_SET(DDP_REG_BASE_MMSYS_CONFIG+0x150,1);
		msleep(10);
		DISP_CPU_REG_SET(DDP_REG_BASE_MMSYS_CONFIG+0x150,0);
		msleep(10);
		DISP_CPU_REG_SET(DDP_REG_BASE_MMSYS_CONFIG+0x150,1);

	#else
#if 0
		mt_set_gpio_mode(GPIO106|0x80000000, GPIO_MODE_00);
		mt_set_gpio_dir(GPIO106|0x80000000, GPIO_DIR_OUT);
		mt_set_gpio_out(GPIO106|0x80000000, GPIO_OUT_ONE);
		msleep(10);
		mt_set_gpio_out(GPIO106|0x80000000, GPIO_OUT_ZERO);
		msleep(10);
		mt_set_gpio_out(GPIO106|0x80000000, GPIO_OUT_ONE);
#endif
	#endif
	}
	else if(0 == strncmp(opt, "lcm0_reset0", 11))
	{
		DISP_CPU_REG_SET(DDP_REG_BASE_MMSYS_CONFIG+0x150,0);
	}
	else if(0 == strncmp(opt, "lcm0_reset1", 11))
	{
		DISP_CPU_REG_SET(DDP_REG_BASE_MMSYS_CONFIG+0x150,1);
	}
	else if (0 == strncmp(opt, "cg", 2))
    {
		char *p = (char *)opt + 2;
		unsigned int enable = (unsigned int) simple_strtoul(p, &p, 10);
		primary_display_enable_path_cg(enable);
	}
	else if (0 == strncmp(opt, "ovl2mem:", 8))
    {
        if (0 == strncmp(opt + 8, "on", 2))
            switch_ovl1_to_mem(true);
        else
            switch_ovl1_to_mem(false);
    }
	else if (0 == strncmp(opt, "dump_layer:", 11))
    {
        if (0 == strncmp(opt + 11, "on", 2))
        {           
            char *p = (char *)opt + 14;
            gCapturePriLayerDownX = simple_strtoul(p, &p, 10);
            gCapturePriLayerDownY = simple_strtoul(p+1, &p, 10);
			gCapturePriLayerNum= simple_strtoul(p+1, &p, 10);
			gCapturePriLayerEnable = 1;
			if(gCapturePriLayerDownX==0)
				gCapturePriLayerDownX = 20;
			if(gCapturePriLayerDownY==0)
				gCapturePriLayerDownY = 20;
            printk("dump_layer En %d DownX %d DownY %d,Num %d",gCapturePriLayerEnable,gCapturePriLayerDownX,gCapturePriLayerDownY,gCapturePriLayerNum);
            
        }
        else if (0 == strncmp(opt + 11, "off", 3))
        {
            gCapturePriLayerEnable = 0;
			gCapturePriLayerNum = OVL_LAYER_NUM;
			printk("dump_layer En %d\n",gCapturePriLayerEnable);
        }
    }
    else if (0 == strncmp(opt, "dump_decouple:", 14))
    {
        if (0 == strncmp(opt + 14, "on", 2))
        {           
            char *p = (char *)opt + 17;
            gCapturePriLayerDownX = simple_strtoul(p, &p, 10);
            gCapturePriLayerDownY = simple_strtoul(p+1, &p, 10);
			gCapturePriLayerNum= simple_strtoul(p+1, &p, 10);
			gCaptureWdmaLayerEnable = 1;
			if(gCapturePriLayerDownX==0)
				gCapturePriLayerDownX = 20;
			if(gCapturePriLayerDownY==0)
				gCapturePriLayerDownY = 20;
            printk("dump_decouple En %d DownX %d DownY %d,Num %d",gCaptureWdmaLayerEnable,gCapturePriLayerDownX,gCapturePriLayerDownY,gCapturePriLayerNum);
            
        }
        else if (0 == strncmp(opt + 14, "off", 3))
        {
            gCaptureWdmaLayerEnable = 0;
			printk("dump_decouple En %d\n",gCaptureWdmaLayerEnable);
        }
    }
    else if(0 == strncmp(opt, "bkl:", 4))
    {
        char *p = (char *)opt + 4;
        unsigned int level = (unsigned int) simple_strtoul(p, &p, 10);

        printk("process_dbg_opt(), set backlight level = %d\n", level);
        primary_display_setbacklight(level);
    }
#ifdef ROME_TODO
#error
    if (0 == strncmp(opt, "hdmion", 6))
    {
//	hdmi_force_init();
    }
    else if (0 == strncmp(opt, "fps:", 4))
    {
        if (0 == strncmp(opt + 4, "on", 2)) {
            dbg_opt.en_fps_log = 1;
        } else if (0 == strncmp(opt + 4, "off", 3)) {
            dbg_opt.en_fps_log = 0;
        } else {
            goto Error;
        }
        reset_fps_logger();
    }
    else if (0 == strncmp(opt, "tl:", 3))
    {
        if (0 == strncmp(opt + 3, "on", 2)) {
            dbg_opt.en_touch_latency_log = 1;
        } else if (0 == strncmp(opt + 3, "off", 3)) {
            dbg_opt.en_touch_latency_log = 0;
        } else {
            goto Error;
        }
    }
    else if (0 == strncmp(opt, "black", 5))
    {
	mtkfb_clear_lcm();
    }
    else if (0 == strncmp(opt, "suspend", 4))
    {
        DISP_PanelEnable(FALSE);
        DISP_PowerEnable(FALSE);
    }
    else if (0 == strncmp(opt, "resume", 4))
    {
        DISP_PowerEnable(TRUE);
        DISP_PanelEnable(TRUE);
    }
    else if (0 == strncmp(opt, "lcm:", 4))
    {
        if (0 == strncmp(opt + 4, "on", 2)) {
            DISP_PanelEnable(TRUE);
        } else if (0 == strncmp(opt + 4, "off", 3)) {
            DISP_PanelEnable(FALSE);
        }
		else if (0 == strncmp(opt + 4, "init", 4)) {
			if (NULL != lcm_drv && NULL != lcm_drv->init) {
        		lcm_drv->init();
    		}
        }else {
            goto Error;
        }
    }
    else if (0 == strncmp(opt, "cabc:", 5))
    {
        if (0 == strncmp(opt + 5, "ui", 2)) {
			mtkfb_set_backlight_mode(1);
        }else if (0 == strncmp(opt + 5, "mov", 3)) {
			mtkfb_set_backlight_mode(3);
        }else if (0 == strncmp(opt + 5, "still", 5)) {
			mtkfb_set_backlight_mode(2);
        }else {
            goto Error;
        }
    }
    else if (0 == strncmp(opt, "lcd:", 4))
    {
        if (0 == strncmp(opt + 4, "on", 2)) {
            DISP_PowerEnable(TRUE);
        } else if (0 == strncmp(opt + 4, "off", 3)) {
            DISP_PowerEnable(FALSE);
        } else {
            goto Error;
        }
    }
    else if (0 == strncmp(opt, "vsynclog:", 9))
    {
        if (0 == strncmp(opt + 9, "on", 2))
        {
            EnableVSyncLog = 1;
        } else if (0 == strncmp(opt + 9, "off", 3)) 
        {
            EnableVSyncLog = 0;
        } else {
            goto Error;
        }
    }
    else if (0 == strncmp(opt, "layer", 5))
    {
        dump_layer_info();
    }
    else if(0 == strncmp(opt, "dither:", 7))
    {
        unsigned lrs, lgs, lbs, dbr, dbg, dbb;
        char *p = (char *)opt + 7;

        lrs = (unsigned int) simple_strtoul(p, &p, 16);
        p++;
        lgs = (unsigned int) simple_strtoul(p, &p, 16);
        p++;
        lbs = (unsigned int) simple_strtoul(p, &p, 16);
        p++;
        dbr = (unsigned int) simple_strtoul(p, &p, 16);
        p++;
        dbg = (unsigned int) simple_strtoul(p, &p, 16);
        p++;
        dbb = (unsigned int) simple_strtoul(p, &p, 16);
        
        pr_info("DISP/DBG " "process_dbg_opt(), %d %d %d %d %d %d\n", lrs, lgs, lbs, dbr, dbg, dbb);
    }
    else if (0 == strncmp(opt, "mtkfblog:", 9))
    {
        if (0 == strncmp(opt + 9, "on", 2)) {
            mtkfb_log_enable(true);
        } else if (0 == strncmp(opt + 9, "off", 3)) {
            mtkfb_log_enable(false);
        } else {
            goto Error;
        }
    }
    else if (0 == strncmp(opt, "displog:", 8))
    {
        if (0 == strncmp(opt + 8, "on", 2)) {
            disp_log_enable(true);
        } else if (0 == strncmp(opt + 8, "off", 3)) {
            disp_log_enable(false);
        } else {
            goto Error;
        }
    }
    else if (0 == strncmp(opt, "mtkfb_vsynclog:", 15))
    {
        if (0 == strncmp(opt + 15, "on", 2)) {
            mtkfb_vsync_log_enable(true);
        } else if (0 == strncmp(opt + 15, "off", 3)) {
            mtkfb_vsync_log_enable(false);
        } else {
            goto Error;
        }
    }
    else if (0 == strncmp(opt, "log:", 4))
    {
        if (0 == strncmp(opt + 4, "on", 2)) {
			mtkfb_log_enable(true);
			disp_log_enable(true);
        } else if (0 == strncmp(opt + 4, "off", 3)) {
            mtkfb_log_enable(false);
			disp_log_enable(false);
        } else {
            goto Error;
        }
    }
    else if (0 == strncmp(opt, "update", 6))
    {
		DISP_UpdateScreen(0, 0, DISP_GetScreenWidth(), DISP_GetScreenHeight());
    }
    else if (0 == strncmp(opt, "pan_disp", 8))
    {
		mtkfb_pan_disp_test();
    }
    else if (0 == strncmp(opt, "sem_cnt", 7))
    {
		mtkfb_show_sem_cnt();
    }
    else if (0 == strncmp(opt, "hang:", 5))
    {
        if (0 == strncmp(opt + 5, "on", 2)) {
            mtkfb_hang_test(true);
        } else if (0 == strncmp(opt + 5, "off", 3)) {
            mtkfb_hang_test(false);
        } else{
            goto Error;
        }
    }
    else if (0 == strncmp(opt, "cpfbonly:", 9))
    {
        if (0 == strncmp(opt + 9, "on", 2))
        {
            mtkfb_capture_fb_only(true);
        }
        else if (0 == strncmp(opt + 9, "off", 3))
        {
            mtkfb_capture_fb_only(false);
        }
    }
    else if (0 == strncmp(opt, "esd:", 4))
    {
        if (0 == strncmp(opt + 4, "on", 2))
        {
            esd_recovery_pause(FALSE);
        }
        else if (0 == strncmp(opt + 4, "off", 3))
        {
            esd_recovery_pause(TRUE);
        }
    }
    else if (0 == strncmp(opt, "HQA:", 4))
    {
        if (0 == strncmp(opt + 4, "NormalToFactory", 15))
        {
            mtkfb_switch_normal_to_factory();
        }
        else if (0 == strncmp(opt + 4, "FactoryToNormal", 15))
        {
            mtkfb_switch_factory_to_normal();
        }
    }
    else if (0 == strncmp(opt, "mmp", 3))
    {
        init_mtkfb_mmp_events();
    }
    else if (0 == strncmp(opt, "dump_ovl:", 9))
    {
        if (0 == strncmp(opt + 9, "on", 2))
        {
            char *p = (char *)opt + 12;
            gCaptureOvlDownX = simple_strtoul(p, &p, 10);
            gCaptureOvlDownY = simple_strtoul(p+1, &p, 10);
            gCaptureOvlThreadEnable = 1;
			wake_up_process(captureovl_task);
        }
        else if (0 == strncmp(opt + 9, "off", 3))
        {
            gCaptureOvlThreadEnable = 0;
        }
    }
    else if (0 == strncmp(opt, "dump_fb:", 8))
    {
        if (0 == strncmp(opt + 8, "on", 2))
        {
            char *p = (char *)opt + 11;
            gCaptureFBDownX = simple_strtoul(p, &p, 10);
            gCaptureFBDownY = simple_strtoul(p+1, &p, 10);
            gCaptureFBPeriod = simple_strtoul(p+1, &p, 10);
            gCaptureFBEnable = 1;
			wake_up_interruptible(&gCaptureFBWQ);
        }
        else if (0 == strncmp(opt + 8, "off", 3))
        {
            gCaptureFBEnable = 0;
        }   
    }
    else
	{
	    if (disphal_process_dbg_opt(opt))
		goto Error;
	}

    return;

Error:
    pr_err("DISP/ERROR " "parse command error!\n\n%s", STR_HELP);
#endif
}


static void process_dbg_cmd(char *cmd)
{
    char *tok;
    
    pr_info("DISP/DBG " "[mtkfb_dbg] %s\n", cmd);
    
    while ((tok = strsep(&cmd, " ")) != NULL)
    {
        process_dbg_opt(tok);
    }
}


// ---------------------------------------------------------------------------
//  Debug FileSystem Routines
// ---------------------------------------------------------------------------

struct dentry *mtkfb_dbgfs = NULL;


static ssize_t debug_open(struct inode *inode, struct file *file)
{
    file->private_data = inode->i_private;
    return 0;
}


static char debug_buffer[4096+DPREC_ERROR_LOG_BUFFER_LENGTH];
extern int mtkfb_fence_get_debug_info(int buf_len, unsigned char *stringbuf);

int debug_get_info(unsigned char *stringbuf, int buf_len)
{
	int i = 0;
	int n = 0;
	
	DISPFUNC();
	
	n += mtkfb_get_debug_state(stringbuf + n, buf_len-n); 	                          DISPMSG("%s,%d, n=%d\n", __func__, __LINE__, n);
	
	n += primary_display_get_debug_state(stringbuf + n, buf_len-n);             DISPMSG("%s,%d, n=%d\n", __func__, __LINE__, n);
	
	n += disp_sync_get_debug_info(stringbuf + n, buf_len-n);                   	DISPMSG("%s,%d, n=%d\n", __func__, __LINE__, n);	
	
	n += dprec_logger_get_result_string_all(stringbuf + n, buf_len-n);	DISPMSG("%s,%d, n=%d\n", __func__, __LINE__, n);	
	
	n += primary_display_check_path(stringbuf + n, buf_len-n);	DISPMSG("%s,%d, n=%d\n", __func__, __LINE__, n);	
	
	n += dprec_logger_get_buf(DPREC_LOGGER_ERROR, stringbuf + n, buf_len-n);	DISPMSG("%s,%d, n=%d\n", __func__, __LINE__, n);
	
	n += dprec_logger_get_buf(DPREC_LOGGER_FENCE, stringbuf + n, buf_len-n);	DISPMSG("%s,%d, n=%d\n", __func__, __LINE__, n);	

	n += dprec_logger_get_buf(DPREC_LOGGER_HWOP, stringbuf + n, buf_len-n); DISPMSG("%s,%d, n=%d\n", __func__, __LINE__, n);	

	stringbuf[n++] = 0;
	return n;
}

void debug_info_dump_to_printk(char *buf, int buf_len)
{
	int i = 0;
	int n = buf_len;
	for(i=0;i<n;i+=256)
		DISPMSG("%s", buf+i);
}

static ssize_t debug_read(struct file *file,
                          char __user *ubuf, size_t count, loff_t *ppos)
{
	const int debug_bufmax = sizeof(debug_buffer) - 1;
	int i = 0;
	int n = 0;
	
	DISPFUNC();
	
	n += debug_get_info(debug_buffer + n, debug_bufmax-n);
	//debug_info_dump_to_printk();
	return simple_read_from_buffer(ubuf, count, ppos, debug_buffer, n);
}

static ssize_t debug_write(struct file *file,
                           const char __user *ubuf, size_t count, loff_t *ppos)
{
    const int debug_bufmax = sizeof(debug_buffer) - 1;
	size_t ret;

	ret = count;

	if (count > debug_bufmax) 
        count = debug_bufmax;

	if (copy_from_user(&debug_buffer, ubuf, count))
		return -EFAULT;

	debug_buffer[count] = 0;

    process_dbg_cmd(debug_buffer);

    return ret;
}


static struct file_operations debug_fops = {
	.read  = debug_read,
    .write = debug_write,
	.open  = debug_open,
};

#ifdef MTKFB_DEBUG_FS_CAPTURE_LAYER_CONTENT_SUPPORT

static ssize_t layer_debug_open(struct inode *inode, struct file *file)
{
    MTKFB_LAYER_DBG_OPTIONS *dbgopt;
    ///record the private data
    file->private_data = inode->i_private;
    dbgopt = (MTKFB_LAYER_DBG_OPTIONS *)file->private_data;

    dbgopt->working_size = DISP_GetScreenWidth()*DISP_GetScreenHeight()*2 + 32;
    dbgopt->working_buf = (unsigned long)vmalloc(dbgopt->working_size);
    if(dbgopt->working_buf == 0)
        pr_info("DISP/DBG Vmalloc to get temp buffer failed\n");

    return 0;
}


static ssize_t layer_debug_read(struct file *file,
                          char __user *ubuf, size_t count, loff_t *ppos)
{
	return 0;
}


static ssize_t layer_debug_write(struct file *file,
                           const char __user *ubuf, size_t count, loff_t *ppos)
{
    MTKFB_LAYER_DBG_OPTIONS *dbgopt = (MTKFB_LAYER_DBG_OPTIONS *)file->private_data;

    pr_info("DISP/DBG " "mtkfb_layer%d write is not implemented yet \n", dbgopt->layer_index);

    return count;
}

static int layer_debug_release(struct inode *inode, struct file *file)
{
    MTKFB_LAYER_DBG_OPTIONS *dbgopt;
    dbgopt = (MTKFB_LAYER_DBG_OPTIONS *)file->private_data;

    if(dbgopt->working_buf != 0)
        vfree((void *)dbgopt->working_buf);

    dbgopt->working_buf = 0;

    return 0;
}


static struct file_operations layer_debug_fops = {
	.read  = layer_debug_read,
    .write = layer_debug_write,
	.open  = layer_debug_open,
    .release = layer_debug_release,
};

#endif

void DBG_Init(void)
{
    mtkfb_dbgfs = debugfs_create_file("mtkfb",
        S_IFREG|S_IRUGO, NULL, (void *)0, &debug_fops);

    memset(&dbg_opt, 0, sizeof(dbg_opt));
	memset(&fps, 0, sizeof(fps));
    dbg_opt.log_fps_wnd_size = DEFAULT_LOG_FPS_WND_SIZE;
	// xuecheng, enable fps log by default
	dbg_opt.en_fps_log = 1;

#ifdef MTKFB_DEBUG_FS_CAPTURE_LAYER_CONTENT_SUPPORT
    {
        unsigned int i;
        unsigned char a[13];

        a[0] = 'm';
        a[1] = 't';
        a[2] = 'k';
        a[3] = 'f';
        a[4] = 'b';
        a[5] = '_';
        a[6] = 'l';
        a[7] = 'a';
        a[8] = 'y';
        a[9] = 'e';
        a[10] = 'r';
        a[11] = '0';
        a[12] = '\0';
        
        for(i=0;i<DDP_OVL_LAYER_MUN;i++)
        {
            a[11] = '0' + i;
            mtkfb_layer_dbg_opt[i].layer_index = i;
            mtkfb_layer_dbgfs[i] = debugfs_create_file(a,
                S_IFREG|S_IRUGO, NULL, (void *)&mtkfb_layer_dbg_opt[i], &layer_debug_fops);
        }
    }
#endif    
}


void DBG_Deinit(void)
{
    debugfs_remove(mtkfb_dbgfs);
#ifdef MTKFB_DEBUG_FS_CAPTURE_LAYER_CONTENT_SUPPORT
    {
        unsigned int i;
        
        for(i=0;i<DDP_OVL_LAYER_MUN;i++)
            debugfs_remove(mtkfb_layer_dbgfs[i]);
    }
#endif
}