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
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
// SPDX-License-Identifier: GPL-2.0
/*
 * nvp6158_v4l2 interface driver
 *
 * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
 *
 * V0.0X01.0X00 first version.
 * V0.0X01.0X01
 * 1. add workqueue to detect ahd state.
 * 2. add more resolution support.
 */
 
#include <linux/clk.h>
#include <linux/device.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/regulator/consumer.h>
#include <linux/sysfs.h>
#include <media/media-entity.h>
#include <media/v4l2-async.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-subdev.h>
#include <media/v4l2-fwnode.h>
#include <linux/pinctrl/consumer.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_graph.h>
#include <linux/of_platform.h>
#include <linux/of_gpio.h>
#include <linux/mfd/syscon.h>
#include <linux/version.h>
#include <linux/rk-camera-module.h>
#include <linux/rk-preisp.h>
 
#include "nvp6158_common.h"
#include "nvp6158_video.h"
#include "nvp6158_coax_protocol.h"
#include "nvp6158_motion.h"
#include "nvp6158_video_eq.h"
#include "nvp6158_drv.h"
#include "nvp6158_audio.h"
#include "nvp6158_video_auto_detect.h"
#include "nvp6158_drv.h"
 
//#define WORK_QUEUE
 
#ifdef WORK_QUEUE
#include <linux/workqueue.h>
 
struct sensor_state_check_work {
   struct workqueue_struct *state_check_wq;
   struct delayed_work d_work;
};
 
#endif
 
#define DRIVER_VERSION                KERNEL_VERSION(0, 0x01, 0x1)
 
#ifndef V4L2_CID_DIGITAL_GAIN
#define V4L2_CID_DIGITAL_GAIN            V4L2_CID_GAIN
#endif
 
#define NVP6158_XVCLK_FREQ            24000000
#define NVP6158_BITS_PER_SAMPLE            8
 
/* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
#define NVP6158_PIXEL_RATE            297000000LL
 
#define OF_CAMERA_PINCTRL_STATE_DEFAULT        "rockchip,camera_default"
#define OF_CAMERA_PINCTRL_STATE_SLEEP        "rockchip,camera_sleep"
 
#define OF_CAMERA_MODULE_REGULATORS        "rockchip,regulator-names"
#define OF_CAMERA_MODULE_REGULATOR_VOLTAGES    "rockchip,regulator-voltages"
 
/* DVP MODE, BT1120 or BT656 */
#define RK_CAMERA_MODULE_DVP_MODE        "rockchip,dvp_mode"
#define RK_CAMERA_MODULE_CHANNEL_NUMS        "rockchip,channel_nums"
#define RK_CAMERA_MODULE_DUAL_EDGE        "rockchip,dual_edge"
#define RK_CAMERA_MODULE_DEFAULT_RECT        "rockchip,default_rect"
 
#define NVP6158_DEFAULT_DVP_MODE        "BT1120"
#define NVP6158_DEFAULT_CHANNEL_NUMS        4U
#define NVP6158_DEFAULT_DUAL_EDGE        0U
#define NVP6158_NAME                "nvp6158"
#define NVP6158_DEFAULT_WIDTH            1920
#define NVP6158_DEFAULT_HEIGHT            1080
 
struct nvp6158_gpio {
   int pltfrm_gpio;
   const char *label;
   enum of_gpio_flags active_low;
};
 
struct nvp6158_regulator {
   struct regulator *regulator;
   u32 min_uV;
   u32 max_uV;
};
 
struct nvp6158_regulators {
   u32 cnt;
   struct nvp6158_regulator *regulator;
};
 
struct nvp6158_pixfmt {
   u32 code;
};
 
struct nvp6158_framesize {
   u16 width;
   u16 height;
   NC_VIVO_CH_FORMATDEF fmt_idx;
   struct v4l2_fract max_fps;
};
 
struct nvp6158_default_rect {
   unsigned int width;
   unsigned int height;
};
 
#ifdef WORK_QUEUE
enum nvp6158_hot_plug_state {
   PLUG_IN = 0,
   PLUG_OUT,
   PLUG_STATE_MAX,
};
#endif
 
struct nvp6158 {
   struct i2c_client    *client;
   struct clk        *xvclk;
   struct gpio_desc    *pwr_gpio;
   struct gpio_desc    *pwr2_gpio;
   struct gpio_desc    *rst_gpio;
   struct gpio_desc    *rst2_gpio;
   struct gpio_desc    *pwdn_gpio;
   struct gpio_desc    *pwdn2_gpio;
 
   struct pinctrl        *pinctrl;
   struct pinctrl_state    *pins_default;
   struct pinctrl_state    *pins_sleep;
 
   struct v4l2_subdev    subdev;
   struct media_pad    pad[PAD_MAX];
   struct v4l2_ctrl_handler ctrl_handler;
   struct mutex        mutex;
   bool            power_on;
   struct nvp6158_regulators regulators;
 
   u32            module_index;
   const char        *module_facing;
   const char        *module_name;
   const char        *len_name;
   const char        *dvp_mode;
   NVP6158_DVP_MODE    mode;
   u32            ch_nums;
   u32            dual_edge;
 
   struct v4l2_mbus_framefmt format;
   const struct nvp6158_framesize *frame_size;
   int streaming;
   struct nvp6158_default_rect defrect;
#ifdef WORK_QUEUE
   struct sensor_state_check_work plug_state_check;
   u8 cur_detect_status;
   u8 last_detect_status;
#endif
   bool hot_plug;
   u8 is_reset;
 
};
 
#define to_nvp6158(sd) container_of(sd, struct nvp6158, subdev)
 
static const struct nvp6158_framesize nvp6158_framesizes[] = {
   {
       .width        = 1280,
       .height        = 720,
       .fmt_idx    = AHD20_720P_30P,
       .max_fps = {
           .numerator = 10000,
           .denominator = 250000,
       },
   }, {
       .width        = 1920,
       .height        = 1080,
       .fmt_idx    = AHD20_1080P_25P,
       .max_fps = {
           .numerator = 10000,
           .denominator = 250000,
       },
   }, {
       .width        = 2048,
       .height        = 1536,
       .fmt_idx    = AHD30_3M_18P,
       .max_fps = {
           .numerator = 10000,
           .denominator = 180000,
       },
   }, {
       .width        = 1280,
       .height        = 1440,
       .fmt_idx    = AHD30_4M_30P,
       .max_fps = {
           .numerator = 10000,
           .denominator = 300000,
       },
   }, {
       .width        = 2560,
       .height        = 1440,
       .fmt_idx    = AHD30_4M_15P,
       .max_fps = {
           .numerator = 10000,
           .denominator = 150000,
       },
   }, {
       .width        = 2592,
       .height        = 1944,
       .fmt_idx    = AHD30_5M_12_5P,
       .max_fps = {
           .numerator = 10000,
           .denominator = 125000,
       },
   }, {
       .width        = 3840,
       .height        = 2160,
       .fmt_idx    = AHD30_8M_7_5P,
       .max_fps = {
           .numerator = 10000,
           .denominator = 75000,
       },
   }, {/* test modes, Interlace mode*/
       .width        = 720,
       .height        = 480,
       .fmt_idx    = AHD20_SD_SH720_NT,
       .max_fps = {
           .numerator = 10000,
           .denominator = 250000,
       },
   }, {
       .width        = 720,
       .height        = 576,
       .fmt_idx    = AHD20_SD_SH720_PAL,
       .max_fps = {
           .numerator = 10000,
           .denominator = 250000,
       },
   }, {
       .width        = 960,
       .height        = 576,
       .fmt_idx    = AHD20_SD_H960_PAL,
       .max_fps = {
           .numerator = 10000,
           .denominator = 250000,
       },
   }, {
       .width        = 1920,
       .height        = 576,
       .fmt_idx    = AHD20_SD_H960_EX_PAL,
       .max_fps = {
           .numerator = 10000,
           .denominator = 250000,
       },
   }
};
 
