Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 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_crtc.h> |
| 15 | #include <drm/drm_plane_helper.h> |
| 16 | #include <drm/drmP.h> |
| 17 | |
| 18 | #include "sun4i_backend.h" |
| 19 | #include "sun4i_drv.h" |
| 20 | #include "sun4i_layer.h" |
| 21 | |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 22 | struct sun4i_plane_desc { |
| 23 | enum drm_plane_type type; |
| 24 | u8 pipe; |
| 25 | const uint32_t *formats; |
| 26 | uint32_t nformats; |
| 27 | }; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 28 | |
| 29 | static int sun4i_backend_layer_atomic_check(struct drm_plane *plane, |
| 30 | struct drm_plane_state *state) |
| 31 | { |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static 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); |
| 39 | struct sun4i_drv *drv = layer->drv; |
| 40 | struct sun4i_backend *backend = drv->backend; |
| 41 | |
| 42 | sun4i_backend_layer_enable(backend, layer->id, false); |
| 43 | } |
| 44 | |
| 45 | static void sun4i_backend_layer_atomic_update(struct drm_plane *plane, |
| 46 | struct drm_plane_state *old_state) |
| 47 | { |
| 48 | struct sun4i_layer *layer = plane_to_sun4i_layer(plane); |
| 49 | struct sun4i_drv *drv = layer->drv; |
| 50 | struct sun4i_backend *backend = drv->backend; |
| 51 | |
| 52 | sun4i_backend_update_layer_coord(backend, layer->id, plane); |
| 53 | sun4i_backend_update_layer_formats(backend, layer->id, plane); |
| 54 | sun4i_backend_update_layer_buffer(backend, layer->id, plane); |
| 55 | sun4i_backend_layer_enable(backend, layer->id, true); |
| 56 | } |
| 57 | |
| 58 | static struct drm_plane_helper_funcs sun4i_backend_layer_helper_funcs = { |
| 59 | .atomic_check = sun4i_backend_layer_atomic_check, |
| 60 | .atomic_disable = sun4i_backend_layer_atomic_disable, |
| 61 | .atomic_update = sun4i_backend_layer_atomic_update, |
| 62 | }; |
| 63 | |
| 64 | static const struct drm_plane_funcs sun4i_backend_layer_funcs = { |
| 65 | .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, |
| 66 | .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, |
| 67 | .destroy = drm_plane_cleanup, |
| 68 | .disable_plane = drm_atomic_helper_disable_plane, |
| 69 | .reset = drm_atomic_helper_plane_reset, |
| 70 | .update_plane = drm_atomic_helper_update_plane, |
| 71 | }; |
| 72 | |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 73 | static const uint32_t sun4i_backend_layer_formats_primary[] = { |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 74 | DRM_FORMAT_ARGB8888, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 75 | DRM_FORMAT_RGB888, |
Maxime Ripard | 47d7fbb | 2016-10-18 10:46:14 +0200 | [diff] [blame] | 76 | DRM_FORMAT_RGB565, |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 77 | DRM_FORMAT_XRGB8888, |
| 78 | }; |
| 79 | |
| 80 | static const uint32_t sun4i_backend_layer_formats_overlay[] = { |
| 81 | DRM_FORMAT_ARGB8888, |
Maxime Ripard | 47d7fbb | 2016-10-18 10:46:14 +0200 | [diff] [blame] | 82 | DRM_FORMAT_ARGB4444, |
| 83 | DRM_FORMAT_ARGB1555, |
| 84 | DRM_FORMAT_RGBA5551, |
| 85 | DRM_FORMAT_RGBA4444, |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 86 | DRM_FORMAT_RGB888, |
Maxime Ripard | 47d7fbb | 2016-10-18 10:46:14 +0200 | [diff] [blame] | 87 | DRM_FORMAT_RGB565, |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 88 | DRM_FORMAT_XRGB8888, |
| 89 | }; |
| 90 | |
| 91 | static const struct sun4i_plane_desc sun4i_backend_planes[] = { |
| 92 | { |
| 93 | .type = DRM_PLANE_TYPE_PRIMARY, |
| 94 | .pipe = 0, |
| 95 | .formats = sun4i_backend_layer_formats_primary, |
| 96 | .nformats = ARRAY_SIZE(sun4i_backend_layer_formats_primary), |
| 97 | }, |
| 98 | { |
| 99 | .type = DRM_PLANE_TYPE_OVERLAY, |
| 100 | .pipe = 1, |
| 101 | .formats = sun4i_backend_layer_formats_overlay, |
| 102 | .nformats = ARRAY_SIZE(sun4i_backend_layer_formats_overlay), |
| 103 | }, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | static struct sun4i_layer *sun4i_layer_init_one(struct drm_device *drm, |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 107 | const struct sun4i_plane_desc *plane) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 108 | { |
| 109 | struct sun4i_drv *drv = drm->dev_private; |
| 110 | struct sun4i_layer *layer; |
| 111 | int ret; |
| 112 | |
| 113 | layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL); |
| 114 | if (!layer) |
| 115 | return ERR_PTR(-ENOMEM); |
| 116 | |
Chen-Yu Tsai | a5154a4 | 2017-02-23 16:05:39 +0800 | [diff] [blame^] | 117 | /* possible crtcs are set later */ |
| 118 | ret = drm_universal_plane_init(drm, &layer->plane, 0, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 119 | &sun4i_backend_layer_funcs, |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 120 | plane->formats, plane->nformats, |
| 121 | plane->type, NULL); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 122 | if (ret) { |
| 123 | dev_err(drm->dev, "Couldn't initialize layer\n"); |
| 124 | return ERR_PTR(ret); |
| 125 | } |
| 126 | |
| 127 | drm_plane_helper_add(&layer->plane, |
| 128 | &sun4i_backend_layer_helper_funcs); |
| 129 | layer->drv = drv; |
| 130 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 131 | return layer; |
| 132 | } |
| 133 | |
| 134 | struct sun4i_layer **sun4i_layers_init(struct drm_device *drm) |
| 135 | { |
| 136 | struct sun4i_drv *drv = drm->dev_private; |
| 137 | struct sun4i_layer **layers; |
| 138 | int i; |
| 139 | |
Chen-Yu Tsai | 2b2c22b | 2017-02-23 16:05:35 +0800 | [diff] [blame] | 140 | layers = devm_kcalloc(drm->dev, ARRAY_SIZE(sun4i_backend_planes) + 1, |
Chen-Yu Tsai | f43fbe7 | 2017-02-17 11:13:27 +0800 | [diff] [blame] | 141 | sizeof(*layers), GFP_KERNEL); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 142 | if (!layers) |
| 143 | return ERR_PTR(-ENOMEM); |
| 144 | |
| 145 | /* |
| 146 | * The hardware is a bit unusual here. |
| 147 | * |
| 148 | * Even though it supports 4 layers, it does the composition |
| 149 | * in two separate steps. |
| 150 | * |
| 151 | * The first one is assigning a layer to one of its two |
| 152 | * pipes. If more that 1 layer is assigned to the same pipe, |
| 153 | * and if pixels overlaps, the pipe will take the pixel from |
| 154 | * the layer with the highest priority. |
| 155 | * |
| 156 | * The second step is the actual alpha blending, that takes |
| 157 | * the two pipes as input, and uses the eventual alpha |
| 158 | * component to do the transparency between the two. |
| 159 | * |
| 160 | * This two steps scenario makes us unable to guarantee a |
| 161 | * robust alpha blending between the 4 layers in all |
| 162 | * situations. So we just expose two layers, one per pipe. On |
| 163 | * SoCs that support it, sprites could fill the need for more |
| 164 | * layers. |
| 165 | */ |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 166 | for (i = 0; i < ARRAY_SIZE(sun4i_backend_planes); i++) { |
| 167 | const struct sun4i_plane_desc *plane = &sun4i_backend_planes[i]; |
Chen-Yu Tsai | 8a16469 | 2017-02-17 11:13:28 +0800 | [diff] [blame] | 168 | struct sun4i_layer *layer; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 169 | |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 170 | layer = sun4i_layer_init_one(drm, plane); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 171 | if (IS_ERR(layer)) { |
| 172 | dev_err(drm->dev, "Couldn't initialize %s plane\n", |
| 173 | i ? "overlay" : "primary"); |
| 174 | return ERR_CAST(layer); |
| 175 | }; |
| 176 | |
| 177 | DRM_DEBUG_DRIVER("Assigning %s plane to pipe %d\n", |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 178 | i ? "overlay" : "primary", plane->pipe); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 179 | regmap_update_bits(drv->backend->regs, SUN4I_BACKEND_ATTCTL_REG0(i), |
| 180 | SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL_MASK, |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 181 | SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL(plane->pipe)); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 182 | |
| 183 | layer->id = i; |
Chen-Yu Tsai | 903795d | 2017-02-17 11:13:29 +0800 | [diff] [blame] | 184 | layers[i] = layer; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | return layers; |
| 188 | } |