hc
2023-03-21 4b55d97acc464242bcd6a8ae77b8ff37c22dec58
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2018 Rockchip Electronics Co. Ltd.
 *
 * Author: Wyon Bi <bivvy.bi@rock-chips.com>
 */
 
#include <linux/clk.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/regmap.h>
#include <linux/mfd/rk618.h>
 
#include <drm/drmP.h>
#include <drm/drm_of.h>
#include <drm/drm_atomic.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_mipi_dsi.h>
#include <drm/drm_panel.h>
 
#include <video/of_display_timing.h>
#include <video/mipi_display.h>
#include <video/videomode.h>
#include <asm/unaligned.h>
 
#include "../rockchip_drm_drv.h"
 
#define HOSTREG(x)        ((x) + 0x1000)
#define DSI_VERSION        HOSTREG(0x0000)
#define DSI_PWR_UP        HOSTREG(0x0004)
#define SHUTDOWNZ        BIT(0)
#define POWER_UP        BIT(0)
#define RESET            0
#define DSI_CLKMGR_CFG        HOSTREG(0x0008)
#define TO_CLK_DIVIDSION(x)    UPDATE(x, 15, 8)
#define TX_ESC_CLK_DIVIDSION(x)    UPDATE(x, 7, 0)
#define DSI_DPI_CFG        HOSTREG(0x000c)
#define EN18_LOOSELY        BIT(10)
#define COLORM_ACTIVE_LOW    BIT(9)
#define SHUTD_ACTIVE_LOW    BIT(8)
#define HSYNC_ACTIVE_LOW    BIT(7)
#define VSYNC_ACTIVE_LOW    BIT(6)
#define DATAEN_ACTIVE_LOW    BIT(5)
#define DPI_COLOR_CODING(x)    UPDATE(x, 4, 2)
#define DPI_VID(x)        UPDATE(x, 1, 0)
#define DSI_PCKHDL_CFG        HOSTREG(0x0018)
#define GEN_VID_RX(x)        UPDATE(x, 6, 5)
#define EN_CRC_RX        BIT(4)
#define EN_ECC_RX        BIT(3)
#define EN_BTA            BIT(2)
#define EN_EOTP_RX        BIT(1)
#define EN_EOTP_TX        BIT(0)
#define DSI_VID_MODE_CFG    HOSTREG(0x001c)
#define LPCMDEN            BIT(12)
#define FRAME_BTA_ACK        BIT(11)
#define EN_NULL_PKT        BIT(10)
#define EN_MULTI_PKT        BIT(9)
#define EN_LP_HFP        BIT(8)
#define EN_LP_HBP        BIT(7)
#define EN_LP_VACT        BIT(6)
#define EN_LP_VFP        BIT(5)
#define EN_LP_VBP        BIT(4)
#define EN_LP_VSA        BIT(3)
#define VID_MODE_TYPE(x)    UPDATE(x, 2, 1)
#define EN_VIDEO_MODE        BIT(0)
#define DSI_VID_PKT_CFG        HOSTREG(0x0020)
#define NULL_PKT_SIZE(x)    UPDATE(x, 30, 21)
#define NUM_CHUNKS(x)        UPDATE(x, 20, 11)
#define VID_PKT_SIZE(x)        UPDATE(x, 10, 0)
#define DSI_CMD_MODE_CFG    HOSTREG(0x0024)
#define TEAR_FX_EN        BIT(14)
#define ACK_RQST_EN        BIT(13)
#define DCS_LW_TX        BIT(12)
#define GEN_LW_TX        BIT(11)
#define MAX_RD_PKT_SIZE        BIT(10)
#define DCS_SR_0P_TX        BIT(9)
#define DCS_SW_1P_TX        BIT(8)
#define DCS_SW_0P_TX        BIT(7)
#define GEN_SR_2P_TX        BIT(6)
#define GEN_SR_1P_TX        BIT(5)
#define GEN_SR_0P_TX        BIT(4)
#define GEN_SW_2P_TX        BIT(3)
#define GEN_SW_1P_TX        BIT(2)
#define GEN_SW_0P_TX        BIT(1)
#define EN_CMD_MODE        BIT(0)
#define DSI_TMR_LINE_CFG    HOSTREG(0x0028)
#define HLINE_TIME(x)        UPDATE(x, 31, 18)
#define HBP_TIME(x)        UPDATE(x, 17, 9)
#define HSA_TIME(x)        UPDATE(x, 8, 0)
#define DSI_VTIMING_CFG        HOSTREG(0x002c)
#define V_ACTIVE_LINES(x)    UPDATE(x, 26, 16)
#define VFP_LINES(x)        UPDATE(x, 15, 10)
#define VBP_LINES(x)        UPDATE(x, 9, 4)
#define VSA_LINES(x)        UPDATE(x, 3, 0)
#define DSI_PHY_TMR_CFG        HOSTREG(0x0030)
#define PHY_HS2LP_TIME(x)    UPDATE(x, 31, 24)
#define PHY_LP2HS_TIME(x)    UPDATE(x, 23, 16)
#define MAX_RD_TIME(x)        UPDATE(x, 14, 0)
#define DSI_GEN_HDR        HOSTREG(0x0034)
#define DSI_GEN_PLD_DATA    HOSTREG(0x0038)
#define DSI_GEN_PKT_STATUS    HOSTREG(0x003c)
#define GEN_RD_CMD_BUSY        BIT(6)
#define GEN_PLD_R_FULL        BIT(5)
#define GEN_PLD_R_EMPTY        BIT(4)
#define GEN_PLD_W_FULL        BIT(3)
#define GEN_PLD_W_EMPTY        BIT(2)
#define GEN_CMD_FULL        BIT(1)
#define GEN_CMD_EMPTY        BIT(0)
#define DSI_TO_CNT_CFG        HOSTREG(0x0040)
#define LPRX_TO_CNT(x)        UPDATE(x, 31, 16)
#define HSTX_TO_CNT(x)        UPDATE(x, 15, 0)
#define DSI_INT_ST0        HOSTREG(0x0044)
#define DSI_INT_ST1        HOSTREG(0x0048)
#define DSI_INT_MSK0        HOSTREG(0x004c)
#define DSI_INT_MSK1        HOSTREG(0x0050)
#define DSI_PHY_RSTZ        HOSTREG(0x0054)
#define PHY_ENABLECLK        BIT(2)
#define DSI_PHY_IF_CFG        HOSTREG(0x0058)
#define PHY_STOP_WAIT_TIME(x)    UPDATE(x, 9, 2)
#define N_LANES(x)        UPDATE(x, 1, 0)
#define DSI_PHY_IF_CTRL        HOSTREG(0x005c)
#define PHY_TX_TRIGGERS(x)    UPDATE(x, 8, 5)
#define PHY_TXEXITULPSLAN    BIT(4)
#define PHY_TXREQULPSLAN    BIT(3)
#define PHY_TXEXITULPSCLK    BIT(2)
#define PHY_RXREQULPSCLK    BIT(1)
#define PHY_TXREQUESCLKHS    BIT(0)
#define DSI_PHY_STATUS        HOSTREG(0x0060)
#define ULPSACTIVENOT3LANE    BIT(12)
#define PHYSTOPSTATE3LANE    BIT(11)
#define ULPSACTIVENOT2LANE    BIT(10)
#define PHYSTOPSTATE2LANE    BIT(9)
#define ULPSACTIVENOT1LANE    BIT(8)
#define PHYSTOPSTATE1LANE    BIT(7)
#define RXULPSESC0LANE        BIT(6)
#define ULPSACTIVENOT0LANE    BIT(5)
#define PHYSTOPSTATE0LANE    BIT(4)
#define PHYULPSACTIVENOTCLK    BIT(3)
#define PHYSTOPSTATECLKLANE    BIT(2)
#define PHYSTOPSTATELANE    (PHYSTOPSTATE0LANE | PHYSTOPSTATECLKLANE)
#define PHYDIRECTION        BIT(1)
#define PHYLOCK            BIT(0)
#define DSI_LP_CMD_TIM        HOSTREG(0x0070)
#define OUTVACT_LPCMD_TIME(x)    UPDATE(x, 15, 8)
#define INVACT_LPCMD_TIME(x)    UPDATE(x, 7, 0)
#define DSI_MAX_REGISTER    DSI_LP_CMD_TIM
 
