sun4i_layer.c 5.5 KB

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