-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathd4xx.c
More file actions
7021 lines (6263 loc) · 198 KB
/
Copy pathd4xx.c
File metadata and controls
7021 lines (6263 loc) · 198 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
/*
* ds5.c - Intel(R) RealSense(TM) D4XX camera driver
*
* Copyright (c) 2017-2023, INTEL CORPORATION. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/media.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/videodev2.h>
#include <linux/version.h>
#include <linux/mutex.h>
#include <media/media-entity.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-subdev.h>
#include <media/v4l2-mediabus.h>
#ifdef CONFIG_VIDEO_D4XX_SERDES
#include <media/max9295.h>
#include <media/max9296.h>
#include <media/max96712.h>
/* Deserializer interface structure for abstraction */
struct dser_interface {
/* Pipeline management */
int (*get_available_pipe_id)(struct device *dev, int vc_id);
int (*set_pipe)(struct device *dev, int pipe_id, u8 data_type1, u8 data_type2, u32 vc_id);
int (*release_pipe)(struct device *dev, int pipe_id);
void (*reset_oneshot)(struct device *dev);
/* Setup and control */
int (*setup_link)(struct device *dev, struct device *s_dev);
int (*setup_control)(struct device *dev, struct device *s_dev);
int (*reset_control)(struct device *dev, struct device *s_dev);
/* Device registration */
int (*sdev_register)(struct device *dev, struct gmsl_link_ctx *g_ctx);
int (*sdev_unregister)(struct device *dev, struct device *s_dev);
/* Power management */
int (*power_on)(struct device *dev);
void (*power_off)(struct device *dev);
int (*init_settings)(struct device *dev);
/* Identification */
const char *name;
};
#else
#include <media/gmsl-link.h>
#define GMSL_CSI_DT_YUV422_8 0x1E
#define GMSL_CSI_DT_RGB_888 0x24
#define GMSL_CSI_DT_RAW_8 0x2A
#define GMSL_CSI_DT_EMBED 0x12
#endif
//#define DS5_DRIVER_NAME "DS5 RealSense camera driver"
#define DS5_DRIVER_NAME "d4xx"
#define DS5_DRIVER_NAME_AWG "d4xx-awg"
#define DS5_DRIVER_NAME_ASR "d4xx-asr"
#define DS5_DRIVER_NAME_CLASS "d4xx-class"
#define DS5_DRIVER_NAME_DFU "d4xx-dfu"
#define DS5_MIPI_SUPPORT_LINES 0x0300
#define DS5_MIPI_SUPPORT_PHY 0x0304
#define DS5_MIPI_DATARATE_MIN 0x0308
#define DS5_MIPI_DATARATE_MAX 0x030A
#define DS5_FW_VERSION 0x030C
#define DS5_FW_BUILD 0x030E
#define DS5_DEVICE_TYPE 0x0310
#define DS5_DEVICE_TYPE_D40X 8
#define DS5_DEVICE_TYPE_D41X 7
#define DS5_DEVICE_TYPE_D45X 6
#define DS5_DEVICE_TYPE_D43X 5
#define DS5_DEVICE_TYPE_D46X 4
#define DS5_DEVICE_TYPE_UNKNOWN 0
#define DS5_MIPI_LANE_NUMS 0x0400
#define DS5_MIPI_LANE_DATARATE 0x0402
#define DS5_MIPI_CONF_STATUS 0x0500
#define DS5_START_STOP_STREAM 0x1000
#define DS5_DEPTH_STREAM_STATUS 0x1004
#define DS5_RGB_STREAM_STATUS 0x1008
#define DS5_IMU_STREAM_STATUS 0x100C
#define DS5_IR_STREAM_STATUS 0x1014
#define DS5_STREAM_DEPTH 0x0
#define DS5_STREAM_RGB 0x1
#define DS5_STREAM_IMU 0x2
#define DS5_STREAM_IR 0x4
#define DS5_STREAM_STOP 0x100
#define DS5_STREAM_START 0x200
#define DS5_STREAM_IDLE 0x1
#define DS5_STREAM_STREAMING 0x2
#define DS5_DEPTH_STREAM_DT 0x4000
#define DS5_DEPTH_STREAM_MD 0x4002
#define DS5_DEPTH_RES_WIDTH 0x4004
#define DS5_DEPTH_RES_HEIGHT 0x4008
#define DS5_DEPTH_FPS 0x400C
#define DS5_DEPTH_OVERRIDE 0x401C
#define DS5_RGB_STREAM_DT 0x4020
#define DS5_RGB_STREAM_MD 0x4022
#define DS5_RGB_RES_WIDTH 0x4024
#define DS5_RGB_RES_HEIGHT 0x4028
#define DS5_RGB_FPS 0x402C
#define DS5_IMU_STREAM_DT 0x4040
#define DS5_IMU_STREAM_MD 0x4042
#define DS5_IMU_RES_WIDTH 0x4044
#define DS5_IMU_RES_HEIGHT 0x4048
#define DS5_IMU_FPS 0x404C
#define DS5_IR_STREAM_DT 0x4080
#define DS5_IR_STREAM_MD 0x4082
#define DS5_IR_RES_WIDTH 0x4084
#define DS5_IR_RES_HEIGHT 0x4088
#define DS5_IR_FPS 0x408C
#define DS5_IR_OVERRIDE 0x409C
#define DS5_DEPTH_CONTROL_BASE 0x4100
#define DS5_RGB_CONTROL_BASE 0x4200
#define DS5_MANUAL_EXPOSURE_LSB 0x0000
#define DS5_MANUAL_EXPOSURE_MSB 0x0002
#define DS5_MANUAL_GAIN 0x0004
#define DS5_LASER_POWER 0x0008
#define DS5_AUTO_EXPOSURE_MODE 0x000C
#define DS5_EXPOSURE_ROI_TOP 0x0010
#define DS5_EXPOSURE_ROI_LEFT 0x0014
#define DS5_EXPOSURE_ROI_BOTTOM 0x0018
#define DS5_EXPOSURE_ROI_RIGHT 0x001C
#define DS5_MANUAL_LASER_POWER 0x0024
#define DS5_PWM_FREQUENCY 0x0028
#define DS5_CAMERA_SYNC_MODE 0x002C
#define DS5_DEPTH_CONFIG_STATUS 0x4800
#define DS5_RGB_CONFIG_STATUS 0x4802
#define DS5_IMU_CONFIG_STATUS 0x4804
#define DS5_IR_CONFIG_STATUS 0x4808
#define DS5_STATUS_STREAMING 0x1
#define DS5_STATUS_INVALID_DT 0x2
#define DS5_STATUS_INVALID_RES 0x4
#define DS5_STATUS_INVALID_FPS 0x8
#define MIPI_LANE_RATE 1000
#define MAX_DEPTH_EXP 200000
#define MAX_RGB_EXP 10000
#define DEF_DEPTH_EXP 33000
#define DEF_RGB_EXP 1660
enum ds5_mux_pad {
DS5_MUX_PAD_EXTERNAL,
DS5_MUX_PAD_DEPTH,
DS5_MUX_PAD_RGB,
DS5_MUX_PAD_IR,
DS5_MUX_PAD_IMU,
DS5_MUX_PAD_COUNT,
};
#define DS5_N_CONTROLS 8
#define CSI2_MAX_VIRTUAL_CHANNELS 4
#define PIPE_NOT_CONFIGURED -1
#define DFU_WAIT_RET_LEN 6
#define DS5_START_POLL_TIME 10
#define DS5_START_MAX_TIME 2000
#define DS5_START_MAX_COUNT (DS5_START_MAX_TIME / DS5_START_POLL_TIME)
#define MAX_DS5_CONFIG_RETRIES 5
/* I2C retry configuration */
#define DS5_I2C_RETRY_COUNT 5
#define DS5_I2C_RETRY_DELAY_US 5000
/* DFU definition section */
#define DFU_MAGIC_NUMBER "/0x01/0x02/0x03/0x04"
#define DFU_BLOCK_SIZE 1024
#ifdef CONFIG_TEGRA_CAMERA_PLATFORM
#define DFU_I2C_STANDARD_MODE 100000
#define DFU_I2C_FAST_MODE 400000
#define DFU_I2C_BUS_CLK_RATE DFU_I2C_FAST_MODE
#endif
#define ds5_read_with_check(state, addr, val) {\
if (ds5_read(state, addr, val)) \
return -EINVAL; }
#define ds5_raw_read_with_check(state, addr, buf, size) {\
if (ds5_raw_read(state, addr, buf, size)) \
return -EINVAL; }
#define ds5_write_with_check(state, addr, val) {\
if (ds5_write(state, addr, val)) \
return -EINVAL; }
#define ds5_raw_write_with_check(state, addr, buf, size) {\
if (ds5_raw_write(state, addr, buf, size)) \
return -EINVAL; }
enum dfu_fw_state {
appIDLE = 0x0000,
appDETACH = 0x0001,
dfuIDLE = 0x0002,
dfuDNLOAD_SYNC = 0x0003,
dfuDNBUSY = 0x0004,
dfuDNLOAD_IDLE = 0x0005,
dfuMANIFEST_SYNC = 0x0006,
dfuMANIFEST = 0x0007,
dfuMANIFEST_WAIT_RESET = 0x0008,
dfuUPLOAD_IDLE = 0x0009,
dfuERROR = 0x000a
};
enum dfu_state {
DS5_DFU_IDLE = 0,
DS5_DFU_RECOVERY,
DS5_DFU_OPEN,
DS5_DFU_IN_PROGRESS,
DS5_DFU_DONE,
DS5_DFU_ERROR
} dfu_state_t;
struct hwm_cmd {
u16 header;
u16 magic_word;
u32 opcode;
u32 param1;
u32 param2;
u32 param3;
u32 param4;
unsigned char Data[0];
};
static const struct hwm_cmd cmd_switch_to_dfu = {
.header = 0x14,
.magic_word = 0xCDAB,
.opcode = 0x1e,
.param1 = 0x01,
};
enum table_id {
COEF_CALIBRATION_ID = 0x19,
DEPTH_CALIBRATION_ID = 0x1f,
RGB_CALIBRATION_ID = 0x20,
IMU_CALIBRATION_ID = 0x22
} table_id_t;
static const struct hwm_cmd get_calib_data = {
.header = 0x14,
.magic_word = 0xCDAB,
.opcode = 0x15,
.param1 = 0x00, //table_id
};
static const struct hwm_cmd set_calib_data = {
.header = 0x0114,
.magic_word = 0xCDAB,
.opcode = 0x62,
.param1 = 0x00, //table_id
.param2 = 0x02, //region
};
static const struct hwm_cmd gvd = {
.header = 0x14,
.magic_word = 0xCDAB,
.opcode = 0x10,
};
static const struct hwm_cmd set_ae_roi = {
.header = 0x14,
.magic_word = 0xCDAB,
.opcode = 0x44,
};
static const struct hwm_cmd get_ae_roi = {
.header = 0x014,
.magic_word = 0xCDAB,
.opcode = 0x45,
};
static const struct hwm_cmd set_ae_setpoint = {
.header = 0x18,
.magic_word = 0xCDAB,
.opcode = 0x2B,
.param1 = 0xa, // AE control
};
static const struct hwm_cmd get_ae_setpoint = {
.header = 0x014,
.magic_word = 0xCDAB,
.opcode = 0x2C,
.param1 = 0xa, // AE control
.param2 = 0, // get current
};
static const struct hwm_cmd erb = {
.header = 0x14,
.magic_word = 0xCDAB,
.opcode = 0x17,
};
static const struct hwm_cmd ewb = {
.header = 0x14,
.magic_word = 0xCDAB,
.opcode = 0x18,
};
static const struct hwm_cmd cmd_hw_reset = {
.header = 0x14,
.magic_word = 0xCDAB,
.opcode = 0x20, /* HW reset opcode */
};
struct __fw_status {
uint32_t spare1;
uint32_t FW_lastVersion;
uint32_t FW_highestVersion;
uint16_t FW_DownloadStatus;
uint16_t DFU_isLocked;
uint16_t DFU_version;
uint8_t ivcamSerialNum[8];
uint8_t spare2[42];
};
/*************************/
struct ds5_ctrls {
struct v4l2_ctrl_handler handler;
struct v4l2_ctrl_handler handler_depth;
struct v4l2_ctrl_handler handler_rgb;
struct v4l2_ctrl_handler handler_y8;
struct v4l2_ctrl_handler handler_imu;
struct {
struct v4l2_ctrl *log;
struct v4l2_ctrl *fw_version;
struct v4l2_ctrl *gvd;
struct v4l2_ctrl *get_depth_calib;
struct v4l2_ctrl *set_depth_calib;
struct v4l2_ctrl *get_coeff_calib;
struct v4l2_ctrl *set_coeff_calib;
struct v4l2_ctrl *ae_roi_get;
struct v4l2_ctrl *ae_roi_set;
struct v4l2_ctrl *ae_setpoint_get;
struct v4l2_ctrl *ae_setpoint_set;
struct v4l2_ctrl *erb;
struct v4l2_ctrl *ewb;
struct v4l2_ctrl *hwmc;
struct v4l2_ctrl *laser_power;
struct v4l2_ctrl *manual_laser_power;
struct v4l2_ctrl *auto_exp;
struct v4l2_ctrl *exposure;
/* in DS5 manual gain only works with manual exposure */
struct v4l2_ctrl *gain;
struct v4l2_ctrl *link_freq;
struct v4l2_ctrl *query_sub_stream;
struct v4l2_ctrl *set_sub_stream;
struct v4l2_ctrl *sync_mode;
};
};
struct ds5_resolution {
u16 width;
u16 height;
u8 n_framerates;
const u16 *framerates;
};
struct ds5_format {
unsigned int n_resolutions;
const struct ds5_resolution *resolutions;
u32 mbus_code;
u8 data_type;
};
struct ds5_sensor {
struct v4l2_subdev sd;
struct media_pad pad;
struct v4l2_mbus_framefmt format;
u16 mux_pad;
struct {
const struct ds5_format *format;
const struct ds5_resolution *resolution;
u16 framerate;
} config;
bool streaming;
/*struct ds5_vchan *vchan;*/
const struct ds5_format *formats;
unsigned int n_formats;
int pipe_id;
u16 pipe_data_type1;
u16 pipe_data_type2;
u32 pipe_vc_id;
u16 cached_dt_value;
u16 cached_md_value;
u16 cached_override_value;
u16 cached_fps_value;
u16 cached_width_value;
u16 cached_height_value;
};
#ifdef CONFIG_TEGRA_CAMERA_PLATFORM
#include <media/camera_common.h>
#define ds5_mux_subdev camera_common_data
#else
struct ds5_mux_subdev {
struct v4l2_subdev subdev;
};
#endif
struct ds5_variant {
const struct ds5_format *formats;
unsigned int n_formats;
};
struct ds5_dfu_dev {
struct cdev ds5_cdev;
struct class *ds5_class;
int device_open_count;
enum dfu_state dfu_state_flag;
unsigned char *dfu_msg;
u16 msg_write_once;
// unsigned char init_v4l_f; // need refactoring
u32 bus_clk_rate;
};
enum {
DS5_DS5U,
DS5_ASR,
DS5_AWG,
};
struct ds5 {
struct { struct ds5_sensor sensor; } depth;
struct { struct ds5_sensor sensor; } ir;
struct { struct ds5_sensor sensor; } rgb;
struct { struct ds5_sensor sensor; } imu;
struct {
struct ds5_mux_subdev sd;
struct media_pad pads[DS5_MUX_PAD_COUNT];
struct ds5_sensor *last_set;
} mux;
struct ds5_ctrls ctrls;
struct ds5_dfu_dev dfu_dev;
bool power;
struct i2c_client *client;
/*struct ds5_vchan virtual_channels[CSI2_MAX_VIRTUAL_CHANNELS];*/
/* All below pointers are used for writing, cannot be const */
struct mutex lock;
struct regmap *regmap;
struct regulator *vcc;
const struct ds5_variant *variant;
int is_depth, is_y8, is_rgb, is_imu;
bool metadata_enabled;
int aggregated;
int reset_ref_ds5;
int reset_ref_dser;
u16 fw_version;
u16 fw_build;
#ifdef CONFIG_VIDEO_D4XX_SERDES
struct gmsl_link_ctx g_ctx;
struct device *ser_dev;
struct device *dser_dev;
struct i2c_client *ser_i2c;
struct i2c_client *dser_i2c;
const struct dser_interface *dser_ops;
bool serdes_primary; /* true for the instance that ran SERDES setup */
#endif
struct ds5_dev *ds5_dev; /* pointer to DS5 device struct */
};
struct ds5_dev {
struct mutex lock;
/*
* Per-camera reset generation counter.
* Incremented whenever the camera undergoes a HW reset,
* either from its own instance or a sibling's instance.
* Each instance stores the last seen generation count
* and compares it on each access to detect if a reset has occurred
* and invalidates its own state if so.
*/
atomic_t reset_gen;
/* Cached device type from HW reset Step 8.
* During probe the first instance resets the camera, causing DS5_DEVICE_TYPE
* to temporarily return 0. Step 8 polls until the register is valid and
* stores the result here so that all four probe instances (and any post-reset
* code path) can use the confirmed value even if the register read returns 0.
*/
u16 cached_device_type;
/* Timestamp (jiffies) of last completed HW reset.
* Used to enforce DS5_HW_RESET_COOLDOWN_MS between consecutive resets
* and prevent GMSL link degradation from rapid reset cycles.
*/
unsigned long last_reset_jiffies;
#ifdef CONFIG_VIDEO_D4XX_SERDES
/* Pointer to the deserializer dev */
struct dser_control *dser_control;
#endif
/* Pointer to the primary DS5 struct */
struct ds5 *ds5_primary;
bool depth_streaming;
bool ir_streaming;
bool rgb_streaming;
bool imu_streaming;
atomic_t ds5_probe_reset_once;
};
#ifdef CONFIG_VIDEO_D4XX_SERDES
static DEFINE_MUTEX(serdes_lock__);
static bool ds5_slots_inited;
#define MAX_DSER_NUM 4
struct dser_control {
struct mutex lock;
/*
* Per-deserializer reset generation counter.
* Replaces the old global ds5_reset_gen for SERDES builds so that
* resetting camera A does not force camera B (on a different deserializer)
* to invalidate its state. Cameras sharing the same deserializer still
* see each other's resets through the shared counter.
*/
atomic_t reset_gen;
struct device *dser_dev;
};
static struct dser_control dser_inited[MAX_DSER_NUM];
#define MAX_DS5_NUM (MAX_DSER_NUM * 2) /* assuming max 2 DS5 cameras per deserializer */
static struct ds5_dev ds5_inited[MAX_DS5_NUM];
static void ds5_init_global_slots_once(void)
{
int i;
if (ds5_slots_inited) {
return;
}
for (i = 0; i < MAX_DS5_NUM; i++)
mutex_init(&ds5_inited[i].lock);
for (i = 0; i < MAX_DSER_NUM; i++)
mutex_init(&dser_inited[i].lock);
ds5_slots_inited = true;
}
static inline atomic_t *ds5_get_reset_gen(struct ds5 *state)
{
return &state->ds5_dev->reset_gen;
}
static inline atomic_t *dser_get_reset_gen(struct ds5 *state)
{
return &state->ds5_dev->dser_control->reset_gen;
}
/* MAX9296 deserializer interface implementation */
static const struct dser_interface max9296_interface = {
.get_available_pipe_id = max9296_get_available_pipe_id,
.set_pipe = max9296_set_pipe,
.release_pipe = max9296_release_pipe,
.reset_oneshot = max9296_reset_oneshot,
.setup_link = max9296_setup_link,
.setup_control = max9296_setup_control,
.reset_control = max9296_reset_control,
.sdev_register = max9296_sdev_register,
.sdev_unregister = max9296_sdev_unregister,
.power_on = max9296_power_on,
.power_off = max9296_power_off,
.init_settings = max9296_init_settings,
.name = "max9296",
};
/* MAX96712 deserializer interface implementation */
static const struct dser_interface max96712_interface = {
.get_available_pipe_id = max96712_get_available_pipe_id,
.set_pipe = max96712_set_pipe,
.release_pipe = max96712_release_pipe,
.reset_oneshot = max96712_reset_oneshot,
.setup_link = max96712_setup_link,
.setup_control = max96712_setup_control,
.reset_control = max96712_reset_control,
.sdev_register = max96712_sdev_register,
.sdev_unregister = max96712_sdev_unregister,
.power_on = max96712_power_on,
.power_off = max96712_power_off,
.init_settings = max96712_init_settings,
.name = "max96712",
};
#else /* !CONFIG_VIDEO_D4XX_SERDES */
static atomic_t ds5_reset_gen = ATOMIC_INIT(0);
static inline atomic_t *ds5_get_reset_gen(struct ds5 *state)
{
return &ds5_reset_gen;
}
#define MAX_DS5_NUM (1) /* assuming max 1 DS5 camera with RDK(?) */
static struct ds5_dev ds5_inited[MAX_DS5_NUM];
static bool ds5_slots_inited;
static DEFINE_MUTEX(ds5_slots_lock__);
static void ds5_init_global_slots_once(void)
{
mutex_lock(&ds5_slots_lock__);
if (!ds5_slots_inited) {
mutex_init(&ds5_inited[0].lock);
ds5_slots_inited = true;
}
mutex_unlock(&ds5_slots_lock__);
}
#endif /* !CONFIG_VIDEO_D4XX_SERDES */
static inline u16 ds5_dev_type(struct ds5 *state, u16 dev_type)
{
if (dev_type == 0 && state->ds5_dev->cached_device_type != 0) {
dev_info(&state->client->dev,
"%s(): device type register returned 0, using cached type 0x%x\n",
__func__, state->ds5_dev->cached_device_type);
dev_type = state->ds5_dev->cached_device_type;
}
return dev_type;
}
#define ds5_from_depth_sd(sd) container_of(sd, struct ds5, depth.sd)
#define ds5_from_ir_sd(sd) container_of(sd, struct ds5, ir.sd)
#define ds5_from_rgb_sd(sd) container_of(sd, struct ds5, rgb.sd)
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 15, 136)
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 148)
static inline void msleep_range(unsigned int delay_base)
{
usleep_range(delay_base * 1000, delay_base * 1000 + 500);
}
#endif
#endif
static int ds5_write(struct ds5 *state, u16 reg, u16 val)
{
int ret;
int retry;
u8 value[2];
value[1] = val >> 8;
value[0] = val & 0x00FF;
dev_dbg(&state->client->dev,
"%s(): writing to register: 0x%04x, value1: 0x%x, value2:0x%x\n",
__func__, reg, value[1], value[0]);
for (retry = 0; retry < DS5_I2C_RETRY_COUNT; retry++) {
ret = regmap_raw_write(state->regmap, reg, value, sizeof(value));
if (ret == 0)
break;
if (retry < DS5_I2C_RETRY_COUNT - 1) {
dev_warn(&state->client->dev,
"%s(): i2c write retry %d, 0x%04x = 0x%x, err %d\n",
__func__, retry + 1, reg, val, ret);
usleep_range(DS5_I2C_RETRY_DELAY_US,
DS5_I2C_RETRY_DELAY_US + 500);
}
}
if (ret < 0)
dev_err(&state->client->dev,
"%s(): i2c write failed after %d retries, 0x%04x = 0x%x, err %d\n",
__func__, DS5_I2C_RETRY_COUNT, reg, val, ret);
else if (state->dfu_dev.dfu_state_flag == DS5_DFU_IDLE)
dev_dbg(&state->client->dev, "%s(): i2c write 0x%04x: 0x%x\n",
__func__, reg, val);
return ret;
}
static int ds5_raw_write(struct ds5 *state, u16 reg,
const void *val, size_t val_len)
{
int ret;
int retry;
for (retry = 0; retry < DS5_I2C_RETRY_COUNT; retry++) {
ret = regmap_raw_write(state->regmap, reg, val, val_len);
if (ret == 0)
break;
if (retry < DS5_I2C_RETRY_COUNT - 1) {
dev_warn(&state->client->dev,
"%s(): i2c raw write retry %d, 0x%04x size(%d), err %d\n",
__func__, retry + 1, reg, (int)val_len, ret);
usleep_range(DS5_I2C_RETRY_DELAY_US,
DS5_I2C_RETRY_DELAY_US + 500);
}
}
if (ret < 0)
dev_err(&state->client->dev,
"%s(): i2c raw write failed after %d retries, 0x%04x size(%d), err %d\n",
__func__, DS5_I2C_RETRY_COUNT, reg, (int)val_len, ret);
else if (state->dfu_dev.dfu_state_flag == DS5_DFU_IDLE)
dev_dbg(&state->client->dev,
"%s(): i2c raw write 0x%04x: %d bytes\n",
__func__, reg, (int)val_len);
return ret;
}
static int ds5_read(struct ds5 *state, u16 reg, u16 *val)
{
int ret;
int retry;
for (retry = 0; retry < DS5_I2C_RETRY_COUNT; retry++) {
ret = regmap_raw_read(state->regmap, reg, val, 2);
if (ret == 0)
break;
if (retry < DS5_I2C_RETRY_COUNT - 1) {
dev_warn(&state->client->dev,
"%s(): i2c read retry %d, 0x%04x, err %d\n",
__func__, retry + 1, reg, ret);
usleep_range(DS5_I2C_RETRY_DELAY_US,
DS5_I2C_RETRY_DELAY_US + 500);
}
}
if (ret < 0)
dev_err(&state->client->dev,
"%s(): i2c read failed after %d retries, 0x%04x, err %d\n",
__func__, DS5_I2C_RETRY_COUNT, reg, ret);
else if (state->dfu_dev.dfu_state_flag == DS5_DFU_IDLE)
dev_dbg(&state->client->dev, "%s(): i2c read 0x%04x: 0x%x\n",
__func__, reg, *val);
return ret;
}
static int ds5_raw_read(struct ds5 *state, u16 reg, void *val, size_t val_len)
{
int ret;
int retry;
for (retry = 0; retry < DS5_I2C_RETRY_COUNT; retry++) {
ret = regmap_raw_read(state->regmap, reg, val, val_len);
if (ret == 0)
break;
if (retry < DS5_I2C_RETRY_COUNT - 1) {
dev_warn(&state->client->dev,
"%s(): i2c raw read retry %d, 0x%04x size(%d), err %d\n",
__func__, retry + 1, reg, (int)val_len, ret);
usleep_range(DS5_I2C_RETRY_DELAY_US,
DS5_I2C_RETRY_DELAY_US + 500);
}
}
if (ret < 0)
dev_err(&state->client->dev,
"%s(): i2c raw read failed after %d retries, 0x%04x size(%d), err %d\n",
__func__, DS5_I2C_RETRY_COUNT, reg, (int)val_len, ret);
return ret;
}
/* Pad ops */
static const u16 ds5_default_framerate = 30;
// **********************
// FIXME: D16 width must be doubled, because an 8-bit format is used. Check how
// the Tegra driver propagates resolutions and formats.
// **********************
//TODO: keep 6, till 5 is supported by FW
static const u16 ds5_framerates[] = {5, 30};
#define DS5_FRAMERATE_DEFAULT_IDX 1
static const u16 ds5_framerate_30 = 30;
static const u16 ds5_framerate_25 = 25;
static const u16 ds5_depth_framerate_to_30[] = {5, 15, 30};
static const u16 ds5_framerate_to_30[] = {5, 10, 15, 30};
static const u16 ds5_framerate_to_60[] = {5, 15, 30, 60};
static const u16 ds5_framerate_to_90[] = {5, 15, 30, 60, 90};
static const u16 ds5_41x_depth_framerate_to_30[] = {6, 15, 30};
static const u16 ds5_41x_framerate_to_30[] = {6, 15, 30};
static const u16 ds5_41x_framerate_to_60_no_15[] = {6, 30, 60};
static const u16 ds5_41x_framerate_to_60[] = {6, 15, 30, 60};
static const u16 ds5_41x_framerate_to_90[] = {6, 15, 30, 60, 90};
static const u16 ds5_framerate_15_25[] = {15, 25};
static const u16 ds5_framerate_15_30[] = {15, 30};
static const u16 ds5_framerate_15_60[] = {15, 30, 60};
static const u16 ds5_framerate_15_90[] = {15, 30, 60, 90};
static const u16 ds5_imu_framerates[] = {50, 100, 200, 400};
static const u16 ds5_framerate_90[] = {90};
static const u16 ds5_framerate_100[] = {100};
/* Helper macro to define resolution entries concisely. */
#define DS5_RES(w, h, fr) \
{ .width = (w), .height = (h), .framerates = (fr), .n_framerates = ARRAY_SIZE(fr) },
#define D401_COMMON_RES \
DS5_RES(1280, 720, ds5_framerate_to_30)\
DS5_RES(848, 480, ds5_framerate_to_60)\
DS5_RES(640, 480, ds5_framerate_to_60)\
DS5_RES(640, 360, ds5_framerate_to_60)\
DS5_RES(480, 270, ds5_framerate_to_60)\
DS5_RES(424, 240, ds5_framerate_to_60)\
static const struct ds5_resolution d40x_depth_sizes[] = {
D401_COMMON_RES
DS5_RES(256, 144, ds5_framerate_90)
};
static const struct ds5_resolution d40x_y8_sizes[] = {
D401_COMMON_RES
};
static const struct ds5_resolution d40x_rgb_sizes[] = {
D401_COMMON_RES
};
static const struct ds5_resolution d40x_calibration_sizes[] = {
DS5_RES(1288, 808, ds5_framerate_15_25)
};
static const struct ds5_resolution d41x_depth_sizes[] = {
{
.width = 1280,
.height = 720,
.framerates = ds5_41x_depth_framerate_to_30,
.n_framerates = ARRAY_SIZE(ds5_41x_depth_framerate_to_30),
}, {
.width = 848,
.height = 480,
.framerates = ds5_41x_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_41x_framerate_to_90),
}, {
.width = 848,
.height = 100,
.framerates = ds5_framerate_100,
.n_framerates = ARRAY_SIZE(ds5_framerate_100),
}, {
.width = 640,
.height = 480,
.framerates = ds5_41x_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_41x_framerate_to_90),
}, {
.width = 640,
.height = 360,
.framerates = ds5_41x_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_41x_framerate_to_90),
}, {
.width = 480,
.height = 270,
.framerates = ds5_41x_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_41x_framerate_to_90),
}, {
.width = 424,
.height = 240,
.framerates = ds5_41x_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_41x_framerate_to_90),
}, {
.width = 256,
.height = 144,
.framerates = ds5_framerate_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_90),
},
};
static const struct ds5_resolution d43x_depth_sizes[] = {
{
.width = 1280,
.height = 720,
.framerates = ds5_depth_framerate_to_30,
.n_framerates = ARRAY_SIZE(ds5_depth_framerate_to_30),
}, {
.width = 848,
.height = 480,
.framerates = ds5_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_90),
}, {
.width = 848,
.height = 100,
.framerates = ds5_framerate_100,
.n_framerates = ARRAY_SIZE(ds5_framerate_100),
}, {
.width = 640,
.height = 480,
.framerates = ds5_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_90),
}, {
.width = 640,
.height = 360,
.framerates = ds5_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_90),
}, {
.width = 480,
.height = 270,
.framerates = ds5_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_90),
}, {
.width = 424,
.height = 240,
.framerates = ds5_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_90),
}, {
.width = 256,
.height = 144,
.framerates = ds5_framerate_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_90),
},
};
static const struct ds5_resolution d46x_depth_sizes[] = {
{
.width = 1280,
.height = 960,
.framerates = ds5_framerates,
.n_framerates = ARRAY_SIZE(ds5_framerates),
}, {
.width = 640,
.height = 480,
.framerates = ds5_framerates,
.n_framerates = ARRAY_SIZE(ds5_framerates),
},
};
static const struct ds5_resolution y8_sizes[] = {
{
.width = 1280,
.height = 720,
.framerates = ds5_depth_framerate_to_30,
.n_framerates = ARRAY_SIZE(ds5_depth_framerate_to_30),
}, {
.width = 848,
.height = 480,
.framerates = ds5_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_90),
}, {
.width = 640,
.height = 480,
.framerates = ds5_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_90),
}, {
.width = 640,
.height = 360,
.framerates = ds5_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_90),
}, {
.width = 480,
.height = 270,
.framerates = ds5_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_90),
}, {
.width = 424,
.height = 240,
.framerates = ds5_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_90),
}
};
static const struct ds5_resolution y8_41x_sizes[] = {
{
.width = 1920,
.height = 1080,
.framerates = ds5_framerate_15_25,
.n_framerates = ARRAY_SIZE(ds5_framerate_15_25),
}, {
.width = 1280,
.height = 720,
.framerates = ds5_41x_depth_framerate_to_30,
.n_framerates = ARRAY_SIZE(ds5_41x_depth_framerate_to_30),
}, {
.width = 960,
.height = 540,