shmob_drm_plane.c 7.1 KB

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