#define PHYREG(x)        ((x) + 0x0c00)
#define MIPI_PHY_REG0        PHYREG(0x0000)
#define LANE_EN_MASK        GENMASK(6, 2)
#define LANE_EN_CK        BIT(6)
#define MIPI_PHY_REG1        PHYREG(0x0004)
#define REG_DA_PPFC        BIT(4)
#define REG_DA_SYNCRST        BIT(2)
#define REG_DA_LDOPD        BIT(1)
#define REG_DA_PLLPD        BIT(0)
#define MIPI_PHY_REG3        PHYREG(0x000c)
#define REG_FBDIV_HI_MASK    GENMASK(5, 5)
#define REG_FBDIV_HI(x)        UPDATE(x, 5, 5)
#define REG_PREDIV_MASK        GENMASK(4, 0)
#define REG_PREDIV(x)        UPDATE(x, 4, 0)
#define MIPI_PHY_REG4        PHYREG(0x0010)
#define REG_FBDIV_LO_MASK    GENMASK(7, 0)
#define REG_FBDIV_LO(x)        UPDATE(x, 7, 0)
#define MIPI_PHY_REG5        PHYREG(0x0014)
#define MIPI_PHY_REG6        PHYREG(0x0018)
#define MIPI_PHY_REG7        PHYREG(0x001c)
#define MIPI_PHY_REG9        PHYREG(0x0024)
#define MIPI_PHY_REG20        PHYREG(0x0080)
#define REG_DIG_RSTN        BIT(0)
#define MIPI_PHY_MAX_REGISTER    PHYREG(0x0348)
 
#define THS_SETTLE_OFFSET    0x00
#define THS_SETTLE_MASK        GENMASK(3, 0)
#define THS_SETTLE(x)        UPDATE(x, 3, 0)
#define TLPX_OFFSET        0x14
#define TLPX_MASK        GENMASK(5, 0)
#define TLPX(x)            UPDATE(x, 5, 0)
#define THS_PREPARE_OFFSET    0x18
#define THS_PREPARE_MASK    GENMASK(6, 0)
#define THS_PREPARE(x)        UPDATE(x, 6, 0)
#define THS_ZERO_OFFSET        0x1c
#define THS_ZERO_MASK        GENMASK(5, 0)
#define THS_ZERO(x)        UPDATE(x, 5, 0)
#define THS_TRAIL_OFFSET    0x20
#define THS_TRAIL_MASK        GENMASK(6, 0)
#define THS_TRAIL(x)        UPDATE(x, 6, 0)
#define THS_EXIT_OFFSET        0x24
#define THS_EXIT_MASK        GENMASK(4, 0)
#define THS_EXIT(x)        UPDATE(x, 4, 0)
#define TCLK_POST_OFFSET    0x28
#define TCLK_POST_MASK        GENMASK(3, 0)
#define TCLK_POST(x)        UPDATE(x, 3, 0)
#define TWAKUP_HI_OFFSET    0x30
#define TWAKUP_HI_MASK        GENMASK(1, 0)
#define TWAKUP_HI(x)        UPDATE(x, 1, 0)
#define TWAKUP_LO_OFFSET    0x34
#define TWAKUP_LO_MASK        GENMASK(7, 0)
#define TWAKUP_LO(x)        UPDATE(x, 7, 0)
#define TCLK_PRE_OFFSET        0x38
#define TCLK_PRE_MASK        GENMASK(3, 0)
#define TCLK_PRE(x)        UPDATE(x, 3, 0)
#define TTA_GO_OFFSET        0x40
#define TTA_GO_MASK        GENMASK(5, 0)
#define TTA_GO(x)        UPDATE(x, 5, 0)
#define TTA_SURE_OFFSET        0x44
#define TTA_SURE_MASK        GENMASK(5, 0)
#define TTA_SURE(x)        UPDATE(x, 5, 0)
#define TTA_WAIT_OFFSET        0x48
#define TTA_WAIT_MASK        GENMASK(5, 0)
#define TTA_WAIT(x)        UPDATE(x, 5, 0)
 
#define PSEC_PER_NSEC    1000L
#define PSEC_PER_SEC    1000000000000LL
 
struct mipi_dphy {
   struct regmap *regmap;
   u8 prediv;
   u16 fbdiv;
   unsigned int rate;
};
 
