xref: /openbmc/linux/drivers/gpu/drm/sun4i/sun4i_layer.c (revision 05cf4fe738242183f1237f1b3a28b4479348c0a1)
1 /*
2  * Copyright (C) 2015 Free Electrons
3  * Copyright (C) 2015 NextThing Co
4  *
5  * Maxime Ripard <maxime.ripard@free-electrons.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  */
12 
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_plane_helper.h>
15 #include <drm/drmP.h>
16 
17 #include "sun4i_backend.h"
18 #include "sun4i_frontend.h"
19 #include "sun4i_layer.h"
20 #include "sunxi_engine.h"
21 
22 static void sun4i_backend_layer_reset(struct drm_plane *plane)
23 {
24 	struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
25 	struct sun4i_layer_state *state;
26 
27 	if (plane->state) {
28 		state = state_to_sun4i_layer_state(plane->state);
29 
30 		__drm_atomic_helper_plane_destroy_state(&state->state);
31 
32 		kfree(state);
33 		plane->state = NULL;
34 	}
35 
36 	state = kzalloc(sizeof(*state), GFP_KERNEL);
37 	if (state) {
38 		__drm_atomic_helper_plane_reset(plane, &state->state);
39 		plane->state->zpos = layer->id;
40 	}
41 }
42 
43 static struct drm_plane_state *
44 sun4i_backend_layer_duplicate_state(struct drm_plane *plane)
45 {
46 	struct sun4i_layer_state *orig = state_to_sun4i_layer_state(plane->state);
47 	struct sun4i_layer_state *copy;
48 
49 	copy = kzalloc(sizeof(*copy), GFP_KERNEL);
50 	if (!copy)
51 		return NULL;
52 
53 	__drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
54 	copy->uses_frontend = orig->uses_frontend;
55 
56 	return &copy->state;
57 }
58 
59 static void sun4i_backend_layer_destroy_state(struct drm_plane *plane,
60 					      struct drm_plane_state *state)
61 {
62 	struct sun4i_layer_state *s_state = state_to_sun4i_layer_state(state);
63 
64 	__drm_atomic_helper_plane_destroy_state(state);
65 
66 	kfree(s_state);
67 }
68 
69 static void sun4i_backend_layer_atomic_disable(struct drm_plane *plane,
70 					       struct drm_plane_state *old_state)
71 {
72 	struct sun4i_layer_state *layer_state = state_to_sun4i_layer_state(old_state);
73 	struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
74 	struct sun4i_backend *backend = layer->backend;
75 
76 	sun4i_backend_layer_enable(backend, layer->id, false);
77 
78 	if (layer_state->uses_frontend) {
79 		unsigned long flags;
80 
81 		spin_lock_irqsave(&backend->frontend_lock, flags);
82 		backend->frontend_teardown = true;
83 		spin_unlock_irqrestore(&backend->frontend_lock, flags);
84 	}
85 }
86 
87 static void sun4i_backend_layer_atomic_update(struct drm_plane *plane,
88 					      struct drm_plane_state *old_state)
89 {
90 	struct sun4i_layer_state *layer_state = state_to_sun4i_layer_state(plane->state);
91 	struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
92 	struct sun4i_backend *backend = layer->backend;
93 	struct sun4i_frontend *frontend = backend->frontend;
94 
95 	if (layer_state->uses_frontend) {
96 		sun4i_frontend_init(frontend);
97 		sun4i_frontend_update_coord(frontend, plane);
98 		sun4i_frontend_update_buffer(frontend, plane);
99 		sun4i_frontend_update_formats(frontend, plane,
100 					      DRM_FORMAT_ARGB8888);
101 		sun4i_backend_update_layer_frontend(backend, layer->id,
102 						    DRM_FORMAT_ARGB8888);
103 		sun4i_frontend_enable(frontend);
104 	} else {
105 		sun4i_backend_update_layer_formats(backend, layer->id, plane);
106 		sun4i_backend_update_layer_buffer(backend, layer->id, plane);
107 	}
108 
109 	sun4i_backend_update_layer_coord(backend, layer->id, plane);
110 	sun4i_backend_update_layer_zpos(backend, layer->id, plane);
111 	sun4i_backend_layer_enable(backend, layer->id, true);
112 }
113 
114 static const struct drm_plane_helper_funcs sun4i_backend_layer_helper_funcs = {
115 	.atomic_disable	= sun4i_backend_layer_atomic_disable,
116 	.atomic_update	= sun4i_backend_layer_atomic_update,
117 };
118 
119 static const struct drm_plane_funcs sun4i_backend_layer_funcs = {
120 	.atomic_destroy_state	= sun4i_backend_layer_destroy_state,
121 	.atomic_duplicate_state	= sun4i_backend_layer_duplicate_state,
122 	.destroy		= drm_plane_cleanup,
123 	.disable_plane		= drm_atomic_helper_disable_plane,
124 	.reset			= sun4i_backend_layer_reset,
125 	.update_plane		= drm_atomic_helper_update_plane,
126 };
127 
128 static const uint32_t sun4i_backend_layer_formats[] = {
129 	DRM_FORMAT_ARGB8888,
130 	DRM_FORMAT_ARGB4444,
131 	DRM_FORMAT_ARGB1555,
132 	DRM_FORMAT_RGBA5551,
133 	DRM_FORMAT_RGBA4444,
134 	DRM_FORMAT_RGB888,
135 	DRM_FORMAT_RGB565,
136 	DRM_FORMAT_UYVY,
137 	DRM_FORMAT_VYUY,
138 	DRM_FORMAT_XRGB8888,
139 	DRM_FORMAT_YUYV,
140 	DRM_FORMAT_YVYU,
141 };
142 
143 static struct sun4i_layer *sun4i_layer_init_one(struct drm_device *drm,
144 						struct sun4i_backend *backend,
145 						enum drm_plane_type type)
146 {
147 	struct sun4i_layer *layer;
148 	int ret;
149 
150 	layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
151 	if (!layer)
152 		return ERR_PTR(-ENOMEM);
153 
154 	/* possible crtcs are set later */
155 	ret = drm_universal_plane_init(drm, &layer->plane, 0,
156 				       &sun4i_backend_layer_funcs,
157 				       sun4i_backend_layer_formats,
158 				       ARRAY_SIZE(sun4i_backend_layer_formats),
159 				       NULL, type, NULL);
160 	if (ret) {
161 		dev_err(drm->dev, "Couldn't initialize layer\n");
162 		return ERR_PTR(ret);
163 	}
164 
165 	drm_plane_helper_add(&layer->plane,
166 			     &sun4i_backend_layer_helper_funcs);
167 	layer->backend = backend;
168 
169 	drm_plane_create_alpha_property(&layer->plane);
170 	drm_plane_create_zpos_property(&layer->plane, 0, 0,
171 				       SUN4I_BACKEND_NUM_LAYERS - 1);
172 
173 	return layer;
174 }
175 
176 struct drm_plane **sun4i_layers_init(struct drm_device *drm,
177 				     struct sunxi_engine *engine)
178 {
179 	struct drm_plane **planes;
180 	struct sun4i_backend *backend = engine_to_sun4i_backend(engine);
181 	int i;
182 
183 	/* We need to have a sentinel at the need, hence the overallocation */
184 	planes = devm_kcalloc(drm->dev, SUN4I_BACKEND_NUM_LAYERS + 1,
185 			      sizeof(*planes), GFP_KERNEL);
186 	if (!planes)
187 		return ERR_PTR(-ENOMEM);
188 
189 	for (i = 0; i < SUN4I_BACKEND_NUM_LAYERS; i++) {
190 		enum drm_plane_type type = i ? DRM_PLANE_TYPE_OVERLAY : DRM_PLANE_TYPE_PRIMARY;
191 		struct sun4i_layer *layer;
192 
193 		layer = sun4i_layer_init_one(drm, backend, type);
194 		if (IS_ERR(layer)) {
195 			dev_err(drm->dev, "Couldn't initialize %s plane\n",
196 				i ? "overlay" : "primary");
197 			return ERR_CAST(layer);
198 		};
199 
200 		layer->id = i;
201 		planes[i] = &layer->plane;
202 	};
203 
204 	return planes;
205 }
206