meson_plane.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
  50. if (IS_ERR(crtc_state))
  51. return PTR_ERR(crtc_state);
  52. clip.x2 = crtc_state->mode.hdisplay;
  53. clip.y2 = crtc_state->mode.vdisplay;
  54. return drm_plane_helper_check_state(state, &clip,
  55. DRM_PLANE_HELPER_NO_SCALING,
  56. DRM_PLANE_HELPER_NO_SCALING,
  57. true, true);
  58. }
  59. /* Takes a fixed 16.16 number and converts it to integer. */
  60. static inline int64_t fixed16_to_int(int64_t value)
  61. {
  62. return value >> 16;
  63. }
  64. static void meson_plane_atomic_update(struct drm_plane *plane,
  65. struct drm_plane_state *old_state)
  66. {
  67. struct meson_plane *meson_plane = to_meson_plane(plane);
  68. struct drm_plane_state *state = plane->state;
  69. struct drm_framebuffer *fb = state->fb;
  70. struct meson_drm *priv = meson_plane->priv;
  71. struct drm_gem_cma_object *gem;
  72. struct drm_rect src = {
  73. .x1 = (state->src_x),
  74. .y1 = (state->src_y),
  75. .x2 = (state->src_x + state->src_w),
  76. .y2 = (state->src_y + state->src_h),
  77. };
  78. struct drm_rect dest = {
  79. .x1 = state->crtc_x,
  80. .y1 = state->crtc_y,
  81. .x2 = state->crtc_x + state->crtc_w,
  82. .y2 = state->crtc_y + state->crtc_h,
  83. };
  84. unsigned long flags;
  85. /*
  86. * Update Coordinates
  87. * Update Formats
  88. * Update Buffer
  89. * Enable Plane
  90. */
  91. spin_lock_irqsave(&priv->drm->event_lock, flags);
  92. /* Enable OSD and BLK0, set max global alpha */
  93. priv->viu.osd1_ctrl_stat = OSD_ENABLE |
  94. (0xFF << OSD_GLOBAL_ALPHA_SHIFT) |
  95. OSD_BLK0_ENABLE;
  96. /* Set up BLK0 to point to the right canvas */
  97. priv->viu.osd1_blk0_cfg[0] = ((MESON_CANVAS_ID_OSD1 << OSD_CANVAS_SEL) |
  98. OSD_ENDIANNESS_LE);
  99. /* On GXBB, Use the old non-HDR RGB2YUV converter */
  100. if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu"))
  101. priv->viu.osd1_blk0_cfg[0] |= OSD_OUTPUT_COLOR_RGB;
  102. switch (fb->pixel_format) {
  103. case DRM_FORMAT_XRGB8888:
  104. /* For XRGB, replace the pixel's alpha by 0xFF */
  105. writel_bits_relaxed(OSD_REPLACE_EN, OSD_REPLACE_EN,
  106. priv->io_base + _REG(VIU_OSD1_CTRL_STAT2));
  107. priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 |
  108. OSD_COLOR_MATRIX_32_ARGB;
  109. break;
  110. case DRM_FORMAT_ARGB8888:
  111. /* For ARGB, use the pixel's alpha */
  112. writel_bits_relaxed(OSD_REPLACE_EN, 0,
  113. priv->io_base + _REG(VIU_OSD1_CTRL_STAT2));
  114. priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 |
  115. OSD_COLOR_MATRIX_32_ARGB;
  116. break;
  117. case DRM_FORMAT_RGB888:
  118. priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_24 |
  119. OSD_COLOR_MATRIX_24_RGB;
  120. break;
  121. case DRM_FORMAT_RGB565:
  122. priv->viu.osd1_blk0_cfg[0] |= OSD_BLK_MODE_16 |
  123. OSD_COLOR_MATRIX_16_RGB565;
  124. break;
  125. };
  126. if (state->crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) {
  127. priv->viu.osd1_interlace = true;
  128. dest.y1 /= 2;
  129. dest.y2 /= 2;
  130. } else
  131. priv->viu.osd1_interlace = false;
  132. /*
  133. * The format of these registers is (x2 << 16 | x1),
  134. * where x2 is exclusive.
  135. * e.g. +30x1920 would be (1919 << 16) | 30
  136. */
  137. priv->viu.osd1_blk0_cfg[1] = ((fixed16_to_int(src.x2) - 1) << 16) |
  138. fixed16_to_int(src.x1);
  139. priv->viu.osd1_blk0_cfg[2] = ((fixed16_to_int(src.y2) - 1) << 16) |
  140. fixed16_to_int(src.y1);
  141. priv->viu.osd1_blk0_cfg[3] = ((dest.x2 - 1) << 16) | dest.x1;
  142. priv->viu.osd1_blk0_cfg[4] = ((dest.y2 - 1) << 16) | dest.y1;
  143. /* Update Canvas with buffer address */
  144. gem = drm_fb_cma_get_gem_obj(fb, 0);
  145. meson_canvas_setup(priv, MESON_CANVAS_ID_OSD1,
  146. gem->paddr, fb->pitches[0],
  147. fb->height, MESON_CANVAS_WRAP_NONE,
  148. MESON_CANVAS_BLKMODE_LINEAR);
  149. spin_unlock_irqrestore(&priv->drm->event_lock, flags);
  150. }
  151. static void meson_plane_atomic_disable(struct drm_plane *plane,
  152. struct drm_plane_state *old_state)
  153. {
  154. struct meson_plane *meson_plane = to_meson_plane(plane);
  155. struct meson_drm *priv = meson_plane->priv;
  156. /* Disable OSD1 */
  157. writel_bits_relaxed(VPP_OSD1_POSTBLEND, 0,
  158. priv->io_base + _REG(VPP_MISC));
  159. }
  160. static const struct drm_plane_helper_funcs meson_plane_helper_funcs = {
  161. .atomic_check = meson_plane_atomic_check,
  162. .atomic_disable = meson_plane_atomic_disable,
  163. .atomic_update = meson_plane_atomic_update,
  164. };
  165. static const struct drm_plane_funcs meson_plane_funcs = {
  166. .update_plane = drm_atomic_helper_update_plane,
  167. .disable_plane = drm_atomic_helper_disable_plane,
  168. .destroy = drm_plane_cleanup,
  169. .reset = drm_atomic_helper_plane_reset,
  170. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  171. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  172. };
  173. static const uint32_t supported_drm_formats[] = {
  174. DRM_FORMAT_ARGB8888,
  175. DRM_FORMAT_XRGB8888,
  176. DRM_FORMAT_RGB888,
  177. DRM_FORMAT_RGB565,
  178. };
  179. int meson_plane_create(struct meson_drm *priv)
  180. {
  181. struct meson_plane *meson_plane;
  182. struct drm_plane *plane;
  183. meson_plane = devm_kzalloc(priv->drm->dev, sizeof(*meson_plane),
  184. GFP_KERNEL);
  185. if (!meson_plane)
  186. return -ENOMEM;
  187. meson_plane->priv = priv;
  188. plane = &meson_plane->base;
  189. drm_universal_plane_init(priv->drm, plane, 0xFF,
  190. &meson_plane_funcs,
  191. supported_drm_formats,
  192. ARRAY_SIZE(supported_drm_formats),
  193. DRM_PLANE_TYPE_PRIMARY, "meson_primary_plane");
  194. drm_plane_helper_add(plane, &meson_plane_helper_funcs);
  195. priv->primary_plane = plane;
  196. return 0;
  197. }