struct rk618_dsi {
   struct drm_bridge base;
   struct drm_connector connector;
   struct drm_display_mode mode;
   struct drm_panel *panel;
   struct mipi_dsi_host host;
   struct mipi_dphy phy;
   unsigned int channel;
   unsigned int lanes;
   enum mipi_dsi_pixel_format format;
   unsigned long mode_flags;
 
   struct device *dev;
   struct rk618 *parent;
   struct regmap *regmap;
   struct clk *clock;
   struct rockchip_drm_sub_dev sub_dev;
};
 
enum {
   NON_BURST_MODE_SYNC_PULSE,
   NON_BURST_MODE_SYNC_EVENT,
   BURST_MODE,
};
 
enum {
   PIXEL_COLOR_CODING_16BIT_1,
   PIXEL_COLOR_CODING_16BIT_2,
   PIXEL_COLOR_CODING_16BIT_3,
   PIXEL_COLOR_CODING_18BIT_1,
   PIXEL_COLOR_CODING_18BIT_2,
   PIXEL_COLOR_CODING_24BIT,
};
 
static inline struct rk618_dsi *bridge_to_dsi(struct drm_bridge *b)
{
   return container_of(b, struct rk618_dsi, base);
}
 
static inline struct rk618_dsi *connector_to_dsi(struct drm_connector *c)
{
   return container_of(c, struct rk618_dsi, connector);
}
 
static inline struct rk618_dsi *host_to_dsi(struct mipi_dsi_host *h)
{
   return container_of(h, struct rk618_dsi, host);
}
 
static inline bool is_clk_lane(u32 offset)
{
   if (offset == 0x100)
       return true;
 
   return false;
}
 
static void rk618_dsi_set_hs_clk(struct rk618_dsi *dsi)
{
   const struct drm_display_mode *mode = &dsi->mode;
   struct mipi_dphy *phy = &dsi->phy;
   struct device *dev = dsi->dev;
   u32 fout, fref, prediv, fbdiv;
   u32 min_delta = UINT_MAX;
   unsigned int value;
 
   if (!of_property_read_u32(dev->of_node, "rockchip,lane-rate", &value)) {
       fout = value * USEC_PER_SEC;
   } else {
       int bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
       unsigned int lanes = dsi->lanes;
       u64 bandwidth;
 
       bandwidth = (u64)mode->clock * 1000 * bpp;
       do_div(bandwidth, lanes);
       bandwidth = div_u64(bandwidth * 10, 9);
       bandwidth = div_u64(bandwidth, USEC_PER_SEC);
       bandwidth = bandwidth * USEC_PER_SEC;
       fout = bandwidth;
   }
 
   if (fout > 1000000000UL)
       fout = 1000000000UL;
 
   fref = clk_get_rate(dsi->parent->clkin);
 
   for (prediv = 1; prediv <= 12; prediv++) {
       u64 tmp;
       u32 delta;
 
       if (fref % prediv)
           continue;
 
       tmp = (u64)fout * prediv;
       do_div(tmp, fref);
       fbdiv = tmp;
 
       if (fbdiv < 12 || fbdiv > 511)
           continue;
 
       if (fbdiv == 15)
           continue;
 
       tmp = (u64)fbdiv * fref;
       do_div(tmp, prediv);
 
       delta = abs(fout - tmp);
       if (!delta) {
           phy->rate = tmp;
           phy->prediv = prediv;
           phy->fbdiv = fbdiv;
           break;
       } else if (delta < min_delta) {
           phy->rate = tmp;
           phy->prediv = prediv;
           phy->fbdiv = fbdiv;
           min_delta = delta;
       }
   }
}
 
static void rk618_dsi_phy_power_off(struct rk618_dsi *dsi)
{
   struct mipi_dphy *phy = &dsi->phy;
 
   regmap_update_bits(phy->regmap, MIPI_PHY_REG0, LANE_EN_MASK, 0);
   regmap_update_bits(phy->regmap, MIPI_PHY_REG1,
              REG_DA_LDOPD | REG_DA_PLLPD,
              REG_DA_LDOPD | REG_DA_PLLPD);
}
 
