summaryrefslogtreecommitdiff
path: root/drivers/media/i2c/ov5648.c
blob: 84604ea7bdf9ec3b37f6b6cc78698bb67cde8be3 (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
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
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2020 Bootlin
 * Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/of_graph.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <linux/videodev2.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-image-sizes.h>
#include <media/v4l2-mediabus.h>

/* Clock rate */

#define OV5648_XVCLK_RATE			24000000

/* Register definitions */

/* System */

#define OV5648_SW_STANDBY_REG			0x100
#define OV5648_SW_STANDBY_STREAM_ON		BIT(0)

#define OV5648_SW_RESET_REG			0x103
#define OV5648_SW_RESET_RESET			BIT(0)

#define OV5648_PAD_OEN0_REG			0x3000
#define OV5648_PAD_OEN1_REG			0x3001
#define OV5648_PAD_OEN2_REG			0x3002
#define OV5648_PAD_OUT0_REG			0x3008
#define OV5648_PAD_OUT1_REG			0x3009

#define OV5648_CHIP_ID_H_REG			0x300a
#define OV5648_CHIP_ID_H_VALUE			0x56
#define OV5648_CHIP_ID_L_REG			0x300b
#define OV5648_CHIP_ID_L_VALUE			0x48

#define OV5648_PAD_OUT2_REG			0x300d
#define OV5648_PAD_SEL0_REG			0x300e
#define OV5648_PAD_SEL1_REG			0x300f
#define OV5648_PAD_SEL2_REG			0x3010
#define OV5648_PAD_PK_REG			0x3011
#define OV5648_PAD_PK_PD_DATO_EN		BIT(7)
#define OV5648_PAD_PK_DRIVE_STRENGTH_1X		(0 << 5)
#define OV5648_PAD_PK_DRIVE_STRENGTH_2X		(2 << 5)
#define OV5648_PAD_PK_FREX_N			BIT(1)

#define OV5648_A_PWC_PK_O0_REG			0x3013
#define OV5648_A_PWC_PK_O0_BP_REGULATOR_N	BIT(3)
#define OV5648_A_PWC_PK_O1_REG			0x3014

#define OV5648_MIPI_PHY0_REG			0x3016
#define OV5648_MIPI_PHY1_REG			0x3017
#define OV5648_MIPI_SC_CTRL0_REG		0x3018
#define OV5648_MIPI_SC_CTRL0_MIPI_LANES(v)	(((v) << 5) & GENMASK(7, 5))
#define OV5648_MIPI_SC_CTRL0_PHY_HS_TX_PD	BIT(4)
#define OV5648_MIPI_SC_CTRL0_PHY_LP_RX_PD	BIT(3)
#define OV5648_MIPI_SC_CTRL0_MIPI_EN		BIT(2)
#define OV5648_MIPI_SC_CTRL0_MIPI_SUSP		BIT(1)
#define OV5648_MIPI_SC_CTRL0_LANE_DIS_OP	BIT(0)
#define OV5648_MIPI_SC_CTRL1_REG		0x3019
#define OV5648_MISC_CTRL0_REG			0x3021
#define OV5648_MIPI_SC_CTRL2_REG		0x3022
#define OV5648_SUB_ID_REG			0x302a

#define OV5648_PLL_CTRL0_REG			0x3034
#define OV5648_PLL_CTRL0_PLL_CHARGE_PUMP(v)	(((v) << 4) & GENMASK(6, 4))
#define OV5648_PLL_CTRL0_BITS(v)		((v) & GENMASK(3, 0))
#define OV5648_PLL_CTRL1_REG			0x3035
#define OV5648_PLL_CTRL1_SYS_DIV(v)		(((v) << 4) & GENMASK(7, 4))
#define OV5648_PLL_CTRL1_MIPI_DIV(v)		((v) & GENMASK(3, 0))
#define OV5648_PLL_MUL_REG			0x3036
#define OV5648_PLL_MUL(v)			((v) & GENMASK(7, 0))
#define OV5648_PLL_DIV_REG			0x3037
#define OV5648_PLL_DIV_ROOT_DIV(v)		((((v) - 1) << 4) & BIT(4))
#define OV5648_PLL_DIV_PLL_PRE_DIV(v)		((v) & GENMASK(3, 0))
#define OV5648_PLL_DEBUG_REG			0x3038
#define OV5648_PLL_BYPASS_REG			0x3039

#define OV5648_PLLS_BYPASS_REG			0x303a
#define OV5648_PLLS_MUL_REG			0x303b
#define OV5648_PLLS_MUL(v)			((v) & GENMASK(4, 0))
#define OV5648_PLLS_CTRL_REG			0x303c
#define OV5648_PLLS_CTRL_PLL_CHARGE_PUMP(v)	(((v) << 4) & GENMASK(6, 4))
#define OV5648_PLLS_CTRL_SYS_DIV(v)		((v) & GENMASK(3, 0))
#define OV5648_PLLS_DIV_REG			0x303d
#define OV5648_PLLS_DIV_PLLS_PRE_DIV(v)		(((v) << 4) & GENMASK(5, 4))
#define OV5648_PLLS_DIV_PLLS_DIV_R(v)		((((v) - 1) << 2) & BIT(2))
#define OV5648_PLLS_DIV_PLLS_SEL_DIV(v)		((v) & GENMASK(1, 0))

#define OV5648_SRB_CTRL_REG			0x3106
#define OV5648_SRB_CTRL_SCLK_DIV(v)		(((v) << 2) & GENMASK(3, 2))
#define OV5648_SRB_CTRL_RESET_ARBITER_EN	BIT(1)
#define OV5648_SRB_CTRL_SCLK_ARBITER_EN		BIT(0)

/* Group Hold */

#define OV5648_GROUP_ADR0_REG			0x3200
#define OV5648_GROUP_ADR1_REG			0x3201
#define OV5648_GROUP_ADR2_REG			0x3202
#define OV5648_GROUP_ADR3_REG			0x3203
#define OV5648_GROUP_LEN0_REG			0x3204
#define OV5648_GROUP_LEN1_REG			0x3205
#define OV5648_GROUP_LEN2_REG			0x3206
#define OV5648_GROUP_LEN3_REG			0x3207
#define OV5648_GROUP_ACCESS_REG			0x3208

/* Exposure/gain/banding */

#define OV5648_EXPOSURE_CTRL_HH_REG		0x3500
#define OV5648_EXPOSURE_CTRL_HH(v)		(((v) & GENMASK(19, 16)) >> 16)
#define OV5648_EXPOSURE_CTRL_HH_VALUE(v)	(((v) << 16) & GENMASK(19, 16))
#define OV5648_EXPOSURE_CTRL_H_REG		0x3501
#define OV5648_EXPOSURE_CTRL_H(v)		(((v) & GENMASK(15, 8)) >> 8)
#define OV5648_EXPOSURE_CTRL_H_VALUE(v)		(((v) << 8) & GENMASK(15, 8))
#define OV5648_EXPOSURE_CTRL_L_REG		0x3502
#define OV5648_EXPOSURE_CTRL_L(v)		((v) & GENMASK(7, 0))
#define OV5648_EXPOSURE_CTRL_L_VALUE(v)		((v) & GENMASK(7, 0))
#define OV5648_MANUAL_CTRL_REG			0x3503
#define OV5648_MANUAL_CTRL_FRAME_DELAY(v)	(((v) << 4) & GENMASK(5, 4))
#define OV5648_MANUAL_CTRL_AGC_MANUAL_EN	BIT(1)
#define OV5648_MANUAL_CTRL_AEC_MANUAL_EN	BIT(0)
#define OV5648_GAIN_CTRL_H_REG			0x350a
#define OV5648_GAIN_CTRL_H(v)			(((v) & GENMASK(9, 8)) >> 8)
#define OV5648_GAIN_CTRL_H_VALUE(v)		(((v) << 8) & GENMASK(9, 8))
#define OV5648_GAIN_CTRL_L_REG			0x350b
#define OV5648_GAIN_CTRL_L(v)			((v) & GENMASK(7, 0))
#define OV5648_GAIN_CTRL_L_VALUE(v)		((v) & GENMASK(7, 0))

#define OV5648_ANALOG_CTRL0_REG_BASE		0x3600
#define OV5648_ANALOG_CTRL1_REG_BASE		0x3700

#define OV5648_AEC_CTRL0_REG			0x3a00
#define OV5648_AEC_CTRL0_DEBUG			BIT(6)
#define OV5648_AEC_CTRL0_DEBAND_EN		BIT(5)
#define OV5648_AEC_CTRL0_DEBAND_LOW_LIMIT_EN	BIT(4)
#define OV5648_AEC_CTRL0_START_SEL_EN		BIT(3)
#define OV5648_AEC_CTRL0_NIGHT_MODE_EN		BIT(2)
#define OV5648_AEC_CTRL0_FREEZE_EN		BIT(0)
#define OV5648_EXPOSURE_MIN_REG			0x3a01
#define OV5648_EXPOSURE_MAX_60_H_REG		0x3a02
#define OV5648_EXPOSURE_MAX_60_L_REG		0x3a03
#define OV5648_AEC_CTRL5_REG			0x3a05
#define OV5648_AEC_CTRL6_REG			0x3a06
#define OV5648_AEC_CTRL7_REG			0x3a07
#define OV5648_BANDING_STEP_50_H_REG		0x3a08
#define OV5648_BANDING_STEP_50_L_REG		0x3a09
#define OV5648_BANDING_STEP_60_H_REG		0x3a0a
#define OV5648_BANDING_STEP_60_L_REG		0x3a0b
#define OV5648_AEC_CTRLC_REG			0x3a0c
#define OV5648_BANDING_MAX_60_REG		0x3a0d
#define OV5648_BANDING_MAX_50_REG		0x3a0e
#define OV5648_WPT_REG				0x3a0f
#define OV5648_BPT_REG				0x3a10
#define OV5648_VPT_HIGH_REG			0x3a11
#define OV5648_AVG_MANUAL_REG			0x3a12
#define OV5648_PRE_GAIN_REG			0x3a13
#define OV5648_EXPOSURE_MAX_50_H_REG		0x3a14
#define OV5648_EXPOSURE_MAX_50_L_REG		0x3a15
#define OV5648_GAIN_BASE_NIGHT_REG		0x3a17
#define OV5648_AEC_GAIN_CEILING_H_REG		0x3a18
#define OV5648_AEC_GAIN_CEILING_L_REG		0x3a19
#define OV5648_DIFF_MAX_REG			0x3a1a
#define OV5648_WPT2_REG				0x3a1b
#define OV5648_LED_ADD_ROW_H_REG		0x3a1c
#define OV5648_LED_ADD_ROW_L_REG		0x3a1d
#define OV5648_BPT2_REG				0x3a1e
#define OV5648_VPT_LOW_REG			0x3a1f
#define OV5648_AEC_CTRL20_REG			0x3a20
#define OV5648_AEC_CTRL21_REG			0x3a21

#define OV5648_AVG_START_X_H_REG		0x5680
#define OV5648_AVG_START_X_L_REG		0x5681
#define OV5648_AVG_START_Y_H_REG		0x5682
#define OV5648_AVG_START_Y_L_REG		0x5683
#define OV5648_AVG_WINDOW_X_H_REG		0x5684
#define OV5648_AVG_WINDOW_X_L_REG		0x5685
#define OV5648_AVG_WINDOW_Y_H_REG		0x5686
#define OV5648_AVG_WINDOW_Y_L_REG		0x5687
#define OV5648_AVG_WEIGHT00_REG			0x5688
#define OV5648_AVG_WEIGHT01_REG			0x5689
#define OV5648_AVG_WEIGHT02_REG			0x568a
#define OV5648_AVG_WEIGHT03_REG			0x568b
#define OV5648_AVG_WEIGHT04_REG			0x568c
#define OV5648_AVG_WEIGHT05_REG			0x568d
#define OV5648_AVG_WEIGHT06_REG			0x568e
#define OV5648_AVG_WEIGHT07_REG			0x568f
#define OV5648_AVG_CTRL10_REG			0x5690
#define OV5648_AVG_WEIGHT_SUM_REG		0x5691
#define OV5648_AVG_READOUT_REG			0x5693

#define OV5648_DIG_CTRL0_REG			0x5a00
#define OV5648_DIG_COMP_MAN_H_REG		0x5a02
#define OV5648_DIG_COMP_MAN_L_REG		0x5a03

#define OV5648_GAINC_MAN_H_REG			0x5a20
#define OV5648_GAINC_MAN_L_REG			0x5a21
#define OV5648_GAINC_DGC_MAN_H_REG		0x5a22
#define OV5648_GAINC_DGC_MAN_L_REG		0x5a23
#define OV5648_GAINC_CTRL0_REG			0x5a24

#define OV5648_GAINF_ANA_NUM_REG		0x5a40
#define OV5648_GAINF_DIG_GAIN_REG		0x5a41

/* Timing */

#define OV5648_CROP_START_X_H_REG		0x3800
#define OV5648_CROP_START_X_H(v)		(((v) & GENMASK(11, 8)) >> 8)
#define OV5648_CROP_START_X_L_REG		0x3801
#define OV5648_CROP_START_X_L(v)		((v) & GENMASK(7, 0))
#define OV5648_CROP_START_Y_H_REG		0x3802
#define OV5648_CROP_START_Y_H(v)		(((v) & GENMASK(11, 8)) >> 8)
#define OV5648_CROP_START_Y_L_REG		0x3803
#define OV5648_CROP_START_Y_L(v)		((v) & GENMASK(7, 0))
#define OV5648_CROP_END_X_H_REG			0x3804
#define OV5648_CROP_END_X_H(v)			(((v) & GENMASK(11, 8)) >> 8)
#define OV5648_CROP_END_X_L_REG			0x3805
#define OV5648_CROP_END_X_L(v)			((v) & GENMASK(7, 0))
#define OV5648_CROP_END_Y_H_REG			0x3806
#define OV5648_CROP_END_Y_H(v)			(((v) & GENMASK(11, 8)) >> 8)
#define OV5648_CROP_END_Y_L_REG			0x3807
#define OV5648_CROP_END_Y_L(v)			((v) & GENMASK(7, 0))
#define OV5648_OUTPUT_SIZE_X_H_REG		0x3808
#define OV5648_OUTPUT_SIZE_X_H(v)		(((v) & GENMASK(11, 8)) >> 8)
#define OV5648_OUTPUT_SIZE_X_L_REG		0x3809
#define OV5648_OUTPUT_SIZE_X_L(v)		((v) & GENMASK(7, 0))
#define OV5648_OUTPUT_SIZE_Y_H_REG		0x380a
#define OV5648_OUTPUT_SIZE_Y_H(v)		(((v) & GENMASK(11, 8)) >> 8)
#define OV5648_OUTPUT_SIZE_Y_L_REG		0x380b
#define OV5648_OUTPUT_SIZE_Y_L(v)		((v) & GENMASK(7, 0))
#define OV5648_HTS_H_REG			0x380c
#define OV5648_HTS_H(v)				(((v) & GENMASK(12, 8)) >> 8)
#define OV5648_HTS_L_REG			0x380d
#define OV5648_HTS_L(v)				((v) & GENMASK(7, 0))
#define OV5648_VTS_H_REG			0x380e
#define OV5648_VTS_H(v)				(((v) & GENMASK(15, 8)) >> 8)
#define OV5648_VTS_L_REG			0x380f
#define OV5648_VTS_L(v)				((v) & GENMASK(7, 0))
#define OV5648_OFFSET_X_H_REG			0x3810
#define OV5648_OFFSET_X_H(v)			(((v) & GENMASK(11, 8)) >> 8)
#define OV5648_OFFSET_X_L_REG			0x3811
#define OV5648_OFFSET_X_L(v)			((v) & GENMASK(7, 0))
#define OV5648_OFFSET_Y_H_REG			0x3812
#define OV5648_OFFSET_Y_H(v)			(((v) & GENMASK(11, 8)) >> 8)
#define OV5648_OFFSET_Y_L_REG			0x3813
#define OV5648_OFFSET_Y_L(v)			((v) & GENMASK(7, 0))
#define OV5648_SUB_INC_X_REG			0x3814
#define OV5648_SUB_INC_X_ODD(v)			(((v) << 4) & GENMASK(7, 4))
#define OV5648_SUB_INC_X_EVEN(v)		((v) & GENMASK(3, 0))
#define OV5648_SUB_INC_Y_REG			0x3815
#define OV5648_SUB_INC_Y_ODD(v)			(((v) << 4) & GENMASK(7, 4))
#define OV5648_SUB_INC_Y_EVEN(v)		((v) & GENMASK(3, 0))
#define OV5648_HSYNCST_H_REG			0x3816
#define OV5648_HSYNCST_H(v)			(((v) >> 8) & 0xf)
#define OV5648_HSYNCST_L_REG			0x3817
#define OV5648_HSYNCST_L(v)			((v) & GENMASK(7, 0))
#define OV5648_HSYNCW_H_REG			0x3818
#define OV5648_HSYNCW_H(v)			(((v) >> 8) & 0xf)
#define OV5648_HSYNCW_L_REG			0x3819
#define OV5648_HSYNCW_L(v)			((v) & GENMASK(7, 0))

#define OV5648_TC20_REG				0x3820
#define OV5648_TC20_DEBUG			BIT(6)
#define OV5648_TC20_FLIP_VERT_ISP_EN		BIT(2)
#define OV5648_TC20_FLIP_VERT_SENSOR_EN		BIT(1)
#define OV5648_TC20_BINNING_VERT_EN		BIT(0)
#define OV5648_TC21_REG				0x3821
#define OV5648_TC21_FLIP_HORZ_ISP_EN		BIT(2)
#define OV5648_TC21_FLIP_HORZ_SENSOR_EN		BIT(1)
#define OV5648_TC21_BINNING_HORZ_EN		BIT(0)

/* Strobe/exposure */

#define OV5648_STROBE_REG			0x3b00
#define OV5648_FREX_EXP_HH_REG			0x3b01
#define OV5648_SHUTTER_DLY_H_REG		0x3b02
#define OV5648_SHUTTER_DLY_L_REG		0x3b03
#define OV5648_FREX_EXP_H_REG			0x3b04
#define OV5648_FREX_EXP_L_REG			0x3b05
#define OV5648_FREX_CTRL_REG			0x3b06
#define OV5648_FREX_MODE_SEL_REG		0x3b07
#define OV5648_FREX_MODE_SEL_FREX_SA1		BIT(4)
#define OV5648_FREX_MODE_SEL_FX1_FM_EN		BIT(3)
#define OV5648_FREX_MODE_SEL_FREX_INV		BIT(2)
#define OV5648_FREX_MODE_SEL_MODE1		0x0
#define OV5648_FREX_MODE_SEL_MODE2		0x1
#define OV5648_FREX_MODE_SEL_ROLLING		0x2
#define OV5648_FREX_EXP_REQ_REG			0x3b08
#define OV5648_FREX_SHUTTER_DLY_REG		0x3b09
#define OV5648_FREX_RST_LEN_REG			0x3b0a
#define OV5648_STROBE_WIDTH_HH_REG		0x3b0b
#define OV5648_STROBE_WIDTH_H_REG		0x3b0c

/* OTP */

#define OV5648_OTP_DATA_REG_BASE		0x3d00
#define OV5648_OTP_PROGRAM_CTRL_REG		0x3d80
#define OV5648_OTP_LOAD_CTRL_REG		0x3d81

/* PSRAM */

#define OV5648_PSRAM_CTRL1_REG			0x3f01
#define OV5648_PSRAM_CTRLF_REG			0x3f0f

/* Black Level */

#define OV5648_BLC_CTRL0_REG			0x4000
#define OV5648_BLC_CTRL1_REG			0x4001
#define OV5648_BLC_CTRL1_START_LINE(v)		((v) & GENMASK(5, 0))
#define OV5648_BLC_CTRL2_REG			0x4002
#define OV5648_BLC_CTRL2_AUTO_EN		BIT(6)
#define OV5648_BLC_CTRL2_RESET_FRAME_NUM(v)	((v) & GENMASK(5, 0))
#define OV5648_BLC_CTRL3_REG			0x4003
#define OV5648_BLC_LINE_NUM_REG			0x4004
#define OV5648_BLC_LINE_NUM(v)			((v) & GENMASK(7, 0))
#define OV5648_BLC_CTRL5_REG			0x4005
#define OV5648_BLC_CTRL5_UPDATE_EN		BIT(1)
#define OV5648_BLC_LEVEL_REG			0x4009

/* Frame */

#define OV5648_FRAME_CTRL_REG			0x4200
#define OV5648_FRAME_ON_NUM_REG			0x4201
#define OV5648_FRAME_OFF_NUM_REG		0x4202

/* MIPI CSI-2 */

#define OV5648_MIPI_CTRL0_REG			0x4800
#define OV5648_MIPI_CTRL0_CLK_LANE_AUTOGATE	BIT(5)
#define OV5648_MIPI_CTRL0_LANE_SYNC_EN		BIT(4)
#define OV5648_MIPI_CTRL0_LANE_SELECT_LANE1	0
#define OV5648_MIPI_CTRL0_LANE_SELECT_LANE2	BIT(3)
#define OV5648_MIPI_CTRL0_IDLE_LP00		0
#define OV5648_MIPI_CTRL0_IDLE_LP11		BIT(2)

#define OV5648_MIPI_CTRL1_REG			0x4801
#define OV5648_MIPI_CTRL2_REG			0x4802
#define OV5648_MIPI_CTRL3_REG			0x4803
#define OV5648_MIPI_CTRL4_REG			0x4804
#define OV5648_MIPI_CTRL5_REG			0x4805
#define OV5648_MIPI_MAX_FRAME_COUNT_H_REG	0x4810
#define OV5648_MIPI_MAX_FRAME_COUNT_L_REG	0x4811
#define OV5648_MIPI_CTRL14_REG			0x4814
#define OV5648_MIPI_DT_SPKT_REG			0x4815
#define OV5648_MIPI_HS_ZERO_MIN_H_REG		0x4818
#define OV5648_MIPI_HS_ZERO_MIN_L_REG		0x4819
#define OV5648_MIPI_HS_TRAIN_MIN_H_REG		0x481a
#define OV5648_MIPI_HS_TRAIN_MIN_L_REG		0x481b
#define OV5648_MIPI_CLK_ZERO_MIN_H_REG		0x481c
#define OV5648_MIPI_CLK_ZERO_MIN_L_REG		0x481d
#define OV5648_MIPI_CLK_PREPARE_MIN_H_REG	0x481e
#define OV5648_MIPI_CLK_PREPARE_MIN_L_REG	0x481f
#define OV5648_MIPI_CLK_POST_MIN_H_REG		0x4820
#define OV5648_MIPI_CLK_POST_MIN_L_REG		0x4821
#define OV5648_MIPI_CLK_TRAIL_MIN_H_REG		0x4822
#define OV5648_MIPI_CLK_TRAIL_MIN_L_REG		0x4823
#define OV5648_MIPI_LPX_P_MIN_H_REG		0x4824
#define OV5648_MIPI_LPX_P_MIN_L_REG		0x4825
#define OV5648_MIPI_HS_PREPARE_MIN_H_REG	0x4826
#define OV5648_MIPI_HS_PREPARE_MIN_L_REG	0x4827
#define OV5648_MIPI_HS_EXIT_MIN_H_REG		0x4828
#define OV5648_MIPI_HS_EXIT_MIN_L_REG		0x4829
#define OV5648_MIPI_HS_ZERO_MIN_UI_REG		0x482a
#define OV5648_MIPI_HS_TRAIL_MIN_UI_REG		0x482b
#define OV5648_MIPI_CLK_ZERO_MIN_UI_REG		0x482c
#define OV5648_MIPI_CLK_PREPARE_MIN_UI_REG	0x482d
#define OV5648_MIPI_CLK_POST_MIN_UI_REG		0x482e
#define OV5648_MIPI_CLK_TRAIL_MIN_UI_REG	0x482f
#define OV5648_MIPI_LPX_P_MIN_UI_REG		0x4830
#define OV5648_MIPI_HS_PREPARE_MIN_UI_REG	0x4831
#define OV5648_MIPI_HS_EXIT_MIN_UI_REG		0x4832
#define OV5648_MIPI_REG_MIN_H_REG		0x4833
#define OV5648_MIPI_REG_MIN_L_REG		0x4834
#define OV5648_MIPI_REG_MAX_H_REG		0x4835
#define OV5648_MIPI_REG_MAX_L_REG		0x4836
#define OV5648_MIPI_PCLK_PERIOD_REG		0x4837
#define OV5648_MIPI_WKUP_DLY_REG		0x4838
#define OV5648_MIPI_LP_GPIO_REG			0x483b
#define OV5648_MIPI_SNR_PCLK_DIV_REG		0x4843

/* ISP */

#define OV5648_ISP_CTRL0_REG			0x5000
#define OV5648_ISP_CTRL0_BLACK_CORRECT_EN	BIT(2)
#define OV5648_ISP_CTRL0_WHITE_CORRECT_EN	BIT(1)
#define OV5648_ISP_CTRL1_REG			0x5001
#define OV5648_ISP_CTRL1_AWB_EN			BIT(0)
#define OV5648_ISP_CTRL2_REG			0x5002
#define OV5648_ISP_CTRL2_WIN_EN			BIT(6)
#define OV5648_ISP_CTRL2_OTP_EN			BIT(1)
#define OV5648_ISP_CTRL2_AWB_GAIN_EN		BIT(0)
#define OV5648_ISP_CTRL3_REG			0x5003
#define OV5648_ISP_CTRL3_BUF_EN			BIT(3)
#define OV5648_ISP_CTRL3_BIN_MAN_SET		BIT(2)
#define OV5648_ISP_CTRL3_BIN_AUTO_EN		BIT(1)
#define OV5648_ISP_CTRL4_REG			0x5004
#define OV5648_ISP_CTRL5_REG			0x5005
#define OV5648_ISP_CTRL6_REG			0x5006
#define OV5648_ISP_CTRL7_REG			0x5007
#define OV5648_ISP_MAN_OFFSET_X_H_REG		0x5008
#define OV5648_ISP_MAN_OFFSET_X_L_REG		0x5009
#define OV5648_ISP_MAN_OFFSET_Y_H_REG		0x500a
#define OV5648_ISP_MAN_OFFSET_Y_L_REG		0x500b
#define OV5648_ISP_MAN_WIN_OFFSET_X_H_REG	0x500c
#define OV5648_ISP_MAN_WIN_OFFSET_X_L_REG	0x500d
#define OV5648_ISP_MAN_WIN_OFFSET_Y_H_REG	0x500e
#define OV5648_ISP_MAN_WIN_OFFSET_Y_L_REG	0x500f
#define OV5648_ISP_MAN_WIN_OUTPUT_X_H_REG	0x5010
#define OV5648_ISP_MAN_WIN_OUTPUT_X_L_REG	0x5011
#define OV5648_ISP_MAN_WIN_OUTPUT_Y_H_REG	0x5012
#define OV5648_ISP_MAN_WIN_OUTPUT_Y_L_REG	0x5013
#define OV5648_ISP_MAN_INPUT_X_H_REG		0x5014
#define OV5648_ISP_MAN_INPUT_X_L_REG		0x5015
#define OV5648_ISP_MAN_INPUT_Y_H_REG		0x5016
#define OV5648_ISP_MAN_INPUT_Y_L_REG		0x5017
#define OV5648_ISP_CTRL18_REG			0x5018
#define OV5648_ISP_CTRL19_REG			0x5019
#define OV5648_ISP_CTRL1A_REG			0x501a
#define OV5648_ISP_CTRL1D_REG			0x501d
#define OV5648_ISP_CTRL1F_REG			0x501f
#define OV5648_ISP_CTRL1F_OUTPUT_EN		3
#define OV5648_ISP_CTRL25_REG			0x5025

#define OV5648_ISP_CTRL3D_REG			0x503d
#define OV5648_ISP_CTRL3D_PATTERN_EN		BIT(7)
#define OV5648_ISP_CTRL3D_ROLLING_BAR_EN	BIT(6)
#define OV5648_ISP_CTRL3D_TRANSPARENT_MODE	BIT(5)
#define OV5648_ISP_CTRL3D_SQUARES_BW_MODE	BIT(4)
#define OV5648_ISP_CTRL3D_PATTERN_COLOR_BARS	0
#define OV5648_ISP_CTRL3D_PATTERN_RANDOM_DATA	1
#define OV5648_ISP_CTRL3D_PATTERN_COLOR_SQUARES	2
#define OV5648_ISP_CTRL3D_PATTERN_INPUT		3

#define OV5648_ISP_CTRL3E_REG			0x503e
#define OV5648_ISP_CTRL4B_REG			0x504b
#define OV5648_ISP_CTRL4B_POST_BIN_H_EN		BIT(5)
#define OV5648_ISP_CTRL4B_POST_BIN_V_EN		BIT(4)
#define OV5648_ISP_CTRL4C_REG			0x504c
#define OV5648_ISP_CTRL57_REG			0x5057
#define OV5648_ISP_CTRL58_REG			0x5058
#define OV5648_ISP_CTRL59_REG			0x5059

#define OV5648_ISP_WINDOW_START_X_H_REG		0x5980
#define OV5648_ISP_WINDOW_START_X_L_REG		0x5981
#define OV5648_ISP_WINDOW_START_Y_H_REG		0x5982
#define OV5648_ISP_WINDOW_START_Y_L_REG		0x5983
#define OV5648_ISP_WINDOW_WIN_X_H_REG		0x5984
#define OV5648_ISP_WINDOW_WIN_X_L_REG		0x5985
#define OV5648_ISP_WINDOW_WIN_Y_H_REG		0x5986
#define OV5648_ISP_WINDOW_WIN_Y_L_REG		0x5987
#define OV5648_ISP_WINDOW_MAN_REG		0x5988

/* White Balance */

#define OV5648_AWB_CTRL_REG			0x5180
#define OV5648_AWB_CTRL_FAST_AWB		BIT(6)
#define OV5648_AWB_CTRL_GAIN_FREEZE_EN		BIT(5)
#define OV5648_AWB_CTRL_SUM_FREEZE_EN		BIT(4)
#define OV5648_AWB_CTRL_GAIN_MANUAL_EN		BIT(3)

#define OV5648_AWB_DELTA_REG			0x5181
#define OV5648_AWB_STABLE_RANGE_REG		0x5182
#define OV5648_AWB_STABLE_RANGE_WIDE_REG	0x5183
#define OV5648_HSIZE_MAN_REG			0x5185

#define OV5648_GAIN_RED_MAN_H_REG		0x5186
#define OV5648_GAIN_RED_MAN_H(v)		(((v) & GENMASK(11, 8)) >> 8)
#define OV5648_GAIN_RED_MAN_L_REG		0x5187
#define OV5648_GAIN_RED_MAN_L(v)		((v) & GENMASK(7, 0))
#define OV5648_GAIN_GREEN_MAN_H_REG		0x5188
#define OV5648_GAIN_GREEN_MAN_H(v)		(((v) & GENMASK(11, 8)) >> 8)
#define OV5648_GAIN_GREEN_MAN_L_REG		0x5189
#define OV5648_GAIN_GREEN_MAN_L(v)		((v) & GENMASK(7, 0))
#define OV5648_GAIN_BLUE_MAN_H_REG		0x518a
#define OV5648_GAIN_BLUE_MAN_H(v)		(((v) & GENMASK(11, 8)) >> 8)
#define OV5648_GAIN_BLUE_MAN_L_REG		0x518b
#define OV5648_GAIN_BLUE_MAN_L(v)		((v) & GENMASK(7, 0))
#define OV5648_GAIN_RED_LIMIT_REG		0x518c
#define OV5648_GAIN_GREEN_LIMIT_REG		0x518d
#define OV5648_GAIN_BLUE_LIMIT_REG		0x518e
#define OV5648_AWB_FRAME_COUNT_REG		0x518f
#define OV5648_AWB_BASE_MAN_REG			0x51df

/* Macros */

#define ov5648_subdev_sensor(s) \
	container_of(s, struct ov5648_sensor, subdev)

#define ov5648_ctrl_subdev(c) \
	(&container_of((c)->handler, struct ov5648_sensor, \
		       ctrls.handler)->subdev)

/* Data structures */

struct ov5648_register_value {
	u16 address;
	u8 value;
	unsigned int delay_ms;
};

/*
 * PLL1 Clock Tree:
 *
 * +-< XVCLK
 * |
 * +-+ pll_pre_div (0x3037 [3:0], special values: 5: 1.5, 7: 2.5)
 *   |
 *   +-+ pll_mul (0x3036 [7:0])
 *     |
 *     +-+ sys_div (0x3035 [7:4])
 *       |
 *       +-+ mipi_div (0x3035 [3:0])
 *       | |
 *       | +-> MIPI_SCLK
 *       | |
 *       | +-+ mipi_phy_div (2)
 *       |   |
 *       |   +-> MIPI_CLK
 *       |
 *       +-+ root_div (0x3037 [4])
 *         |
 *         +-+ bit_div (0x3034 [3:0], 8 bits: 2, 10 bits: 2.5, other: 1)
 *           |
 *           +-+ sclk_div (0x3106 [3:2])
 *             |
 *             +-> SCLK
 *             |
 *             +-+ mipi_div (0x3035, 1: PCLK = SCLK)
 *               |
 *               +-> PCLK
 */

struct ov5648_pll1_config {
	unsigned int pll_pre_div;
	unsigned int pll_mul;
	unsigned int sys_div;
	unsigned int root_div;
	unsigned int sclk_div;
	unsigned int mipi_div;
};

/*
 * PLL2 Clock Tree:
 *
 * +-< XVCLK
 * |
 * +-+ plls_pre_div (0x303d [5:4], special values: 0: 1, 1: 1.5)
 *   |
 *   +-+ plls_div_r (0x303d [2])
 *     |
 *     +-+ plls_mul (0x303b [4:0])
 *       |
 *       +-+ sys_div (0x303c [3:0])
 *         |
 *         +-+ sel_div (0x303d [1:0], special values: 0: 1, 3: 2.5)
 *           |
 *           +-> ADCLK
 */

struct ov5648_pll2_config {
	unsigned int plls_pre_div;
	unsigned int plls_div_r;
	unsigned int plls_mul;
	unsigned int sys_div;
	unsigned int sel_div;
};

/*
 * General formulas for (array-centered) mode calculation:
 * - photo_array_width = 2624
 * - crop_start_x = (photo_array_width - output_size_x) / 2
 * - crop_end_x = crop_start_x + offset_x + output_size_x - 1
 *
 * - photo_array_height = 1956
 * - crop_start_y = (photo_array_height - output_size_y) / 2
 * - crop_end_y = crop_start_y + offset_y + output_size_y - 1
 */

struct ov5648_mode {
	unsigned int crop_start_x;
	unsigned int offset_x;
	unsigned int output_size_x;
	unsigned int crop_end_x;
	unsigned int hts;

	unsigned int crop_start_y;
	unsigned int offset_y;
	unsigned int output_size_y;
	unsigned int crop_end_y;
	unsigned int vts;

	bool binning_x;
	bool binning_y;

	unsigned int inc_x_odd;
	unsigned int inc_x_even;
	unsigned int inc_y_odd;
	unsigned int inc_y_even;

	/* 8-bit frame interval followed by 10-bit frame interval. */
	struct v4l2_fract frame_interval[2];

	/* 8-bit config followed by 10-bit config. */
	const struct ov5648_pll1_config *pll1_config[2];
	const struct ov5648_pll2_config *pll2_config;

	const struct ov5648_register_value *register_values;
	unsigned int register_values_count;
};

struct ov5648_state {
	const struct ov5648_mode *mode;
	u32 mbus_code;

	bool streaming;
};

struct ov5648_ctrls {
	struct v4l2_ctrl *exposure_auto;
	struct v4l2_ctrl *exposure;

	struct v4l2_ctrl *gain_auto;
	struct v4l2_ctrl *gain;

	struct v4l2_ctrl *white_balance_auto;
	struct v4l2_ctrl *red_balance;
	struct v4l2_ctrl *blue_balance;

	struct v4l2_ctrl *link_freq;
	struct v4l2_ctrl *pixel_rate;

	struct v4l2_ctrl_handler handler;
};

struct ov5648_sensor {
	struct device *dev;
	struct i2c_client *i2c_client;
	struct gpio_desc *reset;
	struct gpio_desc *powerdown;
	struct regulator *avdd;
	struct regulator *dvdd;
	struct regulator *dovdd;
	struct clk *xvclk;

	struct v4l2_fwnode_endpoint endpoint;
	struct v4l2_subdev subdev;
	struct media_pad pad;

	struct mutex mutex;

	struct ov5648_state state;
	struct ov5648_ctrls ctrls;
};

/* Static definitions */

/*
 * XVCLK = 24 MHz
 * SCLK  = 84 MHz
 * PCLK  = 84 MHz
 */
static const struct ov5648_pll1_config ov5648_pll1_config_native_8_bits = {
	.pll_pre_div	= 3,
	.pll_mul	= 84,
	.sys_div	= 2,
	.root_div	= 1,
	.sclk_div	= 1,
	.mipi_div	= 1,
};

/*
 * XVCLK = 24 MHz
 * SCLK  = 84 MHz
 * PCLK  = 84 MHz
 */
static const struct ov5648_pll1_config ov5648_pll1_config_native_10_bits = {
	.pll_pre_div	= 3,
	.pll_mul	= 105,
	.sys_div	= 2,
	.root_div	= 1,
	.sclk_div	= 1,
	.mipi_div	= 1,
};

/*
 * XVCLK = 24 MHz
 * ADCLK = 200 MHz
 */
static const struct ov5648_pll2_config ov5648_pll2_config_native = {
	.plls_pre_div	= 3,
	.plls_div_r	= 1,
	.plls_mul	= 25,
	.sys_div	= 1,
	.sel_div	= 1,
};

static const struct ov5648_mode ov5648_modes[] = {
	/* 2592x1944 */
	{
		/* Horizontal */
		.crop_start_x	= 16,
		.offset_x	= 0,
		.output_size_x	= 2592,
		.crop_end_x	= 2607,
		.hts		= 2816,

		/* Vertical */
		.crop_start_y	= 6,
		.offset_y	= 0,
		.output_size_y	= 1944,
		.crop_end_y	= 1949,
		.vts		= 1984,

		/* Subsample increase */
		.inc_x_odd	= 1,
		.inc_x_even	= 1,
		.inc_y_odd	= 1,
		.inc_y_even	= 1,

		/* Frame Interval */
		.frame_interval	= {
			{ 1,	15 },
			{ 1,	15 },
		},

		/* PLL */
		.pll1_config	= {
			&ov5648_pll1_config_native_8_bits,
			&ov5648_pll1_config_native_10_bits,
		},
		.pll2_config	= &ov5648_pll2_config_native,
	},
	/* 1600x1200 (UXGA) */
	{
		/* Horizontal */
		.crop_start_x	= 512,
		.offset_x	= 0,
		.output_size_x	= 1600,
		.crop_end_x	= 2111,
		.hts		= 2816,

		/* Vertical */
		.crop_start_y	= 378,
		.offset_y	= 0,
		.output_size_y	= 1200,
		.crop_end_y	= 1577,
		.vts		= 1984,

		/* Subsample increase */
		.inc_x_odd	= 1,
		.inc_x_even	= 1,
		.inc_y_odd	= 1,
		.inc_y_even	= 1,

		/* Frame Interval */
		.frame_interval	= {
			{ 1,	15 },
			{ 1,	15 },
		},

		/* PLL */
		.pll1_config	= {
			&ov5648_pll1_config_native_8_bits,
			&ov5648_pll1_config_native_10_bits,
		},
		.pll2_config	= &ov5648_pll2_config_native,
	},
	/* 1920x1080 (Full HD) */
	{
		/* Horizontal */
		.crop_start_x	= 352,
		.offset_x	= 0,
		.output_size_x	= 1920,
		.crop_end_x	= 2271,
		.hts		= 2816,

		/* Vertical */
		.crop_start_y	= 438,
		.offset_y	= 0,
		.output_size_y	= 1080,
		.crop_end_y	= 1517,
		.vts		= 1984,

		/* Subsample increase */
		.inc_x_odd	= 1,
		.inc_x_even	= 1,
		.inc_y_odd	= 1,
		.inc_y_even	= 1,

		/* Frame Interval */
		.frame_interval	= {
			{ 1,	15 },
			{ 1,	15 },
		},

		/* PLL */
		.pll1_config	= {
			&ov5648_pll1_config_native_8_bits,
			&ov5648_pll1_config_native_10_bits,
		},
		.pll2_config	= &ov5648_pll2_config_native,
	},
	/* 1280x960 */
	{
		/* Horizontal */
		.crop_start_x	= 16,
		.offset_x	= 8,
		.output_size_x	= 1280,
		.crop_end_x	= 2607,
		.hts		= 1912,

		/* Vertical */
		.crop_start_y	= 6,
		.offset_y	= 6,
		.output_size_y	= 960,
		.crop_end_y	= 1949,
		.vts		= 1496,

		/* Binning */
		.binning_x	= true,

		/* Subsample increase */
		.inc_x_odd	= 3,
		.inc_x_even	= 1,
		.inc_y_odd	= 3,
		.inc_y_even	= 1,

		/* Frame Interval */
		.frame_interval	= {
			{ 1,	30 },
			{ 1,	30 },
		},

		/* PLL */
		.pll1_config	= {
			&ov5648_pll1_config_native_8_bits,
			&ov5648_pll1_config_native_10_bits,
		},
		.pll2_config	= &ov5648_pll2_config_native,
	},
	/* 1280x720 (HD) */
	{
		/* Horizontal */
		.crop_start_x	= 16,
		.offset_x	= 8,
		.output_size_x	= 1280,
		.crop_end_x	= 2607,
		.hts		= 1912,

		/* Vertical */
		.crop_start_y	= 254,
		.offset_y	= 2,
		.output_size_y	= 720,
		.crop_end_y	= 1701,
		.vts		= 1496,

		/* Binning */
		.binning_x	= true,

		/* Subsample increase */
		.inc_x_odd	= 3,
		.inc_x_even	= 1,
		.inc_y_odd	= 3,
		.inc_y_even	= 1,

		/* Frame Interval */
		.frame_interval	= {
			{ 1,	30 },
			{ 1,	30 },
		},

		/* PLL */
		.pll1_config	= {
			&ov5648_pll1_config_native_8_bits,
			&ov5648_pll1_config_native_10_bits,
		},
		.pll2_config	= &ov5648_pll2_config_native,
	},
	/* 640x480 (VGA) */
	{
		/* Horizontal */
		.crop_start_x	= 0,
		.offset_x	= 8,
		.output_size_x	= 640,
		.crop_end_x	= 2623,
		.hts		= 1896,

		/* Vertical */
		.crop_start_y	= 0,
		.offset_y	= 2,
		.output_size_y	= 480,
		.crop_end_y	= 1953,
		.vts		= 984,

		/* Binning */
		.binning_x	= true,

		/* Subsample increase */
		.inc_x_odd	= 7,
		.inc_x_even	= 1,
		.inc_y_odd	= 7,
		.inc_y_even	= 1,

		/* Frame Interval */
		.frame_interval	= {
			{ 1,	30 },
			{ 1,	30 },
		},

		/* PLL */
		.pll1_config	= {
			&ov5648_pll1_config_native_8_bits,
			&ov5648_pll1_config_native_10_bits,
		},
		.pll2_config	= &ov5648_pll2_config_native,
	},
};

static const u32 ov5648_mbus_codes[] = {
	MEDIA_BUS_FMT_SBGGR8_1X8,
	MEDIA_BUS_FMT_SBGGR10_1X10,
};

static const struct ov5648_register_value ov5648_init_sequence[] = {
	/* PSRAM */
	{ OV5648_PSRAM_CTRL1_REG, 0x0d },
	{ OV5648_PSRAM_CTRLF_REG, 0xf5 },
};

static const s64 ov5648_link_freq_menu[] = {
	210000000,
	168000000,
};

static const char *const ov5648_test_pattern_menu[] = {
	"Disabled",
	"Random data",
	"Color bars",
	"Color bars with rolling bar",
	"Color squares",
	"Color squares with rolling bar"
};

static const u8 ov5648_test_pattern_bits[] = {
	0,
	OV5648_ISP_CTRL3D_PATTERN_EN | OV5648_ISP_CTRL3D_PATTERN_RANDOM_DATA,
	OV5648_ISP_CTRL3D_PATTERN_EN | OV5648_ISP_CTRL3D_PATTERN_COLOR_BARS,
	OV5648_ISP_CTRL3D_PATTERN_EN | OV5648_ISP_CTRL3D_ROLLING_BAR_EN |
	OV5648_ISP_CTRL3D_PATTERN_COLOR_BARS,
	OV5648_ISP_CTRL3D_PATTERN_EN | OV5648_ISP_CTRL3D_PATTERN_COLOR_SQUARES,
	OV5648_ISP_CTRL3D_PATTERN_EN | OV5648_ISP_CTRL3D_ROLLING_BAR_EN |
	OV5648_ISP_CTRL3D_PATTERN_COLOR_SQUARES,
};

/* Input/Output */

static int ov5648_read(struct ov5648_sensor *sensor, u16 address, u8 *value)
{
	unsigned char data[2] = { address >> 8, address & 0xff };
	struct i2c_client *client = sensor->i2c_client;
	int ret;

	ret = i2c_master_send(client, data, sizeof(data));
	if (ret < 0) {
		dev_dbg(&client->dev, "i2c send error at address %#04x\n",
			address);
		return ret;
	}

	ret = i2c_master_recv(client, value, 1);
	if (ret < 0) {
		dev_dbg(&client->dev, "i2c recv error at address %#04x\n",
			address);
		return ret;
	}

	return 0;
}

static int ov5648_write(struct ov5648_sensor *sensor, u16 address, u8 value)
{
	unsigned char data[3] = { address >> 8, address & 0xff, value };
	struct i2c_client *client = sensor->i2c_client;
	int ret;

	ret = i2c_master_send(client, data, sizeof(data));
	if (ret < 0) {
		dev_dbg(&client->dev, "i2c send error at address %#04x\n",
			address);
		return ret;
	}

	return 0;
}

static int ov5648_write_sequence(struct ov5648_sensor *sensor,
				 const struct ov5648_register_value *sequence,
				 unsigned int sequence_count)
{
	unsigned int i;
	int ret = 0;

	for (i = 0; i < sequence_count; i++) {
		ret = ov5648_write(sensor, sequence[i].address,
				   sequence[i].value);
		if (ret)
			break;

		if (sequence[i].delay_ms)
			msleep(sequence[i].delay_ms);
	}

	return ret;
}

static int ov5648_update_bits(struct ov5648_sensor *sensor, u16 address,
			      u8 mask, u8 bits)
{
	u8 value = 0;
	int ret;

	ret = ov5648_read(sensor, address, &value);
	if (ret)
		return ret;

	value &= ~mask;
	value |= bits;

	ret = ov5648_write(sensor, address, value);
	if (ret)
		return ret;

	return 0;
}

/* Sensor */

static int ov5648_sw_reset(struct ov5648_sensor *sensor)
{
	return ov5648_write(sensor, OV5648_SW_RESET_REG, OV5648_SW_RESET_RESET);
}

static int ov5648_sw_standby(struct ov5648_sensor *sensor, int standby)
{
	u8 value = 0;

	if (!standby)
		value = OV5648_SW_STANDBY_STREAM_ON;

	return ov5648_write(sensor, OV5648_SW_STANDBY_REG, value);
}

static int ov5648_chip_id_check(struct ov5648_sensor *sensor)
{
	u16 regs[] = { OV5648_CHIP_ID_H_REG, OV5648_CHIP_ID_L_REG };
	u8 values[] = { OV5648_CHIP_ID_H_VALUE, OV5648_CHIP_ID_L_VALUE };
	unsigned int i;
	u8 value;
	int ret;

	for (i = 0; i < ARRAY_SIZE(regs); i++) {
		ret = ov5648_read(sensor, regs[i], &value);
		if (ret < 0)
			return ret;

		if (value != values[i]) {
			dev_err(sensor->dev,
				"chip id value mismatch: %#x instead of %#x\n",
				value, values[i]);
			return -EINVAL;
		}
	}

	return 0;
}

static int ov5648_avdd_internal_power(struct ov5648_sensor *sensor, int on)
{
	return ov5648_write(sensor, OV5648_A_PWC_PK_O0_REG,
			    on ? 0 : OV5648_A_PWC_PK_O0_BP_REGULATOR_N);
}

static int ov5648_pad_configure(struct ov5648_sensor *sensor)
{
	int ret;

	/* Configure pads as input. */

	ret = ov5648_write(sensor, OV5648_PAD_OEN1_REG, 0);
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_PAD_OEN2_REG, 0);
	if (ret)
		return ret;

	/* Disable FREX pin. */

	return ov5648_write(sensor, OV5648_PAD_PK_REG,
			    OV5648_PAD_PK_DRIVE_STRENGTH_1X |
			    OV5648_PAD_PK_FREX_N);
}

