summaryrefslogtreecommitdiffstats
path: root/drivers/media/i2c/ov5693.c
blob: e3c3bed69ad66f415caf2a73384da7a70d70fd19 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
 *
 * Adapted from the atomisp-ov5693 driver, with contributions from:
 *
 * Daniel Scally
 * Jean-Michel Hautbois
 * Fabian Wuthrich
 * Tsuchiya Yuto
 * Jordan Hand
 * Jake Day
 */

#include <asm/unaligned.h>
#include <linux/acpi.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>

#define OV5693_REG_8BIT(n)			((1 << 16) | (n))
#define OV5693_REG_16BIT(n)			((2 << 16) | (n))
#define OV5693_REG_24BIT(n)			((3 << 16) | (n))
#define OV5693_REG_SIZE_SHIFT			16
#define OV5693_REG_ADDR_MASK			0xffff

/* System Control */
#define OV5693_SW_RESET_REG			OV5693_REG_8BIT(0x0103)
#define OV5693_SW_STREAM_REG			OV5693_REG_8BIT(0x0100)
#define OV5693_START_STREAMING			0x01
#define OV5693_STOP_STREAMING			0x00
#define OV5693_SW_RESET				0x01

#define OV5693_REG_CHIP_ID			OV5693_REG_16BIT(0x300a)
/* Yes, this is right. The datasheet for the OV5693 gives its ID as 0x5690 */
#define OV5693_CHIP_ID				0x5690

/* Exposure */
#define OV5693_EXPOSURE_CTRL_REG		OV5693_REG_24BIT(0x3500)
#define OV5693_EXPOSURE_CTRL_MASK		GENMASK(19, 4)
#define OV5693_INTEGRATION_TIME_MARGIN		8
#define OV5693_EXPOSURE_MIN			1
#define OV5693_EXPOSURE_STEP			1

/* Analogue Gain */
#define OV5693_GAIN_CTRL_REG			OV5693_REG_16BIT(0x350a)
#define OV5693_GAIN_CTRL_MASK			GENMASK(10, 4)
#define OV5693_GAIN_MIN				1
#define OV5693_GAIN_MAX				127
#define OV5693_GAIN_DEF				8
#define OV5693_GAIN_STEP			1

/* Digital Gain */
#define OV5693_MWB_RED_GAIN_REG			OV5693_REG_16BIT(0x3400)
#define OV5693_MWB_GREEN_GAIN_REG		OV5693_REG_16BIT(0x3402)
#define OV5693_MWB_BLUE_GAIN_REG		OV5693_REG_16BIT(0x3404)
#define OV5693_MWB_GAIN_MASK			GENMASK(11, 0)
#define OV5693_MWB_GAIN_MAX			0x0fff
#define OV5693_DIGITAL_GAIN_MIN			1
#define OV5693_DIGITAL_GAIN_MAX			4095
#define OV5693_DIGITAL_GAIN_DEF			1024
#define OV5693_DIGITAL_GAIN_STEP		1

/* Timing and Format */
#define OV5693_CROP_START_X_REG			OV5693_REG_16BIT(0x3800)
#define OV5693_CROP_START_Y_REG			OV5693_REG_16BIT(0x3802)
#define OV5693_CROP_END_X_REG			OV5693_REG_16BIT(0x3804)
#define OV5693_CROP_END_Y_REG			OV5693_REG_16BIT(0x3806)
#define OV5693_OUTPUT_SIZE_X_REG		OV5693_REG_16BIT(0x3808)
#define OV5693_OUTPUT_SIZE_Y_REG		OV5693_REG_16BIT(0x380a)

#define OV5693_TIMING_HTS_REG			OV5693_REG_16BIT(0x380c)
#define OV5693_FIXED_PPL			2688U
#define OV5693_TIMING_VTS_REG			OV5693_REG_16BIT(0x380e)
#define OV5693_TIMING_MAX_VTS			0xffff
#define OV5693_TIMING_MIN_VTS			0x04

#define OV5693_OFFSET_START_X_REG		OV5693_REG_16BIT(0x3810)
#define OV5693_OFFSET_START_Y_REG		OV5693_REG_16BIT(0x3812)

#define OV5693_SUB_INC_X_REG			OV5693_REG_8BIT(0x3814)
#define OV5693_SUB_INC_Y_REG			OV5693_REG_8BIT(0x3815)

#define OV5693_FORMAT1_REG			OV5693_REG_8BIT(0x3820)
#define OV5693_FORMAT1_FLIP_VERT_ISP_EN		BIT(6)
#define OV5693_FORMAT1_FLIP_VERT_SENSOR_EN	BIT(1)
#define OV5693_FORMAT1_VBIN_EN			BIT(0)
#define OV5693_FORMAT2_REG			OV5693_REG_8BIT(0x3821)
#define OV5693_FORMAT2_HDR_EN			BIT(7)
#define OV5693_FORMAT2_FLIP_HORZ_ISP_EN		BIT(2)
#define OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN	BIT(1)
#define OV5693_FORMAT2_HBIN_EN			BIT(0)

#define OV5693_ISP_CTRL2_REG			OV5693_REG_8BIT(0x5002)
#define OV5693_ISP_SCALE_ENABLE			BIT(7)

/* Pixel Array */
#define OV5693_NATIVE_WIDTH			2624
#define OV5693_NATIVE_HEIGHT			1956
#define OV5693_NATIVE_START_LEFT		0
#define OV5693_NATIVE_START_TOP			0
#define OV5693_ACTIVE_WIDTH			2592
#define OV5693_ACTIVE_HEIGHT			1944
#define OV5693_ACTIVE_START_LEFT		16
#define OV5693_ACTIVE_START_TOP			6
#define OV5693_MIN_CROP_WIDTH			2
#define OV5693_MIN_CROP_HEIGHT			2

/* Test Pattern */
#define OV5693_TEST_PATTERN_REG			OV5693_REG_8BIT(0x5e00)
#define OV5693_TEST_PATTERN_ENABLE		BIT(7)
#define OV5693_TEST_PATTERN_ROLLING		BIT(6)
#define OV5693_TEST_PATTERN_RANDOM		0x01
#define OV5693_TEST_PATTERN_BARS		0x00

/* System Frequencies */
#define OV5693_XVCLK_FREQ			19200000
#define OV5693_LINK_FREQ_419_2MHZ		419200000
#define OV5693_PIXEL_RATE			167680000

#define to_ov5693_sensor(x) container_of(x, struct ov5693_device, sd)

static const char * const ov5693_supply_names[] = {
	"avdd",		/* Analog power */
	"dovdd",	/* Digital I/O power */
	"dvdd",		/* Digital circuit power */
};

#define OV5693_NUM_SUPPLIES	ARRAY_SIZE(ov5693_supply_names)

struct ov5693_reg {
	u32 reg;
	u8 val;
};

struct ov5693_reg_list {
	u32 num_regs;
	const struct ov5693_reg *regs;
};

struct ov5693_device {
	struct i2c_client *client;
	struct device *dev;

	/* Protect against concurrent changes to controls */
	struct mutex lock;

	struct gpio_desc *reset;
	struct gpio_desc *powerdown;
	struct gpio_desc *privacy_led;
	struct regulator_bulk_data supplies[OV5693_NUM_SUPPLIES];
	struct clk *xvclk;

