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_fillrect
= drm_fb_helper_cfb_fillrect
,
23 .fb_copyarea
= drm_fb_helper_cfb_copyarea
,
24 .fb_imageblit
= drm_fb_helper_cfb_imageblit
,
27 static int armada_fbdev_create(struct drm_fb_helper
*fbh
,
28 struct drm_fb_helper_surface_size
*sizes
)
30 struct drm_device
*dev
= fbh
->dev
;
31 struct drm_mode_fb_cmd2 mode
;
32 struct armada_framebuffer
*dfb
;
33 struct armada_gem_object
*obj
;
38 memset(&mode
, 0, sizeof(mode
));
39 mode
.width
= sizes
->surface_width
;
40 mode
.height
= sizes
->surface_height
;
41 mode
.pitches
[0] = armada_pitch(mode
.width
, sizes
->surface_bpp
);
42 mode
.pixel_format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
,
43 sizes
->surface_depth
);
45 size
= mode
.pitches
[0] * mode
.height
;
46 obj
= armada_gem_alloc_private_object(dev
, size
);
48 DRM_ERROR("failed to allocate fb memory\n");
52 ret
= armada_gem_linear_back(dev
, obj
);
54 drm_gem_object_put(&obj
->obj
);
58 ptr
= armada_gem_map_object(dev
, obj
);
60 drm_gem_object_put(&obj
->obj
);
64 dfb
= armada_framebuffer_create(dev
, &mode
, obj
);
67 * A reference is now held by the framebuffer object if
68 * successful, otherwise this drops the ref for the error path.
70 drm_gem_object_put(&obj
->obj
);
75 info
= drm_fb_helper_alloc_fbi(fbh
);
81 info
->fbops
= &armada_fb_ops
;
82 info
->fix
.smem_start
= obj
->phys_addr
;
83 info
->fix
.smem_len
= obj
->obj
.size
;
84 info
->screen_size
= obj
->obj
.size
;
85 info
->screen_base
= ptr
;
88 drm_fb_helper_fill_info(info
, fbh
, sizes
);
90 DRM_DEBUG_KMS("allocated %dx%d %dbpp fb: 0x%08llx\n",
91 dfb
->fb
.width
, dfb
->fb
.height
, dfb
->fb
.format
->cpp
[0] * 8,
92 (unsigned long long)obj
->phys_addr
);
97 dfb
->fb
.funcs
->destroy(&dfb
->fb
);
101 static int armada_fb_probe(struct drm_fb_helper
*fbh
,
102 struct drm_fb_helper_surface_size
*sizes
)
107 ret
= armada_fbdev_create(fbh
, sizes
);
114 static const struct drm_fb_helper_funcs armada_fb_helper_funcs
= {
115 .fb_probe
= armada_fb_probe
,
118 int armada_fbdev_init(struct drm_device
*dev
)
120 struct armada_private
*priv
= drm_to_armada_dev(dev
);
121 struct drm_fb_helper
*fbh
;
124 fbh
= devm_kzalloc(dev
->dev
, sizeof(*fbh
), GFP_KERNEL
);
130 drm_fb_helper_prepare(dev
, fbh
, &armada_fb_helper_funcs
);
132 ret
= drm_fb_helper_init(dev
, fbh
);
134 DRM_ERROR("failed to initialize drm fb helper\n");
138 ret
= drm_fb_helper_initial_config(fbh
, 32);
140 DRM_ERROR("failed to set initial config\n");
146 drm_fb_helper_fini(fbh
);
152 void armada_fbdev_fini(struct drm_device
*dev
)
154 struct armada_private
*priv
= drm_to_armada_dev(dev
);
155 struct drm_fb_helper
*fbh
= priv
->fbdev
;
158 drm_fb_helper_unregister_fbi(fbh
);
160 drm_fb_helper_fini(fbh
);
163 fbh
->fb
->funcs
->destroy(fbh
->fb
);