static int ov5648_mipi_configure(struct ov5648_sensor *sensor)
{
	struct v4l2_mbus_config_mipi_csi2 *bus_mipi_csi2 =
		&sensor->endpoint.bus.mipi_csi2;
	unsigned int lanes_count = bus_mipi_csi2->num_data_lanes;
	int ret;

	ret = ov5648_write(sensor, OV5648_MIPI_CTRL0_REG,
			   OV5648_MIPI_CTRL0_CLK_LANE_AUTOGATE |
			   OV5648_MIPI_CTRL0_LANE_SELECT_LANE1 |
			   OV5648_MIPI_CTRL0_IDLE_LP11);
	if (ret)
		return ret;

	return ov5648_write(sensor, OV5648_MIPI_SC_CTRL0_REG,
			    OV5648_MIPI_SC_CTRL0_MIPI_LANES(lanes_count) |
			    OV5648_MIPI_SC_CTRL0_PHY_LP_RX_PD |
			    OV5648_MIPI_SC_CTRL0_MIPI_EN);
}

static int ov5648_black_level_configure(struct ov5648_sensor *sensor)
{
	int ret;

	/* Up to 6 lines are available for black level calibration. */

	ret = ov5648_write(sensor, OV5648_BLC_CTRL1_REG,
			   OV5648_BLC_CTRL1_START_LINE(2));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_BLC_CTRL2_REG,
			   OV5648_BLC_CTRL2_AUTO_EN |
			   OV5648_BLC_CTRL2_RESET_FRAME_NUM(5));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_BLC_LINE_NUM_REG,
			   OV5648_BLC_LINE_NUM(4));
	if (ret)
		return ret;

	return ov5648_update_bits(sensor, OV5648_BLC_CTRL5_REG,
				  OV5648_BLC_CTRL5_UPDATE_EN,
				  OV5648_BLC_CTRL5_UPDATE_EN);
}

