armada_overlay.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright (C) 2012 Russell King
  3. * Rewritten from the dovefb driver, and Armada510 manuals.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <drm/drmP.h>
  10. #include <drm/drm_plane_helper.h>
  11. #include "armada_crtc.h"
  12. #include "armada_drm.h"
  13. #include "armada_fb.h"
  14. #include "armada_gem.h"
  15. #include "armada_hw.h"
  16. #include <drm/armada_drm.h>
  17. #include "armada_ioctlP.h"
  18. #include "armada_trace.h"
  19. struct armada_ovl_plane_properties {
  20. uint32_t colorkey_yr;
  21. uint32_t colorkey_ug;
  22. uint32_t colorkey_vb;
  23. #define K2R(val) (((val) >> 0) & 0xff)
  24. #define K2G(val) (((val) >> 8) & 0xff)
  25. #define K2B(val) (((val) >> 16) & 0xff)
  26. int16_t brightness;
  27. uint16_t contrast;
  28. uint16_t saturation;
  29. uint32_t colorkey_mode;
  30. uint32_t colorkey_enable;
  31. };
  32. struct armada_ovl_plane {
  33. struct armada_plane base;
  34. struct drm_framebuffer *old_fb;
  35. struct {
  36. struct armada_plane_work work;
  37. struct armada_regs regs[13];
  38. } vbl;
  39. struct armada_ovl_plane_properties prop;
  40. };
  41. #define drm_to_armada_ovl_plane(p) \
  42. container_of(p, struct armada_ovl_plane, base.base)
  43. static void
  44. armada_ovl_update_attr(struct armada_ovl_plane_properties *prop,
  45. struct armada_crtc *dcrtc)
  46. {
  47. writel_relaxed(prop->colorkey_yr, dcrtc->base + LCD_SPU_COLORKEY_Y);
  48. writel_relaxed(prop->colorkey_ug, dcrtc->base + LCD_SPU_COLORKEY_U);
  49. writel_relaxed(prop->colorkey_vb, dcrtc->base + LCD_SPU_COLORKEY_V);
  50. writel_relaxed(prop->brightness << 16 | prop->contrast,
  51. dcrtc->base + LCD_SPU_CONTRAST);
  52. /* Docs say 15:0, but it seems to actually be 31:16 on Armada 510 */
  53. writel_relaxed(prop->saturation << 16,
  54. dcrtc->base + LCD_SPU_SATURATION);
  55. writel_relaxed(0x00002000, dcrtc->base + LCD_SPU_CBSH_HUE);
  56. spin_lock_irq(&dcrtc->irq_lock);
  57. armada_updatel(prop->colorkey_mode,
  58. CFG_CKMODE_MASK | CFG_ALPHAM_MASK | CFG_ALPHA_MASK,
  59. dcrtc->base + LCD_SPU_DMA_CTRL1);
  60. if (dcrtc->variant->has_spu_adv_reg)
  61. armada_updatel(prop->colorkey_enable,
  62. ADV_GRACOLORKEY | ADV_VIDCOLORKEY,
  63. dcrtc->base + LCD_SPU_ADV_REG);
  64. spin_unlock_irq(&dcrtc->irq_lock);
  65. }
  66. static void armada_ovl_retire_fb(struct armada_ovl_plane *dplane,
  67. struct drm_framebuffer *fb)
  68. {
  69. struct drm_framebuffer *old_fb;
  70. old_fb = xchg(&dplane->old_fb, fb);
  71. if (old_fb)
  72. armada_drm_queue_unref_work(dplane->base.base.dev, old_fb);
  73. }
  74. /* === Plane support === */
  75. static void armada_ovl_plane_work(struct armada_crtc *dcrtc,
  76. struct armada_plane *plane, struct armada_plane_work *work)
  77. {
  78. struct armada_ovl_plane *dplane = container_of(plane, struct armada_ovl_plane, base);
  79. trace_armada_ovl_plane_work(&dcrtc->crtc, &plane->base);
  80. armada_drm_crtc_update_regs(dcrtc, dplane->vbl.regs);
  81. armada_ovl_retire_fb(dplane, NULL);
  82. }
  83. static int
  84. armada_ovl_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  85. struct drm_framebuffer *fb,
  86. int crtc_x, int crtc_y, unsigned crtc_w, unsigned crtc_h,
  87. uint32_t src_x, uint32_t src_y, uint32_t src_w, uint32_t src_h,
  88. struct drm_modeset_acquire_ctx *ctx)
  89. {
  90. struct armada_ovl_plane *dplane = drm_to_armada_ovl_plane(plane);
  91. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  92. struct drm_rect src = {
  93. .x1 = src_x,
  94. .y1 = src_y,
  95. .x2 = src_x + src_w,
  96. .y2 = src_y + src_h,
  97. };
  98. struct drm_rect dest = {
  99. .x1 = crtc_x,
  100. .y1 = crtc_y,
  101. .x2 = crtc_x + crtc_w,
  102. .y2 = crtc_y + crtc_h,
  103. };
  104. const struct drm_rect clip = {
  105. .x2 = crtc->mode.hdisplay,
  106. .y2 = crtc->mode.vdisplay,
  107. };
  108. uint32_t val, ctrl0;
  109. unsigned idx = 0;
  110. bool visible;
  111. int ret;
  112. trace_armada_ovl_plane_update(plane, crtc, fb,
  113. crtc_x, crtc_y, crtc_w, crtc_h,
  114. src_x, src_y, src_w, src_h);
  115. ret = drm_plane_helper_check_update(plane, crtc, fb, &src, &dest, &clip,
  116. DRM_MODE_ROTATE_0,
  117. 0, INT_MAX, true, false, &visible);
  118. if (ret)
  119. return ret;
  120. ctrl0 = CFG_DMA_FMT(drm_fb_to_armada_fb(fb)->fmt) |
  121. CFG_DMA_MOD(drm_fb_to_armada_fb(fb)->mod) |
  122. CFG_CBSH_ENA | CFG_DMA_HSMOOTH | CFG_DMA_ENA;
  123. /* Does the position/size result in nothing to display? */
  124. if (!visible)
  125. ctrl0 &= ~CFG_DMA_ENA;
  126. if (!dcrtc->plane) {
  127. dcrtc->plane = plane;
  128. armada_ovl_update_attr(&dplane->prop, dcrtc);
  129. }
  130. /* FIXME: overlay on an interlaced display */
  131. /* Just updating the position/size? */
  132. if (plane->fb == fb && dplane->base.state.ctrl0 == ctrl0) {
  133. val = (drm_rect_height(&src) & 0xffff0000) |
  134. drm_rect_width(&src) >> 16;
  135. dplane->base.state.src_hw = val;
  136. writel_relaxed(val, dcrtc->base + LCD_SPU_DMA_HPXL_VLN);
  137. val = drm_rect_height(&dest) << 16 | drm_rect_width(&dest);
  138. dplane->base.state.dst_hw = val;
  139. writel_relaxed(val, dcrtc->base + LCD_SPU_DZM_HPXL_VLN);
  140. val = dest.y1 << 16 | dest.x1;
  141. dplane->base.state.dst_yx = val;
  142. writel_relaxed(val, dcrtc->base + LCD_SPU_DMA_OVSA_HPXL_VLN);
  143. return 0;
  144. } else if (~dplane->base.state.ctrl0 & ctrl0 & CFG_DMA_ENA) {
  145. /* Power up the Y/U/V FIFOs on ENA 0->1 transitions */
  146. armada_updatel(0, CFG_PDWN16x66 | CFG_PDWN32x66,
  147. dcrtc->base + LCD_SPU_SRAM_PARA1);
  148. }
  149. if (armada_drm_plane_work_wait(&dplane->base, HZ / 25) == 0)
  150. armada_drm_plane_work_cancel(dcrtc, &dplane->base);
  151. if (plane->fb != fb) {
  152. u32 addrs[3], pixel_format;
  153. int num_planes, hsub;
  154. /*
  155. * Take a reference on the new framebuffer - we want to
  156. * hold on to it while the hardware is displaying it.
  157. */
  158. drm_framebuffer_reference(fb);
  159. if (plane->fb)
  160. armada_ovl_retire_fb(dplane, plane->fb);
  161. src_y = src.y1 >> 16;
  162. src_x = src.x1 >> 16;
  163. armada_drm_plane_calc_addrs(addrs, fb, src_x, src_y);
  164. pixel_format = fb->format->format;
  165. hsub = drm_format_horz_chroma_subsampling(pixel_format);
  166. num_planes = fb->format->num_planes;
  167. /*
  168. * Annoyingly, shifting a YUYV-format image by one pixel
  169. * causes the U/V planes to toggle. Toggle the UV swap.
  170. * (Unfortunately, this causes momentary colour flickering.)
  171. */
  172. if (src_x & (hsub - 1) && num_planes == 1)
  173. ctrl0 ^= CFG_DMA_MOD(CFG_SWAPUV);
  174. armada_reg_queue_set(dplane->vbl.regs, idx, addrs[0],
  175. LCD_SPU_DMA_START_ADDR_Y0);
  176. armada_reg_queue_set(dplane->vbl.regs, idx, addrs[1],
  177. LCD_SPU_DMA_START_ADDR_U0);
  178. armada_reg_queue_set(dplane->vbl.regs, idx, addrs[2],
  179. LCD_SPU_DMA_START_ADDR_V0);
  180. armada_reg_queue_set(dplane->vbl.regs, idx, addrs[0],
  181. LCD_SPU_DMA_START_ADDR_Y1);
  182. armada_reg_queue_set(dplane->vbl.regs, idx, addrs[1],
  183. LCD_SPU_DMA_START_ADDR_U1);
  184. armada_reg_queue_set(dplane->vbl.regs, idx, addrs[2],
  185. LCD_SPU_DMA_START_ADDR_V1);
  186. val = fb->pitches[0] << 16 | fb->pitches[0];
  187. armada_reg_queue_set(dplane->vbl.regs, idx, val,
  188. LCD_SPU_DMA_PITCH_YC);
  189. val = fb->pitches[1] << 16 | fb->pitches[2];
  190. armada_reg_queue_set(dplane->vbl.regs, idx, val,
  191. LCD_SPU_DMA_PITCH_UV);
  192. }
  193. val = (drm_rect_height(&src) & 0xffff0000) | drm_rect_width(&src) >> 16;
  194. if (dplane->base.state.src_hw != val) {
  195. dplane->base.state.src_hw = val;
  196. armada_reg_queue_set(dplane->vbl.regs, idx, val,
  197. LCD_SPU_DMA_HPXL_VLN);
  198. }
  199. val = drm_rect_height(&dest) << 16 | drm_rect_width(&dest);
  200. if (dplane->base.state.dst_hw != val) {
  201. dplane->base.state.dst_hw = val;
  202. armada_reg_queue_set(dplane->vbl.regs, idx, val,
  203. LCD_SPU_DZM_HPXL_VLN);
  204. }
  205. val = dest.y1 << 16 | dest.x1;
  206. if (dplane->base.state.dst_yx != val) {
  207. dplane->base.state.dst_yx = val;
  208. armada_reg_queue_set(dplane->vbl.regs, idx, val,
  209. LCD_SPU_DMA_OVSA_HPXL_VLN);
  210. }
  211. if (dplane->base.state.ctrl0 != ctrl0) {
  212. dplane->base.state.ctrl0 = ctrl0;
  213. armada_reg_queue_mod(dplane->vbl.regs, idx, ctrl0,
  214. CFG_CBSH_ENA | CFG_DMAFORMAT | CFG_DMA_FTOGGLE |
  215. CFG_DMA_HSMOOTH | CFG_DMA_TSTMODE |
  216. CFG_DMA_MOD(CFG_SWAPRB | CFG_SWAPUV | CFG_SWAPYU |
  217. CFG_YUV2RGB) | CFG_DMA_ENA,
  218. LCD_SPU_DMA_CTRL0);
  219. }
  220. if (idx) {
  221. armada_reg_queue_end(dplane->vbl.regs, idx);
  222. armada_drm_plane_work_queue(dcrtc, &dplane->base,
  223. &dplane->vbl.work);
  224. }
  225. return 0;
  226. }
  227. static int armada_ovl_plane_disable(struct drm_plane *plane,
  228. struct drm_modeset_acquire_ctx *ctx)
  229. {
  230. struct armada_ovl_plane *dplane = drm_to_armada_ovl_plane(plane);
  231. struct drm_framebuffer *fb;
  232. struct armada_crtc *dcrtc;
  233. if (!dplane->base.base.crtc)
  234. return 0;
  235. dcrtc = drm_to_armada_crtc(dplane->base.base.crtc);
  236. armada_drm_plane_work_cancel(dcrtc, &dplane->base);
  237. armada_drm_crtc_plane_disable(dcrtc, plane);
  238. dcrtc->plane = NULL;
  239. dplane->base.state.ctrl0 = 0;
  240. fb = xchg(&dplane->old_fb, NULL);
  241. if (fb)
  242. drm_framebuffer_unreference(fb);
  243. return 0;
  244. }
  245. static void armada_ovl_plane_destroy(struct drm_plane *plane)
  246. {
  247. struct armada_ovl_plane *dplane = drm_to_armada_ovl_plane(plane);
  248. drm_plane_cleanup(plane);
  249. kfree(dplane);
  250. }
  251. static int armada_ovl_plane_set_property(struct drm_plane *plane,
  252. struct drm_property *property, uint64_t val)
  253. {
  254. struct armada_private *priv = plane->dev->dev_private;
  255. struct armada_ovl_plane *dplane = drm_to_armada_ovl_plane(plane);
  256. bool update_attr = false;
  257. if (property == priv->colorkey_prop) {
  258. #define CCC(v) ((v) << 24 | (v) << 16 | (v) << 8)
  259. dplane->prop.colorkey_yr = CCC(K2R(val));
  260. dplane->prop.colorkey_ug = CCC(K2G(val));
  261. dplane->prop.colorkey_vb = CCC(K2B(val));
  262. #undef CCC
  263. update_attr = true;
  264. } else if (property == priv->colorkey_min_prop) {
  265. dplane->prop.colorkey_yr &= ~0x00ff0000;
  266. dplane->prop.colorkey_yr |= K2R(val) << 16;
  267. dplane->prop.colorkey_ug &= ~0x00ff0000;
  268. dplane->prop.colorkey_ug |= K2G(val) << 16;
  269. dplane->prop.colorkey_vb &= ~0x00ff0000;
  270. dplane->prop.colorkey_vb |= K2B(val) << 16;
  271. update_attr = true;
  272. } else if (property == priv->colorkey_max_prop) {
  273. dplane->prop.colorkey_yr &= ~0xff000000;
  274. dplane->prop.colorkey_yr |= K2R(val) << 24;
  275. dplane->prop.colorkey_ug &= ~0xff000000;
  276. dplane->prop.colorkey_ug |= K2G(val) << 24;
  277. dplane->prop.colorkey_vb &= ~0xff000000;
  278. dplane->prop.colorkey_vb |= K2B(val) << 24;
  279. update_attr = true;
  280. } else if (property == priv->colorkey_val_prop) {
  281. dplane->prop.colorkey_yr &= ~0x0000ff00;
  282. dplane->prop.colorkey_yr |= K2R(val) << 8;
  283. dplane->prop.colorkey_ug &= ~0x0000ff00;
  284. dplane->prop.colorkey_ug |= K2G(val) << 8;
  285. dplane->prop.colorkey_vb &= ~0x0000ff00;
  286. dplane->prop.colorkey_vb |= K2B(val) << 8;
  287. update_attr = true;
  288. } else if (property == priv->colorkey_alpha_prop) {
  289. dplane->prop.colorkey_yr &= ~0x000000ff;
  290. dplane->prop.colorkey_yr |= K2R(val);
  291. dplane->prop.colorkey_ug &= ~0x000000ff;
  292. dplane->prop.colorkey_ug |= K2G(val);
  293. dplane->prop.colorkey_vb &= ~0x000000ff;
  294. dplane->prop.colorkey_vb |= K2B(val);
  295. update_attr = true;
  296. } else if (property == priv->colorkey_mode_prop) {
  297. if (val == CKMODE_DISABLE) {
  298. dplane->prop.colorkey_mode =
  299. CFG_CKMODE(CKMODE_DISABLE) |
  300. CFG_ALPHAM_CFG | CFG_ALPHA(255);
  301. dplane->prop.colorkey_enable = 0;
  302. } else {
  303. dplane->prop.colorkey_mode =
  304. CFG_CKMODE(val) |
  305. CFG_ALPHAM_GRA | CFG_ALPHA(0);
  306. dplane->prop.colorkey_enable = ADV_GRACOLORKEY;
  307. }
  308. update_attr = true;
  309. } else if (property == priv->brightness_prop) {
  310. dplane->prop.brightness = val - 256;
  311. update_attr = true;
  312. } else if (property == priv->contrast_prop) {
  313. dplane->prop.contrast = val;
  314. update_attr = true;
  315. } else if (property == priv->saturation_prop) {
  316. dplane->prop.saturation = val;
  317. update_attr = true;
  318. }
  319. if (update_attr && dplane->base.base.crtc)
  320. armada_ovl_update_attr(&dplane->prop,
  321. drm_to_armada_crtc(dplane->base.base.crtc));
  322. return 0;
  323. }
  324. static const struct drm_plane_funcs armada_ovl_plane_funcs = {
  325. .update_plane = armada_ovl_plane_update,
  326. .disable_plane = armada_ovl_plane_disable,
  327. .destroy = armada_ovl_plane_destroy,
  328. .set_property = armada_ovl_plane_set_property,
  329. };
  330. static const uint32_t armada_ovl_formats[] = {
  331. DRM_FORMAT_UYVY,
  332. DRM_FORMAT_YUYV,
  333. DRM_FORMAT_YUV420,
  334. DRM_FORMAT_YVU420,
  335. DRM_FORMAT_YUV422,
  336. DRM_FORMAT_YVU422,
  337. DRM_FORMAT_VYUY,
  338. DRM_FORMAT_YVYU,
  339. DRM_FORMAT_ARGB8888,
  340. DRM_FORMAT_ABGR8888,
  341. DRM_FORMAT_XRGB8888,
  342. DRM_FORMAT_XBGR8888,
  343. DRM_FORMAT_RGB888,
  344. DRM_FORMAT_BGR888,
  345. DRM_FORMAT_ARGB1555,
  346. DRM_FORMAT_ABGR1555,
  347. DRM_FORMAT_RGB565,
  348. DRM_FORMAT_BGR565,
  349. };
  350. static const struct drm_prop_enum_list armada_drm_colorkey_enum_list[] = {
  351. { CKMODE_DISABLE, "disabled" },
  352. { CKMODE_Y, "Y component" },
  353. { CKMODE_U, "U component" },
  354. { CKMODE_V, "V component" },
  355. { CKMODE_RGB, "RGB" },
  356. { CKMODE_R, "R component" },
  357. { CKMODE_G, "G component" },
  358. { CKMODE_B, "B component" },
  359. };
  360. static int armada_overlay_create_properties(struct drm_device *dev)
  361. {
  362. struct armada_private *priv = dev->dev_private;
  363. if (priv->colorkey_prop)
  364. return 0;
  365. priv->colorkey_prop = drm_property_create_range(dev, 0,
  366. "colorkey", 0, 0xffffff);
  367. priv->colorkey_min_prop = drm_property_create_range(dev, 0,
  368. "colorkey_min", 0, 0xffffff);
  369. priv->colorkey_max_prop = drm_property_create_range(dev, 0,
  370. "colorkey_max", 0, 0xffffff);
  371. priv->colorkey_val_prop = drm_property_create_range(dev, 0,
  372. "colorkey_val", 0, 0xffffff);
  373. priv->colorkey_alpha_prop = drm_property_create_range(dev, 0,
  374. "colorkey_alpha", 0, 0xffffff);
  375. priv->colorkey_mode_prop = drm_property_create_enum(dev, 0,
  376. "colorkey_mode",
  377. armada_drm_colorkey_enum_list,
  378. ARRAY_SIZE(armada_drm_colorkey_enum_list));
  379. priv->brightness_prop = drm_property_create_range(dev, 0,
  380. "brightness", 0, 256 + 255);
  381. priv->contrast_prop = drm_property_create_range(dev, 0,
  382. "contrast", 0, 0x7fff);
  383. priv->saturation_prop = drm_property_create_range(dev, 0,
  384. "saturation", 0, 0x7fff);
  385. if (!priv->colorkey_prop)
  386. return -ENOMEM;
  387. return 0;
  388. }
  389. int armada_overlay_plane_create(struct drm_device *dev, unsigned long crtcs)
  390. {
  391. struct armada_private *priv = dev->dev_private;
  392. struct drm_mode_object *mobj;
  393. struct armada_ovl_plane *dplane;
  394. int ret;
  395. ret = armada_overlay_create_properties(dev);
  396. if (ret)
  397. return ret;
  398. dplane = kzalloc(sizeof(*dplane), GFP_KERNEL);
  399. if (!dplane)
  400. return -ENOMEM;
  401. ret = armada_drm_plane_init(&dplane->base);
  402. if (ret) {
  403. kfree(dplane);
  404. return ret;
  405. }
  406. dplane->vbl.work.fn = armada_ovl_plane_work;
  407. ret = drm_universal_plane_init(dev, &dplane->base.base, crtcs,
  408. &armada_ovl_plane_funcs,
  409. armada_ovl_formats,
  410. ARRAY_SIZE(armada_ovl_formats),
  411. NULL,
  412. DRM_PLANE_TYPE_OVERLAY, NULL);
  413. if (ret) {
  414. kfree(dplane);
  415. return ret;
  416. }
  417. dplane->prop.colorkey_yr = 0xfefefe00;
  418. dplane->prop.colorkey_ug = 0x01010100;
  419. dplane->prop.colorkey_vb = 0x01010100;
  420. dplane->prop.colorkey_mode = CFG_CKMODE(CKMODE_RGB) |
  421. CFG_ALPHAM_GRA | CFG_ALPHA(0);
  422. dplane->prop.colorkey_enable = ADV_GRACOLORKEY;
  423. dplane->prop.brightness = 0;
  424. dplane->prop.contrast = 0x4000;
  425. dplane->prop.saturation = 0x4000;
  426. mobj = &dplane->base.base.base;
  427. drm_object_attach_property(mobj, priv->colorkey_prop,
  428. 0x0101fe);
  429. drm_object_attach_property(mobj, priv->colorkey_min_prop,
  430. 0x0101fe);
  431. drm_object_attach_property(mobj, priv->colorkey_max_prop,
  432. 0x0101fe);
  433. drm_object_attach_property(mobj, priv->colorkey_val_prop,
  434. 0x0101fe);
  435. drm_object_attach_property(mobj, priv->colorkey_alpha_prop,
  436. 0x000000);
  437. drm_object_attach_property(mobj, priv->colorkey_mode_prop,
  438. CKMODE_RGB);
  439. drm_object_attach_property(mobj, priv->brightness_prop, 256);
  440. drm_object_attach_property(mobj, priv->contrast_prop,
  441. dplane->prop.contrast);
  442. drm_object_attach_property(mobj, priv->saturation_prop,
  443. dplane->prop.saturation);
  444. return 0;
  445. }