static void rk618_dsi_phy_power_on(struct rk618_dsi *dsi, u32 txclkesc)
{
   struct mipi_dphy *phy = &dsi->phy;
   u32 offset, value, index;
   const struct {
       unsigned int rate;
       u8 ths_settle;
       u8 ths_zero;
       u8 ths_trail;
   } timing_table[] = {
       { 110000000, 0x00, 0x03, 0x0c},
       { 150000000, 0x01, 0x04, 0x0d},
       { 200000000, 0x02, 0x04, 0x11},
       { 250000000, 0x03, 0x05, 0x14},
       { 300000000, 0x04, 0x06, 0x18},
       { 400000000, 0x05, 0x07, 0x1d},
       { 500000000, 0x06, 0x08, 0x23},
       { 600000000, 0x07, 0x0a, 0x29},
       { 700000000, 0x08, 0x0b, 0x31},
       { 800000000, 0x09, 0x0c, 0x34},
       {1000000000, 0x0a, 0x0f, 0x40},
   };
   u32 Ttxbyteclkhs, UI, Ttxddrclkhs, Ttxclkesc;
   u32 Tlpx, Ths_exit, Tclk_post, Tclk_pre, Ths_prepare;
   u32 Tta_go, Tta_sure, Tta_wait;
 
   Ttxbyteclkhs = div_u64(PSEC_PER_SEC, phy->rate / 8);
   UI = Ttxddrclkhs = div_u64(PSEC_PER_SEC, phy->rate);
   Ttxclkesc = div_u64(PSEC_PER_SEC, txclkesc);
 
   regmap_update_bits(phy->regmap, MIPI_PHY_REG3, REG_FBDIV_HI_MASK |
              REG_PREDIV_MASK, REG_FBDIV_HI(phy->fbdiv >> 8) |
              REG_PREDIV(phy->prediv));
   regmap_update_bits(phy->regmap, MIPI_PHY_REG4,
              REG_FBDIV_LO_MASK, REG_FBDIV_LO(phy->fbdiv));
   regmap_update_bits(phy->regmap, MIPI_PHY_REG1,
              REG_DA_LDOPD | REG_DA_PLLPD, 0);
 
   regmap_update_bits(phy->regmap, MIPI_PHY_REG0, LANE_EN_MASK,
              LANE_EN_CK | GENMASK(dsi->lanes - 1 + 2, 2));
 
   regmap_update_bits(phy->regmap, MIPI_PHY_REG1,
              REG_DA_SYNCRST, REG_DA_SYNCRST);
   udelay(1);
   regmap_update_bits(phy->regmap, MIPI_PHY_REG1, REG_DA_SYNCRST, 0);
 
   regmap_update_bits(phy->regmap, MIPI_PHY_REG20, REG_DIG_RSTN, 0);
   udelay(1);
   regmap_update_bits(phy->regmap, MIPI_PHY_REG20,
              REG_DIG_RSTN, REG_DIG_RSTN);
 
   /* XXX */
   regmap_write(phy->regmap, MIPI_PHY_REG6, 0x11);
   regmap_write(phy->regmap, MIPI_PHY_REG7, 0x11);
   regmap_write(phy->regmap, MIPI_PHY_REG9, 0xcc);
 
   if (phy->rate < 800000000)
       regmap_update_bits(phy->regmap, MIPI_PHY_REG1,
                  REG_DA_PPFC, REG_DA_PPFC);
   else
       regmap_write(phy->regmap, MIPI_PHY_REG5, 0x30);
 
   for (index = 0; index < ARRAY_SIZE(timing_table); index++)
       if (phy->rate <= timing_table[index].rate)
           break;
 
   if (index == ARRAY_SIZE(timing_table))
       --index;
 
   for (offset = 0x100; offset <= 0x300; offset += 0x80) {
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + THS_SETTLE_OFFSET),
                  THS_SETTLE_MASK,
                  THS_SETTLE(timing_table[index].ths_settle));
 
       /*
        * The value of counter for HS Tlpx Time
        * Tlpx = Tpin_txbyteclkhs * value
        */
       Tlpx = 60 * PSEC_PER_NSEC;
       value = DIV_ROUND_UP(Tlpx, Ttxbyteclkhs);
       Tlpx = Ttxbyteclkhs * value;
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + TLPX_OFFSET),
                  TLPX_MASK, TLPX(value));
 
       /*
        * The value of counter for HS Ths-prepare
        * For clock lane, Ths-prepare(38ns~95ns)
        * For data lane, Ths-prepare(40ns+4UI~85ns+6UI)
        * Ths-prepare = Ttxddrclkhs * value
        */
       if (is_clk_lane(offset))
           Ths_prepare = 65 * PSEC_PER_NSEC;
       else
           Ths_prepare = 65 * PSEC_PER_NSEC + 4 * UI;
 
       value = DIV_ROUND_UP(Ths_prepare, Ttxddrclkhs);
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + THS_PREPARE_OFFSET),
                  THS_PREPARE_MASK, THS_PREPARE(value));
 
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + THS_ZERO_OFFSET),
                  THS_ZERO_MASK,
                  THS_ZERO(timing_table[index].ths_zero));
 
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + THS_TRAIL_OFFSET),
                  THS_TRAIL_MASK,
                  THS_TRAIL(timing_table[index].ths_trail));
 
       /*
        * The value of counter for HS Ths-exit
        * Ths-exit = Tpin_txbyteclkhs * value
        */
       Ths_exit = 120 * PSEC_PER_NSEC;
       value = DIV_ROUND_UP(Ths_exit, Ttxbyteclkhs);
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + THS_EXIT_OFFSET),
                  THS_EXIT_MASK, THS_EXIT(value));
 
       /*
        * The value of counter for HS Tclk-post
        * Tclk-post = Ttxbyteclkhs * value
        */
       Tclk_post = 70 * PSEC_PER_NSEC + 52 * UI;
       value = DIV_ROUND_UP(Tclk_post, Ttxbyteclkhs);
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + TCLK_POST_OFFSET),
                  TCLK_POST_MASK, TCLK_POST(value));
 
       /*
        * The value of counter for HS Twakup
        * Twakup for ulpm,
        * Twakup = Tpin_sys_clk * value
        */
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + TWAKUP_HI_OFFSET),
                  TWAKUP_HI_MASK, TWAKUP_HI(0x3));
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + TWAKUP_LO_OFFSET),
                  TWAKUP_LO_MASK, TWAKUP_LO(0xff));
 
       /*
        * The value of counter for HS Tclk-pre
        * Tclk-pre for clock lane
        * Tclk-pre = Tpin_txbyteclkhs * value
        */
       Tclk_pre = 8 * UI;
       value = DIV_ROUND_UP(Tclk_pre, Ttxbyteclkhs);
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + TCLK_PRE_OFFSET),
                  TCLK_PRE_MASK, TCLK_PRE(value));
 
       /*
        * The value of counter for HS Tta-go
        * Tta-go for turnaround
        * Tta-go = Ttxclkesc * value
        */
       Tta_go = 4 * Tlpx;
       value = DIV_ROUND_UP(Tta_go, Ttxclkesc);
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + TTA_GO_OFFSET),
                  TTA_GO_MASK, TTA_GO(value));
 
       /*
        * The value of counter for HS Tta-sure
        * Tta-sure for turnaround
        * Tta-sure = Ttxclkesc * value
        */
       Tta_sure = 2 * Tlpx;
       value = DIV_ROUND_UP(Tta_sure, Ttxclkesc);
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + TTA_SURE_OFFSET),
                  TTA_SURE_MASK, TTA_SURE(value));
 
       /*
        * The value of counter for HS Tta-wait
        * Tta-wait for turnaround
        * Interval from receiving ppi turnaround request to
        * sending esc request.
        * Tta-wait = Ttxclkesc * value
        */
       Tta_wait = 5 * Tlpx;
       value = DIV_ROUND_UP(Tta_wait, Ttxclkesc);
       regmap_update_bits(phy->regmap,
                  PHYREG(offset + TTA_WAIT_OFFSET),
                  TTA_WAIT_MASK, TTA_WAIT(value));
   }
}
 