	struct ov5693_mode {
		struct v4l2_rect crop;
		struct v4l2_mbus_framefmt format;
		bool binning_x;
		bool binning_y;
		unsigned int inc_x_odd;
		unsigned int inc_y_odd;
		unsigned int vts;
	} mode;
	bool streaming;

	struct v4l2_subdev sd;
	struct media_pad pad;

	struct ov5693_v4l2_ctrls {
		struct v4l2_ctrl_handler handler;
		struct v4l2_ctrl *link_freq;
		struct v4l2_ctrl *pixel_rate;
		struct v4l2_ctrl *exposure;
		struct v4l2_ctrl *analogue_gain;
		struct v4l2_ctrl *digital_gain;
		struct v4l2_ctrl *hflip;
		struct v4l2_ctrl *vflip;
		struct v4l2_ctrl *hblank;
		struct v4l2_ctrl *vblank;
		struct v4l2_ctrl *test_pattern;
	} ctrls;
};

static const struct ov5693_reg ov5693_global_regs[] = {
	{OV5693_REG_8BIT(0x3016), 0xf0},
	{OV5693_REG_8BIT(0x3017), 0xf0},
	{OV5693_REG_8BIT(0x3018), 0xf0},
	{OV5693_REG_8BIT(0x3022), 0x01},
	{OV5693_REG_8BIT(0x3028), 0x44},
	{OV5693_REG_8BIT(0x3098), 0x02},
	{OV5693_REG_8BIT(0x3099), 0x19},
	{OV5693_REG_8BIT(0x309a), 0x02},
	{OV5693_REG_8BIT(0x309b), 0x01},
	{OV5693_REG_8BIT(0x309c), 0x00},
	{OV5693_REG_8BIT(0x30a0), 0xd2},
	{OV5693_REG_8BIT(0x30a2), 0x01},
	{OV5693_REG_8BIT(0x30b2), 0x00},
	{OV5693_REG_8BIT(0x30b3), 0x83},
	{OV5693_REG_8BIT(0x30b4), 0x03},
	{OV5693_REG_8BIT(0x30b5), 0x04},
	{OV5693_REG_8BIT(0x30b6), 0x01},
	{OV5693_REG_8BIT(0x3080), 0x01},
	{OV5693_REG_8BIT(0x3104), 0x21},
	{OV5693_REG_8BIT(0x3106), 0x00},
	{OV5693_REG_8BIT(0x3406), 0x01},
	{OV5693_REG_8BIT(0x3503), 0x07},
	{OV5693_REG_8BIT(0x350b), 0x40},
	{OV5693_REG_8BIT(0x3601), 0x0a},
	{OV5693_REG_8BIT(0x3602), 0x38},
	{OV5693_REG_8BIT(0x3612), 0x80},
	{OV5693_REG_8BIT(0x3620), 0x54},
	{OV5693_REG_8BIT(0x3621), 0xc7},
	{OV5693_REG_8BIT(0x3622), 0x0f},
	{OV5693_REG_8BIT(0x3625), 0x10},
	{OV5693_REG_8BIT(0x3630), 0x55},
	{OV5693_REG_8BIT(0x3631), 0xf4},
	{OV5693_REG_8BIT(0x3632), 0x00},
	{OV5693_REG_8BIT(0x3633), 0x34},
	{OV5693_REG_8BIT(0x3634), 0x02},
	{OV5693_REG_8BIT(0x364d), 0x0d},
	{OV5693_REG_8BIT(0x364f), 0xdd},
	{OV5693_REG_8BIT(0x3660), 0x04},
	{OV5693_REG_8BIT(0x3662), 0x10},
	{OV5693_REG_8BIT(0x3663), 0xf1},
	{OV5693_REG_8BIT(0x3665), 0x00},
	{OV5693_REG_8BIT(0x3666), 0x20},
	{OV5693_REG_8BIT(0x3667), 0x00},
	{OV5693_REG_8BIT(0x366a), 0x80},
	{OV5693_REG_8BIT(0x3680), 0xe0},
	{OV5693_REG_8BIT(0x3681), 0x00},
	{OV5693_REG_8BIT(0x3700), 0x42},
	{OV5693_REG_8BIT(0x3701), 0x14},
	{OV5693_REG_8BIT(0x3702), 0xa0},
	{OV5693_REG_8BIT(0x3703), 0xd8},
	{OV5693_REG_8BIT(0x3704), 0x78},
	{OV5693_REG_8BIT(0x3705), 0x02},
	{OV5693_REG_8BIT(0x370a), 0x00},
	{OV5693_REG_8BIT(0x370b), 0x20},
	{OV5693_REG_8BIT(0x370c), 0x0c},
	{OV5693_REG_8BIT(0x370d), 0x11},
	{OV5693_REG_8BIT(0x370e), 0x00},
	{OV5693_REG_8BIT(0x370f), 0x40},
	{OV5693_REG_8BIT(0x3710), 0x00},
	{OV5693_REG_8BIT(0x371a), 0x1c},
	{OV5693_REG_8BIT(0x371b), 0x05},
	{OV5693_REG_8BIT(0x371c), 0x01},
	{OV5693_REG_8BIT(0x371e), 0xa1},
	{OV5693_REG_8BIT(0x371f), 0x0c},
	{OV5693_REG_8BIT(0x3721), 0x00},
	{OV5693_REG_8BIT(0x3724), 0x10},
	{OV5693_REG_8BIT(0x3726), 0x00},
	{OV5693_REG_8BIT(0x372a), 0x01},
	{OV5693_REG_8BIT(0x3730), 0x10},
	{OV5693_REG_8BIT(0x3738), 0x22},
	{OV5693_REG_8BIT(0x3739), 0xe5},
	{OV5693_REG_8BIT(0x373a), 0x50},
	{OV5693_REG_8BIT(0x373b), 0x02},
	{OV5693_REG_8BIT(0x373c), 0x41},
	{OV5693_REG_8BIT(0x373f), 0x02},
	{OV5693_REG_8BIT(0x3740), 0x42},
	{OV5693_REG_8BIT(0x3741), 0x02},
	{OV5693_REG_8BIT(0x3742), 0x18},
	{OV5693_REG_8BIT(0x3743), 0x01},
	{OV5693_REG_8BIT(0x3744), 0x02},
	{OV5693_REG_8BIT(0x3747), 0x10},
	{OV5693_REG_8BIT(0x374c), 0x04},
	{OV5693_REG_8BIT(0x3751), 0xf0},
	{OV5693_REG_8BIT(0x3752), 0x00},
	{OV5693_REG_8BIT(0x3753), 0x00},
	{OV5693_REG_8BIT(0x3754), 0xc0},
	{OV5693_REG_8BIT(0x3755), 0x00},
	{OV5693_REG_8BIT(0x3756), 0x1a},
	{OV5693_REG_8BIT(0x3758), 0x00},
	{OV5693_REG_8BIT(0x3759), 0x0f},
	{OV5693_REG_8BIT(0x376b), 0x44},
	{OV5693_REG_8BIT(0x375c), 0x04},
	{OV5693_REG_8BIT(0x3774), 0x10},
	{OV5693_REG_8BIT(0x3776), 0x00},
	{OV5693_REG_8BIT(0x377f), 0x08},
	{OV5693_REG_8BIT(0x3780), 0x22},
	{OV5693_REG_8BIT(0x3781), 0x0c},
	{OV5693_REG_8BIT(0x3784), 0x2c},
	{OV5693_REG_8BIT(0x3785), 0x1e},
	{OV5693_REG_8BIT(0x378f), 0xf5},
	{OV5693_REG_8BIT(0x3791), 0xb0},
	{OV5693_REG_8BIT(0x3795), 0x00},
	{OV5693_REG_8BIT(0x3796), 0x64},
	{OV5693_REG_8BIT(0x3797), 0x11},
	{OV5693_REG_8BIT(0x3798), 0x30},
	{OV5693_REG_8BIT(0x3799), 0x41},
	{OV5693_REG_8BIT(0x379a), 0x07},
	{OV5693_REG_8BIT(0x379b), 0xb0},
	{OV5693_REG_8BIT(0x379c), 0x0c},
	{OV5693_REG_8BIT(0x3a04), 0x06},
	{OV5693_REG_8BIT(0x3a05), 0x14},
	{OV5693_REG_8BIT(0x3e07), 0x20},
	{OV5693_REG_8BIT(0x4000), 0x08},
	{OV5693_REG_8BIT(0x4001), 0x04},
	{OV5693_REG_8BIT(0x4004), 0x08},
	{OV5693_REG_8BIT(0x4006), 0x20},
	{OV5693_REG_8BIT(0x4008), 0x24},
	{OV5693_REG_8BIT(0x4009), 0x10},
	{OV5693_REG_8BIT(0x4058), 0x00},
	{OV5693_REG_8BIT(0x4101), 0xb2},
	{OV5693_REG_8BIT(0x4307), 0x31},
	{OV5693_REG_8BIT(0x4511), 0x05},
	{OV5693_REG_8BIT(0x4512), 0x01},
	{OV5693_REG_8BIT(0x481f), 0x30},
	{OV5693_REG_8BIT(0x4826), 0x2c},
	{OV5693_REG_8BIT(0x4d02), 0xfd},
	{OV5693_REG_8BIT(0x4d03), 0xf5},
	{OV5693_REG_8BIT(0x4d04), 0x0c},
	{OV5693_REG_8BIT(0x4d05), 0xcc},
	{OV5693_REG_8BIT(0x4837), 0x0a},
	{OV5693_REG_8BIT(0x5003), 0x20},
	{OV5693_REG_8BIT(0x5013), 0x00},
	{OV5693_REG_8BIT(0x5842), 0x01},
	{OV5693_REG_8BIT(0x5843), 0x2b},
	{OV5693_REG_8BIT(0x5844), 0x01},
	{OV5693_REG_8BIT(0x5845), 0x92},
	{OV5693_REG_8BIT(0x5846), 0x01},
	{OV5693_REG_8BIT(0x5847), 0x8f},
	{OV5693_REG_8BIT(0x5848), 0x01},
	{OV5693_REG_8BIT(0x5849), 0x0c},
	{OV5693_REG_8BIT(0x5e10), 0x0c},
	{OV5693_REG_8BIT(0x3820), 0x00},
	{OV5693_REG_8BIT(0x3821), 0x1e},
	{OV5693_REG_8BIT(0x5041), 0x14}
};

