rcar_du_plane.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * rcar_du_plane.c -- R-Car Display Unit Planes
  3. *
  4. * Copyright (C) 2013-2014 Renesas Electronics Corporation
  5. *
  6. * Contact: 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_atomic_helper.h>
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <drm/drm_fb_cma_helper.h>
  18. #include <drm/drm_gem_cma_helper.h>
  19. #include <drm/drm_plane_helper.h>
  20. #include "rcar_du_drv.h"
  21. #include "rcar_du_kms.h"
  22. #include "rcar_du_plane.h"
  23. #include "rcar_du_regs.h"
  24. #define RCAR_DU_COLORKEY_NONE (0 << 24)
  25. #define RCAR_DU_COLORKEY_SOURCE (1 << 24)
  26. #define RCAR_DU_COLORKEY_MASK (1 << 24)
  27. static u32 rcar_du_plane_read(struct rcar_du_group *rgrp,
  28. unsigned int index, u32 reg)
  29. {
  30. return rcar_du_read(rgrp->dev,
  31. rgrp->mmio_offset + index * PLANE_OFF + reg);
  32. }
  33. static void rcar_du_plane_write(struct rcar_du_group *rgrp,
  34. unsigned int index, u32 reg, u32 data)
  35. {
  36. rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg,
  37. data);
  38. }
  39. static void rcar_du_plane_setup_fb(struct rcar_du_plane *plane)
  40. {
  41. struct rcar_du_plane_state *state =
  42. to_rcar_plane_state(plane->plane.state);
  43. struct drm_framebuffer *fb = plane->plane.state->fb;
  44. struct rcar_du_group *rgrp = plane->group;
  45. unsigned int src_x = state->state.src_x >> 16;
  46. unsigned int src_y = state->state.src_y >> 16;
  47. unsigned int index = state->hwindex;
  48. struct drm_gem_cma_object *gem;
  49. bool interlaced;
  50. u32 mwr;
  51. interlaced = state->state.crtc->state->adjusted_mode.flags
  52. & DRM_MODE_FLAG_INTERLACE;
  53. /* Memory pitch (expressed in pixels). Must be doubled for interlaced
  54. * operation with 32bpp formats.
  55. */
  56. if (state->format->planes == 2)
  57. mwr = fb->pitches[0];
  58. else
  59. mwr = fb->pitches[0] * 8 / state->format->bpp;
  60. if (interlaced && state->format->bpp == 32)
  61. mwr *= 2;
  62. rcar_du_plane_write(rgrp, index, PnMWR, mwr);
  63. /* The Y position is expressed in raster line units and must be doubled
  64. * for 32bpp formats, according to the R8A7790 datasheet. No mention of
  65. * doubling the Y position is found in the R8A7779 datasheet, but the
  66. * rule seems to apply there as well.
  67. *
  68. * Despite not being documented, doubling seem not to be needed when
  69. * operating in interlaced mode.
  70. *
  71. * Similarly, for the second plane, NV12 and NV21 formats seem to
  72. * require a halved Y position value, in both progressive and interlaced
  73. * modes.
  74. */
  75. rcar_du_plane_write(rgrp, index, PnSPXR, src_x);
  76. rcar_du_plane_write(rgrp, index, PnSPYR, src_y *
  77. (!interlaced && state->format->bpp == 32 ? 2 : 1));
  78. gem = drm_fb_cma_get_gem_obj(fb, 0);
  79. rcar_du_plane_write(rgrp, index, PnDSA0R, gem->paddr + fb->offsets[0]);
  80. if (state->format->planes == 2) {
  81. index = (index + 1) % 8;
  82. rcar_du_plane_write(rgrp, index, PnMWR, fb->pitches[0]);
  83. rcar_du_plane_write(rgrp, index, PnSPXR, src_x);
  84. rcar_du_plane_write(rgrp, index, PnSPYR, src_y *
  85. (state->format->bpp == 16 ? 2 : 1) / 2);
  86. gem = drm_fb_cma_get_gem_obj(fb, 1);
  87. rcar_du_plane_write(rgrp, index, PnDSA0R,
  88. gem->paddr + fb->offsets[1]);
  89. }
  90. }
  91. static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane,
  92. unsigned int index)
  93. {
  94. struct rcar_du_plane_state *state =
  95. to_rcar_plane_state(plane->plane.state);
  96. struct rcar_du_group *rgrp = plane->group;
  97. u32 colorkey;
  98. u32 pnmr;
  99. /* The PnALPHAR register controls alpha-blending in 16bpp formats
  100. * (ARGB1555 and XRGB1555).
  101. *
  102. * For ARGB, set the alpha value to 0, and enable alpha-blending when
  103. * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255.
  104. *
  105. * For XRGB, set the alpha value to the plane-wide alpha value and
  106. * enable alpha-blending regardless of the X bit value.
  107. */
  108. if (state->format->fourcc != DRM_FORMAT_XRGB1555)
  109. rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
  110. else
  111. rcar_du_plane_write(rgrp, index, PnALPHAR,
  112. PnALPHAR_ABIT_X | state->alpha);
  113. pnmr = PnMR_BM_MD | state->format->pnmr;
  114. /* Disable color keying when requested. YUV formats have the
  115. * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
  116. * automatically.
  117. */
  118. if ((state->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
  119. pnmr |= PnMR_SPIM_TP_OFF;
  120. /* For packed YUV formats we need to select the U/V order. */
  121. if (state->format->fourcc == DRM_FORMAT_YUYV)
  122. pnmr |= PnMR_YCDF_YUYV;
  123. rcar_du_plane_write(rgrp, index, PnMR, pnmr);
  124. switch (state->format->fourcc) {
  125. case DRM_FORMAT_RGB565:
  126. colorkey = ((state->colorkey & 0xf80000) >> 8)
  127. | ((state->colorkey & 0x00fc00) >> 5)
  128. | ((state->colorkey & 0x0000f8) >> 3);
  129. rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
  130. break;
  131. case DRM_FORMAT_ARGB1555:
  132. case DRM_FORMAT_XRGB1555:
  133. colorkey = ((state->colorkey & 0xf80000) >> 9)
  134. | ((state->colorkey & 0x00f800) >> 6)
  135. | ((state->colorkey & 0x0000f8) >> 3);
  136. rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
  137. break;
  138. case DRM_FORMAT_XRGB8888:
  139. case DRM_FORMAT_ARGB8888:
  140. rcar_du_plane_write(rgrp, index, PnTC3R,
  141. PnTC3R_CODE | (state->colorkey & 0xffffff));
  142. break;
  143. }
  144. }
  145. static void __rcar_du_plane_setup(struct rcar_du_plane *plane,
  146. unsigned int index)
  147. {
  148. struct rcar_du_plane_state *state =
  149. to_rcar_plane_state(plane->plane.state);
  150. struct rcar_du_group *rgrp = plane->group;
  151. u32 ddcr2 = PnDDCR2_CODE;
  152. u32 ddcr4;
  153. /* Data format
  154. *
  155. * The data format is selected by the DDDF field in PnMR and the EDF
  156. * field in DDCR4.
  157. */
  158. ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4);
  159. ddcr4 &= ~PnDDCR4_EDF_MASK;
  160. ddcr4 |= state->format->edf | PnDDCR4_CODE;
  161. rcar_du_plane_setup_mode(plane, index);
  162. if (state->format->planes == 2) {
  163. if (state->hwindex != index) {
  164. if (state->format->fourcc == DRM_FORMAT_NV12 ||
  165. state->format->fourcc == DRM_FORMAT_NV21)
  166. ddcr2 |= PnDDCR2_Y420;
  167. if (state->format->fourcc == DRM_FORMAT_NV21)
  168. ddcr2 |= PnDDCR2_NV21;
  169. ddcr2 |= PnDDCR2_DIVU;
  170. } else {
  171. ddcr2 |= PnDDCR2_DIVY;
  172. }
  173. }
  174. rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
  175. rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
  176. /* Destination position and size */
  177. rcar_du_plane_write(rgrp, index, PnDSXR, plane->plane.state->crtc_w);
  178. rcar_du_plane_write(rgrp, index, PnDSYR, plane->plane.state->crtc_h);
  179. rcar_du_plane_write(rgrp, index, PnDPXR, plane->plane.state->crtc_x);
  180. rcar_du_plane_write(rgrp, index, PnDPYR, plane->plane.state->crtc_y);
  181. /* Wrap-around and blinking, disabled */
  182. rcar_du_plane_write(rgrp, index, PnWASPR, 0);
  183. rcar_du_plane_write(rgrp, index, PnWAMWR, 4095);
  184. rcar_du_plane_write(rgrp, index, PnBTR, 0);
  185. rcar_du_plane_write(rgrp, index, PnMLR, 0);
  186. }
  187. void rcar_du_plane_setup(struct rcar_du_plane *plane)
  188. {
  189. struct rcar_du_plane_state *state =
  190. to_rcar_plane_state(plane->plane.state);
  191. __rcar_du_plane_setup(plane, state->hwindex);
  192. if (state->format->planes == 2)
  193. __rcar_du_plane_setup(plane, (state->hwindex + 1) % 8);
  194. rcar_du_plane_setup_fb(plane);
  195. }
  196. static int rcar_du_plane_atomic_check(struct drm_plane *plane,
  197. struct drm_plane_state *state)
  198. {
  199. struct rcar_du_plane_state *rstate = to_rcar_plane_state(state);
  200. struct rcar_du_plane *rplane = to_rcar_plane(plane);
  201. struct rcar_du_device *rcdu = rplane->group->dev;
  202. if (!state->fb || !state->crtc) {
  203. rstate->format = NULL;
  204. return 0;
  205. }
  206. if (state->src_w >> 16 != state->crtc_w ||
  207. state->src_h >> 16 != state->crtc_h) {
  208. dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
  209. return -EINVAL;
  210. }
  211. rstate->format = rcar_du_format_info(state->fb->pixel_format);
  212. if (rstate->format == NULL) {
  213. dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
  214. state->fb->pixel_format);
  215. return -EINVAL;
  216. }
  217. return 0;
  218. }
  219. static void rcar_du_plane_atomic_update(struct drm_plane *plane,
  220. struct drm_plane_state *old_state)
  221. {
  222. struct rcar_du_plane *rplane = to_rcar_plane(plane);
  223. if (plane->state->crtc)
  224. rcar_du_plane_setup(rplane);
  225. }
  226. static const struct drm_plane_helper_funcs rcar_du_plane_helper_funcs = {
  227. .atomic_check = rcar_du_plane_atomic_check,
  228. .atomic_update = rcar_du_plane_atomic_update,
  229. };
  230. static struct drm_plane_state *
  231. rcar_du_plane_atomic_duplicate_state(struct drm_plane *plane)
  232. {
  233. struct rcar_du_plane_state *state;
  234. struct rcar_du_plane_state *copy;
  235. if (WARN_ON(!plane->state))
  236. return NULL;
  237. state = to_rcar_plane_state(plane->state);
  238. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  239. if (copy == NULL)
  240. return NULL;
  241. __drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
  242. return &copy->state;
  243. }
  244. static void rcar_du_plane_atomic_destroy_state(struct drm_plane *plane,
  245. struct drm_plane_state *state)
  246. {
  247. __drm_atomic_helper_plane_destroy_state(plane, state);
  248. kfree(to_rcar_plane_state(state));
  249. }
  250. static void rcar_du_plane_reset(struct drm_plane *plane)
  251. {
  252. struct rcar_du_plane_state *state;
  253. if (plane->state) {
  254. rcar_du_plane_atomic_destroy_state(plane, plane->state);
  255. plane->state = NULL;
  256. }
  257. state = kzalloc(sizeof(*state), GFP_KERNEL);
  258. if (state == NULL)
  259. return;
  260. state->hwindex = -1;
  261. state->alpha = 255;
  262. state->colorkey = RCAR_DU_COLORKEY_NONE;
  263. state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
  264. plane->state = &state->state;
  265. plane->state->plane = plane;
  266. }
  267. static int rcar_du_plane_atomic_set_property(struct drm_plane *plane,
  268. struct drm_plane_state *state,
  269. struct drm_property *property,
  270. uint64_t val)
  271. {
  272. struct rcar_du_plane_state *rstate = to_rcar_plane_state(state);
  273. struct rcar_du_device *rcdu = to_rcar_plane(plane)->group->dev;
  274. if (property == rcdu->props.alpha)
  275. rstate->alpha = val;
  276. else if (property == rcdu->props.colorkey)
  277. rstate->colorkey = val;
  278. else if (property == rcdu->props.zpos)
  279. rstate->zpos = val;
  280. else
  281. return -EINVAL;
  282. return 0;
  283. }
  284. static int rcar_du_plane_atomic_get_property(struct drm_plane *plane,
  285. const struct drm_plane_state *state, struct drm_property *property,
  286. uint64_t *val)
  287. {
  288. const struct rcar_du_plane_state *rstate =
  289. container_of(state, const struct rcar_du_plane_state, state);
  290. struct rcar_du_device *rcdu = to_rcar_plane(plane)->group->dev;
  291. if (property == rcdu->props.alpha)
  292. *val = rstate->alpha;
  293. else if (property == rcdu->props.colorkey)
  294. *val = rstate->colorkey;
  295. else if (property == rcdu->props.zpos)
  296. *val = rstate->zpos;
  297. else
  298. return -EINVAL;
  299. return 0;
  300. }
  301. static const struct drm_plane_funcs rcar_du_plane_funcs = {
  302. .update_plane = drm_atomic_helper_update_plane,
  303. .disable_plane = drm_atomic_helper_disable_plane,
  304. .reset = rcar_du_plane_reset,
  305. .set_property = drm_atomic_helper_plane_set_property,
  306. .destroy = drm_plane_cleanup,
  307. .atomic_duplicate_state = rcar_du_plane_atomic_duplicate_state,
  308. .atomic_destroy_state = rcar_du_plane_atomic_destroy_state,
  309. .atomic_set_property = rcar_du_plane_atomic_set_property,
  310. .atomic_get_property = rcar_du_plane_atomic_get_property,
  311. };
  312. static const uint32_t formats[] = {
  313. DRM_FORMAT_RGB565,
  314. DRM_FORMAT_ARGB1555,
  315. DRM_FORMAT_XRGB1555,
  316. DRM_FORMAT_XRGB8888,
  317. DRM_FORMAT_ARGB8888,
  318. DRM_FORMAT_UYVY,
  319. DRM_FORMAT_YUYV,
  320. DRM_FORMAT_NV12,
  321. DRM_FORMAT_NV21,
  322. DRM_FORMAT_NV16,
  323. };
  324. int rcar_du_planes_init(struct rcar_du_group *rgrp)
  325. {
  326. struct rcar_du_device *rcdu = rgrp->dev;
  327. unsigned int crtcs;
  328. unsigned int i;
  329. int ret;
  330. /* Create one primary plane per CRTC in this group and seven overlay
  331. * planes.
  332. */
  333. rgrp->num_planes = rgrp->num_crtcs + 7;
  334. crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
  335. for (i = 0; i < rgrp->num_planes; ++i) {
  336. enum drm_plane_type type = i < rgrp->num_crtcs
  337. ? DRM_PLANE_TYPE_PRIMARY
  338. : DRM_PLANE_TYPE_OVERLAY;
  339. struct rcar_du_plane *plane = &rgrp->planes[i];
  340. plane->group = rgrp;
  341. ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
  342. &rcar_du_plane_funcs, formats,
  343. ARRAY_SIZE(formats), type);
  344. if (ret < 0)
  345. return ret;
  346. drm_plane_helper_add(&plane->plane,
  347. &rcar_du_plane_helper_funcs);
  348. if (type == DRM_PLANE_TYPE_PRIMARY)
  349. continue;
  350. drm_object_attach_property(&plane->plane.base,
  351. rcdu->props.alpha, 255);
  352. drm_object_attach_property(&plane->plane.base,
  353. rcdu->props.colorkey,
  354. RCAR_DU_COLORKEY_NONE);
  355. drm_object_attach_property(&plane->plane.base,
  356. rcdu->props.zpos, 1);
  357. }
  358. return 0;
  359. }