static int rk618_dsi_pre_enable(struct rk618_dsi *dsi)
{
   struct drm_display_mode *mode = &dsi->mode;
   u32 esc_clk_div, txclkesc;
   u32 lanebyteclk, dpipclk;
   u32 hsw, hbp, vsw, vfp, vbp;
   u32 hsa_time, hbp_time, hline_time;
   u32 value;
   int ret;
 
   rk618_dsi_set_hs_clk(dsi);
 
   regmap_update_bits(dsi->regmap, DSI_PWR_UP, SHUTDOWNZ, RESET);
 
   /* Configuration of the internal clock dividers */
   esc_clk_div = DIV_ROUND_UP(dsi->phy.rate >> 3, 20000000);
   txclkesc = dsi->phy.rate >> 3 / esc_clk_div;
   value = TO_CLK_DIVIDSION(10) | TX_ESC_CLK_DIVIDSION(esc_clk_div);
   regmap_write(dsi->regmap, DSI_CLKMGR_CFG, value);
 
   /* The DPI interface configuration */
   value = DPI_VID(dsi->channel);
 
   if (mode->flags & DRM_MODE_FLAG_NVSYNC)
       value |= VSYNC_ACTIVE_LOW;
   if (mode->flags & DRM_MODE_FLAG_NHSYNC)
       value |= HSYNC_ACTIVE_LOW;
 
   switch (dsi->format) {
   case MIPI_DSI_FMT_RGB666:
       value |= DPI_COLOR_CODING(PIXEL_COLOR_CODING_18BIT_2);
       break;
   case MIPI_DSI_FMT_RGB666_PACKED:
       value |= DPI_COLOR_CODING(PIXEL_COLOR_CODING_18BIT_1);
       value |= EN18_LOOSELY;
       break;
   case MIPI_DSI_FMT_RGB565:
       value |= DPI_COLOR_CODING(PIXEL_COLOR_CODING_16BIT_1);
       break;
   case MIPI_DSI_FMT_RGB888:
   default:
       value |= DPI_COLOR_CODING(PIXEL_COLOR_CODING_24BIT);
       break;
   }
 
   regmap_write(dsi->regmap, DSI_DPI_CFG, value);
 
   /* Packet handler configuration */
   value = GEN_VID_RX(dsi->channel) | EN_CRC_RX | EN_ECC_RX | EN_BTA;
 
   if (!(dsi->mode_flags & MIPI_DSI_MODE_EOT_PACKET))
       value |= EN_EOTP_TX;
 
   regmap_write(dsi->regmap, DSI_PCKHDL_CFG, value);
 
   /* Video mode configuration */
   value = EN_LP_VACT | EN_LP_VBP | EN_LP_VFP | EN_LP_VSA;
 
   if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HFP))
       value |= EN_LP_HFP;
 
   if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HBP))
       value |= EN_LP_HBP;
 
   if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
       value |= VID_MODE_TYPE(BURST_MODE);
   else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
       value |= VID_MODE_TYPE(NON_BURST_MODE_SYNC_PULSE);
   else
       value |= VID_MODE_TYPE(NON_BURST_MODE_SYNC_EVENT);
 
   regmap_write(dsi->regmap, DSI_VID_MODE_CFG, value);
 
   /* Video packet configuration */
   regmap_write(dsi->regmap, DSI_VID_PKT_CFG,
            VID_PKT_SIZE(mode->hdisplay));
 
   /* Timeout timers configuration */
   regmap_write(dsi->regmap, DSI_TO_CNT_CFG,
            LPRX_TO_CNT(1000) | HSTX_TO_CNT(1000));
 
   hsw = mode->hsync_end - mode->hsync_start;
   hbp = mode->htotal - mode->hsync_end;
   vsw = mode->vsync_end - mode->vsync_start;
   vfp = mode->vsync_start - mode->vdisplay;
   vbp = mode->vtotal - mode->vsync_end;
 
   /* Line timing configuration */
   lanebyteclk = (dsi->phy.rate >> 3) / USEC_PER_SEC;
   dpipclk = mode->clock / USEC_PER_MSEC;
   hline_time = DIV_ROUND_UP(mode->htotal * lanebyteclk, dpipclk);
   hbp_time = DIV_ROUND_UP(hbp * lanebyteclk, dpipclk);
   hsa_time = DIV_ROUND_UP(hsw * lanebyteclk, dpipclk);
   regmap_write(dsi->regmap, DSI_TMR_LINE_CFG, HLINE_TIME(hline_time) |
            HBP_TIME(hbp_time) | HSA_TIME(hsa_time));
 
   /* Vertical timing configuration */
   regmap_write(dsi->regmap, DSI_VTIMING_CFG,
            V_ACTIVE_LINES(mode->vdisplay) | VFP_LINES(vfp) |
            VBP_LINES(vbp) | VSA_LINES(vsw));
 
   /* D-PHY interface configuration */
   value = N_LANES(dsi->lanes - 1) | PHY_STOP_WAIT_TIME(0x20);
   regmap_write(dsi->regmap, DSI_PHY_IF_CFG, value);
 
   /* D-PHY timing configuration */
   value = PHY_HS2LP_TIME(20) | PHY_LP2HS_TIME(16) | MAX_RD_TIME(10000);
   regmap_write(dsi->regmap, DSI_PHY_TMR_CFG, value);
 
   /* enables the D-PHY Clock Lane Module */
   regmap_update_bits(dsi->regmap, DSI_PHY_RSTZ,
              PHY_ENABLECLK, PHY_ENABLECLK);
 
   regmap_write(dsi->regmap, DSI_INT_MSK0, 0);
   regmap_write(dsi->regmap, DSI_INT_MSK1, 0);
 
   regmap_update_bits(dsi->regmap, DSI_VID_MODE_CFG, EN_VIDEO_MODE, 0);
   regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG,
              EN_CMD_MODE, EN_CMD_MODE);
 
   rk618_dsi_phy_power_on(dsi, txclkesc);
 
   /* wait for the PHY to acquire lock */
   ret = regmap_read_poll_timeout(dsi->regmap, DSI_PHY_STATUS,
                      value, value & PHYLOCK, 50, 1000);
   if (ret) {
       dev_err(dsi->dev, "PHY is not locked\n");
       return ret;
   }
 
   /* wait for the lane go to the stop state */
   ret = regmap_read_poll_timeout(dsi->regmap, DSI_PHY_STATUS,
                      value, value & PHYSTOPSTATELANE,
                      50, 1000);
   if (ret) {
       dev_err(dsi->dev, "lane module is not in stop state\n");
       return ret;
   }
 
   regmap_update_bits(dsi->regmap, DSI_PWR_UP, SHUTDOWNZ, POWER_UP);
 
   return 0;
}
 
