rcar_du_plane.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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_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 "rcar_du_drv.h"
  19. #include "rcar_du_kms.h"
  20. #include "rcar_du_plane.h"
  21. #include "rcar_du_regs.h"
  22. #define RCAR_DU_COLORKEY_NONE (0 << 24)
  23. #define RCAR_DU_COLORKEY_SOURCE (1 << 24)
  24. #define RCAR_DU_COLORKEY_MASK (1 << 24)
  25. struct rcar_du_kms_plane {
  26. struct drm_plane plane;
  27. struct rcar_du_plane *hwplane;
  28. };
  29. static inline struct rcar_du_plane *to_rcar_plane(struct drm_plane *plane)
  30. {
  31. return container_of(plane, struct rcar_du_kms_plane, plane)->hwplane;
  32. }
  33. static u32 rcar_du_plane_read(struct rcar_du_group *rgrp,
  34. unsigned int index, u32 reg)
  35. {
  36. return rcar_du_read(rgrp->dev,
  37. rgrp->mmio_offset + index * PLANE_OFF + reg);
  38. }
  39. static void rcar_du_plane_write(struct rcar_du_group *rgrp,
  40. unsigned int index, u32 reg, u32 data)
  41. {
  42. rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg,
  43. data);
  44. }
  45. int rcar_du_plane_reserve(struct rcar_du_plane *plane,
  46. const struct rcar_du_format_info *format)
  47. {
  48. struct rcar_du_group *rgrp = plane->group;
  49. unsigned int i;
  50. int ret = -EBUSY;
  51. mutex_lock(&rgrp->planes.lock);
  52. for (i = 0; i < ARRAY_SIZE(rgrp->planes.planes); ++i) {
  53. if (!(rgrp->planes.free & (1 << i)))
  54. continue;
  55. if (format->planes == 1 ||
  56. rgrp->planes.free & (1 << ((i + 1) % 8)))
  57. break;
  58. }
  59. if (i == ARRAY_SIZE(rgrp->planes.planes))
  60. goto done;
  61. rgrp->planes.free &= ~(1 << i);
  62. if (format->planes == 2)
  63. rgrp->planes.free &= ~(1 << ((i + 1) % 8));
  64. plane->hwindex = i;
  65. ret = 0;
  66. done:
  67. mutex_unlock(&rgrp->planes.lock);
  68. return ret;
  69. }
  70. void rcar_du_plane_release(struct rcar_du_plane *plane)
  71. {
  72. struct rcar_du_group *rgrp = plane->group;
  73. if (plane->hwindex == -1)
  74. return;
  75. mutex_lock(&rgrp->planes.lock);
  76. rgrp->planes.free |= 1 << plane->hwindex;
  77. if (plane->format->planes == 2)
  78. rgrp->planes.free |= 1 << ((plane->hwindex + 1) % 8);
  79. mutex_unlock(&rgrp->planes.lock);
  80. plane->hwindex = -1;
  81. }
  82. void rcar_du_plane_update_base(struct rcar_du_plane *plane)
  83. {
  84. struct rcar_du_group *rgrp = plane->group;
  85. unsigned int index = plane->hwindex;
  86. bool interlaced;
  87. u32 mwr;
  88. interlaced = plane->crtc->mode.flags & DRM_MODE_FLAG_INTERLACE;
  89. /* Memory pitch (expressed in pixels). Must be doubled for interlaced
  90. * operation with 32bpp formats.
  91. */
  92. if (plane->format->planes == 2)
  93. mwr = plane->pitch;
  94. else
  95. mwr = plane->pitch * 8 / plane->format->bpp;
  96. if (interlaced && plane->format->bpp == 32)
  97. mwr *= 2;
  98. rcar_du_plane_write(rgrp, index, PnMWR, mwr);
  99. /* The Y position is expressed in raster line units and must be doubled
  100. * for 32bpp formats, according to the R8A7790 datasheet. No mention of
  101. * doubling the Y position is found in the R8A7779 datasheet, but the
  102. * rule seems to apply there as well.
  103. *
  104. * Despite not being documented, doubling seem not to be needed when
  105. * operating in interlaced mode.
  106. *
  107. * Similarly, for the second plane, NV12 and NV21 formats seem to
  108. * require a halved Y position value, in both progressive and interlaced
  109. * modes.
  110. */
  111. rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
  112. rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y *
  113. (!interlaced && plane->format->bpp == 32 ? 2 : 1));
  114. rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[0]);
  115. if (plane->format->planes == 2) {
  116. index = (index + 1) % 8;
  117. rcar_du_plane_write(rgrp, index, PnMWR, plane->pitch);
  118. rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
  119. rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y *
  120. (plane->format->bpp == 16 ? 2 : 1) / 2);
  121. rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[1]);
  122. }
  123. }
  124. void rcar_du_plane_compute_base(struct rcar_du_plane *plane,
  125. struct drm_framebuffer *fb)
  126. {
  127. struct drm_gem_cma_object *gem;
  128. plane->pitch = fb->pitches[0];
  129. gem = drm_fb_cma_get_gem_obj(fb, 0);
  130. plane->dma[0] = gem->paddr + fb->offsets[0];
  131. if (plane->format->planes == 2) {
  132. gem = drm_fb_cma_get_gem_obj(fb, 1);
  133. plane->dma[1] = gem->paddr + fb->offsets[1];
  134. }
  135. }
  136. static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane,
  137. unsigned int index)
  138. {
  139. struct rcar_du_group *rgrp = plane->group;
  140. u32 colorkey;
  141. u32 pnmr;
  142. /* The PnALPHAR register controls alpha-blending in 16bpp formats
  143. * (ARGB1555 and XRGB1555).
  144. *
  145. * For ARGB, set the alpha value to 0, and enable alpha-blending when
  146. * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255.
  147. *
  148. * For XRGB, set the alpha value to the plane-wide alpha value and
  149. * enable alpha-blending regardless of the X bit value.
  150. */
  151. if (plane->format->fourcc != DRM_FORMAT_XRGB1555)
  152. rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
  153. else
  154. rcar_du_plane_write(rgrp, index, PnALPHAR,
  155. PnALPHAR_ABIT_X | plane->alpha);
  156. pnmr = PnMR_BM_MD | plane->format->pnmr;
  157. /* Disable color keying when requested. YUV formats have the
  158. * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
  159. * automatically.
  160. */
  161. if ((plane->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
  162. pnmr |= PnMR_SPIM_TP_OFF;
  163. /* For packed YUV formats we need to select the U/V order. */
  164. if (plane->format->fourcc == DRM_FORMAT_YUYV)
  165. pnmr |= PnMR_YCDF_YUYV;
  166. rcar_du_plane_write(rgrp, index, PnMR, pnmr);
  167. switch (plane->format->fourcc) {
  168. case DRM_FORMAT_RGB565:
  169. colorkey = ((plane->colorkey & 0xf80000) >> 8)
  170. | ((plane->colorkey & 0x00fc00) >> 5)
  171. | ((plane->colorkey & 0x0000f8) >> 3);
  172. rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
  173. break;
  174. case DRM_FORMAT_ARGB1555:
  175. case DRM_FORMAT_XRGB1555:
  176. colorkey = ((plane->colorkey & 0xf80000) >> 9)
  177. | ((plane->colorkey & 0x00f800) >> 6)
  178. | ((plane->colorkey & 0x0000f8) >> 3);
  179. rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
  180. break;
  181. case DRM_FORMAT_XRGB8888:
  182. case DRM_FORMAT_ARGB8888:
  183. rcar_du_plane_write(rgrp, index, PnTC3R,
  184. PnTC3R_CODE | (plane->colorkey & 0xffffff));
  185. break;
  186. }
  187. }
  188. static void __rcar_du_plane_setup(struct rcar_du_plane *plane,
  189. unsigned int index)
  190. {
  191. struct rcar_du_group *rgrp = plane->group;
  192. u32 ddcr2 = PnDDCR2_CODE;
  193. u32 ddcr4;
  194. /* Data format
  195. *
  196. * The data format is selected by the DDDF field in PnMR and the EDF
  197. * field in DDCR4.
  198. */
  199. ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4);
  200. ddcr4 &= ~PnDDCR4_EDF_MASK;
  201. ddcr4 |= plane->format->edf | PnDDCR4_CODE;
  202. rcar_du_plane_setup_mode(plane, index);
  203. if (plane->format->planes == 2) {
  204. if (plane->hwindex != index) {
  205. if (plane->format->fourcc == DRM_FORMAT_NV12 ||
  206. plane->format->fourcc == DRM_FORMAT_NV21)
  207. ddcr2 |= PnDDCR2_Y420;
  208. if (plane->format->fourcc == DRM_FORMAT_NV21)
  209. ddcr2 |= PnDDCR2_NV21;
  210. ddcr2 |= PnDDCR2_DIVU;
  211. } else {
  212. ddcr2 |= PnDDCR2_DIVY;
  213. }
  214. }
  215. rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
  216. rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
  217. /* Destination position and size */
  218. rcar_du_plane_write(rgrp, index, PnDSXR, plane->width);
  219. rcar_du_plane_write(rgrp, index, PnDSYR, plane->height);
  220. rcar_du_plane_write(rgrp, index, PnDPXR, plane->dst_x);
  221. rcar_du_plane_write(rgrp, index, PnDPYR, plane->dst_y);
  222. /* Wrap-around and blinking, disabled */
  223. rcar_du_plane_write(rgrp, index, PnWASPR, 0);
  224. rcar_du_plane_write(rgrp, index, PnWAMWR, 4095);
  225. rcar_du_plane_write(rgrp, index, PnBTR, 0);
  226. rcar_du_plane_write(rgrp, index, PnMLR, 0);
  227. }
  228. void rcar_du_plane_setup(struct rcar_du_plane *plane)
  229. {
  230. __rcar_du_plane_setup(plane, plane->hwindex);
  231. if (plane->format->planes == 2)
  232. __rcar_du_plane_setup(plane, (plane->hwindex + 1) % 8);
  233. rcar_du_plane_update_base(plane);
  234. }
  235. static int
  236. rcar_du_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  237. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  238. unsigned int crtc_w, unsigned int crtc_h,
  239. uint32_t src_x, uint32_t src_y,
  240. uint32_t src_w, uint32_t src_h)
  241. {
  242. struct rcar_du_plane *rplane = to_rcar_plane(plane);
  243. struct rcar_du_device *rcdu = rplane->group->dev;
  244. const struct rcar_du_format_info *format;
  245. unsigned int nplanes;
  246. int ret;
  247. format = rcar_du_format_info(fb->pixel_format);
  248. if (format == NULL) {
  249. dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
  250. fb->pixel_format);
  251. return -EINVAL;
  252. }
  253. if (src_w >> 16 != crtc_w || src_h >> 16 != crtc_h) {
  254. dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
  255. return -EINVAL;
  256. }
  257. nplanes = rplane->format ? rplane->format->planes : 0;
  258. /* Reallocate hardware planes if the number of required planes has
  259. * changed.
  260. */
  261. if (format->planes != nplanes) {
  262. rcar_du_plane_release(rplane);
  263. ret = rcar_du_plane_reserve(rplane, format);
  264. if (ret < 0)
  265. return ret;
  266. }
  267. rplane->crtc = crtc;
  268. rplane->format = format;
  269. rplane->src_x = src_x >> 16;
  270. rplane->src_y = src_y >> 16;
  271. rplane->dst_x = crtc_x;
  272. rplane->dst_y = crtc_y;
  273. rplane->width = crtc_w;
  274. rplane->height = crtc_h;
  275. rcar_du_plane_compute_base(rplane, fb);
  276. rcar_du_plane_setup(rplane);
  277. mutex_lock(&rplane->group->planes.lock);
  278. rplane->enabled = true;
  279. rcar_du_crtc_update_planes(rplane->crtc);
  280. mutex_unlock(&rplane->group->planes.lock);
  281. return 0;
  282. }
  283. static int rcar_du_plane_disable(struct drm_plane *plane)
  284. {
  285. struct rcar_du_plane *rplane = to_rcar_plane(plane);
  286. if (!rplane->enabled)
  287. return 0;
  288. mutex_lock(&rplane->group->planes.lock);
  289. rplane->enabled = false;
  290. rcar_du_crtc_update_planes(rplane->crtc);
  291. mutex_unlock(&rplane->group->planes.lock);
  292. rcar_du_plane_release(rplane);
  293. rplane->crtc = NULL;
  294. rplane->format = NULL;
  295. return 0;
  296. }
  297. /* Both the .set_property and the .update_plane operations are called with the
  298. * mode_config lock held. There is this no need to explicitly protect access to
  299. * the alpha and colorkey fields and the mode register.
  300. */
  301. static void rcar_du_plane_set_alpha(struct rcar_du_plane *plane, u32 alpha)
  302. {
  303. if (plane->alpha == alpha)
  304. return;
  305. plane->alpha = alpha;
  306. if (!plane->enabled || plane->format->fourcc != DRM_FORMAT_XRGB1555)
  307. return;
  308. rcar_du_plane_setup_mode(plane, plane->hwindex);
  309. }
  310. static void rcar_du_plane_set_colorkey(struct rcar_du_plane *plane,
  311. u32 colorkey)
  312. {
  313. if (plane->colorkey == colorkey)
  314. return;
  315. plane->colorkey = colorkey;
  316. if (!plane->enabled)
  317. return;
  318. rcar_du_plane_setup_mode(plane, plane->hwindex);
  319. }
  320. static void rcar_du_plane_set_zpos(struct rcar_du_plane *plane,
  321. unsigned int zpos)
  322. {
  323. mutex_lock(&plane->group->planes.lock);
  324. if (plane->zpos == zpos)
  325. goto done;
  326. plane->zpos = zpos;
  327. if (!plane->enabled)
  328. goto done;
  329. rcar_du_crtc_update_planes(plane->crtc);
  330. done:
  331. mutex_unlock(&plane->group->planes.lock);
  332. }
  333. static int rcar_du_plane_set_property(struct drm_plane *plane,
  334. struct drm_property *property,
  335. uint64_t value)
  336. {
  337. struct rcar_du_plane *rplane = to_rcar_plane(plane);
  338. struct rcar_du_group *rgrp = rplane->group;
  339. if (property == rgrp->planes.alpha)
  340. rcar_du_plane_set_alpha(rplane, value);
  341. else if (property == rgrp->planes.colorkey)
  342. rcar_du_plane_set_colorkey(rplane, value);
  343. else if (property == rgrp->planes.zpos)
  344. rcar_du_plane_set_zpos(rplane, value);
  345. else
  346. return -EINVAL;
  347. return 0;
  348. }
  349. static const struct drm_plane_funcs rcar_du_plane_funcs = {
  350. .update_plane = rcar_du_plane_update,
  351. .disable_plane = rcar_du_plane_disable,
  352. .set_property = rcar_du_plane_set_property,
  353. .destroy = drm_plane_cleanup,
  354. };
  355. static const uint32_t formats[] = {
  356. DRM_FORMAT_RGB565,
  357. DRM_FORMAT_ARGB1555,
  358. DRM_FORMAT_XRGB1555,
  359. DRM_FORMAT_XRGB8888,
  360. DRM_FORMAT_ARGB8888,
  361. DRM_FORMAT_UYVY,
  362. DRM_FORMAT_YUYV,
  363. DRM_FORMAT_NV12,
  364. DRM_FORMAT_NV21,
  365. DRM_FORMAT_NV16,
  366. };
  367. int rcar_du_planes_init(struct rcar_du_group *rgrp)
  368. {
  369. struct rcar_du_planes *planes = &rgrp->planes;
  370. struct rcar_du_device *rcdu = rgrp->dev;
  371. unsigned int i;
  372. mutex_init(&planes->lock);
  373. planes->free = 0xff;
  374. planes->alpha =
  375. drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
  376. if (planes->alpha == NULL)
  377. return -ENOMEM;
  378. /* The color key is expressed as an RGB888 triplet stored in a 32-bit
  379. * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
  380. * or enable source color keying (1).
  381. */
  382. planes->colorkey =
  383. drm_property_create_range(rcdu->ddev, 0, "colorkey",
  384. 0, 0x01ffffff);
  385. if (planes->colorkey == NULL)
  386. return -ENOMEM;
  387. planes->zpos =
  388. drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7);
  389. if (planes->zpos == NULL)
  390. return -ENOMEM;
  391. for (i = 0; i < ARRAY_SIZE(planes->planes); ++i) {
  392. struct rcar_du_plane *plane = &planes->planes[i];
  393. plane->group = rgrp;
  394. plane->hwindex = -1;
  395. plane->alpha = 255;
  396. plane->colorkey = RCAR_DU_COLORKEY_NONE;
  397. plane->zpos = 0;
  398. }
  399. return 0;
  400. }
  401. int rcar_du_planes_register(struct rcar_du_group *rgrp)
  402. {
  403. struct rcar_du_planes *planes = &rgrp->planes;
  404. struct rcar_du_device *rcdu = rgrp->dev;
  405. unsigned int crtcs;
  406. unsigned int i;
  407. int ret;
  408. crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
  409. for (i = 0; i < RCAR_DU_NUM_KMS_PLANES; ++i) {
  410. struct rcar_du_kms_plane *plane;
  411. plane = devm_kzalloc(rcdu->dev, sizeof(*plane), GFP_KERNEL);
  412. if (plane == NULL)
  413. return -ENOMEM;
  414. plane->hwplane = &planes->planes[i + 2];
  415. plane->hwplane->zpos = 1;
  416. ret = drm_plane_init(rcdu->ddev, &plane->plane, crtcs,
  417. &rcar_du_plane_funcs, formats,
  418. ARRAY_SIZE(formats), false);
  419. if (ret < 0)
  420. return ret;
  421. drm_object_attach_property(&plane->plane.base,
  422. planes->alpha, 255);
  423. drm_object_attach_property(&plane->plane.base,
  424. planes->colorkey,
  425. RCAR_DU_COLORKEY_NONE);
  426. drm_object_attach_property(&plane->plane.base,
  427. planes->zpos, 1);
  428. }
  429. return 0;
  430. }