mtk_drm_plane.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. };
  29. static void mtk_plane_reset(struct drm_plane *plane)
  30. {
  31. struct mtk_plane_state *state;
  32. if (plane->state) {
  33. __drm_atomic_helper_plane_destroy_state(plane->state);
  34. state = to_mtk_plane_state(plane->state);
  35. memset(state, 0, sizeof(*state));
  36. } else {
  37. state = kzalloc(sizeof(*state), GFP_KERNEL);
  38. if (!state)
  39. return;
  40. plane->state = &state->base;
  41. }
  42. state->base.plane = plane;
  43. state->pending.format = DRM_FORMAT_RGB565;
  44. }
  45. static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
  46. {
  47. struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
  48. struct mtk_plane_state *state;
  49. state = kzalloc(sizeof(*state), GFP_KERNEL);
  50. if (!state)
  51. return NULL;
  52. __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
  53. WARN_ON(state->base.plane != plane);
  54. state->pending = old_state->pending;
  55. return &state->base;
  56. }
  57. static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
  58. struct drm_plane_state *state)
  59. {
  60. __drm_atomic_helper_plane_destroy_state(state);
  61. kfree(to_mtk_plane_state(state));
  62. }
  63. static const struct drm_plane_funcs mtk_plane_funcs = {
  64. .update_plane = drm_atomic_helper_update_plane,
  65. .disable_plane = drm_atomic_helper_disable_plane,
  66. .destroy = drm_plane_cleanup,
  67. .reset = mtk_plane_reset,
  68. .atomic_duplicate_state = mtk_plane_duplicate_state,
  69. .atomic_destroy_state = mtk_drm_plane_destroy_state,
  70. };
  71. static int mtk_plane_atomic_check(struct drm_plane *plane,
  72. struct drm_plane_state *state)
  73. {
  74. struct drm_framebuffer *fb = state->fb;
  75. struct drm_crtc_state *crtc_state;
  76. struct drm_rect clip = { 0, };
  77. if (!fb)
  78. return 0;
  79. if (!mtk_fb_get_gem_obj(fb)) {
  80. DRM_DEBUG_KMS("buffer is null\n");
  81. return -EFAULT;
  82. }
  83. if (!state->crtc)
  84. return 0;
  85. crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
  86. if (IS_ERR(crtc_state))
  87. return PTR_ERR(crtc_state);
  88. clip.x2 = crtc_state->mode.hdisplay;
  89. clip.y2 = crtc_state->mode.vdisplay;
  90. return drm_plane_helper_check_state(state, &clip,
  91. DRM_PLANE_HELPER_NO_SCALING,
  92. DRM_PLANE_HELPER_NO_SCALING,
  93. true, true);
  94. }
  95. static void mtk_plane_atomic_update(struct drm_plane *plane,
  96. struct drm_plane_state *old_state)
  97. {
  98. struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
  99. struct drm_crtc *crtc = plane->state->crtc;
  100. struct drm_framebuffer *fb = plane->state->fb;
  101. struct drm_gem_object *gem;
  102. struct mtk_drm_gem_obj *mtk_gem;
  103. unsigned int pitch, format;
  104. dma_addr_t addr;
  105. if (!crtc || WARN_ON(!fb))
  106. return;
  107. gem = mtk_fb_get_gem_obj(fb);
  108. mtk_gem = to_mtk_gem_obj(gem);
  109. addr = mtk_gem->dma_addr;
  110. pitch = fb->pitches[0];
  111. format = fb->pixel_format;
  112. addr += (plane->state->src.x1 >> 16) * drm_format_plane_cpp(format, 0);
  113. addr += (plane->state->src.y1 >> 16) * pitch;
  114. state->pending.enable = true;
  115. state->pending.pitch = pitch;
  116. state->pending.format = format;
  117. state->pending.addr = addr;
  118. state->pending.x = plane->state->dst.x1;
  119. state->pending.y = plane->state->dst.y1;
  120. state->pending.width = drm_rect_width(&plane->state->dst);
  121. state->pending.height = drm_rect_height(&plane->state->dst);
  122. wmb(); /* Make sure the above parameters are set before update */
  123. state->pending.dirty = true;
  124. }
  125. static void mtk_plane_atomic_disable(struct drm_plane *plane,
  126. struct drm_plane_state *old_state)
  127. {
  128. struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
  129. state->pending.enable = false;
  130. wmb(); /* Make sure the above parameter is set before update */
  131. state->pending.dirty = true;
  132. }
  133. static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
  134. .atomic_check = mtk_plane_atomic_check,
  135. .atomic_update = mtk_plane_atomic_update,
  136. .atomic_disable = mtk_plane_atomic_disable,
  137. };
  138. int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
  139. unsigned long possible_crtcs, enum drm_plane_type type)
  140. {
  141. int err;
  142. err = drm_universal_plane_init(dev, plane, possible_crtcs,
  143. &mtk_plane_funcs, formats,
  144. ARRAY_SIZE(formats), type, NULL);
  145. if (err) {
  146. DRM_ERROR("failed to initialize plane\n");
  147. return err;
  148. }
  149. drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
  150. return 0;
  151. }