static int ov5648_isp_configure(struct ov5648_sensor *sensor)
{
	u8 bits;
	int ret;

	/* Enable black and white level correction. */
	bits = OV5648_ISP_CTRL0_BLACK_CORRECT_EN |
	       OV5648_ISP_CTRL0_WHITE_CORRECT_EN;

	ret = ov5648_update_bits(sensor, OV5648_ISP_CTRL0_REG, bits, bits);
	if (ret)
		return ret;

	/* Enable AWB. */
	ret = ov5648_write(sensor, OV5648_ISP_CTRL1_REG,
			   OV5648_ISP_CTRL1_AWB_EN);
	if (ret)
		return ret;

	/* Enable AWB gain and windowing. */
	ret = ov5648_write(sensor, OV5648_ISP_CTRL2_REG,
			   OV5648_ISP_CTRL2_WIN_EN |
			   OV5648_ISP_CTRL2_AWB_GAIN_EN);
	if (ret)
		return ret;

	/* Enable buffering and auto-binning. */
	ret = ov5648_write(sensor, OV5648_ISP_CTRL3_REG,
			   OV5648_ISP_CTRL3_BUF_EN |
			   OV5648_ISP_CTRL3_BIN_AUTO_EN);
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_ISP_CTRL4_REG, 0);
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_ISP_CTRL1F_REG,
			   OV5648_ISP_CTRL1F_OUTPUT_EN);
	if (ret)
		return ret;

	/* Enable post-binning filters. */
	ret = ov5648_write(sensor, OV5648_ISP_CTRL4B_REG,
			   OV5648_ISP_CTRL4B_POST_BIN_H_EN |
			   OV5648_ISP_CTRL4B_POST_BIN_V_EN);
	if (ret)
		return ret;

	/* Disable debanding and night mode. Debug bit seems necessary. */
	ret = ov5648_write(sensor, OV5648_AEC_CTRL0_REG,
			   OV5648_AEC_CTRL0_DEBUG |
			   OV5648_AEC_CTRL0_START_SEL_EN);
	if (ret)
		return ret;

	return ov5648_write(sensor, OV5648_MANUAL_CTRL_REG,
			    OV5648_MANUAL_CTRL_FRAME_DELAY(1));
}

