mtk_drm_plane.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. * Author: CK Hu <ck.hu@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/drm_atomic.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_plane_helper.h>
  18. #include "mtk_drm_crtc.h"
  19. #include "mtk_drm_ddp_comp.h"
  20. #include "mtk_drm_drv.h"
  21. #include "mtk_drm_fb.h"
  22. #include "mtk_drm_gem.h"
  23. #include "mtk_drm_plane.h"
  24. static const u32 formats[] = {
  25. DRM_FORMAT_XRGB8888,
  26. DRM_FORMAT_ARGB8888,
  27. DRM_FORMAT_RGB565,
  28. DRM_FORMAT_UYVY,
  29. DRM_FORMAT_YUYV,
  30. };
  31. static void mtk_plane_reset(struct drm_plane *plane)
  32. {
  33. struct mtk_plane_state *state;
  34. if (plane->state) {
  35. __drm_atomic_helper_plane_destroy_state(plane->state);
  36. state = to_mtk_plane_state(plane->state);
  37. memset(state, 0, sizeof(*state));
  38. } else {
  39. state = kzalloc(sizeof(*state), GFP_KERNEL);
  40. if (!state)
  41. return;
  42. plane->state = &state->base;
  43. }
  44. state->base.plane = plane;
  45. state->pending.format = DRM_FORMAT_RGB565;
  46. }
  47. static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
  48. {
  49. struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
  50. struct mtk_plane_state *state;
  51. state = kzalloc(sizeof(*state), GFP_KERNEL);
  52. if (!state)
  53. return NULL;
  54. __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
  55. WARN_ON(state->base.plane != plane);
  56. state->pending = old_state->pending;
  57. return &state->base;
  58. }
  59. static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
  60. struct drm_plane_state *state)
  61. {
  62. __drm_atomic_helper_plane_destroy_state(state);
  63. kfree(to_mtk_plane_state(state));
  64. }
  65. static const struct drm_plane_funcs mtk_plane_funcs = {
  66. .update_plane = drm_atomic_helper_update_plane,
  67. .disable_plane = drm_atomic_helper_disable_plane,
  68. .destroy = drm_plane_cleanup,
  69. .reset = mtk_plane_reset,
  70. .atomic_duplicate_state = mtk_plane_duplicate_state,
  71. .atomic_destroy_state = mtk_drm_plane_destroy_state,
  72. };
  73. static int mtk_plane_atomic_check(struct drm_plane *plane,
  74. struct drm_plane_state *state)
  75. {
  76. struct drm_framebuffer *fb = state->fb;
  77. struct drm_crtc_state *crtc_state;
  78. if (!fb)
  79. return 0;
  80. if (!mtk_fb_get_gem_obj(fb)) {
  81. DRM_DEBUG_KMS("buffer is null\n");
  82. return -EFAULT;
  83. }
  84. if (!state->crtc)
  85. return 0;
  86. crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
  87. if (IS_ERR(crtc_state))
  88. return PTR_ERR(crtc_state);
  89. return drm_atomic_helper_check_plane_state(state, crtc_state,
  90. DRM_PLANE_HELPER_NO_SCALING,
  91. DRM_PLANE_HELPER_NO_SCALING,
  92. true, true);
  93. }
  94. static void mtk_plane_atomic_update(struct drm_plane *plane,
  95. struct drm_plane_state *old_state)
  96. {
  97. struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
  98. struct drm_crtc *crtc = plane->state->crtc;
  99. struct drm_framebuffer *fb = plane->state->fb;
  100. struct drm_gem_object *gem;
  101. struct mtk_drm_gem_obj *mtk_gem;
  102. unsigned int pitch, format;
  103. dma_addr_t addr;
  104. if (!crtc || WARN_ON(!fb))
  105. return;
  106. gem = mtk_fb_get_gem_obj(fb);
  107. mtk_gem = to_mtk_gem_obj(gem);
  108. addr = mtk_gem->dma_addr;
  109. pitch = fb->pitches[0];
  110. format = fb->format->format;
  111. addr += (plane->state->src.x1 >> 16) * fb->format->cpp[0];
  112. addr += (plane->state->src.y1 >> 16) * pitch;
  113. state->pending.enable = true;
  114. state->pending.pitch = pitch;
  115. state->pending.format = format;
  116. state->pending.addr = addr;
  117. state->pending.x = plane->state->dst.x1;
  118. state->pending.y = plane->state->dst.y1;
  119. state->pending.width = drm_rect_width(&plane->state->dst);
  120. state->pending.height = drm_rect_height(&plane->state->dst);
  121. wmb(); /* Make sure the above parameters are set before update */
  122. state->pending.dirty = true;
  123. }
  124. static void mtk_plane_atomic_disable(struct drm_plane *plane,
  125. struct drm_plane_state *old_state)
  126. {
  127. struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
  128. state->pending.enable = false;
  129. wmb(); /* Make sure the above parameter is set before update */
  130. state->pending.dirty = true;
  131. }
  132. static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
  133. .atomic_check = mtk_plane_atomic_check,
  134. .atomic_update = mtk_plane_atomic_update,
  135. .atomic_disable = mtk_plane_atomic_disable,
  136. };
  137. int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
  138. unsigned long possible_crtcs, enum drm_plane_type type)
  139. {
  140. int err;
  141. err = drm_universal_plane_init(dev, plane, possible_crtcs,
  142. &mtk_plane_funcs, formats,
  143. ARRAY_SIZE(formats), NULL, type, NULL);
  144. if (err) {
  145. DRM_ERROR("failed to initialize plane\n");
  146. return err;
  147. }
  148. drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
  149. return 0;
  150. }