rcar_du_plane.c 13 KB

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