summaryrefslogtreecommitdiff
path: root/bsp/rockchip/dm/graphic/framebuffer/fb-vop1.c
blob: d5391e869615d40d3f549cb4d522938d23f69277 (plain)
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
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-09-23     GuEe-GUI     first version
 */

#include "fb-vop1.h"

#include <cpuport.h>

#define DBG_TAG "fb.vop1"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

/* RK3366 Lite (rk3308 / rk3506 / px30-lit class) */
#define VOP1_LIT_CFG_DONE               0x000
#define VOP1_LIT_WIN0_CTRL0             0x050
#define VOP1_LIT_WIN0_VIR               0x05c
#define VOP1_LIT_WIN0_YRGB_MST          0x060
#define VOP1_LIT_WIN0_ACT_INFO          0x068
#define VOP1_LIT_WIN0_DSP_INFO          0x06c
#define VOP1_LIT_WIN0_DSP_ST            0x070
#define VOP1_LIT_WIN0_ALPHA             0x080
#define VOP1_LIT_WIN1_CTRL0             0x090
#define VOP1_LIT_WIN1_VIR               0x098
#define VOP1_LIT_WIN1_MST               0x0a0
#define VOP1_LIT_WIN1_DSP_INFO          0x0a4
#define VOP1_LIT_WIN1_DSP_ST            0x0a8
#define VOP1_LIT_WIN1_ALPHA             0x0bc

/* RK3288-class big VOP (rk3328 / rk3288 / rk3399) */
#define VOP1_BIG_CFG_DONE               0x000

#define VOP1_WIN_EN_MASK                1
#define VOP1_WIN_EN_SHIFT               0
#define VOP1_WIN_FORMAT_MASK            0x7
#define VOP1_WIN_FORMAT_SHIFT           1

/* -------------------------------------------------------------------------- */
/* Lite window maps                                                            */
/* -------------------------------------------------------------------------- */

static const struct vop1fb_win_regs lite_win0_regs =
{
    .ctrl0 = VOP1_LIT_WIN0_CTRL0,
    .vir = VOP1_LIT_WIN0_VIR,
    .yrgb_mst = VOP1_LIT_WIN0_YRGB_MST,
    .act_info = VOP1_LIT_WIN0_ACT_INFO,
    .dsp_info = VOP1_LIT_WIN0_DSP_INFO,
    .dsp_st = VOP1_LIT_WIN0_DSP_ST,
    .alpha = VOP1_LIT_WIN0_ALPHA,
};

static const struct vop1fb_win_regs lite_win1_regs =
{
    .ctrl0 = VOP1_LIT_WIN1_CTRL0,
    .vir = VOP1_LIT_WIN1_VIR,
    .yrgb_mst = VOP1_LIT_WIN1_MST,
    .dsp_info = VOP1_LIT_WIN1_DSP_INFO,
    .dsp_st = VOP1_LIT_WIN1_DSP_ST,
    .alpha = VOP1_LIT_WIN1_ALPHA,
};

static const struct vop1fb_win_desc lite_primary =
{
    .name = "Win0",
    .regs = &lite_win0_regs,
    .z_order = 1,
};

static const struct vop1fb_win_desc lite_cursor =
{
    .name = "Win1",
    .regs = &lite_win1_regs,
    .z_order = 2,
};

/* -------------------------------------------------------------------------- */
/* Big window maps (win-relative offsets from each win base)                   */
/* -------------------------------------------------------------------------- */

#define VOP1_BIG_WIN_CTRL0              0x00
#define VOP1_BIG_WIN_VIR                0x0c
#define VOP1_BIG_WIN_YRGB_MST           0x10
#define VOP1_BIG_WIN_ACT_INFO           0x18
#define VOP1_BIG_WIN_DSP_INFO           0x1c
#define VOP1_BIG_WIN_DSP_ST             0x20
#define VOP1_BIG_WIN_ALPHA              0x30

static const struct vop1fb_win_regs big_win0_regs =
{
    .ctrl0 = 0xd0 + VOP1_BIG_WIN_CTRL0,
    .vir = 0xd0 + VOP1_BIG_WIN_VIR,
    .yrgb_mst = 0xd0 + VOP1_BIG_WIN_YRGB_MST,
    .act_info = 0xd0 + VOP1_BIG_WIN_ACT_INFO,
    .dsp_info = 0xd0 + VOP1_BIG_WIN_DSP_INFO,
    .dsp_st = 0xd0 + VOP1_BIG_WIN_DSP_ST,
    .alpha = 0xd0 + VOP1_BIG_WIN_ALPHA,
};

