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