1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Russell King
4 * Written from the i915 driver.
5 */
6
7 #include <linux/errno.h>
8 #include <linux/fb.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11
12 #include <drm/drm_crtc_helper.h>
13 #include <drm/drm_drv.h>
14 #include <drm/drm_fb_helper.h>
15 #include <drm/drm_fourcc.h>
16 #include <drm/drm_print.h>
17
18 #include "armada_crtc.h"
19 #include "armada_drm.h"
20 #include "armada_fb.h"
21 #include "armada_gem.h"
22
armada_fbdev_fb_destroy(struct fb_info * info)23 static void armada_fbdev_fb_destroy(struct fb_info *info)
24 {
25 struct drm_fb_helper *fbh = info->par;
26
27 drm_fb_helper_fini(fbh);
28
29 fbh->fb->funcs->destroy(fbh->fb);
30
31 drm_client_release(&fbh->client);
32 }
33
34 static const struct fb_ops armada_fb_ops = {
35 .owner = THIS_MODULE,
36 FB_DEFAULT_IOMEM_OPS,
37 DRM_FB_HELPER_DEFAULT_OPS,
38 .fb_destroy = armada_fbdev_fb_destroy,
39 };
40
41 static const struct drm_fb_helper_funcs armada_fbdev_helper_funcs;
42
armada_fbdev_driver_fbdev_probe(struct drm_fb_helper * fbh,struct drm_fb_helper_surface_size * sizes)43 int armada_fbdev_driver_fbdev_probe(struct drm_fb_helper *fbh,
44 struct drm_fb_helper_surface_size *sizes)
45 {
46 struct drm_device *dev = fbh->dev;
47 struct fb_info *info = fbh->info;
48 struct drm_mode_fb_cmd2 mode;
49 struct armada_framebuffer *dfb;
50 struct armada_gem_object *obj;
51 int size, ret;
52 void *ptr;
53
54 memset(&mode, 0, sizeof(mode));
55 mode.width = sizes->surface_width;
56 mode.height = sizes->surface_height;
57 mode.pitches[0] = armada_pitch(mode.width, sizes->surface_bpp);
58 mode.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
59 sizes->surface_depth);
60
61 size = mode.pitches[0] * mode.height;
62 obj = armada_gem_alloc_private_object(dev, size);
63 if (!obj) {
64 DRM_ERROR("failed to allocate fb memory\n");
65 return -ENOMEM;
66 }
67
68 ret = armada_gem_linear_back(dev, obj);
69 if (ret) {
70 drm_gem_object_put(&obj->obj);
71 return ret;
72 }
73
74 ptr = armada_gem_map_object(dev, obj);
75 if (!ptr) {
76 drm_gem_object_put(&obj->obj);
77 return -ENOMEM;
78 }
79
80 dfb = armada_framebuffer_create(dev,
81 drm_get_format_info(dev, mode.pixel_format,
82 mode.modifier[0]),
83 &mode, obj);
84
85 /*
86 * A reference is now held by the framebuffer object if
87 * successful, otherwise this drops the ref for the error path.
88 */
89 drm_gem_object_put(&obj->obj);
90
91 if (IS_ERR(dfb))
92 return PTR_ERR(dfb);
93
94 info->fbops = &armada_fb_ops;
95 info->fix.smem_start = obj->phys_addr;
96 info->fix.smem_len = obj->obj.size;
97 info->screen_size = obj->obj.size;
98 info->screen_base = ptr;
99 fbh->funcs = &armada_fbdev_helper_funcs;
100 fbh->fb = &dfb->fb;
101
102 drm_fb_helper_fill_info(info, fbh, sizes);
103
104 DRM_DEBUG_KMS("allocated %dx%d %dbpp fb: 0x%08llx\n",
105 dfb->fb.width, dfb->fb.height, dfb->fb.format->cpp[0] * 8,
106 (unsigned long long)obj->phys_addr);
107
108 return 0;
109 }
110