forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathgc2145.c
1487 lines (1315 loc) · 48.7 KB
/
gc2145.c
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 Galaxycore GC2145 camera.
* Copyright (C) 2023, STMicroelectronics SA
*
* Inspired by the imx219.c driver
*
* Datasheet v1.0 available at http://files.pine64.org/doc/datasheet/PinebookPro/GC2145%20CSP%20DataSheet%20release%20V1.0_20131201.pdf
*/
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <linux/units.h>
#include <media/mipi-csi2.h>
#include <media/v4l2-cci.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-mediabus.h>
/* Chip ID */
#define GC2145_CHIP_ID 0x2145
/* Page 0 */
#define GC2145_REG_EXPOSURE CCI_REG16(0x03)
#define GC2145_REG_HBLANK CCI_REG16(0x05)
#define GC2145_REG_VBLANK CCI_REG16(0x07)
#define GC2145_REG_ROW_START CCI_REG16(0x09)
#define GC2145_REG_COL_START CCI_REG16(0x0b)
#define GC2145_REG_WIN_HEIGHT CCI_REG16(0x0d)
#define GC2145_REG_WIN_WIDTH CCI_REG16(0x0f)
#define GC2145_REG_ANALOG_MODE1 CCI_REG8(0x17)
#define GC2145_REG_OUTPUT_FMT CCI_REG8(0x84)
#define GC2145_REG_SYNC_MODE CCI_REG8(0x86)
#define GC2145_SYNC_MODE_COL_SWITCH BIT(4)
#define GC2145_SYNC_MODE_ROW_SWITCH BIT(5)
#define GC2145_REG_BYPASS_MODE CCI_REG8(0x89)
#define GC2145_BYPASS_MODE_SWITCH BIT(5)
#define GC2145_REG_DEBUG_MODE2 CCI_REG8(0x8c)
#define GC2145_REG_DEBUG_MODE3 CCI_REG8(0x8d)
#define GC2145_REG_CROP_ENABLE CCI_REG8(0x90)
#define GC2145_REG_CROP_Y CCI_REG16(0x91)
#define GC2145_REG_CROP_X CCI_REG16(0x93)
#define GC2145_REG_CROP_HEIGHT CCI_REG16(0x95)
#define GC2145_REG_CROP_WIDTH CCI_REG16(0x97)
#define GC2145_REG_GLOBAL_GAIN CCI_REG8(0xb0)
#define GC2145_REG_CHIP_ID CCI_REG16(0xf0)
#define GC2145_REG_PAD_IO CCI_REG8(0xf2)
#define GC2145_REG_PAGE_SELECT CCI_REG8(0xfe)
/* Page 3 */
#define GC2145_REG_DPHY_ANALOG_MODE1 CCI_REG8(0x01)
#define GC2145_DPHY_MODE_PHY_CLK_EN BIT(0)
#define GC2145_DPHY_MODE_PHY_LANE0_EN BIT(1)
#define GC2145_DPHY_MODE_PHY_LANE1_EN BIT(2)
#define GC2145_DPHY_MODE_PHY_CLK_LANE_P2S_SEL BIT(7)
#define GC2145_REG_DPHY_ANALOG_MODE2 CCI_REG8(0x02)
#define GC2145_DPHY_CLK_DIFF(a) ((a) & 0x07)
#define GC2145_DPHY_LANE0_DIFF(a) (((a) & 0x07) << 4)
#define GC2145_REG_DPHY_ANALOG_MODE3 CCI_REG8(0x03)
#define GC2145_DPHY_LANE1_DIFF(a) ((a) & 0x07)
#define GC2145_DPHY_CLK_DELAY BIT(4)
#define GC2145_DPHY_LANE0_DELAY BIT(5)
#define GC2145_DPHY_LANE1_DELAY BIT(6)
#define GC2145_REG_FIFO_FULL_LVL CCI_REG16_LE(0x04)
#define GC2145_REG_FIFO_MODE CCI_REG8(0x06)
#define GC2145_FIFO_MODE_READ_GATE BIT(3)
#define GC2145_FIFO_MODE_MIPI_CLK_MODULE BIT(7)
#define GC2145_REG_BUF_CSI2_MODE CCI_REG8(0x10)
#define GC2145_CSI2_MODE_DOUBLE BIT(0)
#define GC2145_CSI2_MODE_RAW8 BIT(2)
#define GC2145_CSI2_MODE_MIPI_EN BIT(4)
#define GC2145_CSI2_MODE_EN BIT(7)
#define GC2145_REG_MIPI_DT CCI_REG8(0x11)
#define GC2145_REG_LWC CCI_REG16_LE(0x12)
#define GC2145_REG_DPHY_MODE CCI_REG8(0x15)
#define GC2145_DPHY_MODE_TRIGGER_PROG BIT(4)
#define GC2145_REG_FIFO_GATE_MODE CCI_REG8(0x17)
#define GC2145_REG_T_LPX CCI_REG8(0x21)
#define GC2145_REG_T_CLK_HS_PREPARE CCI_REG8(0x22)
#define GC2145_REG_T_CLK_ZERO CCI_REG8(0x23)
#define GC2145_REG_T_CLK_PRE CCI_REG8(0x24)
#define GC2145_REG_T_CLK_POST CCI_REG8(0x25)
#define GC2145_REG_T_CLK_TRAIL CCI_REG8(0x26)
#define GC2145_REG_T_HS_EXIT CCI_REG8(0x27)
#define GC2145_REG_T_WAKEUP CCI_REG8(0x28)
#define GC2145_REG_T_HS_PREPARE CCI_REG8(0x29)
#define GC2145_REG_T_HS_ZERO CCI_REG8(0x2a)
#define GC2145_REG_T_HS_TRAIL CCI_REG8(0x2b)
/* External clock frequency is 24.0MHz */
#define GC2145_XCLK_FREQ (24 * HZ_PER_MHZ)
#define GC2145_NATIVE_WIDTH 1616U
#define GC2145_NATIVE_HEIGHT 1232U
/**
* struct gc2145_mode - GC2145 mode description
* @width: frame width (in pixels)
* @height: frame height (in pixels)
* @reg_seq: registers config sequence to enter into the mode
* @reg_seq_size: size of the sequence
* @pixel_rate: pixel rate associated with the mode
* @crop: window area captured
* @hblank: default horizontal blanking
* @vblank: default vertical blanking
* @link_freq_index: index within the link frequency menu
*/
struct gc2145_mode {
unsigned int width;
unsigned int height;
const struct cci_reg_sequence *reg_seq;
size_t reg_seq_size;
unsigned long pixel_rate;
struct v4l2_rect crop;
unsigned int hblank;
unsigned int vblank;
unsigned int link_freq_index;
};
#define GC2145_DEFAULT_EXPOSURE 0x04e2
#define GC2145_DEFAULT_GLOBAL_GAIN 0x55
static const struct cci_reg_sequence gc2145_common_regs[] = {
{GC2145_REG_PAGE_SELECT, 0x00},
/* SH Delay */
{CCI_REG8(0x12), 0x2e},
/* Flip */
{GC2145_REG_ANALOG_MODE1, 0x14},
/* Analog Conf */
{CCI_REG8(0x18), 0x22}, {CCI_REG8(0x19), 0x0e}, {CCI_REG8(0x1a), 0x01},
{CCI_REG8(0x1b), 0x4b}, {CCI_REG8(0x1c), 0x07}, {CCI_REG8(0x1d), 0x10},
{CCI_REG8(0x1e), 0x88}, {CCI_REG8(0x1f), 0x78}, {CCI_REG8(0x20), 0x03},
{CCI_REG8(0x21), 0x40}, {CCI_REG8(0x22), 0xa0}, {CCI_REG8(0x24), 0x16},
{CCI_REG8(0x25), 0x01}, {CCI_REG8(0x26), 0x10}, {CCI_REG8(0x2d), 0x60},
{CCI_REG8(0x30), 0x01}, {CCI_REG8(0x31), 0x90}, {CCI_REG8(0x33), 0x06},
{CCI_REG8(0x34), 0x01},
/* ISP related */
{CCI_REG8(0x80), 0x7f}, {CCI_REG8(0x81), 0x26}, {CCI_REG8(0x82), 0xfa},
{CCI_REG8(0x83), 0x00}, {CCI_REG8(0x84), 0x02}, {CCI_REG8(0x86), 0x02},
{CCI_REG8(0x88), 0x03},
{GC2145_REG_BYPASS_MODE, 0x03},
{CCI_REG8(0x85), 0x08}, {CCI_REG8(0x8a), 0x00}, {CCI_REG8(0x8b), 0x00},
{GC2145_REG_GLOBAL_GAIN, GC2145_DEFAULT_GLOBAL_GAIN},
{CCI_REG8(0xc3), 0x00}, {CCI_REG8(0xc4), 0x80}, {CCI_REG8(0xc5), 0x90},
{CCI_REG8(0xc6), 0x3b}, {CCI_REG8(0xc7), 0x46},
/* BLK */
{GC2145_REG_PAGE_SELECT, 0x00},
{CCI_REG8(0x40), 0x42}, {CCI_REG8(0x41), 0x00}, {CCI_REG8(0x43), 0x5b},
{CCI_REG8(0x5e), 0x00}, {CCI_REG8(0x5f), 0x00}, {CCI_REG8(0x60), 0x00},
{CCI_REG8(0x61), 0x00}, {CCI_REG8(0x62), 0x00}, {CCI_REG8(0x63), 0x00},
{CCI_REG8(0x64), 0x00}, {CCI_REG8(0x65), 0x00}, {CCI_REG8(0x66), 0x20},
{CCI_REG8(0x67), 0x20}, {CCI_REG8(0x68), 0x20}, {CCI_REG8(0x69), 0x20},
{CCI_REG8(0x76), 0x00}, {CCI_REG8(0x6a), 0x08}, {CCI_REG8(0x6b), 0x08},
{CCI_REG8(0x6c), 0x08}, {CCI_REG8(0x6d), 0x08}, {CCI_REG8(0x6e), 0x08},
{CCI_REG8(0x6f), 0x08}, {CCI_REG8(0x70), 0x08}, {CCI_REG8(0x71), 0x08},
{CCI_REG8(0x76), 0x00}, {CCI_REG8(0x72), 0xf0}, {CCI_REG8(0x7e), 0x3c},
{CCI_REG8(0x7f), 0x00},
{GC2145_REG_PAGE_SELECT, 0x02},
{CCI_REG8(0x48), 0x15}, {CCI_REG8(0x49), 0x00}, {CCI_REG8(0x4b), 0x0b},
/* AEC */
{GC2145_REG_PAGE_SELECT, 0x00},
{GC2145_REG_EXPOSURE, GC2145_DEFAULT_EXPOSURE},
{GC2145_REG_PAGE_SELECT, 0x01},
{CCI_REG8(0x01), 0x04}, {CCI_REG8(0x02), 0xc0}, {CCI_REG8(0x03), 0x04},
{CCI_REG8(0x04), 0x90}, {CCI_REG8(0x05), 0x30}, {CCI_REG8(0x06), 0x90},
{CCI_REG8(0x07), 0x30}, {CCI_REG8(0x08), 0x80}, {CCI_REG8(0x09), 0x00},
{CCI_REG8(0x0a), 0x82}, {CCI_REG8(0x0b), 0x11}, {CCI_REG8(0x0c), 0x10},
{CCI_REG8(0x11), 0x10}, {CCI_REG8(0x13), 0x7b}, {CCI_REG8(0x17), 0x00},
{CCI_REG8(0x1c), 0x11}, {CCI_REG8(0x1e), 0x61}, {CCI_REG8(0x1f), 0x35},
{CCI_REG8(0x20), 0x40}, {CCI_REG8(0x22), 0x40}, {CCI_REG8(0x23), 0x20},
{GC2145_REG_PAGE_SELECT, 0x02},
{CCI_REG8(0x0f), 0x04},
{GC2145_REG_PAGE_SELECT, 0x01},
{CCI_REG8(0x12), 0x35}, {CCI_REG8(0x15), 0xb0}, {CCI_REG8(0x10), 0x31},
{CCI_REG8(0x3e), 0x28}, {CCI_REG8(0x3f), 0xb0}, {CCI_REG8(0x40), 0x90},
{CCI_REG8(0x41), 0x0f},
/* INTPEE */
{GC2145_REG_PAGE_SELECT, 0x02},
{CCI_REG8(0x90), 0x6c}, {CCI_REG8(0x91), 0x03}, {CCI_REG8(0x92), 0xcb},
{CCI_REG8(0x94), 0x33}, {CCI_REG8(0x95), 0x84}, {CCI_REG8(0x97), 0x65},
{CCI_REG8(0xa2), 0x11},
/* DNDD */
{GC2145_REG_PAGE_SELECT, 0x02},
{CCI_REG8(0x80), 0xc1}, {CCI_REG8(0x81), 0x08}, {CCI_REG8(0x82), 0x05},
{CCI_REG8(0x83), 0x08}, {CCI_REG8(0x84), 0x0a}, {CCI_REG8(0x86), 0xf0},
{CCI_REG8(0x87), 0x50}, {CCI_REG8(0x88), 0x15}, {CCI_REG8(0x89), 0xb0},
{CCI_REG8(0x8a), 0x30}, {CCI_REG8(0x8b), 0x10},
/* ASDE */
{GC2145_REG_PAGE_SELECT, 0x01},
{CCI_REG8(0x21), 0x04},
{GC2145_REG_PAGE_SELECT, 0x02},
{CCI_REG8(0xa3), 0x50}, {CCI_REG8(0xa4), 0x20}, {CCI_REG8(0xa5), 0x40},
{CCI_REG8(0xa6), 0x80}, {CCI_REG8(0xab), 0x40}, {CCI_REG8(0xae), 0x0c},
{CCI_REG8(0xb3), 0x46}, {CCI_REG8(0xb4), 0x64}, {CCI_REG8(0xb6), 0x38},
{CCI_REG8(0xb7), 0x01}, {CCI_REG8(0xb9), 0x2b}, {CCI_REG8(0x3c), 0x04},
{CCI_REG8(0x3d), 0x15}, {CCI_REG8(0x4b), 0x06}, {CCI_REG8(0x4c), 0x20},
/* Gamma */
{GC2145_REG_PAGE_SELECT, 0x02},
{CCI_REG8(0x10), 0x09}, {CCI_REG8(0x11), 0x0d}, {CCI_REG8(0x12), 0x13},
{CCI_REG8(0x13), 0x19}, {CCI_REG8(0x14), 0x27}, {CCI_REG8(0x15), 0x37},
{CCI_REG8(0x16), 0x45}, {CCI_REG8(0x17), 0x53}, {CCI_REG8(0x18), 0x69},
{CCI_REG8(0x19), 0x7d}, {CCI_REG8(0x1a), 0x8f}, {CCI_REG8(0x1b), 0x9d},
{CCI_REG8(0x1c), 0xa9}, {CCI_REG8(0x1d), 0xbd}, {CCI_REG8(0x1e), 0xcd},
{CCI_REG8(0x1f), 0xd9}, {CCI_REG8(0x20), 0xe3}, {CCI_REG8(0x21), 0xea},
{CCI_REG8(0x22), 0xef}, {CCI_REG8(0x23), 0xf5}, {CCI_REG8(0x24), 0xf9},
{CCI_REG8(0x25), 0xff},
{GC2145_REG_PAGE_SELECT, 0x00},
{CCI_REG8(0xc6), 0x20}, {CCI_REG8(0xc7), 0x2b},
/* Gamma 2 */
{GC2145_REG_PAGE_SELECT, 0x02},
{CCI_REG8(0x26), 0x0f}, {CCI_REG8(0x27), 0x14}, {CCI_REG8(0x28), 0x19},
{CCI_REG8(0x29), 0x1e}, {CCI_REG8(0x2a), 0x27}, {CCI_REG8(0x2b), 0x33},
{CCI_REG8(0x2c), 0x3b}, {CCI_REG8(0x2d), 0x45}, {CCI_REG8(0x2e), 0x59},
{CCI_REG8(0x2f), 0x69}, {CCI_REG8(0x30), 0x7c}, {CCI_REG8(0x31), 0x89},
{CCI_REG8(0x32), 0x98}, {CCI_REG8(0x33), 0xae}, {CCI_REG8(0x34), 0xc0},
{CCI_REG8(0x35), 0xcf}, {CCI_REG8(0x36), 0xda}, {CCI_REG8(0x37), 0xe2},
{CCI_REG8(0x38), 0xe9}, {CCI_REG8(0x39), 0xf3}, {CCI_REG8(0x3a), 0xf9},
{CCI_REG8(0x3b), 0xff},
/* YCP */
{GC2145_REG_PAGE_SELECT, 0x02},
{CCI_REG8(0xd1), 0x32}, {CCI_REG8(0xd2), 0x32}, {CCI_REG8(0xd3), 0x40},
{CCI_REG8(0xd6), 0xf0}, {CCI_REG8(0xd7), 0x10}, {CCI_REG8(0xd8), 0xda},
{CCI_REG8(0xdd), 0x14}, {CCI_REG8(0xde), 0x86}, {CCI_REG8(0xed), 0x80},
{CCI_REG8(0xee), 0x00}, {CCI_REG8(0xef), 0x3f}, {CCI_REG8(0xd8), 0xd8},
/* ABS */
{GC2145_REG_PAGE_SELECT, 0x01},
{CCI_REG8(0x9f), 0x40},
/* LSC */
{GC2145_REG_PAGE_SELECT, 0x01},
{CCI_REG8(0xc2), 0x14}, {CCI_REG8(0xc3), 0x0d}, {CCI_REG8(0xc4), 0x0c},
{CCI_REG8(0xc8), 0x15}, {CCI_REG8(0xc9), 0x0d}, {CCI_REG8(0xca), 0x0a},
{CCI_REG8(0xbc), 0x24}, {CCI_REG8(0xbd), 0x10}, {CCI_REG8(0xbe), 0x0b},
{CCI_REG8(0xb6), 0x25}, {CCI_REG8(0xb7), 0x16}, {CCI_REG8(0xb8), 0x15},
{CCI_REG8(0xc5), 0x00}, {CCI_REG8(0xc6), 0x00}, {CCI_REG8(0xc7), 0x00},
{CCI_REG8(0xcb), 0x00}, {CCI_REG8(0xcc), 0x00}, {CCI_REG8(0xcd), 0x00},
{CCI_REG8(0xbf), 0x07}, {CCI_REG8(0xc0), 0x00}, {CCI_REG8(0xc1), 0x00},
{CCI_REG8(0xb9), 0x00}, {CCI_REG8(0xba), 0x00}, {CCI_REG8(0xbb), 0x00},
{CCI_REG8(0xaa), 0x01}, {CCI_REG8(0xab), 0x01}, {CCI_REG8(0xac), 0x00},
{CCI_REG8(0xad), 0x05}, {CCI_REG8(0xae), 0x06}, {CCI_REG8(0xaf), 0x0e},
{CCI_REG8(0xb0), 0x0b}, {CCI_REG8(0xb1), 0x07}, {CCI_REG8(0xb2), 0x06},
{CCI_REG8(0xb3), 0x17}, {CCI_REG8(0xb4), 0x0e}, {CCI_REG8(0xb5), 0x0e},
{CCI_REG8(0xd0), 0x09}, {CCI_REG8(0xd1), 0x00}, {CCI_REG8(0xd2), 0x00},
{CCI_REG8(0xd6), 0x08}, {CCI_REG8(0xd7), 0x00}, {CCI_REG8(0xd8), 0x00},
{CCI_REG8(0xd9), 0x00}, {CCI_REG8(0xda), 0x00}, {CCI_REG8(0xdb), 0x00},
{CCI_REG8(0xd3), 0x0a}, {CCI_REG8(0xd4), 0x00}, {CCI_REG8(0xd5), 0x00},
{CCI_REG8(0xa4), 0x00}, {CCI_REG8(0xa5), 0x00}, {CCI_REG8(0xa6), 0x77},
{CCI_REG8(0xa7), 0x77}, {CCI_REG8(0xa8), 0x77}, {CCI_REG8(0xa9), 0x77},
{CCI_REG8(0xa1), 0x80}, {CCI_REG8(0xa2), 0x80},
{GC2145_REG_PAGE_SELECT, 0x01},
{CCI_REG8(0xdf), 0x0d}, {CCI_REG8(0xdc), 0x25}, {CCI_REG8(0xdd), 0x30},
{CCI_REG8(0xe0), 0x77}, {CCI_REG8(0xe1), 0x80}, {CCI_REG8(0xe2), 0x77},
{CCI_REG8(0xe3), 0x90}, {CCI_REG8(0xe6), 0x90}, {CCI_REG8(0xe7), 0xa0},
{CCI_REG8(0xe8), 0x90}, {CCI_REG8(0xe9), 0xa0},
/* AWB */
/* measure window */
{GC2145_REG_PAGE_SELECT, 0x00},
{CCI_REG8(0xec), 0x06}, {CCI_REG8(0xed), 0x04}, {CCI_REG8(0xee), 0x60},
{CCI_REG8(0xef), 0x90}, {CCI_REG8(0xb6), 0x01},
{GC2145_REG_PAGE_SELECT, 0x01},
{CCI_REG8(0x4f), 0x00}, {CCI_REG8(0x4f), 0x00}, {CCI_REG8(0x4b), 0x01},
{CCI_REG8(0x4f), 0x00},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x71}, {CCI_REG8(0x4e), 0x01},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x91}, {CCI_REG8(0x4e), 0x01},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x70}, {CCI_REG8(0x4e), 0x01},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x90}, {CCI_REG8(0x4e), 0x02},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xb0}, {CCI_REG8(0x4e), 0x02},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8f}, {CCI_REG8(0x4e), 0x02},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x6f}, {CCI_REG8(0x4e), 0x02},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xaf}, {CCI_REG8(0x4e), 0x02},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xd0}, {CCI_REG8(0x4e), 0x02},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xf0}, {CCI_REG8(0x4e), 0x02},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xcf}, {CCI_REG8(0x4e), 0x02},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xef}, {CCI_REG8(0x4e), 0x02},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x6e}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8e}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xae}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xce}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x4d}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x6d}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8d}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xad}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xcd}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x4c}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x6c}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8c}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xac}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xcc}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xcb}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x4b}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x6b}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8b}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xab}, {CCI_REG8(0x4e), 0x03},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8a}, {CCI_REG8(0x4e), 0x04},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xaa}, {CCI_REG8(0x4e), 0x04},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xca}, {CCI_REG8(0x4e), 0x04},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xca}, {CCI_REG8(0x4e), 0x04},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xc9}, {CCI_REG8(0x4e), 0x04},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8a}, {CCI_REG8(0x4e), 0x04},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x89}, {CCI_REG8(0x4e), 0x04},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xa9}, {CCI_REG8(0x4e), 0x04},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x0b}, {CCI_REG8(0x4e), 0x05},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x0a}, {CCI_REG8(0x4e), 0x05},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xeb}, {CCI_REG8(0x4e), 0x05},
{CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xea}, {CCI_REG8(0x4e), 0x05},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x09}, {CCI_REG8(0x4e), 0x05},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x29}, {CCI_REG8(0x4e), 0x05},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x2a}, {CCI_REG8(0x4e), 0x05},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x4a}, {CCI_REG8(0x4e), 0x05},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x8a}, {CCI_REG8(0x4e), 0x06},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x49}, {CCI_REG8(0x4e), 0x06},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x69}, {CCI_REG8(0x4e), 0x06},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x89}, {CCI_REG8(0x4e), 0x06},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xa9}, {CCI_REG8(0x4e), 0x06},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x48}, {CCI_REG8(0x4e), 0x06},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x68}, {CCI_REG8(0x4e), 0x06},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x69}, {CCI_REG8(0x4e), 0x06},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xca}, {CCI_REG8(0x4e), 0x07},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xc9}, {CCI_REG8(0x4e), 0x07},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xe9}, {CCI_REG8(0x4e), 0x07},
{CCI_REG8(0x4c), 0x03}, {CCI_REG8(0x4d), 0x09}, {CCI_REG8(0x4e), 0x07},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xc8}, {CCI_REG8(0x4e), 0x07},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xe8}, {CCI_REG8(0x4e), 0x07},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xa7}, {CCI_REG8(0x4e), 0x07},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xc7}, {CCI_REG8(0x4e), 0x07},
{CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xe7}, {CCI_REG8(0x4e), 0x07},
{CCI_REG8(0x4c), 0x03}, {CCI_REG8(0x4d), 0x07}, {CCI_REG8(0x4e), 0x07},
{CCI_REG8(0x4f), 0x01},
{CCI_REG8(0x50), 0x80}, {CCI_REG8(0x51), 0xa8}, {CCI_REG8(0x52), 0x47},
{CCI_REG8(0x53), 0x38}, {CCI_REG8(0x54), 0xc7}, {CCI_REG8(0x56), 0x0e},
{CCI_REG8(0x58), 0x08}, {CCI_REG8(0x5b), 0x00}, {CCI_REG8(0x5c), 0x74},
{CCI_REG8(0x5d), 0x8b}, {CCI_REG8(0x61), 0xdb}, {CCI_REG8(0x62), 0xb8},
{CCI_REG8(0x63), 0x86}, {CCI_REG8(0x64), 0xc0}, {CCI_REG8(0x65), 0x04},
{CCI_REG8(0x67), 0xa8}, {CCI_REG8(0x68), 0xb0}, {CCI_REG8(0x69), 0x00},
{CCI_REG8(0x6a), 0xa8}, {CCI_REG8(0x6b), 0xb0}, {CCI_REG8(0x6c), 0xaf},
{CCI_REG8(0x6d), 0x8b}, {CCI_REG8(0x6e), 0x50}, {CCI_REG8(0x6f), 0x18},
{CCI_REG8(0x73), 0xf0}, {CCI_REG8(0x70), 0x0d}, {CCI_REG8(0x71), 0x60},
{CCI_REG8(0x72), 0x80}, {CCI_REG8(0x74), 0x01}, {CCI_REG8(0x75), 0x01},
{CCI_REG8(0x7f), 0x0c}, {CCI_REG8(0x76), 0x70}, {CCI_REG8(0x77), 0x58},
{CCI_REG8(0x78), 0xa0}, {CCI_REG8(0x79), 0x5e}, {CCI_REG8(0x7a), 0x54},
{CCI_REG8(0x7b), 0x58},
/* CC */
{GC2145_REG_PAGE_SELECT, 0x02},
{CCI_REG8(0xc0), 0x01}, {CCI_REG8(0xc1), 0x44}, {CCI_REG8(0xc2), 0xfd},
{CCI_REG8(0xc3), 0x04}, {CCI_REG8(0xc4), 0xf0}, {CCI_REG8(0xc5), 0x48},
{CCI_REG8(0xc6), 0xfd}, {CCI_REG8(0xc7), 0x46}, {CCI_REG8(0xc8), 0xfd},
{CCI_REG8(0xc9), 0x02}, {CCI_REG8(0xca), 0xe0}, {CCI_REG8(0xcb), 0x45},
{CCI_REG8(0xcc), 0xec}, {CCI_REG8(0xcd), 0x48}, {CCI_REG8(0xce), 0xf0},
{CCI_REG8(0xcf), 0xf0}, {CCI_REG8(0xe3), 0x0c}, {CCI_REG8(0xe4), 0x4b},
{CCI_REG8(0xe5), 0xe0},
/* ABS */
{GC2145_REG_PAGE_SELECT, 0x01},
{CCI_REG8(0x9f), 0x40},
/* Dark sun */
{GC2145_REG_PAGE_SELECT, 0x02},
{CCI_REG8(0x40), 0xbf}, {CCI_REG8(0x46), 0xcf},
};
#define GC2145_640_480_PIXELRATE 30000000
#define GC2145_640_480_LINKFREQ 120000000
#define GC2145_640_480_HBLANK 0x0130
#define GC2145_640_480_VBLANK 0x000c
static const struct cci_reg_sequence gc2145_mode_640_480_regs[] = {
{GC2145_REG_PAGE_SELECT, 0xf0}, {GC2145_REG_PAGE_SELECT, 0xf0},
{GC2145_REG_PAGE_SELECT, 0xf0}, {CCI_REG8(0xfc), 0x06},
{CCI_REG8(0xf6), 0x00}, {CCI_REG8(0xf7), 0x1d}, {CCI_REG8(0xf8), 0x86},
{CCI_REG8(0xfa), 0x00}, {CCI_REG8(0xf9), 0x8e},
/* Disable PAD IO */
{GC2145_REG_PAD_IO, 0x00},
{GC2145_REG_PAGE_SELECT, 0x00},
/* Row/Col start - 0/0 */
{GC2145_REG_ROW_START, 0x0000},
{GC2145_REG_COL_START, 0x0000},
/* Window size 1216/1618 */
{GC2145_REG_WIN_HEIGHT, 0x04c0},
{GC2145_REG_WIN_WIDTH, 0x0652},
/* Scalar more */
{CCI_REG8(0xfd), 0x01}, {CCI_REG8(0xfa), 0x00},
/* Crop 640-480@0-0 */
{GC2145_REG_CROP_ENABLE, 0x01},
{GC2145_REG_CROP_Y, 0x0000},
{GC2145_REG_CROP_X, 0x0000},
{GC2145_REG_CROP_HEIGHT, 0x01e0},
{GC2145_REG_CROP_WIDTH, 0x0280},
/* Subsampling configuration */
{CCI_REG8(0x99), 0x55}, {CCI_REG8(0x9a), 0x06}, {CCI_REG8(0x9b), 0x01},
{CCI_REG8(0x9c), 0x23}, {CCI_REG8(0x9d), 0x00}, {CCI_REG8(0x9e), 0x00},
{CCI_REG8(0x9f), 0x01}, {CCI_REG8(0xa0), 0x23}, {CCI_REG8(0xa1), 0x00},
{CCI_REG8(0xa2), 0x00},
{GC2145_REG_PAGE_SELECT, 0x01},
/* AEC anti-flicker */
{CCI_REG16(0x25), 0x0175},
/* AEC exposure level 1-5 */
{CCI_REG16(0x27), 0x045f}, {CCI_REG16(0x29), 0x045f},
{CCI_REG16(0x2b), 0x045f}, {CCI_REG16(0x2d), 0x045f},
};
#define GC2145_1280_720_PIXELRATE 48000000
#define GC2145_1280_720_LINKFREQ 192000000
#define GC2145_1280_720_HBLANK 0x0156
#define GC2145_1280_720_VBLANK 0x0011
static const struct cci_reg_sequence gc2145_mode_1280_720_regs[] = {
{GC2145_REG_PAGE_SELECT, 0xf0}, {GC2145_REG_PAGE_SELECT, 0xf0},
{GC2145_REG_PAGE_SELECT, 0xf0}, {CCI_REG8(0xfc), 0x06},
{CCI_REG8(0xf6), 0x00}, {CCI_REG8(0xf7), 0x1d}, {CCI_REG8(0xf8), 0x83},
{CCI_REG8(0xfa), 0x00}, {CCI_REG8(0xf9), 0x8e},
/* Disable PAD IO */
{GC2145_REG_PAD_IO, 0x00},
{GC2145_REG_PAGE_SELECT, 0x00},
/* Row/Col start - 240/160 */
{GC2145_REG_ROW_START, 0x00f0},
{GC2145_REG_COL_START, 0x00a0},
/* Window size 736/1296 */
{GC2145_REG_WIN_HEIGHT, 0x02e0},
{GC2145_REG_WIN_WIDTH, 0x0510},
/* Crop 1280-720@0-0 */
{GC2145_REG_CROP_ENABLE, 0x01},
{GC2145_REG_CROP_Y, 0x0000},
{GC2145_REG_CROP_X, 0x0000},
{GC2145_REG_CROP_HEIGHT, 0x02d0},
{GC2145_REG_CROP_WIDTH, 0x0500},
{GC2145_REG_PAGE_SELECT, 0x01},
/* AEC anti-flicker */
{CCI_REG16(0x25), 0x00e6},
/* AEC exposure level 1-5 */
{CCI_REG16(0x27), 0x02b2}, {CCI_REG16(0x29), 0x02b2},
{CCI_REG16(0x2b), 0x02b2}, {CCI_REG16(0x2d), 0x02b2},
};
#define GC2145_1600_1200_PIXELRATE 60000000
#define GC2145_1600_1200_LINKFREQ 240000000
#define GC2145_1600_1200_HBLANK 0x0156
#define GC2145_1600_1200_VBLANK 0x0010
static const struct cci_reg_sequence gc2145_mode_1600_1200_regs[] = {
{GC2145_REG_PAGE_SELECT, 0xf0}, {GC2145_REG_PAGE_SELECT, 0xf0},
{GC2145_REG_PAGE_SELECT, 0xf0}, {CCI_REG8(0xfc), 0x06},
{CCI_REG8(0xf6), 0x00}, {CCI_REG8(0xf7), 0x1d}, {CCI_REG8(0xf8), 0x84},
{CCI_REG8(0xfa), 0x00}, {CCI_REG8(0xf9), 0x8e},
/* Disable PAD IO */
{GC2145_REG_PAD_IO, 0x00},
{GC2145_REG_PAGE_SELECT, 0x00},
/* Row/Col start - 0/0 */
{GC2145_REG_ROW_START, 0x0000},
{GC2145_REG_COL_START, 0x0000},
/* Window size: 1216/1618 */
{GC2145_REG_WIN_HEIGHT, 0x04c0},
{GC2145_REG_WIN_WIDTH, 0x0652},
/* Crop 1600-1200@0-0 */
{GC2145_REG_CROP_ENABLE, 0x01},
{GC2145_REG_CROP_Y, 0x0000},
{GC2145_REG_CROP_X, 0x0000},
{GC2145_REG_CROP_HEIGHT, 0x04b0},
{GC2145_REG_CROP_WIDTH, 0x0640},
{GC2145_REG_PAGE_SELECT, 0x01},
/* AEC anti-flicker */
{CCI_REG16(0x25), 0x00fa},
/* AEC exposure level 1-5 */
{CCI_REG16(0x27), 0x04e2}, {CCI_REG16(0x29), 0x04e2},
{CCI_REG16(0x2b), 0x04e2}, {CCI_REG16(0x2d), 0x04e2},
};
static const s64 gc2145_link_freq_menu[] = {
GC2145_640_480_LINKFREQ,
GC2145_1280_720_LINKFREQ,
GC2145_1600_1200_LINKFREQ,
};
/* Regulators supplies */
static const char * const gc2145_supply_name[] = {
"iovdd", /* Digital I/O (1.7-3V) suppply */
"avdd", /* Analog (2.7-3V) supply */
"dvdd", /* Digital Core (1.7-1.9V) supply */
};
#define GC2145_NUM_SUPPLIES ARRAY_SIZE(gc2145_supply_name)
/* Mode configs */
#define GC2145_MODE_640X480 0
#define GC2145_MODE_1280X720 1
#define GC2145_MODE_1600X1200 2
static const struct gc2145_mode supported_modes[] = {
{
/* 640x480 30fps mode */
.width = 640,
.height = 480,
.reg_seq = gc2145_mode_640_480_regs,
.reg_seq_size = ARRAY_SIZE(gc2145_mode_640_480_regs),
.pixel_rate = GC2145_640_480_PIXELRATE,
.crop = {
.top = 0,
.left = 0,
.width = 640,
.height = 480,
},
.hblank = GC2145_640_480_HBLANK,
.vblank = GC2145_640_480_VBLANK,
.link_freq_index = GC2145_MODE_640X480,
},
{
/* 1280x720 30fps mode */
.width = 1280,
.height = 720,
.reg_seq = gc2145_mode_1280_720_regs,
.reg_seq_size = ARRAY_SIZE(gc2145_mode_1280_720_regs),
.pixel_rate = GC2145_1280_720_PIXELRATE,
.crop = {
.top = 160,
.left = 240,
.width = 1280,
.height = 720,
},
.hblank = GC2145_1280_720_HBLANK,
.vblank = GC2145_1280_720_VBLANK,
.link_freq_index = GC2145_MODE_1280X720,
},
{
/* 1600x1200 20fps mode */
.width = 1600,
.height = 1200,
.reg_seq = gc2145_mode_1600_1200_regs,
.reg_seq_size = ARRAY_SIZE(gc2145_mode_1600_1200_regs),
.pixel_rate = GC2145_1600_1200_PIXELRATE,
.crop = {
.top = 0,
.left = 0,
.width = 1600,
.height = 1200,
},
.hblank = GC2145_1600_1200_HBLANK,
.vblank = GC2145_1600_1200_VBLANK,
.link_freq_index = GC2145_MODE_1600X1200,
},
};
/**
* struct gc2145_format - GC2145 pixel format description
* @code: media bus (MBUS) associated code
* @colorspace: V4L2 colorspace
* @datatype: MIPI CSI2 data type
* @output_fmt: GC2145 output format
* @switch_bit: GC2145 first/second switch
* @row_col_switch: GC2145 switch row and/or column
*/
struct gc2145_format {
unsigned int code;
unsigned int colorspace;
unsigned char datatype;
unsigned char output_fmt;
bool switch_bit;
unsigned char row_col_switch;
};
/* All supported formats */
static const struct gc2145_format supported_formats[] = {
{
.code = MEDIA_BUS_FMT_UYVY8_1X16,
.colorspace = V4L2_COLORSPACE_SRGB,
.datatype = MIPI_CSI2_DT_YUV422_8B,
.output_fmt = 0x00,
},
{
.code = MEDIA_BUS_FMT_VYUY8_1X16,
.colorspace = V4L2_COLORSPACE_SRGB,
.datatype = MIPI_CSI2_DT_YUV422_8B,
.output_fmt = 0x01,
},
{
.code = MEDIA_BUS_FMT_YUYV8_1X16,
.colorspace = V4L2_COLORSPACE_SRGB,
.datatype = MIPI_CSI2_DT_YUV422_8B,
.output_fmt = 0x02,
},
{
.code = MEDIA_BUS_FMT_YVYU8_1X16,
.colorspace = V4L2_COLORSPACE_SRGB,
.datatype = MIPI_CSI2_DT_YUV422_8B,
.output_fmt = 0x03,
},
{
.code = MEDIA_BUS_FMT_RGB565_1X16,
.colorspace = V4L2_COLORSPACE_SRGB,
.datatype = MIPI_CSI2_DT_RGB565,
.output_fmt = 0x06,
.switch_bit = true,
},
{
.code = MEDIA_BUS_FMT_SGRBG8_1X8,
.colorspace = V4L2_COLORSPACE_RAW,
.datatype = MIPI_CSI2_DT_RAW8,
.output_fmt = 0x17,
.row_col_switch = GC2145_SYNC_MODE_COL_SWITCH,
},
{
.code = MEDIA_BUS_FMT_SRGGB8_1X8,
.colorspace = V4L2_COLORSPACE_RAW,
.datatype = MIPI_CSI2_DT_RAW8,
.output_fmt = 0x17,
.row_col_switch = GC2145_SYNC_MODE_COL_SWITCH | GC2145_SYNC_MODE_ROW_SWITCH,
},
{
.code = MEDIA_BUS_FMT_SBGGR8_1X8,
.colorspace = V4L2_COLORSPACE_RAW,
.datatype = MIPI_CSI2_DT_RAW8,
.output_fmt = 0x17,
.row_col_switch = 0,
},
{
.code = MEDIA_BUS_FMT_SGBRG8_1X8,
.colorspace = V4L2_COLORSPACE_RAW,
.datatype = MIPI_CSI2_DT_RAW8,
.output_fmt = 0x17,
.row_col_switch = GC2145_SYNC_MODE_ROW_SWITCH,
},
};
struct gc2145_ctrls {
struct v4l2_ctrl_handler handler;
struct v4l2_ctrl *pixel_rate;
struct v4l2_ctrl *link_freq;
struct v4l2_ctrl *test_pattern;
struct v4l2_ctrl *hflip;
struct v4l2_ctrl *vflip;
struct v4l2_ctrl *hblank;
struct v4l2_ctrl *vblank;
};
struct gc2145 {
struct v4l2_subdev sd;
struct media_pad pad;
struct regmap *regmap;
struct clk *xclk;
struct gpio_desc *reset_gpio;
struct gpio_desc *powerdown_gpio;
struct regulator_bulk_data supplies[GC2145_NUM_SUPPLIES];
/* V4L2 controls */
struct gc2145_ctrls ctrls;
/* Current mode */
const struct gc2145_mode *mode;
};
static inline struct gc2145 *to_gc2145(struct v4l2_subdev *_sd)
{
return container_of(_sd, struct gc2145, sd);
}
static inline struct v4l2_subdev *gc2145_ctrl_to_sd(struct v4l2_ctrl *ctrl)
{
return &container_of(ctrl->handler, struct gc2145,
ctrls.handler)->sd;
}
static const struct gc2145_format *
gc2145_get_format_code(struct gc2145 *gc2145, u32 code)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
if (supported_formats[i].code == code)
break;
}
if (i >= ARRAY_SIZE(supported_formats))
i = 0;
return &supported_formats[i];
}
static void gc2145_update_pad_format(struct gc2145 *gc2145,
const struct gc2145_mode *mode,
struct v4l2_mbus_framefmt *fmt, u32 code,
u32 colorspace)
{
fmt->code = code;
fmt->width = mode->width;
fmt->height = mode->height;
fmt->field = V4L2_FIELD_NONE;
fmt->colorspace = V4L2_COLORSPACE_SRGB;
fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
}
static int gc2145_init_state(struct v4l2_subdev *sd,
struct v4l2_subdev_state *state)
{
struct gc2145 *gc2145 = to_gc2145(sd);
struct v4l2_mbus_framefmt *format;
struct v4l2_rect *crop;
/* Initialize pad format */
format = v4l2_subdev_state_get_format(state, 0);
gc2145_update_pad_format(gc2145, &supported_modes[0], format,
MEDIA_BUS_FMT_RGB565_1X16,
V4L2_COLORSPACE_SRGB);
/* Initialize crop rectangle. */
crop = v4l2_subdev_state_get_crop(state, 0);
*crop = supported_modes[0].crop;
return 0;
}
static int gc2145_get_selection(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_selection *sel)
{
switch (sel->target) {
case V4L2_SEL_TGT_CROP:
sel->r = *v4l2_subdev_state_get_crop(sd_state, 0);
return 0;
case V4L2_SEL_TGT_NATIVE_SIZE:
sel->r.top = 0;
sel->r.left = 0;
sel->r.width = GC2145_NATIVE_WIDTH;
sel->r.height = GC2145_NATIVE_HEIGHT;
return 0;
case V4L2_SEL_TGT_CROP_DEFAULT:
case V4L2_SEL_TGT_CROP_BOUNDS:
sel->r.top = 0;
sel->r.left = 0;
sel->r.width = 1600;
sel->r.height = 1200;
return 0;
}
return -EINVAL;
}
static int gc2145_enum_mbus_code(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_mbus_code_enum *code)
{
if (code->index >= ARRAY_SIZE(supported_formats))
return -EINVAL;
code->code = supported_formats[code->index].code;
return 0;
}
static int gc2145_enum_frame_size(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_frame_size_enum *fse)
{
struct gc2145 *gc2145 = to_gc2145(sd);
const struct gc2145_format *gc2145_format;
u32 code;
if (fse->index >= ARRAY_SIZE(supported_modes))
return -EINVAL;
gc2145_format = gc2145_get_format_code(gc2145, fse->code);
code = gc2145_format->code;
if (fse->code != code)
return -EINVAL;
fse->min_width = supported_modes[fse->index].width;
fse->max_width = fse->min_width;
fse->min_height = supported_modes[fse->index].height;
fse->max_height = fse->min_height;
return 0;
}
static int gc2145_set_pad_format(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_format *fmt)
{
struct gc2145 *gc2145 = to_gc2145(sd);
const struct gc2145_mode *mode;
const struct gc2145_format *gc2145_fmt;
struct v4l2_mbus_framefmt *framefmt;
struct gc2145_ctrls *ctrls = &gc2145->ctrls;
struct v4l2_rect *crop;
gc2145_fmt = gc2145_get_format_code(gc2145, fmt->format.code);
mode = v4l2_find_nearest_size(supported_modes,
ARRAY_SIZE(supported_modes),
width, height,
fmt->format.width, fmt->format.height);
/* In RAW mode, VGA is not possible so use 720p instead */
if (gc2145_fmt->colorspace == V4L2_COLORSPACE_RAW &&
mode == &supported_modes[GC2145_MODE_640X480])
mode = &supported_modes[GC2145_MODE_1280X720];
gc2145_update_pad_format(gc2145, mode, &fmt->format, gc2145_fmt->code,
gc2145_fmt->colorspace);
framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
gc2145->mode = mode;
/* Update pixel_rate based on the mode */
__v4l2_ctrl_s_ctrl_int64(ctrls->pixel_rate, mode->pixel_rate);
/* Update link_freq based on the mode */
__v4l2_ctrl_s_ctrl(ctrls->link_freq, mode->link_freq_index);
/* Update hblank/vblank based on the mode */
__v4l2_ctrl_s_ctrl(ctrls->hblank, mode->hblank);
__v4l2_ctrl_s_ctrl(ctrls->vblank, mode->vblank);
}
*framefmt = fmt->format;
crop = v4l2_subdev_state_get_crop(sd_state, fmt->pad);
*crop = mode->crop;
return 0;
}
static const struct cci_reg_sequence gc2145_common_mipi_regs[] = {
{GC2145_REG_PAGE_SELECT, 0x03},
{GC2145_REG_DPHY_ANALOG_MODE1, GC2145_DPHY_MODE_PHY_CLK_EN |
GC2145_DPHY_MODE_PHY_LANE0_EN |
GC2145_DPHY_MODE_PHY_LANE1_EN |
GC2145_DPHY_MODE_PHY_CLK_LANE_P2S_SEL},
{GC2145_REG_DPHY_ANALOG_MODE2, GC2145_DPHY_CLK_DIFF(2) |
GC2145_DPHY_LANE0_DIFF(2)},
{GC2145_REG_DPHY_ANALOG_MODE3, GC2145_DPHY_LANE1_DIFF(0) |
GC2145_DPHY_CLK_DELAY},
{GC2145_REG_FIFO_MODE, GC2145_FIFO_MODE_READ_GATE |
GC2145_FIFO_MODE_MIPI_CLK_MODULE},
{GC2145_REG_DPHY_MODE, GC2145_DPHY_MODE_TRIGGER_PROG},
/* Clock & Data lanes timing */
{GC2145_REG_T_LPX, 0x10},
{GC2145_REG_T_CLK_HS_PREPARE, 0x04}, {GC2145_REG_T_CLK_ZERO, 0x10},
{GC2145_REG_T_CLK_PRE, 0x10}, {GC2145_REG_T_CLK_POST, 0x10},
{GC2145_REG_T_CLK_TRAIL, 0x05},
{GC2145_REG_T_HS_PREPARE, 0x03}, {GC2145_REG_T_HS_ZERO, 0x0a},
{GC2145_REG_T_HS_TRAIL, 0x06},
};
static int gc2145_config_mipi_mode(struct gc2145 *gc2145,
const struct gc2145_format *gc2145_format)
{
u16 lwc, fifo_full_lvl;
int ret = 0;
/* Common MIPI settings */
cci_multi_reg_write(gc2145->regmap, gc2145_common_mipi_regs,
ARRAY_SIZE(gc2145_common_mipi_regs), &ret);
/*
* Adjust the MIPI buffer settings.
* For YUV/RGB, LWC = image width * 2
* For RAW8, LWC = image width
* For RAW10, LWC = image width * 1.25
*/
if (gc2145_format->colorspace != V4L2_COLORSPACE_RAW)
lwc = gc2145->mode->width * 2;
else
lwc = gc2145->mode->width;
cci_write(gc2145->regmap, GC2145_REG_LWC, lwc, &ret);
/*
* Adjust the MIPI FIFO Full Level
* 640x480 RGB: 0x0190
* 1280x720 / 1600x1200 (aka no scaler) non RAW: 0x0001
* 1600x1200 RAW: 0x0190
*/
if (gc2145_format->colorspace != V4L2_COLORSPACE_RAW) {
if (gc2145->mode->width == 1280 || gc2145->mode->width == 1600)
fifo_full_lvl = 0x0001;
else
fifo_full_lvl = 0x0190;
} else {
fifo_full_lvl = 0x0190;
}
cci_write(gc2145->regmap, GC2145_REG_FIFO_FULL_LVL,
fifo_full_lvl, &ret);
/*
* Set the FIFO gate mode / MIPI wdiv set:
* 0xf1 in case of RAW mode and 0xf0 otherwise
*/
cci_write(gc2145->regmap, GC2145_REG_FIFO_GATE_MODE,
gc2145_format->colorspace == V4L2_COLORSPACE_RAW ?
0xf1 : 0xf0, &ret);
/* Set the MIPI data type */
cci_write(gc2145->regmap, GC2145_REG_MIPI_DT,
gc2145_format->datatype, &ret);
/* Configure mode and enable CSI */
cci_write(gc2145->regmap, GC2145_REG_BUF_CSI2_MODE,
GC2145_CSI2_MODE_RAW8 | GC2145_CSI2_MODE_DOUBLE |
GC2145_CSI2_MODE_EN | GC2145_CSI2_MODE_MIPI_EN, &ret);
return ret;
}
static int gc2145_enable_streams(struct v4l2_subdev *sd,
struct v4l2_subdev_state *state, u32 pad,
u64 streams_mask)
{
struct gc2145 *gc2145 = to_gc2145(sd);
struct i2c_client *client = v4l2_get_subdevdata(&gc2145->sd);
const struct gc2145_format *gc2145_format;
struct v4l2_mbus_framefmt *fmt;
int ret;
ret = pm_runtime_resume_and_get(&client->dev);
if (ret < 0)
return ret;
/* Apply default values of current mode */
cci_multi_reg_write(gc2145->regmap, gc2145->mode->reg_seq,
gc2145->mode->reg_seq_size, &ret);
cci_multi_reg_write(gc2145->regmap, gc2145_common_regs,
ARRAY_SIZE(gc2145_common_regs), &ret);
if (ret) {
dev_err(&client->dev, "%s failed to write regs\n", __func__);
goto err_rpm_put;
}
fmt = v4l2_subdev_state_get_format(state, 0);
gc2145_format = gc2145_get_format_code(gc2145, fmt->code);
/* Set the output format */
cci_write(gc2145->regmap, GC2145_REG_PAGE_SELECT, 0x00, &ret);
cci_write(gc2145->regmap, GC2145_REG_OUTPUT_FMT,
gc2145_format->output_fmt, &ret);
cci_update_bits(gc2145->regmap, GC2145_REG_BYPASS_MODE,
GC2145_BYPASS_MODE_SWITCH,
gc2145_format->switch_bit ? GC2145_BYPASS_MODE_SWITCH
: 0, &ret);
cci_update_bits(gc2145->regmap, GC2145_REG_SYNC_MODE,
GC2145_SYNC_MODE_COL_SWITCH |
GC2145_SYNC_MODE_ROW_SWITCH,
gc2145_format->row_col_switch, &ret);
if (ret) {
dev_err(&client->dev, "%s failed to write regs\n", __func__);
goto err_rpm_put;
}
/* Apply customized values from user */
ret = __v4l2_ctrl_handler_setup(&gc2145->ctrls.handler);
if (ret) {
dev_err(&client->dev, "%s failed to apply ctrls\n", __func__);
goto err_rpm_put;
}
/* Perform MIPI specific configuration */
ret = gc2145_config_mipi_mode(gc2145, gc2145_format);
if (ret) {
dev_err(&client->dev, "%s failed to write mipi conf\n",
__func__);
goto err_rpm_put;
}
cci_write(gc2145->regmap, GC2145_REG_PAGE_SELECT, 0x00, &ret);
return 0;
err_rpm_put:
pm_runtime_mark_last_busy(&client->dev);
pm_runtime_put_autosuspend(&client->dev);
return ret;
}
static int gc2145_disable_streams(struct v4l2_subdev *sd,
struct v4l2_subdev_state *state, u32 pad,
u64 streams_mask)
{
struct gc2145 *gc2145 = to_gc2145(sd);
struct i2c_client *client = v4l2_get_subdevdata(&gc2145->sd);
int ret = 0;
/* Disable lanes & mipi streaming */
cci_write(gc2145->regmap, GC2145_REG_PAGE_SELECT, 0x03, &ret);
cci_update_bits(gc2145->regmap, GC2145_REG_BUF_CSI2_MODE,
GC2145_CSI2_MODE_EN | GC2145_CSI2_MODE_MIPI_EN, 0,
&ret);
cci_write(gc2145->regmap, GC2145_REG_PAGE_SELECT, 0x00, &ret);
if (ret)
dev_err(&client->dev, "%s failed to write regs\n", __func__);
pm_runtime_mark_last_busy(&client->dev);
pm_runtime_put_autosuspend(&client->dev);
return ret;
}
/* Power/clock management functions */
static int gc2145_power_on(struct device *dev)
{
struct v4l2_subdev *sd = dev_get_drvdata(dev);
struct gc2145 *gc2145 = to_gc2145(sd);
int ret;