blob: fbf25d59cf8876309ac5b8869f74c5a139b63fba [file] [log] [blame]
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001/*
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>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010014#include <drm/drm_plane_helper.h>
15#include <drm/drmP.h>
16
17#include "sun4i_backend.h"
Maxime Ripardca07b212018-01-22 10:25:23 +010018#include "sun4i_frontend.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010019#include "sun4i_layer.h"
Icenowy Zheng87969332017-05-17 22:47:17 +080020#include "sunxi_engine.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010021
Maxime Ripardc222f392016-09-19 22:17:50 +020022struct sun4i_plane_desc {
Maxime Ripard185f1142017-04-30 15:45:14 +020023 enum drm_plane_type type;
24 u8 pipe;
25 const uint32_t *formats;
26 uint32_t nformats;
Maxime Ripardc222f392016-09-19 22:17:50 +020027};
Maxime Ripard9026e0d2015-10-29 09:36:23 +010028
Maxime Ripardd540f822018-01-22 10:25:18 +010029static void sun4i_backend_layer_reset(struct drm_plane *plane)
30{
Maxime Ripard2bebcc42017-12-17 17:32:21 +010031 struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
Maxime Ripardd540f822018-01-22 10:25:18 +010032 struct sun4i_layer_state *state;
33
34 if (plane->state) {
35 state = state_to_sun4i_layer_state(plane->state);
36
37 __drm_atomic_helper_plane_destroy_state(&state->state);
38
39 kfree(state);
40 plane->state = NULL;
41 }
42
43 state = kzalloc(sizeof(*state), GFP_KERNEL);
44 if (state) {
45 plane->state = &state->state;
46 plane->state->plane = plane;
Maxime Ripard2bebcc42017-12-17 17:32:21 +010047 plane->state->zpos = layer->id;
Maxime Ripardd540f822018-01-22 10:25:18 +010048 }
49}
50
51static struct drm_plane_state *
52sun4i_backend_layer_duplicate_state(struct drm_plane *plane)
53{
Maxime Ripardca07b212018-01-22 10:25:23 +010054 struct sun4i_layer_state *orig = state_to_sun4i_layer_state(plane->state);
Maxime Ripardd540f822018-01-22 10:25:18 +010055 struct sun4i_layer_state *copy;
56
57 copy = kzalloc(sizeof(*copy), GFP_KERNEL);
58 if (!copy)
59 return NULL;
60
61 __drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
Maxime Ripardca07b212018-01-22 10:25:23 +010062 copy->uses_frontend = orig->uses_frontend;
Maxime Ripardd540f822018-01-22 10:25:18 +010063
64 return &copy->state;
65}
66
67static void sun4i_backend_layer_destroy_state(struct drm_plane *plane,
68 struct drm_plane_state *state)
69{
70 struct sun4i_layer_state *s_state = state_to_sun4i_layer_state(state);
71
72 __drm_atomic_helper_plane_destroy_state(state);
73
74 kfree(s_state);
75}
76
Maxime Ripard9026e0d2015-10-29 09:36:23 +010077static void sun4i_backend_layer_atomic_disable(struct drm_plane *plane,
78 struct drm_plane_state *old_state)
79{
Maxime Ripardca07b212018-01-22 10:25:23 +010080 struct sun4i_layer_state *layer_state = state_to_sun4i_layer_state(old_state);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010081 struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
Chen-Yu Tsaiace6c092017-02-23 16:05:42 +080082 struct sun4i_backend *backend = layer->backend;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010083
84 sun4i_backend_layer_enable(backend, layer->id, false);
Maxime Ripardca07b212018-01-22 10:25:23 +010085
86 if (layer_state->uses_frontend) {
87 unsigned long flags;
88
89 spin_lock_irqsave(&backend->frontend_lock, flags);
90 backend->frontend_teardown = true;
91 spin_unlock_irqrestore(&backend->frontend_lock, flags);
92 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +010093}
94
95static void sun4i_backend_layer_atomic_update(struct drm_plane *plane,
96 struct drm_plane_state *old_state)
97{
Maxime Ripardca07b212018-01-22 10:25:23 +010098 struct sun4i_layer_state *layer_state = state_to_sun4i_layer_state(plane->state);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010099 struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
Chen-Yu Tsaiace6c092017-02-23 16:05:42 +0800100 struct sun4i_backend *backend = layer->backend;
Maxime Ripardca07b212018-01-22 10:25:23 +0100101 struct sun4i_frontend *frontend = backend->frontend;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100102
Maxime Ripardca07b212018-01-22 10:25:23 +0100103 if (layer_state->uses_frontend) {
104 sun4i_frontend_init(frontend);
105 sun4i_frontend_update_coord(frontend, plane);
106 sun4i_frontend_update_buffer(frontend, plane);
107 sun4i_frontend_update_formats(frontend, plane,
108 DRM_FORMAT_ARGB8888);
109 sun4i_backend_update_layer_frontend(backend, layer->id,
110 DRM_FORMAT_ARGB8888);
Maxime Ripardca07b212018-01-22 10:25:23 +0100111 sun4i_frontend_enable(frontend);
112 } else {
Maxime Ripardca07b212018-01-22 10:25:23 +0100113 sun4i_backend_update_layer_formats(backend, layer->id, plane);
114 sun4i_backend_update_layer_buffer(backend, layer->id, plane);
115 }
116
Maxime Ripard098b3382017-12-17 17:34:26 +0100117 sun4i_backend_update_layer_coord(backend, layer->id, plane);
Maxime Ripard47a05f42017-05-01 10:52:32 +0200118 sun4i_backend_update_layer_zpos(backend, layer->id, plane);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100119 sun4i_backend_layer_enable(backend, layer->id, true);
120}
121
Arvind Yadav934d1432017-07-03 21:35:20 +0530122static const struct drm_plane_helper_funcs sun4i_backend_layer_helper_funcs = {
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100123 .atomic_disable = sun4i_backend_layer_atomic_disable,
124 .atomic_update = sun4i_backend_layer_atomic_update,
125};
126
127static const struct drm_plane_funcs sun4i_backend_layer_funcs = {
Maxime Ripardd540f822018-01-22 10:25:18 +0100128 .atomic_destroy_state = sun4i_backend_layer_destroy_state,
129 .atomic_duplicate_state = sun4i_backend_layer_duplicate_state,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100130 .destroy = drm_plane_cleanup,
131 .disable_plane = drm_atomic_helper_disable_plane,
Maxime Ripardd540f822018-01-22 10:25:18 +0100132 .reset = sun4i_backend_layer_reset,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100133 .update_plane = drm_atomic_helper_update_plane,
134};
135
Maxime Ripardc222f392016-09-19 22:17:50 +0200136static const uint32_t sun4i_backend_layer_formats_primary[] = {
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100137 DRM_FORMAT_ARGB8888,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100138 DRM_FORMAT_RGB888,
Maxime Ripard47d7fbb2016-10-18 10:46:14 +0200139 DRM_FORMAT_RGB565,
Maxime Ripardc222f392016-09-19 22:17:50 +0200140 DRM_FORMAT_XRGB8888,
141};
142
143static const uint32_t sun4i_backend_layer_formats_overlay[] = {
144 DRM_FORMAT_ARGB8888,
Maxime Ripard47d7fbb2016-10-18 10:46:14 +0200145 DRM_FORMAT_ARGB4444,
146 DRM_FORMAT_ARGB1555,
147 DRM_FORMAT_RGBA5551,
148 DRM_FORMAT_RGBA4444,
Maxime Ripardc222f392016-09-19 22:17:50 +0200149 DRM_FORMAT_RGB888,
Maxime Ripard47d7fbb2016-10-18 10:46:14 +0200150 DRM_FORMAT_RGB565,
Maxime Ripardc222f392016-09-19 22:17:50 +0200151 DRM_FORMAT_XRGB8888,
152};
153
154static const struct sun4i_plane_desc sun4i_backend_planes[] = {
155 {
156 .type = DRM_PLANE_TYPE_PRIMARY,
157 .pipe = 0,
158 .formats = sun4i_backend_layer_formats_primary,
159 .nformats = ARRAY_SIZE(sun4i_backend_layer_formats_primary),
160 },
161 {
162 .type = DRM_PLANE_TYPE_OVERLAY,
163 .pipe = 1,
164 .formats = sun4i_backend_layer_formats_overlay,
165 .nformats = ARRAY_SIZE(sun4i_backend_layer_formats_overlay),
166 },
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100167};
168
169static struct sun4i_layer *sun4i_layer_init_one(struct drm_device *drm,
Chen-Yu Tsaia0a68fb2017-03-09 18:05:29 +0800170 struct sun4i_backend *backend,
Maxime Ripardc222f392016-09-19 22:17:50 +0200171 const struct sun4i_plane_desc *plane)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100172{
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100173 struct sun4i_layer *layer;
174 int ret;
175
176 layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
177 if (!layer)
178 return ERR_PTR(-ENOMEM);
179
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800180 /* possible crtcs are set later */
181 ret = drm_universal_plane_init(drm, &layer->plane, 0,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100182 &sun4i_backend_layer_funcs,
Maxime Ripardc222f392016-09-19 22:17:50 +0200183 plane->formats, plane->nformats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700184 NULL, plane->type, NULL);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100185 if (ret) {
186 dev_err(drm->dev, "Couldn't initialize layer\n");
187 return ERR_PTR(ret);
188 }
189
190 drm_plane_helper_add(&layer->plane,
191 &sun4i_backend_layer_helper_funcs);
Chen-Yu Tsaia0a68fb2017-03-09 18:05:29 +0800192 layer->backend = backend;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100193
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100194 return layer;
195}
196
Icenowy Zheng7921e142017-05-15 00:30:36 +0800197struct drm_plane **sun4i_layers_init(struct drm_device *drm,
Icenowy Zheng87969332017-05-17 22:47:17 +0800198 struct sunxi_engine *engine)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100199{
Icenowy Zheng7921e142017-05-15 00:30:36 +0800200 struct drm_plane **planes;
Icenowy Zheng87969332017-05-17 22:47:17 +0800201 struct sun4i_backend *backend = engine_to_sun4i_backend(engine);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100202 int i;
203
Icenowy Zheng7921e142017-05-15 00:30:36 +0800204 planes = devm_kcalloc(drm->dev, ARRAY_SIZE(sun4i_backend_planes) + 1,
205 sizeof(*planes), GFP_KERNEL);
206 if (!planes)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100207 return ERR_PTR(-ENOMEM);
208
209 /*
210 * The hardware is a bit unusual here.
211 *
212 * Even though it supports 4 layers, it does the composition
213 * in two separate steps.
214 *
215 * The first one is assigning a layer to one of its two
216 * pipes. If more that 1 layer is assigned to the same pipe,
217 * and if pixels overlaps, the pipe will take the pixel from
218 * the layer with the highest priority.
219 *
220 * The second step is the actual alpha blending, that takes
221 * the two pipes as input, and uses the eventual alpha
222 * component to do the transparency between the two.
223 *
224 * This two steps scenario makes us unable to guarantee a
225 * robust alpha blending between the 4 layers in all
226 * situations. So we just expose two layers, one per pipe. On
227 * SoCs that support it, sprites could fill the need for more
228 * layers.
229 */
Maxime Ripardc222f392016-09-19 22:17:50 +0200230 for (i = 0; i < ARRAY_SIZE(sun4i_backend_planes); i++) {
231 const struct sun4i_plane_desc *plane = &sun4i_backend_planes[i];
Chen-Yu Tsai8a164692017-02-17 11:13:28 +0800232 struct sun4i_layer *layer;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100233
Chen-Yu Tsaia0a68fb2017-03-09 18:05:29 +0800234 layer = sun4i_layer_init_one(drm, backend, plane);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100235 if (IS_ERR(layer)) {
236 dev_err(drm->dev, "Couldn't initialize %s plane\n",
237 i ? "overlay" : "primary");
238 return ERR_CAST(layer);
239 };
240
Maxime Ripard47a05f42017-05-01 10:52:32 +0200241 drm_plane_create_zpos_immutable_property(&layer->plane, i);
242
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100243 DRM_DEBUG_DRIVER("Assigning %s plane to pipe %d\n",
Maxime Ripardc222f392016-09-19 22:17:50 +0200244 i ? "overlay" : "primary", plane->pipe);
Icenowy Zheng87969332017-05-17 22:47:17 +0800245 regmap_update_bits(engine->regs, SUN4I_BACKEND_ATTCTL_REG0(i),
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100246 SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL_MASK,
Maxime Ripardc222f392016-09-19 22:17:50 +0200247 SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL(plane->pipe));
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100248
249 layer->id = i;
Icenowy Zheng7921e142017-05-15 00:30:36 +0800250 planes[i] = &layer->plane;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100251 };
252
Icenowy Zheng7921e142017-05-15 00:30:36 +0800253 return planes;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100254}