hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
// SPDX-License-Identifier: GPL-2.0
/*
 * gc1084 sensor driver
 *
 * Copyright (C) 2022 Rockchip Electronics Co., Ltd.
 *
 * V0.0X01.0X00 first version.
 * V0.0X01.0X01 Add HDR support.
 * V0.0X01.0X02 update sensor driver
 * 1. fix linear mode ae flicker issue.
 * 2. add hdr mode exposure limit issue.
 * 3. fix hdr mode highlighting pink issue.
 * 4. add some debug info.
 */
//#define DEBUG
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/of_graph.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/version.h>
#include <linux/rk-camera-module.h>
#include <linux/rk-preisp.h>
 
#include <media/v4l2-async.h>
#include <media/media-entity.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-subdev.h>
 
#define DRIVER_VERSION        KERNEL_VERSION(0, 0x01, 0x02)
#define GC1084_NAME        "gc1084"
#define GC1084_MEDIA_BUS_FMT    MEDIA_BUS_FMT_SGRBG10_1X10
 
#define MIPI_FREQ_400M        400000000
 
#define GC1084_XVCLK_FREQ    27000000
 
#define GC1084_REG_CHIP_ID_H    0x03F0
#define GC1084_REG_CHIP_ID_L    0x03F1
 
#define GC1084_REG_EXP_H    0x0d03
#define GC1084_REG_EXP_L    0x0d04
 
#define GC1084_REG_VTS_H    0x0000
#define GC1084_REG_VTS_L    0x0001
 
#define GC1084_REG_CTRL_MODE    0x003E
#define GC1084_MODE_SW_STANDBY    0x11
#define GC1084_MODE_STREAMING    0x91
 
#define GC1084_CHIP_ID        0x1084
 
#define GC1084_VTS_MAX        0x3FFF
#define GC1084_HTS_MAX        0xFFF
 
#define GC1084_EXPOSURE_MAX    0x3FFF
#define GC1084_EXPOSURE_MIN    1
#define GC1084_EXPOSURE_STEP    1
 
#define GC1084_GAIN_MIN        0x40
#define GC1084_GAIN_MAX        0x2000
#define GC1084_GAIN_STEP    1
#define GC1084_GAIN_DEFAULT    64
#define REG_NULL        0xFFFF
 
#define GC1084_LANES        1
 
static const char * const gc1084_supply_names[] = {
   "dovdd",    /* Digital I/O power */
   "avdd",     /* Analog power */
   "dvdd",     /* Digital power */
};
 
#define GC1084_NUM_SUPPLIES ARRAY_SIZE(gc1084_supply_names)
 
#define to_gc1084(sd) container_of(sd, struct gc1084, subdev)
 
enum {
   LINK_FREQ_400M_INDEX,
};
 
struct gain_reg_config {
   u32 value;
   u16 analog_gain;
   u16 col_gain;
   u16 reserved;
};
 
struct gc1084_mode {
   u32 width;
   u32 height;
   struct v4l2_fract max_fps;
   u32 hts_def;
   u32 vts_def;
   u32 exp_def;
   u32 link_freq_index;
   const struct reg_sequence *reg_list;
   u32 reg_num;
   u32 hdr_mode;
   u32 vc[PAD_MAX];
};
 
struct gc1084 {
   struct device    *dev;
   struct clk    *xvclk;
   struct regmap    *regmap;
   struct gpio_desc *reset_gpio;
   struct gpio_desc *pwdn_gpio;
   struct regulator_bulk_data supplies[GC1084_NUM_SUPPLIES];
 
   struct v4l2_subdev  subdev;
   struct media_pad    pad;
   struct v4l2_ctrl_handler ctrl_handler;
   struct v4l2_ctrl    *exposure;
   struct v4l2_ctrl    *anal_gain;
   struct v4l2_ctrl    *hblank;
   struct v4l2_ctrl    *vblank;
   struct v4l2_ctrl    *h_flip;
   struct v4l2_ctrl    *v_flip;
   struct v4l2_ctrl    *link_freq;
   struct v4l2_ctrl    *pixel_rate;
 
   struct mutex        lock;
   bool            streaming;
   bool            power_on;
   unsigned int        cfg_num;
   const struct gc1084_mode *cur_mode;
 
   u32        module_index;
   const char      *module_facing;
   const char      *module_name;
   const char      *len_name;
   enum rkmodule_sync_mode    sync_mode;
   u32        cur_vts;
 
   bool              has_init_exp;
   struct preisp_hdrae_exp_s init_hdrae_exp;
};
 
static const struct regmap_config gc1084_regmap_config = {
   .reg_bits = 16,
   .val_bits = 8,
   .max_register = 0x1000,
};
 
static const s64 link_freq_menu_items[] = {
   MIPI_FREQ_400M,
};
 
static const struct reg_sequence gc1084_master_mode_regs[] = {
   {0x0068, 0x85},
   {0x0d6a, 0x80},
   {0x0069, 0x00},
   {0x006a, 0x02},
   {0x0d69, 0x04},
};
 
static const struct reg_sequence gc1084_slave_mode_regs[] = {
   {0x0d67, 0x00},
   {0x0d69, 0x03},
   {0x0d6a, 0x08},
   {0x0d6b, 0x50},
   {0x0d6c, 0x00},
   {0x0d6d, 0x53},
   {0x0d6e, 0x00},
   {0x0d6f, 0x10},
   {0x0d70, 0x00},
   {0x0d71, 0x12},
};
 
/*
 * window size=1280*720 mipi@1lane
 * mclk=27M mipi_clk=400Mbps
 * pixel_line_total=2200 line_frame_total=1125
 * row_time=44.4444us frame_rate=30fps
 */