static unsigned long ov5648_mode_pll1_rate(struct ov5648_sensor *sensor,
					   const struct ov5648_pll1_config *config)
{
	unsigned long xvclk_rate;
	unsigned long pll1_rate;

	xvclk_rate = clk_get_rate(sensor->xvclk);
	pll1_rate = xvclk_rate * config->pll_mul;

	switch (config->pll_pre_div) {
	case 5:
		pll1_rate *= 3;
		pll1_rate /= 2;
		break;
	case 7:
		pll1_rate *= 5;
		pll1_rate /= 2;
		break;
	default:
		pll1_rate /= config->pll_pre_div;
		break;
	}

	return pll1_rate;
}

static int ov5648_mode_pll1_configure(struct ov5648_sensor *sensor,
				      const struct ov5648_mode *mode,
				      u32 mbus_code)
{
	const struct ov5648_pll1_config *config;
	u8 value;
	int ret;

	value = OV5648_PLL_CTRL0_PLL_CHARGE_PUMP(1);

	switch (mbus_code) {
	case MEDIA_BUS_FMT_SBGGR8_1X8:
		config = mode->pll1_config[0];
		value |= OV5648_PLL_CTRL0_BITS(8);
		break;
	case MEDIA_BUS_FMT_SBGGR10_1X10:
		config = mode->pll1_config[1];
		value |= OV5648_PLL_CTRL0_BITS(10);
		break;
	default:
		return -EINVAL;
	}

	ret = ov5648_write(sensor, OV5648_PLL_CTRL0_REG, value);
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_PLL_DIV_REG,
			   OV5648_PLL_DIV_ROOT_DIV(config->root_div) |
			   OV5648_PLL_DIV_PLL_PRE_DIV(config->pll_pre_div));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_PLL_MUL_REG,
			   OV5648_PLL_MUL(config->pll_mul));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_PLL_CTRL1_REG,
			   OV5648_PLL_CTRL1_SYS_DIV(config->sys_div) |
			   OV5648_PLL_CTRL1_MIPI_DIV(config->mipi_div));
	if (ret)
		return ret;

	return ov5648_write(sensor, OV5648_SRB_CTRL_REG,
			    OV5648_SRB_CTRL_SCLK_DIV(config->sclk_div) |
			    OV5648_SRB_CTRL_SCLK_ARBITER_EN);
}