static const struct ov5693_reg_list ov5693_global_setting = {
	.num_regs = ARRAY_SIZE(ov5693_global_regs),
	.regs = ov5693_global_regs,
};

static const struct v4l2_rect ov5693_default_crop = {
	.left = OV5693_ACTIVE_START_LEFT,
	.top = OV5693_ACTIVE_START_TOP,
	.width = OV5693_ACTIVE_WIDTH,
	.height = OV5693_ACTIVE_HEIGHT,
};

static const struct v4l2_mbus_framefmt ov5693_default_fmt = {
	.width = OV5693_ACTIVE_WIDTH,
	.height = OV5693_ACTIVE_HEIGHT,
	.code = MEDIA_BUS_FMT_SBGGR10_1X10,
};

static const s64 link_freq_menu_items[] = {
	OV5693_LINK_FREQ_419_2MHZ
};

static const char * const ov5693_test_pattern_menu[] = {
	"Disabled",
	"Random Data",
	"Colour Bars",
	"Colour Bars with Rolling Bar"
};

static const u8 ov5693_test_pattern_bits[] = {
	0,
	OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_RANDOM,
	OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS,
	OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS |
	OV5693_TEST_PATTERN_ROLLING,
};

/* I2C I/O Operations */

static int ov5693_read_reg(struct ov5693_device *ov5693, u32 addr, u32 *value)
{
	struct i2c_client *client = ov5693->client;
	__be16 reg;
	u8 val[4];
	struct i2c_msg msg[] = {
		{
			.addr	= client->addr,
			.flags	= 0,
			.len	= 2,
			.buf	= (u8 *)&reg,
		},
		{
			.addr	= client->addr,
			.flags	= I2C_M_RD,
			.buf	= (u8 *)&val,
		},
	};
	unsigned int len = ((addr >> OV5693_REG_SIZE_SHIFT) & 3);
	unsigned int i;
	int ret;

	reg = cpu_to_be16(addr & OV5693_REG_ADDR_MASK);

	msg[1].len = len;

	ret = i2c_transfer(client->adapter, msg, 2);
	if (ret < 0)
		return dev_err_probe(&client->dev, ret,
				     "Failed to read register 0x%04x: %d\n",
				     addr & OV5693_REG_ADDR_MASK, ret);

	*value = 0;
	for (i = 0; i < len; ++i) {
		*value <<= 8;
		*value |= val[i];
	}

	return 0;
}

static void ov5693_write_reg(struct ov5693_device *ov5693, u32 addr, u32 value,
			     int *error)
{
	struct i2c_client *client = ov5693->client;
	struct {
		__be16 reg;
		u8 val[4];
	} __packed buf;
	struct i2c_msg msg = {
		.addr	= client->addr,
		.buf	= (u8 *)&buf,
	};
	unsigned int len = ((addr >> OV5693_REG_SIZE_SHIFT) & 3);
	unsigned int i;
	int ret;

	if (*error < 0)
		return;

	buf.reg = cpu_to_be16(addr & OV5693_REG_ADDR_MASK);
	for (i = 0; i < len; ++i) {
		buf.val[len - i - 1] = value & 0xff;
		value >>= 8;
	}

	msg.len	= len + 2;

	ret = i2c_transfer(client->adapter, &msg, 1);
	if (ret < 0) {
		dev_err(&client->dev, "Failed to write register 0x%04x: %d\n",
			addr & OV5693_REG_ADDR_MASK, ret);
		*error = ret;
	}
}

static int ov5693_write_reg_array(struct ov5693_device *ov5693,
				  const struct ov5693_reg_list *reglist)
{
	unsigned int i;
	int ret = 0;

	for (i = 0; i < reglist->num_regs; i++)
		ov5693_write_reg(ov5693, reglist->regs[i].reg,
				 reglist->regs[i].val, &ret);

	return ret;
}