static const struct reg_sequence gc1084_1280x720_liner_settings[] = {
   {0x03fe, 0xf0},
   {0x03fe, 0xf0},
   {0x03fe, 0xf0},
   {0x03fe, 0x00},
   {0x03f2, 0x00},
   {0x03f3, 0x00},
   {0x03f4, 0x36},
   {0x03f5, 0xc0},
   {0x03f6, 0x13},
   {0x03f7, 0x01},
   {0x03f8, 0x32},
   {0x03f9, 0x21},
   {0x03fc, 0xae},
   {0x0d05, 0x08},
   {0x0d06, 0xae},
   {0x0d08, 0x10},
   {0x0d0a, 0x02},
   {0x000c, 0x03},
   {0x0d0d, 0x02},
   {0x0d0e, 0xd4},
   {0x000f, 0x05},
   {0x0010, 0x08},
   {0x0017, 0x08},
   {0x0d73, 0x92},
   {0x0076, 0x00},
   {0x0d76, 0x00},
   {0x0d41, 0x02},
   {0x0d42, 0xee},
   {0x0d7a, 0x0a},
   {0x006b, 0x18},
   {0x0db0, 0x9d},
   {0x0db1, 0x00},
   {0x0db2, 0xac},
   {0x0db3, 0xd5},
   {0x0db4, 0x00},
   {0x0db5, 0x97},
   {0x0db6, 0x09},
   {0x00d2, 0xfc},
   {0x0d19, 0x31},
   {0x0d20, 0x40},
   {0x0d25, 0xcb},
   {0x0d27, 0x03},
   {0x0d29, 0x40},
   {0x0d43, 0x20},
   {0x0058, 0x60},
   {0x00d6, 0x66},
   {0x00d7, 0x19},
   {0x0093, 0x02},
   {0x00d9, 0x14},
   {0x00da, 0xc1},
   {0x0d2a, 0x00},
   {0x0d28, 0x04},
   {0x0dc2, 0x84},
   {0x0050, 0x30},
   {0x0080, 0x07},
   {0x008c, 0x05},
   {0x008d, 0xa8},
   {0x0077, 0x01},
   {0x0078, 0xee},
   {0x0079, 0x02},
   {0x0067, 0xc0},
   {0x0054, 0xff},
   {0x0055, 0x02},
   {0x0056, 0x00},
   {0x0057, 0x04},
   {0x005a, 0xff},
   {0x005b, 0x07},
   {0x00d5, 0x03},
   {0x0102, 0xa9},
   {0x0d03, 0x02},
   {0x0d04, 0xd0},
   {0x007a, 0x60},
   {0x04e0, 0xff},
   {0x0414, 0x75},
   {0x0415, 0x75},
   {0x0416, 0x75},
   {0x0417, 0x75},
   {0x0122, 0x00},
   {0x0121, 0x80},
   {0x0428, 0x10},
   {0x0429, 0x10},
   {0x042a, 0x10},
   {0x042b, 0x10},
   {0x042c, 0x14},
   {0x042d, 0x14},
   {0x042e, 0x18},
   {0x042f, 0x18},
   {0x0430, 0x05},
   {0x0431, 0x05},
   {0x0432, 0x05},
   {0x0433, 0x05},
   {0x0434, 0x05},
   {0x0435, 0x05},
   {0x0436, 0x05},
   {0x0437, 0x05},
   {0x0153, 0x00},
   {0x0190, 0x01},
   {0x0192, 0x02},
   {0x0194, 0x04},
   {0x0195, 0x02},
   {0x0196, 0xd0},
   {0x0197, 0x05},
   {0x0198, 0x00},
   {0x0201, 0x23},
   {0x0202, 0x53},
   {0x0203, 0xce},
   {0x0208, 0x39},
   {0x0212, 0x06},
   {0x0213, 0x40},
   {0x0215, 0x12},
   {0x0229, 0x05},
   {0x023e, 0x98},
   {0x031e, 0x3e},
};
 
static const struct gc1084_mode supported_modes[] = {
   {
       .width = 1280,
       .height = 720,
       .max_fps = {
           .numerator = 10000,
           .denominator = 300000,
       },
       .exp_def = 0x460,
       .hts_def = 0x898,
       .vts_def = 0x465,
       .link_freq_index = LINK_FREQ_400M_INDEX,
       .reg_list = gc1084_1280x720_liner_settings,
       .reg_num = ARRAY_SIZE(gc1084_1280x720_liner_settings),
       .hdr_mode = NO_HDR,
       .vc[PAD0] = V4L2_MBUS_CSI2_CHANNEL_0,
   },
};
 
/* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
/* * 2, to match suitable isp freq */
static u64 to_pixel_rate(u32 index)
{
   u64 pixel_rate = link_freq_menu_items[index] * 2 * GC1084_LANES * 2;
 
   do_div(pixel_rate, 10);
 
   return pixel_rate;
}
 
static inline int gc1084_read_reg(struct gc1084 *gc1084, u16 addr, u8 *value)
{
   unsigned int val;
   int ret;
 
   ret = regmap_read(gc1084->regmap, addr, &val);
   if (ret) {
       dev_err(gc1084->dev, "i2c read failed at addr: %x\n", addr);
       return ret;
   }
 
   *value = val & 0xff;
 
   return 0;
}
 
static inline int gc1084_write_reg(struct gc1084 *gc1084, u16 addr, u8 value)
{
   int ret;
 
   ret = regmap_write(gc1084->regmap, addr, value);
   if (ret) {
       dev_err(gc1084->dev, "i2c write failed at addr: %x\n", addr);
       return ret;
   }
 
   return ret;
}
 