static int ov5648_mode_pll2_configure(struct ov5648_sensor *sensor,
				      const struct ov5648_mode *mode)
{
	const struct ov5648_pll2_config *config = mode->pll2_config;
	int ret;

	ret = ov5648_write(sensor, OV5648_PLLS_DIV_REG,
			   OV5648_PLLS_DIV_PLLS_PRE_DIV(config->plls_pre_div) |
			   OV5648_PLLS_DIV_PLLS_DIV_R(config->plls_div_r) |
			   OV5648_PLLS_DIV_PLLS_SEL_DIV(config->sel_div));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_PLLS_MUL_REG,
			   OV5648_PLLS_MUL(config->plls_mul));
	if (ret)
		return ret;

	return ov5648_write(sensor, OV5648_PLLS_CTRL_REG,
			    OV5648_PLLS_CTRL_PLL_CHARGE_PUMP(1) |
			    OV5648_PLLS_CTRL_SYS_DIV(config->sys_div));
}

static int ov5648_mode_configure(struct ov5648_sensor *sensor,
				 const struct ov5648_mode *mode, u32 mbus_code)
{
	int ret;

	/* Crop Start X */

	ret = ov5648_write(sensor, OV5648_CROP_START_X_H_REG,
			   OV5648_CROP_START_X_H(mode->crop_start_x));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_CROP_START_X_L_REG,
			   OV5648_CROP_START_X_L(mode->crop_start_x));
	if (ret)
		return ret;

	/* Offset X */

	ret = ov5648_write(sensor, OV5648_OFFSET_X_H_REG,
			   OV5648_OFFSET_X_H(mode->offset_x));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_OFFSET_X_L_REG,
			   OV5648_OFFSET_X_L(mode->offset_x));
	if (ret)
		return ret;

	/* Output Size X */

	ret = ov5648_write(sensor, OV5648_OUTPUT_SIZE_X_H_REG,
			   OV5648_OUTPUT_SIZE_X_H(mode->output_size_x));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_OUTPUT_SIZE_X_L_REG,
			   OV5648_OUTPUT_SIZE_X_L(mode->output_size_x));
	if (ret)
		return ret;

	/* Crop End X */

	ret = ov5648_write(sensor, OV5648_CROP_END_X_H_REG,
			   OV5648_CROP_END_X_H(mode->crop_end_x));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_CROP_END_X_L_REG,
			   OV5648_CROP_END_X_L(mode->crop_end_x));
	if (ret)
		return ret;

	/* Horizontal Total Size */

	ret = ov5648_write(sensor, OV5648_HTS_H_REG, OV5648_HTS_H(mode->hts));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_HTS_L_REG, OV5648_HTS_L(mode->hts));
	if (ret)
		return ret;

	/* Crop Start Y */

	ret = ov5648_write(sensor, OV5648_CROP_START_Y_H_REG,
			   OV5648_CROP_START_Y_H(mode->crop_start_y));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_CROP_START_Y_L_REG,
			   OV5648_CROP_START_Y_L(mode->crop_start_y));
	if (ret)
		return ret;

	/* Offset Y */

	ret = ov5648_write(sensor, OV5648_OFFSET_Y_H_REG,
			   OV5648_OFFSET_Y_H(mode->offset_y));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_OFFSET_Y_L_REG,
			   OV5648_OFFSET_Y_L(mode->offset_y));
	if (ret)
		return ret;

	/* Output Size Y */

	ret = ov5648_write(sensor, OV5648_OUTPUT_SIZE_Y_H_REG,
			   OV5648_OUTPUT_SIZE_Y_H(mode->output_size_y));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_OUTPUT_SIZE_Y_L_REG,
			   OV5648_OUTPUT_SIZE_Y_L(mode->output_size_y));
	if (ret)
		return ret;

	/* Crop End Y */

	ret = ov5648_write(sensor, OV5648_CROP_END_Y_H_REG,
			   OV5648_CROP_END_Y_H(mode->crop_end_y));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_CROP_END_Y_L_REG,
			   OV5648_CROP_END_Y_L(mode->crop_end_y));
	if (ret)
		return ret;

	/* Vertical Total Size */

	ret = ov5648_write(sensor, OV5648_VTS_H_REG, OV5648_VTS_H(mode->vts));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_VTS_L_REG, OV5648_VTS_L(mode->vts));
	if (ret)
		return ret;

	/* Flip/Mirror/Binning */

	/*
	 * A debug bit is enabled by default and needs to be cleared for
	 * subsampling to work.
	 */
	ret = ov5648_update_bits(sensor, OV5648_TC20_REG,
				 OV5648_TC20_DEBUG |
				 OV5648_TC20_BINNING_VERT_EN,
				 mode->binning_y ? OV5648_TC20_BINNING_VERT_EN :
				 0);
	if (ret)
		return ret;

	ret = ov5648_update_bits(sensor, OV5648_TC21_REG,
				 OV5648_TC21_BINNING_HORZ_EN,
				 mode->binning_x ? OV5648_TC21_BINNING_HORZ_EN :
				 0);
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_SUB_INC_X_REG,
			   OV5648_SUB_INC_X_ODD(mode->inc_x_odd) |
			   OV5648_SUB_INC_X_EVEN(mode->inc_x_even));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_SUB_INC_Y_REG,
			   OV5648_SUB_INC_Y_ODD(mode->inc_y_odd) |
			   OV5648_SUB_INC_Y_EVEN(mode->inc_y_even));
	if (ret)
		return ret;

	/* PLLs */

	ret = ov5648_mode_pll1_configure(sensor, mode, mbus_code);
	if (ret)
		return ret;

	ret = ov5648_mode_pll2_configure(sensor, mode);
	if (ret)
		return ret;

	/* Extra registers */

	if (mode->register_values) {
		ret = ov5648_write_sequence(sensor, mode->register_values,
					    mode->register_values_count);
		if (ret)
			return ret;
	}

	return 0;
}