static int ov5693_update_bits(struct ov5693_device *ov5693, u32 address,
			      u32 mask, u32 bits)
{
	u32 value = 0;
	int ret;

	ret = ov5693_read_reg(ov5693, address, &value);
	if (ret)
		return ret;

	value &= ~mask;
	value |= bits;

	ov5693_write_reg(ov5693, address, value, &ret);

	return ret;
}

/* V4L2 Controls Functions */

static int ov5693_flip_vert_configure(struct ov5693_device *ov5693,
				      bool enable)
{
	u8 bits = OV5693_FORMAT1_FLIP_VERT_ISP_EN |
		  OV5693_FORMAT1_FLIP_VERT_SENSOR_EN;
	int ret;

	ret = ov5693_update_bits(ov5693, OV5693_FORMAT1_REG, bits,
				 enable ? bits : 0);
	if (ret)
		return ret;

	return 0;
}

static int ov5693_flip_horz_configure(struct ov5693_device *ov5693,
				      bool enable)
{
	u8 bits = OV5693_FORMAT2_FLIP_HORZ_ISP_EN |
		  OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN;
	int ret;

	ret = ov5693_update_bits(ov5693, OV5693_FORMAT2_REG, bits,
				 enable ? bits : 0);
	if (ret)
		return ret;

	return 0;
}

static int ov5693_get_exposure(struct ov5693_device *ov5693, s32 *value)
{
	u32 exposure;
	int ret;

	ret = ov5693_read_reg(ov5693, OV5693_EXPOSURE_CTRL_REG, &exposure);
	if (ret)
		return ret;

	/* The lowest 4 bits are unsupported fractional bits */
	*value = exposure >> 4;

	return 0;
}

static int ov5693_exposure_configure(struct ov5693_device *ov5693,
				     u32 exposure)
{
	int ret = 0;

	exposure = (exposure << 4) & OV5693_EXPOSURE_CTRL_MASK;

	ov5693_write_reg(ov5693, OV5693_EXPOSURE_CTRL_REG, exposure, &ret);

	return ret;
}

static int ov5693_get_gain(struct ov5693_device *ov5693, u32 *gain)
{
	u32 value;
	int ret;

	ret = ov5693_read_reg(ov5693, OV5693_GAIN_CTRL_REG, &value);
	if (ret)
		return ret;

	/* As with exposure, the lowest 4 bits are fractional bits. */
	*gain = value >> 4;

	return ret;
}

static int ov5693_digital_gain_configure(struct ov5693_device *ov5693,
					 u32 gain)
{
	int ret = 0;

	gain &= OV5693_MWB_GAIN_MASK;

	ov5693_write_reg(ov5693, OV5693_MWB_RED_GAIN_REG, gain, &ret);
	ov5693_write_reg(ov5693, OV5693_MWB_GREEN_GAIN_REG, gain, &ret);
	ov5693_write_reg(ov5693, OV5693_MWB_BLUE_GAIN_REG, gain, &ret);

	return ret;
}

static int ov5693_analog_gain_configure(struct ov5693_device *ov5693, u32 gain)
{
	int ret = 0;

	gain = (gain << 4) & OV5693_GAIN_CTRL_MASK;

	ov5693_write_reg(ov5693, OV5693_GAIN_CTRL_REG, gain, &ret);

	return ret;
}

static int ov5693_vts_configure(struct ov5693_device *ov5693, u32 vblank)
{
	u16 vts = ov5693->mode.format.height + vblank;
	int ret = 0;

	ov5693_write_reg(ov5693, OV5693_TIMING_VTS_REG, vts, &ret);

	return ret;
}

static int ov5693_test_pattern_configure(struct ov5693_device *ov5693, u32 idx)
{
	int ret = 0;

	ov5693_write_reg(ov5693, OV5693_TEST_PATTERN_REG,
			 ov5693_test_pattern_bits[idx], &ret);

	return ret;
}

static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
{
	struct ov5693_device *ov5693 =
	    container_of(ctrl->handler, struct ov5693_device, ctrls.handler);
	int ret = 0;

	/* If VBLANK is altered we need to update exposure to compensate */
	if (ctrl->id == V4L2_CID_VBLANK) {
		int exposure_max;

		exposure_max = ov5693->mode.format.height + ctrl->val -
			       OV5693_INTEGRATION_TIME_MARGIN;
		__v4l2_ctrl_modify_range(ov5693->ctrls.exposure,
					 ov5693->ctrls.exposure->minimum,
					 exposure_max,
					 ov5693->ctrls.exposure->step,
					 min(ov5693->ctrls.exposure->val,
					     exposure_max));
	}

	/* Only apply changes to the controls if the device is powered up */
	if (!pm_runtime_get_if_in_use(ov5693->dev))
		return 0;

	switch (ctrl->id) {
	case V4L2_CID_EXPOSURE:
		ret = ov5693_exposure_configure(ov5693, ctrl->val);
		break;
	case V4L2_CID_ANALOGUE_GAIN:
		ret = ov5693_analog_gain_configure(ov5693, ctrl->val);
		break;
	case V4L2_CID_DIGITAL_GAIN:
		ret = ov5693_digital_gain_configure(ov5693, ctrl->val);
		break;
	case V4L2_CID_HFLIP:
		ret = ov5693_flip_horz_configure(ov5693, !!ctrl->val);
		break;
	case V4L2_CID_VFLIP:
		ret = ov5693_flip_vert_configure(ov5693, !!ctrl->val);
		break;
	case V4L2_CID_VBLANK:
		ret = ov5693_vts_configure(ov5693, ctrl->val);
		break;
	case V4L2_CID_TEST_PATTERN:
		ret = ov5693_test_pattern_configure(ov5693, ctrl->val);
		break;
	default:
		ret = -EINVAL;
	}

	pm_runtime_put(ov5693->dev);

	return ret;
}

static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
{
	struct ov5693_device *ov5693 = container_of(ctrl->handler,
						    struct ov5693_device,
						    ctrls.handler);

	switch (ctrl->id) {
	case V4L2_CID_EXPOSURE_ABSOLUTE:
		return ov5693_get_exposure(ov5693, &ctrl->val);
	case V4L2_CID_AUTOGAIN:
		return ov5693_get_gain(ov5693, &ctrl->val);
	default:
		return -EINVAL;
	}
}

static const struct v4l2_ctrl_ops ov5693_ctrl_ops = {
	.s_ctrl = ov5693_s_ctrl,
	.g_volatile_ctrl = ov5693_g_volatile_ctrl
};

/* System Control Functions */