static char *nvp6158_dvp_mode_lists[] = {
   [BT601] = "BT601",
   [BT656_1MUX] = "BT656_1MUX",
   [BT656_2MUX] = "BT656_2MUX",
   [BT656_4MUX] = "BT656_4MUX",
   [BT1120_1MUX] = "BT1120_1MUX",
   [BT1120_2MUX] = "BT1120_2MUX",
   [BT1120_4MUX] = "BT1120_4MUX",
   [BT656I_TEST_MODES] = "BT656I_TEST_MODES"
};
 
static const struct nvp6158_pixfmt nvp6158_formats[] = {
   {
       .code = MEDIA_BUS_FMT_UYVY8_2X8
   },
};
 
static int nvp6158_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
{
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
 
   if ((nvp6158->mode > BT656I_TEST_MODES) &&
       (nvp6158->mode < NVP6158_DVP_MODES_END)) {
       /* for vicap detect bt1120 */
       *std = V4L2_STD_ATSC;
   } else {
       *std = V4L2_STD_PAL;
   }
   return 0;
}
 
/* sensor register write */
static int nvp6158_write(struct i2c_client *client, u8 reg, u8 val)
{
   struct i2c_msg msg;
   u8 buf[2];
   int ret;
 
   dev_info(&client->dev, "write reg(0x%x val:0x%x)!\n", reg, val);
   buf[0] = reg & 0xFF;
   buf[1] = val;
 
   msg.addr = client->addr;
   msg.flags = client->flags;
   msg.buf = buf;
   msg.len = sizeof(buf);
 
   ret = i2c_transfer(client->adapter, &msg, 1);
   if (ret >= 0)
       return 0;
 
   dev_err(&client->dev,
       "nvp6158 write reg(0x%x val:0x%x) failed !\n", reg, val);
 
   return ret;
}
 
/* sensor register read */
static int nvp6158_read(struct i2c_client *client, u8 reg, u8 *val)
{
   struct i2c_msg msg[2];
   u8 buf[1];
   int ret;
 
   buf[0] = reg & 0xFF;
 
   msg[0].addr = client->addr;
   msg[0].flags = client->flags;
   msg[0].buf = buf;
   msg[0].len = sizeof(buf);
 
   msg[1].addr = client->addr;
   msg[1].flags = client->flags | I2C_M_RD;
   msg[1].buf = buf;
   msg[1].len = 1;
 
   ret = i2c_transfer(client->adapter, msg, 2);
   if (ret >= 0) {
       *val = buf[0];
       return 0;
   }
 
   dev_err(&client->dev, "nvp6158 read reg(0x%x) failed !\n", reg);
 
   return ret;
}
 
static int __nvp6158_power_on(struct nvp6158 *nvp6158)
{
   u32 i;
   int ret;
   struct nvp6158_regulator *regulator;
   struct device *dev = &nvp6158->client->dev;
 
   dev_info(dev, "%s(%d)\n", __func__, __LINE__);
 
   if (!IS_ERR_OR_NULL(nvp6158->pins_default)) {
       ret = pinctrl_select_state(nvp6158->pinctrl,
                      nvp6158->pins_default);
       if (ret < 0)
           dev_err(dev, "could not set pins. ret=%d\n", ret);
   }
 
   ret = clk_prepare_enable(nvp6158->xvclk);
   if (ret < 0) {
       dev_err(dev, "Failed to enable xvclk\n");
       return ret;
   }
 
   if (nvp6158->regulators.regulator) {
       for (i = 0; i < nvp6158->regulators.cnt; i++) {
           regulator = nvp6158->regulators.regulator + i;
           if (IS_ERR(regulator->regulator))
               continue;
           regulator_set_voltage(
               regulator->regulator,
               regulator->min_uV,
               regulator->max_uV);
           if (regulator_enable(regulator->regulator)) {
               dev_err(dev,
                   "regulator_enable failed!\n");
               goto disable_clk;
           }
       }
   }
   usleep_range(3000, 5000);
 
   if (!IS_ERR(nvp6158->pwr_gpio)) {
       gpiod_direction_output(nvp6158->pwr_gpio, 1);
       usleep_range(3000, 5000);
   }
 
   if (!IS_ERR(nvp6158->pwr2_gpio)) {
       gpiod_direction_output(nvp6158->pwr2_gpio, 1);
       usleep_range(3000, 5000);
   }
 
   if (!IS_ERR(nvp6158->pwdn_gpio)) {
       gpiod_direction_output(nvp6158->pwdn_gpio, 1);
       usleep_range(1500, 2000);
   }
 
   if (!IS_ERR(nvp6158->pwdn2_gpio)) {
       gpiod_direction_output(nvp6158->pwdn2_gpio, 1);
       usleep_range(1500, 2000);
   }
 
   if (!IS_ERR(nvp6158->rst_gpio)) {
       gpiod_direction_output(nvp6158->rst_gpio, 0);
       usleep_range(50000, 100000);
       gpiod_direction_output(nvp6158->rst_gpio, 1);
       usleep_range(3000, 5000);
   }
 
   if (!IS_ERR(nvp6158->rst2_gpio)) {
       gpiod_direction_output(nvp6158->rst2_gpio, 0);
       usleep_range(1500, 2000);
       gpiod_direction_output(nvp6158->rst2_gpio, 1);
       usleep_range(3000, 5000);
   }
 
   return 0;
 
disable_clk:
   clk_disable_unprepare(nvp6158->xvclk);
 
   return ret;
}
 