static const struct vop1fb_win_regs big_win1_regs =
{
    .ctrl0 = 0x1d0 + VOP1_BIG_WIN_CTRL0,
    .vir = 0x1d0 + VOP1_BIG_WIN_VIR,
    .yrgb_mst = 0x1d0 + VOP1_BIG_WIN_YRGB_MST,
    .act_info = 0x1d0 + VOP1_BIG_WIN_ACT_INFO,
    .dsp_info = 0x1d0 + VOP1_BIG_WIN_DSP_INFO,
    .dsp_st = 0x1d0 + VOP1_BIG_WIN_DSP_ST,
    .alpha = 0x1d0 + VOP1_BIG_WIN_ALPHA,
};

static const struct vop1fb_win_regs big_win2_regs =
{
    .ctrl0 = 0x2d0 + VOP1_BIG_WIN_CTRL0,
    .vir = 0x2d0 + VOP1_BIG_WIN_VIR,
    .yrgb_mst = 0x2d0 + VOP1_BIG_WIN_YRGB_MST,
    .act_info = 0x2d0 + VOP1_BIG_WIN_ACT_INFO,
    .dsp_info = 0x2d0 + VOP1_BIG_WIN_DSP_INFO,
    .dsp_st = 0x2d0 + VOP1_BIG_WIN_DSP_ST,
    .alpha = 0x2d0 + VOP1_BIG_WIN_ALPHA,
};

static const struct vop1fb_win_desc big_primary =
{
    .name = "Win0",
    .regs = &big_win0_regs,
    .z_order = 1,
};

static const struct vop1fb_win_desc big_cursor =
{
    .name = "Win2",
    .regs = &big_win2_regs,
    .z_order = 3,
};

static const struct vop1fb_win_desc big_overlays[] =
{
    {
        .name = "Win1",
        .regs = &big_win1_regs,
        .z_order = 2,
    },
};

static const struct vop1fb_soc_data vop1_lite_soc =
{
    .arch = VOP1FB_ARCH_LITE,
    .version = "vop1-lite",
    .reg_size = 0x200,
    .cfg_done = VOP1_LIT_CFG_DONE,
    .primary = &lite_primary,
    .cursor = &lite_cursor,
    .overlay_count = 0,
};

static const struct vop1fb_soc_data vop1_big_soc =
{
    .arch = VOP1FB_ARCH_BIG,
    .version = "vop1-big",
    .reg_size = 0x400,
    .cfg_done = VOP1_BIG_CFG_DONE,
    .primary = &big_primary,
    .cursor = &big_cursor,
    .overlays = big_overlays,
    .overlay_count = RT_ARRAY_SIZE(big_overlays),
};

static rt_uint32_t vop1fb_modes[] =
{
    RTGRAPHIC_PIXEL_FORMAT_ARGB888,
    RTGRAPHIC_PIXEL_FORMAT_RGB888,
    RTGRAPHIC_PIXEL_FORMAT_RGB565,
};

/* -------------------------------------------------------------------------- */
/* Helpers                                                                    */
/* -------------------------------------------------------------------------- */

rt_uint32_t vop1fb_mode_to_hw(rt_uint32_t mode)
{
    switch (mode)
    {
    case RTGRAPHIC_PIXEL_FORMAT_ARGB888:
    case RTGRAPHIC_PIXEL_FORMAT_ABGR888:
        return VOP1FB_FMT_ARGB8888;
    case RTGRAPHIC_PIXEL_FORMAT_RGB888:
    case RTGRAPHIC_PIXEL_FORMAT_BGR888:
        return VOP1FB_FMT_RGB888;
    case RTGRAPHIC_PIXEL_FORMAT_RGB565:
    case RTGRAPHIC_PIXEL_FORMAT_BGR565:
        return VOP1FB_FMT_RGB565;
    default:
        return VOP1FB_FMT_ARGB8888;
    }
}

rt_uint32_t vop1fb_hw_to_bpp(rt_uint32_t hw_fmt)
{
    switch (hw_fmt)
    {
    case VOP1FB_FMT_RGB565:
        return 16;
    case VOP1FB_FMT_RGB888:
        return 24;
    default:
        return 32;
    }
}

static struct vop1fb *vop1fb_from_plane(struct rt_graphic_plane *plane)
{
    return rt_container_of(plane->graphic, struct vop1fb, parent);
}

static struct vop1fb_plane_priv *vop1fb_plane_priv(struct rt_graphic_plane *plane)
{
    return (struct vop1fb_plane_priv *)plane->priv;
}

rt_inline void vop1fb_reg_write(struct vop1fb *vfb, rt_uint32_t offset, rt_uint32_t v)
{
    HWREG32((rt_uint8_t *)vfb->regs + offset) = v;
}

rt_inline void vop1fb_mask_write(struct vop1fb *vfb, rt_uint32_t offset,
        rt_uint32_t mask, rt_uint32_t shift, rt_uint32_t v)
{
    rt_uint32_t cached = HWREG32((rt_uint8_t *)vfb->regs + offset);
    rt_uint32_t newv = (cached & ~(mask << shift)) | ((v & mask) << shift);

    vop1fb_reg_write(vfb, offset, newv);
}

