-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvd1943.c
More file actions
2513 lines (2197 loc) · 68.3 KB
/
vd1943.c
File metadata and controls
2513 lines (2197 loc) · 68.3 KB
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
// SPDX-License-Identifier: GPL-2.0
/*
* A V4L2 driver for ST VD1943 RGB-NIR Image Sensor.
* Copyright (C) 2024, STMicroelectronics SA
*/
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/iopoll.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <media/v4l2-async.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-event.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-subdev.h>
/* Backward compatibility */
#include <linux/version.h>
#if KERNEL_VERSION(6, 12, 0) > LINUX_VERSION_CODE
#include <asm/unaligned.h>
#else
#include <linux/unaligned.h>
#endif
#if KERNEL_VERSION(6, 9, 0) > LINUX_VERSION_CODE
int vd1943_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,
unsigned int num_of_fw_link_freqs,
const s64 *driver_link_freqs,
unsigned int num_of_driver_link_freqs,
unsigned long *bitmap)
{
unsigned int i;
*bitmap = 0;
if (!num_of_fw_link_freqs) {
dev_err(dev, "no link frequencies in firmware\n");
return -ENODATA;
}
for (i = 0; i < num_of_fw_link_freqs; i++) {
unsigned int j;
for (j = 0; j < num_of_driver_link_freqs; j++) {
if (fw_link_freqs[i] != driver_link_freqs[j])
continue;
dev_dbg(dev, "enabling link frequency %lld Hz\n",
driver_link_freqs[j]);
*bitmap |= BIT(j);
break;
}
}
if (!*bitmap) {
dev_err(dev, "no matching link frequencies found\n");
dev_dbg(dev, "specified in firmware:\n");
for (i = 0; i < num_of_fw_link_freqs; i++)
dev_dbg(dev, "\t%llu Hz\n", fw_link_freqs[i]);
dev_dbg(dev, "driver supported:\n");
for (i = 0; i < num_of_driver_link_freqs; i++)
dev_dbg(dev, "\t%lld Hz\n", driver_link_freqs[i]);
return -ENOENT;
}
return 0;
}
#endif
#if KERNEL_VERSION(6, 8, 0) > LINUX_VERSION_CODE
/*
* Warning : CCI_REGxy_LE definitions doesn't fit exactly with v4l2-cci.h .
* In fact endianness is managed directly in vd1943_read/write() functions.
*/
#include <linux/bitfield.h>
#define CCI_REG_ADDR_MASK GENMASK(15, 0)
#define CCI_REG_WIDTH_SHIFT 16
#define CCI_REG_ADDR(x) FIELD_GET(CCI_REG_ADDR_MASK, x)
#define CCI_REG8(x) ((1 << CCI_REG_WIDTH_SHIFT) | (x))
#define CCI_REG16_LE(x) ((2 << CCI_REG_WIDTH_SHIFT) | (x))
#define CCI_REG32_LE(x) ((4 << CCI_REG_WIDTH_SHIFT) | (x))
#else
#include <media/v4l2-cci.h>
#endif
#if KERNEL_VERSION(5, 18, 0) > LINUX_VERSION_CODE
#define MIPI_CSI2_DT_RAW8 0x2a
#define MIPI_CSI2_DT_RAW10 0x2b
#define MIPI_CSI2_DT_RAW12 0x2c
#else
#include <media/mipi-csi2.h>
#endif
#if KERNEL_VERSION(5, 15, 0) > LINUX_VERSION_CODE
#define HZ_PER_MHZ 1000000UL
#define MEGA 1000000UL
#else
#include <linux/units.h>
#endif
#if KERNEL_VERSION(6, 10, 0) > LINUX_VERSION_CODE
#define MEDIA_BUS_FMT_RGBNIR4X4_1X8 0x3021
#define MEDIA_BUS_FMT_RGBNIR4X4_1X10 0x3022
#define MEDIA_BUS_FMT_RGBNIR4X4_1X12 0x3023
#endif
/* Register Map */
#define VD1943_REG_MODEL_ID CCI_REG32_LE(0x0000)
#define VD1943_MODEL_ID_1_3 0x53393430
#define VD1943_MODEL_ID_1_4 0x53393431
#define VD1943_REG_ROM_REVISION CCI_REG16_LE(0x000c)
#define VD1943_ROM_1_3 0x400
#define VD1943_ROM_1_4 0x540
#define VD1943_REG_CFA_SELECTION CCI_REG16_LE(0x000e)
#define VD1943_OPTICAL_RGBIR 0x00
#define VD1943_OPTICAL_MONO 0x01
#define VD1943_REG_SYSTEM_FSM CCI_REG8(0x0044)
#define VD1943_SYSTEM_FSM_SYSTEM_UP 0x01
#define VD1943_SYSTEM_FSM_BOOT 0x02
#define VD1943_SYSTEM_FSM_SW_STBY 0x03
#define VD1943_SYSTEM_FSM_STREAMING 0x04
#define VD1943_REG_TEMPERATURE CCI_REG16_LE(0x006a)
#define VD1943_REG_SYSTEM_UP CCI_REG8(0x0514)
#define VD1943_CMD_ACK 0x00
#define VD1943_CMD_START_SENSOR 0x01
#define VD1943_REG_BOOT CCI_REG8(0x0515)
#define VD1943_CMD_LOAD_CERTIFICATE 0x01
#define VD1943_CMD_LOAD_FWP 0x02
#define VD1943_CMD_END_BOOT 0x10
#define VD1943_REG_SW_STBY CCI_REG8(0x0516)
#define VD1943_CMD_START_STREAMING 0x01
#define VD1943_CMD_THSENS_READ 0x02
#define VD1943_CMD_UPDATE_VT_RAM_START 0x03
#define VD1943_CMD_UPDATE_VT_RAM_END 0x04
#define VD1943_REG_STREAMING CCI_REG8(0x0517)
#define VD1943_CMD_STOP_STREAM 0x01
#define VD1943_REG_EXT_CLOCK CCI_REG32_LE(0x0734)
#define VD1943_REG_MIPI_DATA_RATE CCI_REG32_LE(0x0738)
#define VD1943_REG_LANE_NB_SEL CCI_REG8(0x0743)
#define VD1943_LANES_NB_4 0
#define VD1943_LANES_NB_2 1
#define VD1943_REG_ROI_WIDTH_OFFSET CCI_REG16_LE(0x090c)
#define VD1943_REG_ROI_HEIGHT_OFFSET CCI_REG16_LE(0x090e)
#define VD1943_REG_ROI_WIDTH CCI_REG16_LE(0x0910)
#define VD1943_REG_ROI_HEIGHT CCI_REG16_LE(0x0912)
#define VD1943_REG_ROI_DT CCI_REG8(0x0914)
#define VD1943_REG_LINE_LENGTH CCI_REG16_LE(0x0934)
#define VD1943_REG_ORIENTATION CCI_REG8(0x0937)
#define VD1943_REG_PATGEN_CTRL CCI_REG8(0x0938)
#define VD1943_REG_OIF_LANE_PHY_MAP CCI_REG8(0x093a)
#define VD1943_REG_OIF_LANE_PHY_SWAP CCI_REG8(0x093b)
#define VD1943_REG_VT_CTRL CCI_REG8(0x0ac6)
#define VD1943_REG_OIF_ISL_ENABLE CCI_REG8(0x0ac7)
#define VD1943_REG_GPIO_0_CTRL CCI_REG8(0x0ad4)
#define VD1943_GPIOX_STROBE_MODE 0x00
#define VD1943_GPIOX_GPIO_IN 0x03
#define VD1943_GPIOX_FSYNC_IN 0x05
#define VD1943_REG_DARKCAL_ENABLE CCI_REG8(0x0af3)
#define VD1943_REG_SENSOR_CONFIGURATION CCI_REG32_LE(0x0b40)
#define VD1943_CONFIG_GS_NATIVE_RAW8 0x01
#define VD1943_CONFIG_GS_NATIVE_RAW10 0x02
#define VD1943_CONFIG_GS_RGB_RAW8 0x05
#define VD1943_CONFIG_GS_RGB_RAW10 0x06
#define VD1943_CONFIG_GS_IR_RAW8 0x0f
#define VD1943_CONFIG_GS_IR_RAW10 0x10
#define VD1943_CONFIG_RS_NATIVE_RAW8 0x1a
#define VD1943_CONFIG_RS_NATIVE_RAW10 0x1b
#define VD1943_CONFIG_RS_NATIVE_RAW12 0x1c
#define VD1943_CONFIG_RS_RGB_RAW8 0x1d
#define VD1943_CONFIG_RS_RGB_RAW10 0x1e
#define VD1943_CONFIG_RS_RGB_RAW12 0x1f
#define VD1943_REG_FRAME_LENGTH CCI_REG16_LE(0x0b46)
#define VD1943_REG_GPIO_CTRL CCI_REG8(0x0b49)
#define VD1943_REG_DARKCAL_PEDESTAL CCI_REG16_LE(0x0b6e)
#define VD1943_REG_ANALOG_GAIN CCI_REG8(0x0c79)
#define VD1943_REG_INTEGRATION_TIME_PRIMARY CCI_REG16_LE(0x0c7a)
#define VD1943_REG_INTEGRATION_TIME_IR CCI_REG16_LE(0x0c7c)
#define VD1943_REG_INTEGRATION_TIME_SHORT CCI_REG16_LE(0x0c7e)
#define VD1943_REG_DIGITAL_GAIN_R CCI_REG16_LE(0x0c80)
#define VD1943_REG_DIGITAL_GAIN_G CCI_REG16_LE(0x0c82)
#define VD1943_REG_DIGITAL_GAIN_B CCI_REG16_LE(0x0c84)
#define VD1943_REG_DIGITAL_GAIN_IR CCI_REG16_LE(0x0c86)
/*
* The VD1943/VD5943 pixel array are organized as follows:
*
* +--------------------------------------+
* | | \
* | +------------------------------+ | |
* | | | | |
* | | | | |
* | | | | |
* | | Default resolution | | | Native height (1984)
* | | 1920 x 1080 | | |
* | | | | |
* | | | | |
* | +------------------------------+ | |
* | | /
* +--------------------------------------+
* <------------------------------------>
* \------------------- Native width (2560)
*
* The native resolution is 2560x1984.
* The recommended/default resolution is 1920x1080.
*/
#define VD1943_NATIVE_WIDTH 2560
#define VD1943_NATIVE_HEIGHT 1984
#define VD1943_DEFAULT_WIDTH 1920
#define VD1943_DEFAULT_HEIGHT 1080
#define VD1943_DEFAULT_MODE 3
/* vd1943/vd5943 is a combined Rolling and Global Shutter sensor */
#define VD1943_GS_MODE 0
#define VD1943_RS_MODE 1
/* Line length and Frame length (settings are for standard 10bits ADC mode) */
#define VD1943_LINE_LENGTH_MIN 3375
#define VD1943_VBLANK_MIN 238U
#define VD1943_FRAME_LENGTH_MAX 0xffff
#define VD1943_DEFAULT_FPS30 30
/* Exposure settings */
#define VD1943_EXPOSURE_MARGIN 27
#define VD1943_EXPOSURE_DEFAULT 840U
/* Output Interface settings */
#define VD1943_MAX_CSI_DATA_LANES 4
/* GPIOs */
#define VD1943_NB_GPIOS 4
/* parse-SNIP: Custom-CIDs */
#define V4L2_CID_TEMPERATURE (V4L2_CID_USER_BASE | 0x1020)
#define V4L2_CID_DARKCAL_PEDESTAL (V4L2_CID_USER_BASE | 0x1024)
#define V4L2_CID_SLAVE_MODE (V4L2_CID_USER_BASE | 0x1025)
#define V4L2_CID_SHUTTER_MODE (V4L2_CID_USER_BASE | 0x1026)
/* parse-SNAP: */
#include "vd1943_fmwpatch_vd1943.c"
#include "vd1943_fmwpatch_vd5943.c"
#include "vd1943_vtpatch.c"
/* regulator supplies */
static const char *const vd1943_supply_names[] = {
"vcore",
"vddio",
"vana",
};
/* -----------------------------------------------------------------------------
* Models, Modes and formats
*/
enum vd1943_models {
VD1943_MODEL_VD1943, // RGBNir variant
VD1943_MODEL_VD5943, // Mono variant
};
struct vd1943_mode {
u32 width;
u32 height;
};
/**
* DOC: Supported Modes
*
* The vd1943/vd5943 driver supports 7 modes described below :
*
* ======= ======== ====================
* Width Height Comment
* ======= ======== ====================
* 2560 1984 Native resolution
* 2560 1920 QHD
* 2560 1600
* 2560 1440
* 1920 1080 Default resolution
* 1280 720
* 640 480
* ======= ======== ====================
*
* Depending of the configured shutter mode (Rolling vs Global), the max frame
* rate can be different. In full resolution the hardware supports up to :
* - 50 fps in Rolling Shutter
* - 100 fps in Global Shutter (depending on MIPI bandwidth)
*
* Note that in lower resolution, framerate can be higher.
*/
static const struct vd1943_mode vd1943_supported_modes[] = {
{
.width = VD1943_NATIVE_WIDTH,
.height = VD1943_NATIVE_HEIGHT,
},
{
.width = VD1943_NATIVE_WIDTH,
.height = 1920,
},
{
.width = VD1943_NATIVE_WIDTH,
.height = 1600,
},
{
.width = VD1943_NATIVE_WIDTH,
.height = 1440,
},
{
.width = VD1943_DEFAULT_WIDTH,
.height = VD1943_DEFAULT_HEIGHT,
},
{
.width = 1280,
.height = 720,
},
{
.width = 640,
.height = 480,
},
};
struct vd1943_config {
u8 sensor_conf;
unsigned int mbus_code;
};
static const struct vd1943_config vdx941_configs[2][2][4] = {
{
/* VD1943_MODEL_VD1943 : RGBNir variant */
{
/* Global Shutter modes */
{
.sensor_conf = VD1943_CONFIG_GS_IR_RAW8,
.mbus_code = MEDIA_BUS_FMT_Y8_1X8,
},
{
.sensor_conf = VD1943_CONFIG_GS_IR_RAW10,
.mbus_code = MEDIA_BUS_FMT_Y10_1X10,
},
{
.sensor_conf = VD1943_CONFIG_GS_RGB_RAW8,
.mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
},
{
.sensor_conf = VD1943_CONFIG_GS_RGB_RAW10,
.mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
},
/* TODO : Custom mbus codes break libcamera apps */
/*{
* .sensor_conf = VD1943_CONFIG_GS_NATIVE_RAW8,
* .mbus_code = MEDIA_BUS_FMT_RGBNIR4X4_1X8,
*},
*{
* .sensor_conf = VD1943_CONFIG_GS_NATIVE_RAW10,
* .mbus_code = MEDIA_BUS_FMT_RGBNIR4X4_1X10,
*},
*/
},
{
/* Rolling Shutter modes */
{
.sensor_conf = VD1943_CONFIG_RS_RGB_RAW8,
.mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
},
{
.sensor_conf = VD1943_CONFIG_RS_RGB_RAW10,
.mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
},
{
.sensor_conf = VD1943_CONFIG_RS_RGB_RAW12,
.mbus_code = MEDIA_BUS_FMT_SGBRG12_1X12,
},
/* TODO : Custom mbus codes break libcamera apps */
/*{
* .sensor_conf = VD1943_CONFIG_RS_NATIVE_RAW8,
* .mbus_code = MEDIA_BUS_FMT_RGBNIR4X4_1X8,
*},
*{
* .sensor_conf = VD1943_CONFIG_RS_NATIVE_RAW10,
* .mbus_code = MEDIA_BUS_FMT_RGBNIR4X4_1X10,
*},
*{
* .sensor_conf = VD1943_CONFIG_RS_NATIVE_RAW12,
* .mbus_code = MEDIA_BUS_FMT_RGBNIR4X4_1X12,
*},
*/
},
},
{
/* VD1943_MODEL_VD5943 : Mono variant */
{
/* Global Shutter modes */
{
.sensor_conf = VD1943_CONFIG_GS_NATIVE_RAW8,
.mbus_code = MEDIA_BUS_FMT_Y8_1X8,
},
{
.sensor_conf = VD1943_CONFIG_GS_NATIVE_RAW10,
.mbus_code = MEDIA_BUS_FMT_Y10_1X10,
},
},
{
/* Rolling Shutter modes */
{
.sensor_conf = VD1943_CONFIG_RS_NATIVE_RAW8,
.mbus_code = MEDIA_BUS_FMT_Y8_1X8,
},
{
.sensor_conf = VD1943_CONFIG_RS_NATIVE_RAW10,
.mbus_code = MEDIA_BUS_FMT_Y10_1X10,
},
{
.sensor_conf = VD1943_CONFIG_RS_NATIVE_RAW12,
.mbus_code = MEDIA_BUS_FMT_Y12_1X12,
},
},
},
};
#if KERNEL_VERSION(6, 8, 0) > LINUX_VERSION_CODE
/* Big endian register addresses and 8b, 16b or 32b little endian values. */
static const struct regmap_config vd1943_regmap_config = {
.reg_bits = 16,
.val_bits = 8,
.reg_format_endian = REGMAP_ENDIAN_BIG,
};
#endif
struct vd1943 {
struct i2c_client *i2c_client;
struct v4l2_subdev sd;
struct media_pad pad;
struct regulator_bulk_data supplies[ARRAY_SIZE(vd1943_supply_names)];
struct gpio_desc *reset_gpio;
struct clk *xclk;
struct regmap *regmap;
u32 xclk_freq;
unsigned long link_freq_bitmap;
u32 pixel_rate;
u8 nb_of_lane;
u64 mipi_bandwidth;
u8 oif_lane_phy_map;
u8 oif_lane_phy_swap;
u32 gpios[VD1943_NB_GPIOS];
u8 ext_vt_sync;
unsigned long ext_leds_mask;
enum vd1943_models model;
bool is_fastboot;
#if KERNEL_VERSION(5, 19, 0) > LINUX_VERSION_CODE
/* lock to protect all members below */
struct mutex lock;
#endif
struct v4l2_ctrl_handler ctrl_handler;
struct v4l2_ctrl *vt_pixrate_ctrl;
struct v4l2_ctrl *hblank_ctrl;
struct v4l2_ctrl *vblank_ctrl;
struct {
struct v4l2_ctrl *hflip_ctrl;
struct v4l2_ctrl *vflip_ctrl;
};
struct v4l2_ctrl *patgen_ctrl;
struct v4l2_ctrl *expo_ctrl;
struct v4l2_ctrl *again_ctrl;
struct v4l2_ctrl *dgain_ctrl;
struct v4l2_ctrl *slave_ctrl;
struct v4l2_ctrl *led_ctrl;
struct v4l2_ctrl *shutter_ctrl;
struct v4l2_ctrl *pedestal_ctrl;
bool streaming;
#if KERNEL_VERSION(5, 19, 0) > LINUX_VERSION_CODE
struct v4l2_mbus_framefmt active_fmt;
struct v4l2_rect active_crop;
#endif
};
static inline struct vd1943 *to_vd1943(struct v4l2_subdev *sd)
{
#if KERNEL_VERSION(6, 2, 0) > LINUX_VERSION_CODE
return container_of(sd, struct vd1943, sd);
#else
return container_of_const(sd, struct vd1943, sd);
#endif
}
static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
{
#if KERNEL_VERSION(6, 2, 0) > LINUX_VERSION_CODE
return &container_of(ctrl->handler, struct vd1943, ctrl_handler)->sd;
#else
return &container_of_const(ctrl->handler, struct vd1943, ctrl_handler)
->sd;
#endif
}
/* -----------------------------------------------------------------------------
* HW access : Big endian reg addresses and 8b, 16b or 32b little endian values
*/
#if KERNEL_VERSION(6, 8, 0) > LINUX_VERSION_CODE
static int vd1943_read(struct vd1943 *sensor, u32 reg, u32 *val, int *err)
{
struct i2c_client *client = sensor->i2c_client;
unsigned int len = (reg >> CCI_REG_WIDTH_SHIFT) & 7;
u8 buf[4];
int ret;
if (err && *err)
return *err;
reg = reg & CCI_REG_ADDR_MASK;
ret = regmap_bulk_read(sensor->regmap, reg, buf, len);
if (ret) {
dev_err(&client->dev, "%s: Error reading reg 0x%4x: %d\n",
__func__, reg, ret);
goto out;
}
switch (len) {
case 1:
*val = buf[0];
break;
case 2:
*val = get_unaligned_le16(buf);
break;
case 4:
*val = get_unaligned_le32(buf);
break;
default:
dev_err(&client->dev,
"%s: Error invalid reg-width %u for reg 0x%04x\n",
__func__, len, reg);
ret = -EINVAL;
break;
}
dev_dbg(&client->dev, "%s: reg 0x%04x -> %d\n", __func__, reg, *val);
out:
if (ret && err)
*err = ret;
return ret;
}
static int vd1943_write(struct vd1943 *sensor, u32 reg, u32 val, int *err)
{
struct i2c_client *client = sensor->i2c_client;
unsigned int len = (reg >> CCI_REG_WIDTH_SHIFT) & 7;
u8 buf[4];
int ret;
if (err && *err)
return *err;
reg = reg & CCI_REG_ADDR_MASK;
switch (len) {
case 1:
buf[0] = val;
break;
case 2:
put_unaligned_le16(val, buf);
break;
case 4:
put_unaligned_le32(val, buf);
break;
default:
dev_err(&client->dev,
"%s: Error invalid reg-width %u for reg 0x%04x\n",
__func__, len, reg);
ret = -EINVAL;
goto out;
}
ret = regmap_bulk_write(sensor->regmap, reg, buf, len);
if (ret)
dev_err(&client->dev, "%s: Error writing reg 0x%04x: %d\n",
__func__, reg, ret);
dev_dbg(&client->dev, "%s: reg 0x%04x <- %d\n", __func__, reg, val);
out:
if (ret && err)
*err = ret;
return ret;
}
#else
#define vd1943_read(sensor, reg, val, err) \
cci_read((sensor)->regmap, reg, (u64 *)val, err)
#define vd1943_write(sensor, reg, val, err) \
cci_write((sensor)->regmap, reg, (u64)val, err)
#endif
static int vd1943_write_array(struct vd1943 *sensor, u32 reg, unsigned int len,
const u8 *array, int *err)
{
unsigned int chunk_sz = 1024;
unsigned int sz;
int ret;
if (err && *err)
return *err;
/*
* This loop isn't necessary but in certains conditions (platforms, cpu
* load, etc.) it has been observed that the bulk write could timeout.
*/
while (len) {
sz = min(len, chunk_sz);
ret = regmap_bulk_write(sensor->regmap, reg, array, sz);
if (ret < 0)
goto out;
len -= sz;
reg += sz;
array += sz;
}
out:
if (ret && err)
*err = ret;
return ret;
}
static int vd1943_poll_reg(struct vd1943 *sensor, u32 reg, u8 poll_val,
int *err)
{
unsigned int val = 0;
int ret;
if (err && *err)
return *err;
ret = regmap_read_poll_timeout(sensor->regmap, CCI_REG_ADDR(reg), val,
(val == poll_val), 2000,
500 * USEC_PER_MSEC);
if (ret && err)
*err = ret;
return ret;
}
static int vd1943_wait_state(struct vd1943 *sensor, int state, int *err)
{
return vd1943_poll_reg(sensor, VD1943_REG_SYSTEM_FSM, state, err);
}
/* -----------------------------------------------------------------------------
* Controls: definitions, helpers and handlers
*/
static const char *const vd1943_tp_menu[] = { "Disabled", "Dgrey" };
static const char *const vd1943_shutter_menu[] = { "Global Shutter",
"Rolling Shutter" };
static const s64 vd1943_link_freq[] = { 375000000UL, 500000000UL, 650000000UL,
750000000UL };
static u8 vd1943_get_datatype(__u32 code)
{
/* TODO : Correctly handle MEDIA_BUS_FMT_RGBNIR4X4 mbus codes */
switch (code) {
case MEDIA_BUS_FMT_Y8_1X8:
case MEDIA_BUS_FMT_SGRBG8_1X8:
case MEDIA_BUS_FMT_SRGGB8_1X8:
case MEDIA_BUS_FMT_SBGGR8_1X8:
case MEDIA_BUS_FMT_SGBRG8_1X8:
default:
return MIPI_CSI2_DT_RAW8;
case MEDIA_BUS_FMT_Y10_1X10:
case MEDIA_BUS_FMT_SGRBG10_1X10:
case MEDIA_BUS_FMT_SRGGB10_1X10:
case MEDIA_BUS_FMT_SBGGR10_1X10:
case MEDIA_BUS_FMT_SGBRG10_1X10:
return MIPI_CSI2_DT_RAW10;
case MEDIA_BUS_FMT_Y12_1X12:
case MEDIA_BUS_FMT_SGRBG12_1X12:
case MEDIA_BUS_FMT_SRGGB12_1X12:
case MEDIA_BUS_FMT_SBGGR12_1X12:
case MEDIA_BUS_FMT_SGBRG12_1X12:
return MIPI_CSI2_DT_RAW12;
}
}
static u8 vd1943_get_bpp(__u32 code)
{
/* TODO : Correctly handle MEDIA_BUS_FMT_RGBNIR4X4 mbus codes */
switch (code) {
case MEDIA_BUS_FMT_Y8_1X8:
case MEDIA_BUS_FMT_SGRBG8_1X8:
case MEDIA_BUS_FMT_SRGGB8_1X8:
case MEDIA_BUS_FMT_SBGGR8_1X8:
case MEDIA_BUS_FMT_SGBRG8_1X8:
default:
return 8;
case MEDIA_BUS_FMT_Y10_1X10:
case MEDIA_BUS_FMT_SGRBG10_1X10:
case MEDIA_BUS_FMT_SRGGB10_1X10:
case MEDIA_BUS_FMT_SBGGR10_1X10:
case MEDIA_BUS_FMT_SGBRG10_1X10:
return 10;
case MEDIA_BUS_FMT_Y12_1X12:
case MEDIA_BUS_FMT_SGRBG12_1X12:
case MEDIA_BUS_FMT_SRGGB12_1X12:
case MEDIA_BUS_FMT_SBGGR12_1X12:
case MEDIA_BUS_FMT_SGBRG12_1X12:
return 12;
}
}
static int vd1943_get_temp_stream_enable(struct vd1943 *sensor, int *temp)
{
return vd1943_read(sensor, VD1943_REG_TEMPERATURE, temp, NULL);
}
static int vd1943_get_temp_stream_disable(struct vd1943 *sensor, int *temp)
{
int ret = 0;
/* request temperature read */
vd1943_write(sensor, VD1943_REG_SW_STBY, VD1943_CMD_THSENS_READ, &ret);
vd1943_poll_reg(sensor, VD1943_REG_SW_STBY, VD1943_CMD_ACK, &ret);
if (ret)
return ret;
return vd1943_get_temp_stream_enable(sensor, temp);
}
static int vd1943_get_temp(struct vd1943 *sensor, int *temp)
{
*temp = 0;
if (sensor->streaming)
return vd1943_get_temp_stream_enable(sensor, temp);
else
return vd1943_get_temp_stream_disable(sensor, temp);
}
static int vd1943_write_sensor_conf(struct vd1943 *sensor, u8 shutter_mode)
{
#if KERNEL_VERSION(5, 19, 0) > LINUX_VERSION_CODE
unsigned int code = sensor->active_fmt.code;
#elif KERNEL_VERSION(6, 8, 0) > LINUX_VERSION_CODE
struct v4l2_subdev_state *state =
v4l2_subdev_get_locked_active_state(&sensor->sd);
unsigned int code =
v4l2_subdev_get_pad_format(&sensor->sd, state, 0)->code;
#else
struct v4l2_subdev_state *state =
v4l2_subdev_get_locked_active_state(&sensor->sd);
unsigned int code = v4l2_subdev_state_get_format(state, 0)->code;
#endif
unsigned int model = sensor->model;
unsigned int i;
int ret = 0;
for (i = 0; i < ARRAY_SIZE(vdx941_configs[model][shutter_mode]); i++)
if (vdx941_configs[model][shutter_mode][i].mbus_code == code)
break;
if (i >= ARRAY_SIZE(vdx941_configs[model][shutter_mode]))
i = 0;
vd1943_write(sensor, VD1943_REG_SENSOR_CONFIGURATION,
vdx941_configs[model][shutter_mode][i].sensor_conf, &ret);
return ret;
}
static int vd1943_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
{
struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
struct vd1943 *sensor = to_vd1943(sd);
struct i2c_client *client = v4l2_get_subdevdata(sd);
int temperature;
int ret = 0;
/* Interact with HW only when it is powered ON */
if (!pm_runtime_get_if_in_use(&client->dev))
return 0;
switch (ctrl->id) {
case V4L2_CID_TEMPERATURE:
ret = vd1943_get_temp(sensor, &temperature);
if (ret)
break;
ctrl->val = temperature;
break;
default:
ret = -EINVAL;
break;
}
pm_runtime_mark_last_busy(&client->dev);
pm_runtime_put_autosuspend(&client->dev);
return ret;
}
static int vd1943_update_controls(struct vd1943 *sensor)
{
#if KERNEL_VERSION(5, 19, 0) > LINUX_VERSION_CODE
#else
struct v4l2_subdev_state *state;
#endif
const struct v4l2_rect *crop;
const struct v4l2_mbus_framefmt *format;
u8 bpp;
u32 vt_pixelrate;
u32 mipi_pixelrate;
u32 mipi_transition_cst_in_px;
u32 mipi_interpacket_delay_in_px;
u64 mipi_virtual_linelength = 0;
u32 linelength;
unsigned int hblank_min;
unsigned int vblank_min, vblank, vblank_max;
unsigned int expo_min, expo_max;
int ret;
#if KERNEL_VERSION(5, 19, 0) > LINUX_VERSION_CODE
crop = &sensor->active_crop;
format = &sensor->active_fmt;
#elif KERNEL_VERSION(6, 8, 0) > LINUX_VERSION_CODE
state = v4l2_subdev_get_locked_active_state(&sensor->sd);
crop = v4l2_subdev_get_pad_crop(&sensor->sd, state, 0);
format = v4l2_subdev_get_pad_format(&sensor->sd, state, 0);
#else
state = v4l2_subdev_get_locked_active_state(&sensor->sd);
crop = v4l2_subdev_state_get_crop(state, 0);
format = v4l2_subdev_state_get_format(state, 0);
#endif
/* With 2 rows of ADC used in GS mode, the VT pixel rate is doubled */
vt_pixelrate = sensor->pixel_rate;
if (sensor->shutter_ctrl->val == VD1943_GS_MODE)
vt_pixelrate = vt_pixelrate * 2;
/* Compute the max pixelrate on the mipi link based on pixel depth */
bpp = vd1943_get_bpp(format->code);
mipi_pixelrate = sensor->mipi_bandwidth / bpp;
/* Compute the mins linelength/hblank given the mipi bandwidth */
if (mipi_pixelrate < vt_pixelrate) {
/*
* Compute a virtual (mipi-equivalent) linelength that could fit
* with MIPI limitation (MIPI link being the bottleneck here).
*/
mipi_transition_cst_in_px = mipi_pixelrate / MEGA / 4;
mipi_interpacket_delay_in_px =
30 * 8 * sensor->nb_of_lane / bpp;
mipi_virtual_linelength = mipi_transition_cst_in_px +
crop->width +
mipi_interpacket_delay_in_px;
mipi_virtual_linelength =
mipi_virtual_linelength * vt_pixelrate / mipi_pixelrate;
}
linelength = max(VD1943_LINE_LENGTH_MIN, (int)mipi_virtual_linelength);
hblank_min = linelength - crop->width;
/* Adjust vblank with a target of 30FPS */
vblank_min = VD1943_VBLANK_MIN;
vblank = vt_pixelrate / (VD1943_DEFAULT_FPS30 * linelength) -
crop->height;
vblank = max(VD1943_VBLANK_MIN, vblank);
vblank_max = VD1943_FRAME_LENGTH_MAX - crop->height;
/*
* Exposure limits (expressed in lines) :
* - GS mode : [4 .. FRAME_LENGTH - 27]
* - RS mode : [1.5 .. FRAME_LENGTH - 10]
*/
expo_min = 4;
expo_max = crop->height + vblank - VD1943_EXPOSURE_MARGIN;
/* Update pixel_rate, blankings and exposure controls */
ret = __v4l2_ctrl_modify_range(sensor->vt_pixrate_ctrl, vt_pixelrate,
vt_pixelrate, 1, vt_pixelrate);
if (ret)
return ret;
ret = __v4l2_ctrl_modify_range(sensor->hblank_ctrl, hblank_min,
hblank_min, 1, hblank_min);
if (ret)
return ret;
ret = __v4l2_ctrl_modify_range(sensor->vblank_ctrl, vblank_min,
vblank_max, 1, vblank);
if (ret)
return ret;
ret = __v4l2_ctrl_s_ctrl(sensor->vblank_ctrl, vblank);
if (ret)
return ret;
ret = __v4l2_ctrl_modify_range(sensor->expo_ctrl, expo_min, expo_max, 1,
VD1943_EXPOSURE_DEFAULT);
if (ret)
return ret;
return __v4l2_ctrl_s_ctrl(sensor->expo_ctrl, VD1943_EXPOSURE_DEFAULT);
}
static int vd1943_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
struct vd1943 *sensor = to_vd1943(sd);
struct i2c_client *client = v4l2_get_subdevdata(sd);
#if KERNEL_VERSION(5, 19, 0) > LINUX_VERSION_CODE
#else
struct v4l2_subdev_state *state;
#endif
const struct v4l2_rect *crop;
const struct v4l2_mbus_framefmt *format;
unsigned int line_length = 0;
unsigned int frame_length = 0;
unsigned int expo_max;
unsigned int gpio_ctrl;
unsigned long gpio_ctrl_new;
unsigned long io;
int ret = 0;
#if KERNEL_VERSION(5, 19, 0) > LINUX_VERSION_CODE
crop = &sensor->active_crop;
format = &sensor->active_fmt;
#elif KERNEL_VERSION(6, 8, 0) > LINUX_VERSION_CODE
state = v4l2_subdev_get_locked_active_state(&sensor->sd);
crop = v4l2_subdev_get_pad_crop(&sensor->sd, state, 0);
format = v4l2_subdev_get_pad_format(&sensor->sd, state, 0);
#else
state = v4l2_subdev_get_locked_active_state(&sensor->sd);
crop = v4l2_subdev_state_get_crop(state, 0);
format = v4l2_subdev_state_get_format(state, 0);
#endif
if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
return 0;
/* Update controls state, range, etc. whatever the state of the HW */
switch (ctrl->id) {
case V4L2_CID_VBLANK:
frame_length = crop->height + ctrl->val;
if (sensor->shutter_ctrl->val == VD1943_GS_MODE)
frame_length = frame_length / 2;
expo_max = frame_length - VD1943_EXPOSURE_MARGIN;
ret = __v4l2_ctrl_modify_range(sensor->expo_ctrl, 0, expo_max,
1,
min(VD1943_EXPOSURE_DEFAULT,
expo_max));
break;
case V4L2_CID_SHUTTER_MODE:
/*
* Because __v4l2_ctrl_handler_setup() could trigger unnecessary
* vd1943_update_controls() call, ensure that the shutter mode
* really change before going further.
*/
if (ctrl->val != ctrl->cur.val)
ret = vd1943_update_controls(sensor);
break;
default:
break;
}
if (ret)
return ret;
/* Interact with HW only when it is powered ON */
if (!pm_runtime_get_if_in_use(&client->dev))
return 0;
switch (ctrl->id) {
case V4L2_CID_HFLIP:
ret = vd1943_write(sensor, VD1943_REG_ORIENTATION,
sensor->hflip_ctrl->val |
(sensor->vflip_ctrl->val << 1),
NULL);
break;
case V4L2_CID_TEST_PATTERN:
vd1943_write(sensor, VD1943_REG_PATGEN_CTRL, ctrl->val, &ret);
vd1943_write(sensor, VD1943_REG_DARKCAL_ENABLE, !ctrl->val,
&ret);
break;
case V4L2_CID_HBLANK:
line_length = crop->width + ctrl->val;
vd1943_write(sensor, VD1943_REG_LINE_LENGTH, line_length, &ret);
break;
case V4L2_CID_VBLANK:
ret = vd1943_write(sensor, VD1943_REG_FRAME_LENGTH,
frame_length, NULL);