sun4i_layer.c 4.7 KB

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