static rt_uint32_t vop1fb_xvir(rt_uint32_t width, rt_uint32_t bpp)
{
    return RT_ALIGN(width * bpp, 32) >> 5;
}

static void vop1fb_cfg_done(struct vop1fb *vfb)
{
    vop1fb_reg_write(vfb, vfb->soc->cfg_done, 1);
}

static void vop1fb_win_disable(struct vop1fb *vfb, const struct vop1fb_win_desc *desc)
{
    const struct vop1fb_win_regs *r = desc->regs;

    vop1fb_mask_write(vfb, r->ctrl0, VOP1_WIN_EN_MASK, VOP1_WIN_EN_SHIFT, 0);
    vop1fb_cfg_done(vfb);
}

static void vop1fb_win_alpha(struct vop1fb *vfb, const struct vop1fb_win_desc *desc,
        rt_uint8_t alpha)
{
    const struct vop1fb_win_regs *r = desc->regs;

    if (!r->alpha)
    {
        return;
    }

    /* alpha_en | alpha_mode | global_alpha */
    vop1fb_reg_write(vfb, r->alpha, RT_BIT(0) | RT_BIT(1) | ((rt_uint32_t)alpha << 4));
}

static rt_err_t vop1fb_win_commit(struct vop1fb *vfb, const struct vop1fb_win_desc *desc,
        rt_ubase_t dma_addr, rt_uint32_t hw_fmt,
        rt_uint32_t src_w, rt_uint32_t src_h,
        rt_uint32_t dst_x, rt_uint32_t dst_y,
        rt_uint32_t dst_w, rt_uint32_t dst_h,
        rt_uint32_t xvir, rt_bool_t enable, rt_uint8_t alpha)
{
    const struct vop1fb_win_regs *r = desc->regs;
    rt_uint32_t act_info, dsp_info, dsp_st;
    rt_uint32_t fmt_shift = VOP1_WIN_FORMAT_SHIFT;

    if (!enable)
    {
        vop1fb_win_disable(vfb, desc);
        return RT_EOK;
    }

    if (vfb->soc->arch == VOP1FB_ARCH_LITE && desc != vfb->soc->primary)
    {
        fmt_shift = 4;
    }

    act_info = ((src_h - 1) << 16) | ((src_w - 1) & 0xffff);
    dsp_info = ((dst_h - 1) << 16) | ((dst_w - 1) & 0xffff);
    dsp_st = (dst_y << 16) | (dst_x & 0xffff);

    vop1fb_mask_write(vfb, r->ctrl0, VOP1_WIN_FORMAT_MASK, fmt_shift, hw_fmt);

    if (r->vir)
    {
        vop1fb_reg_write(vfb, r->vir, xvir);
    }

    vop1fb_reg_write(vfb, r->yrgb_mst, (rt_uint32_t)dma_addr);

    if (r->act_info)
    {
        vop1fb_reg_write(vfb, r->act_info, act_info);
    }

    vop1fb_reg_write(vfb, r->dsp_info, dsp_info);
    vop1fb_reg_write(vfb, r->dsp_st, dsp_st);
    vop1fb_win_alpha(vfb, desc, alpha);
    vop1fb_mask_write(vfb, r->ctrl0, VOP1_WIN_EN_MASK, VOP1_WIN_EN_SHIFT, 1);
    vop1fb_cfg_done(vfb);

    return RT_EOK;
}

static rt_err_t vop1fb_plane_commit(struct rt_graphic_plane *plane, rt_bool_t enable)
{
    struct vop1fb *vfb = vop1fb_from_plane(plane);
    struct vop1fb_plane_priv *priv = vop1fb_plane_priv(plane);
    rt_uint32_t bpp = rt_graphic_mode_bpp(plane->mode);
    rt_uint32_t xvir;
    rt_ubase_t dma;
    rt_uint32_t dst_w, dst_h, dst_x, dst_y;

    if (!priv || !priv->desc)
    {
        return -RT_EINVAL;
    }

    if (!enable || !plane->framebuffer)
    {
        priv->enabled = RT_FALSE;
        vop1fb_win_disable(vfb, priv->desc);
        return RT_EOK;
    }

    dma = priv->dma_handle + priv->pan_offset;
    xvir = vop1fb_xvir(plane->width, bpp);

    if (plane->type == RT_GRAPHIC_PLANE_TYPE_PRIMARY)
    {
        dst_x = 0;
        dst_y = 0;
        dst_w = plane->width;
        dst_h = plane->height;
    }
    else
    {
        dst_x = plane->x;
        dst_y = plane->y;
        dst_w = plane->width;
        dst_h = plane->height;
    }

    priv->enabled = RT_TRUE;

    return vop1fb_win_commit(vfb, priv->desc, dma, vop1fb_mode_to_hw(plane->mode),
            plane->width, plane->height, dst_x, dst_y, dst_w, dst_h, xvir, RT_TRUE,
            plane->alpha ? plane->alpha : 0xff);
}

