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