sun4i_layer.c 5.3 KB

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