static void vop1fb_plane_free_fb(struct rt_graphic_plane *plane)
{
    struct vop1fb *vfb = vop1fb_from_plane(plane);
    struct vop1fb_plane_priv *priv = vop1fb_plane_priv(plane);
    struct rt_device *dev = &vfb->parent.parent;

    if (plane->framebuffer)
    {
        rt_dma_free_coherent(dev, plane->framebuffer_len,
                plane->framebuffer, priv->dma_handle);
        plane->framebuffer = RT_NULL;
        plane->framebuffer_len = 0;
        plane->screen_len = 0;
        priv->dma_handle = 0;
        priv->pan_offset = 0;
    }
}

static rt_err_t vop1fb_plane_update(struct rt_graphic_plane *plane,
        struct rt_device_rect_info *rect)
{
    struct vop1fb_plane_priv *priv = vop1fb_plane_priv(plane);

    RT_UNUSED(rect);

    if (priv)
    {
        priv->enabled = RT_TRUE;
    }

    return vop1fb_plane_commit(plane, RT_TRUE);
}

static rt_err_t vop1fb_plane_fb_remap(struct rt_graphic_plane *plane,
        rt_uint32_t mode, struct rt_device_rect_info *rect)
{
    rt_err_t err;
    struct vop1fb *vfb = vop1fb_from_plane(plane);
    struct vop1fb_plane_priv *priv = vop1fb_plane_priv(plane);
    struct rt_device *dev = &vfb->parent.parent;
    rt_uint32_t bpp = rt_graphic_mode_bpp(mode);
    rt_size_t screen_len, fb_len;
    void *fb;

    if (!rect->width || !rect->height || !bpp)
    {
        return -RT_EINVAL;
    }

    vop1fb_plane_free_fb(plane);

    plane->line_length = RT_ALIGN(rect->width * bpp, 32) / 8;
    plane->bits_per_pixel = bpp;
    screen_len = plane->line_length * rect->height;

    if (plane->type == RT_GRAPHIC_PLANE_TYPE_PRIMARY)
    {
        fb_len = screen_len * 2;
    }
    else
    {
        fb_len = screen_len;
    }

    fb = rt_dma_alloc_coherent(dev, fb_len, &priv->dma_handle);

    if (!fb)
    {
        return -RT_ENOMEM;
    }

    rt_memset(fb, 0, fb_len);

    plane->framebuffer = fb;
    plane->screen_len = screen_len;
    plane->framebuffer_len = fb_len;
    plane->width = rect->width;
    plane->height = rect->height;
    plane->mode = mode;
    priv->pan_offset = 0;

    if (vfb->powered && plane->type == RT_GRAPHIC_PLANE_TYPE_PRIMARY)
    {
        err = vop1fb_plane_commit(plane, RT_TRUE);
    }
    else
    {
        err = RT_EOK;
    }

    return err;
}

static rt_err_t vop1fb_plane_fb_pan_display(struct rt_graphic_plane *plane,
        struct rt_device_rect_info *rect)
{
    struct vop1fb_plane_priv *priv = vop1fb_plane_priv(plane);

    if (rect->width != plane->width || rect->height != plane->height)
    {
        return vop1fb_plane_fb_remap(plane, plane->mode, rect);
    }

    if (plane->framebuffer_len <= plane->screen_len)
    {
        plane->x = rect->x;
        plane->y = rect->y;
        return RT_EOK;
    }

    priv->pan_offset = rect->y * plane->line_length + rect->x * (plane->bits_per_pixel / 8);

    if (priv->pan_offset + plane->screen_len > plane->framebuffer_len)
    {
        priv->pan_offset = 0;
        return -RT_EINVAL;
    }

    plane->x = rect->x;
    plane->y = rect->y;

    return vop1fb_plane_commit(plane, RT_TRUE);
}

static rt_err_t vop1fb_plane_fb_cleanup(struct rt_graphic_plane *plane)
{
    struct vop1fb *vfb = vop1fb_from_plane(plane);
    struct vop1fb_plane_priv *priv = vop1fb_plane_priv(plane);

    if (priv && priv->enabled && priv->desc)
    {
        vop1fb_win_disable(vfb, priv->desc);
        priv->enabled = RT_FALSE;
    }

    vop1fb_plane_free_fb(plane);

    return RT_EOK;
}

static rt_err_t vop1fb_plane_prop_set(struct rt_graphic_plane *plane,
        enum rt_graphic_plane_prop prop, void *value)
{
    struct vop1fb *vfb = vop1fb_from_plane(plane);
    struct vop1fb_plane_priv *priv = vop1fb_plane_priv(plane);

    if (!priv || !priv->desc)
    {
        return -RT_EINVAL;
    }

