sun4i_layer.c 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2015 Free Electrons
 * Copyright (C) 2015 NextThing Co
 *
 * Maxime Ripard <maxime.ripard@free-electrons.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */

#include <drm/drm_atomic_helper.h>
#include <drm/drm_plane_helper.h>
#include <drm/drmP.h>

#include "sun4i_backend.h"
18
#include "sun4i_frontend.h"
19
#include "sun4i_layer.h"
20
#include "sunxi_engine.h"
21

22
struct sun4i_plane_desc {
23 24 25 26
	enum drm_plane_type     type;
	u8                      pipe;
	const uint32_t          *formats;
	uint32_t                nformats;
27
};
28

29 30
static void sun4i_backend_layer_reset(struct drm_plane *plane)
{
31
	struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	struct sun4i_layer_state *state;

	if (plane->state) {
		state = state_to_sun4i_layer_state(plane->state);

		__drm_atomic_helper_plane_destroy_state(&state->state);

		kfree(state);
		plane->state = NULL;
	}

	state = kzalloc(sizeof(*state), GFP_KERNEL);
	if (state) {
		plane->state = &state->state;
		plane->state->plane = plane;
47
		plane->state->zpos = layer->id;
48 49 50 51 52 53
	}
}

static struct drm_plane_state *
sun4i_backend_layer_duplicate_state(struct drm_plane *plane)
{
54
	struct sun4i_layer_state *orig = state_to_sun4i_layer_state(plane->state);
55 56 57 58 59 60 61
	struct sun4i_layer_state *copy;

	copy = kzalloc(sizeof(*copy), GFP_KERNEL);
	if (!copy)
		return NULL;

	__drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
62
	copy->uses_frontend = orig->uses_frontend;
63 64 65 66 67 68 69 70 71 72 73 74 75 76

	return &copy->state;
}

static void sun4i_backend_layer_destroy_state(struct drm_plane *plane,
					      struct drm_plane_state *state)
{
	struct sun4i_layer_state *s_state = state_to_sun4i_layer_state(state);

	__drm_atomic_helper_plane_destroy_state(state);

	kfree(s_state);
}

77 78 79
static void sun4i_backend_layer_atomic_disable(struct drm_plane *plane,
					       struct drm_plane_state *old_state)
{
80
	struct sun4i_layer_state *layer_state = state_to_sun4i_layer_state(old_state);
81
	struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
82
	struct sun4i_backend *backend = layer->backend;
83 84

	sun4i_backend_layer_enable(backend, layer->id, false);
85 86 87 88 89 90 91 92

	if (layer_state->uses_frontend) {
		unsigned long flags;

		spin_lock_irqsave(&backend->frontend_lock, flags);
		backend->frontend_teardown = true;
		spin_unlock_irqrestore(&backend->frontend_lock, flags);
	}
93 94 95 96 97
}

static void sun4i_backend_layer_atomic_update(struct drm_plane *plane,
					      struct drm_plane_state *old_state)
{
98
	struct sun4i_layer_state *layer_state = state_to_sun4i_layer_state(plane->state);
99
	struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
100
	struct sun4i_backend *backend = layer->backend;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	struct sun4i_frontend *frontend = backend->frontend;

	if (layer_state->uses_frontend) {
		sun4i_frontend_init(frontend);
		sun4i_frontend_update_coord(frontend, plane);
		sun4i_frontend_update_buffer(frontend, plane);
		sun4i_frontend_update_formats(frontend, plane,
					      DRM_FORMAT_ARGB8888);
		sun4i_backend_update_layer_frontend(backend, layer->id,
						    DRM_FORMAT_ARGB8888);
		sun4i_frontend_enable(frontend);
	} else {
		sun4i_backend_update_layer_formats(backend, layer->id, plane);
		sun4i_backend_update_layer_buffer(backend, layer->id, plane);
	}
116

117
	sun4i_backend_update_layer_coord(backend, layer->id, plane);
118
	sun4i_backend_update_layer_zpos(backend, layer->id, plane);
119 120 121
	sun4i_backend_layer_enable(backend, layer->id, true);
}

122
static const struct drm_plane_helper_funcs sun4i_backend_layer_helper_funcs = {
123 124 125 126 127
	.atomic_disable	= sun4i_backend_layer_atomic_disable,
	.atomic_update	= sun4i_backend_layer_atomic_update,
};