static int ov5693_mode_configure(struct ov5693_device *ov5693)
{
	const struct ov5693_mode *mode = &ov5693->mode;
	int ret = 0;

	/* Crop Start X */
	ov5693_write_reg(ov5693, OV5693_CROP_START_X_REG, mode->crop.left,
			 &ret);

	/* Offset X */
	ov5693_write_reg(ov5693, OV5693_OFFSET_START_X_REG, 0, &ret);

	/* Output Size X */
	ov5693_write_reg(ov5693, OV5693_OUTPUT_SIZE_X_REG, mode->format.width,
			 &ret);

	/* Crop End X */
	ov5693_write_reg(ov5693, OV5693_CROP_END_X_REG,
			 mode->crop.left + mode->crop.width, &ret);

	/* Horizontal Total Size */
	ov5693_write_reg(ov5693, OV5693_TIMING_HTS_REG, OV5693_FIXED_PPL,
			 &ret);

	/* Crop Start Y */
	ov5693_write_reg(ov5693, OV5693_CROP_START_Y_REG, mode->crop.top,
			 &ret);

	/* Offset Y */
	ov5693_write_reg(ov5693, OV5693_OFFSET_START_Y_REG, 0, &ret);

	/* Output Size Y */
	ov5693_write_reg(ov5693, OV5693_OUTPUT_SIZE_Y_REG, mode->format.height,
			 &ret);

	/* Crop End Y */
	ov5693_write_reg(ov5693, OV5693_CROP_END_Y_REG,
			 mode->crop.top + mode->crop.height, &ret);

	/* Subsample X increase */
	ov5693_write_reg(ov5693, OV5693_SUB_INC_X_REG,
			 ((mode->inc_x_odd << 4) & 0xf0) | 0x01, &ret);
	/* Subsample Y increase */
	ov5693_write_reg(ov5693, OV5693_SUB_INC_Y_REG,
			 ((mode->inc_y_odd << 4) & 0xf0) | 0x01, &ret);

	if (ret)
		return ret;

	/* Binning */
	ret = ov5693_update_bits(ov5693, OV5693_FORMAT1_REG,
				 OV5693_FORMAT1_VBIN_EN,
				 mode->binning_y ? OV5693_FORMAT1_VBIN_EN : 0);
	if (ret)
		return ret;

	ret = ov5693_update_bits(ov5693, OV5693_FORMAT2_REG,
				 OV5693_FORMAT2_HBIN_EN,
				 mode->binning_x ? OV5693_FORMAT2_HBIN_EN : 0);

	return ret;
}

static int ov5693_enable_streaming(struct ov5693_device *ov5693, bool enable)
{
	int ret = 0;

	ov5693_write_reg(ov5693, OV5693_SW_STREAM_REG,
			 enable ? OV5693_START_STREAMING :
				  OV5693_STOP_STREAMING, &ret);

	return ret;
}

static int ov5693_sw_reset(struct ov5693_device *ov5693)
{
	int ret = 0;

	ov5693_write_reg(ov5693, OV5693_SW_RESET_REG, OV5693_SW_RESET, &ret);

	return ret;
}

static int ov5693_sensor_init(struct ov5693_device *ov5693)
{
	int ret;

	ret = ov5693_sw_reset(ov5693);
	if (ret)
		return dev_err_probe(ov5693->dev, ret,
				     "software reset error\n");

	ret = ov5693_write_reg_array(ov5693, &ov5693_global_setting);
	if (ret)
		return dev_err_probe(ov5693->dev, ret,
				     "global settings error\n");

	ret = ov5693_mode_configure(ov5693);
	if (ret)
		return dev_err_probe(ov5693->dev, ret,
				     "mode configure error\n");

	ret = ov5693_enable_streaming(ov5693, false);
	if (ret)
		dev_err(ov5693->dev, "stop streaming error\n");

	return ret;
}

static void ov5693_sensor_powerdown(struct ov5693_device *ov5693)
{
	gpiod_set_value_cansleep(ov5693->privacy_led, 0);
	gpiod_set_value_cansleep(ov5693->reset, 1);
	gpiod_set_value_cansleep(ov5693->powerdown, 1);

	regulator_bulk_disable(OV5693_NUM_SUPPLIES, ov5693->supplies);

	clk_disable_unprepare(ov5693->xvclk);
}

static int ov5693_sensor_powerup(struct ov5693_device *ov5693)
{
	int ret;

	gpiod_set_value_cansleep(ov5693->reset, 1);
	gpiod_set_value_cansleep(ov5693->powerdown, 1);

	ret = clk_prepare_enable(ov5693->xvclk);
	if (ret) {
		dev_err(ov5693->dev, "Failed to enable clk\n");
		goto fail_power;
	}

	ret = regulator_bulk_enable(OV5693_NUM_SUPPLIES, ov5693->supplies);
	if (ret) {
		dev_err(ov5693->dev, "Failed to enable regulators\n");
		goto fail_power;
	}

	gpiod_set_value_cansleep(ov5693->powerdown, 0);
	gpiod_set_value_cansleep(ov5693->reset, 0);
	gpiod_set_value_cansleep(ov5693->privacy_led, 1);

	usleep_range(5000, 7500);

	return 0;

fail_power:
	ov5693_sensor_powerdown(ov5693);
	return ret;
}

static int __maybe_unused ov5693_sensor_suspend(struct device *dev)
{
	struct v4l2_subdev *sd = dev_get_drvdata(dev);
	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);

	ov5693_sensor_powerdown(ov5693);

	return 0;
}

static int __maybe_unused ov5693_sensor_resume(struct device *dev)
{
	struct v4l2_subdev *sd = dev_get_drvdata(dev);
	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
	int ret;

	mutex_lock(&ov5693->lock);

	ret = ov5693_sensor_powerup(ov5693);
	if (ret)
		goto out_unlock;

	ret = ov5693_sensor_init(ov5693);
	if (ret) {
		dev_err(dev, "ov5693 sensor init failure\n");
		goto err_power;
	}

	goto out_unlock;

err_power:
	ov5693_sensor_powerdown(ov5693);
out_unlock:
	mutex_unlock(&ov5693->lock);
	return ret;
}

static int ov5693_detect(struct ov5693_device *ov5693)
{
	int ret;
	u32 id;

	ret = ov5693_read_reg(ov5693, OV5693_REG_CHIP_ID, &id);
	if (ret)
		return ret;

	if (id != OV5693_CHIP_ID)
		return dev_err_probe(ov5693->dev, -ENODEV,
				     "sensor ID mismatch. Found 0x%04x\n", id);

	return 0;
}

/* V4L2 Framework callbacks */

static unsigned int __ov5693_calc_vts(u32 height)
{
	/*
	 * We need to set a sensible default VTS for whatever format height we
	 * happen to be given from set_fmt(). This function just targets
	 * an even multiple of 30fps.
	 */

	unsigned int tgt_fps;

	tgt_fps = rounddown(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / height, 30);

	return ALIGN_DOWN(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / tgt_fps, 2);
}

static struct v4l2_mbus_framefmt *
__ov5693_get_pad_format(struct ov5693_device *ov5693,
			struct v4l2_subdev_state *state,
			unsigned int pad, enum v4l2_subdev_format_whence which)
{
	switch (which) {
	case V4L2_SUBDEV_FORMAT_TRY:
		return v4l2_subdev_get_try_format(&ov5693->sd, state, pad);
	case V4L2_SUBDEV_FORMAT_ACTIVE:
		return &ov5693->mode.format;
	default:
		return NULL;
	}
}

static struct v4l2_rect *
__ov5693_get_pad_crop(struct ov5693_device *ov5693,
		      struct v4l2_subdev_state *state,
		      unsigned int pad, enum v4l2_subdev_format_whence which)
{
	switch (which) {
	case V4L2_SUBDEV_FORMAT_TRY:
		return v4l2_subdev_get_try_crop(&ov5693->sd, state, pad);
	case V4L2_SUBDEV_FORMAT_ACTIVE:
		return &ov5693->mode.crop;
	}

	return NULL;
}

