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