forked from clearlinux-pkgs/linux-iot-lts2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0058-media-i2c-add-TI960-953-SER-DES-driver.patch
1684 lines (1678 loc) · 41.6 KB
/
0058-media-i2c-add-TI960-953-SER-DES-driver.patch
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
From c506c872177c02936841a179ba5404efc3b753f8 Mon Sep 17 00:00:00 2001
From: Meng Wei <[email protected]>
Date: Fri, 26 Oct 2018 09:52:57 +0800
Subject: [PATCH 15/25] media: i2c: add TI960/953 SER/DES driver
TI960 is a versatile sensor hub capable of connecting
serialized sensor data received from four independent
video data streams through an FPD-Link III interface.
Signed-off-by: Chang Ying <[email protected]>
Signed-off-by: Meng Wei <[email protected]>
---
drivers/media/i2c/Kconfig | 6 +
drivers/media/i2c/Makefile | 1 +
drivers/media/i2c/ti960-reg.h | 209 +++++
drivers/media/i2c/ti960.c | 1344 +++++++++++++++++++++++++++++++++
include/media/ti960.h | 62 ++
5 files changed, 1622 insertions(+)
create mode 100644 drivers/media/i2c/ti960-reg.h
create mode 100644 drivers/media/i2c/ti960.c
create mode 100644 include/media/ti960.h
diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index be954b4d9425..dbcb2131ddf7 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -1078,6 +1078,12 @@ config VIDEO_MAX9286
---help---
This is a MAXIM 96705 Serializer and MAXIM 9286 CSI-2 Deserializer driver.
+config VIDEO_TI960
+ tristate "TI960 driver support"
+ depends on I2C && VIDEO_V4L2
+ ---help---
+ This is a driver for TI960 Deserializer.
+
endmenu
menu "Sensors used on soc_camera driver"
diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
index 96ec00673960..4ad43e933238 100644
--- a/drivers/media/i2c/Makefile
+++ b/drivers/media/i2c/Makefile
@@ -114,3 +114,4 @@ obj-$(CONFIG_SDR_MAX2175) += max2175.o
obj-$(CONFIG_VIDEO_CRLMODULE) += crlmodule/
obj-$(CONFIG_VIDEO_TI964) += ti964.o
obj-$(CONFIG_VIDEO_MAX9286) += max9286.o
+obj-$(CONFIG_VIDEO_TI960) += ti960.o
diff --git a/drivers/media/i2c/ti960-reg.h b/drivers/media/i2c/ti960-reg.h
new file mode 100644
index 000000000000..260c17884c7d
--- /dev/null
+++ b/drivers/media/i2c/ti960-reg.h
@@ -0,0 +1,209 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2018 Intel Corporation */
+
+#ifndef TI960_REG_H
+#define TI960_REG_H
+
+struct ti960_register_write {
+ u8 reg;
+ u8 val;
+};
+
+static const struct ti960_register_write ti960_frame_sync_settings[2][5] = {
+ {
+ {0x18, 0x00}, /* Disable frame sync. */
+ {0x19, 0x00},
+ {0x1a, 0x02},
+ {0x1b, 0x0a},
+ {0x1c, 0xd3},
+ },
+ {
+ {0x19, 0x01}, /* Frame sync high time.*/
+ {0x1a, 0x15},
+ {0x1b, 0x09}, /* Frame sync low time. */
+ {0x1c, 0xC3},
+ {0x18, 0x01}, /* Enable frame sync. and use high/low mode */
+ }
+};
+
+static const struct ti960_register_write ti960_gpio_settings[] = {
+ {0x10, 0x81},
+ {0x11, 0x85},
+ {0x12, 0x89},
+ {0x13, 0x8d},
+};
+
+static const struct ti960_register_write ti960_init_settings[] = {
+ {0x0c, 0x0f}, /* RX_PORT_CTL */
+ {0x1f, 0x06}, /* CSI_PLL_CTL */
+ {0x4c, 0x01}, /* FPD3_PORT_SEL */
+ {0x58, 0x5e}, /* BCC_CONFIG */
+ {0x5c, 0xb0}, /* SER_ALIAS_ID */
+ {0x5d, 0x6c}, /* SlaveID[0] */
+ {0x65, 0x60}, /* SlaveAlias[0] */
+ {0x6d, 0x7c}, /* PORT_CONFIG */
+ {0x7c, 0x01}, /* PORT_CONFIG2 */
+ {0x70, 0x2b}, /* RAW10_ID */
+ {0x71, 0x2c}, /* RAW12_ID */
+ {0x72, 0xe4}, /* CSI_VC_MAP */
+ {0x4c, 0x12}, /* FPD3_PORT_SEL */
+ {0x58, 0x5e},
+ {0x5c, 0xb2},
+ {0x5d, 0x6c},
+ {0x65, 0x62},
+ {0x6d, 0x7c},
+ {0x7c, 0x01},
+ {0x70, 0x2b},
+ {0x71, 0x2c},
+ {0x72, 0xee}, /* CSI_VC_MAP */
+ {0x4c, 0x24}, /* FPD3_PORT_SEL */
+ {0x58, 0x5e},
+ {0x5c, 0xb4},
+ {0x5d, 0x6c},
+ {0x65, 0x64},
+ {0x6d, 0x7c},
+ {0x7c, 0x01},
+ {0x70, 0x2b},
+ {0x71, 0x2c},
+ {0x72, 0xe4},
+ {0x4c, 0x38}, /* FPD3_PORT_SEL */
+ {0x58, 0x5e},
+ {0x5c, 0xb6},
+ {0x5d, 0x6c},
+ {0x65, 0x66},
+ {0x6d, 0x7c},
+ {0x7c, 0x01},
+ {0x70, 0x2b},
+ {0x71, 0x2c},
+ {0x72, 0xe4},
+};
+
+static const struct ti960_register_write ti953_init_settings[] = {
+ {0x4c, 0x01},
+ {0xb0, 0x04},
+ {0xb1, 0x03},
+ {0xb2, 0x25},
+ {0xb1, 0x13},
+ {0xb2, 0x25},
+ {0xb0, 0x04},
+ {0xb1, 0x04},
+ {0xb2, 0x30},
+ {0xb1, 0x14},
+ {0xb2, 0x30},
+ {0xb0, 0x04},
+ {0xb1, 0x06},
+ {0xb2, 0x40},
+ {0x42, 0x01}, /* SLAVE_ID_ALIAS_1 */
+ {0x41, 0x93}, /* SLAVE_ID_ALIAS_0 */
+ {0x4c, 0x12},
+ {0xb0, 0x08},
+ {0xb1, 0x03},
+ {0xb2, 0x25},
+ {0xb1, 0x13},
+ {0xb2, 0x25},
+ {0xb0, 0x08},
+ {0xb1, 0x04},
+ {0xb2, 0x30},
+ {0xb1, 0x14},
+ {0xb2, 0x30},
+ {0xb0, 0x08},
+ {0xb1, 0x06},
+ {0xb2, 0x40},
+ {0x42, 0x01},
+ {0x41, 0x93},
+ {0x4c, 0x24},
+ {0xb0, 0x0c},
+ {0xb1, 0x03},
+ {0xb2, 0x25},
+ {0xb1, 0x13},
+ {0xb2, 0x25},
+ {0xb0, 0x0c},
+ {0xb1, 0x04},
+ {0xb2, 0x30},
+ {0xb1, 0x14},
+ {0xb2, 0x30},
+ {0xb0, 0x0c},
+ {0xb1, 0x06},
+ {0xb2, 0x40},
+ {0x42, 0x01},
+ {0x41, 0x93},
+ {0x4c, 0x38},
+ {0xb0, 0x10},
+ {0xb1, 0x03},
+ {0xb2, 0x25},
+ {0xb1, 0x13},
+ {0xb2, 0x25},
+ {0xb0, 0x10},
+ {0xb1, 0x04},
+ {0xb2, 0x30},
+ {0xb1, 0x14},
+ {0xb2, 0x30},
+ {0xb0, 0x10},
+ {0xb1, 0x06},
+ {0xb2, 0x40},
+ {0x42, 0x01},
+ {0x41, 0x93},
+};
+
+static const struct ti960_register_write ti960_init_settings_2[] = {
+ {0xb0, 0x14},
+ {0xb1, 0x03},
+ {0xb2, 0x04},
+ {0xb1, 0x04},
+ {0xb2, 0x04},
+ {0x4c, 0x01},
+ {0x32, 0x01},
+ {0x33, 0x03},
+ {0x32, 0x12},
+ {0x33, 0x03},
+ {0x20, 0x00},
+ {0x21, 0x03},
+};
+
+static const struct ti960_register_write ti953_init_settings_2[] = {
+ {0x06, 0x41},
+ {0x07, 0x28},
+ {0x0e, 0xf0},
+};
+
+/* register definition */
+#define TI960_DEVID 0x0
+#define TI960_RESET 0x1
+#define TI960_CSI_PLL_CTL 0x1f
+#define TI960_FS_CTL 0x18
+#define TI960_FWD_CTL1 0x20
+#define TI960_RX_PORT_SEL 0x4c
+#define TI960_SLAVE_ID0 0x5d
+#define TI960_SLAVE_ALIAS_ID0 0x65
+#define TI960_PORT_CONFIG 0x6d
+#define TI960_BC_GPIO_CTL0 0x6e
+#define TI960_RAW10_ID 0x70
+#define TI960_RAW12_ID 0x71
+#define TI960_PORT_CONFIG2 0x7c
+#define TI960_CSI_CTL 0x33
+
+/* register value definition */
+#define TI960_POWER_ON 0x1
+#define TI960_POWER_OFF 0x20
+#define TI960_FPD3_RAW10_100MHz 0x7f
+#define TI960_FPD3_RAW12_50MHz 0x7d
+#define TI960_FPD3_RAW12_75MHz 0x7e
+#define TI960_FPD3_CSI 0x7c
+#define TI960_RAW12 0x41
+#define TI960_RAW10_NORMAL 0x1
+#define TI960_RAW10_8BIT 0x81
+#define TI960_GPIO0_HIGH 0x09
+#define TI960_GPIO0_LOW 0x08
+#define TI960_GPIO1_HIGH 0x90
+#define TI960_GPIO1_LOW 0x80
+#define TI960_GPIO0_FSIN 0x0a
+#define TI960_GPIO1_FSIN 0xa0
+#define TI960_GPIO0_MASK 0x0f
+#define TI960_GPIO1_MASK 0xf0
+#define TI960_MIPI_800MBPS 0x2
+#define TI960_MIPI_1600MBPS 0x0
+#define TI960_CSI_ENABLE 0x1
+#define TI960_CSI_CONTS_CLOCK 0x2
+#define TI960_CSI_SKEWCAL 0x40
+#define TI960_FSIN_ENABLE 0x1
+#endif
diff --git a/drivers/media/i2c/ti960.c b/drivers/media/i2c/ti960.c
new file mode 100644
index 000000000000..2761dbe17bc8
--- /dev/null
+++ b/drivers/media/i2c/ti960.c
@@ -0,0 +1,1344 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2018 Intel Corporation
+
+#include <linux/device.h>
+#include <linux/gpio.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/wait.h>
+#include <linux/delay.h>
+#include <linux/platform_device.h>
+#include <linux/version.h>
+
+#include <media/media-device.h>
+#include <media/media-entity.h>
+#include <media/ti960.h>
+#include <media/crlmodule.h>
+#include <media/v4l2-device.h>
+#include <media/videobuf2-core.h>
+
+#include "ti960-reg.h"
+
+struct ti960_subdev {
+ struct v4l2_subdev *sd;
+ unsigned short rx_port;
+ unsigned short fsin_gpio;
+ unsigned short phy_i2c_addr;
+ unsigned short alias_i2c_addr;
+ char sd_name[16];
+};
+
+struct ti960 {
+ struct v4l2_subdev sd;
+ struct media_pad pad[NR_OF_TI960_PADS];
+ struct v4l2_ctrl_handler ctrl_handler;
+ struct ti960_pdata *pdata;
+ struct ti960_subdev sub_devs[NR_OF_TI960_SINK_PADS];
+ struct crlmodule_platform_data subdev_pdata[NR_OF_TI960_SINK_PADS];
+ const char *name;
+
+ struct mutex mutex;
+
+ struct regmap *regmap8;
+ struct regmap *regmap16;
+
+ struct v4l2_mbus_framefmt *ffmts[NR_OF_TI960_PADS];
+ struct rect *crop;
+ struct rect *compose;
+
+ struct v4l2_subdev_route *ti960_route;
+
+ unsigned int nsinks;
+ unsigned int nsources;
+ unsigned int nstreams;
+ unsigned int npads;
+
+ struct gpio_chip gc;
+
+ struct v4l2_ctrl *link_freq;
+ struct v4l2_ctrl *test_pattern;
+};
+
+#define to_ti960(_sd) container_of(_sd, struct ti960, sd)
+
+static const s64 ti960_op_sys_clock[] = {400000000, 800000000};
+static const u8 ti960_op_sys_clock_reg_val[] = {
+ TI960_MIPI_800MBPS,
+ TI960_MIPI_1600MBPS
+};
+
+/*
+ * Order matters.
+ *
+ * 1. Bits-per-pixel, descending.
+ * 2. Bits-per-pixel compressed, descending.
+ * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
+ * orders must be defined.
+ */
+static const struct ti960_csi_data_format va_csi_data_formats[] = {
+ { MEDIA_BUS_FMT_SGRBG16_1X16, 16, 16, PIXEL_ORDER_GRBG, 0x2e },
+ { MEDIA_BUS_FMT_SRGGB16_1X16, 16, 16, PIXEL_ORDER_RGGB, 0x2e },
+ { MEDIA_BUS_FMT_SBGGR16_1X16, 16, 16, PIXEL_ORDER_BGGR, 0x2e },
+ { MEDIA_BUS_FMT_SGBRG16_1X16, 16, 16, PIXEL_ORDER_GBRG, 0x2e },
+ { MEDIA_BUS_FMT_SGRBG12_1X12, 12, 12, PIXEL_ORDER_GRBG, 0x2c },
+ { MEDIA_BUS_FMT_SRGGB12_1X12, 12, 12, PIXEL_ORDER_RGGB, 0x2c },
+ { MEDIA_BUS_FMT_SBGGR12_1X12, 12, 12, PIXEL_ORDER_BGGR, 0x2c },
+ { MEDIA_BUS_FMT_SGBRG12_1X12, 12, 12, PIXEL_ORDER_GBRG, 0x2c },
+ { MEDIA_BUS_FMT_SGRBG10_1X10, 10, 10, PIXEL_ORDER_GRBG, 0x2b },
+ { MEDIA_BUS_FMT_SRGGB10_1X10, 10, 10, PIXEL_ORDER_RGGB, 0x2b },
+ { MEDIA_BUS_FMT_SBGGR10_1X10, 10, 10, PIXEL_ORDER_BGGR, 0x2b },
+ { MEDIA_BUS_FMT_SGBRG10_1X10, 10, 10, PIXEL_ORDER_GBRG, 0x2b },
+};
+
+static const uint32_t ti960_supported_codes_pad[] = {
+ MEDIA_BUS_FMT_SBGGR16_1X16,
+ MEDIA_BUS_FMT_SGBRG16_1X16,
+ MEDIA_BUS_FMT_SGRBG16_1X16,
+ MEDIA_BUS_FMT_SRGGB16_1X16,
+ MEDIA_BUS_FMT_SBGGR12_1X12,
+ MEDIA_BUS_FMT_SGBRG12_1X12,
+ MEDIA_BUS_FMT_SGRBG12_1X12,
+ MEDIA_BUS_FMT_SRGGB12_1X12,
+ MEDIA_BUS_FMT_SBGGR10_1X10,
+ MEDIA_BUS_FMT_SGBRG10_1X10,
+ MEDIA_BUS_FMT_SGRBG10_1X10,
+ MEDIA_BUS_FMT_SRGGB10_1X10,
+ 0,
+};
+
+static const uint32_t *ti960_supported_codes[] = {
+ ti960_supported_codes_pad,
+};
+
+static struct regmap_config ti960_reg_config8 = {
+ .reg_bits = 8,
+ .val_bits = 8,
+};
+
+static struct regmap_config ti960_reg_config16 = {
+ .reg_bits = 16,
+ .val_bits = 8,
+ .reg_format_endian = REGMAP_ENDIAN_BIG,
+};
+
+static int ti953_reg_write(struct ti960 *va, unsigned short rx_port,
+ unsigned char reg, unsigned char val)
+{
+ int ret;
+ int retry, timeout = 10;
+ struct i2c_client *client = v4l2_get_subdevdata(&va->sd);
+ unsigned short ser_alias = va->pdata->subdev_info[rx_port].ser_alias;
+
+ client->addr = ser_alias;
+ for (retry = 0; retry < timeout; retry++) {
+ ret = i2c_smbus_write_byte_data(client, reg, val);
+ if (ret < 0) {
+ dev_err(va->sd.dev, "ti953 reg write ret=%x", ret);
+ usleep_range(5000, 6000);
+ } else
+ break;
+ }
+
+ client->addr = TI960_I2C_ADDRESS;
+ if (retry >= timeout) {
+ dev_err(va->sd.dev,
+ "%s:write reg failed: port=%2x, addr=%2x, reg=%2x\n",
+ __func__, rx_port, ser_alias, reg);
+ return -EREMOTEIO;
+ }
+
+ return 0;
+}
+
+static int ti960_reg_read(struct ti960 *va, unsigned char reg, unsigned int *val)
+{
+ int ret, retry, timeout = 10;
+
+ for (retry = 0; retry < timeout; retry++) {
+ ret = regmap_read(va->regmap8, reg, val);
+ if (ret < 0) {
+ dev_err(va->sd.dev, "960 reg read ret=%x", ret);
+ usleep_range(5000, 6000);
+ } else {
+ break;
+ }
+ }
+
+ if (retry >= timeout) {
+ dev_err(va->sd.dev,
+ "%s:devid read failed: reg=%2x, ret=%d\n",
+ __func__, reg, ret);
+ return -EREMOTEIO;
+ }
+
+ return 0;
+}
+
+static int ti960_reg_set_bit(struct ti960 *va, unsigned char reg,
+ unsigned char bit, unsigned char val)
+{
+ int ret;
+ unsigned int reg_val;
+
+ ret = regmap_read(va->regmap8, reg, ®_val);
+ if (ret)
+ return ret;
+ if (val)
+ reg_val |= 1 << bit;
+ else
+ reg_val &= ~(1 << bit);
+
+ return regmap_write(va->regmap8, reg, reg_val);
+}
+
+static int ti960_map_phy_i2c_addr(struct ti960 *va, unsigned short rx_port,
+ unsigned short addr)
+{
+ int rval;
+
+ rval = regmap_write(va->regmap8, TI960_RX_PORT_SEL,
+ (rx_port << 4) + (1 << rx_port));
+ if (rval)
+ return rval;
+
+ return regmap_write(va->regmap8, TI960_SLAVE_ID0, addr);
+}
+
+static int ti960_map_alias_i2c_addr(struct ti960 *va, unsigned short rx_port,
+ unsigned short addr)
+{
+ int rval;
+
+ rval = regmap_write(va->regmap8, TI960_RX_PORT_SEL,
+ (rx_port << 4) + (1 << rx_port));
+ if (rval)
+ return rval;
+
+ return regmap_write(va->regmap8, TI960_SLAVE_ALIAS_ID0, addr);
+}
+
+static int ti960_fsin_gpio_init(struct ti960 *va, unsigned short rx_port,
+ unsigned short fsin_gpio)
+{
+ int rval;
+ int reg_val;
+
+ dev_dbg(va->sd.dev, "%s\n", __func__);
+ rval = regmap_read(va->regmap8, TI960_FS_CTL, ®_val);
+ if (rval) {
+ dev_dbg(va->sd.dev, "Failed to read gpio status.\n");
+ return rval;
+ }
+
+ if (!reg_val & TI960_FSIN_ENABLE) {
+ dev_dbg(va->sd.dev, "FSIN not enabled, skip config FSIN GPIO.\n");
+ return 0;
+ }
+
+ rval = regmap_write(va->regmap8, TI960_RX_PORT_SEL,
+ (rx_port << 4) + (1 << rx_port));
+ if (rval)
+ return rval;
+
+ rval = regmap_read(va->regmap8, TI960_BC_GPIO_CTL0, ®_val);
+ if (rval) {
+ dev_dbg(va->sd.dev, "Failed to read gpio status.\n");
+ return rval;
+ }
+
+ if (fsin_gpio == 0) {
+ reg_val &= ~TI960_GPIO0_MASK;
+ reg_val |= TI960_GPIO0_FSIN;
+ } else {
+ reg_val &= ~TI960_GPIO1_MASK;
+ reg_val |= TI960_GPIO1_FSIN;
+ }
+
+ rval = regmap_write(va->regmap8, TI960_BC_GPIO_CTL0, reg_val);
+ if (rval)
+ dev_dbg(va->sd.dev, "Failed to set gpio.\n");
+
+ return rval;
+}
+
+static int ti960_get_routing(struct v4l2_subdev *sd,
+ struct v4l2_subdev_routing *route)
+{
+ struct ti960 *va = to_ti960(sd);
+ int i;
+
+ for (i = 0; i < min(va->nstreams, route->num_routes); ++i) {
+ route->routes[i].sink_pad = va->ti960_route[i].sink_pad;
+ route->routes[i].sink_stream = va->ti960_route[i].sink_stream;
+ route->routes[i].source_pad = va->ti960_route[i].source_pad;
+ route->routes[i].source_stream = va->ti960_route[i].source_stream;
+ route->routes[i].flags = va->ti960_route[i].flags;
+ }
+
+ route->num_routes = i;
+
+ return 0;
+}
+
+static int ti960_set_routing(struct v4l2_subdev *sd,
+ struct v4l2_subdev_routing *route)
+{
+ struct ti960 *va = to_ti960(sd);
+ int i, j, ret = 0;
+
+ for (i = 0; i < min(route->num_routes, va->nstreams); ++i) {
+ struct v4l2_subdev_route *t = &route->routes[i];
+
+ if (t->sink_stream > va->nstreams - 1 ||
+ t->source_stream > va->nstreams - 1)
+ continue;
+
+ if (t->source_pad != TI960_PAD_SOURCE)
+ continue;
+
+ for (j = 0; j < va->nstreams; j++) {
+ if (t->sink_pad == va->ti960_route[j].sink_pad &&
+ t->source_pad == va->ti960_route[j].source_pad &&
+ t->sink_stream == va->ti960_route[j].sink_stream &&
+ t->source_stream == va->ti960_route[j].source_stream)
+ break;
+ }
+
+ if (j == va->nstreams)
+ continue;
+
+ if (t->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE)
+ va->ti960_route[j].flags |=
+ V4L2_SUBDEV_ROUTE_FL_ACTIVE;
+ else if (!(t->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
+ va->ti960_route[j].flags &=
+ (~V4L2_SUBDEV_ROUTE_FL_ACTIVE);
+ }
+
+ return ret;
+}
+
+static int ti960_enum_mbus_code(struct v4l2_subdev *sd,
+ struct v4l2_subdev_pad_config *cfg,
+ struct v4l2_subdev_mbus_code_enum *code)
+{
+ struct ti960 *va = to_ti960(sd);
+ const uint32_t *supported_code =
+ ti960_supported_codes[code->pad];
+ bool next_stream = false;
+ int i;
+
+ if (code->stream & V4L2_SUBDEV_FLAG_NEXT_STREAM) {
+ next_stream = true;
+ code->stream &= ~V4L2_SUBDEV_FLAG_NEXT_STREAM;
+ }
+
+ if (code->stream > va->nstreams)
+ return -EINVAL;
+
+ if (next_stream) {
+ if (!(va->pad[code->pad].flags & MEDIA_PAD_FL_MULTIPLEX))
+ return -EINVAL;
+ if (code->stream < va->nstreams - 1) {
+ code->stream++;
+ return 0;
+ } else {
+ return -EINVAL;
+ }
+ }
+
+ for (i = 0; supported_code[i]; i++) {
+ if (i == code->index) {
+ code->code = supported_code[i];
+ return 0;
+ }
+ }
+
+ return -EINVAL;
+}
+
+static const struct ti960_csi_data_format
+ *ti960_validate_csi_data_format(u32 code)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(va_csi_data_formats); i++) {
+ if (va_csi_data_formats[i].code == code)
+ return &va_csi_data_formats[i];
+ }
+
+ return &va_csi_data_formats[0];
+}
+
+static int ti960_get_frame_desc(struct v4l2_subdev *sd,
+ unsigned int pad, struct v4l2_mbus_frame_desc *desc)
+{
+ struct ti960 *va = to_ti960(sd);
+ struct v4l2_mbus_frame_desc_entry *entry = desc->entry;
+ u8 vc = 0;
+ int i;
+
+ desc->type = V4L2_MBUS_FRAME_DESC_TYPE_CSI2;
+ desc->num_entries = min_t(int, va->nstreams, V4L2_FRAME_DESC_ENTRY_MAX);
+
+ for (i = 0; i < desc->num_entries; i++) {
+ struct v4l2_mbus_framefmt *ffmt =
+ &va->ffmts[TI960_PAD_SOURCE][i];
+ const struct ti960_csi_data_format *csi_format =
+ ti960_validate_csi_data_format(ffmt->code);
+
+ entry->two_dim.width = ffmt->width;
+ entry->two_dim.height = ffmt->height;
+ entry->pixelcode = ffmt->code;
+ entry->bus.csi2.channel = vc++;
+ entry->bpp = csi_format->compressed;
+ entry++;
+ }
+
+ return 0;
+}
+
+static struct v4l2_mbus_framefmt *
+__ti960_get_ffmt(struct v4l2_subdev *subdev,
+ struct v4l2_subdev_pad_config *cfg,
+ unsigned int pad, unsigned int which,
+ unsigned int stream)
+{
+ struct ti960 *va = to_ti960(subdev);
+
+ if (which == V4L2_SUBDEV_FORMAT_TRY)
+ return v4l2_subdev_get_try_format(subdev, cfg, pad);
+ else
+ return &va->ffmts[pad][stream];
+}
+
+static int ti960_get_format(struct v4l2_subdev *subdev,
+ struct v4l2_subdev_pad_config *cfg,
+ struct v4l2_subdev_format *fmt)
+{
+ struct ti960 *va = to_ti960(subdev);
+
+ if (fmt->stream > va->nstreams)
+ return -EINVAL;
+
+ mutex_lock(&va->mutex);
+ fmt->format = *__ti960_get_ffmt(subdev, cfg, fmt->pad,
+ fmt->which, fmt->stream);
+ mutex_unlock(&va->mutex);
+
+ dev_dbg(subdev->dev, "subdev_format: which: %s, pad: %d, stream: %d.\n",
+ fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE ?
+ "V4L2_SUBDEV_FORMAT_ACTIVE" : "V4L2_SUBDEV_FORMAT_TRY",
+ fmt->pad, fmt->stream);
+
+ dev_dbg(subdev->dev, "framefmt: width: %d, height: %d, code: 0x%x.\n",
+ fmt->format.width, fmt->format.height, fmt->format.code);
+
+ return 0;
+}
+
+static int ti960_set_format(struct v4l2_subdev *subdev,
+ struct v4l2_subdev_pad_config *cfg,
+ struct v4l2_subdev_format *fmt)
+{
+ struct ti960 *va = to_ti960(subdev);
+ const struct ti960_csi_data_format *csi_format;
+ struct v4l2_mbus_framefmt *ffmt;
+
+ if (fmt->stream > va->nstreams)
+ return -EINVAL;
+
+ csi_format = ti960_validate_csi_data_format(
+ fmt->format.code);
+
+ mutex_lock(&va->mutex);
+ ffmt = __ti960_get_ffmt(subdev, cfg, fmt->pad, fmt->which,
+ fmt->stream);
+
+ if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
+ ffmt->width = fmt->format.width;
+ ffmt->height = fmt->format.height;
+ ffmt->code = csi_format->code;
+ }
+ fmt->format = *ffmt;
+ mutex_unlock(&va->mutex);
+
+ dev_dbg(subdev->dev, "framefmt: width: %d, height: %d, code: 0x%x.\n",
+ ffmt->width, ffmt->height, ffmt->code);
+
+ return 0;
+}
+
+static int ti960_open(struct v4l2_subdev *subdev,
+ struct v4l2_subdev_fh *fh)
+{
+ struct v4l2_mbus_framefmt *try_fmt =
+ v4l2_subdev_get_try_format(subdev, fh->pad, 0);
+
+ struct v4l2_subdev_format fmt = {
+ .which = V4L2_SUBDEV_FORMAT_TRY,
+ .pad = TI960_PAD_SOURCE,
+ .format = {
+ .width = TI960_MAX_WIDTH,
+ .height = TI960_MAX_HEIGHT,
+ .code = MEDIA_BUS_FMT_SBGGR12_1X12,
+ },
+ .stream = 0,
+ };
+
+ *try_fmt = fmt.format;
+
+ return 0;
+}
+
+static int ti960_registered(struct v4l2_subdev *subdev)
+{
+ struct ti960 *va = to_ti960(subdev);
+ struct i2c_client *client = v4l2_get_subdevdata(subdev);
+ int i, j, k, l, rval;
+
+ for (i = 0, k = 0; i < va->pdata->subdev_num; i++) {
+ struct ti960_subdev_info *info =
+ &va->pdata->subdev_info[i];
+ struct crlmodule_platform_data *pdata =
+ (struct crlmodule_platform_data *)
+ info->board_info.platform_data;
+
+ if (k >= va->nsinks)
+ break;
+
+ /*
+ * The sensors should not share the same pdata structure.
+ * Clone the pdata for each sensor.
+ */
+ memcpy(&va->subdev_pdata[k], pdata, sizeof(*pdata));
+ if (va->subdev_pdata[k].xshutdown != 0 &&
+ va->subdev_pdata[k].xshutdown != 1) {
+ dev_err(va->sd.dev, "xshutdown(%d) must be 0 or 1 to connect.\n",
+ va->subdev_pdata[k].xshutdown);
+ return -EINVAL;
+ }
+
+ /* If 0 is xshutdown, then 1 would be FSIN, vice versa. */
+ va->sub_devs[k].fsin_gpio = 1 - va->subdev_pdata[k].xshutdown;
+
+ /* Spin sensor subdev suffix name */
+ va->subdev_pdata[k].suffix = info->suffix;
+
+ /*
+ * Change the gpio value to have xshutdown
+ * and rx port included, so in gpio_set those
+ * can be caculated from it.
+ */
+ va->subdev_pdata[k].xshutdown += va->gc.base +
+ info->rx_port * NR_OF_GPIOS_PER_PORT;
+ info->board_info.platform_data = &va->subdev_pdata[k];
+
+ if (!info->phy_i2c_addr || !info->board_info.addr) {
+ dev_err(va->sd.dev, "can't find the physical and alias addr.\n");
+ return -EINVAL;
+ }
+
+ /* Map PHY I2C address. */
+ rval = ti960_map_phy_i2c_addr(va, info->rx_port,
+ info->phy_i2c_addr);
+ if (rval)
+ return rval;
+
+ /* Map 7bit ALIAS I2C address. */
+ rval = ti960_map_alias_i2c_addr(va, info->rx_port,
+ info->board_info.addr << 1);
+ if (rval)
+ return rval;
+
+ va->sub_devs[k].sd = v4l2_i2c_new_subdev_board(
+ va->sd.v4l2_dev, client->adapter,
+ &info->board_info, 0);
+ if (!va->sub_devs[k].sd) {
+ dev_err(va->sd.dev,
+ "can't create new i2c subdev %c\n",
+ info->suffix);
+ continue;
+ }
+ va->sub_devs[k].rx_port = info->rx_port;
+ va->sub_devs[k].phy_i2c_addr = info->phy_i2c_addr;
+ va->sub_devs[k].alias_i2c_addr = info->board_info.addr;
+ memcpy(va->sub_devs[k].sd_name,
+ va->subdev_pdata[k].module_name,
+ min(sizeof(va->sub_devs[k].sd_name) - 1,
+ sizeof(va->subdev_pdata[k].module_name) - 1));
+
+ for (j = 0; j < va->sub_devs[k].sd->entity.num_pads; j++) {
+ if (va->sub_devs[k].sd->entity.pads[j].flags &
+ MEDIA_PAD_FL_SOURCE)
+ break;
+ }
+
+ if (j == va->sub_devs[k].sd->entity.num_pads) {
+ dev_warn(va->sd.dev,
+ "no source pad in subdev %c\n",
+ info->suffix);
+ return -ENOENT;
+ }
+
+ for (l = 0; l < va->nsinks; l++) {
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
+ rval = media_entity_create_link(
+#else
+ rval = media_create_pad_link(
+#endif
+ &va->sub_devs[k].sd->entity, j,
+ &va->sd.entity, l, 0);
+ if (rval) {
+ dev_err(va->sd.dev,
+ "can't create link to %c\n",
+ info->suffix);
+ return -EINVAL;
+ }
+ }
+ k++;
+ }
+
+ return 0;
+}
+
+static int ti960_set_power(struct v4l2_subdev *subdev, int on)
+{
+ struct ti960 *va = to_ti960(subdev);
+ int ret;
+ u8 val;
+
+ ret = regmap_write(va->regmap8, TI960_RESET,
+ (on) ? TI960_POWER_ON : TI960_POWER_OFF);
+ if (ret || !on)
+ return ret;
+
+ /* Configure MIPI clock bsaed on control value. */
+ ret = regmap_write(va->regmap8, TI960_CSI_PLL_CTL,
+ ti960_op_sys_clock_reg_val[
+ v4l2_ctrl_g_ctrl(va->link_freq)]);
+ if (ret)
+ return ret;
+ val = TI960_CSI_ENABLE;
+ val |= TI960_CSI_CONTS_CLOCK;
+ /* Enable skew calculation when 1.6Gbps output is enabled. */
+ if (v4l2_ctrl_g_ctrl(va->link_freq))
+ val |= TI960_CSI_SKEWCAL;
+ return regmap_write(va->regmap8, TI960_CSI_CTL, val);
+}
+
+static bool ti960_broadcast_mode(struct v4l2_subdev *subdev)
+{
+ struct ti960 *va = to_ti960(subdev);
+ struct v4l2_subdev_format fmt = { 0 };
+ struct v4l2_subdev *sd;
+ char *sd_name = NULL;
+ bool first = true;
+ unsigned int h = 0, w = 0, code = 0;
+ bool single_stream = true;
+ int i, rval;
+
+ for (i = 0; i < NR_OF_TI960_SINK_PADS; i++) {
+ struct media_pad *remote_pad =
+ media_entity_remote_pad(&va->pad[i]);
+
+ if (!remote_pad)
+ continue;
+
+ sd = media_entity_to_v4l2_subdev(remote_pad->entity);
+ fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
+ fmt.pad = remote_pad->index;
+ fmt.stream = 0;
+
+ rval = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
+ if (rval)
+ return false;
+
+ if (first) {
+ sd_name = va->sub_devs[i].sd_name;
+ h = fmt.format.height;
+ w = fmt.format.width;
+ code = fmt.format.code;
+ first = false;
+ } else {
+ if (strncmp(sd_name, va->sub_devs[i].sd_name, 16))
+ return false;
+
+ if (h != fmt.format.height || w != fmt.format.width
+ || code != fmt.format.code)
+ return false;
+
+ single_stream = false;
+ }
+ }
+
+ if (single_stream)
+ return false;
+
+ return true;
+}
+
+static int ti960_rx_port_config(struct ti960 *va, int sink, int rx_port)
+{
+ int rval;
+
+ /* Select RX port. */
+ rval = regmap_write(va->regmap8, TI960_RX_PORT_SEL,
+ (rx_port << 4) + (1 << rx_port));
+ if (rval) {
+ dev_err(va->sd.dev, "Failed to select RX port.\n");
+ return rval;
+ }
+
+ rval = regmap_write(va->regmap8, TI960_PORT_CONFIG,
+ TI960_FPD3_CSI);
+ if (rval) {
+ dev_err(va->sd.dev, "Failed to set port config.\n");
+ return rval;
+ }
+
+ /*
+ * TODO: CSI VC MAPPING.
+ */
+
+ return 0;
+}
+
+static int ti960_map_subdevs_addr(struct ti960 *va)
+{
+ unsigned short rx_port, phy_i2c_addr, alias_i2c_addr;
+ int i, rval;
+
+ for (i = 0; i < NR_OF_TI960_SINK_PADS; i++) {
+ rx_port = va->sub_devs[i].rx_port;
+ phy_i2c_addr = va->sub_devs[i].phy_i2c_addr;
+ alias_i2c_addr = va->sub_devs[i].alias_i2c_addr;
+
+ if (!phy_i2c_addr || !alias_i2c_addr)
+ continue;
+
+ rval = ti960_map_phy_i2c_addr(va, rx_port, phy_i2c_addr);
+ if (rval)
+ return rval;
+
+ /* set 7bit alias i2c addr */
+ rval = ti960_map_alias_i2c_addr(va, rx_port,
+ alias_i2c_addr << 1);
+ if (rval)
+ return rval;
+ }
+
+ return 0;