static const struct gain_reg_config gain_reg_configs[] = {
   {  64, 0x0000, 0x0100, 0x0080},
   {  76, 0x0a00, 0x010b, 0x0080},
   {  90, 0x0001, 0x0119, 0x0080},
   { 106, 0x0a01, 0x012a, 0x0080},
   { 128, 0x0002, 0x0200, 0x0080},
   { 152, 0x0a02, 0x0217, 0x0080},
   { 179, 0x0003, 0x0233, 0x0080},
   { 212, 0x0a03, 0x0314, 0x0080},
   { 256, 0x0004, 0x0400, 0x0090},
   { 303, 0x0a04, 0x042f, 0x0090},
   { 358, 0x0005, 0x0526, 0x0090},
   { 425, 0x0a05, 0x0628, 0x0090},
   { 512, 0x0006, 0x0800, 0x00a0},
   { 607, 0x0a06, 0x091e, 0x00a0},
   { 716, 0x1246, 0x0b0c, 0x00a0},
   { 848, 0x1966, 0x0d10, 0x00a0},
   {1024, 0x4004, 0x1000, 0x00a0},
   {1214, 0x4a04, 0x123d, 0x00a0},
   {1434, 0x4005, 0x1619, 0x00b0},
   {1699, 0x4a05, 0x1a23, 0x00c0},
   {2048, 0x4006, 0x2000, 0x00c0},
   {2427, 0x4a06, 0x253b, 0x00c0},
   {2865, 0x5246, 0x2c30, 0x00c0},
   {3393, 0x5946, 0x3501, 0x00d0},
   {4096, 0x6006, 0x3f3f, 0x00e0},
};
 
static int gc1084_set_gain(struct gc1084 *gc1084, u32 gain)
{
   int ret, i = 0;
   u16 pre_gain = 0;
 
   for (i = 0; i < ARRAY_SIZE(gain_reg_configs) - 1; i++)
       if ((gain_reg_configs[i].value <= gain) && (gain < gain_reg_configs[i+1].value))
           break;
 
   ret = gc1084_write_reg(gc1084, 0x00d1, (gain_reg_configs[i].analog_gain >> 8) & 0x3f);
   ret |= gc1084_write_reg(gc1084, 0x00d0, gain_reg_configs[i].analog_gain & 0xff);
 
   ret |= gc1084_write_reg(gc1084, 0x031d, 0x2e);
 
   ret |= gc1084_write_reg(gc1084, 0x0dc1, (gain_reg_configs[i].analog_gain >> 14) & 1);
 
   ret |= gc1084_write_reg(gc1084, 0x031d, 0x28);
 
   ret |= gc1084_write_reg(gc1084, 0x0155, gain_reg_configs[i].reserved & 0xff);
 
   ret |= gc1084_write_reg(gc1084, 0x00b8, gain_reg_configs[i].col_gain >> 8);
   ret |= gc1084_write_reg(gc1084, 0x00b9, gain_reg_configs[i].col_gain & 0xff);
 
   pre_gain = 64 * gain / gain_reg_configs[i].value;
 
   ret |= gc1084_write_reg(gc1084, 0x00b1, (pre_gain >> 6));
   ret |= gc1084_write_reg(gc1084, 0x00b2, ((pre_gain & 0x3f) << 2));
 
   return ret;
}
 
static int gc1084_set_ctrl(struct v4l2_ctrl *ctrl)
{
   struct gc1084 *gc1084 = container_of(ctrl->handler,
                        struct gc1084, ctrl_handler);
   s64 max;
   int ret = 0;
   u32 vts = 0;
 
   /* Propagate change of current control to all related controls */
   switch (ctrl->id) {
   case V4L2_CID_VBLANK:
       /* Update max exposure while meeting expected vblanking */
       max = gc1084->cur_mode->height + ctrl->val - 4;
       __v4l2_ctrl_modify_range(gc1084->exposure,
                    gc1084->exposure->minimum, max,
                    gc1084->exposure->step,
                    gc1084->exposure->default_value);
       break;
   }
   if (!pm_runtime_get_if_in_use(gc1084->dev))
       return 0;
 
   switch (ctrl->id) {
   case V4L2_CID_EXPOSURE:
       if (gc1084->cur_mode->hdr_mode != NO_HDR)
           goto ctrl_end;
       dev_dbg(gc1084->dev, "set exposure value 0x%x\n", ctrl->val);
       ret = gc1084_write_reg(gc1084, GC1084_REG_EXP_H,
                      (ctrl->val >> 8) & 0x3f);
       ret |= gc1084_write_reg(gc1084, GC1084_REG_EXP_L,
                   ctrl->val & 0xff);
       break;
   case V4L2_CID_ANALOGUE_GAIN:
       if (gc1084->cur_mode->hdr_mode != NO_HDR)
           goto ctrl_end;
       dev_dbg(gc1084->dev, "set gain value 0x%x\n", ctrl->val);
       gc1084_set_gain(gc1084, ctrl->val);
       break;
   case V4L2_CID_VBLANK:
       vts = gc1084->cur_mode->height + ctrl->val;
       gc1084->cur_vts = vts;
       ret = gc1084_write_reg(gc1084, GC1084_REG_VTS_H,
                      (vts >> 8) & 0x3f);
       ret |= gc1084_write_reg(gc1084, GC1084_REG_VTS_L,
                   vts & 0xff);
       dev_dbg(gc1084->dev, " set blank value 0x%x\n", ctrl->val);
       break;
   default:
       dev_warn(gc1084->dev, "%s Unhandled id:0x%x, val:0x%x\n",
            __func__, ctrl->id, ctrl->val);
       break;
   }
 
ctrl_end:
   pm_runtime_put(gc1084->dev);
   return ret;
}
 
static const struct v4l2_ctrl_ops gc1084_ctrl_ops = {
   .s_ctrl = gc1084_set_ctrl,
};
 
static int gc1084_get_regulators(struct gc1084 *gc1084)
{
   unsigned int i;
 
   for (i = 0; i < GC1084_NUM_SUPPLIES; i++)
       gc1084->supplies[i].supply = gc1084_supply_names[i];
 
   return devm_regulator_bulk_get(gc1084->dev,
                      GC1084_NUM_SUPPLIES,
                      gc1084->supplies);
}
 
