meson_plane.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (C) 2016 BayLibre, SAS
  3. * Author: Neil Armstrong <narmstrong@baylibre.com>
  4. * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
  5. * Copyright (C) 2014 Endless Mobile
  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 the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Written by:
  21. * Jasper St. Pierre <jstpierre@mecheye.net>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include <linux/platform_device.h>
  27. #include <drm/drmP.h>
  28. #include <drm/drm_atomic.h>
  29. #include <drm/drm_atomic_helper.h>
  30. #include <drm/drm_plane_helper.h>
  31. #include <drm/drm_gem_cma_helper.h>
  32. #include <drm/drm_fb_cma_helper.h>
  33. #include <drm/drm_rect.h>
  34. #include "meson_plane.h"
  35. #include "meson_vpp.h"
  36. #include "meson_viu.h"
  37. #include "meson_canvas.h"
  38. #include "meson_registers.h"
  39. struct meson_plane {
  40. struct drm_plane base;
  41. struct meson_drm *priv;
  42. };
  43. #define to_meson_plane(x) container_of(x, struct meson_plane, base)
  44. static int meson_plane_atomic_check(struct drm_plane *plane,
  45. struct drm_plane_state *state)
  46. {
  47. struct drm_crtc_state *crtc_state;
  48. struct drm_rect clip = { 0, };
  49. if (!state->crtc)
  50. return 0;
  51. crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
  52. if (IS_ERR(crtc_state))
  53. return PTR_ERR(crtc_state);
  54. clip.x2 = crtc_state->mode.hdisplay;
  55. clip.y2 = crtc_state->mode.vdisplay;
  56. return drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
  57. DRM_PLANE_HELPER_NO_SCALING,
  58. DRM_PLANE_HELPER_NO_SCALING,
  59. true, true);
  60. }
  61. /* Takes a fixed 16.16 number and converts it to integer. */
  62. static inline int64_t fixed16_to_int(int64_t value)
  63. {
  64. return value >> 16;
  65. }
  66. static void meson_plane_atomic_update(struct drm_plane *plane,
  67. struct drm_plane_state *old_state)
  68. {
  69. struct meson_plane *meson_plane = to_meson_plane(plane);
  70. struct drm_plane_state *state = plane->state;
  71. struct drm_framebuffer *fb = state->fb;
  72. struct meson_drm *priv = meson_plane->priv;
  73. struct drm_gem_cma_object *gem;
  74. struct drm_rect src = {
  75. .x1 = (state->src_x),
  76. .y1 = (state->src_y),
  77. .x2 = (state->src_x + state->src_w),
  78. .y2 = (state->src_y + state->src_h),
  79. };
  80. struct drm_rect dest = {
  81. .x1 = state->crtc_x,
  82. .y1 = state->crtc_y,
  83. .x2 = state->crtc_x + state->crtc_w,
  84. .y2 = state->crtc_y + state->crtc_h,
  85. };
  86. unsigned long flags;
  87. /*
  88. * Update Coordinates
  89. * Update Formats
  90. * Update Buffer
  91. * Enable Plane
  92. */
  93. spin_lock_irqsave(&priv->drm->event_lock, flags);
  94. /* Enable OSD and BLK0, set max global alpha */
  95. priv->viu.osd1_ctrl_stat = OSD_ENABLE |
  96. (0xFF << OSD_GLOBAL_ALPHA_SHIFT) |
  97. OSD_BLK0_ENABLE;
  98. /* Set up BLK0 to point to the right canvas */
  99. priv->viu.osd1_blk0_cfg[0] = ((MESON_CANVAS_ID_OSD1 << OSD_CANVAS_SEL) |
  100. OSD_ENDIANNESS_LE);
  101. /* On GXBB, Use the old non-HDR RGB2YUV converter */
  102. if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu"))
  103. priv->viu.osd1_blk0_cfg[0] |= OSD_OUTPUT_COLOR_RGB;
  104. switch (fb->format->format) {
  105. case DRM_FORMAT_XRGB8888:
  106. /* For XRGB, replace the pixel's alpha by 0xFF */
  107. writel_bits_relaxed(OSD_REPLACE_EN, OSD_REPLACE_EN,
  108. priv->io_base + _REG(VIU_OSD1_CTRL_STAT2));
  109. priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 |
  110. OSD_COLOR_MATRIX_32_ARGB;
  111. break;
  112. case DRM_FORMAT_ARGB8888:
  113. /* For ARGB, use the pixel's alpha */
  114. writel_bits_relaxed(OSD_REPLACE_EN, 0,
  115. priv->io_base + _REG(VIU_OSD1_CTRL_STAT2));
  116. priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 |
  117. OSD_COLOR_MATRIX_32_ARGB;
  118. break;
  119. case DRM_FORMAT_RGB888:
  120. priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_24 |
  121. OSD_COLOR_MATRIX_24_RGB;
  122. break;
  123. case DRM_FORMAT_RGB565:
  124. priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_16 |
  125. OSD_COLOR_MATRIX_16_RGB565;
  126. break;
  127. };
  128. if (state->crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) {
  129. priv->viu.osd1_interlace = true;
  130. dest.y1 /= 2;
  131. dest.y2 /= 2;
  132. } else
  133. priv->viu.osd1_interlace = false;
  134. /*
  135. * The format of these registers is (x2 << 16 | x1),
  136. * where x2 is exclusive.
  137. * e.g. +30x1920 would be (1919 << 16) | 30
  138. */
  139. priv->viu.osd1_blk0_cfg[1] = ((fixed16_to_int(src.x2) - 1) << 16) |
  140. fixed16_to_int(src.x1);
  141. priv->viu.osd1_blk0_cfg[2] = ((fixed16_to_int(src.y2) - 1) << 16) |
  142. fixed16_to_int(src.y1);
  143. priv->viu.osd1_blk0_cfg[3] = ((dest.x2 - 1) << 16) | dest.x1;
  144. priv->viu.osd1_blk0_cfg[4] = ((dest.y2 - 1) << 16) | dest.y1;
  145. /* Update Canvas with buffer address */
  146. gem = drm_fb_cma_get_gem_obj(fb, 0);
  147. meson_canvas_setup(priv, MESON_CANVAS_ID_OSD1,
  148. gem->paddr, fb->pitches[0],
  149. fb->height, MESON_CANVAS_WRAP_NONE,
  150. MESON_CANVAS_BLKMODE_LINEAR);
  151. spin_unlock_irqrestore(&priv->drm->event_lock, flags);
  152. }
  153. static void meson_plane_atomic_disable(struct drm_plane *plane,
  154. struct drm_plane_state *old_state)
  155. {
  156. struct meson_plane *meson_plane = to_meson_plane(plane);
  157. struct meson_drm *priv = meson_plane->priv;
  158. /* Disable OSD1 */
  159. writel_bits_relaxed(VPP_OSD1_POSTBLEND, 0,
  160. priv->io_base + _REG(VPP_MISC));
  161. }
  162. static const struct drm_plane_helper_funcs meson_plane_helper_funcs = {
  163. .atomic_check = meson_plane_atomic_check,
  164. .atomic_disable = meson_plane_atomic_disable,
  165. .atomic_update = meson_plane_atomic_update,
  166. };
  167. static const struct drm_plane_funcs meson_plane_funcs = {
  168. .update_plane = drm_atomic_helper_update_plane,
  169. .disable_plane = drm_atomic_helper_disable_plane,
  170. .destroy = drm_plane_cleanup,
  171. .reset = drm_atomic_helper_plane_reset,
  172. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  173. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  174. };
  175. static const uint32_t supported_drm_formats[] = {
  176. DRM_FORMAT_ARGB8888,
  177. DRM_FORMAT_XRGB8888,
  178. DRM_FORMAT_RGB888,
  179. DRM_FORMAT_RGB565,
  180. };
  181. int meson_plane_create(struct meson_drm *priv)
  182. {
  183. struct meson_plane *meson_plane;
  184. struct drm_plane *plane;
  185. meson_plane = devm_kzalloc(priv->drm->dev, sizeof(*meson_plane),
  186. GFP_KERNEL);
  187. if (!meson_plane)
  188. return -ENOMEM;
  189. meson_plane->priv = priv;
  190. plane = &meson_plane->base;
  191. drm_universal_plane_init(priv->drm, plane, 0xFF,
  192. &meson_plane_funcs,
  193. supported_drm_formats,
  194. ARRAY_SIZE(supported_drm_formats),
  195. NULL,
  196. DRM_PLANE_TYPE_PRIMARY, "meson_primary_plane");
  197. drm_plane_helper_add(plane, &meson_plane_helper_funcs);
  198. priv->primary_plane = plane;
  199. return 0;
  200. }