summaryrefslogtreecommitdiff
path: root/drivers/media/platform/exynos/fimc-is/fimc-is-device-flite.c
blob: 6611b3011f5ed5387b14845a82e3c7f340d0e099 (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
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
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
/*
 * Samsung Exynos5 SoC series FIMC-IS driver
 *
 * exynos5 fimc-is video 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/module.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <mach/videonode.h>
#include <media/exynos_mc.h>
#include <asm/cacheflush.h>
#include <asm/pgtable.h>
#include <linux/firmware.h>
#include <linux/dma-mapping.h>
#include <linux/scatterlist.h>
#include <linux/videodev2.h>
#include <linux/videodev2_exynos_camera.h>
#include <linux/videodev2_exynos_media.h>
#include <linux/v4l2-mediabus.h>
#include <linux/bug.h>

#include <mach/map.h>
#include <mach/regs-clock.h>
#include <mach/irqs.h>

#include "fimc-is-time.h"
#include "fimc-is-core.h"
#include "fimc-is-regs.h"
#include "fimc-is-hw.h"
#include "fimc-is-interface.h"
#include "fimc-is-device-flite.h"

#define FLITE_MAX_RESET_READY_TIME	(20) /* 100ms */
#define FLITE_MAX_WIDTH_SIZE		(8192)
#define FLITE_MAX_HEIGHT_SIZE		(8192)
/* #define COLORBAR_MODE */

/*FIMCLite*/
/* Camera Source size */
#define FLITE_REG_CISRCSIZE				(0x00)
#define FLITE_REG_CISRCSIZE_SIZE_H(x)			((x) << 16)
#define FLITE_REG_CISRCSIZE_SIZE_V(x)			((x) << 0)
#define FLITE_REG_CISRCSIZE_ORDER422_IN_YCBYCR		(0 << 14)
#define FLITE_REG_CISRCSIZE_ORDER422_IN_YCRYCB		(1 << 14)
#define FLITE_REG_CISRCSIZE_ORDER422_IN_CBYCRY		(2 << 14)
#define FLITE_REG_CISRCSIZE_ORDER422_IN_CRYCBY		(3 << 14)

/* Global control */
#define FLITE_REG_CIGCTRL				0x04
#define FLITE_REG_CIGCTRL_YUV422_1P			(0x1E << 24)
#define FLITE_REG_CIGCTRL_RAW8				(0x2A << 24)
#define FLITE_REG_CIGCTRL_RAW10				(0x2B << 24)
#define FLITE_REG_CIGCTRL_RAW12				(0x2C << 24)
#define FLITE_REG_CIGCTRL_RAW14				(0x2D << 24)

/* User defined formats. x = 0...0xF. */
#define FLITE_REG_CIGCTRL_USER(x)			(0x30 + x - 1)
#define FLITE_REG_CIGCTRL_OLOCAL_DISABLE		(1 << 22)
#define FLITE_REG_CIGCTRL_SHADOWMASK_DISABLE		(1 << 21)
#define FLITE_REG_CIGCTRL_ODMA_DISABLE			(1 << 20)
#define FLITE_REG_CIGCTRL_SWRST_REQ			(1 << 19)
#define FLITE_REG_CIGCTRL_SWRST_RDY			(1 << 18)
#define FLITE_REG_CIGCTRL_SWRST				(1 << 17)
#define FLITE_REG_CIGCTRL_TEST_PATTERN_COLORBAR		(1 << 15)
#define FLITE_REG_CIGCTRL_INVPOLPCLK			(1 << 14)
#define FLITE_REG_CIGCTRL_INVPOLVSYNC			(1 << 13)
#define FLITE_REG_CIGCTRL_INVPOLHREF			(1 << 12)
#define FLITE_REG_CIGCTRL_IRQ_LASTEN0_ENABLE		(0 << 8)
#define FLITE_REG_CIGCTRL_IRQ_LASTEN0_DISABLE		(1 << 8)
#define FLITE_REG_CIGCTRL_IRQ_ENDEN0_ENABLE		(0 << 7)
#define FLITE_REG_CIGCTRL_IRQ_ENDEN0_DISABLE		(1 << 7)
#define FLITE_REG_CIGCTRL_IRQ_STARTEN0_ENABLE		(0 << 6)
#define FLITE_REG_CIGCTRL_IRQ_STARTEN0_DISABLE		(1 << 6)
#define FLITE_REG_CIGCTRL_IRQ_OVFEN0_ENABLE		(0 << 5)
#define FLITE_REG_CIGCTRL_IRQ_OVFEN0_DISABLE		(1 << 5)
#define FLITE_REG_CIGCTRL_SELCAM_MIPI			(1 << 3)

/* Image Capture Enable */
#define FLITE_REG_CIIMGCPT				(0x08)
#define FLITE_REG_CIIMGCPT_IMGCPTEN			(1 << 31)
#define FLITE_REG_CIIMGCPT_CPT_FREN			(1 << 25)
#define FLITE_REG_CIIMGCPT_CPT_FRPTR(x)			((x) << 19)
#define FLITE_REG_CIIMGCPT_CPT_MOD_FRCNT		(1 << 18)
#define FLITE_REG_CIIMGCPT_CPT_MOD_FREN			(0 << 18)
#define FLITE_REG_CIIMGCPT_CPT_FRCNT(x)			((x) << 10)

/* Capture Sequence */
#define FLITE_REG_CICPTSEQ				(0x0C)
#define FLITE_REG_CPT_FRSEQ(x)				((x) << 0)

/* Camera Window Offset */
#define FLITE_REG_CIWDOFST				(0x10)
#define FLITE_REG_CIWDOFST_WINOFSEN			(1 << 31)
#define FLITE_REG_CIWDOFST_CLROVIY			(1 << 31)
#define FLITE_REG_CIWDOFST_WINHOROFST(x)		((x) << 16)
#define FLITE_REG_CIWDOFST_HOROFF_MASK			(0x1fff << 16)
#define FLITE_REG_CIWDOFST_CLROVFICB			(1 << 15)
#define FLITE_REG_CIWDOFST_CLROVFICR			(1 << 14)
#define FLITE_REG_CIWDOFST_WINVEROFST(x)		((x) << 0)
#define FLITE_REG_CIWDOFST_VEROFF_MASK			(0x1fff << 0)

/* Cmaera Window Offset2 */
#define FLITE_REG_CIWDOFST2				(0x14)
#define FLITE_REG_CIWDOFST2_WINHOROFST2(x)		((x) << 16)
#define FLITE_REG_CIWDOFST2_WINVEROFST2(x)		((x) << 0)

/* Camera Output DMA Format */
#define FLITE_REG_CIODMAFMT				(0x18)
#define FLITE_REG_CIODMAFMT_1D_DMA			(1 << 15)
#define FLITE_REG_CIODMAFMT_2D_DMA			(0 << 15)
#define FLITE_REG_CIODMAFMT_PACK12			(1 << 14)
#define FLITE_REG_CIODMAFMT_NORMAL			(0 << 14)
#define FLITE_REG_CIODMAFMT_CRYCBY			(0 << 4)
#define FLITE_REG_CIODMAFMT_CBYCRY			(1 << 4)
#define FLITE_REG_CIODMAFMT_YCRYCB			(2 << 4)
#define FLITE_REG_CIODMAFMT_YCBYCR			(3 << 4)

/* Camera Output Canvas */
#define FLITE_REG_CIOCAN				(0x20)
#define FLITE_REG_CIOCAN_OCAN_V(x)			((x) << 16)
#define FLITE_REG_CIOCAN_OCAN_H(x)			((x) << 0)

/* Camera Output DMA Offset */
#define FLITE_REG_CIOOFF				(0x24)
#define FLITE_REG_CIOOFF_OOFF_V(x)			((x) << 16)
#define FLITE_REG_CIOOFF_OOFF_H(x)			((x) << 0)

/* Camera Output DMA Address */
#define FLITE_REG_CIOSA					(0x30)
#define FLITE_REG_CIOSA_OSA(x)				((x) << 0)

/* Camera Status */
#define FLITE_REG_CISTATUS				(0x40)
#define FLITE_REG_CISTATUS_MIPI_VVALID			(1 << 22)
#define FLITE_REG_CISTATUS_MIPI_HVALID			(1 << 21)
#define FLITE_REG_CISTATUS_MIPI_DVALID			(1 << 20)
#define FLITE_REG_CISTATUS_ITU_VSYNC			(1 << 14)
#define FLITE_REG_CISTATUS_ITU_HREFF			(1 << 13)
#define FLITE_REG_CISTATUS_OVFIY			(1 << 10)
#define FLITE_REG_CISTATUS_OVFICB			(1 << 9)
#define FLITE_REG_CISTATUS_OVFICR			(1 << 8)
#define FLITE_REG_CISTATUS_IRQ_SRC_OVERFLOW		(1 << 7)
#define FLITE_REG_CISTATUS_IRQ_SRC_LASTCAPEND		(1 << 6)
#define FLITE_REG_CISTATUS_IRQ_SRC_FRMSTART		(1 << 5)
#define FLITE_REG_CISTATUS_IRQ_SRC_FRMEND		(1 << 4)
#define FLITE_REG_CISTATUS_IRQ_CAM			(1 << 0)
#define FLITE_REG_CISTATUS_IRQ_MASK			(0xf << 4)

/* Camera Status2 */
#define FLITE_REG_CISTATUS2				(0x44)
#define FLITE_REG_CISTATUS2_LASTCAPEND			(1 << 1)
#define FLITE_REG_CISTATUS2_FRMEND			(1 << 0)

/* Camera Status3 */
#define FLITE_REG_CISTATUS3				0x48
#define FLITE_REG_CISTATUS3_PRESENT_MASK		(0x3F)

/* Qos Threshold */
#define FLITE_REG_CITHOLD				(0xF0)
#define FLITE_REG_CITHOLD_W_QOS_EN			(1 << 30)
#define FLITE_REG_CITHOLD_WTH_QOS(x)			((x) << 0)

/* Camera General Purpose */
#define FLITE_REG_CIGENERAL				(0xFC)
#define FLITE_REG_CIGENERAL_CAM_A			(0)
#define FLITE_REG_CIGENERAL_CAM_B			(1)
#define FLITE_REG_CIGENERAL_CAM_C			(2)
#define FLITE_REG_CIGENERAL_CAM_D			(3)
#define FLITE_REG_CIGENERAL_3AA1_CAM_A			(0 << 14)
#define FLITE_REG_CIGENERAL_3AA1_CAM_B			(1 << 14)
#define FLITE_REG_CIGENERAL_3AA1_CAM_C			(2 << 14)
#define FLITE_REG_CIGENERAL_3AA1_CAM_D			(3 << 14)

#define FLITE_REG_CIFCNTSEQ				0x100

/* BNS */
#define FLITE_REG_BINNINGON				(0x120)
#define FLITE_REG_BINNINGON_CLKGATE_ON(x)		(~(x) << 1)
#define FLITE_REG_BINNINGON_EN(x)			((x) << 0)

#define FLITE_REG_BINNINGCTRL				(0x124)
#define FLITE_REG_BINNINGCTRL_FACTOR_Y(x)		((x) << 22)
#define FLITE_REG_BINNINGCTRL_FACTOR_X(x)		((x) << 17)
#define FLITE_REG_BINNINGCTRL_SHIFT_UP_Y(x)		((x) << 15)
#define FLITE_REG_BINNINGCTRL_SHIFT_UP_X(x)		((x) << 13)
#define FLITE_REG_BINNINGCTRL_PRECISION_BITS(x)		((x) << 10)
#define FLITE_REG_BINNINGCTRL_BITTAGE(x)		((x) << 5)
#define FLITE_REG_BINNINGCTRL_UNITY_SIZE(x)		((x) << 0)

#define FLITE_REG_PEDESTAL				(0x128)
#define FLITE_REG_PEDESTAL_OUT(x)			((x) << 12)
#define FLITE_REG_PEDESTAL_IN(x)			((x) << 0)

#define FLITE_REG_BINNINGTOTAL				(0x12C)
#define FLITE_REG_BINNINGTOTAL_HEIGHT(x)		((x) << 16)
#define FLITE_REG_BINNINGTOTAL_WIDTH(x)			((x) << 0)

#define FLITE_REG_BINNINGINPUT				(0x130)
#define FLITE_REG_BINNINGINPUT_HEIGHT(x)		((x) << 16)
#define FLITE_REG_BINNINGINPUT_WIDTH(x)			((x) << 0)

#define FLITE_REG_BINNINGMARGIN				(0x134)
#define FLITE_REG_BINNINGMARGIN_TOP(x)			((x) << 16)
#define FLITE_REG_BINNINGMARGIN_LEFT(x)			((x) << 0)

#define FLITE_REG_BINNINGOUTPUT				(0x138)
#define FLITE_REG_BINNINGOUTPUT_HEIGHT(x)		((x) << 16)
#define FLITE_REG_BINNINGOUTPUT_WIDTH(x)		((x) << 0)

#define FLITE_REG_WEIGHTX01				(0x13C)
#define FLITE_REG_WEIGHTX01_1(x)			((x) << 16)
#define FLITE_REG_WEIGHTX01_0(x)			((x) << 0)

#define FLITE_REG_WEIGHTX23				(0x140)
#define FLITE_REG_WEIGHTX23_1(x)			((x) << 16)
#define FLITE_REG_WEIGHTX23_0(x)			((x) << 0)

#define FLITE_REG_WEIGHTY01				(0x15C)
#define FLITE_REG_WEIGHTY01_1(x)			((x) << 16)
#define FLITE_REG_WEIGHTY01_0(x)			((x) << 0)

#define FLITE_REG_WEIGHTY23				(0x160)
#define FLITE_REG_WEIGHTY23_1(x)			((x) << 16)
#define FLITE_REG_WEIGHTY23_0(x)			((x) << 0)

static void flite_hw_enable_bns(unsigned long __iomem *base_reg, bool enable)
{
	u32 cfg = 0;

	/* enable */
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_BINNINGON));
	cfg |= FLITE_REG_BINNINGON_CLKGATE_ON(enable);
	cfg |= FLITE_REG_BINNINGON_EN(enable);
	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_BINNINGON));
}