static int gc1084_initialize_controls(struct gc1084 *gc1084)
{
   const struct gc1084_mode *mode;
   struct v4l2_ctrl_handler *handler;
   s64 exposure_max, vblank_def;
   u32 h_blank;
   int ret;
 
   handler = &gc1084->ctrl_handler;
   mode = gc1084->cur_mode;
   ret = v4l2_ctrl_handler_init(handler, 8);
   if (ret)
       return ret;
   handler->lock = &gc1084->lock;
 
   gc1084->link_freq = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
                          ARRAY_SIZE(link_freq_menu_items) - 1, 0,
                          link_freq_menu_items);
 
   gc1084->pixel_rate = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
                          0, to_pixel_rate(LINK_FREQ_400M_INDEX),
                          1, to_pixel_rate(LINK_FREQ_400M_INDEX));
 
   h_blank = mode->hts_def - mode->width;
   gc1084->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
                      h_blank, h_blank, 1, h_blank);
   if (gc1084->hblank)
       gc1084->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
 
   vblank_def = mode->vts_def - mode->height;
   gc1084->cur_vts = mode->vts_def;
   gc1084->vblank = v4l2_ctrl_new_std(handler, &gc1084_ctrl_ops,
                      V4L2_CID_VBLANK, vblank_def,
                      GC1084_VTS_MAX - mode->height,
                      1, vblank_def);
 
   exposure_max = mode->vts_def - 4;
   gc1084->exposure = v4l2_ctrl_new_std(handler, &gc1084_ctrl_ops,
                        V4L2_CID_EXPOSURE, GC1084_EXPOSURE_MIN,
                        exposure_max, GC1084_EXPOSURE_STEP,
                        mode->exp_def);
 
   gc1084->anal_gain = v4l2_ctrl_new_std(handler, &gc1084_ctrl_ops,
                         V4L2_CID_ANALOGUE_GAIN, GC1084_GAIN_MIN,
                         GC1084_GAIN_MAX, GC1084_GAIN_STEP,
                         GC1084_GAIN_DEFAULT);
 
   gc1084->h_flip = v4l2_ctrl_new_std(handler, &gc1084_ctrl_ops,
                      V4L2_CID_HFLIP, 0, 1, 1, 0);
 
   gc1084->v_flip = v4l2_ctrl_new_std(handler, &gc1084_ctrl_ops,
                      V4L2_CID_VFLIP, 0, 1, 1, 0);
 
   if (handler->error) {
       ret = handler->error;
       dev_err(gc1084->dev, "Failed to init controls(%d)\n", ret);
       goto err_free_handler;
   }
 
   gc1084->subdev.ctrl_handler = handler;
   gc1084->has_init_exp = false;
 
   return 0;
 
err_free_handler:
   v4l2_ctrl_handler_free(handler);
   return ret;
}
 
static int __gc1084_power_on(struct gc1084 *gc1084)
{
   int ret;
   struct device *dev = gc1084->dev;
 
   ret = clk_set_rate(gc1084->xvclk, GC1084_XVCLK_FREQ);
   if (ret < 0)
       dev_warn(dev, "Failed to set xvclk rate\n");
 
   if (clk_get_rate(gc1084->xvclk) != GC1084_XVCLK_FREQ)
       dev_warn(dev, "xvclk mismatched, modes are based on 27MHz\n");
 
   ret = clk_prepare_enable(gc1084->xvclk);
   if (ret < 0) {
       dev_err(dev, "Failed to enable xvclk\n");
       return ret;
   }
 
   ret = regulator_bulk_enable(GC1084_NUM_SUPPLIES, gc1084->supplies);
   if (ret < 0) {
       dev_err(dev, "Failed to enable regulators\n");
       goto disable_clk;
   }
 
   if (!IS_ERR(gc1084->reset_gpio))
       gpiod_set_value_cansleep(gc1084->reset_gpio, 1);
 
   usleep_range(1000, 2000);
 
   if (!IS_ERR(gc1084->pwdn_gpio))
       gpiod_set_value_cansleep(gc1084->pwdn_gpio, 1);
   if (!IS_ERR(gc1084->reset_gpio))
       gpiod_set_value_cansleep(gc1084->reset_gpio, 0);
 
   usleep_range(10000, 20000);
 
   return 0;
 
disable_clk:
   clk_disable_unprepare(gc1084->xvclk);
   return ret;
}
 
static void __gc1084_power_off(struct gc1084 *gc1084)
{
   if (!IS_ERR(gc1084->reset_gpio))
       gpiod_set_value_cansleep(gc1084->reset_gpio, 1);
   if (!IS_ERR(gc1084->pwdn_gpio))
       gpiod_set_value_cansleep(gc1084->pwdn_gpio, 0);
 
   regulator_bulk_disable(GC1084_NUM_SUPPLIES, gc1084->supplies);
   clk_disable_unprepare(gc1084->xvclk);
}
 
static int gc1084_check_sensor_id(struct gc1084 *gc1084)
{
   u8 id_h = 0, id_l = 0;
   u16 id = 0;
   int ret = 0;
 
   ret = gc1084_read_reg(gc1084, GC1084_REG_CHIP_ID_H, &id_h);
   ret |= gc1084_read_reg(gc1084, GC1084_REG_CHIP_ID_L, &id_l);
   if (ret) {
       dev_err(gc1084->dev, "Failed to read sensor id, (%d)\n", ret);
       return ret;
   }
 
   id = id_h << 8 | id_l;
   if (id != GC1084_CHIP_ID) {
       dev_err(gc1084->dev, "sensor id: %04X mismatched\n", id);
       return -ENODEV;
   }
 
   dev_info(gc1084->dev, "Detected GC1084 sensor\n");
   return 0;
}
 
static void gc1084_get_module_inf(struct gc1084 *gc1084,
                 struct rkmodule_inf *inf)
{
   memset(inf, 0, sizeof(*inf));
   strlcpy(inf->base.lens, gc1084->len_name, sizeof(inf->base.lens));
   strlcpy(inf->base.sensor, GC1084_NAME, sizeof(inf->base.sensor));
   strlcpy(inf->base.module, gc1084->module_name, sizeof(inf->base.module));
}
 
