mtk_drm_plane.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_enable(struct mtk_drm_plane *mtk_plane, bool enable,
  30. dma_addr_t addr, struct drm_rect *dest)
  31. {
  32. struct drm_plane *plane = &mtk_plane->base;
  33. struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
  34. unsigned int pitch, format;
  35. int x, y;
  36. if (WARN_ON(!plane->state || (enable && !plane->state->fb)))
  37. return;
  38. if (plane->state->fb) {
  39. pitch = plane->state->fb->pitches[0];
  40. format = plane->state->fb->pixel_format;
  41. } else {
  42. pitch = 0;
  43. format = DRM_FORMAT_RGBA8888;
  44. }
  45. x = plane->state->crtc_x;
  46. y = plane->state->crtc_y;
  47. if (x < 0) {
  48. addr -= x * 4;
  49. x = 0;
  50. }
  51. if (y < 0) {
  52. addr -= y * pitch;
  53. y = 0;
  54. }
  55. state->pending.enable = enable;
  56. state->pending.pitch = pitch;
  57. state->pending.format = format;
  58. state->pending.addr = addr;
  59. state->pending.x = x;
  60. state->pending.y = y;
  61. state->pending.width = dest->x2 - dest->x1;
  62. state->pending.height = dest->y2 - dest->y1;
  63. wmb(); /* Make sure the above parameters are set before update */
  64. state->pending.dirty = true;
  65. }
  66. static void mtk_plane_reset(struct drm_plane *plane)
  67. {
  68. struct mtk_plane_state *state;
  69. if (plane->state) {
  70. if (plane->state->fb)
  71. drm_framebuffer_unreference(plane->state->fb);
  72. state = to_mtk_plane_state(plane->state);
  73. memset(state, 0, sizeof(*state));
  74. } else {
  75. state = kzalloc(sizeof(*state), GFP_KERNEL);
  76. if (!state)
  77. return;
  78. plane->state = &state->base;
  79. }
  80. state->base.plane = plane;
  81. state->pending.format = DRM_FORMAT_RGB565;
  82. }
  83. static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
  84. {
  85. struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
  86. struct mtk_plane_state *state;
  87. state = kzalloc(sizeof(*state), GFP_KERNEL);
  88. if (!state)
  89. return NULL;
  90. __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
  91. WARN_ON(state->base.plane != plane);
  92. state->pending = old_state->pending;
  93. return &state->base;
  94. }
  95. static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
  96. struct drm_plane_state *state)
  97. {
  98. __drm_atomic_helper_plane_destroy_state(state);
  99. kfree(to_mtk_plane_state(state));
  100. }
  101. static const struct drm_plane_funcs mtk_plane_funcs = {
  102. .update_plane = drm_atomic_helper_update_plane,
  103. .disable_plane = drm_atomic_helper_disable_plane,
  104. .destroy = drm_plane_cleanup,
  105. .reset = mtk_plane_reset,
  106. .atomic_duplicate_state = mtk_plane_duplicate_state,
  107. .atomic_destroy_state = mtk_drm_plane_destroy_state,
  108. };
  109. static int mtk_plane_atomic_check(struct drm_plane *plane,
  110. struct drm_plane_state *state)
  111. {
  112. struct drm_framebuffer *fb = state->fb;
  113. struct drm_crtc_state *crtc_state;
  114. bool visible;
  115. struct drm_rect dest = {
  116. .x1 = state->crtc_x,
  117. .y1 = state->crtc_y,
  118. .x2 = state->crtc_x + state->crtc_w,
  119. .y2 = state->crtc_y + state->crtc_h,
  120. };
  121. struct drm_rect src = {
  122. /* 16.16 fixed point */
  123. .x1 = state->src_x,
  124. .y1 = state->src_y,
  125. .x2 = state->src_x + state->src_w,
  126. .y2 = state->src_y + state->src_h,
  127. };
  128. struct drm_rect clip = { 0, };
  129. if (!fb)
  130. return 0;
  131. if (!mtk_fb_get_gem_obj(fb)) {
  132. DRM_DEBUG_KMS("buffer is null\n");
  133. return -EFAULT;
  134. }
  135. if (!state->crtc)
  136. return 0;
  137. crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
  138. if (IS_ERR(crtc_state))
  139. return PTR_ERR(crtc_state);
  140. clip.x2 = crtc_state->mode.hdisplay;
  141. clip.y2 = crtc_state->mode.vdisplay;
  142. return drm_plane_helper_check_update(plane, state->crtc, fb,
  143. &src, &dest, &clip,
  144. DRM_PLANE_HELPER_NO_SCALING,
  145. DRM_PLANE_HELPER_NO_SCALING,
  146. true, true, &visible);
  147. }
  148. static void mtk_plane_atomic_update(struct drm_plane *plane,
  149. struct drm_plane_state *old_state)
  150. {
  151. struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
  152. struct drm_crtc *crtc = state->base.crtc;
  153. struct drm_gem_object *gem;
  154. struct mtk_drm_gem_obj *mtk_gem;
  155. struct mtk_drm_plane *mtk_plane = to_mtk_plane(plane);
  156. struct drm_rect dest = {
  157. .x1 = state->base.crtc_x,
  158. .y1 = state->base.crtc_y,
  159. .x2 = state->base.crtc_x + state->base.crtc_w,
  160. .y2 = state->base.crtc_y + state->base.crtc_h,
  161. };
  162. struct drm_rect clip = { 0, };
  163. if (!crtc)
  164. return;
  165. clip.x2 = state->base.crtc->state->mode.hdisplay;
  166. clip.y2 = state->base.crtc->state->mode.vdisplay;
  167. drm_rect_intersect(&dest, &clip);
  168. gem = mtk_fb_get_gem_obj(state->base.fb);
  169. mtk_gem = to_mtk_gem_obj(gem);
  170. mtk_plane_enable(mtk_plane, true, mtk_gem->dma_addr, &dest);
  171. }
  172. static void mtk_plane_atomic_disable(struct drm_plane *plane,
  173. struct drm_plane_state *old_state)
  174. {
  175. struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
  176. state->pending.enable = false;
  177. wmb(); /* Make sure the above parameter is set before update */
  178. state->pending.dirty = true;
  179. }
  180. static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
  181. .atomic_check = mtk_plane_atomic_check,
  182. .atomic_update = mtk_plane_atomic_update,
  183. .atomic_disable = mtk_plane_atomic_disable,
  184. };
  185. int mtk_plane_init(struct drm_device *dev, struct mtk_drm_plane *mtk_plane,
  186. unsigned long possible_crtcs, enum drm_plane_type type,
  187. unsigned int zpos)
  188. {
  189. int err;
  190. err = drm_universal_plane_init(dev, &mtk_plane->base, possible_crtcs,
  191. &mtk_plane_funcs, formats,
  192. ARRAY_SIZE(formats), type, NULL);
  193. if (err) {
  194. DRM_ERROR("failed to initialize plane\n");
  195. return err;
  196. }
  197. drm_plane_helper_add(&mtk_plane->base, &mtk_plane_helper_funcs);
  198. mtk_plane->idx = zpos;
  199. return 0;
  200. }