xref: /linux/drivers/gpu/drm/armada/armada_fb.c (revision 22c55fb9eb92395d999b8404d73e58540d11bdd8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Russell King
4  */
5 
6 #include <drm/drm_modeset_helper.h>
7 #include <drm/drm_fourcc.h>
8 #include <drm/drm_gem_framebuffer_helper.h>
9 
10 #include "armada_drm.h"
11 #include "armada_fb.h"
12 #include "armada_gem.h"
13 #include "armada_hw.h"
14 
15 static const struct drm_framebuffer_funcs armada_fb_funcs = {
16 	.destroy	= drm_gem_fb_destroy,
17 	.create_handle	= drm_gem_fb_create_handle,
18 };
19 
20 struct armada_framebuffer *armada_framebuffer_create(struct drm_device *dev,
21 						     const struct drm_format_info *info,
22 						     const struct drm_mode_fb_cmd2 *mode,
23 						     struct armada_gem_object *obj)
24 {
25 	struct armada_framebuffer *dfb;
26 	uint8_t format, config;
27 	int ret;
28 
29 	switch (mode->pixel_format) {
30 #define FMT(drm, fmt, mod)		\
31 	case DRM_FORMAT_##drm:		\
32 		format = CFG_##fmt;	\
33 		config = mod;		\
34 		break
35 	FMT(RGB565,	565,		CFG_SWAPRB);
36 	FMT(BGR565,	565,		0);
37 	FMT(ARGB1555,	1555,		CFG_SWAPRB);
38 	FMT(ABGR1555,	1555,		0);
39 	FMT(RGB888,	888PACK,	CFG_SWAPRB);
40 	FMT(BGR888,	888PACK,	0);
41 	FMT(XRGB8888,	X888,		CFG_SWAPRB);
42 	FMT(XBGR8888,	X888,		0);
43 	FMT(ARGB8888,	8888,		CFG_SWAPRB);
44 	FMT(ABGR8888,	8888,		0);
45 	FMT(YUYV,	422PACK,	CFG_YUV2RGB | CFG_SWAPYU | CFG_SWAPUV);
46 	FMT(UYVY,	422PACK,	CFG_YUV2RGB);
47 	FMT(VYUY,	422PACK,	CFG_YUV2RGB | CFG_SWAPUV);
48 	FMT(YVYU,	422PACK,	CFG_YUV2RGB | CFG_SWAPYU);
49 	FMT(YUV422,	422,		CFG_YUV2RGB);
50 	FMT(YVU422,	422,		CFG_YUV2RGB | CFG_SWAPUV);
51 	FMT(YUV420,	420,		CFG_YUV2RGB);
52 	FMT(YVU420,	420,		CFG_YUV2RGB | CFG_SWAPUV);
53 	FMT(C8,		PSEUDO8,	0);
54 #undef FMT
55 	default:
56 		return ERR_PTR(-EINVAL);
57 	}
58 
59 	dfb = kzalloc(sizeof(*dfb), GFP_KERNEL);
60 	if (!dfb) {
61 		DRM_ERROR("failed to allocate Armada fb object\n");
62 		return ERR_PTR(-ENOMEM);
63 	}
64 
65 	dfb->fmt = format;
66 	dfb->mod = config;
67 	dfb->fb.obj[0] = &obj->obj;
68 
69 	drm_helper_mode_fill_fb_struct(dev, &dfb->fb, info, mode);
70 
71 	ret = drm_framebuffer_init(dev, &dfb->fb, &armada_fb_funcs);
72 	if (ret) {
73 		kfree(dfb);
74 		return ERR_PTR(ret);
75 	}
76 
77 	/*
78 	 * Take a reference on our object as we're successful - the
79 	 * caller already holds a reference, which keeps us safe for
80 	 * the above call, but the caller will drop their reference
81 	 * to it.  Hence we need to take our own reference.
82 	 */
83 	drm_gem_object_get(&obj->obj);
84 
85 	return dfb;
86 }
87 
88 struct drm_framebuffer *armada_fb_create(struct drm_device *dev,
89 	struct drm_file *dfile, const struct drm_format_info *info,
90 	const struct drm_mode_fb_cmd2 *mode)
91 {
92 	struct armada_gem_object *obj;
93 	struct armada_framebuffer *dfb;
94 	int ret;
95 
96 	DRM_DEBUG_DRIVER("w%u h%u pf%08x f%u p%u,%u,%u\n",
97 		mode->width, mode->height, mode->pixel_format,
98 		mode->flags, mode->pitches[0], mode->pitches[1],
99 		mode->pitches[2]);
100 
101 	/* We can only handle a single plane at the moment */
102 	if (info->num_planes > 1 &&
103 	    (mode->handles[0] != mode->handles[1] ||
104 	     mode->handles[0] != mode->handles[2])) {
105 		ret = -EINVAL;
106 		goto err;
107 	}
108 
109 	obj = armada_gem_object_lookup(dfile, mode->handles[0]);
110 	if (!obj) {
111 		ret = -ENOENT;
112 		goto err;
113 	}
114 
115 	if (obj->obj.import_attach && !obj->sgt) {
116 		ret = armada_gem_map_import(obj);
117 		if (ret)
118 			goto err_unref;
119 	}
120 
121 	/* Framebuffer objects must have a valid device address for scanout */
122 	if (!obj->mapped) {
123 		ret = -EINVAL;
124 		goto err_unref;
125 	}
126 
127 	dfb = armada_framebuffer_create(dev, info, mode, obj);
128 	if (IS_ERR(dfb)) {
129 		ret = PTR_ERR(dfb);
130 		goto err;
131 	}
132 
133 	drm_gem_object_put(&obj->obj);
134 
135 	return &dfb->fb;
136 
137  err_unref:
138 	drm_gem_object_put(&obj->obj);
139  err:
140 	DRM_ERROR("failed to initialize framebuffer: %d\n", ret);
141 	return ERR_PTR(ret);
142 }
143