2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author:Mark Yao <mark.yao@rock-chips.com>
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/kernel.h>
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_crtc_helper.h>
22 #include "rockchip_drm_drv.h"
23 #include "rockchip_drm_fb.h"
24 #include "rockchip_drm_gem.h"
25 #include "rockchip_drm_psr.h"
27 #define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
29 struct rockchip_drm_fb
{
30 struct drm_framebuffer fb
;
31 struct drm_gem_object
*obj
[ROCKCHIP_MAX_FB_BUFFER
];
34 struct drm_gem_object
*rockchip_fb_get_gem_obj(struct drm_framebuffer
*fb
,
37 struct rockchip_drm_fb
*rk_fb
= to_rockchip_fb(fb
);
39 if (plane
>= ROCKCHIP_MAX_FB_BUFFER
)
42 return rk_fb
->obj
[plane
];
45 static void rockchip_drm_fb_destroy(struct drm_framebuffer
*fb
)
47 struct rockchip_drm_fb
*rockchip_fb
= to_rockchip_fb(fb
);
50 for (i
= 0; i
< ROCKCHIP_MAX_FB_BUFFER
; i
++)
51 drm_gem_object_unreference_unlocked(rockchip_fb
->obj
[i
]);
53 drm_framebuffer_cleanup(fb
);
57 static int rockchip_drm_fb_create_handle(struct drm_framebuffer
*fb
,
58 struct drm_file
*file_priv
,
61 struct rockchip_drm_fb
*rockchip_fb
= to_rockchip_fb(fb
);
63 return drm_gem_handle_create(file_priv
,
64 rockchip_fb
->obj
[0], handle
);
67 static int rockchip_drm_fb_dirty(struct drm_framebuffer
*fb
,
68 struct drm_file
*file
,
69 unsigned int flags
, unsigned int color
,
70 struct drm_clip_rect
*clips
,
71 unsigned int num_clips
)
73 rockchip_drm_psr_flush_all(fb
->dev
);
77 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs
= {
78 .destroy
= rockchip_drm_fb_destroy
,
79 .create_handle
= rockchip_drm_fb_create_handle
,
80 .dirty
= rockchip_drm_fb_dirty
,
83 static struct rockchip_drm_fb
*
84 rockchip_fb_alloc(struct drm_device
*dev
, const struct drm_mode_fb_cmd2
*mode_cmd
,
85 struct drm_gem_object
**obj
, unsigned int num_planes
)
87 struct rockchip_drm_fb
*rockchip_fb
;
91 rockchip_fb
= kzalloc(sizeof(*rockchip_fb
), GFP_KERNEL
);
93 return ERR_PTR(-ENOMEM
);
95 drm_helper_mode_fill_fb_struct(dev
, &rockchip_fb
->fb
, mode_cmd
);
97 for (i
= 0; i
< num_planes
; i
++)
98 rockchip_fb
->obj
[i
] = obj
[i
];
100 ret
= drm_framebuffer_init(dev
, &rockchip_fb
->fb
,
101 &rockchip_drm_fb_funcs
);
103 dev_err(dev
->dev
, "Failed to initialize framebuffer: %d\n",
112 static struct drm_framebuffer
*
113 rockchip_user_fb_create(struct drm_device
*dev
, struct drm_file
*file_priv
,
114 const struct drm_mode_fb_cmd2
*mode_cmd
)
116 struct rockchip_drm_fb
*rockchip_fb
;
117 struct drm_gem_object
*objs
[ROCKCHIP_MAX_FB_BUFFER
];
118 struct drm_gem_object
*obj
;
125 hsub
= drm_format_horz_chroma_subsampling(mode_cmd
->pixel_format
);
126 vsub
= drm_format_vert_chroma_subsampling(mode_cmd
->pixel_format
);
127 num_planes
= min(drm_format_num_planes(mode_cmd
->pixel_format
),
128 ROCKCHIP_MAX_FB_BUFFER
);
130 for (i
= 0; i
< num_planes
; i
++) {
131 unsigned int width
= mode_cmd
->width
/ (i
? hsub
: 1);
132 unsigned int height
= mode_cmd
->height
/ (i
? vsub
: 1);
133 unsigned int min_size
;
135 obj
= drm_gem_object_lookup(file_priv
, mode_cmd
->handles
[i
]);
137 dev_err(dev
->dev
, "Failed to lookup GEM object\n");
139 goto err_gem_object_unreference
;
142 min_size
= (height
- 1) * mode_cmd
->pitches
[i
] +
143 mode_cmd
->offsets
[i
] +
144 width
* drm_format_plane_cpp(mode_cmd
->pixel_format
, i
);
146 if (obj
->size
< min_size
) {
147 drm_gem_object_unreference_unlocked(obj
);
149 goto err_gem_object_unreference
;
154 rockchip_fb
= rockchip_fb_alloc(dev
, mode_cmd
, objs
, i
);
155 if (IS_ERR(rockchip_fb
)) {
156 ret
= PTR_ERR(rockchip_fb
);
157 goto err_gem_object_unreference
;
160 return &rockchip_fb
->fb
;
162 err_gem_object_unreference
:
163 for (i
--; i
>= 0; i
--)
164 drm_gem_object_unreference_unlocked(objs
[i
]);
168 static void rockchip_drm_output_poll_changed(struct drm_device
*dev
)
170 struct rockchip_drm_private
*private = dev
->dev_private
;
171 struct drm_fb_helper
*fb_helper
= &private->fbdev_helper
;
174 drm_fb_helper_hotplug_event(fb_helper
);
178 rockchip_atomic_commit_tail(struct drm_atomic_state
*state
)
180 struct drm_device
*dev
= state
->dev
;
182 drm_atomic_helper_commit_modeset_disables(dev
, state
);
184 drm_atomic_helper_commit_modeset_enables(dev
, state
);
186 drm_atomic_helper_commit_planes(dev
, state
,
187 DRM_PLANE_COMMIT_ACTIVE_ONLY
);
189 drm_atomic_helper_commit_hw_done(state
);
191 drm_atomic_helper_wait_for_vblanks(dev
, state
);
193 drm_atomic_helper_cleanup_planes(dev
, state
);
196 static struct drm_mode_config_helper_funcs rockchip_mode_config_helpers
= {
197 .atomic_commit_tail
= rockchip_atomic_commit_tail
,
200 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs
= {
201 .fb_create
= rockchip_user_fb_create
,
202 .output_poll_changed
= rockchip_drm_output_poll_changed
,
203 .atomic_check
= drm_atomic_helper_check
,
204 .atomic_commit
= drm_atomic_helper_commit
,
207 struct drm_framebuffer
*
208 rockchip_drm_framebuffer_init(struct drm_device
*dev
,
209 const struct drm_mode_fb_cmd2
*mode_cmd
,
210 struct drm_gem_object
*obj
)
212 struct rockchip_drm_fb
*rockchip_fb
;
214 rockchip_fb
= rockchip_fb_alloc(dev
, mode_cmd
, &obj
, 1);
215 if (IS_ERR(rockchip_fb
))
218 return &rockchip_fb
->fb
;
221 void rockchip_drm_mode_config_init(struct drm_device
*dev
)
223 dev
->mode_config
.min_width
= 0;
224 dev
->mode_config
.min_height
= 0;
227 * set max width and height as default value(4096x4096).
228 * this value would be used to check framebuffer size limitation
229 * at drm_mode_addfb().
231 dev
->mode_config
.max_width
= 4096;
232 dev
->mode_config
.max_height
= 4096;
234 dev
->mode_config
.funcs
= &rockchip_drm_mode_config_funcs
;
235 dev
->mode_config
.helper_private
= &rockchip_mode_config_helpers
;