static int ov5693_get_fmt(struct v4l2_subdev *sd,
			  struct v4l2_subdev_state *state,
			  struct v4l2_subdev_format *format)
{
	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);

	format->format = ov5693->mode.format;

	return 0;
}

static int ov5693_set_fmt(struct v4l2_subdev *sd,
			  struct v4l2_subdev_state *state,
			  struct v4l2_subdev_format *format)
{
	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
	const struct v4l2_rect *crop;
	struct v4l2_mbus_framefmt *fmt;
	unsigned int hratio, vratio;
	unsigned int width, height;
	unsigned int hblank;
	int exposure_max;

	crop = __ov5693_get_pad_crop(ov5693, state, format->pad, format->which);

	/*
	 * Align to two to simplify the binning calculations below, and clamp
	 * the requested format at the crop rectangle
	 */
	width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
			OV5693_MIN_CROP_WIDTH, crop->width);
	height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
			 OV5693_MIN_CROP_HEIGHT, crop->height);

	/*
	 * We can only support setting either the dimensions of the crop rect
	 * or those dimensions binned (separately) by a factor of two.
	 */
	hratio = clamp_t(unsigned int,
			 DIV_ROUND_CLOSEST(crop->width, width), 1, 2);
	vratio = clamp_t(unsigned int,
			 DIV_ROUND_CLOSEST(crop->height, height), 1, 2);

	fmt = __ov5693_get_pad_format(ov5693, state, format->pad,
				      format->which);

	fmt->width = crop->width / hratio;
	fmt->height = crop->height / vratio;
	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;

	format->format = *fmt;

	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
		return 0;

	mutex_lock(&ov5693->lock);

	ov5693->mode.binning_x = hratio > 1;
	ov5693->mode.inc_x_odd = hratio > 1 ? 3 : 1;
	ov5693->mode.binning_y = vratio > 1;
	ov5693->mode.inc_y_odd = vratio > 1 ? 3 : 1;

	ov5693->mode.vts = __ov5693_calc_vts(fmt->height);

	__v4l2_ctrl_modify_range(ov5693->ctrls.vblank,
				 OV5693_TIMING_MIN_VTS,
				 OV5693_TIMING_MAX_VTS - fmt->height,
				 1, ov5693->mode.vts - fmt->height);
	__v4l2_ctrl_s_ctrl(ov5693->ctrls.vblank,
			   ov5693->mode.vts - fmt->height);

	hblank = OV5693_FIXED_PPL - fmt->width;
	__v4l2_ctrl_modify_range(ov5693->ctrls.hblank, hblank, hblank, 1,
				 hblank);

	exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN;
	__v4l2_ctrl_modify_range(ov5693->ctrls.exposure,
				 ov5693->ctrls.exposure->minimum, exposure_max,
				 ov5693->ctrls.exposure->step,
				 min(ov5693->ctrls.exposure->val,
				     exposure_max));

	mutex_unlock(&ov5693->lock);
	return 0;
}

static int ov5693_get_selection(struct v4l2_subdev *sd,
				struct v4l2_subdev_state *state,
				struct v4l2_subdev_selection *sel)
{
	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);

	switch (sel->target) {
	case V4L2_SEL_TGT_CROP:
		mutex_lock(&ov5693->lock);
		sel->r = *__ov5693_get_pad_crop(ov5693, state, sel->pad,
						sel->which);
		mutex_unlock(&ov5693->lock);
		break;
	case V4L2_SEL_TGT_NATIVE_SIZE:
		sel->r.top = 0;
		sel->r.left = 0;
		sel->r.width = OV5693_NATIVE_WIDTH;
		sel->r.height = OV5693_NATIVE_HEIGHT;
		break;
	case V4L2_SEL_TGT_CROP_BOUNDS:
	case V4L2_SEL_TGT_CROP_DEFAULT:
		sel->r.top = OV5693_ACTIVE_START_TOP;
		sel->r.left = OV5693_ACTIVE_START_LEFT;
		sel->r.width = OV5693_ACTIVE_WIDTH;
		sel->r.height = OV5693_ACTIVE_HEIGHT;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int ov5693_set_selection(struct v4l2_subdev *sd,
				struct v4l2_subdev_state *state,
				struct v4l2_subdev_selection *sel)
{
	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
	struct v4l2_mbus_framefmt *format;
	struct v4l2_rect *__crop;
	struct v4l2_rect rect;

	if (sel->target != V4L2_SEL_TGT_CROP)
		return -EINVAL;

	/*
	 * Clamp the boundaries of the crop rectangle to the size of the sensor
	 * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
	 * disrupted.
	 */
	rect.left = clamp(ALIGN(sel->r.left, 2), OV5693_NATIVE_START_LEFT,
			  OV5693_NATIVE_WIDTH);
	rect.top = clamp(ALIGN(sel->r.top, 2), OV5693_NATIVE_START_TOP,
			 OV5693_NATIVE_HEIGHT);
	rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
			     OV5693_MIN_CROP_WIDTH, OV5693_NATIVE_WIDTH);
	rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
			      OV5693_MIN_CROP_HEIGHT, OV5693_NATIVE_HEIGHT);

	/* Make sure the crop rectangle isn't outside the bounds of the array */
	rect.width = min_t(unsigned int, rect.width,
			   OV5693_NATIVE_WIDTH - rect.left);
	rect.height = min_t(unsigned int, rect.height,
			    OV5693_NATIVE_HEIGHT - rect.top);

	__crop = __ov5693_get_pad_crop(ov5693, state, sel->pad, sel->which);

	if (rect.width != __crop->width || rect.height != __crop->height) {
		/*
		 * Reset the output image size if the crop rectangle size has
		 * been modified.
		 */
		format = __ov5693_get_pad_format(ov5693, state, sel->pad,
						 sel->which);
		format->width = rect.width;
		format->height = rect.height;
	}

	*__crop = rect;
	sel->r = rect;

	return 0;
}

static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
{
	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
	int ret;

	if (enable) {
		ret = pm_runtime_get_sync(ov5693->dev);
		if (ret < 0)
			goto err_power_down;

		mutex_lock(&ov5693->lock);
		ret = __v4l2_ctrl_handler_setup(&ov5693->ctrls.handler);
		if (ret) {
			mutex_unlock(&ov5693->lock);
			goto err_power_down;
		}

		ret = ov5693_enable_streaming(ov5693, true);
		mutex_unlock(&ov5693->lock);
	} else {
		mutex_lock(&ov5693->lock);
		ret = ov5693_enable_streaming(ov5693, false);
		mutex_unlock(&ov5693->lock);
	}
	if (ret)
		goto err_power_down;

	ov5693->streaming = !!enable;

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

	return 0;
err_power_down:
	pm_runtime_put_noidle(ov5693->dev);
	return ret;
}

static int ov5693_g_frame_interval(struct v4l2_subdev *sd,
				   struct v4l2_subdev_frame_interval *interval)
{
	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
	unsigned int framesize = OV5693_FIXED_PPL * (ov5693->mode.format.height +
				 ov5693->ctrls.vblank->val);
	unsigned int fps = DIV_ROUND_CLOSEST(OV5693_PIXEL_RATE, framesize);

