1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) Rockchip Electronics Co., Ltd.
4 * Author:Mark Yao <mark.yao@rock-chips.com>
5 */
6
7 #include <linux/kernel.h>
8
9 #include <drm/drm.h>
10 #include <drm/drm_atomic.h>
11 #include <drm/drm_damage_helper.h>
12 #include <drm/drm_fourcc.h>
13 #include <drm/drm_framebuffer.h>
14 #include <drm/drm_gem_framebuffer_helper.h>
15 #include <drm/drm_probe_helper.h>
16
17 #include "rockchip_drm_drv.h"
18 #include "rockchip_drm_fb.h"
19 #include "rockchip_drm_gem.h"
20
21 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
22 .destroy = drm_gem_fb_destroy,
23 .create_handle = drm_gem_fb_create_handle,
24 .dirty = drm_atomic_helper_dirtyfb,
25 };
26
27 static const struct drm_mode_config_helper_funcs rockchip_mode_config_helpers = {
28 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
29 };
30
31 static struct drm_framebuffer *
rockchip_fb_create(struct drm_device * dev,struct drm_file * file,const struct drm_format_info * info,const struct drm_mode_fb_cmd2 * mode_cmd)32 rockchip_fb_create(struct drm_device *dev, struct drm_file *file,
33 const struct drm_format_info *info,
34 const struct drm_mode_fb_cmd2 *mode_cmd)
35 {
36 struct drm_afbc_framebuffer *afbc_fb;
37 int ret;
38
39 afbc_fb = kzalloc(sizeof(*afbc_fb), GFP_KERNEL);
40 if (!afbc_fb)
41 return ERR_PTR(-ENOMEM);
42
43 ret = drm_gem_fb_init_with_funcs(dev, &afbc_fb->base,
44 file, info, mode_cmd,
45 &rockchip_drm_fb_funcs);
46 if (ret) {
47 kfree(afbc_fb);
48 return ERR_PTR(ret);
49 }
50
51 if (drm_is_afbc(mode_cmd->modifier[0])) {
52 ret = drm_gem_fb_afbc_init(dev, info, mode_cmd, afbc_fb);
53 if (ret) {
54 drm_framebuffer_put(&afbc_fb->base);
55 return ERR_PTR(ret);
56 }
57 }
58
59 return &afbc_fb->base;
60 }
61
62 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
63 .fb_create = rockchip_fb_create,
64 .atomic_check = drm_atomic_helper_check,
65 .atomic_commit = drm_atomic_helper_commit,
66 };
67
rockchip_drm_mode_config_init(struct drm_device * dev)68 void rockchip_drm_mode_config_init(struct drm_device *dev)
69 {
70 dev->mode_config.min_width = 0;
71 dev->mode_config.min_height = 0;
72
73 /*
74 * set max width and height as default value(4096x4096).
75 * this value would be used to check framebuffer size limitation
76 * at drm_mode_addfb().
77 */
78 dev->mode_config.max_width = 4096;
79 dev->mode_config.max_height = 4096;
80
81 dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
82 dev->mode_config.helper_private = &rockchip_mode_config_helpers;
83
84 dev->mode_config.normalize_zpos = true;
85 }
86