shmob_drm_plane.c 7.0 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 <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. {
  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->pixel_format);
  159. if (format == NULL) {
  160. dev_dbg(sdev->dev, "update_plane: unsupported format %08x\n",
  161. fb->pixel_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. {
  180. struct shmob_drm_plane *splane = to_shmob_plane(plane);
  181. struct shmob_drm_device *sdev = plane->dev->dev_private;
  182. splane->format = NULL;
  183. lcdc_write(sdev, LDBnBSIFR(splane->index), 0);
  184. return 0;
  185. }
  186. static void shmob_drm_plane_destroy(struct drm_plane *plane)
  187. {
  188. shmob_drm_plane_disable(plane);
  189. drm_plane_cleanup(plane);
  190. }
  191. static const struct drm_plane_funcs shmob_drm_plane_funcs = {
  192. .update_plane = shmob_drm_plane_update,
  193. .disable_plane = shmob_drm_plane_disable,
  194. .destroy = shmob_drm_plane_destroy,
  195. };
  196. static const uint32_t formats[] = {
  197. DRM_FORMAT_RGB565,
  198. DRM_FORMAT_RGB888,
  199. DRM_FORMAT_ARGB8888,
  200. DRM_FORMAT_NV12,
  201. DRM_FORMAT_NV21,
  202. DRM_FORMAT_NV16,
  203. DRM_FORMAT_NV61,
  204. DRM_FORMAT_NV24,
  205. DRM_FORMAT_NV42,
  206. };
  207. int shmob_drm_plane_create(struct shmob_drm_device *sdev, unsigned int index)
  208. {
  209. struct shmob_drm_plane *splane;
  210. int ret;
  211. splane = devm_kzalloc(sdev->dev, sizeof(*splane), GFP_KERNEL);
  212. if (splane == NULL)
  213. return -ENOMEM;
  214. splane->index = index;
  215. splane->alpha = 255;
  216. ret = drm_plane_init(sdev->ddev, &splane->plane, 1,
  217. &shmob_drm_plane_funcs, formats,
  218. ARRAY_SIZE(formats), false);
  219. return ret;
  220. }