static unsigned long ov5648_mode_mipi_clk_rate(struct ov5648_sensor *sensor,
					       const struct ov5648_mode *mode,
					       u32 mbus_code)
{
	const struct ov5648_pll1_config *config;
	unsigned long pll1_rate;

	switch (mbus_code) {
	case MEDIA_BUS_FMT_SBGGR8_1X8:
		config = mode->pll1_config[0];
		break;
	case MEDIA_BUS_FMT_SBGGR10_1X10:
		config = mode->pll1_config[1];
		break;
	default:
		return 0;
	}

	pll1_rate = ov5648_mode_pll1_rate(sensor, config);

	return pll1_rate / config->sys_div / config->mipi_div / 2;
}

/* Exposure */

static int ov5648_exposure_auto_configure(struct ov5648_sensor *sensor,
					  bool enable)
{
	return ov5648_update_bits(sensor, OV5648_MANUAL_CTRL_REG,
				  OV5648_MANUAL_CTRL_AEC_MANUAL_EN,
				  enable ? 0 : OV5648_MANUAL_CTRL_AEC_MANUAL_EN);
}

static int ov5648_exposure_configure(struct ov5648_sensor *sensor, u32 exposure)
{
	struct ov5648_ctrls *ctrls = &sensor->ctrls;
	int ret;

	if (ctrls->exposure_auto->val != V4L2_EXPOSURE_MANUAL)
		return -EINVAL;

	ret = ov5648_write(sensor, OV5648_EXPOSURE_CTRL_HH_REG,
			   OV5648_EXPOSURE_CTRL_HH(exposure));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_EXPOSURE_CTRL_H_REG,
			   OV5648_EXPOSURE_CTRL_H(exposure));
	if (ret)
		return ret;

	return ov5648_write(sensor, OV5648_EXPOSURE_CTRL_L_REG,
			    OV5648_EXPOSURE_CTRL_L(exposure));
}

static int ov5648_exposure_value(struct ov5648_sensor *sensor,
				 u32 *exposure)
{
	u8 exposure_hh = 0, exposure_h = 0, exposure_l = 0;
	int ret;

	ret = ov5648_read(sensor, OV5648_EXPOSURE_CTRL_HH_REG, &exposure_hh);
	if (ret)
		return ret;

	ret = ov5648_read(sensor, OV5648_EXPOSURE_CTRL_H_REG, &exposure_h);
	if (ret)
		return ret;

	ret = ov5648_read(sensor, OV5648_EXPOSURE_CTRL_L_REG, &exposure_l);
	if (ret)
		return ret;

	*exposure = OV5648_EXPOSURE_CTRL_HH_VALUE((u32)exposure_hh) |
		    OV5648_EXPOSURE_CTRL_H_VALUE((u32)exposure_h) |
		    OV5648_EXPOSURE_CTRL_L_VALUE((u32)exposure_l);

	return 0;
}

/* Gain */

static int ov5648_gain_auto_configure(struct ov5648_sensor *sensor, bool enable)
{
	return ov5648_update_bits(sensor, OV5648_MANUAL_CTRL_REG,
				  OV5648_MANUAL_CTRL_AGC_MANUAL_EN,
				  enable ? 0 : OV5648_MANUAL_CTRL_AGC_MANUAL_EN);
}

static int ov5648_gain_configure(struct ov5648_sensor *sensor, u32 gain)
{
	struct ov5648_ctrls *ctrls = &sensor->ctrls;
	int ret;

	if (ctrls->gain_auto->val)
		return -EINVAL;

	ret = ov5648_write(sensor, OV5648_GAIN_CTRL_H_REG,
			   OV5648_GAIN_CTRL_H(gain));
	if (ret)
		return ret;

	return ov5648_write(sensor, OV5648_GAIN_CTRL_L_REG,
			    OV5648_GAIN_CTRL_L(gain));
}

static int ov5648_gain_value(struct ov5648_sensor *sensor, u32 *gain)
{
	u8 gain_h = 0, gain_l = 0;
	int ret;

	ret = ov5648_read(sensor, OV5648_GAIN_CTRL_H_REG, &gain_h);
	if (ret)
		return ret;

	ret = ov5648_read(sensor, OV5648_GAIN_CTRL_L_REG, &gain_l);
	if (ret)
		return ret;

	*gain = OV5648_GAIN_CTRL_H_VALUE((u32)gain_h) |
		OV5648_GAIN_CTRL_L_VALUE((u32)gain_l);

	return 0;
}

/* White Balance */

static int ov5648_white_balance_auto_configure(struct ov5648_sensor *sensor,
					       bool enable)
{
	return ov5648_write(sensor, OV5648_AWB_CTRL_REG,
			    enable ? 0 : OV5648_AWB_CTRL_GAIN_MANUAL_EN);
}

static int ov5648_white_balance_configure(struct ov5648_sensor *sensor,
					  u32 red_balance, u32 blue_balance)
{
	struct ov5648_ctrls *ctrls = &sensor->ctrls;
	int ret;

	if (ctrls->white_balance_auto->val)
		return -EINVAL;

	ret = ov5648_write(sensor, OV5648_GAIN_RED_MAN_H_REG,
			   OV5648_GAIN_RED_MAN_H(red_balance));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_GAIN_RED_MAN_L_REG,
			   OV5648_GAIN_RED_MAN_L(red_balance));
	if (ret)
		return ret;

	ret = ov5648_write(sensor, OV5648_GAIN_BLUE_MAN_H_REG,
			   OV5648_GAIN_BLUE_MAN_H(blue_balance));
	if (ret)
		return ret;

	return ov5648_write(sensor, OV5648_GAIN_BLUE_MAN_L_REG,
			    OV5648_GAIN_BLUE_MAN_L(blue_balance));
}

/* Flip */

static int ov5648_flip_vert_configure(struct ov5648_sensor *sensor, bool enable)
{
	u8 bits = OV5648_TC20_FLIP_VERT_ISP_EN |
		  OV5648_TC20_FLIP_VERT_SENSOR_EN;

	return ov5648_update_bits(sensor, OV5648_TC20_REG, bits,
				  enable ? bits : 0);
}

static int ov5648_flip_horz_configure(struct ov5648_sensor *sensor, bool enable)
{
	u8 bits = OV5648_TC21_FLIP_HORZ_ISP_EN |
		  OV5648_TC21_FLIP_HORZ_SENSOR_EN;

	return ov5648_update_bits(sensor, OV5648_TC21_REG, bits,
				  enable ? bits : 0);
}

/* Test Pattern */

static int ov5648_test_pattern_configure(struct ov5648_sensor *sensor,
					 unsigned int index)
{
	if (index >= ARRAY_SIZE(ov5648_test_pattern_bits))
		return -EINVAL;

	return ov5648_write(sensor, OV5648_ISP_CTRL3D_REG,
			    ov5648_test_pattern_bits[index]);
}

/* State */

static int ov5648_state_mipi_configure(struct ov5648_sensor *sensor,
				       const struct ov5648_mode *mode,
				       u32 mbus_code)
{
	struct ov5648_ctrls *ctrls = &sensor->ctrls;
	struct v4l2_mbus_config_mipi_csi2 *bus_mipi_csi2 =
		&sensor->endpoint.bus.mipi_csi2;
	unsigned long mipi_clk_rate;
	unsigned int bits_per_sample;
	unsigned int lanes_count;
	unsigned int i, j;
	s64 mipi_pixel_rate;

	mipi_clk_rate = ov5648_mode_mipi_clk_rate(sensor, mode, mbus_code);
	if (!mipi_clk_rate)
		return -EINVAL;

	for (i = 0; i < ARRAY_SIZE(ov5648_link_freq_menu); i++) {
		s64 freq = ov5648_link_freq_menu[i];

		if (freq == mipi_clk_rate)
			break;
	}

	for (j = 0; j < sensor->endpoint.nr_of_link_frequencies; j++) {
		u64 freq = sensor->endpoint.link_frequencies[j];

		if (freq == mipi_clk_rate)
			break;
	}

	if (i == ARRAY_SIZE(ov5648_link_freq_menu)) {
		dev_err(sensor->dev,
			"failed to find %lu clk rate in link freq\n",
			mipi_clk_rate);
	} else if (j == sensor->endpoint.nr_of_link_frequencies) {
		dev_err(sensor->dev,
			"failed to find %lu clk rate in endpoint link-frequencies\n",
			mipi_clk_rate);
	} else {
		__v4l2_ctrl_s_ctrl(ctrls->link_freq, i);
	}

	switch (mbus_code) {
	case MEDIA_BUS_FMT_SBGGR8_1X8:
		bits_per_sample = 8;
		break;
	case MEDIA_BUS_FMT_SBGGR10_1X10:
		bits_per_sample = 10;
		break;
	default:
		return -EINVAL;
	}

	lanes_count = bus_mipi_csi2->num_data_lanes;
	mipi_pixel_rate = mipi_clk_rate * 2 * lanes_count / bits_per_sample;

	__v4l2_ctrl_s_ctrl_int64(ctrls->pixel_rate, mipi_pixel_rate);

	return 0;
}

static int ov5648_state_configure(struct ov5648_sensor *sensor,
				  const struct ov5648_mode *mode,
				  u32 mbus_code)
{
	int ret;

	if (sensor->state.streaming)
		return -EBUSY;

	/* State will be configured at first power on otherwise. */
	if (pm_runtime_enabled(sensor->dev) &&
	    !pm_runtime_suspended(sensor->dev)) {
		ret = ov5648_mode_configure(sensor, mode, mbus_code);
		if (ret)
			return ret;
	}

	ret = ov5648_state_mipi_configure(sensor, mode, mbus_code);
	if (ret)
		return ret;

	sensor->state.mode = mode;
	sensor->state.mbus_code = mbus_code;

	return 0;
}

static int ov5648_state_init(struct ov5648_sensor *sensor)
{
	int ret;

	mutex_lock(&sensor->mutex);
	ret = ov5648_state_configure(sensor, &ov5648_modes[0],
				     ov5648_mbus_codes[0]);
	mutex_unlock(&sensor->mutex);

	return ret;
}

/* Sensor Base */