static void flite_hw_s_coeff_bns(unsigned long __iomem *base_reg,
	u32 factor_x, u32 factor_y)
{
	u32 cfg = 0;
	u32 factor = 0;

	factor = factor_x * factor_y;
	if (factor_x != factor_y)
		err("x y factor is not the same (%d, %d)", factor_x, factor_y);

	/* control */
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_BINNINGCTRL));
	cfg |= FLITE_REG_BINNINGCTRL_FACTOR_Y(factor_y);
	cfg |= FLITE_REG_BINNINGCTRL_FACTOR_X(factor_x);
	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_BINNINGCTRL));

	switch (factor) {
	case 9:
		/* coefficient */
		cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_WEIGHTX01));
		cfg |= FLITE_REG_WEIGHTX01_1(0x20);
		cfg |= FLITE_REG_WEIGHTX01_0(0xE0);
		writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_WEIGHTX01));

		cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_WEIGHTX23));
		cfg |= FLITE_REG_WEIGHTX23_1(0xA0);
		cfg |= FLITE_REG_WEIGHTX23_0(0x60);
		writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_WEIGHTX23));

		cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_WEIGHTY01));
		cfg |= FLITE_REG_WEIGHTY01_1(0x20);
		cfg |= FLITE_REG_WEIGHTY01_0(0xE0);
		writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_WEIGHTY01));

		cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_WEIGHTY23));
		cfg |= FLITE_REG_WEIGHTY23_1(0xA0);
		cfg |= FLITE_REG_WEIGHTY23_0(0x60);
		writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_WEIGHTY23));
		break;
	case 16:
		/* coefficient */
		cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_WEIGHTX01));
		cfg |= FLITE_REG_WEIGHTX01_1(0x40);
		cfg |= FLITE_REG_WEIGHTX01_0(0xC0);
		writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_WEIGHTX01));

		cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_WEIGHTY01));
		cfg |= FLITE_REG_WEIGHTY01_1(0x40);
		cfg |= FLITE_REG_WEIGHTY01_0(0xC0);
		writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_WEIGHTY01));
		break;
	default:
		err("unknown factor!! (%d, %d)", factor_x, factor_y);
		break;
	}
}

static void flite_hw_s_size_bns(unsigned long __iomem *base_reg,
	u32 width, u32 height, u32 otf_width, u32 otf_height)
{
	u32 cfg = 0;

	/* size */
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_BINNINGTOTAL));
	cfg |= FLITE_REG_BINNINGTOTAL_HEIGHT(height);
	cfg |= FLITE_REG_BINNINGTOTAL_WIDTH(width);
	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_BINNINGTOTAL));

	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_BINNINGINPUT));
	cfg |= FLITE_REG_BINNINGINPUT_HEIGHT(height);
	cfg |= FLITE_REG_BINNINGINPUT_WIDTH(width);
	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_BINNINGINPUT));

	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_BINNINGMARGIN));
	cfg |= FLITE_REG_BINNINGMARGIN_TOP(0);
	cfg |= FLITE_REG_BINNINGMARGIN_LEFT(0);
	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_BINNINGMARGIN));

	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_BINNINGOUTPUT));
	cfg |= FLITE_REG_BINNINGOUTPUT_HEIGHT(otf_height);
	cfg |= FLITE_REG_BINNINGOUTPUT_WIDTH(otf_width);
	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_BINNINGOUTPUT));
}

static int flite_hw_set_bns(unsigned long __iomem *base_reg,
        struct fimc_is_image *image)
{
	int ret = 0;
	u32 width, height;
	u32 otf_width, otf_height;
	u32 factor_x, factor_y;

	BUG_ON(!image);

	width = image->window.width;
	height = image->window.height;
	otf_width = image->window.otf_width;
	otf_height = image->window.otf_height;

	if (otf_width == width && otf_height == height) {
		info("input & output sizes are same(%d, %d)\n", otf_width, otf_height);
		goto exit;
	}

	if (otf_width == 0 || otf_height == 0) {
		warn("bns size is zero. s_ctrl(V4L2_CID_IS_S_BNS) first\n");
		goto exit;
	}

