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