    switch (prop)
    {
    case RT_GRAPHIC_PLANE_PROP_Z:
        if (value)
        {
            plane->z = (rt_uint32_t)(rt_ubase_t)value;
        }
        return RT_EOK;

    case RT_GRAPHIC_PLANE_PROP_ALPHA:
        if (value)
        {
            plane->alpha = (rt_uint8_t)(rt_ubase_t)value;
            vop1fb_win_alpha(vfb, priv->desc, plane->alpha);
            vop1fb_cfg_done(vfb);
        }
        return RT_EOK;

    default:
        return -RT_ENOSYS;
    }
}

static const struct rt_graphic_plane_ops vop1fb_primary_ops =
{
    .update = vop1fb_plane_update,
    .fb_remap = vop1fb_plane_fb_remap,
    .fb_pan_display = vop1fb_plane_fb_pan_display,
    .fb_cleanup = vop1fb_plane_fb_cleanup,
    .prop_set = vop1fb_plane_prop_set,
};

static const struct rt_graphic_plane_ops vop1fb_overlay_ops =
{
    .update = vop1fb_plane_update,
    .fb_remap = vop1fb_plane_fb_remap,
    .fb_cleanup = vop1fb_plane_fb_cleanup,
    .prop_set = vop1fb_plane_prop_set,
};

static const struct rt_graphic_plane_ops vop1fb_cursor_ops =
{
    .update = vop1fb_plane_update,
    .fb_remap = vop1fb_plane_fb_remap,
    .fb_cleanup = vop1fb_plane_fb_cleanup,
    .prop_set = vop1fb_plane_prop_set,
};

static rt_err_t vop1fb_clk_enable(struct vop1fb *vfb)
{
    if (!vfb->clk_enabled && !rt_is_err_or_null(vfb->clks))
    {
        rt_clk_array_prepare_enable(vfb->clks);
        vfb->clk_enabled = RT_TRUE;
    }

    return RT_EOK;
}

static void vop1fb_clk_disable(struct vop1fb *vfb)
{
    if (vfb->clk_enabled && !rt_is_err_or_null(vfb->clks))
    {
        rt_clk_array_disable_unprepare(vfb->clks);
        vfb->clk_enabled = RT_FALSE;
    }
}

static rt_err_t vop1fb_dpms_switch(struct rt_graphic_device *gdev, rt_uint32_t dpms)
{
    struct vop1fb *vfb = rt_container_of(gdev, struct vop1fb, parent);
    rt_size_t i;

    switch (dpms)
    {
    case RT_GRAPHIC_DPMS_ON:
        vop1fb_clk_enable(vfb);
        vfb->powered = RT_TRUE;

        if (vfb->primary && vfb->primary->framebuffer)
        {
            vop1fb_plane_commit(vfb->primary, RT_TRUE);
        }

        for (i = 0; i < vfb->overlay_count; ++i)
        {
            struct rt_graphic_plane *plane = vfb->overlays[i];
            struct vop1fb_plane_priv *priv;

            if (!plane || !plane->framebuffer)
            {
                continue;
            }

            priv = vop1fb_plane_priv(plane);

            if (priv && priv->enabled)
            {
                vop1fb_plane_commit(plane, RT_TRUE);
            }
        }

        if (vfb->cursor && vfb->cursor->framebuffer)
        {
            struct vop1fb_plane_priv *priv = vop1fb_plane_priv(vfb->cursor);

            if (priv && priv->enabled)
            {
                vop1fb_plane_commit(vfb->cursor, RT_TRUE);
            }
        }

        rt_graphic_device_update_auto(gdev, RT_GRAPHIC_UPDATE_MS);
        break;

    case RT_GRAPHIC_DPMS_STANDBY:
    case RT_GRAPHIC_DPMS_SUSPEND:
        rt_graphic_device_update_auto(gdev, 0);

        if (vfb->primary)
        {
            vop1fb_plane_commit(vfb->primary, RT_FALSE);
        }

        for (i = 0; i < vfb->overlay_count; ++i)
        {
            if (vfb->overlays[i])
            {
                vop1fb_plane_commit(vfb->overlays[i], RT_FALSE);
            }
        }

        if (vfb->cursor)
        {
            vop1fb_plane_commit(vfb->cursor, RT_FALSE);
        }
        break;

    case RT_GRAPHIC_DPMS_OFF:
        rt_graphic_device_update_auto(gdev, 0);

        if (vfb->primary)
        {
            vop1fb_plane_commit(vfb->primary, RT_FALSE);
        }

        for (i = 0; i < vfb->overlay_count; ++i)
        {
            if (vfb->overlays[i])
            {
                vop1fb_plane_commit(vfb->overlays[i], RT_FALSE);
            }
        }

        if (vfb->cursor)
        {
            vop1fb_plane_commit(vfb->cursor, RT_FALSE);
        }

        vop1fb_clk_disable(vfb);
        vfb->powered = RT_FALSE;
        break;

    default:
        return -RT_EINVAL;
    }

    gdev->dpms = dpms;

    return RT_EOK;
}

