sun4i_layer.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_frontend.h"
  17. #include "sun4i_layer.h"
  18. #include "sunxi_engine.h"
  19. static void sun4i_backend_layer_reset(struct drm_plane *plane)
  20. {
  21. struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
  22. struct sun4i_layer_state *state;
  23. if (plane->state) {
  24. state = state_to_sun4i_layer_state(plane->state);
  25. __drm_atomic_helper_plane_destroy_state(&state->state);
  26. kfree(state);
  27. plane->state = NULL;
  28. }
  29. state = kzalloc(sizeof(*state), GFP_KERNEL);
  30. if (state) {
  31. plane->state = &state->state;
  32. plane->state->plane = plane;
  33. plane->state->zpos = layer->id;
  34. }
  35. }
  36. static struct drm_plane_state *
  37. sun4i_backend_layer_duplicate_state(struct drm_plane *plane)
  38. {
  39. struct sun4i_layer_state *orig = state_to_sun4i_layer_state(plane->state);
  40. struct sun4i_layer_state *copy;
  41. copy = kzalloc(sizeof(*copy), GFP_KERNEL);
  42. if (!copy)
  43. return NULL;
  44. __drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
  45. copy->uses_frontend = orig->uses_frontend;
  46. return &copy->state;
  47. }
  48. static void sun4i_backend_layer_destroy_state(struct drm_plane *plane,
  49. struct drm_plane_state *state)
  50. {
  51. struct sun4i_layer_state *s_state = state_to_sun4i_layer_state(state);
  52. __drm_atomic_helper_plane_destroy_state(state);
  53. kfree(s_state);
  54. }
  55. static void sun4i_backend_layer_atomic_disable(struct drm_plane *plane,
  56. struct drm_plane_state *old_state)
  57. {
  58. struct sun4i_layer_state *layer_state = state_to_sun4i_layer_state(old_state);
  59. struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
  60. struct sun4i_backend *backend = layer->backend;
  61. sun4i_backend_layer_enable(backend, layer->id, false);
  62. if (layer_state->uses_frontend) {
  63. unsigned long flags;
  64. spin_lock_irqsave(&backend->frontend_lock, flags);
  65. backend->frontend_teardown = true;
  66. spin_unlock_irqrestore(&backend->frontend_lock, flags);
  67. }
  68. }
  69. static void sun4i_backend_layer_atomic_update(struct drm_plane *plane,
  70. struct drm_plane_state *old_state)
  71. {
  72. struct sun4i_layer_state *layer_state = state_to_sun4i_layer_state(plane->state);
  73. struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
  74. struct sun4i_backend *backend = layer->backend;
  75. struct sun4i_frontend *frontend = backend->frontend;
  76. if (layer_state->uses_frontend) {
  77. sun4i_frontend_init(frontend);
  78. sun4i_frontend_update_coord(frontend, plane);
  79. sun4i_frontend_update_buffer(frontend, plane);
  80. sun4i_frontend_update_formats(frontend, plane,
  81. DRM_FORMAT_ARGB8888);
  82. sun4i_backend_update_layer_frontend(backend, layer->id,
  83. DRM_FORMAT_ARGB8888);
  84. sun4i_frontend_enable(frontend);
  85. } else {
  86. sun4i_backend_update_layer_formats(backend, layer->id, plane);
  87. sun4i_backend_update_layer_buffer(backend, layer->id, plane);
  88. }
  89. sun4i_backend_update_layer_coord(backend, layer->id, plane);
  90. sun4i_backend_update_layer_zpos(backend, layer->id, plane);
  91. sun4i_backend_layer_enable(backend, layer->id, true);
  92. }
  93. static const struct drm_plane_helper_funcs sun4i_backend_layer_helper_funcs = {
  94. .atomic_disable = sun4i_backend_layer_atomic_disable,
  95. .atomic_update = sun4i_backend_layer_atomic_update,
  96. };
  97. static const struct drm_plane_funcs sun4i_backend_layer_funcs = {
  98. .atomic_destroy_state = sun4i_backend_layer_destroy_state,
  99. .atomic_duplicate_state = sun4i_backend_layer_duplicate_state,
  100. .destroy = drm_plane_cleanup,
  101. .disable_plane = drm_atomic_helper_disable_plane,
  102. .reset = sun4i_backend_layer_reset,
  103. .update_plane = drm_atomic_helper_update_plane,
  104. };
  105. static const uint32_t sun4i_backend_layer_formats[] = {
  106. DRM_FORMAT_ARGB8888,
  107. DRM_FORMAT_ARGB4444,
  108. DRM_FORMAT_ARGB1555,
  109. DRM_FORMAT_RGBA5551,
  110. DRM_FORMAT_RGBA4444,
  111. DRM_FORMAT_RGB888,
  112. DRM_FORMAT_RGB565,
  113. DRM_FORMAT_UYVY,
  114. DRM_FORMAT_VYUY,
  115. DRM_FORMAT_XRGB8888,
  116. DRM_FORMAT_YUYV,
  117. DRM_FORMAT_YVYU,
  118. };
  119. static struct sun4i_layer *sun4i_layer_init_one(struct drm_device *drm,
  120. struct sun4i_backend *backend,
  121. enum drm_plane_type type)
  122. {
  123. struct sun4i_layer *layer;
  124. int ret;
  125. layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
  126. if (!layer)
  127. return ERR_PTR(-ENOMEM);
  128. /* possible crtcs are set later */
  129. ret = drm_universal_plane_init(drm, &layer->plane, 0,
  130. &sun4i_backend_layer_funcs,
  131. sun4i_backend_layer_formats,
  132. ARRAY_SIZE(sun4i_backend_layer_formats),
  133. NULL, type, NULL);
  134. if (ret) {
  135. dev_err(drm->dev, "Couldn't initialize layer\n");
  136. return ERR_PTR(ret);
  137. }
  138. drm_plane_helper_add(&layer->plane,
  139. &sun4i_backend_layer_helper_funcs);
  140. layer->backend = backend;
  141. drm_plane_create_zpos_property(&layer->plane, 0, 0,
  142. SUN4I_BACKEND_NUM_LAYERS - 1);
  143. return layer;
  144. }
  145. struct drm_plane **sun4i_layers_init(struct drm_device *drm,
  146. struct sunxi_engine *engine)
  147. {
  148. struct drm_plane **planes;
  149. struct sun4i_backend *backend = engine_to_sun4i_backend(engine);
  150. int i;
  151. /* We need to have a sentinel at the need, hence the overallocation */
  152. planes = devm_kcalloc(drm->dev, SUN4I_BACKEND_NUM_LAYERS + 1,
  153. sizeof(*planes), GFP_KERNEL);
  154. if (!planes)
  155. return ERR_PTR(-ENOMEM);
  156. for (i = 0; i < SUN4I_BACKEND_NUM_LAYERS; i++) {
  157. enum drm_plane_type type = i ? DRM_PLANE_TYPE_OVERLAY : DRM_PLANE_TYPE_PRIMARY;
  158. struct sun4i_layer *layer;
  159. layer = sun4i_layer_init_one(drm, backend, type);
  160. if (IS_ERR(layer)) {
  161. dev_err(drm->dev, "Couldn't initialize %s plane\n",
  162. i ? "overlay" : "primary");
  163. return ERR_CAST(layer);
  164. };
  165. layer->id = i;
  166. planes[i] = &layer->plane;
  167. };
  168. return planes;
  169. }