/
sec-dsim.c
2089 lines (1745 loc) · 54.5 KB
/
sec-dsim.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
/*
* Samsung MIPI DSIM Bridge
*
* Copyright 2018-2022 NXP
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <asm/unaligned.h>
#include <linux/clk.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/gcd.h>
#include <linux/log2.h>
#include <linux/media-bus-format.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_graph.h>
#include <linux/pm_runtime.h>
#include <drm/bridge/sec_mipi_dsim.h>
#include <drm/drm_vblank.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_bridge.h>
#include <drm/drm_connector.h>
#include <drm/drm_probe_helper.h>
#include <drm/drm_encoder.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_mipi_dsi.h>
#include <drm/drm_panel.h>
#include <video/videomode.h>
#include <video/mipi_display.h>
/* dsim registers */
#define DSIM_VERSION 0x00
#define DSIM_STATUS 0x04
#define DSIM_RGB_STATUS 0x08
#define DSIM_SWRST 0x0c
#define DSIM_CLKCTRL 0x10
#define DSIM_TIMEOUT 0x14
#define DSIM_CONFIG 0x18
#define DSIM_ESCMODE 0x1c
#define DSIM_MDRESOL 0x20
#define DSIM_MVPORCH 0x24
#define DSIM_MHPORCH 0x28
#define DSIM_MSYNC 0x2c
#define DSIM_SDRESOL 0x30
#define DSIM_INTSRC 0x34
#define DSIM_INTMSK 0x38
/* packet */
#define DSIM_PKTHDR 0x3c
#define DSIM_PAYLOAD 0x40
#define DSIM_RXFIFO 0x44
#define DSIM_FIFOTHLD 0x48
#define DSIM_FIFOCTRL 0x4c
#define DSIM_MEMACCHR 0x50
#define DSIM_MULTI_PKT 0x78
/* pll control */
#define DSIM_PLLCTRL_1G 0x90
#define DSIM_PLLCTRL 0x94
#define DSIM_PLLCTRL1 0x98
#define DSIM_PLLCTRL2 0x9c
#define DSIM_PLLTMR 0xa0
/* dphy */
#define DSIM_PHYTIMING 0xb4
#define DSIM_PHYTIMING1 0xb8
#define DSIM_PHYTIMING2 0xbc
/* reg bit manipulation */
#define REG_MASK(e, s) (((1 << ((e) - (s) + 1)) - 1) << (s))
#define REG_PUT(x, e, s) (((x) << (s)) & REG_MASK(e, s))
#define REG_GET(x, e, s) (((x) & REG_MASK(e, s)) >> (s))
/* register bit fields */
#define STATUS_PLLSTABLE BIT(31)
#define STATUS_SWRSTRLS BIT(20)
#define STATUS_TXREADYHSCLK BIT(10)
#define STATUS_ULPSCLK BIT(9)
#define STATUS_STOPSTATECLK BIT(8)
#define STATUS_GET_ULPSDAT(x) REG_GET(x, 7, 4)
#define STATUS_GET_STOPSTATEDAT(x) REG_GET(x, 3, 0)
#define RGB_STATUS_CMDMODE_INSEL BIT(31)
#define RGB_STATUS_GET_RGBSTATE(x) REG_GET(x, 12, 0)
#define CLKCTRL_TXREQUESTHSCLK BIT(31)
#define CLKCTRL_DPHY_SEL_1G BIT(29)
#define CLKCTRL_DPHY_SEL_1P5G (0x0 << 29)
#define CLKCTRL_ESCCLKEN BIT(28)
#define CLKCTRL_PLLBYPASS BIT(29)
#define CLKCTRL_BYTECLKSRC_DPHY_PLL REG_PUT(0, 26, 25)
#define CLKCTRL_BYTECLKEN BIT(24)
#define CLKCTRL_SET_LANEESCCLKEN(x) REG_PUT(x, 23, 19)
#define CLKCTRL_SET_ESCPRESCALER(x) REG_PUT(x, 15, 0)
#define TIMEOUT_SET_BTAOUT(x) REG_PUT(x, 23, 16)
#define TIMEOUT_SET_LPDRTOUT(x) REG_PUT(x, 15, 0)
#define CONFIG_NON_CONTINUOUS_CLOCK_LANE BIT(31)
#define CONFIG_CLKLANE_STOP_START BIT(30)
#define CONFIG_MFLUSH_VS BIT(29)
#define CONFIG_EOT_R03 BIT(28)
#define CONFIG_SYNCINFORM BIT(27)
#define CONFIG_BURSTMODE BIT(26)
#define CONFIG_VIDEOMODE BIT(25)
#define CONFIG_AUTOMODE BIT(24)
#define CONFIG_HSEDISABLEMODE BIT(23)
#define CONFIG_HFPDISABLEMODE BIT(22)
#define CONFIG_HBPDISABLEMODE BIT(21)
#define CONFIG_HSADISABLEMODE BIT(20)
#define CONFIG_SET_MAINVC(x) REG_PUT(x, 19, 18)
#define CONFIG_SET_SUBVC(x) REG_PUT(x, 17, 16)
#define CONFIG_SET_MAINPIXFORMAT(x) REG_PUT(x, 14, 12)
#define CONFIG_SET_SUBPIXFORMAT(x) REG_PUT(x, 10, 8)
#define CONFIG_SET_NUMOFDATLANE(x) REG_PUT(x, 6, 5)
#define CONFIG_SET_LANEEN(x) REG_PUT(x, 4, 0)
#define ESCMODE_SET_STOPSTATE_CNT(X) REG_PUT(x, 31, 21)
#define ESCMODE_FORCESTOPSTATE BIT(20)
#define ESCMODE_FORCEBTA BIT(16)
#define ESCMODE_CMDLPDT BIT(7)
#define ESCMODE_TXLPDT BIT(6)
#define ESCMODE_TXTRIGGERRST BIT(5)
#define MDRESOL_MAINSTANDBY BIT(31)
#define MDRESOL_SET_MAINVRESOL(x) REG_PUT(x, 27, 16)
#define MDRESOL_SET_MAINHRESOL(x) REG_PUT(x, 11, 0)
#define MVPORCH_SET_CMDALLOW(x) REG_PUT(x, 31, 28)
#define MVPORCH_SET_STABLEVFP(x) REG_PUT(x, 26, 16)
#define MVPORCH_SET_MAINVBP(x) REG_PUT(x, 10, 0)
#define MHPORCH_SET_MAINHFP(x) REG_PUT(x, 31, 16)
#define MHPORCH_SET_MAINHBP(x) REG_PUT(x, 15, 0)
#define MSYNC_SET_MAINVSA(x) REG_PUT(x, 31, 22)
#define MSYNC_SET_MAINHSA(x) REG_PUT(x, 15, 0)
#define INTSRC_PLLSTABLE BIT(31)
#define INTSRC_SWRSTRELEASE BIT(30)
#define INTSRC_SFRPLFIFOEMPTY BIT(29)
#define INTSRC_SFRPHFIFOEMPTY BIT(28)
#define INTSRC_FRAMEDONE BIT(24)
#define INTSRC_LPDRTOUT BIT(21)
#define INTSRC_TATOUT BIT(20)
#define INTSRC_RXDATDONE BIT(18)
#define INTSRC_RXTE BIT(17)
#define INTSRC_RXACK BIT(16)
#define INTSRC_MASK (INTSRC_PLLSTABLE | \
INTSRC_SWRSTRELEASE | \
INTSRC_SFRPLFIFOEMPTY | \
INTSRC_SFRPHFIFOEMPTY | \
INTSRC_FRAMEDONE | \
INTSRC_LPDRTOUT | \
INTSRC_TATOUT | \
INTSRC_RXDATDONE | \
INTSRC_RXTE | \
INTSRC_RXACK)
#define INTMSK_MSKPLLSTABLE BIT(31)
#define INTMSK_MSKSWRELEASE BIT(30)
#define INTMSK_MSKSFRPLFIFOEMPTY BIT(29)
#define INTMSK_MSKSFRPHFIFOEMPTY BIT(28)
#define INTMSK_MSKFRAMEDONE BIT(24)
#define INTMSK_MSKLPDRTOUT BIT(21)
#define INTMSK_MSKTATOUT BIT(20)
#define INTMSK_MSKRXDATDONE BIT(18)
#define INTMSK_MSKRXTE BIT(17)
#define INTMSK_MSKRXACK BIT(16)
#define PKTHDR_SET_DATA1(x) REG_PUT(x, 23, 16)
#define PKTHDR_GET_DATA1(x) REG_GET(x, 23, 16)
#define PKTHDR_SET_DATA0(x) REG_PUT(x, 15, 8)
#define PKTHDR_GET_DATA0(x) REG_GET(x, 15, 8)
#define PKTHDR_GET_WC(x) REG_GET(x, 23, 8)
#define PKTHDR_SET_DI(x) REG_PUT(x, 7, 0)
#define PKTHDR_GET_DI(x) REG_GET(x, 7, 0)
#define PKTHDR_SET_DT(x) REG_PUT(x, 5, 0)
#define PKTHDR_GET_DT(x) REG_GET(x, 5, 0)
#define PKTHDR_SET_VC(x) REG_PUT(x, 7, 6)
#define PKTHDR_GET_VC(x) REG_GET(x, 7, 6)
#define FIFOCTRL_FULLRX BIT(25)
#define FIFOCTRL_EMPTYRX BIT(24)
#define FIFOCTRL_FULLHSFR BIT(23)
#define FIFOCTRL_EMPTYHSFR BIT(22)
#define FIFOCTRL_FULLLSFR BIT(21)
#define FIFOCTRL_EMPTYLSFR BIT(20)
#define FIFOCTRL_FULLHMAIN BIT(11)
#define FIFOCTRL_EMPTYHMAIN BIT(10)
#define FIFOCTRL_FULLLMAIN BIT(9)
#define FIFOCTRL_EMPTYLMAIN BIT(8)
#define FIFOCTRL_NINITRX BIT(4)
#define FIFOCTRL_NINITSFR BIT(3)
#define FIFOCTRL_NINITI80 BIT(2)
#define FIFOCTRL_NINITSUB BIT(1)
#define FIFOCTRL_NINITMAIN BIT(0)
#define PLLCTRL_DPDNSWAP_CLK BIT(25)
#define PLLCTRL_DPDNSWAP_DAT BIT(24)
#define PLLCTRL_PLLEN BIT(23)
#define PLLCTRL_SET_PMS(x) REG_PUT(x, 19, 1)
#define PLLCTRL_SET_P(x) REG_PUT(x, 18, 13)
#define PLLCTRL_SET_M(x) REG_PUT(x, 12, 3)
#define PLLCTRL_SET_S(x) REG_PUT(x, 2, 0)
#define PHYTIMING_SET_M_TLPXCTL(x) REG_PUT(x, 15, 8)
#define PHYTIMING_SET_M_THSEXITCTL(x) REG_PUT(x, 7, 0)
#define PHYTIMING1_SET_M_TCLKPRPRCTL(x) REG_PUT(x, 31, 24)
#define PHYTIMING1_SET_M_TCLKZEROCTL(x) REG_PUT(x, 23, 16)
#define PHYTIMING1_SET_M_TCLKPOSTCTL(x) REG_PUT(x, 15, 8)
#define PHYTIMING1_SET_M_TCLKTRAILCTL(x) REG_PUT(x, 7, 0)
#define PHYTIMING2_SET_M_THSPRPRCTL(x) REG_PUT(x, 23, 16)
#define PHYTIMING2_SET_M_THSZEROCTL(x) REG_PUT(x, 15, 8)
#define PHYTIMING2_SET_M_THSTRAILCTL(x) REG_PUT(x, 7, 0)
#define dsim_read(dsim, reg) readl(dsim->base + reg)
#define dsim_write(dsim, val, reg) writel(val, dsim->base + reg)
#define MAX_MAIN_HRESOL 2047
#define MAX_MAIN_VRESOL 2047
#define MAX_SUB_HRESOL 1024
#define MAX_SUB_VRESOL 1024
/* in KHZ */
#define MAX_ESC_CLK_FREQ 20000
/* dsim all irqs index */
#define PLLSTABLE 1
#define SWRSTRELEASE 2
#define SFRPLFIFOEMPTY 3
#define SFRPHFIFOEMPTY 4
#define SYNCOVERRIDE 5
#define BUSTURNOVER 6
#define FRAMEDONE 7
#define LPDRTOUT 8
#define TATOUT 9
#define RXDATDONE 10
#define RXTE 11
#define RXACK 12
#define ERRRXECC 13
#define ERRRXCRC 14
#define ERRESC3 15
#define ERRESC2 16
#define ERRESC1 17
#define ERRESC0 18
#define ERRSYNC3 19
#define ERRSYNC2 20
#define ERRSYNC1 21
#define ERRSYNC0 22
#define ERRCONTROL3 23
#define ERRCONTROL2 24
#define ERRCONTROL1 25
#define ERRCONTROL0 26
#define MIPI_FIFO_TIMEOUT msecs_to_jiffies(250)
#define MIPI_HFP_PKT_OVERHEAD 6
#define MIPI_HBP_PKT_OVERHEAD 6
#define MIPI_HSA_PKT_OVERHEAD 6
#define to_sec_mipi_dsim(dsi) container_of(dsi, struct sec_mipi_dsim, dsi_host)
#define conn_to_sec_mipi_dsim(conn) \
container_of(conn, struct sec_mipi_dsim, connector)
/* used for CEA standard modes */
struct dsim_hblank_par {
char *name; /* drm display mode name */
int vrefresh;
int hfp_wc;
int hbp_wc;
int hsa_wc;
int lanes;
};
struct dsim_pll_pms {
uint32_t bit_clk; /* kHz */
uint32_t p;
uint32_t m;
uint32_t s;
uint32_t k;
};
struct sec_mipi_dsim {
struct mipi_dsi_host dsi_host;
struct drm_connector connector;
struct drm_encoder *encoder;
struct drm_bridge *bridge;
struct drm_bridge *next;
struct drm_panel *panel;
struct device *dev;
void __iomem *base;
/* kHz clocks */
uint32_t pix_clk;
uint32_t bit_clk;
uint32_t pref_clk; /* phy ref clock rate in KHz */
unsigned int lanes;
unsigned int channel; /* virtual channel */
enum mipi_dsi_pixel_format format;
unsigned long mode_flags;
const struct dsim_hblank_par *hpar;
unsigned int pms;
unsigned int p;
unsigned int m;
unsigned int s;
unsigned long long lp_data_rate;
unsigned long long hs_data_rate;
struct videomode vmode;
bool enabled;
struct completion pll_stable;
struct completion ph_tx_done;
struct completion pl_tx_done;
struct completion rx_done;
const struct sec_mipi_dsim_plat_data *pdata;
};
#define DSIM_HBLANK_PARAM(nm, vf, hfp, hbp, hsa, num) \
.name = (nm), \
.vrefresh = (vf), \
.hfp_wc = (hfp), \
.hbp_wc = (hbp), \
.hsa_wc = (hsa), \
.lanes = (num)
#define DSIM_PLL_PMS(c, pp, mm, ss) \
.bit_clk = (c), \
.p = (pp), \
.m = (mm), \
.s = (ss)
static const struct dsim_hblank_par hblank_4lanes[] = {
/* { 88, 148, 44 } */
{ DSIM_HBLANK_PARAM("1920x1080", 60, 60, 105, 27, 4), },
/* { 528, 148, 44 } */
{ DSIM_HBLANK_PARAM("1920x1080", 50, 390, 105, 27, 4), },
/* { 88, 148, 44 } */
{ DSIM_HBLANK_PARAM("1920x1080", 30, 60, 105, 27, 4), },
/* { 110, 220, 40 } */
{ DSIM_HBLANK_PARAM("1280x720" , 60, 78, 159, 24, 4), },
/* { 440, 220, 40 } */
{ DSIM_HBLANK_PARAM("1280x720" , 50, 324, 159, 24, 4), },
/* { 16, 60, 62 } */
{ DSIM_HBLANK_PARAM("720x480" , 60, 6, 39, 40, 4), },
/* { 12, 68, 64 } */
{ DSIM_HBLANK_PARAM("720x576" , 50, 3, 45, 42, 4), },
/* { 16, 48, 96 } */
{ DSIM_HBLANK_PARAM("640x480" , 60, 6, 30, 66, 4), },
};
static const struct dsim_hblank_par hblank_2lanes[] = {
/* { 88, 148, 44 } */
{ DSIM_HBLANK_PARAM("1920x1080", 30, 114, 210, 60, 2), },
/* { 110, 220, 40 } */
{ DSIM_HBLANK_PARAM("1280x720" , 60, 159, 320, 40, 2), },
/* { 440, 220, 40 } */
{ DSIM_HBLANK_PARAM("1280x720" , 50, 654, 320, 40, 2), },
/* { 16, 60, 62 } */
{ DSIM_HBLANK_PARAM("720x480" , 60, 16, 66, 88, 2), },
/* { 12, 68, 64 } */
{ DSIM_HBLANK_PARAM("720x576" , 50, 12, 96, 72, 2), },
/* { 16, 48, 96 } */
{ DSIM_HBLANK_PARAM("640x480" , 60, 18, 66, 138, 2), },
};
static const struct dsim_hblank_par *sec_mipi_dsim_get_hblank_par(const char *name,
int vrefresh,
int lanes)
{
int i, size;
const struct dsim_hblank_par *hpar, *hblank;
if (unlikely(!name))
return NULL;
switch (lanes) {
case 2:
hblank = hblank_2lanes;
size = ARRAY_SIZE(hblank_2lanes);
break;
case 4:
hblank = hblank_4lanes;
size = ARRAY_SIZE(hblank_4lanes);
break;
default:
pr_err("No hblank data for mode %s with %d lanes\n",
name, lanes);
return NULL;
}
for (i = 0; i < size; i++) {
hpar = &hblank[i];
if (!strcmp(name, hpar->name)) {
if (vrefresh != hpar->vrefresh)
continue;
/* found */
return hpar;
}
}
return NULL;
}
static int sec_mipi_dsim_set_pref_rate(struct sec_mipi_dsim *dsim)
{
int ret;
uint32_t rate;
const struct sec_mipi_dsim_plat_data *pdata = dsim->pdata;
const struct sec_mipi_dsim_pll *dpll = pdata->dphy_pll;
const struct sec_mipi_dsim_range *fin_range = &dpll->fin;
ret = pdata->determine_pll_ref_rate(&rate, fin_range->min, fin_range->max);
if (ret)
return ret;
dsim->pref_clk = rate;
return 0;
}
static void sec_mipi_dsim_irq_init(struct sec_mipi_dsim *dsim);
/* For now, dsim only support one device attached */
static int sec_mipi_dsim_host_attach(struct mipi_dsi_host *host,
struct mipi_dsi_device *dsi)
{
struct sec_mipi_dsim *dsim = to_sec_mipi_dsim(host);
const struct sec_mipi_dsim_plat_data *pdata = dsim->pdata;
struct device *dev = dsim->dev;
struct drm_panel *panel;
if (!dsi->lanes || dsi->lanes > pdata->max_data_lanes) {
dev_err(dev, "invalid data lanes number\n");
return -EINVAL;
}
if (dsim->channel)
return -EINVAL;
if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO &&
dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST &&
dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
dev_err(dev, "unsupported dsi mode\n");
return -EINVAL;
}
if (dsi->format != MIPI_DSI_FMT_RGB888 &&
dsi->format != MIPI_DSI_FMT_RGB565 &&
dsi->format != MIPI_DSI_FMT_RGB666 &&
dsi->format != MIPI_DSI_FMT_RGB666_PACKED) {
dev_err(dev, "unsupported pixel format: %#x\n", dsi->format);
return -EINVAL;
}
if (!dsim->next) {
/* 'dsi' must be panel device */
panel = of_drm_find_panel(dsi->dev.of_node);
if (!panel) {
dev_err(dev, "refuse unknown dsi device attach\n");
WARN_ON(!panel);
return -ENODEV;
}
/* Don't support multiple panels */
if (dsim->panel && panel && dsim->panel != panel) {
dev_err(dev, "don't support multiple panels\n");
return -EBUSY;
}
dsim->panel = panel;
}
/* TODO: DSIM 3 lanes has some display issue, so
* avoid 3 lanes enable, and force data lanes to
* be 2.
*/
if (dsi->lanes == 3)
dsi->lanes = 2;
dsim->lanes = dsi->lanes;
dsim->channel = dsi->channel;
dsim->format = dsi->format;
dsim->mode_flags = dsi->mode_flags;
/* TODO: support later */
#if 0
if (dsim->connector.dev)
drm_helper_hpd_irq_event(dsim->connector.dev);
#endif
return 0;
}
static int sec_mipi_dsim_host_detach(struct mipi_dsi_host *host,
struct mipi_dsi_device *dsi)
{
struct sec_mipi_dsim *dsim = to_sec_mipi_dsim(host);
if (WARN_ON(!dsim->next && !dsim->panel))
return -ENODEV;
/* clear the saved dsi parameters */
dsim->lanes = 0;
dsim->channel = 0;
dsim->format = 0;
dsim->mode_flags = 0;
/* detached panel should be NULL */
dsim->panel = NULL;
return 0;
}
static void sec_mipi_dsim_config_cmd_lpm(struct sec_mipi_dsim *dsim,
bool enable)
{
uint32_t escmode;
escmode = dsim_read(dsim, DSIM_ESCMODE);
if (enable)
escmode |= ESCMODE_CMDLPDT;
else
escmode &= ~ESCMODE_CMDLPDT;
dsim_write(dsim, escmode, DSIM_ESCMODE);
}
static void sec_mipi_dsim_write_pl_to_sfr_fifo(struct sec_mipi_dsim *dsim,
const void *payload,
size_t length)
{
uint32_t pl_data;
if (!length)
return;
while (length >= 4) {
pl_data = get_unaligned_le32(payload);
dsim_write(dsim, pl_data, DSIM_PAYLOAD);
payload += 4;
length -= 4;
}
pl_data = 0;
switch (length) {
case 3:
pl_data |= ((u8 *)payload)[2] << 16;
fallthrough;
case 2:
pl_data |= ((u8 *)payload)[1] << 8;
fallthrough;
case 1:
pl_data |= ((u8 *)payload)[0];
dsim_write(dsim, pl_data, DSIM_PAYLOAD);
break;
}
}
static void sec_mipi_dsim_write_ph_to_sfr_fifo(struct sec_mipi_dsim *dsim,
void *header,
bool use_lpm)
{
uint32_t pkthdr;
pkthdr = PKTHDR_SET_DATA1(((u8 *)header)[2]) | /* WC MSB */
PKTHDR_SET_DATA0(((u8 *)header)[1]) | /* WC LSB */
PKTHDR_SET_DI(((u8 *)header)[0]); /* Data ID */
dsim_write(dsim, pkthdr, DSIM_PKTHDR);
}
static int sec_mipi_dsim_read_pl_from_sfr_fifo(struct sec_mipi_dsim *dsim,
void *payload,
size_t length)
{
uint8_t data_type;
uint16_t word_count = 0;
uint32_t fifoctrl, ph, pl;
fifoctrl = dsim_read(dsim, DSIM_FIFOCTRL);
if (WARN_ON(fifoctrl & FIFOCTRL_EMPTYRX))
return -EINVAL;
ph = dsim_read(dsim, DSIM_RXFIFO);
data_type = PKTHDR_GET_DT(ph);
switch (data_type) {
case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
dev_err(dsim->dev, "peripheral report error: (0-7)%x, (8-15)%x\n",
PKTHDR_GET_DATA0(ph), PKTHDR_GET_DATA1(ph));
return -EPROTO;
case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
if (!WARN_ON(length < 2)) {
((u8 *)payload)[1] = PKTHDR_GET_DATA1(ph);
word_count++;
}
fallthrough;
case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
((u8 *)payload)[0] = PKTHDR_GET_DATA0(ph);
word_count++;
length = word_count;
break;
case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
word_count = PKTHDR_GET_WC(ph);
if (word_count > length) {
dev_err(dsim->dev, "invalid receive buffer length\n");
return -EINVAL;
}
length = word_count;
while (word_count >= 4) {
pl = dsim_read(dsim, DSIM_RXFIFO);
((u8 *)payload)[0] = pl & 0xff;
((u8 *)payload)[1] = (pl >> 8) & 0xff;
((u8 *)payload)[2] = (pl >> 16) & 0xff;
((u8 *)payload)[3] = (pl >> 24) & 0xff;
payload += 4;
word_count -= 4;
}
if (word_count > 0) {
pl = dsim_read(dsim, DSIM_RXFIFO);
switch (word_count) {
case 3:
((u8 *)payload)[2] = (pl >> 16) & 0xff;
fallthrough;
case 2:
((u8 *)payload)[1] = (pl >> 8) & 0xff;
fallthrough;
case 1:
((u8 *)payload)[0] = pl & 0xff;
break;
}
}
break;
default:
return -EINVAL;
}
return length;
}
static ssize_t sec_mipi_dsim_host_transfer(struct mipi_dsi_host *host,
const struct mipi_dsi_msg *msg)
{
int ret;
ssize_t bytes = 0;
bool use_lpm;
struct mipi_dsi_packet packet;
struct sec_mipi_dsim *dsim = to_sec_mipi_dsim(host);
if ((msg->rx_buf && !msg->rx_len) || (msg->rx_len && !msg->rx_buf))
return -EINVAL;
ret = mipi_dsi_create_packet(&packet, msg);
if (ret) {
dev_err(dsim->dev, "failed to create dsi packet: %d\n", ret);
return ret;
}
/* need to read data from peripheral */
if (unlikely(msg->rx_buf))
reinit_completion(&dsim->rx_done);
/* config LPM for CMD TX */
use_lpm = msg->flags & MIPI_DSI_MSG_USE_LPM ? true : false;
sec_mipi_dsim_config_cmd_lpm(dsim, use_lpm);
if (packet.payload_length) { /* Long Packet case */
reinit_completion(&dsim->pl_tx_done);
/* write packet payload */
sec_mipi_dsim_write_pl_to_sfr_fifo(dsim,
packet.payload,
packet.payload_length);
/* write packet header */
sec_mipi_dsim_write_ph_to_sfr_fifo(dsim,
packet.header,
use_lpm);
ret = wait_for_completion_timeout(&dsim->ph_tx_done,
MIPI_FIFO_TIMEOUT);
if (!ret) {
dev_err(dsim->dev, "wait payload tx done time out\n");
return -EBUSY;
}
} else {
reinit_completion(&dsim->ph_tx_done);
/* write packet header */
sec_mipi_dsim_write_ph_to_sfr_fifo(dsim,
packet.header,
use_lpm);
ret = wait_for_completion_timeout(&dsim->ph_tx_done,
MIPI_FIFO_TIMEOUT);
if (!ret) {
dev_err(dsim->dev, "wait pkthdr tx done time out\n");
return -EBUSY;
}
}
/* read packet payload */
if (unlikely(msg->rx_buf)) {
ret = wait_for_completion_timeout(&dsim->rx_done,
MIPI_FIFO_TIMEOUT);
if (!ret) {
dev_err(dsim->dev, "wait rx done time out\n");
return -EBUSY;
}
ret = sec_mipi_dsim_read_pl_from_sfr_fifo(dsim,
msg->rx_buf,
msg->rx_len);
if (ret < 0)
return ret;
bytes = msg->rx_len;
} else {
bytes = packet.size;
}
return bytes;
}
static const struct mipi_dsi_host_ops sec_mipi_dsim_host_ops = {
.attach = sec_mipi_dsim_host_attach,
.detach = sec_mipi_dsim_host_detach,
.transfer = sec_mipi_dsim_host_transfer,
};
static int sec_mipi_dsim_bridge_attach(struct drm_bridge *bridge,
enum drm_bridge_attach_flags flags)
{
int ret;
bool attach_bridge = false;
struct sec_mipi_dsim *dsim = bridge->driver_private;
struct device *dev = dsim->dev;
struct device_node *np = dev->of_node;
struct device_node *endpoint, *remote = NULL;
struct drm_bridge *next = ERR_PTR(-ENODEV);
struct drm_encoder *encoder = dsim->encoder;
/* TODO: All bridges and planes should have already been added */
/* A panel has been found, ignor other dsi devices */
if (dsim->panel)
return 0;
/* find next bridge */
endpoint = of_graph_get_next_endpoint(np, NULL);
/* At least one endpoint should be existed */
if (!endpoint)
return -ENODEV;
while (endpoint) {
/* check the endpoint can attach bridge or not */
attach_bridge = of_property_read_bool(endpoint, "attach-bridge");
if (!attach_bridge) {
endpoint = of_graph_get_next_endpoint(np, endpoint);
continue;
}
remote = of_graph_get_remote_port_parent(endpoint);
if (!remote || !of_device_is_available(remote)) {
of_node_put(remote);
endpoint = of_graph_get_next_endpoint(np, endpoint);
continue;
}
next = of_drm_find_bridge(remote);
if (next) {
/* Found */
of_node_put(endpoint);
break;
}
endpoint = of_graph_get_next_endpoint(np, endpoint);
}
/* No workable bridge exists */
if (IS_ERR(next))
return PTR_ERR(next);
/* For the panel driver loading is after dsim bridge,
* defer bridge binding to wait for panel driver ready.
* The disadvantage of probe defer is endless probing
* in some cases.
*/
if (!next)
return -EPROBE_DEFER;
/* duplicate bridges or next bridge exists */
WARN_ON(bridge == next || drm_bridge_get_next_bridge(bridge) || dsim->next);
dsim->next = next;
next->encoder = encoder;
ret = drm_bridge_attach(encoder, next, bridge, flags);
if (ret) {
dev_err(dev, "Unable to attach bridge %s: %d\n",
remote->name, ret);
dsim->next = NULL;
return ret;
}
return 0;
}
static int sec_mipi_dsim_config_pll(struct sec_mipi_dsim *dsim)
{
int ret;
uint32_t pllctrl = 0, status, data_lanes_en, stop;
dsim_write(dsim, 0x8000, DSIM_PLLTMR);
/* TODO: config dp/dn swap if requires */
pllctrl |= PLLCTRL_SET_PMS(dsim->pms) | PLLCTRL_PLLEN;
dsim_write(dsim, pllctrl, DSIM_PLLCTRL);
ret = wait_for_completion_timeout(&dsim->pll_stable, HZ / 10);
if (!ret) {
dev_err(dsim->dev, "wait for pll stable time out\n");
return -EBUSY;
}
/* wait for clk & data lanes to go to stop state */
mdelay(1);
data_lanes_en = (0x1 << dsim->lanes) - 1;
status = dsim_read(dsim, DSIM_STATUS);
if (!(status & STATUS_STOPSTATECLK)) {
dev_err(dsim->dev, "clock is not in stop state\n");
return -EBUSY;
}
stop = STATUS_GET_STOPSTATEDAT(status);
if ((stop & data_lanes_en) != data_lanes_en) {
dev_err(dsim->dev,
"one or more data lanes is not in stop state\n");
return -EBUSY;
}
return 0;
}
static void sec_mipi_dsim_set_main_mode(struct sec_mipi_dsim *dsim)
{
uint32_t bpp, hfp_wc, hbp_wc, hsa_wc, wc;
uint32_t mdresol = 0, mvporch = 0, mhporch = 0, msync = 0;
struct videomode *vmode = &dsim->vmode;
mdresol |= MDRESOL_SET_MAINVRESOL(vmode->vactive) |
MDRESOL_SET_MAINHRESOL(vmode->hactive);
dsim_write(dsim, mdresol, DSIM_MDRESOL);
mvporch |= MVPORCH_SET_MAINVBP(vmode->vback_porch) |
MVPORCH_SET_STABLEVFP(vmode->vfront_porch) |
MVPORCH_SET_CMDALLOW(0x0);
dsim_write(dsim, mvporch, DSIM_MVPORCH);
bpp = mipi_dsi_pixel_format_to_bpp(dsim->format);
/* calculate hfp & hbp word counts */
if (!dsim->hpar) {
wc = DIV_ROUND_UP(vmode->hfront_porch * (bpp >> 3),
dsim->lanes);
hfp_wc = wc > MIPI_HFP_PKT_OVERHEAD ?
wc - MIPI_HFP_PKT_OVERHEAD : vmode->hfront_porch;
wc = DIV_ROUND_UP(vmode->hback_porch * (bpp >> 3),
dsim->lanes);
hbp_wc = wc > MIPI_HBP_PKT_OVERHEAD ?
wc - MIPI_HBP_PKT_OVERHEAD : vmode->hback_porch;
} else {
hfp_wc = dsim->hpar->hfp_wc;
hbp_wc = dsim->hpar->hbp_wc;
}
mhporch |= MHPORCH_SET_MAINHFP(hfp_wc) |
MHPORCH_SET_MAINHBP(hbp_wc);
dsim_write(dsim, mhporch, DSIM_MHPORCH);
/* calculate hsa word counts */
if (!dsim->hpar) {
wc = DIV_ROUND_UP(vmode->hsync_len * (bpp >> 3),
dsim->lanes);
hsa_wc = wc > MIPI_HSA_PKT_OVERHEAD ?
wc - MIPI_HSA_PKT_OVERHEAD : vmode->hsync_len;
} else
hsa_wc = dsim->hpar->hsa_wc;
msync |= MSYNC_SET_MAINVSA(vmode->vsync_len) |
MSYNC_SET_MAINHSA(hsa_wc);
dsim_write(dsim, msync, DSIM_MSYNC);
}
static void sec_mipi_dsim_config_dpi(struct sec_mipi_dsim *dsim)
{
uint32_t config = 0, rgb_status = 0, data_lanes_en;
if (dsim->mode_flags & MIPI_DSI_MODE_VIDEO)
rgb_status &= ~RGB_STATUS_CMDMODE_INSEL;
else
rgb_status |= RGB_STATUS_CMDMODE_INSEL;
dsim_write(dsim, rgb_status, DSIM_RGB_STATUS);
if (dsim->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) {
config |= CONFIG_NON_CONTINUOUS_CLOCK_LANE;
if (dsim->mode_flags & MIPI_DSI_MODE_VIDEO)
config |= CONFIG_CLKLANE_STOP_START;
}
if (dsim->mode_flags & MIPI_DSI_MODE_VSYNC_FLUSH)
config |= CONFIG_MFLUSH_VS;
/* disable EoT packets in HS mode */
if (dsim->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET)
config |= CONFIG_EOT_R03;
if (dsim->mode_flags & MIPI_DSI_MODE_VIDEO) {
config |= CONFIG_VIDEOMODE;
if (dsim->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
config |= CONFIG_BURSTMODE;
else if (dsim->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
config |= CONFIG_SYNCINFORM;
if (dsim->mode_flags & MIPI_DSI_MODE_VIDEO_AUTO_VERT)
config |= CONFIG_AUTOMODE;
if (dsim->mode_flags & MIPI_DSI_MODE_VIDEO_HSE)
config |= CONFIG_HSEDISABLEMODE;
if (dsim->mode_flags & MIPI_DSI_MODE_VIDEO_NO_HFP)
config |= CONFIG_HFPDISABLEMODE;
if (dsim->mode_flags & MIPI_DSI_MODE_VIDEO_NO_HBP)
config |= CONFIG_HBPDISABLEMODE;
if (dsim->mode_flags & MIPI_DSI_MODE_VIDEO_NO_HSA)
config |= CONFIG_HSADISABLEMODE;
}
config |= CONFIG_SET_MAINVC(dsim->channel);
switch (dsim->format) {
case MIPI_DSI_FMT_RGB565:
config |= dsim->mode_flags & MIPI_DSI_MODE_VIDEO ?
CONFIG_SET_MAINPIXFORMAT(0x4) :
CONFIG_SET_MAINPIXFORMAT(0x3);
break;
case MIPI_DSI_FMT_RGB666_PACKED:
if (dsim->mode_flags & MIPI_DSI_MODE_VIDEO)
config |= CONFIG_SET_MAINPIXFORMAT(0x5);
break;
case MIPI_DSI_FMT_RGB666:
config |= CONFIG_SET_MAINPIXFORMAT(0x6);
break;
case MIPI_DSI_FMT_RGB888:
config |= CONFIG_SET_MAINPIXFORMAT(0x7);
break;
default:
config |= CONFIG_SET_MAINPIXFORMAT(0x7);
break;
}
/* config data lanes number and enable lanes */
data_lanes_en = (0x1 << dsim->lanes) - 1;
config |= CONFIG_SET_NUMOFDATLANE(dsim->lanes - 1);