xref: /linux/drivers/gpu/drm/tilcdc/tilcdc_plane.c (revision 38f7e5450ebfc6f2e046a249a3f629ea7bec8c31)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 Texas Instruments
4  * Author: Jyri Sarha <jsarha@ti.com>
5  */
6 
7 #include <drm/drm_atomic.h>
8 #include <drm/drm_atomic_helper.h>
9 #include <drm/drm_fourcc.h>
10 #include <drm/drm_framebuffer.h>
11 
12 #include "tilcdc_drv.h"
13 
14 static const struct drm_plane_funcs tilcdc_plane_funcs = {
15 	.update_plane	= drm_atomic_helper_update_plane,
16 	.disable_plane	= drm_atomic_helper_disable_plane,
17 	.reset		= drm_atomic_helper_plane_reset,
18 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
19 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
20 };
21 
22 static int tilcdc_plane_atomic_check(struct drm_plane *plane,
23 				     struct drm_atomic_state *state)
24 {
25 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
26 									   plane);
27 	struct drm_crtc_state *crtc_state;
28 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
29 									   plane);
30 	unsigned int pitch;
31 
32 	if (!new_state->crtc)
33 		return 0;
34 
35 	if (WARN_ON(!new_state->fb))
36 		return -EINVAL;
37 
38 	if (new_state->crtc_x || new_state->crtc_y) {
39 		drm_err(plane->dev, "%s: crtc position must be zero.",
40 			__func__);
41 		return -EINVAL;
42 	}
43 
44 	crtc_state = drm_atomic_get_new_crtc_state(state, new_state->crtc);
45 	/* we should have a crtc state if the plane is attached to a crtc */
46 	if (WARN_ON(!crtc_state))
47 		return 0;
48 
49 	if (crtc_state->mode.hdisplay != new_state->crtc_w ||
50 	    crtc_state->mode.vdisplay != new_state->crtc_h) {
51 		drm_err(plane->dev,
52 			"%s: Size must match mode (%dx%d == %dx%d)", __func__,
53 			crtc_state->mode.hdisplay, crtc_state->mode.vdisplay,
54 			new_state->crtc_w, new_state->crtc_h);
55 		return -EINVAL;
56 	}
57 
58 	pitch = crtc_state->mode.hdisplay *
59 		new_state->fb->format->cpp[0];
60 	if (new_state->fb->pitches[0] != pitch) {
61 		drm_err(plane->dev,
62 			"Invalid pitch: fb and crtc widths must be the same");
63 		return -EINVAL;
64 	}
65 
66 	if (old_state->fb && new_state->fb->format != old_state->fb->format) {
67 		drm_dbg(plane->dev,
68 			"%s(): pixel format change requires mode_change\n",
69 			__func__);
70 		crtc_state->mode_changed = true;
71 	}
72 
73 	return 0;
74 }
75 
76 static void tilcdc_plane_atomic_update(struct drm_plane *plane,
77 				       struct drm_atomic_state *state)
78 {
79 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
80 									   plane);
81 
82 	if (!new_state->crtc)
83 		return;
84 
85 	if (WARN_ON(!new_state->fb || !new_state->crtc->state))
86 		return;
87 
88 	if (tilcdc_crtc_update_fb(new_state->crtc,
89 				  new_state->fb,
90 				  new_state->crtc->state->event) == 0) {
91 		new_state->crtc->state->event = NULL;
92 	}
93 }
94 
95 static const struct drm_plane_helper_funcs plane_helper_funcs = {
96 	.atomic_check = tilcdc_plane_atomic_check,
97 	.atomic_update = tilcdc_plane_atomic_update,
98 };
99 
100 struct tilcdc_plane *tilcdc_plane_init(struct drm_device *dev)
101 {
102 	struct tilcdc_drm_private *priv = ddev_to_tilcdc_priv(dev);
103 	struct tilcdc_plane *plane;
104 
105 	plane = drmm_universal_plane_alloc(dev, struct tilcdc_plane, base,
106 					   1, &tilcdc_plane_funcs,
107 					   priv->pixelformats,
108 					   priv->num_pixelformats,
109 					   NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
110 	if (IS_ERR(plane))
111 		return plane;
112 
113 	drm_plane_helper_add(&plane->base, &plane_helper_funcs);
114 
115 	return plane;
116 }
117