	factor_x = 2 * width / otf_width;
	factor_y = 2 * height / otf_height;

	flite_hw_s_size_bns(base_reg, width, height, otf_width, otf_height);

	flite_hw_s_coeff_bns(base_reg, factor_x, factor_y);

	flite_hw_enable_bns(base_reg, true);

	info("BNS in(%d, %d), BNS out(%d, %d), ratio(%d, %d)\n",
	width, height, otf_width, otf_height, factor_x, factor_y);
exit:
	return ret;
}

static void flite_hw_set_cam_source_size(unsigned long __iomem *base_reg,
	struct fimc_is_image *image)
{
	u32 cfg = 0;

	BUG_ON(!image);

#ifdef COLORBAR_MODE
	cfg |= FLITE_REG_CISRCSIZE_SIZE_H(640);
	cfg |= FLITE_REG_CISRCSIZE_SIZE_V(480);
#else
	cfg |= FLITE_REG_CISRCSIZE_SIZE_H(image->window.o_width);
	cfg |= FLITE_REG_CISRCSIZE_SIZE_V(image->window.o_height);
#endif

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CISRCSIZE));
}

static void flite_hw_set_dma_offset(unsigned long __iomem *base_reg,
	struct fimc_is_image *image)
{
	u32 cfg = 0;

	BUG_ON(!image);

	/* HACK */
	if (image->format.pixelformat == V4L2_PIX_FMT_SBGGR10 || image->format.pixelformat == V4L2_PIX_FMT_SBGGR12)
		cfg |= FLITE_REG_CIOCAN_OCAN_H(roundup(image->window.o_width, 10));
	else
		cfg |= FLITE_REG_CIOCAN_OCAN_H(image->window.o_width);

	cfg |= FLITE_REG_CIOCAN_OCAN_V(image->window.o_height);

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIOCAN));
}

static void flite_hw_set_cam_channel(unsigned long __iomem *base_reg,
	u32 otf_setting)
{
	u32 cfg = 0;

	cfg |= otf_setting;

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGENERAL));
}

static void flite_hw_set_capture_start(unsigned long __iomem *base_reg)
{
	u32 cfg = 0;

	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIIMGCPT));
	cfg |= FLITE_REG_CIIMGCPT_IMGCPTEN;

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIIMGCPT));
}

static void flite_hw_set_capture_stop(unsigned long __iomem *base_reg)
{
	u32 cfg = 0;

	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIIMGCPT));
	cfg &= ~FLITE_REG_CIIMGCPT_IMGCPTEN;

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIIMGCPT));
}

static int flite_hw_set_source_format(unsigned long __iomem *base_reg, struct fimc_is_image *image)
{
	int ret = 0;
	u32 pixelformat, format, cfg;

	BUG_ON(!image);

	pixelformat = image->format.pixelformat;
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	switch (pixelformat) {
	case V4L2_PIX_FMT_SBGGR8:
	case V4L2_PIX_FMT_SGBRG8:
	case V4L2_PIX_FMT_SGRBG8:
	case V4L2_PIX_FMT_SRGGB8:
		format = HW_FORMAT_RAW8;
		break;
	case V4L2_PIX_FMT_SBGGR10:
	case V4L2_PIX_FMT_SGBRG10:
	case V4L2_PIX_FMT_SGRBG10:
	case V4L2_PIX_FMT_SRGGB10:
		format = HW_FORMAT_RAW10;
		break;
	case V4L2_PIX_FMT_SBGGR12:
	case V4L2_PIX_FMT_SGBRG12:
	case V4L2_PIX_FMT_SGRBG12:
	case V4L2_PIX_FMT_SRGGB12:
		format = HW_FORMAT_RAW10;
		/*
		 * HACK : hal send RAW10 for RAW12
		 * formt = HW_FORMAT_RAW12 << 24;
		 */
		break;
	case V4L2_PIX_FMT_YUYV:
		format = HW_FORMAT_YUV422_8BIT;
		break;
	case V4L2_PIX_FMT_JPEG:
		format = HW_FORMAT_USER;
		break;
	default:
		err("unsupported format(%X)", pixelformat);
		format = HW_FORMAT_RAW10;
		ret = -EINVAL;
		break;
	}

#ifdef COLORBAR_MODE
	cfg |= (HW_FORMAT_YUV422_8BIT << 24);
#else
	cfg |= (format << 24);
#endif

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	return ret;
}

static void flite_hw_set_dma_fmt(unsigned long __iomem *base_reg,
	u32 pixelformat)
{
	u32 cfg = 0;

	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIODMAFMT));

	if (pixelformat == V4L2_PIX_FMT_SBGGR10 || pixelformat == V4L2_PIX_FMT_SBGGR12)
		cfg |= FLITE_REG_CIODMAFMT_PACK12;
	else
		cfg |= FLITE_REG_CIODMAFMT_NORMAL;

	if (pixelformat == V4L2_PIX_FMT_SGRBG8)
		cfg |= FLITE_REG_CIODMAFMT_1D_DMA;
	else
		cfg |= FLITE_REG_CIODMAFMT_2D_DMA;

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIODMAFMT));
}

static void flite_hw_set_output_dma(unsigned long __iomem *base_reg, bool enable,
	u32 pixelformat)
{
	u32 cfg = 0;
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	if (enable) {
		cfg &= ~FLITE_REG_CIGCTRL_ODMA_DISABLE;
		flite_hw_set_dma_fmt(base_reg, pixelformat);
	} else {
		cfg |= FLITE_REG_CIGCTRL_ODMA_DISABLE;
	}

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
}

static void flite_hw_set_output_local(unsigned long __iomem *base_reg, bool enable)
{
	u32 cfg = 0;
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	if (enable)
		cfg &= ~FLITE_REG_CIGCTRL_OLOCAL_DISABLE;
	else
		cfg |= FLITE_REG_CIGCTRL_OLOCAL_DISABLE;

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
}

#ifdef COLORBAR_MODE
static void flite_hw_set_test_pattern_enable(unsigned long __iomem *base_reg)
{
	u32 cfg = 0;

	/* will use for pattern generation testing */
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
	cfg |= FLITE_REG_CIGCTRL_TEST_PATTERN_COLORBAR;

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
}
#endif

static void flite_hw_set_interrupt_source(unsigned long __iomem *base_reg)
{
	u32 cfg = 0;
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	/* for checking stop complete */
	cfg &= ~FLITE_REG_CIGCTRL_IRQ_LASTEN0_DISABLE;

	/* for checking frame start */
	cfg &= ~FLITE_REG_CIGCTRL_IRQ_STARTEN0_DISABLE;

	/* for checking frame end */
	cfg &= ~FLITE_REG_CIGCTRL_IRQ_ENDEN0_DISABLE;

	/* for checking overflow */
	cfg &= ~FLITE_REG_CIGCTRL_IRQ_OVFEN0_DISABLE;

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
}

static void flite_hw_clr_interrupt_source(unsigned long __iomem *base_reg)
{
	u32 cfg = 0;
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	/* for checking stop complete */
	cfg |= FLITE_REG_CIGCTRL_IRQ_LASTEN0_DISABLE;

	/* for checking frame start */
	cfg |= FLITE_REG_CIGCTRL_IRQ_STARTEN0_DISABLE;

	/* for checking frame end */
	cfg |= FLITE_REG_CIGCTRL_IRQ_ENDEN0_DISABLE;

	/* for checking overflow */
	cfg |= FLITE_REG_CIGCTRL_IRQ_OVFEN0_DISABLE;

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
}

static void flite_hw_set_ovf_interrupt_source(unsigned long __iomem *base_reg)
{
	u32 cfg = 0;
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	/* for checking overflow */
	cfg &= ~FLITE_REG_CIGCTRL_IRQ_OVFEN0_DISABLE;

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
}

static void flite_hw_clr_ovf_interrupt_source(unsigned long __iomem *base_reg)
{
	u32 cfg = 0;
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	/* for checking overflow */
	cfg |= FLITE_REG_CIGCTRL_IRQ_OVFEN0_DISABLE;

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
}

static int flite_hw_check_ovf_interrupt_source(unsigned long __iomem *base_reg)
{
	u32 cfg = 0;
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	/* for checking overflow */
	if (cfg & FLITE_REG_CIGCTRL_IRQ_OVFEN0_DISABLE)
		return true;

	return false;
}

static void flite_hw_force_reset(unsigned long __iomem *base_reg)
{
	u32 cfg = 0, retry = 100;
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	/* request sw reset */
	cfg |= FLITE_REG_CIGCTRL_SWRST_REQ;
	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	/* checking reset ready */
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
	while (retry-- && !(cfg & FLITE_REG_CIGCTRL_SWRST_RDY))
		cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	if (!(cfg & FLITE_REG_CIGCTRL_SWRST_RDY))
		warn("[CamIF] sw reset is not read but forcelly");

	/* sw reset */
	cfg |= FLITE_REG_CIGCTRL_SWRST;
	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
	warn("[CamIF] sw reset");
}