static rt_err_t vop1fb_wait_vsync(struct rt_graphic_device *gdev)
{
    struct vop1fb *vfb = rt_container_of(gdev, struct vop1fb, parent);

    if (vfb->irq >= 0)
    {
        rt_atomic_store(&vfb->vsync, 0);

        if (rt_sem_take(&vfb->vsync_sem, rt_tick_from_millisecond(100)) == RT_EOK)
        {
            return RT_EOK;
        }
    }

    rt_thread_mdelay(16);

    return RT_EOK;
}

static const struct rt_graphic_device_ops vop1fb_ops =
{
    .dpms_switch = vop1fb_dpms_switch,
    .wait_vsync = vop1fb_wait_vsync,
};

static void vop1fb_isr(int irqno, void *param)
{
    struct vop1fb *vfb = param;

    RT_UNUSED(irqno);

    rt_atomic_store(&vfb->vsync, 1);
    rt_sem_release(&vfb->vsync_sem);
}

static void vop1fb_plane_init_fb_state(struct rt_graphic_plane *plane, rt_uint32_t mode)
{
    rt_uint32_t bpp = rt_graphic_mode_bpp(mode);

    plane->mode = mode;
    plane->bits_per_pixel = bpp;
    plane->line_length = RT_ALIGN(plane->width * bpp, 32) / 8;
    plane->screen_len = plane->line_length * plane->height;
    plane->framebuffer = RT_NULL;
    plane->framebuffer_len = 0;
}

static rt_err_t vop1fb_plane_init(struct vop1fb *vfb,
        struct rt_graphic_plane *plane,
        const struct vop1fb_win_desc *desc,
        rt_uint32_t width, rt_uint32_t height,
        rt_uint8_t type)
{
    struct vop1fb_plane_priv *priv = vop1fb_plane_priv(plane);

    priv->desc = desc;
    priv->enabled = RT_FALSE;
    priv->pan_offset = 0;
    plane->z = desc->z_order;
    plane->alpha = 0xff;

    if (type == RT_GRAPHIC_PLANE_TYPE_CURSOR)
    {
        plane->width = VOP1FB_CURSOR_W;
        plane->height = VOP1FB_CURSOR_H;
    }
    else if (type == RT_GRAPHIC_PLANE_TYPE_OVERLAY)
    {
        plane->width = width / 4;
        plane->height = height / 4;
        plane->x = plane->width;
        plane->y = plane->height;
    }
    else
    {
        plane->width = width;
        plane->height = height;
    }

    vop1fb_plane_init_fb_state(plane, RTGRAPHIC_PIXEL_FORMAT_ARGB888);

    return RT_EOK;
}

static rt_err_t vop1fb_primary_plane_create(struct vop1fb *vfb,
        rt_uint32_t width, rt_uint32_t height)
{
    rt_err_t err;
    struct rt_graphic_plane *plane;

    plane = rt_graphic_device_alloc_plane(&vfb->parent, sizeof(struct vop1fb_plane_priv),
            &vop1fb_primary_ops, vop1fb_modes, RT_ARRAY_SIZE(vop1fb_modes),
            RT_GRAPHIC_PLANE_TYPE_PRIMARY);

    if (!plane)
    {
        return -RT_ENOMEM;
    }

    if ((err = rt_graphic_device_add_plane(&vfb->parent, plane)))
    {
        rt_graphic_device_free_plane(plane);
        return err;
    }

    vfb->primary = plane;

    err = vop1fb_plane_init(vfb, plane, vfb->soc->primary, width, height,
            RT_GRAPHIC_PLANE_TYPE_PRIMARY);
    if (err)
    {
        vfb->primary = RT_NULL;
        vfb->parent.primary_plane = RT_NULL;
        rt_graphic_device_free_plane(plane);
    }

    return err;
}

static rt_err_t vop1fb_overlay_planes_create(struct vop1fb *vfb,
        rt_uint32_t width, rt_uint32_t height)
{
    rt_err_t err;
    rt_size_t i;
    struct rt_graphic_plane *plane;

    for (i = 0; i < vfb->overlay_count; ++i)
    {
        plane = rt_graphic_device_alloc_plane(&vfb->parent, sizeof(struct vop1fb_plane_priv),
                &vop1fb_overlay_ops, vop1fb_modes, RT_ARRAY_SIZE(vop1fb_modes),
                RT_GRAPHIC_PLANE_TYPE_OVERLAY);

        if (!plane)
        {
            return -RT_ENOMEM;
        }

        rt_snprintf(plane->name, sizeof(plane->name), "ovl-%s", vfb->soc->overlays[i].name);

        if ((err = rt_graphic_device_add_plane(&vfb->parent, plane)))
        {
            rt_graphic_device_free_plane(plane);
            return err;
        }

        vfb->overlays[i] = plane;

        if ((err = vop1fb_plane_init(vfb, plane, &vfb->soc->overlays[i],
                width, height, RT_GRAPHIC_PLANE_TYPE_OVERLAY)))
        {
            return err;
        }
    }

    return RT_EOK;
}