static void rk618_dsi_enable(struct rk618_dsi *dsi)
{
   /* controls the D-PHY PPI txrequestclkhs signal */
   regmap_update_bits(dsi->regmap, DSI_PHY_IF_CTRL,
              PHY_TXREQUESCLKHS, PHY_TXREQUESCLKHS);
 
   /* enables the DPI Video mode transmission */
   regmap_update_bits(dsi->regmap, DSI_PWR_UP, SHUTDOWNZ, RESET);
   regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG, EN_CMD_MODE, 0);
   regmap_update_bits(dsi->regmap, DSI_VID_MODE_CFG,
              EN_VIDEO_MODE, EN_VIDEO_MODE);
   regmap_update_bits(dsi->regmap, DSI_PWR_UP, SHUTDOWNZ, POWER_UP);
 
   dev_info(dsi->dev, "final DSI-Link bandwidth: %lu x %d Mbps\n",
        dsi->phy.rate / USEC_PER_SEC, dsi->lanes);
}
 
static void rk618_dsi_disable(struct rk618_dsi *dsi)
{
   /* enables the Command mode protocol for transmissions */
   regmap_update_bits(dsi->regmap, DSI_PWR_UP, SHUTDOWNZ, RESET);
   regmap_update_bits(dsi->regmap, DSI_PHY_IF_CTRL, PHY_TXREQUESCLKHS, 0);
   regmap_update_bits(dsi->regmap, DSI_VID_MODE_CFG, EN_VIDEO_MODE, 0);
   regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG,
              EN_CMD_MODE, EN_CMD_MODE);
   regmap_update_bits(dsi->regmap, DSI_PWR_UP, SHUTDOWNZ, POWER_UP);
}
 
static void rk618_dsi_post_disable(struct rk618_dsi *dsi)
{
   regmap_update_bits(dsi->regmap, DSI_PWR_UP, SHUTDOWNZ, RESET);
   regmap_update_bits(dsi->regmap, DSI_PHY_RSTZ, PHY_ENABLECLK, 0);
 
   rk618_dsi_phy_power_off(dsi);
}
 
static struct drm_encoder *
rk618_dsi_connector_best_encoder(struct drm_connector *connector)
{
   struct rk618_dsi *dsi = connector_to_dsi(connector);
 
   return dsi->base.encoder;
}
 
static int rk618_dsi_connector_get_modes(struct drm_connector *connector)
{
   struct rk618_dsi *dsi = connector_to_dsi(connector);
 
   return drm_panel_get_modes(dsi->panel);
}
 
static const struct drm_connector_helper_funcs
rk618_dsi_connector_helper_funcs = {
   .get_modes = rk618_dsi_connector_get_modes,
   .best_encoder = rk618_dsi_connector_best_encoder,
};
 
static enum drm_connector_status
rk618_dsi_connector_detect(struct drm_connector *connector, bool force)
{
   return connector_status_connected;
}
 
static void rk618_dsi_connector_destroy(struct drm_connector *connector)
{
   struct rk618_dsi *dsi = connector_to_dsi(connector);
 
   drm_panel_detach(dsi->panel);
   drm_connector_cleanup(connector);
}
 
static const struct drm_connector_funcs rk618_dsi_connector_funcs = {
   .detect = rk618_dsi_connector_detect,
   .fill_modes = drm_helper_probe_single_connector_modes,
   .destroy = rk618_dsi_connector_destroy,
   .reset = drm_atomic_helper_connector_reset,
   .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
   .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
};
 
static void rk618_dsi_bridge_enable(struct drm_bridge *bridge)
{
   struct rk618_dsi *dsi = bridge_to_dsi(bridge);
 
   clk_prepare_enable(dsi->clock);
 
   rk618_dsi_pre_enable(dsi);
   drm_panel_prepare(dsi->panel);
   rk618_dsi_enable(dsi);
   drm_panel_enable(dsi->panel);
}
 
static void rk618_dsi_bridge_disable(struct drm_bridge *bridge)
{
   struct rk618_dsi *dsi = bridge_to_dsi(bridge);
 
   drm_panel_disable(dsi->panel);
   rk618_dsi_disable(dsi);
   drm_panel_unprepare(dsi->panel);
   rk618_dsi_post_disable(dsi);
 
   clk_disable_unprepare(dsi->clock);
}
 
static void rk618_dsi_bridge_mode_set(struct drm_bridge *bridge,
                     struct drm_display_mode *mode,
                     struct drm_display_mode *adj)
{
   struct rk618_dsi *dsi = bridge_to_dsi(bridge);
 
   if (bridge->driver_private)
       drm_mode_copy(&dsi->mode, bridge->driver_private);
   else
       drm_mode_copy(&dsi->mode, adj);
}
 
static int rk618_dsi_bridge_attach(struct drm_bridge *bridge)
{
   struct rk618_dsi *dsi = bridge_to_dsi(bridge);
   struct drm_connector *connector = &dsi->connector;
   struct drm_device *drm = bridge->dev;
   int ret;
 
   ret = drm_connector_init(drm, connector, &rk618_dsi_connector_funcs,
                DRM_MODE_CONNECTOR_DSI);
   if (ret) {
       dev_err(dsi->dev, "Failed to initialize connector with drm\n");
       return ret;
   }
 
   drm_connector_helper_add(connector, &rk618_dsi_connector_helper_funcs);
   drm_connector_attach_encoder(connector, bridge->encoder);
 
   ret = drm_panel_attach(dsi->panel, connector);
   if (ret) {
       dev_err(dsi->dev, "Failed to attach panel\n");
       return ret;
   }
   dsi->sub_dev.connector = &dsi->connector;
   dsi->sub_dev.of_node = dsi->dev->of_node;
   rockchip_drm_register_sub_dev(&dsi->sub_dev);
 
   return 0;
}
 
static void rk618_dsi_bridge_detach(struct drm_bridge *bridge)
{
   struct rk618_dsi *dsi = bridge_to_dsi(bridge);
 
   rockchip_drm_unregister_sub_dev(&dsi->sub_dev);
}
 