static void flite_hw_set_inverse_polarity(unsigned long __iomem *base_reg)
{
	u32 cfg = 0;

	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
	cfg &= ~(FLITE_REG_CIGCTRL_INVPOLPCLK | FLITE_REG_CIGCTRL_INVPOLVSYNC
			| FLITE_REG_CIGCTRL_INVPOLHREF);

	/* cfg |= (FLITE_REG_CIGCTRL_INVPOLPCLK | FLITE_REG_CIGCTRL_INVPOLVSYNC); */

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
}

static void flite_hw_set_camera_type(unsigned long __iomem *base_reg)
{
	u32 cfg = 0;
	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));

	cfg |= FLITE_REG_CIGCTRL_SELCAM_MIPI;

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGCTRL));
}

static void flite_hw_set_window_offset(unsigned long __iomem *base_reg,
        struct fimc_is_image *image)
{
	u32 cfg = 0;
	u32 hoff2, voff2;

	BUG_ON(!image);

	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIWDOFST));
	cfg &= ~(FLITE_REG_CIWDOFST_HOROFF_MASK |
		FLITE_REG_CIWDOFST_VEROFF_MASK);
	cfg |= FLITE_REG_CIWDOFST_WINOFSEN |
		FLITE_REG_CIWDOFST_WINHOROFST(image->window.offs_h) |
		FLITE_REG_CIWDOFST_WINVEROFST(image->window.offs_v);

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIWDOFST));

	hoff2 = image->window.o_width - image->window.width - image->window.offs_h;
	voff2 = image->window.o_height - image->window.height - image->window.offs_v;
	cfg = FLITE_REG_CIWDOFST2_WINHOROFST2(hoff2) |
		FLITE_REG_CIWDOFST2_WINVEROFST2(voff2);

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIWDOFST2));
}

static void flite_hw_set_last_capture_end_clear(unsigned long __iomem *base_reg)
{
	u32 cfg = 0;

	cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CISTATUS2));
	cfg &= ~FLITE_REG_CISTATUS2_LASTCAPEND;

	writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CISTATUS2));
}

int flite_hw_get_present_frame_buffer(unsigned long __iomem *base_reg)
{
	u32 status = 0;

	status = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CISTATUS3));
	status &= FLITE_REG_CISTATUS3_PRESENT_MASK;

	return status;
}

int flite_hw_get_status2(unsigned long __iomem *base_reg)
{
	u32 status = 0;

	status = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CISTATUS2));

	return status;
}

int flite_hw_get_status1(unsigned long __iomem *base_reg)
{
	u32 status = 0;

	status = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CISTATUS));

	return status;
}

int flite_hw_getnclr_status1(unsigned long __iomem *base_reg)
{
	u32 status = 0;

	status = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CISTATUS));
	writel(0, base_reg + TO_WORD_OFFSET(FLITE_REG_CISTATUS));

	return status;
}

void flite_hw_set_status2(unsigned long __iomem *base_reg, u32 val)
{
	writel(val, base_reg + TO_WORD_OFFSET(FLITE_REG_CISTATUS2));
}

void flite_hw_set_start_addr(unsigned long __iomem *base_reg, u32 number, u32 addr)
{
	unsigned long __iomem *target_reg;

	if (number == 0) {
		target_reg = base_reg + TO_WORD_OFFSET(0x30);
	} else {
		number--;
		target_reg = base_reg + TO_WORD_OFFSET(0x200 + (0x4*number));
	}

	writel(addr, target_reg);
}

void flite_hw_set_use_buffer(unsigned long __iomem *base_reg, u32 number)
{
	u32 buffer;
	buffer = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIFCNTSEQ));
	buffer |= 1<<number;
	writel(buffer, base_reg + TO_WORD_OFFSET(FLITE_REG_CIFCNTSEQ));
}

void flite_hw_set_unuse_buffer(unsigned long __iomem *base_reg, u32 number)
{
	u32 buffer;
	buffer = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIFCNTSEQ));
	buffer &= ~(1<<number);
	writel(buffer, base_reg + TO_WORD_OFFSET(FLITE_REG_CIFCNTSEQ));
}

void flite_hw_set_mux(unsigned long __iomem *base_reg, u32 csi_ch, u32 flite_ch)
{
	u32 cfg;
	if ((csi_ch == 0) && (flite_ch == 1)) {
		info("[CSI:D] mux setting : CSI0 <-> FLITE1\n");
		cfg = readl(base_reg + TO_WORD_OFFSET(FLITE_REG_CIGENERAL));
		cfg |= (1 << 12);
		writel(cfg, base_reg + TO_WORD_OFFSET(FLITE_REG_CIGENERAL));
	}
}

int init_fimc_lite(unsigned long __iomem *base_reg)
{
	int i;

	writel(0, base_reg + TO_WORD_OFFSET(FLITE_REG_CIFCNTSEQ));

	for (i = 0; i < 32; i++)
		flite_hw_set_start_addr(base_reg , i, 0xffffffff);

	return 0;
}

static int start_fimc_lite(unsigned long __iomem *base_reg,
	struct fimc_is_image *image,
	u32 otf_setting,
	u32 bns,
	u32 csi_ch,
	u32 flite_ch)
{
	flite_hw_set_cam_channel(base_reg, otf_setting);
	flite_hw_set_cam_source_size(base_reg, image);
	flite_hw_set_dma_offset(base_reg, image);
	flite_hw_set_camera_type(base_reg);
	flite_hw_set_source_format(base_reg, image);
	flite_hw_set_inverse_polarity(base_reg);
	flite_hw_set_interrupt_source(base_reg);
	flite_hw_set_window_offset(base_reg, image);
	flite_hw_set_mux(base_reg, csi_ch, flite_ch);

#ifdef COLORBAR_MODE
	flite_hw_set_test_pattern_enable(base_reg);
#endif

	if (bns)
		flite_hw_set_bns(base_reg, image);

	flite_hw_set_last_capture_end_clear(base_reg);
	flite_hw_set_capture_start(base_reg);

	return 0;
}

static inline void stop_fimc_lite(unsigned long __iomem *base_reg)
{
	flite_hw_set_capture_stop(base_reg);
}

static inline void flite_s_buffer_addr(struct fimc_is_device_flite *device,
	u32 bindex, u32 baddr)
{
	flite_hw_set_start_addr(device->base_reg, bindex, baddr);
}

static inline int flite_s_use_buffer(struct fimc_is_device_flite *flite,
	u32 bindex)
{
	int ret = 0;
	unsigned long target_time;

	BUG_ON(!flite);

	if (!atomic_read(&flite->bcount)) {
		if (flite->buf_done_mode == FLITE_BUF_DONE_EARLY) {
			target_time = jiffies +
				msecs_to_jiffies(flite->buf_done_wait_time);
			while ((target_time > jiffies) &&
					(flite_hw_get_status1(flite->base_reg) && (7 << 20)))
				pr_debug("over vblank (early buffer done)");
		}

		if (flite_hw_get_status1(flite->base_reg) && (7 << 20)) {
			merr("over vblank (buf-mode : %d)", flite, flite->buf_done_mode);
			ret = -EINVAL;
			goto p_err;
		}

		flite_hw_set_use_buffer(flite->base_reg, bindex);
		atomic_inc(&flite->bcount);
		flite_hw_set_output_dma(flite->base_reg, true, flite->image.format.pixelformat);
	} else {
		flite_hw_set_use_buffer(flite->base_reg, bindex);
		atomic_inc(&flite->bcount);
	}

p_err:
	return ret;
}

static inline int flite_s_unuse_buffer(struct fimc_is_device_flite *flite,
	u32 bindex)
{
	int ret = 0;
	unsigned long target_time;

	BUG_ON(!flite);

	if (atomic_read(&flite->bcount) == 1) {
		if (flite->buf_done_mode == FLITE_BUF_DONE_EARLY) {
			target_time = jiffies +
				msecs_to_jiffies(flite->buf_done_wait_time);
			while ((target_time > jiffies) &&
					(flite_hw_get_status1(flite->base_reg) && (7 << 20)))
				pr_debug("over vblank (early buffer done)");
		}

		if (flite_hw_get_status1(flite->base_reg) && (7 << 20)) {
			merr("over vblank (buf-mode : %d)", flite, flite->buf_done_mode);
			ret = -EINVAL;
			goto p_err;
		}

		flite_hw_set_output_dma(flite->base_reg, false, flite->image.format.pixelformat);
		flite_hw_set_unuse_buffer(flite->base_reg, bindex);
		atomic_dec(&flite->bcount);
	} else {
		flite_hw_set_unuse_buffer(flite->base_reg, bindex);
		atomic_dec(&flite->bcount);
	}

p_err:
	return ret;
}

static inline void flite_set_ovf_stop(struct fimc_is_device_flite *flite)
{
	struct fimc_is_device_sensor *device;

	device = container_of(flite->subdev, struct fimc_is_device_sensor, subdev_flite);

	if (test_bit(FIMC_IS_SENSOR_FRONT_DTP_STOP, &device->state))
		return;

	/* set dtp */
	set_bit(FIMC_IS_FLITE_OVERFLOW_STOP, &device->force_stop);

	return;
}