static void __nvp6158_power_off(struct nvp6158 *nvp6158)
{
   u32 i;
   int ret;
   struct nvp6158_regulator *regulator;
   struct device *dev = &nvp6158->client->dev;
 
   dev_info(dev, "%s(%d)\n", __func__, __LINE__);
   clk_disable_unprepare(nvp6158->xvclk);
 
   if (!IS_ERR(nvp6158->rst_gpio))
       gpiod_direction_output(nvp6158->rst_gpio, 0);
 
   if (!IS_ERR(nvp6158->rst2_gpio))
       gpiod_direction_output(nvp6158->rst2_gpio, 0);
 
   if (!IS_ERR(nvp6158->pwdn_gpio))
       gpiod_direction_output(nvp6158->pwdn_gpio, 0);
 
   if (!IS_ERR(nvp6158->pwdn_gpio))
       gpiod_direction_output(nvp6158->pwdn2_gpio, 0);
 
   if (!IS_ERR(nvp6158->pwr_gpio))
       gpiod_direction_output(nvp6158->pwr_gpio, 0);
 
   if (!IS_ERR(nvp6158->pwr2_gpio))
       gpiod_direction_output(nvp6158->pwr2_gpio, 0);
 
   if (!IS_ERR_OR_NULL(nvp6158->pins_sleep)) {
       ret = pinctrl_select_state(nvp6158->pinctrl,
                      nvp6158->pins_sleep);
       if (ret < 0)
           dev_err(dev, "could not set pins\n");
   }
 
   if (nvp6158->regulators.regulator) {
       for (i = 0; i < nvp6158->regulators.cnt; i++) {
           regulator = nvp6158->regulators.regulator + i;
           if (IS_ERR(regulator->regulator))
               continue;
           regulator_disable(regulator->regulator);
       }
   }
}
 
static int nvp6158_power(struct v4l2_subdev *sd, int on)
{
   struct i2c_client *client = v4l2_get_subdevdata(sd);
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
   int ret = 0;
 
   dev_info(&client->dev, "%s: on %d\n", __func__, on);
   mutex_lock(&nvp6158->mutex);
 
   /* If the power state is not modified - no work to do. */
   if (nvp6158->power_on == !!on)
       goto exit;
 
   if (on) {
       ret = __nvp6158_power_on(nvp6158);
       if (ret < 0)
           goto exit;
 
       nvp6158->power_on = true;
   } else {
       __nvp6158_power_off(nvp6158);
       nvp6158->power_on = false;
   }
 
exit:
   mutex_unlock(&nvp6158->mutex);
 
   return ret;
}
 
#define CROP_START(SRC, DST) (((SRC) - (DST)) / 2 / 4 * 4)
/*
 * The resolution of the driver configuration needs to be exactly
 * the same as the current output resolution of the sensor,
 * the input width of the isp needs to be 16 aligned,
 * the input height of the isp needs to be 8 aligned.
 * Can be cropped to standard resolution by this function,
 * otherwise it will crop out strange resolution according
 * to the alignment rules.
 */
static int nvp6158_get_selection(struct v4l2_subdev *sd,
                struct v4l2_subdev_pad_config *cfg,
                struct v4l2_subdev_selection *sel)
{
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
 
   if (sel->target == V4L2_SEL_TGT_CROP_BOUNDS) {
       sel->r.left = CROP_START(0, 0);
       sel->r.width = nvp6158->frame_size->width;
       sel->r.top = CROP_START(0, 0);
       sel->r.height = nvp6158->frame_size->height;
       return 0;
   }
   return -EINVAL;
}
 
static int nvp6158_initialize_controls(struct nvp6158 *nvp6158)
{
   struct v4l2_ctrl_handler *handler;
   int ret;
 
   handler = &nvp6158->ctrl_handler;
   ret = v4l2_ctrl_handler_init(handler, 2);
   if (ret)
       return ret;
   handler->lock = &nvp6158->mutex;
 
   v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
             0, NVP6158_PIXEL_RATE, 1, NVP6158_PIXEL_RATE);
 
   if (handler->error) {
       ret = handler->error;
       dev_err(&nvp6158->client->dev,
           "Failed to init controls(%d)\n", ret);
       goto err_free_handler;
   }
 
   nvp6158->subdev.ctrl_handler = handler;
 
   return 0;
 
err_free_handler:
   v4l2_ctrl_handler_free(handler);
 
   return ret;
}
static void nvp6158_get_default_format(struct nvp6158 *nvp6158)
{
 
   const struct nvp6158_framesize *fsize = &nvp6158_framesizes[0];
   const struct nvp6158_framesize *match = NULL;
   int i = ARRAY_SIZE(nvp6158_framesizes);
   unsigned int min_err = UINT_MAX;
   struct v4l2_mbus_framefmt *format = &nvp6158->format;
   struct nvp6158_default_rect *rect =  &nvp6158->defrect;
 
   while (i--) {
       unsigned int err = abs(fsize->width - rect->width)
               + abs(fsize->height - rect->height);
       if (err < min_err) {
           min_err = err;
           match = fsize;
       }
       fsize++;
   }
 
   if (!match)
       match = &nvp6158_framesizes[0];
 
   format->width = match->width;
   format->height = match->height;
   format->colorspace = V4L2_COLORSPACE_SRGB;
   format->code = nvp6158_formats[0].code;
   if (BT656I_TEST_MODES == nvp6158->mode)
       format->field = V4L2_FIELD_INTERLACED;
   else
       format->field = V4L2_FIELD_NONE;
   nvp6158->frame_size = match;
}
 
static int nvp6158_stream(struct v4l2_subdev *sd, int on)
{
   struct i2c_client *client = v4l2_get_subdevdata(sd);
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
   video_init_all video_init;
   NC_VIVO_CH_FORMATDEF fmt_idx;
   int ch;
 
   dev_info(&client->dev, "%s: on: %d, %dx%d\n", __func__, on,
               nvp6158->frame_size->width,
               nvp6158->frame_size->height);
 
   mutex_lock(&nvp6158->mutex);
   on = !!on;
 
   if (nvp6158->streaming == on)
       goto unlock;
 
   if (on) {
       for (ch = 0; ch < 4; ch++) {
           fmt_idx = nvp6158->frame_size->fmt_idx;
           video_init.ch_param[ch].ch = ch;
           video_init.ch_param[ch].format = fmt_idx;
       }
       video_init.mode = nvp6158->mode;
       nvp6158_start(&video_init, nvp6158->dual_edge ? true : false);
#ifdef WORK_QUEUE
       if (nvp6158->plug_state_check.state_check_wq) {
           dev_info(&client->dev, "%s queue_delayed_work 1000ms", __func__);
           queue_delayed_work(nvp6158->plug_state_check.state_check_wq,
                      &nvp6158->plug_state_check.d_work,
                      msecs_to_jiffies(1000));
       }
#endif
   } else {
#ifdef WORK_QUEUE
       cancel_delayed_work_sync(&nvp6158->plug_state_check.d_work);
       dev_info(&client->dev, "cancle_queue_delayed_work");
#endif
       nvp6158_stop();
   }
 
   nvp6158->streaming = on;
 
unlock:
   mutex_unlock(&nvp6158->mutex);
 
   return 0;
}
 