static rt_err_t vop1fb_cursor_plane_create(struct vop1fb *vfb,
        rt_uint32_t width, rt_uint32_t height)
{
    rt_err_t err;
    struct rt_graphic_plane *plane;

    plane = rt_graphic_device_alloc_plane(&vfb->parent, sizeof(struct vop1fb_plane_priv),
            &vop1fb_cursor_ops, vop1fb_modes, RT_ARRAY_SIZE(vop1fb_modes),
            RT_GRAPHIC_PLANE_TYPE_CURSOR);

    if (!plane)
    {
        return -RT_ENOMEM;
    }

    if ((err = rt_graphic_device_add_plane(&vfb->parent, plane)))
    {
        rt_graphic_device_free_plane(plane);
        return err;
    }

    vfb->cursor = plane;

    return vop1fb_plane_init(vfb, plane, vfb->soc->cursor, width, height,
            RT_GRAPHIC_PLANE_TYPE_CURSOR);
}

static const struct vop1fb_soc_data *vop1fb_soc_from_id(const struct rt_ofw_node_id *id)
{
    if (!id || !id->compatible)
    {
        return RT_NULL;
    }

    /* Lite VOP */
    if (!rt_strcmp(id->compatible, "rockchip,rk3308-vop") ||
        !rt_strcmp(id->compatible, "rockchip,rk3506-vop") ||
        !rt_strcmp(id->compatible, "rockchip,px30-vop-lit"))
    {
        return &vop1_lite_soc;
    }

    /* Big VOP */
    if (!rt_strcmp(id->compatible, "rockchip,rk3328-vop") ||
        !rt_strcmp(id->compatible, "rockchip,rk3288-vop-big") ||
        !rt_strcmp(id->compatible, "rockchip,rk3288-vop-lit") ||
        !rt_strcmp(id->compatible, "rockchip,rk3399-vop-big") ||
        !rt_strcmp(id->compatible, "rockchip,rk3399-vop-lit") ||
        !rt_strcmp(id->compatible, "rockchip,rk3368-vop") ||
        !rt_strcmp(id->compatible, "rockchip,rk3366-vop") ||
        !rt_strcmp(id->compatible, "rockchip,rk3228-vop") ||
        !rt_strcmp(id->compatible, "rockchip,rv1126-vop") ||
        !rt_strcmp(id->compatible, "rockchip,rv1126b-vop") ||
        !rt_strcmp(id->compatible, "rockchip,rv1106-vop"))
    {
        return &vop1_big_soc;
    }

    return RT_NULL;
}

static rt_err_t vop1fb_parse_display(struct vop1fb *vfb, struct rt_platform_device *pdev)
{
    struct rt_device *dev = &pdev->parent;

    vfb->display_w = 1920;
    vfb->display_h = 1080;

    rt_dm_dev_prop_read_u32(dev, "rockchip,fb-width", &vfb->display_w);
    rt_dm_dev_prop_read_u32(dev, "rockchip,fb-height", &vfb->display_h);

    if (!vfb->display_w || !vfb->display_h)
    {
        return -RT_EINVAL;
    }

    return RT_EOK;
}

