forked from realsenseai/realsense_mipi_platform_driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd4xx.c
More file actions
4004 lines (3467 loc) · 104 KB
/
Copy pathd4xx.c
File metadata and controls
4004 lines (3467 loc) · 104 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
/*
* ds5.c - Intel(R) RealSense(TM) D4XX camera driver
*
* Copyright (c) 2017-2019, 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 <media/media-entity.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-subdev.h>
#include <media/v4l2-mediabus.h>
//#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_D45X 6
#define DS5_DEVICE_TYPE_D43X 5
#define DS5_DEVICE_TYPE_D46X 4
#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_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
/* Currently both depth and IR use VC 0 */
#define DS5_DEPTH_VCHAN_N 0
#define DS5_MOTION_T_VCHAN_N 0
//#define DS5_DEBUG_VCHAN_N 1
//#define DS5_MOTION_T_VCHAN_N 2
enum ds5_mux_pad {
DS5_MUX_PAD_EXTERNAL,
DS5_MUX_PAD_DEPTH,
DS5_MUX_PAD_MOTION_T,
DS5_MUX_PAD_RGB,
DS5_MUX_PAD_IMU,
DS5_MUX_PAD_COUNT,
};
#define DS5_N_CONTROLS 8
#define CSI2_MAX_VIRTUAL_CHANNELS 4
#define DFU_WAIT_RET_LEN 6
#define DS5_START_POLL_TIME 10
#define DS5_START_MAX_TIME 1000
#define DS5_START_MAX_COUNT (DS5_START_MAX_TIME / DS5_START_POLL_TIME)
/* DFU definition section */
#define DFU_MAGIC_NUMBER "/0x01/0x02/0x03/0x04"
#define DFU_BLOCK_SIZE 1024
#define DFU_I2C_STANDARD_MODE 100000
#define DFU_I2C_FAST_MODE 400000
#define DFU_I2C_BUS_CLK_RATE DFU_I2C_FAST_MODE
#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,
};
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 {
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 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;
};
#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;
u32 bus_clk_rate;
};
enum {
DS5_DS5U,
DS5_ASR,
DS5_AWG,
};
struct ds5 {
struct {
struct ds5_sensor sensor;
} depth;
struct {
struct ds5_sensor sensor;
} motion_t;
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;
int is_y8;
int is_rgb;
int is_imu;
u16 fw_version;
u16 fw_build;
};
struct ds5_counters {
unsigned int n_res;
unsigned int n_fmt;
unsigned int n_ctrl;
};
#define ds5_from_depth_sd(sd) container_of(sd, struct ds5, depth.sd)
#define ds5_from_motion_t_sd(sd) container_of(sd, struct ds5, motion_t.sd)
#define ds5_from_rgb_sd(sd) container_of(sd, struct ds5, rgb.sd)
static int ds5_write(struct ds5 *state, u16 reg, u16 val)
{
int ret;
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]);
ret = regmap_raw_write(state->regmap, reg, value, sizeof(value));
if (ret < 0)
dev_err(&state->client->dev,
"%s(): i2c write failed %d, 0x%04x = 0x%x\n",
__func__, ret, reg, val);
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 = regmap_raw_write(state->regmap, reg, val, val_len);
if (ret < 0)
dev_err(&state->client->dev,
"%s(): i2c raw write failed %d, %04x size(%d) bytes\n",
__func__, ret, reg, (int)val_len);
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 = regmap_raw_read(state->regmap, reg, val, 2);
if (ret < 0)
dev_err(&state->client->dev, "%s(): i2c read failed %d, 0x%04x\n",
__func__, ret, reg);
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 = regmap_raw_read(state->regmap, reg, val, val_len);
if (ret < 0)
dev_err(&state->client->dev, "%s(): i2c read failed %d, 0x%04x\n",
__func__, ret, reg);
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_15_30[] = {15, 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_framerate_100[] = {100};
static const u16 ds5_framerate_90[] = {90};
static const u16 ds5_imu_framerates[] = {50, 100, 200, 400};
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 ds5_rlt_rgb_sizes[] = {
{
.width = 1280,
.height = 800,
.framerates = ds5_framerate_to_30,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_30),
}, {
.width = 1280,
.height = 720,
.framerates = ds5_framerate_to_30,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_30),
}, {
.width = 848,
.height = 480,
.framerates = ds5_framerate_to_60,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_60),
}, {
.width = 640,
.height = 480,
.framerates = ds5_framerate_to_60,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_60),
}, {
.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 ds5_onsemi_rgb_sizes[] = {
{
.width = 640,
.height = 480,
.framerates = ds5_framerate_to_90,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_90),
}, {
.width = 960,
.height = 720,
.framerates = ds5_framerate_to_60,
.n_framerates = ARRAY_SIZE(ds5_framerate_to_60),
}, {
.width = 1280,
.height = 720,
.framerates = ds5_framerates,
.n_framerates = ARRAY_SIZE(ds5_framerates),
}, {
.width = 1920,
.height = 1080,
.framerates = ds5_framerates,
.n_framerates = ARRAY_SIZE(ds5_framerates),
}, {
.width = 2048,
.height = 1536,
.framerates = ds5_framerates,
.n_framerates = ARRAY_SIZE(ds5_framerates),
},
};
static const struct ds5_resolution ds5_size_w10 = {
.width = 1920,
.height = 1080,
.framerates = &ds5_framerate_30,
.n_framerates = 1,
};
static const struct ds5_resolution d43x_calibration_sizes[] = {
{
.width = 1280,
.height = 800,
.framerates = ds5_framerate_15_30,
.n_framerates = ARRAY_SIZE(ds5_framerate_15_30),
},
};
static const struct ds5_resolution d46x_calibration_sizes[] = {
{
.width = 1600,
.height = 1300,
.framerates = ds5_framerate_15_30,
.n_framerates = ARRAY_SIZE(ds5_framerate_15_30),
},
};
static const struct ds5_resolution ds5_size_imu[] = {
{
.width = 32,
.height = 1,
.framerates = ds5_imu_framerates,
.n_framerates = ARRAY_SIZE(ds5_imu_framerates),
},
};
static const struct ds5_format ds5_depth_formats_d43x[] = {
{
// TODO: 0x31 is replaced with 0x1e since it caused low FPS in Jetson.
.data_type = 0x1e, /* Z16 */
.mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
.n_resolutions = ARRAY_SIZE(d43x_depth_sizes),
.resolutions = d43x_depth_sizes,
}, {
.data_type = 0x2a, /* Y8 */
.mbus_code = MEDIA_BUS_FMT_Y8_1X8,
.n_resolutions = ARRAY_SIZE(d43x_depth_sizes),
.resolutions = d43x_depth_sizes,
}, {
.data_type = 0x24, /* 24-bit Calibration */
.mbus_code = MEDIA_BUS_FMT_RGB888_1X24, /* FIXME */
.n_resolutions = ARRAY_SIZE(d43x_calibration_sizes),
.resolutions = d43x_calibration_sizes,
},
};
static const struct ds5_format ds5_depth_formats_d46x[] = {
{
// TODO: 0x31 is replaced with 0x1e since it caused low FPS in Jetson.
.data_type = 0x1e, /* Z16 */
.mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
.n_resolutions = ARRAY_SIZE(d46x_depth_sizes),
.resolutions = d46x_depth_sizes,
}, {
/* First format: default */
.data_type = 0x2a, /* Y8 */
.mbus_code = MEDIA_BUS_FMT_Y8_1X8,
.n_resolutions = ARRAY_SIZE(d46x_depth_sizes),
.resolutions = d46x_depth_sizes,
}, {
.data_type = 0x24, /* 24-bit Calibration */
.mbus_code = MEDIA_BUS_FMT_RGB888_1X24, /* FIXME */
.n_resolutions = ARRAY_SIZE(d46x_calibration_sizes),
.resolutions = d46x_calibration_sizes,
},
};
#define DS5_DEPTH_N_FORMATS 1
static const struct ds5_format ds5_y_formats_ds5u[] = {
{
/* First format: default */
.data_type = 0x2a, /* Y8 */
.mbus_code = MEDIA_BUS_FMT_Y8_1X8,
.n_resolutions = ARRAY_SIZE(y8_sizes),
.resolutions = y8_sizes,
}, {
.data_type = 0x1e, /* Y8I */
.mbus_code = MEDIA_BUS_FMT_VYUY8_1X16,
.n_resolutions = ARRAY_SIZE(y8_sizes),
.resolutions = y8_sizes,
}, {
.data_type = 0x24, /* 24-bit Calibration */
.mbus_code = MEDIA_BUS_FMT_RGB888_1X24, /* FIXME */
.n_resolutions = ARRAY_SIZE(d43x_calibration_sizes),
.resolutions = d43x_calibration_sizes,
},
};
static const struct ds5_format ds5_rlt_rgb_format = {
.data_type = 0x1e, /* UYVY */
.mbus_code = MEDIA_BUS_FMT_YUYV8_1X16,
.n_resolutions = ARRAY_SIZE(ds5_rlt_rgb_sizes),
.resolutions = ds5_rlt_rgb_sizes,
};
#define DS5_RLT_RGB_N_FORMATS 1
static const struct ds5_format ds5_onsemi_rgb_format = {
.data_type = 0x1e, /* UYVY */
.mbus_code = MEDIA_BUS_FMT_YUYV8_1X16,
.n_resolutions = ARRAY_SIZE(ds5_onsemi_rgb_sizes),
.resolutions = ds5_onsemi_rgb_sizes,
};
#define DS5_ONSEMI_RGB_N_FORMATS 1
static const struct ds5_variant ds5_variants[] = {
[DS5_DS5U] = {
.formats = ds5_y_formats_ds5u,
.n_formats = ARRAY_SIZE(ds5_y_formats_ds5u),
},
};
static const struct ds5_format ds5_imu_formats[] = {
{
/* First format: default */
.data_type = 0x2a, /* IMU DT */
.mbus_code = MEDIA_BUS_FMT_Y8_1X8,
.n_resolutions = ARRAY_SIZE(ds5_size_imu),
.resolutions = ds5_size_imu,
},
};
static const struct v4l2_mbus_framefmt ds5_mbus_framefmt_template = {
.width = 0,
.height = 0,
.code = MEDIA_BUS_FMT_FIXED,
.field = V4L2_FIELD_NONE,
.colorspace = V4L2_COLORSPACE_DEFAULT,
.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT,
.quantization = V4L2_QUANTIZATION_DEFAULT,
.xfer_func = V4L2_XFER_FUNC_DEFAULT,
};
/* Get readable sensor name */
static const char *ds5_get_sensor_name(struct ds5 *state)
{
static const char *sensor_name[] = {"unknown", "RGB", "DEPTH", "Y8", "IMU"};
int sensor_id = state->is_rgb * 1 + state->is_depth * 2 + \
state->is_y8 * 3 + state->is_imu * 4;
if (sensor_id >= (sizeof(sensor_name)/sizeof(*sensor_name)))
sensor_id = 0;
return sensor_name[sensor_id];
}
/* This is needed for .get_fmt()
* and if streaming is started without .set_fmt() */
static void ds5_sensor_format_init(struct ds5_sensor *sensor)
{
const struct ds5_format *fmt;
struct v4l2_mbus_framefmt *ffmt;
unsigned int i;
if (sensor->config.format)
return;
dev_dbg(sensor->sd.dev, "%s(): on pad %u\n", __func__, sensor->mux_pad);
ffmt = &sensor->format;
*ffmt = ds5_mbus_framefmt_template;
/* Use the first format */
fmt = sensor->formats;
ffmt->code = fmt->mbus_code;
/* and the first resolution */
ffmt->width = fmt->resolutions->width;
ffmt->height = fmt->resolutions->height;
sensor->config.format = fmt;
sensor->config.resolution = fmt->resolutions;
/* Set default framerate to 30, or to 1st one if not supported */
for (i = 0; i < fmt->resolutions->n_framerates;i++) {
if (fmt->resolutions->framerates[i] == ds5_framerate_30 /* fps */) {
sensor->config.framerate = ds5_framerate_30;
return;
}
}
sensor->config.framerate = fmt->resolutions->framerates[0];
}
/* No locking needed for enumeration methods */
static int ds5_sensor_enum_mbus_code(struct v4l2_subdev *sd,
struct v4l2_subdev_pad_config *cfg,
struct v4l2_subdev_mbus_code_enum *mce)
{
struct ds5_sensor *sensor = container_of(sd, struct ds5_sensor, sd);
if (mce->pad)
return -EINVAL;
if (mce->index >= sensor->n_formats)
return -EINVAL;
mce->code = sensor->formats[mce->index].mbus_code;
return 0;
}
static int ds5_sensor_enum_frame_size(struct v4l2_subdev *sd,
struct v4l2_subdev_pad_config *cfg,
struct v4l2_subdev_frame_size_enum *fse)
{
struct ds5_sensor *sensor = container_of(sd, struct ds5_sensor, sd);
const struct ds5_format *fmt;
unsigned int i;
for (i = 0, fmt = sensor->formats; i < sensor->n_formats; i++, fmt++)
if (fse->code == fmt->mbus_code)
break;
if (i == sensor->n_formats)
return -EINVAL;
if (fse->index >= fmt->n_resolutions)
return -EINVAL;
fse->min_width = fse->max_width = fmt->resolutions[fse->index].width;
fse->min_height = fse->max_height = fmt->resolutions[fse->index].height;
return 0;
}
static int ds5_sensor_enum_frame_interval(struct v4l2_subdev *sd,
struct v4l2_subdev_pad_config *cfg,
struct v4l2_subdev_frame_interval_enum *fie)
{
struct ds5_sensor *sensor = container_of(sd, struct ds5_sensor, sd);
const struct ds5_format *fmt;
const struct ds5_resolution *res;
unsigned int i;
for (i = 0, fmt = sensor->formats; i < sensor->n_formats; i++, fmt++)
if (fie->code == fmt->mbus_code)
break;
if (i == sensor->n_formats)
return -EINVAL;
for (i = 0, res = fmt->resolutions; i < fmt->n_resolutions; i++, res++)
if (res->width == fie->width && res->height == fie->height)
break;
if (i == fmt->n_resolutions)
return -EINVAL;
if (fie->index >= res->n_framerates)
return -EINVAL;
fie->interval.numerator = 1;
fie->interval.denominator = res->framerates[fie->index];
return 0;
}
static int ds5_sensor_get_fmt(struct v4l2_subdev *sd,
struct v4l2_subdev_pad_config *cfg,
struct v4l2_subdev_format *fmt)
{
struct ds5_sensor *sensor = container_of(sd, struct ds5_sensor, sd);
struct ds5 *state = v4l2_get_subdevdata(sd);
if (fmt->pad)
return -EINVAL;
mutex_lock(&state->lock);
if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
else
fmt->format = sensor->format;
mutex_unlock(&state->lock);
dev_dbg(sd->dev, "%s(): pad %x, code %x, res %ux%u\n",
__func__, fmt->pad, fmt->format.code,
fmt->format.width, fmt->format.height);
return 0;
}
/* Called with lock held */
static const struct ds5_format *ds5_sensor_find_format(
struct ds5_sensor *sensor,
struct v4l2_mbus_framefmt *ffmt,
const struct ds5_resolution **best)
{
const struct ds5_resolution *res;
const struct ds5_format *fmt;
unsigned long best_delta = ~0;
unsigned int i;
for (i = 0, fmt = sensor->formats; i < sensor->n_formats; i++, fmt++) {
if (fmt->mbus_code == ffmt->code)
break;
}
dev_dbg(sensor->sd.dev, "%s(): mbus_code = %x, code = %x \n",
__func__, fmt->mbus_code, ffmt->code);
if (i == sensor->n_formats)
/* Not found, use default */
fmt = sensor->formats;
for (i = 0, res = fmt->resolutions; i < fmt->n_resolutions; i++, res++) {
unsigned long delta = abs(ffmt->width * ffmt->height -
res->width * res->height);
if (delta < best_delta) {