Commit | Line | Data |
---|---|---|
6d62ef3a LP |
1 | /* |
2 | * rcar_du_vsp.h -- R-Car Display Unit VSP-Based Compositor | |
3 | * | |
4 | * Copyright (C) 2015 Renesas Electronics Corporation | |
5 | * | |
6 | * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | */ | |
13 | ||
14 | #include <drm/drmP.h> | |
15 | #include <drm/drm_atomic_helper.h> | |
16 | #include <drm/drm_crtc.h> | |
17 | #include <drm/drm_crtc_helper.h> | |
18 | #include <drm/drm_fb_cma_helper.h> | |
19 | #include <drm/drm_gem_cma_helper.h> | |
20 | #include <drm/drm_plane_helper.h> | |
21 | ||
22 | #include <linux/of_platform.h> | |
23 | #include <linux/videodev2.h> | |
24 | ||
25 | #include <media/vsp1.h> | |
26 | ||
27 | #include "rcar_du_drv.h" | |
28 | #include "rcar_du_kms.h" | |
29 | #include "rcar_du_vsp.h" | |
30 | ||
31 | void rcar_du_vsp_enable(struct rcar_du_crtc *crtc) | |
32 | { | |
33 | const struct drm_display_mode *mode = &crtc->crtc.state->adjusted_mode; | |
2427b303 | 34 | struct rcar_du_device *rcdu = crtc->group->dev; |
6d62ef3a LP |
35 | struct rcar_du_plane_state state = { |
36 | .state = { | |
37 | .crtc = &crtc->crtc, | |
38 | .crtc_x = 0, | |
39 | .crtc_y = 0, | |
40 | .crtc_w = mode->hdisplay, | |
41 | .crtc_h = mode->vdisplay, | |
42 | .src_x = 0, | |
43 | .src_y = 0, | |
44 | .src_w = mode->hdisplay << 16, | |
45 | .src_h = mode->vdisplay << 16, | |
46 | }, | |
47 | .format = rcar_du_format_info(DRM_FORMAT_ARGB8888), | |
6d62ef3a LP |
48 | .source = RCAR_DU_PLANE_VSPD1, |
49 | .alpha = 255, | |
50 | .colorkey = 0, | |
51 | .zpos = 0, | |
52 | }; | |
53 | ||
2427b303 LP |
54 | if (rcdu->info->gen >= 3) |
55 | state.hwindex = (crtc->index % 2) ? 2 : 0; | |
56 | else | |
57 | state.hwindex = crtc->index % 2; | |
58 | ||
6d62ef3a LP |
59 | __rcar_du_plane_setup(crtc->group, &state); |
60 | ||
61 | /* Ensure that the plane source configuration takes effect by requesting | |
62 | * a restart of the group. See rcar_du_plane_atomic_update() for a more | |
63 | * detailed explanation. | |
64 | * | |
65 | * TODO: Check whether this is still needed on Gen3. | |
66 | */ | |
67 | crtc->group->need_restart = true; | |
68 | ||
69 | vsp1_du_setup_lif(crtc->vsp->vsp, mode->hdisplay, mode->vdisplay); | |
70 | } | |
71 | ||
72 | void rcar_du_vsp_disable(struct rcar_du_crtc *crtc) | |
73 | { | |
74 | vsp1_du_setup_lif(crtc->vsp->vsp, 0, 0); | |
75 | } | |
76 | ||
77 | void rcar_du_vsp_atomic_begin(struct rcar_du_crtc *crtc) | |
78 | { | |
79 | vsp1_du_atomic_begin(crtc->vsp->vsp); | |
80 | } | |
81 | ||
82 | void rcar_du_vsp_atomic_flush(struct rcar_du_crtc *crtc) | |
83 | { | |
84 | vsp1_du_atomic_flush(crtc->vsp->vsp); | |
85 | } | |
86 | ||
87 | /* Keep the two tables in sync. */ | |
88 | static const u32 formats_kms[] = { | |
89 | DRM_FORMAT_RGB332, | |
90 | DRM_FORMAT_ARGB4444, | |
91 | DRM_FORMAT_XRGB4444, | |
92 | DRM_FORMAT_ARGB1555, | |
93 | DRM_FORMAT_XRGB1555, | |
94 | DRM_FORMAT_RGB565, | |
95 | DRM_FORMAT_BGR888, | |
96 | DRM_FORMAT_RGB888, | |
97 | DRM_FORMAT_BGRA8888, | |
98 | DRM_FORMAT_BGRX8888, | |
99 | DRM_FORMAT_ARGB8888, | |
100 | DRM_FORMAT_XRGB8888, | |
101 | DRM_FORMAT_UYVY, | |
102 | DRM_FORMAT_VYUY, | |
103 | DRM_FORMAT_YUYV, | |
104 | DRM_FORMAT_YVYU, | |
105 | DRM_FORMAT_NV12, | |
106 | DRM_FORMAT_NV21, | |
107 | DRM_FORMAT_NV16, | |
108 | DRM_FORMAT_NV61, | |
109 | }; | |
110 | ||
111 | static const u32 formats_v4l2[] = { | |
112 | V4L2_PIX_FMT_RGB332, | |
113 | V4L2_PIX_FMT_ARGB444, | |
114 | V4L2_PIX_FMT_XRGB444, | |
115 | V4L2_PIX_FMT_ARGB555, | |
116 | V4L2_PIX_FMT_XRGB555, | |
117 | V4L2_PIX_FMT_RGB565, | |
118 | V4L2_PIX_FMT_RGB24, | |
119 | V4L2_PIX_FMT_BGR24, | |
120 | V4L2_PIX_FMT_ARGB32, | |
121 | V4L2_PIX_FMT_XRGB32, | |
122 | V4L2_PIX_FMT_ABGR32, | |
123 | V4L2_PIX_FMT_XBGR32, | |
124 | V4L2_PIX_FMT_UYVY, | |
125 | V4L2_PIX_FMT_VYUY, | |
126 | V4L2_PIX_FMT_YUYV, | |
127 | V4L2_PIX_FMT_YVYU, | |
128 | V4L2_PIX_FMT_NV12M, | |
129 | V4L2_PIX_FMT_NV21M, | |
130 | V4L2_PIX_FMT_NV16M, | |
131 | V4L2_PIX_FMT_NV61M, | |
132 | }; | |
133 | ||
134 | static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane *plane) | |
135 | { | |
136 | struct rcar_du_vsp_plane_state *state = | |
137 | to_rcar_vsp_plane_state(plane->plane.state); | |
138 | struct drm_framebuffer *fb = plane->plane.state->fb; | |
139 | struct drm_gem_cma_object *gem; | |
140 | struct v4l2_rect src; | |
141 | struct v4l2_rect dst; | |
142 | dma_addr_t paddr[2] = { 0, }; | |
143 | u32 pixelformat = 0; | |
144 | unsigned int i; | |
145 | ||
146 | src.left = state->state.src_x >> 16; | |
147 | src.top = state->state.src_y >> 16; | |
148 | src.width = state->state.src_w >> 16; | |
149 | src.height = state->state.src_h >> 16; | |
150 | ||
151 | dst.left = state->state.crtc_x; | |
152 | dst.top = state->state.crtc_y; | |
153 | dst.width = state->state.crtc_w; | |
154 | dst.height = state->state.crtc_h; | |
155 | ||
156 | gem = drm_fb_cma_get_gem_obj(fb, 0); | |
157 | paddr[0] = gem->paddr + fb->offsets[0]; | |
158 | ||
159 | if (state->format->planes == 2) { | |
160 | gem = drm_fb_cma_get_gem_obj(fb, 1); | |
161 | paddr[1] = gem->paddr + fb->offsets[1]; | |
162 | } | |
163 | ||
164 | for (i = 0; i < ARRAY_SIZE(formats_kms); ++i) { | |
165 | if (formats_kms[i] == state->format->fourcc) { | |
166 | pixelformat = formats_v4l2[i]; | |
167 | break; | |
168 | } | |
169 | } | |
170 | ||
171 | WARN_ON(!pixelformat); | |
172 | ||
173 | vsp1_du_atomic_update(plane->vsp->vsp, plane->index, pixelformat, | |
174 | fb->pitches[0], paddr, &src, &dst); | |
175 | } | |
176 | ||
177 | static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane, | |
178 | struct drm_plane_state *state) | |
179 | { | |
180 | struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state); | |
181 | struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane); | |
182 | struct rcar_du_device *rcdu = rplane->vsp->dev; | |
183 | ||
184 | if (!state->fb || !state->crtc) { | |
185 | rstate->format = NULL; | |
186 | return 0; | |
187 | } | |
188 | ||
189 | if (state->src_w >> 16 != state->crtc_w || | |
190 | state->src_h >> 16 != state->crtc_h) { | |
191 | dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__); | |
192 | return -EINVAL; | |
193 | } | |
194 | ||
195 | rstate->format = rcar_du_format_info(state->fb->pixel_format); | |
196 | if (rstate->format == NULL) { | |
197 | dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__, | |
198 | state->fb->pixel_format); | |
199 | return -EINVAL; | |
200 | } | |
201 | ||
202 | return 0; | |
203 | } | |
204 | ||
205 | static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane, | |
206 | struct drm_plane_state *old_state) | |
207 | { | |
208 | struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane); | |
209 | ||
210 | if (plane->state->crtc) | |
211 | rcar_du_vsp_plane_setup(rplane); | |
212 | else | |
213 | vsp1_du_atomic_update(rplane->vsp->vsp, rplane->index, 0, 0, 0, | |
214 | NULL, NULL); | |
215 | } | |
216 | ||
217 | static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = { | |
218 | .atomic_check = rcar_du_vsp_plane_atomic_check, | |
219 | .atomic_update = rcar_du_vsp_plane_atomic_update, | |
220 | }; | |
221 | ||
222 | static struct drm_plane_state * | |
223 | rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane *plane) | |
224 | { | |
225 | struct rcar_du_vsp_plane_state *state; | |
226 | struct rcar_du_vsp_plane_state *copy; | |
227 | ||
228 | if (WARN_ON(!plane->state)) | |
229 | return NULL; | |
230 | ||
231 | state = to_rcar_vsp_plane_state(plane->state); | |
232 | copy = kmemdup(state, sizeof(*state), GFP_KERNEL); | |
233 | if (copy == NULL) | |
234 | return NULL; | |
235 | ||
236 | __drm_atomic_helper_plane_duplicate_state(plane, ©->state); | |
237 | ||
238 | return ©->state; | |
239 | } | |
240 | ||
241 | static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane *plane, | |
242 | struct drm_plane_state *state) | |
243 | { | |
244 | __drm_atomic_helper_plane_destroy_state(plane, state); | |
245 | kfree(to_rcar_vsp_plane_state(state)); | |
246 | } | |
247 | ||
248 | static void rcar_du_vsp_plane_reset(struct drm_plane *plane) | |
249 | { | |
250 | struct rcar_du_vsp_plane_state *state; | |
251 | ||
252 | if (plane->state) { | |
253 | rcar_du_vsp_plane_atomic_destroy_state(plane, plane->state); | |
254 | plane->state = NULL; | |
255 | } | |
256 | ||
257 | state = kzalloc(sizeof(*state), GFP_KERNEL); | |
258 | if (state == NULL) | |
259 | return; | |
260 | ||
261 | state->alpha = 255; | |
262 | ||
263 | plane->state = &state->state; | |
264 | plane->state->plane = plane; | |
265 | } | |
266 | ||
267 | static int rcar_du_vsp_plane_atomic_set_property(struct drm_plane *plane, | |
268 | struct drm_plane_state *state, struct drm_property *property, | |
269 | uint64_t val) | |
270 | { | |
271 | struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state); | |
272 | struct rcar_du_device *rcdu = to_rcar_vsp_plane(plane)->vsp->dev; | |
273 | ||
274 | if (property == rcdu->props.alpha) | |
275 | rstate->alpha = val; | |
276 | else | |
277 | return -EINVAL; | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
282 | static int rcar_du_vsp_plane_atomic_get_property(struct drm_plane *plane, | |
283 | const struct drm_plane_state *state, struct drm_property *property, | |
284 | uint64_t *val) | |
285 | { | |
286 | const struct rcar_du_vsp_plane_state *rstate = | |
287 | container_of(state, const struct rcar_du_vsp_plane_state, state); | |
288 | struct rcar_du_device *rcdu = to_rcar_vsp_plane(plane)->vsp->dev; | |
289 | ||
290 | if (property == rcdu->props.alpha) | |
291 | *val = rstate->alpha; | |
292 | else | |
293 | return -EINVAL; | |
294 | ||
295 | return 0; | |
296 | } | |
297 | ||
298 | static const struct drm_plane_funcs rcar_du_vsp_plane_funcs = { | |
299 | .update_plane = drm_atomic_helper_update_plane, | |
300 | .disable_plane = drm_atomic_helper_disable_plane, | |
301 | .reset = rcar_du_vsp_plane_reset, | |
302 | .set_property = drm_atomic_helper_plane_set_property, | |
303 | .destroy = drm_plane_cleanup, | |
304 | .atomic_duplicate_state = rcar_du_vsp_plane_atomic_duplicate_state, | |
305 | .atomic_destroy_state = rcar_du_vsp_plane_atomic_destroy_state, | |
306 | .atomic_set_property = rcar_du_vsp_plane_atomic_set_property, | |
307 | .atomic_get_property = rcar_du_vsp_plane_atomic_get_property, | |
308 | }; | |
309 | ||
310 | int rcar_du_vsp_init(struct rcar_du_vsp *vsp) | |
311 | { | |
312 | struct rcar_du_device *rcdu = vsp->dev; | |
313 | struct platform_device *pdev; | |
314 | struct device_node *np; | |
315 | unsigned int i; | |
316 | int ret; | |
317 | ||
318 | /* Find the VSP device and initialize it. */ | |
319 | np = of_parse_phandle(rcdu->dev->of_node, "vsps", vsp->index); | |
320 | if (!np) { | |
321 | dev_err(rcdu->dev, "vsps node not found\n"); | |
322 | return -ENXIO; | |
323 | } | |
324 | ||
325 | pdev = of_find_device_by_node(np); | |
326 | of_node_put(np); | |
327 | if (!pdev) | |
328 | return -ENXIO; | |
329 | ||
330 | vsp->vsp = &pdev->dev; | |
331 | ||
332 | ret = vsp1_du_init(vsp->vsp); | |
333 | if (ret < 0) | |
334 | return ret; | |
335 | ||
336 | /* The VSP2D (Gen3) has 5 RPFs, but the VSP1D (Gen2) is limited to | |
2427b303 | 337 | * 4 RPFs. |
6d62ef3a | 338 | */ |
2427b303 | 339 | vsp->num_planes = rcdu->info->gen >= 3 ? 5 : 4; |
6d62ef3a LP |
340 | |
341 | vsp->planes = devm_kcalloc(rcdu->dev, vsp->num_planes, | |
342 | sizeof(*vsp->planes), GFP_KERNEL); | |
343 | if (!vsp->planes) | |
344 | return -ENOMEM; | |
345 | ||
346 | for (i = 0; i < vsp->num_planes; ++i) { | |
347 | enum drm_plane_type type = i ? DRM_PLANE_TYPE_OVERLAY | |
348 | : DRM_PLANE_TYPE_PRIMARY; | |
349 | struct rcar_du_vsp_plane *plane = &vsp->planes[i]; | |
350 | ||
351 | plane->vsp = vsp; | |
352 | plane->index = i; | |
353 | ||
354 | ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, | |
355 | 1 << vsp->index, | |
356 | &rcar_du_vsp_plane_funcs, | |
357 | formats_kms, | |
358 | ARRAY_SIZE(formats_kms), type, | |
359 | NULL); | |
360 | if (ret < 0) | |
361 | return ret; | |
362 | ||
363 | drm_plane_helper_add(&plane->plane, | |
364 | &rcar_du_vsp_plane_helper_funcs); | |
365 | ||
366 | if (type == DRM_PLANE_TYPE_PRIMARY) | |
367 | continue; | |
368 | ||
369 | drm_object_attach_property(&plane->plane.base, | |
370 | rcdu->props.alpha, 255); | |
371 | } | |
372 | ||
373 | return 0; | |
374 | } |