static long gc1084_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
{
   struct gc1084 *gc1084 = to_gc1084(sd);
   struct rkmodule_hdr_cfg *hdr_cfg;
   long ret = 0;
   u32 stream = 0;
   u64 delay_us = 0;
   u32 fps = 0;
   u32 *sync_mode = NULL;
 
   switch (cmd) {
   case RKMODULE_GET_HDR_CFG:
       hdr_cfg = (struct rkmodule_hdr_cfg *)arg;
       hdr_cfg->esp.mode = HDR_NORMAL_VC;
       hdr_cfg->hdr_mode = gc1084->cur_mode->hdr_mode;
       break;
   case RKMODULE_GET_MODULE_INFO:
       gc1084_get_module_inf(gc1084, (struct rkmodule_inf *)arg);
       break;
   case RKMODULE_SET_QUICK_STREAM:
 
       stream = *((u32 *)arg);
 
       if (stream) {
           ret = gc1084_write_reg(gc1084, GC1084_REG_CTRL_MODE,
               GC1084_MODE_STREAMING);
       } else {
           ret = gc1084_write_reg(gc1084, GC1084_REG_CTRL_MODE,
               GC1084_MODE_SW_STANDBY);
           fps = gc1084->cur_mode->max_fps.denominator /
                 gc1084->cur_mode->max_fps.numerator;
           delay_us = 1000000 / (gc1084->cur_mode->vts_def * fps / gc1084->cur_vts);
           usleep_range(delay_us, delay_us + 2000);
       }
       break;
   case RKMODULE_GET_SYNC_MODE:
       sync_mode = (u32 *)arg;
       *sync_mode = gc1084->sync_mode;
       break;
   case RKMODULE_SET_SYNC_MODE:
       sync_mode = (u32 *)arg;
       gc1084->sync_mode = *sync_mode;
       break;
   default:
       ret = -ENOIOCTLCMD;
       break;
   }
   return ret;
}
 
static int __gc1084_start_stream(struct gc1084 *gc1084)
{
   int ret;
 
   ret = regmap_multi_reg_write(gc1084->regmap,
                    gc1084->cur_mode->reg_list,
                    gc1084->cur_mode->reg_num);
   if (ret)
       return ret;
 
   /* Apply customized control from user */
   mutex_unlock(&gc1084->lock);
   v4l2_ctrl_handler_setup(&gc1084->ctrl_handler);
   mutex_lock(&gc1084->lock);
 
   if (gc1084->has_init_exp && gc1084->cur_mode->hdr_mode != NO_HDR) {
       ret = gc1084_ioctl(&gc1084->subdev, PREISP_CMD_SET_HDRAE_EXP,
                  &gc1084->init_hdrae_exp);
       if (ret) {
           dev_err(gc1084->dev, "init exp fail in hdr mode\n");
           return ret;
       }
   }
 
   if (gc1084->sync_mode == INTERNAL_MASTER_MODE) {
       ret = regmap_multi_reg_write(gc1084->regmap, gc1084_master_mode_regs,
                        ARRAY_SIZE(gc1084_master_mode_regs));
       if (ret)
           dev_err(gc1084->dev,
               "write internal master mode reg failed %d\n", ret);
   } else if (gc1084->sync_mode == EXTERNAL_MASTER_MODE) {
       ret = regmap_multi_reg_write(gc1084->regmap, gc1084_slave_mode_regs,
                        ARRAY_SIZE(gc1084_slave_mode_regs));
       if (ret)
           dev_err(gc1084->dev,
               "write external master mode reg failed %d\n", ret);
   } else if (gc1084->sync_mode == SLAVE_MODE) {
       ret = regmap_multi_reg_write(gc1084->regmap, gc1084_slave_mode_regs,
                        ARRAY_SIZE(gc1084_slave_mode_regs));
       if (ret)
           dev_err(gc1084->dev, "write slave mode reg failed %d\n", ret);
   }
 
   return gc1084_write_reg(gc1084, GC1084_REG_CTRL_MODE,
               GC1084_MODE_STREAMING);
}
 
static int __gc1084_stop_stream(struct gc1084 *gc1084)
{
   gc1084->has_init_exp = false;
   return gc1084_write_reg(gc1084, GC1084_REG_CTRL_MODE,
               GC1084_MODE_SW_STANDBY);
}
 
#ifdef CONFIG_COMPAT
static long gc1084_compat_ioctl32(struct v4l2_subdev *sd,
                 unsigned int cmd, unsigned long arg)
{
   void __user *up = compat_ptr(arg);
   struct rkmodule_inf *inf;
   struct rkmodule_hdr_cfg *hdr;
   struct preisp_hdrae_exp_s *hdrae;
   long ret = 0;
   u32 stream = 0;
   u32 sync_mode;
 
   switch (cmd) {
   case RKMODULE_GET_MODULE_INFO:
       inf = kzalloc(sizeof(*inf), GFP_KERNEL);
       if (!inf) {
           ret = -ENOMEM;
           return ret;
       }
 
       ret = gc1084_ioctl(sd, cmd, inf);
       if (!ret) {
           ret = copy_to_user(up, inf, sizeof(*inf));
           if (ret)
               ret = -EFAULT;
       }
       kfree(inf);
       break;
   case RKMODULE_GET_HDR_CFG:
       hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
       if (!hdr) {
           ret = -ENOMEM;
           return ret;
       }
 
       ret = gc1084_ioctl(sd, cmd, hdr);
       if (!ret) {
           ret = copy_to_user(up, hdr, sizeof(*hdr));
           if (ret)
               ret = -EFAULT;
       }
       kfree(hdr);
       break;
   case RKMODULE_SET_HDR_CFG:
       hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
       if (!hdr) {
           ret = -ENOMEM;
           return ret;
       }
 
       ret = copy_from_user(hdr, up, sizeof(*hdr));
       if (!ret)
           ret = gc1084_ioctl(sd, cmd, hdr);
       else
           ret = -EFAULT;
       kfree(hdr);
       break;
   case PREISP_CMD_SET_HDRAE_EXP:
       hdrae = kzalloc(sizeof(*hdrae), GFP_KERNEL);
       if (!hdrae) {
           ret = -ENOMEM;
           return ret;
       }
 
       ret = copy_from_user(hdrae, up, sizeof(*hdrae));
       if (!ret)
           ret = gc1084_ioctl(sd, cmd, hdrae);
       else
           ret = -EFAULT;
       kfree(hdrae);
       break;
   case RKMODULE_SET_QUICK_STREAM:
       ret = copy_from_user(&stream, up, sizeof(u32));
       if (!ret)
           ret = gc1084_ioctl(sd, cmd, &stream);
       else
           ret = -EFAULT;
       break;
   case RKMODULE_GET_SYNC_MODE:
       ret = gc1084_ioctl(sd, cmd, &sync_mode);
       if (!ret) {
           ret = copy_to_user(up, &sync_mode, sizeof(u32));
           if (ret)
               ret = -EFAULT;
       }
       break;
   case RKMODULE_SET_SYNC_MODE:
       ret = copy_from_user(&sync_mode, up, sizeof(u32));
       if (!ret)
           ret = gc1084_ioctl(sd, cmd, &sync_mode);
       else
           ret = -EFAULT;
       break;
   default:
       ret = -ENOIOCTLCMD;
       break;
   }
   return ret;
}
#endif
 
