xref: /openbmc/linux/drivers/gpu/drm/rockchip/rockchip_drm_fb.c (revision 56ea353ea49ad21dd4c14e7baa235493ec27e766)
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 <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_fb_helper.h>
13 #include <drm/drm_fourcc.h>
14 #include <drm/drm_framebuffer.h>
15 #include <drm/drm_gem_framebuffer_helper.h>
16 #include <drm/drm_probe_helper.h>
17 
18 #include "rockchip_drm_drv.h"
19 #include "rockchip_drm_fb.h"
20 #include "rockchip_drm_gem.h"
21 
22 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
23 	.destroy       = drm_gem_fb_destroy,
24 	.create_handle = drm_gem_fb_create_handle,
25 	.dirty	       = drm_atomic_helper_dirtyfb,
26 };
27 
28 static const struct drm_mode_config_helper_funcs rockchip_mode_config_helpers = {
29 	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
30 };
31 
32 static struct drm_framebuffer *
33 rockchip_fb_create(struct drm_device *dev, struct drm_file *file,
34 		   const struct drm_mode_fb_cmd2 *mode_cmd)
35 {
36 	struct drm_afbc_framebuffer *afbc_fb;
37 	const struct drm_format_info *info;
38 	int ret;
39 
40 	info = drm_get_format_info(dev, mode_cmd);
41 	if (!info)
42 		return ERR_PTR(-ENOMEM);
43 
44 	afbc_fb = kzalloc(sizeof(*afbc_fb), GFP_KERNEL);
45 	if (!afbc_fb)
46 		return ERR_PTR(-ENOMEM);
47 
48 	ret = drm_gem_fb_init_with_funcs(dev, &afbc_fb->base, file, mode_cmd,
49 					 &rockchip_drm_fb_funcs);
50 	if (ret) {
51 		kfree(afbc_fb);
52 		return ERR_PTR(ret);
53 	}
54 
55 	if (drm_is_afbc(mode_cmd->modifier[0])) {
56 		int ret, i;
57 
58 		ret = drm_gem_fb_afbc_init(dev, mode_cmd, afbc_fb);
59 		if (ret) {
60 			struct drm_gem_object **obj = afbc_fb->base.obj;
61 
62 			for (i = 0; i < info->num_planes; ++i)
63 				drm_gem_object_put(obj[i]);
64 
65 			kfree(afbc_fb);
66 			return ERR_PTR(ret);
67 		}
68 	}
69 
70 	return &afbc_fb->base;
71 }
72 
73 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
74 	.fb_create = rockchip_fb_create,
75 	.output_poll_changed = drm_fb_helper_output_poll_changed,
76 	.atomic_check = drm_atomic_helper_check,
77 	.atomic_commit = drm_atomic_helper_commit,
78 };
79 
80 void rockchip_drm_mode_config_init(struct drm_device *dev)
81 {
82 	dev->mode_config.min_width = 0;
83 	dev->mode_config.min_height = 0;
84 
85 	/*
86 	 * set max width and height as default value(4096x4096).
87 	 * this value would be used to check framebuffer size limitation
88 	 * at drm_mode_addfb().
89 	 */
90 	dev->mode_config.max_width = 4096;
91 	dev->mode_config.max_height = 4096;
92 
93 	dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
94 	dev->mode_config.helper_private = &rockchip_mode_config_helpers;
95 
96 	dev->mode_config.normalize_zpos = true;
97 }
98