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