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_plane_helper.h>
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_fourcc.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 	.destroy	= drm_plane_cleanup,
18 	.reset		= drm_atomic_helper_plane_reset,
19 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
20 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
21 };
22 
23 static int tilcdc_plane_atomic_check(struct drm_plane *plane,
24 				     struct drm_atomic_state *state)
25 {
26 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
27 									   plane);
28 	struct drm_crtc_state *crtc_state;
29 	struct drm_plane_state *old_state = plane->state;
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 		dev_err(plane->dev->dev, "%s: crtc position must be zero.",
40 			__func__);
41 		return -EINVAL;
42 	}
43 
44 	crtc_state = drm_atomic_get_existing_crtc_state(new_state->state,
45 							new_state->crtc);
46 	/* we should have a crtc state if the plane is attached to a crtc */
47 	if (WARN_ON(!crtc_state))
48 		return 0;
49 
50 	if (crtc_state->mode.hdisplay != new_state->crtc_w ||
51 	    crtc_state->mode.vdisplay != new_state->crtc_h) {
52 		dev_err(plane->dev->dev,
53 			"%s: Size must match mode (%dx%d == %dx%d)", __func__,
54 			crtc_state->mode.hdisplay, crtc_state->mode.vdisplay,
55 			new_state->crtc_w, new_state->crtc_h);
56 		return -EINVAL;
57 	}
58 
59 	pitch = crtc_state->mode.hdisplay *
60 		new_state->fb->format->cpp[0];
61 	if (new_state->fb->pitches[0] != pitch) {
62 		dev_err(plane->dev->dev,
63 			"Invalid pitch: fb and crtc widths must be the same");
64 		return -EINVAL;
65 	}
66 
67 	if (old_state->fb && new_state->fb->format != old_state->fb->format) {
68 		dev_dbg(plane->dev->dev,
69 			"%s(): pixel format change requires mode_change\n",
70 			__func__);
71 		crtc_state->mode_changed = true;
72 	}
73 
74 	return 0;
75 }
76 
77 static void tilcdc_plane_atomic_update(struct drm_plane *plane,
78 				       struct drm_plane_state *old_state)
79 {
80 	struct drm_plane_state *state = plane->state;
81 
82 	if (!state->crtc)
83 		return;
84 
85 	if (WARN_ON(!state->fb || !state->crtc->state))
86 		return;
87 
88 	if (tilcdc_crtc_update_fb(state->crtc,
89 				  state->fb,
90 				  state->crtc->state->event) == 0) {
91 		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 int tilcdc_plane_init(struct drm_device *dev,
101 		      struct drm_plane *plane)
102 {
103 	struct tilcdc_drm_private *priv = dev->dev_private;
104 	int ret;
105 
106 	ret = drm_plane_init(dev, plane, 1,
107 			     &tilcdc_plane_funcs,
108 			     priv->pixelformats,
109 			     priv->num_pixelformats,
110 			     true);
111 	if (ret) {
112 		dev_err(dev->dev, "Failed to initialize plane: %d\n", ret);
113 		return ret;
114 	}
115 
116 	drm_plane_helper_add(plane, &plane_helper_funcs);
117 
118 	return 0;
119 }
120