1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Russell King
4 * Written from the i915 driver.
7 #include <linux/errno.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
11 #include <drm/drm_fb_helper.h>
12 #include <drm/drm_fourcc.h>
14 #include "armada_crtc.h"
15 #include "armada_drm.h"
16 #include "armada_fb.h"
17 #include "armada_gem.h"
19 static const struct fb_ops armada_fb_ops = {
21 DRM_FB_HELPER_DEFAULT_OPS,
22 .fb_read = drm_fb_helper_cfb_read,
23 .fb_write = drm_fb_helper_cfb_write,
24 .fb_fillrect = drm_fb_helper_cfb_fillrect,
25 .fb_copyarea = drm_fb_helper_cfb_copyarea,
26 .fb_imageblit = drm_fb_helper_cfb_imageblit,
29 static int armada_fbdev_create(struct drm_fb_helper *fbh,
30 struct drm_fb_helper_surface_size *sizes)
32 struct drm_device *dev = fbh->dev;
33 struct drm_mode_fb_cmd2 mode;
34 struct armada_framebuffer *dfb;
35 struct armada_gem_object *obj;
40 memset(&mode, 0, sizeof(mode));
41 mode.width = sizes->surface_width;
42 mode.height = sizes->surface_height;
43 mode.pitches[0] = armada_pitch(mode.width, sizes->surface_bpp);
44 mode.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
45 sizes->surface_depth);
47 size = mode.pitches[0] * mode.height;
48 obj = armada_gem_alloc_private_object(dev, size);
50 DRM_ERROR("failed to allocate fb memory\n");
54 ret = armada_gem_linear_back(dev, obj);
56 drm_gem_object_put(&obj->obj);
60 ptr = armada_gem_map_object(dev, obj);
62 drm_gem_object_put(&obj->obj);
66 dfb = armada_framebuffer_create(dev, &mode, obj);
69 * A reference is now held by the framebuffer object if
70 * successful, otherwise this drops the ref for the error path.
72 drm_gem_object_put(&obj->obj);
77 info = drm_fb_helper_alloc_info(fbh);
83 info->fbops = &armada_fb_ops;
84 info->fix.smem_start = obj->phys_addr;
85 info->fix.smem_len = obj->obj.size;
86 info->screen_size = obj->obj.size;
87 info->screen_base = ptr;
90 drm_fb_helper_fill_info(info, fbh, sizes);
92 DRM_DEBUG_KMS("allocated %dx%d %dbpp fb: 0x%08llx\n",
93 dfb->fb.width, dfb->fb.height, dfb->fb.format->cpp[0] * 8,
94 (unsigned long long)obj->phys_addr);
99 dfb->fb.funcs->destroy(&dfb->fb);
103 static int armada_fb_probe(struct drm_fb_helper *fbh,
104 struct drm_fb_helper_surface_size *sizes)
109 ret = armada_fbdev_create(fbh, sizes);
116 static const struct drm_fb_helper_funcs armada_fb_helper_funcs = {
117 .fb_probe = armada_fb_probe,
120 int armada_fbdev_init(struct drm_device *dev)
122 struct armada_private *priv = drm_to_armada_dev(dev);
123 struct drm_fb_helper *fbh;
126 fbh = devm_kzalloc(dev->dev, sizeof(*fbh), GFP_KERNEL);
132 drm_fb_helper_prepare(dev, fbh, 32, &armada_fb_helper_funcs);
134 ret = drm_fb_helper_init(dev, fbh);
136 DRM_ERROR("failed to initialize drm fb helper\n");
140 ret = drm_fb_helper_initial_config(fbh);
142 DRM_ERROR("failed to set initial config\n");
148 drm_fb_helper_fini(fbh);
150 drm_fb_helper_unprepare(fbh);
155 void armada_fbdev_fini(struct drm_device *dev)
157 struct armada_private *priv = drm_to_armada_dev(dev);
158 struct drm_fb_helper *fbh = priv->fbdev;
161 drm_fb_helper_unregister_info(fbh);
163 drm_fb_helper_fini(fbh);
166 fbh->fb->funcs->destroy(fbh->fb);
168 drm_fb_helper_unprepare(fbh);