1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Russell King
6 #include <drm/drm_modeset_helper.h>
7 #include <drm/drm_fourcc.h>
8 #include <drm/drm_gem_framebuffer_helper.h>
10 #include "armada_drm.h"
11 #include "armada_fb.h"
12 #include "armada_gem.h"
13 #include "armada_hw.h"
15 static const struct drm_framebuffer_funcs armada_fb_funcs = {
16 .destroy = drm_gem_fb_destroy,
17 .create_handle = drm_gem_fb_create_handle,
20 struct armada_framebuffer *armada_framebuffer_create(struct drm_device *dev,
21 const struct drm_mode_fb_cmd2 *mode, struct armada_gem_object *obj)
23 struct armada_framebuffer *dfb;
24 uint8_t format, config;
27 switch (mode->pixel_format) {
28 #define FMT(drm, fmt, mod) \
29 case DRM_FORMAT_##drm: \
33 FMT(RGB565, 565, CFG_SWAPRB);
35 FMT(ARGB1555, 1555, CFG_SWAPRB);
36 FMT(ABGR1555, 1555, 0);
37 FMT(RGB888, 888PACK, CFG_SWAPRB);
38 FMT(BGR888, 888PACK, 0);
39 FMT(XRGB8888, X888, CFG_SWAPRB);
40 FMT(XBGR8888, X888, 0);
41 FMT(ARGB8888, 8888, CFG_SWAPRB);
42 FMT(ABGR8888, 8888, 0);
43 FMT(YUYV, 422PACK, CFG_YUV2RGB | CFG_SWAPYU | CFG_SWAPUV);
44 FMT(UYVY, 422PACK, CFG_YUV2RGB);
45 FMT(VYUY, 422PACK, CFG_YUV2RGB | CFG_SWAPUV);
46 FMT(YVYU, 422PACK, CFG_YUV2RGB | CFG_SWAPYU);
47 FMT(YUV422, 422, CFG_YUV2RGB);
48 FMT(YVU422, 422, CFG_YUV2RGB | CFG_SWAPUV);
49 FMT(YUV420, 420, CFG_YUV2RGB);
50 FMT(YVU420, 420, CFG_YUV2RGB | CFG_SWAPUV);
54 return ERR_PTR(-EINVAL);
57 dfb = kzalloc(sizeof(*dfb), GFP_KERNEL);
59 DRM_ERROR("failed to allocate Armada fb object\n");
60 return ERR_PTR(-ENOMEM);
65 dfb->fb.obj[0] = &obj->obj;
67 drm_helper_mode_fill_fb_struct(dev, &dfb->fb, mode);
69 ret = drm_framebuffer_init(dev, &dfb->fb, &armada_fb_funcs);
76 * Take a reference on our object as we're successful - the
77 * caller already holds a reference, which keeps us safe for
78 * the above call, but the caller will drop their reference
79 * to it. Hence we need to take our own reference.
81 drm_gem_object_get(&obj->obj);
86 struct drm_framebuffer *armada_fb_create(struct drm_device *dev,
87 struct drm_file *dfile, const struct drm_mode_fb_cmd2 *mode)
89 const struct drm_format_info *info = drm_get_format_info(dev, mode);
90 struct armada_gem_object *obj;
91 struct armada_framebuffer *dfb;
94 DRM_DEBUG_DRIVER("w%u h%u pf%08x f%u p%u,%u,%u\n",
95 mode->width, mode->height, mode->pixel_format,
96 mode->flags, mode->pitches[0], mode->pitches[1],
99 /* We can only handle a single plane at the moment */
100 if (info->num_planes > 1 &&
101 (mode->handles[0] != mode->handles[1] ||
102 mode->handles[0] != mode->handles[2])) {
107 obj = armada_gem_object_lookup(dfile, mode->handles[0]);
113 if (obj->obj.import_attach && !obj->sgt) {
114 ret = armada_gem_map_import(obj);
119 /* Framebuffer objects must have a valid device address for scanout */
125 dfb = armada_framebuffer_create(dev, mode, obj);
131 drm_gem_object_put(&obj->obj);
136 drm_gem_object_put(&obj->obj);
138 DRM_ERROR("failed to initialize framebuffer: %d\n", ret);