blob: e1f03e1cc0ac6ed461571202e9a6fb736f17122c [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"
Icenowy Zheng7921e142017-05-15 00:30:36 +080019#include "sun4i_crtc.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010020#include "sun4i_layer.h"
21
Maxime Ripardc222f392016-09-19 22:17:50 +020022struct sun4i_plane_desc {
23 enum drm_plane_type type;
24 u8 pipe;
25 const uint32_t *formats;
26 uint32_t nformats;
27};
Maxime Ripard9026e0d2015-10-29 09:36:23 +010028
29static int sun4i_backend_layer_atomic_check(struct drm_plane *plane,
30 struct drm_plane_state *state)
31{
32 return 0;
33}
34
35static void sun4i_backend_layer_atomic_disable(struct drm_plane *plane,
36 struct drm_plane_state *old_state)
37{
38 struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
Chen-Yu Tsaiace6c092017-02-23 16:05:42 +080039 struct sun4i_backend *backend = layer->backend;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010040
41 sun4i_backend_layer_enable(backend, layer->id, false);
42}
43
44static void sun4i_backend_layer_atomic_update(struct drm_plane *plane,
45 struct drm_plane_state *old_state)
46{
47 struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
Chen-Yu Tsaiace6c092017-02-23 16:05:42 +080048 struct sun4i_backend *backend = layer->backend;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010049
50 sun4i_backend_update_layer_coord(backend, layer->id, plane);
51 sun4i_backend_update_layer_formats(backend, layer->id, plane);
52 sun4i_backend_update_layer_buffer(backend, layer->id, plane);
53 sun4i_backend_layer_enable(backend, layer->id, true);
54}
55
56static struct drm_plane_helper_funcs sun4i_backend_layer_helper_funcs = {
57 .atomic_check = sun4i_backend_layer_atomic_check,
58 .atomic_disable = sun4i_backend_layer_atomic_disable,
59 .atomic_update = sun4i_backend_layer_atomic_update,
60};
61
62static const struct drm_plane_funcs sun4i_backend_layer_funcs = {
63 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
64 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
65 .destroy = drm_plane_cleanup,
66 .disable_plane = drm_atomic_helper_disable_plane,
67 .reset = drm_atomic_helper_plane_reset,
68 .update_plane = drm_atomic_helper_update_plane,
69};
70
Maxime Ripardc222f392016-09-19 22:17:50 +020071static const uint32_t sun4i_backend_layer_formats_primary[] = {
Maxime Ripard9026e0d2015-10-29 09:36:23 +010072 DRM_FORMAT_ARGB8888,
Maxime Ripard9026e0d2015-10-29 09:36:23 +010073 DRM_FORMAT_RGB888,
Maxime Ripard47d7fbb2016-10-18 10:46:14 +020074 DRM_FORMAT_RGB565,
Maxime Ripardc222f392016-09-19 22:17:50 +020075 DRM_FORMAT_XRGB8888,
76};
77
78static const uint32_t sun4i_backend_layer_formats_overlay[] = {
79 DRM_FORMAT_ARGB8888,
Maxime Ripard47d7fbb2016-10-18 10:46:14 +020080 DRM_FORMAT_ARGB4444,
81 DRM_FORMAT_ARGB1555,
82 DRM_FORMAT_RGBA5551,
83 DRM_FORMAT_RGBA4444,
Maxime Ripardc222f392016-09-19 22:17:50 +020084 DRM_FORMAT_RGB888,
Maxime Ripard47d7fbb2016-10-18 10:46:14 +020085 DRM_FORMAT_RGB565,
Maxime Ripardc222f392016-09-19 22:17:50 +020086 DRM_FORMAT_XRGB8888,
87};
88
89static const struct sun4i_plane_desc sun4i_backend_planes[] = {
90 {
91 .type = DRM_PLANE_TYPE_PRIMARY,
92 .pipe = 0,
93 .formats = sun4i_backend_layer_formats_primary,
94 .nformats = ARRAY_SIZE(sun4i_backend_layer_formats_primary),
95 },
96 {
97 .type = DRM_PLANE_TYPE_OVERLAY,
98 .pipe = 1,
99 .formats = sun4i_backend_layer_formats_overlay,
100 .nformats = ARRAY_SIZE(sun4i_backend_layer_formats_overlay),
101 },
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100102};
103
104static struct sun4i_layer *sun4i_layer_init_one(struct drm_device *drm,
Chen-Yu Tsaia0a68fb2017-03-09 18:05:29 +0800105 struct sun4i_backend *backend,
Maxime Ripardc222f392016-09-19 22:17:50 +0200106 const struct sun4i_plane_desc *plane)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100107{
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100108 struct sun4i_layer *layer;
109 int ret;
110
111 layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
112 if (!layer)
113 return ERR_PTR(-ENOMEM);
114
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800115 /* possible crtcs are set later */
116 ret = drm_universal_plane_init(drm, &layer->plane, 0,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100117 &sun4i_backend_layer_funcs,
Maxime Ripardc222f392016-09-19 22:17:50 +0200118 plane->formats, plane->nformats,
119 plane->type, NULL);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100120 if (ret) {
121 dev_err(drm->dev, "Couldn't initialize layer\n");
122 return ERR_PTR(ret);
123 }
124
125 drm_plane_helper_add(&layer->plane,
126 &sun4i_backend_layer_helper_funcs);
Chen-Yu Tsaia0a68fb2017-03-09 18:05:29 +0800127 layer->backend = backend;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100128
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100129 return layer;
130}
131
Icenowy Zheng7921e142017-05-15 00:30:36 +0800132struct drm_plane **sun4i_layers_init(struct drm_device *drm,
133 struct sun4i_crtc *crtc)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100134{
Icenowy Zheng7921e142017-05-15 00:30:36 +0800135 struct drm_plane **planes;
136 struct sun4i_backend *backend = crtc->backend;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100137 int i;
138
Icenowy Zheng7921e142017-05-15 00:30:36 +0800139 planes = devm_kcalloc(drm->dev, ARRAY_SIZE(sun4i_backend_planes) + 1,
140 sizeof(*planes), GFP_KERNEL);
141 if (!planes)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100142 return ERR_PTR(-ENOMEM);
143
144 /*
145 * The hardware is a bit unusual here.
146 *
147 * Even though it supports 4 layers, it does the composition
148 * in two separate steps.
149 *
150 * The first one is assigning a layer to one of its two
151 * pipes. If more that 1 layer is assigned to the same pipe,
152 * and if pixels overlaps, the pipe will take the pixel from
153 * the layer with the highest priority.
154 *
155 * The second step is the actual alpha blending, that takes
156 * the two pipes as input, and uses the eventual alpha
157 * component to do the transparency between the two.
158 *
159 * This two steps scenario makes us unable to guarantee a
160 * robust alpha blending between the 4 layers in all
161 * situations. So we just expose two layers, one per pipe. On
162 * SoCs that support it, sprites could fill the need for more
163 * layers.
164 */
Maxime Ripardc222f392016-09-19 22:17:50 +0200165 for (i = 0; i < ARRAY_SIZE(sun4i_backend_planes); i++) {
166 const struct sun4i_plane_desc *plane = &sun4i_backend_planes[i];
Chen-Yu Tsai8a164692017-02-17 11:13:28 +0800167 struct sun4i_layer *layer;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100168
Chen-Yu Tsaia0a68fb2017-03-09 18:05:29 +0800169 layer = sun4i_layer_init_one(drm, backend, plane);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100170 if (IS_ERR(layer)) {
171 dev_err(drm->dev, "Couldn't initialize %s plane\n",
172 i ? "overlay" : "primary");
173 return ERR_CAST(layer);
174 };
175
176 DRM_DEBUG_DRIVER("Assigning %s plane to pipe %d\n",
Maxime Ripardc222f392016-09-19 22:17:50 +0200177 i ? "overlay" : "primary", plane->pipe);
Chen-Yu Tsaia0a68fb2017-03-09 18:05:29 +0800178 regmap_update_bits(backend->regs, SUN4I_BACKEND_ATTCTL_REG0(i),
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100179 SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL_MASK,
Maxime Ripardc222f392016-09-19 22:17:50 +0200180 SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL(plane->pipe));
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100181
182 layer->id = i;
Icenowy Zheng7921e142017-05-15 00:30:36 +0800183 planes[i] = &layer->plane;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100184 };
185
Icenowy Zheng7921e142017-05-15 00:30:36 +0800186 return planes;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100187}