static int nvp6158_enum_frame_interval(struct v4l2_subdev *sd,
                      struct v4l2_subdev_pad_config *cfg,
                      struct v4l2_subdev_frame_interval_enum *fie)
{
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
   struct i2c_client *client = nvp6158->client;
 
   dev_dbg(&client->dev, "%s enter.\n", __func__);
 
   if (fie->index >= ARRAY_SIZE(nvp6158_framesizes))
       return -EINVAL;
 
   fie->width = nvp6158_framesizes[fie->index].width;
   fie->height = nvp6158_framesizes[fie->index].height;
   fie->interval = nvp6158_framesizes[fie->index].max_fps;
   return 0;
}
 
static int nvp6158_enum_mbus_code(struct v4l2_subdev *sd,
                struct v4l2_subdev_pad_config *cfg,
                struct v4l2_subdev_mbus_code_enum *code)
{
   if (code->index >= ARRAY_SIZE(nvp6158_formats))
       return -EINVAL;
 
   code->code = nvp6158_formats[code->index].code;
 
   return 0;
}
 
static int nvp6158_enum_frame_sizes(struct v4l2_subdev *sd,
                  struct v4l2_subdev_pad_config *cfg,
                  struct v4l2_subdev_frame_size_enum *fse)
{
   struct i2c_client *client = v4l2_get_subdevdata(sd);
   int i = ARRAY_SIZE(nvp6158_formats);
 
   dev_dbg(&client->dev, "%s: enter!\n", __func__);
 
   if (fse->index >= ARRAY_SIZE(nvp6158_framesizes))
       return -EINVAL;
 
   while (--i)
       if (fse->code == nvp6158_formats[i].code)
           break;
 
   fse->code = nvp6158_formats[i].code;
 
   fse->min_width  = nvp6158_framesizes[fse->index].width;
   fse->max_width  = fse->min_width;
   fse->max_height = nvp6158_framesizes[fse->index].height;
   fse->min_height = fse->max_height;
 
   return 0;
}
 
/* indicate N4 no signal channel */
static inline bool nvp6158_no_signal(struct v4l2_subdev *sd, u8 *novid)
{
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
   struct i2c_client *client = nvp6158->client;
   u8 videoloss = 0;
   int ret;
   bool no_signal = false;
 
   nvp6158_write(client, 0xff, 0x00);
   ret = nvp6158_read(client, 0xa8, &videoloss);
   if (ret < 0)
       dev_err(&client->dev, "Failed to read videoloss state!\n");
 
   *novid = videoloss;
   dev_info(&client->dev, "%s: video loss status:0x%x.\n", __func__, videoloss);
   if (videoloss == 0xf) {
       dev_info(&client->dev, "%s: all channels No Video detected.\n", __func__);
       no_signal = true;
   } else {
       dev_info(&client->dev, "%s: channel has some video detection.\n", __func__);
       no_signal = false;
   }
   return no_signal;
}
 
/* indicate N4 channel locked status */
static inline bool nvp6158_sync(struct v4l2_subdev *sd, u8 *lock_st)
{
   struct i2c_client *client = v4l2_get_subdevdata(sd);
   u8 video_lock_status = 0;
   int ret;
   bool has_sync = false;
 
   nvp6158_write(client, 0xff, 0x00);
   ret = nvp6158_read(client, 0xe0, &video_lock_status);
   if (ret < 0)
       dev_err(&client->dev, "Failed to read sync state!\n");
 
   dev_info(&client->dev, "%s: video AGC LOCK status:0x%x.\n",
           __func__, video_lock_status);
   *lock_st = video_lock_status;
   if (video_lock_status) {
       dev_info(&client->dev, "%s: channel has AGC LOCK.\n", __func__);
       has_sync = true;
   } else {
       dev_info(&client->dev, "%s: channel has no AGC LOCK.\n", __func__);
       has_sync = false;
   }
   return has_sync;
}
 
#ifdef WORK_QUEUE
static void nvp6158_plug_state_check_work(struct work_struct *work)
{
   struct sensor_state_check_work *params_check =
       container_of(work, struct sensor_state_check_work, d_work.work);
   struct nvp6158 *nvp6158 =
       container_of(params_check, struct nvp6158, plug_state_check);
   struct i2c_client *client = nvp6158->client;
   struct v4l2_subdev *sd = &nvp6158->subdev;
   u8 novid_status = 0x00;
   u8 sync_status = 0x00;
 
   nvp6158_no_signal(sd, &novid_status);
   nvp6158_sync(sd, &sync_status);
   nvp6158->cur_detect_status = novid_status;
 
   /* detect state change to determine is there has plug motion */
   novid_status = nvp6158->cur_detect_status ^ nvp6158->last_detect_status;
   if (novid_status)
       nvp6158->hot_plug = true;
   else
       nvp6158->hot_plug = false;
   nvp6158->last_detect_status = nvp6158->cur_detect_status;
 
   dev_info(&client->dev, "%s has plug motion? (%s)", __func__,
            nvp6158->hot_plug ? "true" : "false");
   if (nvp6158->hot_plug) {
       dev_info(&client->dev, "queue_delayed_work 1500ms, if has hot plug motion.");
       queue_delayed_work(nvp6158->plug_state_check.state_check_wq,
                  &nvp6158->plug_state_check.d_work, msecs_to_jiffies(1500));
       nvp6158_write(client, 0xFF, 0x20);
       nvp6158_write(client, 0x00, (sync_status << 4) | sync_status);
       usleep_range(3000, 5000);
       nvp6158_write(client, 0x00, 0xFF);
   } else {
       dev_info(&client->dev, "queue_delayed_work 100ms, if no hot plug motion.");
       queue_delayed_work(nvp6158->plug_state_check.state_check_wq,
                  &nvp6158->plug_state_check.d_work, msecs_to_jiffies(100));
   }
}
#endif
 
static int nvp6158_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
                struct v4l2_mbus_config *cfg)
{
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
 
   cfg->type = V4L2_MBUS_BT656;
   if (nvp6158->dual_edge == 1) {
       cfg->flags = RKMODULE_CAMERA_BT656_CHANNELS |
           V4L2_MBUS_PCLK_SAMPLE_RISING |
           V4L2_MBUS_PCLK_SAMPLE_FALLING;
   } else {
       cfg->flags = RKMODULE_CAMERA_BT656_CHANNELS |
           V4L2_MBUS_PCLK_SAMPLE_RISING;
   }
   return 0;
}
 
static int nvp6158_get_fmt(struct v4l2_subdev *sd,
             struct v4l2_subdev_pad_config *cfg,
             struct v4l2_subdev_format *fmt)
{
   struct i2c_client *client = v4l2_get_subdevdata(sd);
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
 
   if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
       struct v4l2_mbus_framefmt *mf;
 