static int gc1084_s_stream(struct v4l2_subdev *sd, int on)
{
   struct gc1084 *gc1084 = to_gc1084(sd);
   int ret = 0;
   unsigned int fps;
   unsigned int delay_us;
 
   fps = DIV_ROUND_CLOSEST(gc1084->cur_mode->max_fps.denominator,
                   gc1084->cur_mode->max_fps.numerator);
 
   dev_info(gc1084->dev, "%s: on: %d, %dx%d@%d\n", __func__, on,
               gc1084->cur_mode->width,
               gc1084->cur_mode->height,
               fps);
 
   mutex_lock(&gc1084->lock);
   on = !!on;
   if (on == gc1084->streaming)
       goto unlock_and_return;
 
   if (on) {
       ret = pm_runtime_get_sync(gc1084->dev);
       if (ret < 0) {
           pm_runtime_put_noidle(gc1084->dev);
           goto unlock_and_return;
       }
 
       ret = __gc1084_start_stream(gc1084);
       if (ret) {
           dev_err(gc1084->dev, "Failed to start gc1084 stream\n");
           pm_runtime_put(gc1084->dev);
           goto unlock_and_return;
       }
   } else {
       __gc1084_stop_stream(gc1084);
       /* delay to enable oneframe complete */
       delay_us = 1000 * 1000 / fps;
       usleep_range(delay_us, delay_us+10);
       dev_info(gc1084->dev, "%s: on: %d, sleep(%dus)\n",
               __func__, on, delay_us);
 
       pm_runtime_put(gc1084->dev);
   }
 
   gc1084->streaming = on;
 
unlock_and_return:
   mutex_unlock(&gc1084->lock);
   return 0;
}
 
static int gc1084_g_frame_interval(struct v4l2_subdev *sd,
                  struct v4l2_subdev_frame_interval *fi)
{
   struct gc1084 *gc1084 = to_gc1084(sd);
   const struct gc1084_mode *mode = gc1084->cur_mode;
 
   fi->interval = mode->max_fps;
 
   return 0;
}
 
static int gc1084_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
               struct v4l2_mbus_config *config)
{
   struct gc1084 *gc1084 = to_gc1084(sd);
   u32 val = 1 << (GC1084_LANES - 1) | V4L2_MBUS_CSI2_CHANNEL_0 |
         V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
 
   config->type = V4L2_MBUS_CSI2_DPHY;
   config->flags = (gc1084->cur_mode->hdr_mode == NO_HDR) ?
           val : (val | V4L2_MBUS_CSI2_CHANNEL_1);
 
   return 0;
}
 
static int gc1084_enum_mbus_code(struct v4l2_subdev *sd,
                struct v4l2_subdev_pad_config *cfg,
                struct v4l2_subdev_mbus_code_enum *code)
{
   if (code->index != 0)
       return -EINVAL;
   code->code = GC1084_MEDIA_BUS_FMT;
   return 0;
}
 
static int gc1084_enum_frame_sizes(struct v4l2_subdev *sd,
                  struct v4l2_subdev_pad_config *cfg,
                  struct v4l2_subdev_frame_size_enum *fse)
{
   struct gc1084 *gc1084 = to_gc1084(sd);
 
   if (fse->index >= gc1084->cfg_num)
       return -EINVAL;
 
   if (fse->code != GC1084_MEDIA_BUS_FMT)
       return -EINVAL;
 
   fse->min_width  = supported_modes[fse->index].width;
   fse->max_width  = supported_modes[fse->index].width;
   fse->max_height = supported_modes[fse->index].height;
   fse->min_height = supported_modes[fse->index].height;
   return 0;
}
 
static int gc1084_enum_frame_interval(struct v4l2_subdev *sd,
                         struct v4l2_subdev_pad_config *cfg,
                         struct v4l2_subdev_frame_interval_enum *fie)
{
   struct gc1084 *gc1084 = to_gc1084(sd);
 
   if (fie->index >= gc1084->cfg_num)
       return -EINVAL;
 
   fie->code = GC1084_MEDIA_BUS_FMT;
   fie->width = supported_modes[fie->index].width;
   fie->height = supported_modes[fie->index].height;
   fie->interval = supported_modes[fie->index].max_fps;
   fie->reserved[0] = supported_modes[fie->index].hdr_mode;
   return 0;
}
 
static int gc1084_set_fmt(struct v4l2_subdev *sd,
             struct v4l2_subdev_pad_config *cfg,
             struct v4l2_subdev_format *fmt)
{
   struct gc1084 *gc1084 = to_gc1084(sd);
   const struct gc1084_mode *mode;
   s64 h_blank, vblank_def;
 
   mutex_lock(&gc1084->lock);
 
   mode = v4l2_find_nearest_size(supported_modes,
                     ARRAY_SIZE(supported_modes),
                     width, height,
                     fmt->format.width, fmt->format.height);
 
