armada_overlay.c 15 KB

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