drm: rcar-du: Implement universal plane support
[pandora-kernel.git] / drivers / gpu / drm / rcar-du / rcar_du_plane.c
1 /*
2  * rcar_du_plane.c  --  R-Car Display Unit Planes
3  *
4  * Copyright (C) 2013-2014 Renesas Electronics Corporation
5  *
6  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <drm/drmP.h>
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_fb_cma_helper.h>
18 #include <drm/drm_gem_cma_helper.h>
19
20 #include "rcar_du_drv.h"
21 #include "rcar_du_kms.h"
22 #include "rcar_du_plane.h"
23 #include "rcar_du_regs.h"
24
25 #define RCAR_DU_COLORKEY_NONE           (0 << 24)
26 #define RCAR_DU_COLORKEY_SOURCE         (1 << 24)
27 #define RCAR_DU_COLORKEY_MASK           (1 << 24)
28
29 static inline struct rcar_du_plane *to_rcar_plane(struct drm_plane *plane)
30 {
31         return container_of(plane, struct rcar_du_plane, plane);
32 }
33
34 static u32 rcar_du_plane_read(struct rcar_du_group *rgrp,
35                               unsigned int index, u32 reg)
36 {
37         return rcar_du_read(rgrp->dev,
38                             rgrp->mmio_offset + index * PLANE_OFF + reg);
39 }
40
41 static void rcar_du_plane_write(struct rcar_du_group *rgrp,
42                                 unsigned int index, u32 reg, u32 data)
43 {
44         rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg,
45                       data);
46 }
47
48 int rcar_du_plane_reserve(struct rcar_du_plane *plane,
49                           const struct rcar_du_format_info *format)
50 {
51         struct rcar_du_group *rgrp = plane->group;
52         unsigned int i;
53         int ret = -EBUSY;
54
55         mutex_lock(&rgrp->planes.lock);
56
57         for (i = 0; i < ARRAY_SIZE(rgrp->planes.planes); ++i) {
58                 if (!(rgrp->planes.free & (1 << i)))
59                         continue;
60
61                 if (format->planes == 1 ||
62                     rgrp->planes.free & (1 << ((i + 1) % 8)))
63                         break;
64         }
65
66         if (i == ARRAY_SIZE(rgrp->planes.planes))
67                 goto done;
68
69         rgrp->planes.free &= ~(1 << i);
70         if (format->planes == 2)
71                 rgrp->planes.free &= ~(1 << ((i + 1) % 8));
72
73         plane->hwindex = i;
74
75         ret = 0;
76
77 done:
78         mutex_unlock(&rgrp->planes.lock);
79         return ret;
80 }
81
82 void rcar_du_plane_release(struct rcar_du_plane *plane)
83 {
84         struct rcar_du_group *rgrp = plane->group;
85
86         if (plane->hwindex == -1)
87                 return;
88
89         mutex_lock(&rgrp->planes.lock);
90         rgrp->planes.free |= 1 << plane->hwindex;
91         if (plane->format->planes == 2)
92                 rgrp->planes.free |= 1 << ((plane->hwindex + 1) % 8);
93         mutex_unlock(&rgrp->planes.lock);
94
95         plane->hwindex = -1;
96 }
97
98 void rcar_du_plane_update_base(struct rcar_du_plane *plane)
99 {
100         struct rcar_du_group *rgrp = plane->group;
101         unsigned int index = plane->hwindex;
102         bool interlaced;
103         u32 mwr;
104
105         interlaced = plane->crtc->mode.flags & DRM_MODE_FLAG_INTERLACE;
106
107         /* Memory pitch (expressed in pixels). Must be doubled for interlaced
108          * operation with 32bpp formats.
109          */
110         if (plane->format->planes == 2)
111                 mwr = plane->pitch;
112         else
113                 mwr = plane->pitch * 8 / plane->format->bpp;
114
115         if (interlaced && plane->format->bpp == 32)
116                 mwr *= 2;
117
118         rcar_du_plane_write(rgrp, index, PnMWR, mwr);
119
120         /* The Y position is expressed in raster line units and must be doubled
121          * for 32bpp formats, according to the R8A7790 datasheet. No mention of
122          * doubling the Y position is found in the R8A7779 datasheet, but the
123          * rule seems to apply there as well.
124          *
125          * Despite not being documented, doubling seem not to be needed when
126          * operating in interlaced mode.
127          *
128          * Similarly, for the second plane, NV12 and NV21 formats seem to
129          * require a halved Y position value, in both progressive and interlaced
130          * modes.
131          */
132         rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
133         rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y *
134                             (!interlaced && plane->format->bpp == 32 ? 2 : 1));
135         rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[0]);
136
137         if (plane->format->planes == 2) {
138                 index = (index + 1) % 8;
139
140                 rcar_du_plane_write(rgrp, index, PnMWR, plane->pitch);
141
142                 rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
143                 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y *
144                                     (plane->format->bpp == 16 ? 2 : 1) / 2);
145                 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[1]);
146         }
147 }
148
149 void rcar_du_plane_compute_base(struct rcar_du_plane *plane,
150                                 struct drm_framebuffer *fb)
151 {
152         struct drm_gem_cma_object *gem;
153
154         plane->pitch = fb->pitches[0];
155
156         gem = drm_fb_cma_get_gem_obj(fb, 0);
157         plane->dma[0] = gem->paddr + fb->offsets[0];
158
159         if (plane->format->planes == 2) {
160                 gem = drm_fb_cma_get_gem_obj(fb, 1);
161                 plane->dma[1] = gem->paddr + fb->offsets[1];
162         }
163 }
164
165 static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane,
166                                      unsigned int index)
167 {
168         struct rcar_du_group *rgrp = plane->group;
169         u32 colorkey;
170         u32 pnmr;
171
172         /* The PnALPHAR register controls alpha-blending in 16bpp formats
173          * (ARGB1555 and XRGB1555).
174          *
175          * For ARGB, set the alpha value to 0, and enable alpha-blending when
176          * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255.
177          *
178          * For XRGB, set the alpha value to the plane-wide alpha value and
179          * enable alpha-blending regardless of the X bit value.
180          */
181         if (plane->format->fourcc != DRM_FORMAT_XRGB1555)
182                 rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
183         else
184                 rcar_du_plane_write(rgrp, index, PnALPHAR,
185                                     PnALPHAR_ABIT_X | plane->alpha);
186
187         pnmr = PnMR_BM_MD | plane->format->pnmr;
188
189         /* Disable color keying when requested. YUV formats have the
190          * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
191          * automatically.
192          */
193         if ((plane->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
194                 pnmr |= PnMR_SPIM_TP_OFF;
195
196         /* For packed YUV formats we need to select the U/V order. */
197         if (plane->format->fourcc == DRM_FORMAT_YUYV)
198                 pnmr |= PnMR_YCDF_YUYV;
199
200         rcar_du_plane_write(rgrp, index, PnMR, pnmr);
201
202         switch (plane->format->fourcc) {
203         case DRM_FORMAT_RGB565:
204                 colorkey = ((plane->colorkey & 0xf80000) >> 8)
205                          | ((plane->colorkey & 0x00fc00) >> 5)
206                          | ((plane->colorkey & 0x0000f8) >> 3);
207                 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
208                 break;
209
210         case DRM_FORMAT_ARGB1555:
211         case DRM_FORMAT_XRGB1555:
212                 colorkey = ((plane->colorkey & 0xf80000) >> 9)
213                          | ((plane->colorkey & 0x00f800) >> 6)
214                          | ((plane->colorkey & 0x0000f8) >> 3);
215                 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
216                 break;
217
218         case DRM_FORMAT_XRGB8888:
219         case DRM_FORMAT_ARGB8888:
220                 rcar_du_plane_write(rgrp, index, PnTC3R,
221                                     PnTC3R_CODE | (plane->colorkey & 0xffffff));
222                 break;
223         }
224 }
225
226 static void __rcar_du_plane_setup(struct rcar_du_plane *plane,
227                                   unsigned int index)
228 {
229         struct rcar_du_group *rgrp = plane->group;
230         u32 ddcr2 = PnDDCR2_CODE;
231         u32 ddcr4;
232
233         /* Data format
234          *
235          * The data format is selected by the DDDF field in PnMR and the EDF
236          * field in DDCR4.
237          */
238         ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4);
239         ddcr4 &= ~PnDDCR4_EDF_MASK;
240         ddcr4 |= plane->format->edf | PnDDCR4_CODE;
241
242         rcar_du_plane_setup_mode(plane, index);
243
244         if (plane->format->planes == 2) {
245                 if (plane->hwindex != index) {
246                         if (plane->format->fourcc == DRM_FORMAT_NV12 ||
247                             plane->format->fourcc == DRM_FORMAT_NV21)
248                                 ddcr2 |= PnDDCR2_Y420;
249
250                         if (plane->format->fourcc == DRM_FORMAT_NV21)
251                                 ddcr2 |= PnDDCR2_NV21;
252
253                         ddcr2 |= PnDDCR2_DIVU;
254                 } else {
255                         ddcr2 |= PnDDCR2_DIVY;
256                 }
257         }
258
259         rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
260         rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
261
262         /* Destination position and size */
263         rcar_du_plane_write(rgrp, index, PnDSXR, plane->width);
264         rcar_du_plane_write(rgrp, index, PnDSYR, plane->height);
265         rcar_du_plane_write(rgrp, index, PnDPXR, plane->dst_x);
266         rcar_du_plane_write(rgrp, index, PnDPYR, plane->dst_y);
267
268         /* Wrap-around and blinking, disabled */
269         rcar_du_plane_write(rgrp, index, PnWASPR, 0);
270         rcar_du_plane_write(rgrp, index, PnWAMWR, 4095);
271         rcar_du_plane_write(rgrp, index, PnBTR, 0);
272         rcar_du_plane_write(rgrp, index, PnMLR, 0);
273 }
274
275 void rcar_du_plane_setup(struct rcar_du_plane *plane)
276 {
277         __rcar_du_plane_setup(plane, plane->hwindex);
278         if (plane->format->planes == 2)
279                 __rcar_du_plane_setup(plane, (plane->hwindex + 1) % 8);
280
281         rcar_du_plane_update_base(plane);
282 }
283
284 static int
285 rcar_du_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
286                        struct drm_framebuffer *fb, int crtc_x, int crtc_y,
287                        unsigned int crtc_w, unsigned int crtc_h,
288                        uint32_t src_x, uint32_t src_y,
289                        uint32_t src_w, uint32_t src_h)
290 {
291         struct rcar_du_plane *rplane = to_rcar_plane(plane);
292         struct rcar_du_device *rcdu = rplane->group->dev;
293         const struct rcar_du_format_info *format;
294         unsigned int nplanes;
295         int ret;
296
297         if (plane->type != DRM_PLANE_TYPE_OVERLAY)
298                 return -EINVAL;
299
300         format = rcar_du_format_info(fb->pixel_format);
301         if (format == NULL) {
302                 dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
303                         fb->pixel_format);
304                 return -EINVAL;
305         }
306
307         if (src_w >> 16 != crtc_w || src_h >> 16 != crtc_h) {
308                 dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
309                 return -EINVAL;
310         }
311
312         nplanes = rplane->format ? rplane->format->planes : 0;
313
314         /* Reallocate hardware planes if the number of required planes has
315          * changed.
316          */
317         if (format->planes != nplanes) {
318                 rcar_du_plane_release(rplane);
319                 ret = rcar_du_plane_reserve(rplane, format);
320                 if (ret < 0)
321                         return ret;
322         }
323
324         rplane->crtc = crtc;
325         rplane->format = format;
326
327         rplane->src_x = src_x >> 16;
328         rplane->src_y = src_y >> 16;
329         rplane->dst_x = crtc_x;
330         rplane->dst_y = crtc_y;
331         rplane->width = crtc_w;
332         rplane->height = crtc_h;
333
334         rcar_du_plane_compute_base(rplane, fb);
335         rcar_du_plane_setup(rplane);
336
337         mutex_lock(&rplane->group->planes.lock);
338         rplane->enabled = true;
339         rcar_du_crtc_update_planes(rplane->crtc);
340         mutex_unlock(&rplane->group->planes.lock);
341
342         return 0;
343 }
344
345 static int rcar_du_plane_disable(struct drm_plane *plane)
346 {
347         struct rcar_du_plane *rplane = to_rcar_plane(plane);
348
349         if (plane->type != DRM_PLANE_TYPE_OVERLAY)
350                 return -EINVAL;
351
352         if (!rplane->enabled)
353                 return 0;
354
355         mutex_lock(&rplane->group->planes.lock);
356         rplane->enabled = false;
357         rcar_du_crtc_update_planes(rplane->crtc);
358         mutex_unlock(&rplane->group->planes.lock);
359
360         rcar_du_plane_release(rplane);
361
362         rplane->crtc = NULL;
363         rplane->format = NULL;
364
365         return 0;
366 }
367
368 /* Both the .set_property and the .update_plane operations are called with the
369  * mode_config lock held. There is this no need to explicitly protect access to
370  * the alpha and colorkey fields and the mode register.
371  */
372 static void rcar_du_plane_set_alpha(struct rcar_du_plane *plane, u32 alpha)
373 {
374         if (plane->alpha == alpha)
375                 return;
376
377         plane->alpha = alpha;
378         if (!plane->enabled || plane->format->fourcc != DRM_FORMAT_XRGB1555)
379                 return;
380
381         rcar_du_plane_setup_mode(plane, plane->hwindex);
382 }
383
384 static void rcar_du_plane_set_colorkey(struct rcar_du_plane *plane,
385                                        u32 colorkey)
386 {
387         if (plane->colorkey == colorkey)
388                 return;
389
390         plane->colorkey = colorkey;
391         if (!plane->enabled)
392                 return;
393
394         rcar_du_plane_setup_mode(plane, plane->hwindex);
395 }
396
397 static void rcar_du_plane_set_zpos(struct rcar_du_plane *plane,
398                                    unsigned int zpos)
399 {
400         mutex_lock(&plane->group->planes.lock);
401         if (plane->zpos == zpos)
402                 goto done;
403
404         plane->zpos = zpos;
405         if (!plane->enabled)
406                 goto done;
407
408         rcar_du_crtc_update_planes(plane->crtc);
409
410 done:
411         mutex_unlock(&plane->group->planes.lock);
412 }
413
414 static int rcar_du_plane_set_property(struct drm_plane *plane,
415                                       struct drm_property *property,
416                                       uint64_t value)
417 {
418         struct rcar_du_plane *rplane = to_rcar_plane(plane);
419         struct rcar_du_group *rgrp = rplane->group;
420
421         if (property == rgrp->planes.alpha)
422                 rcar_du_plane_set_alpha(rplane, value);
423         else if (property == rgrp->planes.colorkey)
424                 rcar_du_plane_set_colorkey(rplane, value);
425         else if (property == rgrp->planes.zpos)
426                 rcar_du_plane_set_zpos(rplane, value);
427         else
428                 return -EINVAL;
429
430         return 0;
431 }
432
433 static const struct drm_plane_funcs rcar_du_plane_funcs = {
434         .update_plane = rcar_du_plane_update,
435         .disable_plane = rcar_du_plane_disable,
436         .set_property = rcar_du_plane_set_property,
437         .destroy = drm_plane_cleanup,
438 };
439
440 static const uint32_t formats[] = {
441         DRM_FORMAT_RGB565,
442         DRM_FORMAT_ARGB1555,
443         DRM_FORMAT_XRGB1555,
444         DRM_FORMAT_XRGB8888,
445         DRM_FORMAT_ARGB8888,
446         DRM_FORMAT_UYVY,
447         DRM_FORMAT_YUYV,
448         DRM_FORMAT_NV12,
449         DRM_FORMAT_NV21,
450         DRM_FORMAT_NV16,
451 };
452
453 int rcar_du_planes_init(struct rcar_du_group *rgrp)
454 {
455         struct rcar_du_planes *planes = &rgrp->planes;
456         struct rcar_du_device *rcdu = rgrp->dev;
457         unsigned int num_planes;
458         unsigned int num_crtcs;
459         unsigned int crtcs;
460         unsigned int i;
461         int ret;
462
463         mutex_init(&planes->lock);
464         planes->free = 0xff;
465
466         planes->alpha =
467                 drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
468         if (planes->alpha == NULL)
469                 return -ENOMEM;
470
471         /* The color key is expressed as an RGB888 triplet stored in a 32-bit
472          * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
473          * or enable source color keying (1).
474          */
475         planes->colorkey =
476                 drm_property_create_range(rcdu->ddev, 0, "colorkey",
477                                           0, 0x01ffffff);
478         if (planes->colorkey == NULL)
479                 return -ENOMEM;
480
481         planes->zpos =
482                 drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7);
483         if (planes->zpos == NULL)
484                 return -ENOMEM;
485
486          /* Create one primary plane per in this group CRTC and seven overlay
487           * planes.
488           */
489         num_crtcs = min(rcdu->num_crtcs - 2 * rgrp->index, 2U);
490         num_planes = num_crtcs + 7;
491
492         crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
493
494         for (i = 0; i < num_planes; ++i) {
495                 enum drm_plane_type type = i < num_crtcs
496                                          ? DRM_PLANE_TYPE_PRIMARY
497                                          : DRM_PLANE_TYPE_OVERLAY;
498                 struct rcar_du_plane *plane = &planes->planes[i];
499
500                 plane->group = rgrp;
501                 plane->hwindex = -1;
502                 plane->alpha = 255;
503                 plane->colorkey = RCAR_DU_COLORKEY_NONE;
504                 plane->zpos = type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
505
506                 ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
507                                                &rcar_du_plane_funcs, formats,
508                                                ARRAY_SIZE(formats), type);
509                 if (ret < 0)
510                         return ret;
511
512                 if (type == DRM_PLANE_TYPE_PRIMARY)
513                         continue;
514
515                 drm_object_attach_property(&plane->plane.base,
516                                            planes->alpha, 255);
517                 drm_object_attach_property(&plane->plane.base,
518                                            planes->colorkey,
519                                            RCAR_DU_COLORKEY_NONE);
520                 drm_object_attach_property(&plane->plane.base,
521                                            planes->zpos, 1);
522         }
523
524         return 0;
525 }