       mf = v4l2_subdev_get_try_format(sd, cfg, 0);
       mutex_lock(&nvp6158->mutex);
       fmt->format = *mf;
       mutex_unlock(&nvp6158->mutex);
       return 0;
#else
   return -ENOTTY;
#endif
   }
 
   mutex_lock(&nvp6158->mutex);
   fmt->format = nvp6158->format;
   mutex_unlock(&nvp6158->mutex);
 
   dev_dbg(&client->dev, "%s: %x %dx%d\n", __func__,
       nvp6158->format.code, nvp6158->format.width,
       nvp6158->format.height);
 
   return 0;
}
 
static void __nvp6158_try_frame_size(struct v4l2_mbus_framefmt *mf,
                   const struct nvp6158_framesize **size)
{
   const struct nvp6158_framesize *fsize = &nvp6158_framesizes[0];
   const struct nvp6158_framesize *match = NULL;
   int i = ARRAY_SIZE(nvp6158_framesizes);
   unsigned int min_err = UINT_MAX;
 
   while (i--) {
       unsigned int err = abs(fsize->width - mf->width)
               + abs(fsize->height - mf->height);
       if (err < min_err) {
           min_err = err;
           match = fsize;
       }
       fsize++;
   }
 
   if (!match)
       match = &nvp6158_framesizes[0];
 
   mf->width  = match->width;
   mf->height = match->height;
 
   if (size)
       *size = match;
}
 
static int nvp6158_set_fmt(struct v4l2_subdev *sd,
             struct v4l2_subdev_pad_config *cfg,
             struct v4l2_subdev_format *fmt)
{
   int index = ARRAY_SIZE(nvp6158_formats);
   struct v4l2_mbus_framefmt *mf = &fmt->format;
   const struct nvp6158_framesize *size = NULL;
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
   int ret = 0;
 
   __nvp6158_try_frame_size(mf, &size);
 
   while (--index >= 0)
       if (nvp6158_formats[index].code == mf->code)
           break;
 
   if (index < 0)
       return -EINVAL;
 
   mf->colorspace = V4L2_COLORSPACE_SRGB;
   mf->code = nvp6158_formats[index].code;
   mf->field = nvp6158->format.field;
 
   mutex_lock(&nvp6158->mutex);
 
   if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
       mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
       *mf = fmt->format;
#else
       return -ENOTTY;
#endif
   } else {
       if (nvp6158->streaming) {
           mutex_unlock(&nvp6158->mutex);
           return -EBUSY;
       }
 
       nvp6158->frame_size = size;
       nvp6158->format = fmt->format;
   }
 
   mutex_unlock(&nvp6158->mutex);
   return ret;
}
 
static int nvp6158_g_frame_interval(struct v4l2_subdev *sd,
                   struct v4l2_subdev_frame_interval *fi)
{
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
   const struct nvp6158_framesize *size = nvp6158->frame_size;
 
   fi->interval = size->max_fps;
 
   return 0;
}
 
static void nvp6158_get_module_inf(struct nvp6158 *nvp6158,
                  struct rkmodule_inf *inf)
{
   memset(inf, 0, sizeof(*inf));
   strlcpy(inf->base.sensor, NVP6158_NAME, sizeof(inf->base.sensor));
   strlcpy(inf->base.module, nvp6158->module_name,
       sizeof(inf->base.module));
   strlcpy(inf->base.lens, nvp6158->len_name, sizeof(inf->base.lens));
}
 
static __maybe_unused void
nvp6158_get_bt656_module_inf(struct nvp6158 *nvp6158,
                  struct rkmodule_bt656_mbus_info *inf)
{
   memset(inf, 0, sizeof(*inf));
   inf->flags = RKMODULE_CAMERA_BT656_PARSE_ID_LSB;
   switch (nvp6158->ch_nums) {
   case 1:
       inf->flags |= RKMODULE_CAMERA_BT656_CHANNEL_0;
       break;
   case 2:
       inf->flags |= RKMODULE_CAMERA_BT656_CHANNEL_0 |
                 RKMODULE_CAMERA_BT656_CHANNEL_1;
       break;
   case 4:
       inf->flags |= RKMODULE_CAMERA_BT656_CHANNELS;
       break;
   default:
       inf->flags |= RKMODULE_CAMERA_BT656_CHANNELS;
   }
}
 
static void nvp6158_get_vicap_rst_inf(struct nvp6158 *nvp6158,
                  struct rkmodule_vicap_reset_info *rst_info)
{
   struct i2c_client *client = nvp6158->client;
 
   rst_info->is_reset = nvp6158->hot_plug;
   nvp6158->hot_plug = false;
   rst_info->src = RKCIF_RESET_SRC_ERR_HOTPLUG;
   dev_info(&client->dev, "%s: rst_info->is_reset:%d.\n", __func__, rst_info->is_reset);
}
 
static void nvp6158_set_vicap_rst_inf(struct nvp6158 *nvp6158,
                  struct rkmodule_vicap_reset_info rst_info)
{
   nvp6158->is_reset = rst_info.is_reset;
}
 
static void nvp6158_set_streaming(struct nvp6158 *nvp6158, int on)
{
   struct i2c_client *client = nvp6158->client;
 
 
   dev_info(&client->dev, "%s: on: %d\n", __func__, on);
 
   if (on) {
       //VDO2/VDO1 enabled VCLK_1_EN/VCLK_2_EN
       nvp6158_write(client, 0xFF, 0x01);
       nvp6158_write(client, 0xCA, 0x66);
   } else {
       //VDO2/VDO1 disable VCLK_1/VCLK_2_DISABLE
       nvp6158_write(client, 0xFF, 0x01);
       nvp6158_write(client, 0xCA, 0x00);
   }
}
 
static long nvp6158_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
{
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
   long ret = 0;
   u32 stream = 0;
 
   switch (cmd) {
   case RKMODULE_GET_MODULE_INFO:
       nvp6158_get_module_inf(nvp6158, (struct rkmodule_inf *)arg);
       break;
   case RKMODULE_GET_BT656_MBUS_INFO:
       nvp6158_get_bt656_module_inf(nvp6158,
                          (struct rkmodule_bt656_mbus_info
                       *)arg);
       break;
   case RKMODULE_GET_START_STREAM_SEQ:
       if ((nvp6158->mode > BT656_4MUX) &&
           (nvp6158->mode < NVP6158_DVP_MODES_END))
           *(int *)arg = RKMODULE_START_STREAM_FRONT;
       break;
   case RKMODULE_GET_VICAP_RST_INFO:
       nvp6158_get_vicap_rst_inf(nvp6158, (struct rkmodule_vicap_reset_info *)arg);
       break;
   case RKMODULE_SET_VICAP_RST_INFO:
       nvp6158_set_vicap_rst_inf(nvp6158, *(struct rkmodule_vicap_reset_info *)arg);
       break;
   case RKMODULE_SET_QUICK_STREAM:
       stream = *((u32 *)arg);
       nvp6158_set_streaming(nvp6158, !!stream);
       break;
   default:
       ret = -ENOTTY;
       break;
   }
 
   return ret;
}
 
