sun4i_layer.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_XRGB8888,
  66. };
  67. static const uint32_t sun4i_backend_layer_formats_overlay[] = {
  68. DRM_FORMAT_ARGB8888,
  69. DRM_FORMAT_RGB888,
  70. DRM_FORMAT_XRGB8888,
  71. };
  72. static const struct sun4i_plane_desc sun4i_backend_planes[] = {
  73. {
  74. .type = DRM_PLANE_TYPE_PRIMARY,
  75. .pipe = 0,
  76. .formats = sun4i_backend_layer_formats_primary,
  77. .nformats = ARRAY_SIZE(sun4i_backend_layer_formats_primary),
  78. },
  79. {
  80. .type = DRM_PLANE_TYPE_OVERLAY,
  81. .pipe = 1,
  82. .formats = sun4i_backend_layer_formats_overlay,
  83. .nformats = ARRAY_SIZE(sun4i_backend_layer_formats_overlay),
  84. },
  85. };
  86. static struct sun4i_layer *sun4i_layer_init_one(struct drm_device *drm,
  87. const struct sun4i_plane_desc *plane)
  88. {
  89. struct sun4i_drv *drv = drm->dev_private;
  90. struct sun4i_layer *layer;
  91. int ret;
  92. layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
  93. if (!layer)
  94. return ERR_PTR(-ENOMEM);
  95. ret = drm_universal_plane_init(drm, &layer->plane, BIT(0),
  96. &sun4i_backend_layer_funcs,
  97. plane->formats, plane->nformats,
  98. plane->type, NULL);
  99. if (ret) {
  100. dev_err(drm->dev, "Couldn't initialize layer\n");
  101. return ERR_PTR(ret);
  102. }
  103. drm_plane_helper_add(&layer->plane,
  104. &sun4i_backend_layer_helper_funcs);
  105. layer->drv = drv;
  106. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  107. drv->primary = &layer->plane;
  108. return layer;
  109. }
  110. struct sun4i_layer **sun4i_layers_init(struct drm_device *drm)
  111. {
  112. struct sun4i_drv *drv = drm->dev_private;
  113. struct sun4i_layer **layers;
  114. int i;
  115. layers = devm_kcalloc(drm->dev, ARRAY_SIZE(sun4i_backend_planes),
  116. sizeof(**layers), GFP_KERNEL);
  117. if (!layers)
  118. return ERR_PTR(-ENOMEM);
  119. /*
  120. * The hardware is a bit unusual here.
  121. *
  122. * Even though it supports 4 layers, it does the composition
  123. * in two separate steps.
  124. *
  125. * The first one is assigning a layer to one of its two
  126. * pipes. If more that 1 layer is assigned to the same pipe,
  127. * and if pixels overlaps, the pipe will take the pixel from
  128. * the layer with the highest priority.
  129. *
  130. * The second step is the actual alpha blending, that takes
  131. * the two pipes as input, and uses the eventual alpha
  132. * component to do the transparency between the two.
  133. *
  134. * This two steps scenario makes us unable to guarantee a
  135. * robust alpha blending between the 4 layers in all
  136. * situations. So we just expose two layers, one per pipe. On
  137. * SoCs that support it, sprites could fill the need for more
  138. * layers.
  139. */
  140. for (i = 0; i < ARRAY_SIZE(sun4i_backend_planes); i++) {
  141. const struct sun4i_plane_desc *plane = &sun4i_backend_planes[i];
  142. struct sun4i_layer *layer = layers[i];
  143. layer = sun4i_layer_init_one(drm, plane);
  144. if (IS_ERR(layer)) {
  145. dev_err(drm->dev, "Couldn't initialize %s plane\n",
  146. i ? "overlay" : "primary");
  147. return ERR_CAST(layer);
  148. };
  149. DRM_DEBUG_DRIVER("Assigning %s plane to pipe %d\n",
  150. i ? "overlay" : "primary", plane->pipe);
  151. regmap_update_bits(drv->backend->regs, SUN4I_BACKEND_ATTCTL_REG0(i),
  152. SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL_MASK,
  153. SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL(plane->pipe));
  154. layer->id = i;
  155. };
  156. return layers;
  157. }