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_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 struct drm_framebuffer *
28 rockchip_fb_alloc(struct drm_device *dev, const struct drm_mode_fb_cmd2 *mode_cmd,
29 		  struct drm_gem_object **obj, unsigned int num_planes)
30 {
31 	struct drm_framebuffer *fb;
32 	int ret;
33 	int i;
34 
35 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
36 	if (!fb)
37 		return ERR_PTR(-ENOMEM);
38 
39 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
40 
41 	for (i = 0; i < num_planes; i++)
42 		fb->obj[i] = obj[i];
43 
44 	ret = drm_framebuffer_init(dev, fb, &rockchip_drm_fb_funcs);
45 	if (ret) {
46 		DRM_DEV_ERROR(dev->dev,
47 			      "Failed to initialize framebuffer: %d\n",
48 			      ret);
49 		kfree(fb);
50 		return ERR_PTR(ret);
51 	}
52 
53 	return fb;
54 }
55 
56 static const struct drm_mode_config_helper_funcs rockchip_mode_config_helpers = {
57 	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
58 };
59 
60 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
61 	.fb_create = drm_gem_fb_create_with_dirty,
62 	.output_poll_changed = drm_fb_helper_output_poll_changed,
63 	.atomic_check = drm_atomic_helper_check,
64 	.atomic_commit = drm_atomic_helper_commit,
65 };
66 
67 struct drm_framebuffer *
68 rockchip_drm_framebuffer_init(struct drm_device *dev,
69 			      const struct drm_mode_fb_cmd2 *mode_cmd,
70 			      struct drm_gem_object *obj)
71 {
72 	struct drm_framebuffer *fb;
73 
74 	fb = rockchip_fb_alloc(dev, mode_cmd, &obj, 1);
75 	if (IS_ERR(fb))
76 		return ERR_CAST(fb);
77 
78 	return fb;
79 }
80 
81 void rockchip_drm_mode_config_init(struct drm_device *dev)
82 {
83 	dev->mode_config.min_width = 0;
84 	dev->mode_config.min_height = 0;
85 
86 	/*
87 	 * set max width and height as default value(4096x4096).
88 	 * this value would be used to check framebuffer size limitation
89 	 * at drm_mode_addfb().
90 	 */
91 	dev->mode_config.max_width = 4096;
92 	dev->mode_config.max_height = 4096;
93 
94 	dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
95 	dev->mode_config.helper_private = &rockchip_mode_config_helpers;
96 }
97