#ifdef CONFIG_COMPAT
static long nvp6158_compat_ioctl32(struct v4l2_subdev *sd,
                  unsigned int cmd, unsigned long arg)
{
   void __user *up = compat_ptr(arg);
   struct rkmodule_inf *inf;
   struct rkmodule_awb_cfg *cfg;
   long ret;
   struct rkmodule_bt656_mbus_info *bt565_inf;
   int *seq;
   struct rkmodule_vicap_reset_info *vicap_rst_inf;
   u32 stream = 0;
 
   switch (cmd) {
   case RKMODULE_GET_MODULE_INFO:
       inf = kzalloc(sizeof(*inf), GFP_KERNEL);
       if (!inf) {
           ret = -ENOMEM;
           return ret;
       }
 
       ret = nvp6158_ioctl(sd, cmd, inf);
       if (!ret) {
           ret = copy_to_user(up, inf, sizeof(*inf));
           if (ret)
               ret = -EFAULT;
       }
       kfree(inf);
       break;
   case RKMODULE_AWB_CFG:
       cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
       if (!cfg) {
           ret = -ENOMEM;
           return ret;
       }
 
       ret = copy_from_user(cfg, up, sizeof(*cfg));
       if (!ret)
           ret = nvp6158_ioctl(sd, cmd, cfg);
       else
           ret = -EFAULT;
       kfree(cfg);
       break;
   case RKMODULE_GET_BT656_MBUS_INFO:
       bt565_inf = kzalloc(sizeof(*bt565_inf), GFP_KERNEL);
       if (!bt565_inf) {
           ret = -ENOMEM;
           return ret;
       }
 
       ret = nvp6158_ioctl(sd, cmd, bt565_inf);
       if (!ret) {
           ret = copy_to_user(up, bt565_inf, sizeof(*bt565_inf));
           if (ret)
               ret = -EFAULT;
       }
       kfree(bt565_inf);
       break;
   case RKMODULE_GET_START_STREAM_SEQ:
       seq = kzalloc(sizeof(*seq), GFP_KERNEL);
       if (!seq) {
           ret = -ENOMEM;
           return ret;
       }
       ret = nvp6158_ioctl(sd, cmd, seq);
       if (!ret) {
           ret = copy_to_user(up, seq, sizeof(*seq));
           if (ret)
               ret = -EFAULT;
       }
       kfree(seq);
       break;
   case RKMODULE_GET_VICAP_RST_INFO:
       vicap_rst_inf = kzalloc(sizeof(*vicap_rst_inf), GFP_KERNEL);
       if (!vicap_rst_inf) {
           ret = -ENOMEM;
           return ret;
       }
 
       ret = nvp6158_ioctl(sd, cmd, vicap_rst_inf);
       if (!ret) {
           ret = copy_to_user(up, vicap_rst_inf, sizeof(*vicap_rst_inf));
           if (ret)
               ret = -EFAULT;
       }
       kfree(vicap_rst_inf);
       break;
   case RKMODULE_SET_VICAP_RST_INFO:
       vicap_rst_inf = kzalloc(sizeof(*vicap_rst_inf), GFP_KERNEL);
       if (!vicap_rst_inf) {
           ret = -ENOMEM;
           return ret;
       }
 
       ret = copy_from_user(vicap_rst_inf, up, sizeof(*vicap_rst_inf));
       if (!ret)
           ret = nvp6158_ioctl(sd, cmd, vicap_rst_inf);
       else
           ret = -EFAULT;
       kfree(vicap_rst_inf);
       break;
   case RKMODULE_SET_QUICK_STREAM:
       ret = copy_from_user(&stream, up, sizeof(u32));
       if (!ret)
           ret = nvp6158_ioctl(sd, cmd, &stream);
       else
           ret = -EFAULT;
       break;
   default:
       ret = -ENOIOCTLCMD;
       break;
   }
 
   return ret;
}
#endif
 
static int nvp6158_runtime_resume(struct device *dev)
{
   struct i2c_client *client = to_i2c_client(dev);
   struct v4l2_subdev *sd = i2c_get_clientdata(client);
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
 
   return __nvp6158_power_on(nvp6158);
}
 
static int nvp6158_runtime_suspend(struct device *dev)
{
   struct i2c_client *client = to_i2c_client(dev);
   struct v4l2_subdev *sd = i2c_get_clientdata(client);
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
 
   __nvp6158_power_off(nvp6158);
 
   return 0;
}
 
static const struct dev_pm_ops nvp6158_pm_ops = {
   SET_RUNTIME_PM_OPS(nvp6158_runtime_suspend,
              nvp6158_runtime_resume, NULL)
};
 
static const struct v4l2_subdev_video_ops nvp6158_video_ops = {
   .s_stream = nvp6158_stream,
   .querystd = nvp6158_querystd,
   .g_frame_interval = nvp6158_g_frame_interval,
};
 
static const struct v4l2_subdev_pad_ops nvp6158_subdev_pad_ops = {
   .enum_mbus_code = nvp6158_enum_mbus_code,
   .enum_frame_size = nvp6158_enum_frame_sizes,
   .get_fmt = nvp6158_get_fmt,
   .set_fmt = nvp6158_set_fmt,
   .get_selection = nvp6158_get_selection,
   .enum_frame_interval = nvp6158_enum_frame_interval,
   .get_mbus_config = nvp6158_g_mbus_config,
};
 
static const struct v4l2_subdev_core_ops nvp6158_core_ops = {
   .s_power = nvp6158_power,
   .ioctl = nvp6158_ioctl,
#ifdef CONFIG_COMPAT
   .compat_ioctl32 = nvp6158_compat_ioctl32,
#endif
};
 
static const struct v4l2_subdev_ops nvp6158_subdev_ops = {
   .core = &nvp6158_core_ops,
   .video = &nvp6158_video_ops,
   .pad   = &nvp6158_subdev_pad_ops,
};
 
static void get_dvp_mode(struct nvp6158 *nvp6158)
{
   struct device *dev = &nvp6158->client->dev;
   char mode[128];
   u32 i;
 
   sprintf(mode, "%s_%dMUX", nvp6158->dvp_mode, nvp6158->ch_nums);
   dev_info(dev, "combined dvp mode is(%s)\n", mode);
   for (i = 0; i < NVP6158_DVP_MODES_END; i++) {
       if (!strcmp(mode, nvp6158_dvp_mode_lists[i]))
           break;
   }
 
   if (i < NVP6158_DVP_MODES_END)
       nvp6158->mode = i;
   else
       nvp6158->mode = BT656I_TEST_MODES;
   dev_info(dev, "get dvp mode (%s)\n", nvp6158_dvp_mode_lists[nvp6158->mode]);
}
 
