armada_overlay.c 15 KB

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