static u32 g_print_cnt;
#define LOG_INTERVAL_OF_DROPS 30
static void tasklet_flite_str0(unsigned long data)
{
	struct v4l2_subdev *subdev;
	struct fimc_is_device_flite *flite;
	struct fimc_is_device_sensor *device;
	struct fimc_is_device_ischain *ischain;
	struct fimc_is_groupmgr *groupmgr;
	struct fimc_is_group *group_3aa, *group_isp;
	u32 bstart, fcount, present;

	subdev = (struct v4l2_subdev *)data;
	flite = v4l2_get_subdevdata(subdev);
	if (!flite) {
		err("flite is NULL");
		BUG();
	}

	device = v4l2_get_subdev_hostdata(subdev);
	if (!device) {
		err("device is NULL");
		BUG();
	}

	present = flite_hw_get_present_frame_buffer(flite->base_reg) - 1;
	bstart = flite->tasklet_param_str;
	fcount = atomic_read(&flite->fcount);
	ischain = device->ischain;

#ifdef TASKLET_MSG
	info("S%d %d\n", bstart, fcount);
#endif

	/* comparing sw state and hw state */
	if (atomic_read(&flite->bcount) == 2) {
		if ((bstart == FLITE_A_SLOT_VALID) &&
			(present != FLITE_A_SLOT_VALID)) {
			mwarn("wrong SW buffer slot A(sw:%d != hw:%d)", flite, bstart, present);
			flite->sw_trigger = bstart = FLITE_B_SLOT_VALID;
		}

		if ((bstart == FLITE_B_SLOT_VALID) &&
			(present != FLITE_B_SLOT_VALID)) {
			mwarn("wrong SW buffer slot B(sw:%d != hw:%d)", flite, bstart, present);
			flite->sw_trigger = bstart = FLITE_A_SLOT_VALID;
		}
	}

	groupmgr = ischain->groupmgr;
	group_3aa = &ischain->group_3aa;
	group_isp = &ischain->group_isp;
	if (unlikely(list_empty(&group_3aa->smp_trigger.wait_list))) {
		atomic_set(&group_3aa->sensor_fcount, fcount + group_3aa->async_shots);

		/*
		 * pcount : program count
		 * current program count(location) in kthread
		 */
		if (((g_print_cnt % LOG_INTERVAL_OF_DROPS) == 0) ||
			(g_print_cnt < LOG_INTERVAL_OF_DROPS)) {
			info("grp1(res %d, rcnt %d, scnt %d), "
				"grp2(res %d, rcnt %d, scnt %d), "
				"fcount %d(%d, %d) pcount %d\n",
				groupmgr->group_smp_res[group_3aa->id].count,
				atomic_read(&group_3aa->rcount),
				atomic_read(&group_3aa->scount),
				groupmgr->group_smp_res[group_isp->id].count,
				atomic_read(&group_isp->rcount),
				atomic_read(&group_isp->scount),
				fcount + group_3aa->async_shots,
				*last_fcount0, *last_fcount1, group_3aa->pcount);
		}
		g_print_cnt++;
	} else {
		g_print_cnt = 0;
		atomic_set(&group_3aa->sensor_fcount, fcount + group_3aa->async_shots);
		up(&group_3aa->smp_trigger);
	}

	v4l2_subdev_notify(subdev, FLITE_NOTIFY_FSTART, &fcount);
}

static void tasklet_flite_str1(unsigned long data)
{
	struct v4l2_subdev *subdev;
	struct fimc_is_device_flite *flite;
	u32 bstart, fcount, present;

	subdev = (struct v4l2_subdev *)data;
	flite = v4l2_get_subdevdata(subdev);
	if (!flite) {
		err("flite is NULL");
		BUG();
	}

	present = flite_hw_get_present_frame_buffer(flite->base_reg) - 1;
	bstart = flite->tasklet_param_str;
	fcount = atomic_read(&flite->fcount);

#ifdef TASKLET_MSG
	info("S%d %d\n", bstart, fcount);
#endif

	/* comparing sw state and hw state */
	if (atomic_read(&flite->bcount) == 2) {
		if ((bstart == FLITE_A_SLOT_VALID) &&
			(present != FLITE_A_SLOT_VALID)) {
			mwarn("wrong SW buffer slot A(sw:%d != hw:%d)", flite, bstart, present);
			flite->sw_trigger = bstart = FLITE_B_SLOT_VALID;
		}

		if ((bstart == FLITE_B_SLOT_VALID) &&
			(present != FLITE_B_SLOT_VALID)) {
			mwarn("wrong SW buffer slot B(sw:%d != hw:%d)", flite, bstart, present);
			flite->sw_trigger = bstart = FLITE_A_SLOT_VALID;
		}
	}

	if (flite->buf_done_mode == FLITE_BUF_DONE_EARLY) {
		flite->early_work_called = false;
		if (flite->early_work_skip == true) {
			info("flite early work skip");
			flite->early_work_skip = false;
		} else {
			queue_delayed_work(flite->early_workqueue, &flite->early_work_wq,
					msecs_to_jiffies(flite->buf_done_wait_time));
		}
	}

	v4l2_subdev_notify(subdev, FLITE_NOTIFY_FSTART, &fcount);
}

static void tasklet_flite_end(unsigned long data)
{
	struct fimc_is_device_flite *flite;
	struct fimc_is_framemgr *framemgr;
	struct fimc_is_frame *frame;
	struct fimc_is_frame *frame_done;
	struct v4l2_subdev *subdev;
	u32 bdone;

	frame_done = NULL;
	subdev = (struct v4l2_subdev *)data;
	flite = v4l2_get_subdevdata(subdev);
	if (!flite) {
		err("flite is NULL");
		BUG();
	}

	if ((flite->buf_done_mode == FLITE_BUF_DONE_EARLY) &&
		test_bit(FLITE_LAST_CAPTURE, &flite->state)) {
		info("Skip due to Last Frame Capture\n");
		return;
	}

#ifdef ENABLE_FLITE_OVERFLOW_STOP
	if (unlikely(flite->overflow_cnt)) {
		flite_set_ovf_stop(flite);
		goto ovf_exit;
	}
#endif

	framemgr = flite->framemgr;
	bdone = flite->tasklet_param_end;

#ifdef TASKLET_MSG
	info("E%d %d\n", bdone, atomic_read(&flite->fcount));
#endif

	if (flite_hw_check_ovf_interrupt_source(flite->base_reg))
		flite_hw_set_ovf_interrupt_source(flite->base_reg);

	framemgr_e_barrier(framemgr, FMGR_IDX_1 + bdone);

	if (test_bit(bdone, &flite->state)) {
		fimc_is_frame_process_head(framemgr, &frame);
		if (frame) {
#ifdef MEASURE_TIME
#ifdef EXTERNAL_TIME
			do_gettimeofday(&frame->tzone[TM_FLITE_END]);
#endif
#endif
			/* 1. current frame transition to completion */
			frame_done = frame;

			/* 2. next frame ready */
			fimc_is_frame_request_head(framemgr, &frame);
			if (frame) {
				flite_s_buffer_addr(flite, bdone,
					frame->dvaddr_buffer[0]);
				set_bit(bdone, &flite->state);
				fimc_is_frame_trans_req_to_pro(framemgr, frame);
			} else {
				if (!flite_s_unuse_buffer(flite, bdone)) {
					clear_bit(bdone, &flite->state);
#ifdef TASKLET_MSG
					merr("[SEN] request is empty0(%d slot)", flite, bdone);
#endif
				} else {
					frame_done = NULL;
				}
			}

			if (frame_done)
				fimc_is_frame_trans_pro_to_com(framemgr, frame_done);

		} else {
#ifdef TASKLET_MSG
			merr("[SEN] process is empty(%d, %ld)", flite, bdone, flite->state);
			fimc_is_frame_print_all(framemgr);
#endif
		}
	} else {
		fimc_is_frame_request_head(framemgr, &frame);
		if (frame) {
			flite_s_buffer_addr(flite, bdone, frame->dvaddr_buffer[0]);
			if (!flite_s_use_buffer(flite, bdone)) {
				set_bit(bdone, &flite->state);
				fimc_is_frame_trans_req_to_pro(framemgr, frame);

				/* swap process queue list */
				fimc_is_frame_swap_process_head(framemgr);
			}
		} else {
#ifdef TASKLET_MSG
			merr("request is empty1(%d slot)", flite, bdone);
			fimc_is_frame_print_all(framemgr);
#endif
		}
	}

	framemgr_x_barrier(framemgr, FMGR_IDX_1 + bdone);

#ifdef ENABLE_FLITE_OVERFLOW_STOP
ovf_exit:
#endif
	v4l2_subdev_notify(subdev, FLITE_NOTIFY_FEND, frame_done);
	flite->early_work_called = true;
}

static void tasklet_flite_end_chk(unsigned long data)
{
	struct v4l2_subdev *subdev = NULL;
	struct fimc_is_device_flite *flite = NULL;
	subdev = (struct v4l2_subdev *)data;
	flite = v4l2_get_subdevdata(subdev);

	if (flite->early_work_called == false) {
		u32 fcount = atomic_read(&flite->fcount);
		info("[CamIF%d][%08d] delayed work queue was slower than end irq",
			flite->instance, fcount);
	}

	return;
}