static int nvp6158_parse_dts(struct nvp6158 *nvp6158)
{
   int ret;
   int elem_size, elem_index;
   const char *str = "";
   struct property *prop;
   struct nvp6158_regulator *regulator;
   struct device *dev = &nvp6158->client->dev;
   struct device_node *np = of_node_get(dev->of_node);
 
   nvp6158->xvclk = devm_clk_get(dev, "xvclk");
   if (IS_ERR(nvp6158->xvclk)) {
       dev_err(dev, "Failed to get xvclk\n");
       return -EINVAL;
   }
   ret = clk_set_rate(nvp6158->xvclk, NVP6158_XVCLK_FREQ);
   if (ret < 0) {
       dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
       return ret;
   }
   if (clk_get_rate(nvp6158->xvclk) != NVP6158_XVCLK_FREQ)
       dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
 
   nvp6158->pinctrl = devm_pinctrl_get(dev);
   if (!IS_ERR(nvp6158->pinctrl)) {
       nvp6158->pins_default =
           pinctrl_lookup_state(nvp6158->pinctrl,
                        OF_CAMERA_PINCTRL_STATE_DEFAULT);
       if (IS_ERR(nvp6158->pins_default))
           dev_err(dev, "could not get default pinstate\n");
 
       nvp6158->pins_sleep =
           pinctrl_lookup_state(nvp6158->pinctrl,
                        OF_CAMERA_PINCTRL_STATE_SLEEP);
       if (IS_ERR(nvp6158->pins_sleep))
           dev_err(dev, "could not get sleep pinstate\n");
   } else {
       dev_err(dev, "no pinctrl\n");
   }
 
   elem_size = of_property_count_elems_of_size(
       np,
       OF_CAMERA_MODULE_REGULATOR_VOLTAGES,
       sizeof(u32));
   prop = of_find_property(
       np,
       OF_CAMERA_MODULE_REGULATORS,
       NULL);
   if (elem_size > 0 && !IS_ERR_OR_NULL(prop)) {
       nvp6158->regulators.regulator =
           devm_kzalloc(&nvp6158->client->dev,
                    elem_size * sizeof(struct nvp6158_regulator),
                    GFP_KERNEL);
       if (!nvp6158->regulators.regulator)
           dev_err(dev, "could not malloc nvp6158_regulator\n");
 
       nvp6158->regulators.cnt = elem_size;
 
       str = NULL;
       elem_index = 0;
       regulator = nvp6158->regulators.regulator;
       if (regulator) {
           do {
               str = of_prop_next_string(prop, str);
               if (!str) {
                   dev_err(dev, "%s is not match %s in dts\n",
                       OF_CAMERA_MODULE_REGULATORS,
                       OF_CAMERA_MODULE_REGULATOR_VOLTAGES);
                   break;
               }
               regulator->regulator =
                   devm_regulator_get_optional(dev, str);
               if (IS_ERR(regulator->regulator))
                   dev_err(dev, "devm_regulator_get %s failed\n",
                       str);
               of_property_read_u32_index(
                   np,
                   OF_CAMERA_MODULE_REGULATOR_VOLTAGES,
                   elem_index++,
                   &regulator->min_uV);
               regulator->max_uV = regulator->min_uV;
               regulator++;
           } while (--elem_size);
       }
   }
 
   if (of_property_read_string(np,
       RK_CAMERA_MODULE_DVP_MODE,
       &nvp6158->dvp_mode)) {
       nvp6158->dvp_mode = NVP6158_DEFAULT_DVP_MODE;
       dev_warn(dev,
           "can not get module %s from dts, use default(%s)!\n",
           RK_CAMERA_MODULE_DVP_MODE,
           NVP6158_DEFAULT_DVP_MODE);
   } else {
       dev_info(dev,
           "get module %s from dts, dvp mode(%s)!\n",
           RK_CAMERA_MODULE_DVP_MODE, nvp6158->dvp_mode);
   }
 
   if (of_property_read_u32(np,
       RK_CAMERA_MODULE_CHANNEL_NUMS,
       &nvp6158->ch_nums)) {
       nvp6158->ch_nums = NVP6158_DEFAULT_CHANNEL_NUMS;
       dev_warn(dev,
           "can not get module %s from dts, use default(%d)!\n",
           RK_CAMERA_MODULE_CHANNEL_NUMS,
           NVP6158_DEFAULT_CHANNEL_NUMS);
   } else {
       dev_info(dev,
           "get module %s from dts, channel_nums(%d)!\n",
           RK_CAMERA_MODULE_DVP_MODE, nvp6158->ch_nums);
   }
 
   if (of_property_read_u32(np,
       RK_CAMERA_MODULE_DUAL_EDGE,
       &nvp6158->dual_edge)) {
       nvp6158->dual_edge = NVP6158_DEFAULT_DUAL_EDGE;
       dev_warn(dev,
           "can not get module %s from dts, use default(%d)!\n",
           RK_CAMERA_MODULE_DUAL_EDGE,
           NVP6158_DEFAULT_DUAL_EDGE);
   } else {
       dev_info(dev,
           "get module %s from dts, dual_edge(%d)!\n",
           RK_CAMERA_MODULE_DUAL_EDGE, nvp6158->dual_edge);
   }
 
 
 
   if (of_property_read_u32_array(np,
       RK_CAMERA_MODULE_DEFAULT_RECT,
       (unsigned int *)&nvp6158->defrect, 2)) {
       nvp6158->defrect.width = NVP6158_DEFAULT_WIDTH;
       nvp6158->defrect.height = NVP6158_DEFAULT_HEIGHT;
       dev_warn(dev,
           "can not get module %s from dts, use default wxh(%dx%d)!\n",
           RK_CAMERA_MODULE_DEFAULT_RECT,
           NVP6158_DEFAULT_WIDTH, NVP6158_DEFAULT_HEIGHT);
   } else {
       dev_info(dev,
           "get module %s from dts, wxh(%dx%d)!\n",
           RK_CAMERA_MODULE_DEFAULT_RECT,
           nvp6158->defrect.width,
           nvp6158->defrect.height);
   }
 
   /* AHD_PWR_EN */
   nvp6158->pwr_gpio = devm_gpiod_get(dev, "pwr", GPIOD_OUT_LOW);
   if (IS_ERR(nvp6158->pwr_gpio))
       dev_warn(dev, "can not find pd-gpios, error %ld\n",
            PTR_ERR(nvp6158->pwr_gpio));
   /* AHD CAM_PWR_EN*/
   nvp6158->pwr2_gpio = devm_gpiod_get(dev, "pwr2", GPIOD_OUT_LOW);
   if (IS_ERR(nvp6158->pwr2_gpio))
       dev_warn(dev, "can not find pd2-gpios, error %ld\n",
            PTR_ERR(nvp6158->pwr2_gpio));
 
   nvp6158->rst_gpio = devm_gpiod_get(dev, "rst", GPIOD_OUT_LOW);
   if (IS_ERR(nvp6158->rst_gpio))
       dev_warn(dev, "can not find rst-gpios, error %ld\n",
            PTR_ERR(nvp6158->rst_gpio));
 
   nvp6158->rst2_gpio = devm_gpiod_get(dev, "rst2", GPIOD_OUT_LOW);
   if (IS_ERR(nvp6158->rst2_gpio))
       dev_warn(dev, "can not find rst2-gpios, error %ld\n",
            PTR_ERR(nvp6158->rst2_gpio));
 
   nvp6158->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
   if (IS_ERR(nvp6158->pwdn_gpio))
       dev_warn(dev, "can not find pwd-gpios, error %ld\n",
            PTR_ERR(nvp6158->pwdn_gpio));
 
   nvp6158->pwdn2_gpio = devm_gpiod_get(dev, "pwdn2", GPIOD_OUT_LOW);
   if (IS_ERR(nvp6158->pwdn2_gpio))
       dev_warn(dev, "can not find pwd2-gpios, error %ld\n",
            PTR_ERR(nvp6158->pwdn2_gpio));
 
   return 0;
}
 
 
static int nvp6158_probe(struct i2c_client *client,
            const struct i2c_device_id *id)
{
   struct device *dev = &client->dev;
   struct device_node *node = dev->of_node;
   struct nvp6158 *nvp6158;
   struct v4l2_subdev *sd;
   __maybe_unused char facing[2];
   int ret, index;
 
   dev_info(dev, "driver version: %02x.%02x.%02x",
        DRIVER_VERSION >> 16,
        (DRIVER_VERSION & 0xff00) >> 8,
        DRIVER_VERSION & 0x00ff);
 
   nvp6158 = devm_kzalloc(dev, sizeof(*nvp6158), GFP_KERNEL);
   if (!nvp6158)
       return -ENOMEM;
 
   ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
                  &nvp6158->module_index);
   ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
                      &nvp6158->module_facing);
   ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
                      &nvp6158->module_name);
   ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
                      &nvp6158->len_name);
   if (ret) {
       dev_err(dev, "could not get %s!\n", RKMODULE_CAMERA_LENS_NAME);
       return -EINVAL;
   }
 
   nvp6158->client = client;
 
   ret = nvp6158_parse_dts(nvp6158);
   if (ret) {
       dev_err(dev, "Failed to analyze dts\n");
       return ret;
   }
   get_dvp_mode(nvp6158);
 
   mutex_init(&nvp6158->mutex);
   nvp6158_get_default_format(nvp6158);
 
   sd = &nvp6158->subdev;
   v4l2_i2c_subdev_init(sd, client, &nvp6158_subdev_ops);
   ret = nvp6158_initialize_controls(nvp6158);
   if (ret)
       goto err_destroy_mutex;
 
   __nvp6158_power_on(nvp6158);
   ret = nvp6158_init(i2c_adapter_id(client->adapter));
   if (ret) {
       dev_err(dev, "Failed to init nvp6158\n");
       goto err_power_off;
   }
 
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
   sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