static rt_err_t vop1fb_probe(struct rt_platform_device *pdev)
{
    rt_err_t err;
    struct rt_device *dev = &pdev->parent;
    struct vop1fb *vfb = rt_calloc(1, sizeof(*vfb));

    if (!vfb)
    {
        return -RT_ENOMEM;
    }

    vfb->soc = vop1fb_soc_from_id(pdev->id);

    if (!vfb->soc)
    {
        err = -RT_ENOSYS;
        goto _fail;
    }

    if ((err = vop1fb_parse_display(vfb, pdev)))
    {
        goto _fail;
    }

    vfb->regs = rt_ofw_iomap(dev->ofw_node, 0);

    if (!vfb->regs)
    {
        err = -RT_EIO;
        goto _fail;
    }

    vfb->clks = rt_clk_get_array(dev);

    if (rt_is_err(vfb->clks))
    {
        err = rt_ptr_err(vfb->clks);
        goto _fail_unmap;
    }

    vfb->irq = rt_dm_dev_get_irq(dev, 0);

    if (vfb->irq >= 0)
    {
        rt_sem_init(&vfb->vsync_sem, "vop1-vsync", 0, RT_IPC_FLAG_FIFO);
        rt_hw_interrupt_install(vfb->irq, vop1fb_isr, vfb, "vop1");
        rt_hw_interrupt_umask(vfb->irq);
    }

    vfb->parent.ops = &vop1fb_ops;

    if ((err = vop1fb_clk_enable(vfb)))
    {
        goto _fail_irq;
    }

    vfb->powered = RT_TRUE;

    vfb->overlay_count = rt_min_t(rt_size_t, vfb->soc->overlay_count, VOP1FB_OVERLAY_MAX);

    if ((err = vop1fb_primary_plane_create(vfb, vfb->display_w, vfb->display_h)))
    {
        LOG_E("%s: create primary plane failed: %s", rt_dm_dev_get_name(dev), rt_strerror(err));
        goto _fail_clk;
    }

    rt_graphic_device_simple_edid(&vfb->parent, vfb->display_w, vfb->display_h, 60);

    if ((err = rt_graphic_device_register(&vfb->parent)))
    {
        rt_graphic_device_del_plane(&vfb->parent, vfb->primary);
        rt_graphic_device_free_plane(vfb->primary);
        vfb->primary = RT_NULL;
        goto _fail_clk;
    }

    if ((err = vop1fb_overlay_planes_create(vfb, vfb->display_w, vfb->display_h)))
    {
        LOG_E("%s: create overlay planes failed: %s", rt_dm_dev_get_name(dev), rt_strerror(err));
        goto _fail_graphic;
    }

    if ((err = vop1fb_cursor_plane_create(vfb, vfb->display_w, vfb->display_h)))
    {
        LOG_E("%s: create cursor plane failed: %s", rt_dm_dev_get_name(dev), rt_strerror(err));
        goto _fail_graphic;
    }

    vop1fb_dpms_switch(&vfb->parent, RT_GRAPHIC_DPMS_ON);

    pdev->parent.user_data = vfb;

    LOG_I("VOP1 %s (%s): %ux%u, primary=%s, %u overlays, cursor=%s",
            vfb->soc->version, pdev->id->compatible,
            vfb->display_w, vfb->display_h,
            vfb->soc->primary->name, (unsigned)vfb->overlay_count,
            vfb->soc->cursor->name);

    return RT_EOK;

_fail_graphic:
    rt_graphic_device_unregister(&vfb->parent);
_fail_clk:
    vop1fb_clk_disable(vfb);
_fail_irq:
    if (vfb->irq >= 0)
    {
        rt_hw_interrupt_mask(vfb->irq);
        rt_sem_detach(&vfb->vsync_sem);
    }

    if (!rt_is_err_or_null(vfb->clks))
    {
        rt_clk_array_put(vfb->clks);
    }

_fail_unmap:
    if (vfb->regs)
    {
        rt_iounmap(vfb->regs);
    }
_fail:
    rt_free(vfb);

    return err;
}

static rt_err_t vop1fb_remove(struct rt_platform_device *pdev)
{
    struct vop1fb *vfb = pdev->parent.user_data;

    if (!vfb)
    {
        return RT_EOK;
    }

    rt_graphic_device_unregister(&vfb->parent);

    vop1fb_dpms_switch(&vfb->parent, RT_GRAPHIC_DPMS_OFF);

    if (vfb->irq >= 0)
    {
        rt_hw_interrupt_mask(vfb->irq);
        rt_sem_detach(&vfb->vsync_sem);
    }

    if (!rt_is_err_or_null(vfb->clks))
    {
        rt_clk_array_put(vfb->clks);
    }

    rt_iounmap(vfb->regs);
    rt_free(vfb);

    pdev->parent.user_data = RT_NULL;

    return RT_EOK;
}

static const struct rt_ofw_node_id vop1_ofw_ids[] =
{
    /* Lite */
    { .compatible = "rockchip,rk3308-vop" },
    { .compatible = "rockchip,rk3506-vop" },
    { .compatible = "rockchip,px30-vop-lit" },
    /* Big */
    { .compatible = "rockchip,rk3328-vop" },
    { .compatible = "rockchip,rk3288-vop-big" },
    { .compatible = "rockchip,rk3288-vop-lit" },
    { .compatible = "rockchip,rk3399-vop-big" },
    { .compatible = "rockchip,rk3399-vop-lit" },
    { .compatible = "rockchip,rk3368-vop" },
    { .compatible = "rockchip,rk3366-vop" },
    { .compatible = "rockchip,rk3228-vop" },
    { .compatible = "rockchip,rv1126-vop" },
    { .compatible = "rockchip,rv1126b-vop" },
    { .compatible = "rockchip,rv1106-vop" },
    { /* sentinel */ }
};

static struct rt_platform_driver vop1_driver =
{
    .name = "vop1-framebuffer",
    .ids = vop1_ofw_ids,

    .probe = vop1fb_probe,
    .remove = vop1fb_remove,
};
RT_PLATFORM_DRIVER_EXPORT(vop1_driver);