static const struct drm_plane_funcs sun4i_backend_layer_funcs = {
128 129
	.atomic_destroy_state	= sun4i_backend_layer_destroy_state,
	.atomic_duplicate_state	= sun4i_backend_layer_duplicate_state,
130 131
	.destroy		= drm_plane_cleanup,
	.disable_plane		= drm_atomic_helper_disable_plane,
132
	.reset			= sun4i_backend_layer_reset,
133 134 135
	.update_plane		= drm_atomic_helper_update_plane,
};

136
static const uint32_t sun4i_backend_layer_formats_primary[] = {
137
	DRM_FORMAT_ARGB8888,
138
	DRM_FORMAT_RGB888,
M
Maxime Ripard 已提交
139
	DRM_FORMAT_RGB565,
140
	DRM_FORMAT_XRGB8888,
141 142 143 144
};

static const uint32_t sun4i_backend_layer_formats_overlay[] = {
	DRM_FORMAT_ARGB8888,
M
Maxime Ripard 已提交
145 146 147 148
	DRM_FORMAT_ARGB4444,
	DRM_FORMAT_ARGB1555,
	DRM_FORMAT_RGBA5551,
	DRM_FORMAT_RGBA4444,
149
	DRM_FORMAT_RGB888,
M
Maxime Ripard 已提交
150
	DRM_FORMAT_RGB565,
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	DRM_FORMAT_XRGB8888,
};

static const struct sun4i_plane_desc sun4i_backend_planes[] = {
	{
		.type = DRM_PLANE_TYPE_PRIMARY,
		.pipe = 0,
		.formats = sun4i_backend_layer_formats_primary,
		.nformats = ARRAY_SIZE(sun4i_backend_layer_formats_primary),
	},
	{
		.type = DRM_PLANE_TYPE_OVERLAY,
		.pipe = 1,
		.formats = sun4i_backend_layer_formats_overlay,
		.nformats = ARRAY_SIZE(sun4i_backend_layer_formats_overlay),
	},
167 168 169
};

static struct sun4i_layer *sun4i_layer_init_one(struct drm_device *drm,
170
						struct sun4i_backend *backend,
171
						const struct sun4i_plane_desc *plane)
172 173 174 175 176 177 178 179
{
	struct sun4i_layer *layer;
	int ret;

	layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
	if (!layer)
		return ERR_PTR(-ENOMEM);

180 181
	/* possible crtcs are set later */
	ret = drm_universal_plane_init(drm, &layer->plane, 0,
182
				       &sun4i_backend_layer_funcs,
183
				       plane->formats, plane->nformats,
184
				       NULL, plane->type, NULL);
185 186 187 188 189 190 191
	if (ret) {
		dev_err(drm->dev, "Couldn't initialize layer\n");
		return ERR_PTR(ret);
	}

	drm_plane_helper_add(&layer->plane,
			     &sun4i_backend_layer_helper_funcs);
192
	layer->backend = backend;
193 194 195 196

	return layer;
}

197
struct drm_plane **sun4i_layers_init(struct drm_device *drm,
198
				     struct sunxi_engine *engine)
199
{
200
	struct drm_plane **planes;
201
	struct sun4i_backend *backend = engine_to_sun4i_backend(engine);
202 203
	int i;

204 205
	/* We need to have a sentinel at the need, hence the overallocation */
	planes = devm_kcalloc(drm->dev, SUN4I_BACKEND_NUM_LAYERS + 1,
206 207
			      sizeof(*planes), GFP_KERNEL);
	if (!planes)
208 209
		return ERR_PTR(-ENOMEM);

210 211
	for (i = 0; i < ARRAY_SIZE(sun4i_backend_planes); i++) {
		const struct sun4i_plane_desc *plane = &sun4i_backend_planes[i];
212
		struct sun4i_layer *layer;
213

214
		layer = sun4i_layer_init_one(drm, backend, plane);
215 216 217 218 219 220
		if (IS_ERR(layer)) {
			dev_err(drm->dev, "Couldn't initialize %s plane\n",
				i ? "overlay" : "primary");
			return ERR_CAST(layer);
		};

221 222
		drm_plane_create_zpos_immutable_property(&layer->plane, i);

223
		layer->id = i;
224
		planes[i] = &layer->plane;
225 226
	};

227
	return planes;
228
}