	interval->interval.numerator = 1;
	interval->interval.denominator = fps;

	return 0;
}

static int ov5693_enum_mbus_code(struct v4l2_subdev *sd,
				 struct v4l2_subdev_state *state,
				 struct v4l2_subdev_mbus_code_enum *code)
{
	/* Only a single mbus format is supported */
	if (code->index > 0)
		return -EINVAL;

	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
	return 0;
}

static int ov5693_enum_frame_size(struct v4l2_subdev *sd,
				  struct v4l2_subdev_state *state,
				  struct v4l2_subdev_frame_size_enum *fse)
{
	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
	struct v4l2_rect *__crop;

	if (fse->index > 1 || fse->code != MEDIA_BUS_FMT_SBGGR10_1X10)
		return -EINVAL;

	__crop = __ov5693_get_pad_crop(ov5693, state, fse->pad, fse->which);
	if (!__crop)
		return -EINVAL;

	fse->min_width = __crop->width / (fse->index + 1);
	fse->min_height = __crop->height / (fse->index + 1);
	fse->max_width = fse->min_width;
	fse->max_height = fse->min_height;

	return 0;
}

static const struct v4l2_subdev_video_ops ov5693_video_ops = {
	.s_stream = ov5693_s_stream,
	.g_frame_interval = ov5693_g_frame_interval,
};

static const struct v4l2_subdev_pad_ops ov5693_pad_ops = {
	.enum_mbus_code = ov5693_enum_mbus_code,
	.enum_frame_size = ov5693_enum_frame_size,
	.get_fmt = ov5693_get_fmt,
	.set_fmt = ov5693_set_fmt,
	.get_selection = ov5693_get_selection,
	.set_selection = ov5693_set_selection,
};

static const struct v4l2_subdev_ops ov5693_ops = {
	.video = &ov5693_video_ops,
	.pad = &ov5693_pad_ops,
};

/* Sensor and Driver Configuration Functions */

static int ov5693_init_controls(struct ov5693_device *ov5693)
{
	const struct v4l2_ctrl_ops *ops = &ov5693_ctrl_ops;
	struct ov5693_v4l2_ctrls *ctrls = &ov5693->ctrls;
	struct v4l2_fwnode_device_properties props;
	int vblank_max, vblank_def;
	int exposure_max;
	int hblank;
	int ret;

	ret = v4l2_ctrl_handler_init(&ctrls->handler, 12);
	if (ret)
		return ret;

	/* link freq */
	ctrls->link_freq = v4l2_ctrl_new_int_menu(&ctrls->handler,
						  NULL, V4L2_CID_LINK_FREQ,
						  0, 0, link_freq_menu_items);
	if (ctrls->link_freq)
		ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;

	/* pixel rate */
	ctrls->pixel_rate = v4l2_ctrl_new_std(&ctrls->handler, NULL,
					      V4L2_CID_PIXEL_RATE, 0,
					      OV5693_PIXEL_RATE, 1,
					      OV5693_PIXEL_RATE);

	/* Exposure */
	exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN;
	ctrls->exposure = v4l2_ctrl_new_std(&ctrls->handler, ops,
					    V4L2_CID_EXPOSURE,
					    OV5693_EXPOSURE_MIN, exposure_max,
					    OV5693_EXPOSURE_STEP, exposure_max);

	/* Gain */
	ctrls->analogue_gain = v4l2_ctrl_new_std(&ctrls->handler,
						 ops, V4L2_CID_ANALOGUE_GAIN,
						 OV5693_GAIN_MIN,
						 OV5693_GAIN_MAX,
						 OV5693_GAIN_STEP,
						 OV5693_GAIN_DEF);

	ctrls->digital_gain = v4l2_ctrl_new_std(&ctrls->handler, ops,
						V4L2_CID_DIGITAL_GAIN,
						OV5693_DIGITAL_GAIN_MIN,
						OV5693_DIGITAL_GAIN_MAX,
						OV5693_DIGITAL_GAIN_STEP,
						OV5693_DIGITAL_GAIN_DEF);

	/* Flip */
	ctrls->hflip = v4l2_ctrl_new_std(&ctrls->handler, ops,
					 V4L2_CID_HFLIP, 0, 1, 1, 0);

	ctrls->vflip = v4l2_ctrl_new_std(&ctrls->handler, ops,
					 V4L2_CID_VFLIP, 0, 1, 1, 0);

	hblank = OV5693_FIXED_PPL - ov5693->mode.format.width;
	ctrls->hblank = v4l2_ctrl_new_std(&ctrls->handler, ops,
					  V4L2_CID_HBLANK, hblank,
					  hblank, 1, hblank);

	if (ctrls->hblank)
		ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;

	vblank_max = OV5693_TIMING_MAX_VTS - ov5693->mode.format.height;
	vblank_def = ov5693->mode.vts - ov5693->mode.format.height;
	ctrls->vblank = v4l2_ctrl_new_std(&ctrls->handler, ops,
					  V4L2_CID_VBLANK,
					  OV5693_TIMING_MIN_VTS,
					  vblank_max, 1, vblank_def);

	ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(
					&ctrls->handler, ops,
					V4L2_CID_TEST_PATTERN,
					ARRAY_SIZE(ov5693_test_pattern_menu) - 1,
					0, 0, ov5693_test_pattern_menu);

	if (ctrls->handler.error) {
		dev_err(ov5693->dev, "Error initialising v4l2 ctrls\n");
		ret = ctrls->handler.error;
		goto err_free_handler;
	}

	/* set properties from fwnode (e.g. rotation, orientation) */
	ret = v4l2_fwnode_device_parse(ov5693->dev, &props);
	if (ret)
		goto err_free_handler;

	ret = v4l2_ctrl_new_fwnode_properties(&ctrls->handler, ops,
					      &props);
	if (ret)
		goto err_free_handler;

	/* Use same lock for controls as for everything else. */
	ctrls->handler.lock = &ov5693->lock;
	ov5693->sd.ctrl_handler = &ctrls->handler;

	return 0;

err_free_handler:
	v4l2_ctrl_handler_free(&ctrls->handler);
	return ret;
}

static int ov5693_configure_gpios(struct ov5693_device *ov5693)
{
	ov5693->reset = devm_gpiod_get_optional(ov5693->dev, "reset",
						GPIOD_OUT_HIGH);
	if (IS_ERR(ov5693->reset)) {
		dev_err(ov5693->dev, "Error fetching reset GPIO\n");
		return PTR_ERR(ov5693->reset);
	}

	ov5693->powerdown = devm_gpiod_get_optional(ov5693->dev, "powerdown",
						    GPIOD_OUT_HIGH);
	if (IS_ERR(ov5693->powerdown)) {
		dev_err(ov5693->dev, "Error fetching powerdown GPIO\n");
		return PTR_ERR(ov5693->powerdown);
	}

	ov5693->privacy_led = devm_gpiod_get_optional(ov5693->dev, "privacy-led",
						      GPIOD_OUT_LOW);
	if (IS_ERR(ov5693->privacy_led)) {
		dev_err(ov5693->dev, "Error fetching privacy-led GPIO\n");
		return PTR_ERR(ov5693->privacy_led);
	}

	return 0;
}