   fmt->format.code = GC1084_MEDIA_BUS_FMT;
   fmt->format.width = mode->width;
   fmt->format.height = mode->height;
   fmt->format.field = V4L2_FIELD_NONE;
   if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
       *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
#else
       mutex_unlock(&gc1084->lock);
       return -ENOTTY;
#endif
   } else {
       gc1084->cur_mode = mode;
       __v4l2_ctrl_s_ctrl(gc1084->link_freq, mode->link_freq_index);
       __v4l2_ctrl_s_ctrl_int64(gc1084->pixel_rate,
                    to_pixel_rate(mode->link_freq_index));
       h_blank = mode->hts_def - mode->width;
       __v4l2_ctrl_modify_range(gc1084->hblank, h_blank,
                    h_blank, 1, h_blank);
       vblank_def = mode->vts_def - mode->height;
       __v4l2_ctrl_modify_range(gc1084->vblank, vblank_def,
                    GC1084_VTS_MAX - mode->height,
                    1, vblank_def);
   }
 
   mutex_unlock(&gc1084->lock);
   return 0;
}
 
static int gc1084_get_fmt(struct v4l2_subdev *sd,
             struct v4l2_subdev_pad_config *cfg,
             struct v4l2_subdev_format *fmt)
{
   struct gc1084 *gc1084 = to_gc1084(sd);
   const struct gc1084_mode *mode = gc1084->cur_mode;
 
   mutex_lock(&gc1084->lock);
   if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
       fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
#else
       mutex_unlock(&gc1084->lock);
       return -ENOTTY;
#endif
   } else {
       fmt->format.width = mode->width;
       fmt->format.height = mode->height;
       fmt->format.code = GC1084_MEDIA_BUS_FMT;
       fmt->format.field = V4L2_FIELD_NONE;
 
       /* format info: width/height/data type/virctual channel */
       if (fmt->pad < PAD_MAX && mode->hdr_mode != NO_HDR)
           fmt->reserved[0] = mode->vc[fmt->pad];
       else
           fmt->reserved[0] = mode->vc[PAD0];
 
   }
   mutex_unlock(&gc1084->lock);
   return 0;
}
 
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
static int gc1084_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
   struct gc1084 *gc1084 = to_gc1084(sd);
   struct v4l2_mbus_framefmt *try_fmt =
               v4l2_subdev_get_try_format(sd, fh->pad, 0);
   const struct gc1084_mode *def_mode = &supported_modes[0];
 
   mutex_lock(&gc1084->lock);
   /* Initialize try_fmt */
   try_fmt->width = def_mode->width;
   try_fmt->height = def_mode->height;
   try_fmt->code = GC1084_MEDIA_BUS_FMT;
   try_fmt->field = V4L2_FIELD_NONE;
   mutex_unlock(&gc1084->lock);
 
   return 0;
}
#endif
 
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
static const struct v4l2_subdev_internal_ops gc1084_internal_ops = {
   .open = gc1084_open,
};
#endif
 
static int gc1084_s_power(struct v4l2_subdev *sd, int on)
{
   struct gc1084 *gc1084 = to_gc1084(sd);
   int ret = 0;
 
   mutex_lock(&gc1084->lock);
 
   if (gc1084->power_on == !!on)
       goto unlock_and_return;
 
   if (on) {
       ret = pm_runtime_get_sync(gc1084->dev);
       if (ret < 0) {
           pm_runtime_put_noidle(gc1084->dev);
           goto unlock_and_return;
       }
       gc1084->power_on = true;
   } else {
       pm_runtime_put(gc1084->dev);
       gc1084->power_on = false;
   }
 
unlock_and_return:
   mutex_unlock(&gc1084->lock);
 
   return ret;
}
 
static const struct v4l2_subdev_core_ops gc1084_core_ops = {
   .s_power = gc1084_s_power,
   .ioctl = gc1084_ioctl,
#ifdef CONFIG_COMPAT
   .compat_ioctl32 = gc1084_compat_ioctl32,
#endif
};
 
static const struct v4l2_subdev_video_ops gc1084_video_ops = {
   .s_stream = gc1084_s_stream,
   .g_frame_interval = gc1084_g_frame_interval,
};
 
static const struct v4l2_subdev_pad_ops gc1084_pad_ops = {
   .enum_mbus_code = gc1084_enum_mbus_code,
   .enum_frame_size = gc1084_enum_frame_sizes,
   .enum_frame_interval = gc1084_enum_frame_interval,
   .get_fmt = gc1084_get_fmt,
   .set_fmt = gc1084_set_fmt,
   .get_mbus_config = gc1084_g_mbus_config,
};
 
static const struct v4l2_subdev_ops gc1084_subdev_ops = {
   .core   = &gc1084_core_ops,
   .video  = &gc1084_video_ops,
   .pad    = &gc1084_pad_ops,
};
 
static int gc1084_runtime_resume(struct device *dev)
{
   struct i2c_client *client = to_i2c_client(dev);
   struct v4l2_subdev *sd = i2c_get_clientdata(client);
   struct gc1084 *gc1084 = to_gc1084(sd);
 
   __gc1084_power_on(gc1084);
   return 0;
}
 
static int gc1084_runtime_suspend(struct device *dev)
{
   struct i2c_client *client = to_i2c_client(dev);
   struct v4l2_subdev *sd = i2c_get_clientdata(client);
   struct gc1084 *gc1084 = to_gc1084(sd);
 
   __gc1084_power_off(gc1084);
   return 0;
}
 
static const struct dev_pm_ops gc1084_pm_ops = {
   SET_RUNTIME_PM_OPS(gc1084_runtime_suspend,
              gc1084_runtime_resume, NULL)
};
 
