xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/omapdrm/omap_plane.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
4*4882a593Smuzhiyun  * Author: Rob Clark <rob.clark@linaro.org>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <drm/drm_atomic.h>
8*4882a593Smuzhiyun #include <drm/drm_atomic_helper.h>
9*4882a593Smuzhiyun #include <drm/drm_plane_helper.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include "omap_dmm_tiler.h"
12*4882a593Smuzhiyun #include "omap_drv.h"
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun  * plane funcs
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define to_omap_plane(x) container_of(x, struct omap_plane, base)
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun struct omap_plane {
21*4882a593Smuzhiyun 	struct drm_plane base;
22*4882a593Smuzhiyun 	enum omap_plane_id id;
23*4882a593Smuzhiyun 	const char *name;
24*4882a593Smuzhiyun };
25*4882a593Smuzhiyun 
omap_plane_prepare_fb(struct drm_plane * plane,struct drm_plane_state * new_state)26*4882a593Smuzhiyun static int omap_plane_prepare_fb(struct drm_plane *plane,
27*4882a593Smuzhiyun 				 struct drm_plane_state *new_state)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	if (!new_state->fb)
30*4882a593Smuzhiyun 		return 0;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	return omap_framebuffer_pin(new_state->fb);
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
omap_plane_cleanup_fb(struct drm_plane * plane,struct drm_plane_state * old_state)35*4882a593Smuzhiyun static void omap_plane_cleanup_fb(struct drm_plane *plane,
36*4882a593Smuzhiyun 				  struct drm_plane_state *old_state)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	if (old_state->fb)
39*4882a593Smuzhiyun 		omap_framebuffer_unpin(old_state->fb);
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun 
omap_plane_atomic_update(struct drm_plane * plane,struct drm_plane_state * old_state)42*4882a593Smuzhiyun static void omap_plane_atomic_update(struct drm_plane *plane,
43*4882a593Smuzhiyun 				     struct drm_plane_state *old_state)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	struct omap_drm_private *priv = plane->dev->dev_private;
46*4882a593Smuzhiyun 	struct omap_plane *omap_plane = to_omap_plane(plane);
47*4882a593Smuzhiyun 	struct drm_plane_state *state = plane->state;
48*4882a593Smuzhiyun 	struct omap_overlay_info info;
49*4882a593Smuzhiyun 	int ret;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	DBG("%s, crtc=%p fb=%p", omap_plane->name, state->crtc, state->fb);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	memset(&info, 0, sizeof(info));
54*4882a593Smuzhiyun 	info.rotation_type = OMAP_DSS_ROT_NONE;
55*4882a593Smuzhiyun 	info.rotation = DRM_MODE_ROTATE_0;
56*4882a593Smuzhiyun 	info.global_alpha = state->alpha >> 8;
57*4882a593Smuzhiyun 	info.zorder = state->normalized_zpos;
58*4882a593Smuzhiyun 	if (state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
59*4882a593Smuzhiyun 		info.pre_mult_alpha = 1;
60*4882a593Smuzhiyun 	else
61*4882a593Smuzhiyun 		info.pre_mult_alpha = 0;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	/* update scanout: */
64*4882a593Smuzhiyun 	omap_framebuffer_update_scanout(state->fb, state, &info);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	DBG("%dx%d -> %dx%d (%d)", info.width, info.height,
67*4882a593Smuzhiyun 			info.out_width, info.out_height,
68*4882a593Smuzhiyun 			info.screen_width);
69*4882a593Smuzhiyun 	DBG("%d,%d %pad %pad", info.pos_x, info.pos_y,
70*4882a593Smuzhiyun 			&info.paddr, &info.p_uv_addr);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	/* and finally, update omapdss: */
73*4882a593Smuzhiyun 	ret = priv->dispc_ops->ovl_setup(priv->dispc, omap_plane->id, &info,
74*4882a593Smuzhiyun 			      omap_crtc_timings(state->crtc), false,
75*4882a593Smuzhiyun 			      omap_crtc_channel(state->crtc));
76*4882a593Smuzhiyun 	if (ret) {
77*4882a593Smuzhiyun 		dev_err(plane->dev->dev, "Failed to setup plane %s\n",
78*4882a593Smuzhiyun 			omap_plane->name);
79*4882a593Smuzhiyun 		priv->dispc_ops->ovl_enable(priv->dispc, omap_plane->id, false);
80*4882a593Smuzhiyun 		return;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	priv->dispc_ops->ovl_enable(priv->dispc, omap_plane->id, true);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
omap_plane_atomic_disable(struct drm_plane * plane,struct drm_plane_state * old_state)86*4882a593Smuzhiyun static void omap_plane_atomic_disable(struct drm_plane *plane,
87*4882a593Smuzhiyun 				      struct drm_plane_state *old_state)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	struct omap_drm_private *priv = plane->dev->dev_private;
90*4882a593Smuzhiyun 	struct omap_plane *omap_plane = to_omap_plane(plane);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	plane->state->rotation = DRM_MODE_ROTATE_0;
93*4882a593Smuzhiyun 	plane->state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY
94*4882a593Smuzhiyun 			   ? 0 : omap_plane->id;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	priv->dispc_ops->ovl_enable(priv->dispc, omap_plane->id, false);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
omap_plane_atomic_check(struct drm_plane * plane,struct drm_plane_state * state)99*4882a593Smuzhiyun static int omap_plane_atomic_check(struct drm_plane *plane,
100*4882a593Smuzhiyun 				   struct drm_plane_state *state)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	struct drm_crtc_state *crtc_state;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	if (!state->fb)
105*4882a593Smuzhiyun 		return 0;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	/* crtc should only be NULL when disabling (i.e., !state->fb) */
108*4882a593Smuzhiyun 	if (WARN_ON(!state->crtc))
109*4882a593Smuzhiyun 		return 0;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	crtc_state = drm_atomic_get_existing_crtc_state(state->state, state->crtc);
112*4882a593Smuzhiyun 	/* we should have a crtc state if the plane is attached to a crtc */
113*4882a593Smuzhiyun 	if (WARN_ON(!crtc_state))
114*4882a593Smuzhiyun 		return 0;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	if (!crtc_state->enable)
117*4882a593Smuzhiyun 		return 0;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	if (state->crtc_x < 0 || state->crtc_y < 0)
120*4882a593Smuzhiyun 		return -EINVAL;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	if (state->crtc_x + state->crtc_w > crtc_state->adjusted_mode.hdisplay)
123*4882a593Smuzhiyun 		return -EINVAL;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	if (state->crtc_y + state->crtc_h > crtc_state->adjusted_mode.vdisplay)
126*4882a593Smuzhiyun 		return -EINVAL;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	if (state->rotation != DRM_MODE_ROTATE_0 &&
129*4882a593Smuzhiyun 	    !omap_framebuffer_supports_rotation(state->fb))
130*4882a593Smuzhiyun 		return -EINVAL;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	return 0;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun static const struct drm_plane_helper_funcs omap_plane_helper_funcs = {
136*4882a593Smuzhiyun 	.prepare_fb = omap_plane_prepare_fb,
137*4882a593Smuzhiyun 	.cleanup_fb = omap_plane_cleanup_fb,
138*4882a593Smuzhiyun 	.atomic_check = omap_plane_atomic_check,
139*4882a593Smuzhiyun 	.atomic_update = omap_plane_atomic_update,
140*4882a593Smuzhiyun 	.atomic_disable = omap_plane_atomic_disable,
141*4882a593Smuzhiyun };
142*4882a593Smuzhiyun 
omap_plane_destroy(struct drm_plane * plane)143*4882a593Smuzhiyun static void omap_plane_destroy(struct drm_plane *plane)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	struct omap_plane *omap_plane = to_omap_plane(plane);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	DBG("%s", omap_plane->name);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	drm_plane_cleanup(plane);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	kfree(omap_plane);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /* helper to install properties which are common to planes and crtcs */
omap_plane_install_properties(struct drm_plane * plane,struct drm_mode_object * obj)155*4882a593Smuzhiyun void omap_plane_install_properties(struct drm_plane *plane,
156*4882a593Smuzhiyun 		struct drm_mode_object *obj)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	struct drm_device *dev = plane->dev;
159*4882a593Smuzhiyun 	struct omap_drm_private *priv = dev->dev_private;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	if (priv->has_dmm) {
162*4882a593Smuzhiyun 		if (!plane->rotation_property)
163*4882a593Smuzhiyun 			drm_plane_create_rotation_property(plane,
164*4882a593Smuzhiyun 							   DRM_MODE_ROTATE_0,
165*4882a593Smuzhiyun 							   DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
166*4882a593Smuzhiyun 							   DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
167*4882a593Smuzhiyun 							   DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 		/* Attach the rotation property also to the crtc object */
170*4882a593Smuzhiyun 		if (plane->rotation_property && obj != &plane->base)
171*4882a593Smuzhiyun 			drm_object_attach_property(obj, plane->rotation_property,
172*4882a593Smuzhiyun 						   DRM_MODE_ROTATE_0);
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	drm_object_attach_property(obj, priv->zorder_prop, 0);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
omap_plane_reset(struct drm_plane * plane)178*4882a593Smuzhiyun static void omap_plane_reset(struct drm_plane *plane)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	struct omap_plane *omap_plane = to_omap_plane(plane);
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	drm_atomic_helper_plane_reset(plane);
183*4882a593Smuzhiyun 	if (!plane->state)
184*4882a593Smuzhiyun 		return;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	/*
187*4882a593Smuzhiyun 	 * Set the zpos default depending on whether we are a primary or overlay
188*4882a593Smuzhiyun 	 * plane.
189*4882a593Smuzhiyun 	 */
190*4882a593Smuzhiyun 	plane->state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY
191*4882a593Smuzhiyun 			   ? 0 : omap_plane->id;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
omap_plane_atomic_set_property(struct drm_plane * plane,struct drm_plane_state * state,struct drm_property * property,u64 val)194*4882a593Smuzhiyun static int omap_plane_atomic_set_property(struct drm_plane *plane,
195*4882a593Smuzhiyun 					  struct drm_plane_state *state,
196*4882a593Smuzhiyun 					  struct drm_property *property,
197*4882a593Smuzhiyun 					  u64 val)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	struct omap_drm_private *priv = plane->dev->dev_private;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	if (property == priv->zorder_prop)
202*4882a593Smuzhiyun 		state->zpos = val;
203*4882a593Smuzhiyun 	else
204*4882a593Smuzhiyun 		return -EINVAL;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	return 0;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
omap_plane_atomic_get_property(struct drm_plane * plane,const struct drm_plane_state * state,struct drm_property * property,u64 * val)209*4882a593Smuzhiyun static int omap_plane_atomic_get_property(struct drm_plane *plane,
210*4882a593Smuzhiyun 					  const struct drm_plane_state *state,
211*4882a593Smuzhiyun 					  struct drm_property *property,
212*4882a593Smuzhiyun 					  u64 *val)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	struct omap_drm_private *priv = plane->dev->dev_private;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	if (property == priv->zorder_prop)
217*4882a593Smuzhiyun 		*val = state->zpos;
218*4882a593Smuzhiyun 	else
219*4882a593Smuzhiyun 		return -EINVAL;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	return 0;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun static const struct drm_plane_funcs omap_plane_funcs = {
225*4882a593Smuzhiyun 	.update_plane = drm_atomic_helper_update_plane,
226*4882a593Smuzhiyun 	.disable_plane = drm_atomic_helper_disable_plane,
227*4882a593Smuzhiyun 	.reset = omap_plane_reset,
228*4882a593Smuzhiyun 	.destroy = omap_plane_destroy,
229*4882a593Smuzhiyun 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
230*4882a593Smuzhiyun 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
231*4882a593Smuzhiyun 	.atomic_set_property = omap_plane_atomic_set_property,
232*4882a593Smuzhiyun 	.atomic_get_property = omap_plane_atomic_get_property,
233*4882a593Smuzhiyun };
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun static const char *plane_id_to_name[] = {
236*4882a593Smuzhiyun 	[OMAP_DSS_GFX] = "gfx",
237*4882a593Smuzhiyun 	[OMAP_DSS_VIDEO1] = "vid1",
238*4882a593Smuzhiyun 	[OMAP_DSS_VIDEO2] = "vid2",
239*4882a593Smuzhiyun 	[OMAP_DSS_VIDEO3] = "vid3",
240*4882a593Smuzhiyun };
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun static const enum omap_plane_id plane_idx_to_id[] = {
243*4882a593Smuzhiyun 	OMAP_DSS_GFX,
244*4882a593Smuzhiyun 	OMAP_DSS_VIDEO1,
245*4882a593Smuzhiyun 	OMAP_DSS_VIDEO2,
246*4882a593Smuzhiyun 	OMAP_DSS_VIDEO3,
247*4882a593Smuzhiyun };
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun /* initialize plane */
omap_plane_init(struct drm_device * dev,int idx,enum drm_plane_type type,u32 possible_crtcs)250*4882a593Smuzhiyun struct drm_plane *omap_plane_init(struct drm_device *dev,
251*4882a593Smuzhiyun 		int idx, enum drm_plane_type type,
252*4882a593Smuzhiyun 		u32 possible_crtcs)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	struct omap_drm_private *priv = dev->dev_private;
255*4882a593Smuzhiyun 	unsigned int num_planes = priv->dispc_ops->get_num_ovls(priv->dispc);
256*4882a593Smuzhiyun 	struct drm_plane *plane;
257*4882a593Smuzhiyun 	struct omap_plane *omap_plane;
258*4882a593Smuzhiyun 	enum omap_plane_id id;
259*4882a593Smuzhiyun 	int ret;
260*4882a593Smuzhiyun 	u32 nformats;
261*4882a593Smuzhiyun 	const u32 *formats;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	if (WARN_ON(idx >= ARRAY_SIZE(plane_idx_to_id)))
264*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	id = plane_idx_to_id[idx];
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	DBG("%s: type=%d", plane_id_to_name[id], type);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
271*4882a593Smuzhiyun 	if (!omap_plane)
272*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	formats = priv->dispc_ops->ovl_get_color_modes(priv->dispc, id);
275*4882a593Smuzhiyun 	for (nformats = 0; formats[nformats]; ++nformats)
276*4882a593Smuzhiyun 		;
277*4882a593Smuzhiyun 	omap_plane->id = id;
278*4882a593Smuzhiyun 	omap_plane->name = plane_id_to_name[id];
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	plane = &omap_plane->base;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	ret = drm_universal_plane_init(dev, plane, possible_crtcs,
283*4882a593Smuzhiyun 				       &omap_plane_funcs, formats,
284*4882a593Smuzhiyun 				       nformats, NULL, type, NULL);
285*4882a593Smuzhiyun 	if (ret < 0)
286*4882a593Smuzhiyun 		goto error;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	drm_plane_helper_add(plane, &omap_plane_helper_funcs);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	omap_plane_install_properties(plane, &plane->base);
291*4882a593Smuzhiyun 	drm_plane_create_zpos_property(plane, 0, 0, num_planes - 1);
292*4882a593Smuzhiyun 	drm_plane_create_alpha_property(plane);
293*4882a593Smuzhiyun 	drm_plane_create_blend_mode_property(plane, BIT(DRM_MODE_BLEND_PREMULTI) |
294*4882a593Smuzhiyun 					     BIT(DRM_MODE_BLEND_COVERAGE));
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	return plane;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun error:
299*4882a593Smuzhiyun 	dev_err(dev->dev, "%s(): could not create plane: %s\n",
300*4882a593Smuzhiyun 		__func__, plane_id_to_name[id]);
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	kfree(omap_plane);
303*4882a593Smuzhiyun 	return NULL;
304*4882a593Smuzhiyun }
305