rcar_du_plane.c 13 KB

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