static int ov5693_get_regulators(struct ov5693_device *ov5693)
{
	unsigned int i;

	for (i = 0; i < OV5693_NUM_SUPPLIES; i++)
		ov5693->supplies[i].supply = ov5693_supply_names[i];

	return devm_regulator_bulk_get(ov5693->dev, OV5693_NUM_SUPPLIES,
				       ov5693->supplies);
}

static int ov5693_check_hwcfg(struct ov5693_device *ov5693)
{
	struct fwnode_handle *fwnode = dev_fwnode(ov5693->dev);
	struct v4l2_fwnode_endpoint bus_cfg = {
		.bus_type = V4L2_MBUS_CSI2_DPHY,
	};
	struct fwnode_handle *endpoint;
	unsigned int i;
	int ret;

	endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
	if (!endpoint)
		return -EPROBE_DEFER; /* Could be provided by cio2-bridge */

	ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
	fwnode_handle_put(endpoint);
	if (ret)
		return ret;

	if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) {
		dev_err(ov5693->dev, "only a 2-lane CSI2 config is supported");
		ret = -EINVAL;
		goto out_free_bus_cfg;
	}

	if (!bus_cfg.nr_of_link_frequencies) {
		dev_err(ov5693->dev, "no link frequencies defined\n");
		ret = -EINVAL;
		goto out_free_bus_cfg;
	}

	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
		if (bus_cfg.link_frequencies[i] == OV5693_LINK_FREQ_419_2MHZ)
			break;

	if (i == bus_cfg.nr_of_link_frequencies) {
		dev_err(ov5693->dev, "supported link freq %ull not found\n",
			OV5693_LINK_FREQ_419_2MHZ);
		ret = -EINVAL;
		goto out_free_bus_cfg;
	}

out_free_bus_cfg:
	v4l2_fwnode_endpoint_free(&bus_cfg);

	return ret;
}

static int ov5693_probe(struct i2c_client *client)
{
	struct ov5693_device *ov5693;
	u32 xvclk_rate;
	int ret = 0;

	ov5693 = devm_kzalloc(&client->dev, sizeof(*ov5693), GFP_KERNEL);
	if (!ov5693)
		return -ENOMEM;

	ov5693->client = client;
	ov5693->dev = &client->dev;

	ret = ov5693_check_hwcfg(ov5693);
	if (ret)
		return ret;

	mutex_init(&ov5693->lock);

	v4l2_i2c_subdev_init(&ov5693->sd, client, &ov5693_ops);

	ov5693->xvclk = devm_clk_get_optional(&client->dev, "xvclk");
	if (IS_ERR(ov5693->xvclk))
		return dev_err_probe(&client->dev, PTR_ERR(ov5693->xvclk),
				     "failed to get xvclk: %ld\n",
				     PTR_ERR(ov5693->xvclk));

	if (ov5693->xvclk) {
		xvclk_rate = clk_get_rate(ov5693->xvclk);
	} else {
		ret = fwnode_property_read_u32(dev_fwnode(&client->dev),
				     "clock-frequency",
				     &xvclk_rate);

		if (ret) {
			dev_err(&client->dev, "can't get clock frequency");
			return ret;
		}
	}

	if (xvclk_rate != OV5693_XVCLK_FREQ)
		dev_warn(&client->dev, "Found clk freq %u, expected %u\n",
			 xvclk_rate, OV5693_XVCLK_FREQ);

	ret = ov5693_configure_gpios(ov5693);
	if (ret)
		return ret;

	ret = ov5693_get_regulators(ov5693);
	if (ret)
		return dev_err_probe(&client->dev, ret,
				     "Error fetching regulators\n");

	ov5693->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
	ov5693->pad.flags = MEDIA_PAD_FL_SOURCE;
	ov5693->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;

	ov5693->mode.crop = ov5693_default_crop;
	ov5693->mode.format = ov5693_default_fmt;
	ov5693->mode.vts = __ov5693_calc_vts(ov5693->mode.format.height);

	ret = ov5693_init_controls(ov5693);
	if (ret)
		return ret;

	ret = media_entity_pads_init(&ov5693->sd.entity, 1, &ov5693->pad);
	if (ret)
		goto err_ctrl_handler_free;

	/*
	 * We need the driver to work in the event that pm runtime is disable in
	 * the kernel, so power up and verify the chip now. In the event that
	 * runtime pm is disabled this will leave the chip on, so that streaming
	 * will work.
	 */

	ret = ov5693_sensor_powerup(ov5693);
	if (ret)
		goto err_media_entity_cleanup;

	ret = ov5693_detect(ov5693);
	if (ret)
		goto err_powerdown;

	pm_runtime_set_active(&client->dev);
	pm_runtime_get_noresume(&client->dev);
	pm_runtime_enable(&client->dev);

	ret = v4l2_async_register_subdev_sensor(&ov5693->sd);
	if (ret) {
		dev_err(&client->dev, "failed to register V4L2 subdev: %d",
			ret);
		goto err_pm_runtime;
	}

	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
	pm_runtime_use_autosuspend(&client->dev);
	pm_runtime_put_autosuspend(&client->dev);

	return ret;

err_pm_runtime:
	pm_runtime_disable(&client->dev);
	pm_runtime_put_noidle(&client->dev);
err_powerdown:
	ov5693_sensor_powerdown(ov5693);
err_media_entity_cleanup:
	media_entity_cleanup(&ov5693->sd.entity);
err_ctrl_handler_free:
	v4l2_ctrl_handler_free(&ov5693->ctrls.handler);

	return ret;
}

static void ov5693_remove(struct i2c_client *client)
{
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);

	v4l2_async_unregister_subdev(sd);
	media_entity_cleanup(&ov5693->sd.entity);
	v4l2_ctrl_handler_free(&ov5693->ctrls.handler);
	mutex_destroy(&ov5693->lock);

	/*
	 * Disable runtime PM. In case runtime PM is disabled in the kernel,
	 * make sure to turn power off manually.
	 */
	pm_runtime_disable(&client->dev);
	if (!pm_runtime_status_suspended(&client->dev))
		ov5693_sensor_powerdown(ov5693);
	pm_runtime_set_suspended(&client->dev);
}

static const struct dev_pm_ops ov5693_pm_ops = {
	SET_RUNTIME_PM_OPS(ov5693_sensor_suspend, ov5693_sensor_resume, NULL)
};

static const struct acpi_device_id ov5693_acpi_match[] = {
	{"INT33BE"},
	{},
};
MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match);

static const struct of_device_id ov5693_of_match[] = {
	{ .compatible = "ovti,ov5693", },
	{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, ov5693_of_match);

static struct i2c_driver ov5693_driver = {
	.driver = {
		.name = "ov5693",
		.acpi_match_table = ov5693_acpi_match,
		.of_match_table = ov5693_of_match,
		.pm = &ov5693_pm_ops,
	},
	.probe_new = ov5693_probe,
	.remove = ov5693_remove,
};
module_i2c_driver(ov5693_driver);

MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
MODULE_LICENSE("GPL");