#ifdef SUPPORTED_EARLY_BUF_DONE
static void wq_func_flite_early_work(struct work_struct *data)
{
	struct fimc_is_device_flite *flite = NULL;
	struct delayed_work *early_work_wq = NULL;

	early_work_wq = container_of(data, struct delayed_work,
		work);
	flite = container_of(early_work_wq, struct fimc_is_device_flite,
		early_work_wq);

	if (!flite) {
		err("flite is NULL");
		BUG();
	}

	flite->tasklet_param_end = flite->sw_trigger;
	tasklet_schedule(&flite->tasklet_flite_early_end);
}

static void chk_early_buf_done(struct fimc_is_device_flite *flite, u32 framerate, u32 position)
{
	/* ms */
	u32 margin = 0;
	u32 duration = 0;

	/* HACK: applied on 15~30fps forcely */
	if (framerate > 15 && framerate <= 30) {

		duration = 1000 / framerate;

		if (position == SENSOR_POSITION_REAR)
			flite->early_buf_done_mode = FLITE_BUF_EARLY_30P;
		else
			flite->early_buf_done_mode = FLITE_BUF_EARLY_20P;

		margin = FLITE_VVALID_TIME_BASE * (flite->early_buf_done_mode * 0.1f);

		if (margin >= duration) {
			/* normal buffer done mode */
			flite->buf_done_mode = FLITE_BUF_DONE_NORMAL;
			flite->early_buf_done_mode = FLITE_BUF_EARLY_NOTHING;
			flite->buf_done_wait_time = 0;
		} else {
			/* early buffer done mode */
			flite->buf_done_mode = FLITE_BUF_DONE_EARLY;
			flite->buf_done_wait_time = duration - margin;
		}
	} else {
		/* normal buffer done mode */
		flite->buf_done_mode = FLITE_BUF_DONE_NORMAL;
		flite->early_buf_done_mode = FLITE_BUF_EARLY_NOTHING;
		flite->buf_done_wait_time = 0;
	}

	info("[CamIF%d] buffer done mode [m%d/em%d/du%d/mg%d/wt%d]", flite->instance,
		flite->buf_done_mode, flite->early_buf_done_mode, duration, margin,
		flite->buf_done_wait_time);
}
#endif

static inline void notify_fcount(struct fimc_is_device_flite *flite)
{
	if (test_bit(FLITE_JOIN_ISCHAIN, &flite->state)) {
		if (flite->instance== FLITE_ID_A)
			writel(atomic_read(&flite->fcount), notify_fcount_sen0);
		else if (flite->instance == FLITE_ID_B)
			writel(atomic_read(&flite->fcount), notify_fcount_sen1);
		else if (flite->instance == FLITE_ID_C)
			writel(atomic_read(&flite->fcount), notify_fcount_sen2);
		else
			err("unresolved channel(%d)", flite->instance);
	}
}

static irqreturn_t fimc_is_flite_isr(int irq, void *data)
{
	u32 status, status1, status2, i;
	struct fimc_is_device_flite *flite;

	flite = data;
	status1 = flite_hw_getnclr_status1(flite->base_reg);
	status = status1 & (3 << 4);

	if (test_bit(FLITE_LAST_CAPTURE, &flite->state)) {
		if (status1) {
			info("[CamIF%d] last status1 : 0x%08X\n", flite->instance, status1);
			goto clear_status;
		}

		err("[CamIF%d] unintended intr is occured", flite->instance);

		for (i = 0; i < 278; i += 4)
			info("REG[%X] : 0x%08X\n", i, readl(flite->base_reg + i));

		flite_hw_force_reset(flite->base_reg);

		goto clear_status;
	}

	if (status) {
		if (status == (3 << 4)) {
#ifdef DBG_FLITEISR
			printk(KERN_CONT "*");
#endif
			/* frame both interrupt since latency */
			if (flite->sw_checker) {
#ifdef DBG_FLITEISR
				printk(KERN_CONT ">");
#endif
				/* frame end interrupt */
				flite->sw_checker = EXPECT_FRAME_START;
				flite->tasklet_param_end = flite->sw_trigger;
				tasklet_schedule(&flite->tasklet_flite_end);
#ifdef DBG_FLITEISR
				printk(KERN_CONT "<");
#endif
				/* frame start interrupt */
				flite->sw_checker = EXPECT_FRAME_END;
				if (flite->sw_trigger)
					flite->sw_trigger = FLITE_A_SLOT_VALID;
				else
					flite->sw_trigger = FLITE_B_SLOT_VALID;
				flite->tasklet_param_str = flite->sw_trigger;
				atomic_inc(&flite->fcount);
				notify_fcount(flite);
				tasklet_schedule(&flite->tasklet_flite_str);
			} else {
				/* W/A: Skip start tasklet at interrupt lost case */
				warn("[CamIF%d] invalid interrupt interval",
					flite->instance);
				goto clear_status;
/* HACK: Disable dead code because of Prevent Issue */
			}
		} else if (status == (2 << 4)) {
			/* W/A: Skip start tasklet at interrupt lost case */
			if (flite->sw_checker != EXPECT_FRAME_START)
				warn("[CamIF%d] Lost end interupt\n",
					flite->instance);
#ifdef DBG_FLITEISR
			printk(KERN_CONT "<");
#endif
			/* frame start interrupt */
			flite->sw_checker = EXPECT_FRAME_END;
			if (flite->sw_trigger)
				flite->sw_trigger = FLITE_A_SLOT_VALID;
			else
				flite->sw_trigger = FLITE_B_SLOT_VALID;
			flite->tasklet_param_str = flite->sw_trigger;
			atomic_inc(&flite->fcount);
			notify_fcount(flite);
			tasklet_schedule(&flite->tasklet_flite_str);
		} else {
			/* W/A: Skip end tasklet at interrupt lost case */
			if (flite->sw_checker != EXPECT_FRAME_END) {
				warn("[CamIF%d] Lost start interupt\n",
					flite->instance);
				goto clear_status;
			}
#ifdef DBG_FLITEISR
			printk(KERN_CONT ">");
#endif
			/* frame end interrupt */
			flite->sw_checker = EXPECT_FRAME_START;
			if (flite->buf_done_mode == FLITE_BUF_DONE_NORMAL)
				flite->tasklet_param_end = flite->sw_trigger;
			tasklet_schedule(&flite->tasklet_flite_end);
		}
	}

clear_status:
	if (status1 & (1 << 6)) {
		/* Last Frame Capture Interrupt */
		info("[CamIF%d] Last Frame Capture(fcount : %d)\n",
			flite->instance, atomic_read(&flite->fcount));

		/* Clear LastCaptureEnd bit */
		status2 = flite_hw_get_status2(flite->base_reg);
		status2 &= ~(0x1 << 1);
		flite_hw_set_status2(flite->base_reg, status2);

		/* Notify last capture */
		set_bit(FLITE_LAST_CAPTURE, &flite->state);
		wake_up(&flite->wait_queue);
	}

	if (status1 & (1 << 8)) {
		u32 ciwdofst;

		flite_hw_clr_ovf_interrupt_source(flite->base_reg);
		flite->overflow_cnt++;

		if ((flite->overflow_cnt % FLITE_OVERFLOW_COUNT == 0) ||
			(flite->overflow_cnt < FLITE_OVERFLOW_COUNT))
			pr_err("[CamIF%d] OFCR(cnt:%u)\n", flite->instance, flite->overflow_cnt);
		ciwdofst = readl(flite->base_reg + TO_WORD_OFFSET(0x10));
		ciwdofst  |= (0x1 << 14);
		writel(ciwdofst, flite->base_reg + TO_WORD_OFFSET(0x10));
		ciwdofst  &= ~(0x1 << 14);
		writel(ciwdofst, flite->base_reg + TO_WORD_OFFSET(0x10));
	}

	if (status1 & (1 << 9)) {
		u32 ciwdofst;

		flite_hw_clr_ovf_interrupt_source(flite->base_reg);
		flite->overflow_cnt++;

		if ((flite->overflow_cnt % FLITE_OVERFLOW_COUNT == 0) ||
			(flite->overflow_cnt < FLITE_OVERFLOW_COUNT))
			pr_err("[CamIF%d] OFCB(cnt:%u)\n", flite->instance, flite->overflow_cnt);
		ciwdofst = readl(flite->base_reg + TO_WORD_OFFSET(0x10));
		ciwdofst  |= (0x1 << 15);
		writel(ciwdofst, flite->base_reg + TO_WORD_OFFSET(0x10));
		ciwdofst  &= ~(0x1 << 15);
		writel(ciwdofst, flite->base_reg + TO_WORD_OFFSET(0x10));
	}

	if (status1 & (1 << 10)) {
		u32 ciwdofst;

		flite_hw_clr_ovf_interrupt_source(flite->base_reg);
		flite->overflow_cnt++;

		if ((flite->overflow_cnt % FLITE_OVERFLOW_COUNT == 0) ||
			(flite->overflow_cnt < FLITE_OVERFLOW_COUNT)) {
			pr_err("[CamIF%d] OFY(cnt:%u)\n", flite->instance, flite->overflow_cnt);
#ifdef ENABLE_FLITE_OVERFLOW_STOP
			tasklet_schedule(&flite->tasklet_flite_end);
#endif
		}
		ciwdofst = readl(flite->base_reg + TO_WORD_OFFSET(0x10));
		ciwdofst  |= (0x1 << 30);
		writel(ciwdofst, flite->base_reg + TO_WORD_OFFSET(0x10));
		ciwdofst  &= ~(0x1 << 30);
		writel(ciwdofst, flite->base_reg + TO_WORD_OFFSET(0x10));
	}

	return IRQ_HANDLED;
}