static const struct drm_bridge_funcs rk618_dsi_bridge_funcs = {
   .attach = rk618_dsi_bridge_attach,
   .detach = rk618_dsi_bridge_detach,
   .mode_set = rk618_dsi_bridge_mode_set,
   .enable = rk618_dsi_bridge_enable,
   .disable = rk618_dsi_bridge_disable,
};
 
static ssize_t rk618_dsi_host_transfer(struct mipi_dsi_host *host,
                      const struct mipi_dsi_msg *msg)
{
   struct rk618_dsi *dsi = host_to_dsi(host);
   struct mipi_dsi_packet packet;
   u32 value, mask;
   int ret;
 
   if (msg->flags & MIPI_DSI_MSG_USE_LPM)
       regmap_update_bits(dsi->regmap, DSI_PHY_IF_CTRL,
                  PHY_TXREQUESCLKHS, 0);
   else
       regmap_update_bits(dsi->regmap, DSI_PHY_IF_CTRL,
                  PHY_TXREQUESCLKHS, PHY_TXREQUESCLKHS);
 
   switch (msg->type) {
   case MIPI_DSI_DCS_SHORT_WRITE:
       regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG, DCS_SW_0P_TX,
                  msg->flags & MIPI_DSI_MSG_USE_LPM ?
                  DCS_SW_0P_TX : 0);
       break;
   case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
       regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG, DCS_SW_1P_TX,
                  msg->flags & MIPI_DSI_MSG_USE_LPM ?
                  DCS_SW_1P_TX : 0);
       break;
   case MIPI_DSI_DCS_LONG_WRITE:
       regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG, DCS_LW_TX,
                  msg->flags & MIPI_DSI_MSG_USE_LPM ?
                  DCS_LW_TX : 0);
       break;
   case MIPI_DSI_DCS_READ:
       regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG, DCS_SR_0P_TX,
                  msg->flags & MIPI_DSI_MSG_USE_LPM ?
                  DCS_SR_0P_TX : 0);
       break;
   case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
       regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG,
                  MAX_RD_PKT_SIZE,
                  msg->flags & MIPI_DSI_MSG_USE_LPM ?
                  MAX_RD_PKT_SIZE : 0);
       break;
   case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
       regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG, GEN_SW_0P_TX,
                  msg->flags & MIPI_DSI_MSG_USE_LPM ?
                  GEN_SW_0P_TX : 0);
       break;
   case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
       regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG, GEN_SW_1P_TX,
                  msg->flags & MIPI_DSI_MSG_USE_LPM ?
                  GEN_SW_1P_TX : 0);
       break;
   case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
       regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG, GEN_SW_2P_TX,
                  msg->flags & MIPI_DSI_MSG_USE_LPM ?
                  GEN_SW_2P_TX : 0);
       break;
   case MIPI_DSI_GENERIC_LONG_WRITE:
       regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG, GEN_LW_TX,
                  msg->flags & MIPI_DSI_MSG_USE_LPM ?
                  GEN_LW_TX : 0);
       break;
   case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
       regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG, GEN_SR_0P_TX,
                  msg->flags & MIPI_DSI_MSG_USE_LPM ?
                  GEN_SR_0P_TX : 0);
       break;
   case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
       regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG, GEN_SR_1P_TX,
                  msg->flags & MIPI_DSI_MSG_USE_LPM ?
                  GEN_SR_1P_TX : 0);
       break;
   case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
       regmap_update_bits(dsi->regmap, DSI_CMD_MODE_CFG, GEN_SR_2P_TX,
                  msg->flags & MIPI_DSI_MSG_USE_LPM ?
                  GEN_SR_2P_TX : 0);
       break;
   default:
       return -EINVAL;
   }
 
   /* create a packet to the DSI protocol */
   ret = mipi_dsi_create_packet(&packet, msg);
   if (ret) {
       dev_err(dsi->dev, "failed to create packet: %d\n", ret);
       return ret;
   }
 
   /* Send payload */
   while (packet.payload_length >= 4) {
       mask = GEN_PLD_W_FULL;
       ret = regmap_read_poll_timeout(dsi->regmap, DSI_GEN_PKT_STATUS,
                          value, !(value & mask),
                          50, 1000);
       if (ret) {
           dev_err(dsi->dev, "Write payload FIFO is full\n");
           return ret;
       }
 
       value = get_unaligned_le32(packet.payload);
       regmap_write(dsi->regmap, DSI_GEN_PLD_DATA, value);
       packet.payload += 4;
       packet.payload_length -= 4;
   }
 
   value = 0;
   switch (packet.payload_length) {
   case 3:
       value |= packet.payload[2] << 16;
       /* Fall through */
   case 2:
       value |= packet.payload[1] << 8;
       /* Fall through */
   case 1:
       value |= packet.payload[0];
       regmap_write(dsi->regmap, DSI_GEN_PLD_DATA, value);
       break;
   }
 
   mask = GEN_CMD_FULL;
   ret = regmap_read_poll_timeout(dsi->regmap, DSI_GEN_PKT_STATUS,
                      value, !(value & mask), 50, 1000);
   if (ret) {
       dev_err(dsi->dev, "Command FIFO is full\n");
       return ret;
   }
 
   /* Send packet header */
   value = get_unaligned_le32(packet.header);
   regmap_write(dsi->regmap, DSI_GEN_HDR, value);
 
   mask = GEN_PLD_W_EMPTY | GEN_CMD_EMPTY;
   ret = regmap_read_poll_timeout(dsi->regmap, DSI_GEN_PKT_STATUS,
                      value, (value & mask) == mask, 50, 1000);
   if (ret) {
       dev_err(dsi->dev, "Write payload FIFO is not empty\n");
       return ret;
   }
 
   if (msg->rx_len) {
       u8 *payload = msg->rx_buf;
       u16 length;
 
       mask = GEN_RD_CMD_BUSY;
       ret = regmap_read_poll_timeout(dsi->regmap, DSI_GEN_PKT_STATUS,
                          value, !(value & mask),
                          50, 1000);
       if (ret) {
           dev_err(dsi->dev,
               "entire response is not stored in the FIFO\n");
           return ret;
       }
 
       /* Receive payload */
       for (length = msg->rx_len; length; length -= 4) {
           mask = GEN_PLD_R_EMPTY;
           ret = regmap_read_poll_timeout(dsi->regmap,
                              DSI_GEN_PKT_STATUS,
                              value, !(value & mask),
                              50, 1000);
           if (ret) {
               dev_err(dsi->dev,
                   "Read payload FIFO is empty\n");
               return ret;
           }
 
           regmap_read(dsi->regmap, DSI_GEN_PLD_DATA, &value);
 
           switch (length) {
           case 3:
               payload[2] = (value >> 16) & 0xff;
               /* Fall through */
           case 2:
               payload[1] = (value >> 8) & 0xff;
               /* Fall through */
           case 1:
               payload[0] = value & 0xff;
               return length;
           }
 
           payload[0] = (value >>  0) & 0xff;
           payload[1] = (value >>  8) & 0xff;
           payload[2] = (value >> 16) & 0xff;
           payload[3] = (value >> 24) & 0xff;
           payload += 4;
       }
   }
 
   return packet.payload_length;
}
 
