blob: f26bde5b9117cabaa6d0cf08af9471cee80f45e8 [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>
14#include <drm/drm_crtc.h>
15#include <drm/drm_plane_helper.h>
16#include <drm/drmP.h>
17
18#include "sun4i_backend.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010019#include "sun4i_layer.h"
20
Maxime Ripardc222f392016-09-19 22:17:50 +020021struct sun4i_plane_desc {
22 enum drm_plane_type type;
23 u8 pipe;
24 const uint32_t *formats;
25 uint32_t nformats;
26};
Maxime Ripard9026e0d2015-10-29 09:36:23 +010027
28static int sun4i_backend_layer_atomic_check(struct drm_plane *plane,
29 struct drm_plane_state *state)
30{
31 return 0;
32}
33
34static void sun4i_backend_layer_atomic_disable(struct drm_plane *plane,
35 struct drm_plane_state *old_state)
36{
37 struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
Chen-Yu Tsaiace6c092017-02-23 16:05:42 +080038 struct sun4i_backend *backend = layer->backend;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010039
40 sun4i_backend_layer_enable(backend, layer->id, false);
41}
42
43static void sun4i_backend_layer_atomic_update(struct drm_plane *plane,
44 struct drm_plane_state *old_state)
45{
46 struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
Chen-Yu Tsaiace6c092017-02-23 16:05:42 +080047 struct sun4i_backend *backend = layer->backend;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010048
49 sun4i_backend_update_layer_coord(backend, layer->id, plane);
50 sun4i_backend_update_layer_formats(backend, layer->id, plane);
51 sun4i_backend_update_layer_buffer(backend, layer->id, plane);
52 sun4i_backend_layer_enable(backend, layer->id, true);
53}
54
55static struct drm_plane_helper_funcs sun4i_backend_layer_helper_funcs = {
56 .atomic_check = sun4i_backend_layer_atomic_check,
57 .atomic_disable = sun4i_backend_layer_atomic_disable,
58 .atomic_update = sun4i_backend_layer_atomic_update,
59};
60
61static const struct drm_plane_funcs sun4i_backend_layer_funcs = {
62 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
63 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
64 .destroy = drm_plane_cleanup,
65 .disable_plane = drm_atomic_helper_disable_plane,
66 .reset = drm_atomic_helper_plane_reset,
67 .update_plane = drm_atomic_helper_update_plane,
68};
69
Maxime Ripardc222f392016-09-19 22:17:50 +020070static const uint32_t sun4i_backend_layer_formats_primary[] = {
Maxime Ripard9026e0d2015-10-29 09:36:23 +010071 DRM_FORMAT_ARGB8888,
Maxime Ripard9026e0d2015-10-29 09:36:23 +010072 DRM_FORMAT_RGB888,
Maxime Ripard47d7fbb2016-10-18 10:46:14 +020073 DRM_FORMAT_RGB565,
Maxime Ripardc222f392016-09-19 22:17:50 +020074 DRM_FORMAT_XRGB8888,
75};
76
77static const uint32_t sun4i_backend_layer_formats_overlay[] = {
78 DRM_FORMAT_ARGB8888,
Maxime Ripard47d7fbb2016-10-18 10:46:14 +020079 DRM_FORMAT_ARGB4444,
80 DRM_FORMAT_ARGB1555,
81 DRM_FORMAT_RGBA5551,
82 DRM_FORMAT_RGBA4444,
Maxime Ripardc222f392016-09-19 22:17:50 +020083 DRM_FORMAT_RGB888,
Maxime Ripard47d7fbb2016-10-18 10:46:14 +020084 DRM_FORMAT_RGB565,
Maxime Ripardc222f392016-09-19 22:17:50 +020085 DRM_FORMAT_XRGB8888,
86};
87
88static const struct sun4i_plane_desc sun4i_backend_planes[] = {
89 {
90 .type = DRM_PLANE_TYPE_PRIMARY,
91 .pipe = 0,
92 .formats = sun4i_backend_layer_formats_primary,
93 .nformats = ARRAY_SIZE(sun4i_backend_layer_formats_primary),
94 },
95 {
96 .type = DRM_PLANE_TYPE_OVERLAY,
97 .pipe = 1,
98 .formats = sun4i_backend_layer_formats_overlay,
99 .nformats = ARRAY_SIZE(sun4i_backend_layer_formats_overlay),
100 },
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100101};
102
103static struct sun4i_layer *sun4i_layer_init_one(struct drm_device *drm,
Chen-Yu Tsaia0a68fb2017-03-09 18:05:29 +0800104 struct sun4i_backend *backend,
Maxime Ripardc222f392016-09-19 22:17:50 +0200105 const struct sun4i_plane_desc *plane)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100106{
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100107 struct sun4i_layer *layer;
108 int ret;
109
110 layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
111 if (!layer)
112 return ERR_PTR(-ENOMEM);
113
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800114 /* possible crtcs are set later */
115 ret = drm_universal_plane_init(drm, &layer->plane, 0,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100116 &sun4i_backend_layer_funcs,
Maxime Ripardc222f392016-09-19 22:17:50 +0200117 plane->formats, plane->nformats,
118 plane->type, NULL);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100119 if (ret) {
120 dev_err(drm->dev, "Couldn't initialize layer\n");
121 return ERR_PTR(ret);
122 }
123
124 drm_plane_helper_add(&layer->plane,
125 &sun4i_backend_layer_helper_funcs);
Chen-Yu Tsaia0a68fb2017-03-09 18:05:29 +0800126 layer->backend = backend;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100127
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100128 return layer;
129}
130
Chen-Yu Tsaia0a68fb2017-03-09 18:05:29 +0800131struct sun4i_layer **sun4i_layers_init(struct drm_device *drm,
132 struct sun4i_backend *backend)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100133{
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100134 struct sun4i_layer **layers;
135 int i;
136
Chen-Yu Tsai2b2c22b2017-02-23 16:05:35 +0800137 layers = devm_kcalloc(drm->dev, ARRAY_SIZE(sun4i_backend_planes) + 1,
Chen-Yu Tsaif43fbe72017-02-17 11:13:27 +0800138 sizeof(*layers), GFP_KERNEL);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100139 if (!layers)
140 return ERR_PTR(-ENOMEM);
141
142 /*
143 * The hardware is a bit unusual here.
144 *
145 * Even though it supports 4 layers, it does the composition
146 * in two separate steps.
147 *
148 * The first one is assigning a layer to one of its two
149 * pipes. If more that 1 layer is assigned to the same pipe,
150 * and if pixels overlaps, the pipe will take the pixel from
151 * the layer with the highest priority.
152 *
153 * The second step is the actual alpha blending, that takes
154 * the two pipes as input, and uses the eventual alpha
155 * component to do the transparency between the two.
156 *
157 * This two steps scenario makes us unable to guarantee a
158 * robust alpha blending between the 4 layers in all
159 * situations. So we just expose two layers, one per pipe. On
160 * SoCs that support it, sprites could fill the need for more
161 * layers.
162 */
Maxime Ripardc222f392016-09-19 22:17:50 +0200163 for (i = 0; i < ARRAY_SIZE(sun4i_backend_planes); i++) {
164 const struct sun4i_plane_desc *plane = &sun4i_backend_planes[i];
Chen-Yu Tsai8a164692017-02-17 11:13:28 +0800165 struct sun4i_layer *layer;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100166
Chen-Yu Tsaia0a68fb2017-03-09 18:05:29 +0800167 layer = sun4i_layer_init_one(drm, backend, plane);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100168 if (IS_ERR(layer)) {
169 dev_err(drm->dev, "Couldn't initialize %s plane\n",
170 i ? "overlay" : "primary");
171 return ERR_CAST(layer);
172 };
173
174 DRM_DEBUG_DRIVER("Assigning %s plane to pipe %d\n",
Maxime Ripardc222f392016-09-19 22:17:50 +0200175 i ? "overlay" : "primary", plane->pipe);
Chen-Yu Tsaia0a68fb2017-03-09 18:05:29 +0800176 regmap_update_bits(backend->regs, SUN4I_BACKEND_ATTCTL_REG0(i),
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100177 SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL_MASK,
Maxime Ripardc222f392016-09-19 22:17:50 +0200178 SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL(plane->pipe));
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100179
180 layer->id = i;
Chen-Yu Tsai903795d2017-02-17 11:13:29 +0800181 layers[i] = layer;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100182 };
183
184 return layers;
185}