shmob_drm_plane.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * shmob_drm_plane.c -- SH Mobile DRM Planes
  3. *
  4. * Copyright (C) 2012 Renesas Electronics Corporation
  5. *
  6. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <drm/drmP.h>
  14. #include <drm/drm_crtc.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include <drm/drm_fb_cma_helper.h>
  17. #include <drm/drm_gem_cma_helper.h>
  18. #include <video/sh_mobile_meram.h>
  19. #include "shmob_drm_drv.h"
  20. #include "shmob_drm_kms.h"
  21. #include "shmob_drm_plane.h"
  22. #include "shmob_drm_regs.h"
  23. struct shmob_drm_plane {
  24. struct drm_plane plane;
  25. unsigned int index;
  26. unsigned int alpha;
  27. const struct shmob_drm_format_info *format;
  28. unsigned long dma[2];
  29. unsigned int src_x;
  30. unsigned int src_y;
  31. unsigned int crtc_x;
  32. unsigned int crtc_y;
  33. unsigned int crtc_w;
  34. unsigned int crtc_h;
  35. };
  36. #define to_shmob_plane(p) container_of(p, struct shmob_drm_plane, plane)
  37. static void shmob_drm_plane_compute_base(struct shmob_drm_plane *splane,
  38. struct drm_framebuffer *fb,
  39. int x, int y)
  40. {
  41. struct drm_gem_cma_object *gem;
  42. unsigned int bpp;
  43. bpp = splane->format->yuv ? 8 : splane->format->bpp;
  44. gem = drm_fb_cma_get_gem_obj(fb, 0);
  45. splane->dma[0] = gem->paddr + fb->offsets[0]
  46. + y * fb->pitches[0] + x * bpp / 8;
  47. if (splane->format->yuv) {
  48. bpp = splane->format->bpp - 8;
  49. gem = drm_fb_cma_get_gem_obj(fb, 1);
  50. splane->dma[1] = gem->paddr + fb->offsets[1]
  51. + y / (bpp == 4 ? 2 : 1) * fb->pitches[1]
  52. + x * (bpp == 16 ? 2 : 1);
  53. }
  54. }
  55. static void __shmob_drm_plane_setup(struct shmob_drm_plane *splane,
  56. struct drm_framebuffer *fb)
  57. {
  58. struct shmob_drm_device *sdev = splane->plane.dev->dev_private;
  59. u32 format;
  60. /* TODO: Support ROP3 mode */
  61. format = LDBBSIFR_EN | (splane->alpha << LDBBSIFR_LAY_SHIFT);
  62. switch (splane->format->fourcc) {
  63. case DRM_FORMAT_RGB565:
  64. case DRM_FORMAT_NV21:
  65. case DRM_FORMAT_NV61:
  66. case DRM_FORMAT_NV42:
  67. format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW;
  68. break;
  69. case DRM_FORMAT_RGB888:
  70. case DRM_FORMAT_NV12:
  71. case DRM_FORMAT_NV16:
  72. case DRM_FORMAT_NV24:
  73. format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW | LDBBSIFR_SWPB;
  74. break;
  75. case DRM_FORMAT_ARGB8888:
  76. default:
  77. format |= LDBBSIFR_SWPL;
  78. break;
  79. }
  80. switch (splane->format->fourcc) {
  81. case DRM_FORMAT_RGB565:
  82. format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB16;
  83. break;
  84. case DRM_FORMAT_RGB888:
  85. format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB24;
  86. break;
  87. case DRM_FORMAT_ARGB8888:
  88. format |= LDBBSIFR_AL_PK | LDBBSIFR_RY | LDDFR_PKF_ARGB32;
  89. break;
  90. case DRM_FORMAT_NV12:
  91. case DRM_FORMAT_NV21:
  92. format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_420;
  93. break;
  94. case DRM_FORMAT_NV16:
  95. case DRM_FORMAT_NV61:
  96. format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_422;
  97. break;
  98. case DRM_FORMAT_NV24:
  99. case DRM_FORMAT_NV42:
  100. format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_444;
  101. break;
  102. }
  103. #define plane_reg_dump(sdev, splane, reg) \
  104. dev_dbg(sdev->ddev->dev, "%s(%u): %s 0x%08x 0x%08x\n", __func__, \
  105. splane->index, #reg, \
  106. lcdc_read(sdev, reg(splane->index)), \
  107. lcdc_read(sdev, reg(splane->index) + LCDC_SIDE_B_OFFSET))
  108. plane_reg_dump(sdev, splane, LDBnBSIFR);
  109. plane_reg_dump(sdev, splane, LDBnBSSZR);
  110. plane_reg_dump(sdev, splane, LDBnBLOCR);
  111. plane_reg_dump(sdev, splane, LDBnBSMWR);
  112. plane_reg_dump(sdev, splane, LDBnBSAYR);
  113. plane_reg_dump(sdev, splane, LDBnBSACR);
  114. lcdc_write(sdev, LDBCR, LDBCR_UPC(splane->index));
  115. dev_dbg(sdev->ddev->dev, "%s(%u): %s 0x%08x\n", __func__, splane->index,
  116. "LDBCR", lcdc_read(sdev, LDBCR));
  117. lcdc_write(sdev, LDBnBSIFR(splane->index), format);
  118. lcdc_write(sdev, LDBnBSSZR(splane->index),
  119. (splane->crtc_h << LDBBSSZR_BVSS_SHIFT) |
  120. (splane->crtc_w << LDBBSSZR_BHSS_SHIFT));
  121. lcdc_write(sdev, LDBnBLOCR(splane->index),
  122. (splane->crtc_y << LDBBLOCR_CVLC_SHIFT) |
  123. (splane->crtc_x << LDBBLOCR_CHLC_SHIFT));
  124. lcdc_write(sdev, LDBnBSMWR(splane->index),
  125. fb->pitches[0] << LDBBSMWR_BSMW_SHIFT);
  126. shmob_drm_plane_compute_base(splane, fb, splane->src_x, splane->src_y);
  127. lcdc_write(sdev, LDBnBSAYR(splane->index), splane->dma[0]);
  128. if (splane->format->yuv)
  129. lcdc_write(sdev, LDBnBSACR(splane->index), splane->dma[1]);
  130. lcdc_write(sdev, LDBCR,
  131. LDBCR_UPF(splane->index) | LDBCR_UPD(splane->index));
  132. dev_dbg(sdev->ddev->dev, "%s(%u): %s 0x%08x\n", __func__, splane->index,
  133. "LDBCR", lcdc_read(sdev, LDBCR));
  134. plane_reg_dump(sdev, splane, LDBnBSIFR);
  135. plane_reg_dump(sdev, splane, LDBnBSSZR);
  136. plane_reg_dump(sdev, splane, LDBnBLOCR);
  137. plane_reg_dump(sdev, splane, LDBnBSMWR);
  138. plane_reg_dump(sdev, splane, LDBnBSAYR);
  139. plane_reg_dump(sdev, splane, LDBnBSACR);
  140. }
  141. void shmob_drm_plane_setup(struct drm_plane *plane)
  142. {
  143. struct shmob_drm_plane *splane = to_shmob_plane(plane);
  144. if (plane->fb == NULL)
  145. return;
  146. __shmob_drm_plane_setup(splane, plane->fb);
  147. }
  148. static int
  149. shmob_drm_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  150. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  151. unsigned int crtc_w, unsigned int crtc_h,
  152. uint32_t src_x, uint32_t src_y,
  153. uint32_t src_w, uint32_t src_h,
  154. struct drm_modeset_acquire_ctx *ctx)
  155. {
  156. struct shmob_drm_plane *splane = to_shmob_plane(plane);
  157. struct shmob_drm_device *sdev = plane->dev->dev_private;
  158. const struct shmob_drm_format_info *format;
  159. format = shmob_drm_format_info(fb->format->format);
  160. if (format == NULL) {
  161. dev_dbg(sdev->dev, "update_plane: unsupported format %08x\n",
  162. fb->format->format);
  163. return -EINVAL;
  164. }
  165. if (src_w >> 16 != crtc_w || src_h >> 16 != crtc_h) {
  166. dev_dbg(sdev->dev, "%s: scaling not supported\n", __func__);
  167. return -EINVAL;
  168. }
  169. splane->format = format;
  170. splane->src_x = src_x >> 16;
  171. splane->src_y = src_y >> 16;
  172. splane->crtc_x = crtc_x;
  173. splane->crtc_y = crtc_y;
  174. splane->crtc_w = crtc_w;
  175. splane->crtc_h = crtc_h;
  176. __shmob_drm_plane_setup(splane, fb);
  177. return 0;
  178. }
  179. static int shmob_drm_plane_disable(struct drm_plane *plane,
  180. struct drm_modeset_acquire_ctx *ctx)
  181. {
  182. struct shmob_drm_plane *splane = to_shmob_plane(plane);
  183. struct shmob_drm_device *sdev = plane->dev->dev_private;
  184. splane->format = NULL;
  185. lcdc_write(sdev, LDBnBSIFR(splane->index), 0);
  186. return 0;
  187. }
  188. static void shmob_drm_plane_destroy(struct drm_plane *plane)
  189. {
  190. drm_plane_force_disable(plane);
  191. drm_plane_cleanup(plane);
  192. }
  193. static const struct drm_plane_funcs shmob_drm_plane_funcs = {
  194. .update_plane = shmob_drm_plane_update,
  195. .disable_plane = shmob_drm_plane_disable,
  196. .destroy = shmob_drm_plane_destroy,
  197. };
  198. static const uint32_t formats[] = {
  199. DRM_FORMAT_RGB565,
  200. DRM_FORMAT_RGB888,
  201. DRM_FORMAT_ARGB8888,
  202. DRM_FORMAT_NV12,
  203. DRM_FORMAT_NV21,
  204. DRM_FORMAT_NV16,
  205. DRM_FORMAT_NV61,
  206. DRM_FORMAT_NV24,
  207. DRM_FORMAT_NV42,
  208. };
  209. int shmob_drm_plane_create(struct shmob_drm_device *sdev, unsigned int index)
  210. {
  211. struct shmob_drm_plane *splane;
  212. int ret;
  213. splane = devm_kzalloc(sdev->dev, sizeof(*splane), GFP_KERNEL);
  214. if (splane == NULL)
  215. return -ENOMEM;
  216. splane->index = index;
  217. splane->alpha = 255;
  218. ret = drm_plane_init(sdev->ddev, &splane->plane, 1,
  219. &shmob_drm_plane_funcs, formats,
  220. ARRAY_SIZE(formats), false);
  221. return ret;
  222. }