static int rk618_dsi_host_attach(struct mipi_dsi_host *host,
                struct mipi_dsi_device *device)
{
   struct rk618_dsi *dsi = host_to_dsi(host);
 
   if (device->lanes < 1 || device->lanes > 4)
       return -EINVAL;
 
   dsi->lanes = device->lanes;
   dsi->channel = device->channel;
   dsi->format = device->format;
   dsi->mode_flags = device->mode_flags;
 
   dsi->panel = of_drm_find_panel(device->dev.of_node);
   if (!dsi->panel)
       return -EPROBE_DEFER;
 
   return 0;
}
 
static int rk618_dsi_host_detach(struct mipi_dsi_host *host,
                struct mipi_dsi_device *device)
{
   return 0;
}
 
static const struct mipi_dsi_host_ops rk618_dsi_host_ops = {
   .attach = rk618_dsi_host_attach,
   .detach = rk618_dsi_host_detach,
   .transfer = rk618_dsi_host_transfer,
};
 
static bool rk618_dsi_readable_reg(struct device *dev, unsigned int reg)
{
   switch (reg) {
   case DSI_VERSION ... DSI_MAX_REGISTER:
       return true;
   default:
       return false;
   }
}
 
static const struct regmap_config rk618_dsi_host_regmap_config = {
   .name = "dsi",
   .reg_bits = 16,
   .val_bits = 32,
   .reg_stride = 4,
   .max_register = DSI_MAX_REGISTER,
   .reg_format_endian = REGMAP_ENDIAN_NATIVE,
   .val_format_endian = REGMAP_ENDIAN_NATIVE,
   .readable_reg = rk618_dsi_readable_reg,
};
 
static bool rk618_dsi_phy_readable_reg(struct device *dev, unsigned int reg)
{
   switch (reg) {
   case MIPI_PHY_REG0 ... MIPI_PHY_MAX_REGISTER:
       return true;
   default:
       return false;
   }
}
 
static const struct regmap_config rk618_dsi_phy_regmap_config = {
   .name = "dphy",
   .reg_bits = 16,
   .val_bits = 32,
   .reg_stride = 4,
   .max_register = MIPI_PHY_MAX_REGISTER,
   .reg_format_endian = REGMAP_ENDIAN_NATIVE,
   .val_format_endian = REGMAP_ENDIAN_NATIVE,
   .readable_reg = rk618_dsi_phy_readable_reg,
};
 
static int rk618_dsi_probe(struct platform_device *pdev)
{
   struct rk618 *rk618 = dev_get_drvdata(pdev->dev.parent);
   struct device *dev = &pdev->dev;
   struct rk618_dsi *dsi;
   int ret;
 
   if (!of_device_is_available(dev->of_node))
       return -ENODEV;
 
   dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
   if (!dsi)
       return -ENOMEM;
 
   dsi->dev = dev;
   dsi->parent = rk618;
   platform_set_drvdata(pdev, dsi);
 
   dsi->clock = devm_clk_get(dev, "dsi");
   if (IS_ERR(dsi->clock)) {
       ret = PTR_ERR(dsi->clock);
       dev_err(dev, "failed to get dsi clock: %d\n", ret);
       return ret;
   }
 
   dsi->regmap = devm_regmap_init_i2c(rk618->client,
                      &rk618_dsi_host_regmap_config);
   if (IS_ERR(dsi->regmap)) {
       ret = PTR_ERR(dsi->regmap);
       dev_err(dev, "failed to allocate host register map: %d\n", ret);
       return ret;
   }
 
   dsi->phy.regmap = devm_regmap_init_i2c(rk618->client,
                          &rk618_dsi_phy_regmap_config);
   if (IS_ERR(dsi->phy.regmap)) {
       ret = PTR_ERR(dsi->phy.regmap);
       dev_err(dev, "failed to allocate phy register map: %d\n", ret);
       return ret;
   }
 
   dsi->base.funcs = &rk618_dsi_bridge_funcs;
   dsi->base.of_node = dev->of_node;
   drm_bridge_add(&dsi->base);
 
   dsi->host.dev = dev;
   dsi->host.ops = &rk618_dsi_host_ops;
   ret = mipi_dsi_host_register(&dsi->host);
   if (ret) {
       drm_bridge_remove(&dsi->base);
       dev_err(dev, "failed to register host: %d\n", ret);
       return ret;
   }
 
   return 0;
}
 
static int rk618_dsi_remove(struct platform_device *pdev)
{
   struct rk618_dsi *dsi = platform_get_drvdata(pdev);
 
   mipi_dsi_host_unregister(&dsi->host);
   drm_bridge_remove(&dsi->base);
 
   return 0;
}
 
static const struct of_device_id rk618_dsi_of_match[] = {
   { .compatible = "rockchip,rk618-dsi", },
   {},
};
MODULE_DEVICE_TABLE(of, rk618_dsi_of_match);
 
static struct platform_driver rk618_dsi_driver = {
   .driver = {
       .name = "rk618-dsi",
       .of_match_table = of_match_ptr(rk618_dsi_of_match),
   },
   .probe = rk618_dsi_probe,
   .remove = rk618_dsi_remove,
};
module_platform_driver(rk618_dsi_driver);
 
MODULE_AUTHOR("Wyon Bi <bivvy.bi@rock-chips.com>");
MODULE_DESCRIPTION("Rockchip RK618 MIPI-DSI driver");
MODULE_LICENSE("GPL v2");