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