#endif
 
#if defined(CONFIG_MEDIA_CONTROLLER)
   for (index = 0; index < nvp6158->ch_nums; index++)
       nvp6158->pad[index].flags = MEDIA_PAD_FL_SOURCE;
   sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
   ret = media_entity_pads_init(&sd->entity, nvp6158->ch_nums, nvp6158->pad);
   if (ret < 0)
       goto err_power_off;
#endif
 
   memset(facing, 0, sizeof(facing));
   if (strcmp(nvp6158->module_facing, "back") == 0)
       facing[0] = 'b';
   else
       facing[0] = 'f';
 
   snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
        nvp6158->module_index, facing,
        NVP6158_NAME, dev_name(sd->dev));
 
   ret = v4l2_async_register_subdev_sensor_common(sd);
   if (ret) {
       dev_err(dev, "v4l2 async register subdev failed\n");
       goto err_clean_entity;
   }
   pm_runtime_set_active(dev);
   pm_runtime_enable(dev);
   pm_runtime_idle(dev);
#ifdef WORK_QUEUE
   /* init work_queue for state_check */
   INIT_DELAYED_WORK(&nvp6158->plug_state_check.d_work, nvp6158_plug_state_check_work);
   nvp6158->plug_state_check.state_check_wq =
       create_singlethread_workqueue("nvp6158_work_queue");
   if (nvp6158->plug_state_check.state_check_wq == NULL) {
       dev_err(dev, "%s(%d): %s create failed.\n", __func__, __LINE__,
             "nvp6158_work_queue");
   }
   nvp6158->cur_detect_status = 0x0;
   nvp6158->last_detect_status = 0x0;
   nvp6158->hot_plug = false;
   nvp6158->is_reset = 0;
 
#endif
   return 0;
 
err_clean_entity:
#if defined(CONFIG_MEDIA_CONTROLLER)
   media_entity_cleanup(&sd->entity);
#endif
err_power_off:
   __nvp6158_power_off(nvp6158);
err_destroy_mutex:
   mutex_destroy(&nvp6158->mutex);
 
   return ret;
}
 
static int nvp6158_remove(struct i2c_client *client)
{
   struct v4l2_subdev *sd = i2c_get_clientdata(client);
   struct nvp6158 *nvp6158 = to_nvp6158(sd);
 
   nvp6158_exit();
   v4l2_ctrl_handler_free(&nvp6158->ctrl_handler);
   mutex_destroy(&nvp6158->mutex);
 
   pm_runtime_disable(&client->dev);
   if (!pm_runtime_status_suspended(&client->dev))
       __nvp6158_power_off(nvp6158);
   pm_runtime_set_suspended(&client->dev);
#ifdef WORK_QUEUE
   if (nvp6158->plug_state_check.state_check_wq != NULL)
       destroy_workqueue(nvp6158->plug_state_check.state_check_wq);
#endif
   return 0;
}
 
#if IS_ENABLED(CONFIG_OF)
static const struct of_device_id nvp6158_of_match[] = {
   { .compatible = "nvp6158-v4l2" },
   {},
};
MODULE_DEVICE_TABLE(of, nvp6158_of_match);
#endif
 
static const struct i2c_device_id nvp6158_match_id[] = {
   { "nvp6158-v4l2", 0 },
   { },
};
 
static struct i2c_driver nvp6158_i2c_driver = {
   .driver = {
       .name = NVP6158_NAME,
       .pm = &nvp6158_pm_ops,
       .of_match_table = of_match_ptr(nvp6158_of_match),
   },
   .probe        = &nvp6158_probe,
   .remove        = &nvp6158_remove,
   .id_table    = nvp6158_match_id,
};
 
static int __init sensor_mod_init(void)
{
   return i2c_add_driver(&nvp6158_i2c_driver);
}
 
static void __exit sensor_mod_exit(void)
{
   i2c_del_driver(&nvp6158_i2c_driver);
}
 
device_initcall_sync(sensor_mod_init);
module_exit(sensor_mod_exit);
 
MODULE_DESCRIPTION("nvp6158 sensor driver");
MODULE_LICENSE("GPL v2");