int fimc_is_flite_open(struct v4l2_subdev *subdev,
	struct fimc_is_framemgr *framemgr)
{
	int ret = 0;
	struct fimc_is_device_flite *flite;
	struct fimc_is_core *core;

	core = (struct fimc_is_core *)dev_get_drvdata(fimc_is_dev);

	BUG_ON(!core);
	BUG_ON(!subdev);
	BUG_ON(!framemgr);

	flite = v4l2_get_subdevdata(subdev);
	if (!flite) {
		err("flite is NULL");
		ret = -EINVAL;
		goto p_err;
	}

	flite->group = 0;
	flite->framemgr = framemgr;
	atomic_set(&flite->fcount, 0);
	atomic_set(&flite->bcount, 0);

	clear_bit(FLITE_JOIN_ISCHAIN, &flite->state);
	clear_bit(FLITE_OTF_WITH_3AA, &flite->state);
	clear_bit(FLITE_LAST_CAPTURE, &flite->state);
	clear_bit(FLITE_A_SLOT_VALID, &flite->state);
	clear_bit(FLITE_B_SLOT_VALID, &flite->state);

	ret = of_irq_get(core->lite_np[flite->instance], 0);
	if (ret > 0)
		ret = request_irq(ret, fimc_is_flite_isr,
			IRQF_SHARED, "fimc-lite",
			flite);
	if (ret)
		err("request_irq(L%d) failed\n", flite->instance);

p_err:
	return ret;
}

int fimc_is_flite_close(struct v4l2_subdev *subdev)
{
	int ret = 0;
	struct fimc_is_device_flite *flite;
	struct fimc_is_core *core;

	core = (struct fimc_is_core *)dev_get_drvdata(fimc_is_dev);

	BUG_ON(!core);
	BUG_ON(!subdev);

	flite = v4l2_get_subdevdata(subdev);
	if (!flite) {
		err("flite is NULL");
		ret = -EINVAL;
		goto p_err;
	}

	free_irq(of_irq_get(core->lite_np[flite->instance], 0), flite);

p_err:
	return ret;
}

/* value : csi ch */
static int flite_init(struct v4l2_subdev *subdev, u32 value)
{
	int ret = 0;
	struct fimc_is_device_flite *flite;

	BUG_ON(!subdev);

	flite = v4l2_get_subdevdata(subdev);
	if (!flite) {
		err("flite is NULL");
		ret = -EINVAL;
		goto p_err;
	}

	flite->csi = value;

p_err:
	return ret;
}

static int flite_stream_on(struct v4l2_subdev *subdev,
	struct fimc_is_device_flite *flite)
{
	int ret = 0;
	u32 otf_setting, bns;
	bool buffer_ready;
	unsigned long flags;
	struct fimc_is_image *image;
	struct fimc_is_framemgr *framemgr;
	struct fimc_is_frame *frame;
	struct fimc_is_device_sensor *device = v4l2_get_subdev_hostdata(subdev);

	BUG_ON(!flite);
	BUG_ON(!flite->framemgr);
	BUG_ON(!device);

	otf_setting = 0;
	buffer_ready = false;
	framemgr = flite->framemgr;
	image = &flite->image;
	bns = device->pdata->is_bns;

	flite->overflow_cnt = 0;
	flite->sw_trigger = FLITE_B_SLOT_VALID;
	flite->sw_checker = EXPECT_FRAME_START;
	flite->tasklet_param_str = 0;
	flite->tasklet_param_end = 0;
	atomic_set(&flite->bcount, 0);
	clear_bit(FLITE_LAST_CAPTURE, &flite->state);
	clear_bit(FLITE_A_SLOT_VALID, &flite->state);
	clear_bit(FLITE_B_SLOT_VALID, &flite->state);

	/* 1. init */
	flite_hw_force_reset(flite->base_reg);
	init_fimc_lite(flite->base_reg);

	/* 2. dma setting */
	framemgr_e_barrier_irqs(framemgr, 0, flags);

	if (framemgr->frame_req_cnt >= 1) {
		fimc_is_frame_request_head(framemgr, &frame);
		flite_s_use_buffer(flite, FLITE_A_SLOT_VALID);
		flite_s_buffer_addr(flite, FLITE_A_SLOT_VALID, frame->dvaddr_buffer[0]);
		set_bit(FLITE_A_SLOT_VALID, &flite->state);
		fimc_is_frame_trans_req_to_pro(framemgr, frame);
		buffer_ready = true;
	}

	if (framemgr->frame_req_cnt >= 1) {
		fimc_is_frame_request_head(framemgr, &frame);
		flite_s_use_buffer(flite, FLITE_B_SLOT_VALID);
		flite_s_buffer_addr(flite, FLITE_B_SLOT_VALID, frame->dvaddr_buffer[0]);
		set_bit(FLITE_B_SLOT_VALID, &flite->state);
		fimc_is_frame_trans_req_to_pro(framemgr, frame);
		buffer_ready = true;
	}

	framemgr_x_barrier_irqr(framemgr, 0, flags);

	flite_hw_set_output_dma(flite->base_reg, buffer_ready, image->format.pixelformat);

	/* 3. otf setting */
	if (device->ischain)
		set_bit(FLITE_JOIN_ISCHAIN, &flite->state);
	else
		clear_bit(FLITE_JOIN_ISCHAIN, &flite->state);

	if (device->ischain && IS_ISCHAIN_OTF(device->ischain)) {
		tasklet_init(&flite->tasklet_flite_str, tasklet_flite_str0, (unsigned long)subdev);
		tasklet_init(&flite->tasklet_flite_end, tasklet_flite_end, (unsigned long)subdev);

		if (device->ischain->group_3aa.id == GROUP_ID_3A0) {
			flite->group = GROUP_ID_3A0;
		} else if (device->ischain->group_3aa.id == GROUP_ID_3A1) {
			flite->group = GROUP_ID_3A1;
		} else {
			merr("invalid otf path(%d)", device, device->ischain->group_3aa.id);
			ret = -EINVAL;
			goto p_err;
		}

		mdbgd_back("Enabling OTF path. target 3aa(%d)\n", flite, flite->group);
		if (flite->instance == FLITE_ID_A) {
			if (flite->group == GROUP_ID_3A0)
				otf_setting = FLITE_REG_CIGENERAL_CAM_A;
			else
				otf_setting = FLITE_REG_CIGENERAL_3AA1_CAM_A;
		} else if (flite->instance == FLITE_ID_B) {
			if (flite->group == GROUP_ID_3A0)
				otf_setting = FLITE_REG_CIGENERAL_CAM_B;
			else
				otf_setting = FLITE_REG_CIGENERAL_3AA1_CAM_B;
		} else {
			merr("invalid FLITE channel for OTF setting", flite);
			ret = -EINVAL;
			goto p_err;
		}

		flite_hw_set_output_local(flite->base_reg, true);
		set_bit(FLITE_OTF_WITH_3AA, &flite->state);
	} else {
		switch (flite->buf_done_mode) {
		case FLITE_BUF_DONE_NORMAL:
			tasklet_init(&flite->tasklet_flite_str, tasklet_flite_str1, (unsigned long)subdev);
			tasklet_init(&flite->tasklet_flite_end, tasklet_flite_end, (unsigned long)subdev);
			break;
		case FLITE_BUF_DONE_EARLY:
			flite->early_work_skip = false;
			flite->early_work_called = false;
			tasklet_init(&flite->tasklet_flite_str, tasklet_flite_str1, (unsigned long)subdev);
			tasklet_init(&flite->tasklet_flite_early_end, tasklet_flite_end, (unsigned long)subdev);
			tasklet_init(&flite->tasklet_flite_end, tasklet_flite_end_chk, (unsigned long)subdev);
			break;
		default:
			tasklet_init(&flite->tasklet_flite_str, tasklet_flite_str1, (unsigned long)subdev);
			tasklet_init(&flite->tasklet_flite_end, tasklet_flite_end, (unsigned long)subdev);
			break;
		}

		flite_hw_set_output_local(flite->base_reg, false);
		clear_bit(FLITE_OTF_WITH_3AA, &flite->state);
	}

	/* 4. register setting */
	start_fimc_lite(flite->base_reg, image, otf_setting, bns, flite->csi, flite->instance);

p_err:
	return ret;
}

