Merge tag 'for-linus-5.2b-rc4-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / mediatek / mtk_drm_plane.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 MediaTek Inc.
4  * Author: CK Hu <ck.hu@mediatek.com>
5  */
6
7 #include <drm/drmP.h>
8 #include <drm/drm_atomic.h>
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_plane_helper.h>
11
12 #include "mtk_drm_crtc.h"
13 #include "mtk_drm_ddp_comp.h"
14 #include "mtk_drm_drv.h"
15 #include "mtk_drm_fb.h"
16 #include "mtk_drm_gem.h"
17 #include "mtk_drm_plane.h"
18
19 static const u32 formats[] = {
20         DRM_FORMAT_XRGB8888,
21         DRM_FORMAT_ARGB8888,
22         DRM_FORMAT_RGB565,
23         DRM_FORMAT_UYVY,
24         DRM_FORMAT_YUYV,
25 };
26
27 static void mtk_plane_reset(struct drm_plane *plane)
28 {
29         struct mtk_plane_state *state;
30
31         if (plane->state) {
32                 __drm_atomic_helper_plane_destroy_state(plane->state);
33
34                 state = to_mtk_plane_state(plane->state);
35                 memset(state, 0, sizeof(*state));
36         } else {
37                 state = kzalloc(sizeof(*state), GFP_KERNEL);
38                 if (!state)
39                         return;
40                 plane->state = &state->base;
41         }
42
43         state->base.plane = plane;
44         state->pending.format = DRM_FORMAT_RGB565;
45 }
46
47 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
48 {
49         struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
50         struct mtk_plane_state *state;
51
52         state = kzalloc(sizeof(*state), GFP_KERNEL);
53         if (!state)
54                 return NULL;
55
56         __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
57
58         WARN_ON(state->base.plane != plane);
59
60         state->pending = old_state->pending;
61
62         return &state->base;
63 }
64
65 static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
66                                         struct drm_plane_state *state)
67 {
68         __drm_atomic_helper_plane_destroy_state(state);
69         kfree(to_mtk_plane_state(state));
70 }
71
72 static const struct drm_plane_funcs mtk_plane_funcs = {
73         .update_plane = drm_atomic_helper_update_plane,
74         .disable_plane = drm_atomic_helper_disable_plane,
75         .destroy = drm_plane_cleanup,
76         .reset = mtk_plane_reset,
77         .atomic_duplicate_state = mtk_plane_duplicate_state,
78         .atomic_destroy_state = mtk_drm_plane_destroy_state,
79 };
80
81 static int mtk_plane_atomic_check(struct drm_plane *plane,
82                                   struct drm_plane_state *state)
83 {
84         struct drm_framebuffer *fb = state->fb;
85         struct drm_crtc_state *crtc_state;
86
87         if (!fb)
88                 return 0;
89
90         if (!state->crtc)
91                 return 0;
92
93         crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
94         if (IS_ERR(crtc_state))
95                 return PTR_ERR(crtc_state);
96
97         return drm_atomic_helper_check_plane_state(state, crtc_state,
98                                                    DRM_PLANE_HELPER_NO_SCALING,
99                                                    DRM_PLANE_HELPER_NO_SCALING,
100                                                    true, true);
101 }
102
103 static void mtk_plane_atomic_update(struct drm_plane *plane,
104                                     struct drm_plane_state *old_state)
105 {
106         struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
107         struct drm_crtc *crtc = plane->state->crtc;
108         struct drm_framebuffer *fb = plane->state->fb;
109         struct drm_gem_object *gem;
110         struct mtk_drm_gem_obj *mtk_gem;
111         unsigned int pitch, format;
112         dma_addr_t addr;
113
114         if (!crtc || WARN_ON(!fb))
115                 return;
116
117         gem = fb->obj[0];
118         mtk_gem = to_mtk_gem_obj(gem);
119         addr = mtk_gem->dma_addr;
120         pitch = fb->pitches[0];
121         format = fb->format->format;
122
123         addr += (plane->state->src.x1 >> 16) * fb->format->cpp[0];
124         addr += (plane->state->src.y1 >> 16) * pitch;
125
126         state->pending.enable = true;
127         state->pending.pitch = pitch;
128         state->pending.format = format;
129         state->pending.addr = addr;
130         state->pending.x = plane->state->dst.x1;
131         state->pending.y = plane->state->dst.y1;
132         state->pending.width = drm_rect_width(&plane->state->dst);
133         state->pending.height = drm_rect_height(&plane->state->dst);
134         wmb(); /* Make sure the above parameters are set before update */
135         state->pending.dirty = true;
136 }
137
138 static void mtk_plane_atomic_disable(struct drm_plane *plane,
139                                      struct drm_plane_state *old_state)
140 {
141         struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
142
143         state->pending.enable = false;
144         wmb(); /* Make sure the above parameter is set before update */
145         state->pending.dirty = true;
146 }
147
148 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
149         .atomic_check = mtk_plane_atomic_check,
150         .atomic_update = mtk_plane_atomic_update,
151         .atomic_disable = mtk_plane_atomic_disable,
152 };
153
154 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
155                    unsigned long possible_crtcs, enum drm_plane_type type)
156 {
157         int err;
158
159         err = drm_universal_plane_init(dev, plane, possible_crtcs,
160                                        &mtk_plane_funcs, formats,
161                                        ARRAY_SIZE(formats), NULL, type, NULL);
162         if (err) {
163                 DRM_ERROR("failed to initialize plane\n");
164                 return err;
165         }
166
167         drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
168
169         return 0;
170 }