static int ov5648_sensor_init(struct ov5648_sensor *sensor)
{
	int ret;

	ret = ov5648_sw_reset(sensor);
	if (ret) {
		dev_err(sensor->dev, "failed to perform sw reset\n");
		return ret;
	}

	ret = ov5648_sw_standby(sensor, 1);
	if (ret) {
		dev_err(sensor->dev, "failed to set sensor standby\n");
		return ret;
	}

	ret = ov5648_chip_id_check(sensor);
	if (ret) {
		dev_err(sensor->dev, "failed to check sensor chip id\n");
		return ret;
	}

	ret = ov5648_avdd_internal_power(sensor, !sensor->avdd);
	if (ret) {
		dev_err(sensor->dev, "failed to set internal avdd power\n");
		return ret;
	}

	ret = ov5648_write_sequence(sensor, ov5648_init_sequence,
				    ARRAY_SIZE(ov5648_init_sequence));
	if (ret) {
		dev_err(sensor->dev, "failed to write init sequence\n");
		return ret;
	}

	ret = ov5648_pad_configure(sensor);
	if (ret) {
		dev_err(sensor->dev, "failed to configure pad\n");
		return ret;
	}

	ret = ov5648_mipi_configure(sensor);
	if (ret) {
		dev_err(sensor->dev, "failed to configure MIPI\n");
		return ret;
	}

	ret = ov5648_isp_configure(sensor);
	if (ret) {
		dev_err(sensor->dev, "failed to configure ISP\n");
		return ret;
	}

	ret = ov5648_black_level_configure(sensor);
	if (ret) {
		dev_err(sensor->dev, "failed to configure black level\n");
		return ret;
	}

	/* Configure current mode. */
	ret = ov5648_state_configure(sensor, sensor->state.mode,
				     sensor->state.mbus_code);
	if (ret) {
		dev_err(sensor->dev, "failed to configure state\n");
		return ret;
	}

	return 0;
}

static int ov5648_sensor_power(struct ov5648_sensor *sensor, bool on)
{
	/* Keep initialized to zero for disable label. */
	int ret = 0;

	/*
	 * General notes about the power sequence:
	 * - power-down GPIO must be active (low) during power-on;
	 * - reset GPIO state does not matter during power-on;
	 * - XVCLK must be provided 1 ms before register access;
	 * - 10 ms are needed between power-down deassert and register access.
	 */

	/* Note that regulator-and-GPIO-based power is untested. */
	if (on) {
		gpiod_set_value_cansleep(sensor->reset, 1);
		gpiod_set_value_cansleep(sensor->powerdown, 1);

		ret = regulator_enable(sensor->dovdd);
		if (ret) {
			dev_err(sensor->dev,
				"failed to enable DOVDD regulator\n");
			goto disable;
		}

		if (sensor->avdd) {
			ret = regulator_enable(sensor->avdd);
			if (ret) {
				dev_err(sensor->dev,
					"failed to enable AVDD regulator\n");
				goto disable;
			}
		}

		ret = regulator_enable(sensor->dvdd);
		if (ret) {
			dev_err(sensor->dev,
				"failed to enable DVDD regulator\n");
			goto disable;
		}

		/* According to OV5648 power up diagram. */
		usleep_range(5000, 10000);

		ret = clk_prepare_enable(sensor->xvclk);
		if (ret) {
			dev_err(sensor->dev, "failed to enable XVCLK clock\n");
			goto disable;
		}

		gpiod_set_value_cansleep(sensor->reset, 0);
		gpiod_set_value_cansleep(sensor->powerdown, 0);

		usleep_range(20000, 25000);
	} else {
disable:
		gpiod_set_value_cansleep(sensor->powerdown, 1);
		gpiod_set_value_cansleep(sensor->reset, 1);

		clk_disable_unprepare(sensor->xvclk);

		regulator_disable(sensor->dvdd);

		if (sensor->avdd)
			regulator_disable(sensor->avdd);

		regulator_disable(sensor->dovdd);
	}

	return ret;
}

/* Controls */

static int ov5648_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
{
	struct v4l2_subdev *subdev = ov5648_ctrl_subdev(ctrl);
	struct ov5648_sensor *sensor = ov5648_subdev_sensor(subdev);
	struct ov5648_ctrls *ctrls = &sensor->ctrls;
	int ret;

	switch (ctrl->id) {
	case V4L2_CID_EXPOSURE_AUTO:
		ret = ov5648_exposure_value(sensor, &ctrls->exposure->val);
		if (ret)
			return ret;
		break;
	case V4L2_CID_AUTOGAIN:
		ret = ov5648_gain_value(sensor, &ctrls->gain->val);
		if (ret)
			return ret;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int ov5648_s_ctrl(struct v4l2_ctrl *ctrl)
{
	struct v4l2_subdev *subdev = ov5648_ctrl_subdev(ctrl);
	struct ov5648_sensor *sensor = ov5648_subdev_sensor(subdev);
	struct ov5648_ctrls *ctrls = &sensor->ctrls;
	unsigned int index;
	bool enable;
	int ret;

	/* Wait for the sensor to be on before setting controls. */
	if (pm_runtime_suspended(sensor->dev))
		return 0;

	switch (ctrl->id) {
	case V4L2_CID_EXPOSURE_AUTO:
		enable = ctrl->val == V4L2_EXPOSURE_AUTO;

		ret = ov5648_exposure_auto_configure(sensor, enable);
		if (ret)
			return ret;

		if (!enable && ctrls->exposure->is_new) {
			ret = ov5648_exposure_configure(sensor,
							ctrls->exposure->val);
			if (ret)
				return ret;
		}
		break;
	case V4L2_CID_AUTOGAIN:
		enable = !!ctrl->val;

		ret = ov5648_gain_auto_configure(sensor, enable);
		if (ret)
			return ret;

		if (!enable) {
			ret = ov5648_gain_configure(sensor, ctrls->gain->val);
			if (ret)
				return ret;
		}
		break;
	case V4L2_CID_AUTO_WHITE_BALANCE:
		enable = !!ctrl->val;

		ret = ov5648_white_balance_auto_configure(sensor, enable);
		if (ret)
			return ret;

		if (!enable) {
			ret = ov5648_white_balance_configure(sensor,
							     ctrls->red_balance->val,
							     ctrls->blue_balance->val);
			if (ret)
				return ret;
		}
		break;
	case V4L2_CID_HFLIP:
		enable = !!ctrl->val;
		return ov5648_flip_horz_configure(sensor, enable);
	case V4L2_CID_VFLIP:
		enable = !!ctrl->val;
		return ov5648_flip_vert_configure(sensor, enable);
	case V4L2_CID_TEST_PATTERN:
		index = (unsigned int)ctrl->val;
		return ov5648_test_pattern_configure(sensor, index);
	default:
		return -EINVAL;
	}

	return 0;
}

static const struct v4l2_ctrl_ops ov5648_ctrl_ops = {
	.g_volatile_ctrl	= ov5648_g_volatile_ctrl,
	.s_ctrl			= ov5648_s_ctrl,
};

static int ov5648_ctrls_init(struct ov5648_sensor *sensor)
{
	struct ov5648_ctrls *ctrls = &sensor->ctrls;
	struct v4l2_ctrl_handler *handler = &ctrls->handler;
	const struct v4l2_ctrl_ops *ops = &ov5648_ctrl_ops;
	int ret;

	v4l2_ctrl_handler_init(handler, 32);

	/* Use our mutex for ctrl locking. */
	handler->lock = &sensor->mutex;

	/* Exposure */

	ctrls->exposure_auto = v4l2_ctrl_new_std_menu(handler, ops,
						      V4L2_CID_EXPOSURE_AUTO,
						      V4L2_EXPOSURE_MANUAL, 0,
						      V4L2_EXPOSURE_AUTO);

	ctrls->exposure = v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE,
					    16, 1048575, 16, 512);

	v4l2_ctrl_auto_cluster(2, &ctrls->exposure_auto, 1, true);

	/* Gain */

	ctrls->gain_auto =
		v4l2_ctrl_new_std(handler, ops, V4L2_CID_AUTOGAIN, 0, 1, 1, 1);

	ctrls->gain = v4l2_ctrl_new_std(handler, ops, V4L2_CID_GAIN, 16, 1023,
					16, 16);

	v4l2_ctrl_auto_cluster(2, &ctrls->gain_auto, 0, true);

	/* White Balance */

	ctrls->white_balance_auto =
		v4l2_ctrl_new_std(handler, ops, V4L2_CID_AUTO_WHITE_BALANCE, 0,
				  1, 1, 1);

	ctrls->red_balance = v4l2_ctrl_new_std(handler, ops,
					       V4L2_CID_RED_BALANCE, 0, 4095,
					       1, 1024);

	ctrls->blue_balance = v4l2_ctrl_new_std(handler, ops,
						V4L2_CID_BLUE_BALANCE, 0, 4095,
						1, 1024);

	v4l2_ctrl_auto_cluster(3, &ctrls->white_balance_auto, 0, false);

	/* Flip */

	v4l2_ctrl_new_std(handler, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
	v4l2_ctrl_new_std(handler, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);

	/* Test Pattern */

	v4l2_ctrl_new_std_menu_items(handler, ops, V4L2_CID_TEST_PATTERN,
				     ARRAY_SIZE(ov5648_test_pattern_menu) - 1,
				     0, 0, ov5648_test_pattern_menu);

	/* MIPI CSI-2 */

	ctrls->link_freq =
		v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
				       ARRAY_SIZE(ov5648_link_freq_menu) - 1,
				       0, ov5648_link_freq_menu);

	ctrls->pixel_rate =
		v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE, 1,
				  INT_MAX, 1, 1);

	if (handler->error) {
		ret = handler->error;
		goto error_ctrls;
	}

	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;

	ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
	ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;

	sensor->subdev.ctrl_handler = handler;

	return 0;

error_ctrls:
	v4l2_ctrl_handler_free(handler);

	return ret;
}

/* Subdev Video Operations */

static int ov5648_s_stream(struct v4l2_subdev *subdev, int enable)
{
	struct ov5648_sensor *sensor = ov5648_subdev_sensor(subdev);
	struct ov5648_state *state = &sensor->state;
	int ret;

	if (enable) {
		ret = pm_runtime_resume_and_get(sensor->dev);
		if (ret < 0)
			return ret;
	}

	mutex_lock(&sensor->mutex);
	ret = ov5648_sw_standby(sensor, !enable);
	mutex_unlock(&sensor->mutex);

	if (ret)
		return ret;

	state->streaming = !!enable;

	if (!enable)
		pm_runtime_put(sensor->dev);

	return 0;
}

static int ov5648_g_frame_interval(struct v4l2_subdev *subdev,
				   struct v4l2_subdev_frame_interval *interval)
{
	struct ov5648_sensor *sensor = ov5648_subdev_sensor(subdev);
	const struct ov5648_mode *mode;
	int ret = 0;

	mutex_lock(&sensor->mutex);

	mode = sensor->state.mode;

	switch (sensor->state.mbus_code) {
	case MEDIA_BUS_FMT_SBGGR8_1X8:
		interval->interval = mode->frame_interval[0];
		break;
	case MEDIA_BUS_FMT_SBGGR10_1X10:
		interval->interval = mode->frame_interval[1];
		break;
	default:
		ret = -EINVAL;
	}

	mutex_unlock(&sensor->mutex);

	return ret;
}

static const struct v4l2_subdev_video_ops ov5648_subdev_video_ops = {
	.s_stream		= ov5648_s_stream,
	.g_frame_interval	= ov5648_g_frame_interval,
	.s_frame_interval	= ov5648_g_frame_interval,
};

/* Subdev Pad Operations */

static int ov5648_enum_mbus_code(struct v4l2_subdev *subdev,
				 struct v4l2_subdev_state *sd_state,
				 struct v4l2_subdev_mbus_code_enum *code_enum)
{
	if (code_enum->index >= ARRAY_SIZE(ov5648_mbus_codes))
		return -EINVAL;

	code_enum->code = ov5648_mbus_codes[code_enum->index];

	return 0;
}

