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
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
|
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2021 Purism SPC
#include <asm/unaligned.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/pm.h>
#include <linux/property.h>
#include <linux/regulator/consumer.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#define HI846_MEDIA_BUS_FORMAT MEDIA_BUS_FMT_SGBRG10_1X10
#define HI846_RGB_DEPTH 10
/* Frame length lines / vertical timings */
#define HI846_REG_FLL 0x0006
#define HI846_FLL_MAX 0xffff
/* Horizontal timing */
#define HI846_REG_LLP 0x0008
#define HI846_LINE_LENGTH 3800
#define HI846_REG_BINNING_MODE 0x000c
#define HI846_REG_IMAGE_ORIENTATION 0x000e
#define HI846_REG_UNKNOWN_0022 0x0022
#define HI846_REG_Y_ADDR_START_VACT_H 0x0026
#define HI846_REG_Y_ADDR_START_VACT_L 0x0027
#define HI846_REG_UNKNOWN_0028 0x0028
#define HI846_REG_Y_ADDR_END_VACT_H 0x002c
#define HI846_REG_Y_ADDR_END_VACT_L 0x002d
#define HI846_REG_Y_ODD_INC_FOBP 0x002e
#define HI846_REG_Y_EVEN_INC_FOBP 0x002f
#define HI846_REG_Y_ODD_INC_VACT 0x0032
#define HI846_REG_Y_EVEN_INC_VACT 0x0033
#define HI846_REG_GROUPED_PARA_HOLD 0x0046
#define HI846_REG_TG_ENABLE 0x004c
#define HI846_REG_UNKNOWN_005C 0x005c
#define HI846_REG_UNKNOWN_006A 0x006a
/*
* Long exposure time. Actually, exposure is a 20 bit value that
* includes the lower 4 bits of 0x0073 too. Only 16 bits are used
* right now
*/
#define HI846_REG_EXPOSURE 0x0074
#define HI846_EXPOSURE_MIN 6
#define HI846_EXPOSURE_MAX_MARGIN 2
#define HI846_EXPOSURE_STEP 1
/* Analog gain controls from sensor */
#define HI846_REG_ANALOG_GAIN 0x0077
#define HI846_ANAL_GAIN_MIN 0
#define HI846_ANAL_GAIN_MAX 240
#define HI846_ANAL_GAIN_STEP 8
/* Digital gain controls from sensor */
#define HI846_REG_MWB_GR_GAIN_H 0x0078
#define HI846_REG_MWB_GR_GAIN_L 0x0079
#define HI846_REG_MWB_GB_GAIN_H 0x007a
#define HI846_REG_MWB_GB_GAIN_L 0x007b
#define HI846_REG_MWB_R_GAIN_H 0x007c
#define HI846_REG_MWB_R_GAIN_L 0x007d
#define HI846_REG_MWB_B_GAIN_H 0x007e
#define HI846_REG_MWB_B_GAIN_L 0x007f
#define HI846_DGTL_GAIN_MIN 512
#define HI846_DGTL_GAIN_MAX 8191
#define HI846_DGTL_GAIN_STEP 1
#define HI846_DGTL_GAIN_DEFAULT 512
#define HI846_REG_X_ADDR_START_HACT_H 0x0120
#define HI846_REG_X_ADDR_END_HACT_H 0x0122
#define HI846_REG_UNKNOWN_012A 0x012a
#define HI846_REG_UNKNOWN_0200 0x0200
#define HI846_REG_UNKNOWN_021C 0x021c
#define HI846_REG_UNKNOWN_021E 0x021e
#define HI846_REG_UNKNOWN_0402 0x0402
#define HI846_REG_UNKNOWN_0404 0x0404
#define HI846_REG_UNKNOWN_0408 0x0408
#define HI846_REG_UNKNOWN_0410 0x0410
#define HI846_REG_UNKNOWN_0412 0x0412
#define HI846_REG_UNKNOWN_0414 0x0414
#define HI846_REG_UNKNOWN_0418 0x0418
#define HI846_REG_UNKNOWN_051E 0x051e
/* Formatter */
#define HI846_REG_X_START_H 0x0804
#define HI846_REG_X_START_L 0x0805
/* MIPI */
#define HI846_REG_UNKNOWN_0900 0x0900
#define HI846_REG_MIPI_TX_OP_EN 0x0901
#define HI846_REG_MIPI_TX_OP_MODE 0x0902
#define HI846_RAW8 BIT(5)
#define HI846_REG_UNKNOWN_090C 0x090c
#define HI846_REG_UNKNOWN_090E 0x090e
#define HI846_REG_UNKNOWN_0914 0x0914
#define HI846_REG_TLPX 0x0915
#define HI846_REG_TCLK_PREPARE 0x0916
#define HI846_REG_TCLK_ZERO 0x0917
#define HI846_REG_UNKNOWN_0918 0x0918
#define HI846_REG_THS_PREPARE 0x0919
#define HI846_REG_THS_ZERO 0x091a
#define HI846_REG_THS_TRAIL 0x091b
#define HI846_REG_TCLK_POST 0x091c
#define HI846_REG_TCLK_TRAIL_MIN 0x091d
#define HI846_REG_UNKNOWN_091E 0x091e
#define HI846_REG_UNKNOWN_0954 0x0954
#define HI846_REG_UNKNOWN_0956 0x0956
#define HI846_REG_UNKNOWN_0958 0x0958
#define HI846_REG_UNKNOWN_095A 0x095a
/* ISP Common */
#define HI846_REG_MODE_SELECT 0x0a00
#define HI846_MODE_STANDBY 0x00
#define HI846_MODE_STREAMING 0x01
#define HI846_REG_FAST_STANDBY_MODE 0x0a02
#define HI846_REG_ISP_EN_H 0x0a04
/* Test Pattern Control */
#define HI846_REG_ISP 0x0a05
#define HI846_REG_ISP_TPG_EN 0x01
#define HI846_REG_TEST_PATTERN 0x020a /* 1-9 */
#define HI846_REG_UNKNOWN_0A0C 0x0a0c
/* Windowing */
#define HI846_REG_X_OUTPUT_SIZE_H 0x0a12
#define HI846_REG_X_OUTPUT_SIZE_L 0x0a13
#define HI846_REG_Y_OUTPUT_SIZE_H 0x0a14
#define HI846_REG_Y_OUTPUT_SIZE_L 0x0a15
/* ISP Common */
#define HI846_REG_PEDESTAL_EN 0x0a1a
#define HI846_REG_UNKNOWN_0A1E 0x0a1e
/* Horizontal Binning Mode */
#define HI846_REG_HBIN_MODE 0x0a22
#define HI846_REG_UNKNOWN_0A24 0x0a24
#define HI846_REG_UNKNOWN_0B02 0x0b02
#define HI846_REG_UNKNOWN_0B10 0x0b10
#define HI846_REG_UNKNOWN_0B12 0x0b12
#define HI846_REG_UNKNOWN_0B14 0x0b14
/* BLC (Black Level Calibration) */
#define HI846_REG_BLC_CTL0 0x0c00
#define HI846_REG_UNKNOWN_0C06 0x0c06
#define HI846_REG_UNKNOWN_0C10 0x0c10
#define HI846_REG_UNKNOWN_0C12 0x0c12
#define HI846_REG_UNKNOWN_0C14 0x0c14
#define HI846_REG_UNKNOWN_0C16 0x0c16
#define HI846_REG_UNKNOWN_0E04 0x0e04
#define HI846_REG_CHIP_ID_L 0x0f16
#define HI846_REG_CHIP_ID_H 0x0f17
#define HI846_CHIP_ID_L 0x46
#define HI846_CHIP_ID_H 0x08
#define HI846_REG_UNKNOWN_0F04 0x0f04
#define HI846_REG_UNKNOWN_0F08 0x0f08
/* PLL */
#define HI846_REG_PLL_CFG_MIPI2_H 0x0f2a
#define HI846_REG_PLL_CFG_MIPI2_L 0x0f2b
#define HI846_REG_UNKNOWN_0F30 0x0f30
#define HI846_REG_PLL_CFG_RAMP1_H 0x0f32
#define HI846_REG_UNKNOWN_0F36 0x0f36
#define HI846_REG_PLL_CFG_MIPI1_H 0x0f38
#define HI846_REG_UNKNOWN_2008 0x2008
#define HI846_REG_UNKNOWN_326E 0x326e
struct hi846_reg {
u16 address;
u16 val;
};
struct hi846_reg_list {
u32 num_of_regs;
const struct hi846_reg *regs;
};
struct hi846_mode {
/* Frame width in pixels */
u32 width;
/* Frame height in pixels */
u32 height;
/* Horizontal timing size */
u32 llp;
/* Link frequency needed for this resolution */
u8 link_freq_index;
u16 fps;
/* Vertical timining size */
u16 frame_len;
const struct hi846_reg_list reg_list_config;
const struct hi846_reg_list reg_list_2lane;
const struct hi846_reg_list reg_list_4lane;
/* Position inside of the 3264x2448 pixel array */
struct v4l2_rect crop;
};
static const struct hi846_reg hi846_init_2lane[] = {
{HI846_REG_MODE_SELECT, 0x0000},
/* regs below are unknown */
{0x2000, 0x100a},
{0x2002, 0x00ff},
{0x2004, 0x0007},
{0x2006, 0x3fff},
{0x2008, 0x3fff},
{0x200a, 0xc216},
{0x200c, 0x1292},
{0x200e, 0xc01a},
{0x2010, 0x403d},
{0x2012, 0x000e},
{0x2014, 0x403e},
{0x2016, 0x0b80},
{0x2018, 0x403f},
{0x201a, 0x82ae},
{0x201c, 0x1292},
{0x201e, 0xc00c},
{0x2020, 0x4130},
{0x2022, 0x43e2},
{0x2024, 0x0180},
{0x2026, 0x4130},
{0x2028, 0x7400},
{0x202a, 0x5000},
{0x202c, 0x0253},
{0x202e, 0x0ad1},
{0x2030, 0x2360},
{0x2032, 0x0009},
{0x2034, 0x5020},
{0x2036, 0x000b},
{0x2038, 0x0002},
{0x203a, 0x0044},
{0x203c, 0x0016},
{0x203e, 0x1792},
{0x2040, 0x7002},
{0x2042, 0x154f},
{0x2044, 0x00d5},
{0x2046, 0x000b},
{0x2048, 0x0019},
{0x204a, 0x1698},
{0x204c, 0x000e},
{0x204e, 0x099a},
{0x2050, 0x0058},
{0x2052, 0x7000},
{0x2054, 0x1799},
{0x2056, 0x0310},
{0x2058, 0x03c3},
{0x205a, 0x004c},
{0x205c, 0x064a},
{0x205e, 0x0001},
{0x2060, 0x0007},
{0x2062, 0x0bc7},
{0x2064, 0x0055},
{0x2066, 0x7000},
{0x2068, 0x1550},
{0x206a, 0x158a},
{0x206c, 0x0004},
{0x206e, 0x1488},
{0x2070, 0x7010},
{0x2072, 0x1508},
{0x2074, 0x0004},
{0x2076, 0x0016},
{0x2078, 0x03d5},
{0x207a, 0x0055},
{0x207c, 0x08ca},
{0x207e, 0x2019},
{0x2080, 0x0007},
{0x2082, 0x7057},
{0x2084, 0x0fc7},
{0x2086, 0x5041},
{0x2088, 0x12c8},
{0x208a, 0x5060},
{0x208c, 0x5080},
{0x208e, 0x2084},
{0x2090, 0x12c8},
{0x2092, 0x7800},
{0x2094, 0x0802},
{0x2096, 0x040f},
{0x2098, 0x1007},
{0x209a, 0x0803},
{0x209c, 0x080b},
{0x209e, 0x3803},
{0x20a0, 0x0807},
{0x20a2, 0x0404},
{0x20a4, 0x0400},
{0x20a6, 0xffff},
{0x20a8, 0xf0b2},
{0x20aa, 0xffef},
{0x20ac, 0x0a84},
{0x20ae, 0x1292},
{0x20b0, 0xc02e},
{0x20b2, 0x4130},
{0x23fe, 0xc056},
{0x3232, 0xfc0c},
{0x3236, 0xfc22},
{0x3248, 0xfca8},
{0x326a, 0x8302},
{0x326c, 0x830a},
{0x326e, 0x0000},
{0x32ca, 0xfc28},
{0x32cc, 0xc3bc},
{0x32ce, 0xc34c},
{0x32d0, 0xc35a},
{0x32d2, 0xc368},
{0x32d4, 0xc376},
{0x32d6, 0xc3c2},
{0x32d8, 0xc3e6},
{0x32da, 0x0003},
{0x32dc, 0x0003},
{0x32de, 0x00c7},
{0x32e0, 0x0031},
{0x32e2, 0x0031},
{0x32e4, 0x0031},
{0x32e6, 0xfc28},
{0x32e8, 0xc3bc},
{0x32ea, 0xc384},
{0x32ec, 0xc392},
{0x32ee, 0xc3a0},
{0x32f0, 0xc3ae},
{0x32f2, 0xc3c4},
{0x32f4, 0xc3e6},
{0x32f6, 0x0003},
{0x32f8, 0x0003},
{0x32fa, 0x00c7},
{0x32fc, 0x0031},
{0x32fe, 0x0031},
{0x3300, 0x0031},
{0x3302, 0x82ca},
{0x3304, 0xc164},
{0x3306, 0x82e6},
{0x3308, 0xc19c},
{0x330a, 0x001f},
{0x330c, 0x001a},
{0x330e, 0x0034},
{0x3310, 0x0000},
{0x3312, 0x0000},
{0x3314, 0xfc94},
{0x3316, 0xc3d8},
/* regs above are unknown */
{HI846_REG_MODE_SELECT, 0x0000},
{HI846_REG_UNKNOWN_0E04, 0x0012},
{HI846_REG_Y_ODD_INC_FOBP, 0x1111},
{HI846_REG_Y_ODD_INC_VACT, 0x1111},
{HI846_REG_UNKNOWN_0022, 0x0008},
{HI846_REG_Y_ADDR_START_VACT_H, 0x0040},
{HI846_REG_UNKNOWN_0028, 0x0017},
{HI846_REG_Y_ADDR_END_VACT_H, 0x09cf},
{HI846_REG_UNKNOWN_005C, 0x2101},
{HI846_REG_FLL, 0x09de},
{HI846_REG_LLP, 0x0ed8},
{HI846_REG_IMAGE_ORIENTATION, 0x0100},
{HI846_REG_BINNING_MODE, 0x0022},
{HI846_REG_HBIN_MODE, 0x0000},
{HI846_REG_UNKNOWN_0A24, 0x0000},
{HI846_REG_X_START_H, 0x0000},
{HI846_REG_X_OUTPUT_SIZE_H, 0x0cc0},
{HI846_REG_Y_OUTPUT_SIZE_H, 0x0990},
{HI846_REG_EXPOSURE, 0x09d8},
{HI846_REG_ANALOG_GAIN, 0x0000},
{HI846_REG_GROUPED_PARA_HOLD, 0x0000},
{HI846_REG_UNKNOWN_051E, 0x0000},
{HI846_REG_UNKNOWN_0200, 0x0400},
{HI846_REG_PEDESTAL_EN, 0x0c00},
{HI846_REG_UNKNOWN_0A0C, 0x0010},
{HI846_REG_UNKNOWN_0A1E, 0x0ccf},
{HI846_REG_UNKNOWN_0402, 0x0110},
{HI846_REG_UNKNOWN_0404, 0x00f4},
{HI846_REG_UNKNOWN_0408, 0x0000},
{HI846_REG_UNKNOWN_0410, 0x008d},
{HI846_REG_UNKNOWN_0412, 0x011a},
{HI846_REG_UNKNOWN_0414, 0x864c},
{HI846_REG_UNKNOWN_021C, 0x0003},
{HI846_REG_UNKNOWN_021E, 0x0235},
{HI846_REG_BLC_CTL0, 0x9150},
{HI846_REG_UNKNOWN_0C06, 0x0021},
{HI846_REG_UNKNOWN_0C10, 0x0040},
{HI846_REG_UNKNOWN_0C12, 0x0040},
{HI846_REG_UNKNOWN_0C14, 0x0040},
{HI846_REG_UNKNOWN_0C16, 0x0040},
{HI846_REG_FAST_STANDBY_MODE, 0x0100},
{HI846_REG_ISP_EN_H, 0x014a},
{HI846_REG_UNKNOWN_0418, 0x0000},
{HI846_REG_UNKNOWN_012A, 0x03b4},
{HI846_REG_X_ADDR_START_HACT_H, 0x0046},
{HI846_REG_X_ADDR_END_HACT_H, 0x0376},
{HI846_REG_UNKNOWN_0B02, 0xe04d},
{HI846_REG_UNKNOWN_0B10, 0x6821},
{HI846_REG_UNKNOWN_0B12, 0x0120},
{HI846_REG_UNKNOWN_0B14, 0x0001},
{HI846_REG_UNKNOWN_2008, 0x38fd},
{HI846_REG_UNKNOWN_326E, 0x0000},
{HI846_REG_UNKNOWN_0900, 0x0320},
{HI846_REG_MIPI_TX_OP_MODE, 0xc31a},
{HI846_REG_UNKNOWN_0914, 0xc109},
{HI846_REG_TCLK_PREPARE, 0x061a},
{HI846_REG_UNKNOWN_0918, 0x0306},
{HI846_REG_THS_ZERO, 0x0b09},
{HI846_REG_TCLK_POST, 0x0c07},
{HI846_REG_UNKNOWN_091E, 0x0a00},
{HI846_REG_UNKNOWN_090C, 0x042a},
{HI846_REG_UNKNOWN_090E, 0x006b},
{HI846_REG_UNKNOWN_0954, 0x0089},
{HI846_REG_UNKNOWN_0956, 0x0000},
{HI846_REG_UNKNOWN_0958, 0xca00},
{HI846_REG_UNKNOWN_095A, 0x9240},
{HI846_REG_UNKNOWN_0F08, 0x2f04},
{HI846_REG_UNKNOWN_0F30, 0x001f},
{HI846_REG_UNKNOWN_0F36, 0x001f},
{HI846_REG_UNKNOWN_0F04, 0x3a00},
{HI846_REG_PLL_CFG_RAMP1_H, 0x025a},
{HI846_REG_PLL_CFG_MIPI1_H, 0x025a},
{HI846_REG_PLL_CFG_MIPI2_H, 0x0024},
{HI846_REG_UNKNOWN_006A, 0x0100},
{HI846_REG_TG_ENABLE, 0x0100},
};
static const struct hi846_reg hi846_init_4lane[] = {
{0x2000, 0x987a},
{0x2002, 0x00ff},
{0x2004, 0x0047},
{0x2006, 0x3fff},
{0x2008, 0x3fff},
{0x200a, 0xc216},
{0x200c, 0x1292},
{0x200e, 0xc01a},
{0x2010, 0x403d},
{0x2012, 0x000e},
{0x2014, 0x403e},
{0x2016, 0x0b80},
{0x2018, 0x403f},
{0x201a, 0x82ae},
{0x201c, 0x1292},
{0x201e, 0xc00c},
{0x2020, 0x4130},
{0x2022, 0x43e2},
{0x2024, 0x0180},
{0x2026, 0x4130},
{0x2028, 0x7400},
{0x202a, 0x5000},
{0x202c, 0x0253},
{0x202e, 0x0ad1},
{0x2030, 0x2360},
{0x2032, 0x0009},
{0x2034, 0x5020},
{0x2036, 0x000b},
{0x2038, 0x0002},
{0x203a, 0x0044},
{0x203c, 0x0016},
{0x203e, 0x1792},
{0x2040, 0x7002},
{0x2042, 0x154f},
{0x2044, 0x00d5},
{0x2046, 0x000b},
{0x2048, 0x0019},
{0x204a, 0x1698},
{0x204c, 0x000e},
{0x204e, 0x099a},
{0x2050, 0x0058},
{0x2052, 0x7000},
{0x2054, 0x1799},
{0x2056, 0x0310},
{0x2058, 0x03c3},
{0x205a, 0x004c},
{0x205c, 0x064a},
{0x205e, 0x0001},
{0x2060, 0x0007},
{0x2062, 0x0bc7},
{0x2064, 0x0055},
{0x2066, 0x7000},
{0x2068, 0x1550},
{0x206a, 0x158a},
{0x206c, 0x0004},
{0x206e, 0x1488},
{0x2070, 0x7010},
{0x2072, 0x1508},
{0x2074, 0x0004},
{0x2076, 0x0016},
{0x2078, 0x03d5},
{0x207a, 0x0055},
{0x207c, 0x08ca},
{0x207e, 0x2019},
{0x2080, 0x0007},
{0x2082, 0x7057},
{0x2084, 0x0fc7},
{0x2086, 0x5041},
{0x2088, 0x12c8},
{0x208a, 0x5060},
{0x208c, 0x5080},
{0x208e, 0x2084},
{0x2090, 0x12c8},
{0x2092, 0x7800},
{0x2094, 0x0802},
{0x2096, 0x040f},
{0x2098, 0x1007},
{0x209a, 0x0803},
{0x209c, 0x080b},
{0x209e, 0x3803},
{0x20a0, 0x0807},
{0x20a2, 0x0404},
{0x20a4, 0x0400},
{0x20a6, 0xffff},
{0x20a8, 0xf0b2},
{0x20aa, 0xffef},
{0x20ac, 0x0a84},
{0x20ae, 0x1292},
{0x20b0, 0xc02e},
{0x20b2, 0x4130},
{0x20b4, 0xf0b2},
{0x20b6, 0xffbf},
{0x20b8, 0x2004},
{0x20ba, 0x403f},
{0x20bc, 0x00c3},
{0x20be, 0x4fe2},
{0x20c0, 0x8318},
{0x20c2, 0x43cf},
{0x20c4, 0x0000},
{0x20c6, 0x9382},
{0x20c8, 0xc314},
{0x20ca, 0x2003},
{0x20cc, 0x12b0},
{0x20ce, 0xcab0},
{0x20d0, 0x4130},
{0x20d2, 0x12b0},
{0x20d4, 0xc90a},
{0x20d6, 0x4130},
{0x20d8, 0x42d2},
{0x20da, 0x8318},
{0x20dc, 0x00c3},
{0x20de, 0x9382},
{0x20e0, 0xc314},
{0x20e2, 0x2009},
{0x20e4, 0x120b},
{0x20e6, 0x120a},
{0x20e8, 0x1209},
{0x20ea, 0x1208},
{0x20ec, 0x1207},
{0x20ee, 0x1206},
{0x20f0, 0x4030},
{0x20f2, 0xc15e},
{0x20f4, 0x4130},
{0x20f6, 0x1292},
{0x20f8, 0xc008},
{0x20fa, 0x4130},
{0x20fc, 0x42d2},
{0x20fe, 0x82a1},
{0x2100, 0x00c2},
{0x2102, 0x1292},
{0x2104, 0xc040},
{0x2106, 0x4130},
{0x2108, 0x1292},
{0x210a, 0xc006},
{0x210c, 0x42a2},
{0x210e, 0x7324},
{0x2110, 0x9382},
{0x2112, 0xc314},
{0x2114, 0x2011},
{0x2116, 0x425f},
{0x2118, 0x82a1},
{0x211a, 0xf25f},
{0x211c, 0x00c1},
{0x211e, 0xf35f},
{0x2120, 0x2406},
{0x2122, 0x425f},
{0x2124, 0x00c0},
{0x2126, 0xf37f},
{0x2128, 0x522f},
{0x212a, 0x4f82},
{0x212c, 0x7324},
{0x212e, 0x425f},
{0x2130, 0x82d4},
{0x2132, 0xf35f},
{0x2134, 0x4fc2},
{0x2136, 0x01b3},
{0x2138, 0x93c2},
{0x213a, 0x829f},
{0x213c, 0x2421},
{0x213e, 0x403e},
{0x2140, 0xfffe},
{0x2142, 0x40b2},
{0x2144, 0xec78},
{0x2146, 0x831c},
{0x2148, 0x40b2},
{0x214a, 0xec78},
{0x214c, 0x831e},
{0x214e, 0x40b2},
{0x2150, 0xec78},
{0x2152, 0x8320},
{0x2154, 0xb3d2},
{0x2156, 0x008c},
{0x2158, 0x2405},
{0x215a, 0x4e0f},
{0x215c, 0x503f},
{0x215e, 0xffd8},
{0x2160, 0x4f82},
{0x2162, 0x831c},
{0x2164, 0x90f2},
{0x2166, 0x0003},
{0x2168, 0x008c},
{0x216a, 0x2401},
{0x216c, 0x4130},
{0x216e, 0x421f},
{0x2170, 0x831c},
{0x2172, 0x5e0f},
{0x2174, 0x4f82},
{0x2176, 0x831e},
{0x2178, 0x5e0f},
{0x217a, 0x4f82},
{0x217c, 0x8320},
{0x217e, 0x3ff6},
{0x2180, 0x432e},
{0x2182, 0x3fdf},
{0x2184, 0x421f},
{0x2186, 0x7100},
{0x2188, 0x4f0e},
{0x218a, 0x503e},
{0x218c, 0xffd8},
{0x218e, 0x4e82},
{0x2190, 0x7a04},
{0x2192, 0x421e},
{0x2194, 0x831c},
{0x2196, 0x5f0e},
{0x2198, 0x4e82},
{0x219a, 0x7a06},
{0x219c, 0x0b00},
{0x219e, 0x7304},
{0x21a0, 0x0050},
{0x21a2, 0x40b2},
{0x21a4, 0xd081},
{0x21a6, 0x0b88},
{0x21a8, 0x421e},
{0x21aa, 0x831e},
{0x21ac, 0x5f0e},
{0x21ae, 0x4e82},
{0x21b0, 0x7a0e},
{0x21b2, 0x521f},
{0x21b4, 0x8320},
{0x21b6, 0x4f82},
{0x21b8, 0x7a10},
{0x21ba, 0x0b00},
{0x21bc, 0x7304},
{0x21be, 0x007a},
{0x21c0, 0x40b2},
{0x21c2, 0x0081},
{0x21c4, 0x0b88},
{0x21c6, 0x4392},
{0x21c8, 0x7a0a},
{0x21ca, 0x0800},
{0x21cc, 0x7a0c},
{0x21ce, 0x0b00},
{0x21d0, 0x7304},
{0x21d2, 0x022b},
{0x21d4, 0x40b2},
{0x21d6, 0xd081},
{0x21d8, 0x0b88},
{0x21da, 0x0b00},
{0x21dc, 0x7304},
{0x21de, 0x0255},
{0x21e0, 0x40b2},
{0x21e2, 0x0081},
{0x21e4, 0x0b88},
{0x21e6, 0x4130},
{0x23fe, 0xc056},
{0x3232, 0xfc0c},
{0x3236, 0xfc22},
{0x3238, 0xfcfc},
{0x323a, 0xfd84},
{0x323c, 0xfd08},
{0x3246, 0xfcd8},
{0x3248, 0xfca8},
{0x324e, 0xfcb4},
{0x326a, 0x8302},
{0x326c, 0x830a},
{0x326e, 0x0000},
{0x32ca, 0xfc28},
{0x32cc, 0xc3bc},
{0x32ce, 0xc34c},
{0x32d0, 0xc35a},
{0x32d2, 0xc368},
{0x32d4, 0xc376},
{0x32d6, 0xc3c2},
{0x32d8, 0xc3e6},
{0x32da, 0x0003},
{0x32dc, 0x0003},
{0x32de, 0x00c7},
{0x32e0, 0x0031},
{0x32e2, 0x0031},
{0x32e4, 0x0031},
{0x32e6, 0xfc28},
{0x32e8, 0xc3bc},
{0x32ea, 0xc384},
{0x32ec, 0xc392},
{0x32ee, 0xc3a0},
{0x32f0, 0xc3ae},
{0x32f2, 0xc3c4},
{0x32f4, 0xc3e6},
{0x32f6, 0x0003},
{0x32f8, 0x0003},
{0x32fa, 0x00c7},
{0x32fc, 0x0031},
{0x32fe, 0x0031},
{0x3300, 0x0031},
{0x3302, 0x82ca},
{0x3304, 0xc164},
{0x3306, 0x82e6},
{0x3308, 0xc19c},
{0x330a, 0x001f},
{0x330c, 0x001a},
{0x330e, 0x0034},
{0x3310, 0x0000},
{0x3312, 0x0000},
{0x3314, 0xfc94},
{0x3316, 0xc3d8},
{0x0a00, 0x0000},
{0x0e04, 0x0012},
{0x002e, 0x1111},
{0x0032, 0x1111},
{0x0022, 0x0008},
{0x0026, 0x0040},
{0x0028, 0x0017},
{0x002c, 0x09cf},
{0x005c, 0x2101},
{0x0006, 0x09de},
{0x0008, 0x0ed8},
{0x000e, 0x0100},
{0x000c, 0x0022},
{0x0a22, 0x0000},
{0x0a24, 0x0000},
{0x0804, 0x0000},
{0x0a12, 0x0cc0},
{0x0a14, 0x0990},
{0x0074, 0x09d8},
{0x0076, 0x0000},
{0x051e, 0x0000},
{0x0200, 0x0400},
{0x0a1a, 0x0c00},
{0x0a0c, 0x0010},
{0x0a1e, 0x0ccf},
{0x0402, 0x0110},
{0x0404, 0x00f4},
{0x0408, 0x0000},
{0x0410, 0x008d},
{0x0412, 0x011a},
{0x0414, 0x864c},
/* for OTP */
{0x021c, 0x0003},
{0x021e, 0x0235},
/* for OTP */
{0x0c00, 0x9950},
{0x0c06, 0x0021},
{0x0c10, 0x0040},
{0x0c12, 0x0040},
{0x0c14, 0x0040},
{0x0c16, 0x0040},
{0x0a02, 0x0100},
{0x0a04, 0x015a},
{0x0418, 0x0000},
{0x0128, 0x0028},
{0x012a, 0xffff},
{0x0120, 0x0046},
{0x0122, 0x0376},
{0x012c, 0x0020},
{0x012e, 0xffff},
{0x0124, 0x0040},
{0x0126, 0x0378},
{0x0746, 0x0050},
{0x0748, 0x01d5},
{0x074a, 0x022b},
{0x074c, 0x03b0},
{0x0756, 0x043f},
{0x0758, 0x3f1d},
{0x0b02, 0xe04d},
{0x0b10, 0x6821},
{0x0b12, 0x0120},
{0x0b14, 0x0001},
{0x2008, 0x38fd},
{0x326e, 0x0000},
{0x0900, 0x0300},
{0x0902, 0xc319},
{0x0914, 0xc109},
{0x0916, 0x061a},
{0x0918, 0x0407},
{0x091a, 0x0a0b},
{0x091c, 0x0e08},
{0x091e, 0x0a00},
{0x090c, 0x0427},
{0x090e, 0x0059},
{0x0954, 0x0089},
{0x0956, 0x0000},
{0x0958, 0xca80},
{0x095a, 0x9240},
{0x0f08, 0x2f04},
{0x0f30, 0x001f},
{0x0f36, 0x001f},
{0x0f04, 0x3a00},
{0x0f32, 0x025a},
{0x0f38, 0x025a},
{0x0f2a, 0x4124},
{0x006a, 0x0100},
{0x004c, 0x0100},
{0x0044, 0x0001},
};
static const struct hi846_reg mode_640x480_config[] = {
{HI846_REG_MODE_SELECT, 0x0000},
{HI846_REG_Y_ODD_INC_FOBP, 0x7711},
{HI846_REG_Y_ODD_INC_VACT, 0x7711},
{HI846_REG_Y_ADDR_START_VACT_H, 0x0148},
{HI846_REG_Y_ADDR_END_VACT_H, 0x08c7},
{HI846_REG_UNKNOWN_005C, 0x4404},
{HI846_REG_FLL, 0x0277},
{HI846_REG_LLP, 0x0ed8},
{HI846_REG_BINNING_MODE, 0x0322},
{HI846_REG_HBIN_MODE, 0x0200},
{HI846_REG_UNKNOWN_0A24, 0x0000},
{HI846_REG_X_START_H, 0x0058},
{HI846_REG_X_OUTPUT_SIZE_H, 0x0280},
{HI846_REG_Y_OUTPUT_SIZE_H, 0x01e0},
/* For OTP */
{HI846_REG_UNKNOWN_021C, 0x0003},
{HI846_REG_UNKNOWN_021E, 0x0235},
{HI846_REG_ISP_EN_H, 0x016a},
{HI846_REG_UNKNOWN_0418, 0x0210},
{HI846_REG_UNKNOWN_0B02, 0xe04d},
{HI846_REG_UNKNOWN_0B10, 0x7021},
{HI846_REG_UNKNOWN_0B12, 0x0120},
{HI846_REG_UNKNOWN_0B14, 0x0001},
{HI846_REG_UNKNOWN_2008, 0x38fd},
{HI846_REG_UNKNOWN_326E, 0x0000},
};
static const struct hi846_reg mode_640x480_mipi_2lane[] = {
{HI846_REG_UNKNOWN_0900, 0x0300},
{HI846_REG_MIPI_TX_OP_MODE, 0x4319},
{HI846_REG_UNKNOWN_0914, 0xc105},
{HI846_REG_TCLK_PREPARE, 0x030c},
{HI846_REG_UNKNOWN_0918, 0x0304},
{HI846_REG_THS_ZERO, 0x0708},
{HI846_REG_TCLK_POST, 0x0b04},
{HI846_REG_UNKNOWN_091E, 0x0500},
{HI846_REG_UNKNOWN_090C, 0x0208},
{HI846_REG_UNKNOWN_090E, 0x009a},
{HI846_REG_UNKNOWN_0954, 0x0089},
{HI846_REG_UNKNOWN_0956, 0x0000},
{HI846_REG_UNKNOWN_0958, 0xca80},
{HI846_REG_UNKNOWN_095A, 0x9240},
{HI846_REG_PLL_CFG_MIPI2_H, 0x4924},
{HI846_REG_TG_ENABLE, 0x0100},
};
static const struct hi846_reg mode_1280x720_config[] = {
{HI846_REG_MODE_SELECT, 0x0000},
{HI846_REG_Y_ODD_INC_FOBP, 0x3311},
{HI846_REG_Y_ODD_INC_VACT, 0x3311},
{HI846_REG_Y_ADDR_START_VACT_H, 0x0238},
{HI846_REG_Y_ADDR_END_VACT_H, 0x07d7},
{HI846_REG_UNKNOWN_005C, 0x4202},
{HI846_REG_FLL, 0x034a},
{HI846_REG_LLP, 0x0ed8},
{HI846_REG_BINNING_MODE, 0x0122},
{HI846_REG_HBIN_MODE, 0x0100},
{HI846_REG_UNKNOWN_0A24, 0x0000},
{HI846_REG_X_START_H, 0x00b0},
{HI846_REG_X_OUTPUT_SIZE_H, 0x0500},
{HI846_REG_Y_OUTPUT_SIZE_H, 0x02d0},
{HI846_REG_EXPOSURE, 0x0344},
/* For OTP */
{HI846_REG_UNKNOWN_021C, 0x0003},
{HI846_REG_UNKNOWN_021E, 0x0235},
{HI846_REG_ISP_EN_H, 0x016a},
{HI846_REG_UNKNOWN_0418, 0x0410},
{HI846_REG_UNKNOWN_0B02, 0xe04d},
{HI846_REG_UNKNOWN_0B10, 0x6c21},
{HI846_REG_UNKNOWN_0B12, 0x0120},
{HI846_REG_UNKNOWN_0B14, 0x0005},
{HI846_REG_UNKNOWN_2008, 0x38fd},
{HI846_REG_UNKNOWN_326E, 0x0000},
};
static const struct hi846_reg mode_1280x720_mipi_2lane[] = {
{HI846_REG_UNKNOWN_0900, 0x0300},
{HI846_REG_MIPI_TX_OP_MODE, 0x4319},
{HI846_REG_UNKNOWN_0914, 0xc109},
{HI846_REG_TCLK_PREPARE, 0x061a},
{HI846_REG_UNKNOWN_0918, 0x0407},
{HI846_REG_THS_ZERO, 0x0a0b},
{HI846_REG_TCLK_POST, 0x0e08},
{HI846_REG_UNKNOWN_091E, 0x0a00},
{HI846_REG_UNKNOWN_090C, 0x0427},
{HI846_REG_UNKNOWN_090E, 0x0145},
{HI846_REG_UNKNOWN_0954, 0x0089},
{HI846_REG_UNKNOWN_0956, 0x0000},
{HI846_REG_UNKNOWN_0958, 0xca80},
{HI846_REG_UNKNOWN_095A, 0x9240},
{HI846_REG_PLL_CFG_MIPI2_H, 0x4124},
{HI846_REG_TG_ENABLE, 0x0100},
};
static const struct hi846_reg mode_1280x720_mipi_4lane[] = {
/* 360Mbps */
{HI846_REG_UNKNOWN_0900, 0x0300},
{HI846_REG_MIPI_TX_OP_MODE, 0xc319},
{HI846_REG_UNKNOWN_0914, 0xc105},
{HI846_REG_TCLK_PREPARE, 0x030c},
{HI846_REG_UNKNOWN_0918, 0x0304},
{HI846_REG_THS_ZERO, 0x0708},
{HI846_REG_TCLK_POST, 0x0b04},
{HI846_REG_UNKNOWN_091E, 0x0500},
{HI846_REG_UNKNOWN_090C, 0x0208},
{HI846_REG_UNKNOWN_090E, 0x008a},
{HI846_REG_UNKNOWN_0954, 0x0089},
{HI846_REG_UNKNOWN_0956, 0x0000},
{HI846_REG_UNKNOWN_0958, 0xca80},
{HI846_REG_UNKNOWN_095A, 0x9240},
{HI846_REG_PLL_CFG_MIPI2_H, 0x4924},
{HI846_REG_TG_ENABLE, 0x0100},
};
static const struct hi846_reg mode_1632x1224_config[] = {
{HI846_REG_MODE_SELECT, 0x0000},
{HI846_REG_Y_ODD_INC_FOBP, 0x3311},
{HI846_REG_Y_ODD_INC_VACT, 0x3311},
{HI846_REG_Y_ADDR_START_VACT_H, 0x0040},
{HI846_REG_Y_ADDR_END_VACT_H, 0x09cf},
{HI846_REG_UNKNOWN_005C, 0x4202},
{HI846_REG_FLL, 0x09de},
{HI846_REG_LLP, 0x0ed8},
{HI846_REG_BINNING_MODE, 0x0122},
{HI846_REG_HBIN_MODE, 0x0100},
{HI846_REG_UNKNOWN_0A24, 0x0000},
{HI846_REG_X_START_H, 0x0000},
{HI846_REG_X_OUTPUT_SIZE_H, 0x0660},
{HI846_REG_Y_OUTPUT_SIZE_H, 0x04c8},
{HI846_REG_EXPOSURE, 0x09d8},
/* For OTP */
{HI846_REG_UNKNOWN_021C, 0x0003},
{HI846_REG_UNKNOWN_021E, 0x0235},
{HI846_REG_ISP_EN_H, 0x016a},
{HI846_REG_UNKNOWN_0418, 0x0000},
{HI846_REG_UNKNOWN_0B02, 0xe04d},
{HI846_REG_UNKNOWN_0B10, 0x6c21},
{HI846_REG_UNKNOWN_0B12, 0x0120},
{HI846_REG_UNKNOWN_0B14, 0x0005},
{HI846_REG_UNKNOWN_2008, 0x38fd},
{HI846_REG_UNKNOWN_326E, 0x0000},
};
static const struct hi846_reg mode_1632x1224_mipi_2lane[] = {
{HI846_REG_UNKNOWN_0900, 0x0300},
{HI846_REG_MIPI_TX_OP_MODE, 0x4319},
{HI846_REG_UNKNOWN_0914, 0xc109},
{HI846_REG_TCLK_PREPARE, 0x061a},
{HI846_REG_UNKNOWN_0918, 0x0407},
{HI846_REG_THS_ZERO, 0x0a0b},
{HI846_REG_TCLK_POST, 0x0e08},
{HI846_REG_UNKNOWN_091E, 0x0a00},
{HI846_REG_UNKNOWN_090C, 0x0427},
{HI846_REG_UNKNOWN_090E, 0x0069},
{HI846_REG_UNKNOWN_0954, 0x0089},
{HI846_REG_UNKNOWN_0956, 0x0000},
{HI846_REG_UNKNOWN_0958, 0xca80},
{HI846_REG_UNKNOWN_095A, 0x9240},
{HI846_REG_PLL_CFG_MIPI2_H, 0x4124},
{HI846_REG_TG_ENABLE, 0x0100},
};
static const struct hi846_reg mode_1632x1224_mipi_4lane[] = {
{HI846_REG_UNKNOWN_0900, 0x0300},
{HI846_REG_MIPI_TX_OP_MODE, 0xc319},
{HI846_REG_UNKNOWN_0914, 0xc105},
{HI846_REG_TCLK_PREPARE, 0x030c},
{HI846_REG_UNKNOWN_0918, 0x0304},
{HI846_REG_THS_ZERO, 0x0708},
{HI846_REG_TCLK_POST, 0x0b04},
{HI846_REG_UNKNOWN_091E, 0x0500},
{HI846_REG_UNKNOWN_090C, 0x0208},
{HI846_REG_UNKNOWN_090E, 0x001c},
{HI846_REG_UNKNOWN_0954, 0x0089},
{HI846_REG_UNKNOWN_0956, 0x0000},
{HI846_REG_UNKNOWN_0958, 0xca80},
{HI846_REG_UNKNOWN_095A, 0x9240},
{HI846_REG_PLL_CFG_MIPI2_H, 0x4924},
{HI846_REG_TG_ENABLE, 0x0100},
};
static const char * const hi846_test_pattern_menu[] = {
"Disabled",
"Solid Colour",
"100% Colour Bars",
"Fade To Grey Colour Bars",
"PN9",
"Gradient Horizontal",
"Gradient Vertical",
"Check Board",
"Slant Pattern",
"Resolution Pattern",
};
#define FREQ_INDEX_640 0
#define FREQ_INDEX_1280 1
static const s64 hi846_link_freqs[] = {
[FREQ_INDEX_640] = 80000000,
[FREQ_INDEX_1280] = 200000000,
};
static const struct hi846_reg_list hi846_init_regs_list_2lane = {
.num_of_regs = ARRAY_SIZE(hi846_init_2lane),
.regs = hi846_init_2lane,
};
static const struct hi846_reg_list hi846_init_regs_list_4lane = {
.num_of_regs = ARRAY_SIZE(hi846_init_4lane),
.regs = hi846_init_4lane,
};
static const struct hi846_mode supported_modes[] = {
{
.width = 640,
.height = 480,
.link_freq_index = FREQ_INDEX_640,
.fps = 120,
.frame_len = 631,
.llp = HI846_LINE_LENGTH,
.reg_list_config = {
.num_of_regs = ARRAY_SIZE(mode_640x480_config),
.regs = mode_640x480_config,
},
.reg_list_2lane = {
.num_of_regs = ARRAY_SIZE(mode_640x480_mipi_2lane),
.regs = mode_640x480_mipi_2lane,
},
.reg_list_4lane = {
.num_of_regs = 0,
},
.crop = {
.left = 0x58,
.top = 0x148,
.width = 640 * 4,
.height = 480 * 4,
},
},
{
.width = 1280,
.height = 720,
.link_freq_index = FREQ_INDEX_1280,
.fps = 90,
.frame_len = 842,
.llp = HI846_LINE_LENGTH,
.reg_list_config = {
.num_of_regs = ARRAY_SIZE(mode_1280x720_config),
.regs = mode_1280x720_config,
},
.reg_list_2lane = {
.num_of_regs = ARRAY_SIZE(mode_1280x720_mipi_2lane),
.regs = mode_1280x720_mipi_2lane,
},
.reg_list_4lane = {
.num_of_regs = ARRAY_SIZE(mode_1280x720_mipi_4lane),
.regs = mode_1280x720_mipi_4lane,
},
.crop = {
.left = 0xb0,
.top = 0x238,
.width = 1280 * 2,
.height = 720 * 2,
},
},
{
.width = 1632,
.height = 1224,
.link_freq_index = FREQ_INDEX_1280,
.fps = 30,
.frame_len = 2526,
.llp = HI846_LINE_LENGTH,
.reg_list_config = {
.num_of_regs = ARRAY_SIZE(mode_1632x1224_config),
.regs = mode_1632x1224_config,
},
.reg_list_2lane = {
.num_of_regs = ARRAY_SIZE(mode_1632x1224_mipi_2lane),
.regs = mode_1632x1224_mipi_2lane,
},
.reg_list_4lane = {
.num_of_regs = ARRAY_SIZE(mode_1632x1224_mipi_4lane),
.regs = mode_1632x1224_mipi_4lane,
},
.crop = {
.left = 0x0,
.top = 0x0,
.width = 1632 * 2,
.height = 1224 * 2,
},
}
};
struct hi846_datafmt {
u32 code;
enum v4l2_colorspace colorspace;
};
static const char * const hi846_supply_names[] = {
"vddio", /* Digital I/O (1.8V or 2.8V) */
"vdda", /* Analog (2.8V) */
"vddd", /* Digital Core (1.2V) */
};
#define HI846_NUM_SUPPLIES ARRAY_SIZE(hi846_supply_names)
struct hi846 {
struct gpio_desc *rst_gpio;
struct gpio_desc *shutdown_gpio;
struct regulator_bulk_data supplies[HI846_NUM_SUPPLIES];
struct clk *clock;
const struct hi846_datafmt *fmt;
struct v4l2_subdev sd;
struct media_pad pad;
struct v4l2_ctrl_handler ctrl_handler;
u8 nr_lanes;
struct v4l2_ctrl *link_freq;
struct v4l2_ctrl *pixel_rate;
struct v4l2_ctrl *vblank;
struct v4l2_ctrl *hblank;
struct v4l2_ctrl *exposure;
struct mutex mutex; /* protect cur_mode, streaming and chip access */
const struct hi846_mode *cur_mode;
bool streaming;
};
static inline struct hi846 *to_hi846(struct v4l2_subdev *sd)
{
return container_of(sd, struct hi846, sd);
}
static const struct hi846_datafmt hi846_colour_fmts[] = {
{ HI846_MEDIA_BUS_FORMAT, V4L2_COLORSPACE_RAW },
};
static const struct hi846_datafmt *hi846_find_datafmt(u32 code)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(hi846_colour_fmts); i++)
if (hi846_colour_fmts[i].code == code)
return &hi846_colour_fmts[i];
return NULL;
}
static inline u8 hi846_get_link_freq_index(struct hi846 *hi846)
{
return hi846->cur_mode->link_freq_index;
}
static u64 hi846_get_link_freq(struct hi846 *hi846)
{
u8 index = hi846_get_link_freq_index(hi846);
return hi846_link_freqs[index];
}
static u64 hi846_calc_pixel_rate(struct hi846 *hi846)
{
u64 link_freq = hi846_get_link_freq(hi846);
u64 pixel_rate = link_freq * 2 * hi846->nr_lanes;
do_div(pixel_rate, HI846_RGB_DEPTH);
return pixel_rate;
}
static int hi846_read_reg(struct hi846 *hi846, u16 reg, u8 *val)
{
struct i2c_client *client = v4l2_get_subdevdata(&hi846->sd);
struct i2c_msg msgs[2];
u8 addr_buf[2];
u8 data_buf[1] = {0};
int ret;
put_unaligned_be16(reg, addr_buf);
msgs[0].addr = client->addr;
msgs[0].flags = 0;
msgs[0].len = sizeof(addr_buf);
msgs[0].buf = addr_buf;
msgs[1].addr = client->addr;
msgs[1].flags = I2C_M_RD;
msgs[1].len = 1;
msgs[1].buf = data_buf;
ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
if (ret != ARRAY_SIZE(msgs)) {
dev_err(&client->dev, "i2c read error: %d\n", ret);
return -EIO;
}
*val = data_buf[0];
return 0;
}
static int hi846_write_reg(struct hi846 *hi846, u16 reg, u8 val)
{
struct i2c_client *client = v4l2_get_subdevdata(&hi846->sd);
u8 buf[3] = { reg >> 8, reg & 0xff, val };
struct i2c_msg msg[] = {
{ .addr = client->addr, .flags = 0,
.len = ARRAY_SIZE(buf), .buf = buf },
};
int ret;
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
if (ret != ARRAY_SIZE(msg)) {
dev_err(&client->dev, "i2c write error\n");
return -EIO;
}
return 0;
}
static void hi846_write_reg_16(struct hi846 *hi846, u16 reg, u16 val, int *err)
{
struct i2c_client *client = v4l2_get_subdevdata(&hi846->sd);
u8 buf[4];
int ret;
if (*err < 0)
return;
put_unaligned_be16(reg, buf);
put_unaligned_be16(val, buf + 2);
ret = i2c_master_send(client, buf, sizeof(buf));
if (ret != sizeof(buf)) {
dev_err(&client->dev, "i2c_master_send != %zu: %d\n",
sizeof(buf), ret);
*err = -EIO;
}
}
static int hi846_write_reg_list(struct hi846 *hi846,
const struct hi846_reg_list *r_list)
{
struct i2c_client *client = v4l2_get_subdevdata(&hi846->sd);
unsigned int i;
int ret = 0;
for (i = 0; i < r_list->num_of_regs; i++) {
hi846_write_reg_16(hi846, r_list->regs[i].address,
r_list->regs[i].val, &ret);
if (ret) {
dev_err_ratelimited(&client->dev,
"failed to write reg 0x%4.4x: %d",
r_list->regs[i].address, ret);
return ret;
}
}
return 0;
}
static int hi846_update_digital_gain(struct hi846 *hi846, u16 d_gain)
{
int ret = 0;
hi846_write_reg_16(hi846, HI846_REG_MWB_GR_GAIN_H, d_gain, &ret);
hi846_write_reg_16(hi846, HI846_REG_MWB_GB_GAIN_H, d_gain, &ret);
hi846_write_reg_16(hi846, HI846_REG_MWB_R_GAIN_H, d_gain, &ret);
hi846_write_reg_16(hi846, HI846_REG_MWB_B_GAIN_H, d_gain, &ret);
return ret;
}
static int hi846_test_pattern(struct hi846 *hi846, u32 pattern)
{
int ret;
u8 val;
if (pattern) {
ret = hi846_read_reg(hi846, HI846_REG_ISP, &val);
if (ret)
return ret;
ret = hi846_write_reg(hi846, HI846_REG_ISP,
val | HI846_REG_ISP_TPG_EN);
if (ret)
return ret;
}
return hi846_write_reg(hi846, HI846_REG_TEST_PATTERN, pattern);
}
static int hi846_set_ctrl(struct v4l2_ctrl *ctrl)
{
struct hi846 *hi846 = container_of(ctrl->handler,
struct hi846, ctrl_handler);
struct i2c_client *client = v4l2_get_subdevdata(&hi846->sd);
s64 exposure_max;
int ret = 0;
u32 shutter, frame_len;
/* Propagate change of current control to all related controls */
if (ctrl->id == V4L2_CID_VBLANK) {
/* Update max exposure while meeting expected vblanking */
exposure_max = hi846->cur_mode->height + ctrl->val -
HI846_EXPOSURE_MAX_MARGIN;
__v4l2_ctrl_modify_range(hi846->exposure,
hi846->exposure->minimum,
exposure_max, hi846->exposure->step,
exposure_max);
}
if (!pm_runtime_get_if_in_use(&client->dev))
return 0;
switch (ctrl->id) {
case V4L2_CID_ANALOGUE_GAIN:
ret = hi846_write_reg(hi846, HI846_REG_ANALOG_GAIN, ctrl->val);
break;
case V4L2_CID_DIGITAL_GAIN:
ret = hi846_update_digital_gain(hi846, ctrl->val);
break;
case V4L2_CID_EXPOSURE:
shutter = ctrl->val;
frame_len = hi846->cur_mode->frame_len;
if (shutter > frame_len - 6) { /* margin */
frame_len = shutter + 6;
if (frame_len > 0xffff) { /* max frame len */
frame_len = 0xffff;
}
}
if (shutter < 6)
shutter = 6;
if (shutter > (0xffff - 6))
shutter = 0xffff - 6;
hi846_write_reg_16(hi846, HI846_REG_FLL, frame_len, &ret);
hi846_write_reg_16(hi846, HI846_REG_EXPOSURE, shutter, &ret);
break;
case V4L2_CID_VBLANK:
/* Update FLL that meets expected vertical blanking */
hi846_write_reg_16(hi846, HI846_REG_FLL,
hi846->cur_mode->height + ctrl->val, &ret);
break;
case V4L2_CID_TEST_PATTERN:
ret = hi846_test_pattern(hi846, ctrl->val);
break;
default:
ret = -EINVAL;
break;
}
pm_runtime_put(&client->dev);
return ret;
}
static const struct v4l2_ctrl_ops hi846_ctrl_ops = {
.s_ctrl = hi846_set_ctrl,
};
static int hi846_init_controls(struct hi846 *hi846)
{
struct v4l2_ctrl_handler *ctrl_hdlr;
s64 exposure_max, h_blank;
int ret;
struct i2c_client *client = v4l2_get_subdevdata(&hi846->sd);
struct v4l2_fwnode_device_properties props;
ctrl_hdlr = &hi846->ctrl_handler;
ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
if (ret)
return ret;
ctrl_hdlr->lock = &hi846->mutex;
hi846->link_freq =
v4l2_ctrl_new_int_menu(ctrl_hdlr, &hi846_ctrl_ops,
V4L2_CID_LINK_FREQ,
ARRAY_SIZE(hi846_link_freqs) - 1,
0, hi846_link_freqs);
if (hi846->link_freq)
hi846->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
hi846->pixel_rate =
v4l2_ctrl_new_std(ctrl_hdlr, &hi846_ctrl_ops,
V4L2_CID_PIXEL_RATE, 0,
hi846_calc_pixel_rate(hi846), 1,
hi846_calc_pixel_rate(hi846));
hi846->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &hi846_ctrl_ops,
V4L2_CID_VBLANK,
hi846->cur_mode->frame_len -
hi846->cur_mode->height,
HI846_FLL_MAX -
hi846->cur_mode->height, 1,
hi846->cur_mode->frame_len -
hi846->cur_mode->height);
h_blank = hi846->cur_mode->llp - hi846->cur_mode->width;
hi846->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &hi846_ctrl_ops,
V4L2_CID_HBLANK, h_blank, h_blank, 1,
h_blank);
if (hi846->hblank)
hi846->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
v4l2_ctrl_new_std(ctrl_hdlr, &hi846_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
HI846_ANAL_GAIN_MIN, HI846_ANAL_GAIN_MAX,
HI846_ANAL_GAIN_STEP, HI846_ANAL_GAIN_MIN);
v4l2_ctrl_new_std(ctrl_hdlr, &hi846_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
HI846_DGTL_GAIN_MIN, HI846_DGTL_GAIN_MAX,
HI846_DGTL_GAIN_STEP, HI846_DGTL_GAIN_DEFAULT);
exposure_max = hi846->cur_mode->frame_len - HI846_EXPOSURE_MAX_MARGIN;
hi846->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &hi846_ctrl_ops,
V4L2_CID_EXPOSURE,
HI846_EXPOSURE_MIN, exposure_max,
HI846_EXPOSURE_STEP,
exposure_max);
v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &hi846_ctrl_ops,
V4L2_CID_TEST_PATTERN,
ARRAY_SIZE(hi846_test_pattern_menu) - 1,
0, 0, hi846_test_pattern_menu);
if (ctrl_hdlr->error) {
dev_err(&client->dev, "v4l ctrl handler error: %d\n",
ctrl_hdlr->error);
return ctrl_hdlr->error;
}
ret = v4l2_fwnode_device_parse(&client->dev, &props);
if (ret)
return ret;
ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &hi846_ctrl_ops,
&props);
if (ret)
return ret;
hi846->sd.ctrl_handler = ctrl_hdlr;
return 0;
}
static int hi846_set_video_mode(struct hi846 *hi846, int fps)
{
struct i2c_client *client = v4l2_get_subdevdata(&hi846->sd);
u64 frame_length;
int ret = 0;
int dummy_lines;
u64 link_freq = hi846_get_link_freq(hi846);
dev_dbg(&client->dev, "%s: link freq: %llu\n", __func__,
hi846_get_link_freq(hi846));
do_div(link_freq, fps);
frame_length = link_freq;
do_div(frame_length, HI846_LINE_LENGTH);
dummy_lines = (frame_length > hi846->cur_mode->frame_len) ?
(frame_length - hi846->cur_mode->frame_len) : 0;
frame_length = hi846->cur_mode->frame_len + dummy_lines;
dev_dbg(&client->dev, "%s: frame length calculated: %llu\n", __func__,
frame_length);
hi846_write_reg_16(hi846, HI846_REG_FLL, frame_length & 0xFFFF, &ret);
hi846_write_reg_16(hi846, HI846_REG_LLP,
HI846_LINE_LENGTH & 0xFFFF, &ret);
return ret;
}
static int hi846_start_streaming(struct hi846 *hi846)
{
struct i2c_client *client = v4l2_get_subdevdata(&hi846->sd);
int ret = 0;
u8 val;
if (hi846->nr_lanes == 2)
ret = hi846_write_reg_list(hi846, &hi846_init_regs_list_2lane);
else
ret = hi846_write_reg_list(hi846, &hi846_init_regs_list_4lane);
if (ret) {
dev_err(&client->dev, "failed to set plls: %d\n", ret);
return ret;
}
ret = hi846_write_reg_list(hi846, &hi846->cur_mode->reg_list_config);
if (ret) {
dev_err(&client->dev, "failed to set mode: %d\n", ret);
return ret;
}
if (hi846->nr_lanes == 2)
ret = hi846_write_reg_list(hi846,
&hi846->cur_mode->reg_list_2lane);
else
ret = hi846_write_reg_list(hi846,
&hi846->cur_mode->reg_list_4lane);
if (ret) {
dev_err(&client->dev, "failed to set mipi mode: %d\n", ret);
return ret;
}
hi846_set_video_mode(hi846, hi846->cur_mode->fps);
ret = __v4l2_ctrl_handler_setup(hi846->sd.ctrl_handler);
if (ret)
return ret;
/*
* Reading 0x0034 is purely done for debugging reasons: It is not
* documented in the DS but only mentioned once:
* "If 0x0034[2] bit is disabled , Visible pixel width and height is 0."
* So even though that sounds like we won't see anything, we don't
* know more about this, so in that case only inform the user but do
* nothing more.
*/
ret = hi846_read_reg(hi846, 0x0034, &val);
if (ret)
return ret;
if (!(val & BIT(2)))
dev_info(&client->dev, "visible pixel width and height is 0\n");
ret = hi846_write_reg(hi846, HI846_REG_MODE_SELECT,
HI846_MODE_STREAMING);
if (ret) {
dev_err(&client->dev, "failed to start stream");
return ret;
}
hi846->streaming = 1;
dev_dbg(&client->dev, "%s: started streaming successfully\n", __func__);
return ret;
}
static void hi846_stop_streaming(struct hi846 *hi846)
{
struct i2c_client *client = v4l2_get_subdevdata(&hi846->sd);
if (hi846_write_reg(hi846, HI846_REG_MODE_SELECT, HI846_MODE_STANDBY))
dev_err(&client->dev, "failed to stop stream");
hi846->streaming = 0;
}
static int hi846_set_stream(struct v4l2_subdev *sd, int enable)
{
struct hi846 *hi846 = to_hi846(sd);
struct i2c_client *client = v4l2_get_subdevdata(sd);
int ret = 0;
if (hi846->streaming == enable)
return 0;
mutex_lock(&hi846->mutex);
if (enable) {
ret = pm_runtime_get_sync(&client->dev);
if (ret < 0) {
pm_runtime_put_noidle(&client->dev);
goto out;
}
ret = hi846_start_streaming(hi846);
}
if (!enable || ret) {
hi846_stop_streaming(hi846);
pm_runtime_put(&client->dev);
}
out:
mutex_unlock(&hi846->mutex);
return ret;
}
static int hi846_power_on(struct hi846 *hi846)
{
int ret;
ret = regulator_bulk_enable(HI846_NUM_SUPPLIES, hi846->supplies);
if (ret < 0)
return ret;
ret = clk_prepare_enable(hi846->clock);
if (ret < 0)
goto err_reg;
if (hi846->shutdown_gpio)
gpiod_set_value_cansleep(hi846->shutdown_gpio, 0);
/* 30us = 2400 cycles at 80Mhz */
usleep_range(30, 60);
if (hi846->rst_gpio)
gpiod_set_value_cansleep(hi846->rst_gpio, 0);
usleep_range(30, 60);
return 0;
err_reg:
regulator_bulk_disable(HI846_NUM_SUPPLIES, hi846->supplies);
return ret;
}
static int hi846_power_off(struct hi846 *hi846)
{
if (hi846->rst_gpio)
gpiod_set_value_cansleep(hi846->rst_gpio, 1);
if (hi846->shutdown_gpio)
gpiod_set_value_cansleep(hi846->shutdown_gpio, 1);
clk_disable_unprepare(hi846->clock);
return regulator_bulk_disable(HI846_NUM_SUPPLIES, hi846->supplies);
}
static int __maybe_unused hi846_suspend(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);
struct v4l2_subdev *sd = i2c_get_clientdata(client);
struct hi846 *hi846 = to_hi846(sd);
if (hi846->streaming)
hi846_stop_streaming(hi846);
return hi846_power_off(hi846);
}
static int __maybe_unused hi846_resume(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);
struct v4l2_subdev *sd = i2c_get_clientdata(client);
struct hi846 *hi846 = to_hi846(sd);
int ret;
ret = hi846_power_on(hi846);
if (ret)
return ret;
if (hi846->streaming) {
ret = hi846_start_streaming(hi846);
if (ret) {
dev_err(dev, "%s: start streaming failed: %d\n",
__func__, ret);
goto error;
}
}
return 0;
error:
hi846_power_off(hi846);
return ret;
}
static int hi846_set_format(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_format *format)
{
struct hi846 *hi846 = to_hi846(sd);
struct v4l2_mbus_framefmt *mf = &format->format;
struct i2c_client *client = v4l2_get_subdevdata(sd);
const struct hi846_datafmt *fmt = hi846_find_datafmt(mf->code);
u32 tgt_fps;
s32 vblank_def, h_blank;
if (!fmt) {
mf->code = hi846_colour_fmts[0].code;
mf->colorspace = hi846_colour_fmts[0].colorspace;
fmt = &hi846_colour_fmts[0];
}
if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
*v4l2_subdev_get_try_format(sd, sd_state, format->pad) = *mf;
return 0;
}
if (hi846->nr_lanes == 2) {
if (!hi846->cur_mode->reg_list_2lane.num_of_regs) {
dev_err(&client->dev,
"this mode is not supported for 2 lanes\n");
return -EINVAL;
}
} else {
if (!hi846->cur_mode->reg_list_4lane.num_of_regs) {
dev_err(&client->dev,
"this mode is not supported for 4 lanes\n");
return -EINVAL;
}
}
mutex_lock(&hi846->mutex);
if (hi846->streaming) {
mutex_unlock(&hi846->mutex);
return -EBUSY;
}
hi846->fmt = fmt;
hi846->cur_mode =
v4l2_find_nearest_size(supported_modes,
ARRAY_SIZE(supported_modes),
width, height, mf->width, mf->height);
dev_dbg(&client->dev, "%s: found mode: %dx%d\n", __func__,
hi846->cur_mode->width, hi846->cur_mode->height);
tgt_fps = hi846->cur_mode->fps;
dev_dbg(&client->dev, "%s: target fps: %d\n", __func__, tgt_fps);
mf->width = hi846->cur_mode->width;
mf->height = hi846->cur_mode->height;
mf->code = HI846_MEDIA_BUS_FORMAT;
mf->field = V4L2_FIELD_NONE;
__v4l2_ctrl_s_ctrl(hi846->link_freq, hi846_get_link_freq_index(hi846));
__v4l2_ctrl_s_ctrl_int64(hi846->pixel_rate,
hi846_calc_pixel_rate(hi846));
/* Update limits and set FPS to default */
vblank_def = hi846->cur_mode->frame_len - hi846->cur_mode->height;
__v4l2_ctrl_modify_range(hi846->vblank,
hi846->cur_mode->frame_len -
hi846->cur_mode->height,
HI846_FLL_MAX - hi846->cur_mode->height, 1,
vblank_def);
__v4l2_ctrl_s_ctrl(hi846->vblank, vblank_def);
h_blank = hi846->cur_mode->llp - hi846->cur_mode->width;
__v4l2_ctrl_modify_range(hi846->hblank, h_blank, h_blank, 1,
h_blank);
dev_dbg(&client->dev, "Set fmt w=%d h=%d code=0x%x colorspace=0x%x\n",
mf->width, mf->height,
fmt->code, fmt->colorspace);
mutex_unlock(&hi846->mutex);
return 0;
}
static int hi846_get_format(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_format *format)
{
struct hi846 *hi846 = to_hi846(sd);
struct v4l2_mbus_framefmt *mf = &format->format;
struct i2c_client *client = v4l2_get_subdevdata(sd);
if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
format->format = *v4l2_subdev_get_try_format(&hi846->sd,
sd_state,
format->pad);
return 0;
}
mutex_lock(&hi846->mutex);
mf->code = HI846_MEDIA_BUS_FORMAT;
mf->colorspace = V4L2_COLORSPACE_RAW;
mf->field = V4L2_FIELD_NONE;
mf->width = hi846->cur_mode->width;
mf->height = hi846->cur_mode->height;
mutex_unlock(&hi846->mutex);
dev_dbg(&client->dev,
"Get format w=%d h=%d code=0x%x colorspace=0x%x\n",
mf->width, mf->height, mf->code, mf->colorspace);
return 0;
}
static int hi846_enum_mbus_code(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_mbus_code_enum *code)
{
if (code->pad || code->index > 0)
return -EINVAL;
code->code = HI846_MEDIA_BUS_FORMAT;
return 0;
}
static int hi846_enum_frame_size(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_frame_size_enum *fse)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
if (fse->pad || fse->index >= ARRAY_SIZE(supported_modes))
return -EINVAL;
if (fse->code != HI846_MEDIA_BUS_FORMAT) {
dev_err(&client->dev, "frame size enum not matching\n");
return -EINVAL;
}
fse->min_width = supported_modes[fse->index].width;
fse->max_width = supported_modes[fse->index].width;
fse->min_height = supported_modes[fse->index].height;
fse->max_height = supported_modes[fse->index].height;
dev_dbg(&client->dev, "%s: max width: %d max height: %d\n", __func__,
fse->max_width, fse->max_height);
return 0;
}
static int hi846_get_selection(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_selection *sel)
{
struct hi846 *hi846 = to_hi846(sd);
switch (sel->target) {
case V4L2_SEL_TGT_CROP:
case V4L2_SEL_TGT_CROP_DEFAULT:
mutex_lock(&hi846->mutex);
switch (sel->which) {
case V4L2_SUBDEV_FORMAT_TRY:
v4l2_subdev_get_try_crop(sd, sd_state, sel->pad);
break;
case V4L2_SUBDEV_FORMAT_ACTIVE:
sel->r = hi846->cur_mode->crop;
break;
}
mutex_unlock(&hi846->mutex);
return 0;
case V4L2_SEL_TGT_CROP_BOUNDS:
case V4L2_SEL_TGT_NATIVE_SIZE:
sel->r.top = 0;
sel->r.left = 0;
sel->r.width = 3264;
sel->r.height = 2448;
return 0;
default:
return -EINVAL;
}
}
static int hi846_init_cfg(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state)
{
struct hi846 *hi846 = to_hi846(sd);
struct v4l2_mbus_framefmt *mf;
mf = v4l2_subdev_get_try_format(sd, sd_state, 0);
mutex_lock(&hi846->mutex);
mf->code = HI846_MEDIA_BUS_FORMAT;
mf->colorspace = V4L2_COLORSPACE_RAW;
mf->field = V4L2_FIELD_NONE;
mf->width = hi846->cur_mode->width;
mf->height = hi846->cur_mode->height;
mutex_unlock(&hi846->mutex);
return 0;
}
static const struct v4l2_subdev_video_ops hi846_video_ops = {
.s_stream = hi846_set_stream,
};
static const struct v4l2_subdev_pad_ops hi846_pad_ops = {
.init_cfg = hi846_init_cfg,
.enum_frame_size = hi846_enum_frame_size,
.enum_mbus_code = hi846_enum_mbus_code,
.set_fmt = hi846_set_format,
.get_fmt = hi846_get_format,
.get_selection = hi846_get_selection,
};
static const struct v4l2_subdev_ops hi846_subdev_ops = {
.video = &hi846_video_ops,
.pad = &hi846_pad_ops,
};
static const struct media_entity_operations hi846_subdev_entity_ops = {
.link_validate = v4l2_subdev_link_validate,
};
static int hi846_identify_module(struct hi846 *hi846)
{
struct i2c_client *client = v4l2_get_subdevdata(&hi846->sd);
int ret;
u8 hi, lo;
ret = hi846_read_reg(hi846, HI846_REG_CHIP_ID_L, &lo);
if (ret)
return ret;
if (lo != HI846_CHIP_ID_L) {
dev_err(&client->dev, "wrong chip id low byte: %x", lo);
return -ENXIO;
}
ret = hi846_read_reg(hi846, HI846_REG_CHIP_ID_H, &hi);
if (ret)
return ret;
if (hi != HI846_CHIP_ID_H) {
dev_err(&client->dev, "wrong chip id high byte: %x", hi);
return -ENXIO;
}
dev_info(&client->dev, "chip id %02X %02X using %d mipi lanes\n",
hi, lo, hi846->nr_lanes);
return 0;
}
static s64 hi846_check_link_freqs(struct hi846 *hi846,
struct v4l2_fwnode_endpoint *ep)
{
const s64 *freqs = hi846_link_freqs;
int freqs_count = ARRAY_SIZE(hi846_link_freqs);
int i, j;
for (i = 0; i < freqs_count; i++) {
for (j = 0; j < ep->nr_of_link_frequencies; j++)
if (freqs[i] == ep->link_frequencies[j])
break;
if (j == ep->nr_of_link_frequencies)
return freqs[i];
}
return 0;
}
static int hi846_parse_dt(struct hi846 *hi846, struct device *dev)
{
struct fwnode_handle *ep;
struct fwnode_handle *fwnode = dev_fwnode(dev);
struct v4l2_fwnode_endpoint bus_cfg = {
.bus_type = V4L2_MBUS_CSI2_DPHY
};
int ret;
s64 fq;
ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
if (!ep) {
dev_err(dev, "unable to find endpoint node\n");
return -ENXIO;
}
ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
fwnode_handle_put(ep);
if (ret) {
dev_err(dev, "failed to parse endpoint node: %d\n", ret);
return ret;
}
if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2 &&
bus_cfg.bus.mipi_csi2.num_data_lanes != 4) {
dev_err(dev, "number of CSI2 data lanes %d is not supported",
bus_cfg.bus.mipi_csi2.num_data_lanes);
v4l2_fwnode_endpoint_free(&bus_cfg);
return -EINVAL;
}
hi846->nr_lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
if (!bus_cfg.nr_of_link_frequencies) {
dev_err(dev, "link-frequency property not found in DT\n");
return -EINVAL;
}
/* Check that link frequences for all the modes are in device tree */
fq = hi846_check_link_freqs(hi846, &bus_cfg);
if (fq) {
dev_err(dev, "Link frequency of %lld is not supported\n", fq);
return -EINVAL;
}
v4l2_fwnode_endpoint_free(&bus_cfg);
hi846->rst_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
if (IS_ERR(hi846->rst_gpio)) {
dev_err(dev, "failed to get reset gpio: %pe\n",
hi846->rst_gpio);
return PTR_ERR(hi846->rst_gpio);
}
hi846->shutdown_gpio = devm_gpiod_get_optional(dev, "shutdown",
GPIOD_OUT_LOW);
if (IS_ERR(hi846->shutdown_gpio)) {
dev_err(dev, "failed to get shutdown gpio: %pe\n",
hi846->shutdown_gpio);
return PTR_ERR(hi846->shutdown_gpio);
}
return 0;
}
static int hi846_probe(struct i2c_client *client)
{
struct hi846 *hi846;
int ret;
int i;
u32 mclk_freq;
hi846 = devm_kzalloc(&client->dev, sizeof(*hi846), GFP_KERNEL);
if (!hi846)
return -ENOMEM;
ret = hi846_parse_dt(hi846, &client->dev);
if (ret) {
dev_err(&client->dev, "failed to check HW configuration: %d",
ret);
return ret;
}
hi846->clock = devm_clk_get(&client->dev, NULL);
if (IS_ERR(hi846->clock)) {
dev_err(&client->dev, "failed to get clock: %pe\n",
hi846->clock);
return PTR_ERR(hi846->clock);
}
mclk_freq = clk_get_rate(hi846->clock);
if (mclk_freq != 25000000)
dev_warn(&client->dev,
"External clock freq should be 25000000, not %u.\n",
mclk_freq);
for (i = 0; i < HI846_NUM_SUPPLIES; i++)
hi846->supplies[i].supply = hi846_supply_names[i];
ret = devm_regulator_bulk_get(&client->dev, HI846_NUM_SUPPLIES,
hi846->supplies);
if (ret < 0)
return ret;
v4l2_i2c_subdev_init(&hi846->sd, client, &hi846_subdev_ops);
mutex_init(&hi846->mutex);
ret = hi846_power_on(hi846);
if (ret)
goto err_mutex;
ret = hi846_identify_module(hi846);
if (ret)
goto err_power_off;
hi846->cur_mode = &supported_modes[0];
ret = hi846_init_controls(hi846);
if (ret) {
dev_err(&client->dev, "failed to init controls: %d", ret);
goto err_power_off;
}
hi846->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
hi846->sd.entity.ops = &hi846_subdev_entity_ops;
hi846->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
hi846->pad.flags = MEDIA_PAD_FL_SOURCE;
ret = media_entity_pads_init(&hi846->sd.entity, 1, &hi846->pad);
if (ret) {
dev_err(&client->dev, "failed to init entity pads: %d", ret);
goto err_v4l2_ctrl_handler_free;
}
ret = v4l2_async_register_subdev_sensor(&hi846->sd);
if (ret < 0) {
dev_err(&client->dev, "failed to register V4L2 subdev: %d",
ret);
goto err_media_entity_cleanup;
}
pm_runtime_set_active(&client->dev);
pm_runtime_enable(&client->dev);
pm_runtime_idle(&client->dev);
return 0;
err_media_entity_cleanup:
media_entity_cleanup(&hi846->sd.entity);
err_v4l2_ctrl_handler_free:
v4l2_ctrl_handler_free(hi846->sd.ctrl_handler);
err_power_off:
hi846_power_off(hi846);
err_mutex:
mutex_destroy(&hi846->mutex);
return ret;
}
static int hi846_remove(struct i2c_client *client)
{
struct v4l2_subdev *sd = i2c_get_clientdata(client);
struct hi846 *hi846 = to_hi846(sd);
v4l2_async_unregister_subdev(sd);
media_entity_cleanup(&sd->entity);
v4l2_ctrl_handler_free(sd->ctrl_handler);
pm_runtime_disable(&client->dev);
if (!pm_runtime_status_suspended(&client->dev))
hi846_suspend(&client->dev);
pm_runtime_set_suspended(&client->dev);
mutex_destroy(&hi846->mutex);
return 0;
}
static const struct dev_pm_ops hi846_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
pm_runtime_force_resume)
SET_RUNTIME_PM_OPS(hi846_suspend, hi846_resume, NULL)
};
static const struct of_device_id hi846_of_match[] = {
{ .compatible = "hynix,hi846", },
{},
};
MODULE_DEVICE_TABLE(of, hi846_of_match);
static struct i2c_driver hi846_i2c_driver = {
.driver = {
.name = "hi846",
.pm = &hi846_pm_ops,
.of_match_table = hi846_of_match,
},
.probe_new = hi846_probe,
.remove = hi846_remove,
};
module_i2c_driver(hi846_i2c_driver);
MODULE_AUTHOR("Angus Ainslie <angus@akkea.ca>");
MODULE_AUTHOR("Martin Kepplinger <martin.kepplinger@puri.sm>");
MODULE_DESCRIPTION("Hynix HI846 sensor driver");
MODULE_LICENSE("GPL v2");
|