static int gc1084_probe(struct i2c_client *client,
            const struct i2c_device_id *id)
{
   struct device *dev = &client->dev;
   struct device_node *node = dev->of_node;
   struct gc1084 *gc1084;
   struct v4l2_subdev *sd;
   char facing[2];
   int ret;
   const char *sync_mode_name = NULL;
 
   dev_info(dev, "driver version: %02x.%02x.%02x",
        DRIVER_VERSION >> 16,
        (DRIVER_VERSION & 0xff00) >> 8,
        DRIVER_VERSION & 0x00ff);
 
   gc1084 = devm_kzalloc(dev, sizeof(*gc1084), GFP_KERNEL);
   if (!gc1084)
       return -ENOMEM;
 
   gc1084->dev = dev;
   gc1084->regmap = devm_regmap_init_i2c(client, &gc1084_regmap_config);
   if (IS_ERR(gc1084->regmap)) {
       dev_err(dev, "Failed to initialize I2C\n");
       return -ENODEV;
   }
 
   ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
                  &gc1084->module_index);
   ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
                      &gc1084->module_facing);
   ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
                      &gc1084->module_name);
   ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
                      &gc1084->len_name);
   if (ret) {
       dev_err(dev, "Failed to get module information\n");
       return -EINVAL;
   }
 
   ret = of_property_read_string(node, RKMODULE_CAMERA_SYNC_MODE,
                     &sync_mode_name);
   if (ret) {
       gc1084->sync_mode = NO_SYNC_MODE;
       dev_err(dev, "could not get sync mode!\n");
   } else {
       if (strcmp(sync_mode_name, RKMODULE_EXTERNAL_MASTER_MODE) == 0)
           gc1084->sync_mode = EXTERNAL_MASTER_MODE;
       else if (strcmp(sync_mode_name, RKMODULE_INTERNAL_MASTER_MODE) == 0)
           gc1084->sync_mode = INTERNAL_MASTER_MODE;
       else if (strcmp(sync_mode_name, RKMODULE_SLAVE_MODE) == 0)
           gc1084->sync_mode = SLAVE_MODE;
   }
 
   gc1084->xvclk = devm_clk_get(gc1084->dev, "xvclk");
   if (IS_ERR(gc1084->xvclk)) {
       dev_err(gc1084->dev, "Failed to get xvclk\n");
       return -EINVAL;
   }
 
   gc1084->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
   if (IS_ERR(gc1084->reset_gpio))
       dev_warn(dev, "Failed to get reset-gpios\n");
 
   gc1084->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_HIGH);
   if (IS_ERR(gc1084->pwdn_gpio))
       dev_warn(dev, "Failed to get pwdn-gpios\n");
 
   ret = gc1084_get_regulators(gc1084);
   if (ret) {
       dev_err(dev, "Failed to get regulators\n");
       return ret;
   }
 
   mutex_init(&gc1084->lock);
 
   /* set default mode */
   gc1084->cur_mode = &supported_modes[0];
   gc1084->cfg_num = ARRAY_SIZE(supported_modes);
   gc1084->cur_vts = gc1084->cur_mode->vts_def;
 
   sd = &gc1084->subdev;
   v4l2_i2c_subdev_init(sd, client, &gc1084_subdev_ops);
   ret = gc1084_initialize_controls(gc1084);
   if (ret)
       goto err_destroy_mutex;
 
   ret = __gc1084_power_on(gc1084);
   if (ret)
       goto err_free_handler;
 
   ret = gc1084_check_sensor_id(gc1084);
   if (ret)
       goto err_power_off;
 
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
   sd->internal_ops = &gc1084_internal_ops;
   sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
#endif
 
#ifdef CONFIG_MEDIA_CONTROLLER
   gc1084->pad.flags = MEDIA_PAD_FL_SOURCE;
   sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
   ret = media_entity_pads_init(&sd->entity, 1, &gc1084->pad);
   if (ret < 0)
       goto err_power_off;
#endif
 
   memset(facing, 0, sizeof(facing));
   if (strcmp(gc1084->module_facing, "back") == 0)
       facing[0] = 'b';
   else
       facing[0] = 'f';
 
   snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
        gc1084->module_index, facing,
        GC1084_NAME, dev_name(sd->dev));
 
   ret = v4l2_async_register_subdev_sensor_common(sd);
   if (ret) {
       dev_err(dev, "Failed to register v4l2 async subdev\n");
       goto err_clean_entity;
   }
 
   pm_runtime_set_active(dev);
   pm_runtime_enable(dev);
   pm_runtime_idle(dev);
 
   return 0;
 
err_clean_entity:
#ifdef CONFIG_MEDIA_CONTROLLER
   media_entity_cleanup(&sd->entity);
#endif
err_power_off:
   __gc1084_power_off(gc1084);
err_free_handler:
   v4l2_ctrl_handler_free(&gc1084->ctrl_handler);
err_destroy_mutex:
   mutex_destroy(&gc1084->lock);
 
   return ret;
}
 
static int gc1084_remove(struct i2c_client *client)
{
   struct v4l2_subdev *sd = i2c_get_clientdata(client);
   struct gc1084 *gc1084 = to_gc1084(sd);
 
   v4l2_async_unregister_subdev(sd);
#ifdef CONFIG_MEDIA_CONTROLLER
   media_entity_cleanup(&sd->entity);
#endif
   v4l2_ctrl_handler_free(&gc1084->ctrl_handler);
   mutex_destroy(&gc1084->lock);
 
   pm_runtime_disable(&client->dev);
   if (!pm_runtime_status_suspended(&client->dev))
       __gc1084_power_off(gc1084);
   pm_runtime_set_suspended(&client->dev);
   return 0;
}
 
static const struct i2c_device_id gc1084_match_id[] = {
   { "gc1084", 0 },
   { },
};
 
static const struct of_device_id gc1084_of_match[] = {
   { .compatible = "galaxycore,gc1084" },
   {},
};
MODULE_DEVICE_TABLE(of, gc1084_of_match);
 
static struct i2c_driver gc1084_i2c_driver = {
   .driver = {
       .name = GC1084_NAME,
       .pm = &gc1084_pm_ops,
       .of_match_table = of_match_ptr(gc1084_of_match),
   },
   .probe      = &gc1084_probe,
   .remove     = &gc1084_remove,
   .id_table   = gc1084_match_id,
};
 
static int __init sensor_mod_init(void)
{
   return i2c_add_driver(&gc1084_i2c_driver);
}
static void __exit sensor_mod_exit(void)
{
   i2c_del_driver(&gc1084_i2c_driver);
}
 
device_initcall_sync(sensor_mod_init);
module_exit(sensor_mod_exit);
 
MODULE_DESCRIPTION("Galaxycore GC1084 Image Sensor driver");
MODULE_LICENSE("GPL v2");