static void ov5648_mbus_format_fill(struct v4l2_mbus_framefmt *mbus_format,
				    u32 mbus_code,
				    const struct ov5648_mode *mode)
{
	mbus_format->width = mode->output_size_x;
	mbus_format->height = mode->output_size_y;
	mbus_format->code = mbus_code;

	mbus_format->field = V4L2_FIELD_NONE;
	mbus_format->colorspace = V4L2_COLORSPACE_RAW;
	mbus_format->ycbcr_enc =
		V4L2_MAP_YCBCR_ENC_DEFAULT(mbus_format->colorspace);
	mbus_format->quantization = V4L2_QUANTIZATION_FULL_RANGE;
	mbus_format->xfer_func =
		V4L2_MAP_XFER_FUNC_DEFAULT(mbus_format->colorspace);
}

static int ov5648_get_fmt(struct v4l2_subdev *subdev,
			  struct v4l2_subdev_state *sd_state,
			  struct v4l2_subdev_format *format)
{
	struct ov5648_sensor *sensor = ov5648_subdev_sensor(subdev);
	struct v4l2_mbus_framefmt *mbus_format = &format->format;

	mutex_lock(&sensor->mutex);

	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
		*mbus_format = *v4l2_subdev_get_try_format(subdev, sd_state,
							   format->pad);
	else
		ov5648_mbus_format_fill(mbus_format, sensor->state.mbus_code,
					sensor->state.mode);

	mutex_unlock(&sensor->mutex);

	return 0;
}

static int ov5648_set_fmt(struct v4l2_subdev *subdev,
			  struct v4l2_subdev_state *sd_state,
			  struct v4l2_subdev_format *format)
{
	struct ov5648_sensor *sensor = ov5648_subdev_sensor(subdev);
	struct v4l2_mbus_framefmt *mbus_format = &format->format;
	const struct ov5648_mode *mode;
	u32 mbus_code = 0;
	unsigned int index;
	int ret = 0;

	mutex_lock(&sensor->mutex);

	if (sensor->state.streaming) {
		ret = -EBUSY;
		goto complete;
	}

	/* Try to find requested mbus code. */
	for (index = 0; index < ARRAY_SIZE(ov5648_mbus_codes); index++) {
		if (ov5648_mbus_codes[index] == mbus_format->code) {
			mbus_code = mbus_format->code;
			break;
		}
	}

	/* Fallback to default. */
	if (!mbus_code)
		mbus_code = ov5648_mbus_codes[0];

	/* Find the mode with nearest dimensions. */
	mode = v4l2_find_nearest_size(ov5648_modes, ARRAY_SIZE(ov5648_modes),
				      output_size_x, output_size_y,
				      mbus_format->width, mbus_format->height);
	if (!mode) {
		ret = -EINVAL;
		goto complete;
	}

	ov5648_mbus_format_fill(mbus_format, mbus_code, mode);

	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
		*v4l2_subdev_get_try_format(subdev, sd_state, format->pad) =
			*mbus_format;
	else if (sensor->state.mode != mode ||
		 sensor->state.mbus_code != mbus_code)
		ret = ov5648_state_configure(sensor, mode, mbus_code);

complete:
	mutex_unlock(&sensor->mutex);

	return ret;
}

static int ov5648_enum_frame_size(struct v4l2_subdev *subdev,
				  struct v4l2_subdev_state *sd_state,
				  struct v4l2_subdev_frame_size_enum *size_enum)
{
	const struct ov5648_mode *mode;

	if (size_enum->index >= ARRAY_SIZE(ov5648_modes))
		return -EINVAL;

	mode = &ov5648_modes[size_enum->index];

	size_enum->min_width = size_enum->max_width = mode->output_size_x;
	size_enum->min_height = size_enum->max_height = mode->output_size_y;

	return 0;
}

static int ov5648_enum_frame_interval(struct v4l2_subdev *subdev,
				      struct v4l2_subdev_state *sd_state,
				      struct v4l2_subdev_frame_interval_enum *interval_enum)
{
	const struct ov5648_mode *mode = NULL;
	unsigned int mode_index;
	unsigned int interval_index;

	if (interval_enum->index > 0)
		return -EINVAL;

	/*
	 * Multiple modes with the same dimensions may have different frame
	 * intervals, so look up each relevant mode.
	 */
	for (mode_index = 0, interval_index = 0;
	     mode_index < ARRAY_SIZE(ov5648_modes); mode_index++) {
		mode = &ov5648_modes[mode_index];

		if (mode->output_size_x == interval_enum->width &&
		    mode->output_size_y == interval_enum->height) {
			if (interval_index == interval_enum->index)
				break;

			interval_index++;
		}
	}

	if (mode_index == ARRAY_SIZE(ov5648_modes))
		return -EINVAL;

	switch (interval_enum->code) {
	case MEDIA_BUS_FMT_SBGGR8_1X8:
		interval_enum->interval = mode->frame_interval[0];
		break;
	case MEDIA_BUS_FMT_SBGGR10_1X10:
		interval_enum->interval = mode->frame_interval[1];
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static const struct v4l2_subdev_pad_ops ov5648_subdev_pad_ops = {
	.enum_mbus_code		= ov5648_enum_mbus_code,
	.get_fmt		= ov5648_get_fmt,
	.set_fmt		= ov5648_set_fmt,
	.enum_frame_size	= ov5648_enum_frame_size,
	.enum_frame_interval	= ov5648_enum_frame_interval,
};

static const struct v4l2_subdev_ops ov5648_subdev_ops = {
	.video		= &ov5648_subdev_video_ops,
	.pad		= &ov5648_subdev_pad_ops,
};

static int ov5648_suspend(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
	struct ov5648_sensor *sensor = ov5648_subdev_sensor(subdev);
	struct ov5648_state *state = &sensor->state;
	int ret = 0;

	mutex_lock(&sensor->mutex);

	if (state->streaming) {
		ret = ov5648_sw_standby(sensor, true);
		if (ret)
			goto complete;
	}

	ret = ov5648_sensor_power(sensor, false);
	if (ret)
		ov5648_sw_standby(sensor, false);

complete:
	mutex_unlock(&sensor->mutex);

	return ret;
}

static int ov5648_resume(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
	struct ov5648_sensor *sensor = ov5648_subdev_sensor(subdev);
	struct ov5648_state *state = &sensor->state;
	int ret = 0;

	mutex_lock(&sensor->mutex);

	ret = ov5648_sensor_power(sensor, true);
	if (ret)
		goto complete;

	ret = ov5648_sensor_init(sensor);
	if (ret)
		goto error_power;

	ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
	if (ret)
		goto error_power;

	if (state->streaming) {
		ret = ov5648_sw_standby(sensor, false);
		if (ret)
			goto error_power;
	}

	goto complete;

error_power:
	ov5648_sensor_power(sensor, false);

complete:
	mutex_unlock(&sensor->mutex);

	return ret;
}

static int ov5648_probe(struct i2c_client *client)
{
	struct device *dev = &client->dev;
	struct fwnode_handle *handle;
	struct ov5648_sensor *sensor;
	struct v4l2_subdev *subdev;
	struct media_pad *pad;
	unsigned long rate;
	int ret;

	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
	if (!sensor)
		return -ENOMEM;

	sensor->dev = dev;
	sensor->i2c_client = client;

	/* Graph Endpoint */

	handle = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
	if (!handle) {
		dev_err(dev, "unable to find endpoint node\n");
		return -EINVAL;
	}

	sensor->endpoint.bus_type = V4L2_MBUS_CSI2_DPHY;

	ret = v4l2_fwnode_endpoint_alloc_parse(handle, &sensor->endpoint);
	fwnode_handle_put(handle);
	if (ret) {
		dev_err(dev, "failed to parse endpoint node\n");
		return ret;
	}

	/* GPIOs */

	sensor->powerdown = devm_gpiod_get_optional(dev, "powerdown",
						    GPIOD_OUT_HIGH);
	if (IS_ERR(sensor->powerdown)) {
		ret = PTR_ERR(sensor->powerdown);
		goto error_endpoint;
	}

	sensor->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
	if (IS_ERR(sensor->reset)) {
		ret = PTR_ERR(sensor->reset);
		goto error_endpoint;
	}

	/* Regulators */

	/* DVDD: digital core */
	sensor->dvdd = devm_regulator_get(dev, "dvdd");
	if (IS_ERR(sensor->dvdd)) {
		dev_err(dev, "cannot get DVDD (digital core) regulator\n");
		ret = PTR_ERR(sensor->dvdd);
		goto error_endpoint;
	}

	/* DOVDD: digital I/O */
	sensor->dovdd = devm_regulator_get(dev, "dovdd");
	if (IS_ERR(sensor->dovdd)) {
		dev_err(dev, "cannot get DOVDD (digital I/O) regulator\n");
		ret = PTR_ERR(sensor->dovdd);
		goto error_endpoint;
	}

	/* AVDD: analog */
	sensor->avdd = devm_regulator_get_optional(dev, "avdd");
	if (IS_ERR(sensor->avdd)) {
		dev_info(dev, "no AVDD regulator provided, using internal\n");
		sensor->avdd = NULL;
	}

	/* External Clock */

	sensor->xvclk = devm_clk_get(dev, NULL);
	if (IS_ERR(sensor->xvclk)) {
		dev_err(dev, "failed to get external clock\n");
		ret = PTR_ERR(sensor->xvclk);
		goto error_endpoint;
	}

	rate = clk_get_rate(sensor->xvclk);
	if (rate != OV5648_XVCLK_RATE) {
		dev_err(dev, "clock rate %lu Hz is unsupported\n", rate);
		ret = -EINVAL;
		goto error_endpoint;
	}

	/* Subdev, entity and pad */

	subdev = &sensor->subdev;
	v4l2_i2c_subdev_init(subdev, client, &ov5648_subdev_ops);

	subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
	subdev->entity.function = MEDIA_ENT_F_CAM_SENSOR;

	pad = &sensor->pad;
	pad->flags = MEDIA_PAD_FL_SOURCE;

	ret = media_entity_pads_init(&subdev->entity, 1, pad);
	if (ret)
		goto error_entity;

	/* Mutex */

	mutex_init(&sensor->mutex);

	/* Sensor */

	ret = ov5648_ctrls_init(sensor);
	if (ret)
		goto error_mutex;

	ret = ov5648_state_init(sensor);
	if (ret)
		goto error_ctrls;

	/* Runtime PM */

	pm_runtime_enable(sensor->dev);
	pm_runtime_set_suspended(sensor->dev);

	/* V4L2 subdev register */

	ret = v4l2_async_register_subdev_sensor(subdev);
	if (ret)
		goto error_pm;

	return 0;

error_pm:
	pm_runtime_disable(sensor->dev);

error_ctrls:
	v4l2_ctrl_handler_free(&sensor->ctrls.handler);

error_mutex:
	mutex_destroy(&sensor->mutex);

error_entity:
	media_entity_cleanup(&sensor->subdev.entity);

error_endpoint:
	v4l2_fwnode_endpoint_free(&sensor->endpoint);

	return ret;
}

static void ov5648_remove(struct i2c_client *client)
{
	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
	struct ov5648_sensor *sensor = ov5648_subdev_sensor(subdev);

	v4l2_async_unregister_subdev(subdev);
	pm_runtime_disable(sensor->dev);
	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
	mutex_destroy(&sensor->mutex);
	media_entity_cleanup(&subdev->entity);
}

static const struct dev_pm_ops ov5648_pm_ops = {
	SET_RUNTIME_PM_OPS(ov5648_suspend, ov5648_resume, NULL)
};

static const struct of_device_id ov5648_of_match[] = {
	{ .compatible = "ovti,ov5648" },
	{ }
};
MODULE_DEVICE_TABLE(of, ov5648_of_match);

static struct i2c_driver ov5648_driver = {
	.driver = {
		.name = "ov5648",
		.of_match_table = ov5648_of_match,
		.pm = &ov5648_pm_ops,
	},
	.probe_new = ov5648_probe,
	.remove	 = ov5648_remove,
};

module_i2c_driver(ov5648_driver);

MODULE_AUTHOR("Paul Kocialkowski <paul.kocialkowski@bootlin.com>");
MODULE_DESCRIPTION("V4L2 driver for the OmniVision OV5648 image sensor");
MODULE_LICENSE("GPL v2");