1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4 * Author:Mark Yao <mark.yao@rock-chips.com>
5 */
6
7 #include <drm/drm.h>
8 #include <drm/drm_fb_helper.h>
9 #include <drm/drm_fourcc.h>
10 #include <drm/drm_probe_helper.h>
11
12 #include "rockchip_drm_drv.h"
13 #include "rockchip_drm_gem.h"
14 #include "rockchip_drm_fb.h"
15 #include "rockchip_drm_fbdev.h"
16
17 #define PREFERRED_BPP 32
18
rockchip_fbdev_mmap(struct fb_info * info,struct vm_area_struct * vma)19 static int rockchip_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
20 {
21 struct drm_fb_helper *helper = info->par;
22 struct rockchip_drm_private *private = helper->dev->dev_private;
23
24 return rockchip_gem_mmap_buf(private->fbdev_bo, vma);
25 }
26
27 static const struct fb_ops rockchip_drm_fbdev_ops = {
28 .owner = THIS_MODULE,
29 DRM_FB_HELPER_DEFAULT_OPS,
30 .fb_mmap = rockchip_fbdev_mmap,
31 .fb_fillrect = drm_fb_helper_cfb_fillrect,
32 .fb_copyarea = drm_fb_helper_cfb_copyarea,
33 .fb_imageblit = drm_fb_helper_cfb_imageblit,
34 };
35
rockchip_drm_fbdev_create(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)36 static int rockchip_drm_fbdev_create(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
37 {
38 struct rockchip_drm_private *private = helper->dev->dev_private;
39 struct drm_mode_fb_cmd2 mode_cmd = {0};
40 struct drm_device *dev = helper->dev;
41 struct rockchip_gem_object *rk_obj;
42 struct drm_framebuffer *fb;
43 unsigned int bytes_per_pixel;
44 unsigned long offset;
45 struct fb_info *fbi;
46 size_t size;
47 int ret;
48
49 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 0x8);
50
51 mode_cmd.width = sizes->surface_width;
52 mode_cmd.height = sizes->surface_height;
53 mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
54 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
55
56 size = mode_cmd.pitches[0] * mode_cmd.height;
57
58 rk_obj = rockchip_gem_create_object(dev, size, true, 0);
59 if (IS_ERR(rk_obj)) {
60 return -ENOMEM;
61 }
62
63 private->fbdev_bo = &rk_obj->base;
64
65 fbi = drm_fb_helper_alloc_fbi(helper);
66 if (IS_ERR(fbi)) {
67 DRM_DEV_ERROR(dev->dev, "Failed to create framebuffer info.\n");
68 ret = PTR_ERR(fbi);
69 goto out;
70 }
71
72 helper->fb = rockchip_drm_framebuffer_init(dev, &mode_cmd, private->fbdev_bo);
73 if (IS_ERR(helper->fb)) {
74 DRM_DEV_ERROR(dev->dev, "Failed to allocate DRM framebuffer.\n");
75 ret = PTR_ERR(helper->fb);
76 goto out;
77 }
78
79 fbi->fbops = &rockchip_drm_fbdev_ops;
80
81 fb = helper->fb;
82 drm_fb_helper_fill_info(fbi, helper, sizes);
83
84 offset = fbi->var.xoffset * bytes_per_pixel;
85 offset += fbi->var.yoffset * fb->pitches[0];
86
87 dev->mode_config.fb_base = 0;
88 fbi->screen_base = rk_obj->kvaddr + offset;
89 fbi->screen_size = rk_obj->base.size;
90 fbi->fix.smem_len = rk_obj->base.size;
91
92 DRM_DEBUG_KMS("FB [%dx%d]-%d kvaddr=%p offset=%ld size=%zu\n", fb->width, fb->height, fb->format->depth,
93 rk_obj->kvaddr, offset, size);
94
95 return 0;
96
97 out:
98 rockchip_gem_free_object(&rk_obj->base);
99 return ret;
100 }
101
102 static const struct drm_fb_helper_funcs rockchip_drm_fb_helper_funcs = {
103 .fb_probe = rockchip_drm_fbdev_create,
104 };
105
rockchip_drm_fbdev_init(struct drm_device * dev)106 int rockchip_drm_fbdev_init(struct drm_device *dev)
107 {
108 struct rockchip_drm_private *private = dev->dev_private;
109 struct drm_fb_helper *helper;
110 int ret;
111
112 if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector) {
113 return -EINVAL;
114 }
115
116 helper = devm_kzalloc(dev->dev, sizeof(*helper), GFP_KERNEL);
117 if (!helper) {
118 return -ENOMEM;
119 }
120 private->fbdev_helper = helper;
121
122 drm_fb_helper_prepare(dev, helper, &rockchip_drm_fb_helper_funcs);
123
124 ret = drm_fb_helper_init(dev, helper);
125 if (ret < 0) {
126 DRM_DEV_ERROR(dev->dev, "Failed to initialize drm fb helper - %d.\n", ret);
127 return ret;
128 }
129
130 ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
131 if (ret < 0) {
132 DRM_DEV_ERROR(dev->dev, "Failed to set initial hw config - %d.\n", ret);
133 goto err_drm_fb_helper_fini;
134 }
135
136 return 0;
137
138 err_drm_fb_helper_fini:
139 drm_fb_helper_fini(helper);
140 return ret;
141 }
142
rockchip_drm_fbdev_fini(struct drm_device * dev)143 void rockchip_drm_fbdev_fini(struct drm_device *dev)
144 {
145 struct rockchip_drm_private *private = dev->dev_private;
146 struct drm_fb_helper *helper = private->fbdev_helper;
147
148 if (!helper) {
149 return;
150 }
151
152 drm_fb_helper_unregister_fbi(helper);
153
154 if (helper->fb) {
155 drm_framebuffer_put(helper->fb);
156 }
157
158 drm_fb_helper_fini(helper);
159 }
160