sun4i_layer.c 5.5 KB

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