armada_overlay.c 15 KB

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