static int flite_stream_off(struct v4l2_subdev *subdev,
	struct fimc_is_device_flite *flite,
	bool nowait)
{
	int ret = 0;
	unsigned long __iomem *base_reg;
	unsigned long flags;
	struct fimc_is_framemgr *framemgr;
	struct fimc_is_frame *frame;

	BUG_ON(!flite);
	BUG_ON(!flite->base_reg);
	BUG_ON(!flite->framemgr);

	base_reg = flite->base_reg;
	framemgr = flite->framemgr;

	/* for preventing invalid memory access */
	flite_hw_set_unuse_buffer(base_reg, FLITE_A_SLOT_VALID);
	flite_hw_set_unuse_buffer(base_reg, FLITE_B_SLOT_VALID);
	flite_hw_set_output_dma(base_reg, false, flite->image.format.pixelformat);
	flite_hw_set_output_local(base_reg, false);

	stop_fimc_lite(base_reg);
	if (!nowait) {
		u32 timetowait;

		timetowait = wait_event_timeout(flite->wait_queue,
			test_bit(FLITE_LAST_CAPTURE, &flite->state),
			FIMC_IS_FLITE_STOP_TIMEOUT);
		if (!timetowait) {
			/* forcely stop */
			stop_fimc_lite(base_reg);
			set_bit(FLITE_LAST_CAPTURE, &flite->state);
			err("last capture timeout:%s", __func__);
			msleep(200);
			flite_hw_force_reset(base_reg);
			ret = -ETIME;
		}
	} else {
		if (flite->buf_done_mode == FLITE_BUF_DONE_EARLY)
			flush_delayed_work(&flite->early_work_wq);
		/*
		 * DTP test can make iommu fault because senosr is streaming
		 * therefore it need  force reset
		 */
		flite_hw_force_reset(base_reg);
	}

	if (flite->buf_done_mode == FLITE_BUF_DONE_EARLY)
		cancel_delayed_work_sync(&flite->early_work_wq);

	/* clr interrupt source */
	flite_hw_clr_interrupt_source(base_reg);

	framemgr_e_barrier_irqs(framemgr, FMGR_IDX_3, flags);

	fimc_is_frame_complete_head(framemgr, &frame);
	while (frame) {
		fimc_is_frame_trans_com_to_fre(framemgr, frame);
		fimc_is_frame_complete_head(framemgr, &frame);
	}

	fimc_is_frame_process_head(framemgr, &frame);
	while (frame) {
		fimc_is_frame_trans_pro_to_fre(framemgr, frame);
		fimc_is_frame_process_head(framemgr, &frame);
	}

	fimc_is_frame_request_head(framemgr, &frame);
	while (frame) {
		fimc_is_frame_trans_req_to_fre(framemgr, frame);
		fimc_is_frame_request_head(framemgr, &frame);
	}

	/* buffer done mode init */
	flite->buf_done_mode = FLITE_BUF_DONE_NORMAL;
	flite->early_buf_done_mode = FLITE_BUF_EARLY_NOTHING;

	framemgr_x_barrier_irqr(framemgr, FMGR_IDX_3, flags);

	return ret;
}

/*
 * enable
 * @X0 : disable
 * @X1 : enable
 * @1X : no waiting flag
 * @0X : waiting flag
 */
static int flite_s_stream(struct v4l2_subdev *subdev, int enable)
{
	int ret = 0;
	bool nowait;
	struct fimc_is_device_flite *flite;

	BUG_ON(!subdev);

	nowait = (enable & FLITE_NOWAIT_MASK) >> FLITE_NOWAIT_SHIFT;
	enable = enable & FLITE_ENABLE_MASK;

	flite = (struct fimc_is_device_flite *)v4l2_get_subdevdata(subdev);
	if (!flite) {
		err("flite is NULL");
		ret = -EINVAL;
		goto p_err;
	}

	if (enable) {
		ret = flite_stream_on(subdev, flite);
		if (ret) {
			err("flite_stream_on is fail(%d)", ret);
			goto p_err;
		}
	} else {
		ret = flite_stream_off(subdev, flite, nowait);
		if (ret) {
			err("flite_stream_off is fail(%d)", ret);
			goto p_err;
		}
	}

p_err:
	mdbgd_back("%s(%d, %d)\n", flite, __func__, enable, ret);
	return 0;
}

static int flite_s_format(struct v4l2_subdev *subdev, struct v4l2_mbus_framefmt *fmt)
{
	int ret = 0;
	struct fimc_is_device_flite *flite;

	BUG_ON(!subdev);
	BUG_ON(!fmt);

	flite = (struct fimc_is_device_flite *)v4l2_get_subdevdata(subdev);
	if (!flite) {
		err("flite is NULL");
		ret = -EINVAL;
		goto p_err;
	}

	flite->image.window.offs_h = 0;
	flite->image.window.offs_v = 0;
	flite->image.window.width = fmt->width;
	flite->image.window.height = fmt->height;
	flite->image.window.o_width = fmt->width;
	flite->image.window.o_height = fmt->height;
	flite->image.format.pixelformat = fmt->code;

p_err:
	mdbgd_back("%s(%dx%d, %X)\n", flite, __func__, fmt->width, fmt->height, fmt->code);
	return ret;
}

static int flite_s_ctrl(struct v4l2_subdev *subdev, struct v4l2_control *ctrl)
{
	int ret = 0;
	struct fimc_is_device_flite *flite;

	BUG_ON(!subdev);
	BUG_ON(!ctrl);

	flite = (struct fimc_is_device_flite *)v4l2_get_subdevdata(subdev);
	if (!flite) {
		err("flite is NULL");
		ret = -EINVAL;
		goto p_err;
	}

	switch (ctrl->id) {
	case V4L2_CID_IS_S_BNS:
		{
			u32 width, height, ratio;

			width = flite->image.window.width;
			height = flite->image.window.height;
			ratio = ctrl->value;

			flite->image.window.otf_width
				= rounddown((width * 1000 / ratio), 4);
			flite->image.window.otf_height
				= rounddown((height * 1000 / ratio), 2);
		}
		break;
	default:
		err("unsupported ioctl(%d)\n", ctrl->id);
		ret = -EINVAL;
		break;
	}

p_err:
	return ret;
}

static const struct v4l2_subdev_core_ops core_ops = {
	.init = flite_init,
	.s_ctrl = flite_s_ctrl,
};

static const struct v4l2_subdev_video_ops video_ops = {
	.s_stream = flite_s_stream,
	.s_mbus_fmt = flite_s_format
};

static const struct v4l2_subdev_ops subdev_ops = {
	.core = &core_ops,
	.video = &video_ops
};

int fimc_is_flite_probe(struct fimc_is_device_sensor *device,
	u32 instance)
{
	int ret = 0;
	struct v4l2_subdev *subdev_flite;
	struct fimc_is_device_flite *flite;
	struct fimc_is_core *core;

	core = dev_get_drvdata(fimc_is_dev);

	BUG_ON(!core);
	BUG_ON(!device);

	subdev_flite = kzalloc(sizeof(struct v4l2_subdev), GFP_KERNEL);
	if (!subdev_flite) {
		merr("subdev_flite is NULL", device);
		ret = -ENOMEM;
		goto err_alloc_subdev_flite;
	}
	device->subdev_flite = subdev_flite;

	flite = kzalloc(sizeof(struct fimc_is_device_flite), GFP_KERNEL);
	if (!flite) {
		merr("flite is NULL", device);
		ret = -ENOMEM;
		goto err_alloc_flite;
	}

	/* pointer to me from device sensor */
	flite->subdev = &device->subdev_flite;
	flite->instance = instance;
	init_waitqueue_head(&flite->wait_queue);

	flite->base_reg = of_iomap(core->lite_np[instance], 0);
	if (!flite->base_reg)
		goto err_reg_v4l2_subdev;

	v4l2_subdev_init(subdev_flite, &subdev_ops);
	v4l2_set_subdevdata(subdev_flite, (void *)flite);
	v4l2_set_subdev_hostdata(subdev_flite, device);
	snprintf(subdev_flite->name, V4L2_SUBDEV_NAME_SIZE, "flite-subdev.%d", instance);
	ret = v4l2_device_register_subdev(&device->v4l2_dev, subdev_flite);
	if (ret) {
		merr("v4l2_device_register_subdev is fail(%d)", device, ret);
		goto err_reg_v4l2_subdev;
	}

	/* buffer done mode is normal (default) */
	flite->buf_done_mode = FLITE_BUF_DONE_NORMAL;
	flite->early_buf_done_mode = FLITE_BUF_EARLY_NOTHING;
	flite->chk_early_buf_done = NULL;
#ifdef SUPPORTED_EARLY_BUF_DONE
	flite->chk_early_buf_done = chk_early_buf_done;
	flite->early_workqueue = alloc_workqueue("fimc-is/early_workqueue/highpri", WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
	if (!flite->early_workqueue) {
		warn("failed to alloc own workqueue, will be use global one");
		goto err_reg_v4l2_subdev;
	} else {
		INIT_DELAYED_WORK(&flite->early_work_wq, wq_func_flite_early_work);
	}
#endif
	info("[BAK:D:%d] %s(%d)\n", instance, __func__, ret);
	return 0;

err_reg_v4l2_subdev:
	kfree(flite);

err_alloc_flite:
	kfree(subdev_flite);
	device->subdev_flite = NULL;

err_alloc_subdev_flite:
	err("[BAK:D:%d] %s(%d)\n", instance, __func__, ret);
	return ret;
}